Skip to content

Fix/heartbeat task due clock skew - #64611

Closed
Rahulkumar070 wants to merge 3 commits into
openclaw:mainfrom
Rahulkumar070:fix/heartbeat-task-due-clock-skew
Closed

Rahulkumar070 wants to merge 3 commits into
openclaw:mainfrom
Rahulkumar070:fix/heartbeat-task-due-clock-skew

Conversation

@Rahulkumar070

Copy link
Copy Markdown
Contributor

Summary

  • Problem: isTaskDue silently fails when system clock moves backward (NTP correction, VM resume, timezone change). nowMs - lastRunMs becomes negative so the task never runs.
  • Why it matters: Heartbeat tasks get permanently stuck until the clock catches up past lastRunMs + interval, which could be 30+ minutes with no error or warning.
  • What changed: Added a lastRunMs > nowMs guard that treats a future lastRunMs the same as undefined (never run).
  • What did NOT change: All existing behavior for normal clock progression is unchanged.

Change Type (select all)

  • Bug fix

Scope (select all touched areas)

  • Gateway / orchestration

Linked Issue/PR

  • This PR fixes a bug or regression

Root Cause (if applicable)

  • Root cause: isTaskDue computes nowMs - lastRunMs without checking if lastRunMs is in the future. A backward clock jump makes this negative, so the condition >= intervalMs is never true.
  • Missing detection / guardrail: No guard for lastRunMs > nowMs (clock skew scenario).
  • Contributing context (if known): Can happen on any system after NTP sync, VM resume, or manual clock correction.

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
  • Target test or file: src/auto-reply/heartbeat.test.ts
  • Scenario the test should lock in: isTaskDue returns true when lastRunMs > nowMs
  • Why this is the smallest reliable guardrail: Pure function, no dependencies, single condition check.
  • Existing test that already covers this (if any): None — isTaskDue had zero tests before this PR.
  • If no new test is added, why not: N/A — test added.

User-visible / Behavior Changes

Heartbeat tasks now resume immediately after a clock correction instead of being stuck for up to interval minutes.

Diagram (if applicable)

Before:
[clock skew] -> lastRunMs=future -> nowMs - lastRunMs < 0 -> task stuck forever

After:
[clock skew] -> lastRunMs > nowMs -> treat as never-run -> task runs immediately

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: Node.js
  • Model/provider: N/A
  • Integration/channel (if any): N/A

Steps

  1. Set lastRunMs to a future timestamp (simulating clock skew)
  2. Call isTaskDue(futureMs, "30m", nowMs)
  3. Observe result

Expected

  • true (task should run)

Actual (before fix)

  • false (task permanently stuck)

Evidence

  • Failing test before + passing after (new unit tests added)

Human Verification (required)

  • Verified scenarios: clock skew (lastRunMs in future), normal elapsed interval, interval not yet elapsed, undefined lastRunMs
  • Edge cases checked: lastRunMs exactly equal to nowMs (returns false, correct — not due yet)
  • What you did NOT verify: actual NTP correction on live system

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Risks and Mitigations

  • Risk: Tasks that were intentionally deferred by setting a future lastRunMs would now run immediately.
    • Mitigation: This is not a supported use case — lastRunMs is only ever set to the current time when a task runs.

@greptile-apps

greptile-apps Bot commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a lastRunMs > nowMs guard to isTaskDue in src/auto-reply/heartbeat.ts so that a future lastRunMs caused by backward clock movement (NTP correction, VM resume) is treated identically to "never run," unblocking stuck heartbeat tasks. The change is a one-liner with four accompanying unit tests covering the undefined, elapsed, not-elapsed, and clock-skew cases.

Confidence Score: 5/5

This PR is safe to merge — the fix is minimal, correctly scoped, and fully tested.

The one-line guard is logically correct, preserves all existing behavior for normal clock progression, and is well-covered by the new unit tests. No P0 or P1 findings.

No files require special attention.

Reviews (1): Last reviewed commit: "fix: treat future lastRunMs as never-run..." | Re-trigger Greptile

@clawsweeper

clawsweeper Bot commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Keep open: current main and v2026.6.11 still lack the clock-skew guard, and the PR is a narrow, source-correct fix, but it needs real behavior proof, current-base validation, and maintainer acceptance of the heartbeat task-state semantic change before merge.

Canonical path: Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review.

So I’m closing this here because the remaining work is already tracked in the canonical issue.

Review details

Best possible solution:

Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review.

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

Yes from source inspection: current main and v2026.6.11 compute nowMs - lastRunMs and compare it to the interval, so isTaskDue(now + 5m, "30m", now) returns false. I did not run a live NTP or VM-resume scenario in this read-only review.

Is this the best way to solve the issue?

Yes for the code shape: the shared isTaskDue predicate is the narrow owner boundary, while clamping at runner callers would duplicate the rule. The proposed branch is not merge-ready until proof and current-base validation are in place.

Security review:

Security review cleared: Security review cleared: the diff only changes a local heartbeat due-time helper and colocated unit tests, with no dependency, CI, credential, network, permission, or code-execution surface changes.

AGENTS.md: found and applied where relevant.

What I checked:

  • stale F-rated PR: PR was opened 2026-04-11T03:19:12Z, is older than 60 days, and the latest review rated it F.
  • proof blocker: real behavior proof is mock_only and proof tier is F, so this branch is not merge-ready without contributor follow-up.
  • no human follow-up: live comments and timeline hydrated by apply contain no non-automation activity after the ClawSweeper review.

