From cc645d9e2c235fd4b8fa71470754ca95f6d5ebfd Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 17 Nov 2026 19:49:34 +0000 Subject: [PATCH 1/2] fix(ui-react): guard completed mic holds through release --- .changeset/steady-mic-release.md | 5 ++ .../src/components/chat/composer-mic.tsx | 23 ++++-- .../tests/browser/composer-mic.pointer.tsx | 71 ++++++++++++++++--- 3 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 .changeset/steady-mic-release.md diff --git a/.changeset/steady-mic-release.md b/.changeset/steady-mic-release.md new file mode 100644 index 0000000000..0acbc1fb58 --- /dev/null +++ b/.changeset/steady-mic-release.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-ui-react": patch +--- + +Keep a completed composer mic hold guarded until release so lifting a long touch cannot press Start or begin dictation. diff --git a/packages/ui-react/src/components/chat/composer-mic.tsx b/packages/ui-react/src/components/chat/composer-mic.tsx index 84d92606ce..a247544c03 100644 --- a/packages/ui-react/src/components/chat/composer-mic.tsx +++ b/packages/ui-react/src/components/chat/composer-mic.tsx @@ -4,7 +4,7 @@ import { MicButton, type MicPhase } from "@schlessera/brain-ui-kit"; /** V1 (#3690): a hold this long opens Start; an earlier release is a tap. */ const HOLD_MS = 600; -/** How long a stray click after a hold or a cancelled hold is swallowed. */ +/** How long to wait for a stray click after release, or a cancelled hold. */ const SWALLOW_MS = 1000; interface ComposerMicProps { @@ -54,17 +54,32 @@ export function ComposerMic(p: ComposerMicProps) { * click (on the mic, or on whatever replaced it). That click is not a tap. * The next press of any kind ends the guard, so a later tap is never lost. */ - function swallowNextClick() { + function swallowNextClick(pointerId?: number) { swallow.current?.(); + let expiry: ReturnType | undefined; + // A completed hold can stay down indefinitely, including after unmount. + // Only its release starts the fallback cleanup for browsers with no click. + const released = (event: globalThis.PointerEvent) => { + if (event.pointerId !== pointerId) return; + window.removeEventListener("pointerup", released, true); + window.removeEventListener("pointercancel", released, true); + expiry = setTimeout(end, SWALLOW_MS); + }; const click = (event: MouseEvent) => { event.preventDefault(); event.stopPropagation(); end(); }; const end = () => { window.removeEventListener("click", click, true); window.removeEventListener("pointerdown", end, true); window.removeEventListener("keydown", end, true); + window.removeEventListener("pointerup", released, true); + window.removeEventListener("pointercancel", released, true); clearTimeout(expiry); if (swallow.current === end) swallow.current = null; }; - const expiry = setTimeout(end, SWALLOW_MS); + if (pointerId === undefined) expiry = setTimeout(end, SWALLOW_MS); + else { + window.addEventListener("pointerup", released, true); + window.addEventListener("pointercancel", released, true); + } window.addEventListener("click", click, true); window.addEventListener("pointerdown", end, true); window.addEventListener("keydown", end, true); @@ -109,7 +124,7 @@ export function ComposerMic(p: ComposerMicProps) { holding.current = false; setPhase("complete"); setProgress(1); - swallowNextClick(); + swallowNextClick(event.pointerId); try { navigator.vibrate?.(10); } catch { /* No haptic on this device. */ } latest.current.onTalkLive?.(); }, HOLD_MS); diff --git a/packages/ui-react/tests/browser/composer-mic.pointer.tsx b/packages/ui-react/tests/browser/composer-mic.pointer.tsx index 30d3bbe7eb..168953cd24 100644 --- a/packages/ui-react/tests/browser/composer-mic.pointer.tsx +++ b/packages/ui-react/tests/browser/composer-mic.pointer.tsx @@ -13,7 +13,8 @@ * its complete property descriptor when the test ends. * * The mic has two timings: a press held 0.6 s opens Start, and after a - * completed or cancelled hold the release's click is swallowed for one second. + * cancelled hold the release's click is swallowed for one second. A completed + * hold stays guarded until its release, however long the press remains down. * Both run on a clock this file controls (`stopClock`). Input stays native; * time passes only when a test advances it, so a slow runner cannot stretch a * dwell between two inputs (#4170, #4184). @@ -253,7 +254,7 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`a comp advance(1); expect(s.client.state.getState().disclosureOpen, `${kind}: the hold opens Start while the mic is still held`).toBe(true); expect(s.haptics, `${kind}: one light haptic while the mic is still held`).toEqual([10]); - // Released one millisecond before the click guard ends. + // Released after 999 ms; the completed hold is still guarded. advance(GUARD - 1); await release(kind, at, HOLD + GUARD - 1); // The release is not a tap: Start stays, and no dictation began. @@ -268,6 +269,27 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`a comp } }); +for (const kind of KINDS) for (const width of [320, 1280] as const) test(`five seconds after Start opens the original release never presses Start ${kind} ${width}`, async ctx => { + premise(); + const s = await scene(ctx, width); + const at = centre(s.mic()!); + const clock = stopClock(ctx); + await press(s, kind, at); + advance(HOLD); + expect(s.client.state.getState().disclosureOpen, "the hold opens Start before release").toBe(true); + advance(5_000); + await release(kind, at, HOLD + 5_000); + expect(s.network.socket()!.frames("conversation_start"), "the original release must not press Start").toHaveLength(0); + expect(s.client.state.getState().disclosureOpen, "Start stays open and unpressed").toBe(true); + expect(s.microphone, "the original release captures nothing").toEqual([]); + // A fresh native press is deliberate consent, even immediately after release. + const start = centre(page.getByRole("button", { name: "Start", exact: true }).element()); + clock.restore(); + await commands.sheetInput(kind, [{ type: "down", ...start, t: 0 }, { type: "up", ...start, t: 100 }]); + await delivered(100); + await expect.poll(() => s.network.socket()!.frames("conversation_start").length, { message: "a deliberate tap presses Start" }).toBe(1); +}); + for (const kind of KINDS) for (const width of [320, 1280] as const) test(`an early release dictates, and never opens Start ${kind} ${width}`, async ctx => { premise(); { @@ -372,7 +394,7 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`withou expect(s.haptics, `${kind}: one light haptic while the mic is still held`).toEqual([10]); const ok = page.getByRole("button", { name: "OK", exact: true }); expect(document.activeElement, `${kind}: the notice takes focus`).toBe(ok.element()); - // Released one millisecond before the click guard ends. + // Released after 999 ms; the completed hold is still guarded. advance(GUARD - 1); await release(kind, at, HOLD + GUARD - 1); expect(s.client.state.getState().active, `${kind}: Start does not open`).toBe(false); @@ -387,7 +409,7 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`withou }); // The mic stays in place under S4, so the release's click lands on it for a mouse and for a touch. -for (const kind of KINDS) for (const width of [320, 1280] as const) test(`one second after a completed hold the release is a tap again ${kind} ${width}`, async ctx => { +for (const kind of KINDS) for (const width of [320, 1280] as const) test(`five seconds after S4 opens the original release is still swallowed ${kind} ${width}`, async ctx => { premise(); { const s = await scene(ctx, width); @@ -397,15 +419,48 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`one se await press(s, kind, at); advance(HOLD); expect(s.host.textContent, `${kind}: S4 replaces Start while the mic is still held`).toContain(S4); - // The case above releases one millisecond earlier, and is swallowed. - advance(GUARD); - await release(kind, at, HOLD + GUARD); - expect(s.ui.stores.voice.getState().mode, `${kind}: the click guard has ended, so the release dictates`).toBe("dictate"); + advance(5_000); + await release(kind, at, HOLD + 5_000); + expect(s.ui.stores.voice.getState().mode, `${kind}: the original release does not dictate`).toBe("idle"); + expect(s.host.textContent, "the release leaves S4 open").toContain(S4); + // The mic remains in place, and a new native tap still dictates. + await press(s, kind, at); + await release(kind, at, 100); + expect(s.ui.stores.voice.getState().mode, "a deliberate tap dictates").toBe("dictate"); expect(s.client.state.getState().active, `${kind}: Start does not open`).toBe(false); expect(s.haptics).toEqual([10]); } }); +// Start replaces the mic, so a mouse release produces no click. Exercise each +// way to end that surviving guard without letting its fallback timer expire. +for (const ending of ["press", "key", "click"] as const) test(`after a completed hold with no release click a new ${ending} ends the guard`, async ctx => { + premise(); + const s = await scene(ctx, 1280); + stopClock(ctx); + await press(s, "mouse", centre(s.mic()!)); + advance(HOLD); + expect(s.client.state.getState().disclosureOpen).toBe(true); + const start = page.getByRole("button", { name: "Start", exact: true }).element() as HTMLButtonElement; + const at = centre(start); + await release("mouse", at, HOLD); + expect(s.network.socket()!.frames("conversation_start"), "a mouse release after replacement makes no click").toHaveLength(0); + if (ending === "press") { + await commands.sheetInput("mouse", [{ type: "down", ...at, t: 0 }, { type: "up", ...at, t: 100 }]); + await delivered(100); + } else if (ending === "key") { + start.focus(); + await userEvent.keyboard("{Enter}"); + } else { + // A click with no preceding press (assistive activation) consumes the guard. + start.click(); + expect(s.network.socket()!.frames("conversation_start"), "the first unpaired click is swallowed").toHaveLength(0); + start.click(); + } + await reached(() => s.network.socket()!.frames("conversation_start").length === 1, `a fresh ${ending} allows Start`); + expect(s.network.socket()!.frames("conversation_start"), "a later deliberate activation is never lost").toHaveLength(1); +}); + test("the keyboard and a screen reader reach both modes", async ctx => { premise(); const s = await scene(ctx, 1280); From 5c35488a8d23b9c8164a3cf0b49fce4efbcce9bb Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 17 Nov 2026 20:05:30 +0000 Subject: [PATCH 2/2] test(ui-react): release mic holds where Start is hit --- .../tests/browser/composer-mic.pointer.tsx | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/ui-react/tests/browser/composer-mic.pointer.tsx b/packages/ui-react/tests/browser/composer-mic.pointer.tsx index 168953cd24..fc33b16baf 100644 --- a/packages/ui-react/tests/browser/composer-mic.pointer.tsx +++ b/packages/ui-react/tests/browser/composer-mic.pointer.tsx @@ -176,6 +176,20 @@ function centre(el: Element) { const box = el.getBoundingClientRect(); return { x: box.x + box.width / 2, y: box.y + box.height / 2 }; } +/** + * The mic's centre falls below Start on a coarse device. Its upper portion + * overlaps Start on all rail projects, so an in-place release can activate it. + */ +function holdPoint(el: Element) { + const box = el.getBoundingClientRect(); + return { x: box.x + box.width / 2, y: box.top + 4 }; +} + +function expectStartUnder(at: Point) { + const start = page.getByRole("button", { name: "Start", exact: true }).element(); + expect(start.contains(document.elementFromPoint(at.x, at.y)), "the original release point hits Start").toBe(true); +} + const phase = (s: Scene) => s.mic()?.dataset.phase ?? "no mic"; const ring = (s: Scene) => Number(s.mic()?.querySelector(".bk-voice-ring")?.getAttribute("data-progress")); const hint = (s: Scene) => s.host.querySelector(".bk-voice-hint")?.textContent ?? null; @@ -232,7 +246,7 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`a comp premise(); { const s = await scene(ctx, width); - const at = centre(s.mic()!); + const at = holdPoint(s.mic()!); const before = s.traffic(); const clock = stopClock(ctx); await press(s, kind, at); @@ -256,6 +270,7 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`a comp expect(s.haptics, `${kind}: one light haptic while the mic is still held`).toEqual([10]); // Released after 999 ms; the completed hold is still guarded. advance(GUARD - 1); + expectStartUnder(at); await release(kind, at, HOLD + GUARD - 1); // The release is not a tap: Start stays, and no dictation began. expect(s.client.state.getState().disclosureOpen, `${kind}: the release does not dismiss Start`).toBe(true); @@ -272,12 +287,13 @@ for (const kind of KINDS) for (const width of [320, 1280] as const) test(`a comp for (const kind of KINDS) for (const width of [320, 1280] as const) test(`five seconds after Start opens the original release never presses Start ${kind} ${width}`, async ctx => { premise(); const s = await scene(ctx, width); - const at = centre(s.mic()!); + const at = holdPoint(s.mic()!); const clock = stopClock(ctx); await press(s, kind, at); advance(HOLD); expect(s.client.state.getState().disclosureOpen, "the hold opens Start before release").toBe(true); advance(5_000); + expectStartUnder(at); await release(kind, at, HOLD + 5_000); expect(s.network.socket()!.frames("conversation_start"), "the original release must not press Start").toHaveLength(0); expect(s.client.state.getState().disclosureOpen, "Start stays open and unpressed").toBe(true);