#20521: feat(heartbeat): inject active cron job summary into heartbeat prompt
size: M
Cluster:
Heartbeat Functionality Improvements
## Problem
After context compaction, the LLM agent loses memory of which tasks are already automated via cron jobs. This causes duplicate work — e.g., the agent manually sends an AI news briefing during heartbeat even though a `ai-news-daily` cron job already handles it.
This is a general problem for any user running both heartbeat and cron: compaction erases the agent's knowledge of scheduled jobs, leading to redundant API calls, duplicate messages, and wasted tokens.
## Solution
During regular heartbeat polls, read the cron store and append a compact summary of enabled cron jobs to the heartbeat prompt:
```
Active cron jobs (do NOT duplicate these in heartbeat — they run automatically):
- ai-news-daily: cron "0 22 * * *" (UTC) [isolated, agentTurn]
- openclaw-backup: every 360m [isolated, agentTurn]
- morning-report: cron "30 22 * * *" [main, systemEvent]
```
Each job entry includes: name, schedule (cron/every/at), session target, and payload type. Capped at 15 jobs to bound token usage.
## Design decisions
- **Only injected for regular heartbeat polls** — exec-event and cron-event runs use specialized prompts and don't need the summary
- **Graceful fallback** — if the cron store is missing or unreadable, the summary is silently skipped
- **Read-only** — uses the existing `loadCronStore` from `src/cron/store.ts`, no new persistence layer
- **Minimal footprint** — new file `src/infra/heartbeat-cron-summary.ts` (~50 lines) + 1-line integration in `heartbeat-runner.ts`
## Files changed
- `src/infra/heartbeat-cron-summary.ts` — new helper to build compact cron summary
- `src/infra/heartbeat-cron-summary.test.ts` — 7 test cases covering all schedule types, disabled jobs, empty stores, and missing files
- `src/infra/heartbeat-runner.ts` — inject summary into regular heartbeat prompt
## Testing
All existing heartbeat tests pass (36 + 2 + 12 = 50 tests), plus 7 new tests for the summary builder.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds active cron job awareness to the heartbeat system by injecting a summary of enabled cron jobs into the heartbeat prompt. This prevents duplicate work after context compaction by reminding the agent which tasks are already automated.
Key changes:
- New helper function `buildHeartbeatCronSummary` reads the cron store and generates a compact text summary of enabled jobs (max 15 jobs)
- Summary includes job name, schedule (cron/every/at), session target (isolated/main), and payload type (agentTurn/systemEvent)
- Only injected during regular heartbeat polls, not for exec-event or cron-event runs
- Gracefully handles missing or unreadable cron stores by returning `undefined`
- Comprehensive test coverage (7 test cases) covering all schedule types, disabled jobs filtering, and edge cases
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The implementation is well-designed with clear boundaries: it only reads from the cron store (no mutations), has comprehensive test coverage (7 test cases), gracefully handles all error conditions, and is narrowly scoped to regular heartbeat polls only. The code follows repository conventions for TypeScript, testing patterns, and error handling.
- No files require special attention
<sub>Last reviewed commit: cbb5cad</sub>
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#21014: fix(cron): suppress main-session summary for HEARTBEAT_OK responses
by nickjlamb · 2026-02-19
81.8%
#8418: fix: notify user after consecutive heartbeat/cron failures
by liaosvcaf · 2026-02-04
80.8%
#6522: fix(cron): deliver original message when agent response is heartbea...
by sidmohan0 · 2026-02-01
80.3%
#11657: fix(cron): treat skipped heartbeat as ok for one-shot jobs
by DukeDeSouth · 2026-02-08
79.5%
#12365: test(heartbeat): don't skip empty HEARTBEAT.md for cron wake events
by tyclaudius-ai · 2026-02-09
78.0%
#5498: Cron: honor next-heartbeat
by sebslight · 2026-01-31
77.9%
#3335: Fixes cron jobs
by hkirat · 2026-01-28
77.7%
#7350: fix(cron): pass agentId and AccountId through heartbeat chain for m...
by codeslayer44 · 2026-02-02
77.2%
#17529: feat(cron): add preCheck gate to skip jobs when nothing changed
by scottgl9 · 2026-02-15
76.0%
#15422: fix(auto-reply): keep cron systemEvent payloads that start with 'Re...
by liuxiaopai-ai · 2026-02-13
75.9%