mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-18 09:22:00 +00:00
支持规则型指标:单元格条件确定性求值与 derived 标签
This commit is contained in:
@@ -25,7 +25,8 @@ from collections.abc import Iterable
|
||||
from typing import Any
|
||||
|
||||
#: 指标的语义字段:只有这几样算定义,运行时值(value、rendered、inputs……)不参与哈希
|
||||
SEMANTIC_FIELDS = ("id", "name", "expression", "unit", "decimals", "format", "kind", "definition", "params", "external")
|
||||
SEMANTIC_FIELDS = ("id", "name", "expression", "unit", "decimals", "format", "kind", "definition", "params", "external",
|
||||
"rule")
|
||||
|
||||
|
||||
def _blank(value: Any) -> bool:
|
||||
|
||||
@@ -105,6 +105,39 @@ async def run_metrics(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
if problem:
|
||||
errors.append(f"指标「{metric_id}」:{problem}")
|
||||
continue
|
||||
if str(definition.get("kind") or "formula") == "rule":
|
||||
rule, problem = _rule_of(definition)
|
||||
if problem:
|
||||
errors.append(f"指标「{metric_id}」:{problem}")
|
||||
continue
|
||||
inputs_cfg = {str(name): str(path) for name, path in (rule.get("inputs") or {}).items()}
|
||||
values_by_name = {name: _value_of(path, tctx) for name, path in inputs_cfg.items()}
|
||||
missing = [name for name, value in values_by_name.items() if value is None]
|
||||
if missing and on_missing != "null":
|
||||
errors.append(f"指标「{metric_id}」:" + _missing_reason([inputs_cfg[name] for name in missing]))
|
||||
continue
|
||||
value: Any = None
|
||||
if not missing:
|
||||
value = rule.get("then", 1) if _rule_matches(values_by_name, rule) else rule.get("else", 0)
|
||||
decimals = display["decimals"]
|
||||
value = round_metric(value, decimals)
|
||||
inputs = [_input_of(path, values_by_name[name], state, ctx, tctx) for name, path in inputs_cfg.items()]
|
||||
metrics.append({
|
||||
"id": metric_id,
|
||||
"name": definition.get("name") or metric_id,
|
||||
"unit": definition.get("unit") or "",
|
||||
"value": value,
|
||||
"expression": "",
|
||||
"decimals": decimals,
|
||||
"format": display["format"],
|
||||
"rendered": _rendered(value, definition, display),
|
||||
"inputs": inputs,
|
||||
"substituted": _rule_text(rule),
|
||||
"recompute_ok": True,
|
||||
"status": "ok" if value is not None else "missing_input",
|
||||
"rule": rule,
|
||||
})
|
||||
continue
|
||||
if str(definition.get("kind") or "formula") == "external":
|
||||
value, external, problem = _external_of(definition)
|
||||
if problem:
|
||||
@@ -468,6 +501,61 @@ def _external_of(definition: dict[str, Any]) -> tuple[Any, dict[str, Any], str |
|
||||
return value, {"source": source, "as_of": as_of.isoformat(), "stale_after_days": days, "stale": stale}, None
|
||||
|
||||
|
||||
def _rule_of(definition: dict[str, Any]) -> tuple[dict[str, Any], str | None]:
|
||||
"""规则型指标:{inputs: {名字: 取值路径}, all: [{input, op, value|values}], then, else}。"""
|
||||
rule = definition.get("rule")
|
||||
if not isinstance(rule, dict):
|
||||
return {}, "规则型指标需要 rule 对象(inputs / all / then / else)"
|
||||
inputs = rule.get("inputs")
|
||||
if not isinstance(inputs, dict) or not inputs:
|
||||
return {}, "规则需要 inputs:输入名 → 取值路径"
|
||||
if any(not isinstance(name, str) or not str(name).strip() or not isinstance(path, str) or not path.strip()
|
||||
for name, path in inputs.items()):
|
||||
return {}, "规则的每个输入都要有名字和取值路径"
|
||||
conditions = rule.get("all")
|
||||
if not isinstance(conditions, list) or not conditions:
|
||||
return {}, "规则需要 all:至少一条条件"
|
||||
for cond in conditions:
|
||||
if not isinstance(cond, dict) or cond.get("input") not in inputs:
|
||||
return {}, "规则的条件要指向一个已声明的输入"
|
||||
if cond.get("op") not in ("eq", "ne", "gt", "gte", "lt", "lte", "in", "not_in"):
|
||||
return {}, "规则条件的运算符不认识"
|
||||
if cond.get("op") in ("in", "not_in") and not isinstance(cond.get("values"), list):
|
||||
return {}, "in / not_in 需要 values 列表"
|
||||
if cond.get("op") not in ("in", "not_in") and "value" not in cond:
|
||||
return {}, "规则条件需要 value"
|
||||
return rule, None
|
||||
|
||||
|
||||
def _rule_matches(values: dict[str, Any], rule: dict[str, Any]) -> bool:
|
||||
"""所有条件都成立才为真;类型对不上(字符串和数比较)按不成立,不抛。"""
|
||||
for cond in rule.get("all") or []:
|
||||
a = values.get(str(cond.get("input")))
|
||||
op = cond.get("op")
|
||||
if op in ("in", "not_in"):
|
||||
found = a in (cond.get("values") or [])
|
||||
if found != (op == "in"):
|
||||
return False
|
||||
continue
|
||||
b = cond.get("value")
|
||||
try:
|
||||
ok = {"eq": a == b, "ne": a != b, "gt": a > b, "gte": a >= b, "lt": a < b, "lte": a <= b}.get(op, False)
|
||||
except TypeError:
|
||||
ok = False
|
||||
if not ok:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _rule_text(rule: dict[str, Any]) -> str:
|
||||
"""规则的一句话写法,给指标清单和证据面板看。"""
|
||||
parts = []
|
||||
for cond in rule.get("all") or []:
|
||||
right = cond.get("values") if cond.get("op") in ("in", "not_in") else cond.get("value")
|
||||
parts.append(f"{cond.get('input')} {cond.get('op')} {right}")
|
||||
return " 且 ".join(parts)
|
||||
|
||||
|
||||
def _display_of(definition: dict[str, Any]) -> tuple[dict[str, Any], str | None]:
|
||||
"""指标的显示格式:{decimals, format, bad_decimals},以及写错时的一句话。
|
||||
|
||||
|
||||
@@ -1088,9 +1088,11 @@ def _check_challenge(node: GraphNode, spec: GraphSpec, result: ValidationResult)
|
||||
|
||||
#: 指标定义的类型。现在只有公式(表达式)这一种在运行时能算;规则、评估、外部常量还没实现,
|
||||
#: 先在校验里挡住,免得写进图里以后被当成能算的指标
|
||||
METRIC_KINDS = ("formula", "external")
|
||||
METRIC_KINDS = ("formula", "external", "rule")
|
||||
#: 定义卡参数的标量类型(和入口表单的字段类型同一套子集)
|
||||
METRIC_PARAM_TYPES = ("string", "number", "integer", "boolean")
|
||||
#: 规则条件能用的比较
|
||||
RULE_OPS = ("eq", "ne", "gt", "gte", "lt", "lte", "in", "not_in")
|
||||
|
||||
|
||||
def metric_definition_issues(node: GraphNode, metric: Any, index: int) -> list[ValidationIssue]:
|
||||
@@ -1133,6 +1135,41 @@ def metric_definition_issues(node: GraphNode, metric: Any, index: int) -> list[V
|
||||
if days is not None and (isinstance(days, bool) or not isinstance(days, int) or days < 0):
|
||||
add("external.stale_after_days 需要是非负整数", f"{field}.external.stale_after_days",
|
||||
code="metrics.external_invalid")
|
||||
elif kind == "rule":
|
||||
rule = metric.get("rule")
|
||||
if not isinstance(rule, dict):
|
||||
add("规则型指标需要 rule 对象(inputs / all / then / else)", f"{field}.rule",
|
||||
code="metrics.rule_invalid")
|
||||
else:
|
||||
inputs = rule.get("inputs")
|
||||
if not isinstance(inputs, dict) or not inputs:
|
||||
add("规则需要 inputs:输入名 → 取值路径(例如 cell(nodes.fetch, 0, 'status'))",
|
||||
f"{field}.rule.inputs", code="metrics.rule_invalid")
|
||||
inputs = {}
|
||||
else:
|
||||
for name, path in inputs.items():
|
||||
if not isinstance(name, str) or not name.strip() or not isinstance(path, str) \
|
||||
or not path.strip():
|
||||
add("规则的每个输入都要有名字和取值路径", f"{field}.rule.inputs", code="metrics.rule_invalid")
|
||||
conditions = rule.get("all")
|
||||
if not isinstance(conditions, list) or not conditions:
|
||||
add("规则需要 all:至少一条条件", f"{field}.rule.all", code="metrics.rule_invalid")
|
||||
conditions = []
|
||||
for j, cond in enumerate(conditions):
|
||||
key = f"{field}.rule.all[{j}]"
|
||||
if not isinstance(cond, dict) or cond.get("input") not in inputs:
|
||||
add("条件要指向一个已声明的输入", key, code="metrics.rule_invalid")
|
||||
continue
|
||||
if cond.get("op") not in RULE_OPS:
|
||||
add(f"条件运算符只能是 {'、'.join(RULE_OPS)}", f"{key}.op", code="metrics.rule_invalid")
|
||||
if cond.get("op") in ("in", "not_in"):
|
||||
if not isinstance(cond.get("values"), list):
|
||||
add("in / not_in 需要 values 列表", f"{key}.values", code="metrics.rule_invalid")
|
||||
elif "value" not in cond:
|
||||
add("条件需要 value", f"{key}.value", code="metrics.rule_invalid")
|
||||
for key in ("then", "else"):
|
||||
if key in rule and (isinstance(rule[key], bool) or not isinstance(rule[key], (int, float))):
|
||||
add(f"规则的 {key} 需要是数", f"{field}.rule.{key}", code="metrics.rule_invalid")
|
||||
elif not metric.get("expression"):
|
||||
add(f"指标定义缺少表达式:{metric.get('id') or '(空)'}", code="metrics.expression_missing")
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ from app.look.calibers import card_inputs
|
||||
from app.tools.datasource import QUERY_PREFIX
|
||||
|
||||
#: 指标只写口径卡配置里真有的这几个键
|
||||
METRIC_KEYS = ("id", "name", "unit", "decimals", "format", "kind", "definition", "params", "external")
|
||||
METRIC_KEYS = ("id", "name", "unit", "decimals", "format", "kind", "definition", "params", "external", "rule")
|
||||
|
||||
|
||||
async def list_calibers(session: AsyncSession, *, workflow_id: str | None = None) -> list[dict[str, Any]]:
|
||||
|
||||
@@ -34,7 +34,7 @@ from typing import Any
|
||||
from app.analysis.contract import LEDGER_KIND
|
||||
|
||||
#: 标签的四种、认法的两种
|
||||
KINDS = ("caliber", "external", "related", "reasoning", "unknown")
|
||||
KINDS = ("caliber", "external", "derived", "related", "reasoning", "unknown")
|
||||
HOWS = ("independent", "composed")
|
||||
#: 界面上标签要写明的两句:比对在封存之后做,不在封存范围内;事后补比的是按现在的口径回看
|
||||
POST_SEAL_NOTE = "封存后比对,不在封存范围内"
|
||||
@@ -308,6 +308,11 @@ def metric_label(card: Mapping[str, Any] | None, metric_id: Any, queries: Mappin
|
||||
**({"definition_hash": card.get("definition_hash")} if card.get("definition_hash") else {})}
|
||||
inputs = [i for i in metric.get("inputs") or [] if isinstance(i, dict)]
|
||||
from_queries = [i for i in inputs if i.get("artifact") and i.get("via") in _SNAPSHOT_VIAS]
|
||||
if metric.get("rule") is not None:
|
||||
# 规则型指标:数由规则从输入算出,不是某条查询与口径等价;输入仍然逐格可追溯
|
||||
return {"kind": "derived", "rule": metric.get("rule"),
|
||||
"queries": [str(i["artifact"]) for i in from_queries],
|
||||
**({"definition_hash": card.get("definition_hash")} if card.get("definition_hash") else {})}
|
||||
if not from_queries:
|
||||
return {"kind": "reasoning"}
|
||||
rows = [queries.get(str(i["artifact"])) for i in from_queries]
|
||||
|
||||
@@ -88,7 +88,7 @@ def test_analysis_numbers_get_no_caliber_label_and_are_not_counted():
|
||||
labelled = {s["id"]: s.get("ref") for s in iter_segments(doc) if s["id"] in labels}
|
||||
assert list(labelled.values()) == ["v:Q1.r0.gmv"]
|
||||
whole = ledger_labels.whole_of(labels, {})
|
||||
assert whole["counts"] == {"caliber": 0, "external": 0, "related": 0, "reasoning": 1, "unknown": 0}
|
||||
assert whole["counts"] == {"caliber": 0, "external": 0, "derived": 0, "related": 0, "reasoning": 1, "unknown": 0}
|
||||
assert ledger_labels.referenced_columns([doc], lambda _: None) == {q: {"gmv"}}
|
||||
|
||||
|
||||
|
||||
@@ -273,8 +273,8 @@ async def test_last_weeks_sales_labels_only_the_sales_number_as_from_the_caliber
|
||||
assert cols["销售额"]["binding"] == {"start": "2026-10-05", "end": "2026-10-11"}
|
||||
assert {c: cols[c]["kind"] for c in COLUMNS[1:]} == {c: "related" for c in COLUMNS[1:]}
|
||||
whole = content["whole"]
|
||||
assert whole["equivalent"] is False and whole["counts"] == {"caliber": 1, "external": 0, "related": 3,
|
||||
"reasoning": 0, "unknown": 0}
|
||||
assert whole["equivalent"] is False and whole["counts"] == {"caliber": 1, "external": 0, "derived": 0,
|
||||
"related": 3, "reasoning": 0, "unknown": 0}
|
||||
# 工件 meta:逐格证明了的口径也记上(召回按旧版本反查只读它)
|
||||
from sqlalchemy import select
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ def test_a_formula_metric_can_carry_a_definition_and_params():
|
||||
|
||||
|
||||
def test_an_unimplemented_kind_is_refused():
|
||||
errors = _errors(_graph(kind="rule"))
|
||||
errors = _errors(_graph(kind="assessment"))
|
||||
assert any(i.code == "metrics.kind_unsupported" and "还没有实现" in i.message for i in errors)
|
||||
|
||||
|
||||
@@ -92,3 +92,59 @@ def test_an_external_metric_is_labelled_external_not_reasoning():
|
||||
label = metric_label(card, "target", {}, {})
|
||||
assert label == {"kind": "external", "source": "预算", "as_of": "2026-01-01", "stale": True,
|
||||
"definition_hash": "abc"}
|
||||
|
||||
|
||||
def test_a_rule_metric_needs_inputs_conditions_and_a_known_operator():
|
||||
bad = _errors(_graph(kind="rule", rule={"inputs": {}, "all": [{"input": "x", "op": "like", "value": 1}]}))
|
||||
assert [i.code for i in bad].count("metrics.rule_invalid") >= 2
|
||||
good = _graph(kind="rule", rule={
|
||||
"inputs": {"status": "cell(nodes.fetch, 0, 'status')"},
|
||||
"all": [{"input": "status", "op": "eq", "value": "paid"}],
|
||||
})
|
||||
assert _errors(good) == []
|
||||
|
||||
|
||||
async def test_a_rule_metric_evaluates_cells_deterministically(tmp_path):
|
||||
from app.api.evidence import _sealed, _sealed_cards
|
||||
from app.engine.runner import run_manager
|
||||
|
||||
await run_manager.setup()
|
||||
try:
|
||||
await S.fresh_source(tmp_path / "rule.db")
|
||||
graph = S.chain(
|
||||
S.node("start", "input"),
|
||||
S.node("fetch", "tool", tool=S.tool(),
|
||||
args={"sql": "SELECT status, amount FROM orders WHERE id = 1"}),
|
||||
S.node("card", "metrics", caliber="大额判断", caliber_version="v1", metrics=[{
|
||||
"id": "big", "name": "大额已支付", "unit": "1", "kind": "rule",
|
||||
"rule": {"inputs": {"status": "cell(nodes.fetch, 0, 'status')",
|
||||
"amount": "cell(nodes.fetch, 0, 'amount')"},
|
||||
"all": [{"input": "status", "op": "eq", "value": "paid"},
|
||||
{"input": "amount", "op": "gte", "value": 50}],
|
||||
"then": 1, "else": 0},
|
||||
}]),
|
||||
S.node("out", "output", fields=[{"name": "v", "value": "{{ nodes.card.metrics[0].value }}"}]))
|
||||
started = await run_manager.start(graph=graph, input_payload={})
|
||||
row = await S.wait(started.id)
|
||||
assert row.status == "succeeded", row.error
|
||||
assert row.output["v"] == "1"
|
||||
sealed = await _sealed(row.id)
|
||||
[card] = [c for _, _, c in _sealed_cards(sealed)]
|
||||
metric = card["metrics"][0]
|
||||
assert metric["rule"]["then"] == 1 and metric["value"] == 1
|
||||
assert [i["path"] for i in metric["inputs"]] == ["cell(nodes.fetch, 0, 'status')",
|
||||
"cell(nodes.fetch, 0, 'amount')"]
|
||||
finally:
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
def test_a_rule_metric_is_labelled_derived_with_its_input_queries():
|
||||
from app.ledger.labels import metric_label
|
||||
|
||||
card = {"definition_hash": "abc", "metrics": [{
|
||||
"id": "big", "rule": {"all": [{"input": "status", "op": "eq", "value": "paid"}]},
|
||||
"inputs": [{"artifact": "q1", "via": "tool_cell", "path": "cell(nodes.fetch, 0, 'status')"}],
|
||||
}]}
|
||||
label = metric_label(card, "big", {}, {})
|
||||
assert label["kind"] == "derived" and label["queries"] == ["q1"]
|
||||
assert label["definition_hash"] == "abc" and label["rule"]["all"][0]["op"] == "eq"
|
||||
|
||||
@@ -358,6 +358,15 @@ export function LedgerPart({ runId, runClass, report, segId, segText, segLedger,
|
||||
<p className="text-2xs text-faint">不来自查询,没有等价证明;正式出具前请核对来源和有效期。</p>
|
||||
</div>
|
||||
)}
|
||||
{label.kind === 'derived' && (
|
||||
<div className="space-y-1" data-ledger-derived="">
|
||||
<p className="font-medium">按规则派生</p>
|
||||
<p className="text-2xs text-dim">
|
||||
数由规则从查询结果算出,不是某条查询与口径等价;输入仍然逐格可追溯。
|
||||
</p>
|
||||
{label.rule?.all?.length ? <p className="text-2xs text-faint">条件:{label.rule.all.length} 条</p> : null}
|
||||
</div>
|
||||
)}
|
||||
{label.kind === 'related' && (
|
||||
<div className="space-y-2">
|
||||
<p className="text-2xs text-dim">{LEDGER_UI.step.relatedSub}{label.reason ? `:${label.reason}` : ''}</p>
|
||||
|
||||
@@ -53,11 +53,12 @@ export interface LedgerRelated {
|
||||
* - unknown:没比对(reason 写为什么)
|
||||
*/
|
||||
export interface LedgerLabel {
|
||||
kind: 'caliber' | 'external' | 'related' | 'reasoning' | 'unknown'
|
||||
kind: 'caliber' | 'external' | 'derived' | 'related' | 'reasoning' | 'unknown'
|
||||
how?: 'independent' | 'composed'
|
||||
source?: string
|
||||
as_of?: string
|
||||
stale?: boolean
|
||||
rule?: { inputs?: Record<string, string>; all?: unknown[]; then?: number; else?: number }
|
||||
caliber?: string
|
||||
workflow_id?: string
|
||||
version?: number
|
||||
|
||||
Reference in New Issue
Block a user