Skip to content

Defer heartbeat delivery while sessions are active - #265

Merged
kevinjosethomas merged 4 commits into
mainfrom
feature/heartbeat-skip-busy
Jun 29, 2026
Merged

Defer heartbeat delivery while sessions are active#265
kevinjosethomas merged 4 commits into
mainfrom
feature/heartbeat-skip-busy

Conversation

@sethkarten

@sethkarten sethkarten commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Defer user /heartbeat and internal rlm_heartbeat cron jobs when the target session is already active.
  • Treat streaming, running bash, or an existing pending follow-up as active work for heartbeat delivery.
  • Preserve existing generic cron behavior: non-heartbeat cron jobs still queue behind busy sessions.

Why

Heartbeat prompts should not pile onto an agent that is already working. If a heartbeat fires while the target session is active, the scheduler now treats that run as skipped and reschedules the next interval instead of enqueueing a follow-up message.

Tests

  • npm test -- cron-jobs.test.ts daemon-mode.test.ts
  • npm run build
  • pre-commit npm run check
  • isolated daemon smoke test: set a heartbeat, queued existing work, waited past the heartbeat interval, and verified no heartbeat follow-up was queued while the job remained scheduled for the next interval

Note

Medium Risk
Changes daemon scheduling and message delivery for heartbeats versus ordinary crons; misclassification could drop heartbeats or alter queue behavior, but behavior is covered by unit tests.

Overview
Heartbeat cron jobs (heartbeat and rlm_heartbeat) no longer enqueue follow-ups when the target session is busy. The daemon returns "skipped" instead, and the scheduler reschedules the next interval via recordSkipResult, which now records lastSkippedAt (also shown in formatAgentCronJob).

Busy is defined by new shouldDeferHeartbeatCronJob: streaming, bash running, or any pending follow-up messages. Generic cron behavior is unchanged—busy sessions still get followUp for non-heartbeat jobs.

When a cron job actually runs on an idle session, delivery goes through session.prompt with streamingBehavior: "followUp" (and heartbeat-specific followUpQueueKey when applicable), so a stream that starts mid-dispatch still queues safely instead of colliding.

Reviewed by Cursor Bugbot for commit da4ff04. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Defer heartbeat cron job delivery while sessions are streaming, running bash, or have pending messages

  • Adds shouldDeferHeartbeatCronJob and isHeartbeatCronJob helpers in cron-jobs.ts that check a HeartbeatCronSessionActivity snapshot to decide whether to skip execution.
  • Updates AgentDaemon.runCronJob in daemon-mode.ts to call shouldDeferHeartbeatCronJob before scheduling; heartbeats return "skipped" and no follow-up is queued when the session is busy.
  • Behavioral Change: previously, heartbeats for busy sessions were enqueued as follow-ups; now they are silently dropped until the next scheduled interval.

Changes since #265 opened

  • Modified AgentDaemon.runCronJob method to defer heartbeat job execution when sessions are streaming or have pending messages, and removed follow-up queue key logic for all cron jobs [5a73a9b]
  • Added lastSkippedAt timestamp tracking to the AgentCronJob interface and AgentCronJobStore class to record when cron jobs are skipped [5a73a9b]
  • Modified AgentDaemon.runCronJob method to pass streamingBehavior: 'followUp' option when prompting idle sessions for non-heartbeat cron jobs [f4501f8]
  • Modified AgentDaemon.runCronJob method to pass a followUpQueueKey option when invoking state.runtime.session.prompt, setting it to heartbeat:${job.id} for heartbeat cron jobs and leaving it undefined for non-heartbeat cron jobs [da4ff04]
  • Added test assertions to verify that session.prompt is called with the correct followUpQueueKey value for both heartbeat and generic cron jobs, and introduced a new test case for idle generic cron jobs [da4ff04]
📊 Macroscope summarized 5a73a9b. 2 files reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted

🗂️ Filtered Issues

No issues evaluated.

@sethkarten
sethkarten marked this pull request as ready for review June 25, 2026 21:01
@sethkarten sethkarten mentioned this pull request Jun 29, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5a73a9b. Configure here.

Comment thread packages/coding-agent/src/modes/daemon/daemon-mode.ts Outdated
Comment thread packages/coding-agent/src/modes/daemon/daemon-mode.ts Outdated
Comment thread packages/coding-agent/src/modes/daemon/daemon-mode.ts
@kevinjosethomas
kevinjosethomas merged commit 369e341 into main Jun 29, 2026
3 checks passed
@kevinjosethomas
kevinjosethomas deleted the feature/heartbeat-skip-busy branch June 29, 2026 20:49
zhengr pushed a commit to zhengr/prime-agent that referenced this pull request Aug 8, 2026
…#265)

* Defer heartbeat cron jobs while sessions are active

* remove dead heartbeat follow-up plumbing and surface deferred skips

* pass followUp streaming behavior on cron prompt to survive mid-call stream start

* coalesce raced heartbeat follow-ups with a per-job queue key

---------

Co-authored-by: Kevin Thomas <kevin.jt2007@gmail.com>
thomaswillner added a commit to thomaswillner/prime-agent that referenced this pull request Aug 29, 2026
…rimeIntellect-ai#265/PrimeIntellect-ai#264 delivered (#12)

