Repository navigation
fix: let finished background commands reach the agent before an active goal advances - #204265
Conversation
Closes #204187. - Add src/sessions/owed.ts: owe() stores an exec result in session_pending_inputs (kind as additive JSON); shouldWake() reads stored rows. - The exec wake turn adopts its owed row by id; the transcript append consumes it once. - Goal continuation does not advance while an owed result is pending. - Restart recovery wakes owed rows from stored state; a failed wake wakes once more from the row. - Remove the in-memory pendingExitWakes set. Co-authored-by: obviyus <[email protected]>
- A restart reads the owed kind from the stored row; an adopted row now keeps it. Co-authored-by: obviyus <[email protected]>
|
🦞👀 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. |
- A wake from stored state settles itself, including after restart recovery. - The live retry keeps the request owners and takes over the removal handle. - shouldWake follows every queued-input page. Co-authored-by: obviyus <[email protected]>
|
Codex review: blocked before merge. What this changesThis PR persists background command results so completion turns can precede Goal continuation and survive Gateway restarts. Example: Command build-42 exits with code 0 while its conversation has an active Goal.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Partial Merge readiness⛔ Blocked before merge - 7 items remain Keep this PR open: it addresses a verified ordering bug, but completion recovery has correctness and authority defects that block merging. Priority: P2 Before merge
Findings
Tests
Agent review detailsHow this fits togetherBackground exec exits enter the session-event queue; pending-input storage retains their results, and Goal continuation consults that storage before scheduling another agent turn. flowchart TD
A[Background command exits] --> B[Pending input store]
B --> C[Session event admission]
C --> D[Agent completion turn]
D --> E[Transcript consumption]
B --> F[Goal continuation check]
E --> F
Technical reviewBest possible solution: Use the existing pending-input and session-event owners to preserve complete result payloads and captured policy through persistence, retries, consumption, and recovery. Do we have a high-confidence way to reproduce the issue? Current main exposes the reported claim-to-mailbox admission window, and the PR adds a deterministic boundary regression; this read-only review executed no target code. Is this the best way to solve the issue? Persisting results in the existing pending-input store fits the reported failure, but recovery must consume authoritative stored payloads while retaining the original admission restrictions. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 8eb7c5628a14. Provenance checked
TestingProof path: in-process harness. SecurityNeeds attention: The new live retry can remove a captured producer tool restriction before starting another agent turn. EvidenceSecurity concerns:
What I checked:
Likely related people:
PR surfaceSource +352, Tests +129, Docs +1. Total +482 across 14 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 23, 2026, 6:18 PM ET / 23:18 UTC. |
- A retry reads the stored row, which carries every folded result. Co-authored-by: obviyus <[email protected]>
- Admission revalidates the requester's authority and store on the retry. Co-authored-by: obviyus <[email protected]>
…queue - A refused enqueue or retry settles the stored row instead of leaving it owed with no wake. Co-authored-by: obviyus <[email protected]>
|
Rejected review findings:
|
- Captured targets always carry a store; owed-result storage keeps its own owner suite. Co-authored-by: obviyus <[email protected]>
|
Rejected review findings:
|
- Both pending-input kernels restage an owed row with its new bytes; ordinary rows keep theirs. Co-authored-by: obviyus <[email protected]>
- Delete enqueueExecExitNotification; the live wake, its retry and restart recovery use wakeOwedExec. - The owed row stores originFacts (requester, command owner, operator profile) as additive JSON; restage keeps it. Co-authored-by: obviyus <[email protected]>
|
Maintainer override: the CI reds on head ci-triage on that head:
|
|
Rejected review findings (final head
|
- A pending-input stage can consume absorbed owed rows in the same transaction (SQLite and memory kernels). - The batch flush folds members into the first row before the shared turn; a restart replays one row, so each result is delivered once. - Retries read the stored row's text. Co-authored-by: obviyus <[email protected]>
- No member is double-carried: each retries its own stored row. Both refusal paths share refuseSharedTurn. Co-authored-by: obviyus <[email protected]>
|
Rejected review findings (final head
The earlier P2 (shared-turn rows consumed separately) is fixed on this head: |
|
Maintainer override: the CI reds on head ci-triage:
|
|
Landed as 463d774. A finished background command's result is now stored in the conversation when the command exits (owed row in session_pending_inputs, no schema change). An active Goal waits for it, its turn appends it once, and a Gateway restart before that turn still delivers it exactly once. Proof: a deterministic race test (red on the merge base, green on head), 20/20 full-file runs of server.chat-goal.test.ts, and real-Gateway runs showing Exec completed before Advance and a single append after restart. The Advance stall after completion turns was a separate bug, fixed in #204327. |
…toring them fails The exec completion runs in the async context of the turn that started the command. An incognito turn's session-actor binding and work signal end with that turn, so the owed row write (and, before #204265, the live wake) failed and the result was only logged. Store and wake outside that context. If owe() still fails, wake the chat once live. Key the durable restart notice test by the adopted process instance, and declare the exec completion tests for src/sessions/owed.ts in the changed-test planner, which missed them for #204265. Co-authored-by: obviyus <[email protected]>
…toring them fails (#204532) ## Problem 1. **Incognito chats never get a background command's completion turn.** On a scratch Gateway with a real model, an incognito chat runs a background command, replies, and then nothing happens when the command finishes. The completion runs in the async context of the turn that started the command. For an incognito chat, that context holds the turn's session-actor binding and work signal, and both end with the turn. - Before #204265 (`463d7748^`): the live wake fails with `SessionDeliveryGenerationUnavailableError`. - On `main`: `owe()` fails first, and the Gateway logs `[exec] completion was not admitted (Chat session storage admission ended)`. So this predates #204265. #204265 only moved the failure point. 2. **A failed `owe()` drops the result.** Since #204265, `notifyOnExit` stores the result (`owe()`) before it wakes the chat, and its catch only logs a failed `owe()`. Any store failure (busy database, store fence) silently drops a produced result. 3. **Five exec-notification test files went red on scheduled `main`** (run 45348156095); #204507 fixed four. #204265's PR CI did not select them. It changed `src/sessions/owed.ts`, and these tests live in `src/agents/`, past the two-import walk. This is the cross-directory miss the #204346 closing loop covers. ## Solution - `notifyOnExit` stores and wakes outside the producing turn's session-actor binding and work scope (`runWithoutSessionActorStorage`, `runOutsideAsyncWorkScope`). Both are existing helpers for independent work. The completion acquires its own session actor, so incognito chats store the owed row and get their completion turn. - If `owe()` still fails, the completion wakes its chat once through the same live wake, without a stored row. That one result loses restart recovery, and the Gateway logs why. It is never only logged. - #204507 already moved four of the five red files to the owed-row contract (owed-id context keys). The fifth, `bash-process-durable.test.ts`, still counted restart notices by the old slug key `exec:<slug>`; its helper now resolves the adopted process instance (`processSessionInstanceId`). - New `bash-tools.exec-notify.store-failure.test.ts`: `owe()` rejects, and the completion still wakes its chat once. - #204346 closing loop: `src/sessions/owed.ts` is now an owner in `scripts/lib/ci-changed-node-test-plan.mts`. A change to it selects a declared set: the 5 test files and the new store-failure test. A planner test checks that selection. ## Impact - Fixes a pre-existing loss: incognito exec results were also lost before #204265 (baseline b8c95a3 repro). On the measured production deployment: 0 events in the retained ~3.8-day window. - Incognito chats get background command completion turns. - A produced exec result is never only logged when its row cannot be stored. - Ordinary chats: no change (Goal ordering and restart exactly-once re-proven below). - Production: 1 file, +27 −7. ## Evidence - **Incognito, real Gateway** (scratch state and ports, real model; the chat runs a background command, replies `STARTED`, then must answer the completion): - before #204265 (`463d7748^`): no completion turn; `[exec] completion delivery failed (SessionDeliveryGenerationUnavailableError …)`. - `main`: no completion turn; `[exec] completion was not admitted (Chat session storage admission ended)`. - this PR: the completion turn runs (`SEEN_…` reply); no exec warnings, so the owed row was stored. - **Ordinary chats, real Gateway, this PR:** `Exec completed` precedes the Goal's Advance turn and is appended once; a result pending across a Gateway restart is appended exactly once after it (4/4 checks). - **Store failure:** `bash-tools.exec-notify.store-failure.test.ts` fails on `main` (the wake is called 0 times) and passes here. - **Tests (rebased on `main`):** the 5 files plus exec background follow-up, requester, queue-full, yield sweep and planner integration: all pass, except that locally on macOS `bash-process-durable.test.ts` stops at the Darwin ACL guard in `@openclaw/fs-safe` before its assertion (CI on Linux runs it). `server.chat-goal.test.ts` 72/72. `pnpm tsgo:core` passes. - **Planner:** `ci-changed-node-test-plan.integration.test.ts` checks that a change to `src/sessions/owed.ts` selects the 6 declared tests. Co-authored-by: Ayaan Zaidi <[email protected]>
…e goal advances (#204265) Closes #204187 ## What Problem This Solves With an active Goal, a background command that finishes during a turn can lose its place to the Goal's automatic "Advance the active goal" turn. The agent then advances the Goal before it has read the command result. A Gateway restart between a command's exit and its completion turn also loses the result, because the result exists only in memory. ## User Impact User impact: a finished background command always reaches the agent before an active Goal advances, and a Gateway restart no longer loses a command result that was waiting for its turn. - After a restart, the completion turn runs with the session principal's current authority. The original channel owner's owner-only tools do not carry across the restart; durable outcomes already behave this way. - Completions that arrive while an earlier completion turn runs still share one turn (#199113). ## Why This Change Was Made The exec result becomes stored state when the command exits (first slice of the owed-result model): - New `src/sessions/owed.ts`. `owe(item)` is the only writer of new owed rows. It stores the result in the existing `session_pending_inputs` table, with `kind: "exec"` and `originFacts` (requester, command owner, operator profile id) as additive JSON in the row message. There is no schema change. `shouldWake(chat)` reads stored rows: it is true while the chat has owed rows that no live turn holds. Consumption does not read `originFacts` yet; a later slice intersects it with current policy without a migration. - The exec wake turn adopts its owed row by id (the turn's input idempotency key) and writes its own bytes into it. Before a shared batch turn starts, `fold` writes the joined text into the first member's row and consumes the other members' rows in one transaction, so a restart at any point replays each result once. If the fold fails, the shared turn is refused and each member retries its own row. The transcript append consumes the row in the same transaction, so a second consumer or a retry after restart appends nothing. - Goal continuation calls `shouldWake` and does not advance while an owed result is pending. The completion turn runs first, and its end continues the Goal. - One wake path, `wakeOwedExec`, serves the live wake, its single retry and restart recovery. A wake that fails before it becomes a turn, or is refused at enqueue, retries once from the stored row; the live retry keeps the captured target, so admission revalidates the requester's authority. A stop, or a second failure, ends the row with a visible cause. Restart recovery wakes owed rows, which it used to refuse as queued human input. | Part | Files | Lines | |---|---|---| | Deleted in-memory exec path | `bash-tools.exec-notify.ts`: the `pendingExitWakes` set and its reads, `enqueueExecExitNotification`, and the separate settle/log block (`hasExecYieldWake` and the yield sweep now read stored rows) | −46 | | Shrunk to stay under the line cap | `session-accessor.pending-inputs.ts`: two duplicated "already consumed" receipts become `consumedPendingInputReceipt` (moved to `session-pending-input-value.ts`) | +26 −33 | | Owed primitive | `sessions/owed.ts` (`owe`, `fold`, `shouldWake`, `readPendingOwed`, `withhold`: +200); `session-pending-input-value.ts` (`readOwedKind`, owed restage, moved receipt: +44); owed-row adoption in `session-accessor.pending-input-request.ts` (+18 −2); absorbed-row consumption in both pending-input kernels and the mutation type (+32) | +294 −2 | | Glue | `bash-tools.exec-notify.ts` (`wakeOwedExec`, settlement, owed row with origin facts: +104); `session-event-batch.ts` (fold before a shared turn, one refusal path: +68 −15); `queued-chat-inputs.ts` restart wake (+22 −1); `goal-continuation.ts` (+11); `session-yield-sweep.ts` (+4 −1) | +209 −17 | | **Production total** | 12 files | **+529 −98 (net +431)** | No parallel in-memory exec-completion path is left. Every exec completion, live or after restart, is an owed row delivered by `wakeOwedExec`. What remains in memory is the shared session-event admission that runs the turn for every producer (cron, hooks, plugins). Its occurrence still carries a copy of the text; that copy can go when session-event admission reads stored rows for all producers, in later owed-result slices. The cron yield-resume path (`execYieldResumes`) is a different consumer and stays out of this slice. ## Evidence - **Race, deterministic:** a new `server.chat-goal.test.ts` case holds the real exec wake in target preparation until the first turn's Goal check runs. It is red on the merge base (Advance overtakes the completion) and green on head. - **No deadlock:** a new case makes the first wake fail in preparation; the stored row wakes the completion turn and the Goal then advances. The existing "keeps pursuing after … exec completion" cases end every completion turn before a model call; they stay green. - **Shared turn:** a new case refuses a shared completion turn before admission; both results are still delivered afterwards. A storage case shows `fold` leaves exactly one row carrying both results; it is red when the absorbed rows are not consumed (both rows remain, so a restart would deliver the second result twice). - **Storage:** a new pending-input case is red without the kind-preserving restage (restart recovery would refuse an adopted row) and green on head. - **Touched suites:** exec background follow-up (28/28, shared-turn cases included), yield sweep (6/6), and the pending-input, queued-input, Goal continuation, session-event and restart-resume suites (19 files, 284 tests). - **Real Gateway, scratch port and state, real model:** an exec result pending in `session_pending_inputs` across a Gateway restart is appended to the transcript exactly once after the restart (5 runs). On head `0abfb478`, which differs from the final head only in the batch's fold-failure path, the same run also shows `Exec completed` before `[System] Advance the active goal` (with #204327 merged). - **Repeat runs (final head `4b887e10`, frozen copy):** `server.chat-goal.test.ts` as a whole file, 20 separate runs: 20/20 passed with 72/72 each (1,440 test executions). Both "keeps pursuing after … takes the next turn" variants passed in every run (40/40). Separate bug, fixed and merged in #204327 (issue #204319): on a real Gateway, a Goal did not advance after a background command's completion turn. Post-turn session maintenance waited in the mailbox and the Goal check deferred to it. This happens on `main` too. A plain Goal advances normally on `main`; an earlier note here that it never advanced came from a bad test prompt. With #204327 the real-Gateway transcript reads: Goal request → `Exec completed …` → `[System] Advance the active goal …`. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 463d774) Co-authored-by: RomneyDa <[email protected]>
…toring them fails (#204532) 1. **Incognito chats never get a background command's completion turn.** On a scratch Gateway with a real model, an incognito chat runs a background command, replies, and then nothing happens when the command finishes. The completion runs in the async context of the turn that started the command. For an incognito chat, that context holds the turn's session-actor binding and work signal, and both end with the turn. - Before #204265 (`463d7748^`): the live wake fails with `SessionDeliveryGenerationUnavailableError`. - On `main`: `owe()` fails first, and the Gateway logs `[exec] completion was not admitted (Chat session storage admission ended)`. So this predates #204265. #204265 only moved the failure point. 2. **A failed `owe()` drops the result.** Since #204265, `notifyOnExit` stores the result (`owe()`) before it wakes the chat, and its catch only logs a failed `owe()`. Any store failure (busy database, store fence) silently drops a produced result. 3. **Five exec-notification test files went red on scheduled `main`** (run 45348156095); #204507 fixed four. #204265's PR CI did not select them. It changed `src/sessions/owed.ts`, and these tests live in `src/agents/`, past the two-import walk. This is the cross-directory miss the #204346 closing loop covers. - `notifyOnExit` stores and wakes outside the producing turn's session-actor binding and work scope (`runWithoutSessionActorStorage`, `runOutsideAsyncWorkScope`). Both are existing helpers for independent work. The completion acquires its own session actor, so incognito chats store the owed row and get their completion turn. - If `owe()` still fails, the completion wakes its chat once through the same live wake, without a stored row. That one result loses restart recovery, and the Gateway logs why. It is never only logged. - #204507 already moved four of the five red files to the owed-row contract (owed-id context keys). The fifth, `bash-process-durable.test.ts`, still counted restart notices by the old slug key `exec:<slug>`; its helper now resolves the adopted process instance (`processSessionInstanceId`). - New `bash-tools.exec-notify.store-failure.test.ts`: `owe()` rejects, and the completion still wakes its chat once. - #204346 closing loop: `src/sessions/owed.ts` is now an owner in `scripts/lib/ci-changed-node-test-plan.mts`. A change to it selects a declared set: the 5 test files and the new store-failure test. A planner test checks that selection. - Fixes a pre-existing loss: incognito exec results were also lost before #204265 (baseline b8c95a3 repro). On the measured production deployment: 0 events in the retained ~3.8-day window. - Incognito chats get background command completion turns. - A produced exec result is never only logged when its row cannot be stored. - Ordinary chats: no change (Goal ordering and restart exactly-once re-proven below). - Production: 1 file, +27 −7. - **Incognito, real Gateway** (scratch state and ports, real model; the chat runs a background command, replies `STARTED`, then must answer the completion): - before #204265 (`463d7748^`): no completion turn; `[exec] completion delivery failed (SessionDeliveryGenerationUnavailableError …)`. - `main`: no completion turn; `[exec] completion was not admitted (Chat session storage admission ended)`. - this PR: the completion turn runs (`SEEN_…` reply); no exec warnings, so the owed row was stored. - **Ordinary chats, real Gateway, this PR:** `Exec completed` precedes the Goal's Advance turn and is appended once; a result pending across a Gateway restart is appended exactly once after it (4/4 checks). - **Store failure:** `bash-tools.exec-notify.store-failure.test.ts` fails on `main` (the wake is called 0 times) and passes here. - **Tests (rebased on `main`):** the 5 files plus exec background follow-up, requester, queue-full, yield sweep and planner integration: all pass, except that locally on macOS `bash-process-durable.test.ts` stops at the Darwin ACL guard in `@openclaw/fs-safe` before its assertion (CI on Linux runs it). `server.chat-goal.test.ts` 72/72. `pnpm tsgo:core` passes. - **Planner:** `ci-changed-node-test-plan.integration.test.ts` checks that a change to `src/sessions/owed.ts` selects the 6 declared tests. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 0164469) Co-authored-by: RomneyDa <[email protected]>
…e goal advances (openclaw#204265) Closes openclaw#204187 ## What Problem This Solves With an active Goal, a background command that finishes during a turn can lose its place to the Goal's automatic "Advance the active goal" turn. The agent then advances the Goal before it has read the command result. A Gateway restart between a command's exit and its completion turn also loses the result, because the result exists only in memory. ## User Impact User impact: a finished background command always reaches the agent before an active Goal advances, and a Gateway restart no longer loses a command result that was waiting for its turn. - After a restart, the completion turn runs with the session principal's current authority. The original channel owner's owner-only tools do not carry across the restart; durable outcomes already behave this way. - Completions that arrive while an earlier completion turn runs still share one turn (openclaw#199113). ## Why This Change Was Made The exec result becomes stored state when the command exits (first slice of the owed-result model): - New `src/sessions/owed.ts`. `owe(item)` is the only writer of new owed rows. It stores the result in the existing `session_pending_inputs` table, with `kind: "exec"` and `originFacts` (requester, command owner, operator profile id) as additive JSON in the row message. There is no schema change. `shouldWake(chat)` reads stored rows: it is true while the chat has owed rows that no live turn holds. Consumption does not read `originFacts` yet; a later slice intersects it with current policy without a migration. - The exec wake turn adopts its owed row by id (the turn's input idempotency key) and writes its own bytes into it. Before a shared batch turn starts, `fold` writes the joined text into the first member's row and consumes the other members' rows in one transaction, so a restart at any point replays each result once. If the fold fails, the shared turn is refused and each member retries its own row. The transcript append consumes the row in the same transaction, so a second consumer or a retry after restart appends nothing. - Goal continuation calls `shouldWake` and does not advance while an owed result is pending. The completion turn runs first, and its end continues the Goal. - One wake path, `wakeOwedExec`, serves the live wake, its single retry and restart recovery. A wake that fails before it becomes a turn, or is refused at enqueue, retries once from the stored row; the live retry keeps the captured target, so admission revalidates the requester's authority. A stop, or a second failure, ends the row with a visible cause. Restart recovery wakes owed rows, which it used to refuse as queued human input. | Part | Files | Lines | |---|---|---| | Deleted in-memory exec path | `bash-tools.exec-notify.ts`: the `pendingExitWakes` set and its reads, `enqueueExecExitNotification`, and the separate settle/log block (`hasExecYieldWake` and the yield sweep now read stored rows) | −46 | | Shrunk to stay under the line cap | `session-accessor.pending-inputs.ts`: two duplicated "already consumed" receipts become `consumedPendingInputReceipt` (moved to `session-pending-input-value.ts`) | +26 −33 | | Owed primitive | `sessions/owed.ts` (`owe`, `fold`, `shouldWake`, `readPendingOwed`, `withhold`: +200); `session-pending-input-value.ts` (`readOwedKind`, owed restage, moved receipt: +44); owed-row adoption in `session-accessor.pending-input-request.ts` (+18 −2); absorbed-row consumption in both pending-input kernels and the mutation type (+32) | +294 −2 | | Glue | `bash-tools.exec-notify.ts` (`wakeOwedExec`, settlement, owed row with origin facts: +104); `session-event-batch.ts` (fold before a shared turn, one refusal path: +68 −15); `queued-chat-inputs.ts` restart wake (+22 −1); `goal-continuation.ts` (+11); `session-yield-sweep.ts` (+4 −1) | +209 −17 | | **Production total** | 12 files | **+529 −98 (net +431)** | No parallel in-memory exec-completion path is left. Every exec completion, live or after restart, is an owed row delivered by `wakeOwedExec`. What remains in memory is the shared session-event admission that runs the turn for every producer (cron, hooks, plugins). Its occurrence still carries a copy of the text; that copy can go when session-event admission reads stored rows for all producers, in later owed-result slices. The cron yield-resume path (`execYieldResumes`) is a different consumer and stays out of this slice. ## Evidence - **Race, deterministic:** a new `server.chat-goal.test.ts` case holds the real exec wake in target preparation until the first turn's Goal check runs. It is red on the merge base (Advance overtakes the completion) and green on head. - **No deadlock:** a new case makes the first wake fail in preparation; the stored row wakes the completion turn and the Goal then advances. The existing "keeps pursuing after … exec completion" cases end every completion turn before a model call; they stay green. - **Shared turn:** a new case refuses a shared completion turn before admission; both results are still delivered afterwards. A storage case shows `fold` leaves exactly one row carrying both results; it is red when the absorbed rows are not consumed (both rows remain, so a restart would deliver the second result twice). - **Storage:** a new pending-input case is red without the kind-preserving restage (restart recovery would refuse an adopted row) and green on head. - **Touched suites:** exec background follow-up (28/28, shared-turn cases included), yield sweep (6/6), and the pending-input, queued-input, Goal continuation, session-event and restart-resume suites (19 files, 284 tests). - **Real Gateway, scratch port and state, real model:** an exec result pending in `session_pending_inputs` across a Gateway restart is appended to the transcript exactly once after the restart (5 runs). On head `0abfb478`, which differs from the final head only in the batch's fold-failure path, the same run also shows `Exec completed` before `[System] Advance the active goal` (with openclaw#204327 merged). - **Repeat runs (final head `4b887e10`, frozen copy):** `server.chat-goal.test.ts` as a whole file, 20 separate runs: 20/20 passed with 72/72 each (1,440 test executions). Both "keeps pursuing after … takes the next turn" variants passed in every run (40/40). Separate bug, fixed and merged in openclaw#204327 (issue openclaw#204319): on a real Gateway, a Goal did not advance after a background command's completion turn. Post-turn session maintenance waited in the mailbox and the Goal check deferred to it. This happens on `main` too. A plain Goal advances normally on `main`; an earlier note here that it never advanced came from a bad test prompt. With openclaw#204327 the real-Gateway transcript reads: Goal request → `Exec completed …` → `[System] Advance the active goal …`. Co-authored-by: Ayaan Zaidi <[email protected]>
…toring them fails (openclaw#204532) ## Problem 1. **Incognito chats never get a background command's completion turn.** On a scratch Gateway with a real model, an incognito chat runs a background command, replies, and then nothing happens when the command finishes. The completion runs in the async context of the turn that started the command. For an incognito chat, that context holds the turn's session-actor binding and work signal, and both end with the turn. - Before openclaw#204265 (`7a8c93cd^`): the live wake fails with `SessionDeliveryGenerationUnavailableError`. - On `main`: `owe()` fails first, and the Gateway logs `[exec] completion was not admitted (Chat session storage admission ended)`. So this predates openclaw#204265. openclaw#204265 only moved the failure point. 2. **A failed `owe()` drops the result.** Since openclaw#204265, `notifyOnExit` stores the result (`owe()`) before it wakes the chat, and its catch only logs a failed `owe()`. Any store failure (busy database, store fence) silently drops a produced result. 3. **Five exec-notification test files went red on scheduled `main`** (run 45348156095); openclaw#204507 fixed four. openclaw#204265's PR CI did not select them. It changed `src/sessions/owed.ts`, and these tests live in `src/agents/`, past the two-import walk. This is the cross-directory miss the openclaw#204346 closing loop covers. ## Solution - `notifyOnExit` stores and wakes outside the producing turn's session-actor binding and work scope (`runWithoutSessionActorStorage`, `runOutsideAsyncWorkScope`). Both are existing helpers for independent work. The completion acquires its own session actor, so incognito chats store the owed row and get their completion turn. - If `owe()` still fails, the completion wakes its chat once through the same live wake, without a stored row. That one result loses restart recovery, and the Gateway logs why. It is never only logged. - openclaw#204507 already moved four of the five red files to the owed-row contract (owed-id context keys). The fifth, `bash-process-durable.test.ts`, still counted restart notices by the old slug key `exec:<slug>`; its helper now resolves the adopted process instance (`processSessionInstanceId`). - New `bash-tools.exec-notify.store-failure.test.ts`: `owe()` rejects, and the completion still wakes its chat once. - openclaw#204346 closing loop: `src/sessions/owed.ts` is now an owner in `scripts/lib/ci-changed-node-test-plan.mts`. A change to it selects a declared set: the 5 test files and the new store-failure test. A planner test checks that selection. ## Impact - Fixes a pre-existing loss: incognito exec results were also lost before openclaw#204265 (baseline e5607f8 repro). On the measured production deployment: 0 events in the retained ~3.8-day window. - Incognito chats get background command completion turns. - A produced exec result is never only logged when its row cannot be stored. - Ordinary chats: no change (Goal ordering and restart exactly-once re-proven below). - Production: 1 file, +27 −7. ## Evidence - **Incognito, real Gateway** (scratch state and ports, real model; the chat runs a background command, replies `STARTED`, then must answer the completion): - before openclaw#204265 (`7a8c93cd^`): no completion turn; `[exec] completion delivery failed (SessionDeliveryGenerationUnavailableError …)`. - `main`: no completion turn; `[exec] completion was not admitted (Chat session storage admission ended)`. - this PR: the completion turn runs (`SEEN_…` reply); no exec warnings, so the owed row was stored. - **Ordinary chats, real Gateway, this PR:** `Exec completed` precedes the Goal's Advance turn and is appended once; a result pending across a Gateway restart is appended exactly once after it (4/4 checks). - **Store failure:** `bash-tools.exec-notify.store-failure.test.ts` fails on `main` (the wake is called 0 times) and passes here. - **Tests (rebased on `main`):** the 5 files plus exec background follow-up, requester, queue-full, yield sweep and planner integration: all pass, except that locally on macOS `bash-process-durable.test.ts` stops at the Darwin ACL guard in `@openclaw/fs-safe` before its assertion (CI on Linux runs it). `server.chat-goal.test.ts` 72/72. `pnpm tsgo:core` passes. - **Planner:** `ci-changed-node-test-plan.integration.test.ts` checks that a change to `src/sessions/owed.ts` selects the 6 declared tests. Co-authored-by: Ayaan Zaidi <[email protected]>
…elded session (#204801) ## Problem `src/gateway/session-yield-sweep.test.ts` > "settles only an unreachable parked lifecycle (exec-completion)" fails intermittently on scheduled `main` (run 45403387860): `expected undefined to be 'failed'` at the final status check. Since #204265, the yield sweep keeps a yielded session waiting while it has a stored owed exec result (`shouldWake`, `session-yield-sweep.ts:45`). When the exec completion's wake settles as `cancelled`, `settleOwedExecWake` withholds the owed row asynchronously: `readPendingOwed` and then `withhold` on the database worker. The test resolved the wake and advanced the fake clock straight away. When the sweep tick ran before `withhold` committed, the sweep still saw the pending row and correctly left the session waiting, so the status stayed unset. ## Solution Test-only change. After the wake settles, the test waits until `shouldWake` (the stored-row check the sweep itself uses) reports no owed result, then advances the clock. Product behavior is unchanged: the next sweep tick settles the session. #204609 and #204621 do not touch this path. #204609 changes only `session-restart-resume-custody.ts`, which only the restart path imports. #204621 changes reply-operation backend readiness, which none of these modules use; the test's wake is mocked. ## Impact No change to production code or user behavior. ## Evidence - Before: intermittent red on scheduled `main` with the assertion above: run 45403387860, and again in run 45849028735 (2026-11-26, `checks-node-compact-large-14`, after #205835), `expected undefined to be 'failed'` in the exec-completion case. The race mechanism (`settleOwedExecWake` withholds the owed row after the wake settles) is unchanged on current `main`. - Negative control (deterministic): with a 500 ms delay inserted before that `withhold` (local throwaway change), current `main` fails the exec-completion case 3/3 with the same assertion; with this change it passes 3/3. - Without the delay, interleaved runs of `session-yield-sweep.test.ts` with this change applied to current `main`: 20/20 green (main alone was also 20/20 locally; the natural race is rare). ## Merge danger Two-way door: test-only, a revert fully undoes it. Blast radius: one test file. Co-authored-by: Ayaan Zaidi <[email protected]>
…elded session (openclaw#204801) ## Problem `src/gateway/session-yield-sweep.test.ts` > "settles only an unreachable parked lifecycle (exec-completion)" fails intermittently on scheduled `main` (run 45403387860): `expected undefined to be 'failed'` at the final status check. Since openclaw#204265, the yield sweep keeps a yielded session waiting while it has a stored owed exec result (`shouldWake`, `session-yield-sweep.ts:45`). When the exec completion's wake settles as `cancelled`, `settleOwedExecWake` withholds the owed row asynchronously: `readPendingOwed` and then `withhold` on the database worker. The test resolved the wake and advanced the fake clock straight away. When the sweep tick ran before `withhold` committed, the sweep still saw the pending row and correctly left the session waiting, so the status stayed unset. ## Solution Test-only change. After the wake settles, the test waits until `shouldWake` (the stored-row check the sweep itself uses) reports no owed result, then advances the clock. Product behavior is unchanged: the next sweep tick settles the session. openclaw#204609 and openclaw#204621 do not touch this path. openclaw#204609 changes only `session-restart-resume-custody.ts`, which only the restart path imports. openclaw#204621 changes reply-operation backend readiness, which none of these modules use; the test's wake is mocked. ## Impact No change to production code or user behavior. ## Evidence - Before: intermittent red on scheduled `main` with the assertion above: run 45403387860, and again in run 45849028735 (2026-11-26, `checks-node-compact-large-14`, after openclaw#205835), `expected undefined to be 'failed'` in the exec-completion case. The race mechanism (`settleOwedExecWake` withholds the owed row after the wake settles) is unchanged on current `main`. - Negative control (deterministic): with a 500 ms delay inserted before that `withhold` (local throwaway change), current `main` fails the exec-completion case 3/3 with the same assertion; with this change it passes 3/3. - Without the delay, interleaved runs of `session-yield-sweep.test.ts` with this change applied to current `main`: 20/20 green (main alone was also 20/20 locally; the natural race is rare). ## Merge danger Two-way door: test-only, a revert fully undoes it. Blast radius: one test file. Co-authored-by: Ayaan Zaidi <[email protected]>
Closes #204187
What Problem This Solves
With an active Goal, a background command that finishes during a turn can lose its place to the Goal's automatic "Advance the active goal" turn. The agent then advances the Goal before it has read the command result. A Gateway restart between a command's exit and its completion turn also loses the result, because the result exists only in memory.
User Impact
User impact: a finished background command always reaches the agent before an active Goal advances, and a Gateway restart no longer loses a command result that was waiting for its turn.
Why This Change Was Made
The exec result becomes stored state when the command exits (first slice of the owed-result model):
src/sessions/owed.ts.owe(item)is the only writer of new owed rows. It stores the result in the existingsession_pending_inputstable, withkind: "exec"andoriginFacts(requester, command owner, operator profile id) as additive JSON in the row message. There is no schema change.shouldWake(chat)reads stored rows: it is true while the chat has owed rows that no live turn holds. Consumption does not readoriginFactsyet; a later slice intersects it with current policy without a migration.foldwrites the joined text into the first member's row and consumes the other members' rows in one transaction, so a restart at any point replays each result once. If the fold fails, the shared turn is refused and each member retries its own row. The transcript append consumes the row in the same transaction, so a second consumer or a retry after restart appends nothing.shouldWakeand does not advance while an owed result is pending. The completion turn runs first, and its end continues the Goal.wakeOwedExec, serves the live wake, its single retry and restart recovery. A wake that fails before it becomes a turn, or is refused at enqueue, retries once from the stored row; the live retry keeps the captured target, so admission revalidates the requester's authority. A stop, or a second failure, ends the row with a visible cause. Restart recovery wakes owed rows, which it used to refuse as queued human input.bash-tools.exec-notify.ts: thependingExitWakesset and its reads,enqueueExecExitNotification, and the separate settle/log block (hasExecYieldWakeand the yield sweep now read stored rows)session-accessor.pending-inputs.ts: two duplicated "already consumed" receipts becomeconsumedPendingInputReceipt(moved tosession-pending-input-value.ts)sessions/owed.ts(owe,fold,shouldWake,readPendingOwed,withhold: +200);session-pending-input-value.ts(readOwedKind, owed restage, moved receipt: +44); owed-row adoption insession-accessor.pending-input-request.ts(+18 −2); absorbed-row consumption in both pending-input kernels and the mutation type (+32)bash-tools.exec-notify.ts(wakeOwedExec, settlement, owed row with origin facts: +104);session-event-batch.ts(fold before a shared turn, one refusal path: +68 −15);queued-chat-inputs.tsrestart wake (+22 −1);goal-continuation.ts(+11);session-yield-sweep.ts(+4 −1)No parallel in-memory exec-completion path is left. Every exec completion, live or after restart, is an owed row delivered by
wakeOwedExec. What remains in memory is the shared session-event admission that runs the turn for every producer (cron, hooks, plugins). Its occurrence still carries a copy of the text; that copy can go when session-event admission reads stored rows for all producers, in later owed-result slices. The cron yield-resume path (execYieldResumes) is a different consumer and stays out of this slice.Evidence
server.chat-goal.test.tscase holds the real exec wake in target preparation until the first turn's Goal check runs. It is red on the merge base (Advance overtakes the completion) and green on head.foldleaves exactly one row carrying both results; it is red when the absorbed rows are not consumed (both rows remain, so a restart would deliver the second result twice).session_pending_inputsacross a Gateway restart is appended to the transcript exactly once after the restart (5 runs). On head0abfb478, which differs from the final head only in the batch's fold-failure path, the same run also showsExec completedbefore[System] Advance the active goal(with fix: keep a goal advancing after a background command's completion turn #204327 merged).4b887e10, frozen copy):server.chat-goal.test.tsas a whole file, 20 separate runs: 20/20 passed with 72/72 each (1,440 test executions). Both "keeps pursuing after … takes the next turn" variants passed in every run (40/40).Separate bug, fixed and merged in #204327 (issue #204319): on a real Gateway, a Goal did not advance after a background command's completion turn. Post-turn session maintenance waited in the mailbox and the Goal check deferred to it. This happens on
maintoo. A plain Goal advances normally onmain; an earlier note here that it never advanced came from a bad test prompt. With #204327 the real-Gateway transcript reads: Goal request →Exec completed …→[System] Advance the active goal ….