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): reserve rate-limit slot before await to block concurrent same-source bursts

Adam Eivy603d9ea413ac
diff --git a/server/services/voice/proactiveTriggers.js b/server/services/voice/proactiveTriggers.js
index 83638edcb7..3ab12b513f 100644
--- a/server/services/voice/proactiveTriggers.js
+++ b/server/services/voice/proactiveTriggers.js
@@ -102,16 +102,28 @@ export const wireProactiveTriggers = ({ io, speak = defaultSpeak, limits = RATE_
// isolated state and a rewire starts fresh.
const lastSpokenAt = new Map();
- // Speak one line, advancing the source's rate-limit bucket only when it
- // actually went out (a quiet-hours/disabled suppression returns ok:false and
- // doesn't consume the budget). May reject if synthesis throws — the caller
- // owns the catch.
+ // Speak one line, advancing the source's rate-limit bucket. The slot is
+ // reserved BEFORE awaiting `speak`: synthesis is async, so a same-tick burst
+ // of same-source events (an error storm) would otherwise all read the stale
+ // timestamp, pass the gate, and start concurrent syntheses — defeating the
+ // per-source limit exactly when it matters. The reservation stands on a line
+ // that goes out; on suppression/failure/throw we roll it back (unless a later
+ // event already claimed the slot) so the budget isn't spent on a non-line.
+ // try/finally only — a synthesis rejection still propagates to the caller's
+ // catch.
const dispatch = async (source, text, priority) => {
if (!text) return;
const now = Date.now();
if (!allowBySource(source, lastSpokenAt.get(source), now, limits)) return;
- const result = await speak({ io, text, priority, source });
- if (result?.ok) lastSpokenAt.set(source, Date.now());
+ const previous = lastSpokenAt.get(source) ?? null;
+ lastSpokenAt.set(source, now);
+ let ok = false;
+ try {
+ const result = await speak({ io, text, priority, source });
+ ok = !!result?.ok;
+ } finally {
+ if (!ok && lastSpokenAt.get(source) === now) lastSpokenAt.set(source, previous);
+ }
};
// EventEmitter doesn't await async listeners, so a rejected dispatch would
diff --git a/server/services/voice/proactiveTriggers.test.js b/server/services/voice/proactiveTriggers.test.js
index a84e5ef889..20d732dd4e 100644
--- a/server/services/voice/proactiveTriggers.test.js
+++ b/server/services/voice/proactiveTriggers.test.js
@@ -152,6 +152,24 @@ describe('wireProactiveTriggers', () => {
expect(speak.mock.calls[0][0].text).toMatch(/first/);
});
+ it('rate-limits a same-tick concurrent burst (slot reserved before await)', async () => {
+ // Synthesis is async; emit two same-source events on the SAME tick (no
+ // flush between) so the second fires while the first speak() is still
+ // pending. The pre-await reservation must block the second — otherwise
+ // both start concurrent syntheses and talk over each other.
+ let resolveSpeak;
+ speak = vi.fn(() => new Promise((r) => { resolveSpeak = () => r({ ok: true }); }));
+ unwire = wireProactiveTriggers({ io: {}, speak, limits: { error: 60_000 } });
+
+ errorEvents.emit('error', { severity: 'critical', message: 'first' });
+ errorEvents.emit('error', { severity: 'critical', message: 'second' });
+ resolveSpeak();
+ await flush();
+
+ expect(speak).toHaveBeenCalledTimes(1);
+ expect(speak.mock.calls[0][0].text).toMatch(/first/);
+ });
+
it('does not advance the bucket when a line was suppressed (ok:false)', async () => {
speak = vi.fn(async () => ({ ok: false, reason: 'quiet-hours' }));
unwire = wireProactiveTriggers({ io: {}, speak, limits: { error: 60_000 } });