Skip to content

Self-improving AI Employees for everyone

fix(theme): defer oscillator scheduling on suspended AC + DevTools self-diagnose (Wave 27)

trohitgfc8017424347
diff --git a/client/src/hooks/useSound.ts b/client/src/hooks/useSound.ts
index a0fd7c1d..184c3b71 100644
--- a/client/src/hooks/useSound.ts
+++ b/client/src/hooks/useSound.ts
@@ -144,9 +144,9 @@ export function useSoundSync(): void {
window.addEventListener('keydown', handler, opts);
window.addEventListener('touchstart', handler, opts);
return () => {
- window.removeEventListener('pointerdown', handler, true);
- window.removeEventListener('keydown', handler, true);
- window.removeEventListener('touchstart', handler, true);
+ window.removeEventListener('pointerdown', handler, opts);
+ window.removeEventListener('keydown', handler, opts);
+ window.removeEventListener('touchstart', handler, opts);
};
}, []);
}
diff --git a/client/src/lib/sound.ts b/client/src/lib/sound.ts
index a8e36db2..d5990550 100644
--- a/client/src/lib/sound.ts
+++ b/client/src/lib/sound.ts
@@ -100,35 +100,43 @@ function play(cfg: OscConfig | undefined): void {
if (!cfg || !enabled) return;
const ac = ensureCtx();
if (!ac) return;
- // WebAudio context starts suspended on most browsers until a user
- // gesture; resume() is a no-op once running.
- if (ac.state === 'suspended') void ac.resume();
- try {
- const osc = ac.createOscillator();
- const gain = ac.createGain();
- osc.type = cfg.type;
- osc.frequency.value = cfg.freq;
- let target: AudioNode = osc;
- if (cfg.lp) {
- const lp = ac.createBiquadFilter();
- lp.type = 'lowpass';
- lp.frequency.value = cfg.lp;
- target.connect(lp);
- target = lp;
- }
- target.connect(gain);
- gain.connect(ac.destination);
- const now = ac.currentTime;
- gain.gain.setValueAtTime(0, now);
- gain.gain.linearRampToValueAtTime(cfg.vol, now + cfg.attack);
- gain.gain.exponentialRampToValueAtTime(0.0001, now + cfg.attack + cfg.decay);
- if (cfg.sweep) {
- osc.frequency.exponentialRampToValueAtTime(cfg.freq * cfg.sweep, now + cfg.dur);
+ // If suspended, defer oscillator scheduling until resume() settles.
+ // Synchronously scheduled oscillators on a suspended context are
+ // silently dropped per the WebAudio autoplay policy
+ // (https://developer.chrome.com/blog/autoplay/#webaudio).
+ const schedule = () => {
+ try {
+ const osc = ac.createOscillator();
+ const gain = ac.createGain();
+ osc.type = cfg.type;
+ osc.frequency.value = cfg.freq;
+ let target: AudioNode = osc;
+ if (cfg.lp) {
+ const lp = ac.createBiquadFilter();
+ lp.type = 'lowpass';
+ lp.frequency.value = cfg.lp;
+ target.connect(lp);
+ target = lp;
+ }
+ target.connect(gain);
+ gain.connect(ac.destination);
+ const now = ac.currentTime;
+ gain.gain.setValueAtTime(0, now);
+ gain.gain.linearRampToValueAtTime(cfg.vol, now + cfg.attack);
+ gain.gain.exponentialRampToValueAtTime(0.0001, now + cfg.attack + cfg.decay);
+ if (cfg.sweep) {
+ osc.frequency.exponentialRampToValueAtTime(cfg.freq * cfg.sweep, now + cfg.dur);
+ }
+ osc.start(now);
+ osc.stop(now + cfg.dur + 0.02);
+ } catch {
+ // WebAudio errors are non-fatal — silently drop.
}
- osc.start(now);
- osc.stop(now + cfg.dur + 0.02);
- } catch {
- // WebAudio errors are non-fatal — silently drop.
+ };
+ if (ac.state === 'running') {
+ schedule();
+ } else {
+ void Promise.resolve(ac.resume()).then(schedule).catch(() => {/* noop */});
}
}
@@ -316,4 +324,70 @@ export const Sounds = {
}
play(activePack[event]);
},
+ /**
+ * Snapshot the engine state for DevTools self-diagnosis. Returns an
+ * object the user can inspect from the browser console:
+ * `> Sounds.diagnose()` →
+ * { enabled, pack, eventsForPack, audioContextState, browserSupport }
+ * Common silent-audio modes:
+ * - `enabled: false` → user hasn't toggled Settings → Audio.
+ * - `pack: 'none'` → :root[data-theme="..."] declares chime/typo
+ * and the validator falls through to none.
+ * - `audioContextState: 'suspended'` after a click → a browser
+ * extension or autoplay-block policy is denying resume().
+ */
+ diagnose(): {
+ enabled: boolean;
+ pack: SoundPackName;
+ eventsForPack: string[];
+ cssSoundPack: string;
+ cssDataTheme: string | null;
+ audioContextState: string | 'no-ac' | 'no-audio-api';
+ browserSupport: boolean;
+ } {
+ const Ctx = typeof window !== 'undefined'
+ ? window.AudioContext || (window as any).webkitAudioContext
+ : undefined;
+ const cssSoundPack = typeof document !== 'undefined'
+ ? getComputedStyle(document.documentElement).getPropertyValue('--sound-pack').trim().replace(/['"]/g, '')
+ : '';
+ const cssDataTheme = typeof document !== 'undefined'
+ ? document.documentElement.getAttribute('data-theme')
+ : null;
+ return {
+ enabled,
+ pack: activePackName,
+ eventsForPack: Object.keys(activePack),
+ cssSoundPack,
+ cssDataTheme,
+ audioContextState: !Ctx ? 'no-audio-api' : audioCtx ? audioCtx.state : 'no-ac',
+ browserSupport: !!Ctx,
+ };
+ },
+ /**
+ * Force a sound to play for testing — bypasses the `enabled` gate.
+ * Call from DevTools: `Sounds.test('click')` after the AC has unlocked.
+ * Returns whether the play scheduled successfully.
+ */
+ test(event: SoundEvent = 'click'): boolean {
+ const cfg = activePack[event];
+ if (!cfg) return false;
+ const ac = ensureCtx();
+ if (!ac) return false;
+ const wasEnabled = enabled;
+ enabled = true;
+ try {
+ play(cfg);
+ return true;
+ } finally {
+ enabled = wasEnabled;
+ }
+ },
};
+
+// Expose on `window` in dev mode so users can self-diagnose silent audio
+// from DevTools (`> Sounds.diagnose()`, `> Sounds.test()`). No-op in
+// production builds — Vite tree-shakes the `import.meta.env.DEV` branch.
+if (typeof window !== 'undefined' && import.meta.env.DEV) {
+ (window as any).Sounds = Sounds;
+}
diff --git a/client/src/themes/cyber.css b/client/src/themes/cyber.css
index 8de1aa53..daa307fe 100644
--- a/client/src/themes/cyber.css
+++ b/client/src/themes/cyber.css
@@ -254,7 +254,9 @@
position: relative;
}
-/* Perspective grid floor */
+/* Perspective grid floor — sits below React Flow nodes (which carry their
+ * own z-index from React Flow internals). pointer-events:none so the
+ * decoration never captures clicks meant for nodes/edges underneath. */
:root[data-theme="cyber"] .canvas::before {
content: ''; position: absolute; left: 0; right: 0; bottom: 0; height: 50%;
background:
@@ -264,9 +266,11 @@
transform-origin: bottom;
pointer-events: none;
opacity: 0.4;
+ z-index: 0;
}
-/* Floating "SIGNAL LOST" status text */
+/* Floating "SIGNAL LOST" status text — sits above the perspective floor
+ * but pointer-events:none keeps it from intercepting interaction. */
:root[data-theme="cyber"] .canvas::after {
content: '◆ SIGNAL ◆ NIGHT_MARKET.7 ◆ 03:47:22';
position: absolute; top: 12px; right: 16px;
@@ -277,6 +281,7 @@
text-shadow: 0 0 4px var(--neon-cyan);
pointer-events: none;
animation: cyber-blink 1.6s steps(2) infinite;
+ z-index: 1;
}
/* ─── BUTTONS — neon outline ─── */
diff --git a/client/src/themes/surveillance.css b/client/src/themes/surveillance.css
index 221fad1a..74501188 100644
--- a/client/src/themes/surveillance.css
+++ b/client/src/themes/surveillance.css
@@ -158,7 +158,16 @@
:root[data-theme="surveillance"] .palette { border-left: 1px solid var(--border-strong); }
:root[data-theme="surveillance"] .chat { border-top: 1px solid var(--border-strong); }
-/* CANVAS — CCTV grid + crosshair */
+/* CANVAS — CCTV grid + crosshair.
+ *
+ * NOTE: the corner-bracket / centre-reticle / REC-text SVG is declared at a
+ * fixed 1920x1080 viewport (matches the upstream handoff). On viewports
+ * larger than 1920x1080 it tiles; on smaller viewports it's clipped to
+ * canvas bounds. The CCTV-monitor framing reads as intentional at typical
+ * desktop sizes. Replacing it with a `preserveAspectRatio="xMidYMid slice"`
+ * SVG that scales would lose the pixel-pitch authenticity the design calls
+ * for, so this is a known limitation, not a bug. The 40px crosshair grid
+ * (third layer) is correctly tiled across the full canvas at any size. */
:root[data-theme="surveillance"] .canvas {
background:
url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='1920' height='1080'><g stroke='%23e82626' stroke-width='1' fill='none' opacity='0.18'><path d='M40 40 H120 M40 40 V120 M1880 40 H1800 M1880 40 V120 M40 1040 H120 M40 1040 V960 M1880 1040 H1800 M1880 1040 V960'/><circle cx='960' cy='540' r='40' opacity='0.2'/><path d='M960 480 V600 M900 540 H1020' opacity='0.25'/></g><text x='80' y='80' fill='%23e82626' font-family='monospace' font-size='14' opacity='0.5'>● REC</text><text x='80' y='1020' fill='%23e82626' font-family='monospace' font-size='12' opacity='0.4'>CAM-04 / 19:42:08</text></svg>"),