Skip to content

fix(heartbeat): persist per-agent enabled and interval settings - #230

Merged
jlia0 merged 1 commit into
mainfrom
jlia0/fix-heartbeat-overrides
Mar 16, 2026
Merged

jlia0 merged 1 commit into
mainfrom
jlia0/fix-heartbeat-overrides

Conversation

@jlia0

@jlia0 jlia0 commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator

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

  • API endpoints (GET /api/agents/:id/heartbeat, PUT /api/agents/:id/heartbeat): Now handle enabled and interval fields from settings.agents[id].heartbeat
  • Frontend API functions: Updated getAgentHeartbeat return type and saveAgentHeartbeat signature to include heartbeat overrides
  • Agent config page: Fixed heartbeat save to persist enabled/interval to per-agent settings instead of global monitoring config

Testing

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

…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-apps

greptile-apps Bot commented Mar 16, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes a long-standing bug where per-agent heartbeat overrides (enabled, interval) were never read from or written to settings.json, meaning the heartbeat cron script always ignored the UI settings. The fix is well-scoped and consistent with existing patterns in the codebase.

Key changes:

  • GET /api/agents/:id/heartbeat now returns enabled (defaulting to true) and interval from settings.agents[id].heartbeat, making the per-agent override visible to the frontend.
  • PUT /api/agents/:id/heartbeat now persists enabled and interval into settings.agents[id].heartbeat via mutateSettings, in addition to writing heartbeat.md.
  • The frontend no longer polls global Settings for the heartbeat interval; it reads and writes the per-agent values through the updated API.
  • saveAgentHeartbeat in api.ts is updated from a bare content string to a structured object, cleanly encapsulating all three heartbeat fields.

Issues found:

  • The interval field in the PUT handler is stored without any server-side validation. A direct API call with a negative or zero value would persist it, potentially causing the cron script to spin or miscalculate delays.
  • Inside the mutateSettings callback a silent no-op guard (if (!s.agents?.[agentId]) return;) can be hit in a race condition, yet the endpoint always returns { ok: true }, falsely signalling success to the caller.

Confidence Score: 3/5

  • Safe to merge after addressing the missing interval validation and the false-positive success response on the race condition path.
  • The core logic of the fix is correct and the type changes are consistent. Two issues lower confidence: no server-side guard against invalid (negative/zero) interval values that could disrupt the cron script, and a silent no-op in mutateSettings that still returns { ok: true } — both are actionable and straightforward to fix before merging.
  • packages/server/src/routes/agents.ts — both issues are in the new PUT handler logic.

Important Files Changed

Filename Overview
packages/server/src/routes/agents.ts GET endpoint now returns per-agent enabled/interval from agent.heartbeat; PUT endpoint persists them via mutateSettings. Two issues: no server-side validation of interval (negative/zero values accepted), and the mutation callback can silently no-op while the handler still returns { ok: true }.
tinyoffice/src/lib/api.ts Clean type updates — getAgentHeartbeat return type extended with enabled/interval, saveAgentHeartbeat parameter changed from a bare content string to a structured object. No issues found.
tinyoffice/src/app/agents/[id]/page.tsx Removes polling of global Settings; heartbeat enabled and interval are now loaded from and saved to the per-agent API response. Logic is correct; one minor note: if the API returns no interval (first load before any save), the UI silently defaults to "300" and will write that on the next save, which is reasonable default behaviour.

Sequence Diagram

sequenceDiagram
    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 }
Loading

Last reviewed commit: b7f6fbb

Comment on lines +304 to +310
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;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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:

Suggested change
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;
});
}

Comment on lines +304 to 313
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 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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:

Suggested change
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);
}

@jlia0
jlia0 merged commit bdc602a into main Mar 16, 2026
1 check passed
@jlia0
jlia0 deleted the jlia0/fix-heartbeat-overrides branch March 16, 2026 21:26
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.

1 participant