Repository navigation
fix(heartbeat): persist per-agent enabled and interval settings - #230
Conversation
…artbeat overrides The heartbeat API endpoints were not reading or persisting per-agent heartbeat overrides (enabled, interval) that the cron script relies on. The GET endpoint now returns enabled and interval from agent settings, and the PUT endpoint persists them to settings.agents[id].heartbeat. The UI now correctly saves these fields to the per-agent endpoint instead of the global monitoring settings. Co-Authored-By: Claude Haiku 4.5 <[email protected]>
Greptile SummaryThis PR fixes a long-standing bug where per-agent heartbeat overrides ( Key changes:
Issues found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant UI as Agent Config Page
participant API as PUT /api/agents/:id/heartbeat
participant FS as heartbeat.md
participant Settings as settings.json
UI->>API: PUT { content, enabled, interval }
API->>API: getSettings() — validate agent exists
alt content provided
API->>FS: writeFileSync(heartbeatMd, content)
end
alt enabled or interval provided
API->>Settings: mutateSettings()<br/>→ agents[id].heartbeat.enabled<br/>→ agents[id].heartbeat.interval
end
API-->>UI: { ok: true }
UI->>API: GET /api/agents/:id/heartbeat
API->>FS: readFileSync(heartbeatMd)
API->>Settings: getSettings()
API-->>UI: { content, path, enabled, interval }
Last reviewed commit: b7f6fbb |
| if (body.enabled != null || body.interval != null) { | ||
| mutateSettings(s => { | ||
| if (!s.agents?.[agentId]) return; | ||
| if (!s.agents[agentId].heartbeat) s.agents[agentId].heartbeat = {}; | ||
| if (body.enabled != null) s.agents[agentId].heartbeat!.enabled = body.enabled; | ||
| if (body.interval != null) s.agents[agentId].heartbeat!.interval = body.interval; | ||
| }); |
There was a problem hiding this comment.
Missing server-side interval validation
The interval value is stored directly without any range check. The frontend guards against NaN with parseInt(...) || 300, but it does not prevent a caller from sending a negative or zero value. A direct API call with { "interval": -60 } or { "interval": 0 } would persist those values, which the heartbeat cron script would then read and likely behave unexpectedly with (tight spin loops, negative delay calculations, etc.).
Consider adding a guard before the mutation:
| if (body.enabled != null || body.interval != null) { | |
| mutateSettings(s => { | |
| if (!s.agents?.[agentId]) return; | |
| if (!s.agents[agentId].heartbeat) s.agents[agentId].heartbeat = {}; | |
| if (body.enabled != null) s.agents[agentId].heartbeat!.enabled = body.enabled; | |
| if (body.interval != null) s.agents[agentId].heartbeat!.interval = body.interval; | |
| }); | |
| // Persist heartbeat overrides to settings.json | |
| if (body.enabled != null || body.interval != null) { | |
| if (body.interval != null && (body.interval <= 0 || !Number.isInteger(body.interval))) { | |
| return c.json({ error: 'interval must be a positive integer (seconds)' }, 400); | |
| } | |
| mutateSettings(s => { | |
| if (!s.agents?.[agentId]) return; | |
| if (!s.agents[agentId].heartbeat) s.agents[agentId].heartbeat = {}; | |
| if (body.enabled != null) s.agents[agentId].heartbeat!.enabled = body.enabled; | |
| if (body.interval != null) s.agents[agentId].heartbeat!.interval = body.interval; | |
| }); | |
| } |
| if (body.enabled != null || body.interval != null) { | ||
| mutateSettings(s => { | ||
| if (!s.agents?.[agentId]) return; | ||
| if (!s.agents[agentId].heartbeat) s.agents[agentId].heartbeat = {}; | ||
| if (body.enabled != null) s.agents[agentId].heartbeat!.enabled = body.enabled; | ||
| if (body.interval != null) s.agents[agentId].heartbeat!.interval = body.interval; | ||
| }); | ||
| } | ||
|
|
||
| return c.json({ ok: true }); |
There was a problem hiding this comment.
Silent ok: true when mutation is skipped
Inside mutateSettings, if the agent has been deleted between the initial existence check (line 292–293) and the callback execution, the inner guard if (!s.agents?.[agentId]) return; exits the callback silently without persisting anything. The outer handler still returns { ok: true }, giving the caller a false indication that the settings were saved.
While this race window is very small in practice, the caller (and the heartbeat cron script) will believe the override was persisted when it was not. Consider checking the return value or propagating a failure:
| if (body.enabled != null || body.interval != null) { | |
| mutateSettings(s => { | |
| if (!s.agents?.[agentId]) return; | |
| if (!s.agents[agentId].heartbeat) s.agents[agentId].heartbeat = {}; | |
| if (body.enabled != null) s.agents[agentId].heartbeat!.enabled = body.enabled; | |
| if (body.interval != null) s.agents[agentId].heartbeat!.interval = body.interval; | |
| }); | |
| } | |
| return c.json({ ok: true }); | |
| // Persist heartbeat overrides to settings.json | |
| if (body.enabled != null || body.interval != null) { | |
| let mutated = false; | |
| mutateSettings(s => { | |
| if (!s.agents?.[agentId]) return; | |
| if (!s.agents[agentId].heartbeat) s.agents[agentId].heartbeat = {}; | |
| if (body.enabled != null) s.agents[agentId].heartbeat!.enabled = body.enabled; | |
| if (body.interval != null) s.agents[agentId].heartbeat!.interval = body.interval; | |
| mutated = true; | |
| }); | |
| if (!mutated) return c.json({ error: `agent '${agentId}' not found` }, 404); | |
| } |
Summary
The heartbeat API endpoints now correctly read and persist per-agent heartbeat overrides (enabled, interval) that the cron script relies on. Previously, these settings were completely ignored by the API.
Changes
GET /api/agents/:id/heartbeat,PUT /api/agents/:id/heartbeat): Now handleenabledandintervalfields fromsettings.agents[id].heartbeatgetAgentHeartbeatreturn type andsaveAgentHeartbeatsignature to include heartbeat overridesTesting
Tested by reviewing changes and verifying types. The heartbeat cron script now correctly reads the per-agent overrides that the UI persists.
🤖 Generated with Claude Code