mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-18 03:02:01 +00:00
问数据:被驳回的那次尝试不写「已手动停止」,会话列表标「被驳回」
为什么:一轮的运行被审批人驳回之后点「重新运行本轮」,「之前的尝试」那一行写「已手动停止」,用户其实没有停过它。会话 列表的 last_status 把被驳回的一轮归成 cancelled,左栏不标——在等审批的那一问被驳回之后,看起来像是答完了(以前驳回 记成失败,左栏标的是「失败」)。 做了什么: - store/chat 的 attemptOf:被驳回的那次记 outcome=rejected,摘要写「被驳回:张工 驳回了「查数」:…」;老留档里记成 cancelled 的不改写。ChatPage 的尝试徽标认 rejected。 - api/conversations 的 last_status:前端落库时 meta 里记着 rejected 的、页面关掉没落库而运行是 cancelled + end_reason=rejected 的,都记 rejected;手动停止照旧 cancelled。types 的 last_status 加上 rejected。 - 左栏:打开过的看最后一轮(相位 cancelled 且带着驳回),没打开过的看 last_status,都标「被驳回」(不画异常色)。 测试:test_conversation_status 加一条(两种认法的被驳回、两种手动停止),改之前失败;check-chat 加一节被驳回(刷新后 认得出、补救区的话、左栏两种、重新运行之后的尝试记录),把前端改动退回去回跑,那一节 4 项不通过,改完通过。
This commit is contained in:
@@ -118,7 +118,7 @@ class ConversationOut(BaseModel):
|
||||
turn_count: int = 0
|
||||
#: 列表里给个副标题,让人一眼认出是哪次聊天
|
||||
last_question: str = ""
|
||||
#: 最后一轮停在哪:running | waiting | error | cancelled | suspended | done,
|
||||
#: 最后一轮停在哪:running | waiting | error | cancelled | rejected | suspended | done,
|
||||
#: 没有轮次时为 None。见 last_status()
|
||||
last_status: str | None = None
|
||||
#: 最后一轮的运行。建图阶段就断了的没有
|
||||
@@ -173,18 +173,24 @@ BUILD_STALE = timedelta(minutes=15)
|
||||
|
||||
|
||||
def last_status(
|
||||
turn: Any, run_status: str | None, pending: bool, now: datetime | None = None,
|
||||
turn: Any, run_status: str | None, pending: bool, now: datetime | None = None, end_reason: str | None = None,
|
||||
) -> str:
|
||||
"""一轮停在哪。轮次状态只有 running / done / error 三种,细分要看运行和元数据。
|
||||
|
||||
轮次状态是前端写的:页面关掉、服务重启时它停在 running,这时要以运行的状态为准;
|
||||
运行也没有的,是断在了建图阶段。已经收尾的轮次以轮次自己为准——它写下的就是
|
||||
用户看到的结局(取消、服务重启挂起在 error 里分开记)。
|
||||
|
||||
被审批人驳回结束的(运行 cancelled + end_reason=rejected,05-已知遗留 (a))单独记 rejected,不和手动停止混成
|
||||
cancelled:前端落库时 meta 里记着 rejected(outcome 照旧是 cancelled),页面关掉没落库的看运行的 end_reason。
|
||||
"""
|
||||
outcome = (turn_meta(turn) or {}).get("outcome")
|
||||
meta = turn_meta(turn) or {}
|
||||
outcome = meta.get("outcome")
|
||||
if turn.status == "done":
|
||||
return "done"
|
||||
if turn.status == "error":
|
||||
if outcome == "cancelled" and meta.get("rejected"):
|
||||
return "rejected"
|
||||
if outcome == "cancelled" or (turn.error or "").strip() == "已取消":
|
||||
return "cancelled"
|
||||
if outcome == "suspended":
|
||||
@@ -192,7 +198,7 @@ def last_status(
|
||||
live = _from_run(run_status, pending)
|
||||
return live if live in ("running", "waiting") else "suspended"
|
||||
return "error"
|
||||
live = _from_run(run_status, pending)
|
||||
live = _from_run(run_status, pending, end_reason)
|
||||
if live:
|
||||
return live
|
||||
updated = turn.updated_at
|
||||
@@ -202,7 +208,9 @@ def last_status(
|
||||
return "error" if stale else "running"
|
||||
|
||||
|
||||
def _from_run(run_status: str | None, pending: bool) -> str | None:
|
||||
def _from_run(run_status: str | None, pending: bool, end_reason: str | None = None) -> str | None:
|
||||
if run_status == "cancelled" and end_reason == "rejected":
|
||||
return "rejected"
|
||||
return {
|
||||
"queued": "running", "running": "running",
|
||||
"interrupted": "waiting" if pending else "suspended",
|
||||
@@ -236,11 +244,14 @@ async def _statuses(
|
||||
run_of = {cid: turn_run_id(t) for cid, t in turns.items()}
|
||||
run_ids = {r for r in run_of.values() if r}
|
||||
runs: dict[str, str] = {}
|
||||
ended: dict[str, str | None] = {}
|
||||
waiting: set[str] = set()
|
||||
if run_ids:
|
||||
runs = dict((await session.execute(
|
||||
select(Run.id, Run.status).where(Run.id.in_(run_ids))
|
||||
)).all())
|
||||
rows = (await session.execute(
|
||||
select(Run.id, Run.status, Run.end_reason).where(Run.id.in_(run_ids))
|
||||
)).all()
|
||||
runs = {r: st for r, st, _ in rows}
|
||||
ended = {r: reason for r, _, reason in rows}
|
||||
stopped = [r for r, st in runs.items() if st == "interrupted"]
|
||||
if stopped:
|
||||
waiting = set((await session.execute(
|
||||
@@ -251,7 +262,8 @@ async def _statuses(
|
||||
out: dict[str, tuple[str, str | None]] = {}
|
||||
for cid, turn in turns.items():
|
||||
run_id = run_of[cid] if run_of[cid] in runs else None
|
||||
out[cid] = (last_status(turn, runs.get(run_id or ""), run_id in waiting, now),
|
||||
out[cid] = (last_status(turn, runs.get(run_id or ""), run_id in waiting, now,
|
||||
end_reason=ended.get(run_id or "")),
|
||||
run_id)
|
||||
return out
|
||||
|
||||
|
||||
@@ -43,9 +43,9 @@ async def _turn(client, conv_id: str, question: str = "订单数多少", **patch
|
||||
return turn
|
||||
|
||||
|
||||
async def _run(status: str, *, pending: bool = False) -> str:
|
||||
async def _run(status: str, *, pending: bool = False, end_reason: str | None = None) -> str:
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name="状态测试", status=status)
|
||||
run = Run(workflow_name="状态测试", status=status, end_reason=end_reason)
|
||||
session.add(run)
|
||||
await session.flush()
|
||||
if pending:
|
||||
@@ -162,6 +162,39 @@ async def test_the_list_says_where_each_conversation_last_stopped(client):
|
||||
assert got == cases
|
||||
|
||||
|
||||
async def test_a_rejected_turn_is_listed_as_rejected_not_cancelled(client):
|
||||
"""被审批人驳回结束的一轮(运行 cancelled + end_reason=rejected,05-已知遗留 (a)):列表说「被驳回」,不和手动停止混成
|
||||
cancelled——发问的人不一定知道被驳回了,以前驳回记成失败,列表标的是「失败」。认法两条:前端落库时记下的
|
||||
meta.rejected,和页面关掉、轮次还停在 running 时运行的 end_reason。手动停止照旧是 cancelled。"""
|
||||
cases: dict[str, tuple[str, str | None]] = {}
|
||||
|
||||
conv = await _conversation(client, "驳回:前端落库了")
|
||||
run = await _run("cancelled", end_reason="rejected")
|
||||
await _turn(client, conv, run_id=run, status="error", error="已取消",
|
||||
meta={"v": 1, "runId": run, "outcome": "cancelled", "rejected": {"sub": "张工 驳回了「付款」"}})
|
||||
cases[conv] = ("rejected", run)
|
||||
|
||||
conv = await _conversation(client, "驳回:页面关了")
|
||||
run = await _run("cancelled", end_reason="rejected")
|
||||
await _turn(client, conv, run_id=run)
|
||||
cases[conv] = ("rejected", run)
|
||||
|
||||
conv = await _conversation(client, "手动停止:页面关了")
|
||||
run = await _run("cancelled", end_reason="stopped")
|
||||
await _turn(client, conv, run_id=run)
|
||||
cases[conv] = ("cancelled", run)
|
||||
|
||||
conv = await _conversation(client, "手动停止:前端落库了")
|
||||
run = await _run("cancelled", end_reason="stopped")
|
||||
await _turn(client, conv, run_id=run, status="error", error="已取消", meta={"v": 1, "runId": run, "outcome": "cancelled"})
|
||||
cases[conv] = ("cancelled", run)
|
||||
|
||||
listed = await _listed(client)
|
||||
assert {cid: (listed[cid]["last_status"], listed[cid]["last_run_id"]) for cid in cases} == cases
|
||||
detail = (await client.get(f"/api/conversations/{next(iter(cases))}")).json()
|
||||
assert detail["last_status"] == "rejected"
|
||||
|
||||
|
||||
async def test_only_the_last_turn_counts(client):
|
||||
conv = await _conversation(client, "后来好了")
|
||||
await _turn(client, conv, status="error", error="查询超时")
|
||||
|
||||
Reference in New Issue
Block a user