Three draft PRs opened and validated on spx-0dte-bot-v2 (PrimeIntellect-ai#268, PrimeIntellect-ai#269,
PrimeIntellect-ai#270), per the input of record's §7 self-refinement contract: session-
scoped environment and process learnings recorded here for the next
session; repo-relevant artifacts live in the V2 PRs themselves.

Claude-Session: https://claude.ai/code/session_01Su2woe3tXT3wQ1oCCkCDtC

Co-authored-by: thomaswillner <thomaswillner@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
thomaswillner added a commit to thomaswillner/prime-agent that referenced this pull request Aug 29, 2026
…e-notes lesson (#13)

* docs(spx-v2): verification pass, queue state, and self-refinement record

No code written this session — a verification pass over already-delivered
work plus the queue-state answer.

Records, so future sessions do not repeat them:

- The audit-challenge / V1-coverage / rag-tot-cot-challenge / corrected-input
  deliverable ALREADY EXISTS (AUDIT_CHALLENGE sections A-D and
  PRIME_AGENT_INPUT_SPX_V2). An operator prompt has now asked for it in at
  least two sessions; redoing it is inventing work.
- Verified queue state from GitHub: PrimeIntellect-ai#266/PR PrimeIntellect-ai#268 merged (and PrimeIntellect-ai#263 with it,
  now main f64029a); PrimeIntellect-ai#265/PR PrimeIntellect-ai#269 and PrimeIntellect-ai#264/PR PrimeIntellect-ai#270 open with CI in flight;
  PrimeIntellect-ai#272 and PrimeIntellect-ai#271 filed, unstarted, no lane.
- Errors and corrections: settle elapsed time from GitHub workflow-run
  timestamps, never the container clock; add_repo push access was
  classifier-denied so a remote session may hold read-only and cannot push;
  register_repo_root denial falls back to reading CLAUDE.md directly.
- MATS/superpowers/routing settled empirically with the exact commands used,
  so the search is not repeated: they are Mac-harness resident, and PrimeIntellect-ai#272/PrimeIntellect-ai#271
  already carry auto-dispatch, which is what routes them to the maker fleet.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G5B7QM1QLQuWSBMCxiCzS6

* docs(spx-v2): implementation-session addendum — access, setup, CI outage, self-review

Appends the second half of the session to the notes: implementing PrimeIntellect-ai#272 after
the operator corrected two access assumptions.

The corrections matter more than the code:
- push DOES work; "I cannot push" was inferred from add_repo's access label
  rather than tested. A dry-run push proved it. Also: the refspec push form is
  classifier-denied while `git push -u origin <branch>` succeeds.
- this host is not the MacBook (uname, no /Users, no ~/.prime).

Also records the environment setup the Makefile assumes (venv before
v2-install, ruff 0.15.22 via python -m, seeding the gitignored account.yaml,
and proving PYTHONPATH beats editable installs in a worktree), the method that
diagnosed the repo-wide CI outage in two calls (zero recorded steps, then the
same workflow red on main), and two test defects self-review caught before
pushing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G5B7QM1QLQuWSBMCxiCzS6

* docs(spx-v2): final queue state — PrimeIntellect-ai#273 merged, only PrimeIntellect-ai#271 remains

Closes the record for this session.

- All five briefed PRs merged (PrimeIntellect-ai#268, PrimeIntellect-ai#263, PrimeIntellect-ai#270, PrimeIntellect-ai#269, PrimeIntellect-ai#273); main is c84855d.
  Issues PrimeIntellect-ai#266 and PrimeIntellect-ai#272 closed by their PRs. PrimeIntellect-ai#271 is the only open brief and
  was never authorised, so never started. Runtime testing is unblocked.
- The CI outage (13:51Z-15:47Z) was account-level and hit main identically;
  recovery was visible as `changes` taking 9s with real steps instead of 2s
  with none. Nothing in the diff ever needed changing.
- Records the scope misjudgement worth carrying forward: a Codex P1 mapped
  directly to an acceptance checkbox I had deferred as out of scope. When a
  finding maps to an acceptance criterion it is in scope by definition.
- Records the auto-merge hazard: squash composes the commit message from the
  PR body, so a body left stale after a review round writes false claims into
  main permanently. Rewrite the body before merge; keep corrections visible.
- Records a published test claim that had not been executed, and the rule
  that follows from it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G5B7QM1QLQuWSBMCxiCzS6

* docs(spx-v2): correct the queue state — the last brief landed while idle

Records, without editing the now-false text away, that sections 9 and 4 went
stale five hours after they were written. The correction matters more than the
content: this file exists to stop sessions trusting notes over GitHub, and it
caught its own author.

- main is 8d2139c. Between 16:12Z and 21:54Z the fleet merged PrimeIntellect-ai#279 (the PrimeIntellect-ai#58
  alert-bridge race, FIXED — stop carrying it as a standing exception), PrimeIntellect-ai#278
  (AGENTS.md invariants), PrimeIntellect-ai#283 (repo cleanup), and PrimeIntellect-ai#284, which delivered the
  last brief and closed PrimeIntellect-ai#165 with a keyword.
- Section 3's routing conclusion was confirmed by events: PrimeIntellect-ai#271 was delivered
  by the Mac maker fleet via auto-dispatch, exactly as argued, and the remote
  session correctly declined to open a second lane.
- Flags issue-state drift: PrimeIntellect-ai#271, PrimeIntellect-ai#276 and PrimeIntellect-ai#274 are delivered and merged yet
  still open, because a title reference is not a closing keyword. That is the
  mirror image of the hazard the V2 CLAUDE.md documents, and it leaves
  open-work disagreeing with main. Operator action, named as such.
- Records the residual PrimeIntellect-ai#284 deferred on stated grounds (PrimeIntellect-ai#286), which is a
  known open edge on the LIVE path.
- States the next slice: S4 / PrimeIntellect-ai#236, the first whose exit criteria need a real
  broker order. Certification stays 0/12; the system has never placed a trade.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G5B7QM1QLQuWSBMCxiCzS6

---------

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants