Repository navigation
fix(loop): discard an agent session that never reached a prompt - #553
Merged
Merged
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
loop run在进入 turn 循环之前就创建 agent session,但若干「第一轮直接退出」的路径从未 prompt:会话被留下:内容为空、标题是 goal objective、且
parentSessionId缺失。而且永远无法自愈 —— 没有 entry 的会话不会出现在list_sessions里,桌面端由session_created推送建立的 stub(import_discovered_session)也就永远等不到后台 import 的修复。实测:一个 32-worker 的 goal 产生了正好 32 个这样的空会话,每次被拒/提前退出的 run 一个 —— 包括 workspace guard 开始串行化同 cwd worker 池之后的每一次 relaunch。
改动
fix(loop)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 的会话不受影响。未改动
packages/rpc的线上契约形状(parentSessionId本就是 optional 字段),改的只是它何时被填充。