Skip to content

fix(loop): discard an agent session that never reached a prompt - #553

Merged
huichen merged 2 commits into
mainfrom
claude/loop-orphan-session
Sep 11, 2026
Merged

huichen merged 2 commits into
mainfrom
claude/loop-orphan-session

Conversation

@huichen

@huichen huichen commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

问题

loop run 在进入 turn 循环之前就创建 agent session,但若干「第一轮直接退出」的路径从未 prompt:

  • P0-1 workspace guard 拒绝与 peer 抢同一工作区(degrade-to-serial)
  • 第一轮判定为 wait / terminal
  • 没有可 claim 的 todo

会话被留下:内容为空、标题是 goal objective、且 parentSessionId 缺失。而且永远无法自愈 —— 没有 entry 的会话不会出现在 list_sessions 里,桌面端由 session_created 推送建立的 stub(import_discovered_session)也就永远等不到后台 import 的修复。

实测:一个 32-worker 的 goal 产生了正好 32 个这样的空会话,每次被拒/提前退出的 run 一个 —— 包括 workspace guard 开始串行化同 cwd worker 池之后的每一次 relaunch。

改动

commit 内容
fix(loop) 当会话是本次调用新建、且从未执行过任何 turn 时,把它删掉。run_turns 现在回报 turn 是否真的到达过模型;跑过 turn 的会话仍然永不删除(retention 契约不变),retention 提示也不再宣传一个已被删除的 id。
fix(agent) get_state 原本 session_name 读内存、parent_session_id 读磁盘,于是「新建但尚未 prompt」的会话完全不含 parentSessionId 字段。改为内存优先、磁盘兜底(磁盘本就由同一字段写入,持久化后二者不可能不一致)。

验证

两个早期退出路径都加了断言(去掉修复即失败,报 unused session must be discarded, got {"mock-session-1"}):

  • run_workspace_conflict_and_force_workspace —— guard 拒绝后不得留下会话
  • run_monitor_not_due_waits —— wait 退出后不得留下会话

get_state 新增 get_state_reports_parent_before_it_is_committed_to_disk:去掉修复即失败,实际输出里根本没有 parentSessionId 键(与线上症状一致)。

现有契约测试 console_sweep 的「session must be retained after a completed run」仍然通过,确认跑过 turn 的会话不受影响。

cargo fmt -p future-loop -p future-agent --check   # clean
cargo clippy -p future-loop -p future-agent --all-targets -- -D warnings   # clean
cargo test -p future-loop        # 67 test binaries, 0 failed
cargo test -p future-agent       # 1714 lib tests (+ integration), 0 failed

未改动 packages/rpc 的线上契约形状(parentSessionId 本就是 optional 字段),改的只是它何时被填充。

`get_state` read `parent_session_id` from disk while `session_name` came
from memory. A session created via `new_session` (what `loop run` does)
knows its parent immediately but only commits it at its first run
boundary, so every freshly created, not-yet-prompted session reported no
`parentSessionId` at all.

Clients that bind lineage on the `session_created` push lost the parent
permanently: a session with no entries never reappears in `list_sessions`,
so the desktop's `import_discovered_session` stub — created from that very
`get_state` — can never be healed by the background import pass.

Prefer the in-memory value and keep disk as the durable fallback (it is
written from this same field, so the two cannot disagree once persisted).
`run` mints its agent session before the turn loop, but several first-turn
outcomes break out without ever prompting: the P0-1 workspace guard refusing
to race a peer (degrade-to-serial), a wait/terminal first decision, or no
claimable todo. The session was left behind empty and titled with the goal
objective, and nothing ever heals it — a session with no entries never
reappears in `list_sessions`, so the desktop stub created from the
`session_created` push (import_discovered_session) keeps the empty
conversation forever, un-healable by the background import pass.

Observed in production: one 32-worker goal produced exactly 32 such sessions,
one per refused or early-exit run — including every relaunch after the
workspace guard started serializing a same-cwd worker pool.

Discard the session when this invocation minted it and no turn ever executed.
`run_turns` now reports whether a turn actually reached the model; the
retention contract for sessions that *did* run is unchanged (still never
deleted), and the retention print no longer advertises a deleted id.

Tests: the two early-exit paths now assert the session is gone (both fail
without the fix, reporting the leaked `mock-session-1`), and the existing
"session must be retained after a completed run" contract still holds.
@huichen
huichen enabled auto-merge (squash) September 11, 2026 01:17
@huichen
huichen merged commit fc81c01 into main Sep 11, 2026
12 checks passed
@huichen
huichen deleted the claude/loop-orphan-session branch September 11, 2026 01:21
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