Skip to content

feat(cron): database-backed heartbeat monitor scratch replaces HEARTBEAT.md - #112967

Merged
steipete merged 20 commits into
mainfrom
claude/heartbeat-scratch-migration
Jul 23, 2026
Merged

steipete merged 20 commits into
mainfrom
claude/heartbeat-scratch-migration

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Part of the automation-unification program (#110950). Heartbeat cadence already moved to system-owned cron monitor jobs (#112585), but heartbeat context still lived in a workspace HEARTBEAT.md file the runtime re-read on every tick — a legacy file store the storage policy says belongs in SQLite, with no revision safety and no way for the monitor to own its own context.

Why This Change Was Made

  • Database-backed per-job scratch. New additive cron_job_scratch table in the shared state DB (no schema-version bump; declared in the canonical schema + idempotent ensure). 256 KiB cap, compare-and-swap revisions, and a content-NULL tombstone so the revision lineage stays monotonic across unset/recreate — a stale writer can never resurrect old content (ABA-safe).
  • Heartbeat runner reads scratch, not files. Preflight resolves the agent's monitor job + scratch in one SQLite query. Missing-vs-empty semantics are preserved (empty-heartbeat-file skip, tasks: parsing, prose appended to the prompt). All steady-state HEARTBEAT.md reads, the workspace path hint, and the bootstrap-file injection/filter paths are deleted.
  • Agent self-editing. heartbeat_respond gains an optional scratch parameter that atomically replaces the monitor scratch for future heartbeats. The value never echoes back into model-visible tool output (only scratchUpdated/scratchBytes), and reply payloads carry it via a non-enumerable symbol so it cannot leak through channel data.
  • Doctor-owned migration. openclaw doctor --fix imports each enrolled agent's HEARTBEAT.md into its monitor's scratch: SHA-256 provenance, verify-after-write, archive under <state>/backups/heartbeat-migration/, then remove the file. Symlinks escaping the workspace, hard links, non-UTF-8, and >256 KiB files are refused with warnings; reruns are idempotent; operator-edited scratch is never overwritten.
  • Operator surfaces. Admin-only cron.scratch.get/cron.scratch.set gateway RPCs (closed schemas expose only content/revision/updatedAt + currentRevision; migration provenance stays internal) and openclaw cron scratch <jobId> [--set|--file|--unset|--expected-revision].
  • Cleanup. Workspace bootstrap no longer seeds HEARTBEAT.md; disabled heartbeats keep a stable disabled monitor row (scratch survives re-enable); job removal prunes scratch; the dead Codex HEARTBEAT.md collaboration-guidance plumbing is deleted and prompt snapshots regenerated.

Converting individual tasks: entries into standalone cron jobs is intentionally deferred to a follow-up (semantic change).

User Impact

Heartbeat checklists live in the cron monitor's private scratch instead of a workspace file. Existing setups migrate with openclaw doctor --fix (original file archived). Default heartbeat prompt copy changed accordingly. agents.defaults.skipOptionalBootstrapFiles: ["HEARTBEAT.md"] remains accepted as a no-op.

Evidence

  • Focused local proof (post-rebase on b9a28cd): scratch store (CAS/tombstone/provenance), doctor migration (preview/migrate/idempotent rerun, operator-scratch conflict, external-symlink rejection), heartbeat monitor reconcile, heartbeat runner suites (65 tests across 5 files incl. tool-response scratch commit + no-leak), workspace/bootstrap/system-prompt, doctor health contributions, cron CLI, gateway cron validation, codex attempt-context + thread-lifecycle.
  • tsgo core + core-test + extensions + extensions-test green; oxlint/oxfmt clean; pnpm build green; pnpm prompt:snapshots:gen/check current; max-lines ratchet OK.
  • Codex autoreview (gpt-5.6-sol, high): initial pass returned 3 findings (revision-ABA on unset, provenance leak through the closed result schema, stale Codex HEARTBEAT.md guidance) — all fixed in this PR with regression coverage.
  • Known local proof gap: extensions/codex/src/app-server/run-attempt.test.ts stalls on this heavily loaded host (load >100, swap exhausted; stalls identically without the diff-relevant shards); relying on hosted CI for that lane.

@steipete
steipete marked this pull request as ready for review July 23, 2026 07:44
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: web-ui App: web-ui gateway Gateway runtime cli CLI command changes scripts Repository scripts commands Command implementations agents Agent runtime and tooling extensions: codex size: XL maintainer Maintainer-authored PR labels Jul 23, 2026
@steipete
steipete requested a review from a team as a code owner July 23, 2026 08:55
@openclaw-barnacle openclaw-barnacle Bot added the app: android App: android label Jul 23, 2026
steipete added 16 commits July 23, 2026 09:56
…scratch

- new additive cron_job_scratch SQLite table (revision tombstones keep
  compare-and-swap monotonic across unset/recreate; 256KiB cap)
- heartbeat runner reads monitor scratch instead of workspace HEARTBEAT.md;
  heartbeat_respond gains a private scratch replacement parameter
- openclaw doctor --fix migrates HEARTBEAT.md into scratch (hash-verified,
  archived under state backups, idempotent, symlink-contained)
- gateway cron.scratch.get/set admin RPCs + openclaw cron scratch CLI
- workspace bootstrap no longer seeds HEARTBEAT.md; Codex heartbeat file
  guidance removed; docs and prompt snapshots updated
…non-default agent monitors

- doctor migration groups agents by heartbeat source file and imports into
  every monitor before archiving/removing the shared file once
- exempt heartbeat payloads from the main-session default-agent restriction:
  monitors only poke the wake bus, so non-default agents converge again
- document why disabled monitors retain their last cadence (config default
  already resolves before the fallback)
…m on release, archive first, report scratch as pending
…since-train, inventory path, prompt snapshot, regenerated docs map, SDK baseline, protocol bindings
@steipete
steipete force-pushed the claude/heartbeat-scratch-migration branch from 60d1fc5 to 35014d5 Compare July 23, 2026 16:59
@steipete
steipete merged commit 3e2b3ea into main Jul 23, 2026
122 checks passed
@steipete
steipete deleted the claude/heartbeat-scratch-migration branch July 23, 2026 18:10
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 24, 2026
…EAT.md (openclaw#112967)

* feat(cron): move heartbeat context into database-backed per-job cron scratch

- new additive cron_job_scratch SQLite table (revision tombstones keep
  compare-and-swap monotonic across unset/recreate; 256KiB cap)
- heartbeat runner reads monitor scratch instead of workspace HEARTBEAT.md;
  heartbeat_respond gains a private scratch replacement parameter
- openclaw doctor --fix migrates HEARTBEAT.md into scratch (hash-verified,
  archived under state backups, idempotent, symlink-contained)
- gateway cron.scratch.get/set admin RPCs + openclaw cron scratch CLI
- workspace bootstrap no longer seeds HEARTBEAT.md; Codex heartbeat file
  guidance removed; docs and prompt snapshots updated

* fix(cron): review round 2 — shared-workspace heartbeat migration and non-default agent monitors

- doctor migration groups agents by heartbeat source file and imports into
  every monitor before archiving/removing the shared file once
- exempt heartbeat payloads from the main-session default-agent restriction:
  monitors only poke the wake bus, so non-default agents converge again
- document why disabled monitors retain their last cadence (config default
  already resolves before the fallback)

* fix(cron): honor configured cron store, legacy heartbeat fallback, and safer doctor claim

* fix(cron): claim HEARTBEAT.md before committing scratch and restore without clobbering

* fix(cron): pin migration CAS to precondition revision, re-verify claim on release, archive first, report scratch as pending

* docs(heartbeat): remove retired config options

* fix(cron): crash-recoverable migration claims, partial-import rollback, latest-response scratch pairing

* test(heartbeat): keep latest scratch proposal paired

* fix(cron): roll back committed scratch on changed-claim release and restore no-row state

* fix(cron): revision-guarded rollback delete and recreated-file detection on claim release

* fix(cron): treat every failed claim re-verification as a migration conflict

* test(heartbeat): rename ack test after ackMaxChars retirement

* fix(heartbeat): keep monitor scratch out of bypass-scope runs

* fix(cron): resolve claimed symlinks on release and gate legacy fallback on proven scratch state

* fix(cron): strict claim-name recovery and per-entry migration grouping

* fix(ci): heartbeat scratch gate repairs — lint causes, dead exports, since-train, inventory path, prompt snapshot, regenerated docs map, SDK baseline, protocol bindings

* fix(cron): live-owner claim guard and canonical entry-key migration grouping

* fix(cron): archive claimed inode on release and flag orphan claims beside recreated files

* docs(cron): document process-global state-db invariant for scratch service ops

* chore(i18n): refresh native inventory line numbers after protocol binding regen
obviyus added a commit that referenced this pull request Sep 2, 2026
…5508)

Closes #135457

## What Problem This Solves

The Control UI showed an empty **Heartbeat monitor** field for every heartbeat automation, even when the Gateway already held private per-job scratch. The command-line interface showed the stored scratch correctly.

`jobToForm` projects text from public cron payloads. A heartbeat payload has no text because its monitor instructions live in private scratch. The page never called the existing `cron.scratch.get` method, so the read-only field always received the empty fallback.

## Fix

- Fetch scratch once when an administrator selects a heartbeat automation.
- Keep the result in ephemeral page display state, outside `CronFormState` and all add or update payloads.
- Revalidate the Gateway connection, selected job, request generation, heartbeat payload kind, and current administrator access after the request resolves.
- Clear display state when the connection, access, selection, or panel changes.
- Show scratch-read failures instead of presenting a false empty success state.
- Keep `cron.scratch.get` at `operator.admin` and keep scratch out of `cron.list`, `cron.get`, and `CronJob`.

Command, script, and skill-review automation payloads still use their existing read-only projection.

## Evidence

Baseline main: `2837afb07dc9faa27f631760b5f4de9dbb8de940`

Sanitized real-Gateway screenshot and observation: #135508 (comment)

- Red, real Gateway: existing heartbeat scratch was returned by the Gateway, but the Control UI field stayed empty and sent no `cron.scratch.get` request.
- Green, real Gateway: the field displayed the exact stored scratch and remained read-only.
- The green observation recorded one `cron.scratch.get`, an empty mutable `payloadText`, and no scratch in `cron.list` or `cron.get`.
- Mock browser end-to-end tests cover the visible authorized result, the read-only operator path, and a visible read failure: 3 passed.
- The cron page unit suite covers an in-flight administrator-to-read-only downgrade: 35 passed.
- Real-Gateway browser end-to-end proof: 1 passed.
- Oxfmt, Oxlint, and `git diff --check` passed.

Production: `+56/-3`. Tests and test support: `+172/-10`.

## Regression provenance

PR #112967 introduced private heartbeat scratch without changing the existing UI projection. PR #130030 later made the empty projection explicit with the **Heartbeat monitor** label.

## AI assistance

AI-assisted repair and validation. A maintainer independently inspected the storage owner, Gateway authorization, protocol schemas, UI projection and save boundaries, sibling payload kinds, tests, docs, history, and real browser behavior.

Co-authored-by: gaoanze <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 3, 2026
…nclaw#135508)

Closes openclaw#135457

## What Problem This Solves

The Control UI showed an empty **Heartbeat monitor** field for every heartbeat automation, even when the Gateway already held private per-job scratch. The command-line interface showed the stored scratch correctly.

`jobToForm` projects text from public cron payloads. A heartbeat payload has no text because its monitor instructions live in private scratch. The page never called the existing `cron.scratch.get` method, so the read-only field always received the empty fallback.

## Fix

- Fetch scratch once when an administrator selects a heartbeat automation.
- Keep the result in ephemeral page display state, outside `CronFormState` and all add or update payloads.
- Revalidate the Gateway connection, selected job, request generation, heartbeat payload kind, and current administrator access after the request resolves.
- Clear display state when the connection, access, selection, or panel changes.
- Show scratch-read failures instead of presenting a false empty success state.
- Keep `cron.scratch.get` at `operator.admin` and keep scratch out of `cron.list`, `cron.get`, and `CronJob`.

Command, script, and skill-review automation payloads still use their existing read-only projection.

## Evidence

Baseline main: `1e7394c79ab1f55e912791169dee1bd31052159d`

Sanitized real-Gateway screenshot and observation: openclaw#135508 (comment)

- Red, real Gateway: existing heartbeat scratch was returned by the Gateway, but the Control UI field stayed empty and sent no `cron.scratch.get` request.
- Green, real Gateway: the field displayed the exact stored scratch and remained read-only.
- The green observation recorded one `cron.scratch.get`, an empty mutable `payloadText`, and no scratch in `cron.list` or `cron.get`.
- Mock browser end-to-end tests cover the visible authorized result, the read-only operator path, and a visible read failure: 3 passed.
- The cron page unit suite covers an in-flight administrator-to-read-only downgrade: 35 passed.
- Real-Gateway browser end-to-end proof: 1 passed.
- Oxfmt, Oxlint, and `git diff --check` passed.

Production: `+56/-3`. Tests and test support: `+172/-10`.

## Regression provenance

PR openclaw#112967 introduced private heartbeat scratch without changing the existing UI projection. PR openclaw#130030 later made the empty projection explicit with the **Heartbeat monitor** label.

## AI assistance

AI-assisted repair and validation. A maintainer independently inspected the storage owner, Gateway authorization, protocol schemas, UI projection and save boundaries, sibling payload kinds, tests, docs, history, and real browser behavior.

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

Labels

agents Agent runtime and tooling app: android App: android app: web-ui App: web-ui cli CLI command changes commands Command implementations docs Improvements or additions to documentation extensions: codex gateway Gateway runtime maintainer Maintainer-authored PR scripts Repository scripts size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant