Commit 7c710194f for llama.cpp

commit 7c710194f31192e67dd57d853e88563ed90c9f0b
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date:   Sat Dec 5 20:59:14 2026 +0100

    ui: stop the microphone warm-up from prompting in Safari (#33805)

    Hovering the record button opened the microphone ahead of the press
    whenever the Permissions API said granted. WebKit grants a request
    made without a press only within a minute (on iOS) of the last
    capture, while the Permissions API keeps saying granted for longer,
    so the hover could bring up Safari's permission prompt on its own.

    In WebKit, the warm-up now opens the microphone only when one was let
    go moments ago. Also notes in ui/lib/voice/AGENTS.md how Safari keeps
    and drops microphone grants.

    Assisted-by: Claude Code (Claude Opus 5.5)

diff --git a/ui/lib/voice/AGENTS.md b/ui/lib/voice/AGENTS.md
new file mode 100644
index 000000000..5b1dfa944
--- /dev/null
+++ b/ui/lib/voice/AGENTS.md
@@ -0,0 +1,7 @@
+# Instructions for ui/lib/voice
+
+These add to the repository's [AGENTS.md](../../../AGENTS.md) for Llama Voice: microphone capture, live levels, transcription and the recorder the chat's voice messages and dictation run on.
+
+- **Safari forgets a microphone grant; the page cannot keep it.** With a site on "Ask", WebKit keeps an Allow only for the loaded page: a reload or a relaunch of a Dock or Home Screen web app drops it, and so does 10 minutes without a capture. A Home Screen web app on iOS asks again on every launch. Only the browser's own setting for the site (Allow) makes it last; no code here can.
+- **Without a press, WebKit asks sooner.** A `getUserMedia` call outside a user gesture is granted only within a minute (on iOS) of the last capture, while `navigator.permissions.query` keeps answering `granted` for as long as the page holds the grant, as if a press were coming. So anything that opens the microphone ahead of a press (`MicrophoneWarmup`) checks `microphoneAllowedWithoutPress`, which in WebKit also needs a microphone let go moments ago (`stopStream` records when), or the prompt appears on a hover.
+- **A press keeps its gesture through promises.** WebKit carries the gesture of a click or a mouse press through awaited promises and `enumerateDevices`, so a recording may resolve its device and options before it calls `getUserMedia`. Anything else in between, such as a timer or a network round trip, may drop it.
diff --git a/ui/lib/voice/src/constants.ts b/ui/lib/voice/src/constants.ts
index 3b56c078a..d72d73205 100644
--- a/ui/lib/voice/src/constants.ts
+++ b/ui/lib/voice/src/constants.ts
@@ -187,6 +187,17 @@ export const VOICE_GAIN = {
  */
 export const VOICE_WARMUP_RELEASE_MS = 1500;

+/**
+ * WebKit (Safari, and every browser on iOS) asks again when a page opens the microphone
+ * without a press a while after it last let one go: after a minute on iOS. The
+ * Permissions API keeps saying granted meanwhile, so a microphone opened ahead of a press
+ * (MicrophoneWarmup) only opens this soon after the last one was let go.
+ */
+export const VOICE_WEBKIT_UNPROMPTED_REOPEN_MS = 45_000;
+
+/** navigator.vendor in WebKit browsers: Safari, and every browser on iOS. */
+export const VOICE_WEBKIT_VENDOR = 'Apple Computer, Inc.';
+
 /** Messages of the errors a recording, a transcription or a realtime session ends with. */
 export const VOICE_ERROR_MESSAGE = {
 	captureAlreadyStarted: 'Voice capture already started',
diff --git a/ui/lib/voice/src/microphone.ts b/ui/lib/voice/src/microphone.ts
index 6825e60a7..068a399d5 100644
--- a/ui/lib/voice/src/microphone.ts
+++ b/ui/lib/voice/src/microphone.ts
@@ -12,7 +12,10 @@ import {
 	VOICE_BUILT_IN_LABEL_PATTERN,
 	VOICE_DEVICE_ALIAS,
 	VOICE_MICROPHONE_CONSTRAINTS,
-	VOICE_MICROPHONE_PERMISSION
+	VOICE_MICROPHONE_PERMISSION,
+	VOICE_TRACK_LIVE,
+	VOICE_WEBKIT_UNPROMPTED_REOPEN_MS,
+	VOICE_WEBKIT_VENDOR
 } from './constants.js';

 export const VOICE_MICROPHONE_AUTO = 'auto';
@@ -21,6 +24,9 @@ export const VOICE_MICROPHONE_DEFAULT = 'default';
 // Chrome lists the system default and the communications device as extra entries
 const ALIASES = new Set<string>(Object.values(VOICE_DEVICE_ALIAS));

+// when a live microphone was last let go, see microphoneAllowedWithoutPress
+let releasedAt = -Infinity;
+
 export interface Microphone {
 	deviceId: string;
 	label: string;
@@ -84,7 +90,11 @@ export async function resolveMicrophone(choice: string): Promise<string | undefi

 /** Lets go of a microphone: its tracks stop, and the browser's recording indicator goes. */
 export function stopStream(stream: MediaStream | null | undefined): void {
-	for (const track of stream?.getTracks() ?? []) track.stop();
+	for (const track of stream?.getTracks() ?? []) {
+		if (track.readyState === VOICE_TRACK_LIVE) releasedAt = Date.now();
+
+		track.stop();
+	}
 }

 /**
@@ -121,3 +131,19 @@ export async function microphoneAllowed(): Promise<boolean> {
 		return false;
 	}
 }
+
+/**
+ * Whether the page may open the microphone with no press and without the browser asking:
+ * access was granted, and in WebKit a microphone was let go only moments ago (see
+ * VOICE_WEBKIT_UNPROMPTED_REOPEN_MS).
+ */
+export async function microphoneAllowedWithoutPress(): Promise<boolean> {
+	if (
+		navigator.vendor === VOICE_WEBKIT_VENDOR &&
+		Date.now() - releasedAt > VOICE_WEBKIT_UNPROMPTED_REOPEN_MS
+	) {
+		return false;
+	}
+
+	return microphoneAllowed();
+}
diff --git a/ui/lib/voice/src/warm-microphone.ts b/ui/lib/voice/src/warm-microphone.ts
index 4d029c092..85d1ee81d 100644
--- a/ui/lib/voice/src/warm-microphone.ts
+++ b/ui/lib/voice/src/warm-microphone.ts
@@ -1,5 +1,5 @@
 import { VOICE_TRACK_LIVE, VOICE_WARMUP_RELEASE_MS } from './constants.js';
-import { microphoneAllowed, microphoneRequest, stopStream } from './microphone.js';
+import { microphoneAllowedWithoutPress, microphoneRequest, stopStream } from './microphone.js';
 import type { VoiceCaptureOptions } from './types.js';

 type MicrophoneOptions = Pick<VoiceCaptureOptions, 'constraints' | 'deviceId'>;
@@ -35,7 +35,8 @@ export class MicrophoneWarmup {
 		this.drop();

 		const stream = (async () => {
-			if (!navigator.mediaDevices?.getUserMedia || !(await microphoneAllowed())) return null;
+			if (!navigator.mediaDevices?.getUserMedia || !(await microphoneAllowedWithoutPress()))
+				return null;

 			try {
 				return await navigator.mediaDevices.getUserMedia(request);
diff --git a/ui/lib/voice/tests/warm-microphone.test.ts b/ui/lib/voice/tests/warm-microphone.test.ts
index 931235f11..cdd690e1e 100644
--- a/ui/lib/voice/tests/warm-microphone.test.ts
+++ b/ui/lib/voice/tests/warm-microphone.test.ts
@@ -1,8 +1,12 @@
 // Guards opening the microphone ahead of a recording: only when the page may already use
 // it, handed over to a recording with the same options, and let go otherwise.

-import { VOICE_MICROPHONE_CONSTRAINTS } from '../src/constants.js';
-import { microphoneRequest } from '../src/microphone.js';
+import {
+	VOICE_MICROPHONE_CONSTRAINTS,
+	VOICE_WEBKIT_UNPROMPTED_REOPEN_MS,
+	VOICE_WEBKIT_VENDOR
+} from '../src/constants.js';
+import { microphoneRequest, stopStream } from '../src/microphone.js';
 import { MicrophoneWarmup } from '../src/warm-microphone.js';
 import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

@@ -91,6 +95,21 @@ describe('MicrophoneWarmup', () => {
 		expect(getUserMedia).not.toHaveBeenCalled();
 	});

+	it('in WebKit, opens only soon after a microphone was let go: later the browser asks again', async () => {
+		(navigator as { vendor?: string }).vendor = VOICE_WEBKIT_VENDOR;
+
+		const warmup = new MicrophoneWarmup();
+
+		stopStream(fakeStream());
+		warmup.open();
+		expect(await warmup.take()).not.toBeNull();
+
+		await vi.advanceTimersByTimeAsync(VOICE_WEBKIT_UNPROMPTED_REOPEN_MS + 1);
+		warmup.open();
+		expect(await warmup.take()).toBeNull();
+		expect(getUserMedia).toHaveBeenCalledOnce();
+	});
+
 	it('lets go of a microphone opened for other options', async () => {
 		const warmup = new MicrophoneWarmup();
 		const opened = fakeStream();