Skip to content

feat(cron): system-owned heartbeat monitor jobs replace the dedicated interval scheduler - #112585

Merged
steipete merged 9 commits into
mainfrom
claude/heartbeat-cron-monitor
Jul 22, 2026
Merged

steipete merged 9 commits into
mainfrom
claude/heartbeat-cron-monitor

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Phase 2 (first stage) of the automation-unification program (#110950): OpenClaw ran two independent schedulers — the cron service and a dedicated per-agent heartbeat interval timer buried in heartbeat-runner.ts (scheduleNext + state.timer + phase-seek machinery). Two cadence owners meant duplicated scheduling logic, invisible heartbeat cadence (no cron listing, no run records), and no path to the RFC's "everything is a cron" end state.

Why This Change Was Made

Heartbeat cadence moves into the cron scheduler as system-owned monitor jobs, while everything the heartbeat runner is actually good at stays put:

  • New internal payload kind {kind:"heartbeat"}: executing the job only pokes requestHeartbeat({source:"interval", intent:"scheduled"}). The wake queue keeps coalescing/busy-retry; the runner keeps its cooldown gate (nextDueMs), quiet-hours guard, flood guard, and per-agent dispatch. A tick outside active hours is skipped by the exact guards that governed the old timer — behavior-preserving by construction.
  • Gateway convergence (server-cron-heartbeat-jobs.ts): one declaration-keyed job per heartbeat-enabled agent (heartbeat:<agentId>, schedule: every + deterministic per-agent phase anchor so multi-agent beats stay spread), converged at cron start and on config reload; monitors for unconfigured agents are pruned. Config remains the single source of truth — the job is not client-writable (reported in the protocol job schema, rejected by create/patch schemas, read-only in the Control UI).
  • The dedicated timer is deleted: scheduleNext, state.timer, the setTimeout-cap clamp, and the self-poke are gone from heartbeat-runner.ts. Event-driven wakes (exec events, hooks, tasks, session-state) are untouched.

Timing safety: cron fires at/after the anchor-aligned boundary (setTimeout never fires early), both sides derive the boundary from the same phase function and seed, and the wake queue's coalesce delay only lands the handler past the due time — so the runner's not-due cooldown cannot eat a legitimate tick.

Not in this PR (follow-ups tracked on #110950): migrating agents.*.heartbeat config keys into cron job records via doctor, database-backed per-job scratch space, and the HEARTBEAT.md → database migration.

User Impact

Heartbeat behavior is unchanged (same cadence, same guards, same delivery). Operators gain visibility: openclaw cron list --all now shows each agent's heartbeat monitor ("Heartbeat ()") with its schedule and run state. Docs updated (docs/gateway/heartbeat.md, docs/automation/cron-jobs.md).

Evidence

  • New regressions: monitor convergence/prune/failure-containment (src/gateway/server-cron-heartbeat-jobs.test.ts), heartbeat payload fires an interval wake with no system event (src/cron/service.heartbeat-payload.test.ts).
  • Scheduler tests converted from timer self-fire to wake-queue poke stimulus (heartbeat-runner.scheduler.test.ts, 25 green; heartbeat-runner.active-hours-schedule.e2e.test.ts rewritten, 7 green); timer-mechanics-only tests deleted with the timer.
  • Full sweeps green: src/cron + gateway cron suites (145 files / 1,599 tests), all 30 src/infra/heartbeat* files + server-runtime-services (267 tests).
  • tsgo core + core-test lanes clean; oxlint clean, no new suppressions; oxfmt applied. Net diff +629/−504.
  • Structured autoreview (Codex gpt-5.6-sol, xhigh) over the full branch diff: six cycles. Every accepted finding fixed with a regression (commitment fan-out on targeted ticks, cron-disabled fallback timer incl. clamp chaining, reconcile serialization with bounded retry, complete system-ownership boundary across create/upsert/patch/delete/prune). Final cycle: all three remaining findings refuted with cited code (add/persistUpdatedJob arm the scheduler timer; reconvergence shares the exact restart-heartbeat reload trigger the old scheduler used; interval ticks intentionally run on enrollment snapshots as the replaced broadcast timer did — recorded inline).
  • No packaging, lazy-boundary, or dynamic-import changes (one new statically-imported gateway module); exact-head CI validates the build lanes before merge.

@steipete
steipete marked this pull request as ready for review July 22, 2026 07:20
@steipete
steipete requested a review from a team as a code owner July 22, 2026 07:20
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: web-ui App: web-ui gateway Gateway runtime size: XL maintainer Maintainer-authored PR labels Jul 22, 2026
steipete added 8 commits July 22, 2026 00:21
…scheduler

- new internal cron payload kind {kind:"heartbeat"}: execution pokes
  requestHeartbeat({source:"interval"}); reported in the protocol job
  schema, not accepted from client create/patch
- gateway converges one declaration-keyed monitor job per heartbeat-enabled
  agent (schedule every+deterministic phase anchor) at startup and on
  config reload; removes monitors for unconfigured agents
- heartbeat runner loses its interval setTimeout machinery; nextDueMs
  stays as the cooldown gate, event wakes unchanged
- converge/prune/failure-containment tests for heartbeat monitor jobs
- heartbeat payload run fires an interval wake, no system event
- scheduler tests converted from timer self-fire to wake-queue pokes;
  timer-mechanics-only tests deleted with the timer
- persisted-shape accepts the heartbeat payload kind
- docs: heartbeat cadence ownership + system payload kind
- targeted cron-monitor interval ticks use the full per-agent path so
  due-commitment sessions still deliver
- cron-disabled gateways keep a local fallback interval timer (shipped
  cron.enabled=false contract; removed when heartbeat config folds into
  cron in #110950)
- heartbeat job reconciliations serialize with latest-wins epochs and a
  bounded 30s retry after a failed convergence pass
- targeted monitor redirect skips wakes carrying heartbeat overrides and
  surfaces the per-agent terminal skip reason instead of not-due
- cron-disabled fallback timer re-arms with a 1s floor after each firing
  so a dropped wake cannot end the chain
- heartbeat payloads are system-owned at the service boundary: add requires
  the gateway opt-in, patches to the kind are rejected
- prune only jobs proven to be monitors (prefix AND heartbeat payload)
- existing monitors reject every update patch; declarative upserts on the
  monitor key require the gateway opt-in even with a different payload
- converge scopes declarative matching to real monitors so a colliding
  user job with the same key is never adopted or overwritten
- monitor removal requires the gateway systemOwned opt-in; ad-hoc
  API/CLI deletion is rejected, reconciliation cleanup still prunes
@steipete
steipete force-pushed the claude/heartbeat-cron-monitor branch from f58669d to 6045a74 Compare July 22, 2026 07:23
@clawsweeper

clawsweeper Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: feat(cron): system-owned heartbeat monitor jobs replace the dedicated interval scheduler This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@steipete
steipete merged commit 4e9ae9f into main Jul 22, 2026
119 checks passed
@steipete
steipete deleted the claude/heartbeat-cron-monitor branch July 22, 2026 21:03
@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 23, 2026
… interval scheduler (openclaw#112585)

* feat(cron): system-owned heartbeat monitor jobs replace the interval scheduler

- new internal cron payload kind {kind:"heartbeat"}: execution pokes
  requestHeartbeat({source:"interval"}); reported in the protocol job
  schema, not accepted from client create/patch
- gateway converges one declaration-keyed monitor job per heartbeat-enabled
  agent (schedule every+deterministic phase anchor) at startup and on
  config reload; removes monitors for unconfigured agents
- heartbeat runner loses its interval setTimeout machinery; nextDueMs
  stays as the cooldown gate, event wakes unchanged

* test(cron): heartbeat monitor regressions; docs for cron-owned cadence

- converge/prune/failure-containment tests for heartbeat monitor jobs
- heartbeat payload run fires an interval wake, no system event
- scheduler tests converted from timer self-fire to wake-queue pokes;
  timer-mechanics-only tests deleted with the timer
- persisted-shape accepts the heartbeat payload kind
- docs: heartbeat cadence ownership + system payload kind

* fix(cron): heartbeat monitor review round 1

- targeted cron-monitor interval ticks use the full per-agent path so
  due-commitment sessions still deliver
- cron-disabled gateways keep a local fallback interval timer (shipped
  cron.enabled=false contract; removed when heartbeat config folds into
  cron in openclaw#110950)
- heartbeat job reconciliations serialize with latest-wins epochs and a
  bounded 30s retry after a failed convergence pass

* fix(cron): chain clamped fallback heartbeat timers past the setTimeout cap

* fix(cron): heartbeat monitor review round 3

- targeted monitor redirect skips wakes carrying heartbeat overrides and
  surfaces the per-agent terminal skip reason instead of not-due
- cron-disabled fallback timer re-arms with a 1s floor after each firing
  so a dropped wake cannot end the chain
- heartbeat payloads are system-owned at the service boundary: add requires
  the gateway opt-in, patches to the kind are rejected

* fix(cron): heartbeat monitor review round 4 — full ownership enforcement

- prune only jobs proven to be monitors (prefix AND heartbeat payload)
- existing monitors reject every update patch; declarative upserts on the
  monitor key require the gateway opt-in even with a different payload

* fix(cron): complete heartbeat monitor ownership boundary

- converge scopes declarative matching to real monitors so a colliding
  user job with the same key is never adopted or overwritten
- monitor removal requires the gateway systemOwned opt-in; ad-hoc
  API/CLI deletion is rejected, reconciliation cleanup still prunes

* docs(cron): record intentional enrollment-snapshot semantics for monitor ticks

* fix(cron): repair heartbeat monitor CI gates
JFWaskin pushed a commit to JFWaskin/openclaw that referenced this pull request Aug 5, 2026
…nclaw#79192)

The original PR openclaw#79192 targeted the pre-openclaw#112585 monolithic heartbeat-runner
architecture. That architecture has been fully retired on main; this is a
v2 rewrite that re-anchors the feature on the new
heartbeat-cooldown.ts + cron monitor rows + split cron service seams.

What is preserved:
  * cron.maintenance config (window + maintenanceAgents + allowManualRun)
  * daily maintenance window with timezone awareness
  * role isolation: non-roster agents are deferred during the window
  * deferred replay in FIFO order on phase exit
  * manual run gate (force pierces; allowManualRun opt-in)
  * Heartbeat wake deferral with reason 'maintenance-window'
  * cron.status / CronJobState additive diagnostics

What is intentionally different from the original:
  * No src/infra/maintenance-phase.ts (the module is gone in main); the
    decision logic lives in src/cron/maintenance-policy.ts
  * shouldDeferWake gains a maintenanceWindow input; manual intent is the
    sole pierce path (D1: allowManualRun is the opt-in knob)
  * Manual run admission goes through inspectManualRunPreflight, which
    returns 'maintenance-blocked' (added to CronRunResult reason union)
  * Hot reload of cron.maintenance.* requires a cron service restart in
    v2; a v3 follow-up will thread live config into the running service

Validation:
  * pnpm tsgo:core            (passes)
  * pnpm tsgo:core:test       (passes)
  * 66 new unit tests pass
  * Protocol conformance preserved (CronJobState gains 3 additive fields)
  * Evidence: artifacts/pr79192-v2-maintenance-proof.mjs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant