Skip to content

fix(cron): prevent stale outcomes after same-millisecond restarts - #127003

Merged
steipete merged 1 commit into
mainfrom
fix/cron-recovery-exact-receipt
Aug 26, 2026
Merged

steipete merged 1 commit into
mainfrom
fix/cron-recovery-exact-receipt

Conversation

@steipete

@steipete steipete commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where a cron run interrupted by a Gateway restart could be reported as successful when another run for the same job began in the same millisecond and had already finalized its task row.

Why This Change Was Made

Cron receipts already provide the durable execution owner, but task identity and restart lookup still accepted any same-job/same-start candidate. Receipt-backed task IDs now put the receipt UUID first (cron:<job>:<startedAt>:<receiptId>[:<publicRunId>]), and receipt-backed recovery accepts only that exact owner-native prefix. Receipt-less legacy recovery keeps its existing broad matching.

Pre-upgrade caller-ID-only and reservation-keyed task rows do not encode receipt ownership. When one is paired with an active receipt, the states “this receipt finalized before crashing” and “an earlier receipt finalized, then a same-millisecond receipt crashed” are durably indistinguishable. The approved compatibility policy is fail closed: report that rare ambiguous upgrade-window state as interrupted rather than guess and silently attribute stale success. No schema, protocol, or version change is required.

Provenance: broad execution-start recovery came from #114820 (6ca7c6488ed9), while durable receipts came from #122948 (d3308e2cfd9d); composing timestamp-level task lookup with receipt-level ownership exposed this gap.

User Impact

Current runs and unambiguous receipt-backed recovery preserve the exact finalized outcome. Genuinely ambiguous pre-upgrade in-flight manual runs are conservatively reported as interrupted after restart instead of potentially showing another run’s success.

Production LOC: +44/-14 (net +30) | Tests: +159/-25 (net +134)

Evidence

  • Before fix: focused same-millisecond and legacy caller-ID recovery regressions both restored stale lastRunStatus: "ok"; manual identity/fallback regressions could not find a receipt-prefixed terminal row (4/4 failures).
  • Exact-head focused suites: 115/115 passed across recovery, task ledger, operations, owner hardening, and execution binding.
  • node scripts/check-changed.mjs: passed, including core/test typechecks, type-aware lint, formatting, dead exports, import cycles, schema, database-first, and dependency guards.
  • pnpm build: passed on the exact published head.
  • Fresh diff-scoped autoreview: clean, no accepted/actionable findings (0.98 confidence).
  • Secretless Gateway crash/restart proof: a command automation persisted receipt a09b58d8-c729-4dd7-b2d2-a48d5a7d403f and exact receipt-keyed task row, was abruptly SIGKILLed, then recovered after supervised Gateway restart with the receipt interrupted, the task failed, running_at_ms cleared, and the job/history marked error; no provider, model, channel, or inherited credentials were used.

Published head: 743bf7e4f95bc20d518ddd360c3c9da9e75642e9
Tested parent/main cutoff: 3a759c85c5862b4d95c6d873b3e33c544ee14827
Live base tip at publication: 378e45a6052d0ae840e9998e5f1bb61cc211f69d
Head tree: c6f4731a4efeb7dfb942010a714abc84059c1f0c

@clawsweeper

clawsweeper Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Aug 21, 2026
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Aug 21, 2026
@clawsweeper

clawsweeper Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 26, 2026, 3:53 AM ET / 07:53 UTC.

ClawSweeper review

What this changes

The PR makes cron task rows and restart recovery use the durable receipt UUID as the primary run identity, while retaining a manual public run ID as a suffix.

Regression provenance

Possible regression — probable (reviewed change; failure trace). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 4 items remain

The patch correctly binds restart recovery to the durable receipt and covers the same-millisecond and legacy ambiguity cases; no actionable defect was found. It should remain open for protected maintainer review; likely related person: steipete (high confidence).

