Repository navigation
fix(cron): ended cron runs admit late turns from background work - #199585
Conversation
… work A cron run is one-shot: once its executor finalizes, nothing should start another turn on its run session. Each producer of background work decided that separately, and two of them disagreeing caused the detached-media deadlock and retry storm. One rule now owns it. isEndedCronRun refuses a new turn on a cron run key when no run of that session keeps a live execution owner, at both turn admission owners: the Gateway agent-turn preparation and the session-event handoff. The exec-notify acceptance check that duplicated it is removed. isOneShotRunSessionKey replaces the duplicated cron-run-or-subagent checks. Background commands still running when a cron run finalizes are neither awaited nor stopped; the run history now names them in its diagnostics. 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. |
|
Codex review: needs maintainer review before merge. What this changesThis PR centralizes refusal of new turns on ended cron runs and records background commands still running at completion. Example: An agent request targets agent:main:cron:nightly:run:one-shot after its cron executor finishes.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this maintainer-authored PR open: it closes a real admission gap on current main, and the earlier correctness findings are resolved with no additional code findings. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherCron owns each isolated run's execution lifetime; Gateway requests and background completion events enter session admission, which either queues authorized work or returns a refusal. flowchart TD
A[Cron executor] --> B[Run registry]
C[Agent RPC] --> D[Gateway preparation]
E[Background completion] --> F[Session event admission]
B --> G[Ended run check]
D --> G
F --> G
G --> H[Admit work or refuse]
Technical reviewBest possible solution: Keep the executor-lifetime predicate in the existing run registry and apply it through both admission owners while preserving accepted-event custody. Do we have a high-confidence way to reproduce the issue? Current-main source permits an agent request on an ended cron run key without checking executor ownership; supplied Gateway tests exercise that refusal after the fix, but this review ran no target code. Is this the best way to solve the issue? The shared predicate is a focused repair using existing ownership state, and its acceptance-only lifetime preserves the earlier queued-completion contract. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 4f592f12c35c. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +120, Tests +199, Docs +1. Total +320 across 19 files. Added test files: 1. 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. LabelsLabel changes: No label 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 HistoryReview history (4 earlier review cycles)
Reviewed November 15, 2026, 10:15 AM ET / 15:15 UTC (Revision 5). |
Exec completions for a cron run must be admitted while the run is live. Holding a second completion in the batching lane until the first settles lets the run end first, and the ended-run refusal then drops it. Restore the cron bypass (#199113) on the key, not on the removed producer-side acceptance check. Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
An accepted completion follow-up registers a run on the same session key and session id. Counting it as a live owner let a later completion start another turn after the originating executor had ended (the case #172690 guarded with captured origin run ids). The ended-run rule now counts only the run carrying the job invocation. Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
The run id is part of a cron run session key, so the key names its one executor. Filtering by an optional session id made a caller without a resolved target see a live run as ended and refuse its event. Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Landed. A cron run now receives background results only while its executor owns it; once it ends, late turns from background commands, subagents or other producers are refused once with a typed error instead of starting a turn on the dead run. Live completions still reach the run immediately, and still-running background commands are named in the run history. Proof: new real-Gateway cron one-shot E2E (yield, no-yield, subagent), admission boundary regressions red on the previous predicates, green CI, ClawSweeper ready. |
…nclaw#199585) ## What Problem This Solves Fixes: a cron run can be handed late background work, such as a command exit or a completion, after it has ended. Each producer decided by itself whether a cron run could receive a later turn. When two producers disagreed (media expected a later turn; the runner waited for the media), production hit a deadlock and a retry storm of about 16.5k attempts. ## User Impact A cron run receives background results only while its executor still owns it: as an inline tool result, or through `sessions_yield`, which the cron executor resumes in place. After the run ends, the Gateway refuses any new turn on its run session one time, with `cron run has ended; inspect the result in its run record`. Results stay in their durable records (process log, subagent registry, task record). Background commands that are still running when the run ends are not awaited. The run history now names them in its diagnostics, so the run does not look complete while its commands continue. Subagent behavior does not change. ## Why This Change Was Made One rule now has one owner. `isEndedCronRun` (cron run key, and no run of that session has a live execution owner) is the same check exec-notify did before. Both turn admission owners call it: - Gateway agent-turn preparation (`agent-session-prepare.ts`), next to the other session start refusals. - The session-event handoff (`enqueueSessionEventForHost`), as the acceptance check. Exec exit wakes reach a session through this path, not through `agent` RPC. The check runs at enqueue and again until the occurrence is accepted. `isOneShotRunSessionKey` (cron run key or subagent key) replaces the duplicated `subagent || cron run` checks. | Producer | Before | After | |---|---|---| | Background exec exit (`bash-tools.exec-notify.ts`) | Own `assertExitNotificationAcceptanceCurrent` cron check; own `hiddenSubagent \|\| isCronRunSessionKey` one-shot check | No cron check. One-shot uses `isOneShotRunSessionKey`. The session-event admission refuses an ended run, and the warning still says `inspect the result with process log` | | Exec exit-wake guidance (`bash-tools.exec-output.ts`) | Read the producer's cron assertion | One-shot runs promise a wake only through `sessions_yield` (`isOneShotRunSessionKey`) | | Exec default timeout (`exec-tool-timeout.ts`) | `isSubagentSessionKey \|\| isCronRunSessionKey` | `isOneShotRunSessionKey` | | Exec batching (`session-event-batch.ts`) | Bypassed batching when the exec cron assertion was present | No cron special case. A batched turn is admitted through the same gate | | `agent` RPC turns on a cron run key (for example subagent completion, manual) | No ended-run refusal | Refused with the typed `INVALID_REQUEST` above | | Media (`media-generate-background-shared.ts`) | Inline in cron runs (openclaw#199529) | Unchanged | | `sessions_send` | Cron-specific branches | Unchanged. No site means only "requester cannot receive a later turn": the follow-up path lets yielded subagent requesters receive registry turns, and the executor keeps the `:trigger` case plus a wait-failure notice that applies to cron only | The cron executor comments on the intended asymmetry: subagents are awaited because their output is the run's output; background commands are not, because they can be services. Jobs with `deleteAfterRun` still delete the run session, and that stops its commands (existing behavior). The `cronRunContinuation` marker is not changed (separate follow-up). ## Evidence - New QA-lab Gateway e2e `test/e2e/qa-lab/runtime/cron-one-shot-run.e2e.test.ts` (real Gateway, mock-openai). Each case checks that the run reaches status `ok`, that an `agent` turn on the ended run key is refused with the typed error, and that no model request occurs after the run ends: - (a) background `sleep 3; echo …`, then `sessions_yield`: the run resumes in place, and its summary contains the command output. - (b) background `sleep 30; echo …`, no yield: the run ends in less than 25 s. Its diagnostics name the running command. Its exit later logs `completion was not admitted (cron run has ended…); inspect the result with process log` exactly once and starts no turn. - (c) the cron run spawns a child and ends its turn: the run completes with the child's result (regression guard). - New-behavior assertions: the `agent` refusal (all cases), the diagnostics entry and the refusal text (b). The yield resume and the child result are existing behavior. I did not run this e2e on the base. - `test/e2e/qa-lab/media/cron-image-generation.e2e.test.ts` passes. - Unit test at the admission boundary (`agent-session-preparation.worker.test.ts`): a cron run key is admitted while a run context owns it, and refused with `INVALID_REQUEST` after the run context is released. - Focused suites pass: exec background follow-up, exec notify, session-event handoff/readmission/permissions, gateway agent-turn, cron isolated-agent (62 files), qa-lab mock-openai. - On this head (rebased on main after openclaw#199529 landed): `pnpm tsgo:core`, `pnpm check:test-types`, `pnpm tsgo:scripts`, mock-export, max-lines, line-cap and assertion-safety ratchets, format, and oxlint all pass. The only oxlint finding is the existing max-lines on `session-event-handoff.ts`, which is grandfathered and has the same line count as main. - I did not run the full `ocx build --dts` and packed-install smoke on this rebased head. Both passed on the identical pre-rebase change. <details><summary>Follow-up finding: image_generate in a hidden subagent that never yields</summary> From reading the code only; no test. `shouldDetachMediaGenerationTask` detaches for every non-cron key, so a subagent's image detaches. Its completion goes through `deliverSubagentAnnouncement` as an `agent` turn on the subagent key. Gateway admission does not refuse it: inter-session input that is not `subagent_settle` does not reactivate the child registry row (`agent-run-subagent.ts`). The result is not the refused-completion retry storm. It looks like a late orphan turn in the finished child session, whose output probably does not reach the parent. The fix is not a one-line predicate swap, so it is not in this PR. </details> **Review follow-up (b8b06bb):** live cron completions bypass the exec completion batching lane again, keyed on the cron run session key instead of the removed producer-side acceptance check (the reason openclaw#199113 excluded them). New regression `admits every completion that arrives while its cron run is live` in `bash-tools.exec-background-followup.test.ts`: two background commands finish while the run is live and both are admitted; it fails with the bypass removed and passes with it. **Review follow-up 2:** the ended-run rule counts only the executor's own run (the run carrying the job invocation), so an accepted completion follow-up on the same session cannot make an ended origin look live (the case openclaw#172690 guarded with captured origin run ids). The admission boundary test now holds a follow-up owner while the executor releases and expects the refusal; it fails on the previous predicate and passes now. `check-lint-core-4` fails only in `lint extension package stripe`, which this PR does not touch. Co-authored-by: Ayaan Zaidi <[email protected]>
…199679) Keep the three one-shot response markers local to their only consumer module. They were introduced as unused exports in 703442c (openclaw#199585). Fixture output and public scenario behavior stay unchanged.
What Problem This Solves
Fixes: a cron run can be handed late background work, such as a command exit or a completion, after it has ended. Each producer decided by itself whether a cron run could receive a later turn. When two producers disagreed (media expected a later turn; the runner waited for the media), production hit a deadlock and a retry storm of about 16.5k attempts.
User Impact
A cron run receives background results only while its executor still owns it: as an inline tool result, or through
sessions_yield, which the cron executor resumes in place. After the run ends, the Gateway refuses any new turn on its run session one time, withcron run has ended; inspect the result in its run record. Results stay in their durable records (process log, subagent registry, task record). Background commands that are still running when the run ends are not awaited. The run history now names them in its diagnostics, so the run does not look complete while its commands continue. Subagent behavior does not change.Why This Change Was Made
One rule now has one owner.
isEndedCronRun(cron run key, and no run of that session has a live execution owner) is the same check exec-notify did before. Both turn admission owners call it:agent-session-prepare.ts), next to the other session start refusals.enqueueSessionEventForHost), as the acceptance check. Exec exit wakes reach a session through this path, not throughagentRPC. The check runs at enqueue and again until the occurrence is accepted.isOneShotRunSessionKey(cron run key or subagent key) replaces the duplicatedsubagent || cron runchecks.bash-tools.exec-notify.ts)assertExitNotificationAcceptanceCurrentcron check; ownhiddenSubagent || isCronRunSessionKeyone-shot checkisOneShotRunSessionKey. The session-event admission refuses an ended run, and the warning still saysinspect the result with process logbash-tools.exec-output.ts)sessions_yield(isOneShotRunSessionKey)exec-tool-timeout.ts)isSubagentSessionKey || isCronRunSessionKeyisOneShotRunSessionKeysession-event-batch.ts)agentRPC turns on a cron run key (for example subagent completion, manual)INVALID_REQUESTabovemedia-generate-background-shared.ts)sessions_send:triggercase plus a wait-failure notice that applies to cron onlyThe cron executor comments on the intended asymmetry: subagents are awaited because their output is the run's output; background commands are not, because they can be services. Jobs with
deleteAfterRunstill delete the run session, and that stops its commands (existing behavior). ThecronRunContinuationmarker is not changed (separate follow-up).Evidence
test/e2e/qa-lab/runtime/cron-one-shot-run.e2e.test.ts(real Gateway, mock-openai). Each case checks that the run reaches statusok, that anagentturn on the ended run key is refused with the typed error, and that no model request occurs after the run ends:sleep 3; echo …, thensessions_yield: the run resumes in place, and its summary contains the command output.sleep 30; echo …, no yield: the run ends in less than 25 s. Its diagnostics name the running command. Its exit later logscompletion was not admitted (cron run has ended…); inspect the result with process logexactly once and starts no turn.agentrefusal (all cases), the diagnostics entry and the refusal text (b). The yield resume and the child result are existing behavior. I did not run this e2e on the base.test/e2e/qa-lab/media/cron-image-generation.e2e.test.tspasses.agent-session-preparation.worker.test.ts): a cron run key is admitted while a run context owns it, and refused withINVALID_REQUESTafter the run context is released.pnpm tsgo:core,pnpm check:test-types,pnpm tsgo:scripts, mock-export, max-lines, line-cap and assertion-safety ratchets, format, and oxlint all pass. The only oxlint finding is the existing max-lines onsession-event-handoff.ts, which is grandfathered and has the same line count as main.ocx build --dtsand packed-install smoke on this rebased head. Both passed on the identical pre-rebase change.Follow-up finding: image_generate in a hidden subagent that never yields
From reading the code only; no test.
shouldDetachMediaGenerationTaskdetaches for every non-cron key, so a subagent's image detaches. Its completion goes throughdeliverSubagentAnnouncementas anagentturn on the subagent key. Gateway admission does not refuse it: inter-session input that is notsubagent_settledoes not reactivate the child registry row (agent-run-subagent.ts). The result is not the refused-completion retry storm. It looks like a late orphan turn in the finished child session, whose output probably does not reach the parent. The fix is not a one-line predicate swap, so it is not in this PR.Review follow-up (b8b06bb): live cron completions bypass the exec completion batching lane again, keyed on the cron run session key instead of the removed producer-side acceptance check (the reason #199113 excluded them). New regression
admits every completion that arrives while its cron run is liveinbash-tools.exec-background-followup.test.ts: two background commands finish while the run is live and both are admitted; it fails with the bypass removed and passes with it.Review follow-up 2: the ended-run rule counts only the executor's own run (the run carrying the job invocation), so an accepted completion follow-up on the same session cannot make an ended origin look live (the case #172690 guarded with captured origin run ids). The admission boundary test now holds a follow-up owner while the executor releases and expects the refusal; it fails on the previous predicate and passes now.
check-lint-core-4fails only inlint extension package stripe, which this PR does not touch.