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 (gemini): skip in-memory ids in boot re-arm (covers overdue branch)

Adam Eivydd2900e2aaff
diff --git a/server/services/voice/timers.js b/server/services/voice/timers.js
index d1df0d7d00..e9444b32b5 100644
--- a/server/services/voice/timers.js
+++ b/server/services/voice/timers.js
@@ -75,14 +75,6 @@ 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}`));
@@ -147,6 +139,12 @@ export async function initVoiceTimers() {
fireAt: rec.fireAt,
createdAt: typeof rec.createdAt === 'number' ? rec.createdAt : now,
};
+ // A timer already live in memory is authoritative — it was scheduled
+ // post-boot (during init's async read; routes are up before this
+ // fire-and-forget init settles) OR is a duplicate id in a corrupt store.
+ // Skip the persisted copy entirely so we neither re-fire it as overdue nor
+ // arm a second handle for it (both would double-notify).
+ if (active.has(timer.id)) continue;
if (timer.fireAt <= now) {
// Overdue — notify once. Not added to `active`, so the post-loop persist
// drops it from the store.
diff --git a/server/services/voice/timers.test.js b/server/services/voice/timers.test.js
index 1b219f0573..2ebeeadd8f 100644
--- a/server/services/voice/timers.test.js
+++ b/server/services/voice/timers.test.js
@@ -136,19 +136,26 @@ describe('initVoiceTimers', () => {
expect(second).toEqual({ skipped: true });
});
- it('re-arming a timer already in memory does not double-fire (boot race)', async () => {
+ it('skips a persisted record whose id is already live in memory (boot race, both branches)', async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-05-26T12:00:00Z'));
+ const now = Date.now();
// 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.
+ // init is still mid-read, so init reads the same id back. Mark the persisted
+ // copy OVERDUE to exercise the overdue branch — the in-memory timer is
+ // authoritative, so init must skip the record entirely (neither fire it as
+ // overdue nor re-arm a second handle), leaving exactly one live timer.
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
+ await tick(); await tick(); // let scheduleTimer's persist settle first
+ storeRef.value = {
+ version: 1,
+ timers: [{ id: scheduled.id, label: 'overlap', fireAt: now - 1000, createdAt: now }],
+ };
+ const res = await initVoiceTimers();
+ expect(res).toEqual({ armed: 0, fired: 0 }); // skipped — not fired-as-overdue, not re-armed
addNotificationMock.mockClear();
await vi.advanceTimersByTimeAsync(300000);
- expect(addNotificationMock).toHaveBeenCalledTimes(1); // not 2
+ expect(addNotificationMock).toHaveBeenCalledTimes(1); // the one live timer fires once
expect(addNotificationMock).toHaveBeenCalledWith(
expect.objectContaining({ title: '⏰ overlap', metadata: expect.objectContaining({ timerId: scheduled.id }) })
);