Priority: P1
Reviewed head: 743bf7e4f95bc20d518ddd360c3c9da9e75642e9
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, owner-bound fix with relevant regression coverage and no discrete correctness finding.
Proof confidence 🌊 off-meta tidepool Not applicable: The repository-member PR body records a secretless Gateway crash/restart proof; external-contributor proof gating does not apply.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The repository-member PR body records a secretless Gateway crash/restart proof; external-contributor proof gating does not apply.
Evidence reviewed 5 items Current-main gap: Current main accepts any same-job, same-millisecond task row through a broad run-ID prefix or matching start time, and recovery does not pass receipt identity to that lookup.
Owner-bound repair: The PR constructs receipt-first task IDs and permits receipt-backed recovery only for that exact receipt prefix; recovery passes the active receipt ID into the task lookup.
Boundary regression coverage: The new recovery test creates a finalized task for one receipt, then recovers a second receipt with the same timestamp and asserts the second run is interrupted rather than restored as successful.
Findings None None.
Security None None.

Live Verification

Command: pnpm test src/cron/service/run-recovery.test.ts

Result: FAIL (partial) — step 2 expect_output does not restore a prior same-millisecond task for a different receipt: expected terminal output was not visible within 30 seconds: "does not restore a prior same-millisecond task for a different receipt"

$ node --import tsx scripts/test-projects.mts src/cron/service/run-recovery.test.ts
[test] starting test/vitest/vitest.cron.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-127003-UvjcBa/target

 ✓  cron  src/cron/service/run-recovery.test.ts (12 tests) 498ms
     ✓ retires a stale settling receipt after its marker is already gone  384ms

 Test Files  1 passed (1)
      Tests  12 passed (12)
   Start at  07:54:31
   Duration  9.24s (transform 6.87s, setup 971ms, import 7.42s, tests 498ms, environment 0ms)

[test] passed 1 Vitest shard in 15.60s
$ node --import tsx scripts/test-projects.mts src/cron/service/run-recovery.test.ts
[test] starting test/vitest/vitest.cron.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-127003-UvjcBa/target

 ✓  cron  src/cron/service/run-recovery.test.ts (12 tests) 390ms

 Test Files  1 passed (1)
      Tests  12 passed (12)
   Start at  07:54:48
   Duration  4.58s (transform 2.33s, setup 440ms, import 3.52s, tests 390ms, environment 0ms)

[test] passed 1 Vitest shard in 11.53s

Assertions:

  • FAIL expect_output: does not restore a prior same-millisecond task for a different receipt

How this fits together

Cron admission creates a receipt and task-ledger row for each job execution. Gateway restart recovery reads those records to restore a completed outcome or record an interrupted one.

flowchart LR
  A[Cron job] --> B[Run admission]
  B --> C[Durable receipt]
  B --> D[Task ledger row]
  C --> E[Restart recovery]
  D --> E
  E --> F[Job history and status]
Loading

Decision needed

Question Recommendation
Should the documented fail-closed outcome for ambiguous pre-upgrade manual task rows be accepted for this upgrade path? Accept fail-closed recovery: Merge with the documented behavior that an indistinguishable historical manual row is reported interrupted.

Why: The alternative is silently attributing an older successful task row to a different receipt; choosing that upgrade policy is maintainer-owned.

Before merge

  • Resolve merge risk (P1) - Merging intentionally reports a rare, indistinguishable pre-upgrade manual caller-ID task as interrupted rather than restoring a potentially stale success.
  • Resolve merge risk (P2) - This bug fix adds 30 net production lines; the receipt-first identity must remain the sole recovery discriminator rather than gaining another fallback path.
  • Complete next step (P2) - The protected maintainer label and intentional upgrade behavior require maintainer handling; no narrow automated repair is needed.
Agent review details

Security

None.

PR surface

Source +30, Tests +134. Total +164 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 4 44 14 +30
Tests 2 159 25 +134
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 203 39 +164

Review metrics

Metric Value Why it matters
Production versus tests production +44/-14 (net +30); tests +159/-25 (net +134) The production increase is confined to carrying exact receipt ownership across admission and recovery, with focused boundary regressions.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/cron/service/ops.test.ts, serialized state: src/cron/service/ops.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster

Relationship: canonical
Canonical: #127003
Summary: This PR is the canonical repair for the remaining gap between durable receipt ownership and timestamp-level task recovery.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Accept the documented upgrade behavior (recommended)
    Retain exact receipt matching so an ambiguous pre-upgrade manual task resolves as interrupted instead of inheriting another run’s success.
  2. Pause for an alternate migration policy
    Hold the PR if maintainers require historical manual task rows to retain successful outcomes despite the missing receipt identity.

Technical review

Best possible solution:

Keep the durable receipt as the sole recovery owner, preserve manual public IDs only as suffixes, and report historically ambiguous pre-upgrade rows as interrupted.

