From 77b4c9c68ac15aad16676a52c287bea97118f94e Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Sat, 17 Oct 2026 09:19:24 +0800 Subject: [PATCH] =?UTF-8?q?E4=20=E7=9A=84=E7=89=88=E6=9C=AC=E7=94=A8?= =?UTF-8?q?=E9=87=8F=E5=8F=AA=E7=AE=97=E6=9C=89=E5=8F=A3=E5=BE=84=E5=8D=A1?= =?UTF-8?q?=E7=9A=84=E5=B7=A5=E4=BD=9C=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 验收:E4 的用量把没有口径卡的普通发布工作流也当成口径计数。_e4 按(工作流, 版本)数所有 run_class=formal 的运行, 不看那一版是不是口径。 改法:数完之后按 WorkflowVersion 的图过一遍,只留图上有源头口径卡的版本(recall.source_cards:metrics 节点、不是 转抄别处口径的 caliber_from,和召回认口径卡的同一个判断)。比对里等价已证明的运行数(proven_runs)本来就只来自口径, 不动。写进了模块说明。 测试:test_ledger_api 新增一条:一个有口径卡、一个没有的已发布工作流各跑两次正式运行,E4 用量里只有前者、formal_runs 为 2。去掉修复回跑挂。 --- backend/app/ledger/stats.py | 27 ++++++++++++++++++++++++--- backend/tests/test_ledger_api.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/backend/app/ledger/stats.py b/backend/app/ledger/stats.py index 180d984a..64f37605 100644 --- a/backend/app/ledger/stats.py +++ b/backend/app/ledger/stats.py @@ -17,7 +17,8 @@ 段对话的算重复的形状,给出这样的形状几个、有重复形状的运行占几成;问题聚类(app/chat/questions:全站唯一的聚类,只取问题 原文、不含 history、排除系统派生的运行)里 ≥ REPEAT_RUNS 个问题的类占几成。认证队列的转化(第 3 波,ledger/certify):固化成 口径草稿几份(origin.freeze 的工作流)、其中发布了几份、点了「不是口径」几项。 -- E4 召回:每个口径版本被用了多少次(比对里等价已证明的运行数、这一版的正式运行数);每次发布(口径升到某一版)召回了几组、 +- E4 召回:每个口径版本被用了多少次(比对里等价已证明的运行数、这一版的正式运行数——只算图上有源头口径卡的版本,没有口径卡 + 的普通发布工作流的正式运行不算);每次发布(口径升到某一版)召回了几组、 收过尾的几组、其中数变了的几组和比例、重算失败的几组(ledger/recall:召回运行和它收尾时记的 ledger.recalled,第 4 波)。 按召回运行的发起时间筛窗口;表结构变了之后的口径试算不算召回。 """ @@ -289,8 +290,10 @@ async def _e4(session: Any, contents: list[dict[str, Any]], **scope: Any) -> dic stmt = stmt.where(Run.created_at >= scope["since"]) if scope.get("until") is not None: stmt = stmt.where(Run.created_at < scope["until"]) - formal = {ledger_labels.caliber_key(w, v): int(n) - for w, v, n in (await session.execute(stmt.group_by(Run.workflow_id, Run.version))).all()} + counted = {(w, v): int(n) for w, v, n in (await session.execute(stmt.group_by(Run.workflow_id, Run.version))).all()} + # 只算有口径卡的那几版:没有口径卡的普通发布工作流的正式运行不是「这个口径版本被用了」 + calibers = await _caliber_versions(session, counted) + formal = {ledger_labels.caliber_key(w, v): n for (w, v), n in counted.items() if (w, v) in calibers} recalls = int(await session.scalar(select(func.count()).select_from(Run).where(Run.origin_kind == "recall")) or 0) usage = [{"caliber": key, "workflow_id": key.rsplit("@", 1)[0], "version": int(key.rsplit("@", 1)[1]), "proven_runs": proven.get(key, 0), "formal_runs": formal.get(key, 0)} @@ -298,6 +301,24 @@ async def _e4(session: Any, contents: list[dict[str, Any]], **scope: Any) -> dic return {"usage": usage, "recall_runs": recalls, "per_publish": await _per_publish(session, **scope)} +async def _caliber_versions(session: Any, keys: Collection[tuple[str, int]]) -> set[tuple[str, int]]: + """这几个(工作流, 版本)里图上有源头口径卡的那些(recall.source_cards:metrics 节点、不是转抄别处的 caliber_from)。""" + from sqlalchemy import or_ + + from app.db.models import WorkflowVersion + from app.ledger.recall import source_cards + + keys = list(keys) + out: set[tuple[str, int]] = set() + for i in range(0, len(keys), 200): + chunk = keys[i:i + 200] + rows = (await session.execute(select(WorkflowVersion.workflow_id, WorkflowVersion.version, WorkflowVersion.graph) + .where(or_(*[(WorkflowVersion.workflow_id == w) & (WorkflowVersion.version == v) + for w, v in chunk])))).all() + out |= {(w, v) for w, v, graph in rows if source_cards(graph)} + return out + + async def _per_publish(session: Any, **scope: Any) -> dict[str, Any]: """每次发布召回了几组、其中数变了的比例(E4 下半):按(工作流、新版本)分,读召回运行收尾时记的结论。""" from app.core.events import EventType diff --git a/backend/tests/test_ledger_api.py b/backend/tests/test_ledger_api.py index 2e5eeff2..3814e4e5 100644 --- a/backend/tests/test_ledger_api.py +++ b/backend/tests/test_ledger_api.py @@ -295,6 +295,35 @@ async def test_stats_count_what_the_fixtures_did(client, monkeypatch): assert body["e4"]["per_publish"]["available"] is True and body["e4"]["per_publish"]["groups"] == 0 +async def test_e4_usage_counts_formal_runs_only_of_workflows_with_a_caliber_card(client): + """E4 的版本用量只算有口径卡的工作流(下一轮验收:没有口径卡的普通发布工作流的正式运行也被当成口径计数)。卡是转抄别处 + 口径(caliber_from)的不算源头口径,同 recall.source_cards。""" + from datetime import datetime, timezone + + from app.db.models import Run, Workflow, WorkflowVersion + + since = datetime.now(timezone.utc) + card = {"id": "card", "type": "metrics", "position": {"x": 0, "y": 0}, + "data": {"label": "口径卡", "config": {"caliber": "周销售", "caliber_version": "v1", "metrics": []}}} + plain = {"id": "out", "type": "output", "position": {"x": 0, "y": 0}, "data": {"label": "出口", "config": {}}} + async with SessionLocal() as session: + made = {} + for name, nodes in (("有口径卡", [card, plain]), ("普通工作流", [plain])): + wf = Workflow(name=name, graph={"nodes": nodes, "edges": []}, status="published", published_version=1) + session.add(wf) + await session.flush() + session.add(WorkflowVersion(workflow_id=wf.id, version=1, graph={"nodes": nodes, "edges": []})) + for _ in range(2): + session.add(Run(workflow_id=wf.id, version=1, run_class="formal", status="succeeded", + graph={"nodes": nodes, "edges": []}, input={})) + made[name] = wf.id + await session.commit() + body = (await client.get("/api/ledger/stats", params={"since": since.isoformat()})).json() + usage = {u["caliber"]: u for u in body["e4"]["usage"]} + assert usage[f"{made['有口径卡']}@1"]["formal_runs"] == 2 + assert f"{made['普通工作流']}@1" not in usage, usage + + async def test_stats_take_the_run_ids_the_caller_names(monkeypatch): """评测(第 3 波)直接调 compute_stats,点名自己的运行:只算这几次。""" from app.ledger.stats import compute_stats