账本接口:刚跑完未封存答 pending 只限结束后两分钟内,早就结束、一直没封存的照实答「运行还没有封存」

复核 W4R-08:上一次给 GET /api/runs/{id}/ledger 加的 pending 分支没有时间界限——成功结束、要比对、没封存的运行一律答
pending。它本来只为 runner 落库 run.finished 之后、封存之前的那一下(页面收到 run.finished 立刻来取)。封存出了错、或者是
封存这件事之前的老数据,会永远答 pending:每个轮次底部、证据面板都转两分钟「正在比对口径」,再取一次还是 pending;改之前
答的 ineligible「运行还没有封存,不比对」才是实情。

- ledger/view.py:新增 SEAL_GRACE_S = 120(和页面等比对结论的上限 store/ledger.ts 的两分钟同一个量级),_just_finished 看
  finished_at 在不在这段时间里(没记结束时间的不算;库里读回 naive 的按 UTC 算,同 db/base.UTCDateTime)。只有刚结束的才答
  pending,其余照旧走 ineligible(NOT_SEALED);_stamp 里的局部 datetime 导入挪到模块顶上
- 测试:刚跑完的那条补上 finished_at=现在;新增一条结束已久、一直没封存的和一条没记结束时间的,都答 ineligible、原因是
  没封存、不排按需比对。去掉 _just_finished 回跑,新测试失败(答的是 pending)
This commit is contained in:
ThearchyHelios
2026-10-16 22:13:52 +08:00
parent 9ad6c035d0
commit 9886e10be2
2 changed files with 47 additions and 6 deletions
+31 -2
View File
@@ -102,11 +102,14 @@ async def test_a_run_that_just_finished_and_is_about_to_be_sealed_is_pending(cli
await ledger_match.drain()
from app.db.models import Run
from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc)
async with SessionLocal() as session:
fresh = Run(workflow_name="刚跑完", status="succeeded", run_class="exploratory", origin_kind="chat", graph={},
input={}, output={})
input={}, output={}, finished_at=now)
failed = Run(workflow_name="失败了", status="failed", run_class="exploratory", origin_kind="chat", graph={},
input={}, output={})
input={}, output={}, finished_at=now)
session.add_all([fresh, failed])
await session.commit()
fresh_id, failed_id = fresh.id, failed.id
@@ -117,6 +120,32 @@ async def test_a_run_that_just_finished_and_is_about_to_be_sealed_is_pending(cli
assert (await ledger_of(client, failed_id))["state"] == "ineligible"
async def test_a_run_that_finished_long_ago_and_was_never_sealed_says_so(client, monkeypatch):
"""「刚跑完、马上封存」只是结束后的那一会儿:成功结束了好一阵、一直没封存的(封存时出了错、封存这件事之前的老数据),
再答 pending 就是让页面一直转圈、等两分钟、点「再取一次」还是 pending。照实说没封存(ineligible,NOT_SEALED);
结束时间没记的同样不当成刚结束。"""
from datetime import datetime, timedelta, timezone
from app.db.models import Run
from app.ledger import view as ledger_view
old = datetime.now(timezone.utc) - timedelta(seconds=ledger_view.SEAL_GRACE_S + 60)
async with SessionLocal() as session:
stale = Run(workflow_name="很早以前跑完", status="succeeded", run_class="exploratory", origin_kind="chat",
graph={}, input={}, output={}, finished_at=old)
undated = Run(workflow_name="没记结束时间", status="succeeded", run_class="exploratory", origin_kind="chat",
graph={}, input={}, output={})
session.add_all([stale, undated])
await session.commit()
stale_id, undated_id = stale.id, undated.id
scheduled: list[str] = []
monkeypatch.setattr(ledger_match, "schedule", lambda run_id, **kw: scheduled.append(run_id) or True)
for run_id in (stale_id, undated_id):
body = await ledger_of(client, run_id)
assert body["state"] == "ineligible" and body["reason"] == ledger_match.NOT_SEALED, body
assert scheduled == []
async def test_runs_that_are_not_compared_say_why(client, monkeypatch):
row = await S.explore(monkeypatch, S.EQUIVALENT, origin="api", text="本周销售额 [[v:Q1.r0.销售额]]。")
body = await ledger_of(client, row.id)