Skip to content

fix(cron): remove deleted job sessions - #119520

Merged
steipete merged 8 commits into
openclaw:mainfrom
goutamadwant:fix/cron-remove-session-cleanup
Aug 9, 2026
Merged

steipete merged 8 commits into
openclaw:mainfrom
goutamadwant:fix/cron-remove-session-cleanup

Conversation

@goutamadwant

@goutamadwant goutamadwant commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where operators deleting an isolated cron job would still see its reusable agent:<agent>:cron:<jobId> base session in session-management surfaces.

Closes #46369.

Why This Change Was Made

Cleanup now runs after the cron definition is durably removed and uses the canonical session lifecycle mutation, including transcript archival. If a run was already admitted, deletion waits for that exact active marker to clear, preserving the run's lifecycle claim.

A per-store, per-job cleanup fence makes same-ID creation release the shared cron-store lock, wait through the asynchronous session mutation, and retry afterward. The fence lives beside the canonical store-partition operation queue, so sibling CronService instances cannot overtake each other's cleanup or make the remover trust a stale service-local job snapshot. Cleanup reacquires that lock and reloads committed cron state before deleting the guarded session row.

Per-run history remains owned by cron retention, and shared main sessions are not removed.

User Impact

Deleted isolated/current cron jobs no longer leave ghost base sessions in the session store or Control UI. Active-run deletion, same-ID job replacement, sibling service instances, unrelated jobs, shared sessions, and retained run history preserve their existing behavior.

Evidence

Real behavior proof on implementation head ee789bb used an isolated loopback gateway and its SQLite-backed session store:

openclaw_version=2026.7.2 (ee789bb)
cron_add={name:"proof-cleanup",enabled:false,sessionTarget:"isolated"}
before={job_present:true,base_session_present:true,session_kind:"cron"}
cron_rm={ok:true,removed:true}
after={job_present:false,base_session_present:false}
gateway_shutdown=clean

The disposable job was created and removed through the real CLI/gateway flow. Its canonical base-session fixture was inserted through the runtime session accessor before removal; this proof does not depend on an external model or channel provider.

Fresh final-head proof on 066a4673ae81e4f07beb38b18bd6f702441c08f2 repeated that flow against an isolated dev Gateway and SQLite state directory:

openclaw_version=2026.8.1 (066a467)
cron_add={enabled:false,sessionTarget:"isolated"}
before={job_present:true,base_session_count:1,session_kind:"cron"}
cron_rm={ok:true,removed:true}
after={job_present:false,base_session_count:0}
gateway_shutdown=clean

The final-head run also used the runtime session accessor to seed the canonical row, invoked no provider or channel, and retained private mode-0600 raw artifacts locally.

The current head b8e3c03d77f76e97b2dba6dd9cce1b321c620627 is a patch-identical rebase of that reviewed cron repair onto main after CI fixture fix #120847; no cron/session runtime content changed after the trace.

The final concurrency regression uses two CronService instances sharing one SQLite cron partition. It removes an active job through one service, attempts a same-ID replacement through the other, proves creation remains fenced until exact-marker cleanup finishes, then verifies the replacement session survives.

Validation on final source head b8e3c03d77f76e97b2dba6dd9cce1b321c620627, rebased onto current main with CI watchdog fix #120700 and config-recovery fixture fix #120847:

node scripts/run-vitest.mjs src/cron/service.remove-session-cleanup.test.ts src/cron/active-jobs.test.ts src/cron/active-jobs-manual-run.test.ts src/cron/session-reaper.test.ts src/cron/service/ops.test.ts src/cron/service.one-shot-schedule-ownership.test.ts src/cron/service.removal-postcommit.test.ts
# 7 files / 138 tests passed, including held session-writer lock-order proof and the read-only missing-database regression

.agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main --engine codex --model gpt-5.6-sol --thinking high
# clean: no accepted/actionable findings

git diff --check origin/main...HEAD
# passed

node scripts/run-tsgo.mjs -p tsconfig.plugin-sdk.dts.json --declaration true
# passed

