Repository navigation
fix(sessions): keep one agent's session cleanup and /stop from killing another agent's work - #161736
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs changes before merge. Reviewed September 30, 2026, 5:22 AM ET / 09:22 UTC (Revision 2). ClawSweeper reviewWhat this changesThe branch makes session deletion, reset, archive, and /stop select an agent’s reply runs and queued work by agent and session identity, with regression tests and session documentation. Merge readiness⛔ Needs changes before merge - 2 items remain Keep this PR open. Current main still clears session work by shared raw key, and the branch addresses the reported cross-agent interruption. The new Gateway regression file still needs the measured test cost required by repository policy. Priority: P1 Review scores
Verification
How this fits togetherGateway session actions and /stop enter cancellation handlers before a session changes. Those handlers inspect shared reply and command queues to decide which agent turns end and which continue. flowchart LR
A[Session action or stop] --> B[Select agent and session]
B --> C[Find reply runs]
B --> D[Find queued messages]
B --> E[Find queued commands]
C --> F[Cancel selected work]
D --> F
E --> F
F --> G[Other agents continue]
Before merge
Findings
Agent review detailsSecurityNone. PR surfaceSource +287, Tests +321, Docs +3. Total +611 across 27 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep cancellation selection in the queue and reply-run owners, preserve own-session cancellation, and handle the distinct embedded-run ID collision in #141379. Do we have a high-confidence way to reproduce the issue? Yes, from source: two agents with the same raw session key reach process-wide cleanup that current main performs by key alone. The PR reports before-fix failures, but this read-only review did not execute them. Is this the best way to solve the issue? Yes. Recording ownership when work enters shared queues and selecting the owning agent and session at cancellation is a focused repair that preserves lane scheduling. Full review comments:
Overall correctness: patch is correct AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 94ee9b54006b. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Match reply operations and queued follow-ups against the selected agent, request-key aliases, and session identity in both lifecycle cleanup stages. Preserve foreign work in shared raw-key queues while keeping own-session cancellation and owner settlement intact. Gateway-boundary regressions cover delete, reset, archive, global and ordinary key collisions, queued work, and own-session cancellation. Real isolated Gateway proof preserves main through repeated research deletion/reset and still aborts a deleted main-owned session. Validation: related unit and boundary suites, two session end-to-end flows, full builds, and all 27 core test-type graphs passed. check-changed stops at the inherited parseBoolean declaration in scripts/ci-build-manifest.mjs:101; neither that file nor its guard changes here.
Capture session ownership when embedded, CLI, and compaction commands enter their lanes. Count and remove only the selected agent and session incarnation, while preserving sibling commands sharing a raw key. Keep lane names and scheduling unchanged. Regression: seven raw-lane lifecycle cases fail before the fix; all 10 lifecycle cases and 18 queue cleanup tests pass on Blacksmith Testbox. Formatting verified remotely; local dependencies are absent.
Share the lifecycle reply-operation selector with both Stop paths. Resolve queued work and active runs by agent and session identity, and clear captured queues before signaling cancellation. Preserve the distinct whole-drain cleanup contract for other callers. Boundary regressions reproduce foreign queue loss in six cases and cross-agent active-run cancellation in both Stop entry points before the fix. All 174 focused and sibling tests pass on Blacksmith Testbox. Complete changed-file gates in stages, including runtime and test typechecks, lint, and lifecycle guards. Extract the abort queue fixture for the line-cap ratchet and correct earlier queue regression lint.
An aborted, unretained pre-backend reservation cleared its registry slot without settling its owner promise. The exact-owner lifecycle drain introduced in 196ad44 therefore timed out Incognito expiry after successfully cancelling the queued run. Complete the reservation through its existing owner while preserving retained execution and delivery barriers and all agent-scoped selection. Blacksmith proof: origin/main 84ce02b passed all 10 Incognito tests. Before the fix, head 1920e85 failed the expiry test and all three new reservation-settlement cases. After the fix, 49 focused tests passed; the expanded 218-file selection passed 3112 tests with two opt-in benchmarks skipped. All selected files were observed, including lifecycle caller grep matches and the existing scope and Stop regressions. check-changed passed, including runtime types, all 27 core test-type graphs, formatting, and zero-warning lint. Standalone costs with one worker: registry cancellation 13 tests in 2.80s wall; Incognito lifetime 10 tests in 38.87s wall. Staged secret scan passed. Production +1/-1 LOC; tests +39/-13 LOC.
1920e85 to
49ed430
Compare
|
Merged via squash.
|
…g another agent's work (openclaw#161736) * fix(sessions): isolate lifecycle cleanup by agent Match reply operations and queued follow-ups against the selected agent, request-key aliases, and session identity in both lifecycle cleanup stages. Preserve foreign work in shared raw-key queues while keeping own-session cancellation and owner settlement intact. Gateway-boundary regressions cover delete, reset, archive, global and ordinary key collisions, queued work, and own-session cancellation. Real isolated Gateway proof preserves main through repeated research deletion/reset and still aborts a deleted main-owned session. Validation: related unit and boundary suites, two session end-to-end flows, full builds, and all 27 core test-type graphs passed. check-changed stops at the inherited parseBoolean declaration in scripts/ci-build-manifest.mjs:101; neither that file nor its guard changes here. * fix(sessions): clear owned commands from shared session lanes Capture session ownership when embedded, CLI, and compaction commands enter their lanes. Count and remove only the selected agent and session incarnation, while preserving sibling commands sharing a raw key. Keep lane names and scheduling unchanged. Regression: seven raw-lane lifecycle cases fail before the fix; all 10 lifecycle cases and 18 queue cleanup tests pass on Blacksmith Testbox. Formatting verified remotely; local dependencies are absent. * fix(reply): scope stop cancellation to agent-owned sessions Share the lifecycle reply-operation selector with both Stop paths. Resolve queued work and active runs by agent and session identity, and clear captured queues before signaling cancellation. Preserve the distinct whole-drain cleanup contract for other callers. Boundary regressions reproduce foreign queue loss in six cases and cross-agent active-run cancellation in both Stop entry points before the fix. All 174 focused and sibling tests pass on Blacksmith Testbox. Complete changed-file gates in stages, including runtime and test typechecks, lint, and lifecycle guards. Extract the abort queue fixture for the line-cap ratchet and correct earlier queue regression lint. * fix(reply): settle aborted queued reservations An aborted, unretained pre-backend reservation cleared its registry slot without settling its owner promise. The exact-owner lifecycle drain introduced in 196ad44 therefore timed out Incognito expiry after successfully cancelling the queued run. Complete the reservation through its existing owner while preserving retained execution and delivery barriers and all agent-scoped selection. Blacksmith proof: origin/main 8454fca passed all 10 Incognito tests. Before the fix, head 1920e85 failed the expiry test and all three new reservation-settlement cases. After the fix, 49 focused tests passed; the expanded 218-file selection passed 3112 tests with two opt-in benchmarks skipped. All selected files were observed, including lifecycle caller grep matches and the existing scope and Stop regressions. check-changed passed, including runtime types, all 27 core test-type graphs, formatting, and zero-warning lint. Standalone costs with one worker: registry cancellation 13 tests in 2.80s wall; Incognito lifetime 10 tests in 38.87s wall. Staged secret scan passed. Production +1/-1 LOC; tests +39/-13 LOC. Co-authored-by: Peter Steinberger <[email protected]>
… module 8b94d33 (openclaw#161736) called resolveActiveReplyOperationForSessionId from reply-run-registry.registry.ts, but that name exists there only as an export alias (export { resolveReplyRunForCurrentSessionId as ... }), which creates no local binding, so pnpm tsgo:core fails on main with TS2552.
What Problem This Solves
One agent's session operations could destroy another agent's live work. With two agents (for example
mainandresearch) whose sessions share a raw session key such asglobal:/stop(both the fast abort path and the command stop path) did the same: main's queued follow-ups were deleted and main's run was aborted.The operator sees main's turn silently disappear mid-reply, with no error, because another agent touched its own session.
Why This Change Was Made
Root cause: follow-up queues, embedded session command lanes, and the reply-run registry are process-wide and keyed by raw session keys, while the cleanup paths matched on the key alone.
prepareSessionLifecycleDrain(src/gateway/server-methods/sessions-lifecycle-drain.ts) cancelled by raw key and then ran an unscopedclearSessionQueues;commands-session-abort.tsandabort-operation.tsdid the same for/stop.The fix makes ownership explicit where the work is produced and matched:
Follow-ups:
clearSessionLifecycleQueues/hasSessionLifecycleQueueWork(src/auto-reply/reply/queue/cleanup.ts) match agent, request-key aliases, and session identity, and are used for both cleanup stages and for authoritative-work detection.Command lanes: queue entries now carry an optional
sessionTargetcaptured at enqueue by the embedded, CLI, and compaction producers.clearCommandLane(lane, matches)andcountQueuedCommandsInLaneremove/count only the selected agent's entries in shared raw lanes. Lane names and scheduling are unchanged.Reply runs: one selector in
reply-run-registry.registry.tsresolves the exact agent-owned operations; lifecycle drain and both stop paths abort and await settlement only for those.Owner settlement: aborting an unretained pre-backend reply reservation cleared its registry slot but never settled its owner promise (
reply-run-registry.operation.ts). The old drain waited on the slot and never noticed; the exact-owner drain waits on owner settlement, so Incognito expiry timed out after cancelling the run. The abort now completes the reservation through its existingcomplete()(production +1/−1).Own-session behavior is preserved: main deleting/resetting/stopping its own
globalsession still cancels its run and clears its queued follow-ups and lane commands (including the default agent's raw lanes, which an independent review flagged on the first iteration and is now covered).Production LOC: roughly +290 net across the three owner surfaces. This is the ownership record the shared queues were missing, not a guard layered on top; the stop paths reuse the lifecycle selector instead of a parallel matcher.
Not covered here (tracked as a follow-up): a few other callers of the generic raw-key
clearSessionQueues(provider review, worker placement, rewind, fallback session abort) can still pass raw aliases. The generic API is kept because those paths intentionally tear down a whole drain; each needs its own reproduction.User Impact
In multi-agent setups with shared session keys (notably
session.scope: global), deleting, resetting, archiving, or stopping one agent's session no longer kills or drops another agent's in-flight reply, queued follow-ups, or queued commands.Evidence
sessions-lifecycle-agent-scope.test.ts10/10, stop owner-boundary tests 16/16; 174/174 across 14 related files; on Blacksmith Testbox the related suites pass (9 files / 59 tests plus queue and command-queue shards, 5 shards green).server.incognito-lifetime.test.tspasses 10/10 onorigin/main(negative control), failed on the pre-settlement head, and passes 10/10 after; three new reservation-settlement cases fail before and pass after. Expanded 218-file lifecycle selection (every caller of delete/reset/archive drains): 3,112 passed, two opt-in benchmarks skipped.check-changedpasses; all 27 core test-type graphs pass; Codex autoreview on the full branch: no accepted findings.