Files
AgentLab/backend/tests/test_whatif_selection.py
T
ThearchyHelios 09237a4c99 数字推演复核修正(第 1 轮,后端):排除行时说不准的指标不再报「无变化」,快照读不出来时不给值,遮罩按用到的快照在线程里算
- 排除了行,指标却有输入对不上任何快照(老运行没存完整值的容器、加工过的结果、按位置从整份结果里取的一格、由查询结果
  算出来的数):以前一律报「无变化」、报告引用处报「不受影响」,或者拿原值算出一个不对应任何一份数据的数。现在回
  unsupported 并写明是哪个输入说不准;写它的节点在那份快照交回之前就跑完的影响不到它,照常不算(按事件序号比先后)。
  卡上没记全输入的指标,有任何改动都标 unsupported
- 查询快照文件不在、内容与哈希不一致时,落在它上面的 cell()、agent 字段不给值、不能改、不给代入式,用到它的指标推演
  不了;遮罩照样按台账记的数据源算,不再因为快照坏了就失效
- 遮罩改成用到哪份快照才算哪份:读快照放进重算的工作线程,查库交回事件循环。以前每次请求都在事件循环里把这次运行的
  全部快照读一遍、复验一遍哈希
- 输入带 row_span(实际用到的快照行);新增 GET …/evidence/whatif/rows,按页取封存范围内一份快照的行(遮罩列写
  「已遮罩」,只读)。界面据此给整份结果勾行、只让用到的行可勾
- 不能改、也没有行可勾的输入(对不上快照的列表、文本)列进「不支持推演的输入」并写明原因
- 下游口径卡顺着画布的边找,经中间节点用到卡上值的也标;改动碰到了指标、它却推演不了时也标「可能受影响」
- 超大整数不再让接口 500:新值超出可计算范围回 422,算出浮点放不下的整数按「不是有限的数值」说明;非法值的说明不露出
  None、单引号这些 Python 写法
- 补测试并逐个做过变异:封存核对没过、事后加的遮罩、合并查询的遮罩、指标集被改(409)、两份报告、循环里先算卡后算
  产出、手写事件核对取哪次执行和边界、重算的几处保护、勾行接口。取整和口径卡逐个对拍(含二进制半中间的数、负小数位):
  metrics.py 只许加 value_artifact,取整没法抽成共用函数,先用对拍守着
2026-10-05 05:43:17 +08:00

88 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""数字推演取哪一份:同一张口径卡执行过几次时推演哪一次、那次执行之前各节点的产出和跑完的时刻、快照什么时候交回。
照 api/evidence.py 的 _Sealed 手写事件(虚构的工件 id),不起运行、不碰库:真跑的循环图里报告总是引用最后一次执行,
节点也常常不会在口径卡之后再跑,这几条边界只有手写的事件才拿得到。
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
import pytest
from app.api.coded import CodedHTTPException
from app.api.evidence import _Sealed
from app.api.whatif import _card_of, _finished_before, _outputs_before, _returned
FILL1, FILL2, A1, A2, OUT1, OUT2, Q1 = ("f1" * 32, "f2" * 32, "a1" * 32, "a2" * 32, "c1" * 32, "c2" * 32, "d1" * 32)
@dataclass
class Report:
node_id: str
doc: dict[str, Any] | None = field(default_factory=dict)
def sealed(events: list[tuple[int, str, str | None, dict[str, Any]]]) -> _Sealed:
return _Sealed(run_id="r1", graph={"nodes": [{"id": "card", "type": "metrics"}]},
seal={"sealed": True, "ok": True, "manifest_seq": 99, "legacy": False}, events=events)
#: 循环两轮:fill → card,fill → card。第一轮的卡读到 FILL1,第二轮读到 FILL2
LOOP = [
(1, "node.started", "fill", {}),
(2, "tool.end", "fill", {"query_artifact": Q1}),
(3, "node.finished", "fill", {"artifact": FILL1}),
(4, "node.started", "card", {}),
(5, "node.finished", "card", {"artifact": OUT1, "evidence": [{"kind": "metric_set", "artifact": A1}]}),
(6, "node.started", "fill", {}),
(7, "node.finished", "fill", {"artifact": FILL2, "evidence": [{"kind": "query", "artifact": Q1}]}),
(8, "node.started", "card", {}),
(9, "node.finished", "card", {"artifact": OUT2, "evidence": [{"kind": "metric_set", "artifact": A2}]}),
]
CARDS = [("card", A1, {"metrics": [{"id": "s", "value": 1}]}), ("card", A2, {"metrics": [{"id": "s", "value": 2}]})]
def cites(*artifacts: str, node: str = "write") -> Report:
return Report(node, {"catalog": {f"m:{i}": {"kind": "metric", "node_id": "card", "artifact": a}
for i, a in enumerate(artifacts)}})
def test_the_execution_the_report_cites_is_chosen_over_the_last_one():
artifact, card, started = _card_of(sealed(LOOP), CARDS, "card", [cites(A1)], None)
assert (artifact, card["metrics"][0]["value"], started) == (A1, 1, 4)
def test_without_a_citation_the_last_execution_is_used():
assert _card_of(sealed(LOOP), CARDS, "card", [cites()], None)[0] == A2
assert _card_of(sealed(LOOP), CARDS, "card", [], None)[0] == A2
def test_a_chosen_report_decides_which_execution_is_used():
"""两份报告各引用一次执行:指定了 report 就按那一份,不指定按任意一份引用的(取后一次)。"""
reports = [cites(A1, node="weekly"), cites(A2, node="daily")]
assert _card_of(sealed(LOOP), CARDS, "card", reports, "weekly")[0] == A1
assert _card_of(sealed(LOOP), CARDS, "card", reports, "daily")[0] == A2
assert _card_of(sealed(LOOP), CARDS, "card", reports, None)[0] == A2
def test_a_card_whose_content_does_not_verify_is_refused():
with pytest.raises(CodedHTTPException) as caught:
_card_of(sealed(LOOP), CARDS[1:], "card", [cites(A1)], None)
assert caught.value.status_code == 409
def test_node_outputs_and_finish_times_stop_at_the_chosen_execution():
"""第一轮的卡(第 4 条开始)读到的是 FILL1:第二轮 fill 的产出在它之后,不算。"""
events = sealed(LOOP)
assert _outputs_before(events, 4) == {"fill": FILL1}
assert _finished_before(events, 4) == {"fill": 3}
assert _outputs_before(events, 8) == {"fill": FILL2, "card": OUT1}
assert _finished_before(events, 8) == {"fill": 7, "card": 5}
def test_a_snapshot_counts_as_returned_the_first_time_it_shows_up():
assert _returned(sealed(LOOP)) == {Q1: 2}
merged_only = [(3, "node.finished", "merged", {"artifact": OUT1, "evidence": [{"kind": "query", "artifact": Q1}]})]
assert _returned(sealed(merged_only)) == {Q1: 3}