Likely related people:

  • chinar-amrutkar: History shows the heartbeat task batching series added isTaskDue, task selection, and heartbeatTaskState. (role: introduced heartbeat task batching behavior; confidence: high; commits: 103bebd651a5, ba0942670743, cebea1bf956b; files: src/auto-reply/heartbeat.ts, src/infra/heartbeat-runner.ts)
  • steipete: The task-batching commits were committed by Peter Steinberger, and later heartbeat scheduler/timeout fixes in the same runtime area are under this handle's history. (role: feature committer and recent heartbeat area contributor; confidence: medium; commits: 103bebd651a5, bbc4bee7a22b, 31169ff3b436; files: src/auto-reply/heartbeat.ts, src/infra/heartbeat-runner.ts)
  • Rahulkumar070: Beyond this PR, this contributor authored the merged parser fix touching the same heartbeat helper and colocated tests. (role: recent adjacent contributor; confidence: medium; commits: 3b57af038858; files: src/auto-reply/heartbeat.ts, src/auto-reply/heartbeat.test.ts)
  • jalehman: The nearby merged heartbeat parser PR was reviewed and merged by this person, making them a practical routing candidate for adjacent helper/test behavior. (role: adjacent reviewer and merger; confidence: medium; commits: 3b57af038858; files: src/auto-reply/heartbeat.ts, src/auto-reply/heartbeat.test.ts)

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

@clawsweeper

clawsweeper Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes:

This PR changes heartbeat task due calculation to treat a future lastRunMs as due and adds unit coverage for never-run, elapsed, not-elapsed, and clock-skew cases.

Maintainer follow-up before merge:

This is already an open contributor implementation PR; the remaining action is maintainer review, rebase or cherry-pick, and validation rather than an automated replacement fix PR.

Review details

Best possible solution:

Heartbeat task due checks should treat future last-run timestamps as due, preserve normal elapsed/not-elapsed behavior, and keep the regression locked in with focused isTaskDue unit coverage.

Acceptance criteria:

  • pnpm test src/auto-reply/heartbeat.test.ts
  • pnpm check:changed

What I checked:

  • Current main lacks clock-skew guard: At current main, isTaskDue returns true only when lastRunMs is undefined, then compares nowMs - lastRunMs >= intervalMs; with lastRunMs > nowMs, that difference is negative and the task is still treated as not due. (src/auto-reply/heartbeat.ts:289, acae48b790fa)
  • Heartbeat task mode depends on this helper: Task-mode heartbeat filters parsed tasks through isTaskDue; if none pass, the run returns reason: "no-tasks-due", so this helper controls whether skewed tasks run. (src/infra/heartbeat-runner.ts:729, acae48b790fa)
  • Task state is documented as last-run state: Heartbeat docs describe heartbeatTaskState as per-task last-run timestamps that survive restarts and advance after normal completion, not as a documented future deferral mechanism. Public docs: docs/gateway/heartbeat.md. (docs/gateway/heartbeat.md:424, acae48b790fa)
  • Current tests omit the skew case: Current heartbeat tests import parsing/content helpers but not isTaskDue, and the file has no future-timestamp due regression coverage. (src/auto-reply/heartbeat.test.ts:2, acae48b790fa)
  • PR patch matches the remaining defect: The provided PR diff adds lastRunMs > nowMs to the due guard and adds tests for never-run, elapsed, not-yet-elapsed, and future timestamp behavior. (src/auto-reply/heartbeat.ts:288, 903a00950f0a)
  • Security review: The provided PR file list is limited to src/auto-reply/heartbeat.ts and src/auto-reply/heartbeat.test.ts; it does not touch CI workflows, action refs, dependency sources, lockfiles, install/build/release scripts, publishing metadata, permissions, secrets, downloaded artifacts, network calls, or command execution paths. (903a00950f0a)

Likely related people:

  • steipete: Available current-main blame/log for the heartbeat helper, runner, tests, and docs points to a972c9e, making this a practical maintainer routing candidate for heartbeat task behavior. (role: recent maintainer; confidence: medium; commits: a972c9ec4547; files: src/auto-reply/heartbeat.ts, src/auto-reply/heartbeat.test.ts, src/infra/heartbeat-runner.ts)

Remaining risk / open question:

  • The source branch includes earlier parseHeartbeatTasks commits that current main already appears to have, so maintainers may need a rebase or a cherry-pick of only the clock-skew guard and tests.
  • No validation was run in this read-only review; targeted heartbeat unit tests and the changed-lane gate still need to pass before merge.

Codex review notes: model gpt-5.5, reasoning high; reviewed against acae48b790fa.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels May 19, 2026
@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. label May 19, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels May 20, 2026
@clawsweeper

clawsweeper Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@RomneyDa

Copy link
Copy Markdown
Member

Heads up: this PR needs to be updated against current main before the new required Dependency Guard check can pass.

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Jun 13, 2026
@clawsweeper clawsweeper Bot added the merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. label Jun 13, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Jun 14, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added stale Marked as stale due to inactivity triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jul 18, 2026
@clawsweeper

clawsweeper Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: Fix/heartbeat task due clock skew This is item 1/1 in the current shard. Shard 9/22.

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.

@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Jul 19, 2026
@steipete

Copy link
Copy Markdown
Contributor

Thank you for identifying and testing the backward-clock edge case; that insight is valid. This particular implementation has since been superseded by the merged heartbeat-task migration in #113165 (commit b23c7bd0f84), which removes both legacy isTaskDue and parseHeartbeatTasks rather than retaining the old in-prompt task scheduler. Current heartbeat tasks are independently scheduled cron jobs; the system-owned heartbeat-monitor path and src/infra/heartbeat-runner.scheduler.test.ts cover canonical cadence and execution. The remaining far-future cron run-marker case is separately tracked and addressed by #111743. Because this branch changes and tests a helper that no longer exists on main, closing this PR as superseded rather than merging dead API back into the current scheduler. Thank you again for the original regression tests and analysis.

@steipete steipete closed this Jul 27, 2026
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. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants