Repository navigation
fix(agents): persist terminal session state after force-clearing stuck embedded runs - #112166
Conversation
|
Codex review: needs maintainer review before merge. Reviewed July 27, 2026, 6:46 AM ET / 10:46 UTC. ClawSweeper reviewWhat this changesThe PR records a Merge readinessThis PR remains necessary: it addresses a concrete stale-session recovery gap and the latest branch preserves both ownership guards requested in earlier review cycles. The supplied terminal proof covers the new persistence and recovery cases, but the change writes terminal metadata into persisted session state, so it should remain with the assigned reviewer for normal merge review rather than be auto-closed or routed to an automated repair. Priority: P1 Review scores
Verification
How this fits togetherEmbedded-agent recovery handles runs that stop making progress after a provider or execution stall. It clears active run registries and now also updates the matching persisted session entry so the next inbound message can take the terminal-state recovery path instead of treating a dead run as active. flowchart LR
A[Provider or agent stall] --> B[Heartbeat or cron recovery]
B --> C[Capture current embedded run and reply operation]
C --> D[Abort and drain recovery]
D --> E[Persist matching session as killed]
E --> F[Terminal session entry]
F --> G[Next inbound message]
G --> H[Fast recovery admission]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +118, Tests +376. Total +494 across 7 files. View PR surface stats
Review metricsNone. Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Land the bounded recovery fix after the assigned reviewer confirms that Do we have a high-confidence way to reproduce the issue? No live current-main failure was run in this read-only review, but the source-level path is concrete and the PR supplies focused regression tests for force-clear persistence, replacement-session safety, and terminal-state admission recovery. Is this the best way to solve the issue? Yes. Persisting the existing terminal representation at the shared force-clear boundary is narrower and safer than adding separate heartbeat and cron repair paths, provided the atomic ownership predicates remain in place. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 91b1f9676f9e. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (4 earlier review cycles)
|
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
Maintainer confirmation: heartbeat recovery and cron timeout force-clears represent the same terminal outcome ( |
|
Merged via squash.
|
…k embedded runs (openclaw#112166) * fix(agents): persist terminal session state after force-clearing stuck embedded runs * test(agents): use auto-cleanup temp dir tracker in force-clear test * fix(agents): guard force-clear terminal-state write with session ID * fix(agents): make force-clear session-ID guard atomic * fix(agents): bind force-clear to current run owners --------- Co-authored-by: Ayaan Zaidi <[email protected]>
What Problem This Solves
Fixes an issue where a stuck embedded-agent run that was force-cleared by heartbeat recovery (or cron timeout cleanup) left the persisted session entry in a non-terminal state. The next incoming message could not use the existing fast-recovery path in
dispatch-from-config.lifecycle.ts, so a subsequent provider stall would again wait for the full heartbeat timeout instead of being reclaimed immediately. It also causedopenclaw sessionsto show stale or unknown token/runtime state for the affected session.Why This Change Was Made
The force-clear path (
forceClearEmbeddedAgentRun) only cleaned up in-memory registries (ACTIVE_EMBEDDED_RUNS, reply-run registry) and never wrote a terminal snapshot to the SQLite session entry. Normal run termination emits a lifecycle event that persistsstatus,abortedLastRun,startedAt,endedAt,runtimeMs, etc.; force-clear skipped this entirely.This change centralizes the terminal-state persistence inside
abortAndDrainEmbeddedAgentRun: after a successful force-clear, it patches the session entry tostatus: "killed",abortedLastRun: true, and the corresponding ended/runtime timestamps. Both existing callers—heartbeat stuck-session recovery and cron timeout cleanup—benefit automatically.The persistence helper guards against overwriting a newer session entry that has claimed the same session key: the
sessionIdcheck runs inside theupdateSessionEntrycallback, which executes under the store's serialized write lock, so the compare-and-write is atomic—a replacement session cannot interleave.User Impact
killed) inopenclaw sessionsand other session listings.Evidence
src/agents/embedded-agent-runner/runs.force-clear-terminal.test.tsstatus: "killed",abortedLastRun: true,endedAt, andruntimeMs.sessionKeyis omitted.src/auto-reply/reply/dispatch-from-config.stale-recovery.test.tsReal behavior proof
Lint and format:
Revision log
loadSessionEntrycheck beforeupdateSessionEntry.updateSessionEntrycallback, making the compare-and-write atomic under the store's serialized write lock. Eliminates the TOCTOU race where a replacement session could interleave between the separate load and update.