Do we have a high-confidence way to reproduce the issue?

Yes—current main’s timestamp/prefix task lookup and the PR’s same-millisecond regression scenario provide a high-confidence source-level reproduction path.

Is this the best way to solve the issue?

Yes—the durable receipt is the owner-native record, so threading it through task creation and recovery is narrower and safer than ranking timestamp-matched rows.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 378e45a6052d.

Labels

Label justifications:

  • P1: A Gateway restart can otherwise report another cron execution’s success for the active job run.
  • merge-risk: 🚨 compatibility: Ambiguous pre-upgrade manual task rows intentionally change from restored success to interrupted.
  • merge-risk: 🚨 session-state: The change decides which durable task outcome becomes the cron job’s visible post-restart state.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The repository-member PR body records a secretless Gateway crash/restart proof; external-contributor proof gating does not apply.

Evidence

What I checked:

  • Current-main gap: Current main accepts any same-job, same-millisecond task row through a broad run-ID prefix or matching start time, and recovery does not pass receipt identity to that lookup. (src/cron/service/task-runs.ts:180, 378e45a6052d)
  • Owner-bound repair: The PR constructs receipt-first task IDs and permits receipt-backed recovery only for that exact receipt prefix; recovery passes the active receipt ID into the task lookup. (src/cron/service/task-runs.ts:185, 743bf7e4f95b)
  • Boundary regression coverage: The new recovery test creates a finalized task for one receipt, then recovers a second receipt with the same timestamp and asserts the second run is interrupted rather than restored as successful. (src/cron/service/run-recovery.test.ts:289, 743bf7e4f95b)
  • Feature provenance: Merged cron work established restart recovery and durable receipt ownership before this follow-up composes the two identities. (src/cron/service/task-runs.ts:180, d3308e2cfd9d)
  • Scoped production/test delta: The diff contains production +44/-14 and tests +159/-25; the production growth carries the exact receipt from both admission paths into the single recovery discriminator. (src/cron/service/task-runs.ts:139, 743bf7e4f95b)

Likely related people:

  • steipete: The related merged work on restart recovery and durable receipts supplies the owner boundary this PR composes. (role: introduced adjacent merged cron recovery and receipt behavior; confidence: high; commits: 6ca7c6488ed9, d3308e2cfd9d; files: src/cron/service/task-runs.ts, src/cron/service/run-recovery.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (5 earlier review cycles)
  • reviewed 2026-08-21T04:36:28.871Z sha f8368ec :: needs maintainer review before merge. :: none
  • reviewed 2026-08-21T05:09:52.629Z sha f8368ec :: needs maintainer review before merge. :: none
  • reviewed 2026-08-21T07:28:21.552Z sha 06f1322f8af9b8dd953013579882c268019a939f :: needs maintainer review before merge. :: none
  • reviewed 2026-08-21T07:52:32.137Z sha 084bf31 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-21T08:56:15.411Z sha 084bf31 :: needs maintainer review before merge. :: none

@steipete
steipete force-pushed the fix/cron-recovery-exact-receipt branch from f8368ec to 06f1322 Compare August 21, 2026 07:17
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Aug 21, 2026
@steipete
steipete force-pushed the fix/cron-recovery-exact-receipt branch from 06f1322 to 084bf31 Compare August 21, 2026 07:46
@clawsweeper clawsweeper Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Aug 21, 2026
Preserve receipt-less legacy matching, but fail closed when an active
receipt has only an ambiguous pre-upgrade task identity.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0220e-6236-745d-8b14-759d26595b12
@steipete
steipete force-pushed the fix/cron-recovery-exact-receipt branch from 084bf31 to 743bf7e Compare August 26, 2026 07:48
@steipete steipete self-assigned this Aug 26, 2026
@steipete
steipete marked this pull request as ready for review August 26, 2026 07:49
@clawsweeper

clawsweeper Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@steipete
steipete merged commit 9c8393f into main Aug 26, 2026
263 checks passed
@steipete
steipete deleted the fix/cron-recovery-exact-receipt branch August 26, 2026 09:05

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 27, 2026
Preserve receipt-less legacy matching, but fail closed when an active
receipt has only an ambiguous pre-upgrade task identity.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0220e-6236-745d-8b14-759d26595b12

Co-authored-by: Amp <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants