Skip to content

fix: reduce cloud sync heartbeat interval from 30s to 60s#409

Merged
khaliqgant merged 2 commits into
mainfrom
fix/reduce-heartbeat-interval
Feb 12, 2026
Merged

fix: reduce cloud sync heartbeat interval from 30s to 60s#409
khaliqgant merged 2 commits into
mainfrom
fix/reduce-heartbeat-interval

Conversation

@khaliqgant
Copy link
Copy Markdown
Member

@khaliqgant khaliqgant commented Feb 12, 2026

Summary

  • Increases default daemon heartbeat interval from 30s to 60s, halving cloud API load
  • With 11+ active daemons each making 5 API calls per heartbeat cycle (heartbeat, messages, agents, messages/sync, metrics), the cloud server was handling ~22 req/sec on a single 512MB Fly.io instance
  • Adds AGENT_RELAY_HEARTBEAT_INTERVAL env var for per-deployment tuning without code changes

Impact

  • Cloud API load: ~22 req/sec → ~11 req/sec
  • Message/agent sync freshness: 30s → 60s (acceptable for cross-machine discovery)
  • Env var allows reverting to 30s or tuning further per workspace

Test plan

  • Verify daemon starts and heartbeats at 60s interval
  • Verify AGENT_RELAY_HEARTBEAT_INTERVAL=30000 overrides back to 30s
  • Monitor cloud API request rate after deploy

🤖 Generated with Claude Code


Open with Devin

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

View 4 additional findings in Devin Review.

Open in Devin Review

Comment thread packages/daemon/src/cloud-sync.ts Outdated
Comment on lines +122 to +124
heartbeatInterval: config.heartbeatInterval
|| (process.env.AGENT_RELAY_HEARTBEAT_INTERVAL ? parseInt(process.env.AGENT_RELAY_HEARTBEAT_INTERVAL, 10) : 0)
|| 60000, // 60 seconds (reduced from 30s to lower cloud API load)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Negative or very small env var value bypasses || fallback, causing rapid-fire heartbeats

If AGENT_RELAY_HEARTBEAT_INTERVAL is set to a negative value (e.g., "-5000"), parseInt returns -5000, which is truthy and passes through the || chain without falling back to the 60000 default. When passed to setInterval at cloud-sync.ts:202-204, Node.js clamps values ≤ 0 to 1ms, causing heartbeats to fire ~1000 times/second — the exact opposite of this PR's goal of reducing API load.

Root Cause

The || operator on line 122-124 only guards against falsy values (0, NaN, undefined). Negative numbers are truthy in JavaScript:

heartbeatInterval: config.heartbeatInterval
  || (process.env.AGENT_RELAY_HEARTBEAT_INTERVAL ? parseInt(process.env.AGENT_RELAY_HEARTBEAT_INTERVAL, 10) : 0)
  || 60000,

If the env var is "-1000", parseInt("-1000", 10) returns -1000 (truthy), so the final || 60000 is never reached. Similarly, a very small positive value like "10" (10ms) would also pass through, causing excessive API calls.

Impact: An operator misconfiguring the env var with a negative or very small value would cause the daemon to flood the cloud API with requests, potentially causing a denial of service on the cloud server — which is the very problem this PR aims to mitigate.

Suggested change
heartbeatInterval: config.heartbeatInterval
|| (process.env.AGENT_RELAY_HEARTBEAT_INTERVAL ? parseInt(process.env.AGENT_RELAY_HEARTBEAT_INTERVAL, 10) : 0)
|| 60000, // 60 seconds (reduced from 30s to lower cloud API load)
heartbeatInterval: config.heartbeatInterval
|| Math.max(10000, (process.env.AGENT_RELAY_HEARTBEAT_INTERVAL ? parseInt(process.env.AGENT_RELAY_HEARTBEAT_INTERVAL, 10) : 0))
|| 60000, // 60 seconds (reduced from 30s to lower cloud API load)
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Allows overriding the default 30s heartbeat interval via env var without
code changes. This enables per-deployment tuning from the workspace
Dockerfile or provisioner config.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@khaliqgant khaliqgant force-pushed the fix/reduce-heartbeat-interval branch from 1ee3c70 to c7754d9 Compare February 12, 2026 19:13
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 6 additional findings in Devin Review.

Open in Devin Review

Comment thread packages/daemon/src/cloud-sync.ts Outdated
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@khaliqgant khaliqgant merged commit e569b2a into main Feb 12, 2026
31 checks passed
@khaliqgant khaliqgant deleted the fix/reduce-heartbeat-interval branch February 12, 2026 19:22
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