Skip to content

chore: add diagnostic logging for AskUserQuestion / ExitPlanMode reply flow#66

Merged
quick-sort merged 1 commit into
mainfrom
chore/pending-ask-diagnostic-logging
Jun 19, 2026
Merged

chore: add diagnostic logging for AskUserQuestion / ExitPlanMode reply flow#66
quick-sort merged 1 commit into
mainfrom
chore/pending-ask-diagnostic-logging

Conversation

@hilr

@hilr hilr commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

生产环境里 AskUserQuestion / ExitPlanMode 的回复流转有 bug:question 发到 channel 后用户回复,但 agent 不等回复就继续按默认情况执行。需要先加日志看清楚回复在哪一步丢了。

加 INFO 级别日志覆盖整条回复链路:

main.py

  • dispatch_loop received message — channel 收到用户消息
  • handle_message entry — 进入 handler
  • router replied directly — router 直接回(没到 agent)
  • dispatching to agent ... has_pending_question=X — 即将调 agent.run
  • handle_message done

claude_code.py

  • run() entry ... pending_kind=X pending_tool_use_id=Y — 进入 agent.run,带 prompt 预览和 pending 状态
  • no pending ask — sending prompt as fresh query — 走了新 query 分支(不该出现的时候出现就是 bug)
  • consuming pending ask — 检测到 pending,开始消费用户回复
  • sending tool_result to CLI / tool_result written to CLI stdin — 写 stdin 前后
  • 拦截分支:... detected — setting pending ask / ... pending ask saved, returning from run()

Test Plan

  • 全套 183 个测试通过(无行为变化,只是加日志)

复现后要看的日志关键点

  1. 第一次(出 question):应该看到 AskUserQuestion detected → pending ask saved, returning from run()
  2. 用户回复那一刻:应该看到 dispatch_loop received message → handle_message entry → dispatching to agent has_pending_question=True → run() entry pending_kind=question
  3. 解析并发回:应该看到 consuming pending ask → sending tool_result to CLI

第 2 步 has_pending_question=False 或者 pending_kind=None → pending 状态丢了;第 3 步看不到 sending tool_result → 解析路径出问题。

…y flow

Pending replies (AskUserQuestion answers, ExitPlanMode approvals) aren't
reaching the CLI reliably in production. Add INFO logs at every state
transition so the agent-box.log trace shows exactly where the reply
chain breaks:

- main.py: log every incoming message, router outcome, and the
  has_pending_question flag before dispatching to the agent
- claude_code.py run() entry: log prompt preview and current pending
  state (kind + tool_use_id), so it's visible whether a reply is about
  to be consumed as a tool_result or mistakenly sent as a fresh query
- _send_tool_result: log the content being written and confirmation
  after the write
- Interception block: log when AskUserQuestion / ExitPlanMode is
  detected, when _pending_ask is saved, and when run() returns to wait

No behavior change — 183 tests still pass.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@quick-sort quick-sort merged commit d9b33db into main Jun 19, 2026
1 check passed
@quick-sort quick-sort deleted the chore/pending-ask-diagnostic-logging branch June 19, 2026 16:22
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.

2 participants