Skip to content

/agent 断网时自动对账:不再误报「不确定有没有开始」 - #64

Merged
NginxL merged 2 commits into
mainfrom
fix/agent-network
Dec 5, 2026
Merged

NginxL merged 2 commits into
mainfrom
fix/agent-network

Conversation

@NginxL

@NginxL NginxL commented Dec 5, 2026

Copy link
Copy Markdown
Owner

做了什么

/agent(飞书、微信共用 core/openmate_core/agent.py)能扛住本机和 Persona hub 之间的连接中途断开。

背景。 2026-12-05 微信两次报「Persona 不确定这个 /agent 有没有开始处理」:hub 转发启动请求的途中 bus 断了,返回 502 assistant_in_doubt。

  • 其中一次会话其实已经开起来、一直在跑,但 OpenMate 判了失败,答案没回到聊天里,用户重发后多做了一遍。
  • 另一次是真的没开,提示却一模一样。

改动:

  • 新会话启动结果不确定时自动对账,不再直接判失败。
    • 触发条件:assistant_in_doubt、unknown、op_id_reused、可能已发出时到期、pending 到期。
    • 怎么找:先用 GET /api/v1/assistant 做端到端探测,再从 reporters 和会话列表第一页取候选,只在首条用户文本里匹配带引号的请求编号。
    • 找到:接管这个会话,答案照常回到聊天。
    • 确认没开才重开一次,条件是进入不确定满 90 秒、探测连续稳定 30 秒、相隔 20 秒的两次查询都没找到,并且仍在请求创建后 600 秒内。重开换新的 op 和新编号(<id>.2),旧编号的回复会被拒绝;之后如果旧会话晚到,会被停掉。
    • 自动代答和文档代答不重开、不出声。
  • 同一 op 重试取消三次上限(op id 本身幂等)。
  • 续聊和任务 Agent 转交结果不明时,到会话记录里按 client_msg_id 或编号找。
    • 找到就继续;限时没找到就说明「消息可能稍后送达」,不自动重发。
    • 202 later 有了 120 秒上限。
  • 带 X-Persona-Auth-Refused: token 头的 401 按「没发出去」重试(bus 半开时 hub 会透传一次)。
  • 扩展重启后,已记录但可能没发出的请求先用原 op id 或 client_msg_id 再问 hub(hub 去重)。回执不确定时才对账。
  • 飞书话题:对账期间来的追问,会等首条请求的会话确认后再续到同一会话。
  • 对账只写 INFO 日志,不在聊天里新增消息。

怎么验证的

  • 新增 tests/test_agent_network.py,共 31 个用例:覆盖接管、重开门槛、候选晚出现、首条消息未可见、bus 断开、长 prompt、旧编号清理、二次不确定、401、pending 到期、重启续跑、隔夜、停止、换账号、静默代答、续聊、转交、话题追问。在 b04ddf4 上逐个失败。
  • bash scripts/test.sh 和 PYTHON=/usr/bin/python3 bash scripts/test.sh:core 945、飞书 419、微信 186 全部通过。
  • 依赖的 hub 行为(assistant_in_doubt 回执、reporters、会话列表分页、401 拒绝头)按线上 hub ab8920cc03 的源码核对过。

没做什么

  • 对账期间不在聊天里提示「正在核对」:那是新增的以用户身份发出的消息,要用户点头。
  • 上游改动没做:manager 把快照 publish 挪到回答之后、按 op id 去重并让 in_doubt 可重试、relay 不透传 401。这些另行提给 Persona。
  • 真实断链下的端到端实测没做:没法人为制造 bus 中途断开。上线后看真实运行的日志。

NginxL and others added 2 commits December 5, 2026 18:26
…ailing

When the link between this Mac and the Persona hub drops while the hub relays a new conversation's
start, the hub answers 502 assistant_in_doubt and keeps the receipt unknown. On 2026-12-05 one such
start had in fact opened its conversation: the run failed, the answer never reached the chat and the
user asked again; another had not opened anything, with the same message.

- Uncertain starts (in_doubt, unknown, op_id_reused, a possibly-sent try at the deadline, pending at
  the deadline) are reconciled: an end-to-end probe (GET /api/v1/assistant), candidates from the
  reporters and the first page of the session list, a match only on the quoted request ref in the
  first user text. Found: adopted, the answer goes back to the chat. Absent only after 90 s, 30 s of
  stable probes and two clear lookups 20 s apart, within 600 s of the request: re-opened once under
  a new op and ref (<id>.2); a late old conversation is stopped and its replies refused. Quiet runs
  (auto-reply, documents) never re-open and fail silently.
- The same op is retried without the 3-tries cap (op ids are idempotent).
- Follow-ups and hand-ons whose delivery is unknown are looked up by client_msg_id / ref in the
  transcript; unseen within 120 s they fail saying the message may still arrive (never resent).
- A 401 with X-Persona-Auth-Refused: token is retried as unsent.
- After a restart, a recorded but maybe-unsent start or send asks again under the same op id /
  client_msg_id (the hub deduplicates) and reconciles only an uncertain answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- 核心(飞书、微信都生效):hub 回答「不确定有没有开始」时,不再直接判失败,而是到 Persona 里核对。
  - 会话已经开了就接管,答案照常回到聊天里。
  - 确认没开(等满 90 秒、连接稳定 30 秒、两次查询都没找到),才在 10 分钟内换新编号重开一次。
  - 续聊和转交结果不明时,到会话记录里找;找不到就说明消息可能稍后送达,不重发。
  - 扩展重启后,先用原编号再问 hub。
- 不新增权限,也不新增以用户身份发的消息。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@NginxL
NginxL merged commit 973608c into main Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant