From 3a8b100f92100b5b59845441588b0d204ab9ff75 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Mon, 9 Nov 2026 12:24:06 -0800 Subject: [PATCH 01/28] test: remove low-value speech, sidebar and composer checks (#40470) Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .../scripts/ci/sharding/ci-shard-timings.json | 1 - .../verify-computer-native.mjs | 14 - ...untime-mobile-dictation-controller.test.ts | 28 +- src/main/speech/model-manager.test.ts | 88 +----- src/main/speech/stt-service.test.ts | 142 +-------- .../inline-rich-draft-consumers.test.tsx | 58 ---- .../rows/section-header-create-host.test.tsx | 45 --- .../header-folder-composition.test.tsx | 293 ------------------ ...worktree-header-section-boundaries.test.ts | 38 --- 9 files changed, 6 insertions(+), 701 deletions(-) delete mode 100644 src/renderer/src/components/sidebar/worktree-list/viewport/header-folder-composition.test.tsx delete mode 100644 src/renderer/src/components/sidebar/worktree-list/viewport/worktree-header-section-boundaries.test.ts diff --git a/config/scripts/ci/sharding/ci-shard-timings.json b/config/scripts/ci/sharding/ci-shard-timings.json index afd1ef5bf40..82251701666 100644 --- a/config/scripts/ci/sharding/ci-shard-timings.json +++ b/config/scripts/ci/sharding/ci-shard-timings.json @@ -5689,7 +5689,6 @@ "src/renderer/src/components/sidebar/worktree-list/rows/repo-header-ssh-isolation.test.tsx": 2945, "src/renderer/src/components/sidebar/worktree-list/rows/section-header-create-host.test.tsx": 2393, "src/renderer/src/components/sidebar/worktree-list/viewport/hard-scroll-up.test.ts": 66, - "src/renderer/src/components/sidebar/worktree-list/viewport/header-folder-composition.test.tsx": 3725, "src/renderer/src/components/sidebar/worktree-list/viewport/use-scroll-to-top.test.ts": 293, "src/renderer/src/components/sidebar/worktree-list/viewport/use-scroll-to-top.test.tsx": 269, "src/renderer/src/components/sidebar/worktree-list/drag/worktree-manual-order-store-write.test.ts": 2144, diff --git a/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs b/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs index f0c1e76f622..4c8e6a3ecbb 100644 --- a/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs +++ b/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs @@ -13,20 +13,6 @@ const checks = [ args: ['test', '--package-path', 'native/computer-use-macos'], enabled: process.platform === 'darwin' }, - { - name: 'Linux provider Python syntax', - command: 'python3', - args: [ - '-c', - [ - 'import ast, pathlib', - 'source=pathlib.Path("native/computer-use-linux/runtime.py").read_text(encoding="utf-8")', - 'ast.parse(source)', - 'print("syntax-ok")' - ].join(';') - ], - enabled: true - }, { name: 'Linux snapshot renderer tests', command: 'python3', diff --git a/src/main/runtime/speech/runtime-mobile-dictation-controller.test.ts b/src/main/runtime/speech/runtime-mobile-dictation-controller.test.ts index 9040d53c061..d113ac4a69f 100644 --- a/src/main/runtime/speech/runtime-mobile-dictation-controller.test.ts +++ b/src/main/runtime/speech/runtime-mobile-dictation-controller.test.ts @@ -11,7 +11,6 @@ import type { RuntimeStore } from '../construction/runtime-store-contract' type Sink = (event: { type: string; text?: string }) => void let sink: Sink = () => {} -let startedTerms: unknown // A finish transcribes what was said; tests that never settle it model a slow transcription. let transcription: Promise = Promise.resolve() const stopDictation = vi.fn(async (_owner: string, options?: { discard?: boolean }) => { @@ -29,9 +28,8 @@ beforeEach(() => { const factories = { createModelManager: () => ({ getModelState: async () => ({ status: 'ready' }) }), createSttService: () => ({ - startDictation: async (_modelId: string, next: Sink, terms: unknown) => { + startDictation: async (_modelId: string, next: Sink) => { sink = next - startedTerms = terms }, feedAudio, stopDictation @@ -175,27 +173,3 @@ describe('mobile dictation at the cloud length cap', () => { expect(controller.feed(chunk)).toEqual({ dictationId: 'd-1', audioLimitReached: true }) }) }) - -describe('mobile dictation dictionary', () => { - it("spells with the dictating worktree's repo terms before the host's personal ones", async () => { - const store = { - getSettings: () => ({ - voice: { ...getDefaultVoiceSettings(), enabled: true, dictationTerms: [{ term: 'Orca' }] } - }), - getRepo: (id: string) => - id === 'repo-1' - ? { id, dictationTerms: [{ term: 'kubectl', heardAs: 'cube control' }] } - : undefined - } - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the controller reads only getSettings and getRepo. - const controller = new RuntimeMobileDictationController(() => store as unknown as RuntimeStore) - await controller.start({ - dictationId: 'd-1', - modelId: 'test-model', - worktreeId: 'repo-1::/work/repo', - clientId: 'phone-a' - }) - - expect(startedTerms).toEqual([{ term: 'kubectl', heardAs: 'cube control' }, { term: 'Orca' }]) - }) -}) diff --git a/src/main/speech/model-manager.test.ts b/src/main/speech/model-manager.test.ts index 5767578b0fa..ddcd490e104 100644 --- a/src/main/speech/model-manager.test.ts +++ b/src/main/speech/model-manager.test.ts @@ -1,7 +1,6 @@ -import 'node:crypto' -import { existsSync, mkdirSync, mkdtempSync, rmSync, truncateSync, writeFileSync } from 'node:fs' +import { mkdirSync, mkdtempSync, rmSync, truncateSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' -import { dirname, join } from 'node:path' +import { join } from 'node:path' import { beforeEach, describe, expect, it, vi } from 'vitest' import { SPEECH_MODEL_CATALOG } from './model-catalog' import { ModelManager } from './model-manager' @@ -26,17 +25,6 @@ vi.mock('./openai-api-key-store', () => ({ })) type ModelManagerInternals = { - verifyFileSha256: (filePath: string, expectedSha256: string) => Promise - downloadFileWithRetry: ( - url: string, - filePath: string, - expectedSize: number, - modelId: string, - isAborted: () => boolean, - signal: AbortSignal, - completedBytes?: number, - modelTotalBytes?: number - ) => Promise downloadFile: ( url: string, dest: string, @@ -62,78 +50,6 @@ describe('ModelManager', () => { hasOpenAiSpeechApiKeyMock.mockReturnValue(false) }) - it('installs individually verified model files through a staging directory', async () => { - const dir = mkdtempSync(join(tmpdir(), 'orca-model-manager-')) - try { - const manifest = SPEECH_MODEL_CATALOG.find( - (model) => model.id === 'zipformer-streaming-zh-14m' - )! - const manager = new ModelManager(dir, openElectronSpeechDownloadRequest) - const internals = manager as unknown as ModelManagerInternals - const downloadMock = vi - .spyOn(internals, 'downloadFileWithRetry') - .mockImplementation(async (_url, filePath, expectedSize) => { - writeFileSync(filePath, '') - truncateSync(filePath, expectedSize) - }) - const verifyMock = vi.spyOn(internals, 'verifyFileSha256').mockResolvedValue() - - await manager.downloadModel(manifest.id) - - const modelDir = manager.getModelDir(manifest.id) - expect(downloadMock).toHaveBeenCalledTimes(manifest.downloadFiles?.length ?? 0) - expect(verifyMock).toHaveBeenCalledTimes(manifest.downloadFiles?.length ?? 0) - let expectedOffset = 0 - for (const [index, file] of (manifest.downloadFiles ?? []).entries()) { - expect(downloadMock.mock.calls[index]?.slice(6)).toEqual([ - expectedOffset, - manifest.sizeBytes - ]) - expectedOffset += file.sizeBytes - expect(existsSync(join(modelDir, file.name))).toBe(true) - } - expect(existsSync(`${modelDir}.partial`)).toBe(false) - await expect(manager.getModelState(manifest.id)).resolves.toEqual({ - id: manifest.id, - status: 'ready' - }) - } finally { - rmSync(dir, { recursive: true, force: true }) - } - }) - - it('deletes a ready local model and reports it as not downloaded', async () => { - const dir = mkdtempSync(join(tmpdir(), 'orca-model-manager-')) - try { - const manifest = SPEECH_MODEL_CATALOG.find( - (model) => model.id === 'zipformer-streaming-zh-14m' - ) - expect(manifest?.files).toBeDefined() - const manager = new ModelManager(dir, openElectronSpeechDownloadRequest) - const modelDir = manager.getModelDir(manifest!.id) - for (const file of manifest!.downloadFiles ?? []) { - const path = join(modelDir, file.name) - mkdirSync(dirname(path), { recursive: true }) - writeFileSync(path, '') - truncateSync(path, file.sizeBytes) - } - - await expect(manager.getModelState(manifest!.id)).resolves.toEqual({ - id: manifest!.id, - status: 'ready' - }) - await manager.deleteModel(manifest!.id) - - expect(existsSync(modelDir)).toBe(false) - await expect(manager.getModelState(manifest!.id)).resolves.toEqual({ - id: manifest!.id, - status: 'not-downloaded' - }) - } finally { - rmSync(dir, { recursive: true, force: true }) - } - }) - it('reports a model downloaded in the old sherpa-onnx format as needing an update', async () => { const dir = mkdtempSync(join(tmpdir(), 'orca-model-manager-')) try { diff --git a/src/main/speech/stt-service.test.ts b/src/main/speech/stt-service.test.ts index 6ce68a1effe..cdd27198850 100644 --- a/src/main/speech/stt-service.test.ts +++ b/src/main/speech/stt-service.test.ts @@ -2,16 +2,7 @@ import type { ModelManager } from './model-manager' import type { SpeechModelState } from '../../shared/speech-types' import { beforeEach, describe, expect, it, vi } from 'vitest' -const { - MockOpenAiTranscriptionSession, - MockWorker, - getCloudSessions, - getCreatedWorkerCount, - getLastWorker, - readOpenAiSpeechApiKeyMock, - resetCloudSessions, - resetWorkers -} = vi.hoisted(() => { +const { MockWorker, getCreatedWorkerCount, getLastWorker, resetWorkers } = vi.hoisted(() => { class HoistedMockWorker extends EventTarget { static created = 0 static instances: HoistedMockWorker[] = [] @@ -86,34 +77,10 @@ const { } } - class HoistedMockOpenAiTranscriptionSession { - static instances: HoistedMockOpenAiTranscriptionSession[] = [] - feedCalls: { samples: Float32Array; sampleRate: number }[] = [] - - constructor(readonly readTarget: () => { model: string; apiKey: string }) { - HoistedMockOpenAiTranscriptionSession.instances.push(this) - } - - feedAudio(samples: Float32Array, sampleRate: number): void { - this.feedCalls.push({ samples, sampleRate }) - } - - finish(): Promise { - const target = this.readTarget() - return Promise.resolve(`${target.model}:${target.apiKey}`) - } - } - return { - MockOpenAiTranscriptionSession: HoistedMockOpenAiTranscriptionSession, MockWorker: HoistedMockWorker, - getCloudSessions: () => HoistedMockOpenAiTranscriptionSession.instances, getCreatedWorkerCount: () => HoistedMockWorker.created, getLastWorker: () => HoistedMockWorker.instances.at(-1), - readOpenAiSpeechApiKeyMock: vi.fn(() => 'test-openai-key'), - resetCloudSessions: () => { - HoistedMockOpenAiTranscriptionSession.instances = [] - }, resetWorkers: () => { HoistedMockWorker.created = 0 HoistedMockWorker.instances = [] @@ -190,12 +157,12 @@ vi.mock('./model-catalog', () => ({ vi.mock('./cloud-transcription-target', () => ({ resolveCloudTranscriptionTarget: (modelId: string) => ({ model: modelId, - apiKey: readOpenAiSpeechApiKeyMock() + apiKey: 'test-openai-key' }) })) vi.mock('./openai-transcription-client', () => ({ - OpenAiTranscriptionSession: MockOpenAiTranscriptionSession + OpenAiTranscriptionSession: class {} })) import { START_DICTATION_TIMEOUT_MS, SttService, type SttEventSink } from './stt-service' @@ -204,9 +171,7 @@ const DEFAULT_IDLE_UNLOAD_MS = 15 * 60 * 1000 describe('SttService', () => { beforeEach(() => { - resetCloudSessions() resetWorkers() - readOpenAiSpeechApiKeyMock.mockClear() }) it('gives each device dictating at once only its own text from one recognizer', async () => { @@ -376,18 +341,6 @@ describe('SttService', () => { }) }) - it('frees the warm worker at once when a local dictation is discarded', async () => { - const service = new SttService(readyModels(), spawnMockWorker) - - await service.startDictation('model-a', vi.fn(), undefined, 'mobile:1') - // Decoding the rest of the audio would hold the worker; a discard must not wait for it. - getLastWorker()!.emitStoppedOnStop = false - await service.stopDictation('mobile:1', { discard: true }) - await service.startDictation('model-a', vi.fn(), undefined, 'mobile:2') - - expect(getCreatedWorkerCount()).toBe(1) - }) - it('keeps a recognizer marked for unload until its dictation ends, then unloads it', async () => { const service = new SttService(readyModels(), spawnMockWorker) @@ -528,41 +481,6 @@ describe('SttService', () => { expect(worker!.terminated).toBe(true) }) - it('uses the OpenAI transcription session without creating a worker', async () => { - const sink = vi.fn() - const service = new SttService( - testModels({ - getModelState: vi.fn().mockResolvedValue({ id: 'openai-model', status: 'ready' }), - getModelDir: vi.fn().mockReturnValue('/tmp/model-a') - }), - spawnMockWorker - ) - - await service.startDictation('openai-model', sink, undefined, 'desktop') - service.feedAudio(new Float32Array([0.25, -0.25]), 48000, 'desktop') - await service.stopDictation('desktop') - - expect(getCreatedWorkerCount()).toBe(0) - expect(getCloudSessions()).toHaveLength(1) - expect(getCloudSessions()[0].feedCalls).toHaveLength(1) - expect(sink).toHaveBeenCalledWith({ type: 'ready' }) - expect(sink).toHaveBeenCalledWith({ - type: 'final', - text: 'openai-model:test-openai-key' - }) - expect(sink).toHaveBeenCalledWith({ type: 'stopped' }) - }) - - it('frees an idle local recognizer when a cloud dictation starts', async () => { - const service = new SttService(readyModels(), spawnMockWorker) - await service.startDictation('model-a', vi.fn(), undefined, 'desktop') - await service.stopDictation('desktop') - - await service.startDictation('openai-model', vi.fn(), undefined, 'desktop') - - expect(getLastWorker()!.terminated).toBe(true) - }) - it('lets a model that is still loading finish when its dictation is canceled', async () => { vi.useFakeTimers() try { @@ -635,44 +553,6 @@ describe('SttService', () => { } }) - it('does not treat internal warm-worker replacement as startup cancellation', async () => { - const service = new SttService(readyModels(), spawnMockWorker) - - await service.startDictation('whisper-model', vi.fn(), undefined, 'desktop:1', { - dictationLanguage: 'ja' - }) - await service.stopDictation('desktop:1') - - await expect( - service.startDictation('whisper-model', vi.fn(), undefined, 'desktop:1', { - dictationLanguage: 'ko' - }) - ).resolves.toBe(undefined) - expect(getCreatedWorkerCount()).toBe(2) - }) - - it('removes lifecycle listeners when the active worker errors', async () => { - const sink = vi.fn() - const service = new SttService(readyModels(), spawnMockWorker) - - await service.startDictation('model-a', sink, undefined, 'desktop') - const worker = getLastWorker() - expect(worker).toBeDefined() - expect(worker!.listenerCount('message')).toBe(1) - expect(worker!.listenerCount('error')).toBe(1) - expect(worker!.listenerCount('exit')).toBe(1) - - worker!.emit('error', new Error('worker failed')) - - expect(sink).toHaveBeenCalledWith({ - type: 'error', - error: 'Error: worker failed' - }) - expect(worker!.listenerCount('message')).toBe(0) - expect(worker!.listenerCount('error')).toBe(0) - expect(worker!.listenerCount('exit')).toBe(0) - }) - it('allows slow offline stop decoding before terminating the worker', async () => { vi.useFakeTimers() try { @@ -773,22 +653,6 @@ describe('SttService', () => { expect(events).toContainEqual({ type: 'stopped' }) }) - it('shares one stop message across concurrent stop callers', async () => { - const service = new SttService(readyModels(), spawnMockWorker) - - await service.startDictation('model-a', vi.fn(), undefined, 'desktop') - const worker = getLastWorker() - expect(worker).toBeDefined() - worker!.emitStoppedOnStop = false - - const firstStop = service.stopDictation('desktop') - const secondStop = service.stopDictation('desktop') - expect(worker!.messages.filter((message) => message.type === 'stop')).toHaveLength(1) - - worker!.emit('message', stoppedReply(worker!)) - await Promise.all([firstStop, secondStop]) - }) - it('does not retain or reuse a worker that timed out while stopping', async () => { vi.useFakeTimers() try { diff --git a/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx b/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx index 7cb534c294d..696d62e5a97 100644 --- a/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx +++ b/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx @@ -23,10 +23,6 @@ import { setNativeChatComposerDraftStorageForTests, type StoredNativeChatComposerDraft } from '../../drafts/native-chat-composer-draft-storage' -import { - replayNativeChatComposerDraftJournal, - snapshotNativeChatComposerDraftJournal -} from '../../drafts/native-chat-composer-draft-journal' import { resolveNativeChatPastedText } from '../../paste/native-chat-composer-pasted-text-references' import { nativeChatPastedTextToken } from '../../paste/native-chat-pasted-text-reference' import { @@ -163,60 +159,6 @@ it('keeps a rich return through an actual composing editor transaction and next expect(nextSend().skills).toEqual([{ name: 'review', path: '/returned/SKILL.md' }]) }) -it('replays a full rich addition into existing content, renders it and sends resolved files/images/skills', () => { - const edit = returned() - const paste = 'd851d135-98ac-44d1-b122-caf190002345' - const draft = readNativeChatComposerDraft(edit.scopeKey) - updateNativeChatComposerDraft( - edit.scopeKey, - { - text: `${draft.text}\n${nativeChatPastedTextToken(paste)}`, - document: { - ...draft.document, - content: [ - ...(draft.document?.content ?? []), - { - type: 'paragraph', - content: [ - { - type: 'nativeChatFileReference', - attrs: { token: nativeChatPastedTextToken(paste), path: '' } - } - ] - } - ] - }, - pastedTextFiles: { [paste]: '/remote/paste.txt' }, - images: [{ id: 'new', path: '/new.png' }] - }, - 'immediate' - ) - handBackNativeChatQueuedEdit(edit) - discardNativeChatQueuedEdit(edit) - // Simulate another renderer run reading the actual addition journal before storage confirmation. - const raw = localStorage.getItem('orca:nativeChatComposerDraftJournal:v1')! - localStorage.setItem( - 'orca:nativeChatComposerDraftJournal:v1', - raw.replaceAll(/"run":"[^"]+"/g, '"run":"previous-run"') - ) - const loaded = new Map([ - [scope, { text: 'existing', images: [], savedAt: 0 }] - ]) - replayNativeChatComposerDraftJournal(loaded, snapshotNativeChatComposerDraftJournal()) - updateNativeChatComposerDraft(scope, loaded.get(scope)!, 'immediate') - render() - expect(promptSkillRefs(promptEditor(screen.getByRole('textbox')).getJSON())).toEqual([ - { name: 'review', path: '/returned/SKILL.md' } - ]) - expect(nextSend()).toMatchObject({ - blocks: [ - { type: 'text', text: expect.stringContaining('@/remote/paste.txt') }, - { type: 'image-ref', path: '/new.png' } - ], - skills: [{ name: 'review', path: '/returned/SKILL.md' }] - }) -}) - // Joined replacement boundary: real edit owner, picker, mounted rich editor and wire serializer. const leaseRpc = vi.hoisted(() => vi.fn( diff --git a/src/renderer/src/components/sidebar/worktree-list/rows/section-header-create-host.test.tsx b/src/renderer/src/components/sidebar/worktree-list/rows/section-header-create-host.test.tsx index 810b4bbb9c8..f6c5a0d8cc7 100644 --- a/src/renderer/src/components/sidebar/worktree-list/rows/section-header-create-host.test.tsx +++ b/src/renderer/src/components/sidebar/worktree-list/rows/section-header-create-host.test.tsx @@ -163,48 +163,3 @@ it("leaves an empty twin header to the composer's focused host, not the registra expect(useAppStore.getState().activeModal).toBe('new-workspace-composer') expect(useAppStore.getState().modalData).not.toHaveProperty('initialWorkspaceTarget') }) - -it('arms a remote root header with its ordering anchor in a view without host headers', () => { - const remote = { ...serverTwin, id: 'remote-twin' } - useAppStore.setState({ repos: [localTwin, remote] }) - const ctx = headerContext() - ctx.headerDrag = { - ...ctx.headerDrag, - canReorderRepoHeaders: true, - rootProjectHeaderKeys: ['group:projects', 'repo:repo-twin'], - rootProjectHeaderCountByOccurrenceKey: new Map([ - ['["","group:projects"]', 2], - ['["","repo:repo-twin"]', 2] - ]), - sidebarRepoHeaderIdsByBucket: new Map([['ungrouped', ['repo-twin']]]), - repoHeaderIndexByRepoId: new Map([['repo-twin', 0]]), - repoHeaderBucketByRepoId: new Map([['repo-twin', 'ungrouped']]) - } - const view = render( - - {renderWorktreeSectionHeaderRow({ - ctx, - row: { - type: 'header', - key: 'remote-header', - count: 1, - icon: { kind: 'project' }, - repo: remote, - anchorRepo: localTwin, - hostId: 'runtime:home', - repeated: true - }, - vItem: { key: 'header', index: 0, start: 0, size: 35, end: 35, lane: 0 }, - isActiveStickyHeader: false, - hasStickyHost: false, - hasHeaderTopSpacing: false, - measureVirtualRowElement: vi.fn() - })} - - ) - const header = view.container.querySelector('[data-root-project-header-key]') - expect(header?.getAttribute('data-root-project-header-key')).toBe('repo:repo-twin') - expect(header?.getAttribute('data-repo-header-id')).toBe('repo-twin') - expect(header?.getAttribute('data-root-project-header-host')).toBe('runtime:home') - expect(header?.getAttribute('data-repo-header-drag-handle')).toBe('') -}) diff --git a/src/renderer/src/components/sidebar/worktree-list/viewport/header-folder-composition.test.tsx b/src/renderer/src/components/sidebar/worktree-list/viewport/header-folder-composition.test.tsx deleted file mode 100644 index f8f77fea67f..00000000000 --- a/src/renderer/src/components/sidebar/worktree-list/viewport/header-folder-composition.test.tsx +++ /dev/null @@ -1,293 +0,0 @@ -// @vitest-environment happy-dom -import { act, cleanup, fireEvent, render } from '@testing-library/react' -import { afterEach, expect, it, vi } from 'vitest' -import { useAppStore } from '@/store' -import { TooltipProvider } from '@/components/ui/tooltip' -import { - makeRepo, - makeFolderWorkspace -} from '../../../worktree-jump/worktree-jump-palette-test-fixtures' -import { createProjectGroup } from '../../../../../../shared/project-groups' -import type { Repo } from '../../../../../../shared/repo-types' -import type { GroupHeaderRow } from '../../../../../../shared/sidebar-list/row-types' -import { useFolderWorkspacePathStatusRows } from '../listing/use-folder-path-statuses' -import { useProjectHeaderHostContexts } from './use-project-header-host-contexts' -import { - renderWorktreeVirtualRow, - type WorktreeVirtualRowContext -} from '../rows/virtual-row-dispatch' -import type { SectionHeaderRowContext } from '../rows/section-header-row-context' -import { INITIAL_REPO_DRAG_STATE } from '../drag/project-header-drag-contract' -import { INITIAL_PROJECT_GROUP_DRAG_STATE } from '../drag/project-group-header-drag-contract' -import { environment, publish, ssh } from '../listing/host-status-test-fixtures' - -const initial = useAppStore.getInitialState() -const originalApi = Object.getOwnPropertyDescriptor(window, 'api') -afterEach(() => { - cleanup() - useAppStore.setState(initial, true) - if (originalApi) { - Object.defineProperty(window, 'api', originalApi) - } else { - Reflect.deleteProperty(window, 'api') - } -}) - -it('composes real dispatched headers, current repo gates and folder cache refresh without unrelated viewport work', async () => { - useAppStore.setState(initial, true) - const group = { - ...createProjectGroup({ - name: 'Repo', - parentPath: '/folders', - createdFrom: 'folder-scan', - tabOrder: 0, - now: 1 - }), - id: 'root', - executionHostId: 'local' - } - const nestedGroup = { ...group, id: 'nested', parentGroupId: 'root', parentPath: null } - const repo: Repo = { ...makeRepo(), executionHostId: 'runtime:A', connectionId: 'target-0' } - const nested: Repo = { - ...makeRepo(), - id: 'nested-repo', - path: '/elsewhere/repo', - projectGroupId: 'nested', - connectionId: 'nested-A', - executionHostId: 'local' - } - const folder = makeFolderWorkspace({ - projectGroupId: 'root', - folderPath: '/folders/workspace', - executionHostId: 'local' - }) - const projectGroups = [group, nestedGroup] - const folderWorkspaces = [folder] - const repoMap = new Map([ - [repo.id, repo], - [nested.id, nested] - ]) - useAppStore.setState({ - repos: [repo, nested], - projectGroups, - folderWorkspaces, - runtimeEnvironments: [environment('A'), environment('B')] - }) - publish('A', 1) - publish('B', 1) - useAppStore.getState().setEnvironmentSshTargetsMetadata('A', []) - useAppStore - .getState() - .setEnvironmentSshConnectionState('A', 'target-0', ssh('connected', 'target-0')) - let exists = false - const getPathStatus = vi.fn(async () => ({ - path: '/folders', - exists, - ...(exists ? {} : { reason: 'missing' as const }) - })) - // External filesystem boundary only; request, routing, cache and reader stay real. - Object.defineProperty(window, 'api', { - configurable: true, - value: { ...window.api, folderWorkspaces: { ...window.api?.folderWorkspaces, getPathStatus } } - }) - const fetch = vi.fn(useAppStore.getState().fetchFolderWorkspacePathStatus) - useAppStore.setState({ fetchFolderWorkspacePathStatus: fetch }) - const allRepoIds = [repo.id, nested.id] - const rows: GroupHeaderRow[] = [ - { type: 'header', key: 'repo', label: 'Repo', icon: { kind: 'project' }, count: 1, repo }, - { - type: 'header', - key: 'folder', - label: 'Repo', - icon: { kind: 'project' }, - count: 1, - projectGroup: group - } - ] - const hosts = [ - { - id: 'local' as const, - label: 'Local', - kind: 'local' as const, - detail: '', - health: 'available' as const - }, - { - id: 'runtime:A' as const, - label: 'A', - kind: 'runtime' as const, - detail: '', - health: 'available' as const - } - ] - const create = vi.fn() - const toggle = vi.fn() - const measure = vi.fn() - let viewportRenders = 0 - function Viewport({ rows }: { rows: GroupHeaderRow[] }) { - viewportRenders++ - const getCachedFolderWorkspacePathStatus = useFolderWorkspacePathStatusRows({ - allRepoIds, - repoMap, - projectGroups, - folderWorkspaces - }) - const projectHeaderHostContexts = useProjectHeaderHostContexts({ rows, hosts, repoMap }) - const header: SectionHeaderRowContext = { - groupBy: 'repo', - collapsedGroups: new Set(), - workspaceStatuses: [], - projectGroups, - highlightedRevealRowKey: null, - dragOverStatus: null, - pinDragOver: false, - projectHeaderHostContexts, - getCachedFolderWorkspacePathStatus, - toggleGroupWithScrollAnchor: toggle, - headerDrag: { - canReorderRepoHeaders: false, - canReorderProjectGroupHeaders: false, - orderedHostIds: [], - hostDrag: { - state: { draggingHostId: null, dropIndex: null, dropIndicatorY: null }, - onHandlePointerDown: vi.fn() - }, - repoDrag: { state: INITIAL_REPO_DRAG_STATE, onHandlePointerDown: vi.fn() }, - projectGroupDrag: { state: INITIAL_PROJECT_GROUP_DRAG_STATE, onHandlePointerDown: vi.fn() }, - sidebarRepoHeaderIdsByBucket: new Map(), - sidebarProjectGroupHeaderIdsByBucket: new Map(), - repoHeaderIndexByRepoId: new Map(), - repoHeaderBucketByRepoId: new Map(), - projectGroupHeaderIndexByGroupId: new Map(), - projectGroupHeaderBucketByGroupId: new Map(), - repoHeaderSectionEndByRepoId: new Map(), - projectGroupHeaderSectionEndByGroupId: new Map(), - rootProjectHeaderKeys: undefined, - rootProjectHeaderCountByOccurrenceKey: new Map(), - rootProjectHeaderSectionEndByKey: new Map() - }, - projectActions: { - getWorktreeVisibilityDefaults: () => undefined, - onOpenRepoSettings: vi.fn(), - onOpenWorktreeVisibility: vi.fn(), - onCreateGroupFromRepo: vi.fn(), - onMoveProjectToGroup: vi.fn(), - onRemoveProjectFromGroup: vi.fn(), - onRemoveProject: vi.fn(), - onCreateForRepo: create - }, - onRenameProjectGroup: vi.fn(), - onDeleteProjectGroup: vi.fn(), - onCreateFolderWorkspace: create, - onWorkspaceStatusDragOver: vi.fn(), - onWorkspaceStatusDragLeave: vi.fn(), - onWorkspacePinDragOver: vi.fn(), - onWorkspacePinDragLeave: vi.fn(), - onWorkspaceStatusDrop: vi.fn() - } - const ctx: WorktreeVirtualRowContext = { - renderRows: rows, - firstHeaderIndex: 0, - activeStickyHeaderIndexRef: { current: 0 }, - activeStickyHostIndexRef: { current: null }, - measureVirtualRowElement: measure, - header, - groupBy: header.groupBy, - workspaceStatuses: header.workspaceStatuses, - activeWorktreeId: null, - orderedHostIds: header.headerDrag.orderedHostIds, - hostDrag: header.headerDrag.hostDrag, - toggleGroupWithScrollAnchor: toggle, - importedWorktreeCardActionState: new Map(), - newExternalWorktreeInboxActionState: new Map(), - onShowImportedWorktrees: vi.fn(), - onKeepImportedWorktreesHidden: vi.fn(), - onOpenWorktreeVisibility: header.projectActions.onOpenWorktreeVisibility, - onOpenSuppressExternalWorktreeInbox: vi.fn(), - onWorkspaceStatusDragOver: header.onWorkspaceStatusDragOver, - onWorkspaceStatusDragLeave: header.onWorkspaceStatusDragLeave, - onWorkspaceStatusDrop: header.onWorkspaceStatusDrop, - get worktreeDragState(): WorktreeVirtualRowContext['worktreeDragState'] { - throw new Error('Header fixture cannot read worktree drag state') - }, - get item(): WorktreeVirtualRowContext['item'] { - throw new Error('Header fixture cannot render worktree items') - }, - get folderWorkspace(): WorktreeVirtualRowContext['folderWorkspace'] { - throw new Error('Header fixture cannot render folder workspace rows') - } - } - return ( - - {rows.map((row, index) => - renderWorktreeVirtualRow(ctx, row, { - key: row.key, - index, - start: index * 35, - size: 35, - end: (index + 1) * 35, - lane: 0 - }) - )} - - ) - } - const view = render() - await act(async () => {}) - expect(view.getByRole('button', { name: 'Repo — A' })).toBeTruthy() - expect(view.getByRole('button', { name: 'Repo — Local' })).toBeTruthy() - expect(fetch).toHaveBeenCalledTimes(2) - expect(fetch.mock.calls.map(([request, options]) => [request.scope, options])).toEqual([ - ['project-group', { force: true, runtimeEnvironmentId: null }], - ['folder-workspace', { force: true, runtimeEnvironmentId: null }] - ]) - const folderButton = () => view.getByRole('button', { name: 'Create workspace for Repo' }) - expect(folderButton().getAttribute('aria-disabled')).toBe('true') - const settled = viewportRenders - for (let index = 0; index < 10; index++) { - act(() => - useAppStore.setState({ - sshConnectionStates: new Map([['B', ssh(index % 2 ? 'connected' : 'disconnected', 'B')]]) - }) - ) - act(() => publish('B', index + 2)) - } - expect(viewportRenders).toBe(settled) - expect(fetch).toHaveBeenCalledTimes(2) - act(() => - useAppStore - .getState() - .setEnvironmentSshConnectionState('A', 'target-0', ssh('disconnected', 'target-0')) - ) - expect( - view - .getByRole('button', { name: 'Reconnect SSH target before creating workspaces for Repo' }) - .hasAttribute('disabled') - ).toBe(true) - expect(viewportRenders).toBe(settled) - exists = true - await act(async () => { - useAppStore.setState({ - sshConnectionStates: new Map([['nested-A', ssh('connected', 'nested-A')]]) - }) - }) - expect(fetch).toHaveBeenCalledTimes(4) - expect(getPathStatus).toHaveBeenCalledTimes(4) - expect(folderButton().getAttribute('aria-disabled')).toBe('false') - fireEvent.click(folderButton()) - expect(create).toHaveBeenCalledExactlyOnceWith(group) - expect(toggle).not.toHaveBeenCalled() - expect(measure).toHaveBeenCalled() - expect( - view.container - .querySelector('[data-worktree-virtual-row-key="repo"]') - ?.getAttribute('data-worktree-sticky-header-active') - ).toBe('') - // Reuse the virtual row key for another owner: the mounted JSX control reads its new resource. - view.rerender( - - ) - expect( - view.getByRole('button', { name: 'Create new worktree for Repo' }).hasAttribute('disabled') - ).toBe(false) -}) diff --git a/src/renderer/src/components/sidebar/worktree-list/viewport/worktree-header-section-boundaries.test.ts b/src/renderer/src/components/sidebar/worktree-list/viewport/worktree-header-section-boundaries.test.ts deleted file mode 100644 index 1b014b043ec..00000000000 --- a/src/renderer/src/components/sidebar/worktree-list/viewport/worktree-header-section-boundaries.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { expect, it } from 'vitest' -import type { RenderRow } from '../listing/render-row' -import { getRootProjectHeaderSectionEndByKey } from './worktree-header-section-boundaries' - -it('ends a root group before a removed project header and its pending creation', () => { - const rows: RenderRow[] = [ - { - type: 'header', - key: 'group:projects', - count: 1, - icon: { kind: 'project' }, - projectGroup: { - id: 'projects', - name: 'Projects', - parentPath: null, - parentGroupId: null, - createdFrom: 'manual', - tabOrder: 0, - isCollapsed: false, - color: null, - createdAt: 1, - updatedAt: 1 - } - }, - { - type: 'header', - key: 'repo:member', - count: 0, - icon: { kind: 'project' }, - repo: { id: 'member', path: '/member', displayName: 'Member', badgeColor: '', addedAt: 1 }, - projectGroupDepth: 1 - }, - { type: 'header', key: 'repo:removed', count: 0, icon: { kind: 'project' } }, - { type: 'pending-creation', key: 'creation', creationId: 'creation', repo: undefined } - ] - - expect([...getRootProjectHeaderSectionEndByKey(rows, 0).values()]).toEqual([60]) -}) From c66199ab941a729eb0792dac07b209020d54f653 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:26:57 +0000 Subject: [PATCH 02/28] feat(tab-group): make the split pane landing rows outline buttons (#40469) --- .../components/tab-group/SplitPaneLanding.tsx | 40 +++++++++------- .../tab-group/SplitPaneLandingAgentRow.tsx | 48 ++++++++----------- .../tab-group/split-pane-landing-row.tsx | 5 +- 3 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/renderer/src/components/tab-group/SplitPaneLanding.tsx b/src/renderer/src/components/tab-group/SplitPaneLanding.tsx index b8675b5aefe..407e6d55057 100644 --- a/src/renderer/src/components/tab-group/SplitPaneLanding.tsx +++ b/src/renderer/src/components/tab-group/SplitPaneLanding.tsx @@ -23,12 +23,9 @@ import { ensureSimulatorTab, getSimulatorTabForWorktree } from '@/lib/simulator/tabs/ensure-simulator-tab' +import { Button } from '@/components/ui/button' import { SplitPaneLandingAgentRow } from './SplitPaneLandingAgentRow' -import { - LANDING_ROW_CLASS, - LandingRowShortcut, - moveLandingRowFocus -} from './split-pane-landing-row' +import { LandingRowLabel, LandingRowShortcut, moveLandingRowFocus } from './split-pane-landing-row' /** Fills an empty split pane so the user picks what opens there; each action creates into this group. */ export function SplitPaneLanding({ @@ -142,12 +139,12 @@ export function SplitPaneLanding({ return (
-
+
{t('components.tabGroup.splitPaneLanding.heading', 'Open in this pane')}
moveLandingRowFocus(event, listRef.current)} > -
+
{onClosePane ? ( - + + {t('components.tabGroup.splitPaneLanding.closePane', 'Close pane')} + + ) : null}
@@ -201,10 +202,17 @@ type LandingRow = { function LandingRowButton({ row }: { row: LandingRow }): React.JSX.Element { const Icon = row.icon return ( - + ) } diff --git a/src/renderer/src/components/tab-group/SplitPaneLandingAgentRow.tsx b/src/renderer/src/components/tab-group/SplitPaneLandingAgentRow.tsx index 5f3be9f4759..5e6a95c40ed 100644 --- a/src/renderer/src/components/tab-group/SplitPaneLandingAgentRow.tsx +++ b/src/renderer/src/components/tab-group/SplitPaneLandingAgentRow.tsx @@ -2,7 +2,8 @@ import { Bot, ChevronDown } from 'lucide-react' import { useAppStore } from '../../store' import { useShortcutKeyDetails } from '@/hooks/useShortcutLabel' import { useTranslate } from '@/i18n/use-translate' -import { cn } from '@/lib/utils' +import { Button } from '@/components/ui/button' +import { ButtonGroup } from '@/components/ui/button-group' import { getAgentCatalog } from '@/lib/agent-catalog/agent-catalog' import { AgentIcon } from '@/components/agent/icons/AgentIcon' import { focusTerminalTabSurface } from '@/lib/focus-terminal-tab-surface' @@ -18,9 +19,9 @@ import { resolveTerminalAgentTabShortcut } from '../terminal-agent-tab-shortcut' import { launchAgentTabShortcut } from '../agent-tab-shortcut-launch' -import { LANDING_ROW_CLASS, LandingRowShortcut } from './split-pane-landing-row' +import { LandingRowLabel, LandingRowShortcut } from './split-pane-landing-row' -/** The default agent as one row; the chevron opens the + menu's agent list for any other. */ +/** The default agent as a split button; the chevron opens the + menu's agent list for any other. */ export function SplitPaneLandingAgentRow({ worktreeId, groupId, @@ -59,36 +60,32 @@ export function SplitPaneLandingAgentRow({ } return ( - // Why: a stretched button keeps the row one click target while the chevron stays its own button. -
- - + + - + - - - -
+ ) } diff --git a/src/renderer/src/components/tab-group/split-pane-landing-row.tsx b/src/renderer/src/components/tab-group/split-pane-landing-row.tsx index ab31be8e758..63b53e93b04 100644 --- a/src/renderer/src/components/tab-group/split-pane-landing-row.tsx +++ b/src/renderer/src/components/tab-group/split-pane-landing-row.tsx @@ -1,8 +1,9 @@ import type { ShortcutKeyComboDetails } from '@/hooks/useShortcutLabel' import { ShortcutKeyCombo } from '../ShortcutKeyCombo' -export const LANDING_ROW_CLASS = - 'flex h-9 w-full items-center gap-3 rounded-md px-2.5 text-sm text-foreground transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground focus-visible:outline-none' +export function LandingRowLabel({ label }: { label: string }): React.JSX.Element { + return {label} +} export function LandingRowShortcut({ shortcut From b602376e8b3bb36a9e6af519e01ef4650ca0dffc Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:31:52 -0800 Subject: [PATCH 03/28] Show AskUserQuestion option previews on desktop and phone (#40109) * Show AskUserQuestion option previews on desktop and phone Claude can attach a preview (a mockup or snippet) to each option of a single-select question. Orca dropped it, so users chose between bare labels. Options now carry the preview through the journal, terminal asks and the phone, and the question card shows the selected option's preview as plain monospace text. Questions with previews confirm explicitly instead of auto-advancing, so the preview stays readable. * Keep phone previews bounded and digit focus on the named option Phone previews get a capped height with their own vertical scroll, and opening one never scrolls past its top. A digit always focuses its row before the chosen-row check, so Enter confirms the option the user named. Tighter preview line height, a stable phone row root, and a persistence test whose expected previews come from the fixed input. * Narrow the reopened question with a type guard instead of a cast --- .../native-chat/input/MobileNativeChatAsk.tsx | 21 ++-- .../input/MobileNativeChatAskOptionRow.tsx | 48 +++++++++- .../use-mobile-native-chat-reply-scroll.ts | 37 ++++++- .../claude-structured-prompt-items.test.ts | 47 +++++++++ .../journal/claude-structured-prompt-items.ts | 7 +- .../journal-prompt-body-bounds.ts | 6 +- ...journal-prompt-preview-persistence.test.ts | 96 +++++++++++++++++++ .../NativeChatQuestionCard.fixture.tsx | 41 ++++++++ .../prompts/NativeChatQuestionCard.test.tsx | 40 ++++++++ .../prompts/NativeChatQuestionCard.tsx | 25 ++++- .../prompts/NativeChatQuestionOption.tsx | 87 ++++++++++------- .../prompts/use-native-chat-question-keys.ts | 6 +- .../journal/agent-session-journal-schemas.ts | 3 +- .../journal/agent-session-journal-types.ts | 2 + .../agent-status-field-normalization.test.ts | 49 ++++++++++ .../agent-status-field-normalization.ts | 20 +++- .../input/native-chat-ask-card-rules.ts | 15 ++- .../input/native-chat-ask-dismiss-key.ts | 3 +- .../input/native-chat-ask-option-preview.ts | 11 +++ .../input/native-chat-ask-types.ts | 2 +- .../native-chat/input/native-chat-ask.ts | 29 +++--- src/shared/structured-agent-question-card.ts | 3 +- 22 files changed, 514 insertions(+), 84 deletions(-) create mode 100644 src/main/claude/structured/journal/claude-structured-prompt-items.test.ts create mode 100644 src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-preview-persistence.test.ts create mode 100644 src/shared/agent-status/contracts/agent-status-field-normalization.test.ts create mode 100644 src/shared/native-chat/input/native-chat-ask-option-preview.ts diff --git a/mobile/src/session/native-chat/input/MobileNativeChatAsk.tsx b/mobile/src/session/native-chat/input/MobileNativeChatAsk.tsx index d201f87b06d..2ad2426c74b 100644 --- a/mobile/src/session/native-chat/input/MobileNativeChatAsk.tsx +++ b/mobile/src/session/native-chat/input/MobileNativeChatAsk.tsx @@ -12,6 +12,7 @@ import type { AgentJournalFreeTextInput } from '../../../../../src/shared/agent- import { askAcceptsEmptyAnswer, askConfirmAction, + askQuestionShowsPreviews, askConfirmLabel, askTypedText, toggleAskOption @@ -65,7 +66,8 @@ export function MobileNativeChatAsk({ }: Props): React.JSX.Element { useMobileColorScheme() const t = useMobileTranslation() - const { scrollRef, inputRef, revealReply, onViewportLayout } = useMobileNativeChatReplyScroll() + const { scrollRef, inputRef, revealReply, scrollViewProps, revealContent } = + useMobileNativeChatReplyScroll() const [index, setIndex] = useState(0) const [selections, setSelections] = useState(() => prompt.questions.map(() => [])) const [otherText, setOtherText] = useState(() => @@ -109,11 +111,15 @@ export function MobileNativeChatAsk({ return } const cur = selections[qi] ?? [] + // A pick reveals its preview, so a re-tap keeps it and the user confirms. + const keep = optIndex !== OTHER && askQuestionShowsPreviews(prompt.questions[qi]) const picked = !cur.includes(optIndex) - const next = selections.map((s, i) => (i === qi ? toggleAskOption(cur, optIndex, multi) : s)) + const next = selections.map((s, i) => + i === qi ? toggleAskOption(cur, optIndex, multi, keep) : s + ) setSelections(next) autoAdvance.cancel() - if (picked && !multi && optIndex !== OTHER) { + if (picked && !multi && optIndex !== OTHER && !keep) { autoAdvance.schedule(() => void advance(next)) } } @@ -246,12 +252,7 @@ export function MobileNativeChatAsk({ ) : null} - + void selected: boolean multi?: boolean disabled: boolean onPress: () => void }): React.JSX.Element { useMobileColorScheme() - return ( + const row = ( ) + // One root shape whether or not the preview shows, so selecting never remounts the row. + return ( + <> + {row} + {preview && selected ? ( + { + const { y, height } = event.nativeEvent.layout + onPreviewLayout?.(y, y + height) + }} + > + + {/* Mockups are drawn in columns, so they scroll sideways instead of wrapping at phone width. */} + + + {preview} + + + + + ) : null} + + ) } const styles = themedStyleSheet(() => ({ @@ -106,5 +136,19 @@ const styles = themedStyleSheet(() => ({ optionDescription: { color: colors.textSecondary, fontSize: typography.metaSize + }, + preview: { + backgroundColor: colors.bgPanel, + borderWidth: 1, + borderColor: colors.borderSubtle, + borderRadius: concentricRadius(radii.surface, spacing.xs), + padding: spacing.sm, + marginBottom: spacing.xs, + maxHeight: 220 + }, + previewText: { + color: colors.textPrimary, + fontFamily: typography.monoFamily, + fontSize: typography.metaSize } })) diff --git a/mobile/src/session/native-chat/input/use-mobile-native-chat-reply-scroll.ts b/mobile/src/session/native-chat/input/use-mobile-native-chat-reply-scroll.ts index fe9fd376050..9226a6f7f2f 100644 --- a/mobile/src/session/native-chat/input/use-mobile-native-chat-reply-scroll.ts +++ b/mobile/src/session/native-chat/input/use-mobile-native-chat-reply-scroll.ts @@ -1,11 +1,18 @@ import { useRef } from 'react' -import type { LayoutChangeEvent, ScrollView, TextInput } from 'react-native' +import type { + LayoutChangeEvent, + NativeScrollEvent, + NativeSyntheticEvent, + ScrollView, + TextInput +} from 'react-native' // Keep a focused reply in view when its card shrinks above the keyboard. export function useMobileNativeChatReplyScroll() { const scrollRef = useRef(null) const inputRef = useRef(null) const viewportHeight = useRef(0) + const scrollOffset = useRef(0) const revealReply = (): void => { if (inputRef.current?.isFocused()) { scrollRef.current?.scrollToEnd({ animated: false }) @@ -20,5 +27,31 @@ export function useMobileNativeChatReplyScroll() { revealReply() } } - return { scrollRef, inputRef, revealReply, onViewportLayout } + const onScroll = (event: NativeSyntheticEvent): void => { + scrollOffset.current = event.nativeEvent.contentOffset.y + } + // Scroll just enough to show content that opened below the visible edge, never past its top. + const revealContent = (top: number, bottom: number): void => { + const hidden = bottom - (scrollOffset.current + viewportHeight.current) + if (viewportHeight.current > 0 && hidden > 0) { + scrollRef.current?.scrollTo({ + y: Math.min(scrollOffset.current + hidden, top), + animated: true + }) + } + } + const scrollViewProps = { + ref: scrollRef, + onLayout: onViewportLayout, + onScroll, + scrollEventThrottle: 16 + } + return { + scrollRef, + inputRef, + revealReply, + onViewportLayout, + scrollViewProps, + revealContent + } } diff --git a/src/main/claude/structured/journal/claude-structured-prompt-items.test.ts b/src/main/claude/structured/journal/claude-structured-prompt-items.test.ts new file mode 100644 index 00000000000..0be0dd589c7 --- /dev/null +++ b/src/main/claude/structured/journal/claude-structured-prompt-items.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it, vi } from 'vitest' +import { claudeQuestionItems } from './claude-structured-prompt-items' + +const LAYOUT_A = '┌──────┬────────┐\n│ nav │ main │\n└──────┴────────┘' + +describe('claudeQuestionItems', () => { + it('keeps option previews on single-select questions only', () => { + const [item] = claudeQuestionItems({ + sessionId: 'session-1', + prompt: { + requestId: 'req-1', + promptKey: 'req-1', + toolUseId: 'toolu-1', + toolName: 'AskUserQuestion', + kind: 'question', + input: { + questions: [ + { + question: 'Which layout?', + header: 'Layout', + multiSelect: false, + options: [ + { label: 'Sidebar', description: 'Nav on the left', preview: LAYOUT_A }, + { label: 'Top bar', description: 'Nav on top', preview: ' \n ' } + ] + }, + { + question: 'Which panes?', + header: 'Panes', + multiSelect: true, + options: [ + { label: 'Files', description: 'File tree', preview: 'files' }, + { label: 'Git', description: 'Changes', preview: 'git' } + ] + } + ] + }, + suggestions: [], + questionIds: ['q1', 'q2'], + settle: vi.fn() + } + }) + const questions = item!.body.questions! + expect(questions[0]!.options.map((option) => option.preview)).toEqual([LAYOUT_A, undefined]) + expect(questions[1]!.options.map((option) => option.preview)).toEqual([undefined, undefined]) + }) +}) diff --git a/src/main/claude/structured/journal/claude-structured-prompt-items.ts b/src/main/claude/structured/journal/claude-structured-prompt-items.ts index ef669549d3b..adefb290d77 100644 --- a/src/main/claude/structured/journal/claude-structured-prompt-items.ts +++ b/src/main/claude/structured/journal/claude-structured-prompt-items.ts @@ -10,6 +10,7 @@ import { truncateToolDetail } from '../../../../shared/native-chat/tools/native-chat-tool-summary' import { isPlanApprovalSubject } from '../../../../shared/agent-session/agent-session-approval-subject' +import { askOptionPreview } from '../../../../shared/native-chat/input/native-chat-ask-option-preview' import { boundJournalPromptBody } from '../../../native-chat/agent-session-journal/item-lifecycle/journal-prompt-body-bounds' import { claudeRecord, claudeText } from './claude-structured-item-translation' import { encodeClaudeQuestionOptionId } from './claude-structured-prompt-replies' @@ -77,16 +78,20 @@ function questionOptions( if (!Array.isArray(question.options)) { return [] } + // Claude draws previews only on single-select questions. + const singleSelect = question.multiSelect !== true return question.options.flatMap((value, index) => { const option = claudeRecord(value) const label = claudeText(option?.label) const description = claudeText(option?.description) + const preview = singleSelect ? askOptionPreview(option) : null return label ? [ { id: encodeClaudeQuestionOptionId(questionAddress, `choice-${index + 1}`), label, - ...(description ? { description } : {}) + ...(description ? { description } : {}), + ...(preview ? { preview } : {}) } ] : [] diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-body-bounds.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-body-bounds.ts index 34945214586..d8114bb097d 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-body-bounds.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-body-bounds.ts @@ -13,6 +13,7 @@ export const MAX_JOURNAL_PROMPT_OPTIONS = 64 export const MAX_JOURNAL_GROUPED_PROMPT_QUESTIONS = 4 const JOURNAL_PROMPT_OPTION_LIMITS = { inlineHeadBytes: 1024 } +const JOURNAL_PROMPT_OPTION_PREVIEW_LIMITS = { inlineHeadBytes: 8 * 1024 } // Agent prompt content permits larger bounds than client replies but must still fit settlement batches. const JOURNAL_PROMPT_TEXT_LIMITS = { inlineHeadBytes: 128 * 1024 } const JOURNAL_PROMPT_ID_MAX_BYTES = 1024 @@ -133,7 +134,10 @@ function boundPromptOptions( label: boundInlineText(option.label, JOURNAL_PROMPT_OPTION_LIMITS).text, ...(option.description === undefined ? {} - : { description: boundInlineText(option.description, JOURNAL_PROMPT_OPTION_LIMITS).text }) + : { description: boundInlineText(option.description, JOURNAL_PROMPT_OPTION_LIMITS).text }), + ...(option.preview === undefined + ? {} + : { preview: boundInlineText(option.preview, JOURNAL_PROMPT_OPTION_PREVIEW_LIMITS).text }) })) } diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-preview-persistence.test.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-preview-persistence.test.ts new file mode 100644 index 00000000000..e4f2a12f4f8 --- /dev/null +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-prompt-preview-persistence.test.ts @@ -0,0 +1,96 @@ +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, expect, it } from 'vitest' +import { + AGENT_JOURNAL_THREAD_SCOPE, + type AgentJournalQuestionItem +} from '../../../../shared/agent-session/journal/agent-session-journal-types' +import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' +import { openAgentSessionJournal } from '../journal-store-factory' +import { + closeTestJournalHostDatabases, + openTestJournalHostDatabase +} from '../store-lifecycle/journal-host-database-test-support' +import { boundJournalPromptBody, cancelledJournalPromptBody } from './journal-prompt-body-bounds' + +let root: string | undefined + +afterEach(async () => { + await closeTestJournalHostDatabases() + if (root) { + await rm(root, { recursive: true, force: true }) + } +}) + +// Control characters escape to six bytes each, so this question only fits a journal row on its own. +const CONTROL = '\u0001' +// Each preview fits the 8 KiB bound, so it must come back exactly as written. +const PREVIEWS = [1, 2, 3, 4].map((q) => + [1, 2, 3, 4].map((o) => `layout ${q}.${o}${CONTROL.repeat(8_192 - 12)}`) +) + +function largestQuestion(): AgentJournalQuestionItem { + const questions = [1, 2, 3, 4].map((q) => ({ + id: `q${q}`, + question: `Question ${q}${CONTROL.repeat(30_000)}`, + multiSelect: false, + options: [1, 2, 3, 4].map((o) => ({ + id: `q${q}:choice-${o}`, + label: `Layout ${o}`, + preview: PREVIEWS[q - 1]![o - 1]! + })) + })) + return boundJournalPromptBody({ + kind: 'question', + question: '4 grouped questions from Claude', + options: [], + questions, + resolution: { state: 'pending', selectedOptionId: null, resolvedBy: null, resolvedAt: null } + }) +} + +it('saves, cancels and reopens a question whose previews make it larger than one batch', async () => { + root = await mkdtemp(join(tmpdir(), 'orca-prompt-previews-')) + const open = () => + openAgentSessionJournal({ + identity: { + sessionId: 'session-previews', + workspaceId: 'workspace-1', + hostId: 'local', + agent: 'codex', + providerHandle: codexProviderHandle('thread-1') + }, + database: openTestJournalHostDatabase(root!), + now: () => 1_000 + }) + const identity = { provider: 'orca' as const, clientMessageId: 'claude-prompt:session:req-1' } + const body = largestQuestion() + const journal = await open() + await journal.appendItem(identity, body, { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }) + await journal.appendLifecycleBatch({ + settlementId: 'cancel-prompt', + fence: 1, + mutations: [ + { + kind: 'item', + identity, + body: cancelledJournalPromptBody(body)!, + turnScope: AGENT_JOURNAL_THREAD_SCOPE + } + ] + }) + await journal.close() + + const reopened = await open() + const saved = reopened + .snapshot() + .items.map((item) => item.body) + .find((item): item is AgentJournalQuestionItem => item.kind === 'question') + await reopened.close() + expect(saved).toMatchObject({ kind: 'question', resolution: { state: 'cancelled' } }) + const previews = saved!.questions!.flatMap((question) => + question.options.map((option) => option.preview) + ) + expect(previews).toEqual(PREVIEWS.flat()) +}) diff --git a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.fixture.tsx b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.fixture.tsx index df35dd0e807..5e68681bfc9 100644 --- a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.fixture.tsx +++ b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.fixture.tsx @@ -231,3 +231,44 @@ export const historyLinks = defineFixture({ ), interact: (frame) => frame.querySelector('button[aria-expanded]')?.click() }) + +export const previews = defineFixture({ + width: 560, + render: () => ( +
+ {}} + onCancel={() => {}} + /> +
+ ), + interact: async (frame) => { + frame.querySelectorAll('button[aria-pressed]')[0]?.click() + await new Promise(requestAnimationFrame) + } +}) diff --git a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.test.tsx b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.test.tsx index cf50c7ec441..1e89b19d5aa 100644 --- a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.test.tsx +++ b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.test.tsx @@ -86,4 +86,44 @@ describe('NativeChatQuestionCard', () => { expect(pressAt(10, 10, 'ArrowDown')).toBe(true) expect(document.activeElement).toBe(rows[0]) }) + + it('shows a picked preview, keeps it on a second click, and confirms the digit-named option with Enter', () => { + vi.useFakeTimers() + container.setAttribute('data-native-chat-root', 'true') + const onAnswer = vi.fn() + act(() => { + root.render( + {}} + shouldFocus + /> + ) + }) + const rows = container.querySelectorAll('button[aria-pressed]') + act(() => rows[0]!.focus()) + pressKey({ key: '2' }, rows[0]!) + act(() => rows[1]!.click()) + act(() => vi.runAllTimers()) + expect(container.querySelector('pre')?.textContent).toBe('| nav |\n| main |') + expect(document.activeElement).toBe(rows[1]) + expect(onAnswer).not.toHaveBeenCalled() + pressKey({ key: 'ArrowUp' }, rows[1]!) + pressKey({ key: '2' }, rows[0]!) + expect(document.activeElement).toBe(rows[1]) + pressKey({ key: 'Enter' }, rows[1]!) + expect(onAnswer).toHaveBeenCalledWith([{ indices: [1], other: '' }]) + }) }) diff --git a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.tsx b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.tsx index 91986107d6e..0f2e27541f1 100644 --- a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.tsx +++ b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionCard.tsx @@ -18,6 +18,7 @@ import type { AgentJournalFreeTextInput } from '../../../../../shared/agent-sess import { askAcceptsEmptyAnswer, askConfirmAction, + askQuestionShowsPreviews, askConfirmLabel, askTypedText, toggleAskOption @@ -82,6 +83,7 @@ export function NativeChatQuestionCard({ const questionAllowsOther = Array.isArray(allowOther) ? (allowOther[index] ?? false) : allowOther const freeTextInput = freeTextInputs?.[index] const fieldTakesFocus = q.options.length === 0 || undefined + const showsPreviews = askQuestionShowsPreviews(q) const answerPlaceholder = freeTextInput?.placeholder ?? t('components.native-chat.question.otherPlaceholder', 'Type your answer') @@ -156,15 +158,31 @@ export function NativeChatQuestionCard({ } } - // Briefly show a single-select pick before advancing; picking again retracts it. - const pickOption = (optionIndex: number): void => { + // Briefly show a single-select pick before advancing; picking again retracts it. With previews, + // a pick stays put: a digit focuses its row, and Enter on the chosen row confirms. + const pickOption = ( + optionIndex: number, + via: 'pointer' | 'digit' | 'enter' = 'pointer' + ): void => { const cur = selections[index] ?? [] + // Focus first, so Enter after any digit acts on the row that digit named. + if (showsPreviews && via === 'digit') { + cardRef.current + ?.querySelectorAll('button[aria-pressed]') + [optionIndex]?.focus() + } + if (showsPreviews && cur.includes(optionIndex)) { + if (via === 'enter') { + confirm(true) + } + return + } const picked = !cur.includes(optionIndex) const chosen = toggleAskOption(cur, optionIndex, q.multiSelect) const next = selections.map((s, i) => (i === index ? chosen : s)) setSelections(next) autoAdvance.cancel() - if (picked && !q.multiSelect) { + if (picked && !q.multiSelect && !showsPreviews) { autoAdvance.schedule(() => advanceOrSubmit(next, otherText)) } } @@ -298,6 +316,7 @@ export function NativeChatQuestionCard({ initialFocus={i === 0} label={opt.label} description={opt.description} + preview={opt.preview} selected={(selections[index] ?? []).includes(i)} disabled={isSubmitting} onSelect={() => pickOption(i)} diff --git a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionOption.tsx b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionOption.tsx index 9887464590f..ef575dc5ba4 100644 --- a/src/renderer/src/components/native-chat/prompts/NativeChatQuestionOption.tsx +++ b/src/renderer/src/components/native-chat/prompts/NativeChatQuestionOption.tsx @@ -1,11 +1,17 @@ import { Check } from 'lucide-react' import { cn } from '@/lib/utils' +// Stable, so it runs when the preview opens rather than on every render; the option list is height-capped. +function revealPreview(element: HTMLPreElement | null): void { + element?.scrollIntoView?.({ block: 'nearest' }) +} + export function NativeChatQuestionOption({ badge, initialFocus, label, description, + preview, selected, disabled, onSelect @@ -14,47 +20,60 @@ export function NativeChatQuestionOption({ initialFocus: boolean label: string description?: string + /** Shown under the row while it is selected, as the agent wrote it. */ + preview?: string selected: boolean disabled: boolean onSelect: () => void }): React.JSX.Element { return ( - + + {selected ? : badge} + + + {label} + {description ? ( + + {description} + + ) : null} + + + {preview && selected ? ( +
+          {preview}
+        
+ ) : null} +
) } diff --git a/src/renderer/src/components/native-chat/prompts/use-native-chat-question-keys.ts b/src/renderer/src/components/native-chat/prompts/use-native-chat-question-keys.ts index e0696c342d2..39f41cdb290 100644 --- a/src/renderer/src/components/native-chat/prompts/use-native-chat-question-keys.ts +++ b/src/renderer/src/components/native-chat/prompts/use-native-chat-question-keys.ts @@ -7,7 +7,7 @@ export function useNativeChatQuestionKeys( cardRef: RefObject, enabled: boolean, optionCount: number, - onPick: (optionIndex: number) => void, + onPick: (optionIndex: number, via: 'digit' | 'enter') => void, onStep: (delta: 1 | -1) => boolean ): void { const onKeyDown = useEffectEvent((event: KeyboardEvent): void => { @@ -48,7 +48,7 @@ export function useNativeChatQuestionKeys( return } event.preventDefault() - onPick(optionIndex) + onPick(optionIndex, 'digit') } else if (isArrow) { const up = event.key === 'ArrowUp' if ( @@ -82,7 +82,7 @@ export function useNativeChatQuestionKeys( } } else if (event.key === 'Enter' && focusedIndex !== -1) { event.preventDefault() - onPick(focusedIndex) + onPick(focusedIndex, 'enter') } }) useEffect(() => { diff --git a/src/shared/agent-session/journal/agent-session-journal-schemas.ts b/src/shared/agent-session/journal/agent-session-journal-schemas.ts index 0538c3616b3..d8d5be4abfd 100644 --- a/src/shared/agent-session/journal/agent-session-journal-schemas.ts +++ b/src/shared/agent-session/journal/agent-session-journal-schemas.ts @@ -116,7 +116,8 @@ const Block = openDiscriminatedUnion( const PromptOption = z.object({ id: z.string(), label: z.string(), - description: z.string().optional() + description: z.string().optional(), + preview: z.string().optional() }) const FreeTextInput = z.object({ diff --git a/src/shared/agent-session/journal/agent-session-journal-types.ts b/src/shared/agent-session/journal/agent-session-journal-types.ts index 6ee590b3aec..5b9658505a3 100644 --- a/src/shared/agent-session/journal/agent-session-journal-types.ts +++ b/src/shared/agent-session/journal/agent-session-journal-types.ts @@ -137,6 +137,8 @@ export type AgentJournalPromptOption = { id: string label: string description?: string + /** A block the user compares before choosing (a mockup or snippet), shown as plain monospace text. */ + preview?: string } export type AgentJournalFreeTextInput = { diff --git a/src/shared/agent-status/contracts/agent-status-field-normalization.test.ts b/src/shared/agent-status/contracts/agent-status-field-normalization.test.ts new file mode 100644 index 00000000000..1f08034e25c --- /dev/null +++ b/src/shared/agent-status/contracts/agent-status-field-normalization.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from 'vitest' +import { normalizeInteractivePromptField } from './agent-status-field-normalization' +import { parseAskFromStatus } from '../../native-chat/input/native-chat-ask' + +function layoutQuestion(preview: (index: number) => string): string { + return JSON.stringify({ + questions: [ + { + question: 'Which layout?', + header: 'Layout', + multiSelect: false, + options: ['Sidebar', 'Top bar', 'Split', 'Tabs'].map((label, index) => ({ + label, + description: `Layout ${index + 1}`, + preview: preview(index) + })) + } + ] + }) +} + +describe('normalizeInteractivePromptField', () => { + it('shortens oversize previews and keeps every option and the preview layout', () => { + const fitted = normalizeInteractivePromptField( + layoutQuestion((index) => `+--${index}--+\n`.repeat(600)), + 4000 + ) + const ask = parseAskFromStatus(fitted, 'AskUserQuestion') + const question = ask!.questions[0]! + expect(question.options.map((option) => option.label)).toEqual([ + 'Sidebar', + 'Top bar', + 'Split', + 'Tabs' + ]) + expect(question.hasPreviews).toBe(true) + expect(question.options.every((option) => option.preview?.endsWith('…'))).toBe(true) + }) + + it('never turns blank previews into previews', () => { + const fitted = normalizeInteractivePromptField( + layoutQuestion(() => ' '.repeat(1200)), + 4000 + ) + const question = parseAskFromStatus(fitted, 'AskUserQuestion')!.questions[0]! + expect(question.options).toHaveLength(4) + expect(question.hasPreviews).toBeUndefined() + }) +}) diff --git a/src/shared/agent-status/contracts/agent-status-field-normalization.ts b/src/shared/agent-status/contracts/agent-status-field-normalization.ts index 984658fcbcc..06a5982f306 100644 --- a/src/shared/agent-status/contracts/agent-status-field-normalization.ts +++ b/src/shared/agent-status/contracts/agent-status-field-normalization.ts @@ -154,7 +154,7 @@ function isEcmaTrimWhitespace(code: number): boolean { ) } -// Only provided strings update optional fields; absence must not erase cached state. Preserve option labels and shorten presentation fields only. +// Only provided strings update optional fields; absence must not erase cached state. Preserve option labels and shorten presentation fields (and previews) only. const COMPACTABLE_INTERACTIVE_PROMPT_FIELDS = new Set([ 'description', 'detail', @@ -163,6 +163,14 @@ const COMPACTABLE_INTERACTIVE_PROMPT_FIELDS = new Set([ 'summary' ]) +// A cut preview stays non-blank: whether Claude drew its preview layout decides the answer keystrokes. +function compactPreview(preview: string, maxLength: number): string { + if (preview.length <= maxLength || !/\S/.test(preview)) { + return truncatePreservingSurrogates(preview, maxLength) + } + return `${truncatePreservingSurrogates(preview, Math.max(maxLength - 1, 0))}…` +} + function compactInteractivePromptValue(value: unknown, maxStringLength: number): unknown { if (Array.isArray(value)) { return value.map((item) => compactInteractivePromptValue(item, maxStringLength)) @@ -173,9 +181,13 @@ function compactInteractivePromptValue(value: unknown, maxStringLength: number): return Object.fromEntries( Object.entries(value).map(([key, item]) => [ key, - typeof item === 'string' && COMPACTABLE_INTERACTIVE_PROMPT_FIELDS.has(key) - ? truncatePreservingSurrogates(item, maxStringLength) - : compactInteractivePromptValue(item, maxStringLength) + typeof item !== 'string' + ? compactInteractivePromptValue(item, maxStringLength) + : key === 'preview' + ? compactPreview(item, maxStringLength) + : COMPACTABLE_INTERACTIVE_PROMPT_FIELDS.has(key) + ? truncatePreservingSurrogates(item, maxStringLength) + : item ]) ) } diff --git a/src/shared/native-chat/input/native-chat-ask-card-rules.ts b/src/shared/native-chat/input/native-chat-ask-card-rules.ts index 98dab1c2ff8..397cebca85c 100644 --- a/src/shared/native-chat/input/native-chat-ask-card-rules.ts +++ b/src/shared/native-chat/input/native-chat-ask-card-rules.ts @@ -3,14 +3,23 @@ import type { AskQuestion } from './native-chat-ask-types' // How a stepped question card picks, labels and confirms; desktop and mobile draw it differently. -/** Multi-select keeps picks in option order so the answer reads as the options do. */ +/** A single-select whose pick reveals a preview: picking only selects, and the user confirms. */ +export function askQuestionShowsPreviews( + question: { multiSelect: boolean; options: readonly { preview?: string }[] } | undefined +): boolean { + return question !== undefined && !question.multiSelect && question.options.some((o) => o.preview) +} + +/** Multi-select keeps picks in option order so the answer reads as the options do. `keepChosen`: + * picking the chosen option again keeps it, as a preview question's pick does. */ export function toggleAskOption( picked: readonly number[], optionIndex: number, - multiSelect: boolean + multiSelect: boolean, + keepChosen = false ): number[] { if (picked.includes(optionIndex)) { - return picked.filter((index) => index !== optionIndex) + return keepChosen ? [...picked] : picked.filter((index) => index !== optionIndex) } return multiSelect ? [...picked, optionIndex].sort((a, b) => a - b) : [optionIndex] } diff --git a/src/shared/native-chat/input/native-chat-ask-dismiss-key.ts b/src/shared/native-chat/input/native-chat-ask-dismiss-key.ts index 27be080a71a..6e2e3261c47 100644 --- a/src/shared/native-chat/input/native-chat-ask-dismiss-key.ts +++ b/src/shared/native-chat/input/native-chat-ask-dismiss-key.ts @@ -19,7 +19,8 @@ export function nativeChatAskDismissKey(prompt: AskPrompt | null): string | null ...question, question: askDismissKeyText(question.question), header: askDismissKeyText(question.header), - options: question.options.map((option) => ({ + // Status and transcript copies shorten previews differently; labels already identify the option. + options: question.options.map(({ preview: _preview, ...option }) => ({ ...option, description: askDismissKeyText(option.description) })) diff --git a/src/shared/native-chat/input/native-chat-ask-option-preview.ts b/src/shared/native-chat/input/native-chat-ask-option-preview.ts new file mode 100644 index 00000000000..f33053c6d31 --- /dev/null +++ b/src/shared/native-chat/input/native-chat-ask-option-preview.ts @@ -0,0 +1,11 @@ +/** The option's preview as Claude shows it: a non-blank string, kept verbatim. */ +export function askOptionPreview(option: unknown): string | null { + return option && + typeof option === 'object' && + 'preview' in option && + typeof option.preview === 'string' && + // Why not trim(): history rescans run this per option, and trim copies the whole preview. + /\S/.test(option.preview) + ? option.preview + : null +} diff --git a/src/shared/native-chat/input/native-chat-ask-types.ts b/src/shared/native-chat/input/native-chat-ask-types.ts index 66db1b23082..b978c6a0e43 100644 --- a/src/shared/native-chat/input/native-chat-ask-types.ts +++ b/src/shared/native-chat/input/native-chat-ask-types.ts @@ -1,4 +1,4 @@ -export type AskOption = { label: string; description?: string } +export type AskOption = { label: string; description?: string; preview?: string } export type AskQuestion = { question: string header?: string diff --git a/src/shared/native-chat/input/native-chat-ask.ts b/src/shared/native-chat/input/native-chat-ask.ts index a402c3b0236..a76ee2cf177 100644 --- a/src/shared/native-chat/input/native-chat-ask.ts +++ b/src/shared/native-chat/input/native-chat-ask.ts @@ -1,4 +1,5 @@ import { nativeChatAskScanOrder } from './native-chat-ask-order' +import { askOptionPreview } from './native-chat-ask-option-preview' import type { AskOption, AskPrompt, @@ -39,12 +40,12 @@ function parseCanonicalQuestionsInput(input: unknown): AskPrompt | null { } const text = 'question' in raw && typeof raw.question === 'string' ? raw.question : '' const rawOptions = 'options' in raw ? raw.options : undefined - const options = parseOptions(rawOptions) + const multiSelect = + 'multiSelect' in raw + ? raw.multiSelect === true + : ('multiple' in raw && raw.multiple === true) || ('multi' in raw && raw.multi === true) + const options = parseOptions(rawOptions, multiSelect) if (text || options.length > 0) { - const multiSelect = - 'multiSelect' in raw - ? raw.multiSelect === true - : ('multiple' in raw && raw.multiple === true) || ('multi' in raw && raw.multi === true) questions.push({ question: text, header: 'header' in raw && typeof raw.header === 'string' ? raw.header : undefined, @@ -59,20 +60,10 @@ function parseCanonicalQuestionsInput(input: unknown): AskPrompt | null { /** Claude only switches a single-select to its preview layout for a non-blank preview. */ export function askOptionsHavePreview(rawOptions: unknown): boolean { - return ( - Array.isArray(rawOptions) && - rawOptions.some( - (option) => - option && - typeof option === 'object' && - 'preview' in option && - typeof option.preview === 'string' && - option.preview.trim().length > 0 - ) - ) + return Array.isArray(rawOptions) && rawOptions.some((option) => askOptionPreview(option) !== null) } -function parseOptions(raw: unknown): AskOption[] { +function parseOptions(raw: unknown, multiSelect: boolean): AskOption[] { if (!Array.isArray(raw)) { return [] } @@ -87,12 +78,14 @@ function parseOptions(raw: unknown): AskOption[] { 'label' in option && typeof option.label === 'string' ) { + const preview = multiSelect ? null : askOptionPreview(option) return { label: option.label, description: 'description' in option && typeof option.description === 'string' ? option.description - : undefined + : undefined, + ...(preview ? { preview } : {}) } } return null diff --git a/src/shared/structured-agent-question-card.ts b/src/shared/structured-agent-question-card.ts index 3c98b68c5f2..19487c33e7b 100644 --- a/src/shared/structured-agent-question-card.ts +++ b/src/shared/structured-agent-question-card.ts @@ -25,7 +25,8 @@ export function structuredQuestionCard( multiSelect: question.multiSelect, options: question.options.map((option) => ({ label: option.label, - ...(option.description ? { description: option.description } : {}) + ...(option.description ? { description: option.description } : {}), + ...(option.preview ? { preview: option.preview } : {}) })) })) }, From 3ee128db1a3e0128229cbab5ecc16a2c81f7a9e3 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:33:19 -0800 Subject: [PATCH 04/28] Keep Claude background tasks attached to their subagent after restart (#39958) * Keep Claude background tasks linked to their subagent across restarts * Keep task ownership scoped to each Claude invocation * Keep partial task events scoped to their invocation * Distinguish pending task facts from admitted invocations * Wait for saved task ownership before interpreting Claude events * Verify journal binding order and saved task liveness * Keep owned task updates from reopening Claude root turns * Exercise owned historical notifications with provider identity --- .../claude-parent-task-stop-2.1.319.jsonl | 10 + .../claude-background-stop-records.test.ts | 41 + .../claude/claude-background-task-frames.ts | 12 + .../claude/claude-background-task-identity.ts | 107 +++ .../claude/claude-background-task-roster.ts | 33 +- .../claude-background-task-row-journal.ts | 103 +-- .../claude-background-task-row-lifecycle.ts | 21 + .../claude-background-task-row-writer.ts | 36 + .../claude/claude-background-task-rows.ts | 143 ++-- .../claude/claude-background-task-start.ts | 18 +- ...de-background-task-turn-resumption.test.ts | 75 ++ src/main/claude/claude-child-work-decoder.ts | 183 ++-- src/main/claude/claude-child-work-evidence.ts | 66 +- ...aude-child-work-roster-performance.test.ts | 89 ++ .../claude-journal-bound-delivery.test.ts | 112 +++ .../claude/claude-journal-bound-delivery.ts | 135 +++ .../claude-journal-translator-contract.ts | 5 + src/main/claude/claude-message-journaling.ts | 5 +- .../claude-parent-task-frames.test-fixture.ts | 11 + .../claude-parent-task-recovery.test.ts | 785 ++++++++++++++++++ .../claude-stream-json-connection.test.ts | 14 + src/main/claude/claude-subagent-identities.ts | 14 +- .../claude-subagent-journaled-identities.ts | 35 +- src/main/claude/claude-subagent-linkage.ts | 11 +- ...aude-subagent-resume-after-restart.test.ts | 90 +- .../claude/claude-subagent-task-frames.ts | 8 + src/main/claude/claude-task-invocations.ts | 165 ++++ .../structured/claude-owned-task-send.test.ts | 116 +++ ...claude-structured-dispatch-test-support.ts | 2 + .../claude-structured-event-delivery.ts | 28 +- .../claude-structured-session-acquisition.ts | 20 +- .../claude-structured-session-adapter.ts | 11 +- .../claude-structured-session-close.ts | 2 + ...laude-structured-session-exit-lifecycle.ts | 1 + .../claude-structured-session-publication.ts | 8 +- .../claude-structured-session-state.ts | 17 +- .../claude-structured-journal-translation.ts | 199 ++--- ...aude-structured-session-journal-control.ts | 36 +- .../launch/claude-structured-options.test.ts | 2 + ...ructured-agent-session-event-sink-queue.ts | 19 + .../structured-agent-session-event-sink.ts | 3 + ...nt-session-history-subagent-roster.test.ts | 43 + .../agent-session-journal-producer.test.ts | 32 + .../journal/agent-session-journal-producer.ts | 21 +- .../native-chat-transcript-projection.test.ts | 31 + 45 files changed, 2551 insertions(+), 367 deletions(-) create mode 100644 src/main/claude/__fixtures__/claude-parent-task-stop-2.1.319.jsonl create mode 100644 src/main/claude/claude-background-task-identity.ts create mode 100644 src/main/claude/claude-child-work-roster-performance.test.ts create mode 100644 src/main/claude/claude-journal-bound-delivery.test.ts create mode 100644 src/main/claude/claude-journal-bound-delivery.ts create mode 100644 src/main/claude/claude-parent-task-frames.test-fixture.ts create mode 100644 src/main/claude/claude-parent-task-recovery.test.ts create mode 100644 src/main/claude/claude-task-invocations.ts create mode 100644 src/main/claude/structured/claude-owned-task-send.test.ts create mode 100644 src/main/native-chat/agent-session-wire/publication/history/agent-session-history-subagent-roster.test.ts create mode 100644 src/shared/agent-session/journal/agent-session-journal-producer.test.ts diff --git a/src/main/claude/__fixtures__/claude-parent-task-stop-2.1.319.jsonl b/src/main/claude/__fixtures__/claude-parent-task-stop-2.1.319.jsonl new file mode 100644 index 00000000000..f61bb00f483 --- /dev/null +++ b/src/main/claude/__fixtures__/claude-parent-task-stop-2.1.319.jsonl @@ -0,0 +1,10 @@ +{"at":4444,"frame":{"type":"system","subtype":"background_tasks_changed","tasks":[{"task_id":"agent-parent","run_id":"0mw894hmc-1120e0bd","task_type":"local_agent","subagent_type":"general-purpose","description":"Parent ownership probe"}],"uuid":"parent-task-frame-1","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":4446,"frame":{"type":"system","subtype":"task_started","task_id":"agent-parent","run_id":"0mw894hmc-1120e0bd","tool_use_id":"toolu_parent","description":"Parent ownership probe","subagent_type":"general-purpose","is_backgrounded":true,"spawn_depth":1,"task_type":"local_agent","prompt":"First use Bash with run_in_background true to run `sleep 100`. Then use Bash in the foreground to run `sleep 100`. Do not run any other commands. When both are done, briefly report that you finished.","uuid":"parent-task-frame-2","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7437,"frame":{"type":"system","subtype":"background_tasks_changed","tasks":[{"task_id":"agent-parent","run_id":"0mw894hmc-1120e0bd","task_type":"local_agent","subagent_type":"general-purpose","description":"Parent ownership probe"},{"task_id":"shell-child","run_id":"0mw894jxj-fd21549d","task_type":"local_bash","description":"Sleep 100 seconds in background","parent_task_id":"agent-parent"}],"uuid":"parent-task-frame-3","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7439,"frame":{"type":"system","subtype":"task_started","task_id":"shell-child","run_id":"0mw894jxj-fd21549d","owned_by_subagent":true,"tool_use_id":"toolu_shell","description":"Sleep 100 seconds in background","is_backgrounded":true,"parent_task_id":"agent-parent","task_type":"local_bash","uuid":"parent-task-frame-4","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7446,"frame":{"type":"system","subtype":"task_updated","task_id":"agent-parent","run_id":"0mw894hmc-1120e0bd","patch":{"status":"killed","end_time":1794167703280},"uuid":"parent-task-frame-5","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7446,"frame":{"type":"system","subtype":"task_notification","task_id":"agent-parent","run_id":"0mw894hmc-1120e0bd","tool_use_id":"toolu_parent","status":"stopped","output_file":"/tmp/claude/tasks/agent-parent.output","summary":"Parent ownership probe","uuid":"parent-task-frame-6","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7446,"frame":{"type":"system","subtype":"background_tasks_changed","tasks":[{"task_id":"shell-child","run_id":"0mw894jxj-fd21549d","task_type":"local_bash","description":"Sleep 100 seconds in background","parent_task_id":"agent-parent"}],"uuid":"parent-task-frame-7","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7449,"frame":{"type":"system","subtype":"task_updated","task_id":"shell-child","run_id":"0mw894jxj-fd21549d","patch":{"status":"killed","end_time":1794167703284},"uuid":"parent-task-frame-8","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7450,"frame":{"type":"system","subtype":"task_notification","task_id":"shell-child","run_id":"0mw894jxj-fd21549d","tool_use_id":"toolu_shell","status":"stopped","output_file":"/tmp/claude/tasks/shell-child.output","summary":"Sleep 100 seconds in background","uuid":"parent-task-frame-9","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} +{"at":7450,"frame":{"type":"system","subtype":"background_tasks_changed","tasks":[],"uuid":"parent-task-frame-10","session_id":"819cf9f8-e43c-4ad7-b50f-54aa158a726a"}} diff --git a/src/main/claude/claude-background-stop-records.test.ts b/src/main/claude/claude-background-stop-records.test.ts index 31e7a477a0c..699d30ca2d6 100644 --- a/src/main/claude/claude-background-stop-records.test.ts +++ b/src/main/claude/claude-background-stop-records.test.ts @@ -8,6 +8,7 @@ import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/ag import { agentChildWorkLiveness } from '../../shared/agent-status/child-work/agent-status-child-work-liveness' import { conversationCommandBlocked } from '../native-chat/agent-session-wire/operations/structured-conversation-command-admission' import type { CapturedFrame } from './claude-captured-frame-builders.test-fixture' +import { PARENT_TASK_STOP } from './claude-parent-task-frames.test-fixture' import { RESUMED_BY_MESSAGE } from './claude-captured-task-frames.test-fixture' import { hostWithParent, parent, producer } from './claude-child-work-producer-harness.test-fixture' @@ -78,6 +79,46 @@ async function droppedFromRoster() { } describe('a background Stop acts on the host child records', () => { + it('keeps a stopped owner and its independently stoppable shell attached from real task frames', async () => { + const host = hostWithParent() + const run = await producer(host) + run.replay(PARENT_TASK_STOP.slice(0, 7)) + const owner = run.records().find((record) => record.kind === 'agent') + expect(owner).toMatchObject({ membership: 'settled', outcome: 'cancelled' }) + expect(run.records().find((record) => record.kind === 'command')).toMatchObject({ + membership: 'live', + stoppable: true, + parentChildWorkId: owner?.childWorkId + }) + const targets = agentChildWorkStopTargets( + host.getStructuredChildWorkViews(parent), + 'shell-child' + ) + expect(targets.map((target) => target.providerId)).toEqual(['shell-child']) + await run.adapter.stopBackgroundTasks({ + sessionId: 'session-1', + fence: 7, + taskIds: targets.map((target) => target.providerId) + }) + expect(run.claude.connections[0]!.calls.filter((call) => call.subtype === 'stop_task')).toEqual( + [{ subtype: 'stop_task', params: { taskId: 'shell-child' } }] + ) + run.replay(PARENT_TASK_STOP.slice(7)) + expect(run.records().find((record) => record.kind === 'command')).toMatchObject({ + membership: 'settled', + outcome: 'cancelled' + }) + + // Start-only delivery in reverse owner order must converge too; no original tool traffic is replayed. + const childFirst = await producer() + childFirst.replay([PARENT_TASK_STOP[3]!, PARENT_TASK_STOP[1]!]) + const resolvedOwner = childFirst.records().find((record) => record.kind === 'agent') + expect(childFirst.records().find((record) => record.kind === 'command')).toMatchObject({ + membership: 'live', + parentChildWorkId: resolvedOwner?.childWorkId + }) + }) + it("stops the row's own task, which the records keep after the roster dropped it", async () => { const { records, blocked, stopCalls, stop, basis } = await droppedFromRoster() diff --git a/src/main/claude/claude-background-task-frames.ts b/src/main/claude/claude-background-task-frames.ts index 5b776d6af04..c2948a166e9 100644 --- a/src/main/claude/claude-background-task-frames.ts +++ b/src/main/claude/claude-background-task-frames.ts @@ -88,3 +88,15 @@ export function liveClaudeTaskRunState(status: unknown): AgentSessionBackgroundT return null } } + +export const CLAUDE_BACKGROUND_TASK_ROW_SUBTYPES: ReadonlySet = new Set([ + 'task_started', + 'task_updated', + 'task_progress', + 'task_notification' +]) + +export const CLAUDE_BACKGROUND_TASK_SILENT_REFRESH_SUBTYPES: ReadonlySet = new Set([ + 'task_progress', + 'background_tasks_changed' +]) diff --git a/src/main/claude/claude-background-task-identity.ts b/src/main/claude/claude-background-task-identity.ts new file mode 100644 index 00000000000..2d19b843dde --- /dev/null +++ b/src/main/claude/claude-background-task-identity.ts @@ -0,0 +1,107 @@ +import type { AgentJournalItemIdentity } from '../../shared/agent-session/journal/agent-session-journal-identity' +import { parseAgentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key' +import type { AgentJournalProducerLinkage } from '../../shared/agent-session/journal/agent-session-journal-types' +import { isBackgroundTaskBlock } from '../../shared/native-chat/native-chat-types' +import type { StructuredAgentSessionLifecycleJournal } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-journal-views' + +export type ClaudeBackgroundTaskDescriptor = { + identity: AgentJournalItemIdentity + generation: number + toolUseId?: string + linkage: AgentJournalProducerLinkage +} + +export type ClaudeBackgroundTaskIndex = { + latest?: ClaudeBackgroundTaskDescriptor + unresolved?: ClaudeBackgroundTaskDescriptor + aliases: Map +} + +export function claudeBackgroundTaskIdentity( + taskId: string, + generation = 1 +): AgentJournalItemIdentity { + return { + provider: 'orca', + clientMessageId: `claude-background-task:${taskId}${generation > 1 ? `#${generation}` : ''}` + } +} + +export function indexClaudeBackgroundTask( + index: ClaudeBackgroundTaskIndex, + itemId: string, + taskId: string, + toolUseId: string | undefined, + linkage: AgentJournalProducerLinkage +): void { + const identity = parseAgentJournalItemKey(itemId) + if (identity?.provider !== 'orca') { + return + } + const base = `claude-background-task:${taskId}` + const generation = + identity.clientMessageId === base + ? 1 + : identity.clientMessageId.startsWith(`${base}#`) + ? Number(identity.clientMessageId.slice(base.length + 1)) + : Number.NaN + if (!Number.isSafeInteger(generation) || generation < 1) { + return + } + const descriptor = { identity, generation, toolUseId, linkage } + if (!index.latest || generation >= index.latest.generation) { + index.latest = descriptor + } + if (toolUseId === undefined) { + if (!index.unresolved || generation >= index.unresolved.generation) { + index.unresolved = descriptor + } + } else { + const prior = index.aliases.get(toolUseId) + if (!prior || generation >= prior.generation) { + index.aliases.set(toolUseId, descriptor) + } + } +} + +export function selectClaudeBackgroundTask( + index: ClaudeBackgroundTaskIndex | undefined, + toolUseId?: string +): ClaudeBackgroundTaskDescriptor | undefined { + return toolUseId === undefined + ? index?.latest + : (index?.aliases.get(toolUseId) ?? index?.unresolved) +} + +export function resolveClaudeBackgroundTaskDescriptor( + journal: StructuredAgentSessionLifecycleJournal, + id: string, + toolUseId?: string +): ClaudeBackgroundTaskDescriptor { + const index: ClaudeBackgroundTaskIndex = { aliases: new Map() } + journal.visitItems((itemId, _sequence, body) => { + if (body.kind !== 'message') { + return + } + const block = body.blocks.find(isBackgroundTaskBlock) + if (block?.taskId === id) { + indexClaudeBackgroundTask(index, itemId, id, block.parentToolUseId, {}) + } + }) + return ( + selectClaudeBackgroundTask(index, toolUseId) ?? { + identity: claudeBackgroundTaskIdentity(id, (index.latest?.generation ?? 0) + 1), + generation: (index.latest?.generation ?? 0) + 1, + toolUseId, + linkage: {} + } + ) +} + +export function resolveClaudeBackgroundTaskIdentity( + journal: StructuredAgentSessionLifecycleJournal, + id: string, + toolUseId: string | undefined +): AgentJournalItemIdentity { + return resolveClaudeBackgroundTaskDescriptor(journal, id, toolUseId).identity +} diff --git a/src/main/claude/claude-background-task-roster.ts b/src/main/claude/claude-background-task-roster.ts index 422ec533fca..80e89a514e6 100644 --- a/src/main/claude/claude-background-task-roster.ts +++ b/src/main/claude/claude-background-task-roster.ts @@ -1,3 +1,4 @@ +import type { ClaudeTaskInvocations } from './claude-task-invocations' import { isSettledBackgroundTaskState } from '../../shared/native-chat/input/native-chat-background-task-row' import { classifyClaudeBackgroundTaskKind, @@ -15,16 +16,23 @@ import type { export function observeClaudeBackgroundTaskRoster( tasks: unknown[], rows: Map, - revise: (id: string, change: ClaudeBackgroundTaskChange) => void + revise: (id: string, change: ClaudeBackgroundTaskChange) => void, + admitOwned?: (id: string, task: Record) => boolean | void ): void { for (const entry of tasks) { const task = record(entry) const id = task === null ? null : taskId(task) + if (task && id && task.ambient !== true && task.skip_transcript !== true) { + if (admitOwned?.(id, task) === false) { + continue + } + } const row = id === null ? undefined : rows.get(id) if ( task === null || id === null || task.ambient === true || + task.skip_transcript === true || !row || isSettledBackgroundTaskState(row.block.state) ) { @@ -37,3 +45,26 @@ export function observeClaudeBackgroundTaskRoster( }) } } + +export function observeOwnedClaudeBackgroundTaskRoster(input: { + tasks: unknown[] + rows: Map + invocations: ClaudeTaskInvocations + revise: (id: string, change: ClaudeBackgroundTaskChange) => void + attribute: (id: string, row: ClaudeBackgroundTaskRow) => void + admit: (id: string, task: Record) => void +}): void { + observeClaudeBackgroundTaskRoster(input.tasks, input.rows, input.revise, (id, task) => { + const selected = input.invocations.selection(task, id) + if (!selected?.acceptsStart || selected.invocation.excluded || selected.invocation.monitor) { + return false + } + const row = input.rows.get(id) + if (row) { + input.attribute(id, row) + } else if (selected.invocation.parentTaskId !== undefined) { + input.admit(id, task) + } + return true + }) +} diff --git a/src/main/claude/claude-background-task-row-journal.ts b/src/main/claude/claude-background-task-row-journal.ts index e78bf4f0c09..3ea4197ff06 100644 --- a/src/main/claude/claude-background-task-row-journal.ts +++ b/src/main/claude/claude-background-task-row-journal.ts @@ -8,60 +8,22 @@ import type { } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' import type { StructuredAgentSessionLifecycleJournal } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-journal-views' import type { ClaudeBackgroundTaskRow } from './claude-background-task-row-lifecycle' -import { parseAgentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key' +import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key' +import { estimateStructuredAgentSessionItemBytes } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink-estimate' +import { + claudeBackgroundTaskIdentity, + resolveClaudeBackgroundTaskIdentity +} from './claude-background-task-identity' +export { + claudeBackgroundTaskIdentity, + resolveClaudeBackgroundTaskIdentity +} from './claude-background-task-identity' const MAX_RESOLVED_TASK_IDENTITIES = 512 const ADMITTED: StructuredAgentSessionSinkAdmission = { accepted: true } -/** Generation suffixes preserve restarted runs; generation 1 retains the legacy journal key. */ -export function claudeBackgroundTaskIdentity( - taskId: string, - generation = 1 -): AgentJournalItemIdentity { - const key = - generation > 1 - ? `claude-background-task:${taskId}#${generation}` - : `claude-background-task:${taskId}` - return { provider: 'orca', clientMessageId: key } -} - export const claudeBackgroundTaskBody = backgroundTaskJournalBody -export function resolveClaudeBackgroundTaskIdentity( - journal: StructuredAgentSessionLifecycleJournal, - id: string, - toolUseId: string | undefined -): AgentJournalItemIdentity { - let maxGeneration = 0 - let matchingGeneration: number | undefined - let parentlessGeneration: number | undefined - journal.visitItems((itemId, _sequence, body) => { - const identity = parseAgentJournalItemKey(itemId) - if (!identity || identity.provider !== 'orca') { - return - } - const taskBlock = body.kind === 'message' ? body.blocks.find(isBackgroundTaskBlock) : undefined - if (!taskBlock || taskBlock.taskId !== id) { - return - } - const generation = persistedTaskGeneration(identity.clientMessageId, id) - if (generation === null) { - return - } - maxGeneration = Math.max(maxGeneration, generation) - if (taskBlock.parentToolUseId === toolUseId) { - matchingGeneration = Math.max(matchingGeneration ?? 0, generation) - } else if (taskBlock.parentToolUseId === undefined) { - parentlessGeneration = Math.max(parentlessGeneration ?? 0, generation) - } - }) - // A prior parentless row cannot be proved distinct from the later aliased outcome. - return claudeBackgroundTaskIdentity( - id, - matchingGeneration ?? parentlessGeneration ?? (maxGeneration === 0 ? 1 : maxGeneration + 1) - ) -} - export class ClaudeBackgroundTaskIdentityResolver { private journal: StructuredAgentSessionLifecycleJournal | null = null private epoch: string | null = null @@ -102,19 +64,6 @@ export class ClaudeBackgroundTaskIdentityResolver { } } -function persistedTaskGeneration(clientMessageId: string, taskId: string): number | null { - const base = `claude-background-task:${taskId}` - if (clientMessageId === base) { - return 1 - } - const prefix = `${base}#` - if (!clientMessageId.startsWith(prefix)) { - return null - } - const generation = Number(clientMessageId.slice(prefix.length)) - return Number.isSafeInteger(generation) && generation > 1 ? generation : null -} - export function writeClaudeBackgroundTaskRow( sink: StructuredAgentSessionEventSink, identities: ClaudeBackgroundTaskIdentityResolver, @@ -127,7 +76,7 @@ export function writeClaudeBackgroundTaskRow( lifecycle = false ): StructuredAgentSessionSinkAdmission { const body = claudeBackgroundTaskBody(row.block) - const serialized = JSON.stringify(body) + const serialized = JSON.stringify([body, row.linkage]) if (serialized === row.lastSerialized) { return ADMITTED } @@ -135,16 +84,42 @@ export function writeClaudeBackgroundTaskRow( const identity = claudeBackgroundTaskIdentity(id, row.generation) // A reused row belongs to the current run, so it must count as executing. const appendOptions = { + ...row.linkage, turnScope: turnScope(), executes: true as const, ...(lifecycle ? { lifecycle: true } : {}) } const publishOptions = lifecycle ? { lifecycle: true } : {} + const toolUseId = row.block.parentToolUseId const resolveIdentity = (journal: StructuredAgentSessionLifecycleJournal) => - identities.resolve(journal, id, row.toolUseId) + identities.resolve(journal, id, toolUseId) const appendAndPublish = sink.tryAppendResolvedItemAndPublish let admission: StructuredAgentSessionSinkAdmission - if (appendAndPublish) { + if (sink.tryReviseResolvedItemAndPublish) { + const identitySizeBound = claudeBackgroundTaskIdentity(id, Number.MAX_SAFE_INTEGER) + // A saved alias is at most512 characters, each at most6 JSON bytes. + const reservedBytes = + estimateStructuredAgentSessionItemBytes(identitySizeBound, body) + 512 * 6 + 64 + admission = sink.tryReviseResolvedItemAndPublish( + reservedBytes, + (journal) => { + const resolved = resolveIdentity(journal) + const saved = journal.itemBody(agentJournalItemKey(resolved)) + const savedBlock = + saved?.kind === 'message' ? saved.blocks.find(isBackgroundTaskBlock) : undefined + const block = body.kind === 'message' ? body.blocks.find(isBackgroundTaskBlock) : undefined + const alias = block?.parentToolUseId ?? savedBlock?.parentToolUseId + return { + identity: resolved, + body: + block && alias && block.parentToolUseId === undefined + ? claudeBackgroundTaskBody({ ...block, parentToolUseId: alias }) + : body + } + }, + appendOptions + ) + } else if (appendAndPublish) { // Budget for any safe generation suffix before the deferred sink resolves its durable identity. const identitySizeBound = claudeBackgroundTaskIdentity(id, Number.MAX_SAFE_INTEGER) admission = appendAndPublish(identitySizeBound, body, resolveIdentity, appendOptions) diff --git a/src/main/claude/claude-background-task-row-lifecycle.ts b/src/main/claude/claude-background-task-row-lifecycle.ts index e612b26ffa2..112157ec7d7 100644 --- a/src/main/claude/claude-background-task-row-lifecycle.ts +++ b/src/main/claude/claude-background-task-row-lifecycle.ts @@ -1,3 +1,5 @@ +import type { ClaudeTaskInvocation } from './claude-task-invocations' +import type { AgentJournalProducerLinkage } from '../../shared/agent-session/journal/agent-session-journal-types' import { canReplaceBackgroundTaskState, isSettledBackgroundTaskState @@ -19,6 +21,8 @@ import { export type ClaudeBackgroundTaskRow = { block: NativeChatBackgroundTaskBlock + linkage?: AgentJournalProducerLinkage + invocation?: ClaudeTaskInvocation lastSerialized: string | null /** Immutable alias for persistence/coalescing; the final frame may enrich the block's parent. */ toolUseId?: string @@ -208,3 +212,20 @@ export function reviseClaudeBackgroundTaskRow( } row.block = next } + +export function attributeClaudeBackgroundTaskRow(id: string, row: ClaudeBackgroundTaskRow): void { + const invocation = row.invocation + const toolUseId = invocation?.toolUseId + if (toolUseId && row.block.parentToolUseId === undefined) { + row.block = { ...row.block, parentToolUseId: toolUseId } + } + const parent = invocation?.reportedParentTaskId + if (parent) { + row.linkage = { + agentId: id, + parentAgentId: parent, + producerKind: 'background', + ...(toolUseId ? { providerParentRef: toolUseId } : {}) + } + } +} diff --git a/src/main/claude/claude-background-task-row-writer.ts b/src/main/claude/claude-background-task-row-writer.ts index d1f71279e70..b4551e1e480 100644 --- a/src/main/claude/claude-background-task-row-writer.ts +++ b/src/main/claude/claude-background-task-row-writer.ts @@ -1,3 +1,6 @@ +import { CLAUDE_BACKGROUND_TASK_SILENT_REFRESH_SUBTYPES } from './claude-background-task-frames' +import type { ClaudeTaskInvocations } from './claude-task-invocations' +import { claudeBackgroundTaskToolUseId } from './claude-background-task-row-lifecycle' import type { AgentJournalTurnScope } from '../../shared/agent-session/journal/agent-session-journal-turn-scope' import type { StructuredAgentSessionEventSink, @@ -94,3 +97,36 @@ export class ClaudeBackgroundTaskRowWriter { this.identities.clear() } } + +export function createClaudeBackgroundTaskTurnOpener( + openOutputTurn: ((frame: Record, observedAt: number) => void) | undefined, + invocations: ClaudeTaskInvocations +) { + return ( + journaling: { frame: Record; observedAt: number } | null, + open: boolean, + id: string | null, + row?: ClaudeBackgroundTaskRow + ): (() => void) => { + const frame = journaling?.frame + const invocation = + row?.invocation ?? + (frame && id !== null + ? (invocations.selection(frame, id)?.invocation ?? + invocations.get(id, claudeBackgroundTaskToolUseId(frame))) + : undefined) + return () => { + // Task inventory from a known child cannot assert execution of the root. + const subtype = frame?.subtype + if ( + journaling && + open && + invocation?.parentTaskId === undefined && + (typeof subtype !== 'string' || + !CLAUDE_BACKGROUND_TASK_SILENT_REFRESH_SUBTYPES.has(subtype)) + ) { + openOutputTurn?.(journaling.frame, journaling.observedAt) + } + } + } +} diff --git a/src/main/claude/claude-background-task-rows.ts b/src/main/claude/claude-background-task-rows.ts index be03348a337..bc5dca47ebc 100644 --- a/src/main/claude/claude-background-task-rows.ts +++ b/src/main/claude/claude-background-task-rows.ts @@ -1,9 +1,15 @@ +import { ClaudeTaskInvocations } from './claude-task-invocations' import type { AgentJournalTurnScope } from '../../shared/agent-session/journal/agent-session-journal-turn-scope' import { isSettledBackgroundTaskState } from '../../shared/native-chat/input/native-chat-background-task-row' import type { StructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' import { BoundedMap } from '../../shared/bounded-map' -import { record, taskAliasId } from './claude-background-task-frames' import { + record, + taskAliasId, + CLAUDE_BACKGROUND_TASK_ROW_SUBTYPES +} from './claude-background-task-frames' +import { + attributeClaudeBackgroundTaskRow, claudeBackgroundTaskPatchChange, claudeBackgroundTaskToolUseId, canonicalClaudeBackgroundTaskId, @@ -19,31 +25,23 @@ import { ensureClaudeBackgroundTaskRowSlot, type ClaudeBackgroundTaskLedgerSizes } from './claude-background-task-memory' -import { ClaudeBackgroundTaskRowWriter } from './claude-background-task-row-writer' +import { + ClaudeBackgroundTaskRowWriter, + createClaudeBackgroundTaskTurnOpener +} from './claude-background-task-row-writer' import { observeClaudeBackgroundTaskStart } from './claude-background-task-start' -import { observeClaudeBackgroundTaskRoster } from './claude-background-task-roster' +import { observeOwnedClaudeBackgroundTaskRoster } from './claude-background-task-roster' import { ClaudeSubagentIds } from './claude-subagent-id-aliases' import { ClaudeOverflowTerminalRows } from './claude-overflow-terminal-rows' const MAX_TASK_ROWS = 64 -const TASK_SUBTYPES: ReadonlySet = new Set([ - 'task_started', - 'task_updated', - 'task_progress', - 'task_notification' -]) - -const SILENT_ROW_REFRESH_SUBTYPES: ReadonlySet = new Set([ - 'task_progress', - 'background_tasks_changed' -]) - export type ClaudeBackgroundTaskRowsDeps = { sink: StructuredAgentSessionEventSink /** Require a forwarded top-level invocation on first admission to exclude nested children. */ isForwardedParentTool: (toolUseId: string) => boolean /** Recover earlier-run subagent ownership when Claude does not repeat its announcement. */ + taskInvocations?: ClaudeTaskInvocations rosteredByEarlierRun?: (taskId: string) => boolean /** Reopen provider-resumed turns before journaling output so the session reports working. */ openOutputTurn?: (frame: Record, observedAt: number) => void @@ -56,13 +54,17 @@ export type ClaudeBackgroundTaskRowsDeps = { export class ClaudeBackgroundTaskRows { private readonly rows = new Map() private readonly writer: ClaudeBackgroundTaskRowWriter + private readonly openTurn: ReturnType private readonly overflowTerminalRows: ClaudeOverflowTerminalRows private readonly ledgers = new ClaudeBackgroundTaskLedgers() private readonly ids = new ClaudeSubagentIds() private readonly now: () => number + private readonly taskInvocations: ClaudeTaskInvocations constructor(private readonly deps: ClaudeBackgroundTaskRowsDeps) { + this.taskInvocations = deps.taskInvocations ?? new ClaudeTaskInvocations() this.now = deps.now ?? (() => Date.now()) + this.openTurn = createClaudeBackgroundTaskTurnOpener(deps.openOutputTurn, this.taskInvocations) this.writer = new ClaudeBackgroundTaskRowWriter( deps.sink, deps.turnScope, @@ -87,6 +89,7 @@ export class ClaudeBackgroundTaskRows { private journaling: { frame: Record; observedAt: number } | null = null observe(message: Record, observedAt: number = this.now()): boolean { + this.taskInvocations.observe(message) this.journaling = { frame: message, observedAt } // Claude wakes the lead for any task the lead launched, rowless subagents included; open that turn at the notification, once per frame. const toolUseId = @@ -101,7 +104,8 @@ export class ClaudeBackgroundTaskRows { if (frameId) { this.wakeFrames.set(frameId, true) } - this.deps.openOutputTurn?.(message, observedAt) + const id = canonicalClaudeBackgroundTaskId(message, this.ids) + this.openTurn(this.journaling, true, id)() } try { return this.observeFrame(message) @@ -111,10 +115,10 @@ export class ClaudeBackgroundTaskRows { } /** Monitor results may precede announcements; their notifications belong to the tool call. */ - observeMonitorToolResult(taskId: unknown): void { + observeMonitorToolResult(taskId: unknown, toolUseId?: string): void { const id = taskAliasId(taskId) if (id !== undefined) { - this.ledgers.rememberForeign(id, 'ambient') + this.taskInvocations.excludeMonitor(id, toolUseId) } } @@ -126,28 +130,46 @@ export class ClaudeBackgroundTaskRows { if (!Array.isArray(message.tasks)) { return false } - observeClaudeBackgroundTaskRoster(message.tasks, this.rows, (id, change) => - this.revise(id, change) - ) + observeOwnedClaudeBackgroundTaskRoster({ + tasks: message.tasks, + rows: this.rows, + invocations: this.taskInvocations, + revise: (id, change) => this.revise(id, change), + attribute: attributeClaudeBackgroundTaskRow, + admit: (id, task) => { + if (!this.ledgers.terminalTaskIds.has(id)) { + this.observeStart(id, task, true) + } + } + }) return true } - if (typeof message.subtype !== 'string' || !TASK_SUBTYPES.has(message.subtype)) { + if ( + typeof message.subtype !== 'string' || + !CLAUDE_BACKGROUND_TASK_ROW_SUBTYPES.has(message.subtype) + ) { return false } const id = canonicalClaudeBackgroundTaskId(message, this.ids) if (id === null) { return false } + const selected = this.taskInvocations.selection(message, id) + if (selected?.invocation.excluded || selected?.invocation.monitor) { + return true + } + if (selected?.current === false) { + if (message.subtype !== 'task_notification') { + return true + } + const generation = this.ledgers.generations.next(id) + const row = newClaudeBackgroundTaskRowFromNotification(id, message, this.now(), generation) + row.invocation = selected.invocation + this.writeRow(id, row) + return true + } if (message.subtype === 'task_started') { - return observeClaudeBackgroundTaskStart({ - id, - message, - rows: this.rows, - ledgers: this.ledgers, - isForwardedParentTool: this.deps.isForwardedParentTool, - openRow: (taskId, frame) => this.openRow(taskId, frame), - maxRows: MAX_TASK_ROWS - }) + return this.observeStart(id, message) } if (this.ledgers.foreign.has(id)) { return true @@ -162,6 +184,28 @@ export class ClaudeBackgroundTaskRows { return this.observePatch(id, message) } + private observeStart(id: string, message: Record, roster = false): boolean { + const selected = this.taskInvocations.selection(message, id) + if (!selected?.acceptsStart || selected.invocation.excluded || selected.invocation.monitor) { + return true + } + return observeClaudeBackgroundTaskStart({ + id, + message: { + ...message, + ...(roster ? { is_backgrounded: true } : {}), + tool_use_id: selected.invocation.toolUseId + }, + owned: selected.invocation.parentTaskId !== undefined, + rows: this.rows, + ledgers: this.ledgers, + isForwardedParentTool: this.deps.isForwardedParentTool, + openRow: (taskId, frame) => this.openRow(taskId, frame), + refresh: (id) => this.write(id), + maxRows: MAX_TASK_ROWS + }) + } + settleSession(): void { for (const [id, row] of this.rows) { if (!isSettledBackgroundTaskState(row.block.state)) { @@ -180,13 +224,13 @@ export class ClaudeBackgroundTaskRows { this.ids.clear() } - retryPendingWrites() { - return this.writer.retryPendingWrites() - } + retryPendingWrites = () => this.writer.retryPendingWrites() private openRow(id: string, message: Record): void { const generation = this.ledgers.generations.next(id) - this.rows.set(id, newClaudeBackgroundTaskRow(id, message, this.now(), generation)) + const row = newClaudeBackgroundTaskRow(id, message, this.now(), generation) + row.invocation = this.taskInvocations.get(id, claudeBackgroundTaskToolUseId(message)) + this.rows.set(id, row) this.write(id) } @@ -218,10 +262,9 @@ export class ClaudeBackgroundTaskRows { return true } const generation = this.ledgers.generations.next(id) - this.rows.set( - id, - newClaudeBackgroundTaskRowFromNotification(id, message, this.now(), generation) - ) + const row = newClaudeBackgroundTaskRowFromNotification(id, message, this.now(), generation) + row.invocation = this.taskInvocations.get(id, claudeBackgroundTaskToolUseId(message)) + this.rows.set(id, row) this.write(id) return true } @@ -230,6 +273,7 @@ export class ClaudeBackgroundTaskRows { if (this.ledgers.fallbackTaskIds.has(id)) { const change = claudeBackgroundTaskPatchChange(message) if (change.state && isSettledBackgroundTaskState(change.state)) { + this.taskInvocations.settleFrame(id, message) this.ledgers.rememberTerminal(this.rows, id, claudeBackgroundTaskToolUseId(message)) this.overflowTerminalRows.observePatch(id, message, change) return true @@ -248,6 +292,7 @@ export class ClaudeBackgroundTaskRows { } const change = claudeBackgroundTaskPatchChange(message) if (change.state && isSettledBackgroundTaskState(change.state)) { + this.taskInvocations.settleFrame(id, message) this.ledgers.rememberTerminal(this.rows, id, claudeBackgroundTaskToolUseId(message)) } if (row) { @@ -280,23 +325,7 @@ export class ClaudeBackgroundTaskRows { openOutputTurn = true, lifecycle = false ): void { - const journaling = this.journaling - this.writer.write( - id, - row, - () => { - // Why: progress and roster ticks refresh the row only. Opening a turn - // here flips an idle chat back to working. - const subtype = journaling?.frame.subtype - if ( - journaling && - openOutputTurn && - (typeof subtype !== 'string' || !SILENT_ROW_REFRESH_SUBTYPES.has(subtype)) - ) { - this.deps.openOutputTurn?.(journaling.frame, journaling.observedAt) - } - }, - lifecycle - ) + attributeClaudeBackgroundTaskRow(id, row) + this.writer.write(id, row, this.openTurn(this.journaling, openOutputTurn, id, row), lifecycle) } } diff --git a/src/main/claude/claude-background-task-start.ts b/src/main/claude/claude-background-task-start.ts index ed70d1b2da3..76f6ad07b1b 100644 --- a/src/main/claude/claude-background-task-start.ts +++ b/src/main/claude/claude-background-task-start.ts @@ -12,7 +12,7 @@ import { } from './claude-background-task-memory' import { isClaudeSubagentTask } from './claude-subagent-task-frames' -export function observeClaudeBackgroundTaskStart(input: { +type ClaudeBackgroundTaskStartInput = { id: string message: Record rows: Map @@ -20,7 +20,14 @@ export function observeClaudeBackgroundTaskStart(input: { isForwardedParentTool: (toolUseId: string) => boolean openRow: (id: string, message: Record) => void maxRows: number -}): boolean { +} + +export function observeClaudeBackgroundTaskStart( + input: ClaudeBackgroundTaskStartInput & { + owned: boolean + refresh: (id: string, row: ClaudeBackgroundTaskRow) => void + } +): boolean { const { id, message, rows, ledgers } = input if (ledgers.fallbackTaskIds.has(id)) { if (ledgers.terminalTaskIds.has(id)) { @@ -39,10 +46,6 @@ export function observeClaudeBackgroundTaskStart(input: { return false } } - if (message.ambient === true || message.skip_transcript === true) { - ledgers.rememberForeign(id, 'ambient') - return true - } if (isClaudeSubagentTask(message)) { ledgers.rememberForeign(id, 'roster') return true @@ -57,6 +60,7 @@ export function observeClaudeBackgroundTaskStart(input: { ledgers.foreign.delete(id) // A live task's duplicate announcement is redelivery, not a new run. if (!isSettledBackgroundTaskState(existing.block.state)) { + input.refresh(id, existing) return true } if (shouldRestartClaudeBackgroundTaskRow(existing, message)) { @@ -81,7 +85,7 @@ export function observeClaudeBackgroundTaskStart(input: { ledgers.foreign.delete(id) const toolUseId = claudeBackgroundTaskToolUseId(message) // Absence of a parent is not evidence of an unforwarded parent. - if (toolUseId !== undefined && !input.isForwardedParentTool(toolUseId)) { + if (toolUseId !== undefined && !input.owned && !input.isForwardedParentTool(toolUseId)) { ledgers.rememberForeign(id, 'sidechain') return true } diff --git a/src/main/claude/claude-background-task-turn-resumption.test.ts b/src/main/claude/claude-background-task-turn-resumption.test.ts index 9ae77f35372..13a2d59ba17 100644 --- a/src/main/claude/claude-background-task-turn-resumption.test.ts +++ b/src/main/claude/claude-background-task-turn-resumption.test.ts @@ -161,3 +161,78 @@ describe('a typed background-task row opens the turn it resumes', () => { expect(projected(items())).toBe('idle') }) }) + +it('keeps an alias-only owned notification idle even when its alias was forwarded at the root', () => { + const { translator, items } = harness() + spawnToolCall(translator) + translator.handle( + systemFrame('owned-start', { + subtype: 'task_started', + task_id: 'owned', + tool_use_id: TOOL, + parent_task_id: 'owner', + task_type: 'local_bash', + is_backgrounded: true + }) + ) + settleTurn(translator) + translator.handle( + systemFrame('owned-terminal', { + subtype: 'task_notification', + tool_use_id: TOOL, + status: 'completed', + summary: 'Finished child' + }) + ) + expect(items().find((item) => item.itemId.includes('owned'))?.body).toMatchObject({ + kind: 'message', + blocks: expect.arrayContaining([ + expect.objectContaining({ type: 'background-task', taskId: 'owned', state: 'done' }) + ]) + }) + expect(projected(items())).toBe('idle') + translator.dispose() +}) + +it('keeps owned terminal overflow writes idle without a retained row invocation', () => { + const { translator, items } = harness() + for (let index = 0; index < 65; index += 1) { + translator.handle( + systemFrame(`owned-start-${index}`, { + subtype: 'task_started', + task_id: `owned-${index}`, + parent_task_id: 'owner', + task_type: 'local_bash', + is_backgrounded: true + }) + ) + } + settleTurn(translator) + translator.handle( + systemFrame('overflow-owned-patch', { + subtype: 'task_updated', + task_id: 'owned-64', + patch: { status: 'failed' } + }) + ) + translator.handle( + systemFrame('overflow-owned-orphan', { + subtype: 'task_notification', + task_id: 'owned-terminal-only', + parent_task_id: 'owner', + status: 'completed' + }) + ) + const terminal = items().filter( + (item) => + item.body.kind === 'message' && + item.body.blocks.some( + (block) => + block.type === 'background-task' && + ['owned-64', 'owned-terminal-only'].includes(block.taskId) + ) + ) + expect(terminal).toHaveLength(2) + expect(projected(items())).toBe('idle') + translator.dispose() +}) diff --git a/src/main/claude/claude-child-work-decoder.ts b/src/main/claude/claude-child-work-decoder.ts index 67dd220f3fa..d155db75b49 100644 --- a/src/main/claude/claude-child-work-decoder.ts +++ b/src/main/claude/claude-child-work-decoder.ts @@ -1,14 +1,7 @@ // Roster changes, turn endings and spawn results cannot settle a child. -import type { - AgentChildWorkKind, - AgentChildWorkOperation, - AgentChildWorkOutcome -} from '../../shared/agent-status/child-work/agent-status-child-work' -import type { - AgentChildWorkEvidence, - AgentChildWorkLiveObservation -} from '../../shared/agent-status/child-work/agent-status-child-work-evidence' +import { ClaudeTaskInvocations } from './claude-task-invocations' +import type { AgentChildWorkEvidence } from '../../shared/agent-status/child-work/agent-status-child-work-evidence' import { classifyClaudeBackgroundTaskKind, liveClaudeTaskRunState, @@ -21,83 +14,34 @@ import { taskUsageTotalTokens, terminalClaudeTaskRunState } from './claude-background-task-frames' -import { isClaudeSubagentTask } from './claude-subagent-task-frames' +import { claudeStartedTaskKind } from './claude-subagent-task-frames' import { ClaudeAgentWaitCalls } from './claude-agent-wait-calls' -import { ORCA_ORCHESTRATION_WAIT_COMMAND } from '../../shared/orca-cli-invocations' import { claudeChildWorkOutcome, + claudeChildWorkObservation, + type DecodedClaudeTask, + type EndedClaudeTask, claudeTaskProgressFacts, type ClaudeTaskFacts } from './claude-child-work-evidence' -type DecodedClaudeTask = { - kind: AgentChildWorkKind - backgrounded: boolean - /** Monitors stay monitoring until the provider reports them running. */ - running: boolean - name?: string - description?: string - toolUseId?: string - /** Runs `orca orchestration check --wait`: waiting on worker agents, so agent work. */ - awaitsAgents?: true -} - type ClaudeToolOwner = (toolUseId: string) => string | null type PendingEdge = (observedAt: number) => AgentChildWorkEvidence -function observation( - id: string, - task: DecodedClaudeTask, - facts: ClaudeTaskFacts, - observedAt: number, - waiting: boolean -): AgentChildWorkLiveObservation { - const operation: AgentChildWorkOperation | undefined = facts.toolName - ? { toolName: facts.toolName, basis: 'reported', observedAt } - : task.awaitsAgents - ? { - toolName: 'Bash', - input: ORCA_ORCHESTRATION_WAIT_COMMAND, - basis: 'reported', - observedAt - } - : undefined - return { - handle: { - idKind: 'task_id', - id, - ...(task.toolUseId !== undefined ? { runId: task.toolUseId } : {}) - }, - kind: task.kind, - residency: task.backgrounded ? 'background' : 'foreground', - state: waiting ? 'waiting' : task.running || task.kind !== 'monitor' ? 'working' : 'monitoring', - ...(task.name ? { name: task.name, agentType: task.name } : {}), - ...(task.description ? { description: task.description } : {}), - ...(facts.totalTokens !== undefined ? { totalTokens: facts.totalTokens } : {}), - ...(operation ? { operation } : {}), - ...(facts.lastMessage ? { lastMessage: facts.lastMessage } : {}), - // Only background tasks expose a stop target. - stoppable: task.backgrounded - } -} - export class ClaudeChildWorkDecoder { private readonly live = new Map() /** Retain ended runs for the session to distinguish wakes from late starts. */ - private readonly ended = new Map< - string, - { - run: string | undefined - outcome: AgentChildWorkOutcome - task?: DecodedClaudeTask - } - >() + private readonly ended = new Map() + private readonly childrenByOwner = new Map>() private waiting: ReadonlySet = new Set() private pending: PendingEdge[] = [] private readonly agentWaits = new ClaudeAgentWaitCalls() /** `ownerOf` names the subagent that made a child tool call; an ending task wakes it. */ + constructor(readonly taskInvocations = new ClaudeTaskInvocations()) {} + observe(message: Record, ownerOf?: ClaudeToolOwner): void { + this.taskInvocations.observe(message) if (message.type === 'assistant') { this.agentWaits.observe(message) return @@ -105,10 +49,31 @@ export class ClaudeChildWorkDecoder { if (message.type !== 'system') { return } + if (message.subtype === 'background_tasks_changed') { + if (Array.isArray(message.tasks)) { + for (const entry of message.tasks) { + const task = record(entry) + const id = task && taskId(task) + if ( + task && + id && + !this.ended.has(id) && + this.taskInvocations.parentOf(id, taskAliasId(task.tool_use_id)) + ) { + this.started(id, task, true) + } + } + } + return + } const id = taskId(message) if (!id) { return } + const selected = this.taskInvocations.selection(message, id) + if (selected && (!selected.current || selected.invocation.excluded)) { + return + } switch (message.subtype) { case 'task_started': this.started(id, message) @@ -145,8 +110,10 @@ export class ClaudeChildWorkDecoder { } clear(): void { + this.taskInvocations.clear() this.live.clear() this.ended.clear() + this.childrenByOwner.clear() this.agentWaits.clear() this.waiting = new Set() this.pending.push((observedAt) => ({ type: 'session-ended', observedAt })) @@ -158,12 +125,14 @@ export class ClaudeChildWorkDecoder { return pending.map((edge) => edge(observedAt)) } - private started(id: string, message: Record): void { - const toolUseId = taskAliasId(message.tool_use_id) - const awaitsAgents = this.agentWaits.take(toolUseId) - if (message.ambient === true || message.skip_transcript === true) { + private started(id: string, message: Record, roster = false): void { + const selection = this.taskInvocations.selection(message, id) + if (!selection?.acceptsStart || selection.invocation.excluded) { return } + const invocation = selection.invocation + const toolUseId = invocation.toolUseId + const awaitsAgents = this.agentWaits.take(toolUseId) const existing = this.live.get(id) const ended = existing ? undefined : this.ended.get(id) const restart = ended !== undefined @@ -171,12 +140,13 @@ export class ClaudeChildWorkDecoder { if (ended?.run !== undefined && ended.run === toolUseId && ended.outcome !== 'succeeded') { return } - const kind = startedTaskKind(message) + const kind = claudeStartedTaskKind(message) this.upsert( id, { kind: kind !== 'unknown' ? kind : (existing?.kind ?? 'unknown'), backgrounded: + roster || message.is_backgrounded === true || kind === 'workflow' || kind === 'monitor' || @@ -185,16 +155,22 @@ export class ClaudeChildWorkDecoder { name: taskName(message) ?? existing?.name, description: taskDescription(message.description) ?? existing?.description, toolUseId: toolUseId ?? existing?.toolUseId, + parentTaskId: invocation.parentTaskId, ...(awaitsAgents || existing?.awaitsAgents ? { awaitsAgents: true as const } : {}) }, {}, - restart + restart, + roster ) } /** Claude resumes a finished subagent when a task it launched notifies; show it working from that frame, not its restart a moment later. */ - private wakeOwner(toolUseId: string | undefined, ownerOf: ClaudeToolOwner | undefined): void { - const ownerId = toolUseId ? ownerOf?.(toolUseId) : null + private wakeOwner( + toolUseId: string | undefined, + ownerOf: ClaudeToolOwner | undefined, + parentTaskId?: string + ): void { + const ownerId = parentTaskId ?? (toolUseId ? ownerOf?.(toolUseId) : null) const owner = ownerId ? this.ended.get(ownerId) : undefined if (ownerId && owner?.outcome === 'succeeded' && owner.task?.kind === 'agent') { this.upsert(ownerId, { ...owner.task, running: true }, {}, true) @@ -207,11 +183,8 @@ export class ClaudeChildWorkDecoder { return } // Progress descriptions describe the current activity, not the task. - this.upsert( - id, - { ...task, name: task.name ?? taskName(message) }, - claudeTaskProgressFacts(message) - ) + const name = task.name ?? taskName(message) + this.upsert(id, { ...task, name }, claudeTaskProgressFacts(message)) } private updated( @@ -230,7 +203,6 @@ export class ClaudeChildWorkDecoder { } return } - // Late updates cannot reopen an ended task. if (!existing && this.ended.has(id)) { return } @@ -251,6 +223,7 @@ export class ClaudeChildWorkDecoder { name: name ?? existing?.name, description: description ?? existing?.description, toolUseId: existing?.toolUseId, + parentTaskId: existing?.parentTaskId, ...(existing?.awaitsAgents ? { awaitsAgents: true as const } : {}) }, {}, @@ -262,11 +235,44 @@ export class ClaudeChildWorkDecoder { id: string, task: DecodedClaudeTask, facts: ClaudeTaskFacts, - restart = false + restart = false, + unchangedRoster = false ): void { + if (unchangedRoster && JSON.stringify(task) === JSON.stringify(this.live.get(id))) { + return + } + const ownerBecameLive = !this.live.has(id) || restart this.ended.delete(id) + this.removeOwnerIndex(id) this.live.set(id, task) + if (task.parentTaskId) { + const children = this.childrenByOwner.get(task.parentTaskId) ?? new Set() + children.add(id) + this.childrenByOwner.set(task.parentTaskId, children) + } this.report(id, task, facts, restart) + // Admission can resolve a child's raw owner only after that owner's live edge. + if (!ownerBecameLive) { + return + } + for (const childId of this.childrenByOwner.get(id) ?? []) { + const child = this.live.get(childId) + if (child && childId !== id) { + this.report(childId, child, {}) + } + } + } + + private removeOwnerIndex(id: string): void { + const parent = this.live.get(id)?.parentTaskId + if (parent === undefined) { + return + } + const children = this.childrenByOwner.get(parent) + children?.delete(id) + if (children?.size === 0) { + this.childrenByOwner.delete(parent) + } } /** Read waiting state at drain so all queued live edges agree. */ @@ -274,7 +280,7 @@ export class ClaudeChildWorkDecoder { this.pending.push((observedAt) => ({ type: 'live', observedAt, - child: observation(id, task, facts, observedAt, this.waiting.has(id)), + child: claudeChildWorkObservation(id, task, facts, observedAt, this.waiting.has(id)), ...(restart ? { restart: true } : {}) })) } @@ -290,6 +296,8 @@ export class ClaudeChildWorkDecoder { ownerOf?: ClaudeToolOwner ): void { const task = this.live.get(id) + this.taskInvocations.settle(id, status, reported.runId ?? task?.toolUseId) + this.removeOwnerIndex(id) this.live.delete(id) const outcome = claudeChildWorkOutcome(status) this.ended.set(id, { @@ -300,7 +308,7 @@ export class ClaudeChildWorkDecoder { }) // Every ending but a stop notifies, which wakes the subagent that launched the task. if (outcome !== 'cancelled') { - this.wakeOwner(reported.runId ?? task?.toolUseId, ownerOf) + this.wakeOwner(reported.runId ?? task?.toolUseId, ownerOf, task?.parentTaskId) } this.pending.push((observedAt) => ({ type: 'ended', @@ -316,10 +324,3 @@ export class ClaudeChildWorkDecoder { })) } } - -/** Releases predating `task_type` still name an agent in `subagent_type`. */ -function startedTaskKind(message: Record): AgentChildWorkKind { - return isClaudeSubagentTask(message) - ? 'agent' - : classifyClaudeBackgroundTaskKind(message.task_type) -} diff --git a/src/main/claude/claude-child-work-evidence.ts b/src/main/claude/claude-child-work-evidence.ts index 33ff92e4267..7110cd50489 100644 --- a/src/main/claude/claude-child-work-evidence.ts +++ b/src/main/claude/claude-child-work-evidence.ts @@ -1,4 +1,9 @@ -import type { AgentChildWorkOutcome } from '../../shared/agent-status/child-work/agent-status-child-work' +import { ORCA_ORCHESTRATION_WAIT_COMMAND } from '../../shared/orca-cli-invocations' +import type { + AgentChildWorkKind, + AgentChildWorkOperation, + AgentChildWorkOutcome +} from '../../shared/agent-status/child-work/agent-status-child-work' import type { AgentChildWorkEvidence, AgentChildWorkLiveObservation @@ -17,6 +22,62 @@ import { type ClaudeToolUse } from './structured/journal/claude-structured-item-translation' +export type EndedClaudeTask = { + run: string | undefined + outcome: AgentChildWorkOutcome + task?: DecodedClaudeTask +} + +export type DecodedClaudeTask = { + kind: AgentChildWorkKind + backgrounded: boolean + /** Monitors stay monitoring until the provider reports them running. */ + running: boolean + name?: string + description?: string + toolUseId?: string + parentTaskId?: string + /** Runs `orca orchestration check --wait`: waiting on worker agents, so agent work. */ + awaitsAgents?: true +} + +export function claudeChildWorkObservation( + id: string, + task: DecodedClaudeTask, + facts: ClaudeTaskFacts, + observedAt: number, + waiting: boolean +): AgentChildWorkLiveObservation { + const operation: AgentChildWorkOperation | undefined = facts.toolName + ? { toolName: facts.toolName, basis: 'reported', observedAt } + : task.awaitsAgents + ? { + toolName: 'Bash', + input: ORCA_ORCHESTRATION_WAIT_COMMAND, + basis: 'reported', + observedAt + } + : undefined + return { + handle: { + idKind: 'task_id', + id, + ...(task.toolUseId !== undefined ? { runId: task.toolUseId } : {}) + }, + ...(task.parentTaskId ? { ownerId: task.parentTaskId } : {}), + kind: task.kind, + residency: task.backgrounded ? 'background' : 'foreground', + state: waiting ? 'waiting' : task.running || task.kind !== 'monitor' ? 'working' : 'monitoring', + ...(task.name ? { name: task.name, agentType: task.name } : {}), + ...(task.description ? { description: task.description } : {}), + ...(facts.totalTokens !== undefined ? { totalTokens: facts.totalTokens } : {}), + ...(operation ? { operation } : {}), + ...(facts.lastMessage ? { lastMessage: facts.lastMessage } : {}), + // Only background tasks expose a stop target. + stoppable: task.backgrounded + } +} + export type ClaudeTaskFacts = { toolName?: string lastMessage?: string @@ -57,6 +118,9 @@ export function withClaudeChildWorkOwners( return evidence } const owned = (child: AgentChildWorkLiveObservation): AgentChildWorkLiveObservation => { + if (child.ownerId !== undefined) { + return child + } const ownerId = child.handle.runId === undefined ? null : ownerOf(child.handle.runId) return ownerId !== null && ownerId !== child.handle.id ? { ...child, ownerId } : child } diff --git a/src/main/claude/claude-child-work-roster-performance.test.ts b/src/main/claude/claude-child-work-roster-performance.test.ts new file mode 100644 index 00000000000..36f68abfa97 --- /dev/null +++ b/src/main/claude/claude-child-work-roster-performance.test.ts @@ -0,0 +1,89 @@ +import { expect, it } from 'vitest' +import { ClaudeChildWorkDecoder } from './claude-child-work-decoder' +import { createAgentStatusStore } from '../../shared/agent-status/store/agent-status-store' +import { createAgentChildWorkAdmission } from '../../shared/agent-status/child-work/agent-status-child-work-admission' +import { reconcileAgentChildWorkEvidence } from '../../shared/agent-status/child-work/agent-status-child-work-reconciliation' +import { parent } from './claude-child-work-producer-harness.test-fixture' + +it('emits only new or changed roster facts at10 and100 live tasks', () => { + for (const count of [10, 100]) { + const decoder = new ClaudeChildWorkDecoder() + const store = createAgentStatusStore({ epoch: 'roster-perf', mode: 'authority' }) + store.applyMutation({ parent: { subject: parent } }) + let minted = 0 + const admission = createAgentChildWorkAdmission(store, { + mintChildWorkId: () => `child-${++minted}` + }) + let clock = 1_700_000_000_000 + let rosterEdges = 0 + let rosterWrites = 0 + const feed = (message: Record, rosterBound = 0) => { + decoder.observe({ type: 'system', ...message }) + const evidence = decoder.drain(++clock) + const before = store.getSnapshot().revision + reconcileAgentChildWorkEvidence({ store, admission, parent, provider: 'claude', evidence }) + const writes = store.getSnapshot().revision - before + if (message.subtype === 'background_tasks_changed') { + const live = evidence.filter((edge) => edge.type === 'live').length + expect(live).toBeLessThanOrEqual(rosterBound) + expect(writes).toBeLessThanOrEqual(rosterBound) + rosterEdges += live + rosterWrites += writes + } + } + const owner = { + task_id: 'owner', + task_type: 'local_agent', + tool_use_id: 'tool-owner', + is_backgrounded: true + } + feed({ subtype: 'task_started', ...owner }) + let shellOwner = 'owner' + const shell = (id: number) => ({ + task_id: `shell-${id}`, + task_type: 'local_bash', + description: `shell${id}`, + parent_task_id: shellOwner + }) + const live: number[] = [] + for (let id = 0; id < count; id++) { + live.push(id) + const roster = { subtype: 'background_tasks_changed', tasks: [owner, ...live.map(shell)] } + // Only one task is newly present; unchanged replacement rosters need no host writes. + feed(roster, 1) + feed({ + subtype: 'task_started', + ...shell(id), + tool_use_id: `tool-${id}`, + is_backgrounded: true + }) + feed(roster) + } + // The introductions above need at most one edge/write each, independent of full-roster size. + expect(rosterEdges).toBeLessThanOrEqual(count) + expect(rosterWrites).toBeLessThanOrEqual(count) + rosterEdges = 0 + rosterWrites = 0 + feed({ subtype: 'task_started', ...owner, task_id: 'owner-second', tool_use_id: 'tool-second' }) + shellOwner = 'owner-second' + // A real owner change invalidates exactly the affected children, then its duplicate invalidates none. + feed({ subtype: 'background_tasks_changed', tasks: live.map(shell) }, count) + feed({ subtype: 'background_tasks_changed', tasks: live.map(shell) }) + for (let id = 0; id < count; id++) { + feed({ + subtype: 'task_notification', + task_id: `shell-${id}`, + tool_use_id: `tool-${id}`, + status: 'completed' + }) + live.shift() + feed({ subtype: 'background_tasks_changed', tasks: [owner, ...live.map(shell)] }) + } + // N introductions require at most N live edges and N store writes, independent of roster size. + expect(rosterEdges).toBeLessThanOrEqual(count) + expect(rosterWrites).toBeLessThanOrEqual(count) + expect(store.getChildren(parent).filter((child) => child.kind === 'command')).toHaveLength( + count + ) + } +}) diff --git a/src/main/claude/claude-journal-bound-delivery.test.ts b/src/main/claude/claude-journal-bound-delivery.test.ts new file mode 100644 index 00000000000..6c14507c9cf --- /dev/null +++ b/src/main/claude/claude-journal-bound-delivery.test.ts @@ -0,0 +1,112 @@ +import { expect, it, vi } from 'vitest' +import { ClaudeJournalBoundDelivery } from './claude-journal-bound-delivery' +import { bindClaudeJournalReadingControl } from './structured/journal/claude-structured-session-journal-control' +import type { + StructuredAgentSessionEventSink, + StructuredAgentSessionReadingControl +} from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' + +function unboundSink() { + let notify: (() => void) | undefined + let control: StructuredAgentSessionReadingControl | undefined + let bound = false + const sink: StructuredAgentSessionEventSink = { + appendItem: () => {}, + appendTombstone: () => {}, + publish: () => {}, + onJournalBound: (listener) => { + notify = listener + return () => { + notify = undefined + } + }, + journalLinkage: () => + bound + ? { + epoch: 'test', + visitItemsWithLinkage: () => {}, + threadGoal: () => null, + threadGoalSource: () => ({ goal: null, row: null }) + } + : null, + bindReadingControl: (next) => { + control = next + return () => {} + } + } + return { + sink, + bind: () => { + bound = true + notify?.() + }, + pressure: (paused: boolean) => control?.[paused ? 'pauseReading' : 'resumeReading']() + } +} + +it('holds ordered interpretation until binding while preserving independent sink pressure', () => { + const state = unboundSink() + let published = false + const delivery = new ClaudeJournalBoundDelivery(state.sink, () => published) + const connection = { pauseReading: vi.fn(), resumeReading: vi.fn() } + bindClaudeJournalReadingControl(state.sink, connection, null, delivery) + const observed: string[] = [] + delivery.deliver(() => { + observed.push('start') + delivery.deliver(() => observed.push('later')) + }) + delivery.deliver(() => observed.push('terminal')) + published = true + delivery.flush() + state.pressure(true) + expect(observed).toEqual([]) + state.bind() + expect(observed).toEqual(['start', 'terminal', 'later']) + expect(connection.resumeReading).not.toHaveBeenCalled() + state.pressure(false) + expect(connection.resumeReading).toHaveBeenCalledOnce() +}) + +it('terminates the uncommitted prefix on disposal and ignores a late binding', () => { + const state = unboundSink() + const delivery = new ClaudeJournalBoundDelivery(state.sink) + const interpret = vi.fn() + delivery.deliver(interpret) + delivery.dispose() + state.bind() + delivery.deliver(interpret) + expect(interpret).not.toHaveBeenCalled() +}) + +it('fails a bounded unbound prefix instead of silently losing or replaying observations', () => { + const state = unboundSink() + const delivery = new ClaudeJournalBoundDelivery(state.sink) + const failure = vi.fn() + const interpret = vi.fn() + delivery.onFailure = failure + const payload = 'x'.repeat(1024 * 1024) + expect(() => { + for (let index = 0; index < 33; index++) { + delivery.deliver(interpret, payload) + } + }).toThrow('capacity exceeded') + state.bind() + expect(failure).toHaveBeenCalledOnce() + expect(interpret).not.toHaveBeenCalled() +}) + +it('reports binding interpretation failure through its owning failure path', () => { + const state = unboundSink() + const delivery = new ClaudeJournalBoundDelivery(state.sink) + const failure = vi.fn() + delivery.onFailure = failure + const error = new Error('invalid saved identity') + delivery.deliver(() => { + throw error + }) + const later = vi.fn() + delivery.deliver(later) + expect(() => state.bind()).not.toThrow() + expect(failure).toHaveBeenCalledWith(error) + expect(later).not.toHaveBeenCalled() +}) diff --git a/src/main/claude/claude-journal-bound-delivery.ts b/src/main/claude/claude-journal-bound-delivery.ts new file mode 100644 index 00000000000..f095cc7576d --- /dev/null +++ b/src/main/claude/claude-journal-bound-delivery.ts @@ -0,0 +1,135 @@ +import type { + StructuredAgentSessionEventSink, + StructuredAgentSessionReadingControl +} from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' + +/** Own provider observations until their saved identity input is available. */ +export class ClaudeJournalBoundDelivery { + private pending: ({ run: () => void; bytes: number } | undefined)[] = [] + private entries = 0 + private bytes = 0 + private draining = false + private closed = false + private unsubscribe?: () => void + private control?: StructuredAgentSessionReadingControl + private paused = false + onFailure?: (error: Error) => void + + constructor( + private sink?: StructuredAgentSessionEventSink, + private readonly published: () => boolean = () => true + ) { + this.bind(sink) + } + + bind(sink: StructuredAgentSessionEventSink | undefined): void { + this.unsubscribe?.() + this.sink = sink + this.unsubscribe = sink?.onJournalBound?.(() => this.flush()) + } + + get ready(): boolean { + return ( + !this.closed && + this.published() && + (!this.sink?.onJournalBound || this.sink.journalLinkage?.() !== null) + ) + } + + bindReadingControl(control: StructuredAgentSessionReadingControl): () => void { + this.control = control + if (this.paused) { + control.pauseReading() + } + return () => { + if (this.control === control) { + this.control = undefined + } + } + } + + deliver(run: () => void, payload?: unknown): void { + if (this.closed) { + return + } + if (this.ready && !this.draining) { + run() + return + } + const bytes = Buffer.byteLength(JSON.stringify(payload ?? null), 'utf8') + 64 + if (this.entries >= 1_024 || this.bytes + bytes > 32 * 1024 * 1024) { + const error = new Error('Claude event delivery capacity exceeded before journal binding') + this.dispose() + this.onFailure?.(error) + throw error + } + this.pending.push({ run, bytes }) + this.entries++ + this.bytes += bytes + this.updateReading() + } + + interpret(event: T, handle: (event: T) => void): void { + if (event.type === 'ended' || event.type === 'retiring') { + this.dispose() + handle(event) + } else if (this.ready) { + handle(event) + } else { + this.deliver(() => handle(event), event) + } + } + + flush(): void { + if (!this.ready || this.draining) { + this.updateReading() + return + } + this.draining = true + try { + while (this.pending.length && !this.closed) { + const prefix = this.pending + this.pending = [] + for (let index = 0; index < prefix.length; index++) { + if (this.closed) { + break + } + const entry = prefix[index]! + prefix[index] = undefined + this.entries-- + this.bytes -= entry.bytes + entry.run() + } + } + } catch (cause) { + this.dispose() + const error = cause instanceof Error ? cause : new Error(String(cause)) + if (!this.onFailure) { + throw error + } + this.onFailure(error) + } finally { + this.draining = false + this.updateReading() + } + } + + /** Retirement ends uncommitted delivery; it must never replay into a disposed translator. */ + dispose(): void { + this.closed = true + this.pending = [] + this.entries = 0 + this.bytes = 0 + this.unsubscribe?.() + this.unsubscribe = undefined + this.updateReading() + } + + private updateReading(): void { + const paused = !this.closed && this.published() && !this.ready + if (paused !== this.paused) { + this.paused = paused + this.control?.[paused ? 'pauseReading' : 'resumeReading']() + } + } +} diff --git a/src/main/claude/claude-journal-translator-contract.ts b/src/main/claude/claude-journal-translator-contract.ts index 96af6b7a240..21f4057ca02 100644 --- a/src/main/claude/claude-journal-translator-contract.ts +++ b/src/main/claude/claude-journal-translator-contract.ts @@ -1,3 +1,5 @@ +import type { ClaudeTaskInvocations } from './claude-task-invocations' +import type { ClaudeJournalBoundDelivery } from './claude-journal-bound-delivery' import type { AgentSessionContextReport } from '../../shared/agent-session/agent-session-context-usage' import type { AgentJournalTurnScope } from '../../shared/agent-session/journal/agent-session-journal-turn-scope' import type { @@ -16,6 +18,7 @@ import type { AgentSessionAccountKind } from '../../shared/agent-session/agent-s export type ClaudeJournalTranslatorDeps = { sink: StructuredAgentSessionEventSink + delivery?: ClaudeJournalBoundDelivery bindPromptItemId?: (journalItemId: string, promptKey: string) => void coalesceMs?: number schedule?: AgentSessionDeltaCoalescerDeps['schedule'] @@ -29,6 +32,7 @@ export type ClaudeJournalTranslatorDeps = { } export type ClaudeJournalTranslator = { + readonly taskInvocations: ClaudeTaskInvocations handle: (event: ClaudeStructuredSessionEvent) => void journalPrompts: Pick< ClaudeJournalPrompts, @@ -45,6 +49,7 @@ export type ClaudeJournalTranslator = { readonly openTurnInLiveProviderCycle: boolean readonly openTurnHasInputWork: boolean flush: () => void + childTaskParent?: (taskId: string, run?: string) => string | undefined childToolOwner?: ClaudeChildToolQueries['childToolOwner'] childActivity?: ClaudeChildToolQueries['childActivity'] retryPendingTaskRows?: () => StructuredAgentSessionSinkAdmission diff --git a/src/main/claude/claude-message-journaling.ts b/src/main/claude/claude-message-journaling.ts index 86c36ee9394..b25929a818a 100644 --- a/src/main/claude/claude-message-journaling.ts +++ b/src/main/claude/claude-message-journaling.ts @@ -196,7 +196,10 @@ export function journalClaudeMessage( tool.name === 'Monitor' && ctx.toolOrigins.has(result.toolUseId) ) { - ctx.backgroundTasks.observeMonitorToolResult(claudeRecord(message.tool_use_result)?.taskId) + ctx.backgroundTasks.observeMonitorToolResult( + claudeRecord(message.tool_use_result)?.taskId, + result.toolUseId + ) } ctx.tools.delete(result.toolUseId) changed = true diff --git a/src/main/claude/claude-parent-task-frames.test-fixture.ts b/src/main/claude/claude-parent-task-frames.test-fixture.ts new file mode 100644 index 00000000000..a90897291da --- /dev/null +++ b/src/main/claude/claude-parent-task-frames.test-fixture.ts @@ -0,0 +1,11 @@ +// CLI 2.1.319, isolated real run: parent Stop, live child roster, then separate child Stop; IDs/paths sanitized. +import { readFileSync } from 'node:fs' +import type { CapturedFrame } from './claude-captured-frame-builders.test-fixture' + +export const PARENT_TASK_STOP: CapturedFrame[] = readFileSync( + new URL('./__fixtures__/claude-parent-task-stop-2.1.319.jsonl', import.meta.url), + 'utf8' +) + .trim() + .split('\n') + .map((line) => JSON.parse(line)) diff --git a/src/main/claude/claude-parent-task-recovery.test.ts b/src/main/claude/claude-parent-task-recovery.test.ts new file mode 100644 index 00000000000..c5812f98b32 --- /dev/null +++ b/src/main/claude/claude-parent-task-recovery.test.ts @@ -0,0 +1,785 @@ +import { afterEach, expect, it } from 'vitest' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { + producer, + frame, + system, + toolUse, + toolResult +} from './claude-child-work-producer-harness.test-fixture' +import { createTrackedJournalOpener } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { createDeferredStructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' +import { createClaudeJournalTranslator } from './structured/journal/claude-structured-journal-translation' +import { testEventSinkLogging } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-logger-test-support' +import { claudeProviderHandle } from '../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' +import { + claudeBackgroundTaskBody, + claudeBackgroundTaskIdentity +} from './claude-background-task-row-journal' +import { AGENT_JOURNAL_THREAD_SCOPE } from '../../shared/agent-session/journal/agent-session-journal-types' +import { + adapterAtPublishFor, + fakeClaude, + identityFor +} from './structured/claude-structured-session-test-support' +import type { AgentChildWorkEvidence } from '../../shared/agent-status/child-work/agent-status-child-work-evidence' +import { ClaudeSubagentIdentities } from './claude-subagent-identities' +import { ClaudeJournaledSubagents } from './claude-subagent-journaled-identities' + +const journals = createTrackedJournalOpener() +const cleanup: string[] = [] +afterEach(async () => { + await journals.closeAll() + for (const path of cleanup.splice(0)) { + await rm(path, { recursive: true, force: true }) + } +}) + +it('retains explicit ambient and skip-transcript exclusion when roster omits those optional flags', async () => { + for (const flag of ['ambient', 'skip_transcript']) { + const run = await producer() + run.send( + system('task_started', { + task_id: 'excluded', + task_type: 'local_bash', + parent_task_id: 'owner', + is_backgrounded: true, + tool_use_id: 'tool-old', + [flag]: true + }) + ) + run.send( + system('background_tasks_changed', { + tasks: [{ task_id: 'excluded', task_type: 'local_bash', parent_task_id: 'owner' }] + }) + ) + run.send( + system('task_updated', { + task_id: 'excluded', + patch: { status: 'running', is_backgrounded: true, description: 'Excluded shell' } + }) + ) + expect.soft(run.records(), flag).toEqual([]) + expect + .soft( + [...run.journalItems.values()].filter( + (row) => + row.body.kind === 'message' && + row.body.blocks.some((block) => block.type === 'background-task') + ), + flag + ) + .toEqual([]) + run.send( + system('task_notification', { + task_id: 'excluded', + tool_use_id: 'tool-old', + status: 'stopped' + }) + ) + run.send(toolUse('tool-new', 'Bash', { command: 'sleep 1', run_in_background: true })) + run.send( + system('task_started', { + task_id: 'excluded', + task_type: 'local_bash', + tool_use_id: 'tool-new', + is_backgrounded: true + }) + ) + expect(run.records().find((row) => row.kind === 'command')).toMatchObject({ + membership: 'live' + }) + run.send( + system('task_progress', { + task_id: 'excluded', + tool_use_id: 'tool-pending', + parent_task_id: 'pending-owner', + [flag]: true + }) + ) + run.send( + system('task_notification', { + task_id: 'excluded', + tool_use_id: 'tool-new', + status: 'stopped' + }) + ) + run.send( + system('task_started', { + task_id: 'excluded', + task_type: 'local_bash', + tool_use_id: 'tool-pending', + is_backgrounded: true + }) + ) + expect(run.records().filter((row) => row.membership === 'live')).toEqual([]) + expect( + [...run.journalItems.values()].some( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.parentToolUseId === 'tool-pending' + ) + ) + ).toBe(false) + } +}) + +it('uses the matching durable generation ownership when an earlier run receives a terminal revision', async () => { + const root = await mkdtemp(join(tmpdir(), 'orca-parent-review-codex-')) + cleanup.push(root) + const journal = await journals.open({ + identity: { + sessionId: 'orca-session', + workspaceId: 'workspace-1', + hostId: 'local', + agent: 'claude', + providerHandle: claudeProviderHandle('claude-session', null) + }, + now: () => 9000, + stateDirectory: root + }) + for (const [generation, tool, owner] of [ + [1, 'tool-first', 'owner-first'], + [2, 'tool-second', 'owner-second'], + [3, 'tool-root', undefined] + ] as const) { + await journal.appendItem( + claudeBackgroundTaskIdentity('reused', generation), + claudeBackgroundTaskBody({ + type: 'background-task', + taskId: 'reused', + kind: 'command', + label: 'sleep', + parentToolUseId: tool, + state: 'unverifiable', + startedAt: 1 + }), + { + fence: 1, + turnScope: AGENT_JOURNAL_THREAD_SCOPE, + ...(owner + ? { + agentId: 'reused', + parentAgentId: owner, + producerKind: 'background' as const, + providerParentRef: tool + } + : {}) + } + ) + } + const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) + deferred.bind({ journal, fence: 1, publish: () => {} }) + const translator = createClaudeJournalTranslator({ sink: deferred.sink, coalesceMs: 0 }) + translator.handle({ + type: 'message', + sessionId: 'orca-session', + message: { + type: 'system', + subtype: 'task_notification', + task_id: 'reused', + tool_use_id: 'tool-first', + session_id: 'claude-session', + uuid: 'historical-owned-completion', + status: 'completed' + } + }) + expect(await deferred.drained()).toEqual({ ok: true }) + expect(journal.runningTurn()).toBeNull() + const prior = journal + .snapshot() + .items.find( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.parentToolUseId === 'tool-first' + ) + ) + translator.handle({ + type: 'message', + sessionId: 'orca-session', + message: { + type: 'system', + subtype: 'task_notification', + task_id: 'reused', + status: 'completed' + } + }) + expect(await deferred.drained()).toEqual({ ok: true }) + const rootRow = journal + .snapshot() + .items.find( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.parentToolUseId === 'tool-root' + ) + ) + expect(rootRow?.agentId).toBeUndefined() + expect(rootRow?.body.kind === 'message' ? rootRow.body.blocks : []).toContainEqual( + expect.objectContaining({ taskId: 'reused', parentToolUseId: 'tool-root', state: 'done' }) + ) + translator.handle({ + type: 'message', + sessionId: 'orca-session', + message: system('task_started', { + task_id: 'reused', + task_type: 'local_bash', + tool_use_id: 'tool-first', + parent_task_id: 'owner-first', + is_backgrounded: true + }) + }) + expect(await deferred.drained()).toEqual({ ok: true }) + const oldBody = journal + .snapshot() + .items.find((row) => row.providerParentRef === 'tool-first')?.body + expect(oldBody?.kind === 'message' ? oldBody.blocks : []).toContainEqual( + expect.objectContaining({ parentToolUseId: 'tool-first', state: 'done' }) + ) + translator.dispose() + deferred.close() + const queued = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) + const pending = createClaudeJournalTranslator({ sink: queued.sink, coalesceMs: 0 }) + const feed = (subtype: string, fields: Record) => + pending.handle({ + type: 'message', + sessionId: 'orca-session', + message: system(subtype, fields) + }) + feed('task_started', { + task_id: 'queued', + task_type: 'local_bash', + tool_use_id: 'queued-old', + parent_task_id: 'owner-first', + is_backgrounded: true + }) + feed('task_notification', { task_id: 'queued', tool_use_id: 'queued-old', status: 'stopped' }) + feed('task_progress', { + task_id: 'queued', + tool_use_id: 'queued-new', + description: 'New operation before announcement' + }) + feed('task_started', { + task_id: 'queued', + task_type: 'local_bash', + tool_use_id: 'queued-new', + parent_task_id: 'owner-second', + is_backgrounded: true + }) + feed('task_updated', { + task_id: 'queued', + tool_use_id: 'queued-old', + patch: { status: 'completed' } + }) + feed('task_progress', { + task_id: 'queued', + tool_use_id: 'queued-old', + description: 'Old operation' + }) + queued.bind({ journal, fence: 1, publish: () => {} }) + expect(await queued.drained()).toEqual({ ok: true }) + const queuedRows = journal + .snapshot() + .items.filter( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.taskId === 'queued' + ) + ) + expect(queuedRows).toHaveLength(2) + const current = queuedRows.find((row) => row.providerParentRef === 'queued-new') + expect(current).toMatchObject({ parentAgentId: 'owner-second' }) + expect(current?.body.kind === 'message' ? current.body.blocks : []).toContainEqual( + expect.objectContaining({ parentToolUseId: 'queued-new', state: 'working' }) + ) + pending.dispose() + queued.close() + expect(prior).toMatchObject({ parentAgentId: 'owner-first', providerParentRef: 'tool-first' }) +}) + +it('advances a journaled nested agent attempt rather than replacing it with the parent placeholder', async () => { + const root = await mkdtemp(join(tmpdir(), 'orca-nested-parent-recovery-')) + cleanup.push(root) + const journal = await journals.open({ + identity: { + sessionId: 'orca-session', + workspaceId: 'workspace-1', + hostId: 'local', + agent: 'claude', + providerHandle: claudeProviderHandle('claude-session', null) + }, + now: () => 9000, + stateDirectory: root + }) + await journal.appendItem( + { provider: 'claude', sessionId: 'claude-session', uuid: 'saved-attempt' }, + { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'previous attempt' }] }, + { + fence: 1, + turnScope: AGENT_JOURNAL_THREAD_SCOPE, + agentId: 'nested', + parentAgentId: 'owner', + producerKind: 'agent', + providerParentRef: 'tool-second', + attempt: 2 + } + ) + const journaled = new ClaudeJournaledSubagents(() => journal) + const identities = new ClaudeSubagentIdentities({ journaled }) + identities.observeSystemFrame({ + type: 'system', + subtype: 'background_tasks_changed', + tasks: [{ task_id: 'nested', task_type: 'local_agent', parent_task_id: 'owner' }] + }) + identities.observeSystemFrame({ + type: 'system', + subtype: 'task_started', + task_id: 'nested', + tool_use_id: 'tool-third', + task_type: 'local_agent', + parent_task_id: 'owner' + }) + expect(identities.linkage.settledLinkageFor('tool-third')).toMatchObject({ + kind: 'linked', + linkage: { attempt: 3 } + }) +}) + +it.each(['notification', 'killed', 'failed'])( + 'keeps distinct shell invocations isolated after %s', + async (ending) => { + const run = await producer() + run.send( + system('task_started', { + task_id: 'owner', + task_type: 'local_agent', + tool_use_id: 'spawn-owner', + is_backgrounded: true + }) + ) + run.send( + system('task_started', { + task_id: 'reused', + task_type: 'local_bash', + tool_use_id: 'tool-first', + parent_task_id: 'owner', + is_backgrounded: true + }) + ) + run.send( + ending === 'notification' + ? system('task_notification', { + task_id: 'reused', + tool_use_id: 'tool-first', + status: 'stopped' + }) + : system('task_updated', { task_id: 'reused', patch: { status: ending } }) + ) + run.send( + system('task_progress', { + task_id: 'reused', + tool_use_id: 'tool-root', + description: 'New operation before announcement' + }) + ) + expect(run.records().find((row) => row.kind === 'command')).toMatchObject({ + membership: 'settled', + stoppable: false + }) + expect( + [...run.journalItems.values()].filter( + (row) => + row.body.kind === 'message' && + row.body.blocks.some((block) => block.type === 'background-task') + ) + ).toHaveLength(1) + run.send(toolUse('tool-root', 'Bash', { command: 'sleep 1', run_in_background: true })) + run.send( + system('task_started', { + task_id: 'reused', + task_type: 'local_bash', + tool_use_id: 'tool-root', + is_backgrounded: true + }) + ) + run.send( + system('task_updated', { + task_id: 'reused', + tool_use_id: 'tool-first', + patch: { status: 'completed' } + }) + ) + run.send( + system('task_progress', { + task_id: 'reused', + tool_use_id: 'tool-first', + description: 'Old operation' + }) + ) + const newer = [...run.journalItems.values()].find( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.parentToolUseId === 'tool-root' + ) + ) + expect.soft(newer).toBeDefined() + expect.soft(newer?.agentId).toBeUndefined() + const owner = run.records().find((row) => row.kind === 'agent') + const child = run.records().find((row) => row.kind === 'command') + expect(child?.parentChildWorkId).not.toBe(owner?.childWorkId) + expect(child).toMatchObject({ membership: 'live', stoppable: true }) + expect(JSON.stringify(child)).not.toContain('Old operation') + expect(newer?.body.kind === 'message' ? newer.body.blocks : []).toContainEqual( + expect.objectContaining({ parentToolUseId: 'tool-root', state: 'working' }) + ) + } +) + +it.each([false, true])( + 'keeps Monitor invocation reports out of the transcript, reused ID: %s', + async (reused) => { + const run = await producer() + if (reused) { + run.send(toolUse('tool-old', 'Bash', { command: 'sleep 1', run_in_background: true })) + run.send( + system('task_started', { + task_id: 'monitor-task', + task_type: 'local_bash', + tool_use_id: 'tool-old', + is_backgrounded: true + }) + ) + run.send( + system('task_notification', { + task_id: 'monitor-task', + tool_use_id: 'tool-old', + status: 'completed' + }) + ) + } + run.send(toolUse('tool-monitor', 'Monitor', { command: 'watch' })) + run.send({ + ...toolResult('tool-monitor', 'Started'), + tool_use_result: { taskId: 'monitor-task' } + }) + run.send( + system('task_started', { + task_id: 'monitor-task', + task_type: 'local_bash', + tool_use_id: 'tool-monitor', + is_backgrounded: true + }) + ) + run.send( + system('background_tasks_changed', { + tasks: [{ task_id: 'monitor-task', task_type: 'local_bash', parent_task_id: 'owner' }] + }) + ) + expect( + [...run.journalItems.values()].filter( + (row) => + row.body.kind === 'message' && + row.body.blocks.some((block) => block.type === 'background-task') + ) + ).toHaveLength(reused ? 1 : 0) + expect(run.records().some((row) => row.membership === 'live')).toBe(true) + } +) + +it('retains pending ownership and terminal evidence without granting a partial frame live authority', async () => { + const run = await producer() + run.send( + system('task_started', { + task_id: 'reused', + task_type: 'local_bash', + tool_use_id: 'tool-old', + is_backgrounded: true + }) + ) + run.send( + system('task_progress', { + task_id: 'reused', + tool_use_id: 'tool-owned', + parent_task_id: 'owner' + }) + ) + run.send( + system('task_notification', { + task_id: 'reused', + tool_use_id: 'tool-old', + status: 'stopped' + }) + ) + run.send( + system('task_started', { + task_id: 'reused', + task_type: 'local_bash', + tool_use_id: 'tool-owned', + is_backgrounded: true + }) + ) + expect(run.records().find((row) => row.kind === 'command')).toMatchObject({ + membership: 'live', + stoppable: true + }) + expect( + [...run.journalItems.values()].find( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.parentToolUseId === 'tool-owned' + ) + ) + ).toMatchObject({ agentId: 'reused' }) + run.send( + system('task_notification', { + task_id: 'reused', + tool_use_id: 'tool-stopped-before-start', + status: 'stopped' + }) + ) + run.send( + system('task_notification', { + task_id: 'reused', + tool_use_id: 'tool-owned', + status: 'stopped' + }) + ) + run.send( + system('task_started', { + task_id: 'reused', + task_type: 'local_bash', + tool_use_id: 'tool-stopped-before-start', + is_backgrounded: true + }) + ) + expect(run.records().filter((row) => row.membership === 'live')).toEqual([]) + const stoppedBody = [...run.journalItems.values()].find( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => + block.type === 'background-task' && block.parentToolUseId === 'tool-stopped-before-start' + ) + )?.body + expect(stoppedBody?.kind === 'message' ? stoppedBody.blocks : []).toContainEqual( + expect.objectContaining({ parentToolUseId: 'tool-stopped-before-start', state: 'idle' }) + ) +}) + +it.each(['alias', 'no-alias', 'partial-alias', 'known-terminal'])( + 'admits a resumed agent only when the terminal did not identify that invocation: %s', + async (shape) => { + const run = await producer() + run.send( + system('task_notification', { + task_id: 'owner', + status: 'stopped', + ...(shape === 'known-terminal' ? { tool_use_id: 'spawn-owner' } : {}) + }) + ) + if (shape === 'partial-alias') { + run.send(system('task_progress', { task_id: 'owner', tool_use_id: 'spawn-owner' })) + } + run.send( + system('task_started', { + task_id: 'owner', + task_type: 'local_agent', + is_backgrounded: true, + ...(shape === 'no-alias' ? {} : { tool_use_id: 'spawn-owner' }) + }) + ) + run.send( + system('task_started', { + task_id: 'shell', + task_type: 'local_bash', + tool_use_id: 'shell-tool', + parent_task_id: 'owner', + is_backgrounded: true + }) + ) + const owner = run.records().find((row) => row.kind === 'agent') + const shell = run.records().find((row) => row.kind === 'command') + if (shape === 'known-terminal') { + expect(owner?.membership).not.toBe('live') + } else { + expect(owner).toMatchObject({ membership: 'live', stoppable: true }) + expect(shell).toMatchObject({ + membership: 'live', + stoppable: true, + parentChildWorkId: owner?.childWorkId + }) + } + } +) + +it('releases an alias-less roster against reopened ownership on binding without another provider frame', async () => { + const root = await mkdtemp(join(tmpdir(), 'orca-parent-binding-')) + cleanup.push(root) + const identity = identityFor() + const open = () => journals.open({ identity, now: () => 9000, stateDirectory: root }) + let journal = await open() + for (const id of ['saved-shell', 'saved-only']) { + await journal.appendItem( + claudeBackgroundTaskIdentity(id, 1), + claudeBackgroundTaskBody({ + type: 'background-task', + taskId: id, + kind: 'command', + label: 'sleep', + parentToolUseId: 'saved-tool', + state: 'unverifiable', + startedAt: 1 + }), + { + fence: 1, + turnScope: AGENT_JOURNAL_THREAD_SCOPE, + agentId: id, + parentAgentId: 'saved-owner', + producerKind: 'background', + providerParentRef: 'saved-tool' + } + ) + } + const originalId = journal.snapshot().items[0]!.itemId + await journals.closeAll() + journal = await open() + const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) + deferred.sink.appendItem( + { provider: 'claude', sessionId: 'saved-session', uuid: 'before-bind' }, + { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Earlier queued write' }] }, + { turnScope: AGENT_JOURNAL_THREAD_SCOPE } + ) + const claude = fakeClaude() + const evidence: AgentChildWorkEvidence[] = [] + const adapter = adapterAtPublishFor( + claude, + {}, + [], + [], + undefined, + undefined, + (_sessionId, batch) => evidence.push(...batch) + ) + await adapter.acquire({ identity, fence: 1, spawnToken: 'binding-test', events: deferred.sink }) + const roster = () => + claude.connections[0]!.handlers.onMessage?.( + system('background_tasks_changed', { + tasks: [{ task_id: 'saved-shell', task_type: 'local_bash' }] + }) + ) + roster() + claude.connections[0]!.handlers.onMessage?.( + frame({ + type: 'assistant', + message: { + id: 'held-assistant', + role: 'assistant', + content: [{ type: 'text', text: 'Later provider write' }] + } + }) + ) + expect(evidence.filter((edge) => edge.type === 'live')).toEqual([]) + deferred.bind({ journal, fence: 1, publish: () => {} }) + expect(await deferred.drained()).toEqual({ ok: true }) + expect(evidence).toContainEqual( + expect.objectContaining({ + type: 'live', + child: expect.objectContaining({ + ownerId: 'saved-owner', + stoppable: true + }) + }) + ) + const liveIds = evidence + .filter((edge) => edge.type === 'live') + .map((edge) => edge.child.handle.id) + expect(liveIds).toContain('saved-shell') + expect(liveIds).not.toContain('saved-only') + const queuedMessages = journal + .snapshot() + .items.filter( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => + block.type === 'text' && + (block.text === 'Earlier queued write' || block.text === 'Later provider write') + ) + ) + expect(queuedMessages.map((row) => row.body)).toMatchObject([ + { kind: 'message', blocks: [{ type: 'text', text: 'Earlier queued write' }] }, + { kind: 'message', blocks: [{ type: 'text', text: 'Later provider write' }] } + ]) + expect(queuedMessages[0]!.sequence).toBeLessThan(queuedMessages[1]!.sequence) + expect(journal.item(originalId)).toMatchObject({ + agentId: 'saved-shell', + parentAgentId: 'saved-owner', + providerParentRef: 'saved-tool' + }) + expect(journal.item(originalId)?.body).toMatchObject({ + kind: 'message', + blocks: expect.arrayContaining([ + expect.objectContaining({ + taskId: 'saved-shell', + parentToolUseId: 'saved-tool', + state: 'working' + }) + ]) + }) + roster() + expect(await deferred.drained()).toEqual({ ok: true }) + expect( + journal + .snapshot() + .items.filter( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.taskId === 'saved-shell' + ) + ) + ).toHaveLength(1) + expect(journal.snapshot().items.find((row) => row.agentId === 'saved-only')?.body).toMatchObject({ + kind: 'message', + blocks: expect.arrayContaining([expect.objectContaining({ state: 'unverifiable' })]) + }) + const retired = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) + const replacement = adapterAtPublishFor(claude) + await replacement.acquire({ + identity, + fence: 1, + spawnToken: 'retired-test', + events: retired.sink + }) + claude.connections[1]!.handlers.onMessage?.( + system('task_started', { + task_id: 'retired-shell', + task_type: 'local_bash', + is_backgrounded: true + }) + ) + await replacement.releaseAcquisition({ sessionId: identity.sessionId }) + retired.bind({ journal, fence: 1, publish: () => {} }) + expect(await retired.drained()).toEqual({ ok: true }) + expect( + journal + .snapshot() + .items.some( + (row) => + row.body.kind === 'message' && + row.body.blocks.some( + (block) => block.type === 'background-task' && block.taskId === 'retired-shell' + ) + ) + ).toBe(false) + retired.close() + await adapter.releaseAcquisition({ sessionId: identity.sessionId }) + deferred.close() +}) diff --git a/src/main/claude/claude-stream-json-connection.test.ts b/src/main/claude/claude-stream-json-connection.test.ts index 17b63de60f7..95c7ec08f6a 100644 --- a/src/main/claude/claude-stream-json-connection.test.ts +++ b/src/main/claude/claude-stream-json-connection.test.ts @@ -147,6 +147,20 @@ function processState(pid: number): 'running' | 'exited' { } describe('Claude stream-json connection', () => { + it('completes task Stop through the real SDK while semantic reading is paused', async () => { + const scenario = scriptScenario([HOLD_OPEN]) + const connection = await open(launchFor(scenario)) + await connection.initializationResult() + connection.pauseReading?.() + await connection.stopTask('owned-shell') + expect(scenario.readReport().controlRequests).toContainEqual( + expect.objectContaining({ + request: expect.objectContaining({ subtype: 'stop_task', task_id: 'owned-shell' }) + }) + ) + connection.resumeReading?.() + }) + it('settles a send only once the frame reached the child, and replays reach onMessage', async () => { const replay = { type: 'user', diff --git a/src/main/claude/claude-subagent-identities.ts b/src/main/claude/claude-subagent-identities.ts index 7cdd45d5c6c..ebafff0d1da 100644 --- a/src/main/claude/claude-subagent-identities.ts +++ b/src/main/claude/claude-subagent-identities.ts @@ -1,3 +1,4 @@ +import { ClaudeTaskInvocations } from './claude-task-invocations' import { BoundedMap } from '../../shared/bounded-map' import { ClaudeSubagentIds } from './claude-subagent-id-aliases' import type { ClaudeJournaledSubagents } from './claude-subagent-journaled-identities' @@ -19,6 +20,7 @@ export type ClaudeSubagentIdentitiesDeps = { /** Resolve grandchildren through the child that journaled their spawn call. */ childOwnerRefOf?: (toolUseId: string) => string | null journaled?: ClaudeJournaledSubagents + taskInvocations?: ClaudeTaskInvocations } export class ClaudeSubagentIdentities { @@ -26,9 +28,11 @@ export class ClaudeSubagentIdentities { private readonly known = new BoundedMap({ maxEntries: MAX_KNOWN_SUBAGENTS }) + readonly taskInvocations: ClaudeTaskInvocations readonly linkage: ClaudeSubagentLinkageSource & ClaudeAgentLinkageSource constructor(private readonly deps: ClaudeSubagentIdentitiesDeps) { + this.taskInvocations = deps.taskInvocations ?? new ClaudeTaskInvocations(deps.journaled) this.ids = new ClaudeSubagentIds(deps.journaled?.canonical) this.linkage = new ClaudeSubagentLinkage({ ids: this.ids, @@ -39,11 +43,16 @@ export class ClaudeSubagentIdentities { }, isForwardedParentTool: deps.isForwardedParentTool, childOwnerRefOf: deps.childOwnerRefOf, + parentTaskOf: this.parentOf, spawnRefOf: (canonicalId) => this.known.get(canonicalId)?.run ?? null }) } + parentOf = (id: string, run?: string): string | undefined => + this.taskInvocations.parentOf(id, run) + observeSystemFrame(message: Record): boolean { + this.taskInvocations.observe(message) const frame = readClaudeSubagentTaskFrame(message) if (!frame) { return false @@ -62,7 +71,10 @@ export class ClaudeSubagentIdentities { if (frame.toolUseId) { this.ids.alias(frame.toolUseId, frame.taskId) } - if (frame.announcesSubagent) { + if ( + frame.announcesSubagent && + this.taskInvocations.selection(message, frame.taskId)?.acceptsStart !== false + ) { this.announce(frame.taskId, frame.toolUseId) } return true diff --git a/src/main/claude/claude-subagent-journaled-identities.ts b/src/main/claude/claude-subagent-journaled-identities.ts index 7094a9b49c5..82b51b7d0f3 100644 --- a/src/main/claude/claude-subagent-journaled-identities.ts +++ b/src/main/claude/claude-subagent-journaled-identities.ts @@ -1,3 +1,10 @@ +import { + indexClaudeBackgroundTask, + selectClaudeBackgroundTask, + type ClaudeBackgroundTaskIndex, + type ClaudeBackgroundTaskDescriptor +} from './claude-background-task-identity' +import { isBackgroundTaskBlock } from '../../shared/native-chat/native-chat-types' import type { AgentJournalProducerLinkage } from '../../shared/agent-session/journal/agent-session-journal-types' import { namedSubagents } from '../../shared/native-chat/transcript/native-chat-subagent-items' import type { StructuredAgentSessionLinkageJournal } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-journal-views' @@ -6,6 +13,8 @@ import { isBoundedClaudeTaskId } from './claude-background-task-tracker' type Reading = { canonicalByToolUse: Map attemptByAgent: Map + parents: Map> + backgrounds: Map subagents: Set } @@ -23,6 +32,15 @@ export class ClaudeJournaledSubagents { /** Zero means no journaled attempt. */ attempt = (agentId: string): number => this.current()?.attemptByAgent.get(agentId) ?? 0 + parent = (taskId: string, run?: string): string | undefined => + (run === undefined + ? [...(this.current()?.parents.get(taskId)?.values() ?? [])].at(-1) + : this.current()?.parents.get(taskId)?.get(run) + )?.parentAgentId + + background = (taskId: string, run?: string): ClaudeBackgroundTaskDescriptor | undefined => + selectClaudeBackgroundTask(this.current()?.backgrounds.get(taskId), run) + isSubagent = (taskId: string): boolean => this.current()?.subagents.has(taskId) === true private current(): Reading | null { @@ -43,9 +61,24 @@ function read(journal: StructuredAgentSessionLinkageJournal): Reading { const reading: Reading = { canonicalByToolUse: new Map(), attemptByAgent: new Map(), + parents: new Map(), + backgrounds: new Map(), subagents: new Set() } - journal.visitItemsWithLinkage((_itemId, _sequence, body, linkage) => { + journal.visitItemsWithLinkage((itemId, _sequence, body, linkage) => { + if (linkage.agentId !== undefined && linkage.producerKind === 'agent') { + const parents = reading.parents.get(linkage.agentId) ?? new Map() + parents.delete(linkage.providerParentRef) + parents.set(linkage.providerParentRef, linkage) + reading.parents.set(linkage.agentId, parents) + } + if (body.kind === 'message') { + for (const block of body.blocks.filter(isBackgroundTaskBlock)) { + const index = reading.backgrounds.get(block.taskId) ?? { aliases: new Map() } + indexClaudeBackgroundTask(index, itemId, block.taskId, block.parentToolUseId, linkage) + reading.backgrounds.set(block.taskId, index) + } + } readAgentRow(reading, linkage) if (body.kind === 'message' && linkage.agentId === undefined) { for (const entry of body.blocks.flatMap(namedSubagents)) { diff --git a/src/main/claude/claude-subagent-linkage.ts b/src/main/claude/claude-subagent-linkage.ts index 8d6b221959d..ba8eed43498 100644 --- a/src/main/claude/claude-subagent-linkage.ts +++ b/src/main/claude/claude-subagent-linkage.ts @@ -31,6 +31,7 @@ export type ClaudeSubagentLinkageDeps = { isForwardedParentTool?: (toolUseId: string) => boolean /** Map sidechain tool calls to their child owner to identify grandchildren's parents. */ childOwnerRefOf?: (toolUseId: string) => string | null + parentTaskOf?: (canonicalId: string) => string | undefined spawnRefOf?: (canonicalId: string) => string | null } @@ -62,14 +63,16 @@ export class ClaudeSubagentLinkage linkageForAgent = (agentId: string): AgentJournalProducerLinkage => { const spawnRef = this.deps.spawnRefOf?.(agentId) ?? null if (spawnRef === null) { - return { agentId, producerKind: 'agent' } + const parentAgentId = this.deps.parentTaskOf?.(agentId) + return { agentId, producerKind: 'agent', ...(parentAgentId ? { parentAgentId } : {}) } } const { linkage } = this.settledLinkageFor(spawnRef) const { parentAgentId, ...rest } = linkage + const parent = this.deps.parentTaskOf?.(agentId) ?? parentAgentId return { ...rest, agentId, - ...(parentAgentId === undefined || parentAgentId === agentId ? {} : { parentAgentId }) + ...(parent === undefined || parent === agentId ? {} : { parentAgentId: parent }) } } @@ -125,6 +128,10 @@ export class ClaudeSubagentLinkage settled: boolean, depth: number ): ParentAgentVerdict { + const explicit = this.deps.parentTaskOf?.(this.deps.ids.canonical(parentToolUseId)) + if (explicit !== undefined) { + return { kind: 'known', agentId: explicit } + } const ownerRef = this.deps.childOwnerRefOf?.(parentToolUseId) ?? null if (ownerRef === null || depth >= MAX_PARENT_RESOLUTION_DEPTH) { return { kind: 'known' } diff --git a/src/main/claude/claude-subagent-resume-after-restart.test.ts b/src/main/claude/claude-subagent-resume-after-restart.test.ts index d2cc875ba42..049cc308d76 100644 --- a/src/main/claude/claude-subagent-resume-after-restart.test.ts +++ b/src/main/claude/claude-subagent-resume-after-restart.test.ts @@ -1,3 +1,5 @@ +import { PARENT_TASK_STOP } from './claude-parent-task-frames.test-fixture' +import { blockOf } from './claude-background-task-row-test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -102,10 +104,13 @@ async function openJournal(): Promise { }) } -function acquire(journal: AgentSessionJournal) { +function acquire(journal: AgentSessionJournal, delayed = false) { const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) const translator = createClaudeJournalTranslator({ sink: deferred.sink, coalesceMs: 0 }) - deferred.bind({ journal, fence: 1, publish: () => {} }) + const bind = () => deferred.bind({ journal, fence: 1, publish: () => {} }) + if (!delayed) { + bind() + } const settle = async (): Promise => { await expect(deferred.drained()).resolves.toEqual({ ok: true }) } @@ -115,7 +120,7 @@ function acquire(journal: AgentSessionJournal) { translator.dispose() deferred.close() } - return { translator, settle, exit } + return { translator, settle, exit, bind } } const proseRow = (journal: AgentSessionJournal, text: string): AgentJournalRenderItem | undefined => @@ -128,6 +133,85 @@ const proseRow = (journal: AgentSessionJournal, text: string): AgentJournalRende ) describe('a Claude subagent resumed after its provider restarted', () => { + it.each(['terminal-only', 'roster-first', 'unbound-roster-first', 'parent-omitting-roster'])( + 'retains one owned shell across close/reopen: %s', + async (order) => { + let journal = await openJournal() + const first = acquire(journal) + first.translator.handle(userTurn('parent-task-turn')) + // Contract: a parentless admission can learn ownership from a later roster without a body change. + first.translator.handle( + frame({ ...PARENT_TASK_STOP[3]!.frame, parent_task_id: undefined, tool_use_id: undefined }) + ) + await first.settle() + const beforeOwnership = journal + .snapshot() + .items.find((item) => blockOf(item.body)?.taskId === 'shell-child') + expect(beforeOwnership?.agentId).toBeUndefined() + first.translator.handle(frame(PARENT_TASK_STOP[2]!.frame)) + await first.settle() + expect(journal.item(beforeOwnership!.itemId)).toMatchObject({ + agentId: 'shell-child', + parentAgentId: 'agent-parent' + }) + first.translator.handle(frame(PARENT_TASK_STOP[3]!.frame)) + await first.settle() + const shell = journal + .snapshot() + .items.find((item) => blockOf(item.body)?.taskId === 'shell-child') + expect(shell).toMatchObject({ + agentId: 'shell-child', + parentAgentId: 'agent-parent', + producerKind: 'background', + providerParentRef: 'toolu_shell' + }) + await first.exit() + expect(blockOf(journal.item(shell!.itemId)?.body)?.state).toBe('unverifiable') + await journals.closeAll() + + journal = await openJournal() + const delayed = order === 'unbound-roster-first' + const resumed = acquire(journal, delayed) + if (!delayed) { + expect(resumed.translator.childTaskParent?.('shell-child')).toBe('agent-parent') + } + if (order === 'parent-omitting-roster') { + resumed.translator.handle( + frame({ + type: 'system', + subtype: 'background_tasks_changed', + tasks: [{ task_id: 'shell-child', task_type: 'local_bash' }] + }) + ) + await resumed.settle() + expect(journal.item(shell!.itemId)).toMatchObject({ parentAgentId: 'agent-parent' }) + expect(blockOf(journal.item(shell!.itemId)?.body)?.state).toBe('working') + } else if (order !== 'terminal-only') { + resumed.translator.handle(frame(PARENT_TASK_STOP[2]!.frame)) + resumed.translator.handle(frame(PARENT_TASK_STOP[3]!.frame)) + } + if (delayed) { + resumed.bind() + } + // Contract: a terminal revision must retain saved ownership even without a repeated start. + resumed.translator.handle(frame(PARENT_TASK_STOP[8]!.frame)) + await resumed.settle() + const shells = journal + .snapshot() + .items.filter((item) => blockOf(item.body)?.taskId === 'shell-child') + expect(shells).toHaveLength(1) + expect(shells[0]).toMatchObject({ + itemId: shell!.itemId, + agentId: 'shell-child', + parentAgentId: 'agent-parent', + producerKind: 'background', + providerParentRef: 'toolu_shell' + }) + expect(blockOf(shells[0]?.body)?.state).toBe('idle') + await resumed.exit() + } + ) + it('keeps the canonical id its first run was given, so one child stays one child', async () => { const journal = await openJournal() diff --git a/src/main/claude/claude-subagent-task-frames.ts b/src/main/claude/claude-subagent-task-frames.ts index 487756f83c4..89602d480ce 100644 --- a/src/main/claude/claude-subagent-task-frames.ts +++ b/src/main/claude/claude-subagent-task-frames.ts @@ -1,3 +1,4 @@ +import type { AgentChildWorkKind } from '../../shared/agent-status/child-work/agent-status-child-work' // Task IDs alone cannot distinguish agents from shells or workflows; task_type identifies the kind. import { @@ -63,3 +64,10 @@ export function readClaudeSubagentTaskFrame( excluded: announcement && !subagent } } + +/** Releases predating `task_type` still name an agent in `subagent_type`. */ +export function claudeStartedTaskKind(message: Record): AgentChildWorkKind { + return isClaudeSubagentTask(message) + ? 'agent' + : classifyClaudeBackgroundTaskKind(message.task_type) +} diff --git a/src/main/claude/claude-task-invocations.ts b/src/main/claude/claude-task-invocations.ts new file mode 100644 index 00000000000..fd417cb0f32 --- /dev/null +++ b/src/main/claude/claude-task-invocations.ts @@ -0,0 +1,165 @@ +import { record, taskAliasId, taskId } from './claude-background-task-frames' +import type { ClaudeBackgroundTaskDescriptor } from './claude-background-task-identity' + +export type ClaudeTaskInvocation = { + toolUseId?: string + parentTaskId?: string + /** Only current provider evidence may correct durable producer attribution. */ + reportedParentTaskId?: string + excluded: boolean + monitor: boolean + ended?: { status: string; toolUseId?: string } + /** Only accepted current evidence proves an alias is historical after replacement. */ + observed: boolean +} + +type Selection = { invocation: ClaudeTaskInvocation; current: boolean; acceptsStart: boolean } +type Task = { current: ClaudeTaskInvocation; aliases: Map } + +/** Acquisition-local provider facts; saved ownership never establishes a live task. */ +export class ClaudeTaskInvocations { + private readonly tasks = new Map() + private readonly observations = new WeakMap>() + + constructor( + private readonly saved?: { + background: (id: string, run?: string) => ClaudeBackgroundTaskDescriptor | undefined + parent: (id: string, run?: string) => string | undefined + } + ) {} + + observe(message: Record): void { + if (message.type !== 'system' || this.observations.has(message)) { + return + } + const selections = new Map() + this.observations.set(message, selections) + const roster = message.subtype === 'background_tasks_changed' + const entries = roster && Array.isArray(message.tasks) ? message.tasks : [message] + for (const entry of entries) { + const frame = record(entry) + const id = frame && taskId(frame) + if (!frame || !id) { + continue + } + const run = taskAliasId(frame.tool_use_id) ?? taskAliasId(record(frame.patch)?.tool_use_id) + const start = message.subtype === 'task_started' + const selection = this.select(id, run, start) + const { invocation, acceptsStart } = selection + if ((!start && !roster) || acceptsStart) { + invocation.observed ||= selection.current + invocation.toolUseId ??= run + invocation.reportedParentTaskId = + taskAliasId(frame.parent_task_id) ?? invocation.reportedParentTaskId + invocation.parentTaskId = invocation.reportedParentTaskId ?? invocation.parentTaskId + invocation.excluded ||= frame.ambient === true || frame.skip_transcript === true + if (start && acceptsStart) { + invocation.ended = undefined + } + } + if (message.subtype === 'task_notification') { + invocation.ended = { status: String(frame.status), toolUseId: invocation.toolUseId } + } + if (invocation.toolUseId) { + this.tasks.get(id)!.aliases.set(invocation.toolUseId, invocation) + } + selections.set(id, selection) + if (roster) { + this.observations.set(frame, new Map([[id, selection]])) + } + } + } + + selection(message: Record, id: string): Selection | undefined { + this.observe(message) + return this.observations.get(message)?.get(id) + } + + get(id: string, run?: string): ClaudeTaskInvocation | undefined { + const task = this.tasks.get(id) + return run === undefined + ? task?.current + : (task?.aliases.get(run) ?? + (task?.current.toolUseId === undefined ? task?.current : undefined)) + } + + parentOf = (id: string, run?: string): string | undefined => { + const task = this.tasks.get(id) + const invocation = run === undefined ? task?.current : task?.aliases.get(run) + return invocation + ? invocation.parentTaskId + : (this.saved?.background(id, run)?.linkage.parentAgentId ?? this.saved?.parent(id, run)) + } + + excludeMonitor(id: string, run?: string): void { + this.select(id, taskAliasId(run), true).invocation.monitor = true + } + + settle(id: string, status: unknown, run?: string): void { + const invocation = this.get(id, run) + if (invocation) { + invocation.ended = { status: String(status), toolUseId: invocation.toolUseId } + } + } + + settleFrame(id: string, message: Record): void { + this.settle( + id, + record(message.patch)?.status ?? message.status, + taskAliasId(message.tool_use_id) ?? taskAliasId(record(message.patch)?.tool_use_id) + ) + } + + clear(): void { + this.tasks.clear() + } + + private select(id: string, run: string | undefined, start: boolean): Selection { + let task = this.tasks.get(id) + if (!task) { + const latest = this.saved?.background(id) + const current = this.recover(id, latest ? undefined : run) + task = { current, aliases: new Map() } + if (current.toolUseId) { + task.aliases.set(current.toolUseId, current) + } + this.tasks.set(id, task) + } + let invocation = task.current + if (run !== undefined && invocation.toolUseId !== undefined && invocation.toolUseId !== run) { + const saved = this.saved?.background(id, run) + invocation = task.aliases.get(run) ?? this.recover(id, run) + // A known historical alias cannot replace the current invocation; a pending tool result can. + if ( + start && + !invocation.observed && + saved?.toolUseId !== run && + (task.current.ended !== undefined || !task.current.observed) + ) { + task.current = invocation + } + task.aliases.set(run, invocation) + } + const current = task.current === invocation + return { + invocation, + current, + acceptsStart: + current && + (invocation.ended === undefined || + (start && + (invocation.ended.status === 'completed' || invocation.ended.toolUseId === undefined))) + } + } + + private recover(id: string, run?: string): ClaudeTaskInvocation { + const descriptor = this.saved?.background(id, run) + return { + toolUseId: run ?? descriptor?.toolUseId, + parentTaskId: descriptor?.linkage.parentAgentId ?? this.saved?.parent(id, run), + observed: false, + excluded: false, + monitor: false + } + } +} diff --git a/src/main/claude/structured/claude-owned-task-send.test.ts b/src/main/claude/structured/claude-owned-task-send.test.ts new file mode 100644 index 00000000000..1f842127849 --- /dev/null +++ b/src/main/claude/structured/claude-owned-task-send.test.ts @@ -0,0 +1,116 @@ +import { expect, it } from 'vitest' +import { + adapterFor, + claudeFrame, + fakeClaude, + identityFor +} from './claude-structured-session-test-support' +import { PARENT_TASK_STOP } from '../claude-parent-task-frames.test-fixture' +import { createQueuedMessageTestRig } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture' +import { eventually } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture' +import { HOST_TEST_SESSION as SESSION } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +import type { AgentChildWorkEvidence } from '../../../shared/agent-status/child-work/agent-status-child-work-evidence' + +it('delivers an ordinary send after an owned roster/start refresh without inventing root work', async () => { + const claude = fakeClaude() + const evidence: AgentChildWorkEvidence[] = [] + const rig = await createQueuedMessageTestRig({ agent: 'claude' }) + const adapter = adapterFor( + claude, + {}, + [], + [], + undefined, + undefined, + (_sessionId, batch) => evidence.push(...batch), + (outcome) => { + void rig.host.settleLateDispatch({ ...outcome, sessionId: SESSION }) + } + ) + let acquired = false + try { + rig.dispatch.mockImplementation(async (input) => { + if (!acquired) { + await adapter.acquire({ + identity: identityFor(SESSION), + fence: 1, + spawnToken: 'spawn-1', + events: rig.providerEvents() + }) + acquired = true + } + return adapter.dispatch({ ...input, sessionId: SESSION, fence: 1 }) + }) + const first = rig.send('start the work') + expect(await first.result).toMatchObject({ ok: true }) + await eventually(async () => + expect((await rig.submission(first.id))?.dispatchState).toBe('accepted') + ) + const connection = claude.connections[0]! + claudeFrame(connection, { + type: 'assistant', + uuid: 'root-answer', + parent_tool_use_id: null, + message: { role: 'assistant', content: [{ type: 'text', text: 'Started.' }] } + }) + claudeFrame(connection, { + type: 'result', + subtype: 'success', + uuid: 'root-result', + parent_tool_use_id: null, + duration_ms: 1000 + }) + await rig.host.collaboratorsForTests().runtimeState.flushAllEventSinks() + expect(rig.host.currentWork(SESSION)!.working()).toBe(false) + + // Recorded CLI roster has no tool alias; the following start enriches that same task. + const roster = PARENT_TASK_STOP[2]!.frame + claudeFrame(connection, { + ...roster, + tasks: Array.isArray(roster.tasks) ? roster.tasks.slice(1) : [] + }) + claudeFrame(connection, PARENT_TASK_STOP[3]!.frame) + await rig.host.collaboratorsForTests().runtimeState.flushAllEventSinks() + const snapshot = await rig.host.journalSnapshot(SESSION) + const children = snapshot.items.filter( + (item) => + item.body.kind === 'message' && + item.body.blocks.some( + (block) => block.type === 'background-task' && block.taskId === 'shell-child' + ) + ) + expect(children).toHaveLength(1) + expect(children[0]).toMatchObject({ agentId: 'shell-child', parentAgentId: 'agent-parent' }) + expect(evidence).toContainEqual( + expect.objectContaining({ + type: 'live', + child: expect.objectContaining({ + handle: expect.objectContaining({ id: 'shell-child' }), + ownerId: 'agent-parent', + stoppable: true + }) + }) + ) + const rootAfterChild = rig.host + .collaboratorsForTests() + .sessions.get(SESSION)! + .journal.runningTurn() + + const followup = rig.send('continue normally') + expect(await followup.result).toMatchObject({ ok: true }) + await eventually(async () => + expect((await rig.submission(followup.id))?.handedOverAt).toBeDefined() + ) + expect(rootAfterChild).toBeNull() + expect(connection.sent).toHaveLength(2) + expect(connection.sent[1]).toMatchObject({ + message: { + content: [{ type: 'text', text: 'continue normally' }] + } + }) + } finally { + await adapter.closeAll() + await rig.dispose() + claude.dispose() + } +}) diff --git a/src/main/claude/structured/claude-structured-dispatch-test-support.ts b/src/main/claude/structured/claude-structured-dispatch-test-support.ts index 63850f38e96..28964e3371a 100644 --- a/src/main/claude/structured/claude-structured-dispatch-test-support.ts +++ b/src/main/claude/structured/claude-structured-dispatch-test-support.ts @@ -1,3 +1,4 @@ +import { ClaudeJournalBoundDelivery } from '../claude-journal-bound-delivery' import { vi, type Mock } from 'vitest' import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types' import { retireClaudeDispatchWaiters } from './claude-structured-dispatch' @@ -21,6 +22,7 @@ export function sessionFor(send: Mock = vi.fn().mockResolvedValue(undefined)): C dispatchWaiters: [], retiredDispatchWaiters: [], replayContentFallbackBlocked: false, + delivery: new ClaudeJournalBoundDelivery(), backgroundTasks: new ClaudeBackgroundTaskTracker(), childWork: new ClaudeChildWorkDecoder(), commands: new ClaudeSlashCommandCatalog(), diff --git a/src/main/claude/structured/claude-structured-event-delivery.ts b/src/main/claude/structured/claude-structured-event-delivery.ts index bf79ece6d23..0bd7e9189bf 100644 --- a/src/main/claude/structured/claude-structured-event-delivery.ts +++ b/src/main/claude/structured/claude-structured-event-delivery.ts @@ -30,6 +30,7 @@ export function emitClaudeStructuredSessionEvent({ deps.onEvent?.(event) } if (event.type === 'ended' || event.type === 'retiring') { + session?.delivery.dispose() session?.childWork.clear() session?.backgroundTasks.clear() } else if (event.type === 'message') { @@ -56,19 +57,18 @@ export function deliverClaudeConnectionEvent( exits: ReadonlyMap, attempt: ClaudeAcquisitionAttempt, sessionId: string, - event: () => void + event: () => void, + payload?: unknown ): void { - if (!attempt.published) { - attempt.buffered.push(event) - return - } - if (exits.get(sessionId)?.error instanceof ClaudePluginInputError) { - return - } - if ( - sessions.get(sessionId)?.connection === attempt.connection || - exits.get(sessionId)?.connection === attempt.connection - ) { - event() - } + attempt.delivery.deliver(() => { + if (attempt.cancelled || exits.get(sessionId)?.error instanceof ClaudePluginInputError) { + return + } + if ( + sessions.get(sessionId)?.connection === attempt.connection || + exits.get(sessionId)?.connection === attempt.connection + ) { + event() + } + }, payload) } diff --git a/src/main/claude/structured/claude-structured-session-acquisition.ts b/src/main/claude/structured/claude-structured-session-acquisition.ts index 47bb00a664d..68dc786dbb0 100644 --- a/src/main/claude/structured/claude-structured-session-acquisition.ts +++ b/src/main/claude/structured/claude-structured-session-acquisition.ts @@ -27,10 +27,7 @@ import { readClaudeTranscriptEntryUuid } from '../claude-transcript-entry-uuid' import { claudeTurnResumePointWriter } from './claude-structured-resume-point' import { withAgentSessionCreatePhase } from '../../observability/agent-session-instrumentation' import { resolveClaudeAcquisitionLaunch } from './launch/claude-structured-acquisition-launch' -import { - bindClaudeConnectionJournalControls, - createClaudeSessionJournalTranslator -} from './journal/claude-structured-session-journal-control' +import * as journal from './journal/claude-structured-session-journal-control' import { isRootClaudeFrame } from '../claude-turn-opening' import { createClaudeStartupOutcome, endsClaudeStartup } from './claude-structured-startup-outcome' import { claudeStartupResultError } from '../claude-stream-json-exit-error' @@ -70,7 +67,7 @@ export async function acquireClaudeSession({ initProof.reject(error) } const delivery = { attempt, initProof, callbacks, sessionId, sessions } - const translator = createClaudeSessionJournalTranslator( + const translator = journal.createClaudeSessionJournalTranslator( input.events, String(input.fence), delivery, @@ -81,6 +78,8 @@ export async function acquireClaudeSession({ const recordTurnEnd = claudeTurnResumePointWriter(sessionId, sessions, deps) const observedOptions: sessionState.ClaudeSession['reportedOptions'] = {} const handlePluginUi = claudePluginUiRecovery(delivery, () => liveSession) + const deliver = (payload: unknown, run: () => void): void => + callbacks.deliver(attempt, sessionId, run, payload) const onMessage = (message: Record): void => { if ( ignoresClaudeRetiredConnection(liveSession, sessions.get(sessionId), exits.get(sessionId)) @@ -144,7 +143,7 @@ export async function acquireClaudeSession({ liveSession, settlements, () => - callbacks.deliver(attempt, sessionId, () => { + deliver(message, () => { if (liveSession && (modelReport || permissionChanged)) { callbacks.emit(liveSession, input.events, { type: 'options-reported', @@ -175,7 +174,7 @@ export async function acquireClaudeSession({ ) } const emit = (event: Parameters[2]): void => - callbacks.deliver(attempt, sessionId, () => callbacks.emit(liveSession, input.events, event)) + deliver(event, () => callbacks.emit(liveSession, input.events, event)) try { const launch = await resolveClaudeAcquisitionLaunch({ @@ -230,11 +229,12 @@ export async function acquireClaudeSession({ ) attempt.connection = connection naming?.attach(connection) - unbindReadingControl = bindClaudeConnectionJournalControls( + unbindReadingControl = journal.bindClaudeConnectionJournalControls( input.events, connection, translator, - deps.now ? { now: deps.now } : {} + deps.now ? { now: deps.now } : {}, + attempt.delivery ) acquisitions.assertCurrent(sessionId, attempt) if (connection.pid === undefined) { @@ -262,6 +262,7 @@ export async function acquireClaudeSession({ leafUuid: observedLeafUuid, prompts, translator, + delivery: attempt.delivery, ...(unbindReadingControl ? { unbindReadingControl } : {}), process, initialization: { @@ -293,6 +294,7 @@ export async function acquireClaudeSession({ }) return acquisition } catch (error) { + attempt.delivery.dispose() unbindReadingControl?.() throw await resolveClaudeAcquisitionError({ error: await claudeStartupError(error, attempt.connection), diff --git a/src/main/claude/structured/claude-structured-session-adapter.ts b/src/main/claude/structured/claude-structured-session-adapter.ts index c72b025529e..08a6f150dfc 100644 --- a/src/main/claude/structured/claude-structured-session-adapter.ts +++ b/src/main/claude/structured/claude-structured-session-adapter.ts @@ -91,8 +91,15 @@ export class ClaudeStructuredSessionAdapter implements StructuredAgentSessionAda ...(naming ? { naming } : {}), callbacks: { publish: start.publish, - deliver: (attempt, sessionId, event) => - deliverClaudeConnectionEvent(this.sessions, this.exits, attempt, sessionId, event), + deliver: (attempt, sessionId, event, payload) => + deliverClaudeConnectionEvent( + this.sessions, + this.exits, + attempt, + sessionId, + event, + payload + ), emit: (session, _events, event) => this.emit(session, event), handleExit: (sessionId, attempt, error) => observeClaudeSessionExit(this.exitLifecycle, sessionId, attempt, error), diff --git a/src/main/claude/structured/claude-structured-session-close.ts b/src/main/claude/structured/claude-structured-session-close.ts index 4eb98131f6d..ff1154cdca3 100644 --- a/src/main/claude/structured/claude-structured-session-close.ts +++ b/src/main/claude/structured/claude-structured-session-close.ts @@ -86,6 +86,7 @@ export async function resolveClaudeAcquisitionError(input: { } export function settleClaudeExitedSession(session: ClaudeSession): void { + session.delivery.dispose() retireClaudeDispatchWaiters(session) settleClaudeTurnEndWaiters(session) for (const prompt of session.prompts.clear()) { @@ -113,6 +114,7 @@ async function finalizeClaudePublishedSession( input: CloseClaudePublishedSessionInput, session: ClaudeSession ): Promise { + session.delivery.dispose() settleClaudeTurnEndWaiters(session) // Null settles callbacks without writing responses during SDK cleanup. for (const prompt of session.prompts.clear()) { diff --git a/src/main/claude/structured/claude-structured-session-exit-lifecycle.ts b/src/main/claude/structured/claude-structured-session-exit-lifecycle.ts index f25a3e8465a..f8ca501b502 100644 --- a/src/main/claude/structured/claude-structured-session-exit-lifecycle.ts +++ b/src/main/claude/structured/claude-structured-session-exit-lifecycle.ts @@ -58,6 +58,7 @@ export function observeClaudeSessionExit( return } const observedAt = lifecycle.deps.now?.() ?? Date.now() + session.delivery.dispose() session.events?.observeExit?.(claudeHeldSends(session), observedAt) lifecycle.endings.record(sessionId, session.acquisitionGeneration, claudeExitEnding(error)) lifecycle.sessions.delete(sessionId) diff --git a/src/main/claude/structured/claude-structured-session-publication.ts b/src/main/claude/structured/claude-structured-session-publication.ts index b45af38b353..30aac7965ca 100644 --- a/src/main/claude/structured/claude-structured-session-publication.ts +++ b/src/main/claude/structured/claude-structured-session-publication.ts @@ -93,6 +93,7 @@ export function createClaudeSessionPublication(input: { leafUuid: string | null prompts: ClaudePromptRegistry translator: ClaudeJournalTranslator | null + delivery: ClaudeAcquisitionAttempt['delivery'] unbindReadingControl?: () => void process: AgentSessionAcquisition['process'] initialization: Omit< @@ -130,7 +131,7 @@ export function createClaudeSessionPublication(input: { retiredDispatchWaiters: [], replayContentFallbackBlocked: false, backgroundTasks: new ClaudeBackgroundTaskTracker(), - childWork: new ClaudeChildWorkDecoder(), + childWork: new ClaudeChildWorkDecoder(input.translator?.taskInvocations), // An unread catalog is unavailable, not empty. commands: new ClaudeSlashCommandCatalog(), dispatchSequence: 0, @@ -148,6 +149,7 @@ export function createClaudeSessionPublication(input: { ), fastModeAtStart: false, translator: input.translator, + delivery: input.delivery, events: input.request.events, ...(input.unbindReadingControl ? { unbindReadingControl: input.unbindReadingControl } : {}), link, @@ -208,9 +210,7 @@ export async function publishClaudeSessionAttempt({ await withAgentSessionCreatePhase('publish', recordPhase, async () => { callbacks.publish(session) attempt.published = true - for (const event of attempt.buffered.splice(0)) { - event() - } + attempt.delivery.flush() }) // Buffered exit delivery can remove the session during publication. if (sessions.get(sessionId) !== session) { diff --git a/src/main/claude/structured/claude-structured-session-state.ts b/src/main/claude/structured/claude-structured-session-state.ts index 5ced67286d1..c0fb4d1f620 100644 --- a/src/main/claude/structured/claude-structured-session-state.ts +++ b/src/main/claude/structured/claude-structured-session-state.ts @@ -13,6 +13,7 @@ import type { ClaudeStructuredLaunch } from './launch/claude-structured-launch-r import type { StructuredLaunchRequest } from '../../runtime/agent-session/launch/structured-agent-launch-composition' import type { ClaudeJournalTranslator } from '../claude-journal-translator-contract' import type { ClaudePromptRegistry } from '../claude-prompt-registry' +import { ClaudeJournalBoundDelivery } from '../claude-journal-bound-delivery' import { cancelProcessAcquisition } from '@orca/process-host/cancel-process-acquisition' import type { AgentSessionFastModeState } from '../../../shared/agent-session/agent-session-wire' import type { AgentChildWorkEvidence } from '../../../shared/agent-status/child-work/agent-status-child-work-evidence' @@ -172,6 +173,7 @@ export type ClaudeSession = { closeEnded?: boolean translator: ClaudeJournalTranslator | null events: StructuredAgentSessionEventSink | undefined + delivery: ClaudeJournalBoundDelivery unbindReadingControl?: () => void /** Provider identity and initialize proof retained for the host-awaited open. */ link: AgentSessionProviderHandleLink @@ -207,7 +209,7 @@ export type ClaudeSessionExit = { export type ClaudeAcquisitionAttempt = { connection: ClaudeStreamJsonConnection | null prompts: ClaudePromptRegistry - buffered: (() => void)[] + delivery: ClaudeJournalBoundDelivery published: boolean cancelled: boolean exitProven: boolean @@ -224,16 +226,17 @@ export function createClaudeAcquisitionAttempt( const finished = new Promise((resolve) => { finish = resolve }) - return { + const attempt: ClaudeAcquisitionAttempt = { connection: null, prompts, - buffered: [], + delivery: new ClaudeJournalBoundDelivery(undefined, () => attempt.published), published: false, cancelled: false, exitProven: false, finished, finish } + return attempt } export class ClaudeAcquisitionRegistry { @@ -304,6 +307,7 @@ export async function cancelClaudeAcquisitionAttempt( return cancelProcessAcquisition({ cancel: () => { attempt.cancelled = true + attempt.delivery.dispose() }, connection: () => attempt.connection, exitProven: () => attempt.exitProven, @@ -314,7 +318,12 @@ export async function cancelClaudeAcquisitionAttempt( export type ClaudeAcquireCallbacks = { /** Publishes the session, its start ending admitted first so its exit is never missed. */ publish: (session: ClaudeSession) => void - deliver: (attempt: ClaudeAcquisitionAttempt, sessionId: string, event: () => void) => void + deliver: ( + attempt: ClaudeAcquisitionAttempt, + sessionId: string, + event: () => void, + payload?: unknown + ) => void emit: ( session: ClaudeSession | null, events: StructuredAgentSessionEventSink | undefined, diff --git a/src/main/claude/structured/journal/claude-structured-journal-translation.ts b/src/main/claude/structured/journal/claude-structured-journal-translation.ts index 5fa4f06cf27..b9a7cd8de4e 100644 --- a/src/main/claude/structured/journal/claude-structured-journal-translation.ts +++ b/src/main/claude/structured/journal/claude-structured-journal-translation.ts @@ -1,7 +1,6 @@ -import type { - ClaudeJournalTranslator, - ClaudeJournalTranslatorDeps -} from '../../claude-journal-translator-contract' +import { ClaudeTaskInvocations } from '../../claude-task-invocations' +import { ClaudeJournalBoundDelivery } from '../../claude-journal-bound-delivery' +import type * as contract from '../../claude-journal-translator-contract' import { claudeStreamingMessageBody, type ClaudeToolUse @@ -46,10 +45,11 @@ import { } from '../../claude-plugin-input-failure-journal' export function createClaudeJournalTranslator( - deps: ClaudeJournalTranslatorDeps -): ClaudeJournalTranslator { + deps: contract.ClaudeJournalTranslatorDeps +): contract.ClaudeJournalTranslator { const coverage = createClaudeFinalOutputCoverage(deps.sink) deps = { ...deps, sink: coverage.sink } + const delivery = deps.delivery ?? new ClaudeJournalBoundDelivery(deps.sink) const tools = new Map() const turnScope = () => turn.turnScope const prompts = new ClaudeJournalPrompts({ @@ -70,7 +70,9 @@ export function createClaudeJournalTranslator( const providerFallback = createClaudeProviderFrameFallback(deps.sink, fallbackId, turnScope) const toolOrigins = new ClaudeToolOriginRegistry() const journaled = new ClaudeJournaledSubagents(() => deps.sink.journalLinkage?.() ?? null) + const taskInvocations = new ClaudeTaskInvocations(journaled) const subagents = new ClaudeSubagentIdentities({ + taskInvocations, isForwardedParentTool: (toolUseId) => toolOrigins.has(toolUseId), childOwnerRefOf: (toolUseId) => toolOrigins.childOwnerRef(toolUseId), journaled @@ -94,6 +96,7 @@ export function createClaudeJournalTranslator( sink: deps.sink, isForwardedParentTool: (toolUseId) => toolOrigins.has(toolUseId), rosteredByEarlierRun: journaled.isSubagent, + taskInvocations, // Task output must open a resumed turn so the session does not appear idle while producing rows. openOutputTurn: (frame, observedAt) => turn.ensureOpen(frame, claudeStreamTurnSource(frame), observedAt), @@ -158,99 +161,100 @@ export function createClaudeJournalTranslator( backgroundTasks.settleSession() settleClaudeInterruptedTurn(turn, observedAt, failed) } - return { - handle: withClaudeConversationContent((event) => { - if (event.type === 'retiring') { - journalClaudePluginInputFailure({ event, turn, sink: deps.sink, finish }) - return - } - if (event.type === 'ended') { - finish(event.observedAt ?? Date.now(), event.failure?.kind === 'pluginInputRequired') - return - } - turn.observeStartedSend(event.type === 'message' ? (event.startedSend ?? null) : null) - if (event.type === 'message') { - context.observe(event.message, event.observedAt ?? Date.now()) - // Root init starts a cycle; replays after root work fold into it, while task frames do not mark cycle work. - if (isRootClaudeFrame(event.message)) { - if (event.message.type === 'system' && event.message.subtype === 'init') { - turn.observeProviderCycleStart() - } else if ( - event.startsTurn === true || - event.message.type === 'assistant' || - event.message.type === 'stream_event' - ) { - turn.observeProviderCycleWork() - turn.inputChoiceWorkObserved ||= - event.message.type !== 'assistant' || - event.message.is_api_error_message !== true || - event.startedSend != null - } - } - } - if (event.type === 'message' && absorbClaudeCommandFrame(messageContext, event.message)) { - return - } - if ( - event.type === 'message' && - journalClaudeStream(messageContext, event.message, event.observedAt ?? Date.now()) - ) { - return - } - // Observe announcements before flushing so buffered rows receive their resolved producer stamp. - const announced = event.type === 'message' && subagents.observeSystemFrame(event.message) - // Only ahead of a frame that can write a row: one per thinking token rewrote the whole row. - if (!(event.type === 'message' && isClaudeProgressFrame(event.message))) { - flush() - } - if (announced) { - corrections.retry() - streamedText.reattribute() - streamedThinking.reattribute() - } - if (event.type === 'prompt') { - prompts.handle(event) - } else if (event.type === 'prompt-cancelled') { - prompts.retryPendingCancellations() - prompts.cancel(event.promptKey) - } else if (event.type === 'message' && event.message.type === 'result') { - journalClaudeResult(resultContext, event.message, event.observedAt ?? Date.now()) - } else if (event.type === 'message') { - backgroundTasks.observe(event.message, event.observedAt ?? Date.now()) - messageContext.usageLimits.observe(event.message, turn.command?.rejections) - const kind = claudeProviderFrameKind(event.message) - if ( - !journalClaudeMessage( - messageContext, - event.message, - event.startsTurn === true, - event.observedAt ?? Date.now(), - event.requestedAt, - event.clientMessageId - ) + const handle = withClaudeConversationContent((event) => { + if (event.type === 'retiring') { + journalClaudePluginInputFailure({ event, turn, sink: deps.sink, finish }) + return + } + if (event.type === 'ended') { + finish(event.observedAt ?? Date.now(), event.failure?.kind === 'pluginInputRequired') + return + } + turn.observeStartedSend(event.type === 'message' ? (event.startedSend ?? null) : null) + if (event.type === 'message') { + context.observe(event.message, event.observedAt ?? Date.now()) + // Root init starts a cycle; replays after root work fold into it, while task frames do not mark cycle work. + if (isRootClaudeFrame(event.message)) { + if (event.message.type === 'system' && event.message.subtype === 'init') { + turn.observeProviderCycleStart() + } else if ( + event.startsTurn === true || + event.message.type === 'assistant' || + event.message.type === 'stream_event' ) { - providerFallback.append( - kind, - event.message, - undefined, - undefined, - undefined, - corrections.stampFor(claudeFrameParentRef(event.message)) - ) + turn.observeProviderCycleWork() + turn.inputChoiceWorkObserved ||= + event.message.type !== 'assistant' || + event.message.is_api_error_message !== true || + event.startedSend != null } - context.observeResponse(event.message, event.observedAt ?? Date.now()) - publishClaudeTurnActivity(turn, deps.sink, event) - // Idle also closes faults without a result; the live cause decides failed reopening. - if (claudeSessionStateEndsTurn(event.message)) { - corrections.abandon() - // No verdict: the CLI said the turn is over, not how it ended. - turn.settle({ state: 'completed', completedAt: event.observedAt ?? Date.now() }) - } - } else if (event.type === 'provider-frame') { - providerFallback.append(event.kind, event.payload) - publishClaudeTurnActivity(turn, deps.sink, event) } - }), + } + if (event.type === 'message' && absorbClaudeCommandFrame(messageContext, event.message)) { + return + } + if ( + event.type === 'message' && + journalClaudeStream(messageContext, event.message, event.observedAt ?? Date.now()) + ) { + return + } + // Observe announcements before flushing so buffered rows receive their resolved producer stamp. + const announced = event.type === 'message' && subagents.observeSystemFrame(event.message) + // Only ahead of a frame that can write a row: one per thinking token rewrote the whole row. + if (!(event.type === 'message' && isClaudeProgressFrame(event.message))) { + flush() + } + if (announced) { + corrections.retry() + streamedText.reattribute() + streamedThinking.reattribute() + } + if (event.type === 'prompt') { + prompts.handle(event) + } else if (event.type === 'prompt-cancelled') { + prompts.retryPendingCancellations() + prompts.cancel(event.promptKey) + } else if (event.type === 'message' && event.message.type === 'result') { + journalClaudeResult(resultContext, event.message, event.observedAt ?? Date.now()) + } else if (event.type === 'message') { + backgroundTasks.observe(event.message, event.observedAt ?? Date.now()) + messageContext.usageLimits.observe(event.message, turn.command?.rejections) + const kind = claudeProviderFrameKind(event.message) + if ( + !journalClaudeMessage( + messageContext, + event.message, + event.startsTurn === true, + event.observedAt ?? Date.now(), + event.requestedAt, + event.clientMessageId + ) + ) { + providerFallback.append( + kind, + event.message, + undefined, + undefined, + undefined, + corrections.stampFor(claudeFrameParentRef(event.message)) + ) + } + context.observeResponse(event.message, event.observedAt ?? Date.now()) + publishClaudeTurnActivity(turn, deps.sink, event) + // Idle also closes faults without a result; the live cause decides failed reopening. + if (claudeSessionStateEndsTurn(event.message)) { + corrections.abandon() + // No verdict: the CLI said the turn is over, not how it ended. + turn.settle({ state: 'completed', completedAt: event.observedAt ?? Date.now() }) + } + } else if (event.type === 'provider-frame') { + providerFallback.append(event.kind, event.payload) + publishClaudeTurnActivity(turn, deps.sink, event) + } + }) + return { + handle: (event) => delivery.interpret(event, handle), journalPrompts: prompts, get currentTurnId() { return turn.id @@ -269,6 +273,8 @@ export function createClaudeJournalTranslator( return turn.isOpen && turn.inputChoiceWorkObserved }, flush, + taskInvocations, + childTaskParent: subagents.parentOf, childToolOwner: childQueries.childToolOwner, childActivity: childQueries.childActivity, retryPendingTaskRows: () => backgroundTasks.retryPendingWrites(), @@ -288,6 +294,7 @@ export function createClaudeJournalTranslator( modelMayHaveChanged: () => context.modelMayHaveChanged(), modelWritten: (model) => context.modelWritten(model), dispose: () => { + delivery.dispose() flush() context.dispose() streamedText.dispose() diff --git a/src/main/claude/structured/journal/claude-structured-session-journal-control.ts b/src/main/claude/structured/journal/claude-structured-session-journal-control.ts index 06cd38da791..374e73737dc 100644 --- a/src/main/claude/structured/journal/claude-structured-session-journal-control.ts +++ b/src/main/claude/structured/journal/claude-structured-session-journal-control.ts @@ -10,6 +10,7 @@ import type { ClaudeStreamJsonConnection } from '../../claude-stream-json-connec import type { ClaudeJournalTranslator } from '../../claude-journal-translator-contract' import { createClaudeJournalTranslator } from './claude-structured-journal-translation' import type { ClaudeInitProof } from '../claude-structured-init-proof' +import type { ClaudeJournalBoundDelivery } from '../../claude-journal-bound-delivery' import type { ClaudeAcquisitionAttempt, ClaudeSession, @@ -24,9 +25,12 @@ export function createClaudeSessionJournalTranslator( acceptStartupFailure: (message: Record) => boolean ): ClaudeJournalTranslator | null { const { attempt } = failure + attempt.delivery.onFailure = createClaudeJournalFailureHandler(failure) + attempt.delivery.bind(sink) return sink ? createClaudeJournalTranslator({ sink, + delivery: attempt.delivery, account: () => attempt.account, model: () => { const session = currentSession() @@ -83,13 +87,30 @@ function retryOwedWritesOnDrain( export function bindClaudeJournalReadingControl( sink: StructuredAgentSessionEventSink | undefined, connection: Pick, - translator: ClaudeJournalTranslator | null + translator: ClaudeJournalTranslator | null, + delivery?: ClaudeJournalBoundDelivery ): (() => void) | undefined { if (!connection.pauseReading || !connection.resumeReading) { return undefined } let sinkPaused = false - return sink?.bindReadingControl?.({ + let journalPaused = false + const resume = () => { + if (!sinkPaused && !journalPaused) { + connection.resumeReading?.() + } + } + const unbindDelivery = delivery?.bindReadingControl({ + pauseReading: () => { + journalPaused = true + connection.pauseReading?.() + }, + resumeReading: () => { + journalPaused = false + resume() + } + }) + const unbindSink = sink?.bindReadingControl?.({ pauseReading: () => { sinkPaused = true connection.pauseReading?.() @@ -98,20 +119,25 @@ export function bindClaudeJournalReadingControl( sinkPaused = false const retried = retryOwedWritesOnDrain(translator) if (!sinkPaused && (retried.accepted || retried.reason !== 'backpressure')) { - connection.resumeReading?.() + resume() } } }) + return () => { + unbindDelivery?.() + unbindSink?.() + } } export function bindClaudeConnectionJournalControls( sink: StructuredAgentSessionEventSink | undefined, connection: ClaudeStreamJsonConnection, translator: ClaudeJournalTranslator | null, - capture: ClaudeContextUsageCaptureOptions + capture: ClaudeContextUsageCaptureOptions, + delivery?: ClaudeJournalBoundDelivery ): (() => void) | undefined { const unbinds = [ - bindClaudeJournalReadingControl(sink, connection, translator), + bindClaudeJournalReadingControl(sink, connection, translator, delivery), bindClaudeContextUsageCapture(connection, translator, capture) ].filter((unbind): unbind is () => void => unbind !== undefined) return unbinds.length === 0 diff --git a/src/main/claude/structured/launch/claude-structured-options.test.ts b/src/main/claude/structured/launch/claude-structured-options.test.ts index e9954a2d159..0caf086fbf8 100644 --- a/src/main/claude/structured/launch/claude-structured-options.test.ts +++ b/src/main/claude/structured/launch/claude-structured-options.test.ts @@ -1,3 +1,4 @@ +import { ClaudeJournalBoundDelivery } from '../../claude-journal-bound-delivery' import { describe, expect, it, vi } from 'vitest' import { setClaudeStructuredOption } from './claude-structured-options' import type { ClaudeSession } from '../claude-structured-session-state' @@ -25,6 +26,7 @@ function sessionFor(setModel: ClaudeSession['connection']['setModel']): ClaudeSe dispatchWaiters: [], retiredDispatchWaiters: [], replayContentFallbackBlocked: false, + delivery: new ClaudeJournalBoundDelivery(), backgroundTasks: new ClaudeBackgroundTaskTracker(), childWork: new ClaudeChildWorkDecoder(), commands: new ClaudeSlashCommandCatalog(), diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink-queue.ts b/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink-queue.ts index 40c82e530c5..777553f8c7f 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink-queue.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink-queue.ts @@ -49,6 +49,7 @@ export class StructuredAgentSessionSinkQueue { private readonly buffered: Admitted[] = [] private readonly waitingPublications = new Map() private readonly waiters: StructuredAgentSessionDrainWaiter[] = [] + private readonly boundListeners = new Set<() => void>() constructor( private readonly deps: { @@ -76,6 +77,16 @@ export class StructuredAgentSessionSinkQueue { journalLinkage = (): StructuredAgentSessionLinkageJournal | null => this.target?.journal ?? null + onJournalBound = (listener: () => void): (() => void) => { + if (!this.closed) { + this.boundListeners.add(listener) + if (this.target) { + listener() + } + } + return () => this.boundListeners.delete(listener) + } + journalHistory = (): StructuredAgentSessionHistoryJournal | null => this.target?.journal ?? null journalItems = (): StructuredAgentSessionTransitionJournal | null => this.target?.journal ?? null @@ -104,6 +115,13 @@ export class StructuredAgentSessionSinkQueue { for (const operation of this.buffered.splice(0)) { this.handOver(operation, target) } + for (const listener of this.boundListeners) { + try { + listener() + } catch (error) { + this.fail(error) + } + } } unbind(): void { @@ -112,6 +130,7 @@ export class StructuredAgentSessionSinkQueue { close(): void { this.closed = true + this.boundListeners.clear() this.dropBuffered() this.updateBackpressure() } diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink.ts b/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink.ts index 1f3fec98a57..31a4e93c384 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink.ts @@ -125,6 +125,8 @@ export type StructuredAgentSessionEventSink = StructuredAgentSessionTransitionSi ): StructuredAgentSessionSinkAdmission journalEpoch?(): string | null journalLinkage?(): StructuredAgentSessionLinkageJournal | null + /** Deferred sinks notify after binding; an already-bound listener runs immediately. */ + onJournalBound?(listener: () => void): () => void journalHistory?(): StructuredAgentSessionHistoryJournal | null /** Apply the journal’s personal-Stop cancellation rule; false until bound. */ journalStopDecidesTurn?(turnId: string, endedAt: number): boolean @@ -268,6 +270,7 @@ export function createDeferredStructuredAgentSessionEventSink(deps: { ...createStructuredAgentSessionTransitionMembers(queue), journalEpoch: queue.journalEpoch, journalLinkage: queue.journalLinkage, + onJournalBound: queue.onJournalBound, journalHistory: queue.journalHistory, journalStopDecidesTurn: queue.journalStopDecidesTurn, appendLifecycleBatch: (settlementId, mutations) => { diff --git a/src/main/native-chat/agent-session-wire/publication/history/agent-session-history-subagent-roster.test.ts b/src/main/native-chat/agent-session-wire/publication/history/agent-session-history-subagent-roster.test.ts new file mode 100644 index 00000000000..57c49408903 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/publication/history/agent-session-history-subagent-roster.test.ts @@ -0,0 +1,43 @@ +import { expect, it } from 'vitest' +import type { AgentJournalRenderItem } from '../../../../../shared/agent-session/journal/agent-session-journal-types' +import { offPageSubagentRoster } from './agent-session-history-subagent-roster' + +it('supplies the off-page owner name for a page containing only its shell task', () => { + const entry = { id: 'owner', label: 'Audit', state: 'stopped' as const } + const owner: AgentJournalRenderItem = { + itemId: 'owner-row', + sequence: 1, + revision: 1, + observedAt: 1, + turnScope: { kind: 'thread' }, + body: { + kind: 'message', + role: 'assistant', + blocks: [{ ...entry, type: 'subagent', groupId: 'turn' }] + } + } + const shell: AgentJournalRenderItem = { + itemId: 'shell-row', + sequence: 2, + revision: 1, + observedAt: 2, + turnScope: { kind: 'thread' }, + agentId: 'shell', + parentAgentId: 'owner', + producerKind: 'background', + body: { + kind: 'message', + role: 'assistant', + blocks: [ + { + type: 'background-task', + taskId: 'shell', + kind: 'command', + label: 'sleep', + state: 'working' + } + ] + } + } + expect(offPageSubagentRoster([owner, shell], [shell])).toMatchObject([{ entry }]) +}) diff --git a/src/shared/agent-session/journal/agent-session-journal-producer.test.ts b/src/shared/agent-session/journal/agent-session-journal-producer.test.ts new file mode 100644 index 00000000000..07106f8d23e --- /dev/null +++ b/src/shared/agent-session/journal/agent-session-journal-producer.test.ts @@ -0,0 +1,32 @@ +import { expect, it } from 'vitest' +import { + agentJournalItemSubagentId, + isRootAgentJournalItem +} from './agent-session-journal-producer' +import type { NativeChatBlock } from '../../native-chat/native-chat-types' + +it('groups only a background producer own task row under its owner on both journal and message readers', () => { + const linkage = { agentId: 'shell', parentAgentId: 'owner', producerKind: 'background' as const } + const task: NativeChatBlock = { + type: 'background-task', + taskId: 'shell', + kind: 'command', + label: 'sleep', + state: 'working' + } + const output: NativeChatBlock = { type: 'text', text: 'workflow output' } + for (const [blocks, expected] of [ + [[task], 'owner'], + [[output], 'shell'], + [[{ ...task, taskId: 'another' }], 'shell'] + ] as const) { + expect(agentJournalItemSubagentId({ ...linkage, blocks })).toBe(expected) + const journal = { + ...linkage, + body: { kind: 'message' as const, role: 'assistant' as const, blocks: [...blocks] } + } + expect(agentJournalItemSubagentId(journal)).toBe(expected) + expect(isRootAgentJournalItem(journal)).toBe(false) + } + expect(isRootAgentJournalItem({})).toBe(true) +}) diff --git a/src/shared/agent-session/journal/agent-session-journal-producer.ts b/src/shared/agent-session/journal/agent-session-journal-producer.ts index ffdb3867fa4..18e06e7d3fe 100644 --- a/src/shared/agent-session/journal/agent-session-journal-producer.ts +++ b/src/shared/agent-session/journal/agent-session-journal-producer.ts @@ -1,5 +1,6 @@ // Root means no producer id, never a matching value. First-write attribution survives revisions without linkage; all producers share one session timeline. +import type { NativeChatBlock } from '../../native-chat/native-chat-types' import type { AgentJournalProducerLinkage, AgentJournalRenderItem @@ -12,11 +13,23 @@ export function isRootAgentJournalItem( return agentJournalItemSubagentId(item) === null } -/** The subagent that produced a row, or null when the session's own agent did. +type ProducerItem = AgentJournalProducerLinkage & { + blocks?: readonly NativeChatBlock[] + body?: AgentJournalRenderItem['body'] +} + +/** The subagent section for a row, or null when the session's own agent produced it. * Same reading of absence as {@link isRootAgentJournalItem}. */ -export function agentJournalItemSubagentId( - item: Pick | undefined -): string | null { +export function agentJournalItemSubagentId(item: ProducerItem | undefined): string | null { + const blocks = item?.blocks ?? (item?.body?.kind === 'message' ? item.body.blocks : []) + if ( + item?.agentId !== undefined && + item.producerKind === 'background' && + item.parentAgentId !== undefined && + blocks.some((block) => block.type === 'background-task' && block.taskId === item.agentId) + ) { + return item.parentAgentId + } return item?.agentId ?? null } diff --git a/src/shared/native-chat/transcript/native-chat-transcript-projection.test.ts b/src/shared/native-chat/transcript/native-chat-transcript-projection.test.ts index 232893a127f..0128029e0f6 100644 --- a/src/shared/native-chat/transcript/native-chat-transcript-projection.test.ts +++ b/src/shared/native-chat/transcript/native-chat-transcript-projection.test.ts @@ -78,6 +78,37 @@ describe("a subagent's rows are not the conversation's", () => { row('answer', say('The review found nothing.')) ] + it('places background work inside its owner even when the owner output is windowed out', () => { + const shell = row( + 'shell', + [ + { + type: 'background-task', + taskId: 'shell-child', + kind: 'command', + label: 'sleep', + state: 'working', + startedAt: 1 + } + ], + { agentId: 'shell-child', parentAgentId: 'agent-parent', producerKind: 'background' } + ) + for (const ownerRows of [ + [], + [row('owner', say('working'), { agentId: 'agent-parent', producerKind: 'agent' })] + ]) { + const projected = projectNativeChatTranscript([...ownerRows, shell]) + expect(projected.conversation).toEqual([]) + expect([...projected.subagentRows.keys()]).toEqual(['agent-parent']) + expect( + projected.subagentRows + .get('agent-parent') + ?.flatMap(({ message }) => message.blocks) + .some((block) => block.type === 'background-task' && block.taskId === 'shell-child') + ).toBe(true) + } + }) + it("keeps the session's own rows as the conversation, and none of the subagent's", () => { const { conversation, subagentRows } = projectNativeChatTranscript(transcript) expect(conversation.map((message) => message.id)).toEqual(['ask', 'delegate', 'answer']) From c804008e4f464a563d152b0b353d3a67a158d542 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:34:12 -0800 Subject: [PATCH 05/28] Show friendly advice for Codex turn failures (#39959) * Show friendly Codex turn failure advice * Match terminal failure callback to status row type * Keep terminal diagnostics in Details and clarify unknown failure advice * Preserve usage-limit diagnostic metadata * Validate custom host advice in phone renderer fixture * Check categorized compaction failures and retained diagnostics * Complete localized model-unavailable notices * test(mobile): keep the failure notice helpers main's test cleanup removed --- mobile/src/i18n/en-runtime-required.json | 6 +- mobile/src/i18n/locales/de.json | 4 + mobile/src/i18n/locales/en.json | 4 + mobile/src/i18n/locales/es.json | 4 + mobile/src/i18n/locales/fr.json | 4 + mobile/src/i18n/locales/ja.json | 4 + mobile/src/i18n/locales/ko.json | 4 + mobile/src/i18n/locales/pt.json | 4 + mobile/src/i18n/locales/ru.json | 4 + mobile/src/i18n/locales/tr.json | 4 + mobile/src/i18n/locales/zh-Hant.json | 4 + mobile/src/i18n/locales/zh.json | 4 + .../agent-session-send-failure-copy.ts | 21 ++ ...leNativeChatMessage.status-notices.test.ts | 70 +++++ .../transcript/MobileNativeChatNoticeRow.tsx | 10 +- .../structured/catalog/codex-usage-limits.ts | 8 +- .../journal/codex-provider-retry-row.test.ts | 12 +- ...codex-structured-journal-generic-frames.ts | 61 +++-- .../codex-structured-journal-translation.ts | 18 +- .../journal/codex-turn-failures.test.ts | 216 ++++++++++++++++ .../structured/journal/codex-turn-failures.ts | 240 ++++++++++++++++++ .../turns/codex-command-turn-claim.test.ts | 22 +- .../NativeChatNoticeRow.failures.test.tsx | 69 +++++ .../notices/NativeChatNoticeRow.fixture.tsx | 45 ++++ .../messages/notices/NativeChatNoticeRow.tsx | 13 +- .../agent-session-failure-words-text.ts | 2 + ...ent-session-provider-failure-words-text.ts | 31 +++ .../src/i18n/en-runtime-required.json | 4 + src/renderer/src/i18n/locales/de.json | 5 + src/renderer/src/i18n/locales/en.json | 4 + src/renderer/src/i18n/locales/es.json | 5 + src/renderer/src/i18n/locales/fr.json | 5 + src/renderer/src/i18n/locales/ja.json | 5 + src/renderer/src/i18n/locales/ko.json | 5 + src/renderer/src/i18n/locales/pt.json | 5 + src/renderer/src/i18n/locales/ru.json | 5 + src/renderer/src/i18n/locales/tr.json | 5 + src/renderer/src/i18n/locales/zh-Hant.json | 4 + src/renderer/src/i18n/locales/zh.json | 4 + .../failures/agent-session-failure-copy.ts | 7 + .../failures/agent-session-failure-words.ts | 14 +- .../failures/agent-session-failure.ts | 6 + .../agent-session-model-failure-words.ts | 19 ++ 43 files changed, 932 insertions(+), 58 deletions(-) create mode 100644 src/main/codex/structured/journal/codex-turn-failures.test.ts create mode 100644 src/main/codex/structured/journal/codex-turn-failures.ts create mode 100644 src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.failures.test.tsx create mode 100644 src/renderer/src/components/native-chat/messages/notices/agent-session-provider-failure-words-text.ts create mode 100644 src/shared/agent-session/failures/agent-session-model-failure-words.ts diff --git a/mobile/src/i18n/en-runtime-required.json b/mobile/src/i18n/en-runtime-required.json index acba365e2f1..ff6c952994d 100644 --- a/mobile/src/i18n/en-runtime-required.json +++ b/mobile/src/i18n/en-runtime-required.json @@ -315,7 +315,11 @@ "forkOriginalMissingNamed": "{{agent}} could not find this response in \"{{detail}}\". Open that chat and fork from there.", "forkOriginalUnavailable": "{{agent}} cannot fork this response in the original chat. Open it and fork from there.", "forkOriginalUnavailableNamed": "{{agent}} cannot fork this response in \"{{detail}}\". Open that chat and fork from there.", - "pluginInputRequired": "{{agent}} stopped because the \"{{plugin}}\" plugin is waiting for input in a terminal pane. Disable the plugin for chat sessions, or run the task in a {{agent}} terminal." + "pluginInputRequired": "{{agent}} stopped because the \"{{plugin}}\" plugin is waiting for input in a terminal pane. Disable the plugin for chat sessions, or run the task in a {{agent}} terminal.", + "providerResponseFailed": "{{agent}} couldn't finish this response. Open Details for more information.", + "providerSafetyStopped": "{{agent}} stopped this response after a safety check. Review your request and send again.", + "providerUnavailable": "{{agent}} couldn't respond because the service is busy. Pick another model, or wait a little and send again.", + "selectedModel": "the selected model" }, "fileReader": { "reviewNotes_one": "{{count}} review note", diff --git a/mobile/src/i18n/locales/de.json b/mobile/src/i18n/locales/de.json index 48043e5b8b3..93c90b1859d 100644 --- a/mobile/src/i18n/locales/de.json +++ b/mobile/src/i18n/locales/de.json @@ -2153,6 +2153,10 @@ "providerExitedRejection": "{{agent}} wurde gestoppt, bevor diese Nachricht gesendet wurde.", "providerExitedRow": "{{agent}} wurde während dieser Antwort gestoppt. Die Unterhaltung kann fortgesetzt werden.", "providerRateLimited": "{{agent}} hat das Ratenlimit erreicht und versucht es erneut.", + "providerUnavailable": "{{agent}} konnte nicht antworten, weil der Dienst ausgelastet ist. Wähle ein anderes Modell oder warte kurz und sende erneut.", + "providerSafetyStopped": "{{agent}} hat diese Antwort nach einer Sicherheitsprüfung gestoppt. Überprüfe deine Anfrage und sende sie erneut.", + "providerResponseFailed": "{{agent}} konnte diese Antwort nicht abschließen. Öffnen Sie die Details für weitere Informationen.", + "selectedModel": "das ausgewählte Modell", "providerRejected": "{{agent}} hat diese Nachricht nicht angenommen.", "providerRejectedQuoted": "{{agent}} hat diese Nachricht nicht angenommen: {{detail}}.", "providerRetryLastError": "Letzter Fehler: {{detail}}.", diff --git a/mobile/src/i18n/locales/en.json b/mobile/src/i18n/locales/en.json index 46765ee3df1..5e37b9c0859 100644 --- a/mobile/src/i18n/locales/en.json +++ b/mobile/src/i18n/locales/en.json @@ -2638,6 +2638,10 @@ "modelUnavailable": "{{agent}} couldn't use {{detail}}. Pick another model and send again.", "modelUnavailableUpdate": "{{agent}} couldn't use {{detail}}. Update {{agent}} to {{minimumVersion}} or later to keep it after a restart, or pick another model and send again.", "modelNotSelected": "{{agent}} has no model selected. Check your model selection and {{agent}} sign-in, then send again.", + "providerUnavailable": "{{agent}} couldn't respond because the service is busy. Pick another model, or wait a little and send again.", + "providerSafetyStopped": "{{agent}} stopped this response after a safety check. Review your request and send again.", + "providerResponseFailed": "{{agent}} couldn't finish this response. Open Details for more information.", + "selectedModel": "the selected model", "providerRejected": "{{agent}} didn't accept this message.", "providerRejectedQuoted": "{{agent}} didn't accept this message: {{detail}}.", "attachmentEmpty": "An image on this message is empty, so the message was not sent.", diff --git a/mobile/src/i18n/locales/es.json b/mobile/src/i18n/locales/es.json index 158cd39f958..23bea482416 100644 --- a/mobile/src/i18n/locales/es.json +++ b/mobile/src/i18n/locales/es.json @@ -2221,6 +2221,10 @@ "providerExitedRejection": "{{agent}} se detuvo antes de que se enviara este mensaje.", "providerExitedRow": "{{agent}} se detuvo mientras esta respuesta estaba en curso. Puede continuar en esta conversación.", "providerRateLimited": "{{agent}} alcanzó un límite de solicitudes y está reintentando.", + "providerUnavailable": "{{agent}} no pudo responder porque el servicio está ocupado. Elige otro modelo o espera un poco y vuelve a enviar el mensaje.", + "providerSafetyStopped": "{{agent}} detuvo esta respuesta tras una comprobación de seguridad. Revisa tu solicitud y vuelve a enviarla.", + "providerResponseFailed": "{{agent}} no pudo terminar esta respuesta. Abre los detalles para obtener más información.", + "selectedModel": "el modelo seleccionado", "providerRejected": "{{agent}} no aceptó este mensaje.", "providerRejectedQuoted": "{{agent}} no aceptó este mensaje: {{detail}}.", "providerRetryLastError": "Último error: {{detail}}.", diff --git a/mobile/src/i18n/locales/fr.json b/mobile/src/i18n/locales/fr.json index c7f2225a033..030a9037c45 100644 --- a/mobile/src/i18n/locales/fr.json +++ b/mobile/src/i18n/locales/fr.json @@ -2222,6 +2222,10 @@ "providerExitedRejection": "{{agent}} s'est arrêté avant l'envoi de ce message.", "providerExitedRow": "{{agent}} s'est arrêté pendant que cette réponse était en cours. Vous pouvez continuer dans cette conversation.", "providerRateLimited": "{{agent}} est limité en débit et réessaie.", + "providerUnavailable": "{{agent}} n’a pas pu répondre car le service est occupé. Choisissez un autre modèle ou patientez un peu, puis renvoyez le message.", + "providerSafetyStopped": "{{agent}} a arrêté cette réponse après un contrôle de sécurité. Vérifiez votre demande et renvoyez-la.", + "providerResponseFailed": "{{agent}} n’a pas pu terminer cette réponse. Ouvrez les détails pour en savoir plus.", + "selectedModel": "le modèle sélectionné", "providerRejected": "{{agent}} n'a pas accepté ce message.", "providerRejectedQuoted": "{{agent}} n'a pas accepté ce message : {{detail}}.", "providerRetryLastError": "Dernière erreur : {{detail}}.", diff --git a/mobile/src/i18n/locales/ja.json b/mobile/src/i18n/locales/ja.json index ed93625b9f0..c6410cd1e67 100644 --- a/mobile/src/i18n/locales/ja.json +++ b/mobile/src/i18n/locales/ja.json @@ -2149,6 +2149,10 @@ "providerExitedRejection": "このメッセージが送信される前に {{agent}} が停止しました。", "providerExitedRow": "この応答の途中で {{agent}} が停止しました。この会話で続けることができます。", "providerRateLimited": "{{agent}} はレート制限を受けているため、再試行しています。", + "providerUnavailable": "サービスが混雑しているため、{{agent}} は応答できませんでした。別のモデルを選ぶか、しばらく待ってから再送してください。", + "providerSafetyStopped": "{{agent}} は安全確認の後、この応答を停止しました。リクエストを見直して再送してください。", + "providerResponseFailed": "{{agent}} はこの応答を完了できませんでした。詳細を開いて確認してください。", + "selectedModel": "選択されたモデル", "providerRejected": "{{agent}} はこのメッセージを受け付けませんでした。", "providerRejectedQuoted": "{{agent}} はこのメッセージを受け付けませんでした: {{detail}}。", "providerRetryLastError": "直近のエラー: {{detail}}。", diff --git a/mobile/src/i18n/locales/ko.json b/mobile/src/i18n/locales/ko.json index 108a7a20015..2b166821479 100644 --- a/mobile/src/i18n/locales/ko.json +++ b/mobile/src/i18n/locales/ko.json @@ -2149,6 +2149,10 @@ "providerExitedRejection": "이 메시지가 전송되기 전에 {{agent}}이(가) 중지되었습니다.", "providerExitedRow": "이 응답이 진행되는 동안 {{agent}}이(가) 중지되었습니다. 이 대화에서 계속할 수 있습니다.", "providerRateLimited": "{{agent}}이(가) 속도 제한에 걸려 다시 시도하고 있습니다.", + "providerUnavailable": "서비스가 혼잡하여 {{agent}}이(가) 응답하지 못했습니다. 다른 모델을 선택하거나 잠시 기다린 후 다시 보내세요.", + "providerSafetyStopped": "{{agent}}이(가) 안전 검사 후 이 응답을 중단했습니다. 요청을 검토한 후 다시 보내세요.", + "providerResponseFailed": "{{agent}}이(가) 이 응답을 완료하지 못했습니다. 자세한 내용을 열어 확인하세요.", + "selectedModel": "선택한 모델", "providerRejected": "{{agent}}이(가) 이 메시지를 받아들이지 않았습니다.", "providerRejectedQuoted": "{{agent}}이(가) 이 메시지를 받아들이지 않았습니다: {{detail}}.", "providerRetryLastError": "마지막 오류: {{detail}}.", diff --git a/mobile/src/i18n/locales/pt.json b/mobile/src/i18n/locales/pt.json index 675f0e6c313..ea08a696d58 100644 --- a/mobile/src/i18n/locales/pt.json +++ b/mobile/src/i18n/locales/pt.json @@ -2221,6 +2221,10 @@ "providerExitedRejection": "{{agent}} parou antes de esta mensagem ser enviada.", "providerExitedRow": "{{agent}} parou enquanto esta resposta estava em andamento. Você pode continuar nesta conversa.", "providerRateLimited": "{{agent}} atingiu o limite de requisições e está tentando novamente.", + "providerUnavailable": "{{agent}} não conseguiu responder porque o serviço está ocupado. Escolha outro modelo ou aguarde um pouco e envie novamente.", + "providerSafetyStopped": "{{agent}} interrompeu esta resposta após uma verificação de segurança. Revise sua solicitação e envie novamente.", + "providerResponseFailed": "{{agent}} não conseguiu concluir esta resposta. Abra os detalhes para obter mais informações.", + "selectedModel": "o modelo selecionado", "providerRejected": "{{agent}} não aceitou esta mensagem.", "providerRejectedQuoted": "{{agent}} não aceitou esta mensagem: {{detail}}.", "providerRetryLastError": "Último erro: {{detail}}.", diff --git a/mobile/src/i18n/locales/ru.json b/mobile/src/i18n/locales/ru.json index 97b5c0be44f..b3753c99614 100644 --- a/mobile/src/i18n/locales/ru.json +++ b/mobile/src/i18n/locales/ru.json @@ -2289,6 +2289,10 @@ "providerExitedRejection": "{{agent}} остановился до отправки этого сообщения.", "providerExitedRow": "{{agent}} остановился во время ответа. Вы можете продолжить этот разговор.", "providerRateLimited": "{{agent}} повторяет попытку из-за ограничения частоты запросов.", + "providerUnavailable": "{{agent}} не смог ответить, поскольку сервис занят. Выберите другую модель или немного подождите и отправьте сообщение снова.", + "providerSafetyStopped": "{{agent}} остановил этот ответ после проверки безопасности. Проверьте запрос и отправьте его снова.", + "providerResponseFailed": "{{agent}} не удалось завершить этот ответ. Откройте подробности, чтобы узнать больше.", + "selectedModel": "выбранную модель", "providerRejected": "{{agent}} не принял это сообщение.", "providerRejectedQuoted": "{{agent}} не принял это сообщение: {{detail}}.", "providerRetryLastError": "Последняя ошибка: {{detail}}.", diff --git a/mobile/src/i18n/locales/tr.json b/mobile/src/i18n/locales/tr.json index 9fd904d49c4..69e6eb64810 100644 --- a/mobile/src/i18n/locales/tr.json +++ b/mobile/src/i18n/locales/tr.json @@ -2153,6 +2153,10 @@ "providerExitedRejection": "Bu mesaj gönderilmeden önce {{agent}} durdu.", "providerExitedRow": "Bu yanıt hazırlanırken {{agent}} durdu. Bu konuşmada devam edebilirsiniz.", "providerRateLimited": "{{agent}} istek hızı sınırına ulaştı ve yeniden deniyor.", + "providerUnavailable": "Hizmet meşgul olduğu için {{agent}} yanıt veremedi. Başka bir model seçin veya biraz bekleyip yeniden gönderin.", + "providerSafetyStopped": "{{agent}}, bir güvenlik kontrolünden sonra bu yanıtı durdurdu. İsteğinizi gözden geçirip yeniden gönderin.", + "providerResponseFailed": "{{agent}} bu yanıtı tamamlayamadı. Daha fazla bilgi için ayrıntıları açın.", + "selectedModel": "seçilen modeli", "providerRejected": "{{agent}} bu mesajı kabul etmedi.", "providerRejectedQuoted": "{{agent}} bu mesajı kabul etmedi: {{detail}}.", "providerRetryLastError": "Son hata: {{detail}}.", diff --git a/mobile/src/i18n/locales/zh-Hant.json b/mobile/src/i18n/locales/zh-Hant.json index 6512122f33a..7fad8bd6227 100644 --- a/mobile/src/i18n/locales/zh-Hant.json +++ b/mobile/src/i18n/locales/zh-Hant.json @@ -2149,6 +2149,10 @@ "providerExitedRejection": "{{agent}} 在傳送此訊息前已停止。", "providerExitedRow": "{{agent}} 在產生此回覆時停止了。您可以在此對話中繼續。", "providerRateLimited": "{{agent}} 受到速率限制,正在重試。", + "providerUnavailable": "{{agent}} 因服務忙碌而無法回覆。請選擇其他模型,或稍候片刻後重新傳送。", + "providerSafetyStopped": "{{agent}} 在安全檢查後停止了此次回覆。請檢查您的請求後重新傳送。", + "providerResponseFailed": "{{agent}} 未能完成此回覆。開啟詳細資料以取得更多資訊。", + "selectedModel": "所選模型", "providerRejected": "{{agent}} 未接受此訊息。", "providerRejectedQuoted": "{{agent}} 未接受此訊息:{{detail}}。", "providerRetryLastError": "上次錯誤:{{detail}}。", diff --git a/mobile/src/i18n/locales/zh.json b/mobile/src/i18n/locales/zh.json index 9982692931e..1d9441b494c 100644 --- a/mobile/src/i18n/locales/zh.json +++ b/mobile/src/i18n/locales/zh.json @@ -2149,6 +2149,10 @@ "providerExitedRejection": "{{agent}} 在发送此消息前已停止。", "providerExitedRow": "{{agent}} 在生成此回复时停止了。您可以在此对话中继续。", "providerRateLimited": "{{agent}} 已被限流,正在重试。", + "providerUnavailable": "{{agent}} 因服务繁忙而无法回复。请选择其他模型,或稍等片刻后重新发送。", + "providerSafetyStopped": "{{agent}} 在安全检查后停止了此次回复。请检查您的请求后重新发送。", + "providerResponseFailed": "{{agent}} 未能完成此回复。打开详情了解更多信息。", + "selectedModel": "所选模型", "providerRejected": "{{agent}} 未接受此消息。", "providerRejectedQuoted": "{{agent}} 未接受此消息:{{detail}}。", "providerRetryLastError": "上次错误:{{detail}}。", diff --git a/mobile/src/session/agent-session-send-failure-copy.ts b/mobile/src/session/agent-session-send-failure-copy.ts index 1564774d712..0a10fcac96f 100644 --- a/mobile/src/session/agent-session-send-failure-copy.ts +++ b/mobile/src/session/agent-session-send-failure-copy.ts @@ -1,3 +1,4 @@ +import { AGENT_SESSION_FAILURE_COPY } from '../../../src/shared/agent-session/failures/agent-session-failure-copy' import type { AgentSessionFailureCopyId, AgentSessionFailureCopyValues @@ -53,6 +54,26 @@ export const SEND_FAILURE_COPY = { "{{agent}} couldn't use {{detail}}. Update {{agent}} to {{minimumVersion}} or later to keep it after a restart, or pick another model and send again.", values ), + selectedModel: (values) => + translate('session.failure.selectedModel', AGENT_SESSION_FAILURE_COPY.selectedModel, values), + providerUnavailable: (values) => + translate( + 'session.failure.providerUnavailable', + AGENT_SESSION_FAILURE_COPY.providerUnavailable, + values + ), + providerSafetyStopped: (values) => + translate( + 'session.failure.providerSafetyStopped', + AGENT_SESSION_FAILURE_COPY.providerSafetyStopped, + values + ), + providerResponseFailed: (values) => + translate( + 'session.failure.providerResponseFailed', + AGENT_SESSION_FAILURE_COPY.providerResponseFailed, + values + ), providerRejected: (values) => translate('session.failure.providerRejected', "{{agent}} didn't accept this message.", values), providerRejectedQuoted: (values) => diff --git a/mobile/src/session/native-chat/transcript/MobileNativeChatMessage.status-notices.test.ts b/mobile/src/session/native-chat/transcript/MobileNativeChatMessage.status-notices.test.ts index a6e395f3ff7..8adab75b691 100644 --- a/mobile/src/session/native-chat/transcript/MobileNativeChatMessage.status-notices.test.ts +++ b/mobile/src/session/native-chat/transcript/MobileNativeChatMessage.status-notices.test.ts @@ -2,11 +2,18 @@ import { createElement, type ReactNode } from 'react' import { act, create, type ReactTestRenderer } from 'react-test-renderer' import { afterEach, describe, expect, it, vi } from 'vitest' import type { AgentJournalStatusItem } from '../../../../../src/shared/agent-session/journal/agent-session-journal-types' +import { isAdmissibleAgentJournalItemBody } from '../../../../../src/shared/agent-session/journal/agent-session-journal-schemas' import { projectStructuredItemToNativeChat } from '../../../../../src/shared/agent-session/view/structured-agent-session-projection' import type { NativeChatMessage } from '../../../../../src/shared/native-chat/native-chat-types' +import { agentSessionFailureFact } from '../../../../../src/shared/agent-session/failures/agent-session-failure' +import { agentSessionFailureWords } from '../../../../../src/shared/agent-session/failures/agent-session-failure-words' import { colors } from '../../../theme/mobile-theme' import { activateMobileUiLocale } from '../../../i18n/mobile-i18n' import { MarkdownLinkHandlerContext } from '../../../components/markdown-link-handler' +import { + MobileNativeChatNoticeContext, + type MobileNativeChatNoticeView +} from '../../mobile-native-chat-notice-context' vi.mock('react-native', async () => { const React = await import('react') @@ -95,6 +102,69 @@ describe('MobileNativeChatMessage status notices', () => { observedAt: 2, body })! + const renderNotice = (body: AgentJournalStatusItem, names: MobileNativeChatNoticeView) => { + act(() => { + renderer = create( + createElement( + MobileNativeChatNoticeContext.Provider, + { value: names }, + createElement(MobileNativeChatMessage, { message: statusRow(body) }) + ) + ) + }) + return renderer!.root.findAll( + (node) => String(node.type) === 'Text' && node.props.selectable === true + )[0]! + } + + it('localizes a canonical failure and discloses its diagnostic only on request', async () => { + await activateMobileUiLocale('fr') + const diagnostic = 'Internal provider diagnostic: modelNotFound' + const notice = renderNotice( + { + kind: 'status', + tone: 'error', + ...agentSessionFailureWords(agentSessionFailureFact('modelUnavailable'), { + agentName: 'Codex', + surface: 'row' + }), + providerFrame: { + provider: 'codex', + kind: 'notification:error', + payload: { + head: diagnostic, + byteLength: diagnostic.length, + digest: 'fixture', + truncated: false + } + } + }, + { hostName: null, agentName: 'Codex' } + ) + expect(String(notice.props.children)).toContain('le modèle sélectionné') + expect(String(notice.props.children)).not.toContain(diagnostic) + const details = () => + renderer!.root.findAll( + (node) => String(node.type) === 'Text' && String(node.props.children).includes(diagnostic) + ) + expect(details()).toHaveLength(0) + const disclosure = renderer!.root.find( + (node) => String(node.type) === 'Pressable' && node.props['aria-expanded'] === false + ) + act(() => disclosure.props.onPress()) + expect(details().length).toBeGreaterThan(0) + act(() => renderer?.unmount()) + const hostAdvice: unknown = { + kind: 'status', + text: 'Custom host advice: use the enterprise model.', + failure: agentSessionFailureFact('modelUnavailable') + } + if (!isAdmissibleAgentJournalItemBody(hostAdvice) || hostAdvice.kind !== 'status') { + throw new Error('Expected host status row') + } + const custom = renderNotice(hostAdvice, { hostName: null, agentName: 'Codex' }) + expect(custom.props.children).toBe('Custom host advice: use the enterprise model.') + }) it('links a CI result line to its pull request and commit through the link sheet', () => { const openLink = vi.fn() diff --git a/mobile/src/session/native-chat/transcript/MobileNativeChatNoticeRow.tsx b/mobile/src/session/native-chat/transcript/MobileNativeChatNoticeRow.tsx index b849b08da44..ac094b90491 100644 --- a/mobile/src/session/native-chat/transcript/MobileNativeChatNoticeRow.tsx +++ b/mobile/src/session/native-chat/transcript/MobileNativeChatNoticeRow.tsx @@ -73,7 +73,15 @@ function noticeLineText( // Only reword text fully described by its fact; other host text stays as written. const context = { agentName: names.agentName } const surface = - failure?.kind === 'savedConversationMissing' || failure?.kind === 'pluginInputRequired' + failure && + [ + 'savedConversationMissing', + 'pluginInputRequired', + 'modelUnavailable', + 'providerUnavailable', + 'providerSafetyStopped', + 'providerResponseFailed' + ].includes(failure.kind) ? (['row', 'rejection'] as const).find( (candidate) => notice.text === agentSessionFailureSentence(failure, candidate, context) ) diff --git a/src/main/codex/structured/catalog/codex-usage-limits.ts b/src/main/codex/structured/catalog/codex-usage-limits.ts index 491657029dc..ca4bfd0c639 100644 --- a/src/main/codex/structured/catalog/codex-usage-limits.ts +++ b/src/main/codex/structured/catalog/codex-usage-limits.ts @@ -49,8 +49,12 @@ export class CodexUsageLimits { } failure(payload: unknown) { - const error = readCodexErrorInfo(payload)?.error - if (!error || !USAGE_LIMIT_ERRORS.includes(error)) { + const info = readCodexErrorInfo(payload) + if ( + !info || + (!USAGE_LIMIT_ERRORS.includes(info.error) && + !(info.error === 'responseTooManyFailedAttempts' && info.status === 429)) + ) { return null } const message = readCodexErrorMessage(payload) diff --git a/src/main/codex/structured/journal/codex-provider-retry-row.test.ts b/src/main/codex/structured/journal/codex-provider-retry-row.test.ts index d6d98dffaf3..22510166c37 100644 --- a/src/main/codex/structured/journal/codex-provider-retry-row.test.ts +++ b/src/main/codex/structured/journal/codex-provider-retry-row.test.ts @@ -166,14 +166,16 @@ describe('a Codex stream error it is about to retry', () => { 'stream disconnected before completion: error sending request for url (http://127.0.0.1:9/v1/responses)' const errorFrameRows = reduced(rows).flatMap((row) => row.body.kind === 'status' && row.body.providerFrame?.kind === 'notification:error' - ? [{ tone: row.body.tone, text: row.body.text }] + ? [row.body] : [] ) - expect(errorFrameRows).toEqual([ - { tone: 'warning', text: `Codex is retrying: Reconnecting... 1/2.\n${cause}` }, - { tone: 'warning', text: `Codex is retrying: Reconnecting... 2/2.\n${cause}` }, - { tone: 'error', text: cause } + expect(errorFrameRows).toMatchObject([ + { tone: 'warning', failure: { kind: 'providerRetrying' } }, + { tone: 'warning', failure: { kind: 'providerRetrying' } }, + { tone: 'error', failure: { kind: 'providerResponseFailed' } } ]) + expect(errorFrameRows[2]?.text).not.toContain(cause) + expect(errorFrameRows[2]?.providerFrame?.payload.head).toContain(cause) expect(reduced(rows).map((row) => row.body)).toContainEqual( expect.objectContaining({ kind: 'turn', state: 'completed', outcome: 'failure' }) ) diff --git a/src/main/codex/structured/journal/codex-structured-journal-generic-frames.ts b/src/main/codex/structured/journal/codex-structured-journal-generic-frames.ts index 4e4c73c68e6..55f95da2325 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-generic-frames.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-generic-frames.ts @@ -28,9 +28,7 @@ import { isWordlessProviderFrame } from '../../../../shared/native-chat/native-chat-provider-frame-summary' import type { CodexRowAttribution } from '../background-work/codex-subagent-linkage' -import { codexAuthenticationFailure } from '../session-lifecycle/codex-authentication-failure' -import { agentSessionFailureWords } from '../../../../shared/agent-session/failures/agent-session-failure-words' -import { CodexUsageLimits } from '../catalog/codex-usage-limits' +import { CodexTurnFailures } from './codex-turn-failures' const OVERFLOW_BUCKET = '__codex-generic-overflow__' type TurnRows = { drawn: number; wordless: number } @@ -74,7 +72,7 @@ export class CodexJournalGenericFrames { private bookkeepingBytes = 0 private fallbackSequence = 0 private cancelSuppressionFlush: (() => void) | null = null - private readonly usageLimits = new CodexUsageLimits() + private readonly failures: CodexTurnFailures constructor( private readonly deps: Pick< @@ -85,6 +83,11 @@ export class CodexJournalGenericFrames { }, private readonly activeTurn: (threadId: string) => string | null ) { + this.failures = new CodexTurnFailures({ + account: deps.account, + activeTurn, + append: (threadId, payload, frame) => this.appendFrameRow(threadId, payload, frame) + }) this.schedule = deps.schedule ?? defaultSchedule this.suppressionCoalesceMs = deps.coalesceMs ?? 60 } @@ -94,31 +97,26 @@ export class CodexJournalGenericFrames { payload: unknown, threadId: string ): CodexJournalTranslationAdmission { - if (this.usageLimits.observe(kind, payload)) { + if (this.failures.observe(kind, payload)) { return CODEX_JOURNAL_ADMITTED } + if (kind === 'notification:error' && !readCodexErrorWillRetry(payload)) { + return this.failures.appendTerminal(kind, payload, payload, threadId) + } const translated = unhandledProviderFrameJournalItem('codex', kind, payload) - // Declined frames count as success; rejection would force-close the provider. - const ended = kind === 'notification:error' && !readCodexErrorWillRetry(payload) - const usageLimit = ended ? this.usageLimits.failure(payload) : null - const failure = - usageLimit ?? (ended ? codexAuthenticationFailure(payload, this.deps.account?.()) : null) - return translated - ? this.appendFrameRow(threadId, payload, { - ...translated, - body: failure - ? { - ...translated.body, - ...(usageLimit ? { tone: 'warning' } : {}), - ...agentSessionFailureWords(failure, { - provider: 'codex', - agentName: 'Codex', - surface: 'row' - }) - } - : translated.body - }) - : CODEX_JOURNAL_ADMITTED + return translated ? this.appendFrameRow(threadId, payload, translated) : CODEX_JOURNAL_ADMITTED + } + + appendCompletionFailure( + payload: unknown, + threadId: string, + onShown: (turnId: string) => void + ): CodexJournalTranslationAdmission { + return this.failures.appendCompletion(payload, threadId, onShown) + } + + forgetTurnFailure(threadId: string, turnId: string): void { + this.failures.forget(threadId, turnId) } appendFrameRow( @@ -159,7 +157,9 @@ export class CodexJournalGenericFrames { const identity = { provider: 'orca' as const, clientMessageId: - translated.body.failure?.kind === 'usageLimitReached' && frameTurnId + translated.body.failure && + translated.body.failure.kind !== 'providerRetrying' && + frameTurnId ? `terminal-failure:codex:${JSON.stringify([threadId, frameTurnId])}` : `provider-frame:codex:${this.deps.acquisitionId ?? 'acquisition'}:${this.fallbackSequence}` } @@ -181,7 +181,11 @@ export class CodexJournalGenericFrames { : { ...rows, drawn: rows.drawn + 1 } ) this.recordBucket(bucket) - return publish(this.deps.sink) + const publication = publish(this.deps.sink) + if (publication.accepted && translated.body.failure?.kind === 'providerRetrying') { + this.failures.observeRetry(payload, threadId) + } + return publication } suppress(threadId: string, turnId: string, count = 1): void { @@ -243,6 +247,7 @@ export class CodexJournalGenericFrames { dispose(): void { this.cancelSuppressionFlush?.() + this.failures.dispose() this.genericRowsByTurn.clear() this.suppressedRowsByTurn.clear() this.bucketOrder.clear() diff --git a/src/main/codex/structured/journal/codex-structured-journal-translation.ts b/src/main/codex/structured/journal/codex-structured-journal-translation.ts index 142a8a16d9a..a5cfa72d0bb 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-translation.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-translation.ts @@ -210,9 +210,21 @@ function createCodexTranslation( return CODEX_JOURNAL_ADMITTED } if (event.method === 'turn/started' || event.method === 'turn/completed') { - return event.method === 'turn/started' - ? turnBoundaries.start(event) - : turnBoundaries.complete(event) + if (event.method === 'turn/started') { + return turnBoundaries.start(event) + } + const turnId = readCodexTurnId(event.params) ?? activeTurns.current(event.threadId) + const failure = genericFrames.appendCompletionFailure(event.params, event.threadId, (id) => + commands.errorShown({ turnId: id }) + ) + if (!failure.accepted) { + return failure + } + const admission = turnBoundaries.complete(event) + if (admission.accepted && turnId) { + genericFrames.forgetTurnFailure(event.threadId, turnId) + } + return admission } const compaction = compactions.handle(event) if (compaction) { diff --git a/src/main/codex/structured/journal/codex-turn-failures.test.ts b/src/main/codex/structured/journal/codex-turn-failures.test.ts new file mode 100644 index 00000000000..9a89ea774a8 --- /dev/null +++ b/src/main/codex/structured/journal/codex-turn-failures.test.ts @@ -0,0 +1,216 @@ +import { readFileSync } from 'node:fs' +import { join } from 'node:path' +import { describe, expect, it } from 'vitest' +import { createCodexJournalTranslator } from './codex-structured-journal-translation' +import { + LIFECYCLE_KEY, + THREAD_ID, + TURN_ID, + notification, + recorder, + reduced, + translatorFor +} from './codex-turn-lifecycle-contract-test-fixture' + +const DIAGNOSTIC = 'Provider diagnostic: internal request failure' + +function error(code: unknown, extra: Record = {}) { + return { + threadId: THREAD_ID, + turnId: TURN_ID, + willRetry: false, + error: { message: DIAGNOSTIC, codexErrorInfo: code }, + ...extra + } +} + +function statuses(tap: ReturnType) { + return reduced(tap.rows).flatMap((row) => + row.body.kind === 'status' && row.body.failure?.kind !== 'providerRetrying' ? [row.body] : [] + ) +} + +describe('Codex terminal failure categories', () => { + it.each([ + ['rateLimitExceeded', 'usageLimitReached'], + ['flexUnavailable', 'providerUnavailable'], + ['modelNotFound', 'modelUnavailable'], + ['policyCapacityUnavailable', 'providerUnavailable'], + ['misalignmentPolicyViolation', 'providerSafetyStopped'], + ['serverOverloaded', 'providerUnavailable'], + ['cyberPolicy', 'providerSafetyStopped'] + ])('categorizes %s without exposing its diagnostic', (code, kind) => { + for (const info of [code, { [code]: { httpStatusCode: 401 } }]) { + const tap = recorder() + const translator = translatorFor(tap) + translator.handle(notification('error', error(info))) + expect(statuses(tap)).toMatchObject([{ failure: { kind } }]) + expect(statuses(tap)[0]?.text).not.toContain(DIAGNOSTIC) + expect(statuses(tap)[0]?.providerFrame?.payload.head).toContain(DIAGNOSTIC) + translator.dispose() + } + }) + + it('uses the existing usage-limit category for HTTP 429 retry exhaustion', () => { + const tap = recorder() + const translator = translatorFor(tap) + translator.handle( + notification('error', error({ responseTooManyFailedAttempts: { httpStatusCode: 429 } })) + ) + expect(statuses(tap)).toMatchObject([ + { tone: 'warning', failure: { kind: 'usageLimitReached' } } + ]) + }) + + it('ends a recorded-shape failed completion carrying an unknown future code', () => { + const frames: { + case: string + method: string + params: { + threadId: string + turn?: { id: string; status: string; error?: Record } + } + }[] = readFileSync( + join(__dirname, '../../__fixtures__/codex-app-server-turn-endings.jsonl'), + 'utf8' + ) + .trim() + .split('\n') + .map((line) => JSON.parse(line)) + const captured = frames.find( + (frame) => frame.case === '0.141.0-conn-refused' && frame.method === 'turn/completed' + ) + if (!captured?.params.turn) { + throw new Error('Missing captured failed completion') + } + const tap = recorder() + const translator = createCodexJournalTranslator({ + sink: tap.sink, + sessionId: 'session-1', + primaryThreadId: () => captured.params.threadId + }) + const on = (method: string, params: unknown) => + translator.handle({ + type: 'notification', + sessionId: 'session-1', + threadId: captured.params.threadId, + method, + params + }) + on('turn/started', { turn: { id: captured.params.turn.id } }) + expect(reduced(tap.rows).map((row) => row.body)).toMatchObject([ + { kind: 'turn', state: 'running' } + ]) + expect( + on('turn/completed', { + ...captured.params, + turn: { + ...captured.params.turn, + error: { ...captured.params.turn.error, codexErrorInfo: 'futureFailureAfter0167' } + } + }) + ).toEqual({ accepted: true }) + expect(reduced(tap.rows).map((row) => row.body)).toMatchObject([ + { kind: 'turn', state: 'completed', outcome: 'failure' }, + { kind: 'status', failure: { kind: 'providerResponseFailed' } } + ]) + }) + + it('shows a completion-only safety stop while preserving an ordinary Stop', () => { + for (const error of [{ message: DIAGNOSTIC, codexErrorInfo: 'tooManyDenials' }, null]) { + const tap = recorder() + const translator = translatorFor(tap) + translator.handle(notification('turn/started', { turn: { id: TURN_ID } })) + translator.handle( + notification('turn/completed', { turn: { id: TURN_ID, status: 'interrupted', error } }) + ) + expect(reduced(tap.rows).find((row) => row.key === LIFECYCLE_KEY)?.body).toMatchObject({ + kind: 'turn', + state: 'interrupted', + outcome: 'cancellation' + }) + expect(statuses(tap)).toMatchObject( + error ? [{ failure: { kind: 'providerSafetyStopped' } }] : [] + ) + } + }) + + it('keeps completion-only sign-in diagnostics behind Details', () => { + const tap = recorder() + const translator = translatorFor(tap) + translator.handle(notification('turn/started', { turn: { id: TURN_ID } })) + translator.handle( + notification('turn/completed', { + turn: { + id: TURN_ID, + status: 'failed', + error: error({ responseStreamDisconnected: { httpStatusCode: 401 } }).error + } + }) + ) + expect(statuses(tap)).toMatchObject([{ failure: { kind: 'notSignedIn' } }]) + expect(statuses(tap)[0]?.text).not.toContain(DIAGNOSTIC) + expect(statuses(tap)[0]?.providerFrame?.payload.head).toContain(DIAGNOSTIC) + }) + + it('keeps recovered sign-in advice through completion backpressure without another failure row', () => { + const tap = recorder() + let rejectCompletion = true + tap.sink.tryAppendItem = (identity, body, options) => { + if (body.kind === 'turn' && body.state === 'completed' && rejectCompletion) { + rejectCompletion = false + return { accepted: false, reason: 'backpressure' } + } + tap.sink.appendItem(identity, body, options) + return { accepted: true } + } + const translator = translatorFor(tap) + translator.handle(notification('turn/started', { turn: { id: TURN_ID } })) + translator.handle( + notification('error', { + ...error({ responseStreamDisconnected: { httpStatusCode: 401 } }, { willRetry: true }), + error: { + message: 'Reconnecting... 1/2', + codexErrorInfo: { responseStreamDisconnected: { httpStatusCode: 401 } }, + additionalDetails: DIAGNOSTIC + } + }) + ) + translator.handle(notification('error', error('other'))) + const completion = notification('turn/completed', { + turn: { id: TURN_ID, status: 'failed', error: error('other').error } + }) + expect(translator.handle(completion)).toEqual({ accepted: false, reason: 'backpressure' }) + expect(translator.handle(completion)).toEqual({ accepted: true }) + const terminal = tap.rows.filter( + (row) => row.body.kind === 'status' && row.body.failure?.kind !== 'providerRetrying' + ) + expect(terminal.map((row) => row.body)).toMatchObject([{ failure: { kind: 'notSignedIn' } }]) + expect(statuses(tap)[0]?.text).not.toContain(DIAGNOSTIC) + expect(statuses(tap)[0]?.providerFrame?.payload.head).toContain(DIAGNOSTIC) + expect(reduced(tap.rows).find((row) => row.key === LIFECYCLE_KEY)?.body).toMatchObject({ + state: 'completed', + outcome: 'failure' + }) + }) + + it('uses the last non-other retry only for matching text and scope, including empty details', () => { + const tap = recorder() + const translator = translatorFor(tap) + const retry = (code: unknown, details: string) => + notification('error', { + ...error(code, { willRetry: true }), + error: { message: DIAGNOSTIC, codexErrorInfo: code, additionalDetails: details } + }) + translator.handle(retry({ responseStreamDisconnected: { httpStatusCode: 401 } }, '')) + translator.handle(notification('error', error('other'))) + expect(statuses(tap).at(-1)?.failure?.kind).toBe('providerResponseFailed') + translator.handle(retry({ responseStreamDisconnected: { httpStatusCode: 401 } }, DIAGNOSTIC)) + translator.handle(retry('futureRetryCode', DIAGNOSTIC)) + translator.handle(notification('error', error('other'))) + expect(statuses(tap).at(-1)?.failure?.kind).toBe('providerResponseFailed') + translator.handle(retry({ responseStreamDisconnected: { httpStatusCode: 401 } }, DIAGNOSTIC)) + translator.handle(notification('error', error('other', { turnId: 'another-turn' }))) + expect(statuses(tap).at(-1)?.failure?.kind).toBe('providerResponseFailed') + }) +}) diff --git a/src/main/codex/structured/journal/codex-turn-failures.ts b/src/main/codex/structured/journal/codex-turn-failures.ts new file mode 100644 index 00000000000..48b30d08edf --- /dev/null +++ b/src/main/codex/structured/journal/codex-turn-failures.ts @@ -0,0 +1,240 @@ +import { createHash } from 'node:crypto' +import { agentSessionFailureFact } from '../../../../shared/agent-session/failures/agent-session-failure' +import type { AgentSessionAccountKind } from '../../../../shared/agent-session/agent-session-availability' +import { + readCodexErrorInfo, + readCodexTurnId, + readCodexTurnStatus +} from '../../app-server/codex-structured-thread-facts' +import { readRecord } from '../../app-server/codex-item-field-readers' +import { codexAuthenticationFailure } from '../session-lifecycle/codex-authentication-failure' +import { CodexUsageLimits } from '../catalog/codex-usage-limits' +import { agentSessionFailureWords } from '../../../../shared/agent-session/failures/agent-session-failure-words' +import { + unhandledProviderFrameJournalItem, + type UnhandledProviderFrameJournalItem +} from '../../../native-chat/agent-session-wire/providers/unhandled-provider-frame' +import { + CODEX_JOURNAL_ADMITTED, + type CodexJournalTranslationAdmission +} from './codex-structured-journal-contracts' +import { MAX_CODEX_GENERIC_TURN_BUCKETS } from './codex-structured-journal-limits' +import type { AgentJournalStatusItem } from '../../../../shared/agent-session/journal/agent-session-journal-types' + +type ErrorInfo = { error: string; status?: number } +type TerminalSignature = { code?: string; message?: string; status?: number } +type FailureEvidence = + | { phase: 'retry'; info: ErrorInfo; text: string } + | { phase: 'terminal'; signature: TerminalSignature } + +function digest(value: string): string { + return createHash('sha256').update(value).digest('hex') +} + +function scopeKey(threadId: string, turnId: string | null): string | null { + return turnId === null ? null : digest(JSON.stringify([threadId, turnId])) +} + +function errorStatus(payload: unknown): number | undefined { + const status = readCodexErrorInfo(payload)?.status + return typeof status === 'number' && Number.isFinite(status) ? status : undefined +} + +function failureText(payload: unknown): string | undefined { + const error = readRecord(readRecord(payload).error) + return typeof error.additionalDetails === 'string' + ? error.additionalDetails + : typeof error.message === 'string' + ? error.message + : undefined +} + +function terminalSignature(payload: unknown): TerminalSignature { + const info = readCodexErrorInfo(payload) + const message = readRecord(readRecord(payload).error).message + const status = errorStatus(payload) + return { + ...(info ? { code: digest(info.error) } : {}), + ...(typeof message === 'string' ? { message: digest(message) } : {}), + ...(status !== undefined ? { status } : {}) + } +} + +export class CodexTurnFailures { + private readonly usageLimits = new CodexUsageLimits() + private readonly evidence = new Map() + + constructor( + private readonly deps: { + account?: () => AgentSessionAccountKind | undefined + activeTurn: (threadId: string) => string | null + append: ( + threadId: string, + payload: unknown, + frame: Omit & { body: AgentJournalStatusItem } + ) => CodexJournalTranslationAdmission + } + ) {} + + appendTerminal( + kind: string, + payload: unknown, + errorPayload: unknown, + threadId: string + ): CodexJournalTranslationAdmission { + const translated = unhandledProviderFrameJournalItem('codex', kind, payload, undefined, { + reportsFailure: true + }) + if (!translated) { + return CODEX_JOURNAL_ADMITTED + } + const failure = this.failure(errorPayload, threadId, this.deps.account?.()) + const admission = this.deps.append(threadId, payload, { + ...translated, + body: { + ...translated.body, + ...(failure.kind === 'usageLimitReached' ? { tone: 'warning' } : {}), + ...agentSessionFailureWords(failure, { + provider: 'codex', + agentName: 'Codex', + surface: 'row' + }) + } + }) + if (admission.accepted) { + this.admittedTerminal( + errorPayload, + threadId, + readCodexTurnId(payload) ?? this.deps.activeTurn(threadId) + ) + } + return admission + } + + appendCompletion( + payload: unknown, + threadId: string, + onShown: (turnId: string) => void + ): CodexJournalTranslationAdmission { + const turn = readRecord(readRecord(payload).turn) + const status = readCodexTurnStatus(payload) + const turnId = readCodexTurnId(payload) ?? this.deps.activeTurn(threadId) + if (!turnId || (status !== 'failed' && !(status === 'interrupted' && turn.error))) { + return CODEX_JOURNAL_ADMITTED + } + const errorPayload = { turnId, error: turn.error, willRetry: false } + const admission = this.repeatsTerminal(errorPayload, threadId, turnId) + ? CODEX_JOURNAL_ADMITTED + : this.appendTerminal('notification:turn/completed', payload, errorPayload, threadId) + if (admission.accepted) { + onShown(turnId) + } + return admission + } + + observe(kind: string, payload: unknown): boolean { + return this.usageLimits.observe(kind, payload) + } + + observeRetry(payload: unknown, threadId: string): void { + const info = readCodexErrorInfo(payload) + const text = failureText(payload) + const key = scopeKey(threadId, readCodexTurnId(payload)) + if (!key || !info || info.error === 'other' || text === undefined) { + return + } + this.remember(key, { + phase: 'retry', + info: { + error: info.error.length <= 128 ? info.error : 'other', + status: errorStatus(payload) + }, + text: digest(text) + }) + } + + private failure(payload: unknown, threadId: string, account?: AgentSessionAccountKind) { + let info = readCodexErrorInfo(payload) + const key = scopeKey(threadId, readCodexTurnId(payload)) + const previous = key ? this.evidence.get(key) : undefined + const text = failureText(payload) + if ( + readRecord(payload).willRetry === false && + info?.error === 'other' && + previous?.phase === 'retry' && + text !== undefined && + previous.text === digest(text) + ) { + info = previous.info + } + const effective = { + error: { + ...readRecord(readRecord(payload).error), + codexErrorInfo: info ? { [info.error]: { httpStatusCode: info.status } } : undefined + } + } + const existing = + this.usageLimits.failure(effective) ?? codexAuthenticationFailure(effective, account) + if (existing) { + // Sign-in advice must not quote provider text; the bounded frame retains diagnostics. + return existing.kind === 'notSignedIn' ? { ...existing, detail: undefined } : existing + } + switch (info?.error ?? 'other') { + case 'modelNotFound': + return agentSessionFailureFact('modelUnavailable') + case 'serverOverloaded': + case 'flexUnavailable': + case 'policyCapacityUnavailable': + return agentSessionFailureFact('providerUnavailable') + case 'cyberPolicy': + case 'misalignmentPolicyViolation': + case 'tooManyDenials': + return agentSessionFailureFact('providerSafetyStopped') + default: + return agentSessionFailureFact('providerResponseFailed') + } + } + + private repeatsTerminal(payload: unknown, threadId: string, turnId: string): boolean { + const key = scopeKey(threadId, turnId) + const previous = key ? this.evidence.get(key) : undefined + if (previous?.phase !== 'terminal') { + return false + } + const next = terminalSignature(payload) + return ( + (next.code === undefined || next.code === previous.signature.code) && + (next.message === undefined || next.message === previous.signature.message) && + (next.status === undefined || next.status === previous.signature.status) + ) + } + + private admittedTerminal(payload: unknown, threadId: string, turnId: string | null): void { + const key = scopeKey(threadId, turnId) + if (key) { + this.remember(key, { phase: 'terminal', signature: terminalSignature(payload) }) + } + } + + forget(threadId: string, turnId: string): void { + const key = scopeKey(threadId, turnId) + if (key) { + this.evidence.delete(key) + } + } + + dispose(): void { + this.evidence.clear() + } + + private remember(key: string, evidence: FailureEvidence): void { + this.evidence.delete(key) + this.evidence.set(key, evidence) + if (this.evidence.size > MAX_CODEX_GENERIC_TURN_BUCKETS) { + const oldest = this.evidence.keys().next().value + if (oldest !== undefined) { + this.evidence.delete(oldest) + } + } + } +} diff --git a/src/main/codex/structured/turns/codex-command-turn-claim.test.ts b/src/main/codex/structured/turns/codex-command-turn-claim.test.ts index 4a5a638401c..0895b51afb3 100644 --- a/src/main/codex/structured/turns/codex-command-turn-claim.test.ts +++ b/src/main/codex/structured/turns/codex-command-turn-claim.test.ts @@ -160,14 +160,20 @@ describe('a Codex turn a conversation command claims', () => { agentJournalItemKey(COMMAND.resultIdentity) ) expect(rows.map((write) => write.turnScope)).toEqual(rows.map(() => scope)) - expect( - rows.some( - (write) => - write.body.kind === 'status' && - write.body.tone === 'error' && - write.body.text.includes('Selected model is at capacity') - ) - ).toBe(true) + const failures = rows.filter( + (write) => write.body.kind === 'status' && write.body.tone === 'error' + ) + expect(failures.map((write) => write.body)).toMatchObject([ + { + kind: 'status', + failure: { kind: 'providerUnavailable' }, + providerFrame: { + payload: { + head: expect.stringContaining('Selected model is at capacity') + } + } + } + ]) }) it('leaves the next turn to write its own record after a failed compaction', () => { diff --git a/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.failures.test.tsx b/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.failures.test.tsx new file mode 100644 index 00000000000..f8d2ab5778e --- /dev/null +++ b/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.failures.test.tsx @@ -0,0 +1,69 @@ +// @vitest-environment happy-dom +import '@testing-library/jest-dom/vitest' +import { cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, expect, it } from 'vitest' +import { agentSessionFailureFact } from '../../../../../../shared/agent-session/failures/agent-session-failure' +import { agentSessionFailureWords } from '../../../../../../shared/agent-session/failures/agent-session-failure-words' +import { NativeChatNoticeRow } from './NativeChatNoticeRow' +import { i18n } from '@/i18n/i18n' + +afterEach(async () => { + cleanup() + await i18n.changeLanguage('en') +}) + +it('localizes canonical failure facts while keeping host advice and diagnostics intact', async () => { + await i18n.changeLanguage('fr') + const diagnostic = 'Internal provider diagnostic: modelNotFound' + const payload = { + head: diagnostic, + byteLength: diagnostic.length, + digest: 'fixture', + truncated: false + } + const kinds = [ + 'modelUnavailable', + 'providerUnavailable', + 'providerSafetyStopped', + 'providerResponseFailed' + ] as const + const { container } = render( + <> + {kinds.map((kind) => ( + + ))} + + + ) + const visibleLines = [...container.querySelectorAll('p')] + .map((node) => node.textContent) + .join('\n') + expect(visibleLines).toContain('le modèle sélectionné') + expect(visibleLines).not.toContain('the selected model') + expect(visibleLines).not.toContain(diagnostic) + expect(screen.getByText('Custom host advice: use the enterprise model.')).toBeInTheDocument() + const details = container.querySelector('details')! + expect(details.open).toBe(false) + fireEvent.click(details.querySelector('summary')!) + expect(details.open).toBe(true) + expect(details.querySelector('pre')).toHaveTextContent(diagnostic) +}) diff --git a/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.fixture.tsx b/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.fixture.tsx index 7dbb128be4a..54ff875e949 100644 --- a/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.fixture.tsx +++ b/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.fixture.tsx @@ -1,7 +1,52 @@ import { defineFixture } from '../../../../component-fixtures/component-fixture' +import { i18n } from '../../../../i18n/i18n' +import { agentSessionFailureFact } from '../../../../../../shared/agent-session/failures/agent-session-failure' +import { agentSessionFailureWords } from '../../../../../../shared/agent-session/failures/agent-session-failure-words' import type { NativeChatTextBlock } from '../../../../../../shared/native-chat/native-chat-types' import { NativeChatNoticeRow } from './NativeChatNoticeRow' +export const codexFailures = defineFixture({ + width: 440, + interact: async () => { + await i18n.changeLanguage('fr') + }, + render: () => ( +
+ {( + [ + 'providerUnavailable', + 'providerSafetyStopped', + 'providerResponseFailed', + 'modelUnavailable' + ] as const + ).map((kind) => ( + + ))} +
+ ) +}) + function guardianReview(text: string): NativeChatTextBlock { const head = JSON.stringify({ message: text }) return { diff --git a/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.tsx b/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.tsx index e24e3385ffe..d77038317c9 100644 --- a/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.tsx +++ b/src/renderer/src/components/native-chat/messages/notices/NativeChatNoticeRow.tsx @@ -239,9 +239,16 @@ export function NativeChatNoticeRow({ block.usageLimitTurnItemId === orcaStopView.usageLimitTurnItemId // Reword only text fully described by its fact; other host text may carry command advice. const factSurface = - failure?.kind === 'notSignedIn' || - failure?.kind === 'savedConversationMissing' || - failure?.kind === 'pluginInputRequired' + failure && + [ + 'notSignedIn', + 'savedConversationMissing', + 'pluginInputRequired', + 'modelUnavailable', + 'providerUnavailable', + 'providerSafetyStopped', + 'providerResponseFailed' + ].includes(failure.kind) ? (['row', 'rejection'] as const).find( (surface) => block.text === agentSessionFailureSentence(failure, surface, { agentName }) ) diff --git a/src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.ts b/src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.ts index da067b3b101..aa57f4261bb 100644 --- a/src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.ts +++ b/src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.ts @@ -13,6 +13,7 @@ import { STOP_FAILURE_PIECES } from './agent-session-failure-stop-words-text' import { PROVIDER_EXIT_PIECES } from './agent-session-provider-exit-words-text' import { PROVIDER_RETRY_PIECES } from './agent-session-provider-retry-words-text' import { USAGE_LIMIT_PIECES } from './agent-session-usage-limit-words-text' +import { PROVIDER_FAILURE_PIECES } from './agent-session-provider-failure-words-text' import { START_FAILURE_PIECES } from './agent-session-start-failure-words-text' // The pieces a refusal notice says too keep the notice's keys, so each has one translation. @@ -181,6 +182,7 @@ const PIECES: Record translate( 'components.native-chat.failureWords.providerRejected', diff --git a/src/renderer/src/components/native-chat/messages/notices/agent-session-provider-failure-words-text.ts b/src/renderer/src/components/native-chat/messages/notices/agent-session-provider-failure-words-text.ts new file mode 100644 index 00000000000..0aa86372ed6 --- /dev/null +++ b/src/renderer/src/components/native-chat/messages/notices/agent-session-provider-failure-words-text.ts @@ -0,0 +1,31 @@ +import { translate } from '@/i18n/i18n' +import { + AGENT_SESSION_FAILURE_COPY as COPY, + type AgentSessionFailureCopyId, + type AgentSessionFailureCopyValues +} from '../../../../../../shared/agent-session/failures/agent-session-failure-copy' + +export const PROVIDER_FAILURE_PIECES = { + selectedModel: (values) => + translate('components.native-chat.failureWords.selectedModel', COPY.selectedModel, values), + providerUnavailable: (values) => + translate( + 'components.native-chat.failureWords.providerUnavailable', + COPY.providerUnavailable, + values + ), + providerSafetyStopped: (values) => + translate( + 'components.native-chat.failureWords.providerSafetyStopped', + COPY.providerSafetyStopped, + values + ), + providerResponseFailed: (values) => + translate( + 'components.native-chat.failureWords.providerResponseFailed', + COPY.providerResponseFailed, + values + ) +} satisfies Partial< + Record string> +> diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index 8633e53f48e..85fbf332a0d 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -1623,17 +1623,21 @@ "providerRateLimited": "{{agent}} is rate-limited and retrying.", "providerRejected": "{{agent}} didn't accept this message.", "providerRejectedQuoted": "{{agent}} didn't accept this message: {{detail}}.", + "providerResponseFailed": "{{agent}} couldn't finish this response. Open Details for more information.", "providerRetryLastError": "Last error: {{detail}}.", "providerRetryNumber": "Retry {{attempt}}.", "providerRetryNumberOf": "Retry {{attempt}} of {{maxRetries}}.", "providerRetrying": "{{agent}} hit a temporary problem and is retrying.", "providerRetryingQuoted": "{{agent}} is retrying: {{detail}}.", + "providerSafetyStopped": "{{agent}} stopped this response after a safety check. Review your request and send again.", "providerStartFailed": "{{agent}} stopped before it finished starting.", + "providerUnavailable": "{{agent}} couldn't respond because the service is busy. Pick another model, or wait a little and send again.", "queueFull": "Too many messages were waiting for the agent, so this one was not sent.", "runCommandAgain": "Run /{{command}} again.", "runCommandWhenDone": "Run /{{command}} when it's done.", "runtimeNotInstalled": "{{agent}} isn't installed on the computer running this chat. Install it from the chat in a current version of Orca.", "savedConversationMissing": "{{agent}} couldn't find this chat's saved conversation.", + "selectedModel": "the selected model", "sendAgainToTryOnceMore": "Send your message again to try once more.", "sendToTryAgain": "Send your message to try again.", "sessionNotRestored": "{{agent}} couldn't reopen its earlier session, so this chat continues in a new one. {{agent}} doesn't remember the earlier messages.", diff --git a/src/renderer/src/i18n/locales/de.json b/src/renderer/src/i18n/locales/de.json index e8b5fce4702..b7d13c0a29d 100644 --- a/src/renderer/src/i18n/locales/de.json +++ b/src/renderer/src/i18n/locales/de.json @@ -19287,6 +19287,11 @@ "providerExitedRow": "{{agent}} wurde während dieser Antwort gestoppt. Die Unterhaltung kann fortgesetzt werden.", "providerExitedRejection": "{{agent}} wurde gestoppt, bevor diese Nachricht gesendet wurde.", "providerExitedAnswer": "{{agent}} wurde gestoppt, daher wurde die Antwort nicht gesendet. Zum Fortfahren eine Nachricht senden.", + "providerUnavailable": "{{agent}} konnte nicht antworten, weil der Dienst ausgelastet ist. Wähle ein anderes Modell oder warte kurz und sende erneut.", + "providerSafetyStopped": "{{agent}} hat diese Antwort nach einer Sicherheitsprüfung gestoppt. Überprüfe deine Anfrage und sende sie erneut.", + "providerResponseFailed": "{{agent}} konnte diese Antwort nicht abschließen. Öffnen Sie die Details für weitere Informationen.", + "modelUnavailable": "{{agent}} konnte {{detail}} nicht verwenden. Ein anderes Modell auswählen und erneut senden.", + "selectedModel": "das ausgewählte Modell", "providerRejected": "{{agent}} hat diese Nachricht nicht angenommen.", "providerRejectedQuoted": "{{agent}} hat diese Nachricht nicht angenommen: {{detail}}.", "attachmentEmpty": "Ein Bild in dieser Nachricht ist leer, daher wurde die Nachricht nicht gesendet.", diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 03b11ac3bb5..cbda726753e 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -19869,6 +19869,10 @@ "modelUnavailable": "{{agent}} couldn't use {{detail}}. Pick another model and send again.", "modelUnavailableUpdate": "{{agent}} couldn't use {{detail}}. Update {{agent}} to {{minimumVersion}} or later to keep it after a restart, or pick another model and send again.", "modelNotSelected": "{{agent}} has no model selected. Check your model selection and {{agent}} sign-in, then send again.", + "providerUnavailable": "{{agent}} couldn't respond because the service is busy. Pick another model, or wait a little and send again.", + "providerSafetyStopped": "{{agent}} stopped this response after a safety check. Review your request and send again.", + "providerResponseFailed": "{{agent}} couldn't finish this response. Open Details for more information.", + "selectedModel": "the selected model", "providerRejected": "{{agent}} didn't accept this message.", "providerRejectedQuoted": "{{agent}} didn't accept this message: {{detail}}.", "attachmentEmpty": "An image on this message is empty, so the message was not sent.", diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index b5d46135395..91adf61cfd7 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -19021,6 +19021,11 @@ "providerExitedRow": "{{agent}} se detuvo mientras esta respuesta estaba en curso. Puede continuar en esta conversación.", "providerExitedRejection": "{{agent}} se detuvo antes de que se enviara este mensaje.", "providerExitedAnswer": "{{agent}} se detuvo, así que su respuesta no se envió. Envíe un mensaje para continuar.", + "providerUnavailable": "{{agent}} no pudo responder porque el servicio está ocupado. Elige otro modelo o espera un poco y vuelve a enviar el mensaje.", + "providerSafetyStopped": "{{agent}} detuvo esta respuesta tras una comprobación de seguridad. Revisa tu solicitud y vuelve a enviarla.", + "providerResponseFailed": "{{agent}} no pudo terminar esta respuesta. Abre los detalles para obtener más información.", + "modelUnavailable": "{{agent}} no pudo usar {{detail}}. Elige otro modelo y vuelve a enviar el mensaje.", + "selectedModel": "el modelo seleccionado", "providerRejected": "{{agent}} no aceptó este mensaje.", "providerRejectedQuoted": "{{agent}} no aceptó este mensaje: {{detail}}.", "attachmentEmpty": "Una imagen de este mensaje está vacía, así que el mensaje no se envió.", diff --git a/src/renderer/src/i18n/locales/fr.json b/src/renderer/src/i18n/locales/fr.json index c9887b7ca7f..7a8bbf7ad7d 100644 --- a/src/renderer/src/i18n/locales/fr.json +++ b/src/renderer/src/i18n/locales/fr.json @@ -19260,6 +19260,11 @@ "providerExitedRow": "{{agent}} s'est arrêté pendant que cette réponse était en cours. Vous pouvez continuer dans cette conversation.", "providerExitedRejection": "{{agent}} s'est arrêté avant l'envoi de ce message.", "providerExitedAnswer": "{{agent}} s'est arrêté, donc votre réponse n'a pas été envoyée. Envoyez un message pour continuer.", + "providerUnavailable": "{{agent}} n’a pas pu répondre car le service est occupé. Choisissez un autre modèle ou patientez un peu, puis renvoyez le message.", + "providerSafetyStopped": "{{agent}} a arrêté cette réponse après un contrôle de sécurité. Vérifiez votre demande et renvoyez-la.", + "providerResponseFailed": "{{agent}} n’a pas pu terminer cette réponse. Ouvrez les détails pour en savoir plus.", + "modelUnavailable": "{{agent}} n'a pas pu utiliser {{detail}}. Choisissez un autre modèle, puis renvoyez le message.", + "selectedModel": "le modèle sélectionné", "providerRejected": "{{agent}} n'a pas accepté ce message.", "providerRejectedQuoted": "{{agent}} n'a pas accepté ce message : {{detail}}.", "attachmentEmpty": "Une image de ce message est vide, le message n'a donc pas été envoyé.", diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index 613f7cac950..b3d7e56f49c 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -18604,6 +18604,11 @@ "providerExitedRow": "この応答の途中で {{agent}} が停止しました。この会話で続けることができます。", "providerExitedRejection": "このメッセージが送信される前に {{agent}} が停止しました。", "providerExitedAnswer": "{{agent}} が停止したため、回答は送信されませんでした。続けるにはメッセージを送信してください。", + "providerUnavailable": "サービスが混雑しているため、{{agent}} は応答できませんでした。別のモデルを選ぶか、しばらく待ってから再送してください。", + "providerSafetyStopped": "{{agent}} は安全確認の後、この応答を停止しました。リクエストを見直して再送してください。", + "providerResponseFailed": "{{agent}} はこの応答を完了できませんでした。詳細を開いて確認してください。", + "modelUnavailable": "{{agent}} は {{detail}} を使用できませんでした。別のモデルを選択して、もう一度送信してください。", + "selectedModel": "選択されたモデル", "providerRejected": "{{agent}} はこのメッセージを受け付けませんでした。", "providerRejectedQuoted": "{{agent}} はこのメッセージを受け付けませんでした: {{detail}}。", "attachmentEmpty": "このメッセージの画像が空のため、メッセージは送信されませんでした。", diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index 0f99b624163..bd509836c0f 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -18604,6 +18604,11 @@ "providerExitedRow": "이 응답이 진행되는 동안 {{agent}}이(가) 중지되었습니다. 이 대화에서 계속할 수 있습니다.", "providerExitedRejection": "이 메시지가 전송되기 전에 {{agent}}이(가) 중지되었습니다.", "providerExitedAnswer": "{{agent}}이(가) 중지되어 답변이 전송되지 않았습니다. 계속하려면 메시지를 보내세요.", + "providerUnavailable": "서비스가 혼잡하여 {{agent}}이(가) 응답하지 못했습니다. 다른 모델을 선택하거나 잠시 기다린 후 다시 보내세요.", + "providerSafetyStopped": "{{agent}}이(가) 안전 검사 후 이 응답을 중단했습니다. 요청을 검토한 후 다시 보내세요.", + "providerResponseFailed": "{{agent}}이(가) 이 응답을 완료하지 못했습니다. 자세한 내용을 열어 확인하세요.", + "modelUnavailable": "{{agent}}이(가) {{detail}}을(를) 사용하지 못했습니다. 다른 모델을 선택하고 다시 보내세요.", + "selectedModel": "선택한 모델", "providerRejected": "{{agent}}이(가) 이 메시지를 받아들이지 않았습니다.", "providerRejectedQuoted": "{{agent}}이(가) 이 메시지를 받아들이지 않았습니다: {{detail}}.", "attachmentEmpty": "이 메시지의 이미지가 비어 있어 메시지가 전송되지 않았습니다.", diff --git a/src/renderer/src/i18n/locales/pt.json b/src/renderer/src/i18n/locales/pt.json index d7b928d3ad3..35b47a935ef 100644 --- a/src/renderer/src/i18n/locales/pt.json +++ b/src/renderer/src/i18n/locales/pt.json @@ -20693,6 +20693,11 @@ "providerExitedRow": "{{agent}} parou enquanto esta resposta estava em andamento. Você pode continuar nesta conversa.", "providerExitedRejection": "{{agent}} parou antes de esta mensagem ser enviada.", "providerExitedAnswer": "{{agent}} parou, por isso sua resposta não foi enviada. Envie uma mensagem para continuar.", + "providerUnavailable": "{{agent}} não conseguiu responder porque o serviço está ocupado. Escolha outro modelo ou aguarde um pouco e envie novamente.", + "providerSafetyStopped": "{{agent}} interrompeu esta resposta após uma verificação de segurança. Revise sua solicitação e envie novamente.", + "providerResponseFailed": "{{agent}} não conseguiu concluir esta resposta. Abra os detalhes para obter mais informações.", + "modelUnavailable": "{{agent}} não conseguiu usar {{detail}}. Selecione outro modelo e envie novamente.", + "selectedModel": "o modelo selecionado", "providerRejected": "{{agent}} não aceitou esta mensagem.", "providerRejectedQuoted": "{{agent}} não aceitou esta mensagem: {{detail}}.", "attachmentEmpty": "Uma imagem nesta mensagem está vazia, por isso a mensagem não foi enviada.", diff --git a/src/renderer/src/i18n/locales/ru.json b/src/renderer/src/i18n/locales/ru.json index 466dfb0eaf1..8f07a41b469 100644 --- a/src/renderer/src/i18n/locales/ru.json +++ b/src/renderer/src/i18n/locales/ru.json @@ -19857,6 +19857,11 @@ "providerExitedRow": "{{agent}} остановился во время ответа. Вы можете продолжить этот разговор.", "providerExitedRejection": "{{agent}} остановился до отправки этого сообщения.", "providerExitedAnswer": "{{agent}} остановился, поэтому ваш ответ не отправлен. Отправьте сообщение, чтобы продолжить.", + "providerUnavailable": "{{agent}} не смог ответить, поскольку сервис занят. Выберите другую модель или немного подождите и отправьте сообщение снова.", + "providerSafetyStopped": "{{agent}} остановил этот ответ после проверки безопасности. Проверьте запрос и отправьте его снова.", + "providerResponseFailed": "{{agent}} не удалось завершить этот ответ. Откройте подробности, чтобы узнать больше.", + "modelUnavailable": "{{agent}} не смог использовать {{detail}}. Выберите другую модель и отправьте сообщение снова.", + "selectedModel": "выбранную модель", "providerRejected": "{{agent}} не принял это сообщение.", "providerRejectedQuoted": "{{agent}} не принял это сообщение: {{detail}}.", "attachmentEmpty": "В сообщении пустое изображение, поэтому оно не отправлено.", diff --git a/src/renderer/src/i18n/locales/tr.json b/src/renderer/src/i18n/locales/tr.json index 3b7a679fe24..95304867e99 100644 --- a/src/renderer/src/i18n/locales/tr.json +++ b/src/renderer/src/i18n/locales/tr.json @@ -19271,6 +19271,11 @@ "providerExitedRow": "Bu yanıt hazırlanırken {{agent}} durdu. Bu konuşmada devam edebilirsiniz.", "providerExitedRejection": "Bu mesaj gönderilmeden önce {{agent}} durdu.", "providerExitedAnswer": "{{agent}} durduğu için yanıtınız gönderilmedi. Devam etmek için bir mesaj gönderin.", + "providerUnavailable": "Hizmet meşgul olduğu için {{agent}} yanıt veremedi. Başka bir model seçin veya biraz bekleyip yeniden gönderin.", + "providerSafetyStopped": "{{agent}}, bir güvenlik kontrolünden sonra bu yanıtı durdurdu. İsteğinizi gözden geçirip yeniden gönderin.", + "providerResponseFailed": "{{agent}} bu yanıtı tamamlayamadı. Daha fazla bilgi için ayrıntıları açın.", + "modelUnavailable": "{{agent}}, {{detail}} modelini kullanamadı. Başka bir model seçip yeniden gönderin.", + "selectedModel": "seçilen modeli", "providerRejected": "{{agent}} bu mesajı kabul etmedi.", "providerRejectedQuoted": "{{agent}} bu mesajı kabul etmedi: {{detail}}.", "attachmentEmpty": "Bu mesajdaki bir görsel boş olduğu için mesaj gönderilmedi.", diff --git a/src/renderer/src/i18n/locales/zh-Hant.json b/src/renderer/src/i18n/locales/zh-Hant.json index 51bfeb5d8b5..6c6ffa4853d 100644 --- a/src/renderer/src/i18n/locales/zh-Hant.json +++ b/src/renderer/src/i18n/locales/zh-Hant.json @@ -19029,6 +19029,10 @@ "modelUnavailable": "{{agent}} 無法使用 {{detail}}。請選擇其他模型並重新傳送。", "modelUnavailableUpdate": "{{agent}} 無法使用 {{detail}}。請將 {{agent}} 更新到 {{minimumVersion}} 或更新版本,以便重新啟動後繼續使用;或選擇其他模型並重新傳送。", "modelNotSelected": "{{agent}} 沒有選擇模型。請檢查模型選擇和 {{agent}} 登入狀態,然後重新傳送。", + "providerUnavailable": "{{agent}} 因服務忙碌而無法回覆。請選擇其他模型,或稍候片刻後重新傳送。", + "providerSafetyStopped": "{{agent}} 在安全檢查後停止了此次回覆。請檢查您的請求後重新傳送。", + "providerResponseFailed": "{{agent}} 未能完成此回覆。開啟詳細資料以取得更多資訊。", + "selectedModel": "所選模型", "providerRejected": "{{agent}} 未接受此訊息。", "providerRejectedQuoted": "{{agent}} 未接受此訊息:{{detail}}。", "attachmentEmpty": "此訊息中的一張圖片為空,因此訊息未傳送。", diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index 48c96ba1076..c410a72c92d 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -18547,6 +18547,10 @@ "modelUnavailable": "{{agent}} 无法使用 {{detail}}。请选择其他模型并重新发送。", "modelUnavailableUpdate": "{{agent}} 无法使用 {{detail}}。请将 {{agent}} 更新到 {{minimumVersion}} 或更高版本,以便重启后继续使用;或者选择其他模型并重新发送。", "modelNotSelected": "{{agent}} 没有选择模型。请检查模型选择和 {{agent}} 登录状态,然后重新发送。", + "providerUnavailable": "{{agent}} 因服务繁忙而无法回复。请选择其他模型,或稍等片刻后重新发送。", + "providerSafetyStopped": "{{agent}} 在安全检查后停止了此次回复。请检查您的请求后重新发送。", + "providerResponseFailed": "{{agent}} 未能完成此回复。打开详情了解更多信息。", + "selectedModel": "所选模型", "providerRejected": "{{agent}} 未接受此消息。", "providerRejectedQuoted": "{{agent}} 未接受此消息:{{detail}}。", "attachmentEmpty": "此消息中的一张图片为空,因此消息未发送。", diff --git a/src/shared/agent-session/failures/agent-session-failure-copy.ts b/src/shared/agent-session/failures/agent-session-failure-copy.ts index c8ddccf4cab..67f16c264eb 100644 --- a/src/shared/agent-session/failures/agent-session-failure-copy.ts +++ b/src/shared/agent-session/failures/agent-session-failure-copy.ts @@ -116,6 +116,13 @@ export const AGENT_SESSION_FAILURE_COPY = { claudeEffortInvalid: 'Claude effort is invalid; choose low, medium, high, xhigh, or max and send again.', modelUnavailable: "{{agent}} couldn't use {{detail}}. Pick another model and send again.", + selectedModel: 'the selected model', + providerUnavailable: + "{{agent}} couldn't respond because the service is busy. Pick another model, or wait a little and send again.", + providerSafetyStopped: + '{{agent}} stopped this response after a safety check. Review your request and send again.', + providerResponseFailed: + "{{agent}} couldn't finish this response. Open Details for more information.", modelNotSelected: '{{agent}} has no model selected. Check your model selection and {{agent}} sign-in, then send again.', modelUnavailableUpdate: diff --git a/src/shared/agent-session/failures/agent-session-failure-words.ts b/src/shared/agent-session/failures/agent-session-failure-words.ts index fe50b8a9e02..a9a14e3868a 100644 --- a/src/shared/agent-session/failures/agent-session-failure-words.ts +++ b/src/shared/agent-session/failures/agent-session-failure-words.ts @@ -23,6 +23,7 @@ import { import { commandRefusedByReason } from './agent-session-command-refusal-words' import { agent, attachmentProblemSentence } from './agent-session-attachment-failure-words' import { joinSentences } from '../../sentence-joining' +import { modelUnavailableSentence } from './agent-session-model-failure-words' import { quotingPersonDetail } from './agent-session-failure-quoted-detail' import { requestTimeoutSeconds } from '../agent-session-request-timeout' import { @@ -31,6 +32,7 @@ import { DISPATCH_REJECTED_QUEUE_FULL, DISPATCH_REJECTED_WRITE_FAILED } from '../requests/structured-agent-session-dispatch-rejection' + // Build legacy text/reason/error and newer typed facts together. Quote provider text only when authored for a person; shared sentence pieces let desktop translate the same fact. declare const failureSentence: unique symbol @@ -196,13 +198,11 @@ const FAILURE_SENTENCES = { ), restartFailed: couldNot('couldNotRestart'), claudeEffortInvalid: (_context, _fact, _surface, say) => say('claudeEffortInvalid'), - modelUnavailable: (context, fact, _surface, say) => { - const words = { ...agent(say, context), detail: fact.detail?.text ?? 'this model' } - return fact.agentUpdate - ? say('modelUnavailableUpdate', { ...words, minimumVersion: fact.agentUpdate }) - : say('modelUnavailable', words) - }, + modelUnavailable: modelUnavailableSentence, modelNotSelected: (context, _fact, _surface, say) => say('modelNotSelected', agent(say, context)), + providerUnavailable: namingAgent('providerUnavailable'), + providerSafetyStopped: namingAgent('providerSafetyStopped'), + providerResponseFailed: namingAgent('providerResponseFailed'), providerRejected: (context, fact, _surface, say) => quotingPersonDetail( say, @@ -286,7 +286,7 @@ export function agentSessionFailureSentence( fact: AgentSessionFailureFact, surface: AgentSessionFailureSurface, context: AgentSessionFailureWordsContext = {}, - /** Desktop passes its translations; the host and the phone keep English. */ + /** Desktop and phone pass their translations; the host keeps English. */ say: AgentSessionFailureSay = sayAgentSessionFailureEnglish ): string { const sentence: Sentence = FAILURE_SENTENCES[fact.kind] diff --git a/src/shared/agent-session/failures/agent-session-failure.ts b/src/shared/agent-session/failures/agent-session-failure.ts index 60981877dbc..0fd673b2bea 100644 --- a/src/shared/agent-session/failures/agent-session-failure.ts +++ b/src/shared/agent-session/failures/agent-session-failure.ts @@ -52,6 +52,9 @@ export const AGENT_SESSION_FAILURE_KINDS = [ 'providerExited', 'restartFailed', 'providerRejected', + 'providerUnavailable', + 'providerSafetyStopped', + 'providerResponseFailed', 'pluginInputRequired', 'modelUnavailable', /** The agent had no model when the message reached it; why (selection, sign-in) is unknown. */ @@ -101,6 +104,9 @@ export function isAgentSessionFailureKind(value: unknown): value is AgentSession /** Kinds only a status row reports: none is ever why a message was not sent. */ const STATUS_ROW_ONLY_FAILURE_KINDS = [ + 'providerUnavailable', + 'providerSafetyStopped', + 'providerResponseFailed', 'compactionFailed', 'compactionUnconfirmed', 'cancelUnconfirmed', diff --git a/src/shared/agent-session/failures/agent-session-model-failure-words.ts b/src/shared/agent-session/failures/agent-session-model-failure-words.ts new file mode 100644 index 00000000000..660566ebbda --- /dev/null +++ b/src/shared/agent-session/failures/agent-session-model-failure-words.ts @@ -0,0 +1,19 @@ +import type { AgentSessionFailureFact } from './agent-session-failure' +import type { AgentSessionFailureSay } from './agent-session-failure-copy' +import type { + AgentSessionFailureSurface, + AgentSessionFailureWordsContext +} from './agent-session-failure-words' +import { agent } from './agent-session-attachment-failure-words' + +export function modelUnavailableSentence( + context: AgentSessionFailureWordsContext, + fact: AgentSessionFailureFact, + _surface: AgentSessionFailureSurface, + say: AgentSessionFailureSay +): string { + const words = { ...agent(say, context), detail: fact.detail?.text ?? say('selectedModel') } + return fact.agentUpdate + ? say('modelUnavailableUpdate', { ...words, minimumVersion: fact.agentUpdate }) + : say('modelUnavailable', words) +} From 2e1c6475ee1147829868239a9a1e5394ab1b6ecf Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:35:26 -0800 Subject: [PATCH 06/28] Use Claude CLI titles for default native chat naming (#40069) * Reuse Claude CLI titles for default chat naming * Preserve independent naming while fencing Claude title reuse * Keep optional title account lookup from blocking chat naming * Clarify deferred preparation in naming tests * Bind first chat naming to its ready handover * Exercise title cleanup through a real queued command * Use the timestamped operation fixture for compaction * test: exercise naming fallback through Send now * test: exercise terminal naming fallback after final starts --- .../claude-agent-sdk-control-requests.test.ts | 15 + .../claude-agent-sdk-control-requests.ts | 10 + .../claude-conversation-title-owner.test.ts | 111 +++ .../claude-conversation-title-owner.ts | 88 +++ .../claude-structured-session-adapter.ts | 2 + ...ctured-agent-session-delivery-loop-deps.ts | 5 + .../structured-agent-session-delivery-loop.ts | 4 + .../structured-agent-session-host-delivery.ts | 12 +- .../structured-agent-session-conversations.ts | 5 +- .../structured-agent-session-host-types.ts | 4 + .../host/structured-agent-session-host.ts | 6 +- ...ueued-message-rig-provider.test-fixture.ts | 14 +- ...session-queued-message-rig.test-fixture.ts | 19 +- ...structured-agent-session-adapter-router.ts | 3 + .../structured-agent-session-adapter.ts | 4 + .../structured-chat-name-publication.ts | 50 ++ .../structured-chat-naming-title.test.ts | 134 ++++ .../structured-chat-naming-types.ts | 52 ++ .../structured-chat-naming-work.ts | 69 +- .../native-chat/structured-chat-naming.ts | 140 ++-- .../structured-chat-title-handover.ts | 183 +++++ .../structured-chat-naming-runtime.test.ts | 643 +++++++++++++++++- .../naming/structured-chat-naming-runtime.ts | 170 ++++- .../agent-session-record-conversation-name.ts | 7 +- .../records/agent-session-record-store.ts | 16 +- 25 files changed, 1636 insertions(+), 130 deletions(-) create mode 100644 src/main/claude/structured/claude-conversation-title-owner.test.ts create mode 100644 src/main/claude/structured/claude-conversation-title-owner.ts create mode 100644 src/main/native-chat/structured-chat-name-publication.ts create mode 100644 src/main/native-chat/structured-chat-naming-title.test.ts create mode 100644 src/main/native-chat/structured-chat-naming-types.ts create mode 100644 src/main/native-chat/structured-chat-title-handover.ts diff --git a/src/main/claude/claude-agent-sdk-control-requests.test.ts b/src/main/claude/claude-agent-sdk-control-requests.test.ts index f2e7f19fff6..ccec589542b 100644 --- a/src/main/claude/claude-agent-sdk-control-requests.test.ts +++ b/src/main/claude/claude-agent-sdk-control-requests.test.ts @@ -6,6 +6,21 @@ afterEach(() => { vi.useRealTimers() }) +it('bounds the optional persisted title request and ignores its late response', async () => { + vi.useFakeTimers() + const pending = Promise.withResolvers() + const generateSessionTitle = vi.fn(() => pending.promise) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only the feature-detected method is called. + const controls = createClaudeControlSurface({ generateSessionTitle } as unknown as Query) + const result = controls.generateSessionTitle!('Repair login') + const expired = expect(result).rejects.toThrow('claude generate_session_title request timed out') + await vi.advanceTimersByTimeAsync(30_000) + await expired + pending.resolve('Late title') + await expect(result).rejects.toThrow('timed out') + expect(generateSessionTitle).toHaveBeenCalledWith('Repair login', { persist: true }) +}) + describe('createClaudeControlSurface stopTask', () => { it('bounds a lost reply and permits a later stop request', async () => { vi.useFakeTimers() diff --git a/src/main/claude/claude-agent-sdk-control-requests.ts b/src/main/claude/claude-agent-sdk-control-requests.ts index 980093a4e9c..addeba66e28 100644 --- a/src/main/claude/claude-agent-sdk-control-requests.ts +++ b/src/main/claude/claude-agent-sdk-control-requests.ts @@ -114,6 +114,7 @@ export type ClaudeControlSurface = { getContextUsage: (options?: ClaudeControlOptions) => Promise /** Names `sessionId` as the user's rename; refused once the CLI has moved to another session. */ renameSession: (title: string, sessionId: string, options?: ClaudeControlOptions) => Promise + generateSessionTitle?: (description: string) => Promise } type InterruptingQuery = { @@ -123,7 +124,16 @@ type InterruptingQuery = { } export function createClaudeControlSurface(query: Query): ClaudeControlSurface { + const title = 'generateSessionTitle' in query ? query.generateSessionTitle : null return { + ...(typeof title === 'function' + ? { + generateSessionTitle: (description: string) => + runClaudeControl('generate_session_title', () => + Promise.resolve(title.call(query, description, { persist: true })) + ) + } + : {}), interrupt: (options) => runClaudeControl( 'interrupt', diff --git a/src/main/claude/structured/claude-conversation-title-owner.test.ts b/src/main/claude/structured/claude-conversation-title-owner.test.ts new file mode 100644 index 00000000000..5a31653bbd5 --- /dev/null +++ b/src/main/claude/structured/claude-conversation-title-owner.test.ts @@ -0,0 +1,111 @@ +import { ClaudeProfileRouter } from '../../claude-accounts/claude-profile-router' +import * as installedRouter from '../../claude-accounts/claude-profile-installed-router' +import { afterEach, expect, it, vi } from 'vitest' +import { acquired, fakeClaude, PROVIDER_SESSION_ID } from './claude-structured-session-test-support' +import { agentSessionRecordFixture } from '../../../shared/agent-session/lifecycle/agent-session-record.test-fixture' + +afterEach(() => vi.restoreAllMocks()) + +it('reads only cached support and rejects a retired acquisition or changed account/context', async () => { + const cliFlag = vi.fn(async () => 'supported' as const) + const cli = fakeClaude() + const router = new ClaudeProfileRouter({ + getSettings: () => ({ + claudeManagedAccounts: [], + activeClaudeManagedAccountId: null, + agentStatusHooksEnabled: false, + disabledTuiAgents: [] + }), + dataRoot: '/isolated', + userHome: '/users/test', + env: {} + }) + const preparation = vi.spyOn(router, 'preparation') + vi.spyOn(installedRouter, 'getClaudeProfileRouter').mockReturnValue(router) + const adapter = await acquired(cli, { + cliFlag, + account: 'system', + claudeConfigDir: '/accounts/claude', + accountRecipe: { route: 'system', home: '/accounts/claude', configDir: '/accounts/claude' } + }) + try { + const record = agentSessionRecordFixture() + record.sessionId = 'session-1' + record.accountHome = { variable: 'CLAUDE_CONFIG_DIR', path: '/accounts/claude' } + record.providerHandleChain[0]!.handle.nativeId = PROVIDER_SESSION_ID + const generate = vi.fn(async () => 'Broken chat title') + cli.connections[0]!.generateSessionTitle = generate + const owner = adapter.captureConversationTitleOwner(record)! + expect(owner.isCurrent(record)).toBe(true) + expect(await owner.generate('Repair login', () => owner.isCurrent(record))).toBe( + 'Broken chat title' + ) + expect(cliFlag).toHaveBeenLastCalledWith( + { option: 'generate_session_title', firstVersion: '2.1.310' }, + 250, + false + ) + expect( + owner.isCurrent({ ...record, accountHome: { variable: 'CLAUDE_CONFIG_DIR', path: '/other' } }) + ).toBe(false) + expect( + owner.isCurrent({ + ...record, + providerContextBoundary: { operationId: 'clear-1', afterFence: 7, clearedAt: 1 } + }) + ).toBe(false) + const system = router.preparation() + preparation.mockReturnValue({ + ...system, + provenance: 'profile:other', + configDir: '/accounts/other' + }) + expect(owner.isCurrent(record)).toBe(false) + preparation.mockReturnValue(system) + expect(owner.isCurrent(record)).toBe(true) + await adapter.closeAll() + expect(owner.isCurrent(record)).toBe(false) + } finally { + await adapter.closeAll() + cli.dispose() + } +}) + +it.each(['unknown', 'unsupported'] as const)( + 'keeps %s CLIs on the existing generator path', + async (support) => { + const cli = fakeClaude() + const adapter = await acquired(cli, { cliFlag: async () => support }) + try { + const record = agentSessionRecordFixture() + record.sessionId = 'session-1' + record.providerHandleChain[0]!.handle.nativeId = PROVIDER_SESSION_ID + const generate = vi.fn(async () => 'Do not use') + cli.connections[0]!.generateSessionTitle = generate + const owner = adapter.captureConversationTitleOwner(record)! + expect(await owner.generate('Repair login', () => true)).toBeNull() + expect(generate).not.toHaveBeenCalled() + } finally { + await adapter.closeAll() + cli.dispose() + } + } +) + +it('retains retirement knowledge when the optional title method is missing', async () => { + const cli = fakeClaude() + const adapter = await acquired(cli) + try { + const record = agentSessionRecordFixture() + record.sessionId = 'session-1' + record.providerHandleChain[0]!.handle.nativeId = PROVIDER_SESSION_ID + const owner = adapter.captureConversationTitleOwner(record)! + expect(owner.isCurrent(record)).toBe(true) + expect(await owner.generate('Repair login', () => owner.isCurrent(record))).toBeNull() + await adapter.closeAll() + expect(owner.isCurrent(record)).toBe(false) + } finally { + await adapter.closeAll() + cli.dispose() + } +}) diff --git a/src/main/claude/structured/claude-conversation-title-owner.ts b/src/main/claude/structured/claude-conversation-title-owner.ts new file mode 100644 index 00000000000..640b72c668c --- /dev/null +++ b/src/main/claude/structured/claude-conversation-title-owner.ts @@ -0,0 +1,88 @@ +import { resolve } from 'node:path' +import { getClaudeProfileRouter } from '../../claude-accounts/claude-profile-installed-router' +import { agentSessionAccountHomesEqual } from '../../../shared/agent-session/lifecycle/agent-session-account-home' +import { claudeNamingContextId } from '../claude-conversation-naming' +import { CLAUDE_CACHED_FLAG_CHECK_BUDGET_MS } from '../claude-cli-flag-support' +import type { StructuredAgentSessionAdapter } from '../../native-chat/agent-session-wire/providers/structured-agent-session-adapter' +import type { ClaudeSession } from './claude-structured-session-state' + +export const captureClaudeConversationTitleOwner = + ( + sessions: Map + ): NonNullable => + (record) => { + const session = sessions.get(record.sessionId) + if (!session) { + return null + } + const { connection, acquisitionGeneration, fence, providerSessionId, initialization } = session + const boundary = record.providerContextBoundary?.operationId + const account = () => { + try { + const policy = getClaudeProfileRouter()?.preparation() + const recipe = initialization.accountRecipe + if (policy) { + if ((recipe?.route ?? initialization.account) === 'managed') { + if ( + !policy.provenance.startsWith('profile:') || + (recipe?.route === 'managed' && + policy.provenance !== `profile:${recipe.accountId}`) || + resolve(initialization.cwd, policy.configDir) !== initialization.claudeConfigDir + ) { + return null + } + } else if (policy.provenance !== 'system') { + return null + } + } + return JSON.stringify([ + policy?.provenance, + policy?.provenance.startsWith('profile:') ? policy.configDir : null + ]) + } catch { + return null + } + } + const selectedAccount = account() + const isCurrent: NonNullable< + ReturnType> + >['isCurrent'] = (current) => + sessions.get(record.sessionId) === session && + session.connection === connection && + !connection.closed && + !session.closeFinalization && + session.acquisitionGeneration === acquisitionGeneration && + session.fence === fence && + current.lease.runtimeFence === fence && + current.provider === 'claude' && + session.providerSessionId === providerSessionId && + claudeNamingContextId(current) === providerSessionId && + current.providerContextBoundary?.operationId === boundary && + agentSessionAccountHomesEqual(current.accountHome, record.accountHome) && + current.location.workspaceId === record.location.workspaceId && + current.location.executionHostId === record.location.executionHostId && + session.initialization === initialization && + selectedAccount !== null && + account() === selectedAccount + return { + isCurrent, + generate: async (firstPrompt, shouldExecute) => { + if ( + !session.initialization.startup.opened() || + !connection.generateSessionTitle || + !shouldExecute() + ) { + return null + } + // Verified support floor, not the request's introduction version; never start a probe here. + const supported = await session.cliFlag?.( + { option: 'generate_session_title', firstVersion: '2.1.310' }, + CLAUDE_CACHED_FLAG_CHECK_BUDGET_MS, + false + ) + return supported === 'supported' && shouldExecute() + ? connection.generateSessionTitle?.(firstPrompt) + : null + } + } + } diff --git a/src/main/claude/structured/claude-structured-session-adapter.ts b/src/main/claude/structured/claude-structured-session-adapter.ts index 08a6f150dfc..45ea207c93a 100644 --- a/src/main/claude/structured/claude-structured-session-adapter.ts +++ b/src/main/claude/structured/claude-structured-session-adapter.ts @@ -50,9 +50,11 @@ import { claudePromptCancelRoute } from './journal/claude-structured-prompt-repl import { claudeForkForRewind } from '../claude-rewind-fork' import { copyClaudeConversation } from '../claude-conversation-copy' import { ClaudeConversationNaming } from '../claude-conversation-naming' +import { captureClaudeConversationTitleOwner } from './claude-conversation-title-owner' export class ClaudeStructuredSessionAdapter implements StructuredAgentSessionAdapter { private readonly sessions = new Map() + captureConversationTitleOwner = captureClaudeConversationTitleOwner(this.sessions) private readonly acquisitions = new ClaudeAcquisitionRegistry() private readonly exits = new Map() private readonly settledExitErrors = new Map() diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop-deps.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop-deps.ts index b8502b87fe4..040de3949c9 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop-deps.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop-deps.ts @@ -1,4 +1,5 @@ import type { AgentJournalSubmission } from '../../../../shared/agent-session/journal/agent-session-journal-types' +import type { AgentSessionJournal } from '../../agent-session-journal/journal-store' import type { UnsentSendHold } from '../../agent-session-journal/queued-messages/journal-unsent-send-hold' import type { AgentChatPermissionChoice } from '../../../../shared/agent-launch/permissions/agent-chat-permission-mode' import type { StructuredAgentSessionStartFlights } from '../startup/structured-agent-session-start-flight' @@ -20,6 +21,10 @@ import type { } from '../host/structured-agent-session-host-types' export type StructuredAgentSessionDeliveryLoopDeps = { + captureTitleSourceUnavailable?: ( + sessionId: string, + journal: AgentSessionJournal + ) => (() => void) | undefined startFlights?: Pick deliveryOrder: StructuredAgentSessionDeliveryOrder sessions: ReadonlyMap diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop.ts index 392fab76744..584b12e8095 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-delivery-loop.ts @@ -299,6 +299,10 @@ export class StructuredAgentSessionDeliveryLoop { /** Clear under serialization so the next accept can wake a new loop. */ private stop(sessionId: string): 'stop' { + const journal = this.deps.sessions.get(sessionId)?.journal + if (journal && this.running.has(sessionId)) { + this.deps.captureTitleSourceUnavailable?.(sessionId, journal)?.() + } this.running.delete(sessionId) this.deps.deliveryOrder.parkSends(sessionId) return 'stop' diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-host-delivery.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-host-delivery.ts index 4d22aeb5e8a..7537b242ae6 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-host-delivery.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-host-delivery.ts @@ -87,6 +87,8 @@ export function createStructuredAgentSessionConversationDelivery(input: { > }): StructuredAgentSessionConversationDelivery { const { deps, sessions } = input + const captureTitleSourceUnavailable = (sessionId: string, journal: AgentSessionJournal) => + deps.conversationNaming?.captureTitleSourceUnavailable?.(sessionId, journal) const retainedWork = (sessionId: string, continuingId?: string) => ({ currentWork: () => hostStructuredAgentSessionCurrentWork({ store: deps.store, sessions }, sessionId), @@ -101,6 +103,7 @@ export function createStructuredAgentSessionConversationDelivery(input: { [...(sessions.get(sessionId)?.child?.heldSends?.() ?? [])].some((id) => id !== continuingId) }) const loop = new StructuredAgentSessionDeliveryLoop({ + captureTitleSourceUnavailable, sessions, adapter: deps.adapter, agents: deps.agents, @@ -239,13 +242,19 @@ export function createStructuredAgentSessionConversationDelivery(input: { const wakesQueued = new Set() const afterCommit = (sessionId: string, journal: AgentSessionJournal): void => { input.deliveryOrder.reconcile(sessionId, journal) + if (wakesQueued.has(sessionId)) { + return + } + const sourceUnavailable = captureTitleSourceUnavailable(sessionId, journal) const work = hostStructuredAgentSessionCurrentWork({ store: deps.store, sessions }, sessionId) if ( - wakesQueued.has(sessionId) || !work || structuredAgentSessionCommandRunning(work) || !selectableStructuredSubmission(journal, work) ) { + if (!loop.isRunning(sessionId)) { + sourceUnavailable?.() + } return } wakesQueued.add(sessionId) @@ -256,6 +265,7 @@ export function createStructuredAgentSessionConversationDelivery(input: { }) .catch((error: unknown) => { wakesQueued.delete(sessionId) + sourceUnavailable?.() deps.logger.warn('waking the delivery loop after a commit failed', { scope: 'delivery-wake', sessionId, diff --git a/src/main/native-chat/agent-session-wire/host/structured-agent-session-conversations.ts b/src/main/native-chat/agent-session-wire/host/structured-agent-session-conversations.ts index dba13c59687..69faf212bc2 100644 --- a/src/main/native-chat/agent-session-wire/host/structured-agent-session-conversations.ts +++ b/src/main/native-chat/agent-session-wire/host/structured-agent-session-conversations.ts @@ -138,7 +138,10 @@ export class StructuredAgentSessionConversations extends Map< return adopted } - observeConversationPublications(sessionId: string, observer: PublicationObserver): () => void { + observeConversationPublications = ( + sessionId: string, + observer: PublicationObserver + ): (() => void) => { if (this.ended) { this.deliverHint(observer, { kind: 'end', source: null, journal: null }, () => {}) return () => {} diff --git a/src/main/native-chat/agent-session-wire/host/structured-agent-session-host-types.ts b/src/main/native-chat/agent-session-wire/host/structured-agent-session-host-types.ts index 47a2ed7ede4..9673d8f4f10 100644 --- a/src/main/native-chat/agent-session-wire/host/structured-agent-session-host-types.ts +++ b/src/main/native-chat/agent-session-wire/host/structured-agent-session-host-types.ts @@ -245,6 +245,10 @@ export type SetUserConversationName = StructuredAgentSessionConversationNaming[' export type StructuredChatNamingFailure = AgentSessionNamingError & { forName: string } export type StructuredAgentSessionConversationNaming = { + captureTitleSourceUnavailable?: ( + sessionId: string, + journal: AgentSessionJournal + ) => (() => void) | undefined publishName: (workspaceId: string, sessionId: string) => void activeSessionIds: () => string[] failure: (sessionId: string) => StructuredChatNamingFailure | undefined diff --git a/src/main/native-chat/agent-session-wire/host/structured-agent-session-host.ts b/src/main/native-chat/agent-session-wire/host/structured-agent-session-host.ts index 2413f4f7a98..1d4ed8ab39b 100644 --- a/src/main/native-chat/agent-session-wire/host/structured-agent-session-host.ts +++ b/src/main/native-chat/agent-session-wire/host/structured-agent-session-host.ts @@ -304,9 +304,9 @@ export class StructuredAgentSessionHost { readonly continuationContext: ReturnType - observeConversationPublications: typeof this.sessions.observeConversationPublications = ( - ...args - ) => this.sessions.observeConversationPublications(...args) + observeConversationPublications = this.sessions.observeConversationPublications + beginConversationTitleWait = (sessionId: string) => + this.runtimeState.acquireAborts.begin(sessionId) journalSnapshot = (sessionId: string) => this.backgroundTasks.journalSnapshot(sessionId) journalCurrentRead = this.reads.journalCurrentRead queuedSendAnswer = this.reads.queuedSendAnswer diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts index 49d54ab848a..8bd8ec50fd5 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts @@ -1,5 +1,5 @@ import type { AgentMessageSource } from '../../../../../shared/agent-session/agent-session-message-source' -import { vi, type Mock } from 'vitest' +import { expect, vi, type Mock } from 'vitest' import { parseAgentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import { activeProviderContext } from '../../../../../shared/agent-session/agent-session-provider-context' @@ -35,6 +35,18 @@ export type QueuedRigProviderOptions = { /** How long a winding-down child is given after the interrupt before the Stop ends it. */ export const QUEUED_RIG_STOP_GRACE_MS = 2_000 +export async function proveQueuedRigStart( + host: StructuredAgentSessionHost, + sessionId: string, + proveOpen: () => void +): Promise { + await eventually(() => + expect(host.collaboratorsForTests().sessions.get(sessionId)?.child?.phase).toBe('starting') + ) + proveOpen() + await Promise.resolve() +} + export function createQueuedRigProvider( store: Pick, options: QueuedRigProviderOptions, diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts index d5bc36ec59d..7325f03f268 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts @@ -32,11 +32,13 @@ import { seedQueuedRigLegacyJournal } from './structured-agent-session-queued-le import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { createQueuedRigProvider, + proveQueuedRigStart, eventually, type RigSendOptions, type QueuedRigProviderOptions } from './structured-agent-session-queued-message-rig-provider.test-fixture' import { claudeAndCodexDeclared } from '../../providers/testing/structured-agent-session-adapter-router-test-support' +import { createTestParams } from './structured-agent-session-create-test-fixture' export const QUEUED_RIG_CALLER = { callerKey: 'client-1' } export type QueuedMessageTestRig = Awaited> @@ -44,6 +46,7 @@ export type QueuedMessageTestRig = Awaited number @@ -97,7 +100,11 @@ export async function createQueuedMessageTestRig( : {}) }) const pinLegacy = options.legacyInputs && seedQueuedRigLegacyJournal(root, SESSION, options.agent) - expect(await host.attach(QUEUED_RIG_CALLER, firstAttach)).toMatchObject({ ok: true }) + expect( + options.atRest + ? await host.create(QUEUED_RIG_CALLER, createTestParams(undefined, firstAttach)) + : await host.attach(QUEUED_RIG_CALLER, firstAttach) + ).toMatchObject({ ok: true }) if (pinLegacy) { pinLegacy(host) } @@ -274,14 +281,6 @@ export async function createQueuedMessageTestRig( }) } - async function proveStart(): Promise { - await eventually(() => - expect(host.collaboratorsForTests().sessions.get(SESSION)?.child?.phase).toBe('starting') - ) - provider.proveOpen() - await Promise.resolve() - } - async function dispose(): Promise { await host.flushAllStreamedEvents() await closeTestJournalHostDatabase(root) @@ -325,7 +324,7 @@ export async function createQueuedMessageTestRig( queuePause, restartOffers, resume, - proveStart, + proveStart: () => proveQueuedRigStart(host, SESSION, provider.proveOpen), dispose } } diff --git a/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter-router.ts b/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter-router.ts index 3427d6bf0c6..492ff9bc30c 100644 --- a/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter-router.ts +++ b/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter-router.ts @@ -96,6 +96,9 @@ export class StructuredAgentSessionAdapterRouter implements Adapter { } } + captureConversationTitleOwner: NonNullable = (record) => + this.liveOwnerOrNull(record.sessionId)?.captureConversationTitleOwner?.(record) ?? null + renameProviderSession: NonNullable = (input) => this.capabilityOwner( input.record.sessionId, diff --git a/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter.ts b/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter.ts index d327cb5c9d3..31a3adcbf6b 100644 --- a/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter.ts +++ b/src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter.ts @@ -347,6 +347,10 @@ export type StructuredAgentSessionAdapter = StructuredAgentSessionAdapterStop & inheritance?: boolean }): Promise | undefined stopProviderNaming?(): void + captureConversationTitleOwner?(record: AgentSessionRecord): { + isCurrent: (record: AgentSessionRecord) => boolean + generate: (firstPrompt: string, shouldExecute: () => boolean) => Promise + } | null /** True proves child exit; root/processless verdicts throw only after finalization (stopAgentSessionProviderRoot). */ closeSession?( sessionId: string, diff --git a/src/main/native-chat/structured-chat-name-publication.ts b/src/main/native-chat/structured-chat-name-publication.ts new file mode 100644 index 00000000000..f344d0fc605 --- /dev/null +++ b/src/main/native-chat/structured-chat-name-publication.ts @@ -0,0 +1,50 @@ +import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' +import type { StructuredChatNamingDeps } from './structured-chat-naming' + +export async function runStructuredChatNameGeneration(input: { + automaticTitle?: Parameters[3] + getRecord: () => AgentSessionRecord | null | undefined + applicable: (current?: AgentSessionRecord) => boolean + generate: ( + stillCurrent: () => boolean, + automaticTitle?: Parameters[3] + ) => Promise<{ name: string; shouldApply?: (record?: AgentSessionRecord) => boolean } | null> + setName: ( + name: string, + applicable: (record: AgentSessionRecord) => boolean + ) => Promise +}): Promise { + let owner: ((record: AgentSessionRecord) => boolean) | undefined + const stillCurrent = (latest?: AgentSessionRecord) => { + const record = latest ?? input.getRecord() + return !!record && input.applicable(record) && (owner?.(record) ?? true) + } + if (!stillCurrent()) { + return + } + const name = await input.generate( + stillCurrent, + input.automaticTitle + ? { + ...input.automaticTitle, + retainOwnership: (check) => { + owner = check + } + } + : undefined + ) + if (!name || !stillCurrent()) { + return + } + const named = await input.setName( + name.name, + (current) => stillCurrent(current) && (name.shouldApply?.(current) ?? true) + ) + // Independent fallback retains the canonical name/context owner; only timeout retains live ownership. + if (!named && name.shouldApply && stillCurrent()) { + const fallback = await input.generate(stillCurrent) + if (fallback && stillCurrent()) { + await input.setName(fallback.name, stillCurrent) + } + } +} diff --git a/src/main/native-chat/structured-chat-naming-title.test.ts b/src/main/native-chat/structured-chat-naming-title.test.ts new file mode 100644 index 00000000000..c83dbaab30a --- /dev/null +++ b/src/main/native-chat/structured-chat-naming-title.test.ts @@ -0,0 +1,134 @@ +import { expect, it, vi } from 'vitest' +import { + createStructuredChatNamingHandler, + type StructuredChatNamingDeps +} from './structured-chat-naming' +import { agentSessionRecordFixture } from './agent-session-record-test-fixture' +import { + compareAndSetAgentSessionRecordName, + setAgentSessionRecordUserName +} from '../runtime/agent-session/records/agent-session-record-conversation-name' + +function rig( + generate: StructuredChatNamingDeps['generate'], + captureAutomaticTitle: StructuredChatNamingDeps['captureAutomaticTitle'] = () => ({ owner: null }) +) { + let record = agentSessionRecordFixture({ sessionId: 'chat-1' }) + let beforeWrite: (() => Promise) | undefined + const handler = createStructuredChatNamingHandler({ + getStore: () => ({ + getRecord: () => record, + compareAndSetConversationName: async (_id, name, expected, context, applicable) => + compareAndSetAgentSessionRecordName( + async (apply) => { + if (expected !== null) { + await beforeWrite?.() + } + record = apply(record) + return record + }, + name, + expected, + context, + applicable + ), + setUserConversationName: async (_id, name, write) => { + const result = setAgentSessionRecordUserName(record, name, write, 1) + record = result.record + return result + } + }), + getSettings: () => ({ nativeChatAutoName: true }), + readFirstPrompt: async () => 'Repair login', + readJournalFirstPrompt: async () => 'Repair login', + workerTaskTitle: () => null, + generate, + captureAutomaticTitle, + wait: async () => {}, + onNamed: () => {}, + logger: { warn: vi.fn(), error: vi.fn() } + }) + return { + handler, + record: () => record, + beforeWrite: (run: () => Promise) => { + beforeWrite = run + }, + start: () => + handler( + { + sessionId: 'chat-1', + workspaceId: 'workspace-1', + agent: 'claude', + status: 'working', + latestPrompt: 'Repair login', + updatedAt: 1 + }, + { replay: false, firstInputSubmissionKey: 'input-1' } + ), + done: () => vi.waitFor(() => expect(handler.activeSessionIds()).toEqual([])) + } +} + +it('tries CLI reuse once while independent generation retries, and skips it on manual regeneration', async () => { + const reuse = vi.fn() + const generate = vi.fn( + async (_record, _prompt, _current, automatic) => { + if (automatic) { + reuse() + } + return generate.mock.calls.length === 1 + ? { error: 'Retry independent generation' } + : { name: 'Login repair' } + } + ) + const state = rig(generate) + state.start() + await state.done() + expect(state.record().conversationName).toBe('Login repair') + expect(reuse).toHaveBeenCalledOnce() + state.handler.regenerate('chat-1') + await state.done() + expect(reuse).toHaveBeenCalledOnce() +}) + +it.each(['owner', 'same-text rename'] as const)( + 'rejects a queued title write after %s changes', + async (change) => { + let owned = true + const state = rig( + async (_record, _prompt, _current, automatic) => + automatic ? { name: 'CLI title', shouldApply: () => owned } : { name: 'Independent title' }, + () => ({ + owner: { isCurrent: () => owned, generate: async () => null } + }) + ) + state.beforeWrite(async () => { + if (change === 'owner') { + owned = false + } else { + await state.handler.setUserName('chat-1', 'Repair login', 'rename') + } + }) + state.start() + await state.done() + expect(state.record().conversationName).toBe( + change === 'owner' ? 'Independent title' : 'Repair login' + ) + } +) + +it('uses current naming settings if they change while the title write is queued', async () => { + let eligible = true + const generate = vi.fn( + async (_record, _prompt, _current, automatic) => + automatic ? { name: 'CLI title', shouldApply: () => eligible } : { name: 'Configured title' } + ) + const state = rig(generate) + state.beforeWrite(async () => { + eligible = false + }) + state.start() + await state.done() + expect(state.record().conversationName).toBe('Configured title') +}) diff --git a/src/main/native-chat/structured-chat-naming-types.ts b/src/main/native-chat/structured-chat-naming-types.ts new file mode 100644 index 00000000000..9f8b4c8fb34 --- /dev/null +++ b/src/main/native-chat/structured-chat-naming-types.ts @@ -0,0 +1,52 @@ +import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' +import type { GlobalSettings } from '../../shared/global-settings-types' +import type { AgentSessionRecordStore } from '../runtime/agent-session/records/agent-session-record-store' +import type { AgentGenerationFailureOutput } from '../text-generation/agent-failure-output' +import type { StructuredAgentSessionLogger } from './agent-session-wire/host/structured-agent-session-logger' +import type { StructuredAgentSessionAdapter } from './agent-session-wire/providers/structured-agent-session-adapter' +import type { AgentSessionJournal } from './agent-session-journal/journal-store' + +export type AutomaticChatTitle = { + owner: ReturnType> + pending?: { + ready: Promise + ended: () => boolean + recheck: () => void + unavailable: (journal: AgentSessionJournal) => void + dispose: () => void + } + retainOwnership?: (isCurrent: (record: AgentSessionRecord) => boolean) => void +} + +/** null means unavailable/canceled: no failure to display or retry. */ +export type StructuredChatNameGeneration = + | { name: string; shouldApply?: (record?: AgentSessionRecord) => boolean } + | { error: string; errorCode?: string; failureOutput?: AgentGenerationFailureOutput } + | null + +export type StructuredChatNamingDeps = { + getStore: () => Pick< + AgentSessionRecordStore, + 'getRecord' | 'compareAndSetConversationName' | 'setUserConversationName' + > | null + getSettings: () => Pick + readFirstPrompt: (sessionId: string, hostStartedAt: number) => Promise + /** The current context's first prompt, for a name the user asked for again. */ + readJournalFirstPrompt: (sessionId: string) => Promise + now?: () => number + workerTaskTitle: (record: AgentSessionRecord) => { title: string } | null + captureAutomaticTitle?: ( + record: AgentSessionRecord, + firstInputSubmissionKey?: string, + applicable?: () => boolean + ) => AutomaticChatTitle | undefined + generate: ( + record: AgentSessionRecord, + firstPrompt: string, + shouldExecute?: () => boolean, + automaticTitle?: AutomaticChatTitle + ) => Promise + onNamed: (workspaceId: string, sessionId: string) => void + logger: StructuredAgentSessionLogger + wait?: (ms: number) => Promise +} diff --git a/src/main/native-chat/structured-chat-naming-work.ts b/src/main/native-chat/structured-chat-naming-work.ts index 0772d0f3446..c3a9ab0f345 100644 --- a/src/main/native-chat/structured-chat-naming-work.ts +++ b/src/main/native-chat/structured-chat-naming-work.ts @@ -1,8 +1,45 @@ import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' +import type { AutomaticChatTitle, StructuredChatNamingDeps } from './structured-chat-naming-types' +import type { AgentSessionJournal } from './agent-session-journal/journal-store' -export type NamingRequest = { record: AgentSessionRecord; automatic: boolean; firstPrompt?: string } +export type NamingRequest = { + record: AgentSessionRecord + automatic: boolean + firstPrompt?: string + automaticTitle?: AutomaticChatTitle + seedingName?: string | null +} type NamingJob = { desired?: NamingRequest; executing?: NamingRequest; preparing: boolean } +export function namingRequestOwnsRecord(request: NamingRequest, current: AgentSessionRecord) { + return ( + !request.automaticTitle?.pending?.ended() && + current.conversationNameSource !== 'user' && + (current.conversationName === request.record.conversationName || + (request.seedingName !== undefined && current.conversationName === request.seedingName)) && + (current.providerContextBoundary?.operationId ?? null) === + (request.record.providerContextBoundary?.operationId ?? null) + ) +} + +export function automaticChatNamingRequest( + deps: StructuredChatNamingDeps, + record: AgentSessionRecord, + firstInputSubmissionKey: string | undefined, + applicable: (request: NamingRequest) => boolean +): NamingRequest { + const request: NamingRequest = { record, automatic: true } + if ( + record.conversationName === undefined && + !(!record.providerContextBoundary && deps.workerTaskTitle(record)) + ) { + request.automaticTitle = deps.captureAutomaticTitle?.(record, firstInputSubmissionKey, () => + applicable(request) + ) + } + return request +} + export function createStructuredChatNamingWork(deps: { applicable: (request: NamingRequest) => boolean prepare: (request: NamingRequest) => Promise @@ -21,6 +58,7 @@ export function createStructuredChatNamingWork(deps: { .execute(request, firstPrompt) .catch((error: unknown) => deps.warn(sessionId, error)) .finally(() => { + request.automaticTitle?.pending?.dispose() job.executing = undefined drain(sessionId, job) deps.changed(request.record) @@ -31,7 +69,8 @@ export function createStructuredChatNamingWork(deps: { } } return { - run(record: AgentSessionRecord, automatic: boolean) { + run(request: NamingRequest) { + const { record, automatic } = request const { sessionId } = record const job: NamingJob = jobs.get(sessionId) ?? { preparing: false } const pending = job.desired ?? job.executing @@ -42,9 +81,12 @@ export function createStructuredChatNamingWork(deps: { record.providerContextBoundary?.operationId && !(automatic && !pending.automatic && pending.firstPrompt === undefined) ) { + request.automaticTitle?.pending?.dispose() return } - job.desired = { record, automatic } + job.desired?.automaticTitle?.pending?.dispose() + job.executing?.automaticTitle?.pending?.dispose() + job.desired = request jobs.set(sessionId, job) if (!job.preparing) { job.preparing = true @@ -53,10 +95,14 @@ export function createStructuredChatNamingWork(deps: { const request = job.desired try { const prepared = await deps.prepare(request) + if (!prepared) { + request.automaticTitle?.pending?.dispose() + } if (job.desired === request) { job.desired = prepared ?? undefined } } catch (error) { + request.automaticTitle?.pending?.dispose() if (job.desired === request) { job.desired = undefined } @@ -73,6 +119,23 @@ export function createStructuredChatNamingWork(deps: { } deps.changed(record) }, + recheck(sessionId: string) { + const job = jobs.get(sessionId) + job?.desired?.automaticTitle?.pending?.recheck() + job?.executing?.automaticTitle?.pending?.recheck() + }, + captureSourceUnavailable(sessionId: string, journal: AgentSessionJournal) { + const job = jobs.get(sessionId) + const desired = job?.desired?.automaticTitle?.pending + const executing = job?.executing?.automaticTitle?.pending + if (!desired && !executing) { + return undefined + } + return () => { + desired?.unavailable(journal) + executing?.unavailable(journal) + } + }, activeSessionIds: () => [...jobs] .filter( diff --git a/src/main/native-chat/structured-chat-naming.ts b/src/main/native-chat/structured-chat-naming.ts index 98604d411f9..ec4e230b256 100644 --- a/src/main/native-chat/structured-chat-naming.ts +++ b/src/main/native-chat/structured-chat-naming.ts @@ -1,5 +1,19 @@ +import type { + StructuredChatNameGeneration, + StructuredChatNamingDeps +} from './structured-chat-naming-types' +export type { + StructuredChatNameGeneration, + StructuredChatNamingDeps +} from './structured-chat-naming-types' +import { runStructuredChatNameGeneration } from './structured-chat-name-publication' import { chatNamingFailure } from '../../shared/chat-naming-failure-copy' -import { createStructuredChatNamingWork, type NamingRequest } from './structured-chat-naming-work' +import { + automaticChatNamingRequest, + createStructuredChatNamingWork, + namingRequestOwnsRecord, + type NamingRequest +} from './structured-chat-naming-work' import type { StructuredAgentSessionStatusObserverOptions } from './agent-session-wire/observations/structured-agent-session-status-observation' import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' import type { AgentSessionStatusSummary } from '../../shared/agent-session/agent-session-wire' @@ -9,8 +23,6 @@ import { } from '../../shared/source-control/ai/generation/conversation-name-generation' import { hasStructuredChatPromptText } from '../../shared/agent-session/requests/structured-agent-session-first-prompt' import { sliceAtCodeUnitLimit } from '../../shared/surrogate-safe-text-slice' -import type { GlobalSettings } from '../../shared/global-settings-types' -import type { AgentSessionRecordStore } from '../runtime/agent-session/records/agent-session-record-store' import { formatAgentGenerationFailureOutputForDisplay, type AgentGenerationFailureOutput @@ -19,37 +31,7 @@ import type { StructuredAgentSessionConversationNaming, StructuredChatNamingFailure } from './agent-session-wire/host/structured-agent-session-host-types' -import { - neverThrowingStructuredAgentSessionLogger, - type StructuredAgentSessionLogger -} from './agent-session-wire/host/structured-agent-session-logger' - -/** null means unavailable/canceled: no failure to display or retry. */ -export type StructuredChatNameGeneration = - | { name: string } - | { error: string; errorCode?: string; failureOutput?: AgentGenerationFailureOutput } - | null - -export type StructuredChatNamingDeps = { - getStore: () => Pick< - AgentSessionRecordStore, - 'getRecord' | 'compareAndSetConversationName' | 'setUserConversationName' - > | null - getSettings: () => Pick - readFirstPrompt: (sessionId: string, hostStartedAt: number) => Promise - /** The current context's first prompt, for a name the user asked for again. */ - readJournalFirstPrompt: (sessionId: string) => Promise - now?: () => number - workerTaskTitle: (record: AgentSessionRecord) => { title: string } | null - generate: ( - record: AgentSessionRecord, - firstPrompt: string, - shouldExecute?: () => boolean - ) => Promise - onNamed: (workspaceId: string, sessionId: string) => void - logger: StructuredAgentSessionLogger - wait?: (ms: number) => Promise -} +import { neverThrowingStructuredAgentSessionLogger } from './agent-session-wire/host/structured-agent-session-logger' const GENERATION_RETRY_DELAYS_MS = [2000, 4000] const NAMING_ERROR_LIMIT = 1024 @@ -79,6 +61,7 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps error }) const refresh = (workspaceId: string, sessionId: string) => { + work.recheck(sessionId) try { deps.onNamed(workspaceId, sessionId) } catch (error) { @@ -89,33 +72,36 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps const record = deps.getStore()?.getRecord(sessionId) return !!record && contextId(record) === context } - const applicable = (request: NamingRequest) => { - const store = deps.getStore() + const applicable = (request: NamingRequest, latest?: AgentSessionRecord) => { + const current = latest ?? deps.getStore()?.getRecord(request.record.sessionId) return ( - !!store && - generationStillCurrent(store, request.record) && + !!current && + namingRequestOwnsRecord(request, current) && (!request.automatic || deps.getSettings().nativeChatAutoName !== false) ) } const work = createStructuredChatNamingWork({ applicable, prepare, - execute: async (request, firstPrompt) => { - if (!applicable(request)) { - return - } - const name = await generateName( - request.record, - firstPrompt, - request.record.conversationName ?? '', - request.automatic ? GENERATION_RETRY_DELAYS_MS : [], - () => applicable(request) - ) - const store = deps.getStore() - if (name && store && applicable(request)) { - await setName(store, request.record, name) - } - }, + execute: (request, firstPrompt) => + runStructuredChatNameGeneration({ + automaticTitle: request.automaticTitle, + getRecord: () => deps.getStore()?.getRecord(request.record.sessionId), + applicable: (current) => applicable(request, current), + generate: (stillCurrent, automaticTitle) => + generateName( + request.record, + firstPrompt, + request.record.conversationName ?? '', + request.automatic ? GENERATION_RETRY_DELAYS_MS : [], + stillCurrent, + automaticTitle + ), + setName: async (name, applicable) => { + const store = deps.getStore() + return store ? setName(store, request.record, name, applicable) : null + } + }), changed: (record) => refresh(record.location.workspaceId, record.sessionId), warn }) @@ -153,18 +139,24 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps firstPrompt: string, forName: string, retryDelays: readonly number[], - stillCurrent: () => boolean - ): Promise { + stillCurrent: () => boolean, + automaticTitle?: Parameters[3] + ): Promise<{ name: string; shouldApply?: (record?: AgentSessionRecord) => boolean } | null> { attempted.set(record.sessionId, contextId(record)) for (let attempt = 0; stillCurrent(); attempt += 1) { let failure: NamingRunFailure try { - const result = await deps.generate(record, firstPrompt, stillCurrent) + const result = await deps.generate( + record, + firstPrompt, + stillCurrent, + attempt === 0 ? automaticTitle : undefined + ) if (!result) { return null } if ('name' in result && result.name) { - return result.name + return result } failure = 'error' in result ? result : chatNamingFailure('noName') } catch (error) { @@ -189,27 +181,24 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps return null } - async function setName(store: NamingStore, record: AgentSessionRecord, name: string) { + async function setName( + store: NamingStore, + record: AgentSessionRecord, + name: string, + applicable?: (record: AgentSessionRecord) => boolean + ) { const named = await store.compareAndSetConversationName( record.sessionId, name, record.conversationName ?? null, - contextId(record) + contextId(record), + applicable ) if (named) { forget(record.sessionId) refresh(named.location.workspaceId, named.sessionId) } - } - - const generationStillCurrent = (store: NamingStore, record: AgentSessionRecord) => { - const current = store.getRecord(record.sessionId) - return ( - current !== null && - current.conversationNameSource !== 'user' && - current.conversationName === record.conversationName && - contextId(current) === contextId(record) - ) + return named } async function prepare(request: NamingRequest): Promise { @@ -241,9 +230,10 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps return { ...request, firstPrompt } } // Seeding is independent of the expensive execution owner. + request.seedingName = conversationNameSeed(firstPrompt) const seeded = await store.compareAndSetConversationName( record.sessionId, - conversationNameSeed(firstPrompt), + request.seedingName, null, contextId(record) ) @@ -251,6 +241,7 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps return null } request.record = seeded + delete request.seedingName request.firstPrompt = firstPrompt refresh(seeded.location.workspaceId, seeded.sessionId) return request @@ -274,7 +265,9 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps return } attempted.set(summary.sessionId, contextId(record)) - work.run(record, true) + work.run( + automaticChatNamingRequest(deps, record, options.firstInputSubmissionKey, applicable) + ) } catch (error) { warn(summary.sessionId, error) } @@ -282,6 +275,7 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps const naming: StructuredAgentSessionConversationNaming = { publishName: refresh, + captureTitleSourceUnavailable: work.captureSourceUnavailable, activeSessionIds: work.activeSessionIds, failure, failureOutput: (sessionId) => { @@ -294,7 +288,7 @@ export function createStructuredChatNamingHandler(deps: StructuredChatNamingDeps if (!store || !record) { return false } - work.run(record, false) + work.run({ record, automatic: false }) return true }, // Not a job: a pending generation must not swallow the user's rename. The record's 'user' diff --git a/src/main/native-chat/structured-chat-title-handover.ts b/src/main/native-chat/structured-chat-title-handover.ts new file mode 100644 index 00000000000..ecd72d6daad --- /dev/null +++ b/src/main/native-chat/structured-chat-title-handover.ts @@ -0,0 +1,183 @@ +import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' +import { sessionCrewChanges } from '../runtime/orchestration/crew/crew-changes' +import type { StructuredAgentSessionHost } from './agent-session-wire/host/structured-agent-session-host' +import type { JournalPublication } from './agent-session-wire/host/structured-agent-session-conversations' +import type { AutomaticChatTitle } from './structured-chat-naming-types' + +export function deferredStructuredChatTitle(input: { + record: AgentSessionRecord + firstInputSubmissionKey: string + host: StructuredAgentSessionHost + currentHost: () => StructuredAgentSessionHost | null + applicable: () => boolean + eligible: () => boolean + subscribeSettings: (changed: () => void) => () => void +}): NonNullable { + const { sessionId } = input.record + const ready = Promise.withResolvers() + const releases: (() => void)[] = [] + let settled = false + let ended = false + let publication: JournalPublication | undefined + const release = (dispose: () => void) => (settled ? dispose() : releases.push(dispose)) + const settle = (owner: AutomaticChatTitle['owner'] = null) => { + if (settled) { + return + } + settled = true + for (const dispose of releases.splice(0)) { + dispose() + } + ready.resolve(owner) + } + const dispose = () => { + ended = true + settle() + } + const store = input.host.deps.store + const recheck = () => { + if (settled) { + return + } + try { + if ( + input.currentHost() !== input.host || + input.host.deps.store !== store || + !input.applicable() + ) { + dispose() + } else if (!input.eligible()) { + settle() + } + } catch { + settle() + } + } + const pending: NonNullable = { + ready: ready.promise, + ended: () => ended, + recheck, + unavailable: (journal) => { + if (publication?.journal === journal) { + settle() + } + }, + dispose + } + let key: unknown + try { + key = JSON.parse(input.firstInputSubmissionKey) + } catch { + settle() + } + if ( + !Array.isArray(key) || + key.length !== 2 || + typeof key[0] !== 'string' || + !key[0] || + typeof key[1] !== 'string' || + !key[1] + ) { + settle() + return pending + } + const [epoch, clientMessageId] = key + const abort = input.host.beginConversationTitleWait(sessionId) + release(abort.end) + const aborted = () => settle() + abort.signal.addEventListener('abort', aborted, { once: true }) + release(() => abort.signal.removeEventListener('abort', aborted)) + if (abort.signal.aborted) { + settle() + } + if (settled) { + return pending + } + try { + release(input.subscribeSettings(recheck)) + release( + sessionCrewChanges.subscribe((facets) => { + if (facets.has('topology') || facets.has('records')) { + recheck() + } + }) + ) + release( + input.host.observeConversationPublications(sessionId, { + next: (hint) => { + if (settled) { + return + } + if (hint.kind === 'end') { + dispose() + return + } + recheck() + if (settled) { + return + } + if ( + hint.kind === 'detach' || + !hint.source || + !hint.journal || + hint.cursor?.epoch !== epoch + ) { + settle() + return + } + if (publication && (publication.source !== hint.source || hint.kind === 'reset')) { + settle() + return + } + const submission = hint.journal.submission(clientMessageId) + const card = submission?.queuedMessageId + ? hint.journal.queuedMessages.get(submission.queuedMessageId) + : undefined + if ( + !submission || + submission.dispatchState !== 'pending' || + card?.holdReason || + card?.state === 'withdrawn' || + card?.state === 'returned' + ) { + settle() + return + } + if (!publication) { + publication = hint + // A reset cannot prove which child first received an already handed-over input. + if ( + hint.kind !== 'reset' || + submission.handedOverAt !== undefined || + !submission.handoverRecorded + ) { + settle() + } + return + } + for (const row of hint.rows ?? []) { + if ( + row.kind !== 'dispatch' || + row.state !== 'pending' || + row.epoch !== hint.journal.physicalCursor().epoch || + row.clientMessageId !== clientMessageId + ) { + continue + } + const record = store.getRecord(sessionId) + const owner = + record && record.lease.runtimeFence === row.fence + ? input.host.deps.adapter.captureConversationTitleOwner?.(record) + : null + settle(owner && record && owner.isCurrent(record) ? owner : null) + return + } + }, + error: () => settle() + }) + ) + } catch { + settle() + } + return pending +} diff --git a/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.test.ts b/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.test.ts index f4082664bd6..b2bad3c6433 100644 --- a/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.test.ts +++ b/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.test.ts @@ -1,3 +1,19 @@ +import { ClaudeProfileRouter } from '../../../claude-accounts/claude-profile-router' +import { + acquired, + fakeClaude, + identityFor, + recordingJournalSink, + PROVIDER_SESSION_ID +} from '../../../claude/structured/claude-structured-session-test-support' +import type { AgentSessionRecord } from '../../../../shared/agent-session/lifecycle/agent-session-record' +import type { Query } from '@anthropic-ai/claude-agent-sdk' +import { createClaudeControlSurface } from '../../../claude/claude-agent-sdk-control-requests' +import { + createStructuredChatNamingHandler, + type StructuredChatNamingDeps +} from '../../../native-chat/structured-chat-naming' +import { compareAndSetAgentSessionRecordName } from '../records/agent-session-record-conversation-name' import { rm } from 'node:fs/promises' import { installClaudeProfileRouter } from '../../../claude-accounts/claude-profile-installed-router' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' @@ -7,6 +23,20 @@ import { normalizeSourceControlAiSettings } from '../../../../shared/source-cont import { structuredChatNamingDeps } from './structured-chat-naming-runtime' import type { AgentJournalSnapshot } from '../../../../shared/agent-session/journal/agent-session-journal-types' import type * as TextGeneration from '../../../text-generation/commit-message-text-generation' +import type { StructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-host' +import { + createQueuedMessageTestRig, + QUEUED_RIG_CALLER +} from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture' +import { holdDelivery } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-delivery-hold.test-fixture' +import { holdUnsentSends } from '../../../native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold' +import { AgentSessionAcquisitionRootExitObservedError } from '../../../native-chat/agent-session-wire/acquisition/structured-agent-session-acquisition-errors' +import { AgentSessionStartFailure } from '../../../native-chat/agent-session-wire/startup/structured-agent-session-start-failure' +import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../shared/agent-session/journal/agent-session-journal-types' +import { + HOST_TEST_SESSION, + hostTestOperationId +} from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' const mocks = vi.hoisted(() => ({ account: vi.fn((_provider: string, id: string) => ({ @@ -14,7 +44,13 @@ const mocks = vi.hoisted(() => ({ XDG_STATE_HOME: `/profiles/${id}/state` })), native: vi.fn(async () => 'Native title'), - generate: vi.fn(async () => ({ success: true as const, name: 'Login repair' })), + titleOwner: vi.fn(), + host: vi.fn<() => StructuredAgentSessionHost | undefined>(), + record: vi.fn(), + generate: vi.fn(async () => ({ + success: true as const, + name: 'Login repair' + })), target: vi.fn(async (cwd: string) => ({ kind: 'local' as const, cwd })), publishConversationName: vi.fn<(sessionId: string) => void>(), journalCurrentRead: vi.fn<(sessionId: string) => Promise>() @@ -28,11 +64,15 @@ vi.mock('../../../agent-hooks/first-work/first-work-generation-target', () => ({ resolveGenerationTarget: mocks.target })) vi.mock('../../../native-chat/agent-session-wire/host/structured-agent-session-registry', () => ({ - getStructuredAgentSessionHost: () => ({ - deps: { adapter: { generateText: mocks.native } }, - publishConversationName: mocks.publishConversationName, - journalCurrentRead: mocks.journalCurrentRead - }) + getStructuredAgentSessionHost: () => + mocks.host() ?? { + deps: { + store: { getRecord: mocks.record }, + adapter: { generateText: mocks.native, captureConversationTitleOwner: mocks.titleOwner } + }, + publishConversationName: mocks.publishConversationName, + journalCurrentRead: mocks.journalCurrentRead + } })) vi.mock('../../../managed-data-accounts/service', () => ({ @@ -43,13 +83,20 @@ vi.mock('../../../managed-data-accounts/service', () => ({ })) const roots: string[] = [] afterEach(async () => { + vi.useRealTimers() installClaudeProfileRouter(undefined) await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }))) }) -beforeEach(() => vi.clearAllMocks()) +beforeEach(() => { + vi.clearAllMocks() + mocks.titleOwner.mockReset() + mocks.host.mockReset() + mocks.record.mockReset() +}) function rig() { const settings = getDefaultSettings('/isolated') + const settingsListeners = new Set<() => void>() settings.defaultTuiAgent = 'codex' settings.sourceControlAi = normalizeSourceControlAiSettings(settings.sourceControlAi) const runtime = { @@ -67,13 +114,99 @@ function rig() { const logger = { warn: vi.fn(), error: vi.fn() } return { settings, + settingsChanged: () => settingsListeners.forEach((changed) => changed()), runtime, logger, - deps: structuredChatNamingDeps(() => ({ getSettings: () => settings }), runtime, logger) + deps: structuredChatNamingDeps( + () => ({ + getSettings: () => settings, + onSettingsChanged: (changed) => { + settingsListeners.add(changed) + return () => { + settingsListeners.delete(changed) + } + } + }), + runtime, + logger + ) } } describe('structured chat naming runtime', () => { + it('uses the CLI title only for automatic default naming and honors changed settings', async () => { + const state = rig() + const record = agentSessionRecordFixture() + const pending = Promise.withResolvers() + const generate = vi.fn(() => pending.promise) + mocks.titleOwner.mockReturnValue({ isCurrent: () => true, generate }) + const result = state.deps.generate( + record, + 'Repair login', + () => true, + state.deps.captureAutomaticTitle!(record) + ) + await vi.waitFor(() => expect(generate).toHaveBeenCalled()) + pending.resolve('Broken chat title') + expect(await result).toMatchObject({ name: 'Broken chat title' }) + expect(mocks.generate).not.toHaveBeenCalled() + await state.deps.generate(record, 'Repair login') + expect(mocks.generate).toHaveBeenCalledOnce() + + generate.mockImplementationOnce(async () => { + state.settings.sourceControlAi!.actions!.conversationName = { + commandInputTemplate: 'Custom {basePrompt}' + } + return 'Wrong settings title' + }) + expect( + await state.deps.generate( + record, + 'Repair login', + () => true, + state.deps.captureAutomaticTitle!(record) + ) + ).toEqual({ name: 'Login repair' }) + }) + + it.each(['action', 'shared', 'legacy', 'seeded-default', 'effort', 'template'] as const)( + 'preserves authored %s naming choices without consulting the CLI', + async (choice) => { + const state = rig() + const source = state.settings.sourceControlAi! + if (choice === 'action') { + source.modelOverridesByOperation = { + conversationName: { selectedModelByAgent: { claude: 'opus' } } + } + } + if (choice === 'shared') { + source.selectedModelByAgent = { claude: 'opus' } + } + if (choice === 'legacy') { + state.settings.commitMessageAi!.selectedModelByAgent = { claude: 'opus' } + } + if (choice === 'seeded-default') { + state.settings.commitMessageAi!.selectedModelByAgent = { claude: 'sonnet' } + state.settings.commitMessageAi!.selectedThinkingByModel = { sonnet: 'high' } + } + if (choice === 'effort') { + source.selectedThinkingByModel = { sonnet: 'high' } + } + if (choice === 'template') { + source.actions!.conversationName = { commandInputTemplate: 'Custom {basePrompt}' } + } + const record = agentSessionRecordFixture() + expect( + await state.deps.generate( + record, + 'Repair login', + () => true, + state.deps.captureAutomaticTitle!(record) + ) + ).toEqual({ name: 'Login repair' }) + expect(mocks.titleOwner).not.toHaveBeenCalled() + } + ) it.each(['grok', 'antigravity'] as const)( 'codes %s native naming failures without exposing provider text', async (provider) => { @@ -94,3 +227,497 @@ describe('structured chat naming runtime', () => { } ) }) + +function automaticNaming(state: ReturnType, before?: 'prompt' | 'seed') { + let record = agentSessionRecordFixture() + record.sessionId = 'session-1' + record.lease.sessionId = 'session-1' + record.providerHandleChain[0]!.handle.nativeId = PROVIDER_SESSION_ID + const blocked = Promise.withResolvers() + const entered = Promise.withResolvers() + const awaitPreparationRelease = async (stage: 'prompt' | 'seed') => { + if (before === stage) { + entered.resolve() + await blocked.promise + } + } + const store = { + getRecord: () => record, + compareAndSetConversationName: async (_id, name, expected, context, applicable) => + compareAndSetAgentSessionRecordName( + async (apply) => { + if (expected === null) { + await awaitPreparationRelease('seed') + } + record = apply(record) + return record + }, + name, + expected, + context, + applicable + ), + setUserConversationName: vi.fn() + } satisfies NonNullable> + mocks.record.mockImplementation(() => record) + const handler = createStructuredChatNamingHandler({ + ...state.deps, + getStore: () => store, + readFirstPrompt: async () => { + await awaitPreparationRelease('prompt') + return 'Repair login' + }, + wait: async () => {} + }) + return { + handler, + entered: entered.promise, + release: () => blocked.resolve(), + record: () => record, + replace: () => { + record = { ...record, lease: { ...record.lease, runtimeFence: 8 } } + }, + start: () => + handler( + { + sessionId: record.sessionId, + workspaceId: 'workspace-1', + agent: 'claude', + status: 'working', + latestPrompt: 'Repair login', + updatedAt: 1 + }, + { replay: false, firstInputSubmissionKey: 'input-1' } + ) + } +} + +it.each(['prompt', 'seed', 'workspace', 'support'] as const)( + 'retains the accepted input owner while %s preparation is pending', + async (stage) => { + const state = rig() + const blocked = Promise.withResolvers() + const entered = Promise.withResolvers() + if (stage === 'workspace') { + state.runtime.resolveWorkspace.mockImplementationOnce(async () => { + entered.resolve() + await blocked.promise + return { path: '/workspace/folder', executionHostId: 'local' } + }) + } + const naming = automaticNaming( + state, + stage === 'prompt' || stage === 'seed' ? stage : undefined + ) + const control = vi.fn(async () => 'Successor title') + mocks.titleOwner.mockImplementation(() => { + const fence = naming.record().lease.runtimeFence + return { + isCurrent: (current: AgentSessionRecord) => current.lease.runtimeFence === fence, + generate: async (_prompt: string, shouldExecute: () => boolean) => { + if (stage === 'support') { + entered.resolve() + await blocked.promise + } + return shouldExecute() ? control() : null + } + } + }) + naming.start() + await (stage === 'workspace' || stage === 'support' ? entered.promise : naming.entered) + naming.replace() + naming.release() + blocked.resolve() + await vi.waitFor(() => expect(naming.handler.activeSessionIds()).toEqual([])) + expect(control).not.toHaveBeenCalled() + expect(mocks.generate).toHaveBeenCalledOnce() + expect(naming.record().conversationName).toBe('Login repair') + } +) + +it.each([false, true])( + 'bounds timeout fallback to its owner (retired=%s), retries independently, and ignores late CLI results', + async (retired) => { + const state = rig() + const pending = Promise.withResolvers() + const generateSessionTitle = vi.fn(() => pending.promise) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: this flow calls only the feature-detected title method. + const controls = createClaudeControlSurface({ generateSessionTitle } as unknown as Query) + let owned = true + mocks.titleOwner.mockReturnValue({ + isCurrent: () => owned, + generate: controls.generateSessionTitle + }) + mocks.generate + .mockResolvedValueOnce({ success: false, error: 'Retry naming' }) + .mockImplementationOnce(async () => { + if (retired) { + owned = false + } + return { success: true, name: 'Recovered name' } + }) + const naming = automaticNaming(state) + vi.useFakeTimers() + naming.start() + await vi.advanceTimersByTimeAsync(30_000) + expect(naming.record().conversationName).toBe(retired ? 'Repair login' : 'Recovered name') + pending.resolve('Late CLI name') + await vi.advanceTimersByTimeAsync(0) + expect(naming.record().conversationName).toBe(retired ? 'Repair login' : 'Recovered name') + expect(generateSessionTitle).toHaveBeenCalledOnce() + expect(mocks.generate).toHaveBeenCalledTimes(2) + expect(naming.handler.activeSessionIds()).toEqual([]) + } +) + +it.each([ + { support: 'unsupported' as const, transition: 'close', response: 'unused' }, + { support: 'unknown' as const, transition: 'replacement', response: 'unused' }, + { support: 'supported' as const, transition: 'replacement', response: 'error' }, + { support: 'supported' as const, transition: 'close', response: 'null' } +])( + 'finishes independent naming after $transition with support=$support and response=$response', + async ({ support, transition, response }) => { + const cli = fakeClaude() + const adapter = await acquired(cli, { cliFlag: async () => support }) + const state = rig() + const naming = automaticNaming(state) + const entered = Promise.withResolvers() + const independent = + Promise.withResolvers< + Awaited> + >() + const control = vi.fn(async () => { + if (response === 'error') { + throw new Error('Optional control rejected') + } + return response === 'null' ? null : 'Wrong live title' + }) + cli.connections[0]!.generateSessionTitle = control + mocks.titleOwner.mockImplementation((record) => adapter.captureConversationTitleOwner(record)) + mocks.generate.mockImplementationOnce(async () => { + entered.resolve() + return independent.promise + }) + try { + naming.start() + await entered.promise + await adapter.closeSession('session-1') + if (transition === 'replacement') { + naming.replace() + await adapter.acquire({ + identity: identityFor(), + fence: 8, + spawnToken: 'replacement', + events: recordingJournalSink() + }) + cli.connections[1]!.generateSessionTitle = control + } + independent.resolve({ success: true, name: 'Independent completion' }) + await vi.waitFor(() => expect(naming.handler.activeSessionIds()).toEqual([])) + expect(naming.record().conversationName).toBe('Independent completion') + expect(control).toHaveBeenCalledTimes(response === 'unused' ? 0 : 1) + } finally { + await adapter.closeAll() + cli.dispose() + } + } +) + +it('discards a retired CLI result and publishes only independent fallback text', async () => { + const cli = fakeClaude() + const adapter = await acquired(cli) + const state = rig() + const naming = automaticNaming(state) + const pending = Promise.withResolvers() + const started = Promise.withResolvers() + cli.connections[0]!.generateSessionTitle = async () => { + started.resolve() + return pending.promise + } + mocks.titleOwner.mockImplementation((record) => adapter.captureConversationTitleOwner(record)) + try { + naming.start() + await started.promise + await adapter.closeSession('session-1') + pending.resolve('Stale CLI title') + await vi.waitFor(() => expect(naming.handler.activeSessionIds()).toEqual([])) + expect(naming.record().conversationName).toBe('Login repair') + } finally { + await adapter.closeAll() + cli.dispose() + } +}) + +it.each(['capture', 'pending result'] as const)( + 'keeps the seed and existing naming failure when account lookup fails at %s', + async (stage) => { + const cli = fakeClaude() + const adapter = await acquired(cli) + const state = rig() + const naming = automaticNaming(state) + const router = new ClaudeProfileRouter({ + getSettings: () => ({ + claudeManagedAccounts: [], + activeClaudeManagedAccountId: null, + agentStatusHooksEnabled: false, + disabledTuiAgents: [] + }), + dataRoot: '/isolated', + userHome: '/users/test', + env: {} + }) + const preparation = vi.spyOn(router, 'preparation') + installClaudeProfileRouter(router) + const unavailable = () => { + throw new Error('Selected account folder missing') + } + const pending = Promise.withResolvers() + const started = Promise.withResolvers() + cli.connections[0]!.generateSessionTitle = async () => { + started.resolve() + return pending.promise + } + mocks.titleOwner.mockImplementation((record) => adapter.captureConversationTitleOwner(record)) + state.runtime.resolveNamingEnvironment.mockImplementation(async () => unavailable()) + try { + if (stage === 'capture') { + preparation.mockImplementation(unavailable) + } + naming.start() + if (stage === 'pending result') { + await started.promise + preparation.mockImplementation(unavailable) + pending.resolve('Unusable account title') + } + await vi.waitFor(() => expect(naming.handler.activeSessionIds()).toEqual([])) + expect(naming.record().conversationName).toBe('Repair login') + expect(naming.handler.failure('session-1')?.messageCode).toBe('agentNotRun') + } finally { + await adapter.closeAll() + cli.dispose() + } + } +) + +async function atRestNaming() { + const state = rig() + const chat = await createQueuedMessageTestRig({ + agent: 'claude', + atRest: true, + starting: true, + restartable: true + }) + await vi.waitFor(() => + expect( + chat.host.collaboratorsForTests().conversationDelivery.loop.isRunning(HOST_TEST_SESSION) + ).toBe(false) + ) + mocks.host.mockReturnValue(chat.host) + const title = vi.fn(async () => 'CLI first handover title') + chat.host.deps.adapter.captureConversationTitleOwner = (_record) => { + const child = chat.host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)?.child + if (!child || child.phase !== 'ready') { + return null + } + const isCurrent = (current: AgentSessionRecord) => + current.lease.runtimeFence === child.fence && + chat.host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)?.child === child + return { isCurrent, generate: title } + } + const handler = createStructuredChatNamingHandler({ ...state.deps, now: () => 0 }) + chat.host.deps.onSessionStatusChanged = handler + chat.host.deps.conversationNaming = handler + const done = () => vi.waitFor(() => expect(handler.activeSessionIds()).toEqual([])) + const seeded = () => + vi.waitFor(() => + expect(chat.store.getRecord(HOST_TEST_SESSION)?.conversationName).toBe('Repair login') + ) + return { state, chat, title, handler, done, seeded } +} + +it.each(['fresh', 'startup retry', 'projected context'] as const)( + 'names an at-rest chat from its first ready handover (%s)', + async (scenario) => { + const { chat, title, done, seeded } = await atRestNaming() + try { + expect(chat.starts).not.toHaveBeenCalled() + const journal = chat.host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)!.journal + if (scenario === 'projected context') { + await journal.replaceEpochItems('handle_forked', 1, []) + expect(journal.cursor().epoch).not.toBe(journal.physicalCursor().epoch) + } + if (scenario === 'startup retry') { + chat.failNextStart( + new AgentSessionAcquisitionRootExitObservedError( + new AgentSessionStartFailure(new Error('Temporary start failure'), 'safe') + ) + ) + } + const sent = chat.send('Repair login') + expect(await sent.result).toMatchObject({ ok: true }) + await seeded() + expect(title).not.toHaveBeenCalled() + expect(mocks.generate).not.toHaveBeenCalled() + await journal.resolveDispatch({ + clientMessageId: 'different-input', + state: 'pending', + fence: chat.store.getRecord(HOST_TEST_SESSION)!.lease.runtimeFence, + turnScope: AGENT_JOURNAL_THREAD_SCOPE + }) + expect(title).not.toHaveBeenCalled() + await chat.proveStart() + await done() + expect(chat.dispatch).toHaveBeenCalled() + expect(chat.store.getRecord(HOST_TEST_SESSION)?.conversationName).toBe( + 'CLI first handover title' + ) + expect(title).toHaveBeenCalledOnce() + expect(chat.starts).toHaveBeenCalledTimes(scenario === 'startup retry' ? 2 : 1) + expect(mocks.generate).not.toHaveBeenCalled() + } finally { + await chat.dispose() + } + } +) + +it.each(['same-text rename', 'settings', 'auto-off', 'shutdown', 'Stop', 'close', 'held'] as const)( + 'releases a pending first-input title on %s without adopting a future child', + async (exit) => { + const { chat, state, title, handler, done, seeded } = await atRestNaming() + const preclaim = exit === 'Stop' || exit === 'close' || exit === 'held' ? holdDelivery() : null + let stopping: Promise | undefined + try { + expect(await chat.send('Repair login').result).toMatchObject({ ok: true }) + await seeded() + if (exit === 'same-text rename') { + await handler.setUserName(HOST_TEST_SESSION, 'Repair login', 'rename') + } + if (exit === 'settings') { + state.settings.sourceControlAi!.actions!.conversationName = { + commandInputTemplate: 'Custom {basePrompt}' + } + state.settingsChanged() + } + if (exit === 'auto-off') { + state.settings.nativeChatAutoName = false + state.settingsChanged() + } + if (exit === 'shutdown') { + chat.host.stopDelivery() + } + if (preclaim) { + await preclaim.held + expect(chat.starts).not.toHaveBeenCalled() + if (exit === 'Stop') { + stopping = chat.stop() + } + if (exit === 'close') { + stopping = chat.host.close(HOST_TEST_SESSION, 'user-close') + } + if (exit === 'held') { + await holdUnsentSends( + chat.host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)!.journal, + { + fence: chat.store.getRecord(HOST_TEST_SESSION)!.lease.runtimeFence, + hold: { cause: 'chatClosed' } + } + ) + } + } + await done() + const independent = + exit === 'settings' || exit === 'Stop' || exit === 'close' || exit === 'held' + expect(chat.store.getRecord(HOST_TEST_SESSION)?.conversationName).toBe( + independent ? 'Login repair' : 'Repair login' + ) + expect(mocks.generate).toHaveBeenCalledTimes(independent ? 1 : 0) + expect(title).not.toHaveBeenCalled() + preclaim?.release() + await stopping + if (exit !== 'shutdown' && !preclaim) { + await chat.proveStart() + } + expect(title).not.toHaveBeenCalled() + } finally { + preclaim?.release() + await stopping + await chat.dispose() + } + } +) + +it('retires the selected first-handover source without adopting a replacement child', async () => { + const { chat, title, done } = await atRestNaming() + const entered = Promise.withResolvers() + const result = Promise.withResolvers() + title.mockImplementation(async () => { + entered.resolve() + return result.promise + }) + try { + expect(await chat.send('Repair login').result).toMatchObject({ ok: true }) + await chat.proveStart() + await entered.promise + await chat.host.close(HOST_TEST_SESSION, 'user-close') + const successor = chat.send('A follow-up') + expect(await successor.result).toMatchObject({ ok: true }) + await chat.proveStart() + result.resolve('Stale first CLI title') + await done() + expect(chat.store.getRecord(HOST_TEST_SESSION)?.conversationName).toBe('Login repair') + expect(title).toHaveBeenCalledOnce() + } finally { + result.resolve('Late') + await chat.dispose() + } +}) + +it('finishes Send now naming during delivery even if recording its failure cannot commit', async () => { + const { chat, title, done, seeded } = await atRestNaming() + const release = chat.holdNextStart() + let releaseSecond: (() => void) | undefined + chat.failNextStart(new AgentSessionStartFailure(new Error('Startup refused'), 'never')) + const fields = { command: 'compact' as const } + const command = chat.host.conversationCommand(QUEUED_RIG_CALLER, { + ...fields, + envelope: chat.envelope(fields, 'agentSession.conversationCommand', hostTestOperationId()) + }) + try { + await vi.waitFor(() => expect(chat.starts).toHaveBeenCalledOnce()) + const sent = chat.send('Repair login') + const queued = await sent.result + if (!queued.ok || !('queued' in queued.value)) { + throw new Error('expected a queued receipt') + } + const messageId = queued.value.queued.messageId + expect(await chat.submission(sent.id)).toBeUndefined() + expect(await chat.sendNow(messageId)).toMatchObject({ ok: true }) + expect(await chat.handoff(messageId)).toMatchObject({ + queuedMessageId: messageId, + dispatchState: 'pending' + }) + await seeded() + const journal = chat.host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)!.journal + const failure = vi + .spyOn(journal, 'appendLifecycleBatch') + .mockRejectedValue(new Error('Journal write unavailable')) + releaseSecond = chat.holdNextStart() + release() + await vi.waitFor(() => expect(chat.starts).toHaveBeenCalledTimes(2)) + chat.failNextStart(new AgentSessionStartFailure(new Error('Startup refused'), 'never')) + releaseSecond() + await done() + expect(chat.store.getRecord(HOST_TEST_SESSION)?.conversationName).toBe('Login repair') + expect((await chat.handoff(messageId))?.dispatchState).toBe('pending') + expect(title).not.toHaveBeenCalled() + failure.mockRestore() + } finally { + release() + releaseSecond?.() + vi.restoreAllMocks() + await chat.stop() + await command + await chat.dispose() + } +}) diff --git a/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts b/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts index 8a1e7918a54..10a29046d2f 100644 --- a/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts +++ b/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts @@ -1,5 +1,10 @@ +import { ClaudeControlRequestTimeoutError } from '../../../claude/claude-agent-sdk-control-requests' import { normalizeSourceControlAiSettings } from '../../../../shared/source-control/ai/settings/source-control-ai-settings' -import { resolveSourceControlActionRecipe } from '../../../../shared/source-control/ai/source-control-ai-operation-policy' +import { + resolveSourceControlActionRecipe, + resolveInstructionsFromNormalized +} from '../../../../shared/source-control/ai/source-control-ai-operation-policy' +import { readSourceControlAiModelChoiceForHost } from '../../../../shared/source-control/ai/settings/source-control-ai-model-selection' import { normalizeSourceControlActionRecipe } from '../../../../shared/source-control/ai/recipes/source-control-ai-actions' import { renderConversationNamePrompt, @@ -22,6 +27,7 @@ import { resolveTextGenerationParams } from '../../../text-generation/commit-message-text-generation' import { resolveGenerationTarget } from '../../../agent-hooks/first-work/first-work-generation-target' +import { deferredStructuredChatTitle } from '../../../native-chat/structured-chat-title-handover' type StructuredChatNamingRuntime = { resolveWorkspace: ( @@ -33,11 +39,68 @@ type StructuredChatNamingRuntime = { retitleOpenTab: StructuredChatNamingDeps['onNamed'] } +function canReuseClaudeTitle( + current: Parameters[0], + model: string +): boolean { + const source = normalizeSourceControlAiSettings(current.sourceControlAi, current.commitMessageAi) + const recipe = resolveSourceControlActionRecipe({ + settings: current, + actionId: 'conversationName' + }) + const override = source.modelOverridesByOperation?.conversationName + const choices = [override, source, current.commitMessageAi] + return ( + !recipe.agentId && + !recipe.agentArgs?.trim() && + recipe.commandInputTemplate === '{basePrompt}' && + !resolveInstructionsFromNormalized(source, null, 'conversationName', undefined) && + !choices.some( + (choice) => + readSourceControlAiModelChoiceForHost(choice, LOCAL_COMMIT_MESSAGE_HOST_KEY, 'claude') !== + undefined + ) && + (override?.selectedThinkingByModel?.[model] ?? + source.selectedThinkingByModel[model] ?? + current.commitMessageAi?.selectedThinkingByModel?.[model]) === undefined + ) +} + +function resolveChatNamingParams( + current: Parameters[0], + provider: Parameters[0]['provider'], + recipe = resolveSourceControlActionRecipe({ settings: current, actionId: 'conversationName' }) +) { + const source = normalizeSourceControlAiSettings(current.sourceControlAi, current.commitMessageAi) + const useChatAgent = !recipe.agentId && !recipe.agentArgs?.trim() + return resolveTextGenerationParams( + useChatAgent + ? { + ...current, + sourceControlAi: { + ...source, + actions: { + ...source.actions, + conversationName: normalizeSourceControlActionRecipe({ + ...recipe, + agentId: provider + }) + } + } + } + : current, + LOCAL_COMMIT_MESSAGE_HOST_KEY, + 'conversationName', + null + ) +} + export function structuredChatNamingDeps( getStore: () => { getSettings: () => Parameters[0] & AgentLaunchProfileSettings & ReturnType + onSettingsChanged?: (changed: () => void) => () => void }, runtime: StructuredChatNamingRuntime, logger: StructuredAgentSessionLogger @@ -60,7 +123,49 @@ export function structuredChatNamingDeps( : '' }, workerTaskTitle: runtime.workerTaskTitle, - generate: async (record, firstPrompt, shouldExecute) => { + captureAutomaticTitle: (record, firstInputSubmissionKey, applicable) => { + const current = getStore().getSettings() + if ( + record.provider !== 'claude' || + record.location.wslDistro !== null || + current.nativeChatAutoName === false + ) { + return undefined + } + const settings = resolveChatNamingParams(current, record.provider) + if (!settings.ok || !canReuseClaudeTitle(current, settings.params.model)) { + return undefined + } + const host = getStructuredAgentSessionHost() + const owner = host?.deps.adapter.captureConversationTitleOwner?.(record) ?? null + return { + owner, + pending: + !owner && host && firstInputSubmissionKey && applicable + ? deferredStructuredChatTitle({ + record, + firstInputSubmissionKey, + host, + currentHost: getStructuredAgentSessionHost, + applicable, + eligible: () => { + const current = getStore().getSettings() + const resolved = resolveChatNamingParams(current, record.provider) + return resolved.ok && canReuseClaudeTitle(current, resolved.params.model) + }, + subscribeSettings: (changed) => + getStore().onSettingsChanged?.(changed) ?? (() => {}) + }) + : undefined + } + }, + generate: async function generate(record, firstPrompt, shouldExecute, automaticTitle) { + const titleOwner = automaticTitle?.pending + ? await automaticTitle.pending.ready + : automaticTitle?.owner + if (shouldExecute?.() === false || automaticTitle?.pending?.ended()) { + return null + } // Unsupported execution contexts keep the seed regardless of native naming settings. if (record.location.wslDistro !== null) { return null @@ -70,10 +175,6 @@ export function structuredChatNamingDeps( return null } const current = getStore().getSettings() - const source = normalizeSourceControlAiSettings( - current.sourceControlAi, - current.commitMessageAi - ) const recipe = resolveSourceControlActionRecipe({ settings: current, actionId: 'conversationName' @@ -108,30 +209,47 @@ export function structuredChatNamingDeps( return chatNamingFailure(record.provider === 'grok' ? 'grokFailed' : 'antigravityFailed') } } - const settings = resolveTextGenerationParams( - useChatAgent - ? { - ...current, - sourceControlAi: { - ...source, - actions: { - ...source.actions, - conversationName: normalizeSourceControlActionRecipe({ - ...recipe, - agentId: record.provider - }) - } - } - } - : current, - LOCAL_COMMIT_MESSAGE_HOST_KEY, - 'conversationName', - null - ) + const settings = resolveChatNamingParams(current, record.provider, recipe) if (!settings.ok) { logger.warn(settings.error, log) return { error: settings.error } } + if ( + automaticTitle && + record.provider === 'claude' && + current.nativeChatAutoName !== false && + canReuseClaudeTitle(current, settings.params.model) + ) { + const owner = titleOwner + if (owner) { + const isCurrent = ( + current = getStructuredAgentSessionHost()?.deps.store?.getRecord(record.sessionId) ?? + record + ) => (shouldExecute?.() ?? true) && owner.isCurrent(current) + const eligible = () => + getStore().getSettings().nativeChatAutoName !== false && + canReuseClaudeTitle(getStore().getSettings(), settings.params.model) + let output: unknown + try { + if (isCurrent()) { + output = await owner.generate(firstPrompt, () => isCurrent() && eligible()) + } + } catch (error) { + if (error instanceof ClaudeControlRequestTimeoutError) { + // A timed-out control is still in flight; its fallback keeps the original live owner. + automaticTitle.retainOwnership?.(owner.isCurrent) + if (!isCurrent()) { + return null + } + } + } + const name = typeof output === 'string' ? sanitizeGeneratedConversationName(output) : '' + if (name && isCurrent() && eligible()) { + return { name, shouldApply: (current) => isCurrent(current) && eligible() } + } + return generate(record, firstPrompt, shouldExecute, undefined) + } + } const target = useChatAgent ? await chatNamingAgentTarget( record, diff --git a/src/main/runtime/agent-session/records/agent-session-record-conversation-name.ts b/src/main/runtime/agent-session/records/agent-session-record-conversation-name.ts index 3b9df841fbe..96c62725300 100644 --- a/src/main/runtime/agent-session/records/agent-session-record-conversation-name.ts +++ b/src/main/runtime/agent-session/records/agent-session-record-conversation-name.ts @@ -77,11 +77,13 @@ export async function compareAndSetAgentSessionRecordName( ) => Promise, name: string | null, expected: string | null, - contextId: string | null = null + contextId: string | null = null, + applicable?: (record: AgentSessionRecord) => boolean ): Promise { let matched = false const record = await mutate((current) => { matched = + (applicable?.(current) ?? true) && current.conversationNameSource !== 'user' && (current.conversationName ?? null) === expected && (current.providerContextBoundary?.operationId ?? null) === contextId @@ -94,5 +96,6 @@ export type CompareAndSetConversationName = ( sessionId: string, name: string | null, expected: string | null, - contextId?: string | null + contextId?: string | null, + applicable?: (record: AgentSessionRecord) => boolean ) => Promise diff --git a/src/main/runtime/agent-session/records/agent-session-record-store.ts b/src/main/runtime/agent-session/records/agent-session-record-store.ts index 9462f27c275..5dc57674663 100644 --- a/src/main/runtime/agent-session/records/agent-session-record-store.ts +++ b/src/main/runtime/agent-session/records/agent-session-record-store.ts @@ -175,8 +175,20 @@ export class AgentSessionRecordStore extends AgentSessionRecordTabs { setUserConversationName = (sessionId: string, name: string, write: AgentSessionUserNameWrite) => setAgentSessionRecordUserNameThrough((apply) => this.mutate(sessionId, apply), name, write) - compareAndSetConversationName: CompareAndSetConversationName = (id, name, expected, context) => - compareAndSetAgentSessionRecordName((apply) => this.mutate(id, apply), name, expected, context) + compareAndSetConversationName: CompareAndSetConversationName = ( + id, + name, + expected, + context, + applicable + ) => + compareAndSetAgentSessionRecordName( + (apply) => this.mutate(id, apply), + name, + expected, + context, + applicable + ) /** Launch history is metadata and does not contend with the writer lease. */ pinLaunchDirectory = (sessionId: string, launchDirectory: string): Promise => From 387fe35bfb3977ccb8895363e580f50c9ce08ee2 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Mon, 9 Nov 2026 12:36:29 -0800 Subject: [PATCH 07/28] test: remove low-value mobile and attachment cases (#40475) Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .../use-mobile-accounts-snapshot.test.tsx | 46 +----------- ...bile-native-chat-transcript-writer.test.ts | 75 ------------------- .../native-chat-image-references.test.tsx | 27 ------- ...-native-chat-composer-attachments.test.tsx | 35 +-------- .../native-chat-composer-composition.test.tsx | 12 ++- 5 files changed, 7 insertions(+), 188 deletions(-) delete mode 100644 mobile/src/session/native-chat/mobile-native-chat-transcript-writer.test.ts diff --git a/mobile/src/accounts/use-mobile-accounts-snapshot.test.tsx b/mobile/src/accounts/use-mobile-accounts-snapshot.test.tsx index 56c8d293793..a64c3166b1b 100644 --- a/mobile/src/accounts/use-mobile-accounts-snapshot.test.tsx +++ b/mobile/src/accounts/use-mobile-accounts-snapshot.test.tsx @@ -5,15 +5,9 @@ import { afterEach, describe, expect, it, vi } from 'vitest' // Also tells TanStack observers on a microtask, so act() sees replies. import '../test-support/query-client-test-renderer' import { settleQueryNotifications } from '../test-support/rpc-recording/hook-mount' -import type { OrcaQueryStoreBackend } from '../device-store/query-storage' import { createOrcaQueryClient } from '../query/orca-query-client' -import { createHostQueryPersistence, persistedHostQueryKey } from '../query/host-query-persistence' import type { RpcClient } from '../transport/rpc/rpc-client-contract' -import { - accountsSnapshotFamily, - accountsSnapshotKey, - accountsSnapshotOptions -} from './accounts-snapshot-stream' +import { accountsSnapshotKey } from './accounts-snapshot-stream' import { useMobileAccountsSnapshot } from './use-mobile-accounts-snapshot' const HOST = 'host-1' @@ -89,24 +83,6 @@ async function mountAccounts(queryClient: QueryClient, client: RpcClient) { } } -function memoryStore(): OrcaQueryStoreBackend { - const rows = new Map() - return { - hostId: HOST, - read: async (keys) => ({ status: 'ok', values: keys.map((key) => rows.get(key) ?? null) }), - write: async (ops) => { - for (const op of ops) { - if (op.value === null) { - rows.delete(op.key) - } else { - rows.set(op.key, op.value) - } - } - return { written: true } - } - } -} - describe('accounts snapshot', () => { it('still shows usage when the stream fails and accounts.list answers', async () => { const port = accountsClient(SNAPSHOT) @@ -128,24 +104,4 @@ describe('accounts snapshot', () => { 'claude@example.test' ]) }) - - it('never counts a stored snapshot as live, even one written after the connect', async () => { - const store = memoryStore() - // Native Home's own stream stores the snapshot after the connect the page then reads. - const home = createOrcaQueryClient() - const homePersistence = createHostQueryPersistence(home, store) - home.getQueryCache().build(home, accountsSnapshotOptions(HOST)) - home.setQueryData(accountsSnapshotKey(HOST), SNAPSHOT, { updatedAt: CONNECTED_AT + 1 }) - await settleQueryNotifications() - await homePersistence.flush() - home.clear() - - const page = createOrcaQueryClient() - await createHostQueryPersistence(page, store).restore([ - persistedHostQueryKey(accountsSnapshotFamily, accountsSnapshotKey(HOST))! - ]) - const read = await mountAccounts(page, accountsClient(SNAPSHOT).client) - expect(read().snapshot?.claude.accounts.map((account) => account.id)).toEqual(['claude-1']) - expect(read().live).toBe(false) - }) }) diff --git a/mobile/src/session/native-chat/mobile-native-chat-transcript-writer.test.ts b/mobile/src/session/native-chat/mobile-native-chat-transcript-writer.test.ts deleted file mode 100644 index 08a181f1f7d..00000000000 --- a/mobile/src/session/native-chat/mobile-native-chat-transcript-writer.test.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import type { NativeChatMessage } from '../../../../src/shared/native-chat/native-chat-types' -import { publishPageDeviceStore } from '../../mobile-web-shell/bridge/page-device-store' -import { emitPageAppInactive } from '../../mobile-web-shell/bridge/bridge-app-inactive' -import { noteChatTranscript } from './mobile-native-chat-transcript-writer' - -function message(id: string): NativeChatMessage { - return { - id, - role: 'assistant', - blocks: [{ type: 'text', text: id }], - timestamp: 1, - source: 'transcript' - } -} - -describe('chat copies when the app leaves the foreground', () => { - const writes: string[] = [] - - beforeEach(() => { - writes.length = 0 - publishPageDeviceStore({ - available: () => true, - read: async (_table, keys) => ({ status: 'ok', values: keys.map(() => null) }), - write: async (_table, ops) => { - writes.push(...ops.map((op) => op.key)) - return { written: true } - } - }) - }) - - afterEach(() => { - vi.useRealTimers() - publishPageDeviceStore(null) - }) - - function note(rows: NativeChatMessage[]): void { - noteChatTranscript( - 'chat-leaving', - rows, - () => true, - (savedAt) => ({ - kind: 'terminal', - savedAt, - messages: rows, - hasMore: false, - beforeOffset: null - }) - ) - } - - it('saves new rows on leaving and writes nothing when a flap brings none', async () => { - vi.useFakeTimers() - const [m1, m2, m3] = [message('m1'), message('m2'), message('m3')] - note([m1, m2, m3]) - emitPageAppInactive() - await vi.runAllTimersAsync() - expect(writes).toHaveLength(1) - - // Two minutes later, Notification Centre and back: the rows on screen are a new list with the same rows. - vi.setSystemTime(Date.now() + 120_000) - note([m1, m2, m3]) - emitPageAppInactive() - note([m1, m2, m3]) - emitPageAppInactive() - await vi.runAllTimersAsync() - expect(writes).toHaveLength(1) - - // A middle row changed (a tool result landed), with the last row the same. - note([m1, { ...m2, blocks: [{ type: 'text', text: 'm2 with its result' }] }, m3]) - emitPageAppInactive() - await vi.runAllTimersAsync() - expect(writes).toHaveLength(2) - }) -}) diff --git a/src/renderer/src/components/native-chat/attachments/chips/native-chat-image-references.test.tsx b/src/renderer/src/components/native-chat/attachments/chips/native-chat-image-references.test.tsx index 34c3b2bc7ac..f9db36dcad0 100644 --- a/src/renderer/src/components/native-chat/attachments/chips/native-chat-image-references.test.tsx +++ b/src/renderer/src/components/native-chat/attachments/chips/native-chat-image-references.test.tsx @@ -199,17 +199,6 @@ describe('composer image chips', () => { expect(promptValue(composer.textbox())).toBe(`look ${chip('missing')}`) }) - it('leaves a file path that reads like a marker as typed when a message comes back', () => { - const scope = structuredAgentSessionDraftScopeKey('s1') - const file = formatNativeChatFileReference('/repo/[Image #1].md') - handBackStructuredAgentSessionMessage( - 's1', - 'm1', - message(`see ${file} and [Image #1]`, ['/up/a.png']) - ) - expect(readNativeChatComposerDraft(scope).text).toBe(`see ${file} and ${chip('returned-m1-1')}`) - }) - it('keeps a stored draft’s file reference when a message comes back', async () => { const scope = structuredAgentSessionDraftScopeKey('s1') const composer = renderComposer(scope) @@ -224,22 +213,6 @@ describe('composer image chips', () => { await waitFor(() => expect(back.chips()).toEqual(['Image #1'])) }) - it('adds a returned message once even after typing, by its image ids', () => { - const scope = structuredAgentSessionDraftScopeKey('s1') - const returned = message('see [Image #1]', ['/up/a.png']) - handBackStructuredAgentSessionMessage('s1', 'm1', returned) - updateNativeChatComposerDraft( - scope, - { text: `${readNativeChatComposerDraft(scope).text} and more` }, - 'immediate' - ) - handBackStructuredAgentSessionMessage('s1', 'm1', returned) - expect(readNativeChatComposerDraft(scope)).toMatchObject({ - text: `see ${chip('returned-m1-1')} and more`, - images: [{ id: 'returned-m1-1' }] - }) - }) - it('settles a late upload into its own conversation, bound by the same id, while another is shown', async () => { updateNativeChatComposerDraft('chat-b', { text: 'B draft' }, 'immediate') const composer = renderComposer('chat-a') diff --git a/src/renderer/src/components/native-chat/attachments/chips/use-native-chat-composer-attachments.test.tsx b/src/renderer/src/components/native-chat/attachments/chips/use-native-chat-composer-attachments.test.tsx index c297c00bcf0..313dcf03174 100644 --- a/src/renderer/src/components/native-chat/attachments/chips/use-native-chat-composer-attachments.test.tsx +++ b/src/renderer/src/components/native-chat/attachments/chips/use-native-chat-composer-attachments.test.tsx @@ -57,30 +57,6 @@ vi.mock( }) ) -function publishUnrelatedStatuses(): void { - for (let tick = 1; tick <= 24; tick += 1) { - useAppStore.setState((state) => ({ - runtimeStatusByEnvironmentId: new Map(state.runtimeStatusByEnvironmentId).set('other-host', { - status: null, - checkedAt: tick - }) - })) - useAppStore.setState((state) => ({ - agentStatusByPaneKey: { - ...state.agentStatusByPaneKey, - 'other-tab:other-pane': { - paneKey: 'other-tab:other-pane', - state: 'working', - prompt: 'background work', - updatedAt: tick, - stateStartedAt: 1, - stateHistory: [] - } - } - })) - } -} - describe('useNativeChatComposerAttachments', () => { afterEach(() => { useAppStore.setState(initialStoreState, true) @@ -100,17 +76,15 @@ describe('useNativeChatComposerAttachments', () => { storage.drafts.set(scope, { text: '', images: [image], owner: storedOwner, savedAt: 1 }) draftStorage.setNativeChatComposerDraftStorageForTests(storage) publishOwner(scope, storedOwner) - const resolveOwner = vi.fn((key: string) => + draftStoreOwner1.setNativeChatComposerDraftOwnerResolver((key) => resolveNativeChatDraftOwner(useAppStore.getState(), key) ) - draftStoreOwner1.setNativeChatComposerDraftOwnerResolver(resolveOwner) const old = Promise.withResolvers() const next = Promise.withResolvers() const pathExists = vi.fn().mockReturnValueOnce(old.promise).mockReturnValueOnce(next.promise) const originalApi = window.api Object.assign(window, { api: { fs: { pathExists } } }) let probe: Awaited> | undefined - const originalState = useAppStore.getState() try { await draftStoreOwner2.hydrateNativeChatComposerDrafts() probe = await renderProbe(scope, true) @@ -130,15 +104,8 @@ describe('useNativeChatComposerAttachments', () => { { id: image.id, path: '', unavailableName: 'shot.png' } ]) expect(draftStoreOwner3.readNativeChatComposerDraft(scope).owner).toEqual(storedOwner) - resolveOwner.mockClear() - act(publishUnrelatedStatuses) - expect(resolveOwner).not.toHaveBeenCalled() } finally { act(() => probe?.root.unmount()) - useAppStore.setState({ - runtimeStatusByEnvironmentId: originalState.runtimeStatusByEnvironmentId, - agentStatusByPaneKey: originalState.agentStatusByPaneKey - }) Object.assign(window, { api: originalApi }) } }) diff --git a/src/renderer/src/components/native-chat/composer/editor/native-chat-composer-composition.test.tsx b/src/renderer/src/components/native-chat/composer/editor/native-chat-composer-composition.test.tsx index ca88b74749a..8b4e1ccc070 100644 --- a/src/renderer/src/components/native-chat/composer/editor/native-chat-composer-composition.test.tsx +++ b/src/renderer/src/components/native-chat/composer/editor/native-chat-composer-composition.test.tsx @@ -108,7 +108,7 @@ function textarea(): HTMLTextAreaElement { } describe('native chat composer composition ownership', () => { - it('preserves the focused browser preedit through 120 stale streaming rerenders', () => { + it('preserves the focused preedit through a stale streaming rerender', () => { const textareaRef = createRef() const onImeSettled = vi.fn() const props = fieldProps({ textareaRef, onImeSettled }) @@ -118,12 +118,10 @@ describe('native chat composer composition ownership', () => { fireEvent.compositionStart(input) changePrompt(input, '가') - for (let index = 0; index < 120; index += 1) { - view.rerender() - expect(textarea()).toBe(input) - expect(document.activeElement).toBe(input) - expect(promptValue(input)).toBe('가') - } + view.rerender() + expect(textarea()).toBe(input) + expect(document.activeElement).toBe(input) + expect(promptValue(input)).toBe('가') fireEvent.compositionEnd(input, { data: '가' }) expect(onImeSettled).toHaveBeenCalledOnce() From fe1620cdf569d52fac978cd264cd1b081bda95b6 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:39:58 +0000 Subject: [PATCH 08/28] fix(chat): always show copy and fork under a finished agent turn (#40474) --- .../messages/actions/NativeChatAgentControls.tsx | 2 +- .../messages/actions/use-native-chat-reveal-latest.ts | 2 +- .../messages/rows/NativeChatAssistantMessageRow.tsx | 9 ++------- .../native-chat/messages/rows/NativeChatMessageRow.tsx | 2 +- .../pane/actions/NativeChatContextMenuItems.tsx | 2 +- .../viewport/find/use-native-chat-find-matches.ts | 2 +- 6 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/renderer/src/components/native-chat/messages/actions/NativeChatAgentControls.tsx b/src/renderer/src/components/native-chat/messages/actions/NativeChatAgentControls.tsx index 34e6b81cbf3..5db891afcaf 100644 --- a/src/renderer/src/components/native-chat/messages/actions/NativeChatAgentControls.tsx +++ b/src/renderer/src/components/native-chat/messages/actions/NativeChatAgentControls.tsx @@ -21,7 +21,7 @@ export function NativeChatAgentControls({ }): React.JSX.Element { const t = useTranslate() return ( - // Hover-only row chrome: find skips it rather than counting what the mouse happens to show. + // Row chrome, not transcript: find skips it.
{/* A visual line means nothing pasted outside Orca, so the copy leaves it out. An image-only response has nothing to copy, so it has no Copy. */} diff --git a/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts b/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts index 00c476f9091..19b8e0e927f 100644 --- a/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts +++ b/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts @@ -56,7 +56,7 @@ export type NativeChatMessageListHandle = { readerAtEnd: () => boolean /** Bring a find match into view, clear of the find bar; the reader stops following the latest. */ revealFindMatch: (match: Range, bar: DOMRectReadOnly | null) => void - /** What the row drawing this message copies from its hover button; null when none draws it. */ + /** What the row drawing this message copies from its Copy button; null when none draws it. */ messageCopyText: (messageId: string) => string | null /** Open a subagent's activity where it sits in the conversation and bring it into view. */ revealSubagent: (agentId: string) => void diff --git a/src/renderer/src/components/native-chat/messages/rows/NativeChatAssistantMessageRow.tsx b/src/renderer/src/components/native-chat/messages/rows/NativeChatAssistantMessageRow.tsx index 8f7fc2045ae..9af89d8fddb 100644 --- a/src/renderer/src/components/native-chat/messages/rows/NativeChatAssistantMessageRow.tsx +++ b/src/renderer/src/components/native-chat/messages/rows/NativeChatAssistantMessageRow.tsx @@ -76,16 +76,11 @@ export function NativeChatAssistantMessageRow({ !message.agentStart && !inSubagentSection && isRootAgentJournalItem(message) - // A row whose fork is opening keeps its controls up so the spinner stays visible. - const revealOnHover = - fork?.pendingItemId === message.id - ? 'mt-1 w-fit select-none' - : 'mt-1 w-fit select-none transition-opacity can-hover:pointer-events-none can-hover:opacity-0 group-hover:pointer-events-auto group-hover:opacity-100 [.group:has(:focus-visible)_&]:pointer-events-auto [.group:has(:focus-visible)_&]:opacity-100' return (
0 ? message.id : undefined} data-native-chat-message-tone={isSystem ? 'faint' : undefined} className={cn( @@ -126,7 +121,7 @@ export function NativeChatAssistantMessageRow({ ) : null } className={cn( - revealOnHover, + 'mt-1 w-fit select-none', // They hang into the gap below; a section keeps them inside its border instead. !inSubagentSection && '-mb-5' )} diff --git a/src/renderer/src/components/native-chat/messages/rows/NativeChatMessageRow.tsx b/src/renderer/src/components/native-chat/messages/rows/NativeChatMessageRow.tsx index fbf666c74ab..11c9bbda907 100644 --- a/src/renderer/src/components/native-chat/messages/rows/NativeChatMessageRow.tsx +++ b/src/renderer/src/components/native-chat/messages/rows/NativeChatMessageRow.tsx @@ -55,7 +55,7 @@ export type MessageRowProps = { activeTurnIsWorking?: boolean /** This row's tool run is the turn's last, so it is the one still live. */ trailingRun?: boolean - /** Hover controls would overlap the next row. */ + /** Response controls would overlap the next row. */ continuesTurn?: boolean /** In a turn: whether this row holds the response's actions (its last words, else its last images). */ endsResponse?: boolean diff --git a/src/renderer/src/components/native-chat/pane/actions/NativeChatContextMenuItems.tsx b/src/renderer/src/components/native-chat/pane/actions/NativeChatContextMenuItems.tsx index bd19901886f..c39e388ffc4 100644 --- a/src/renderer/src/components/native-chat/pane/actions/NativeChatContextMenuItems.tsx +++ b/src/renderer/src/components/native-chat/pane/actions/NativeChatContextMenuItems.tsx @@ -9,7 +9,7 @@ import type { NativeChatContextMenuTarget } from './native-chat-context-menu-tar import type { NativeChatRewindSurface } from '../../messages/actions/use-native-chat-rewind-host' function copyText(text: string): void { - // Best-effort, as the hover copy buttons are: the clipboard can reject when unfocused. + // Best-effort, as the row Copy buttons are: the clipboard can reject when unfocused. window.api.ui.writeClipboardText(text).catch(() => {}) } diff --git a/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find-matches.ts b/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find-matches.ts index b0dcadc3168..2801dd93655 100644 --- a/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find-matches.ts +++ b/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find-matches.ts @@ -15,7 +15,7 @@ const chatFindHighlights = createDomTextSearchHighlights({ const TRANSCRIPT_COLUMN_SELECTOR = '[data-native-chat-transcript-column]' const TRANSCRIPT_SCROLL_SELECTOR = '[data-native-chat-scroll]' -/** Row chrome that only shows on hover (timestamps, copy): marked, so counts never follow the mouse. */ +/** Row chrome, not transcript (timestamps, copy): marked, so find counts only message text. */ const SKIPPED_TEXT_SELECTOR = '.sr-only, [hidden], [data-native-chat-find-skip]' /** A streamed reply's text node while its words fade in: each new word is its own element. */ const WORD_GROUP_SELECTOR = '[data-word-group]' From acf3aead78ff8d368b6f7d7533287cf36df9784b Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:42:22 +0000 Subject: [PATCH 09/28] fix(agent-status): show Codex's mid-turn async question as waiting for the user (#39087) * fix(agent-status): show Codex's mid-turn async question as waiting for the user * fix(agent-status): keep Codex's async question waiting on paired hosts under its working title The phone's PTY-title renewal turned the waiting row back into working, because Codex keeps its working title while the question waits. * fix(agent-status): clear Codex's open async questions when the user sends a plain message Orca's chat card answers an async question as a quoted message, not Codex's reply wrapper, so the pane stayed waiting until the turn ended. * refactor(agent-status): move the legacy status test seed out of listener-state --- src/main/agent-hooks/server/server-cleanup.ts | 1 + ...server-ingest-codex-async-question.test.ts | 140 ++++++++++++++++++ .../codex-async-question-title-hold.ts | 18 +++ .../runtime-mobile-agent-status-projection.ts | 8 +- ...mobile-codex-async-question-status.test.ts | 38 +++++ ...tion-background-turn-notifications.test.ts | 6 +- .../legacy-agent-status-test-seed.ts | 20 +++ .../agent-hook-listener/listener-state.ts | 25 +--- .../agent-hook-listener-test-harness.ts | 3 +- .../agent-hook-listener/pane-state-claims.ts | 3 +- .../claude-helper-kill.test-fixture.ts | 3 +- .../providers/codex-async-questions.ts | 137 +++++++++++++++++ .../providers/codex-events.ts | 19 ++- .../providers/codex-state.ts | 12 +- .../native-chat-interactive-prompt.test.ts | 13 ++ .../input/native-chat-interactive-prompt.ts | 4 +- 16 files changed, 414 insertions(+), 36 deletions(-) create mode 100644 src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts create mode 100644 src/main/runtime/session-lifecycle/projection/codex-async-question-title-hold.ts create mode 100644 src/main/runtime/session-lifecycle/projection/runtime-mobile-codex-async-question-status.test.ts create mode 100644 src/shared/agent-hook-listener/legacy-agent-status-test-seed.ts create mode 100644 src/shared/agent-hook-listener/providers/codex-async-questions.ts diff --git a/src/main/agent-hooks/server/server-cleanup.ts b/src/main/agent-hooks/server/server-cleanup.ts index 0e19a595502..be3d6c2b5d1 100644 --- a/src/main/agent-hooks/server/server-cleanup.ts +++ b/src/main/agent-hooks/server/server-cleanup.ts @@ -265,6 +265,7 @@ export abstract class AgentHookServerCleanup extends AgentHookServerAuthorityFen // A replacement remote process may reuse the pane; discard the lost owner's children. this.state.codexSubagentRosterByPaneKey.delete(paneKey) this.state.codexLeadStateByPaneKey.delete(paneKey) + this.state.codexAsyncQuestionsByPaneKey.delete(paneKey) } else if (deleted.payload.agentType === 'claude') { this.state.claudeSubagentRosterByPaneKey.delete(paneKey) this.state.claudeLeadStateByPaneKey.delete(paneKey) diff --git a/src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts b/src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts new file mode 100644 index 00000000000..e38f537c8ce --- /dev/null +++ b/src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts @@ -0,0 +1,140 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { normalizeHookPayload } from '../../../../../../shared/agent-hook-listener/normalization/normalize-hook-payload' +import { createHookListenerState } from '../../../../../../shared/agent-hook-listener/listener-state' +import { buildRelayHookEnvelope } from '../../../../../../wsl-guest/agent-hook-envelope-build' +import { AgentHookServer, _internals } from '../../../agent-hook-server' +import { PANE, buildBody } from '../../server.test-fixtures' + +vi.mock('../../../../../telemetry/client', () => ({ track: vi.fn() })) +vi.mock('../../../../../telemetry/cohort-classifier', () => ({ + getCohortAtEmit: () => ({ nth_repo_added: 2 }) +})) + +// Shapes captured from a live Codex 0.166 run. +const QUESTIONS = { + questions: [ + { title: 'Which database should the probe use?', options: ['Postgres', 'SQLite', 'MySQL'] }, + { title: 'What should the probe name the new table?' } + ] +} +const ASK = { + hook_event_name: 'PreToolUse', + turn_id: 'turn-1', + tool_name: 'request_user_input_async', + tool_input: QUESTIONS, + tool_use_id: 'call_3_1' +} +const ACCEPTED = { + ...ASK, + hook_event_name: 'PostToolUse', + tool_response: '{"accepted":true}' +} +const reply = (index: number) => ({ + hook_event_name: 'UserPromptSubmit', + turn_id: 'turn-1', + prompt: `\n${JSON.stringify([ + { + answer: 'SQLite', + question: QUESTIONS.questions[index]!.title, + questionItemId: JSON.stringify(['request_user_input_async', 'call_3_1', index]) + } + ])}\n` +}) + +beforeEach(() => { + _internals.resetCachesForTests() +}) + +afterEach(() => { + vi.restoreAllMocks() +}) + +// The execution host normalizes each hook, then main ingests the relayed row, as for SSH and WSL. +function codexPane() { + const host = createHookListenerState() + const server = new AgentHookServer() + return { + report(payload: Record) { + const event = normalizeHookPayload( + host, + 'codex', + buildBody(payload, { worktreeId: 'folder-workspace' }), + 'production' + ) + if (!event) { + throw new Error('Expected a Codex hook event') + } + server.ingestRemote(buildRelayHookEnvelope(event, 'codex'), 'wsl:Ubuntu') + return { host: event.payload, main: server.getStatusSnapshotForPane(PANE)[0] } + } + } +} + +const waitingOnQuestion = { + state: 'waiting', + toolName: 'request_user_input_async', + interactivePrompt: JSON.stringify(QUESTIONS) +} + +describe('Codex async questions', () => { + it('waits on an open question through the turn until every question is answered', () => { + const pane = codexPane() + pane.report({ hook_event_name: 'UserPromptSubmit', turn_id: 'turn-1', prompt: 'Set up a db' }) + + expect(pane.report(ASK).main).toMatchObject(waitingOnQuestion) + // Codex accepts the call at once and keeps working; the question is still open. + const accepted = pane.report(ACCEPTED) + expect(accepted.host).toMatchObject(waitingOnQuestion) + expect(accepted.main).toMatchObject(waitingOnQuestion) + pane.report({ hook_event_name: 'PreToolUse', turn_id: 'turn-1', tool_name: 'Bash' }) + // An approval on screen wins over the open question. + const approval = pane.report({ + hook_event_name: 'PermissionRequest', + turn_id: 'turn-1', + tool_name: 'Bash', + tool_input: { command: 'pnpm tc' } + }).main + expect(approval?.state).toBe('waiting') + expect(approval?.interactivePrompt).toContain('"approval"') + pane.report({ hook_event_name: 'PostToolUse', turn_id: 'turn-1', tool_name: 'Bash' }) + expect(pane.report(reply(0)).main).toMatchObject(waitingOnQuestion) + + const answered = pane.report(reply(1)).main + expect(answered).toMatchObject({ state: 'working' }) + expect(answered?.interactivePrompt).toBeUndefined() + }) + + it('drops open questions when the user sends a plain message instead of a reply', () => { + const pane = codexPane() + pane.report({ hook_event_name: 'UserPromptSubmit', turn_id: 'turn-1', prompt: 'Set up a db' }) + pane.report(ASK) + expect(pane.report(ACCEPTED).main).toMatchObject(waitingOnQuestion) + + // Orca's chat card answers an async question as a quoted message, as Codex clients do. + const steered = pane.report({ + hook_event_name: 'UserPromptSubmit', + turn_id: 'turn-1', + prompt: '> Which database should the probe use?\n\nSQLite' + }).main + expect(steered).toMatchObject({ state: 'working' }) + expect(steered?.interactivePrompt).toBeUndefined() + }) + + it('drops an unanswered question when the turn ends', () => { + const pane = codexPane() + pane.report({ hook_event_name: 'UserPromptSubmit', turn_id: 'turn-1', prompt: 'Set up a db' }) + pane.report(ASK) + expect(pane.report(ACCEPTED).main).toMatchObject(waitingOnQuestion) + + expect(pane.report({ hook_event_name: 'Stop', turn_id: 'turn-1' }).main).toMatchObject({ + state: 'done' + }) + const next = pane.report({ + hook_event_name: 'UserPromptSubmit', + turn_id: 'turn-2', + prompt: 'Next' + }).main + expect(next).toMatchObject({ state: 'working' }) + expect(next?.interactivePrompt).toBeUndefined() + }) +}) diff --git a/src/main/runtime/session-lifecycle/projection/codex-async-question-title-hold.ts b/src/main/runtime/session-lifecycle/projection/codex-async-question-title-hold.ts new file mode 100644 index 00000000000..846a071b2c1 --- /dev/null +++ b/src/main/runtime/session-lifecycle/projection/codex-async-question-title-hold.ts @@ -0,0 +1,18 @@ +import { agentStatusAuthorityObservedAt } from '../../../../shared/agent-status/contracts/agent-status-freshness' +import type { AgentStatusEntry } from '../../../../shared/agent-status/contracts/agent-status-types' +import { CODEX_ASYNC_QUESTION_TOOL } from '../../../../shared/codex-async-question-input' +import type { RuntimePtyWorktreeRecord } from '../../terminal/records/runtime-terminal-state-records' + +/** Codex's title stays working while its async question waits; only the hook sees the question. */ +export function codexAsyncQuestionOutlivesWorkingTitle( + status: AgentStatusEntry, + pty: RuntimePtyWorktreeRecord +): boolean { + return ( + status.state === 'waiting' && + status.toolName === CODEX_ASYNC_QUESTION_TOOL && + status.interactivePrompt != null && + pty.lastAgentStatus === 'working' && + agentStatusAuthorityObservedAt(status) > (pty.providerGenerationResetAtEpochMs ?? 0) + ) +} diff --git a/src/main/runtime/session-lifecycle/projection/runtime-mobile-agent-status-projection.ts b/src/main/runtime/session-lifecycle/projection/runtime-mobile-agent-status-projection.ts index 6529fe2eb65..17761ac1130 100644 --- a/src/main/runtime/session-lifecycle/projection/runtime-mobile-agent-status-projection.ts +++ b/src/main/runtime/session-lifecycle/projection/runtime-mobile-agent-status-projection.ts @@ -13,6 +13,7 @@ import type { AgentProviderSessionMetadata } from '../../../../shared/agent-sess import { terminalTitleBlocksExplicitAgentStatus } from '../../workspaces/process-listing/runtime-worktree-status-projection' import type { HookLiveAgentRow } from '../../workspaces/process-listing/hook-live-agent-row' import type { RuntimePtyWorktreeRecord } from '../../terminal/records/runtime-terminal-state-records' +import { codexAsyncQuestionOutlivesWorkingTitle } from './codex-async-question-title-hold' type HookTurnFacts = Pick< AgentStatusEntry, @@ -120,9 +121,10 @@ export function renewRuntimeMobileAgentStatusFromPtyTitle( // Same-class Claude title repaints can postdate a fresh permission hook without // contradicting it; only a working or released-pane title retires the question (#11761). if ( - (status.state === 'waiting' || status.state === 'blocked') && - pty.lastAgentStatus === 'idle' && - Date.now() - agentStatusAuthorityObservedAt(status) <= AGENT_STATUS_STALE_AFTER_MS + ((status.state === 'waiting' || status.state === 'blocked') && + pty.lastAgentStatus === 'idle' && + Date.now() - agentStatusAuthorityObservedAt(status) <= AGENT_STATUS_STALE_AFTER_MS) || + codexAsyncQuestionOutlivesWorkingTitle(status, pty) ) { return status } diff --git a/src/main/runtime/session-lifecycle/projection/runtime-mobile-codex-async-question-status.test.ts b/src/main/runtime/session-lifecycle/projection/runtime-mobile-codex-async-question-status.test.ts new file mode 100644 index 00000000000..37ead87f35a --- /dev/null +++ b/src/main/runtime/session-lifecycle/projection/runtime-mobile-codex-async-question-status.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from 'vitest' +import type { AgentStatusEntry } from '../../../../shared/agent-status/contracts/agent-status-types' +import { renewRuntimeMobileAgentStatusFromPtyTitle } from './runtime-mobile-agent-status-projection' +import { makeTuiIdlePty } from '../../terminal/wait/tui-idle-wait-test-harness' + +const QUESTION = '{"questions":[{"title":"Which database should the probe use?"}]}' + +function waitingOnAsyncQuestion(hookAt: number): AgentStatusEntry { + return { + state: 'waiting', + prompt: 'Set up a db', + updatedAt: hookAt, + stateStartedAt: hookAt, + agentType: 'codex', + toolName: 'request_user_input_async', + interactivePrompt: QUESTION, + mainAgent: { state: 'waiting', stateStartedAt: hookAt }, + paneKey: 'tab-1:leaf-1', + stateHistory: [] + } +} + +describe('paired/mobile Codex status while an async question waits', () => { + it('keeps the question when Codex repaints its working title after the hook', () => { + const hookAt = 1_700_000_000_000 + const out = renewRuntimeMobileAgentStatusFromPtyTitle( + waitingOnAsyncQuestion(hookAt), + makeTuiIdlePty({ + lastOscTitle: '⠋ repo', + lastOscTitleEpochMs: hookAt + 2_000, + lastAgentStatus: 'working', + lastAgentStatusStartedAtEpochMs: hookAt - 10_000, + lastAgentStatusRichInvalidatedAtEpochMs: hookAt - 10_000 + }) + ) + expect(out).toMatchObject({ state: 'waiting', interactivePrompt: QUESTION }) + }) +}) diff --git a/src/renderer/src/hooks/agent-hook-completion-background-turn-notifications.test.ts b/src/renderer/src/hooks/agent-hook-completion-background-turn-notifications.test.ts index 4bf38b045af..e2e46eb7d36 100644 --- a/src/renderer/src/hooks/agent-hook-completion-background-turn-notifications.test.ts +++ b/src/renderer/src/hooks/agent-hook-completion-background-turn-notifications.test.ts @@ -3,10 +3,8 @@ import { mkdtemp, mkdir, readFile, writeFile, appendFile, rm } from 'node:fs/pro import { tmpdir } from 'node:os' import { join } from 'node:path' import { ClaudeHelperKillPoller } from '../../../shared/agent-hook-listener/providers/claude-helper-kill-poller' -import { - createHookListenerState, - seedLegacyAgentStatusForTests -} from '../../../shared/agent-hook-listener/listener-state' +import { createHookListenerState } from '../../../shared/agent-hook-listener/listener-state' +import { seedLegacyAgentStatusForTests } from '../../../shared/agent-hook-listener/legacy-agent-status-test-seed' import type { AgentHookEventPayload } from '../../../shared/agent-hook-listener/listener-event' import { createTestStore } from '../store/testing/app-store-test-fixture' import { makeTab } from '../store/terminals/terminal-tab.test-fixture' diff --git a/src/shared/agent-hook-listener/legacy-agent-status-test-seed.ts b/src/shared/agent-hook-listener/legacy-agent-status-test-seed.ts new file mode 100644 index 00000000000..56f3a32424a --- /dev/null +++ b/src/shared/agent-hook-listener/legacy-agent-status-test-seed.ts @@ -0,0 +1,20 @@ +import { AGENT_STATUS_2A_CURRENT_PRODUCER_MODE } from '../agent-status/transport/agent-status-legacy-adapter' +import type { AgentHookEventPayload } from './listener-event' +import { admitLegacyAgentStatus, type HookListenerState } from './listener-state' + +/** Test harnesses seed the same compatibility region without exposing a mutable Map. */ +export function seedLegacyAgentStatusForTests( + state: HookListenerState, + entry: AgentHookEventPayload +): void { + if ( + !admitLegacyAgentStatus( + state, + 'main-status-update', + entry, + AGENT_STATUS_2A_CURRENT_PRODUCER_MODE + ) + ) { + throw new Error('Test legacy agent-status seed was refused') + } +} diff --git a/src/shared/agent-hook-listener/listener-state.ts b/src/shared/agent-hook-listener/listener-state.ts index 0d19068e10b..634cf60ce5b 100644 --- a/src/shared/agent-hook-listener/listener-state.ts +++ b/src/shared/agent-hook-listener/listener-state.ts @@ -3,7 +3,6 @@ import type { AgentChildWorkKind } from '../agent-status/child-work/agent-status import type { ClaudeLeadTurnState, CodexLeadTurnState } from './main-agent-turn-state' import { - AGENT_STATUS_2A_CURRENT_PRODUCER_MODE, createAgentStatusLegacyAdapter, type AgentStatusLegacyAdapter, type AgentStatusLegacyAdapterOptions, @@ -15,6 +14,7 @@ import type { ClaudeSubagentRoster } from '../claude-subagent-roster' import type { ClaudeToolRequest } from './claude-tool-request-identity' import type { CodexSubagentRoster } from '../codex-subagent-roster' import type { CodexSubagentTranscriptState } from '../codex-subagent-transcript' +import type { CodexAsyncQuestions } from './providers/codex-async-questions' import type { MuseSessionLogState } from '../muse-session-log' import type { AgentHookEventPayload, ToolSnapshot } from './listener-event' import { @@ -74,6 +74,8 @@ export type HookListenerState = { codexSubagentTranscriptByPaneKey: Map /** Root Codex state/model, kept separate from child hook traffic. */ codexLeadStateByPaneKey: Map + /** Mid-turn async questions Codex's TUI holds open; its own status keeps saying working. */ + codexAsyncQuestionsByPaneKey: Map /** Ephemeral forked Codex threads (/side) per pane; they never own the pane's status. */ codexEphemeralForkSessionsByPaneKey: Map /** A rollout-less Codex start held until its prompt shows a new conversation or the hidden title thread. */ @@ -157,6 +159,7 @@ export function createHookListenerState( codexSubagentRosterByPaneKey: new Map(), codexSubagentTranscriptByPaneKey: new Map(), codexLeadStateByPaneKey: new Map(), + codexAsyncQuestionsByPaneKey: new Map(), codexEphemeralForkSessionsByPaneKey: new Map(), codexHeldEphemeralStartByPaneKey: new Map(), grokActiveTurnByPaneKey: new Map(), @@ -213,23 +216,6 @@ export function getLegacyStatusListingOrder( return legacyStatusAdapter(state).listingOrder(paneKey) } -/** Test harnesses seed the same compatibility region without exposing a mutable Map. */ -export function seedLegacyAgentStatusForTests( - state: HookListenerState, - entry: AgentHookEventPayload -): void { - if ( - !admitLegacyAgentStatus( - state, - 'main-status-update', - entry, - AGENT_STATUS_2A_CURRENT_PRODUCER_MODE - ) - ) { - throw new Error('Test legacy agent-status seed was refused') - } -} - export function clearPaneCacheState(state: HookListenerState, paneKey: string): void { deletePaneScopedCacheEntry(state.lastPromptByPaneKey, paneKey) deletePaneScopedCacheEntry(state.lastToolByPaneKey, paneKey) @@ -255,6 +241,7 @@ export function clearPaneCacheState(state: HookListenerState, paneKey: string): state.codexSubagentRosterByPaneKey.delete(paneKey) state.codexSubagentTranscriptByPaneKey.delete(paneKey) state.codexLeadStateByPaneKey.delete(paneKey) + state.codexAsyncQuestionsByPaneKey.delete(paneKey) state.codexEphemeralForkSessionsByPaneKey.delete(paneKey) state.codexHeldEphemeralStartByPaneKey.delete(paneKey) state.grokActiveTurnByPaneKey.delete(paneKey) @@ -295,6 +282,7 @@ export function movePaneCacheState( movePaneScopedMapEntries(state.codexSubagentRosterByPaneKey, fromPaneKey, toPaneKey) movePaneScopedMapEntries(state.codexSubagentTranscriptByPaneKey, fromPaneKey, toPaneKey) movePaneScopedMapEntries(state.codexLeadStateByPaneKey, fromPaneKey, toPaneKey) + movePaneScopedMapEntries(state.codexAsyncQuestionsByPaneKey, fromPaneKey, toPaneKey) movePaneScopedMapEntries(state.codexEphemeralForkSessionsByPaneKey, fromPaneKey, toPaneKey) movePaneScopedMapEntries(state.codexHeldEphemeralStartByPaneKey, fromPaneKey, toPaneKey) movePaneScopedMapEntries(state.grokActiveTurnByPaneKey, fromPaneKey, toPaneKey) @@ -340,6 +328,7 @@ export function clearAllListenerCaches(state: HookListenerState): void { state.codexSubagentRosterByPaneKey.clear() state.codexSubagentTranscriptByPaneKey.clear() state.codexLeadStateByPaneKey.clear() + state.codexAsyncQuestionsByPaneKey.clear() state.codexEphemeralForkSessionsByPaneKey.clear() state.codexHeldEphemeralStartByPaneKey.clear() state.grokActiveTurnByPaneKey.clear() diff --git a/src/shared/agent-hook-listener/normalization/agent-hook-listener-test-harness.ts b/src/shared/agent-hook-listener/normalization/agent-hook-listener-test-harness.ts index cfa104bb293..1d34ef4a795 100644 --- a/src/shared/agent-hook-listener/normalization/agent-hook-listener-test-harness.ts +++ b/src/shared/agent-hook-listener/normalization/agent-hook-listener-test-harness.ts @@ -1,5 +1,6 @@ import { normalizeHookPayload } from './normalize-hook-payload' -import { seedLegacyAgentStatusForTests, type HookListenerState } from '../listener-state' +import type { HookListenerState } from '../listener-state' +import { seedLegacyAgentStatusForTests } from '../legacy-agent-status-test-seed' import { makePaneKey } from '../../workspace-layout/stable-pane-id' const LEAF_ID = '11111111-1111-4111-8111-111111111111' diff --git a/src/shared/agent-hook-listener/pane-state-claims.ts b/src/shared/agent-hook-listener/pane-state-claims.ts index 3d161b6a6d4..228eda7fa1a 100644 --- a/src/shared/agent-hook-listener/pane-state-claims.ts +++ b/src/shared/agent-hook-listener/pane-state-claims.ts @@ -11,6 +11,7 @@ export function paneHasStateClaims(state: HookListenerState, paneKey: string): b state.claudeLaunchedBackgroundTasksByPaneKey.has(paneKey) || state.claudeSessionOwnerByPaneKey.has(paneKey) || state.codexSubagentRosterByPaneKey.has(paneKey) || - state.codexLeadStateByPaneKey.has(paneKey) + state.codexLeadStateByPaneKey.has(paneKey) || + state.codexAsyncQuestionsByPaneKey.has(paneKey) ) } diff --git a/src/shared/agent-hook-listener/providers/claude-helper-kill.test-fixture.ts b/src/shared/agent-hook-listener/providers/claude-helper-kill.test-fixture.ts index 17799a9f92f..f5b7993c56b 100644 --- a/src/shared/agent-hook-listener/providers/claude-helper-kill.test-fixture.ts +++ b/src/shared/agent-hook-listener/providers/claude-helper-kill.test-fixture.ts @@ -2,7 +2,8 @@ import { mkdtemp, writeFile, mkdir, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, vi } from 'vitest' -import { createHookListenerState, seedLegacyAgentStatusForTests } from '../listener-state' +import { createHookListenerState } from '../listener-state' +import { seedLegacyAgentStatusForTests } from '../legacy-agent-status-test-seed' import { normalizeHookPayload } from '../normalization/normalize-hook-payload' import { ClaudeHelperKillPoller } from './claude-helper-kill-poller' import { readClaudeTranscriptChunk } from './claude-transcript-cursor' diff --git a/src/shared/agent-hook-listener/providers/codex-async-questions.ts b/src/shared/agent-hook-listener/providers/codex-async-questions.ts new file mode 100644 index 00000000000..8e13c291fc2 --- /dev/null +++ b/src/shared/agent-hook-listener/providers/codex-async-questions.ts @@ -0,0 +1,137 @@ +import { + CODEX_ASYNC_QUESTION_TOOL, + parseCodexAsyncQuestionsInput +} from '../../codex-async-question-input' +import type { HookListenerState } from '../listener-state' +import { readString } from '../tool-input-preview' + +/** One `request_user_input_async` call and the question indexes it still waits on. */ +type CodexAsyncQuestionCall = { + /** The tool input as Codex sent it, published as the row's interactivePrompt. */ + prompt: string + unanswered: Set +} + +/** Pending mid-turn async questions in one Codex pane, by tool call id, oldest first. */ +export type CodexAsyncQuestions = Map + +const REPLY_OPEN = '' +const REPLY_CLOSE = '' + +/** The questions Codex's TUI is holding open for the user; the newest call is shown. */ +export type CodexPendingAsyncQuestion = { toolName: string; prompt: string } + +/** + * Codex asks a non-blocking question mid-turn: its status stays working, PostToolUse lands ~20 ms + * after PreToolUse, and each answer arrives as a UserPromptSubmit reply. Only the hooks show it, + * so track it here. A skip (Ctrl+]) sends no hook and stays pending until the turn ends or the + * user sends a plain message. + */ +export function trackCodexAsyncQuestions( + state: HookListenerState, + paneKey: string, + eventName: unknown, + hookPayload: Record +): CodexPendingAsyncQuestion | undefined { + // The TUI drops unanswered questions when the turn ends. + if (eventName === 'Stop' || eventName === 'Interrupt' || eventName === 'SessionStart') { + state.codexAsyncQuestionsByPaneKey.delete(paneKey) + return undefined + } + if ( + eventName === 'PreToolUse' && + readString(hookPayload, 'tool_name') === CODEX_ASYNC_QUESTION_TOOL + ) { + openCall(state, paneKey, hookPayload) + } else if (eventName === 'UserPromptSubmit') { + answerCalls(state, paneKey, readString(hookPayload, 'prompt')) + } + return pendingCodexAsyncQuestion(state, paneKey) +} + +export function pendingCodexAsyncQuestion( + state: HookListenerState, + paneKey: string +): CodexPendingAsyncQuestion | undefined { + const calls = state.codexAsyncQuestionsByPaneKey.get(paneKey) + const newest = calls ? [...calls.values()].at(-1) : undefined + return newest ? { toolName: CODEX_ASYNC_QUESTION_TOOL, prompt: newest.prompt } : undefined +} + +function openCall( + state: HookListenerState, + paneKey: string, + hookPayload: Record +): void { + const callId = readString(hookPayload, 'tool_use_id') + const input = hookPayload['tool_input'] + const questions = + input && typeof input === 'object' && 'questions' in input && Array.isArray(input.questions) + ? input.questions + : null + if (!callId || !questions || !parseCodexAsyncQuestionsInput(input)) { + return + } + let calls = state.codexAsyncQuestionsByPaneKey.get(paneKey) + if (!calls) { + calls = new Map() + state.codexAsyncQuestionsByPaneKey.set(paneKey, calls) + } + calls.set(callId, { + prompt: JSON.stringify(input), + unanswered: new Set(questions.keys()) + }) +} + +// Each answer is `[{answer, question, questionItemId: '["request_user_input_async", callId, index]'}]`. +function answerCalls(state: HookListenerState, paneKey: string, prompt: string | undefined): void { + const calls = state.codexAsyncQuestionsByPaneKey.get(paneKey) + const body = prompt?.trim() + if (!calls) { + return + } + // A plain message answers or abandons them all, as native chat's transcript scan reads it. + if (!body?.startsWith(REPLY_OPEN) || !body.endsWith(REPLY_CLOSE)) { + state.codexAsyncQuestionsByPaneKey.delete(paneKey) + return + } + for (const [callId, index] of readReplyItems( + body.slice(REPLY_OPEN.length, -REPLY_CLOSE.length) + )) { + const call = calls.get(callId) + call?.unanswered.delete(index) + if (call?.unanswered.size === 0) { + calls.delete(callId) + } + } + if (calls.size === 0) { + state.codexAsyncQuestionsByPaneKey.delete(paneKey) + } +} + +function readReplyItems(json: string): [string, number][] { + const items = parseJson(json) + if (!Array.isArray(items)) { + return [] + } + return items.flatMap((item): [string, number][] => { + const id = + item && typeof item === 'object' && 'questionItemId' in item + ? parseJson(String(item.questionItemId)) + : null + return Array.isArray(id) && + id[0] === CODEX_ASYNC_QUESTION_TOOL && + typeof id[1] === 'string' && + typeof id[2] === 'number' + ? [[id[1], id[2]]] + : [] + }) +} + +function parseJson(text: string): unknown { + try { + return JSON.parse(text) + } catch { + return null + } +} diff --git a/src/shared/agent-hook-listener/providers/codex-events.ts b/src/shared/agent-hook-listener/providers/codex-events.ts index 2e1b1e423e8..18bfd176914 100644 --- a/src/shared/agent-hook-listener/providers/codex-events.ts +++ b/src/shared/agent-hook-listener/providers/codex-events.ts @@ -40,6 +40,7 @@ import { resolveCodexPaneStatus, setCodexMainAgentTurnState } from './codex-state' +import { pendingCodexAsyncQuestion, trackCodexAsyncQuestions } from './codex-async-questions' export function buildCodexStatusPayload( state: HookListenerState, @@ -55,6 +56,11 @@ export function buildCodexStatusPayload( }) : (state.lastToolByPaneKey.get(paneKey) ?? {}) const lead = state.codexLeadStateByPaneKey.get(paneKey) + // An approval or blocking question on screen wins; otherwise a waiting row shows the open async question. + const asyncQuestion = + options.stateName === 'waiting' && !snapshot.interactivePrompt + ? pendingCodexAsyncQuestion(state, paneKey) + : undefined return normalizeAgentStatusPayload({ state: options.stateName, @@ -64,9 +70,9 @@ export function buildCodexStatusPayload( }), agentType: 'codex', model: lead?.model, - toolName: snapshot.toolName, + toolName: asyncQuestion?.toolName ?? snapshot.toolName, toolInput: snapshot.toolInput, - interactivePrompt: snapshot.interactivePrompt, + interactivePrompt: asyncQuestion?.prompt ?? snapshot.interactivePrompt, lastAssistantMessage: snapshot.lastAssistantMessage, lastAssistantMessageIsToolOutput: snapshot.lastAssistantMessageIsToolOutput, interrupted: mainAgentTurnInterrupted(lead), @@ -242,6 +248,11 @@ export function normalizeCodexEvent( transcriptPath, stateName ) + // An open async question needs the user even while Codex keeps working. + const leadState = + trackCodexAsyncQuestions(state, paneKey, eventName, hookPayload) && ownedState === 'working' + ? 'waiting' + : ownedState const previousLead = state.codexLeadStateByPaneKey.get(paneKey) const transcript = state.codexSubagentTranscriptByPaneKey.get(paneKey) const turnId = readString(hookPayload, 'turn_id') @@ -260,10 +271,10 @@ export function normalizeCodexEvent( !previousLead?.turnId || codexTranscriptTurnEnd(transcript, previousLead.turnId) !== undefined)) const record = setCodexMainAgentTurnState(state, paneKey, { - state: ownedState, + state: leadState, ...(failedTurn ? { outcome: 'failure' as const } - : codexLeadOutcomeForEvent(eventName, previousLead, ownedState)), + : codexLeadOutcomeForEvent(eventName, previousLead, leadState)), model: normalizeOptionalField(hookPayload['model'], AGENT_MODEL_MAX_LENGTH) ?? (eventName === 'SessionStart' ? undefined : previousLead?.model), diff --git a/src/shared/agent-hook-listener/providers/codex-state.ts b/src/shared/agent-hook-listener/providers/codex-state.ts index 34e43c2143b..e96db368fbb 100644 --- a/src/shared/agent-hook-listener/providers/codex-state.ts +++ b/src/shared/agent-hook-listener/providers/codex-state.ts @@ -1,6 +1,7 @@ import type { AgentMainAgentStatus } from '../../agent-status/lifecycle/main-agent-status' import type { ParsedAgentStatusPayload } from '../../agent-status/contracts/agent-status-types' import { isAskUserQuestionTool } from '../../agent-status/lifecycle/agent-question-answered-intent' +import { CODEX_ASYNC_QUESTION_TOOL } from '../../codex-async-question-input' import { continueMainAgentStatus, foldAgentLeadStatus, @@ -191,9 +192,14 @@ export function codexLeadStateForHookEvent( toolName?: string ): CodexLeadTurnState['state'] | undefined { // Preserve execution-host reviewer ownership; event names alone would turn every relayed approval into waiting. - return eventName === 'PermissionRequest' && normalizedState === 'working' - ? 'working' - : codexHookEventState(eventName, toolName) + if (eventName === 'PermissionRequest' && normalizedState === 'working') { + return 'working' + } + // The execution host tracks Codex's async questions; its waiting outlasts the events around it. + if (normalizedState === 'waiting' && toolName === CODEX_ASYNC_QUESTION_TOOL) { + return 'waiting' + } + return codexHookEventState(eventName, toolName) } /** Which children an unlabelled relayed poll may retire by omitting them. */ diff --git a/src/shared/native-chat/input/native-chat-interactive-prompt.test.ts b/src/shared/native-chat/input/native-chat-interactive-prompt.test.ts index 99ba0cc39b5..12eb8b61f4d 100644 --- a/src/shared/native-chat/input/native-chat-interactive-prompt.test.ts +++ b/src/shared/native-chat/input/native-chat-interactive-prompt.test.ts @@ -58,4 +58,17 @@ describe('resolveNativeChatInteractivePromptCard', () => { expect(resolve(JSON.stringify({ approval: { tool: 'Bash' } }))?.kind).toBe('approval') expect(resolve(null)?.kind).toBe('question') }) + + it("keeps Codex's async question on the transcript's copy, which drops answered questions", () => { + const card = resolveNativeChatInteractivePromptCard({ + interactivePrompt: JSON.stringify({ + questions: [{ title: 'Which database?', options: ['Postgres'] }, { title: 'Table name?' }] + }), + toolName: 'request_user_input_async', + agent: 'codex', + messages: [pendingAsk], + transcriptSettled: true + }) + expect(card?.kind === 'question' && card.prompt.questions[0]?.question).toBe('Which path?') + }) }) diff --git a/src/shared/native-chat/input/native-chat-interactive-prompt.ts b/src/shared/native-chat/input/native-chat-interactive-prompt.ts index c542dcd430e..6b782f0481e 100644 --- a/src/shared/native-chat/input/native-chat-interactive-prompt.ts +++ b/src/shared/native-chat/input/native-chat-interactive-prompt.ts @@ -120,8 +120,10 @@ export function resolveNativeChatInteractivePromptCard(args: { if (statusCard?.kind === 'approval') { return statusCard } + // Codex's async questions come from the transcript, which knows which ones were answered. + const liveAsk = statusCard?.prompt.replyAsMessage ? null : (statusCard?.prompt ?? null) const prompt = resolveNativeChatAsk({ - liveAsk: statusCard?.prompt ?? null, + liveAsk, messages: args.messages, transcriptSettled: args.transcriptSettled, transcriptAsk: args.transcriptAsk From 81e5e5a9ef83e015f19bcc079a03414c09605898 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:46:25 +0000 Subject: [PATCH 10/28] feat(settings): redesign Voice settings around a setup card and a rated model list (#40452) --- src/main/speech/model-catalog.ts | 24 -- .../speech-model-update-target.test.ts | 1 - .../settings-capability-section-renderers.tsx | 4 +- .../VoiceDictationLanguageSetting.fixture.tsx | 1 - .../VoiceDictationLanguageSetting.test.tsx | 25 -- .../voice/VoiceDictationLanguageSetting.tsx | 15 +- .../voice/VoiceDictationSettingsSection.tsx | 101 ------ .../settings/voice/VoiceInputSettings.tsx | 96 ++++++ .../settings/voice/VoiceMicrophoneSetting.tsx | 60 +--- .../settings/voice/VoicePane.test.tsx | 9 +- .../components/settings/voice/VoicePane.tsx | 228 ++++++------ .../settings/voice/VoiceSetupCard.fixture.tsx | 60 ++++ .../settings/voice/VoiceSetupCard.tsx | 177 ++++++++++ .../voice/VoiceSpeechModelQuality.tsx | 103 ++++++ .../settings/voice/VoiceSpeechModelRow.tsx | 191 ++++++++++ .../voice/VoiceSpeechModelSection.fixture.tsx | 128 +++++-- .../voice/VoiceSpeechModelSection.test.tsx | 41 +-- .../voice/VoiceSpeechModelSection.tsx | 325 ++++++------------ .../voice/speech-model-description.ts | 69 ---- .../settings/voice/speech-model-download.ts | 26 ++ .../voice/use-voice-microphone-devices.ts | 67 ++++ .../voice/voice-speech-model-rows.test.ts | 42 +++ .../settings/voice/voice-speech-model-rows.ts | 47 +++ src/renderer/src/i18n/locales/de.json | 81 +++-- src/renderer/src/i18n/locales/en.json | 82 +++-- src/renderer/src/i18n/locales/es.json | 81 +++-- src/renderer/src/i18n/locales/fr.json | 81 +++-- src/renderer/src/i18n/locales/ja.json | 81 +++-- src/renderer/src/i18n/locales/ko.json | 81 +++-- src/renderer/src/i18n/locales/pt.json | 81 +++-- src/renderer/src/i18n/locales/ru.json | 81 +++-- src/renderer/src/i18n/locales/tr.json | 81 +++-- src/renderer/src/i18n/locales/zh-Hant.json | 81 +++-- src/renderer/src/i18n/locales/zh.json | 81 +++-- src/shared/speech-types.ts | 1 - 35 files changed, 1833 insertions(+), 900 deletions(-) delete mode 100644 src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx create mode 100644 src/renderer/src/components/settings/voice/VoiceInputSettings.tsx create mode 100644 src/renderer/src/components/settings/voice/VoiceSetupCard.fixture.tsx create mode 100644 src/renderer/src/components/settings/voice/VoiceSetupCard.tsx create mode 100644 src/renderer/src/components/settings/voice/VoiceSpeechModelQuality.tsx create mode 100644 src/renderer/src/components/settings/voice/VoiceSpeechModelRow.tsx delete mode 100644 src/renderer/src/components/settings/voice/speech-model-description.ts create mode 100644 src/renderer/src/components/settings/voice/speech-model-download.ts create mode 100644 src/renderer/src/components/settings/voice/use-voice-microphone-devices.ts create mode 100644 src/renderer/src/components/settings/voice/voice-speech-model-rows.test.ts create mode 100644 src/renderer/src/components/settings/voice/voice-speech-model-rows.ts diff --git a/src/main/speech/model-catalog.ts b/src/main/speech/model-catalog.ts index 97ab776a901..258a129f940 100644 --- a/src/main/speech/model-catalog.ts +++ b/src/main/speech/model-catalog.ts @@ -18,8 +18,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'parakeet-tdt-0.6b-v3-int8', label: 'Parakeet TDT v3', - description: - 'Highest accuracy for 25 European languages. Punctuation, capitalization, and word-level timestamps.', type: 'transducer', provider: 'local', language: 'multilingual', @@ -35,8 +33,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'parakeet-tdt-0.6b-v2-int8', label: 'Parakeet TDT v2', - description: - 'English only. Faster than v3 with similar accuracy. Punctuation and capitalization.', type: 'transducer', provider: 'local', language: 'en', @@ -51,7 +47,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'zipformer-bilingual-zh-en', label: 'Zipformer Bilingual', - description: 'Chinese + English with code-switching. Low-latency real-time streaming.', type: 'transducer', provider: 'local', language: 'zh-en', @@ -65,8 +60,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'paraformer-bilingual-zh-en', label: 'Paraformer Bilingual', - description: - 'Chinese (Mandarin + dialects) + English. Strong on accented and regional Chinese.', type: 'paraformer', provider: 'local', language: 'zh-en', @@ -80,7 +73,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'zipformer-streaming-en-20m', label: 'Zipformer Streaming EN', - description: 'English only. Lightweight 20M-param model, good balance of speed and size.', type: 'transducer', provider: 'local', language: 'en', @@ -94,7 +86,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'zipformer-streaming-zh-14m', label: 'Zipformer Streaming ZH', - description: 'Chinese only. Ultra-lightweight 14M-param model, ideal for low-resource devices.', type: 'transducer', provider: 'local', language: 'zh', @@ -108,7 +99,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'zipformer-streaming-korean', label: 'Zipformer Streaming KO', - description: 'Korean only. Low-latency real-time streaming.', type: 'transducer', provider: 'local', language: 'ko', @@ -122,7 +112,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'parakeet-tdt-ctc-0.6b-ja-int8', label: 'Parakeet TDT-CTC JA', - description: 'Japanese only. Trained on 35k+ hours of natural speech. Punctuation included.', type: 'nemo-ctc', provider: 'local', language: 'ja', @@ -136,7 +125,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'whisper-tiny', label: 'Whisper Tiny', - description: '90+ languages. Lower accuracy than Parakeet but broadest language coverage.', type: 'whisper', provider: 'local', language: 'multilingual', @@ -152,8 +140,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'whisper-large-v3-turbo', label: 'Whisper Large v3 Turbo', - description: - 'Best accuracy for Japanese, Korean, and Chinese, and 90+ languages. Slower than Parakeet.', type: 'whisper', provider: 'local', language: 'multilingual', @@ -168,8 +154,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'sense-voice-zh-en-ja-ko-yue', label: 'SenseVoice', - description: - 'Chinese, English, Japanese, Korean, and Cantonese with automatic language detection.', type: 'senseVoice', provider: 'local', language: 'multilingual', @@ -185,8 +169,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'nemotron-3.5-asr-streaming-0.6b', label: 'Nemotron 3.5', - description: - 'English, Spanish, German and many more languages, with automatic language detection.', type: 'transducer', provider: 'local', language: 'multilingual', @@ -202,7 +184,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'openai-gpt-transcribe', label: 'GPT Transcribe', - description: 'Cloud transcription with high accuracy. Requires an OpenAI API key.', type: 'openai', provider: 'openai', language: 'multilingual', @@ -214,8 +195,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'openai-compatible', label: 'OpenAI-compatible', - description: - 'Any service with an OpenAI-style transcription API, such as Groq, Mistral or a local server.', type: 'openai', provider: 'openai-compatible', language: 'multilingual', @@ -227,8 +206,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'openai-gpt-4o-mini-transcribe', label: 'GPT-4o mini Transcribe', - description: - 'Cloud transcription with strong accuracy and low cost. Requires an OpenAI API key.', type: 'openai', provider: 'openai', language: 'multilingual', @@ -242,7 +219,6 @@ export const SPEECH_MODEL_CATALOG: SpeechModelManifest[] = [ { id: 'openai-gpt-4o-transcribe', label: 'GPT-4o Transcribe', - description: 'Cloud transcription with higher accuracy. Requires an OpenAI API key.', type: 'openai', provider: 'openai', language: 'multilingual', diff --git a/src/renderer/src/components/dictation/speech-model-update-target.test.ts b/src/renderer/src/components/dictation/speech-model-update-target.test.ts index c59e4e8bdd6..538bf45e94d 100644 --- a/src/renderer/src/components/dictation/speech-model-update-target.test.ts +++ b/src/renderer/src/components/dictation/speech-model-update-target.test.ts @@ -6,7 +6,6 @@ function model(id: string, extra: Partial = {}): SpeechMode return { id, label: id, - description: '', type: 'transducer', provider: 'local', language: 'en', diff --git a/src/renderer/src/components/settings/page/settings-capability-section-renderers.tsx b/src/renderer/src/components/settings/page/settings-capability-section-renderers.tsx index e3ec3097cca..5cddf1f09a0 100644 --- a/src/renderer/src/components/settings/page/settings-capability-section-renderers.tsx +++ b/src/renderer/src/components/settings/page/settings-capability-section-renderers.tsx @@ -177,8 +177,8 @@ export function renderDesktopCapabilitySettingsSections( id="voice" title={t('auto.components.settings.Settings.5063bb47a5', 'Voice')} description={t( - 'auto.components.settings.Settings.eb1176a14e', - 'Local speech-to-text dictation with on-device models.' + 'components.settings.voice.sectionDescription', + 'Talk instead of typing in any pane. Speech is recognized on this computer unless you pick a cloud model.' )} searchEntries={navigation.getSectionSearchEntries('voice')} > diff --git a/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.fixture.tsx b/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.fixture.tsx index 7e048404a80..9fde64dff1f 100644 --- a/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.fixture.tsx +++ b/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.fixture.tsx @@ -15,7 +15,6 @@ function model( return { id: label, label, - description: '', type, provider: 'local', language: 'multilingual', diff --git a/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.test.tsx b/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.test.tsx index 94d39441b7f..fea193fe3bf 100644 --- a/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.test.tsx +++ b/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.test.tsx @@ -35,7 +35,6 @@ it('keeps a selected language visible when the chosen model does not support it' const selectedModel: SpeechModelManifest = { id: 'limited', label: 'Limited model', - description: '', type: 'whisper', provider: 'local', language: 'multilingual', @@ -59,27 +58,3 @@ it('keeps a selected language visible when the chosen model does not support it' fireEvent.click(screen.getByRole('combobox', { name: 'Dictation Language' })) expect(screen.getByRole('option', { name: 'Korean' })).toBeTruthy() }) - -it('keeps the language control disabled for models that ignore language settings', () => { - const selectedModel: SpeechModelManifest = { - id: 'parakeet', - label: 'Parakeet', - description: '', - type: 'transducer', - provider: 'local', - language: 'multilingual', - languages: ['en', 'es'], - sampleRate: 16000, - streaming: false - } - render( - - ) - - const trigger = screen.getByRole('combobox', { name: 'Dictation Language' }) - expect(trigger.getAttribute('disabled')).not.toBeNull() -}) diff --git a/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.tsx b/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.tsx index 22553593c17..8d0072f6688 100644 --- a/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.tsx +++ b/src/renderer/src/components/settings/voice/VoiceDictationLanguageSetting.tsx @@ -55,12 +55,14 @@ export function VoiceDictationLanguageSetting({ const intlLocale = useIntlLocale() const modelIgnoresLanguage = selectedModel !== undefined && !selectedModel.dictationLanguages const language = voiceSettings.dictationLanguage || DICTATION_LANGUAGE_AUTO - const choices = selectedModel?.dictationLanguages ?? DICTATION_LANGUAGES - // Keep a choice the model lacks selectable rather than showing a blank trigger. - const codes = - language === DICTATION_LANGUAGE_AUTO || choices.includes(language) - ? choices - : [language, ...choices] + // Every language, even ones this model lacks: the choice also rates the other models. + const codes = [ + ...new Set([ + ...(language === DICTATION_LANGUAGE_AUTO ? [] : [language]), + ...DICTATION_LANGUAGES, + ...(selectedModel?.dictationLanguages ?? []) + ]) + ] const collator = uiCollator(NAME_COLLATOR, intlLocale) const options = codes .map((code) => ({ code, name: languageDisplayName(code, intlLocale) })) @@ -99,7 +101,6 @@ export function VoiceDictationLanguageSetting({ 'auto.components.settings.SettingsSearchableSelect.searchLanguages', 'Search languages…' )} - disabled={!voiceSettings.enabled || modelIgnoresLanguage} onValueChange={(value) => onUpdateVoiceSettings({ dictationLanguage: value })} /> } diff --git a/src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx b/src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx deleted file mode 100644 index 9edcdec2532..00000000000 --- a/src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx +++ /dev/null @@ -1,101 +0,0 @@ -import type { VoiceSettings } from '../../../../../shared/speech-types' -import { - dictationShortcutMode, - dictationShortcutModeSettings -} from '../../../../../shared/dictation-shortcut-mode' -import { Label } from '../../ui/label' -import { Separator } from '../../ui/separator' -import { Switch } from '../../ui/switch' -import { useShortcutLabel } from '@/hooks/useShortcutLabel' -import { useTranslate } from '@/i18n/use-translate' -import { VoiceMicrophoneSetting } from './VoiceMicrophoneSetting' - -type VoiceDictationSettingsSectionProps = { - voiceSettings: VoiceSettings - permissionPending: boolean - onToggleVoiceDictation: () => void - onUpdateVoiceSettings: (updates: Partial) => void -} - -export function VoiceDictationSettingsSection({ - voiceSettings, - permissionPending, - onToggleVoiceDictation, - onUpdateVoiceSettings -}: VoiceDictationSettingsSectionProps): React.JSX.Element { - const t = useTranslate() - const shortcutLabel = useShortcutLabel('voice.dictation') - const selectedMode = dictationShortcutMode(voiceSettings) - const modeLabels = { - auto: t('auto.components.settings.VoicePane.dictationModeAuto', 'Auto'), - hold: t('auto.components.settings.VoicePane.174da92062', 'Hold'), - toggle: t('auto.components.settings.VoicePane.118b3c2dee', 'Toggle') - } - - return ( - <> -
-
- -

- {t( - 'auto.components.settings.VoicePane.dictationShortcutDescription', - 'Press {{shortcut}} to dictate text into any focused pane.', - { shortcut: shortcutLabel } - )} -

-
- void onToggleVoiceDictation()} - className="disabled:cursor-wait disabled:opacity-70" - /> -
- - - -
-
- -

- {t( - 'auto.components.settings.VoicePane.dictationModesWithAutoDescription', - 'Auto: hold {{shortcut}} to dictate until you let go, or tap it to start and stop. Hold: dictate while {{shortcut}} is held. Toggle: press {{shortcut}} once to start, again to stop.', - { shortcut: shortcutLabel } - )} -

-
-
- {(['auto', 'hold', 'toggle'] as const).map((mode) => ( - - ))} -
-
- - - - - - - - ) -} diff --git a/src/renderer/src/components/settings/voice/VoiceInputSettings.tsx b/src/renderer/src/components/settings/voice/VoiceInputSettings.tsx new file mode 100644 index 00000000000..11645289e91 --- /dev/null +++ b/src/renderer/src/components/settings/voice/VoiceInputSettings.tsx @@ -0,0 +1,96 @@ +import type { VoiceSettings } from '../../../../../shared/speech-types' +import { + dictationShortcutMode, + dictationShortcutModeSettings, + type DictationShortcutMode +} from '../../../../../shared/dictation-shortcut-mode' +import { useAppStore } from '@/store' +import { ShortcutKeyCombo } from '@/components/ShortcutKeyCombo' +import { useShortcutKeyDetails, useShortcutLabel } from '@/hooks/useShortcutLabel' +import { useTranslate } from '@/i18n/use-translate' +import { Button } from '../../ui/button' +import { + SettingsRow, + SettingsSegmentedControl, + SettingsSubsectionHeader +} from '../form-controls/SettingsFormControls' +import { VoiceMicrophoneSetting } from './VoiceMicrophoneSetting' + +export const VOICE_MICROPHONE_ROW_ID = 'voice-microphone' + +type VoiceInputSettingsProps = { + voiceSettings: VoiceSettings + onUpdateVoiceSettings: (updates: Partial) => void +} + +export function VoiceInputSettings({ + voiceSettings, + onUpdateVoiceSettings +}: VoiceInputSettingsProps): React.JSX.Element { + const t = useTranslate() + const openSettingsTarget = useAppStore((s) => s.openSettingsTarget) + const shortcutLabel = useShortcutLabel('voice.dictation') + const shortcutKeys = useShortcutKeyDetails('voice.dictation') + + return ( +
+ +
+ + + +
+ } + /> + + value={dictationShortcutMode(voiceSettings)} + onChange={(mode) => onUpdateVoiceSettings(dictationShortcutModeSettings(mode))} + ariaLabel={t('auto.components.settings.VoicePane.ba4a900d1d', 'Dictation Mode')} + options={[ + { + value: 'auto', + label: t('auto.components.settings.VoicePane.dictationModeAuto', 'Auto') + }, + { + value: 'hold', + label: t('auto.components.settings.VoicePane.174da92062', 'Hold') + }, + { + value: 'toggle', + label: t('auto.components.settings.VoicePane.118b3c2dee', 'Toggle') + } + ]} + /> + } + /> +
+ +
+
+
+ ) +} diff --git a/src/renderer/src/components/settings/voice/VoiceMicrophoneSetting.tsx b/src/renderer/src/components/settings/voice/VoiceMicrophoneSetting.tsx index 38fc213baf7..4a7fb26886c 100644 --- a/src/renderer/src/components/settings/voice/VoiceMicrophoneSetting.tsx +++ b/src/renderer/src/components/settings/voice/VoiceMicrophoneSetting.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useMemo, useRef, useState } from 'react' +import { useCallback, useMemo, useState } from 'react' import { toast } from 'sonner' import type { VoiceSettings } from '../../../../../shared/speech-types' import { Button } from '../../ui/button' @@ -6,15 +6,14 @@ import { Label } from '../../ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../../ui/select' import { buildVoiceMicrophoneSelectOptions, - listVoiceMicrophoneDevices, - microphoneDeviceIdFromSelectValue, - type VoiceMicrophoneDevice + microphoneDeviceIdFromSelectValue } from '@/components/dictation/microphone-devices' import { useMountedRef } from '@/hooks/useMountedRef' import { translate } from '@/i18n/i18n' import { useTranslate } from '@/i18n/use-translate' import { useUiLanguageRevision } from '@/i18n/use-ui-language-revision' import { extractIpcErrorMessage } from '@/lib/ipc-error' +import { useVoiceMicrophoneDevices } from './use-voice-microphone-devices' type VoiceMicrophoneSettingProps = { voiceSettings: VoiceSettings @@ -62,66 +61,17 @@ function microphoneAccessErrorMessage(error: unknown): string { ) } -function sameDeviceList( - a: readonly VoiceMicrophoneDevice[], - b: readonly VoiceMicrophoneDevice[] -): boolean { - return ( - a.length === b.length && - a.every((device, index) => { - const other = b[index] - return device.deviceId === other?.deviceId && device.label === other.label - }) - ) -} - export function VoiceMicrophoneSetting({ voiceSettings, onUpdateVoiceSettings }: VoiceMicrophoneSettingProps): React.JSX.Element { const t = useTranslate() const languageRevision = useUiLanguageRevision() - const [devices, setDevices] = useState([]) - const [devicesKnown, setDevicesKnown] = useState(false) + // Rescan when dictation enables because permission and real device labels may arrive with that toggle. + const { devices, devicesKnown, refreshDevices } = useVoiceMicrophoneDevices(voiceSettings.enabled) const [accessPending, setAccessPending] = useState(false) const [accessError, setAccessError] = useState(null) const mountedRef = useMountedRef() - // Bluetooth emits repeated devicechange events; reject stale enumeration results. - const refreshGenerationRef = useRef(0) - - const refreshDevices = useCallback(async (): Promise => { - const generation = refreshGenerationRef.current + 1 - refreshGenerationRef.current = generation - if (typeof navigator === 'undefined' || !navigator.mediaDevices?.enumerateDevices) { - return - } - let next: VoiceMicrophoneDevice[] = [] - try { - next = listVoiceMicrophoneDevices(await navigator.mediaDevices.enumerateDevices()) - } catch { - next = [] - } - if (!mountedRef.current || refreshGenerationRef.current !== generation) { - return - } - setDevicesKnown(next.length > 0) - setDevices((current) => (sameDeviceList(current, next) ? current : next)) - }, [mountedRef]) - - // Rescan when dictation enables because permission and real device labels may arrive with that toggle. - useEffect(() => { - void refreshDevices() - if (typeof navigator === 'undefined' || !navigator.mediaDevices?.addEventListener) { - return - } - const handleDeviceChange = (): void => { - void refreshDevices() - } - navigator.mediaDevices.addEventListener('devicechange', handleDeviceChange) - return () => { - navigator.mediaDevices.removeEventListener('devicechange', handleDeviceChange) - } - }, [refreshDevices, voiceSettings.enabled]) // A generic stream grants discovery even when the saved device is stale. const openStreamAndRefreshDevices = useCallback(async (): Promise => { diff --git a/src/renderer/src/components/settings/voice/VoicePane.test.tsx b/src/renderer/src/components/settings/voice/VoicePane.test.tsx index 19e1fc6f5f7..0694de92ee3 100644 --- a/src/renderer/src/components/settings/voice/VoicePane.test.tsx +++ b/src/renderer/src/components/settings/voice/VoicePane.test.tsx @@ -19,7 +19,8 @@ const { useAppStoreMock, useShortcutLabelMock } = vi.hoisted(() => ({ vi.mock('@/store/index', () => ({ useAppStore: useAppStoreMock })) vi.mock('@/hooks/useShortcutLabel', () => ({ - useShortcutLabel: useShortcutLabelMock + useShortcutLabel: useShortcutLabelMock, + useShortcutKeyDetails: () => ({ keys: [], doubleTap: false }) })) vi.mock('sonner', () => ({ @@ -108,6 +109,12 @@ describe('VoicePane', () => { root.render() }) + const cloudTab = Array.from( + container.querySelectorAll('[role="radio"]') + ).find((button) => button.textContent === 'Cloud') + await act(async () => { + cloudTab?.dispatchEvent(new MouseEvent('click', { bubbles: true })) + }) const disconnect = container.querySelector( 'button[aria-label="Disconnect OpenAI API key"]' ) diff --git a/src/renderer/src/components/settings/voice/VoicePane.tsx b/src/renderer/src/components/settings/voice/VoicePane.tsx index 9b008cdd441..b29ea9ff4a7 100644 --- a/src/renderer/src/components/settings/voice/VoicePane.tsx +++ b/src/renderer/src/components/settings/voice/VoicePane.tsx @@ -12,15 +12,18 @@ import { OpenAiCompatibleProviderDialog } from './OpenAiCompatibleProviderDialog import { OpenAiTranscriptionSettingsRow } from './OpenAiTranscriptionSettingsRow' import { handleVoiceDictationToggle } from './voice-dictation-toggle' import { VoiceDictationLanguageSetting } from './VoiceDictationLanguageSetting' -import { VoiceDictationSettingsSection } from './VoiceDictationSettingsSection' +import { VOICE_MICROPHONE_ROW_ID, VoiceInputSettings } from './VoiceInputSettings' +import { VoiceSetupCard } from './VoiceSetupCard' +import { cancelSpeechModelDownload, startSpeechModelDownload } from './speech-model-download' import { VoiceModelUnloadSetting } from './VoiceModelUnloadSetting' import { VoiceSpeechModelSection } from './VoiceSpeechModelSection' -import { SettingsSwitchRow } from '../form-controls/SettingsFormControls' +import { SettingsSubsectionHeader, SettingsSwitchRow } from '../form-controls/SettingsFormControls' import { VoiceSpeechGpuSetting } from './VoiceSpeechGpuSetting' -import { matchesSettingsSearch } from '../search/settings-search' -import { getOpenaiTranscriptionSearchEntry } from './voice-pane-search' import { useTranslate } from '@/i18n/use-translate' import { readIpcErrorMessage } from '@/lib/ipc-error' +import { useLanguageReadiness } from '@/i18n/language-readiness' +import { motionSafeScrollBehavior } from '@/hooks/usePrefersReducedMotion' +import { speechModelRatingLanguage } from '../../../../../shared/speech-model-quality' type VoicePaneProps = { settings: GlobalSettings @@ -35,8 +38,10 @@ export function VoicePane({ settings, updateSettings }: VoicePaneProps): React.J const modelStates = useAppStore((s) => s.modelStates) const refreshModelStates = useAppStore((s) => s.refreshModelStates) const markFeatureTipsSeen = useAppStore((s) => s.markFeatureTipsSeen) - const settingsSearchQuery = useAppStore((s) => s.settingsSearchQuery ?? '') - const [catalog, setCatalog] = useState([]) + const systemLanguageTag = useLanguageReadiness((s) => s.detection?.detectedTag) + // null until main answers, so the card and list never show a state the catalog would contradict. + const [loadedCatalog, setCatalog] = useState(null) + const catalog = loadedCatalog ?? [] const [permissionPending, setPermissionPending] = useState(false) const [openAiDialogOpen, setOpenAiDialogOpen] = useState(false) const [openAiApiKeyDraft, setOpenAiApiKeyDraft] = useState('') @@ -143,18 +148,11 @@ export function VoicePane({ settings, updateSettings }: VoicePaneProps): React.J } const selectedModel = catalog.find((m) => m.id === voiceSettings.sttModel) - const showOpenAiSettingsRow = - voiceSettings.openAiApiKeyConfigured || - selectedModel?.provider === 'openai' || - (settingsSearchQuery.trim() !== '' && - matchesSettingsSearch(settingsSearchQuery, getOpenaiTranscriptionSearchEntry())) const compatibleModel = catalog.find((m) => m.provider === 'openai-compatible') const compatibleConfigured = compatibleModel !== undefined && modelStates.some((s) => s.id === compatibleModel.id && s.status === 'ready') - const showCompatibleSettingsRow = - compatibleConfigured || selectedModel?.provider === 'openai-compatible' const openOpenAiDialog = (modelId: string | null = null): void => { setPendingCloudModelId(modelId) @@ -167,11 +165,11 @@ export function VoicePane({ settings, updateSettings }: VoicePaneProps): React.J setCompatibleDialogOpen(true) } - const openCloudSetup = (manifest: SpeechModelManifest): void => { - if (manifest.provider === 'openai-compatible') { - openCompatibleDialog(manifest.id) + const openCloudSetup = (modelId: string): void => { + if (catalog.find((m) => m.id === modelId)?.provider === 'openai-compatible') { + openCompatibleDialog(modelId) } else { - openOpenAiDialog(manifest.id) + openOpenAiDialog(modelId) } } @@ -252,95 +250,127 @@ export function VoicePane({ settings, updateSettings }: VoicePaneProps): React.J } } + const selectedState = modelStates.find((s) => s.id === voiceSettings.sttModel) + const ratingLanguage = + speechModelRatingLanguage(voiceSettings.dictationLanguage, systemLanguageTag ?? undefined) ?? + 'en' + return ( -
- void toggleVoiceDictation()} - onUpdateVoiceSettings={updateVoiceSettings} - /> - - - - - - - - - - - updateVoiceSettings({ removeFillerWords: !voiceSettings.removeFillerWords }) - } - /> - - - - - - {selectedModel?.provider === 'local' && ( +
+ {loadedCatalog ? ( <> - + void toggleVoiceDictation()} + onDownloadModel={(modelId) => startSpeechModelDownload(modelId, refreshModelStates)} + onCancelDownload={(modelId) => cancelSpeechModelDownload(modelId, refreshModelStates)} + onShowMicrophone={() => + document + .getElementById(VOICE_MICROPHONE_ROW_ID) + ?.scrollIntoView({ behavior: motionSafeScrollBehavior(), block: 'center' }) + } + /> + + + {voiceSettings.openAiApiKeyConfigured ? ( + <> + + openOpenAiDialog(null)} + onClear={() => void clearOpenAiApiKey()} + /> + + ) : null} + {compatibleConfigured ? ( + openCompatibleDialog(null)} + onClear={() => void removeCompatibleProvider()} + /> + ) : null} + + } + /> + + ) : null} + + + +
+ + +
+ + + +
+ + + updateVoiceSettings({ removeFillerWords: !voiceSettings.removeFillerWords }) + } + /> +
+ + + + + + + +
+ +
- - )} - - {showOpenAiSettingsRow && ( - <> - - - openOpenAiDialog(null)} - onClear={() => void clearOpenAiApiKey()} - /> - - )} - - {showCompatibleSettingsRow && ( - <> - - openCompatibleDialog(null)} - onClear={() => void removeCompatibleProvider()} - /> - - )} +
+
{}} + onDownloadModel={() => {}} + onCancelDownload={() => {}} + onShowMicrophone={() => {}} + /> + ) +} + +export const newUser = defineFixture({ + width: 760, + render: () => render(false, undefined, undefined) +}) + +export const on = defineFixture({ + width: 760, + render: () => render(true, parakeet, { id: 'v3', status: 'ready' }) +}) + +export const off = defineFixture({ + width: 760, + render: () => render(false, parakeet, { id: 'v3', status: 'ready' }) +}) + +export const needsUpdate = defineFixture({ + width: 760, + render: () => render(true, parakeet, { id: 'v3', status: 'not-downloaded', needsUpdate: true }) +}) + +export const downloading = defineFixture({ + width: 760, + render: () => render(true, parakeet, { id: 'v3', status: 'downloading', progress: 0.42 }) +}) diff --git a/src/renderer/src/components/settings/voice/VoiceSetupCard.tsx b/src/renderer/src/components/settings/voice/VoiceSetupCard.tsx new file mode 100644 index 00000000000..32a11b6e2a1 --- /dev/null +++ b/src/renderer/src/components/settings/voice/VoiceSetupCard.tsx @@ -0,0 +1,177 @@ +import { CircleAlert, Check, Download, Mic } from 'lucide-react' +import type { + SpeechModelManifest, + SpeechModelState, + VoiceSettings +} from '../../../../../shared/speech-types' +import { resolveMicrophoneDevice } from '@/components/dictation/microphone-devices' +import { useShortcutLabel } from '@/hooks/useShortcutLabel' +import { useTranslate } from '@/i18n/use-translate' +import { cn } from '@/lib/utils' +import { Button } from '../../ui/button' +import { Progress } from '../../ui/progress' +import { Switch } from '../../ui/switch' +import { useVoiceMicrophoneDevices } from './use-voice-microphone-devices' + +type VoiceSetupCardProps = { + voiceSettings: VoiceSettings + selectedModel: SpeechModelManifest | undefined + selectedState: SpeechModelState | undefined + permissionPending: boolean + onToggleVoiceDictation: () => void + onDownloadModel: (modelId: string) => void + onCancelDownload: (modelId: string) => void + onShowMicrophone: () => void +} + +/** The pane's first card: dictation's on/off switch and the one problem to fix, if any. */ +export function VoiceSetupCard({ + voiceSettings, + selectedModel, + selectedState, + permissionPending, + onToggleVoiceDictation, + onDownloadModel, + onCancelDownload, + onShowMicrophone +}: VoiceSetupCardProps): React.JSX.Element { + const t = useTranslate() + const shortcut = useShortcutLabel('voice.dictation') + const { devices, devicesKnown } = useVoiceMicrophoneDevices(voiceSettings.enabled) + const microphone = resolveMicrophoneDevice({ + devices: devicesKnown ? devices : null, + preferredDeviceId: voiceSettings.microphoneDeviceId, + preferredDeviceLabel: voiceSettings.microphoneDeviceLabel + }) + const downloading = + selectedState?.status === 'downloading' || selectedState?.status === 'extracting' + const ready = selectedState?.status === 'ready' + const totalMb = Math.round((selectedModel?.sizeBytes ?? 0) / 1_000_000) + const names = { model: selectedModel?.label ?? '', size: totalMb, shortcut } + + let tone: 'neutral' | 'ok' | 'warn' = 'neutral' + let icon = + let title: string + let description: string + let body: React.ReactNode = null + + if (selectedModel && downloading) { + icon = + const percent = Math.round((selectedState?.progress ?? 0) * 100) + title = t('speechModelUpdate.downloadingTitle', 'Downloading {{model}}', names) + description = t( + 'components.settings.voice.setup.downloadingDescription', + 'You can leave this page. Dictation works once the download finishes.' + ) + body = ( +
+ + + {t('speechModelUpdate.progress', '{{done}} of {{total}} MB', { + done: Math.round((selectedState?.progress ?? 0) * totalMb), + total: totalMb + })} + + +
+ ) + } else if (selectedModel && selectedState?.needsUpdate) { + icon = + title = t('speechModelUpdate.updateTitle', 'Update your speech model') + description = t( + 'speechModelUpdate.updateDescription', + 'Dictation now runs on a faster engine. {{model}} needs a one-time download ({{size}} MB) before you can dictate again.', + names + ) + body = ( +
+ +
+ ) + } else if (!selectedModel || !ready) { + title = t('components.settings.voice.setup.title', 'Set up dictation') + description = t( + 'components.settings.voice.setup.pickModel', + 'Talk instead of typing in any pane. Download a speech model below to start; models on this computer keep your audio here.' + ) + } else if (voiceSettings.enabled && microphone.kind === 'missing') { + tone = 'warn' + icon = + title = t( + 'components.settings.voice.setup.microphoneMissingTitle', + '{{microphone}} isn’t connected', + { + microphone: + voiceSettings.microphoneDeviceLabel || + t('components.settings.voice.setup.savedMicrophone', 'Your microphone') + } + ) + description = t( + 'components.settings.voice.setup.microphoneMissingDescription', + 'Dictation uses the system default microphone until it’s back.' + ) + body = ( +
+ +
+ ) + } else if (voiceSettings.enabled) { + tone = 'ok' + icon = + title = t('components.settings.voice.setup.onTitle', 'Dictation is on') + description = + selectedModel.provider === 'local' + ? t( + 'components.settings.voice.setup.onLocal', + 'Press {{shortcut}} in any pane. {{model}} runs on this computer.', + names + ) + : t( + 'components.settings.voice.setup.onCloud', + 'Press {{shortcut}} in any pane. {{model}} runs in the cloud.', + names + ) + } else { + title = t('components.settings.voice.setup.offTitle', 'Dictation is off') + description = t( + 'components.settings.voice.setup.offDescription', + 'Turn it on to dictate with {{shortcut}} in any pane.', + names + ) + } + + return ( +
+
+ {icon} +
+
+
+
{title}
+

{description}

+
+ {body} +
+ +
+ ) +} diff --git a/src/renderer/src/components/settings/voice/VoiceSpeechModelQuality.tsx b/src/renderer/src/components/settings/voice/VoiceSpeechModelQuality.tsx new file mode 100644 index 00000000000..48ea0d150eb --- /dev/null +++ b/src/renderer/src/components/settings/voice/VoiceSpeechModelQuality.tsx @@ -0,0 +1,103 @@ +import type { SpeechModelQuality } from '../../../../../shared/speech-model-quality' +import { useIntlLocale, useTranslate } from '@/i18n/use-translate' +import { uiDateTimeFormat, uiNumberFormat } from '@/i18n/ui-intl-formatters' +import { cn } from '@/lib/utils' +import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip' + +// Intl formatter caches key on option identity, so these must be module constants. +const PERCENT_FORMAT: Intl.NumberFormatOptions = { maximumFractionDigits: 1 } +const DATE_FORMAT: Intl.DateTimeFormatOptions = { dateStyle: 'medium', timeZone: 'UTC' } +const SEGMENTS = [1, 2, 3, 4, 5] as const + +export function VoiceSpeechModelQuality({ + quality +}: { + quality: SpeechModelQuality +}): React.JSX.Element { + const t = useTranslate() + const intlLocale = useIntlLocale() + if (quality.kind === 'unsupported') { + return ( + + {t('components.settings.voice.quality.unsupported', 'Doesn’t support your language')} + + ) + } + if (quality.kind === 'unmeasured') { + return ( + + {t('components.settings.voice.quality.unmeasured', 'Not measured')} + + ) + } + const { tier, errorRate } = quality + const word = [ + t('components.settings.voice.quality.poor', 'Poor'), + t('components.settings.voice.quality.fair', 'Fair'), + t('components.settings.voice.quality.good', 'Good'), + t('components.settings.voice.quality.veryGood', 'Very good'), + t('components.settings.voice.quality.excellent', 'Excellent') + ][tier - 1] + const percent = uiNumberFormat(PERCENT_FORMAT, intlLocale).format(errorRate.percent) + const values = { + percent, + clips: errorRate.clips, + date: uiDateTimeFormat(DATE_FORMAT, intlLocale).format(new Date(errorRate.measuredOn)) + } + const rate = + errorRate.metric === 'cer' + ? t( + 'components.settings.voice.quality.characterErrorRate', + '{{percent}}% character error rate', + values + ) + : t('components.settings.voice.quality.wordErrorRate', '{{percent}}% word error rate', values) + const test = + errorRate.test === 'librispeech' + ? t( + 'components.settings.voice.quality.librispeech', + 'Test: LibriSpeech read speech, {{clips}} clips, measured {{date}}.', + values + ) + : t( + 'components.settings.voice.quality.fleurs', + 'Test: FLEURS read speech, {{clips}} clips, measured {{date}}.', + values + ) + const bench = t( + 'components.settings.voice.quality.bench', + 'Orca’s benchmark on a Mac with Apple silicon.' + ) + + return ( + + + + + + +

{rate}

+

+ {test} {bench} +

+
+
+ ) +} diff --git a/src/renderer/src/components/settings/voice/VoiceSpeechModelRow.tsx b/src/renderer/src/components/settings/voice/VoiceSpeechModelRow.tsx new file mode 100644 index 00000000000..f27575438d7 --- /dev/null +++ b/src/renderer/src/components/settings/voice/VoiceSpeechModelRow.tsx @@ -0,0 +1,191 @@ +import { Check, Download, Loader2, Trash2, X } from 'lucide-react' +import { useTranslate } from '@/i18n/use-translate' +import { cn } from '@/lib/utils' +import { Button } from '../../ui/button' +import { Progress } from '../../ui/progress' +import type { VoiceSpeechModelRowData } from './voice-speech-model-rows' +import { VoiceSpeechModelQuality } from './VoiceSpeechModelQuality' + +export type VoiceSpeechModelRowActions = { + onUse: (modelId: string) => void + onDownload: (modelId: string) => void + onCancelDownload: (modelId: string) => void + onDelete: (modelId: string) => void + onSetUpCloud: (modelId: string) => void +} + +type VoiceSpeechModelRowProps = VoiceSpeechModelRowActions & { + row: VoiceSpeechModelRowData + active: boolean + deletePending: boolean +} + +export function VoiceSpeechModelRow({ + row: { manifest, state, quality }, + active, + deletePending, + onUse, + onDownload, + onCancelDownload, + onDelete, + onSetUpCloud +}: VoiceSpeechModelRowProps): React.JSX.Element { + const t = useTranslate() + const isCloud = manifest.provider !== 'local' + const retired = quality === null + const ready = state?.status === 'ready' + const downloading = state?.status === 'downloading' || state?.status === 'extracting' + const needsUpdate = state?.needsUpdate === true && !downloading + const totalMb = Math.round((manifest.sizeBytes ?? 0) / 1_000_000) + const percent = Math.round((state?.progress ?? 0) * 100) + + const deleteButton = ( + + ) + + let subline: React.ReactNode = null + if (retired) { + subline = t( + 'components.settings.voice.models.retired', + 'No longer offered. Delete it to free up space.' + ) + } else if (state?.status === 'extracting') { + subline = t('auto.components.settings.VoicePane.61a16c8141', 'Extracting...') + } else if (downloading) { + subline = t('speechModelUpdate.progress', '{{done}} of {{total}} MB', { + done: Math.round((state?.progress ?? 0) * totalMb), + total: totalMb + }) + } else if (needsUpdate) { + subline = ( + + {t( + 'auto.components.settings.VoicePane.updateToFasterVersion', + 'Update to the faster version ({{value0}} MB)', + { value0: totalMb } + )} + + ) + } else if (manifest.provider === 'openai') { + subline = t('components.settings.voice.models.sendsToOpenAi', 'Sends your audio to OpenAI.') + } else if (manifest.provider === 'openai-compatible') { + subline = t( + 'components.settings.voice.models.sendsToCompatible', + 'Sends your audio to the service you set up.' + ) + } + + let action: React.ReactNode + if (retired) { + action = ( + + ) + } else if (downloading) { + action = ( + <> + + + {percent}% + + + + ) + } else if (needsUpdate) { + action = ( + <> + + {deleteButton} + + ) + } else if (ready) { + action = ( + <> + {active ? ( + + + {t('components.settings.voice.models.inUse', 'In use')} + + ) : ( + + )} + {isCloud ? null : deleteButton} + + ) + } else if (isCloud) { + action = ( + + ) + } else { + action = ( + + ) + } + + return ( +
+
+
{manifest.label}
+ {subline ?
{subline}
: null} +
+
+ {quality ? : null} +
+
+ {isCloud + ? t('components.settings.voice.models.cloud', 'Cloud') + : retired + ? null + : t('auto.components.settings.VoicePane.91980ce124', '{{value0}} MB', { + value0: totalMb + })} +
+
{action}
+
+ ) +} diff --git a/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.fixture.tsx b/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.fixture.tsx index 6ebdd0c6c0b..42c20fd70fc 100644 --- a/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.fixture.tsx +++ b/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.fixture.tsx @@ -1,60 +1,112 @@ import { defineFixture } from '../../../component-fixtures/component-fixture' import { getDefaultVoiceSettings } from '../../../../../shared/constants' -import type { SpeechModelManifest } from '../../../../../shared/speech-types' +import type { + SpeechModelErrorRate, + SpeechModelManifest, + SpeechModelState +} from '../../../../../shared/speech-types' import { VoiceSpeechModelSection } from './VoiceSpeechModelSection' -function model(id: string, label: string, sizeBytes: number, description: string) { +const rates = (entries: Record): SpeechModelErrorRate[] => + Object.entries(entries).map(([language, percent]) => ({ + language, + percent, + metric: ['ja', 'ko', 'zh'].includes(language) ? 'cer' : 'wer', + test: language === 'en' ? 'librispeech' : 'fleurs', + clips: language === 'en' ? 73 : 5, + measuredOn: '2026-11-09' + })) + +function local( + id: string, + label: string, + sizeBytes: number, + languages: string[], + errorRates: SpeechModelErrorRate[], + replacedBy?: string +): SpeechModelManifest { return { id, label, - description, type: 'transducer', provider: 'local', - language: 'en', - languages: ['en'], + language: 'multilingual', + languages, + errorRates, sizeBytes, sampleRate: 16000, - streaming: false - } satisfies SpeechModelManifest + streaming: false, + replacedBy + } } -const catalog = [ - model('v3', 'Parakeet TDT v3', 739_508_576, 'Highest accuracy for 25 European languages.'), - model( - 'v2', - 'Parakeet TDT v2', - 729_574_912, - 'English only. Faster than v3 with similar accuracy.' - ), - model( - 'nemotron', - 'Nemotron 3.5', - 751_094_240, - '28 languages including Korean, Chinese and Japanese.' - ) +const catalog: SpeechModelManifest[] = [ + local('v3', 'Parakeet TDT v3', 739_508_576, ['en', 'de'], rates({ en: 3.7, de: 2.2 })), + local('v2', 'Parakeet TDT v2', 729_574_912, ['en'], rates({ en: 2.8 })), + local('zko', 'Zipformer Streaming KO', 0, ['ko'], [], 'sense'), + local('turbo', 'Whisper Large v3 Turbo', 886_381_760, ['en', 'ko'], rates({ en: 4.5, ko: 2.6 })), + local('sense', 'SenseVoice', 252_684_608, ['en', 'ko'], rates({ en: 6.9, ko: 10 })), + local('nemotron', 'Nemotron 3.5', 751_094_240, ['en', 'ko'], rates({ en: 5.3, ko: 11.8 })), + local('tiny', 'Whisper Tiny', 45_981_088, ['en', 'ko'], rates({ en: 13, ko: 24.9 })), + { + id: 'gpt', + label: 'GPT Transcribe', + type: 'openai', + provider: 'openai', + language: 'multilingual', + languages: ['en', 'ko'], + sampleRate: 16000, + streaming: false + } ] -export const updateAvailable = defineFixture({ - width: 640, - render: () => ( +function render( + sttModel: string, + modelStates: SpeechModelState[], + ratingLanguage: string +): React.JSX.Element { + return ( {}} onOpenCloudSetup={() => {}} onRefreshModelStates={() => {}} + cloudSettings={null} /> - ), - interact: (frame) => { - frame - .querySelector('button') - ?.dispatchEvent( - new PointerEvent('pointerdown', { bubbles: true, button: 0, pointerType: 'mouse' }) - ) - } + ) +} + +export const english = defineFixture({ + width: 760, + render: () => + render( + 'v3', + [ + { id: 'v3', status: 'ready' }, + { id: 'tiny', status: 'ready' }, + { id: 'sense', status: 'downloading', progress: 0.42 } + ], + 'en' + ) +}) + +export const korean = defineFixture({ + width: 760, + render: () => render('', [], 'ko') +}) + +export const updateAndRetired = defineFixture({ + width: 760, + render: () => + render( + 'v2', + [ + { id: 'v2', status: 'not-downloaded', needsUpdate: true }, + { id: 'zko', status: 'ready' } + ], + 'en' + ) }) diff --git a/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.test.tsx b/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.test.tsx index 6a5a0433801..b019dc293be 100644 --- a/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.test.tsx +++ b/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.test.tsx @@ -2,14 +2,12 @@ // @vitest-environment happy-dom import { act } from 'react' -import type { ReactNode } from 'react' import { createRoot, type Root } from 'react-dom/client' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { SpeechModelManifest, SpeechModelState } from '../../../../../shared/speech-types' import { getDefaultVoiceSettings } from '../../../../../shared/constants' const toastErrorMock = vi.hoisted(() => vi.fn()) -const menuDismissMock = vi.hoisted(() => vi.fn()) vi.mock('sonner', () => ({ toast: { @@ -18,50 +16,16 @@ vi.mock('sonner', () => ({ })) vi.mock('@/i18n/i18n', () => ({ + getIntlLocale: () => 'en', translate: (_key: string, fallback: string, values?: Record) => values ? fallback.replace('{{value0}}', values.value0) : fallback })) -vi.mock('../../ui/dropdown-menu', () => ({ - DropdownMenu: ({ children }: { children: ReactNode }) =>
{children}
, - DropdownMenuTrigger: ({ children }: { children: ReactNode }) => <>{children}, - DropdownMenuContent: ({ children }: { children: ReactNode }) =>
{children}
, - DropdownMenuItem: ({ - children, - disabled, - onSelect, - className - }: { - children: ReactNode - disabled?: boolean - onSelect?: (event: Event) => void - className?: string - }) => ( -
{ - if (!disabled) { - const selectEvent = new Event('select', { cancelable: true }) - onSelect?.(selectEvent) - if (!selectEvent.defaultPrevented) { - menuDismissMock() - } - } - }} - > - {children} -
- ) -})) - import { VoiceSpeechModelSection } from './VoiceSpeechModelSection' const localModel: SpeechModelManifest = { id: 'model-a', label: 'Local Model', - description: 'Runs offline', provider: 'local', language: 'en', languages: ['en'], @@ -107,8 +71,10 @@ function renderSection(args: { catalog={catalog} modelStates={modelStates} onUpdateVoiceSettings={vi.fn()} + ratingLanguage="en" onOpenCloudSetup={vi.fn()} onRefreshModelStates={args.refreshModelStates ?? vi.fn()} + cloudSettings={null} /> ) }) @@ -119,7 +85,6 @@ function renderSection(args: { describe('VoiceSpeechModelSection', () => { beforeEach(() => { toastErrorMock.mockReset() - menuDismissMock.mockReset() }) afterEach(() => { diff --git a/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.tsx b/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.tsx index aabb070722e..2d6d9b461c8 100644 --- a/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.tsx +++ b/src/renderer/src/components/settings/voice/VoiceSpeechModelSection.tsx @@ -1,247 +1,138 @@ import { useState } from 'react' import { toast } from 'sonner' import type { - VoiceSettings, SpeechModelManifest, - SpeechModelState + SpeechModelState, + VoiceSettings } from '../../../../../shared/speech-types' -import { Button } from '../../ui/button' -import { Label } from '../../ui/label' +import { useTranslate, useIntlLocale } from '@/i18n/use-translate' +import { languageDisplayName } from '@/i18n/ui-intl-formatters' import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuItem, - DropdownMenuTrigger -} from '../../ui/dropdown-menu' -import { Cloud, Download, Trash2, Loader2, ChevronDown, Check } from 'lucide-react' -import { useTranslate } from '@/i18n/use-translate' -import { speechModelDescription } from './speech-model-description' -import { isOfferedSpeechModel } from '../../../../../shared/speech-model-retirement' - -function describeSpeechModelDownloadError(error: unknown): string { - const message = error instanceof Error ? error.message : String(error) - // Strip Electron’s invoke wrapper so download errors show the underlying cause. - return message.replace(/^Error invoking remote method '[^']+': (?:Error: )?/, '') -} + SettingsSegmentedControl, + SettingsSubsectionHeader +} from '../form-controls/SettingsFormControls' +import { + speechModelTab, + voiceSpeechModelRows, + type VoiceSpeechModelTab +} from './voice-speech-model-rows' +import { VoiceSpeechModelRow } from './VoiceSpeechModelRow' +import { cancelSpeechModelDownload, startSpeechModelDownload } from './speech-model-download' type VoiceSpeechModelSectionProps = { voiceSettings: VoiceSettings catalog: SpeechModelManifest[] modelStates: SpeechModelState[] + /** The language quality is rated for. */ + ratingLanguage: string onUpdateVoiceSettings: (updates: Partial) => void - /** A cloud model that isn't set up yet: open its provider's setup. */ - onOpenCloudSetup: (manifest: SpeechModelManifest) => void + onOpenCloudSetup: (modelId: string) => void onRefreshModelStates: () => void + /** Provider rows (API keys, base URL) shown under the cloud models. */ + cloudSettings: React.ReactNode } export function VoiceSpeechModelSection({ voiceSettings, catalog, modelStates, + ratingLanguage, onUpdateVoiceSettings, onOpenCloudSetup, - onRefreshModelStates + onRefreshModelStates, + cloudSettings }: VoiceSpeechModelSectionProps): React.JSX.Element { const t = useTranslate() - const [pendingDeleteModelIds, setPendingDeleteModelIds] = useState>(() => new Set()) - const getModelState = (id: string): SpeechModelState | undefined => - modelStates.find((s) => s.id === id) - + const intlLocale = useIntlLocale() const selectedModel = catalog.find((m) => m.id === voiceSettings.sttModel) - const selectedModelState = voiceSettings.sttModel - ? getModelState(voiceSettings.sttModel) - : undefined - const selectedIsReady = selectedModelState?.status === 'ready' + const [chosenTab, setTab] = useState(null) + const tab = chosenTab ?? (selectedModel ? speechModelTab(selectedModel) : 'local') + const [pendingDeleteModelIds, setPendingDeleteModelIds] = useState>( + () => new Set() + ) + const rows = voiceSpeechModelRows(catalog, modelStates, tab, ratingLanguage) + + const setDeletePending = (modelId: string, pending: boolean): void => + setPendingDeleteModelIds((prev) => { + const next = new Set(prev) + if (pending) { + next.add(modelId) + } else { + next.delete(modelId) + } + return next + }) + + const deleteModel = (modelId: string): void => { + if (pendingDeleteModelIds.has(modelId)) { + return + } + setDeletePending(modelId, true) + void window.api.speech + .deleteModel(modelId) + .then(onRefreshModelStates) + .catch(() => + toast.error(t('auto.components.settings.VoicePane.68de13f72c', 'Failed to delete model.')) + ) + .finally(() => setDeletePending(modelId, false)) + } return ( -
-
- -

- {selectedModel && selectedIsReady - ? `${selectedModel.label} — ${speechModelDescription(selectedModel)}` - : t( - 'auto.components.settings.VoicePane.e24f7d43d2', - 'Select a speech model. Local models run offline; cloud models require an API key.' - )} -

-
- - - - - - {catalog.map((manifest) => { - const mState = getModelState(manifest.id) - const isReady = mState?.status === 'ready' - const isCloud = manifest.provider !== 'local' - const isRetired = !isOfferedSpeechModel(manifest) - // Retired local models stay listed only while downloaded, so their files can be deleted. - if (isRetired && (isCloud || !isReady)) { - return null - } - const isDownloading = - mState?.status === 'downloading' || mState?.status === 'extracting' - const isActive = voiceSettings.sttModel === manifest.id - // Only the old sherpa-onnx files are on disk; downloading replaces them. - const needsUpdate = mState?.needsUpdate === true && !isDownloading - const deletePending = pendingDeleteModelIds.has(manifest.id) - const sizeMb = manifest.sizeBytes ? Math.round(manifest.sizeBytes / 1_000_000) : null - - return ( - { - if (isRetired) { - event.preventDefault() - } else if (isReady) { - onUpdateVoiceSettings({ sttModel: manifest.id }) - } else if (isCloud) { - onOpenCloudSetup(manifest) - } else if (!isDownloading) { - // Keep the menu open so download progress remains visible. - event.preventDefault() - void window.api.speech.downloadModel(manifest.id).catch((error: unknown) => - toast.error( - t( - 'auto.components.settings.VoicePane.cfde55c7b0', - 'Failed to download model.' - ), - // Include the raw cause because it is the diagnosable failure users can report. - { description: describeSpeechModelDownloadError(error) } - ) - ) - } - }} - className={`group flex items-center gap-2.5 py-2.5 ${ - !isCloud && !isReady && !isDownloading ? 'opacity-50' : '' - }`} - > - - {isActive && isReady ? ( - - ) : isDownloading ? ( - - ) : isCloud ? ( - - ) : null} - -
-
- {manifest.label} - {!isCloud && ( - - {manifest.streaming - ? t('auto.components.settings.VoicePane.d504ab05f0', 'streaming') - : t('auto.components.settings.VoicePane.8f4d2a51d7', 'offline')} - - )} - {manifest.recommended && ( - - {t('auto.components.settings.VoicePane.1ba81c0ff0', 'recommended')} - - )} - - {isDownloading && mState?.progress !== undefined - ? mState.status === 'extracting' - ? t('auto.components.settings.VoicePane.61a16c8141', 'Extracting...') - : `${Math.round(mState.progress * 100)}%` - : isCloud - ? null - : t('auto.components.settings.VoicePane.91980ce124', '{{value0}} MB', { - value0: sizeMb - })} - -
-

- {speechModelDescription(manifest)} -

- {needsUpdate && ( -

- {t( - 'auto.components.settings.VoicePane.updateToFasterVersion', - 'Update to the faster version ({{value0}} MB)', - { value0: sizeMb } - )} -

- )} -
- {!isCloud && (isReady || needsUpdate) ? ( - - ) : !isCloud && !isReady && !isDownloading ? ( - - - - ) : null} -
- ) - })} -
-
+ value={tab} + onChange={setTab} + ariaLabel={t('components.settings.voice.models.where', 'Where models run')} + options={[ + { + value: 'local', + label: t('components.settings.voice.models.local', 'On this computer') + }, + { value: 'cloud', label: t('components.settings.voice.models.cloud', 'Cloud') } + ]} + /> + } + /> + {tab === 'cloud' ? ( +

+ {t( + 'components.settings.voice.models.cloudDescription', + 'Cloud models send your audio to the provider. Models on this computer keep it here.' + )} +

+ ) : null} +
+ {rows.map((row) => ( +
+ onUpdateVoiceSettings({ sttModel: modelId })} + onDownload={(modelId) => startSpeechModelDownload(modelId, onRefreshModelStates)} + onCancelDownload={(modelId) => + cancelSpeechModelDownload(modelId, onRefreshModelStates) + } + onDelete={deleteModel} + onSetUpCloud={onOpenCloudSetup} + /> +
+ ))} +
+ {tab === 'cloud' ? cloudSettings : null}
) } diff --git a/src/renderer/src/components/settings/voice/speech-model-description.ts b/src/renderer/src/components/settings/voice/speech-model-description.ts deleted file mode 100644 index 1f1f0abffb4..00000000000 --- a/src/renderer/src/components/settings/voice/speech-model-description.ts +++ /dev/null @@ -1,69 +0,0 @@ -import type { SpeechModelManifest } from '../../../../../shared/speech-types' -import { translate } from '@/i18n/i18n' - -// Models transcribe-cpp can't run; their users moved to SenseVoice or Parakeet. -const retiredForEngine = (): string => - translate( - 'settings.voice.modelDescriptions.retiredForEngine', - 'No longer offered: the speech engine can no longer run it. Delete it to free up space.' - ) - -const DESCRIPTIONS: Record string> = { - 'parakeet-tdt-0.6b-v3-int8': () => - translate( - 'settings.voice.modelDescriptions.parakeetV3', - 'Highest accuracy for 25 European languages. Punctuation, capitalization, and word-level timestamps.' - ), - 'parakeet-tdt-0.6b-v2-int8': () => - translate( - 'settings.voice.modelDescriptions.parakeetV2', - 'English only. Faster than v3 with similar accuracy. Punctuation and capitalization.' - ), - 'zipformer-bilingual-zh-en': retiredForEngine, - 'paraformer-bilingual-zh-en': retiredForEngine, - 'zipformer-streaming-en-20m': () => - translate( - 'settings.voice.modelDescriptions.zipformerStreamingEnRetired', - 'No longer offered: it drops the start of sentences. Delete it to free up space.' - ), - 'zipformer-streaming-zh-14m': retiredForEngine, - 'zipformer-streaming-korean': retiredForEngine, - 'parakeet-tdt-ctc-0.6b-ja-int8': retiredForEngine, - 'whisper-tiny': () => - translate( - 'settings.voice.modelDescriptions.whisperTiny', - '90+ languages. Lower accuracy than Parakeet but broadest language coverage.' - ), - 'whisper-large-v3-turbo': () => - translate( - 'settings.voice.modelDescriptions.whisperLargeV3Turbo', - 'Best accuracy for Japanese, Korean, and Chinese, and 90+ languages. Slower than Parakeet.' - ), - 'sense-voice-zh-en-ja-ko-yue': () => - translate( - 'settings.voice.modelDescriptions.senseVoice', - 'Chinese, English, Japanese, Korean, and Cantonese with automatic language detection.' - ), - 'openai-compatible': () => - translate( - 'settings.voice.modelDescriptions.openAiCompatible', - 'Any service with an OpenAI-style transcription API, such as Groq, Mistral or a local server.' - ), - 'nemotron-3.5-asr-streaming-0.6b': () => - translate( - 'settings.voice.modelDescriptions.nemotron', - 'English, Spanish, German and many more languages, with automatic language detection.' - ), - 'openai-gpt-transcribe': () => - translate( - 'settings.voice.modelDescriptions.gptTranscribe', - 'Cloud transcription with high accuracy. Requires an OpenAI API key.' - ) -} - -/** Main's catalog arrives in English over IPC; unknown ids from a newer main keep it. */ -export function speechModelDescription(manifest: SpeechModelManifest): string { - return Object.hasOwn(DESCRIPTIONS, manifest.id) - ? DESCRIPTIONS[manifest.id]() - : manifest.description -} diff --git a/src/renderer/src/components/settings/voice/speech-model-download.ts b/src/renderer/src/components/settings/voice/speech-model-download.ts new file mode 100644 index 00000000000..db4b882650e --- /dev/null +++ b/src/renderer/src/components/settings/voice/speech-model-download.ts @@ -0,0 +1,26 @@ +import { toast } from 'sonner' +import { translate } from '@/i18n/i18n' + +function describeSpeechModelDownloadError(error: unknown): string { + const message = error instanceof Error ? error.message : String(error) + // Strip Electron’s invoke wrapper so download errors show the underlying cause. + return message.replace(/^Error invoking remote method '[^']+': (?:Error: )?/, '') +} + +/** Starts a model download; a failure becomes a toast, and model states refresh either way. */ +export function startSpeechModelDownload(modelId: string, refreshModelStates: () => void): void { + void window.api.speech + .downloadModel(modelId) + .catch((error: unknown) => + toast.error( + translate('auto.components.settings.VoicePane.cfde55c7b0', 'Failed to download model.'), + // Include the raw cause because it is the diagnosable failure users can report. + { description: describeSpeechModelDownloadError(error) } + ) + ) + .finally(refreshModelStates) +} + +export function cancelSpeechModelDownload(modelId: string, refreshModelStates: () => void): void { + void window.api.speech.cancelDownload(modelId).finally(refreshModelStates) +} diff --git a/src/renderer/src/components/settings/voice/use-voice-microphone-devices.ts b/src/renderer/src/components/settings/voice/use-voice-microphone-devices.ts new file mode 100644 index 00000000000..de2f0bf55af --- /dev/null +++ b/src/renderer/src/components/settings/voice/use-voice-microphone-devices.ts @@ -0,0 +1,67 @@ +import { useCallback, useEffect, useRef, useState } from 'react' +import { + listVoiceMicrophoneDevices, + type VoiceMicrophoneDevice +} from '@/components/dictation/microphone-devices' +import { useMountedRef } from '@/hooks/useMountedRef' + +function sameDeviceList( + a: readonly VoiceMicrophoneDevice[], + b: readonly VoiceMicrophoneDevice[] +): boolean { + return ( + a.length === b.length && + a.every((device, index) => { + const other = b[index] + return device.deviceId === other?.deviceId && device.label === other.label + }) + ) +} + +/** The live microphone list, rescanned on devicechange and whenever `rescanKey` changes. */ +export function useVoiceMicrophoneDevices(rescanKey: unknown): { + devices: VoiceMicrophoneDevice[] + devicesKnown: boolean + refreshDevices: () => Promise +} { + const [devices, setDevices] = useState([]) + const [devicesKnown, setDevicesKnown] = useState(false) + const mountedRef = useMountedRef() + // Bluetooth emits repeated devicechange events; reject stale enumeration results. + const refreshGenerationRef = useRef(0) + + const refreshDevices = useCallback(async (): Promise => { + const generation = refreshGenerationRef.current + 1 + refreshGenerationRef.current = generation + if (typeof navigator === 'undefined' || !navigator.mediaDevices?.enumerateDevices) { + return + } + let next: VoiceMicrophoneDevice[] = [] + try { + next = listVoiceMicrophoneDevices(await navigator.mediaDevices.enumerateDevices()) + } catch { + next = [] + } + if (!mountedRef.current || refreshGenerationRef.current !== generation) { + return + } + setDevicesKnown(next.length > 0) + setDevices((current) => (sameDeviceList(current, next) ? current : next)) + }, [mountedRef]) + + useEffect(() => { + void refreshDevices() + if (typeof navigator === 'undefined' || !navigator.mediaDevices?.addEventListener) { + return + } + const handleDeviceChange = (): void => { + void refreshDevices() + } + navigator.mediaDevices.addEventListener('devicechange', handleDeviceChange) + return () => { + navigator.mediaDevices.removeEventListener('devicechange', handleDeviceChange) + } + }, [refreshDevices, rescanKey]) + + return { devices, devicesKnown, refreshDevices } +} diff --git a/src/renderer/src/components/settings/voice/voice-speech-model-rows.test.ts b/src/renderer/src/components/settings/voice/voice-speech-model-rows.test.ts new file mode 100644 index 00000000000..0521ff111ed --- /dev/null +++ b/src/renderer/src/components/settings/voice/voice-speech-model-rows.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from 'vitest' +import type { SpeechModelManifest } from '../../../../../shared/speech-types' +import { voiceSpeechModelRows } from './voice-speech-model-rows' + +const model = ( + id: string, + languages: string[], + extra: Partial = {} +): SpeechModelManifest => ({ + id, + label: id, + type: 'transducer', + provider: 'local', + language: 'multilingual', + languages, + sampleRate: 16000, + streaming: false, + ...extra +}) + +describe('voiceSpeechModelRows', () => { + it('puts models without the language last and lists retired models only while on disk', () => { + const catalog = [ + model('english-only', ['en']), + model('retired-on-disk', ['ko'], { replacedBy: 'korean' }), + model('korean', ['en', 'ko']), + model('retired-gone', ['ko'], { replacedBy: 'korean' }), + model('cloud', ['ko'], { provider: 'openai' }) + ] + const rows = voiceSpeechModelRows( + catalog, + [{ id: 'retired-on-disk', status: 'ready' }], + 'local', + 'ko' + ) + expect(rows.map((row) => row.manifest.id)).toEqual([ + 'korean', + 'english-only', + 'retired-on-disk' + ]) + }) +}) diff --git a/src/renderer/src/components/settings/voice/voice-speech-model-rows.ts b/src/renderer/src/components/settings/voice/voice-speech-model-rows.ts new file mode 100644 index 00000000000..5e58965d70f --- /dev/null +++ b/src/renderer/src/components/settings/voice/voice-speech-model-rows.ts @@ -0,0 +1,47 @@ +import type { SpeechModelManifest, SpeechModelState } from '../../../../../shared/speech-types' +import { isOfferedSpeechModel } from '../../../../../shared/speech-model-retirement' +import { + speechModelQuality, + type SpeechModelQuality +} from '../../../../../shared/speech-model-quality' + +export type VoiceSpeechModelTab = 'local' | 'cloud' + +export type VoiceSpeechModelRowData = { + manifest: SpeechModelManifest + state: SpeechModelState | undefined + /** null for a retired model, which is only listed so its files can be deleted. */ + quality: SpeechModelQuality | null +} + +export function speechModelTab(manifest: SpeechModelManifest): VoiceSpeechModelTab { + return manifest.provider === 'local' ? 'local' : 'cloud' +} + +/** A tab's rows: models for the language first, then ones that can't handle it, then retired downloads. */ +export function voiceSpeechModelRows( + catalog: readonly SpeechModelManifest[], + modelStates: readonly SpeechModelState[], + tab: VoiceSpeechModelTab, + language: string +): VoiceSpeechModelRowData[] { + const supported: VoiceSpeechModelRowData[] = [] + const unsupported: VoiceSpeechModelRowData[] = [] + const retired: VoiceSpeechModelRowData[] = [] + for (const manifest of catalog) { + if (speechModelTab(manifest) !== tab) { + continue + } + const state = modelStates.find((s) => s.id === manifest.id) + if (!isOfferedSpeechModel(manifest)) { + // Retired models stay listed only while their files are on disk. + if (manifest.provider === 'local' && (state?.status === 'ready' || state?.needsUpdate)) { + retired.push({ manifest, state, quality: null }) + } + continue + } + const quality = speechModelQuality(manifest, language) + ;(quality.kind === 'unsupported' ? unsupported : supported).push({ manifest, state, quality }) + } + return [...supported, ...unsupported, ...retired] +} diff --git a/src/renderer/src/i18n/locales/de.json b/src/renderer/src/i18n/locales/de.json index b7d13c0a29d..454f252e06d 100644 --- a/src/renderer/src/i18n/locales/de.json +++ b/src/renderer/src/i18n/locales/de.json @@ -972,19 +972,6 @@ "missingHost": "Die Proxy-URL muss einen Host enthalten." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "Höchste Genauigkeit für 25 europäische Sprachen. Zeichensetzung, Groß- und Kleinschreibung und Zeitstempel auf Wortebene.", - "parakeetV2": "Nur Englisch. Schneller als v3 bei ähnlicher Genauigkeit. Zeichensetzung und Groß- und Kleinschreibung.", - "zipformerStreamingEnRetired": "Wird nicht mehr angeboten: Es verschluckt den Anfang von Sätzen. Löschen Sie es, um Speicherplatz freizugeben.", - "whisperTiny": "Über 90 Sprachen. Geringere Genauigkeit als Parakeet, aber die größte Sprachabdeckung.", - "senseVoice": "Chinesisch, Englisch, Japanisch, Koreanisch und Kantonesisch mit automatischer Erkennung der gesprochenen Sprache.", - "gptTranscribe": "Cloud-Transkription mit hoher Genauigkeit. Benötigt einen OpenAI-API-Schlüssel.", - "whisperLargeV3Turbo": "Beste Genauigkeit für Japanisch, Koreanisch und Chinesisch sowie über 90 Sprachen. Langsamer als Parakeet.", - "retiredForEngine": "Nicht mehr verfügbar: Die Spracherkennung kann dieses Modell nicht mehr ausführen. Löschen Sie es, um Speicherplatz freizugeben.", - "nemotron": "Englisch, Spanisch, Deutsch und viele weitere Sprachen, mit automatischer Spracherkennung." - } - }, "diagnostics": { "errors": { "creatingDisabled": "das Erstellen von Dateien zur Prüfung ist deaktiviert", @@ -8946,7 +8933,6 @@ "7807c11c4d": "Allgemein", "6855b0f77d": "Die grundlegenden Abläufe abschließen, die Orca für die parallele Arbeit mit Agenten nützlich machen.", "6d119427ef": "Checkliste für den Einstieg", - "eb1176a14e": "Lokales Sprachdiktat mit Modellen auf dem Gerät.", "5063bb47a5": "Spracheingabe", "7118953f14": "Agenten die Steuerung beliebiger Apps auf dem Computer ermöglichen.", "c9841721cb": "Computersteuerung", @@ -9461,7 +9447,6 @@ "VoicePane": { "6fa734ed95": "{{value0}} löschen", "68de13f72c": "Modell konnte nicht gelöscht werden.", - "1ba81c0ff0": "empfohlen", "cfde55c7b0": "Modell konnte nicht heruntergeladen werden.", "43fd4f454b": "Spracherkennungsmodell", "ba4a900d1d": "Diktatmodus", @@ -9472,10 +9457,6 @@ "506df81ba6": "OpenAI-API-Schlüssel gespeichert", "91980ce124": "{{value0}} MB", "61a16c8141": "Wird entpackt…", - "8f4d2a51d7": "offline", - "d504ab05f0": "Streaming", - "fbe5990716": "Modell auswählen", - "e24f7d43d2": "Ein Spracherkennungsmodell auswählen. Lokale Modelle funktionieren offline; Cloud-Modelle benötigen einen API-Schlüssel.", "174da92062": "Gedrückt halten", "118b3c2dee": "Umschalten", "ad5d036ecc": "Mikrofonberechtigung konnte nicht angefordert werden. Das Diktat wurde nicht aktiviert.", @@ -9483,7 +9464,6 @@ "1eac933202": "Die macOS-Einstellungen „Privacy & Security“ wurden geöffnet. Nach der Zugriffsfreigabe das Diktat erneut aktivieren.", "cd9fe37556": "Mikrofonberechtigung erteilt", "openAiKeyName": "OpenAI-Transkriptionsschlüssel", - "dictationShortcutDescription": "Mit {{shortcut}} Text in den jeweils fokussierten Bereich diktieren.", "dictationModeAuto": "Automatisch", "dictationModesWithAutoDescription": "Automatisch: Halten Sie {{shortcut}} gedrückt, um bis zum Loslassen zu diktieren, oder tippen Sie kurz zum Starten und Stoppen. Gedrückt halten: Diktieren, solange {{shortcut}} gedrückt bleibt. Umschalten: {{shortcut}} einmal zum Starten und erneut zum Stoppen drücken.", "speechGpuLabel": "GPU verwenden", @@ -20705,6 +20685,67 @@ "fillerWords": { "title": "Füllwörter entfernen", "description": "Entfernt Laute wie „äh“ und „ähm“ aus diktiertem Text, passend zur Diktiersprache." + }, + "sectionDescription": "Sprechen statt tippen, in jedem Bereich. Die Sprache wird auf diesem Computer erkannt, außer du wählst ein Cloud-Modell.", + "input": { + "title": "Eingabe", + "shortcut": "Tastenkürzel", + "shortcutDescription": "Startet das Diktieren im fokussierten Bereich.", + "changeShortcut": "Ändern" + }, + "language": { + "title": "Sprache" + }, + "cleanup": { + "title": "Bereinigung" + }, + "advanced": { + "title": "Erweitert" + }, + "setup": { + "downloadingDescription": "Du kannst diese Seite verlassen. Diktieren funktioniert, sobald der Download fertig ist.", + "title": "Diktieren einrichten", + "pickModel": "Sprechen statt tippen, in jedem Bereich. Lade unten ein Sprachmodell herunter; Modelle auf diesem Computer behalten deine Audiodaten hier.", + "microphoneMissingTitle": "{{microphone}} ist nicht verbunden", + "microphoneMissingDescription": "Diktieren nutzt das Standardmikrofon des Systems, bis es wieder da ist.", + "changeMicrophone": "Mikrofon ändern", + "onTitle": "Diktieren ist an", + "onLocal": "Drücke {{shortcut}} in einem beliebigen Bereich. {{model}} läuft auf diesem Computer.", + "onCloud": "Drücke {{shortcut}} in einem beliebigen Bereich. {{model}} läuft in der Cloud.", + "offTitle": "Diktieren ist aus", + "offDescription": "Schalte es ein, um mit {{shortcut}} in jedem Bereich zu diktieren.", + "savedMicrophone": "Dein Mikrofon" + }, + "quality": { + "unsupported": "Unterstützt deine Sprache nicht", + "unmeasured": "Nicht gemessen", + "poor": "Schwach", + "fair": "Mäßig", + "good": "Gut", + "veryGood": "Sehr gut", + "excellent": "Hervorragend", + "characterErrorRate": "{{percent}} % Zeichenfehlerrate", + "wordErrorRate": "{{percent}} % Wortfehlerrate", + "librispeech": "Test: LibriSpeech (vorgelesene Sprache), {{clips}} Clips, gemessen am {{date}}.", + "fleurs": "Test: FLEURS (vorgelesene Sprache), {{clips}} Clips, gemessen am {{date}}.", + "bench": "Orcas Benchmark auf einem Mac mit Apple Silicon.", + "label": "Qualität: {{word}}" + }, + "models": { + "retired": "Wird nicht mehr angeboten. Lösche es, um Speicherplatz freizugeben.", + "sendsToOpenAi": "Sendet deine Audiodaten an OpenAI.", + "delete": "Löschen", + "cancelDownload": "Download abbrechen", + "inUse": "In Verwendung", + "use": "Verwenden", + "setUpCloud": "Einrichten", + "download": "Herunterladen", + "cloud": "Cloud", + "description": "Die Qualität ist für {{language}} bewertet. Fahre über eine Bewertung, um den Test dahinter zu sehen.", + "where": "Wo Modelle laufen", + "local": "Auf diesem Computer", + "cloudDescription": "Cloud-Modelle senden deine Audiodaten an den Anbieter. Modelle auf diesem Computer behalten sie hier.", + "sendsToCompatible": "Sendet deine Audiodaten an den Dienst, den du eingerichtet hast." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index cbda726753e..fd3e66b2e85 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -1144,20 +1144,6 @@ "missingHost": "Proxy URL must include a host." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "Highest accuracy for 25 European languages. Punctuation, capitalization, and word-level timestamps.", - "parakeetV2": "English only. Faster than v3 with similar accuracy. Punctuation and capitalization.", - "zipformerStreamingEnRetired": "No longer offered: it drops the start of sentences. Delete it to free up space.", - "whisperTiny": "90+ languages. Lower accuracy than Parakeet but broadest language coverage.", - "senseVoice": "Chinese, English, Japanese, Korean, and Cantonese with automatic language detection.", - "gptTranscribe": "Cloud transcription with high accuracy. Requires an OpenAI API key.", - "openAiCompatible": "Any service with an OpenAI-style transcription API, such as Groq, Mistral or a local server.", - "whisperLargeV3Turbo": "Best accuracy for Japanese, Korean, and Chinese, and 90+ languages. Slower than Parakeet.", - "retiredForEngine": "No longer offered: the speech engine can no longer run it. Delete it to free up space.", - "nemotron": "English, Spanish, German and many more languages, with automatic language detection." - } - }, "diagnostics": { "errors": { "creatingDisabled": "creating review files is disabled", @@ -9264,7 +9250,6 @@ "7807c11c4d": "General", "6855b0f77d": "Finish the core workflows that make Orca useful for parallel agent work.", "6d119427ef": "Onboarding checklist", - "eb1176a14e": "Local speech-to-text dictation with on-device models.", "5063bb47a5": "Voice", "7118953f14": "Enable agents to control any app on your computer.", "c9841721cb": "Computer Use", @@ -9783,7 +9768,6 @@ "VoicePane": { "6fa734ed95": "Delete {{value0}}", "68de13f72c": "Failed to delete model.", - "1ba81c0ff0": "recommended", "cfde55c7b0": "Failed to download model.", "43fd4f454b": "Speech Model", "ba4a900d1d": "Dictation Mode", @@ -9794,10 +9778,6 @@ "506df81ba6": "OpenAI API key saved", "91980ce124": "{{value0}} MB", "61a16c8141": "Extracting...", - "8f4d2a51d7": "offline", - "d504ab05f0": "streaming", - "fbe5990716": "Select Model", - "e24f7d43d2": "Select a speech model. Local models run offline; cloud models require an API key.", "174da92062": "Hold", "118b3c2dee": "Toggle", "ad5d036ecc": "Could not request microphone permission. Voice dictation was not enabled.", @@ -9805,7 +9785,6 @@ "1eac933202": "Opened macOS Privacy & Security. Enable dictation again after granting access.", "cd9fe37556": "Microphone permission granted", "openAiKeyName": "Your OpenAI transcription key", - "dictationShortcutDescription": "Press {{shortcut}} to dictate text into any focused pane.", "dictationModeAuto": "Auto", "dictationModesWithAutoDescription": "Auto: hold {{shortcut}} to dictate until you let go, or tap it to start and stop. Hold: dictate while {{shortcut}} is held. Toggle: press {{shortcut}} once to start, again to stop.", "speechGpuLabel": "Use GPU", @@ -21405,6 +21384,67 @@ "edit": "Edit", "removeLabel": "Remove OpenAI-compatible provider", "setUp": "Set up" + }, + "sectionDescription": "Talk instead of typing in any pane. Speech is recognized on this computer unless you pick a cloud model.", + "input": { + "title": "Input", + "shortcut": "Shortcut", + "shortcutDescription": "Starts dictation in the focused pane.", + "changeShortcut": "Change" + }, + "language": { + "title": "Language" + }, + "cleanup": { + "title": "Cleanup" + }, + "advanced": { + "title": "Advanced" + }, + "setup": { + "downloadingDescription": "You can leave this page. Dictation works once the download finishes.", + "title": "Set up dictation", + "pickModel": "Talk instead of typing in any pane. Download a speech model below to start; models on this computer keep your audio here.", + "microphoneMissingTitle": "{{microphone}} isn’t connected", + "microphoneMissingDescription": "Dictation uses the system default microphone until it’s back.", + "changeMicrophone": "Change Microphone", + "onTitle": "Dictation is on", + "onLocal": "Press {{shortcut}} in any pane. {{model}} runs on this computer.", + "onCloud": "Press {{shortcut}} in any pane. {{model}} runs in the cloud.", + "offTitle": "Dictation is off", + "offDescription": "Turn it on to dictate with {{shortcut}} in any pane.", + "savedMicrophone": "Your microphone" + }, + "quality": { + "unsupported": "Doesn’t support your language", + "unmeasured": "Not measured", + "poor": "Poor", + "fair": "Fair", + "good": "Good", + "veryGood": "Very good", + "excellent": "Excellent", + "characterErrorRate": "{{percent}}% character error rate", + "wordErrorRate": "{{percent}}% word error rate", + "librispeech": "Test: LibriSpeech read speech, {{clips}} clips, measured {{date}}.", + "fleurs": "Test: FLEURS read speech, {{clips}} clips, measured {{date}}.", + "bench": "Orca’s benchmark on a Mac with Apple silicon.", + "label": "Quality: {{word}}" + }, + "models": { + "retired": "No longer offered. Delete it to free up space.", + "sendsToOpenAi": "Sends your audio to OpenAI.", + "sendsToCompatible": "Sends your audio to the service you set up.", + "delete": "Delete", + "cancelDownload": "Cancel download", + "inUse": "In use", + "use": "Use", + "setUpCloud": "Set up", + "download": "Download", + "cloud": "Cloud", + "description": "Quality is rated for {{language}}. Hover a rating to see the test behind it.", + "where": "Where models run", + "local": "On this computer", + "cloudDescription": "Cloud models send your audio to the provider. Models on this computer keep it here." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index 91adf61cfd7..76d24032fdf 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -503,19 +503,6 @@ "missingHost": "La URL de proxy debe incluir un host." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "Máxima precisión para 25 idiomas europeos. Puntuación, mayúsculas y minúsculas y marcas de tiempo por palabra.", - "parakeetV2": "Solo inglés. Más rápido que v3 con una precisión similar. Puntuación y mayúsculas y minúsculas.", - "zipformerStreamingEnRetired": "Ya no se ofrece: omite el comienzo de las frases. Elimínalo para liberar espacio.", - "whisperTiny": "Más de 90 idiomas. Menor precisión que Parakeet, pero con la mayor cobertura de idiomas.", - "senseVoice": "Chino, inglés, japonés, coreano y cantonés con detección automática de idioma.", - "gptTranscribe": "Transcripción en la nube con alta precisión. Requiere una clave API de OpenAI.", - "whisperLargeV3Turbo": "La mejor precisión en japonés, coreano y chino, y más de 90 idiomas. Más lento que Parakeet.", - "retiredForEngine": "Ya no se ofrece: el motor de voz ya no puede ejecutarlo. Elimínelo para liberar espacio.", - "nemotron": "Inglés, español, alemán y muchos idiomas más, con detección automática del idioma." - } - }, "diagnostics": { "errors": { "creatingDisabled": "la creación de archivos de revisión está desactivada", @@ -8529,7 +8516,6 @@ "7807c11c4d": "General", "6855b0f77d": "Termina los flujos de trabajo principales que hacen que Orca sea útil para el trabajo de agentes en paralelo.", "6d119427ef": "Lista de verificación de incorporación", - "eb1176a14e": "Dictado local de voz a texto con modelos en el dispositivo.", "5063bb47a5": "Voz", "7118953f14": "Permite que los agentes controlen cualquier app en tu computadora.", "c9841721cb": "Computer Use", @@ -9039,7 +9025,6 @@ }, "VoicePane": { "68de13f72c": "No se pudo eliminar el modelo.", - "1ba81c0ff0": "recomendado", "cfde55c7b0": "No se pudo descargar el modelo.", "43fd4f454b": "Modelo de voz", "ba4a900d1d": "Modo de dictado", @@ -9050,10 +9035,6 @@ "506df81ba6": "Clave API de OpenAI guardada", "91980ce124": "{{value0}} MB", "61a16c8141": "Extrayendo...", - "8f4d2a51d7": "sin conexión", - "d504ab05f0": "streaming", - "fbe5990716": "Seleccionar modelo", - "e24f7d43d2": "Selecciona un modelo de voz. Los modelos locales funcionan sin conexión; los modelos en la nube requieren una clave API.", "174da92062": "Mantener", "118b3c2dee": "Alternar", "ad5d036ecc": "No se pudo solicitar permiso para el micrófono. El dictado de voz no se habilitó.", @@ -9062,7 +9043,6 @@ "cd9fe37556": "Permiso de micrófono concedido", "6fa734ed95": "Eliminar {{value0}}", "openAiKeyName": "Su clave de transcripción OpenAI", - "dictationShortcutDescription": "Pulsa {{shortcut}} para dictar texto en cualquier panel con foco.", "dictationModeAuto": "Automático", "dictationModesWithAutoDescription": "Automático: mantén {{shortcut}} para dictar hasta que lo sueltes, o tócalo para empezar y parar. Mantener: dicta mientras {{shortcut}} se mantiene presionado. Alternar: pulsa {{shortcut}} una vez para empezar y otra para parar.", "speechGpuLabel": "Usar GPU", @@ -20493,6 +20473,67 @@ "fillerWords": { "title": "Quitar muletillas", "description": "Quita sonidos como «ehm» y «em» del texto dictado, según el idioma de dictado." + }, + "sectionDescription": "Habla en lugar de escribir en cualquier panel. La voz se reconoce en este equipo, salvo que elijas un modelo en la nube.", + "input": { + "title": "Entrada", + "shortcut": "Atajo", + "shortcutDescription": "Inicia el dictado en el panel activo.", + "changeShortcut": "Cambiar" + }, + "language": { + "title": "Idioma" + }, + "cleanup": { + "title": "Limpieza" + }, + "advanced": { + "title": "Avanzado" + }, + "setup": { + "downloadingDescription": "Puedes salir de esta página. El dictado funcionará cuando termine la descarga.", + "title": "Configurar el dictado", + "pickModel": "Habla en lugar de escribir en cualquier panel. Descarga abajo un modelo de voz para empezar; los modelos de este equipo mantienen tu audio aquí.", + "microphoneMissingTitle": "{{microphone}} no está conectado", + "microphoneMissingDescription": "El dictado usa el micrófono predeterminado del sistema hasta que vuelva.", + "changeMicrophone": "Cambiar micrófono", + "onTitle": "El dictado está activado", + "onLocal": "Pulsa {{shortcut}} en cualquier panel. {{model}} se ejecuta en este equipo.", + "onCloud": "Pulsa {{shortcut}} en cualquier panel. {{model}} se ejecuta en la nube.", + "offTitle": "El dictado está desactivado", + "offDescription": "Actívalo para dictar con {{shortcut}} en cualquier panel.", + "savedMicrophone": "Tu micrófono" + }, + "quality": { + "unsupported": "No admite tu idioma", + "unmeasured": "Sin medir", + "poor": "Mala", + "fair": "Regular", + "good": "Buena", + "veryGood": "Muy buena", + "excellent": "Excelente", + "characterErrorRate": "{{percent}} % de error por carácter", + "wordErrorRate": "{{percent}} % de error por palabra", + "librispeech": "Prueba: LibriSpeech (habla leída), {{clips}} clips, medida el {{date}}.", + "fleurs": "Prueba: FLEURS (habla leída), {{clips}} clips, medida el {{date}}.", + "bench": "Benchmark de Orca en un Mac con Apple silicon.", + "label": "Calidad: {{word}}" + }, + "models": { + "retired": "Ya no se ofrece. Bórralo para liberar espacio.", + "sendsToOpenAi": "Envía tu audio a OpenAI.", + "delete": "Borrar", + "cancelDownload": "Cancelar descarga", + "inUse": "En uso", + "use": "Usar", + "setUpCloud": "Configurar", + "download": "Descargar", + "cloud": "Nube", + "description": "La calidad se valora para {{language}}. Pasa el cursor sobre una valoración para ver la prueba.", + "where": "Dónde se ejecutan los modelos", + "local": "En este equipo", + "cloudDescription": "Los modelos en la nube envían tu audio al proveedor. Los de este equipo lo mantienen aquí.", + "sendsToCompatible": "Envía tu audio al servicio que configuraste." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/fr.json b/src/renderer/src/i18n/locales/fr.json index 7a8bbf7ad7d..5e54997ebbb 100644 --- a/src/renderer/src/i18n/locales/fr.json +++ b/src/renderer/src/i18n/locales/fr.json @@ -742,19 +742,6 @@ "missingHost": "L’URL du proxy doit inclure un hôte." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "Précision maximale pour 25 langues européennes. Ponctuation, majuscules et minuscules, et horodatage par mot.", - "parakeetV2": "Anglais uniquement. Plus rapide que v3 avec une précision similaire. Ponctuation, majuscules et minuscules.", - "zipformerStreamingEnRetired": "N’est plus proposé : il omet le début des phrases. Supprimez-le pour libérer de l’espace.", - "whisperTiny": "Plus de 90 langues. Moins précis que Parakeet, mais offre la plus large couverture linguistique.", - "senseVoice": "Chinois, anglais, japonais, coréen et cantonais avec détection automatique de la langue.", - "gptTranscribe": "Transcription dans le cloud avec une grande précision. Nécessite une clé API OpenAI.", - "whisperLargeV3Turbo": "Meilleure précision pour le japonais, le coréen et le chinois, et plus de 90 langues. Plus lent que Parakeet.", - "retiredForEngine": "N’est plus proposé : le moteur vocal ne peut plus l’exécuter. Supprimez-le pour libérer de l’espace.", - "nemotron": "Anglais, espagnol, allemand et bien d’autres langues, avec détection automatique de la langue." - } - }, "diagnostics": { "errors": { "creatingDisabled": "la création de fichiers de révision est désactivée", @@ -8814,7 +8801,6 @@ "7807c11c4d": "Général", "6855b0f77d": "Terminez les workflows essentiels qui rendent Orca utile au travail parallèle avec des agents.", "6d119427ef": "Checklist d'intégration", - "eb1176a14e": "Dictée vocale locale avec modèles embarqués sur l'appareil.", "5063bb47a5": "Voix", "7118953f14": "Permettez aux agents de contrôler n'importe quelle app de votre ordinateur.", "c9841721cb": "Computer Use", @@ -9329,7 +9315,6 @@ "VoicePane": { "6fa734ed95": "Supprimer {{value0}}", "68de13f72c": "Échec de la suppression du modèle.", - "1ba81c0ff0": "recommandé", "cfde55c7b0": "Échec du téléchargement du modèle.", "43fd4f454b": "Modèle vocal", "ba4a900d1d": "Mode de dictée", @@ -9340,10 +9325,6 @@ "506df81ba6": "Clé API OpenAI enregistrée", "91980ce124": "{{value0}} Mo", "61a16c8141": "Extraction...", - "8f4d2a51d7": "hors ligne", - "d504ab05f0": "streaming", - "fbe5990716": "Sélectionner un modèle", - "e24f7d43d2": "Sélectionnez un modèle vocal. Les modèles locaux fonctionnent hors ligne ; les modèles cloud nécessitent une clé API.", "174da92062": "Maintien", "118b3c2dee": "Bascule", "ad5d036ecc": "Impossible de demander l'autorisation d'accès au micro. La dictée vocale n'a pas été activée.", @@ -9351,7 +9332,6 @@ "1eac933202": "Panneau Confidentialité et sécurité de macOS ouvert. Réactivez la dictée après avoir accordé l'accès.", "cd9fe37556": "Autorisation d'accès au micro accordée", "openAiKeyName": "Votre clé de transcription OpenAI", - "dictationShortcutDescription": "Appuyez sur {{shortcut}} pour dicter du texte dans n'importe quel volet actif.", "dictationModeAuto": "Auto", "dictationModesWithAutoDescription": "Auto : maintenez {{shortcut}} pour dicter jusqu’à ce que vous relâchiez, ou appuyez brièvement pour démarrer et arrêter. Maintien : dictez tant que {{shortcut}} est maintenu. Bascule : appuyez une fois sur {{shortcut}} pour démarrer, puis à nouveau pour arrêter.", "speechGpuLabel": "Utiliser le GPU", @@ -20810,6 +20790,67 @@ "fillerWords": { "title": "Supprimer les hésitations", "description": "Supprime les sons comme « euh » et « hum » du texte dicté, selon la langue de dictée." + }, + "sectionDescription": "Parlez au lieu de taper, dans n’importe quel volet. La parole est reconnue sur cet ordinateur, sauf si vous choisissez un modèle cloud.", + "input": { + "title": "Entrée", + "shortcut": "Raccourci", + "shortcutDescription": "Lance la dictée dans le volet actif.", + "changeShortcut": "Modifier" + }, + "language": { + "title": "Langue" + }, + "cleanup": { + "title": "Nettoyage" + }, + "advanced": { + "title": "Avancé" + }, + "setup": { + "downloadingDescription": "Vous pouvez quitter cette page. La dictée fonctionnera une fois le téléchargement terminé.", + "title": "Configurer la dictée", + "pickModel": "Parlez au lieu de taper, dans n’importe quel volet. Téléchargez un modèle vocal ci-dessous pour commencer ; les modèles de cet ordinateur gardent votre audio ici.", + "microphoneMissingTitle": "{{microphone}} n’est pas connecté", + "microphoneMissingDescription": "La dictée utilise le micro par défaut du système jusqu’à son retour.", + "changeMicrophone": "Changer de micro", + "onTitle": "La dictée est activée", + "onLocal": "Appuyez sur {{shortcut}} dans n’importe quel volet. {{model}} tourne sur cet ordinateur.", + "onCloud": "Appuyez sur {{shortcut}} dans n’importe quel volet. {{model}} tourne dans le cloud.", + "offTitle": "La dictée est désactivée", + "offDescription": "Activez-la pour dicter avec {{shortcut}} dans n’importe quel volet.", + "savedMicrophone": "Votre micro" + }, + "quality": { + "unsupported": "Ne prend pas en charge votre langue", + "unmeasured": "Non mesuré", + "poor": "Faible", + "fair": "Passable", + "good": "Bonne", + "veryGood": "Très bonne", + "excellent": "Excellente", + "characterErrorRate": "{{percent}} % d’erreurs par caractère", + "wordErrorRate": "{{percent}} % d’erreurs par mot", + "librispeech": "Test : LibriSpeech (parole lue), {{clips}} extraits, mesuré le {{date}}.", + "fleurs": "Test : FLEURS (parole lue), {{clips}} extraits, mesuré le {{date}}.", + "bench": "Benchmark d’Orca sur un Mac avec puce Apple.", + "label": "Qualité : {{word}}" + }, + "models": { + "retired": "N’est plus proposé. Supprimez-le pour libérer de l’espace.", + "sendsToOpenAi": "Envoie votre audio à OpenAI.", + "delete": "Supprimer", + "cancelDownload": "Annuler le téléchargement", + "inUse": "Utilisé", + "use": "Utiliser", + "setUpCloud": "Configurer", + "download": "Télécharger", + "cloud": "Cloud", + "description": "La qualité est évaluée pour {{language}}. Survolez une note pour voir le test.", + "where": "Où les modèles tournent", + "local": "Sur cet ordinateur", + "cloudDescription": "Les modèles cloud envoient votre audio au fournisseur. Ceux de cet ordinateur le gardent ici.", + "sendsToCompatible": "Envoie votre audio au service que vous avez configuré." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index b3d7e56f49c..6f309bdfab3 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -497,19 +497,6 @@ "missingHost": "プロキシ URL にはホストが必要です。" } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "ヨーロッパの 25 言語で最高の精度。句読点、大文字・小文字、単語単位のタイムスタンプに対応。", - "parakeetV2": "英語のみ。v3 と同程度の精度で、より高速。句読点と大文字・小文字に対応。", - "zipformerStreamingEnRetired": "提供を終了しました。文の冒頭が抜け落ちます。削除して空き容量を確保してください。", - "whisperTiny": "90 以上の言語に対応。Parakeet より精度は低いものの、対応言語が最も豊富。", - "senseVoice": "中国語、英語、日本語、韓国語、広東語に対応。言語を自動検出。", - "gptTranscribe": "高精度なクラウド文字起こし。OpenAI API キーが必要です。", - "whisperLargeV3Turbo": "日本語・韓国語・中国語で最も高い精度。90以上の言語に対応。Parakeetより低速です。", - "retiredForEngine": "提供終了: 音声エンジンでこのモデルを実行できなくなりました。削除して空き容量を確保してください。", - "nemotron": "英語、スペイン語、ドイツ語など多数の言語に対応。言語を自動判別します。" - } - }, "diagnostics": { "errors": { "creatingDisabled": "確認用ファイルの作成は無効になっています", @@ -8273,7 +8260,6 @@ "7807c11c4d": "一般", "6855b0f77d": "Orca を並列 Agent 作業に活用するためのコアワークフローを完了します。", "6d119427ef": "オンボーディングチェックリスト", - "eb1176a14e": "オンデバイスモデルを使用したローカルの音声からテキストへのディクテーション。", "5063bb47a5": "音声", "7118953f14": "Agent がコンピュータ上の任意のアプリを制御できるようにします。", "c9841721cb": "コンピュータ操作", @@ -8783,7 +8769,6 @@ }, "VoicePane": { "68de13f72c": "モデルの削除に失敗しました。", - "1ba81c0ff0": "推奨", "cfde55c7b0": "モデルのダウンロードに失敗しました。", "43fd4f454b": "音声モデル", "ba4a900d1d": "ディクテーションモード", @@ -8794,10 +8779,6 @@ "506df81ba6": "OpenAI API キーが保存されました", "91980ce124": "{{value0}} MB", "61a16c8141": "抽出中…", - "8f4d2a51d7": "オフライン", - "d504ab05f0": "ストリーミング", - "fbe5990716": "モデルの選択", - "e24f7d43d2": "音声モデルを選択します。ローカルモデルはオフラインで実行されます。クラウドモデルには API キーが必要です。", "174da92062": "ホールド", "118b3c2dee": "トグル", "ad5d036ecc": "マイクの許可を要求できませんでした。音声ディクテーションが有効になっていませんでした。", @@ -8806,7 +8787,6 @@ "cd9fe37556": "マイクの許可が与えられました", "6fa734ed95": "{{value0}} を削除", "openAiKeyName": "OpenAI 転写キー", - "dictationShortcutDescription": "{{shortcut}} を押すと、フォーカスされているペインにテキストを音声入力できます。", "dictationModeAuto": "自動", "dictationModesWithAutoDescription": "自動:{{shortcut}} を押し続けると離すまで音声入力し、短く押すと開始と停止を切り替えます。ホールド:{{shortcut}} を押している間、音声入力します。トグル:{{shortcut}} を1回押すと開始し、もう一度押すと停止します。", "speechGpuLabel": "GPU を使用", @@ -19986,6 +19966,67 @@ "fillerWords": { "title": "フィラーを削除", "description": "音声入力の言語に合わせて、「えーと」「うーん」などのつなぎ言葉を入力テキストから取り除きます。" + }, + "sectionDescription": "どのペインでも、入力の代わりに話せます。クラウドモデルを選ばない限り、音声はこのコンピューター上で認識されます。", + "input": { + "title": "入力", + "shortcut": "ショートカット", + "shortcutDescription": "フォーカス中のペインで音声入力を開始します。", + "changeShortcut": "変更" + }, + "language": { + "title": "言語" + }, + "cleanup": { + "title": "クリーンアップ" + }, + "advanced": { + "title": "詳細設定" + }, + "setup": { + "downloadingDescription": "このページを離れても大丈夫です。ダウンロードが終わると音声入力が使えます。", + "title": "音声入力を設定", + "pickModel": "どのペインでも、入力の代わりに話せます。下の音声モデルをダウンロードして始めましょう。このコンピューター上のモデルなら音声は外に出ません。", + "microphoneMissingTitle": "{{microphone}} が接続されていません", + "microphoneMissingDescription": "接続されるまで、システムの既定のマイクを使います。", + "changeMicrophone": "マイクを変更", + "onTitle": "音声入力はオンです", + "onLocal": "どのペインでも {{shortcut}} を押します。{{model}} はこのコンピューター上で動作します。", + "onCloud": "どのペインでも {{shortcut}} を押します。{{model}} はクラウドで動作します。", + "offTitle": "音声入力はオフです", + "offDescription": "オンにすると、どのペインでも {{shortcut}} で音声入力できます。", + "savedMicrophone": "選択したマイク" + }, + "quality": { + "unsupported": "お使いの言語に非対応", + "unmeasured": "未測定", + "poor": "低い", + "fair": "まずまず", + "good": "良い", + "veryGood": "とても良い", + "excellent": "非常に優秀", + "characterErrorRate": "文字誤り率 {{percent}}%", + "wordErrorRate": "単語誤り率 {{percent}}%", + "librispeech": "テスト:LibriSpeech(朗読音声)、{{clips}} クリップ、{{date}} に測定。", + "fleurs": "テスト:FLEURS(朗読音声)、{{clips}} クリップ、{{date}} に測定。", + "bench": "Apple シリコン搭載 Mac での Orca のベンチマーク。", + "label": "品質:{{word}}" + }, + "models": { + "retired": "提供終了。削除すると空き容量が増えます。", + "sendsToOpenAi": "音声を OpenAI に送信します。", + "delete": "削除", + "cancelDownload": "ダウンロードをキャンセル", + "inUse": "使用中", + "use": "使用", + "setUpCloud": "設定", + "download": "ダウンロード", + "cloud": "クラウド", + "description": "品質は{{language}}での評価です。評価にカーソルを合わせるとテストの詳細が表示されます。", + "where": "モデルの実行場所", + "local": "このコンピューター", + "cloudDescription": "クラウドモデルは音声をプロバイダーに送信します。このコンピューター上のモデルなら音声は外に出ません。", + "sendsToCompatible": "音声を設定したサービスに送信します。" } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index bd509836c0f..068db9b1fbd 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -497,19 +497,6 @@ "missingHost": "프록시 URL에는 호스트가 포함되어야 합니다." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "유럽 25개 언어에서 가장 높은 정확도. 문장 부호, 대소문자, 단어별 타임스탬프 지원.", - "parakeetV2": "영어 전용. v3와 정확도는 비슷하며 속도는 더 빠릅니다. 문장 부호와 대소문자 지원.", - "zipformerStreamingEnRetired": "더 이상 제공되지 않습니다. 문장 앞부분이 누락됩니다. 삭제하여 공간을 확보하세요.", - "whisperTiny": "90개 이상 언어 지원. Parakeet보다 정확도는 낮지만 지원 언어가 가장 다양합니다.", - "senseVoice": "중국어, 영어, 일본어, 한국어, 광둥어를 지원하며 언어를 자동으로 감지합니다.", - "gptTranscribe": "정확도가 높은 클라우드 전사. OpenAI API 키가 필요합니다.", - "whisperLargeV3Turbo": "일본어, 한국어, 중국어에서 가장 정확하며 90개 이상의 언어를 지원합니다. Parakeet보다 느립니다.", - "retiredForEngine": "더 이상 제공되지 않음: 음성 엔진에서 더 이상 실행할 수 없습니다. 삭제하여 공간을 확보하세요.", - "nemotron": "영어, 스페인어, 독일어 등 다양한 언어를 지원하며 언어를 자동으로 감지합니다." - } - }, "diagnostics": { "errors": { "creatingDisabled": "검토 파일 생성이 비활성화되어 있습니다", @@ -8236,7 +8223,6 @@ "7807c11c4d": "일반", "6855b0f77d": "Orca를 병렬 에이전트 작업에 유용하게 만드는 핵심 워크플로를 완료합니다.", "6d119427ef": "온보딩 체크리스트", - "eb1176a14e": "온디바이스 모델을 사용한 로컬 음성-텍스트 받아쓰기.", "5063bb47a5": "음성", "7118953f14": "agents가 컴퓨터의 모든 앱을 제어할 수 있도록 하세요.", "c9841721cb": "컴퓨터 사용", @@ -8746,7 +8732,6 @@ }, "VoicePane": { "68de13f72c": "모델을 삭제하지 못했습니다.", - "1ba81c0ff0": "추천", "cfde55c7b0": "모델을 다운로드하지 못했습니다.", "43fd4f454b": "음성 모델", "ba4a900d1d": "받아쓰기 모드", @@ -8757,10 +8742,6 @@ "506df81ba6": "OpenAI API 키가 저장되었습니다.", "91980ce124": "{{value0}}MB", "61a16c8141": "추출 중...", - "8f4d2a51d7": "오프라인", - "d504ab05f0": "스트리밍", - "fbe5990716": "모델 선택", - "e24f7d43d2": "음성 모델을 선택하세요. 로컬 모델은 오프라인으로 실행됩니다. 클라우드 모델에는 API 키가 필요합니다.", "174da92062": "길게 누르기", "118b3c2dee": "토글", "ad5d036ecc": "마이크 권한을 요청할 수 없습니다. 음성 받아쓰기가 활성화되지 않았습니다.", @@ -8769,7 +8750,6 @@ "cd9fe37556": "마이크 권한이 부여되었습니다.", "6fa734ed95": "{{value0}} 삭제", "openAiKeyName": "OpenAI 트랜스크립션 키", - "dictationShortcutDescription": "{{shortcut}} 단축키를 눌러 포커스된 패널에 텍스트를 받아쓰세요.", "dictationModeAuto": "자동", "dictationModesWithAutoDescription": "자동: {{shortcut}} 단축키를 길게 누르면 손을 뗄 때까지 받아쓰기하고, 짧게 누르면 시작하고 중지합니다. 길게 누르기: {{shortcut}} 단축키를 누르고 있는 동안 받아쓰기합니다. 토글: {{shortcut}} 단축키를 한 번 누르면 받아쓰기를 시작하고, 다시 누르면 중지합니다.", "speechGpuLabel": "GPU 사용", @@ -19986,6 +19966,67 @@ "fillerWords": { "title": "군말 제거", "description": "받아쓰기 언어에 맞춰 받아쓴 텍스트에서 “음”, “흠” 같은 소리를 지웁니다." + }, + "sectionDescription": "어느 창에서든 입력 대신 말하세요. 클라우드 모델을 고르지 않으면 음성은 이 컴퓨터에서 인식됩니다.", + "input": { + "title": "입력", + "shortcut": "단축키", + "shortcutDescription": "포커스된 창에서 받아쓰기를 시작합니다.", + "changeShortcut": "변경" + }, + "language": { + "title": "언어" + }, + "cleanup": { + "title": "정리" + }, + "advanced": { + "title": "고급" + }, + "setup": { + "downloadingDescription": "이 페이지를 나가도 됩니다. 다운로드가 끝나면 받아쓰기를 쓸 수 있습니다.", + "title": "받아쓰기 설정", + "pickModel": "어느 창에서든 입력 대신 말하세요. 아래에서 음성 모델을 다운로드해 시작하세요. 이 컴퓨터의 모델은 오디오를 밖으로 보내지 않습니다.", + "microphoneMissingTitle": "{{microphone}}이(가) 연결되어 있지 않습니다", + "microphoneMissingDescription": "다시 연결될 때까지 시스템 기본 마이크를 사용합니다.", + "changeMicrophone": "마이크 변경", + "onTitle": "받아쓰기가 켜져 있습니다", + "onLocal": "어느 창에서든 {{shortcut}}을(를) 누르세요. {{model}}은(는) 이 컴퓨터에서 실행됩니다.", + "onCloud": "어느 창에서든 {{shortcut}}을(를) 누르세요. {{model}}은(는) 클라우드에서 실행됩니다.", + "offTitle": "받아쓰기가 꺼져 있습니다", + "offDescription": "켜면 어느 창에서든 {{shortcut}}으로 받아쓸 수 있습니다.", + "savedMicrophone": "선택한 마이크" + }, + "quality": { + "unsupported": "사용하는 언어를 지원하지 않음", + "unmeasured": "측정 안 됨", + "poor": "낮음", + "fair": "보통", + "good": "좋음", + "veryGood": "매우 좋음", + "excellent": "탁월함", + "characterErrorRate": "문자 오류율 {{percent}}%", + "wordErrorRate": "단어 오류율 {{percent}}%", + "librispeech": "테스트: LibriSpeech(낭독 음성), 클립 {{clips}}개, {{date}} 측정.", + "fleurs": "테스트: FLEURS(낭독 음성), 클립 {{clips}}개, {{date}} 측정.", + "bench": "Apple 실리콘 Mac에서 측정한 Orca 벤치마크.", + "label": "품질: {{word}}" + }, + "models": { + "retired": "더 이상 제공되지 않습니다. 삭제하면 공간을 확보할 수 있습니다.", + "sendsToOpenAi": "오디오를 OpenAI로 보냅니다.", + "delete": "삭제", + "cancelDownload": "다운로드 취소", + "inUse": "사용 중", + "use": "사용", + "setUpCloud": "설정", + "download": "다운로드", + "cloud": "클라우드", + "description": "품질은 {{language}} 기준입니다. 평가에 마우스를 올리면 테스트 내용을 볼 수 있습니다.", + "where": "모델 실행 위치", + "local": "이 컴퓨터", + "cloudDescription": "클라우드 모델은 오디오를 제공업체로 보냅니다. 이 컴퓨터의 모델은 오디오를 밖으로 보내지 않습니다.", + "sendsToCompatible": "오디오를 설정한 서비스로 보냅니다." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/pt.json b/src/renderer/src/i18n/locales/pt.json index 35b47a935ef..12065876a1f 100644 --- a/src/renderer/src/i18n/locales/pt.json +++ b/src/renderer/src/i18n/locales/pt.json @@ -1717,19 +1717,6 @@ "missingHost": "A URL de proxy deve incluir um host." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "Máxima precisão para 25 idiomas europeus. Pontuação, maiúsculas e minúsculas e marcas de tempo por palavra.", - "parakeetV2": "Somente inglês. Mais rápido que v3 com precisão semelhante. Pontuação e maiúsculas e minúsculas.", - "zipformerStreamingEnRetired": "Não é mais oferecido: ele perde o início das frases. Exclua-o para liberar espaço.", - "whisperTiny": "Mais de 90 idiomas. Menor precisão que Parakeet, mas com a maior cobertura de idiomas.", - "senseVoice": "Chinês, inglês, japonês, coreano e cantonês com detecção automática de idioma.", - "gptTranscribe": "Transcrição na nuvem com alta precisão. Exige uma chave de API da OpenAI.", - "whisperLargeV3Turbo": "A melhor precisão em japonês, coreano e chinês, e mais de 90 idiomas. Mais lento que o Parakeet.", - "retiredForEngine": "Não é mais oferecido: o mecanismo de fala não consegue mais executá-lo. Exclua-o para liberar espaço.", - "nemotron": "Inglês, espanhol, alemão e muitos outros idiomas, com detecção automática de idioma." - } - }, "diagnostics": { "errors": { "creatingDisabled": "a criação de arquivos de revisão está desativada", @@ -5147,7 +5134,6 @@ "7807c11c4d": "Geral", "6855b0f77d": "Conclua os fluxos de trabalho essenciais que tornam o Orca útil para trabalho paralelo com agentes.", "6d119427ef": "Checklist de introdução", - "eb1176a14e": "Ditado de voz para texto local com modelos no dispositivo.", "5063bb47a5": "Voz", "7118953f14": "Permita que agentes controlem qualquer aplicativo no seu computador.", "c9841721cb": "Uso do Computador", @@ -6480,7 +6466,6 @@ "VoicePane": { "6fa734ed95": "Excluir {{value0}}", "68de13f72c": "Falha ao excluir o modelo.", - "1ba81c0ff0": "recomendado", "cfde55c7b0": "Falha ao baixar o modelo.", "43fd4f454b": "Modelo de Voz", "ba4a900d1d": "Modo de Ditado", @@ -6491,10 +6476,6 @@ "506df81ba6": "Chave de API da OpenAI salva", "91980ce124": "{{value0}} MB", "61a16c8141": "Extraindo...", - "8f4d2a51d7": "off-line", - "d504ab05f0": "streaming", - "fbe5990716": "Selecionar Modelo", - "e24f7d43d2": "Selecione um modelo de voz. Modelos locais funcionam off-line; modelos na nuvem exigem uma chave de API.", "174da92062": "Manter Pressionado", "118b3c2dee": "Alternar", "ad5d036ecc": "Não foi possível solicitar a permissão do microfone. O ditado por voz não foi ativado.", @@ -6502,7 +6483,6 @@ "1eac933202": "Privacidade e Segurança do macOS aberta. Ative o ditado novamente após conceder o acesso.", "cd9fe37556": "Permissão do microfone concedida", "openAiKeyName": "Sua chave de transcrição da OpenAI", - "dictationShortcutDescription": "Pressione {{shortcut}} para ditar texto em qualquer painel com foco.", "dictationModeAuto": "Automático", "dictationModesWithAutoDescription": "Automático: mantenha {{shortcut}} pressionado para ditar até soltar, ou toque para iniciar e parar. Manter pressionado: dite enquanto mantém {{shortcut}} pressionado. Alternar: pressione {{shortcut}} uma vez para iniciar e novamente para parar.", "speechGpuLabel": "Usar GPU", @@ -21675,6 +21655,67 @@ "fillerWords": { "title": "Remover hesitações", "description": "Remove sons como “hã” e “hum” do texto ditado, conforme o idioma do ditado." + }, + "sectionDescription": "Fale em vez de digitar em qualquer painel. A fala é reconhecida neste computador, a menos que você escolha um modelo na nuvem.", + "input": { + "title": "Entrada", + "shortcut": "Atalho", + "shortcutDescription": "Inicia o ditado no painel em foco.", + "changeShortcut": "Alterar" + }, + "language": { + "title": "Idioma" + }, + "cleanup": { + "title": "Limpeza" + }, + "advanced": { + "title": "Avançado" + }, + "setup": { + "downloadingDescription": "Você pode sair desta página. O ditado funciona quando o download terminar.", + "title": "Configurar o ditado", + "pickModel": "Fale em vez de digitar em qualquer painel. Baixe um modelo de voz abaixo para começar; modelos neste computador mantêm seu áudio aqui.", + "microphoneMissingTitle": "{{microphone}} não está conectado", + "microphoneMissingDescription": "O ditado usa o microfone padrão do sistema até ele voltar.", + "changeMicrophone": "Trocar microfone", + "onTitle": "O ditado está ativado", + "onLocal": "Pressione {{shortcut}} em qualquer painel. {{model}} roda neste computador.", + "onCloud": "Pressione {{shortcut}} em qualquer painel. {{model}} roda na nuvem.", + "offTitle": "O ditado está desativado", + "offDescription": "Ative para ditar com {{shortcut}} em qualquer painel.", + "savedMicrophone": "Seu microfone" + }, + "quality": { + "unsupported": "Não suporta seu idioma", + "unmeasured": "Não medido", + "poor": "Ruim", + "fair": "Razoável", + "good": "Boa", + "veryGood": "Muito boa", + "excellent": "Excelente", + "characterErrorRate": "{{percent}}% de erro por caractere", + "wordErrorRate": "{{percent}}% de erro por palavra", + "librispeech": "Teste: LibriSpeech (fala lida), {{clips}} clipes, medido em {{date}}.", + "fleurs": "Teste: FLEURS (fala lida), {{clips}} clipes, medido em {{date}}.", + "bench": "Benchmark do Orca em um Mac com Apple silicon.", + "label": "Qualidade: {{word}}" + }, + "models": { + "retired": "Não é mais oferecido. Exclua para liberar espaço.", + "sendsToOpenAi": "Envia seu áudio para a OpenAI.", + "delete": "Excluir", + "cancelDownload": "Cancelar download", + "inUse": "Em uso", + "use": "Usar", + "setUpCloud": "Configurar", + "download": "Baixar", + "cloud": "Nuvem", + "description": "A qualidade é avaliada para {{language}}. Passe o cursor sobre uma avaliação para ver o teste.", + "where": "Onde os modelos rodam", + "local": "Neste computador", + "cloudDescription": "Modelos na nuvem enviam seu áudio ao provedor. Os deste computador o mantêm aqui.", + "sendsToCompatible": "Envia seu áudio para o serviço que você configurou." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/ru.json b/src/renderer/src/i18n/locales/ru.json index 8f07a41b469..6cc23c88138 100644 --- a/src/renderer/src/i18n/locales/ru.json +++ b/src/renderer/src/i18n/locales/ru.json @@ -1014,19 +1014,6 @@ "missingHost": "URL прокси должен содержать адрес сервера." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "Наивысшая точность для 25 европейских языков. Пунктуация, регистр букв и временные метки для каждого слова.", - "parakeetV2": "Только английский. Быстрее v3 при схожей точности. Пунктуация и регистр букв.", - "zipformerStreamingEnRetired": "Больше не предлагается: теряет начало фраз. Удалите модель, чтобы освободить место.", - "whisperTiny": "Более 90 языков. Точность ниже, чем у Parakeet, но самый широкий охват языков.", - "senseVoice": "Китайский, английский, японский, корейский и кантонский с автоматическим определением языка.", - "gptTranscribe": "Облачная расшифровка с высокой точностью. Требуется API-ключ OpenAI.", - "whisperLargeV3Turbo": "Лучшая точность для японского, корейского и китайского, а также более 90 языков. Медленнее, чем Parakeet.", - "retiredForEngine": "Больше не предлагается: движок распознавания речи больше не может её запускать. Удалите её, чтобы освободить место.", - "nemotron": "Английский, испанский, немецкий и многие другие языки, с автоматическим определением языка." - } - }, "diagnostics": { "errors": { "creatingDisabled": "создание файлов для проверки отключено", @@ -9298,7 +9285,6 @@ "7807c11c4d": "Общие", "6855b0f77d": "Завершите настройку основных процессов Orca для параллельной работы агентов.", "6d119427ef": "Знакомство с Orca", - "eb1176a14e": "Локальная голосовая диктовка с моделями на устройстве.", "5063bb47a5": "Голос", "7118953f14": "Разрешите агентам управлять любыми приложениями на компьютере.", "c9841721cb": "Управление компьютером", @@ -9813,7 +9799,6 @@ "VoicePane": { "6fa734ed95": "Удалить {{value0}}", "68de13f72c": "Не удалось удалить модель.", - "1ba81c0ff0": "рекомендуется", "cfde55c7b0": "Не удалось скачать модель.", "43fd4f454b": "Речевая модель", "ba4a900d1d": "Режим диктовки", @@ -9824,10 +9809,6 @@ "506df81ba6": "API-ключ OpenAI сохранён", "91980ce124": "{{value0}} МБ", "61a16c8141": "Распаковка…", - "8f4d2a51d7": "без сети", - "d504ab05f0": "потоковая обработка", - "fbe5990716": "Выберите модель", - "e24f7d43d2": "Выберите речевую модель. Локальные модели работают без сети; облачным нужен API-ключ.", "174da92062": "Удержание", "118b3c2dee": "Переключение", "ad5d036ecc": "Не удалось запросить доступ к микрофону. Голосовая диктовка не включена.", @@ -9835,7 +9816,6 @@ "1eac933202": "Открыт раздел macOS «Конфиденциальность и безопасность». После выдачи доступа включите диктовку снова.", "cd9fe37556": "Доступ к микрофону разрешён", "openAiKeyName": "Ваш ключ расшифровки OpenAI", - "dictationShortcutDescription": "Нажмите {{shortcut}}, чтобы диктовать текст в любую активную панель.", "dictationModeAuto": "Авто", "dictationModesWithAutoDescription": "Авто: удерживайте {{shortcut}}, чтобы диктовать до отпускания, или нажмите коротко, чтобы начать и остановить. Удержание: диктуйте, пока удерживаете {{shortcut}}. Переключение: нажмите {{shortcut}} для начала и ещё раз для остановки.", "speechGpuLabel": "Использовать GPU", @@ -21392,6 +21372,67 @@ "fillerWords": { "title": "Удалять слова-паразиты", "description": "Убирает звуки вроде «э» и «эм» из надиктованного текста с учётом языка диктовки." + }, + "sectionDescription": "Говорите вместо того, чтобы печатать, в любой панели. Речь распознаётся на этом компьютере, если вы не выберете облачную модель.", + "input": { + "title": "Ввод", + "shortcut": "Сочетание клавиш", + "shortcutDescription": "Запускает диктовку в активной панели.", + "changeShortcut": "Изменить" + }, + "language": { + "title": "Язык" + }, + "cleanup": { + "title": "Очистка" + }, + "advanced": { + "title": "Дополнительно" + }, + "setup": { + "downloadingDescription": "Можно уйти с этой страницы. Диктовка заработает, когда загрузка закончится.", + "title": "Настроить диктовку", + "pickModel": "Говорите вместо того, чтобы печатать, в любой панели. Скачайте модель речи ниже, чтобы начать; модели на этом компьютере не отправляют звук наружу.", + "microphoneMissingTitle": "{{microphone}} не подключён", + "microphoneMissingDescription": "Пока он не вернётся, диктовка использует системный микрофон по умолчанию.", + "changeMicrophone": "Сменить микрофон", + "onTitle": "Диктовка включена", + "onLocal": "Нажмите {{shortcut}} в любой панели. {{model}} работает на этом компьютере.", + "onCloud": "Нажмите {{shortcut}} в любой панели. {{model}} работает в облаке.", + "offTitle": "Диктовка выключена", + "offDescription": "Включите, чтобы диктовать с помощью {{shortcut}} в любой панели.", + "savedMicrophone": "Ваш микрофон" + }, + "quality": { + "unsupported": "Не поддерживает ваш язык", + "unmeasured": "Не измерено", + "poor": "Плохо", + "fair": "Средне", + "good": "Хорошо", + "veryGood": "Очень хорошо", + "excellent": "Отлично", + "characterErrorRate": "{{percent}}% ошибок в символах", + "wordErrorRate": "{{percent}}% ошибок в словах", + "librispeech": "Тест: LibriSpeech (чтение вслух), клипов: {{clips}}, измерено {{date}}.", + "fleurs": "Тест: FLEURS (чтение вслух), клипов: {{clips}}, измерено {{date}}.", + "bench": "Бенчмарк Orca на Mac с Apple silicon.", + "label": "Качество: {{word}}" + }, + "models": { + "retired": "Больше не предлагается. Удалите, чтобы освободить место.", + "sendsToOpenAi": "Отправляет звук в OpenAI.", + "delete": "Удалить", + "cancelDownload": "Отменить загрузку", + "inUse": "Используется", + "use": "Использовать", + "setUpCloud": "Настроить", + "download": "Скачать", + "cloud": "Облако", + "description": "Качество оценено для языка: {{language}}. Наведите на оценку, чтобы увидеть тест.", + "where": "Где работают модели", + "local": "На этом компьютере", + "cloudDescription": "Облачные модели отправляют звук провайдеру. Модели на этом компьютере оставляют его здесь.", + "sendsToCompatible": "Отправляет звук в настроенный вами сервис." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/tr.json b/src/renderer/src/i18n/locales/tr.json index 95304867e99..002982551d7 100644 --- a/src/renderer/src/i18n/locales/tr.json +++ b/src/renderer/src/i18n/locales/tr.json @@ -948,19 +948,6 @@ "missingHost": "Proxy URL'si bir ana makine içermelidir." } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "25 Avrupa dili için en yüksek doğruluk. Noktalama, büyük/küçük harf kullanımı ve sözcük düzeyinde zaman damgaları.", - "parakeetV2": "Yalnızca İngilizce. Benzer doğrulukla v3'ten daha hızlı. Noktalama ve büyük/küçük harf kullanımı.", - "zipformerStreamingEnRetired": "Artık sunulmuyor: cümlelerin başını atlıyor. Yer açmak için silin.", - "whisperTiny": "90'dan fazla dil. Parakeet'ten daha düşük doğruluk, ancak en geniş dil kapsamı.", - "senseVoice": "Otomatik dil algılamayla Çince, İngilizce, Japonca, Korece ve Kantonca.", - "gptTranscribe": "Yüksek doğrulukla bulut transkripsiyonu. OpenAI API anahtarı gerektirir.", - "whisperLargeV3Turbo": "Japonca, Korece ve Çince için en iyi doğruluk, ayrıca 90'dan fazla dil. Parakeet'ten daha yavaş.", - "retiredForEngine": "Artık sunulmuyor: konuşma motoru bu modeli artık çalıştıramıyor. Yer açmak için silin.", - "nemotron": "İngilizce, İspanyolca, Almanca ve daha birçok dil; dil otomatik algılanır." - } - }, "diagnostics": { "errors": { "creatingDisabled": "inceleme dosyası oluşturma devre dışı", @@ -8930,7 +8917,6 @@ "7807c11c4d": "Genel", "6855b0f77d": "Orca'yı paralel ajan çalışması için kullanışlı kılan temel iş akışlarını tamamlayın.", "6d119427ef": "Başlangıç kontrol listesi", - "eb1176a14e": "Cihaz üzerindeki modellerle yerel sesten metne dikte.", "5063bb47a5": "Ses", "7118953f14": "Ajanların bilgisayarınızdaki herhangi bir uygulamayı kontrol etmesini sağlayın.", "c9841721cb": "Bilgisayar Kullanımı", @@ -9445,7 +9431,6 @@ "VoicePane": { "6fa734ed95": "{{value0}} silinsin mi", "68de13f72c": "Model silinemedi.", - "1ba81c0ff0": "önerilen", "cfde55c7b0": "Model indirilemedi.", "43fd4f454b": "Konuşma Modeli", "ba4a900d1d": "Dikte Modu", @@ -9456,10 +9441,6 @@ "506df81ba6": "OpenAI API anahtarı kaydedildi", "91980ce124": "{{value0}} MB", "61a16c8141": "Çıkarılıyor...", - "8f4d2a51d7": "çevrimdışı", - "d504ab05f0": "akış", - "fbe5990716": "Model Seç", - "e24f7d43d2": "Bir konuşma modeli seçin. Yerel modeller çevrimdışı çalışır; bulut modelleri bir API anahtarı gerektirir.", "174da92062": "Basılı Tut", "118b3c2dee": "Aç/kapat", "ad5d036ecc": "Mikrofon izni istenemedi. Sesli dikte etkinleştirilmedi.", @@ -9467,7 +9448,6 @@ "1eac933202": "macOS Gizlilik ve Güvenlik açıldı. Erişimi verdikten sonra dikteyi tekrar etkinleştirin.", "cd9fe37556": "Mikrofon izni verildi", "openAiKeyName": "OpenAI transkripsiyon anahtarınız", - "dictationShortcutDescription": "Odaktaki herhangi bir bölmeye metin dikte etmek için {{shortcut}} tuşlarına basın.", "dictationModeAuto": "Otomatik", "dictationModesWithAutoDescription": "Otomatik: bırakana kadar dikte etmek için {{shortcut}} tuşlarını basılı tutun veya başlatıp durdurmak için kısaca dokunun. Basılı tut: {{shortcut}} basılıyken dikte edin. Aç/kapat: başlatmak için {{shortcut}} tuşlarına bir kez, durdurmak için tekrar basın.", "speechGpuLabel": "GPU kullan", @@ -20687,6 +20667,67 @@ "fillerWords": { "title": "Dolgu seslerini kaldır", "description": "Dikte edilen metinden “hmm” gibi duraksama seslerini dikte diline göre çıkarır." + }, + "sectionDescription": "Her bölmede yazmak yerine konuşun. Bulut modeli seçmediğiniz sürece konuşma bu bilgisayarda tanınır.", + "input": { + "title": "Giriş", + "shortcut": "Kısayol", + "shortcutDescription": "Odaktaki bölmede dikteyi başlatır.", + "changeShortcut": "Değiştir" + }, + "language": { + "title": "Dil" + }, + "cleanup": { + "title": "Temizleme" + }, + "advanced": { + "title": "Gelişmiş" + }, + "setup": { + "downloadingDescription": "Bu sayfadan ayrılabilirsiniz. İndirme bitince dikte çalışır.", + "title": "Dikteyi kur", + "pickModel": "Her bölmede yazmak yerine konuşun. Başlamak için aşağıdan bir konuşma modeli indirin; bu bilgisayardaki modeller sesinizi burada tutar.", + "microphoneMissingTitle": "{{microphone}} bağlı değil", + "microphoneMissingDescription": "Geri gelene kadar dikte sistemin varsayılan mikrofonunu kullanır.", + "changeMicrophone": "Mikrofonu değiştir", + "onTitle": "Dikte açık", + "onLocal": "Herhangi bir bölmede {{shortcut}} tuşuna basın. {{model}} bu bilgisayarda çalışır.", + "onCloud": "Herhangi bir bölmede {{shortcut}} tuşuna basın. {{model}} bulutta çalışır.", + "offTitle": "Dikte kapalı", + "offDescription": "Her bölmede {{shortcut}} ile dikte etmek için açın.", + "savedMicrophone": "Mikrofonunuz" + }, + "quality": { + "unsupported": "Dilinizi desteklemiyor", + "unmeasured": "Ölçülmedi", + "poor": "Zayıf", + "fair": "Orta", + "good": "İyi", + "veryGood": "Çok iyi", + "excellent": "Mükemmel", + "characterErrorRate": "%{{percent}} karakter hata oranı", + "wordErrorRate": "%{{percent}} kelime hata oranı", + "librispeech": "Test: LibriSpeech (sesli okuma), {{clips}} klip, ölçüm tarihi {{date}}.", + "fleurs": "Test: FLEURS (sesli okuma), {{clips}} klip, ölçüm tarihi {{date}}.", + "bench": "Orca'nın Apple silikonlu bir Mac'teki kıyaslaması.", + "label": "Kalite: {{word}}" + }, + "models": { + "retired": "Artık sunulmuyor. Yer açmak için silin.", + "sendsToOpenAi": "Sesinizi OpenAI'ye gönderir.", + "delete": "Sil", + "cancelDownload": "İndirmeyi iptal et", + "inUse": "Kullanımda", + "use": "Kullan", + "setUpCloud": "Kur", + "download": "İndir", + "cloud": "Bulut", + "description": "Kalite {{language}} için değerlendirildi. Testi görmek için bir puanın üzerine gelin.", + "where": "Modellerin çalıştığı yer", + "local": "Bu bilgisayarda", + "cloudDescription": "Bulut modelleri sesinizi sağlayıcıya gönderir. Bu bilgisayardaki modeller sesi burada tutar.", + "sendsToCompatible": "Sesinizi kurduğunuz hizmete gönderir." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/zh-Hant.json b/src/renderer/src/i18n/locales/zh-Hant.json index 6c6ffa4853d..11bd936a4f6 100644 --- a/src/renderer/src/i18n/locales/zh-Hant.json +++ b/src/renderer/src/i18n/locales/zh-Hant.json @@ -921,19 +921,6 @@ "missingHost": "代理 URL 必須包含主機。" } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "為 25 種歐洲語言提供最高準確度。支援標點符號、大小寫及詞級時間戳記。", - "parakeetV2": "僅限英文。準確度與 v3 相近,速度更快。支援標點符號及大小寫。", - "zipformerStreamingEnRetired": "已不再提供:它會遺漏句子開頭。刪除它以釋放空間。", - "whisperTiny": "支援 90 多種語言。準確度低於 Parakeet,但語言涵蓋範圍最廣。", - "senseVoice": "支援中文、英文、日文、韓文及粵語,並自動偵測語言。", - "gptTranscribe": "準確度高的雲端轉錄。需要 OpenAI API 金鑰。", - "whisperLargeV3Turbo": "日語、韓語和中文準確度最高,支援 90 多種語言。速度比 Parakeet 慢。", - "retiredForEngine": "已不再提供:語音引擎已無法執行此模型。刪除它以釋放空間。", - "nemotron": "支援英語、西班牙語、德語等多種語言,可自動辨識語言。" - } - }, "diagnostics": { "errors": { "creatingDisabled": "已停用建立檢視檔案", @@ -8767,7 +8754,6 @@ "7807c11c4d": "一般", "6855b0f77d": "完成核心工作流程,讓 Orca 有效協助代理程式並行工作。", "6d119427ef": "入門清單", - "eb1176a14e": "使用裝置上模型進行本機語音到文字聽寫。", "5063bb47a5": "語音", "7118953f14": "使代理程式能夠控制您電腦上的任何應用程式。", "c9841721cb": "電腦控制", @@ -9282,7 +9268,6 @@ "VoicePane": { "6fa734ed95": "刪除 {{value0}}", "68de13f72c": "刪除模型失敗。", - "1ba81c0ff0": "推薦", "cfde55c7b0": "下載模型失敗。", "43fd4f454b": "語音模型", "ba4a900d1d": "聽寫模式", @@ -9293,10 +9278,6 @@ "506df81ba6": "OpenAI API 金鑰已儲存", "91980ce124": "{{value0}} MB", "61a16c8141": "正在解壓縮...", - "8f4d2a51d7": "離線", - "d504ab05f0": "串流", - "fbe5990716": "選擇模型", - "e24f7d43d2": "選擇語音模型。本機模型離線執行;雲端模型需要 API 金鑰。", "174da92062": "按住", "118b3c2dee": "切換", "ad5d036ecc": "無法請求麥克風權限。未啟用語音聽寫。", @@ -9304,7 +9285,6 @@ "1eac933202": "已開啟 macOS 的「隱私權與安全性」。授予存取權限後再次啟用聽寫。", "cd9fe37556": "已授予麥克風權限", "openAiKeyName": "您的 OpenAI 轉錄金鑰", - "dictationShortcutDescription": "按 {{shortcut}} 可將文字聽寫到目前取得焦點的任意窗格中。", "dictationModeAuto": "自動", "dictationModesWithAutoDescription": "自動:按住 {{shortcut}} 聽寫,放開即停止;輕按則開始或停止。按住:按住 {{shortcut}} 期間進行聽寫。切換:按一次 {{shortcut}} 開始,再按一次停止。", "speechGpuLabel": "使用 GPU", @@ -20388,6 +20368,67 @@ "fillerWords": { "title": "移除語氣詞", "description": "依聽寫語言,從聽寫文字中移除「嗯」「呃」之類的語氣詞。" + }, + "sectionDescription": "在任何面板中用說話取代打字。除非你選擇雲端模型,否則語音都在這台電腦上辨識。", + "input": { + "title": "輸入", + "shortcut": "快捷鍵", + "shortcutDescription": "在目前聚焦的面板中開始聽寫。", + "changeShortcut": "變更" + }, + "language": { + "title": "語言" + }, + "cleanup": { + "title": "清理" + }, + "advanced": { + "title": "進階" + }, + "setup": { + "downloadingDescription": "你可以離開此頁面。下載完成後即可使用聽寫。", + "title": "設定聽寫", + "pickModel": "在任何面板中用說話取代打字。先在下方下載一個語音模型;本機模型會把你的音訊留在這台電腦上。", + "microphoneMissingTitle": "{{microphone}} 未連接", + "microphoneMissingDescription": "在它重新連接前,聽寫會使用系統預設麥克風。", + "changeMicrophone": "更換麥克風", + "onTitle": "聽寫已開啟", + "onLocal": "在任何面板中按 {{shortcut}}。{{model}} 在這台電腦上執行。", + "onCloud": "在任何面板中按 {{shortcut}}。{{model}} 在雲端執行。", + "offTitle": "聽寫已關閉", + "offDescription": "開啟後,可在任何面板中按 {{shortcut}} 聽寫。", + "savedMicrophone": "你的麥克風" + }, + "quality": { + "unsupported": "不支援你的語言", + "unmeasured": "未測量", + "poor": "差", + "fair": "普通", + "good": "良好", + "veryGood": "很好", + "excellent": "優秀", + "characterErrorRate": "字元錯誤率 {{percent}}%", + "wordErrorRate": "詞錯誤率 {{percent}}%", + "librispeech": "測試:LibriSpeech(朗讀語音),{{clips}} 段音訊,測於 {{date}}。", + "fleurs": "測試:FLEURS(朗讀語音),{{clips}} 段音訊,測於 {{date}}。", + "bench": "Orca 在 Apple 晶片 Mac 上的基準測試。", + "label": "品質:{{word}}" + }, + "models": { + "retired": "已不再提供。刪除即可釋放空間。", + "sendsToOpenAi": "會把你的音訊傳送給 OpenAI。", + "delete": "刪除", + "cancelDownload": "取消下載", + "inUse": "使用中", + "use": "使用", + "setUpCloud": "設定", + "download": "下載", + "cloud": "雲端", + "description": "品質依{{language}}評定。將指標停在評分上可查看所用測試。", + "where": "模型執行位置", + "local": "這台電腦", + "cloudDescription": "雲端模型會把你的音訊傳送給服務商。本機模型會把音訊留在這台電腦上。", + "sendsToCompatible": "會把你的音訊傳送到你設定的服務。" } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index c410a72c92d..31c5adba076 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -497,19 +497,6 @@ "missingHost": "代理 URL 必须包含主机。" } }, - "voice": { - "modelDescriptions": { - "parakeetV3": "为 25 种欧洲语言提供最高准确度。支持标点、大小写和词级时间戳。", - "parakeetV2": "仅限英语。准确度与 v3 相近,速度更快。支持标点和大小写。", - "zipformerStreamingEnRetired": "已不再提供:它会丢失句子开头。删除它以释放空间。", - "whisperTiny": "支持 90 多种语言。准确度低于 Parakeet,但语言覆盖最广。", - "senseVoice": "支持中文、英语、日语、韩语和粤语,并自动检测语言。", - "gptTranscribe": "准确度高的云端转录。需要 OpenAI API 密钥。", - "whisperLargeV3Turbo": "日语、韩语和中文准确度最高,支持 90 多种语言。速度比 Parakeet 慢。", - "retiredForEngine": "已不再提供:语音引擎已无法运行此模型。删除它以释放空间。", - "nemotron": "支持英语、西班牙语、德语等多种语言,可自动识别语言。" - } - }, "diagnostics": { "errors": { "creatingDisabled": "已禁用创建查看文件", @@ -8236,7 +8223,6 @@ "7807c11c4d": "通用", "6855b0f77d": "完成使 Orca 对于并行智能体工作有用的核心工作流程。", "6d119427ef": "入门清单", - "eb1176a14e": "使用设备上模型进行本地语音到文本听写。", "5063bb47a5": "语音", "7118953f14": "使智能体能够控制您计算机上的任何应用程序。", "c9841721cb": "计算机控制", @@ -8746,7 +8732,6 @@ }, "VoicePane": { "68de13f72c": "删除模型失败。", - "1ba81c0ff0": "推荐", "cfde55c7b0": "下载模型失败。", "43fd4f454b": "语音模型", "ba4a900d1d": "听写模式", @@ -8757,10 +8742,6 @@ "506df81ba6": "OpenAI API 密钥已保存", "91980ce124": "{{value0}} MB", "61a16c8141": "正在提取...", - "8f4d2a51d7": "离线", - "d504ab05f0": "流媒体", - "fbe5990716": "选择型号", - "e24f7d43d2": "选择语音模型,本地模型离线运行;云模型需要 API 密钥。", "174da92062": "按住", "118b3c2dee": "切换", "ad5d036ecc": "无法请求麦克风权限。未启用语音听写。", @@ -8769,7 +8750,6 @@ "cd9fe37556": "已授予麦克风权限", "6fa734ed95": "删除 {{value0}}", "openAiKeyName": "您的 OpenAI 转录密钥", - "dictationShortcutDescription": "按 {{shortcut}} 可将文本听写到当前聚焦的任意窗格中。", "dictationModeAuto": "自动", "dictationModesWithAutoDescription": "自动:按住 {{shortcut}} 听写,松开即停止;轻按则开始或停止。按住:按住 {{shortcut}} 期间进行听写。切换:按一次 {{shortcut}} 开始,再按一次停止。", "speechGpuLabel": "使用 GPU", @@ -19985,6 +19965,67 @@ "fillerWords": { "title": "移除语气词", "description": "按听写语言,从听写文本中去掉“嗯”“呃”之类的语气词。" + }, + "sectionDescription": "在任意面板中用说话代替打字。除非你选择云端模型,否则语音都在这台电脑上识别。", + "input": { + "title": "输入", + "shortcut": "快捷键", + "shortcutDescription": "在当前聚焦的面板中开始听写。", + "changeShortcut": "更改" + }, + "language": { + "title": "语言" + }, + "cleanup": { + "title": "清理" + }, + "advanced": { + "title": "高级" + }, + "setup": { + "downloadingDescription": "你可以离开此页面。下载完成后即可使用听写。", + "title": "设置听写", + "pickModel": "在任意面板中用说话代替打字。先在下方下载一个语音模型;本机模型会把你的音频留在这台电脑上。", + "microphoneMissingTitle": "{{microphone}} 未连接", + "microphoneMissingDescription": "在它重新连接前,听写会使用系统默认麦克风。", + "changeMicrophone": "更换麦克风", + "onTitle": "听写已开启", + "onLocal": "在任意面板中按 {{shortcut}}。{{model}} 在这台电脑上运行。", + "onCloud": "在任意面板中按 {{shortcut}}。{{model}} 在云端运行。", + "offTitle": "听写已关闭", + "offDescription": "开启后,可在任意面板中按 {{shortcut}} 听写。", + "savedMicrophone": "你的麦克风" + }, + "quality": { + "unsupported": "不支持你的语言", + "unmeasured": "未测量", + "poor": "差", + "fair": "一般", + "good": "良好", + "veryGood": "很好", + "excellent": "优秀", + "characterErrorRate": "字符错误率 {{percent}}%", + "wordErrorRate": "词错误率 {{percent}}%", + "librispeech": "测试:LibriSpeech(朗读语音),{{clips}} 段音频,测于 {{date}}。", + "fleurs": "测试:FLEURS(朗读语音),{{clips}} 段音频,测于 {{date}}。", + "bench": "Orca 在 Apple 芯片 Mac 上的基准测试。", + "label": "质量:{{word}}" + }, + "models": { + "retired": "已不再提供。删除即可释放空间。", + "sendsToOpenAi": "会把你的音频发送给 OpenAI。", + "delete": "删除", + "cancelDownload": "取消下载", + "inUse": "使用中", + "use": "使用", + "setUpCloud": "设置", + "download": "下载", + "cloud": "云端", + "description": "质量按{{language}}评定。将指针悬停在评分上可查看所用测试。", + "where": "模型运行位置", + "local": "这台电脑", + "cloudDescription": "云端模型会把你的音频发送给服务商。本机模型会把音频留在这台电脑上。", + "sendsToCompatible": "会把你的音频发送到你设置的服务。" } }, "repositoryHooks": { diff --git a/src/shared/speech-types.ts b/src/shared/speech-types.ts index bd50b1dc7e4..bedd641f99f 100644 --- a/src/shared/speech-types.ts +++ b/src/shared/speech-types.ts @@ -29,7 +29,6 @@ export type SpeechModelErrorRate = { export type SpeechModelManifest = { id: string label: string - description: string type: SpeechModelType provider: SpeechModelProvider /** The text pipeline's hint: one language, a bilingual pair such as 'zh-en', or 'multilingual'. */ From 4e42cc2a3f8992c8aa9a09d5091dc00ceac3660a Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:49:37 +0000 Subject: [PATCH 11/28] fix(speech): keep a slow GPU model load on the GPU while it still makes progress (#40472) The GPU stall watch gave the whole model load one 25 s budget, so a cold disk or busy machine restarted a healthy load on the CPU and could miss the 60 s start limit. The speech process now reports each load step, and the watch restarts on it; a step that hangs for 25 s still moves to the CPU. --- src/main/speech/stt-recognizer.ts | 2 ++ .../speech/stt-transcribe-cpp-recognizer.ts | 5 ++++ .../speech/stt-worker-cpu-fallback.test.ts | 27 ++++++++++++++++++- src/main/speech/stt-worker-cpu-fallback.ts | 9 +++++-- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/speech/stt-recognizer.ts b/src/main/speech/stt-recognizer.ts index 2b91dd8d43f..1998151130c 100644 --- a/src/main/speech/stt-recognizer.ts +++ b/src/main/speech/stt-recognizer.ts @@ -15,6 +15,8 @@ export type SttRecognizerEvent = | { type: 'chunk'; streamId: string; text: string; samples: number } /** Pause-cut streams only: speech was heard that no chunk has committed yet. */ | { type: 'speech-pending'; streamId: string; pending: boolean } + /** A load step finished, so a slow load is told apart from a hung GPU. */ + | { type: 'loading' } export type SttRecognizerPost = (event: SttRecognizerEvent) => void diff --git a/src/main/speech/stt-transcribe-cpp-recognizer.ts b/src/main/speech/stt-transcribe-cpp-recognizer.ts index e8c62efda14..9d5b51f0852 100644 --- a/src/main/speech/stt-transcribe-cpp-recognizer.ts +++ b/src/main/speech/stt-transcribe-cpp-recognizer.ts @@ -91,7 +91,12 @@ export async function createTranscribeCppRecognizer( if (!gguf) { throw new Error(`No .gguf model file in: ${init.files.join(', ')}`) } + // Device setup (Metal shader library, Vulkan drivers) and reading the weights each report done, + // so a cold disk or a busy machine isn't taken for a GPU that hung. + await transcribe.initialize() + post({ type: 'loading' }) const model = await transcribe.TranscribeModel.load(join(init.modelDir, gguf)) + post({ type: 'loading' }) // The child runs one call at a time, so every stream can share one session. const session = model.createSession({ nThreads: performanceCoreThreads() }) const { languages } = model.capabilities diff --git a/src/main/speech/stt-worker-cpu-fallback.test.ts b/src/main/speech/stt-worker-cpu-fallback.test.ts index 582acc144d1..ffc7a6cb42c 100644 --- a/src/main/speech/stt-worker-cpu-fallback.test.ts +++ b/src/main/speech/stt-worker-cpu-fallback.test.ts @@ -1,5 +1,5 @@ import { EventEmitter } from 'node:events' -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { spawnSttWorkerWithCpuFallback } from './stt-worker-cpu-fallback' class FakeChild extends EventEmitter { @@ -115,6 +115,31 @@ describe('GPU speech process falling back to the CPU', () => { expect(marks()).toBe(0) }) + it('keeps a slow GPU load that still finishes its steps, and moves a silent one to the CPU', () => { + vi.useFakeTimers() + try { + const { children, events } = setup() + const [gpu] = children + vi.advanceTimersByTime(20_000) + gpu.emit('message', { type: 'loading' }) + vi.advanceTimersByTime(20_000) + gpu.emit('message', { type: 'loading' }) + vi.advanceTimersByTime(20_000) + gpu.emit('message', { type: 'ready' }) + expect(children).toHaveLength(1) + expect(events).toEqual([{ type: 'ready' }]) + + const silent = setup() + vi.advanceTimersByTime(20_000) + silent.children[0].emit('message', { type: 'loading' }) + vi.advanceTimersByTime(25_000) + expect(silent.children).toHaveLength(2) + expect(silent.children[1].cpuOnly).toBe(true) + } finally { + vi.useRealTimers() + } + }) + it('fails the worker when the CPU replacement cannot load the model mid-dictation', () => { const { worker, children } = setup() const exits: number[] = [] diff --git a/src/main/speech/stt-worker-cpu-fallback.ts b/src/main/speech/stt-worker-cpu-fallback.ts index 63bdeba05a4..d86c3d1b15b 100644 --- a/src/main/speech/stt-worker-cpu-fallback.ts +++ b/src/main/speech/stt-worker-cpu-fallback.ts @@ -17,7 +17,7 @@ type ChildMessage = | { type: 'chunk'; streamId: string; text: string; samples: number } | { type: 'speech-pending'; streamId: string; pending: boolean } | { - type: 'ready' | 'stopped' | 'error' | 'partial' | 'final' + type: 'ready' | 'loading' | 'stopped' | 'error' | 'partial' | 'final' streamId?: string text?: string error?: string @@ -33,7 +33,8 @@ type StreamBacklog = { pending: 'stop' | 'discard' | null } -// Well under the session's 60 s start and stop timeouts, so a CPU replay still fits; covers first-run shader compiles. +// Per load step or decode. Well under the session's 60 s start and stop timeouts, so a CPU replay still fits; +// covers first-run shader compiles. const GPU_STALL_TIMEOUT_MS = 25_000 /** @@ -132,6 +133,10 @@ class SttCpuFallbackWorker extends EventEmitter implements SttWorkerProcess { this.emit('exit', 1) return } + if (message.type === 'loading') { + this.watchForStall(true) + return + } if (message.type === 'chunk') { const { streamId, text, samples } = message const stream = this.streams.get(streamId) From ab9f29c1da8e6e08ac145231d504d266bfa97442 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:57:55 -0800 Subject: [PATCH 12/28] Fork Codex replies after rewinding a chat (#40140) * Fork Codex replies from the current history after rewind * Strengthen Codex fork preparation failure coverage * Preserve Codex fork compatibility and original chat guidance * Explain when Codex cannot start for a fork --- ...le-structured-agent-session-launch.test.ts | 28 ++ .../mobile-structured-agent-session-launch.ts | 6 +- .../codex/codex-conversation-copy.test.ts | 344 +++++++++++-- src/main/codex/codex-conversation-copy.ts | 459 +++++++++--------- src/main/codex/codex-fork-history.ts | 100 ++++ .../agent-conversation-copy-error.ts | 2 + .../structured-agent-session-fork-source.ts | 24 +- .../structured-agent-session-fork.ts | 2 +- ...ed-agent-session-precommit-refusal.test.ts | 61 +++ 9 files changed, 754 insertions(+), 272 deletions(-) create mode 100644 src/main/codex/codex-fork-history.ts diff --git a/mobile/src/session/mobile-structured-agent-session-launch.test.ts b/mobile/src/session/mobile-structured-agent-session-launch.test.ts index b5b62c474c0..457d3fb3c2e 100644 --- a/mobile/src/session/mobile-structured-agent-session-launch.test.ts +++ b/mobile/src/session/mobile-structured-agent-session-launch.test.ts @@ -57,3 +57,31 @@ it('settles a proven missing-original fork refusal but keeps a lost earlier atte (await createMobileStructuredAgentSession(lost, 'workspace-1', 'opencode', forkFrom)).kind ).toBe('unknown') }) + +it('settles a proven fork read refusal without settling a prior lost copy', async () => { + const refusal = { + ok: true, + result: { + ok: false, + refusal: { + code: 'agent_session_identity_required', + details: { reason: 'transcriptUnreadable' }, + createAttemptRefusedBeforeCommit: true, + message: 'Codex could not read the saved chat.' + } + } + } + const client = clientReturning({ ok: true, result: { supported: true } }, refusal) + const forkFrom = { sessionId: 'source-chat', throughItemId: 'codex:source:selected:1' } + expect( + (await createMobileStructuredAgentSession(client, 'workspace-1', 'codex', forkFrom)).kind + ).toBe('failed') + const lost = clientReturning({ ok: true, result: { supported: true } }, refusal) + lost.sendRequest + .mockImplementationOnce(async () => ({ ok: true, result: { supported: true } })) + .mockRejectedValueOnce(markRpcDeliveryUnknown(new Error('copy reply lost'))) + .mockResolvedValueOnce(refusal) + expect( + (await createMobileStructuredAgentSession(lost, 'workspace-1', 'codex', forkFrom)).kind + ).toBe('unknown') +}) diff --git a/mobile/src/session/mobile-structured-agent-session-launch.ts b/mobile/src/session/mobile-structured-agent-session-launch.ts index 55dbc1ebef0..d7cb85e2a31 100644 --- a/mobile/src/session/mobile-structured-agent-session-launch.ts +++ b/mobile/src/session/mobile-structured-agent-session-launch.ts @@ -7,7 +7,8 @@ import type { } from '../../../src/shared/agent-session/agent-session-wire' import { isDefinitiveAgentSessionCreateRefusal, - isAgentSessionForkSourceAdmissionRefusal + isAgentSessionForkSourceAdmissionRefusal, + isAgentSessionCreateAdmissionRefusal } from '../../../src/shared/agent-session/failures/agent-session-definitive-refusal' import { createStructuredAgentSessionId, @@ -187,7 +188,8 @@ export async function createMobileStructuredAgentSession( forkFrom && !priorCreateOutcomeUnknown && result.refusal.createAttemptRefusedBeforeCommit === true && - isAgentSessionForkSourceAdmissionRefusal(result.refusal) + (isAgentSessionForkSourceAdmissionRefusal(result.refusal) || + isAgentSessionCreateAdmissionRefusal(result.refusal)) ) { return { kind: 'failed', diff --git a/src/main/codex/codex-conversation-copy.test.ts b/src/main/codex/codex-conversation-copy.test.ts index bafac8ca255..17938242104 100644 --- a/src/main/codex/codex-conversation-copy.test.ts +++ b/src/main/codex/codex-conversation-copy.test.ts @@ -10,15 +10,24 @@ import { join } from 'node:path' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key' import { agentSessionRecordFixture } from '../native-chat/agent-session-record-test-fixture' -import { copyCodexConversation, codexForkHistory } from './codex-conversation-copy' +import { copyCodexConversation } from './codex-conversation-copy' +import { codexForkHistory } from './codex-fork-history' +import { CodexAppServerTimeoutError } from './app-server/codex-app-server-session' import type { CodexAppServerRpc } from './app-server/codex-app-server-session' -const state = vi.hoisted(() => ({ - home: '', - paths: new Map(), - request: vi.fn(), - initialize: vi.fn() -})) +const state = vi.hoisted(() => { + const session: { + run?: (body: (rpc: CodexAppServerRpc) => Promise) => Promise + transport?: 'native' | 'unsupported-read' + } = {} + return { + home: '', + paths: new Map(), + request: vi.fn(), + initialize: vi.fn(), + session + } +}) vi.mock('../native-chat/codex-local-history', async (importOriginal) => { const original = await importOriginal() return { ...original, readCodexLocalHistory: vi.fn(original.readCodexLocalHistory) } @@ -30,19 +39,58 @@ vi.mock('../native-chat/codex-rollout-history-stream', async (importOriginal) => vi.mock('../native-chat/session-file-resolver', () => ({ resolveSessionFilePath: async (_agent: string, id: string) => state.paths.get(id) ?? null })) -vi.mock('./app-server/codex-app-server-session', async (importOriginal) => ({ - ...(await importOriginal()), - runCodexAppServerSession: async ( - launch: unknown, - body: (rpc: CodexAppServerRpc) => Promise - ) => { - state.initialize(launch) - return body({ request: state.request, notify: () => {} }) +vi.mock('./app-server/codex-app-server-session', async (importOriginal) => { + const original = await importOriginal() + return { + ...original, + runCodexAppServerSession: async ( + launch: AppServerSession.CodexAppServerInvocation, + body: (rpc: CodexAppServerRpc) => Promise + ) => { + state.initialize(launch) + if (state.session.transport) { + const server = String.raw` + let experimental = false + require('node:readline').createInterface({ input: process.stdin }).on('line', line => { + const message = JSON.parse(line) + if (typeof message.id !== 'number') return + if (message.method === 'initialize') { + experimental = message.params.capabilities?.experimentalApi === true + } + const error = message.method === 'thread/read' && process.argv[1] === 'unsupported-read' + ? { code: -32601, message: 'thread/read is not supported yet' } + : message.method === 'thread/turns/list' && !experimental + ? { code: -32600, message: 'thread/turns/list requires experimentalApi capability' } + : null + process.stdout.write(JSON.stringify({ id: message.id, ...(error ? { error } : { result: {} }) }) + '\n') + }) + ` + return original.runCodexAppServerSession( + { + ...launch, + command: process.execPath, + args: ['-e', server, state.session.transport], + cliPath: null + }, + (rpc) => + body({ + ...rpc, + request: async (method, params) => { + await rpc.request(method, params) + return state.request(method, params) + } + }) + ) + } + return state.session.run + ? state.session.run(body) + : body({ request: state.request, notify: () => {} }) + } } -})) +}) -const SOURCE = '00000000-0000-4000-8000-000000000001' -const CHILD = '00000000-0000-4000-8000-000000000002' +const SOURCE = '01a1bdd2-6d33-7a21-9a1d-c14f0117bc8c' +const CHILD = '01a1bde0-5ded-7f03-a1bf-3dae1613d8c7' let root: string beforeEach(async () => { @@ -50,6 +98,8 @@ beforeEach(async () => { state.home = root state.paths.clear() state.request.mockReset() + state.session.run = undefined + state.session.transport = undefined state.initialize.mockReset() vi.mocked(readCodexLocalHistory).mockClear() vi.mocked(readCodexRolloutBytes).mockClear() @@ -85,7 +135,12 @@ async function history(id: string, records: object[]) { state.paths.set(id, path) return path } -function copy(selected = `${SOURCE}:a1`, settled = true, turnId?: string) { +function copy( + selected = `${SOURCE}:a1`, + settled = true, + turnId?: string, + options: { signal?: AbortSignal; selectedItemId?: string } = {} +) { return copyCodexConversation( { record: agentSessionRecordFixture({ @@ -102,15 +157,18 @@ function copy(selected = `${SOURCE}:a1`, settled = true, turnId?: string) { providerHandle: null }, providerSessionId: SOURCE, + signal: options.signal, title: 'Source fork', accountHome: { variable: 'CODEX_HOME', path: root }, checkpoint: { - selectedItemId: agentJournalItemKey({ - provider: 'legacy', - agent: 'codex', - sessionId: SOURCE, - recordId: selected - }), + selectedItemId: + options.selectedItemId ?? + agentJournalItemKey({ + provider: 'legacy', + agent: 'codex', + sessionId: SOURCE, + recordId: selected + }), settled, ...(turnId ? { turnId } : {}) } @@ -150,10 +208,12 @@ async function childPrefix(lines?: number) { return path } -it('uses a persisted terminal canonical boundary for an idle imported latest reply with no live owner', async () => { +it('keeps omitted-mode compatibility for a persisted terminal imported reply', async () => { await history(SOURCE, turn('canonical', 1)) await childPrefix() - state.request.mockResolvedValue({ thread: { id: CHILD } }) + state.request.mockImplementation(async (method) => ({ + thread: { id: method === 'thread/read' ? SOURCE : CHILD } + })) await expect(copy(undefined, false)).resolves.toEqual({ sessionId: CHILD, coverage: { unit: 'codex-turn', ids: ['canonical'] } @@ -168,9 +228,11 @@ it('uses beforeTurnId only for the immediate next effective root and opts into t await history(SOURCE, [...turn(undefined, 1), ...turn('canonical-next', 2)]) await childPrefix(3) state.request.mockImplementation(async (method) => - method === 'thread/fork' - ? { thread: { id: CHILD } } - : { data: [{ id: 'legacy-first' }, { id: 'canonical-next' }], nextCursor: null } + method === 'thread/read' + ? { thread: { id: SOURCE, historyMode: 'paginated' } } + : method === 'thread/fork' + ? { thread: { id: CHILD } } + : { data: [{ id: 'legacy-first' }, { id: 'canonical-next' }], nextCursor: null } ) await expect(copy()).resolves.toEqual({ sessionId: CHILD, @@ -196,6 +258,9 @@ it('forks and reverts only the child for legacy, legacy, canonical, counting aga state.paths.set(CHILD, path) let reverted = false state.request.mockImplementation(async (method) => { + if (method === 'thread/read') { + return { thread: { id: SOURCE, historyMode: 'paginated' } } + } if (method === 'thread/fork') { return { thread: { id: CHILD, historyMode: 'paginated' } } } @@ -249,7 +314,8 @@ it('uses exact physical identities across repeated prompts rather than a retaine expect(result.selected.id).toBe('second') }) -it('freezes all-unrecorded root IDs on the source, excluding child replacements and later source growth', async () => { +it('keeps method-not-found compatibility with frozen unrecorded IDs despite source growth and child replacements', async () => { + state.session.transport = 'unsupported-read' await history(SOURCE, [...turn(undefined, 1), ...turn(undefined, 2), ...turn(undefined, 3)]) let reverted = false state.request.mockImplementation(async (method, params) => { @@ -290,7 +356,7 @@ it('freezes all-unrecorded root IDs on the source, excluding child replacements }) }) -it('certifies recorded turns in a standalone copied rollout with no physical history base', async () => { +it('keeps legacy-mode compatibility for recorded turns in a standalone copied rollout', async () => { const rows = await recordedCodexRollout('c') const source = rows.find((row) => row.type === 'session_meta')! const started = rows.find( @@ -312,9 +378,221 @@ it('certifies recorded turns in a standalone copied rollout with no physical his .join('\n') await writeFile(path, `${serialized}\n`) } - state.request.mockResolvedValue({ thread: { id: CHILD } }) + state.request.mockImplementation(async (method) => ({ + thread: { id: method === 'thread/read' ? SOURCE : CHILD, historyMode: 'legacy' } + })) await expect(copy(undefined, true, String(started.payload.turn_id))).resolves.toEqual({ sessionId: CHILD, coverage: { unit: 'codex-turn', ids: ['01a1b0d7-ac1d-7e00-85b8-806a4d8d494b'] } }) }) + +it('forks the current paged native prefix after rewind without source or child rollout authority', async () => { + state.session.transport = 'native' + await history(SOURCE, [ + ...turn('01a1bdd2-6d5e-7960-8fbb-30c962a5eeaa', 1), + ...turn('01a1bdd3-1293-7bc2-b750-017ca47738f9', 2) + ]) + const selected = '01a1bdd4-2977-7f81-8495-eb160b46419a' + state.request.mockImplementation(async (method, params) => { + if (method === 'thread/read') { + return { thread: { id: SOURCE, historyMode: 'paginated', status: { type: 'notLoaded' } } } + } + if (method === 'thread/fork') { + return { thread: { id: CHILD, historyMode: 'paginated' } } + } + if (!params?.cursor) { + return { + data: [{ id: '01a1bdd2-6d5e-7960-8fbb-30c962a5eeaa', status: 'completed' }], + nextCursor: 'page-2' + } + } + return { + data: [ + { id: '01a1bdd2-b7a1-7c63-8b19-cc361c2baefb', status: 'completed' }, + { id: selected, status: 'completed' }, + ...(params.threadId === CHILD + ? [{ id: '01a1bde0-5e1e-7ad0-ab18-f9ed07fd00e7', status: 'completed' }] + : []) + ], + nextCursor: null + } + }) + await expect( + copy(undefined, false, selected, { selectedItemId: `codex:${SOURCE}:${selected}:1` }) + ).resolves.toEqual({ + sessionId: '01a1bde0-5ded-7f03-a1bf-3dae1613d8c7', + coverage: { + unit: 'codex-turn', + ids: [ + '01a1bdd2-6d5e-7960-8fbb-30c962a5eeaa', + '01a1bdd2-b7a1-7c63-8b19-cc361c2baefb', + '01a1bdd4-2977-7f81-8495-eb160b46419a' + ] + } + }) + expect(state.request).toHaveBeenCalledWith('thread/fork', { + threadId: SOURCE, + lastTurnId: '01a1bdd4-2977-7f81-8495-eb160b46419a', + excludeTurns: true, + cwd: root + }) +}) + +it('bridges a saved physical reply with an absent command candidate without restoring a removed native turn', async () => { + await history(SOURCE, [...turn('kept', 1), ...turn('removed', 2)]) + state.request.mockImplementation(async (method) => { + if (method === 'thread/read') { + return { thread: { id: SOURCE, historyMode: 'paginated' } } + } + if (method === 'thread/fork') { + return { thread: { id: CHILD } } + } + return { data: [{ id: 'kept', status: 'completed' }], nextCursor: null } + }) + await expect(copy(undefined, true, 'orca-command')).resolves.toEqual({ + sessionId: CHILD, + coverage: { unit: 'codex-turn', ids: ['kept'] } + }) + await expect(copy(`${SOURCE}:a2`, true, 'orca-command')).rejects.toMatchObject({ + refusal: { + code: 'agent_session_operation_invalid', + details: { reason: 'savedConversationMissing' }, + createAttemptRefusedBeforeCommit: true + } + }) + await expect(copy(undefined, true)).resolves.toEqual({ + sessionId: CHILD, + coverage: { unit: 'codex-turn', ids: ['kept'] } + }) + await expect( + copy(undefined, true, 'removed', { selectedItemId: `codex:${SOURCE}:removed:1` }) + ).rejects.toMatchObject({ + refusal: { + details: { reason: 'savedConversationMissing' }, + createAttemptRefusedBeforeCommit: true + } + }) +}) + +it('keeps read faults in preparation and a contradictory copied prefix uncertain', async () => { + const selected = '01a1bdd4-2977-7f81-8495-eb160b46419a' + await history(SOURCE, turn(selected, 1)) + await childPrefix() + const prepare = () => copy(undefined, true, selected) + state.request.mockRejectedValue(new Error('read failed')) + await expect(prepare()).rejects.toMatchObject({ + refusal: { + code: 'agent_session_identity_required', + details: { reason: 'transcriptUnreadable' }, + createAttemptRefusedBeforeCommit: true + } + }) + state.request.mockResolvedValue({ thread: { id: CHILD, historyMode: 'paginated' } }) + await expect(prepare()).rejects.toMatchObject({ + refusal: { + code: 'agent_session_identity_required', + details: { reason: 'transcriptUnreadable' }, + createAttemptRefusedBeforeCommit: true + } + }) + state.request.mockImplementation(async (method, params) => { + if (method === 'thread/read') { + return { thread: { id: SOURCE, historyMode: 'paginated' } } + } + if (method === 'thread/fork') { + return { thread: { id: CHILD } } + } + return { + data: (params?.threadId === SOURCE + ? ['01a1bdd2-6d5e-7960-8fbb-30c962a5eeaa', selected] + : [selected, '01a1bdd2-6d5e-7960-8fbb-30c962a5eeaa'] + ).map((id) => ({ + id + })), + nextCursor: null + } + }) + await expect(prepare()).rejects.not.toHaveProperty('refusal') +}) + +it('refuses invalid and repeated source cursors before copying', async () => { + state.request.mockImplementation(async (method) => + method === 'thread/read' + ? { thread: { id: SOURCE, historyMode: 'paginated' } } + : { data: [{ id: 'first' }], nextCursor: 'same-page' } + ) + await expect(copy(undefined, true, 'missing')).rejects.toMatchObject({ + refusal: { details: { reason: 'transcriptUnreadable' }, createAttemptRefusedBeforeCommit: true } + }) + state.request.mockImplementation(async (method) => + method === 'thread/read' + ? { thread: { id: SOURCE, historyMode: 'paginated' } } + : { data: [], nextCursor: 17 } + ) + await expect(copy(undefined, true, 'missing')).rejects.toMatchObject({ + refusal: { details: { reason: 'transcriptUnreadable' }, createAttemptRefusedBeforeCommit: true } + }) +}) + +it('cannot dispatch an abandoned preparation after its lifetime ends, but keeps an invoked fork uncertain', async () => { + const listed = Promise.withResolvers() + const release = Promise.withResolvers() + const expired = Promise.withResolvers() + const lifetime = new AbortController() + let abandoned: Promise | undefined + let childCreated = false + state.request.mockImplementation(async (method) => { + if (method === 'thread/read') { + return { thread: { id: SOURCE, historyMode: 'paginated' } } + } + if (method === 'thread/fork') { + childCreated = true + return { thread: { id: CHILD } } + } + listed.resolve() + return release.promise + }) + state.session.run = async (body) => { + abandoned = body({ request: state.request, notify: () => {}, signal: lifetime.signal }) + return Promise.race([abandoned, expired.promise]) + } + const preparation = copy(undefined, true, 'selected') + await listed.promise + lifetime.abort(new CodexAppServerTimeoutError('session ended')) + expired.reject(lifetime.signal.reason) + await expect(preparation).rejects.toMatchObject({ + refusal: { createAttemptRefusedBeforeCommit: true } + }) + release.resolve({ data: [{ id: 'selected' }], nextCursor: null }) + await expect(abandoned).rejects.toBe(lifetime.signal.reason) + expect(childCreated).toBe(false) + const forked = Promise.withResolvers() + const lost = Promise.withResolvers() + const pending = Promise.withResolvers() + state.session.run = async (body) => + Promise.race([body({ request: state.request, notify: () => {} }), lost.promise]) + state.request.mockImplementation(async (method) => { + if (method === 'thread/read') { + return { thread: { id: SOURCE, historyMode: 'paginated' } } + } + if (method === 'thread/fork') { + forked.resolve() + return pending.promise + } + return { data: [{ id: 'selected' }], nextCursor: null } + }) + const invoked = copy(undefined, true, 'selected') + await forked.promise + const timeout = new CodexAppServerTimeoutError('reply lost') + lost.reject(timeout) + await expect(invoked).rejects.not.toHaveProperty('refusal') + pending.reject(timeout) + state.session.run = undefined + state.session.transport = undefined + const caller = new AbortController() + caller.abort(new Error('caller cancelled')) + await expect(copy(undefined, true, 'selected', { signal: caller.signal })).rejects.toBe( + caller.signal.reason + ) +}) diff --git a/src/main/codex/codex-conversation-copy.ts b/src/main/codex/codex-conversation-copy.ts index f0bc1e43c49..cb3b3a9adee 100644 --- a/src/main/codex/codex-conversation-copy.ts +++ b/src/main/codex/codex-conversation-copy.ts @@ -1,15 +1,19 @@ -import { AgentConversationCopyError } from '../native-chat/agent-conversation-copy-error' +import { TEXT_GENERATION_FAILURE_COPY } from '../../shared/text-generation-failure-copy' +import { codexForkHistory } from './codex-fork-history' +import { + AgentConversationCopyError, + AgentConversationCopyStartError, + agentConversationCopyRefusal +} from '../native-chat/agent-conversation-copy-error' +import { isAgentSessionRefusalError } from '../../shared/agent-session/failures/agent-session-wire-refusals' import { join } from 'node:path' import { requireLegacyAgentSessionAccountHome } from '../../shared/agent-session/lifecycle/agent-session-account-home' import { parseAgentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key' -import { asRecord, parseJsonObject } from '../ai-vault/session-scanner-values' +import { asRecord } from '../ai-vault/session-scanner-values' import type { StructuredAgentSessionAdapter } from '../native-chat/agent-session-wire/providers/structured-agent-session-adapter' -import { createLegacyIdentityTracker } from '../native-chat/agent-session-journal/history-import/journal-legacy-identity' -import { readCodexLocalHistory } from '../native-chat/codex-local-history' import { resolveSessionFilePath } from '../native-chat/session-file-resolver' -import { decodeCodexTranscriptLine } from '../native-chat/transcript-line-decoders-codex' import { - CodexAppServerTimeoutError, + isCodexAppServerUnsupportedError, type CodexAppServerRpc } from './app-server/codex-app-server-session' import { @@ -18,117 +22,28 @@ import { } from './structured/session-lifecycle/codex-provider-session-rename' type CopyInput = Parameters>[0] -type Root = { id?: string; terminal: boolean; prompt: boolean } function unavailable(): never { - throw new AgentConversationCopyError( - 'Codex could not locate an exact saved boundary in this older chat. Fork a response in a newer chat.' - ) -} - -/** Use the importer's physical lineage and identity accounting, including discarded window rows. */ -export async function codexForkHistory(input: { - path: string - roots: string[] - selectedItemId: string - turnId?: string - sessionId: string - signal?: AbortSignal - inheritedOnly?: boolean -}) { - const identity = parseAgentJournalItemKey(input.selectedItemId) - const tracker = createLegacyIdentityTracker({ - transcriptAgent: 'codex', - agent: 'codex', - sessionId: input.sessionId - }) - const roots: Root[] = [] - let selected: Root | undefined - let lineIndex = 0 - let referenceBacked = false - await readCodexLocalHistory({ - filePath: input.path, - roots: input.roots, - visitRecord: ({ record, selected }) => { - if (selected && record?.type === 'session_meta') { - referenceBacked = asRecord(record.payload)?.history_base != null - } - }, - visit: (line, fallbackId, selectedSegment, recordId) => { - if (input.inheritedOnly && referenceBacked && selectedSegment) { - return - } - input.signal?.throwIfAborted() - const tracked = tracker.identify(line, lineIndex++) - const raw = parseJsonObject(line) - const payload = asRecord(raw?.payload) - if ( - raw?.type === 'event_msg' && - payload?.type === 'thread_rolled_back' && - typeof payload.num_turns === 'number' - ) { - roots.splice(Math.max(0, roots.length - payload.num_turns)) - return - } - if (raw?.type === 'event_msg' && payload?.type === 'task_started') { - roots.push({ - id: typeof payload.turn_id === 'string' ? payload.turn_id : undefined, - terminal: false, - prompt: false - }) - } - const message = decodeCodexTranscriptLine(line, fallbackId, { historyImport: true }) - if (message?.role === 'user') { - const previous = roots.at(-1) - if (!previous || (previous.prompt && (!previous.id || previous.terminal))) { - roots.push({ terminal: false, prompt: true }) - } else { - previous.prompt = true - } - } - const root = roots.at(-1) - if ( - root && - raw?.type === 'event_msg' && - (payload?.type === 'task_complete' || payload?.type === 'turn_aborted') && - (!payload.turn_id || payload.turn_id === root.id) - ) { - root.terminal = true - } - const importedMatch = - identity?.provider === 'legacy' && - identity.recordId === - (recordId ?? (tracked.provider === 'legacy' ? tracked.recordId : undefined)) - if ( - root && - ((importedMatch && message?.role === 'assistant') || - (input.turnId && root.id === input.turnId)) - ) { - if (selected && selected !== root) { - return unavailable() - } - selected = root - } - } - }) - const index = selected ? roots.indexOf(selected) : -1 - if (index < 0) { - return unavailable() - } - return { roots, index, selected: roots[index] } + throw new AgentConversationCopyError('Codex could not verify the copied chat.') } async function turns( rpc: CodexAppServerRpc, threadId: string, - count: number -): Promise<{ id: string }[]> { - const all: { id: string }[] = [] + through: number | string +): Promise<{ id: string; status?: unknown }[]> { + const all: { id: string; status?: unknown }[] = [] let cursor: string | null = null const seen = new Set() do { const page = asRecord( - await rpc.request('thread/turns/list', { threadId, cursor, sortDirection: 'asc', limit: 100 }) + await rpc.request('thread/turns/list', { + threadId, + cursor, + sortDirection: 'asc', + limit: 100, + itemsView: 'summary' + }) ) if (!Array.isArray(page?.data)) { return unavailable() @@ -138,7 +53,13 @@ async function turns( if (typeof turn?.id !== 'string') { return unavailable() } - all.push({ id: turn.id }) + all.push({ id: turn.id, status: turn.status }) + if (turn.id === through) { + return all + } + } + if (page.nextCursor != null && (typeof page.nextCursor !== 'string' || !page.nextCursor)) { + return unavailable() } cursor = typeof page.nextCursor === 'string' ? page.nextCursor : null if (cursor && seen.has(cursor)) { @@ -147,7 +68,7 @@ async function turns( if (cursor) { seen.add(cursor) } - } while (cursor && all.length < count) + } while (cursor && (typeof through === 'string' || all.length < through)) return all } @@ -155,140 +76,226 @@ export async function copyCodexConversation( input: CopyInput, invocation: CodexProviderInvocationDeps | undefined ) { - if (!input.record || !input.checkpoint || !invocation) { - return unavailable() - } - const { record, checkpoint } = input - const signal = AbortSignal.any([ - AbortSignal.timeout(60_000), - ...(input.signal ? [input.signal] : []) - ]) - const home = requireLegacyAgentSessionAccountHome(record.accountHome).path - const stores = [join(home, 'sessions'), join(home, 'archived_sessions')] - const find = async (id: string) => - (await resolveSessionFilePath( - 'codex', - id, - { codexSessionsDirs: stores, strictErrors: true }, - signal - )) ?? unavailable() - const history = await codexForkHistory({ - path: await find(input.providerSessionId), - roots: stores, - selectedItemId: checkpoint.selectedItemId, - turnId: checkpoint.turnId, - sessionId: input.providerSessionId, - signal - }) - const beforeTurnId = history.roots[history.index + 1]?.id - const lastTurnId = history.selected.id - if (!checkpoint.settled && !history.selected.terminal && !beforeTurnId) { - throw new AgentConversationCopyError( - "Codex hasn't finished this response yet. Wait for it to finish, then try again." - ) - } - return runCodexProviderAppServer( - record, - { ...invocation, resolveLaunchArgs: () => record.launchArgs ?? invocation.resolveLaunchArgs() }, - { timeoutMs: 60_000, signal, experimentalApi: !lastTurnId && Boolean(beforeTurnId) }, - async (rpc, cwd) => { - const prefix = history.roots.slice(0, history.index + 1) - const sourceTurns = prefix.every((root) => root.id) - ? prefix.map((root) => ({ id: root.id! })) - : await turns(rpc, input.providerSessionId, history.roots.length) - if ( - sourceTurns.length < prefix.length || - prefix.some((root, index) => root.id && root.id !== sourceTurns[index]?.id) - ) { - return unavailable() - } - const coverage = { - unit: 'codex-turn' as const, - ids: sourceTurns.slice(0, prefix.length).map((turn) => turn.id) - } - const fork = asRecord( - await rpc.request('thread/fork', { - threadId: input.providerSessionId, - ...(lastTurnId ? { lastTurnId } : beforeTurnId ? { beforeTurnId } : {}), - excludeTurns: true, - cwd - }) - ) - const thread = asRecord(fork?.thread) - const id = thread?.id - if (typeof id !== 'string' || !id.trim() || id === input.providerSessionId) { - return unavailable() - } - const captured = await codexForkHistory({ - path: await find(id), + let starting = false + let dispatched = false + try { + if (!input.record || !input.checkpoint || !invocation) { + return unavailable() + } + const { record, checkpoint } = input + const signal = AbortSignal.any([ + AbortSignal.timeout(60_000), + ...(input.signal ? [input.signal] : []) + ]) + const home = requireLegacyAgentSessionAccountHome(record.accountHome).path + const stores = [join(home, 'sessions'), join(home, 'archived_sessions')] + const legacy = parseAgentJournalItemKey(checkpoint.selectedItemId)?.provider === 'legacy' + const find = async (id: string) => + (await resolveSessionFilePath( + 'codex', + id, + { codexSessionsDirs: stores, strictErrors: true }, + signal + )) ?? unavailable() + const physicalHistory = async (turnId: string | undefined) => { + const path = await find(input.providerSessionId) + return codexForkHistory({ + path, roots: stores, selectedItemId: checkpoint.selectedItemId, - turnId: checkpoint.turnId, - sessionId: id, - inheritedOnly: true, + turnId, + sessionId: input.providerSessionId, signal }) - if ( - captured.index !== history.index || - captured.roots.length < prefix.length || - captured.roots - .slice(0, prefix.length) - .some((root, index) => root.id && root.id !== coverage.ids[index]) - ) { - return unavailable() - } - if (lastTurnId || beforeTurnId) { - if (captured.roots.length !== prefix.length) { - return unavailable() - } - } else { - // Trim the actual capture; coverage identities were frozen on the source before fork. - if (thread?.historyMode !== 'paginated') { - return unavailable() - } - const childTurns = await turns(rpc, id, captured.roots.length + 1) - if (childTurns.length !== captured.roots.length) { - return unavailable() - } - for (const [index, root] of captured.roots.entries()) { - if (root.id && root.id !== childTurns[index]?.id) { - return unavailable() + } + const missing = (): never => { + throw agentConversationCopyRefusal( + 'agent_session_operation_invalid', + { reason: 'savedConversationMissing' }, + 'Codex could not find this response in the saved chat. Open the chat again, then fork a response still in its history.' + ) + } + let history = legacy && !checkpoint.turnId ? await physicalHistory(undefined) : undefined + starting = true + return await runCodexProviderAppServer( + record, + { + ...invocation, + resolveLaunchArgs: () => record.launchArgs ?? invocation.resolveLaunchArgs() + }, + { timeoutMs: 60_000, signal, experimentalApi: true }, + async (rpc, cwd) => { + starting = false + let metadata + try { + metadata = asRecord( + asRecord( + await rpc.request('thread/read', { + threadId: input.providerSessionId, + includeTurns: false + }) + )?.thread + ) + if ( + metadata?.id !== input.providerSessionId || + (metadata.historyMode != null && + metadata.historyMode !== 'legacy' && + metadata.historyMode !== 'paginated') + ) { + throw new Error('Invalid source metadata') + } + } catch (error) { + if (!isCodexAppServerUnsupportedError(error)) { + throw error } } - const firstRemoved = childTurns[captured.index + 1] - if (firstRemoved) { - await rpc.request('thread/revert', { threadId: id, beforeTurnId: firstRemoved.id }) - const kept = await turns(rpc, id, captured.index + 2) + let native = metadata?.historyMode === 'paginated' + let sourceTurns = + native && checkpoint.turnId + ? await turns(rpc, input.providerSessionId, checkpoint.turnId) + : undefined + if (!native || !sourceTurns || sourceTurns.at(-1)?.id !== checkpoint.turnId) { + if (native && !legacy) { + return missing() + } + history ??= await physicalHistory(native && legacy ? undefined : checkpoint.turnId) + if (native && history.selected.id) { + sourceTurns = await turns(rpc, input.providerSessionId, history.selected.id) + if (sourceTurns.at(-1)?.id !== history.selected.id) { + return missing() + } + } else { + native = false + } + } + const lastTurnId = native ? sourceTurns!.at(-1)!.id : history!.selected.id + const beforeTurnId = native ? undefined : history!.roots[history!.index + 1]?.id + const terminal = native + ? ['completed', 'failed', 'interrupted'].includes(String(sourceTurns!.at(-1)?.status)) + : history!.selected.terminal + if (!checkpoint.settled && !terminal && !beforeTurnId) { + throw agentConversationCopyRefusal( + 'agent_session_operation_invalid', + { reason: 'turnActive' }, + "Codex hasn't finished this response yet. Wait for it to finish, then try again." + ) + } + const prefix = native ? sourceTurns! : history!.roots.slice(0, history!.index + 1) + sourceTurns = native + ? sourceTurns! + : prefix.every((root) => root.id) + ? prefix.map((root) => ({ id: root.id! })) + : await turns(rpc, input.providerSessionId, history!.roots.length) + if ( + sourceTurns.length < prefix.length || + prefix.some((root, index) => root.id && root.id !== sourceTurns![index]?.id) + ) { + return missing() + } + const coverage = { + unit: 'codex-turn' as const, + ids: sourceTurns.slice(0, prefix.length).map((turn) => turn.id) + } + rpc.signal?.throwIfAborted() + signal.throwIfAborted() + dispatched = true + const fork = asRecord( + await rpc.request('thread/fork', { + threadId: input.providerSessionId, + ...(lastTurnId ? { lastTurnId } : beforeTurnId ? { beforeTurnId } : {}), + excludeTurns: true, + cwd + }) + ) + const thread = asRecord(fork?.thread) + const id = thread?.id + if (typeof id !== 'string' || !id.trim() || id === input.providerSessionId) { + return unavailable() + } + if (native) { + const captured = await turns(rpc, id, lastTurnId!) if ( - kept.length !== captured.index + 1 || - kept.at(-1)?.id !== childTurns[captured.index]?.id + captured.length !== coverage.ids.length || + captured.some((turn, index) => turn.id !== coverage.ids[index]) ) { return unavailable() } + return { sessionId: id, coverage } } + const captured = await codexForkHistory({ + path: await find(id), + roots: stores, + selectedItemId: checkpoint.selectedItemId, + turnId: checkpoint.turnId, + sessionId: id, + inheritedOnly: true, + signal + }) + if ( + captured.index !== history!.index || + captured.roots.length < prefix.length || + captured.roots + .slice(0, prefix.length) + .some((root, index) => root.id && root.id !== coverage.ids[index]) + ) { + return unavailable() + } + if (lastTurnId || beforeTurnId) { + if (captured.roots.length !== prefix.length) { + return unavailable() + } + } else { + // Trim the actual capture; coverage identities were frozen on the source before fork. + if (thread?.historyMode !== 'paginated') { + return unavailable() + } + const childTurns = await turns(rpc, id, captured.roots.length + 1) + if (childTurns.length !== captured.roots.length) { + return unavailable() + } + for (const [index, root] of captured.roots.entries()) { + if (root.id && root.id !== childTurns[index]?.id) { + return unavailable() + } + } + const firstRemoved = childTurns[captured.index + 1] + if (firstRemoved) { + await rpc.request('thread/revert', { threadId: id, beforeTurnId: firstRemoved.id }) + const kept = await turns(rpc, id, captured.index + 2) + if ( + kept.length !== captured.index + 1 || + kept.at(-1)?.id !== childTurns[captured.index]?.id + ) { + return unavailable() + } + } + } + return { sessionId: id, coverage } } - return { sessionId: id, coverage } + ) + } catch (error) { + input.signal?.throwIfAborted() + if (!dispatched) { + const refusal = isAgentSessionRefusalError(error) + ? error + : agentConversationCopyRefusal( + 'agent_session_identity_required', + { reason: 'transcriptUnreadable' }, + starting + ? TEXT_GENERATION_FAILURE_COPY.agentCouldNotStart.replace('{{label}}', 'Codex') + : 'Codex could not read the saved chat. Open it again, then try forking the response.' + ) + if (starting) { + refusal.cause = new AgentConversationCopyStartError(refusal.refusal.message) + } + refusal.refusal.createAttemptRefusedBeforeCommit = true + throw refusal } - ).catch((error: unknown) => { if (error instanceof AgentConversationCopyError) { throw error } - if (error instanceof CodexAppServerTimeoutError || signal.aborted) { - throw new AgentConversationCopyError('Codex took too long to copy this chat. Try again.', { - cause: error - }) - } - if ( - error instanceof Error && - /(?:in[- ]progress|active turn|turn.*running)/i.test(error.message) - ) { - throw new AgentConversationCopyError( - 'Codex could not copy this chat while a response was running. Wait for it to finish, then try again.', - { cause: error } - ) - } - throw new AgentConversationCopyError('Codex could not copy this chat. Try again.', { + throw new AgentConversationCopyError('Codex could not verify the copied chat.', { cause: error }) - }) + } } diff --git a/src/main/codex/codex-fork-history.ts b/src/main/codex/codex-fork-history.ts new file mode 100644 index 00000000000..26d345e2693 --- /dev/null +++ b/src/main/codex/codex-fork-history.ts @@ -0,0 +1,100 @@ +import { AgentConversationCopyError } from '../native-chat/agent-conversation-copy-error' +import { parseAgentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key' +import { asRecord, parseJsonObject } from '../ai-vault/session-scanner-values' +import { createLegacyIdentityTracker } from '../native-chat/agent-session-journal/history-import/journal-legacy-identity' +import { readCodexLocalHistory } from '../native-chat/codex-local-history' +import { decodeCodexTranscriptLine } from '../native-chat/transcript-line-decoders-codex' + +type Root = { id?: string; terminal: boolean; prompt: boolean } + +/** Use the importer's physical lineage and identity accounting, including discarded window rows. */ +export async function codexForkHistory(input: { + path: string + roots: string[] + selectedItemId: string + turnId?: string + sessionId: string + signal?: AbortSignal + inheritedOnly?: boolean +}) { + const identity = parseAgentJournalItemKey(input.selectedItemId) + const tracker = createLegacyIdentityTracker({ + transcriptAgent: 'codex', + agent: 'codex', + sessionId: input.sessionId + }) + const roots: Root[] = [] + let selected: Root | undefined + let lineIndex = 0 + let referenceBacked = false + await readCodexLocalHistory({ + filePath: input.path, + roots: input.roots, + visitRecord: ({ record, selected }) => { + if (selected && record?.type === 'session_meta') { + referenceBacked = asRecord(record.payload)?.history_base != null + } + }, + visit: (line, fallbackId, selectedSegment, recordId) => { + if (input.inheritedOnly && referenceBacked && selectedSegment) { + return + } + input.signal?.throwIfAborted() + const tracked = tracker.identify(line, lineIndex++) + const raw = parseJsonObject(line) + const payload = asRecord(raw?.payload) + if ( + raw?.type === 'event_msg' && + payload?.type === 'thread_rolled_back' && + typeof payload.num_turns === 'number' + ) { + roots.splice(Math.max(0, roots.length - payload.num_turns)) + return + } + if (raw?.type === 'event_msg' && payload?.type === 'task_started') { + roots.push({ + id: typeof payload.turn_id === 'string' ? payload.turn_id : undefined, + terminal: false, + prompt: false + }) + } + const message = decodeCodexTranscriptLine(line, fallbackId, { historyImport: true }) + if (message?.role === 'user') { + const previous = roots.at(-1) + if (!previous || (previous.prompt && (!previous.id || previous.terminal))) { + roots.push({ terminal: false, prompt: true }) + } else { + previous.prompt = true + } + } + const root = roots.at(-1) + if ( + root && + raw?.type === 'event_msg' && + (payload?.type === 'task_complete' || payload?.type === 'turn_aborted') && + (!payload.turn_id || payload.turn_id === root.id) + ) { + root.terminal = true + } + const importedMatch = + identity?.provider === 'legacy' && + identity.recordId === + (recordId ?? (tracked.provider === 'legacy' ? tracked.recordId : undefined)) + if ( + root && + ((importedMatch && message?.role === 'assistant') || + (input.turnId && root.id === input.turnId)) + ) { + if (selected && selected !== root) { + throw new AgentConversationCopyError('Codex could not verify the copied chat.') + } + selected = root + } + } + }) + const index = selected ? roots.indexOf(selected) : -1 + if (index < 0) { + throw new AgentConversationCopyError('Codex could not verify the copied chat.') + } + return { roots, index, selected: roots[index] } +} diff --git a/src/main/native-chat/agent-conversation-copy-error.ts b/src/main/native-chat/agent-conversation-copy-error.ts index 8924223a2a2..64fb08a76f8 100644 --- a/src/main/native-chat/agent-conversation-copy-error.ts +++ b/src/main/native-chat/agent-conversation-copy-error.ts @@ -8,6 +8,8 @@ import type { AgentSessionRefusalDetailsByCode } from '../../shared/agent-sessio /** Only copy errors authored here may pass through the host's generic provider failure. */ export class AgentConversationCopyError extends Error {} +export class AgentConversationCopyStartError extends AgentConversationCopyError {} + export function agentConversationCopyRefusal( code: C, details: NoInfer, diff --git a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork-source.ts b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork-source.ts index 5c60bb1c6bc..dba1bd926a7 100644 --- a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork-source.ts +++ b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork-source.ts @@ -9,7 +9,10 @@ import { type AgentSessionExecutionLocation } from '../../../../shared/agent-session/lifecycle/agent-session-record' import type { StructuredAgentId } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle' -import { isAgentSessionRefusalError } from '../../../../shared/agent-session/failures/agent-session-wire-refusals' +import { + isAgentSessionRefusalError, + type AgentSessionRefusalError +} from '../../../../shared/agent-session/failures/agent-session-wire-refusals' import { isAgentSessionForkSourceAdmissionRefusal } from '../../../../shared/agent-session/failures/agent-session-definitive-refusal' import { AGENT_SESSION_FAILURE_COPY, @@ -17,6 +20,7 @@ import { } from '../../../../shared/agent-session/failures/agent-session-failure-copy' import { AgentConversationCopyError, + AgentConversationCopyStartError, agentConversationCopyRefusal } from '../../agent-conversation-copy-error' import { acpAgentName } from '../../../acp/session/acp-structured-agent-definitions' @@ -70,7 +74,7 @@ async function resolveForkSource(input: StructuredAgentSessionForkInput) { const capability = host.deps.agents.capabilities(input.agent)?.fork const { head, pendingClear } = activeProviderContext(record) const fail = ( - message: string, + message: string | AgentSessionRefusalError, reason: | 'conversationCleared' | 'rewindUnconfirmed' @@ -79,16 +83,16 @@ async function resolveForkSource(input: StructuredAgentSessionForkInput) { | 'providerRejected' | 'launchFolderMissing' = 'savedConversationMissing' ): never => { - const refusal = agentConversationCopyRefusal( - 'agent_session_operation_invalid', - { reason }, - message - ) - if (original) { + const refusal = + typeof message === 'string' + ? agentConversationCopyRefusal('agent_session_operation_invalid', { reason }, message) + : message + const failureReason = refusal.refusal.details?.reason + if (original && !(refusal.cause instanceof AgentConversationCopyStartError)) { const piece = - reason === 'savedConversationMissing' + failureReason === 'savedConversationMissing' ? 'forkOriginalMissing' - : reason === 'turnActive' + : failureReason === 'turnActive' ? 'forkOriginalLatestOnly' : 'forkOriginalUnavailable' const title = record.conversationName?.trim() diff --git a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork.ts b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork.ts index ae112b21465..184a31f819a 100644 --- a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork.ts +++ b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-fork.ts @@ -51,7 +51,7 @@ export async function prepareStructuredAgentSessionFork( accountHome: record.accountHome }).catch((error: unknown) => { if (isAgentSessionRefusalError(error)) { - throw error + return fail(error) } host.deps.logger.warn('native conversation copy failed', { scope: 'chat-fork', diff --git a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-precommit-refusal.test.ts b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-precommit-refusal.test.ts index fdab266aaf1..441bff0acff 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-precommit-refusal.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-precommit-refusal.test.ts @@ -1,3 +1,6 @@ +import { copyCodexConversation } from '../../../../../codex/codex-conversation-copy' +import type * as AppServerSession from '../../../../../codex/app-server/codex-app-server-session' +import { tmpdir } from 'node:os' import { createRestTestRig } from '../../../../../native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig' import { forkTestSource } from '../../../../../native-chat/agent-session-wire/acquisition/testing/structured-agent-session-fork.test-fixture' import { agentSessionRecordFixture } from '../../../../../native-chat/agent-session-record-test-fixture' @@ -21,6 +24,15 @@ import { RpcDispatcher } from '../../../dispatcher' import { STRUCTURED_AGENT_SESSION_METHODS } from '../structured-agent-session' import './structured-agent-session-create' +const codex = vi.hoisted(() => ({ request: vi.fn() })) +vi.mock('../../../../../codex/app-server/codex-app-server-session', async (importOriginal) => ({ + ...(await importOriginal()), + runCodexAppServerSession: async ( + _launch: unknown, + body: (rpc: AppServerSession.CodexAppServerRpc) => Promise + ) => body({ request: codex.request, notify: () => {} }) +})) + const SESSION = 'session-alpha' const OPERATION = '1800000000000-00000000000000000000000000000001' const WORKTREE = 'id:workspace-1' @@ -310,3 +322,52 @@ it('does not prove no copy when source revalidation fails after the native copy' await rig.dispose() } }) + +it('settles a redirected Codex preparation refusal but not a dispatched child-read failure', async () => { + const { rig, test, createFork } = await forkHost() + test.record.conversationName = ' Release notes ' + try { + test.copy.mockImplementation((input) => + copyCodexConversation(input, { + resolveLaunchArgs: () => [], + resolveWorkspacePath: async () => tmpdir(), + resolveCommand: () => ({ command: 'codex', prefixArgs: [] }) + }) + ) + codex.request.mockRejectedValue(new Error('source read failed')) + expect(refusalOf(await createFork('codex_source_read_failed'))).toMatchObject({ + code: 'agent_session_identity_required', + details: { reason: 'transcriptUnreadable' }, + createAttemptRefusedBeforeCommit: true + }) + expect(rig.store.getRecord('codex_source_read_failed')).toBeNull() + codex.request.mockImplementation(async (method: string) => { + if (method === 'thread/read') { + return { thread: { id: '019e6b26-18e2-7103-b7a0-7ba6bb071123', historyMode: 'paginated' } } + } + if (method === 'thread/fork') { + return { thread: { id: '01a1bde0-5ded-7f03-a1bf-3dae1613d8c7' } } + } + throw new Error('child read failed') + }) + // Source listing succeeds before the child read fails. + codex.request + .mockImplementationOnce(async () => ({ + thread: { id: '019e6b26-18e2-7103-b7a0-7ba6bb071123', historyMode: 'paginated' } + })) + .mockImplementationOnce(async () => ({ + data: [{ id: 'completed-turn', status: 'completed' }], + nextCursor: null + })) + const refusal = refusalOf(await createFork('codex_child_read_failed')) + expect(refusal).toMatchObject({ + code: 'agent_session_operation_invalid', + details: { reason: 'providerRejected' } + }) + expect(refusal).not.toHaveProperty('createAttemptRefusedBeforeCommit') + expect(rig.store.getRecord('codex_child_read_failed')).toBeNull() + } finally { + setStructuredAgentSessionHost(null) + await rig.dispose() + } +}) From c55bee048576a43d529b413924678721dc4d30b8 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:06:44 +0000 Subject: [PATCH 13/28] refactor(claude): move the kill-delivered receipt check out of the session adapter (#40481) The adapter crossed the 300-line cap after #40069 and main's lint fails. The same receipt check ran twice in it; one shared predicate brings the file back under the cap with no behaviour change. --- .../claude-kill-delivered-exit-receipt.ts | 10 ++++++++++ .../claude-structured-session-adapter.ts | 16 +++------------- 2 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 src/main/claude/structured/claude-kill-delivered-exit-receipt.ts diff --git a/src/main/claude/structured/claude-kill-delivered-exit-receipt.ts b/src/main/claude/structured/claude-kill-delivered-exit-receipt.ts new file mode 100644 index 00000000000..4fa1bbb75ef --- /dev/null +++ b/src/main/claude/structured/claude-kill-delivered-exit-receipt.ts @@ -0,0 +1,10 @@ +import { AgentSessionAcquisitionRootExitObservedError } from '../../native-chat/agent-session-wire/acquisition/structured-agent-session-acquisition-errors' + +export function isKillDeliveredExitReceipt( + receipt: Error | undefined +): receipt is AgentSessionAcquisitionRootExitObservedError { + return ( + receipt instanceof AgentSessionAcquisitionRootExitObservedError && + receipt.deathEvidenceKind === 'kill-delivered' + ) +} diff --git a/src/main/claude/structured/claude-structured-session-adapter.ts b/src/main/claude/structured/claude-structured-session-adapter.ts index 45ea207c93a..f2b69b423d1 100644 --- a/src/main/claude/structured/claude-structured-session-adapter.ts +++ b/src/main/claude/structured/claude-structured-session-adapter.ts @@ -1,5 +1,5 @@ import { dispatchClaudeCommand } from './claude-structured-command-dispatch' -import { AgentSessionAcquisitionRootExitObservedError } from '../../native-chat/agent-session-wire/acquisition/structured-agent-session-acquisition-errors' +import { isKillDeliveredExitReceipt } from './claude-kill-delivered-exit-receipt' import { claudeAcquireFailure } from '../claude-startup-failure' import { claudeStartPublication, openClaudeStart } from './claude-structured-start-ending' import { AgentSessionStartEndings } from '../../native-chat/agent-session-wire/startup/structured-agent-session-start-failure' @@ -259,11 +259,7 @@ export class ClaudeStructuredSessionAdapter implements StructuredAgentSessionAda try { const closed = await close() const receipt = this.settledExitErrors.get(sessionId) - if ( - closed && - receipt instanceof AgentSessionAcquisitionRootExitObservedError && - receipt.deathEvidenceKind === 'kill-delivered' - ) { + if (closed && isKillDeliveredExitReceipt(receipt)) { throw receipt } return closed @@ -290,13 +286,7 @@ export class ClaudeStructuredSessionAdapter implements StructuredAgentSessionAda options ) => this.releaseAcquisition({ sessionId, closeOptions: options }).finally(() => { - const receipt = this.settledExitErrors.get(sessionId) - if ( - !( - receipt instanceof AgentSessionAcquisitionRootExitObservedError && - receipt.deathEvidenceKind === 'kill-delivered' - ) - ) { + if (!isKillDeliveredExitReceipt(this.settledExitErrors.get(sessionId))) { this.settledExitErrors.delete(sessionId) } }) From 313fb8733e37d4c6e10f725e720177ea7c022a78 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:18:06 +0000 Subject: [PATCH 14/28] fix(speech): reload the warm recognizer once the VAD download lands (#40477) A recognizer that loaded before the Silero VAD file existed kept cutting text only at stop until its next restart. When the VAD lands, the speech service now unloads that recognizer once no dictation uses it and warms it again, so pause cuts start on first use. A running dictation is never interrupted. --- src/main/speech/speech-vad-model.ts | 25 ++++++++++--- src/main/speech/stt-service.test.ts | 54 ++++++++++++++++++++++++---- src/main/speech/stt-service.ts | 22 ++++++++++++ src/main/speech/stt-session-start.ts | 3 +- src/main/speech/stt-session-state.ts | 2 ++ 5 files changed, 93 insertions(+), 13 deletions(-) diff --git a/src/main/speech/speech-vad-model.ts b/src/main/speech/speech-vad-model.ts index 5aa954a01b0..12403954e06 100644 --- a/src/main/speech/speech-vad-model.ts +++ b/src/main/speech/speech-vad-model.ts @@ -19,10 +19,7 @@ export function downloadSileroVadInBackground(models: Pick -): string | undefined { +function placedSileroVadPath(models: Pick): string | undefined { const [file] = SILERO_VAD_MODEL.downloadFiles const path = join(models.getModelDir(SILERO_VAD_MODEL.id), file.name) if (path === verifiedVadPath) { @@ -36,6 +33,24 @@ export function sileroVadModelPath( } catch { // Not downloaded yet, e.g. the model came before VAD support. } - downloadSileroVadInBackground(models) return undefined } + +/** Whether a model-state change for `modelId` means the Silero VAD file is now in place. */ +export function sileroVadLanded( + models: Pick, + modelId: string +): boolean { + return modelId === SILERO_VAD_MODEL.id && placedSileroVadPath(models) !== undefined +} + +/** The downloaded Silero VAD file; when it's missing, starts fetching it and returns undefined. */ +export function sileroVadModelPath( + models: Pick +): string | undefined { + const path = placedSileroVadPath(models) + if (!path) { + downloadSileroVadInBackground(models) + } + return path +} diff --git a/src/main/speech/stt-service.test.ts b/src/main/speech/stt-service.test.ts index cdd27198850..f397c55b819 100644 --- a/src/main/speech/stt-service.test.ts +++ b/src/main/speech/stt-service.test.ts @@ -1,6 +1,9 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' import type { ModelManager } from './model-manager' import type { SpeechModelState } from '../../shared/speech-types' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' const { MockWorker, getCreatedWorkerCount, getLastWorker, resetWorkers } = vi.hoisted(() => { class HoistedMockWorker extends EventTarget { @@ -104,9 +107,16 @@ const stoppedReply = (worker: InstanceType) => ({ streamId: worker.messages.findLast((message) => message.type === 'stop')?.streamId }) -const testModels = (models: Pick): ModelManager => - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SttService reads getModelState and getModelDir, and fetches the VAD with downloadModel. - ({ ...models, downloadModel: vi.fn().mockResolvedValue(undefined) }) as never +const testModels = ( + models: Pick, + onModelStateChange: ModelManager['setProgressCallback'] = () => () => {} +): ModelManager => + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SttService reads getModelState and getModelDir, fetches the VAD with downloadModel and watches it land. + ({ + ...models, + downloadModel: vi.fn().mockResolvedValue(undefined), + setProgressCallback: onModelStateChange + }) as never const readyModels = (): ModelManager => testModels({ @@ -122,6 +132,7 @@ async function flushMicrotasks(): Promise { } vi.mock('./model-catalog', () => ({ + isLocalSpeechModel: (manifest: { provider: string }) => manifest.provider === 'local', getCatalogModel: (id: string) => id === 'openai-model' ? { @@ -174,6 +185,31 @@ describe('SttService', () => { resetWorkers() }) + it('reloads a recognizer loaded before the VAD landed once the dictation using it ends', async () => { + const modelsDir = mkdtempSync(join(tmpdir(), 'stt-vad-')) + onTestFinished(() => rmSync(modelsDir, { recursive: true, force: true })) + const watchModels = vi.fn(() => () => {}) + const models = testModels( + { getModelState: readyModels().getModelState, getModelDir: (id) => join(modelsDir, id) }, + watchModels + ) + const service = new SttService(models, spawnMockWorker) + await service.startDictation('model-a', vi.fn(), undefined, 'desktop') + const initOf = (worker = getLastWorker()) => worker?.messages.find((m) => m.type === 'init') + expect(initOf()).toMatchObject({ vadModelPath: undefined }) + + const vadPath = join(modelsDir, 'silero-vad-v6.2', 'silero-vad-v6.2-F32.gguf') + mkdirSync(join(modelsDir, 'silero-vad-v6.2')) + writeFileSync(vadPath, Buffer.alloc(1_241_056)) + watchModels.mock.calls[0]?.[0]('silero-vad-v6.2', -1) + await flushMicrotasks() + expect(getCreatedWorkerCount()).toBe(1) + + await service.stopDictation('desktop') + await vi.waitFor(() => expect(getCreatedWorkerCount()).toBe(2)) + expect(initOf()).toMatchObject({ vadModelPath: vadPath }) + }) + it('gives each device dictating at once only its own text from one recognizer', async () => { const service = new SttService(readyModels(), spawnMockWorker) const desktop = vi.fn() @@ -739,15 +775,19 @@ describe('SttService', () => { async (event) => { vi.useFakeTimers() try { - const models: Pick = { + const models: Pick< + ModelManager, + 'getModelState' | 'getModelDir' | 'downloadModel' | 'setProgressCallback' + > = { getModelState: vi.fn().mockResolvedValue({ id: 'model-a', status: 'ready' }), getModelDir: vi.fn().mockReturnValue('/tmp/model-a'), - downloadModel: vi.fn().mockResolvedValue(undefined) + downloadModel: vi.fn().mockResolvedValue(undefined), + setProgressCallback: () => () => {} } - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This lifecycle reads getModelState/getModelDir and downloadModel (VAD) on the model manager. + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This lifecycle reads getModelState/getModelDir, downloadModel (VAD) and setProgressCallback on the model manager. const service = new SttService(models as ModelManager, spawnMockWorker) const sink = vi.fn() await service.startDictation('model-a', sink, undefined, 'desktop') diff --git a/src/main/speech/stt-service.ts b/src/main/speech/stt-service.ts index 048c4872135..2333dfae0ed 100644 --- a/src/main/speech/stt-service.ts +++ b/src/main/speech/stt-service.ts @@ -6,6 +6,7 @@ import { } from '../../shared/dictation-text-pipeline' import { getCatalogModel } from './model-catalog' import type { ModelManager } from './model-manager' +import { sileroVadLanded } from './speech-vad-model' import type { SpawnSttWorkerProcess } from './stt-worker-process' import { prewarmSttModel } from './stt-session-prewarm' import { startSttDictation } from './stt-session-start' @@ -45,6 +46,27 @@ export class SttService { constructor(modelManager: ModelManager, spawnWorker: SpawnSttWorkerProcess) { this.state = createSttSessionState(modelManager, spawnWorker) + modelManager.setProgressCallback((modelId) => { + if (sileroVadLanded(modelManager, modelId)) { + void this.reloadForVad() + } + }) + } + + /** A recognizer loaded before the VAD landed cuts text only at stop; reload it once no dictation uses it. */ + private async reloadForVad(): Promise { + const engine = this.state.engine + if (!engine || engine.retired || engine.vadModelPath) { + return + } + const { modelId, language } = engine + const unloaded = this.unloadModelWhenIdle() + const generation = this.state.prewarmGeneration + await unloaded + // A newer pre-warm or a dictation took over; loading is theirs now. + if (this.state.prewarmGeneration === generation && !this.state.engine) { + await this.prewarm(modelId, language) + } } /** `terms`: the dictionary for where this dictation goes, repo terms first (mergeDictationTerms). */ diff --git a/src/main/speech/stt-session-start.ts b/src/main/speech/stt-session-start.ts index cd815fbc920..ee7bd5b1833 100644 --- a/src/main/speech/stt-session-start.ts +++ b/src/main/speech/stt-session-start.ts @@ -187,7 +187,7 @@ export function spawnSttEngine( if (!speechModulesDir) { throw new Error(LOCAL_SPEECH_UNAVAILABLE_ERROR) } - // A VAD fetched now serves the next engine start. + // A VAD fetched now reloads this engine once it is idle (SttService). const vadModelPath = sileroVadModelPath(state.modelManager) clearSttIdleTeardownTimer(state) const worker = state.spawnWorker() @@ -210,6 +210,7 @@ export function spawnSttEngine( worker, detach, retired: false, + vadModelPath, onUnloaded: [], ready: readyPromise.catch((error: unknown) => { failSttEngine(state, engine, STT_WORKER_EXITED_ERROR) diff --git a/src/main/speech/stt-session-state.ts b/src/main/speech/stt-session-state.ts index 21c081dacf9..c54d4dd5eed 100644 --- a/src/main/speech/stt-session-state.ts +++ b/src/main/speech/stt-session-state.ts @@ -19,6 +19,8 @@ export type SttEngine = SttRecognizerConfig & { detach: () => void /** Torn down on purpose, so a load it cut short is not a failure of the model. */ retired: boolean + /** Absent when the VAD wasn't downloaded yet at load, so this process cuts text only at stop. */ + vadModelPath: string | undefined /** Called once this process is gone; set when it must not outlive its last dictation. */ onUnloaded: (() => void)[] } From 18996530407d9744890f196813f3d5cc8061cd4b Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:18:14 +0000 Subject: [PATCH 15/28] feat(settings): manage dictation vocabulary in Voice settings, per user and per repo (#40471) * feat(settings): add a Vocabulary group to Voice settings for personal and repo terms * feat(speech): use the repo name as a dictation hint without recasing one-word names * fix(settings): use Input and Button styling as the design system defines it * fix(settings): chain quick repo-term edits, report a host that drops them, keep the hint off phone replies * fix(settings): key a pending repo-term write by host too * refactor(settings): catch a dropped repo-term write in updateRepo and keep the name hint out of stored terms * fix(settings): one vocabulary chip per term with its aliases, and no repo suffix on the scope tab * fix(settings): skip a bare vocabulary term that already has an entry --- src/main/ipc/speech.ts | 7 +- .../runtime/speech/runtime-dictation-terms.ts | 6 +- .../speech/runtime-mobile-speech-catalog.ts | 4 +- src/main/speech/stt-service.ts | 2 +- .../components/dictation/dictation-context.ts | 6 +- .../components/settings/voice/VoicePane.tsx | 8 + .../settings/voice/VoiceVocabularySetting.tsx | 269 ++++++++++++++++++ .../settings/voice/voice-pane-search.ts | 18 ++ src/renderer/src/i18n/locales/de.json | 23 +- src/renderer/src/i18n/locales/en.json | 23 +- src/renderer/src/i18n/locales/es.json | 23 +- src/renderer/src/i18n/locales/fr.json | 23 +- src/renderer/src/i18n/locales/ja.json | 23 +- src/renderer/src/i18n/locales/ko.json | 23 +- src/renderer/src/i18n/locales/pt.json | 23 +- src/renderer/src/i18n/locales/ru.json | 23 +- src/renderer/src/i18n/locales/tr.json | 23 +- src/renderer/src/i18n/locales/zh-Hant.json | 23 +- src/renderer/src/i18n/locales/zh.json | 23 +- src/renderer/src/store/repos/repo-update.ts | 8 + src/shared/dictation-dictionary.test.ts | 21 +- src/shared/dictation-dictionary.ts | 47 ++- src/shared/speech-types.ts | 3 + 23 files changed, 619 insertions(+), 33 deletions(-) create mode 100644 src/renderer/src/components/settings/voice/VoiceVocabularySetting.tsx diff --git a/src/main/ipc/speech.ts b/src/main/ipc/speech.ts index 2451c5acff7..98f7f52f9b0 100644 --- a/src/main/ipc/speech.ts +++ b/src/main/ipc/speech.ts @@ -6,7 +6,7 @@ import { type MessagePortMain } from 'electron' import { abortWhenRendererGone } from './renderer-lifetime-abort' -import { mergeDictationTerms } from '../../shared/dictation-dictionary' +import { dictationTermsFor } from '../../shared/dictation-dictionary' import { SPEECH_MODEL_CATALOG } from '../speech/model-catalog' import { deleteLocalSpeechModel } from '../speech/speech-model-deletion' import { getSpeechModelManager, getSpeechSttService } from '../speech/speech-runtime-service' @@ -220,7 +220,10 @@ export function registerSpeechHandlers(store: Store): void { break } }, - mergeDictationTerms(context?.repoTerms, store.getSettings().voice?.dictationTerms), + dictationTermsFor(store.getSettings().voice, { + dictationTerms: context?.repoTerms, + displayName: context?.repoName + }), owner, store.getSettings().voice ) diff --git a/src/main/runtime/speech/runtime-dictation-terms.ts b/src/main/runtime/speech/runtime-dictation-terms.ts index 13785a2cc2d..63838db9179 100644 --- a/src/main/runtime/speech/runtime-dictation-terms.ts +++ b/src/main/runtime/speech/runtime-dictation-terms.ts @@ -1,12 +1,12 @@ -import { mergeDictationTerms, type DictationTerm } from '../../../shared/dictation-dictionary' +import { dictationTermsFor, type DictationTerm } from '../../../shared/dictation-dictionary' import { getRepoIdFromWorktreeId } from '../../../shared/worktree/id' import type { RuntimeStore } from '../construction/runtime-store-contract' -/** This host's dictionary for a worktree: its repo's terms, then the personal ones. A folder workspace has no repo row, so it gets the personal ones. */ +/** This host's dictionary for a worktree; a folder workspace has no repo row, so it gets the personal terms. */ export function hostDictationTerms( store: RuntimeStore, worktreeId: string | undefined ): DictationTerm[] { const repo = worktreeId ? store.getRepo(getRepoIdFromWorktreeId(worktreeId)) : undefined - return mergeDictationTerms(repo?.dictationTerms, store.getSettings().voice?.dictationTerms) + return dictationTermsFor(store.getSettings().voice, repo) } diff --git a/src/main/runtime/speech/runtime-mobile-speech-catalog.ts b/src/main/runtime/speech/runtime-mobile-speech-catalog.ts index fa042e5be68..6619e4f7e0b 100644 --- a/src/main/runtime/speech/runtime-mobile-speech-catalog.ts +++ b/src/main/runtime/speech/runtime-mobile-speech-catalog.ts @@ -69,7 +69,9 @@ export class RuntimeMobileSpeechCatalog { dictationMode: voice.dictationMode === 'hold' ? 'hold' : 'toggle', dictationHoldToRecord: voice.dictationHoldToRecord !== false, removeFillerWords: voice.removeFillerWords !== false, - dictationTerms: hostDictationTerms(store, worktreeId), + // The phone has no Whisper prompt to bias, and its text pass would only join the name; keeping the + // hint out also spares a phone page from before hints, which would treat the name as a full term. + dictationTerms: hostDictationTerms(store, worktreeId).filter((entry) => !entry.hint), models } } diff --git a/src/main/speech/stt-service.ts b/src/main/speech/stt-service.ts index 2333dfae0ed..bacc4b753ed 100644 --- a/src/main/speech/stt-service.ts +++ b/src/main/speech/stt-service.ts @@ -69,7 +69,7 @@ export class SttService { } } - /** `terms`: the dictionary for where this dictation goes, repo terms first (mergeDictationTerms). */ + /** `terms`: the dictionary for where this dictation goes, repo terms first (dictationTermsFor). */ startDictation( modelId: string, sink: SttEventSink, diff --git a/src/renderer/src/components/dictation/dictation-context.ts b/src/renderer/src/components/dictation/dictation-context.ts index f2cbe4088fe..08f9db5290a 100644 --- a/src/renderer/src/components/dictation/dictation-context.ts +++ b/src/renderer/src/components/dictation/dictation-context.ts @@ -11,5 +11,9 @@ export function captureDictationContext(): DictationContext { return {} } const repo = selectRepoByIdForActiveWorkspace(state, getRepoIdFromWorktreeId(worktreeId)) - return { worktreeId, ...(repo?.dictationTerms ? { repoTerms: repo.dictationTerms } : {}) } + return { + worktreeId, + ...(repo?.dictationTerms ? { repoTerms: repo.dictationTerms } : {}), + ...(repo ? { repoName: repo.displayName } : {}) + } } diff --git a/src/renderer/src/components/settings/voice/VoicePane.tsx b/src/renderer/src/components/settings/voice/VoicePane.tsx index b29ea9ff4a7..e95683cda94 100644 --- a/src/renderer/src/components/settings/voice/VoicePane.tsx +++ b/src/renderer/src/components/settings/voice/VoicePane.tsx @@ -12,6 +12,7 @@ import { OpenAiCompatibleProviderDialog } from './OpenAiCompatibleProviderDialog import { OpenAiTranscriptionSettingsRow } from './OpenAiTranscriptionSettingsRow' import { handleVoiceDictationToggle } from './voice-dictation-toggle' import { VoiceDictationLanguageSetting } from './VoiceDictationLanguageSetting' +import { VoiceVocabularySetting } from './VoiceVocabularySetting' import { VOICE_MICROPHONE_ROW_ID, VoiceInputSettings } from './VoiceInputSettings' import { VoiceSetupCard } from './VoiceSetupCard' import { cancelSpeechModelDownload, startSpeechModelDownload } from './speech-model-download' @@ -331,6 +332,13 @@ export function VoicePane({ settings, updateSettings }: VoicePaneProps): React.J + + + +
) => void +} + +const NO_TERMS: readonly DictationTerm[] = [] + +export function VoiceVocabularySetting({ + voiceSettings, + onUpdateVoiceSettings +}: VoiceVocabularySettingProps): React.JSX.Element { + const t = useTranslate() + // The repo you're working in; a folder workspace has none, so only personal terms show. + const repo = useAppStore((s) => selectRepoByIdForActiveWorkspace(s, s.activeRepoId)) + const updateRepo = useAppStore((s) => s.updateRepo) + const [chosenScope, setScope] = useState('personal') + // A repo write is a round trip (RPC for a remote host); the next edit builds on it, not on the stored list. + const [pending, setPending] = useState<{ + repoId: string + hostId: ExecutionHostId + terms: DictationTerm[] + } | null>(null) + const scope = repo ? chosenScope : 'personal' + const personalTerms = voiceSettings.dictationTerms ?? NO_TERMS + const repoTerms = + pending && repo && pending.repoId === repo.id && pending.hostId === getRepoExecutionHostId(repo) + ? pending.terms + : (repo?.dictationTerms ?? NO_TERMS) + const terms = scope === 'repo' ? repoTerms : personalTerms + + const saveRepoTerms = async ( + repoId: string, + hostId: ExecutionHostId, + next: DictationTerm[] + ): Promise => { + const write = { repoId, hostId, terms: next } + setPending(write) + if (!(await updateRepo(repoId, { dictationTerms: next }, { hostId }))) { + toast.error( + t( + 'components.settings.voice.vocabulary.repoSaveFailed', + "Couldn't save the repo's terms. The host running this repo may need an Orca update." + ) + ) + } + setPending((current) => (current === write ? null : current)) + } + + const saveTerms = (next: DictationTerm[]): void => { + if (scope === 'repo' && repo) { + void saveRepoTerms(repo.id, getRepoExecutionHostId(repo), next) + } else { + onUpdateVoiceSettings({ dictationTerms: next }) + } + } + + return ( +
+ +
+

+ {scope === 'repo' && repo + ? t( + 'components.settings.voice.vocabulary.repoScope', + 'Only used while you dictate in {{repo}}.', + { repo: repo.displayName } + ) + : t('components.settings.voice.vocabulary.personalScope', 'Used in every repo.')} +

+ {repo ? ( + + ) : null} +
+ {terms.length > 0 ? ( +
    + {chipsBySpelling(terms).map((chip) => ( + saveTerms(terms.filter((entry) => entry.term !== chip.term))} + /> + ))} +
+ ) : null} + = MAX_DICTATION_TERMS} + onAdd={(entry) => { + // A term already keys its own spelling, so a bare one needs no second entry. + const exists = terms.some( + (other) => + other.term === entry.term && (!entry.heardAs || other.heardAs === entry.heardAs) + ) + if (!exists) { + saveTerms([...terms, entry]) + } + }} + /> + + onUpdateVoiceSettings({ + dictationRepoNameHint: voiceSettings.dictationRepoNameHint === false + }) + } + /> +
+ ) +} + +type VocabularyChipModel = { term: string; aliases: string[] } + +// One chip per spelling: the stored list keeps one entry per "heard as". +function chipsBySpelling(terms: readonly DictationTerm[]): VocabularyChipModel[] { + const chips = new Map() + for (const { term, heardAs } of terms) { + const chip = chips.get(term) ?? { term, aliases: [] } + if (heardAs) { + chip.aliases.push(heardAs) + } + chips.set(term, chip) + } + return [...chips.values()] +} + +function VocabularyChip({ + chip, + onRemove +}: { + chip: VocabularyChipModel + onRemove: () => void +}): React.JSX.Element { + const t = useTranslate() + return ( +
  • + {chip.term} + {chip.aliases.length > 0 ? ( + + {t('components.settings.voice.vocabulary.heardAsChip', '← {{heardAs}}', { + heardAs: chip.aliases.join(', ') + })} + + ) : null} + +
  • + ) +} + +function VocabularyAddRow({ + full, + onAdd +}: { + full: boolean + onAdd: (entry: DictationTerm) => void +}): React.JSX.Element { + const t = useTranslate() + const [term, setTerm] = useState('') + const [heardAs, setHeardAs] = useState('') + const canAdd = !full && term.trim() !== '' + + const add = (): void => { + if (!canAdd) { + return + } + onAdd({ term: term.trim(), ...(heardAs.trim() ? { heardAs: heardAs.trim() } : {}) }) + setTerm('') + setHeardAs('') + } + const addOnEnter = (event: React.KeyboardEvent): void => { + if (event.key === 'Enter') { + event.preventDefault() + add() + } + } + + return ( +
    + setTerm(event.target.value)} + onKeyDown={addOnEnter} + /> + setHeardAs(event.target.value)} + onKeyDown={addOnEnter} + /> + +
    + ) +} diff --git a/src/renderer/src/components/settings/voice/voice-pane-search.ts b/src/renderer/src/components/settings/voice/voice-pane-search.ts index ff52e8b254c..55abb31d426 100644 --- a/src/renderer/src/components/settings/voice/voice-pane-search.ts +++ b/src/renderer/src/components/settings/voice/voice-pane-search.ts @@ -120,6 +120,24 @@ export const getVoicePaneSearchEntries = createLocalizedCatalog(() => [ ...translateSearchKeyword('auto.components.settings.voice.pane.search.10d45a9fce', 'stt') ] }, + { + title: translate('auto.components.settings.voice.pane.search.vocabularyTitle', 'Vocabulary'), + description: translate( + 'auto.components.settings.voice.pane.search.vocabularyDescription', + 'Names and code terms dictation spells your way, for you or for one repo.' + ), + keywords: [ + ...translateSearchKeyword('auto.components.settings.voice.pane.search.7640ed9848', 'voice'), + ...translateSearchKeyword( + 'auto.components.settings.voice.pane.search.089d31a45b', + 'dictation' + ), + ...translateSearchKeyword( + 'auto.components.settings.voice.pane.search.vocabularyKeyword', + 'dictionary' + ) + ] + }, { title: translate( 'auto.components.settings.voice.pane.search.fillerWordsTitle', diff --git a/src/renderer/src/i18n/locales/de.json b/src/renderer/src/i18n/locales/de.json index 454f252e06d..47e9692fe35 100644 --- a/src/renderer/src/i18n/locales/de.json +++ b/src/renderer/src/i18n/locales/de.json @@ -11299,7 +11299,10 @@ "modelMemoryKeyword": "speicher", "fillerWordsTitle": "Füllwörter entfernen", "fillerWordsDescription": "Entfernt „äh“, „ähm“ und ähnliche Laute aus diktiertem Text.", - "fillerWordsKeyword": "füllwörter" + "fillerWordsKeyword": "füllwörter", + "vocabularyTitle": "Wortschatz", + "vocabularyDescription": "Namen und Code-Begriffe, die das Diktat auf deine Weise schreibt, für dich oder ein Repo.", + "vocabularyKeyword": "wörterbuch" } } }, @@ -20746,6 +20749,24 @@ "local": "Auf diesem Computer", "cloudDescription": "Cloud-Modelle senden deine Audiodaten an den Anbieter. Modelle auf diesem Computer behalten sie hier.", "sendsToCompatible": "Sendet deine Audiodaten an den Dienst, den du eingerichtet hast." + }, + "vocabulary": { + "title": "Wortschatz", + "description": "Namen und Code-Begriffe, die du auf deine Weise geschrieben haben willst. Gib an, wie das Modell sie falsch hört, wenn es sie immer wieder verwechselt.", + "repoScope": "Nur beim Diktieren in {{repo}}.", + "personalScope": "In jedem Repo.", + "scope": "Begriffe für", + "personal": "Persönlich · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "{{term}} entfernen", + "term": "Wort oder Wendung", + "heardAs": "Gehört als (optional)", + "heardAsLabel": "Gehört als", + "add": "Hinzufügen", + "repoNameHint": "Repo-Namen als Hinweis verwenden", + "repoNameHintDescription": "Hilft, den Namen des Repos zu erkennen.", + "repoSaveFailed": "Die Begriffe des Repos konnten nicht gespeichert werden. Auf dem Host dieses Repos ist eventuell ein Orca-Update nötig." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index fd3e66b2e85..c0bd1dd1335 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -11635,7 +11635,10 @@ "modelMemoryKeyword": "memory", "fillerWordsTitle": "Remove Filler Words", "fillerWordsDescription": "Drop “um”, “uh” and similar sounds from dictated text.", - "fillerWordsKeyword": "filler words" + "fillerWordsKeyword": "filler words", + "vocabularyTitle": "Vocabulary", + "vocabularyDescription": "Names and code terms dictation spells your way, for you or for one repo.", + "vocabularyKeyword": "dictionary" } } }, @@ -21445,6 +21448,24 @@ "where": "Where models run", "local": "On this computer", "cloudDescription": "Cloud models send your audio to the provider. Models on this computer keep it here." + }, + "vocabulary": { + "title": "Vocabulary", + "description": "Names and code terms to spell your way. Add what the model mishears it as, if it keeps getting it wrong.", + "repoScope": "Only used while you dictate in {{repo}}.", + "personalScope": "Used in every repo.", + "scope": "Terms for", + "personal": "Personal · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "Remove {{term}}", + "term": "Word or phrase", + "heardAs": "Heard as (optional)", + "heardAsLabel": "Heard as", + "add": "Add", + "repoNameHint": "Use the repo name as a hint", + "repoNameHintDescription": "Helps recognize the repo's name.", + "repoSaveFailed": "Couldn't save the repo's terms. The host running this repo may need an Orca update." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index 76d24032fdf..fdbe57e1fcb 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -10882,7 +10882,10 @@ "modelMemoryKeyword": "memoria", "fillerWordsTitle": "Quitar muletillas", "fillerWordsDescription": "Quita «ehm», «em» y sonidos parecidos del texto dictado.", - "fillerWordsKeyword": "muletillas" + "fillerWordsKeyword": "muletillas", + "vocabularyTitle": "Vocabulario", + "vocabularyDescription": "Nombres y términos de código que el dictado escribe a tu manera, para ti o para un repo.", + "vocabularyKeyword": "diccionario" } } }, @@ -20534,6 +20537,24 @@ "local": "En este equipo", "cloudDescription": "Los modelos en la nube envían tu audio al proveedor. Los de este equipo lo mantienen aquí.", "sendsToCompatible": "Envía tu audio al servicio que configuraste." + }, + "vocabulary": { + "title": "Vocabulario", + "description": "Nombres y términos de código que se escriben a tu manera. Añade cómo lo oye mal el modelo si se equivoca una y otra vez.", + "repoScope": "Solo se usa al dictar en {{repo}}.", + "personalScope": "Se usa en todos los repos.", + "scope": "Términos para", + "personal": "Personal · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "Quitar {{term}}", + "term": "Palabra o frase", + "heardAs": "Se oye como (opcional)", + "heardAsLabel": "Se oye como", + "add": "Añadir", + "repoNameHint": "Usar el nombre del repo como pista", + "repoNameHintDescription": "Ayuda a reconocer el nombre del repo.", + "repoSaveFailed": "No se pudieron guardar los términos del repo. Puede que el host de este repo necesite actualizar Orca." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/fr.json b/src/renderer/src/i18n/locales/fr.json index 5e54997ebbb..28f2e380f9a 100644 --- a/src/renderer/src/i18n/locales/fr.json +++ b/src/renderer/src/i18n/locales/fr.json @@ -11167,7 +11167,10 @@ "modelMemoryKeyword": "mémoire", "fillerWordsTitle": "Supprimer les hésitations", "fillerWordsDescription": "Supprime « euh », « hum » et les sons similaires du texte dicté.", - "fillerWordsKeyword": "hésitations" + "fillerWordsKeyword": "hésitations", + "vocabularyTitle": "Vocabulaire", + "vocabularyDescription": "Noms et termes de code que la dictée écrit à votre façon, pour vous ou pour un dépôt.", + "vocabularyKeyword": "dictionnaire" } } }, @@ -20851,6 +20854,24 @@ "local": "Sur cet ordinateur", "cloudDescription": "Les modèles cloud envoient votre audio au fournisseur. Ceux de cet ordinateur le gardent ici.", "sendsToCompatible": "Envoie votre audio au service que vous avez configuré." + }, + "vocabulary": { + "title": "Vocabulaire", + "description": "Noms et termes de code à écrire à votre façon. Ajoutez ce que le modèle entend à la place s’il se trompe toujours.", + "repoScope": "Utilisé seulement quand vous dictez dans {{repo}}.", + "personalScope": "Utilisé dans tous les dépôts.", + "scope": "Termes pour", + "personal": "Personnel · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "Retirer {{term}}", + "term": "Mot ou expression", + "heardAs": "Entendu comme (facultatif)", + "heardAsLabel": "Entendu comme", + "add": "Ajouter", + "repoNameHint": "Utiliser le nom du dépôt comme indice", + "repoNameHintDescription": "Aide à reconnaître le nom du dépôt.", + "repoSaveFailed": "Impossible d’enregistrer les termes du dépôt. L’hôte de ce dépôt doit peut-être mettre Orca à jour." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index 6f309bdfab3..4d90f59f72d 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -10626,7 +10626,10 @@ "modelMemoryKeyword": "メモリ", "fillerWordsTitle": "フィラーを削除", "fillerWordsDescription": "「えーと」「うーん」などのつなぎ言葉を音声入力のテキストから取り除きます。", - "fillerWordsKeyword": "フィラー" + "fillerWordsKeyword": "フィラー", + "vocabularyTitle": "語彙", + "vocabularyDescription": "音声入力が自分の書き方で表記する名前やコード用語。個人用またはリポジトリ用。", + "vocabularyKeyword": "辞書" } } }, @@ -20027,6 +20030,24 @@ "local": "このコンピューター", "cloudDescription": "クラウドモデルは音声をプロバイダーに送信します。このコンピューター上のモデルなら音声は外に出ません。", "sendsToCompatible": "音声を設定したサービスに送信します。" + }, + "vocabulary": { + "title": "語彙", + "description": "名前やコード用語を自分の書き方で入力します。モデルが繰り返し聞き間違える場合は、聞き取られる言い方も追加してください。", + "repoScope": "{{repo}} で音声入力するときだけ使います。", + "personalScope": "すべてのリポジトリで使います。", + "scope": "用語の対象", + "personal": "個人 · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "{{term}} を削除", + "term": "単語またはフレーズ", + "heardAs": "聞き取られ方(任意)", + "heardAsLabel": "聞き取られ方", + "add": "追加", + "repoNameHint": "リポジトリ名をヒントとして使う", + "repoNameHintDescription": "リポジトリ名を認識しやすくします。", + "repoSaveFailed": "リポジトリの用語を保存できませんでした。このリポジトリのホストで Orca の更新が必要かもしれません。" } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index 068db9b1fbd..db599a834a2 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -10589,7 +10589,10 @@ "modelMemoryKeyword": "메모리", "fillerWordsTitle": "군말 제거", "fillerWordsDescription": "받아쓴 텍스트에서 “음”, “흠” 같은 소리를 지웁니다.", - "fillerWordsKeyword": "군말" + "fillerWordsKeyword": "군말", + "vocabularyTitle": "어휘", + "vocabularyDescription": "받아쓰기가 원하는 대로 표기하는 이름과 코드 용어, 개인용 또는 저장소용.", + "vocabularyKeyword": "사전" } } }, @@ -20027,6 +20030,24 @@ "local": "이 컴퓨터", "cloudDescription": "클라우드 모델은 오디오를 제공업체로 보냅니다. 이 컴퓨터의 모델은 오디오를 밖으로 보내지 않습니다.", "sendsToCompatible": "오디오를 설정한 서비스로 보냅니다." + }, + "vocabulary": { + "title": "어휘", + "description": "원하는 대로 표기할 이름과 코드 용어입니다. 모델이 계속 잘못 들으면 잘못 들리는 말도 추가하세요.", + "repoScope": "{{repo}}에서 받아쓸 때만 사용합니다.", + "personalScope": "모든 저장소에서 사용합니다.", + "scope": "용어 대상", + "personal": "개인 · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "{{term}} 삭제", + "term": "단어 또는 구문", + "heardAs": "들리는 말(선택)", + "heardAsLabel": "들리는 말", + "add": "추가", + "repoNameHint": "저장소 이름을 힌트로 사용", + "repoNameHintDescription": "저장소 이름을 더 잘 인식하도록 돕습니다.", + "repoSaveFailed": "저장소 용어를 저장하지 못했습니다. 이 저장소의 호스트에서 Orca 업데이트가 필요할 수 있습니다." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/pt.json b/src/renderer/src/i18n/locales/pt.json index 12065876a1f..1aad807a1d8 100644 --- a/src/renderer/src/i18n/locales/pt.json +++ b/src/renderer/src/i18n/locales/pt.json @@ -6597,7 +6597,10 @@ "modelMemoryKeyword": "memória", "fillerWordsTitle": "Remover hesitações", "fillerWordsDescription": "Remove “hã”, “hum” e sons parecidos do texto ditado.", - "fillerWordsKeyword": "hesitações" + "fillerWordsKeyword": "hesitações", + "vocabularyTitle": "Vocabulário", + "vocabularyDescription": "Nomes e termos de código que o ditado escreve do seu jeito, para você ou para um repositório.", + "vocabularyKeyword": "dicionário" } } }, @@ -21716,6 +21719,24 @@ "local": "Neste computador", "cloudDescription": "Modelos na nuvem enviam seu áudio ao provedor. Os deste computador o mantêm aqui.", "sendsToCompatible": "Envia seu áudio para o serviço que você configurou." + }, + "vocabulary": { + "title": "Vocabulário", + "description": "Nomes e termos de código escritos do seu jeito. Adicione como o modelo os ouve errado, se ele continuar errando.", + "repoScope": "Usado só ao ditar em {{repo}}.", + "personalScope": "Usado em todos os repositórios.", + "scope": "Termos para", + "personal": "Pessoal · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "Remover {{term}}", + "term": "Palavra ou frase", + "heardAs": "Ouvido como (opcional)", + "heardAsLabel": "Ouvido como", + "add": "Adicionar", + "repoNameHint": "Usar o nome do repositório como dica", + "repoNameHintDescription": "Ajuda a reconhecer o nome do repositório.", + "repoSaveFailed": "Não foi possível salvar os termos do repositório. O host deste repositório pode precisar de uma atualização do Orca." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/ru.json b/src/renderer/src/i18n/locales/ru.json index 6cc23c88138..58e12c44554 100644 --- a/src/renderer/src/i18n/locales/ru.json +++ b/src/renderer/src/i18n/locales/ru.json @@ -11651,7 +11651,10 @@ "modelMemoryKeyword": "память", "fillerWordsTitle": "Удалять слова-паразиты", "fillerWordsDescription": "Убирает «э», «эм» и похожие звуки из надиктованного текста.", - "fillerWordsKeyword": "паразиты" + "fillerWordsKeyword": "паразиты", + "vocabularyTitle": "Словарь", + "vocabularyDescription": "Имена и термины из кода, которые диктовка пишет по-вашему, для вас или для репозитория.", + "vocabularyKeyword": "словарь" } } }, @@ -21433,6 +21436,24 @@ "local": "На этом компьютере", "cloudDescription": "Облачные модели отправляют звук провайдеру. Модели на этом компьютере оставляют его здесь.", "sendsToCompatible": "Отправляет звук в настроенный вами сервис." + }, + "vocabulary": { + "title": "Словарь", + "description": "Имена и термины из кода, которые нужно писать по-вашему. Если модель всё время ошибается, добавьте, как она их слышит.", + "repoScope": "Используется только при диктовке в {{repo}}.", + "personalScope": "Используется во всех репозиториях.", + "scope": "Термины для", + "personal": "Личные · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "Удалить {{term}}", + "term": "Слово или фраза", + "heardAs": "Слышится как (необязательно)", + "heardAsLabel": "Слышится как", + "add": "Добавить", + "repoNameHint": "Использовать имя репозитория как подсказку", + "repoNameHintDescription": "Помогает распознавать имя репозитория.", + "repoSaveFailed": "Не удалось сохранить термины репозитория. Возможно, на хосте этого репозитория нужно обновить Orca." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/tr.json b/src/renderer/src/i18n/locales/tr.json index 002982551d7..0893ffd23e7 100644 --- a/src/renderer/src/i18n/locales/tr.json +++ b/src/renderer/src/i18n/locales/tr.json @@ -11283,7 +11283,10 @@ "modelMemoryKeyword": "bellek", "fillerWordsTitle": "Dolgu seslerini kaldır", "fillerWordsDescription": "Dikte edilen metinden “hmm” gibi duraksama seslerini çıkarır.", - "fillerWordsKeyword": "dolgu" + "fillerWordsKeyword": "dolgu", + "vocabularyTitle": "Sözcük dağarcığı", + "vocabularyDescription": "Diktenin senin yazımınla yazdığı adlar ve kod terimleri, senin için veya bir depo için.", + "vocabularyKeyword": "sözlük" } } }, @@ -20728,6 +20731,24 @@ "local": "Bu bilgisayarda", "cloudDescription": "Bulut modelleri sesinizi sağlayıcıya gönderir. Bu bilgisayardaki modeller sesi burada tutar.", "sendsToCompatible": "Sesinizi kurduğunuz hizmete gönderir." + }, + "vocabulary": { + "title": "Sözcük dağarcığı", + "description": "Kendi yazımınla yazılacak adlar ve kod terimleri. Model sürekli yanlış duyuyorsa nasıl duyduğunu da ekle.", + "repoScope": "Yalnızca {{repo}} içinde dikte ederken kullanılır.", + "personalScope": "Her depoda kullanılır.", + "scope": "Terimler", + "personal": "Kişisel · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "{{term}} kaldır", + "term": "Sözcük veya ifade", + "heardAs": "Duyulan biçim (isteğe bağlı)", + "heardAsLabel": "Duyulan biçim", + "add": "Ekle", + "repoNameHint": "Depo adını ipucu olarak kullan", + "repoNameHintDescription": "Depo adının tanınmasına yardımcı olur.", + "repoSaveFailed": "Deponun terimleri kaydedilemedi. Bu deponun ana makinesinde Orca güncellemesi gerekebilir." } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/zh-Hant.json b/src/renderer/src/i18n/locales/zh-Hant.json index 11bd936a4f6..b04ad95e54d 100644 --- a/src/renderer/src/i18n/locales/zh-Hant.json +++ b/src/renderer/src/i18n/locales/zh-Hant.json @@ -11120,7 +11120,10 @@ "modelMemoryKeyword": "記憶體", "fillerWordsTitle": "移除語氣詞", "fillerWordsDescription": "從聽寫文字中移除「嗯」「呃」之類的語氣詞。", - "fillerWordsKeyword": "語氣詞" + "fillerWordsKeyword": "語氣詞", + "vocabularyTitle": "詞彙", + "vocabularyDescription": "聽寫依你的寫法拼寫的名稱和程式碼術語,可用於個人或某個儲存庫。", + "vocabularyKeyword": "詞典" } } }, @@ -20429,6 +20432,24 @@ "local": "這台電腦", "cloudDescription": "雲端模型會把你的音訊傳送給服務商。本機模型會把音訊留在這台電腦上。", "sendsToCompatible": "會把你的音訊傳送到你設定的服務。" + }, + "vocabulary": { + "title": "詞彙", + "description": "依你的寫法拼寫的名稱和程式碼術語。如果模型總是聽錯,可以加上它聽成的說法。", + "repoScope": "僅在 {{repo}} 中聽寫時使用。", + "personalScope": "在所有儲存庫中使用。", + "scope": "術語範圍", + "personal": "個人 · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "移除 {{term}}", + "term": "詞語或片語", + "heardAs": "聽成(選填)", + "heardAsLabel": "聽成", + "add": "新增", + "repoNameHint": "將儲存庫名稱用作提示", + "repoNameHintDescription": "幫助辨識儲存庫名稱。", + "repoSaveFailed": "無法儲存儲存庫的術語。執行此儲存庫的主機可能需要更新 Orca。" } }, "repositoryHooks": { diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index 31c5adba076..8985678ae5f 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -10589,7 +10589,10 @@ "modelMemoryKeyword": "内存", "fillerWordsTitle": "移除语气词", "fillerWordsDescription": "从听写文本中去掉“嗯”“呃”之类的语气词。", - "fillerWordsKeyword": "语气词" + "fillerWordsKeyword": "语气词", + "vocabularyTitle": "词汇", + "vocabularyDescription": "听写按你的写法拼写的名称和代码术语,可用于个人或某个仓库。", + "vocabularyKeyword": "词典" } } }, @@ -20026,6 +20029,24 @@ "local": "这台电脑", "cloudDescription": "云端模型会把你的音频发送给服务商。本机模型会把音频留在这台电脑上。", "sendsToCompatible": "会把你的音频发送到你设置的服务。" + }, + "vocabulary": { + "title": "词汇", + "description": "按你的写法拼写的名称和代码术语。如果模型总是听错,可以加上它听成的说法。", + "repoScope": "仅在 {{repo}} 中听写时使用。", + "personalScope": "在所有仓库中使用。", + "scope": "术语范围", + "personal": "个人 · {{total}}", + "repo": "{{repo}} · {{total}}", + "heardAsChip": "← {{heardAs}}", + "remove": "移除 {{term}}", + "term": "词语或短语", + "heardAs": "听成(可选)", + "heardAsLabel": "听成", + "add": "添加", + "repoNameHint": "将仓库名用作提示", + "repoNameHintDescription": "帮助识别仓库名称。", + "repoSaveFailed": "无法保存仓库的术语。运行此仓库的主机可能需要更新 Orca。" } }, "repositoryHooks": { diff --git a/src/renderer/src/store/repos/repo-update.ts b/src/renderer/src/store/repos/repo-update.ts index 3a7604b3831..995331a18a7 100644 --- a/src/renderer/src/store/repos/repo-update.ts +++ b/src/renderer/src/store/repos/repo-update.ts @@ -158,6 +158,14 @@ export function createRepoUpdateActions( ) { return false } + // A host from before dictionaries drops the field and still answers with its row. + if ( + sanitizedUpdates.dictationTerms !== undefined && + JSON.stringify(updatedRepo?.dictationTerms ?? []) !== + JSON.stringify(sanitizedUpdates.dictationTerms) + ) { + return false + } set((s) => { const nextRepos = s.repos.map((r) => { const matchesOwner = ownerHasExplicitHost diff --git a/src/shared/dictation-dictionary.test.ts b/src/shared/dictation-dictionary.test.ts index 1e81f897337..f7b159cd011 100644 --- a/src/shared/dictation-dictionary.test.ts +++ b/src/shared/dictation-dictionary.test.ts @@ -1,5 +1,9 @@ import { describe, expect, it } from 'vitest' -import { createDictationDictionary, mergeDictationTerms } from './dictation-dictionary' +import { + createDictationDictionary, + dictationTermsFor, + dictationVocabulary +} from './dictation-dictionary' import { createDictationTextPipeline } from './dictation-text-pipeline' const spell = (terms: Parameters[0], text: string): string => @@ -55,8 +59,21 @@ describe('createDictationDictionary', () => { ) }) + it('lets the repo name join its spoken words but never recase a single word', () => { + const oneWord = dictationTermsFor({}, { displayName: 'orca' }) + const words = dictationTermsFor({}, { displayName: 'dd-scratch-repo' }) + + expect(createDictationDictionary(oneWord)).toBeNull() + expect(dictationVocabulary(oneWord)).toEqual(['orca']) + expect(spell(words, 'Open dd scratch repo now.')).toBe('Open dd-scratch-repo now.') + expect(dictationTermsFor({ dictationRepoNameHint: false }, { displayName: 'orca' })).toEqual([]) + }) + it("prefers a repo's spelling over a personal one", () => { - const terms = mergeDictationTerms([{ term: 'RPC' }], [{ term: 'rpc' }]) + const terms = dictationTermsFor( + { dictationTerms: [{ term: 'rpc' }] }, + { dictationTerms: [{ term: 'RPC' }] } + ) expect(spell(terms, 'Call the rpc.')).toBe('Call the RPC.') }) }) diff --git a/src/shared/dictation-dictionary.ts b/src/shared/dictation-dictionary.ts index a66d3f5785b..ad448055809 100644 --- a/src/shared/dictation-dictionary.ts +++ b/src/shared/dictation-dictionary.ts @@ -1,5 +1,8 @@ -/** A word or phrase dictation should spell `term`'s way, optionally also replacing what the model hears instead. */ -export type DictationTerm = { term: string; heardAs?: string } +/** + * A word or phrase dictation should spell `term`'s way, optionally also replacing what the model hears instead. + * `hint` marks the repo name: it biases recognition and joins its words, but never recases a single word. + */ +export type DictationTerm = { term: string; heardAs?: string; hint?: true } export const MAX_DICTATION_TERMS = 500 export const MAX_DICTATION_TERM_LENGTH = 100 @@ -20,32 +23,49 @@ function wordKey(text: string): string { return (text.normalize('NFKC').toLowerCase().match(WORD_RE) ?? []).join('') } -/** Well-formed entries only, trimmed and capped; undefined when the value isn't a list. */ +/** Well-formed, distinct entries only, trimmed and capped; undefined when the value isn't a list. */ export function normalizeDictationTerms(value: unknown): DictationTerm[] | undefined { if (!Array.isArray(value)) { return undefined } const terms: DictationTerm[] = [] + const seen = new Set() for (const entry of value) { const term: unknown = entry?.term const heardAs: unknown = entry?.heardAs if (typeof term !== 'string' || !term.trim()) { continue } - const alias = typeof heardAs === 'string' ? heardAs.trim() : '' + const spelling = term.trim().slice(0, MAX_DICTATION_TERM_LENGTH) + const alias = + typeof heardAs === 'string' ? heardAs.trim().slice(0, MAX_DICTATION_TERM_LENGTH) : '' + // The same spelling and alias twice is one stored entry. + const id = `${spelling}\u0000${alias}` + if (seen.has(id)) { + continue + } + seen.add(id) terms.push({ - term: term.trim().slice(0, MAX_DICTATION_TERM_LENGTH), - ...(alias ? { heardAs: alias.slice(0, MAX_DICTATION_TERM_LENGTH) } : {}) + term: spelling, + ...(alias ? { heardAs: alias } : {}) }) } return terms.slice(0, MAX_DICTATION_TERMS) } -/** Repo terms first, so a repo's spelling wins over a personal one for the same words. */ -export function mergeDictationTerms(repoTerms: unknown, personalTerms: unknown): DictationTerm[] { +/** + * The dictionary for a dictation into `repo` (none for a folder workspace): its terms first, so a repo's + * spelling wins over a personal one, then the personal terms, then its name as a hint unless that's off. + */ +export function dictationTermsFor( + voice: { dictationTerms?: unknown; dictationRepoNameHint?: boolean } | undefined, + repo: { dictationTerms?: unknown; displayName?: string } | undefined +): DictationTerm[] { + const name = repo?.displayName?.trim().slice(0, MAX_DICTATION_TERM_LENGTH) return [ - ...(normalizeDictationTerms(repoTerms) ?? []), - ...(normalizeDictationTerms(personalTerms) ?? []) + ...(normalizeDictationTerms(repo?.dictationTerms) ?? []), + ...(normalizeDictationTerms(voice?.dictationTerms) ?? []), + ...(name && voice?.dictationRepoNameHint !== false ? [{ term: name, hint: true as const }] : []) ] } @@ -75,8 +95,11 @@ export function createDictationDictionary( add(wordKey(heardAs), term) } } - for (const { term } of terms ?? []) { - add(wordKey(term), term) + for (const { term, hint } of terms ?? []) { + // A one-word repo name like "orca" would recase every "Orca"; only a name of several words joins. + if (!hint || (term.match(WORD_RE)?.length ?? 0) > 1) { + add(wordKey(term), term) + } } if (byKey.size === 0 && unspacedAliases.length === 0) { return null diff --git a/src/shared/speech-types.ts b/src/shared/speech-types.ts index bedd641f99f..dafcdfeba76 100644 --- a/src/shared/speech-types.ts +++ b/src/shared/speech-types.ts @@ -132,10 +132,13 @@ export type VoiceSettings = { speechGpu?: SpeechGpuMode /** Personal dictionary, used in every repo; each repo adds its own Repo.dictationTerms. */ dictationTerms?: DictationTerm[] + /** Adds the current repo's name as a hint (DictationTerm.hint). Absent = on. */ + dictationRepoNameHint?: boolean } /** Where a dictation goes: an engine in the desktop's own process takes repoTerms, a host resolves worktreeId itself. */ export type DictationContext = { worktreeId?: string repoTerms?: DictationTerm[] + repoName?: string } From 854928969e2d5a7251cfba06bea708d401350a75 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:19:11 +0000 Subject: [PATCH 16/28] feat(mobile): add Shift, Ctrl and Alt latches and opt-in agent keys to the terminal key row (#38776) * feat(mobile): smart rail keys for the terminal agent's state, with Shift/Ctrl/Alt latches * fix(mobile): cancel key presses only when the key row really moves * fix(mobile): keep Smart rail on through Reset Defaults and finish two-write keys * fix(mobile): only a finger scrolling the key row cancels its presses * fix(mobile): hold the smart group until the tap's press lands, and drop clicks on keys that moved * refactor(mobile): simplify the smart rail touch hook and layout read * chore(mobile): allowlist the Shift, Ctrl and Alt key names * feat(mobile): Codex async question keys on the smart rail * fix(mobile): drop the Codex Always key, whose key differs by approval Codex binds "don't ask again" to p for a command-prefix rule and a for session and file approvals (0.136 through 0.166). The permission hook doesn't say which one is on screen. --- ...obile-localization-coverage-allowlist.json | 21 ++ .../src/components/CustomKeyModal.styles.ts | 3 + mobile/src/components/CustomKeyModal.tsx | 10 +- mobile/src/components/SmartRailRow.tsx | 24 +++ .../components/TerminalShortcutSettings.tsx | 22 +- mobile/src/i18n/locales/en.json | 23 ++ .../src/session/MobileSessionCommandDock.tsx | 10 +- mobile/src/session/MobileSessionSheets.tsx | 3 + .../session/use-mobile-session-lifecycle.ts | 21 +- .../use-mobile-session-screen-state.ts | 3 + .../session/MobileSessionAccessoryKeys.tsx | 163 +++++++++++--- .../session/MobileSessionLatchKeys.tsx | 58 +++++ .../session/MobileSessionSmartKeys.tsx | 112 ++++++++++ .../session/mobile-terminal-tab-agent.ts | 21 ++ .../terminal/session/terminal-dock-styles.ts | 54 +++++ .../session/use-terminal-dock-keys.ts | 6 +- .../session/use-terminal-key-row-touch.ts | 141 ++++++++++++ .../src/terminal/terminal-accessory-layout.ts | 69 ++++-- .../terminal/input/agent-smart-keys.test.ts | 71 +++++++ src/shared/terminal/input/agent-smart-keys.ts | 200 ++++++++++++++++++ 20 files changed, 967 insertions(+), 68 deletions(-) create mode 100644 mobile/src/components/SmartRailRow.tsx create mode 100644 mobile/src/terminal/session/MobileSessionLatchKeys.tsx create mode 100644 mobile/src/terminal/session/MobileSessionSmartKeys.tsx create mode 100644 mobile/src/terminal/session/use-terminal-key-row-touch.ts create mode 100644 src/shared/terminal/input/agent-smart-keys.test.ts create mode 100644 src/shared/terminal/input/agent-smart-keys.ts diff --git a/config/mobile-localization-coverage-allowlist.json b/config/mobile-localization-coverage-allowlist.json index 50d8243f0f7..265dd13cf28 100644 --- a/config/mobile-localization-coverage-allowlist.json +++ b/config/mobile-localization-coverage-allowlist.json @@ -307,6 +307,27 @@ "dynamic": false, "count": 1 }, + { + "filePath": "mobile/src/terminal/session/MobileSessionLatchKeys.tsx", + "kind": "object-property:label", + "text": "Shift", + "dynamic": false, + "count": 1 + }, + { + "filePath": "mobile/src/terminal/session/MobileSessionLatchKeys.tsx", + "kind": "object-property:label", + "text": "Ctrl", + "dynamic": false, + "count": 1 + }, + { + "filePath": "mobile/src/terminal/session/MobileSessionLatchKeys.tsx", + "kind": "object-property:label", + "text": "Alt", + "dynamic": false, + "count": 1 + }, { "filePath": "mobile/src/tasks/mobile-smart-source-modes.ts", "kind": "object-property:label", diff --git a/mobile/src/components/CustomKeyModal.styles.ts b/mobile/src/components/CustomKeyModal.styles.ts index a95b5abe3be..2921025e632 100644 --- a/mobile/src/components/CustomKeyModal.styles.ts +++ b/mobile/src/components/CustomKeyModal.styles.ts @@ -41,6 +41,9 @@ export const customKeyModalStyles = themedStyleSheet(() => ({ borderRadius: radii.surface, overflow: 'hidden' }, + smartRailGroup: { + marginTop: spacing.md + }, shortcutForm: { paddingTop: spacing.sm }, diff --git a/mobile/src/components/CustomKeyModal.tsx b/mobile/src/components/CustomKeyModal.tsx index b6dfac3cdc6..42afc17fd3c 100644 --- a/mobile/src/components/CustomKeyModal.tsx +++ b/mobile/src/components/CustomKeyModal.tsx @@ -19,6 +19,7 @@ import { useMobileTranslation } from '../i18n/use-mobile-translation' import { MobileSwitch } from './MobileSwitch' import { ListGroup, ListRow } from './list/ListGroup' import { FieldTextInput } from './composer-input/FieldTextInput' +import { SmartRailRow } from './SmartRailRow' type Step = 'choose-type' | 'shortcut-combo' | 'special-keys' | 'text-macro' @@ -62,9 +63,11 @@ type Props = { onClose: () => void onKeysChanged: (keys: CustomKey[]) => void onManageShortcuts?: () => void + smartRail?: { enabled: boolean; onChange: (enabled: boolean) => void } } -export function CustomKeyModal({ visible, onClose, onKeysChanged, onManageShortcuts }: Props) { +export function CustomKeyModal(props: Props) { + const { visible, onClose, onKeysChanged, onManageShortcuts, smartRail } = props useMobileColorScheme() const t = useMobileTranslation() const [step, setStep] = useState('choose-type') @@ -235,6 +238,11 @@ export function CustomKeyModal({ visible, onClose, onKeysChanged, onManageShortc ) : null} )} + {step === 'choose-type' && smartRail ? ( + + + + ) : null} {step === 'shortcut-combo' && ( diff --git a/mobile/src/components/SmartRailRow.tsx b/mobile/src/components/SmartRailRow.tsx new file mode 100644 index 00000000000..8789fcd0f42 --- /dev/null +++ b/mobile/src/components/SmartRailRow.tsx @@ -0,0 +1,24 @@ +import { ListRow } from './list/ListGroup' +import { MobileSwitch } from './MobileSwitch' +import { useMobileTranslation } from '../i18n/use-mobile-translation' + +/** The Smart rail setting: keys for what the terminal's agent is doing, at the front of the key row. */ +export function SmartRailRow({ + enabled, + onChange +}: { + enabled: boolean + onChange: (enabled: boolean) => void +}): React.JSX.Element { + const t = useMobileTranslation() + const title = t('components.smartRailRow.title', 'Smart rail') + return ( + + } + /> + ) +} diff --git a/mobile/src/components/TerminalShortcutSettings.tsx b/mobile/src/components/TerminalShortcutSettings.tsx index db5bfe5b64e..37e14d0620f 100644 --- a/mobile/src/components/TerminalShortcutSettings.tsx +++ b/mobile/src/components/TerminalShortcutSettings.tsx @@ -27,6 +27,7 @@ import { setTerminalAccessoryBuiltInVisible } from '../terminal/terminal-accessory-layout' import { MobileSwitch } from './MobileSwitch' +import { SmartRailRow } from './SmartRailRow' // Absolutely positioned DragReorderList rows require one fixed height per section. const REORDER_ROW_HEIGHT = 56 @@ -148,10 +149,13 @@ export function TerminalShortcutSettings({ [persistLayout] ) + // Resetting the keys leaves the Smart rail setting alone. const resetBuiltInKeys = useCallback(() => { - const next = defaultLayout() - setShortcutLayout(next) - persistLayout(next) + setShortcutLayout((current) => { + const next = { ...defaultLayout(), smartRail: current.smartRail } + persistLayout(next) + return next + }) }, [defaultLayout, persistLayout]) const reorderCustomKeys = useCallback( @@ -182,6 +186,18 @@ export function TerminalShortcutSettings({ return ( <> + + + setShortcutLayout((current) => { + const next = { ...current, smartRail: enabled } + persistLayout(next) + return next + }) + } + /> + {hasPad ? ( { - // A ctrl/alt/shift held on the Duo rail turns the next typed letter into a combo, while the rail shows it. - if ( - !keysInRail || - !dockKeys.interceptTypedCharacter(text, event.nativeEvent.text, liveInputRef) - ) { + // A held Shift, Ctrl or Alt on the key row or the Duo rail turns the next typed letter into a combo. + if (!dockKeys.interceptTypedCharacter(text, event.nativeEvent.text, liveInputRef)) { handleLiveInputChange(event) } }} diff --git a/mobile/src/session/MobileSessionSheets.tsx b/mobile/src/session/MobileSessionSheets.tsx index 027af33cf82..fe6f590a3d2 100644 --- a/mobile/src/session/MobileSessionSheets.tsx +++ b/mobile/src/session/MobileSessionSheets.tsx @@ -77,6 +77,8 @@ export function MobileSessionSheets({ controller }: { controller: MobileSessionC confirmDiscardMarkdown, handleDeleteCustomKey, handleManageShortcuts, + smartRailEnabled, + handleSmartRailChange, handleClearTerminal, handleCreateTerminal, launchQuickCommand, @@ -363,6 +365,7 @@ export function MobileSessionSheets({ controller }: { controller: MobileSessionC onClose={() => setShowCustomKeyModal(false)} onKeysChanged={setCustomKeys} onManageShortcuts={handleManageShortcuts} + smartRail={{ enabled: smartRailEnabled, onChange: handleSmartRailChange }} /> { + // A read that started before the toggle must not flip it back. + layoutReadRevisionRef.current += 1 + setSmartRailEnabled(enabled) + saveTerminalSmartRail(enabled, padAvailable).catch((error: unknown) => { + console.warn('[terminal-keys] the store would not take the Smart rail setting', error) + setSmartRailEnabled(!enabled) + }) + }, + [padAvailable] + ) + useFocusEffect( useCallback(() => { const releaseKeys = refreshCustomKeys() @@ -195,6 +213,7 @@ export function useMobileSessionLifecycle(scope: MobileSessionTabReconciliationM }, [connState, scheduleDelayedAction, subscribeToTerminal, terminalResume, unsubscribeTerminal]) return { accessoryPreferencesReady: customKeysLoaded && accessoryLayoutLoaded, + handleSmartRailChange, pendingForegroundRecoveryRef } } diff --git a/mobile/src/session/use-mobile-session-screen-state.ts b/mobile/src/session/use-mobile-session-screen-state.ts index eb0f7d28b74..bde653c4045 100644 --- a/mobile/src/session/use-mobile-session-screen-state.ts +++ b/mobile/src/session/use-mobile-session-screen-state.ts @@ -82,6 +82,7 @@ export function useMobileSessionScreenState(scope: MobileSessionFoundationModel) const [renameTarget, setRenameTarget] = useState(null) const [customKeys, setCustomKeys] = useState([]) const [directionPadEnabled, setDirectionPadEnabled] = useState(true) + const [smartRailEnabled, setSmartRailEnabled] = useState(false) const [visibleBuiltInIds, setVisibleBuiltInIds] = useState( getDefaultTerminalAccessoryBuiltInIds ) @@ -182,6 +183,8 @@ export function useMobileSessionScreenState(scope: MobileSessionFoundationModel) setCustomKeys, directionPadEnabled, setDirectionPadEnabled, + smartRailEnabled, + setSmartRailEnabled, visibleBuiltInIds, setVisibleBuiltInIds, showCustomKeyModal, diff --git a/mobile/src/terminal/session/MobileSessionAccessoryKeys.tsx b/mobile/src/terminal/session/MobileSessionAccessoryKeys.tsx index f88d4d336c7..737327fab48 100644 --- a/mobile/src/terminal/session/MobileSessionAccessoryKeys.tsx +++ b/mobile/src/terminal/session/MobileSessionAccessoryKeys.tsx @@ -21,6 +21,17 @@ import { styles as sessionStyles } from '../../session/mobile-session-styles' import { useKeyboardPersistingTaps } from '../../platform/keyboard-persisting-taps' import type { MobileSessionController } from '../../session/use-mobile-session-controller' import { useMobileSessionKeyboardLift } from './use-mobile-session-keyboard-lift' +import { + AGENT_SMART_KEY_STEP_MS, + type AgentSmartKey +} from '../../../../src/shared/terminal/input/agent-smart-keys' +import type { TerminalAccessoryKey } from '../terminal-accessory-keys' +import { resolveMobileTerminalTabSmartKeys } from './mobile-terminal-tab-agent' +import { MobileSessionSmartKeys } from './MobileSessionSmartKeys' +import { MobileSessionLatchKeys } from './MobileSessionLatchKeys' +import { isAccessibilityClick, useTerminalKeyRowTouch } from './use-terminal-key-row-touch' + +type PreparedPress = ReturnType // The terminal's key row above the live input; every key goes straight to the terminal. export function MobileSessionAccessoryKeys({ @@ -46,10 +57,26 @@ export function MobileSessionAccessoryKeys({ stopAccessoryRepeat, cancelAccessoryRepeat, prepareAccessoryPress, - handlePaste + handlePaste, + dockKeys, + activeSessionTab, + terminals, + smartRailEnabled } = controller const keyboardLift = useMobileSessionKeyboardLift(controller.dockBottomInset) - const accessoryPresses = useRef(new Map>()) + const accessoryPresses = useRef(new Map()) + const scrollRef = useRef(null) + const latestSmartKeys = useMemo( + () => + smartRailEnabled ? resolveMobileTerminalTabSmartKeys(activeSessionTab, terminals) : null, + [smartRailEnabled, activeSessionTab, terminals] + ) + const touch = useTerminalKeyRowTouch({ + scrollRef, + latest: latestSmartKeys, + cancelAccessoryRepeat + }) + const smartKeys = touch.shown const keepsKeyboard = useKeyboardPersistingTaps('always') // An unmounting Pressable never fires onPressOut, so a held key would outlive its row. useEffect(() => cancelAccessoryRepeat, [cancelAccessoryRepeat]) @@ -70,8 +97,57 @@ export function MobileSessionAccessoryKeys({ ] const sendKeyLabel = (label: string) => t('session.accessoryKeys.sendKey', 'Send {{label}}', { label }) + // Held Shift, Ctrl or Alt turn a key into its combo and are used up when it sends; keys that take none send as they are. + const prepareBuiltInPress = (key: TerminalAccessoryKey): PreparedPress => { + const chord = dockKeys.chordInput(key.id) + const press = prepareAccessoryPress(chord ?? createTerminalLiveAccessoryInput(key)) + const takeChord = () => { + if (chord) { + dockKeys.takeModifiers() + } + } + return { + tap: () => { + takeChord() + return press.tap() + }, + hold: (firstRepeatDelayMs) => { + takeChord() + press.hold(firstRepeatDelayMs) + } + } + } + // Later writes belong to the same press, so a scroll after release mustn't drop them; a tab switch does. + const sendSmartKey = (key: AgentSmartKey) => { + const handle = controller.activeHandleRef.current + void (async () => { + for (const [index, bytes] of key.writes.entries()) { + if (index > 0) { + await new Promise((resolve) => setTimeout(resolve, AGENT_SMART_KEY_STEP_MS)) + } + if (controller.activeHandleRef.current !== handle) { + return + } + if (!(await prepareAccessoryPress({ bytes }).tap())) { + return + } + } + })() + } + const latchIndex = builtInKeys.findIndex((key) => key.id === 'tab') + 1 + const latchKeys = ( + touch.arm('latch')} + onPress={(modifier, event) => + touch.fire('latch', event, () => dockKeys.tapModifier(modifier)) + } + /> + ) return ( - + {/* Why: fixed keyboard escape hatch; outside the ScrollView so it can't scroll away (#5106). */} {keyboardLift > 0 && ( + {smartKeys ? ( + touch.arm(`smart:${key.id}`)} + onPress={(key, event) => touch.fire(`smart:${key.id}`, event, () => sendSmartKey(key))} + /> + ) : null} { - if (activeHandle) { - void toggleDisplayMode(activeHandle) - } - }} + onPressIn={() => touch.arm('displayMode')} + onPress={(event) => + touch.fire('displayMode', event, () => { + if (activeHandle) { + void toggleDisplayMode(activeHandle) + } + }) + } accessibilityRole="button" accessibilityLabel={ phoneDisplay @@ -133,7 +222,8 @@ export function MobileSessionAccessoryKeys({ style={keyStyle} disabled={!canSend} hitSlop={TERMINAL_KEY_HIT_SLOP} - onPress={() => void handlePaste()} + onPressIn={() => touch.arm('paste')} + onPress={(event) => touch.fire('paste', event, () => void handlePaste())} accessibilityRole="button" accessibilityLabel={t( 'session.accessoryKeys.pasteFromClipboard', @@ -143,20 +233,22 @@ export function MobileSessionAccessoryKeys({ {t('session.accessoryKeys.paste', 'Paste')} )} - {builtInKeys.map((key) => ( + {latchIndex === 0 ? latchKeys : null} + {builtInKeys.flatMap((key, index) => [ { + touch.arm(key.id) if (!key.repeatable) { return } - const press = prepareAccessoryPress(createTerminalLiveAccessoryInput(key)) - accessoryPresses.current.set(key.id, press) + const prepared = prepareBuiltInPress(key) + accessoryPresses.current.set(key.id, prepared) if (event.type === 'keydown') { - press.hold(TERMINAL_ACCESSORY_REPEAT_DELAY_MS) + prepared.hold(TERMINAL_ACCESSORY_REPEAT_DELAY_MS) } }} onPressOut={() => { @@ -165,23 +257,23 @@ export function MobileSessionAccessoryKeys({ } }} onPress={(event) => { - if (key.repeatable) { - // Accessibility clicks have no touch-down to prepare a sender. - const accessibilityClick = - event.type === 'click' && - 'detail' in event.nativeEvent && - event.nativeEvent.detail === 0 - const press = - (!accessibilityClick && accessoryPresses.current.get(key.id)) || - prepareAccessoryPress(createTerminalLiveAccessoryInput(key)) - accessoryPresses.current.delete(key.id) - void press.tap() - return - } - void handleAccessoryKey(createTerminalLiveAccessoryInput(key)) + // Accessibility clicks have no touch-down to prepare a sender. + const prepared = + (key.repeatable && + !isAccessibilityClick(event) && + accessoryPresses.current.get(key.id)) || + prepareBuiltInPress(key) + accessoryPresses.current.delete(key.id) + touch.fire(key.id, event, () => void prepared.tap()) }} onLongPress={ - key.repeatable ? () => accessoryPresses.current.get(key.id)?.hold() : undefined + key.repeatable + ? () => { + if (touch.stillArmed(key.id)) { + accessoryPresses.current.get(key.id)?.hold() + } + } + : undefined } delayLongPress={TERMINAL_ACCESSORY_REPEAT_DELAY_MS} // A web