Exact-head hosted CI is required before landing. Per-run session rows intentionally remain governed by cron retention. A session-cleanup failure is logged and does not roll back an already-durable job deletion, preventing retry ambiguity.

Contributor Credit

The original repair and concurrency coverage are by @goutamadwant. The maintainer follow-up keeps the contributor's four authored commits and credits Goutam on every maintainer-authored repair commit.

Disclosure: AI was used to understand the codebase and review the fix.

@goutamadwant
goutamadwant force-pushed the fix/cron-remove-session-cleanup branch from 1cc79fa to f21b978 Compare August 5, 2026 06:46
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Aug 5, 2026
@clawsweeper

clawsweeper Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

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

ClawSweeper review

What this changes

Removes an isolated or current cron job’s reusable base session after the job is durably deleted, while preserving active runs, shared sessions, and retained run history.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep open: current main still leaves an isolated cron job’s base session after deletion, while this PR repairs that lifecycle boundary with focused concurrency coverage and sufficient runtime proof.

Priority: P2
Reviewed head: 7ff55d543a55f9eb92d22669fe0b701411d3d710

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Strong runtime proof, focused owner-boundary coverage, and no actionable correctness or security finding support an above-average repair.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR body provides an after-fix CLI, Gateway, and SQLite trace showing the base-session count fall from one to zero after cron removal.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body provides an after-fix CLI, Gateway, and SQLite trace showing the base-session count fall from one to zero after cron removal.
Evidence reviewed 6 items Current-main defect: Current main persists cron deletion and removes scratch data but does not remove the reusable base session; the session reaper separately retains base sessions while pruning completed run sessions.
Owner-boundary repair: The PR records the exact active marker, fences same-ID recreation by cron-store partition, releases the cron lock before session I/O, then rechecks committed job state before canonical session removal.
Canonical session mutation: The new cleanup derives the stable cron base-session key, performs a read-only exact lookup, and removes only the expected entry through the established lifecycle mutation with transcript archival.
Findings None None.
Security None None.

How this fits together

Cron deletion removes durable job definitions and their isolated-agent session state. The resulting session store feeds session-management surfaces such as the Control UI.

flowchart LR
A[Operator deletes cron job] --> B[Durable cron deletion]
B --> C[Active-run and same-ID fence]
C --> D[Canonical session lifecycle]
D --> E[Session store]
E --> F[Control UI session list]
Loading

Before merge

  • Resolve merge risk (P1) - The branch is behind main; before landing, GitHub should produce the current merge candidate and complete required exact-head validation, particularly for the session-state race coverage.
  • Complete next step (P2) - No mechanical repair remains; maintainers should refresh the behind-main merge candidate and rely on completed exact-head hosted validation before landing.
Agent review details

Security

None.

PR surface

Source +184, Tests +408. Total +592 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 5 194 10 +184
Tests 2 409 1 +408
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 603 11 +592

Review metrics

Metric Value Why it matters
Production vs test LOC production +194/-10; tests +409/-1 The production growth is justified by the cron-store ownership fence and canonical session lifecycle boundary; the larger test delta covers concurrency and persistence invariants.

Stored data model

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

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #46369
Summary: This PR is the concrete candidate fix for the open canonical report of deleted cron jobs retaining base sessions.

Members:

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

Merge-risk options

Maintainer options:

  1. Refresh the merge candidate (recommended)
    Rebase or refresh the branch against current main and require green exact-head hosted checks before merging the session-lifecycle change.

Technical review

Best possible solution:

Land a current-main merge candidate that deletes only the deleted job’s isolated base session through the canonical lifecycle path while preserving replacement and active-run state.

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

Yes—source inspection of current main shows cron deletion does not invoke base-session cleanup, while the durable session entry remains eligible to appear in session-management surfaces.

Is this the best way to solve the issue?

