From 2a4af63f0054c2212a58adc5708e83a4b16ea635 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 29 Nov 2026 12:31:45 -0800 Subject: [PATCH] CODY-025: stopped or replaced speech never starts; CODY-026: README; note when the phone page holds off idle exit - ui/speaker.ts: a generation counter. Every play() and stop() advances it; a play() that finds it changed after resume() or decodeAudioData() returns without creating a source. - apps/even-client/test/speaker.test.ts: nine tests with a fake sound engine whose resume and decode finish when the test says. - README: 'Not in this slice' no longer says text-to-speech is absent. - controller: one diagnostic line when phone-page activity starts holding off the idle exit. Co-Authored-By: Claude Fable 5.2 --- README.md | 13 +- apps/even-client/src/core/controller.ts | 2 + apps/even-client/src/ui/speaker.ts | 24 ++- apps/even-client/test/speaker.test.ts | 227 ++++++++++++++++++++++++ 4 files changed, 255 insertions(+), 11 deletions(-) create mode 100644 apps/even-client/test/speaker.test.ts diff --git a/README.md b/README.md index 713ddfe..864ad78 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ the implementer read from the server's log, and what Cody reproduced independent |---|---| | **What is where today** | Server (bridge and static page): `d65beeab77288049b801e4969ac9b68821833b23`, since 2026-11-29 19:50 UTC. Even account, beta channel: client 0.4.1, built from that commit. Both deployed and checked by the implementer, not reproduced by Cody. This repository's client is **0.5.0, a candidate: not deployed, not uploaded, not on the device.** | | Client, bridge and contract | Implemented. | -| Automated tests with fakes (375) | Passing. | +| Automated tests with fakes (384) | Passing. | | Client in Even's official simulator | 9 scenarios, run by the implementer, simulator only. Eight run with the menu, hands free and live preview switched off (the tap path and idle exit); the ninth launches the app as shipped and covers the menu and the microphone opening and closing for hands free, with a silent microphone, so no name is ever heard. Live preview is not exercised in the simulator. One unexplained failure of the idle-exit scenario in the Round 19 runs; see `docs/test-evidence.md`. | | Bridge against a real Hermes gateway | Passing (9 scenarios) with a **stub model**. | | Built-in speech-to-text (Whisper, in the bridge) | Passing (11 scenarios) on a real speech recording, including with no network. Not yet on audio from the glasses. | @@ -34,7 +34,7 @@ the implementer read from the server's log, and what Cody reproduced independent | Opening menu (Hands free, Agents); hands-free mode: calling an agent by name ("Wendy, ...") | Introduced in 0.3.0 (`44ea114`, no longer deployed); part of what is deployed and on beta now (0.4.1, `d65beea`). Fakes: passing. Simulator: menu and microphone open/close only (scenario 9). Device: tried by Dan on 2026-11-28, "pretty cool"; a name reached an agent and a different name switched agent. No item-by-item report; section D of the device plan has not been run. `docs/decisions/0004-always-listening.md`, `docs/evidence/device/2026-11-28.md`. | | Words shown while recording (Agents only); "Heard: ..." while the agent works | Introduced in 0.4.0; part of what is deployed and on beta now (0.4.1, `d65beea`). Fakes: passing. Not in the simulator. Device, Dan, 2026-11-28: "Heard" appears; **live words did not appear**, cause unknown; and Dan wants them in hands free, not Agents (open, not built). `docs/decisions/0005-live-preview.md`, `docs/evidence/device/2026-11-28.md`. | | Sound check on the phone page (can the page play sound, untouched, later; which outputs it sees) | 0.4.1, deployed and on beta (`d65beea`). A probe for issue #1, not a feature. Fake sound engine: passing. Device, Dan, 2026-11-29, earbuds connected: tone heard in the earbuds, allowed without a touch, played 20 s after a press, no phone voice, one unnamed output (`docs/evidence/device/2026-11-29.md`). Not in the simulator. | -| Answers read aloud ("Voice" on the opening menu; ElevenLabs voices per agent, called by the bridge; first 900 characters) | 0.5.0 candidate. Fakes only: a fake provider and a fake loudspeaker. **The real provider has never been called; MP3 playback has never run in a browser, the simulator, the Even app or on the device; not deployed, not uploaded.** `docs/decisions/0006-spoken-answers.md`. | +| Answers read aloud ("Voice" on the opening menu; ElevenLabs voices per agent, called by the bridge; first 900 characters) | 0.5.0 candidate. Fakes only: a fake provider and a fake loudspeaker. The playback code itself is tested against a fake sound engine. **The real provider has never been called; MP3 decoding and playback have never run in a real browser, the simulator, the Even app or on the device; not deployed, not uploaded, not installed.** `docs/decisions/0006-spoken-answers.md`. | | Real tools and approval flow | One tool use seen (a Telegram message, reported by Dan). The approval path has **not** been exercised against a real agent. | | Sustained load, backups, monitoring, upgrades on the server | **Not verified.** | | Clean-host install (`deploy/install.sh`) | Proven on a disposable Ubuntu 24.04 container with systemd (`tests/deploy/runbook-proof.sh`), not on a second real server. | @@ -128,6 +128,9 @@ What each suite does and does not show is in `docs/test-evidence.md`. ## Not in this slice -On-device wake-word detection (names are recognised on the bridge), camera features, -text-to-speech, billing, store launch, and running alongside Even's native captions (the public -SDK does not offer it). +On-device wake-word detection (names are recognised on the bridge), camera features, speech +made on the phone or by a native Android component (the 0.5.0 candidate's spoken answers are +generated by the bridge through ElevenLabs and only played by the page; see +`docs/decisions/0006-spoken-answers.md`), knowing which sound output is in use or whether +earbuds are worn (issue #1), billing, store launch, and running alongside Even's native captions +(the public SDK does not offer it). diff --git a/apps/even-client/src/core/controller.ts b/apps/even-client/src/core/controller.ts index 97091dd..309983e 100644 --- a/apps/even-client/src/core/controller.ts +++ b/apps/even-client/src/core/controller.ts @@ -846,6 +846,8 @@ export class Controller { /** The phone page is being used (touch or key): do not leave under the user's fingers. */ notePhoneActivity(): void { + // Said once per stretch of use, so a session that did not leave by itself can be explained. + if (this.now() >= this.phoneBusyUntil) this.note('lifecycle', 'phone page touched: not leaving the glasses while it is in use'); this.phoneBusyUntil = this.now() + this.phoneGraceMs; this.scheduleIdleExit(); } diff --git a/apps/even-client/src/ui/speaker.ts b/apps/even-client/src/ui/speaker.ts index 8e94fd5..b5e01d9 100644 --- a/apps/even-client/src/ui/speaker.ts +++ b/apps/even-client/src/ui/speaker.ts @@ -7,16 +7,22 @@ import type { SpeakerPort } from '../core/controller.ts'; * * Device evidence (docs/evidence/device/2026-11-29.md): in the Even app on Dan's phone a page * may play sound without a touch, and did so 20 s after a press. Playing an MP3 this way has - * NOT been tried on the device. + * NOT been tried in any real browser or on the device; the tests use a fake sound engine. */ export function createSpeaker(win: Window): SpeakerPort | null { const w = win as unknown as { AudioContext?: typeof AudioContext; webkitAudioContext?: typeof AudioContext }; const Ctor = w.AudioContext ?? w.webkitAudioContext; if (!Ctor) return null; let context: AudioContext | null = null; + /** + * Counts every play() and every stop(). A play() that finds a different number when one of + * its waits is over has been stopped or replaced, and must not start (or touch) anything. + */ + let generation = 0; let current: { source: AudioBufferSourceNode; done: () => void } | null = null; - const stop = () => { + /** Silence whatever is sounding. Does not by itself invalidate work still in progress. */ + const silence = () => { const playing = current; current = null; if (!playing) return; @@ -31,16 +37,19 @@ export function createSpeaker(win: Window): SpeakerPort | null { return { async play(audio: Uint8Array): Promise { - stop(); + // A new play replaces the old one, whatever stage that had reached. + const mine = ++generation; + silence(); context ??= new Ctor(); const ctx = context; await Promise.race([ctx.resume(), new Promise((r) => win.setTimeout(r, 800))]); + if (mine !== generation) return; if (ctx.state !== 'running') throw new Error(`sound is blocked (${ctx.state})`); // decodeAudioData takes ownership of the buffer it is given: hand it a copy. const copy = audio.buffer.slice(audio.byteOffset, audio.byteOffset + audio.byteLength) as ArrayBuffer; const buffer = await ctx.decodeAudioData(copy); - // Stopped, or replaced by a newer answer, while decoding. - if (current) stop(); + // Stopped, or replaced by a newer answer, while decoding: nothing is created or started. + if (mine !== generation) return; const source = ctx.createBufferSource(); source.buffer = buffer; source.connect(ctx.destination); @@ -53,6 +62,9 @@ export function createSpeaker(win: Window): SpeakerPort | null { source.start(); }); }, - stop, + stop(): void { + generation += 1; + silence(); + }, }; } diff --git a/apps/even-client/test/speaker.test.ts b/apps/even-client/test/speaker.test.ts new file mode 100644 index 0000000..fadd9dd --- /dev/null +++ b/apps/even-client/test/speaker.test.ts @@ -0,0 +1,227 @@ +/** + * The real loudspeaker code against a FAKE sound engine whose resume() and decodeAudioData() + * finish only when the test says so. This shows that stopped or replaced speech never starts; + * it says nothing about MP3 decoding or playback in a real browser, the Even app or a phone. + */ +import { beforeEach, describe, expect, it } from 'vitest'; +import { createSpeaker } from '../src/ui/speaker.ts'; + +interface Deferred { + promise: Promise; + resolve: (value: T) => void; + reject: (err: Error) => void; +} +function deferred(): Deferred { + let resolve!: (value: T) => void; + let reject!: (err: Error) => void; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return { promise, resolve, reject }; +} + +class FakeSource { + buffer: unknown = null; + onended: (() => void) | null = null; + started = 0; + stopped = 0; + connect(): void {} + start(): void { + this.started += 1; + } + stop(): void { + this.stopped += 1; + } +} + +class FakeAudioContext { + static last: FakeAudioContext; + state: 'suspended' | 'running' = 'suspended'; + destination = {}; + resumes: Array> = []; + decodes: Array & { label: string }> = []; + sources: FakeSource[] = []; + /** When true, resume() succeeds at once (an engine that is already running). */ + ready = false; + constructor() { + FakeAudioContext.last = this; + } + resume(): Promise { + if (this.ready) { + this.state = 'running'; + return Promise.resolve(); + } + const d = deferred(); + this.resumes.push(d); + return d.promise.then(() => { + this.state = 'running'; + }); + } + decodeAudioData(data: ArrayBuffer): Promise<{ label: string }> { + const label = new TextDecoder().decode(data); + const d = Object.assign(deferred<{ label: string }>(), { label }); + this.decodes.push(d); + return d.promise; + } + createBufferSource(): FakeSource { + const source = new FakeSource(); + this.sources.push(source); + return source; + } +} + +const bytes = (label: string) => new TextEncoder().encode(label); +const tick = () => new Promise((r) => setTimeout(r, 0)); +const win = () => ({ AudioContext: FakeAudioContext, setTimeout: (fn: () => void, ms: number) => setTimeout(fn, ms) }) as unknown as Window; +const ctx = () => FakeAudioContext.last; +/** A promise's state without waiting for it. */ +async function settled(p: Promise): Promise<'pending' | 'resolved' | 'rejected'> { + const marker = {}; + return Promise.race([p.then(() => 'resolved' as const, () => 'rejected' as const), tick().then(() => marker)]).then((v) => (v === marker ? 'pending' : (v as 'resolved' | 'rejected'))); +} +const starts = () => ctx().sources.reduce((n, s) => n + s.started, 0); + +describe('loudspeaker (fake sound engine)', () => { + let speaker: NonNullable>; + beforeEach(() => { + speaker = createSpeaker(win())!; + }); + + it('there is no loudspeaker in a page without a sound engine', () => { + expect(createSpeaker({ setTimeout } as unknown as Window)).toBeNull(); + }); + + it('plays to the end: resolves when the audio finishes by itself', async () => { + const playing = speaker.play(bytes('one')); + ctx().resumes[0]!.resolve(); + await tick(); + expect(ctx().decodes.map((d) => d.label)).toEqual(['one']); + ctx().decodes[0]!.resolve({ label: 'one' }); + await tick(); + expect(ctx().sources).toHaveLength(1); + expect(ctx().sources[0]!.started).toBe(1); + expect(ctx().sources[0]!.buffer).toEqual({ label: 'one' }); + expect(await settled(playing)).toBe('pending'); + ctx().sources[0]!.onended!(); + expect(await settled(playing)).toBe('resolved'); + expect(ctx().sources[0]!.stopped).toBe(0); + // Stopping afterwards touches nothing. + speaker.stop(); + expect(ctx().sources[0]!.stopped).toBe(0); + }); + + it('stop while it is sounding stops that source once and ends the play', async () => { + const playing = speaker.play(bytes('one')); + ctx().resumes[0]!.resolve(); + await tick(); + ctx().decodes[0]!.resolve({ label: 'one' }); + await tick(); + speaker.stop(); + expect(ctx().sources[0]!.stopped).toBe(1); + expect(await settled(playing)).toBe('resolved'); + speaker.stop(); + expect(ctx().sources[0]!.stopped).toBe(1); + expect(starts()).toBe(1); + }); + + it('stop while the engine is still starting: nothing is decoded or started later', async () => { + const playing = speaker.play(bytes('one')); + speaker.stop(); + ctx().resumes[0]!.resolve(); + await tick(); + expect(await settled(playing)).toBe('resolved'); + expect(ctx().decodes).toHaveLength(0); + expect(ctx().sources).toHaveLength(0); + }); + + it('stop while decoding: the decoded audio is never started', async () => { + const playing = speaker.play(bytes('one')); + ctx().resumes[0]!.resolve(); + await tick(); + expect(ctx().decodes).toHaveLength(1); + speaker.stop(); + ctx().decodes[0]!.resolve({ label: 'one' }); + await tick(); + expect(await settled(playing)).toBe('resolved'); + expect(ctx().sources).toHaveLength(0); + expect(starts()).toBe(0); + }); + + it('two answers decoding at once, the older finishing last: only the newer one is heard', async () => { + const older = speaker.play(bytes('older')); + ctx().resumes[0]!.resolve(); + await tick(); + ctx().ready = true; + const newer = speaker.play(bytes('newer')); + await tick(); + expect(ctx().decodes.map((d) => d.label)).toEqual(['older', 'newer']); + // The newer one is ready first and starts. + ctx().decodes[1]!.resolve({ label: 'newer' }); + await tick(); + expect(ctx().sources).toHaveLength(1); + expect(ctx().sources[0]!.buffer).toEqual({ label: 'newer' }); + // Then the older one arrives: it starts nothing and does not stop the newer one. + ctx().decodes[0]!.resolve({ label: 'older' }); + await tick(); + expect(await settled(older)).toBe('resolved'); + expect(ctx().sources).toHaveLength(1); + expect(ctx().sources[0]!.stopped).toBe(0); + expect(await settled(newer)).toBe('pending'); + ctx().sources[0]!.onended!(); + expect(await settled(newer)).toBe('resolved'); + expect(starts()).toBe(1); + }); + + it('two answers decoding at once, the older finishing first: it is not heard either', async () => { + const older = speaker.play(bytes('older')); + ctx().resumes[0]!.resolve(); + await tick(); + ctx().ready = true; + const newer = speaker.play(bytes('newer')); + await tick(); + ctx().decodes[0]!.resolve({ label: 'older' }); + await tick(); + expect(await settled(older)).toBe('resolved'); + expect(ctx().sources).toHaveLength(0); + ctx().decodes[1]!.resolve({ label: 'newer' }); + await tick(); + expect(ctx().sources.map((s) => s.buffer)).toEqual([{ label: 'newer' }]); + speaker.stop(); + expect(await settled(newer)).toBe('resolved'); + }); + + it('a new answer while one is sounding stops the old source and plays the new', async () => { + const first = speaker.play(bytes('first')); + ctx().resumes[0]!.resolve(); + await tick(); + ctx().decodes[0]!.resolve({ label: 'first' }); + await tick(); + ctx().ready = true; + const second = speaker.play(bytes('second')); + expect(ctx().sources[0]!.stopped).toBe(1); + expect(await settled(first)).toBe('resolved'); + await tick(); + ctx().decodes[1]!.resolve({ label: 'second' }); + await tick(); + expect(ctx().sources).toHaveLength(2); + expect(ctx().sources[1]!.started).toBe(1); + // The old source's late "ended" changes nothing. + ctx().sources[0]!.onended?.(); + expect(await settled(second)).toBe('pending'); + expect(ctx().sources[1]!.stopped).toBe(0); + }); + + it('a blocked engine and undecodable audio are failures, and leave nothing playing', async () => { + const blocked = speaker.play(bytes('one')); // resume never settles; the 800 ms race ends it + blocked.catch(() => undefined); // looked at below, after the wait + await new Promise((r) => setTimeout(r, 850)); + expect(await settled(blocked)).toBe('rejected'); + ctx().ready = true; + const bad = speaker.play(bytes('two')); + await tick(); + ctx().decodes[0]!.reject(new Error('EncodingError')); + expect(await settled(bad)).toBe('rejected'); + expect(ctx().sources).toHaveLength(0); + }); +});