Skip to content

fix(core): agent-session idle close must not fire during unsolicited background turns#1515

Open
lyq3 wants to merge 1 commit into
chenhg5:mainfrom
lyq3:fix/idle-timeout-unsolicited-guard
Open

fix(core): agent-session idle close must not fire during unsolicited background turns#1515
lyq3 wants to merge 1 commit into
chenhg5:mainfrom
lyq3:fix/idle-timeout-unsolicited-guard

Conversation

@lyq3

@lyq3 lyq3 commented Jul 9, 2026

Copy link
Copy Markdown

Follow-up to #1338 (found while comparing it with the now-closed #1492).

问题 / Problem

#1338 的空闲关闭定时器只被前台回合取消/重置:

  • 会话在做后台工作时(unsolicited reader 转发的后台任务输出),定时器照常计时,到点会把正在干活的 live agent 进程杀掉;
  • 后台回合正常完成后也不会重启空闲时钟,空闲语义变成"距上次前台回合结束"而非"距最后一次活动"。

The idle-close timer from #1338 is only cancelled/rescheduled by foreground turns. A session relaying unsolicited background events keeps counting down and gets its live agent killed mid-work; a cleanly-finished background turn never restarts the idle clock.

修复 / Fix

镜像前台回合的 begin/end 配对(核心 2 处调用):

  • unsolicited reader 标记回合活跃时 cancelAgentSessionIdleClose
  • 回合干净结束后 scheduleAgentSessionIdleClose —— 放在 eventsNeedResync 清零之后,因为调度在 resync 未决时会被拒绝。

测试 / Testing

  • 2 个回归测试(TestAgentSessionIdleTimeout_UnsolicitedTurnCancelsClose / _ReschedulesAfterUnsolicitedTurn),已验证无此修复时必失败(误杀复现),修复后通过,含 -race
  • 移植真机 blackbox E2E(tests/blackbox/p1):真实 Claude Code 进程被回收(/proc 观测)→ 下一条消息 resume 且第一轮埋的暗号完好;
  • 补上 agent_session_idle_timeout_mins 的中英文使用文档(docs/usage.md / usage.zh-CN.md,Add per-session idle timeout for live agent processes #1338 只更新了 config.example);
  • go vet + 全量 go test ./... 全绿(no_web tag)。

🤖 Generated with Claude Code

The idle-close timer added in chenhg5#1338 is only cancelled by foreground
turns: unsolicited background activity (background task completions
relayed by the unsolicited reader) neither cancels nor resets it, so a
session doing background work past the timeout gets its live agent
killed mid-stream, and a completed background turn never restarts the
idle clock.

Cancel the timer when the unsolicited reader marks a turn active, and
reschedule it after the turn completes cleanly (after eventsNeedResync
is cleared, since scheduling is refused while a resync is pending) —
mirroring the existing foreground begin/end pairing.

Also adds a real-agent blackbox E2E for agent_session_idle_timeout_mins
(process reaped, next message resumes with context intact) and usage
docs (EN/zh) for the option.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@lyq3 lyq3 requested a review from chenhg5 as a code owner July 9, 2026 03:49
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