Yes—the cleanup belongs to the cron deletion owner and delegates row removal to the canonical session lifecycle accessor rather than broadening retention cleanup or adding a second session path.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides an after-fix CLI, Gateway, and SQLite trace showing the base-session count fall from one to zero after cron removal.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: Deleting an isolated cron job can leave a stale session-management entry, a bounded session-state bug without evidence of data loss or core unavailability.
  • merge-risk: 🚨 session-state: The patch changes removal of persisted agent session rows, so a merge-candidate check must preserve same-ID replacement and active-run state.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides an after-fix CLI, Gateway, and SQLite trace showing the base-session count fall from one to zero after cron removal.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides an after-fix CLI, Gateway, and SQLite trace showing the base-session count fall from one to zero after cron removal.

Evidence

What I checked:

  • Current-main defect: Current main persists cron deletion and removes scratch data but does not remove the reusable base session; the session reaper separately retains base sessions while pruning completed run sessions. (src/cron/service/ops-mutations.ts:439, 484765582656)
  • Owner-boundary repair: The PR records the exact active marker, fences same-ID recreation by cron-store partition, releases the cron lock before session I/O, then rechecks committed job state before canonical session removal. (src/cron/service/ops-mutations.ts:509, 7ff55d543a55)
  • Canonical session mutation: The new cleanup derives the stable cron base-session key, performs a read-only exact lookup, and removes only the expected entry through the established lifecycle mutation with transcript archival. (src/cron/session-reaper.ts:59, 7ff55d543a55)
  • Race and persistence coverage: Regression coverage exercises missing databases, active runs, held session-writer lock ordering, same-ID replacement across two services, unrelated sessions, and shared main sessions. (src/cron/service.remove-session-cleanup.test.ts:26, 7ff55d543a55)
  • Main and release provenance: The eight repair commits are after merge base 6ad761a and are neither on current main nor in the latest supplied release v2026.7.1-2, so the change remains necessary and unshipped. (src/cron/service/ops-mutations.ts:439, 7ff55d543a55)
  • Real behavior proof: The PR body records an after-fix isolated CLI-to-Gateway-to-SQLite run where cron removal changed the base-session count from 1 to 0 and shut down cleanly; supplied exact-head checks show the relevant CI gates succeeding. (7ff55d543a55)

Likely related people:

  • steipete: Current-main blame associates the cron removal, active-job lifecycle, and session-reaper boundary with recent area work; this person also authored the co-credited concurrency follow-ups on the PR. (role: recent area contributor; confidence: high; commits: 6246f67784e6, f1f6aff3dad2, d21640e1b67e; files: src/cron/service/ops-mutations.ts, src/cron/active-jobs.ts, src/cron/session-reaper.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 (10 earlier review cycles; latest 8 shown)
  • reviewed 2026-08-05T09:47:45.754Z sha de11a30e1e592abe42bdb18071b38b4fc6e1e413 :: needs changes before merge. :: [P1] Defer cleanup until the admitted run is inactive | [P1] Fence same-ID re-adds through session deletion
  • reviewed 2026-08-05T12:07:41.407Z sha 0bb2f0eedb0286d680e067c7c52b338f80fa1afa :: needs maintainer review before merge. :: none
  • reviewed 2026-08-06T04:48:42.542Z sha 17ae3379380e8487b891c07909e57140528fcb14 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-07T22:19:45.932Z sha 17ae3379380e8487b891c07909e57140528fcb14 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-08T18:57:00.847Z sha 205ba34780dc2764973c94e5b8ca10dfacb68b22 :: needs changes before merge. :: [P1] Release the cron lock before mutating the session store
  • reviewed 2026-08-08T20:50:56.771Z sha 205ba34780dc2764973c94e5b8ca10dfacb68b22 :: needs real behavior proof before merge. :: [P1] Release the cron lock before mutating the session store
  • reviewed 2026-08-09T01:51:22.584Z sha 8cf56b130eafc51a7122ffce94ca829e1c589fa3 :: needs real behavior proof before merge. :: [P2] Avoid materializing a session database during a cleanup probe
  • reviewed 2026-08-09T02:38:51.985Z sha 066a4673ae81e4f07beb38b18bd6f702441c08f2 :: needs maintainer review before merge. :: none

@goutamadwant
goutamadwant force-pushed the fix/cron-remove-session-cleanup branch from f21b978 to ee789bb Compare August 5, 2026 09:00
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P2 Normal backlog priority with limited blast radius. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. 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. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P1 High-priority user-facing bug, regression, or broken workflow. labels Aug 5, 2026
@goutamadwant
goutamadwant force-pushed the fix/cron-remove-session-cleanup branch 2 times, most recently from a7b6701 to 17ae337 Compare August 6, 2026 04:03
@steipete steipete self-assigned this Aug 8, 2026
@steipete
steipete force-pushed the fix/cron-remove-session-cleanup branch from 17ae337 to 205ba34 Compare August 8, 2026 18:09
@steipete

steipete commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

The head moved to address the remaining cross-service concurrency boundary. The same-ID cleanup fence is now owned by the shared cron store partition rather than one CronServiceState; cleanup reacquires that lock and reloads committed state before the guarded session removal. Focused post-rebase proof passed 7 files / 133 tests, and whole-branch autoreview is clean. Please review exact head 205ba34780dc2764973c94e5b8ca10dfacb68b22.

@clawsweeper

clawsweeper Bot commented Aug 8, 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 repository_dispatch).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P2 Normal backlog priority with limited blast radius. and removed 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: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Aug 9, 2026
@steipete
steipete force-pushed the fix/cron-remove-session-cleanup branch from 066a467 to b8e3c03 Compare August 9, 2026 05:00
@steipete

steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

Exact head b8e3c03d77f76e97b2dba6dd9cce1b321c620627 is a clean patch-identical rebase of the previously reviewed cron repair onto current main, now including CI fixture repair #120847. The cron/session diff and exact-head runtime trace are unchanged. Fresh post-rebase proof passed 7 files / 138 tests, the declaration type proof, git diff --check, and whole-branch autoreview. Please refresh the durable verdict for this exact head while hosted CI runs.

@clawsweeper

clawsweeper Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@steipete
steipete force-pushed the fix/cron-remove-session-cleanup branch from b8e3c03 to 7ff55d5 Compare August 9, 2026 07:14
@steipete

steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Exact-head refresh: 7ff55d543a55f9eb92d22669fe0b701411d3d710 is a patch-identical rebase of the reviewed cron repair onto current main at 6ad761a36b89348968ca94fbc59a02084ab45709, including the heartbeat admission test repair from #120955.

Fresh proof on this head:

  • git range-diff marks all eight commits equivalent; Goutam's four authored commits and the four maintainer co-author trailers are preserved.
  • Seven focused cron/session files passed: 138/138 tests.
  • git diff --check, changed-file formatting, focused type-aware lint, and Plugin SDK declaration generation passed.
  • Fresh branch autoreview reported no accepted/actionable findings.

The private Gateway/SQLite runtime proof remains applicable because the cron patch is unchanged: removing the disposable cron changed its base-session count from 1 to 0 and the Gateway shut down cleanly.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@steipete

steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 9, 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 repository_dispatch).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 9, 2026
@steipete
steipete merged commit 13cb098 into openclaw:main Aug 9, 2026
127 checks passed
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 10, 2026
* fix(cron): remove deleted job sessions

* fix(cron): guard deferred session cleanup

* test(cron): remove invalid cleanup await

* fix(cron): fence deferred session cleanup

* fix(cron): share removal cleanup fence

Co-authored-by: Goutam Adwant <[email protected]>

* fix(cron): preserve stored add result type

Co-authored-by: Goutam Adwant <[email protected]>

* fix(cron): release lock before session cleanup

Co-authored-by: Goutam Adwant <[email protected]>

* fix(cron): avoid materializing cleanup store

Co-authored-by: Goutam Adwant <[email protected]>

---------

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

Labels

merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: L 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.

[Bug]: cron.remove does not clean up session records in sessions.json

2 participants