From b88ebd039769b681601163bd1b0786626000cff3 Mon Sep 17 00:00:00 2001 From: Lily Shen <115414357+lilyshen0722@users.noreply.github.com> Date: Tue, 4 Aug 2026 13:03:22 -0700 Subject: [PATCH 1/2] fix(agents): heartbeat is opt-in, not opt-out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dispatch guard was `enabled === false`, so an installation that had never expressed an opinion about heartbeats still woke on a timer. Nobody chose that — it is what a default of "on" produces when the setting lives in a config field most owners never see. Measured on production 2026-08-04: of 245 active installations across 48 distinct owners, 166 were ticking on that default alone, and 59 were off only because someone had gone and set the flag by hand. Each tick spends the owner's own model quota, and for BYO agents that is a bill we send to someone who never asked for a heartbeat and has no product surface to stop one (#832). It also runs a heartbeat with nothing behind it: the prompt tells the agent to read a HEARTBEAT.md that nothing provisions on the CAP path (#800). The modal outcome instance-wide is an agent waking hourly to execute an instruction pointing at nothing. Both guards now require `enabled === true`. The migration is unusually clean: this turns off exactly the 166 that never opted in and preserves all 20 that set the flag explicitly, so no owner who chose a heartbeat loses one. Tests cover `undefined` vs `true` specifically, which is the distinction that changed — a test written only against `false` would have passed before and after. Mutation-verified: reverting the guard fails exactly the two undefined cases and nothing else. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_016s8YysVUNmmiNXw3cTrZFJ --- .../schedulerService.heartbeatOptIn.test.js | 80 +++++++++++++++++++ backend/services/schedulerService.ts | 11 ++- 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 backend/__tests__/unit/services/schedulerService.heartbeatOptIn.test.js diff --git a/backend/__tests__/unit/services/schedulerService.heartbeatOptIn.test.js b/backend/__tests__/unit/services/schedulerService.heartbeatOptIn.test.js new file mode 100644 index 0000000000..79adccbb00 --- /dev/null +++ b/backend/__tests__/unit/services/schedulerService.heartbeatOptIn.test.js @@ -0,0 +1,80 @@ +/** + * Heartbeat dispatch is opt-in (#832 follow-up). + * + * The guard used to be `enabled === false`, which meant an install that had + * never expressed an opinion still woke on a timer. Measured on production + * 2026-08-04: 166 of 245 active installations, across 48 distinct owners, were + * ticking hourly on that default alone — each spending its own owner's model + * quota to run a heartbeat that has no content contract behind it (#800). + * + * The distinction this file defends is `undefined` vs `true`. A test that only + * covered `false` would have passed against the old behaviour too. + */ + +jest.mock('jsonwebtoken', () => ({ sign: jest.fn(), verify: jest.fn(), decode: jest.fn() })); +jest.mock('../../../services/agentEventService', () => ({ + enqueue: jest.fn().mockResolvedValue({ _id: 'evt' }), +})); +jest.mock('../../../models/AgentRegistry', () => ({ + AgentInstallation: { find: jest.fn() }, +})); +// The enqueue path builds an activity hint from recent posts; that aggregate +// is not what this file is testing, and unmocked it blocks on a real Mongo. +jest.mock('../../../models/Post', () => ({ aggregate: jest.fn().mockResolvedValue([]) })); +jest.mock('../../../models/Message', () => ({ aggregate: jest.fn().mockResolvedValue([]) })); + +const AgentEventService = require('../../../services/agentEventService'); +const { AgentInstallation } = require('../../../models/AgentRegistry'); +const SchedulerService = require('../../../services/schedulerService').constructor; + +const install = (heartbeat) => ({ + _id: 'i1', + agentName: 'a', + instanceId: 'default', + podId: 'p1', + status: 'active', + config: heartbeat === undefined ? {} : { heartbeat }, +}); + +const mockFind = (rows) => { + const lean = jest.fn().mockResolvedValue(rows); + const select = jest.fn().mockReturnValue({ lean }); + AgentInstallation.find.mockReturnValue({ select, lean }); +}; + +describe('dispatchAgentHeartbeats — opt-in', () => { + beforeEach(() => jest.clearAllMocks()); + + test('an install with no heartbeat config does NOT fire', async () => { + mockFind([install(undefined)]); + const r = await SchedulerService.dispatchAgentHeartbeats({ + trigger: 'test', respectIntervals: false, + }); + expect(r.enqueued).toBe(0); + expect(AgentEventService.enqueue).not.toHaveBeenCalled(); + }); + + test('an install with heartbeat present but enabled unset does NOT fire', async () => { + mockFind([install({ everyMinutes: 60 })]); + const r = await SchedulerService.dispatchAgentHeartbeats({ + trigger: 'test', respectIntervals: false, + }); + expect(r.enqueued).toBe(0); + }); + + test('enabled:false still does not fire', async () => { + mockFind([install({ enabled: false, everyMinutes: 60 })]); + const r = await SchedulerService.dispatchAgentHeartbeats({ + trigger: 'test', respectIntervals: false, + }); + expect(r.enqueued).toBe(0); + }); + + test('enabled:true DOES fire — opting in still works', async () => { + mockFind([install({ enabled: true, everyMinutes: 60 })]); + const r = await SchedulerService.dispatchAgentHeartbeats({ + trigger: 'test', respectIntervals: false, + }); + expect(r.enqueued).toBeGreaterThan(0); + }); +}); diff --git a/backend/services/schedulerService.ts b/backend/services/schedulerService.ts index 1ee3079128..a935fecd7b 100644 --- a/backend/services/schedulerService.ts +++ b/backend/services/schedulerService.ts @@ -844,7 +844,7 @@ class SchedulerService { if (installation?.config?.heartbeat?.global === true) { const agentKey = `${agentName}:${instanceId}`; if (seenGlobalAgents.has(agentKey)) continue; - if (installation?.config?.heartbeat?.enabled === false) continue; + if (installation?.config?.heartbeat?.enabled !== true) continue; seenGlobalAgents.add(agentKey); toProcess.push({ installation, isGlobal: true }); } else { @@ -937,8 +937,15 @@ class SchedulerService { const enqueueResults = await Promise.all( toProcess.map(async ({ installation, isGlobal }) => { + // Opt-in, not opt-out. `undefined` must NOT fire: an install that never + // expressed an opinion about heartbeats does not get one. The previous + // `=== false` check made every agent anyone installed wake on a timer + // forever unless someone found and set a field they never see — 166 of + // 245 active installs across 48 owners were ticking hourly on that + // default alone, spending each owner's own model quota to run a + // heartbeat with no content behind it (#800). const heartbeatEnabled = installation?.config?.heartbeat?.enabled; - if (heartbeatEnabled === false) return { enqueued: 0, skippedByInterval: 0 }; + if (heartbeatEnabled !== true) return { enqueued: 0, skippedByInterval: 0 }; const autonomyEnabled = installation?.config?.autonomy?.enabled; if (autonomyEnabled === false) return { enqueued: 0, skippedByInterval: 0 }; From 3988df42d6438f303c7135cc7be1e5945749c80b Mon Sep 17 00:00:00 2001 From: Lily Shen <115414357+lilyshen0722@users.noreply.github.com> Date: Tue, 4 Aug 2026 13:13:05 -0700 Subject: [PATCH 2/2] =?UTF-8?q?test:=20opt=20the=20cue=20fixture=20in=20?= =?UTF-8?q?=E2=80=94=20dispatch=20no=20longer=20fires=20on=20an=20unset=20?= =?UTF-8?q?flag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #804's cue tests build an installation that never sets heartbeat.enabled, which was fine when the guard was `=== false`. Under opt-in it does not dispatch, so the suite failed at its own `expect(enqueue).toHaveBeenCalled()` precondition before reaching any content assertion. The fixture opts in. These tests are about what the cue says, not about the gate — the gate has its own file. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_016s8YysVUNmmiNXw3cTrZFJ --- .../unit/services/schedulerService.heartbeatCue.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/__tests__/unit/services/schedulerService.heartbeatCue.test.js b/backend/__tests__/unit/services/schedulerService.heartbeatCue.test.js index a0ce4f4474..495e3d71a5 100644 --- a/backend/__tests__/unit/services/schedulerService.heartbeatCue.test.js +++ b/backend/__tests__/unit/services/schedulerService.heartbeatCue.test.js @@ -27,7 +27,9 @@ const INSTALLATION = { agentName: 'openclaw', instanceId: 'nova', podId: '69b7ddff0ce64c9648365fc4', - config: { autonomy: { heartbeat: true } }, + // Heartbeat dispatch is opt-in — an install that never set `enabled` does not + // fire at all, so a cue fixture has to opt in before it can assert on content. + config: { autonomy: { heartbeat: true }, heartbeat: { enabled: true } }, }; const mockInstallations = (installations) => {