mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-22 09:41:57 +00:00
3e311ec551
整合(3c-integrate) - 公共层:跨天时长(formatSpan / formatOffset / formatLapse)、本机署名(lib/actor)、检索分类色令牌、 数据源目录(useDatasources)收进公共层;失败说明能一次列出多个写坏的自定义工具,查询超时指到数据源的查询时限 - 管理页收尾:设置页接入统一的离开前确认;检查脚本不再把沙箱里的真实值打进输出 - 画布 ↔ 右栏:点出具印章、点节点能跳到右栏对应位置;「定位」直接聚焦到出问题的字段 - 回答流 / 问数据 / 记录页:失败信息带「去哪修」且不再被翻译两遍;协作团队的结局按执行截取; 等审批时断流重连、而运行已在空档里跑完的,补全事件 扫尾(3c-sweep) - 术语统一:开始运行 / 继续运行、工作流最大步数;未保存的运行统一叫「未保存的工作流」(后端新运行同步) - 沙箱报错不再带 Python 异常类名;新增 check-tokens,未定义的 CSS 变量为 0,两个旧别名迁移后删除 检查脚本(3c-checks) - check-ui 跟上新界面(运行列表按 data 属性找行),加上令牌与选中态的回归断言,输出里的名字打码 - 新增 check-all:按顺序跑全部检查、汇总退出码,--repeat 列出偶发项 - 单节出错只记这一节失败、不再带崩整个脚本;外壳遥测浮层「重新检测」时焦点不再掉到 body 网页工具与 fake-ip - Clash / mihomo 一类代理开 TUN + fake-ip 时,查什么都先拿到 198.18 段的假地址,被当成内网拦截。 新增 AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES(默认空):落在段里的解析结果改向公共 DNS(DoH)核实真实地址, 公网放行、内网照拦、核实不了按拦截处理。DoH 默认先问境外的,境内公共 DNS 会对被封锁的域名返回污染结果 - 列表型环境变量接受逗号分隔(原来注释说逗号分隔、实际只认 JSON 数组);域名解析改为异步,不再阻塞事件循环 验证(沙箱):tsc 干净,pytest 866 passed(两遍),检查脚本全绿。
243 lines
10 KiB
Python
243 lines
10 KiB
Python
"""会话列表认得每个会话的最后一轮停在哪;轮次的可信度元数据有自己的一列。
|
||
|
||
以前左栏只能给「这次打开页面以来取回过内存」的会话标状态。没打开过的会话,
|
||
哪怕最后一轮正停在审批上、或者已经失败,列表里也和正常完成的长得一样——
|
||
人得一个个点进去才知道哪里在等他。
|
||
|
||
元数据(出具档位、运行类别、未查库、结局、耗时…)以前挂在 review.meta 下,
|
||
于是没复核过的轮次 review 也不为空,「没复核过」和「复核过、没问题」分不开。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from datetime import timedelta
|
||
|
||
import pytest
|
||
from httpx import ASGITransport, AsyncClient
|
||
|
||
from app.db.base import SessionLocal, utcnow
|
||
from app.db.models import Approval, ConversationTurn, Run
|
||
from app.main import app
|
||
|
||
|
||
@pytest.fixture
|
||
async def client():
|
||
transport = ASGITransport(app=app)
|
||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||
yield c
|
||
|
||
|
||
async def _conversation(client, title: str) -> str:
|
||
r = await client.post("/api/conversations", json={"kind": "chat", "title": title})
|
||
assert r.status_code == 201, r.text
|
||
return r.json()["id"]
|
||
|
||
|
||
async def _turn(client, conv_id: str, question: str = "订单数多少", **patch) -> dict:
|
||
r = await client.post(f"/api/conversations/{conv_id}/turns", json={"question": question})
|
||
assert r.status_code == 201, r.text
|
||
turn = r.json()
|
||
if patch:
|
||
r = await client.patch(f"/api/conversations/{conv_id}/turns/{turn['id']}", json=patch)
|
||
assert r.status_code == 200, r.text
|
||
turn = r.json()
|
||
return turn
|
||
|
||
|
||
async def _run(status: str, *, pending: bool = False) -> str:
|
||
async with SessionLocal() as session:
|
||
run = Run(workflow_name="状态测试", status=status)
|
||
session.add(run)
|
||
await session.flush()
|
||
if pending:
|
||
session.add(Approval(run_id=run.id, node_id="gate", status="pending"))
|
||
await session.commit()
|
||
return run.id
|
||
|
||
|
||
async def _listed(client) -> dict[str, dict]:
|
||
r = await client.get("/api/conversations")
|
||
assert r.status_code == 200, r.text
|
||
return {c["id"]: c for c in r.json()}
|
||
|
||
|
||
# --------------------------------------------------------------------------
|
||
# REQ-1:meta 自己一列
|
||
# --------------------------------------------------------------------------
|
||
|
||
META = {"v": 1, "runClass": "exploratory", "noQuery": True, "ms": 8200, "queries": 0}
|
||
|
||
|
||
async def test_turn_meta_is_stored_and_read_back_on_its_own(client):
|
||
conv = await _conversation(client, "元数据")
|
||
turn = await _turn(client, conv, meta=META, status="done", answer="12 家")
|
||
assert turn["meta"] == META
|
||
assert turn["review"] is None, "只写了 meta,review 得还是「没复核过」"
|
||
|
||
detail = (await client.get(f"/api/conversations/{conv}")).json()
|
||
assert detail["turns"][0]["meta"] == META
|
||
|
||
|
||
async def test_patching_review_and_meta_do_not_overwrite_each_other(client):
|
||
conv = await _conversation(client, "互不覆盖")
|
||
turn = await _turn(client, conv, meta=META)
|
||
review = {"verdict": "annotated", "note": "检索降级"}
|
||
r = await client.patch(f"/api/conversations/{conv}/turns/{turn['id']}", json={"review": review})
|
||
assert r.json()["meta"] == META
|
||
r = await client.patch(f"/api/conversations/{conv}/turns/{turn['id']}",
|
||
json={"meta": {**META, "ms": 9000}})
|
||
assert r.json()["review"] == review
|
||
assert r.json()["meta"]["ms"] == 9000
|
||
|
||
|
||
async def test_old_turns_with_meta_under_review_still_read_as_meta(client):
|
||
"""迁移之前写进去的:meta 挂在 review 下。读的时候由后端兜底,前端只认 turn.meta。"""
|
||
conv = await _conversation(client, "老数据")
|
||
legacy = {"verdict": "ok", "meta": {"v": 1, "outcome": "cancelled"}}
|
||
turn = await _turn(client, conv, review=legacy, status="error", error="已取消")
|
||
assert turn["meta"] == {"v": 1, "outcome": "cancelled"}
|
||
# 新列一旦写过就以它为准
|
||
r = await client.patch(f"/api/conversations/{conv}/turns/{turn['id']}", json={"meta": META})
|
||
assert r.json()["meta"] == META
|
||
|
||
|
||
# --------------------------------------------------------------------------
|
||
# REQ-2:列表里的最后一轮状态
|
||
# --------------------------------------------------------------------------
|
||
|
||
|
||
async def test_the_list_says_where_each_conversation_last_stopped(client):
|
||
cases: dict[str, tuple[str, str | None]] = {}
|
||
|
||
conv = await _conversation(client, "完成")
|
||
run = await _run("succeeded")
|
||
await _turn(client, conv, run_id=run, status="done", answer="12")
|
||
cases[conv] = ("done", run)
|
||
|
||
conv = await _conversation(client, "失败")
|
||
run = await _run("failed")
|
||
await _turn(client, conv, run_id=run, status="error", error="查询超时")
|
||
cases[conv] = ("error", run)
|
||
|
||
conv = await _conversation(client, "取消")
|
||
await _turn(client, conv, status="error", error="已取消")
|
||
cases[conv] = ("cancelled", None)
|
||
|
||
conv = await _conversation(client, "重启挂起")
|
||
run = await _run("interrupted")
|
||
await _turn(client, conv, run_id=run, status="error", error="服务重启,这一轮中断了",
|
||
meta={"v": 1, "outcome": "suspended", "runId": run})
|
||
cases[conv] = ("suspended", run)
|
||
|
||
conv = await _conversation(client, "在跑")
|
||
run = await _run("running")
|
||
await _turn(client, conv, run_id=run)
|
||
cases[conv] = ("running", run)
|
||
|
||
conv = await _conversation(client, "等审批")
|
||
run = await _run("interrupted", pending=True)
|
||
await _turn(client, conv, run_id=run)
|
||
cases[conv] = ("waiting", run)
|
||
|
||
conv = await _conversation(client, "停着没审批")
|
||
run = await _run("interrupted")
|
||
await _turn(client, conv, run_id=run)
|
||
cases[conv] = ("suspended", run)
|
||
|
||
conv = await _conversation(client, "页面关了但跑完了")
|
||
run = await _run("succeeded")
|
||
await _turn(client, conv, run_id=run)
|
||
cases[conv] = ("done", run)
|
||
|
||
conv = await _conversation(client, "页面关了而且失败了")
|
||
run = await _run("failed")
|
||
await _turn(client, conv, run_id=run)
|
||
cases[conv] = ("error", run)
|
||
|
||
conv = await _conversation(client, "正在建图")
|
||
await _turn(client, conv)
|
||
cases[conv] = ("running", None)
|
||
|
||
listed = await _listed(client)
|
||
got = {cid: (listed[cid]["last_status"], listed[cid]["last_run_id"]) for cid in cases}
|
||
assert got == cases
|
||
|
||
|
||
async def test_only_the_last_turn_counts(client):
|
||
conv = await _conversation(client, "后来好了")
|
||
await _turn(client, conv, status="error", error="查询超时")
|
||
run = await _run("succeeded")
|
||
await _turn(client, conv, run_id=run, status="done", answer="好了")
|
||
assert (await _listed(client))[conv]["last_status"] == "done"
|
||
|
||
|
||
async def test_a_build_abandoned_long_ago_is_not_shown_as_running(client):
|
||
"""建图是跟着浏览器那条流走的:页面关了就没了,轮次却一直停在 running。"""
|
||
conv = await _conversation(client, "建到一半关了")
|
||
turn = await _turn(client, conv)
|
||
async with SessionLocal() as session:
|
||
row = await session.get(ConversationTurn, turn["id"])
|
||
row.updated_at = utcnow() - timedelta(hours=2)
|
||
await session.commit()
|
||
assert (await _listed(client))[conv]["last_status"] == "error"
|
||
|
||
|
||
async def test_an_empty_conversation_has_no_status(client):
|
||
conv = await _conversation(client, "空的")
|
||
item = (await _listed(client))[conv]
|
||
assert item["last_status"] is None and item["last_run_id"] is None
|
||
|
||
|
||
async def test_the_detail_carries_the_same_status(client):
|
||
conv = await _conversation(client, "详情也带")
|
||
run = await _run("interrupted", pending=True)
|
||
await _turn(client, conv, run_id=run)
|
||
detail = (await client.get(f"/api/conversations/{conv}")).json()
|
||
assert (detail["last_status"], detail["last_run_id"]) == ("waiting", run)
|
||
|
||
|
||
# --------------------------------------------------------------------------
|
||
# 重试之后:meta.runId 才是这一轮现在的运行
|
||
# --------------------------------------------------------------------------
|
||
|
||
|
||
async def test_a_retried_turn_reports_the_new_run_not_the_old_one(client):
|
||
"""重试会换一次运行,可 run_id 列清不掉(TurnPatch 的 None 表示「不改」),
|
||
还指着上一次失败的运行;前端写的 meta.runId 才是权威。列表得按它报,
|
||
不然重试正在建图时,左栏还挂着上一次的「失败」。"""
|
||
conv = await _conversation(client, "重试了")
|
||
old = await _run("failed")
|
||
turn = await _turn(client, conv, run_id=old, status="error", error="查询超时",
|
||
meta={"v": 1, "runId": old})
|
||
# 重试:重新建图,还没有运行
|
||
r = await client.patch(f"/api/conversations/{conv}/turns/{turn['id']}",
|
||
json={"status": "running", "meta": {"v": 1, "runId": None}})
|
||
assert r.status_code == 200
|
||
item = (await _listed(client))[conv]
|
||
assert (item["last_status"], item["last_run_id"]) == ("running", None)
|
||
|
||
# 重试出来的运行起来了,还在跑
|
||
new = await _run("running")
|
||
await client.patch(f"/api/conversations/{conv}/turns/{turn['id']}",
|
||
json={"meta": {"v": 1, "runId": new}})
|
||
item = (await _listed(client))[conv]
|
||
assert (item["last_status"], item["last_run_id"]) == ("running", new)
|
||
detail = (await client.get(f"/api/conversations/{conv}")).json()
|
||
assert (detail["last_status"], detail["last_run_id"]) == ("running", new)
|
||
|
||
|
||
async def test_old_turns_without_run_id_in_meta_still_use_the_column(client):
|
||
conv = await _conversation(client, "老轮次")
|
||
run = await _run("interrupted", pending=True)
|
||
await _turn(client, conv, run_id=run, meta={"v": 1, "ms": 1200})
|
||
item = (await _listed(client))[conv]
|
||
assert (item["last_status"], item["last_run_id"]) == ("waiting", run)
|
||
|
||
|
||
async def test_turns_carry_updated_at_for_the_same_staleness_clock(client):
|
||
"""前端判断「建图断了」要和后端用同一只钟:后端按 updated_at 算。"""
|
||
conv = await _conversation(client, "带时间")
|
||
turn = await _turn(client, conv)
|
||
assert turn.get("updated_at"), turn
|
||
detail = (await client.get(f"/api/conversations/{conv}")).json()
|
||
assert detail["turns"][0]["updated_at"]
|