Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): make arm() idempotent to close boot-race double-fire

Adam Eivya055664d02be
diff --git a/server/services/voice/timers.js b/server/services/voice/timers.js
index 71e05284bc..d1df0d7d00 100644
--- a/server/services/voice/timers.js
+++ b/server/services/voice/timers.js
@@ -75,6 +75,14 @@ async function fire(timer) {
// triggers. `unref` so a lone pending timer never keeps the process (or a test
// runner) alive on its own.
function arm(timer) {
+ // Idempotent per id: clear any handle already armed for this id before
+ // replacing the map entry, so re-arming the same id never leaves a second
+ // live setTimeout behind. This closes a boot race — `initVoiceTimers` runs
+ // fire-and-forget while routes are already up, so a `timer_set` that persists
+ // a record before init's read resolves would otherwise be armed twice (once
+ // by scheduleTimer, once by init re-reading the same id) and double-fire.
+ const existing = active.get(timer.id);
+ if (existing) clearTimeout(existing.handle);
const delay = Math.max(0, timer.fireAt - Date.now());
timer.handle = setTimeout(() => {
fire(timer).catch((err) => console.error(`❌ voice-timer fire failed: ${err.message}`));
diff --git a/server/services/voice/timers.test.js b/server/services/voice/timers.test.js
index d7b36f5a03..1b219f0573 100644
--- a/server/services/voice/timers.test.js
+++ b/server/services/voice/timers.test.js
@@ -135,4 +135,23 @@ describe('initVoiceTimers', () => {
const second = await initVoiceTimers();
expect(second).toEqual({ skipped: true });
});
+
+ it('re-arming a timer already in memory does not double-fire (boot race)', async () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date('2026-05-26T12:00:00Z'));
+ // Simulate the race: a timer_set is scheduled (and persisted) while boot
+ // init is still reading the store, so init reads the same record back and
+ // re-arms its id. Idempotent arm() must keep it to a single fire.
+ const scheduled = scheduleTimer({ totalMs: 300000, label: 'overlap' });
+ await tick(); await tick(); // let scheduleTimer's persist write the store
+ const res = await initVoiceTimers(); // reads the just-persisted record
+ expect(res).toEqual({ armed: 1, fired: 0 }); // re-armed the same id
+ addNotificationMock.mockClear();
+ await vi.advanceTimersByTimeAsync(300000);
+ expect(addNotificationMock).toHaveBeenCalledTimes(1); // not 2
+ expect(addNotificationMock).toHaveBeenCalledWith(
+ expect.objectContaining({ title: '⏰ overlap', metadata: expect.objectContaining({ timerId: scheduled.id }) })
+ );
+ vi.useRealTimers();
+ });
});