From 0b1efd9b907eacc6a0f6805300cbb874d98dee9c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:31:49 -0800 Subject: [PATCH 01/80] fix(vm-recipes): record each VM before create runs so a quit or crash can't leak it (#39241) * fix(vm-recipes): record each VM before create runs so a quit or crash can't leak it Orca now saves a "provisioning" record for a recipe VM before running its create command, and updates it when create finishes. If Orca quits or crashes mid-create, or create fails without returning a result, the VM stays listed in Settings > Cloud VMs as Failed with its instance id, so the user can run Cleanup (destroy gets the instance id) or remove it from the list. On startup, provisioning records with no create running are marked as interrupted. Records without a recipe result live in a separate file that older builds never read, so downgrading keeps working. Records also store the repo path so Cleanup, Copy command, suspend and resume still work after the repo is removed or re-added under a new id (#17293). * fix(vm-recipes): keep no runtime row when create is cancelled or rejected before its script starts * fix(vm-recipes): harden write-ahead records from review - Never overwrite a provisioning-records file this build cannot read; refuse new records instead. - Remove the provisioning-records file when empty instead of rewriting it on every store write. - Only fall back to the saved repo path while it still defines the same recipe. - Don't let Cleanup or Attach act on a create that is still running or never returned a result. - Show Provisioning before Cleanup disabled; stamp running records with the save time. * fix(vm-recipes): keep new store code off direct sync fs calls and render-path translate * fix(vm-recipes): detect a missing provisioning-records file without a type assertion --- skill-guides/orca-per-workspace-env.md | 6 + src/cli/bundled-skill-guides.ts | 6 + src/main/ephemeral-vm/failed-start-cleanup.ts | 4 +- src/main/ephemeral-vm/runtime-attachment.ts | 4 + .../runtime-provisioning-persistence.ts | 216 +++++++++++++++--- src/main/ephemeral-vm/runtime-service.test.ts | 173 +++++++++++++- src/main/ephemeral-vm/runtime-service.ts | 80 ++++--- .../workspace-resume-user-disconnect.test.ts | 6 +- src/main/ephemeral-vm/workspace-resume.ts | 11 +- src/main/ephemeral-vm/workspace-suspend.ts | 4 +- src/main/ipc/ephemeral-vm-recipe-context.ts | 27 ++- ...hemeral-vm-runtime-handler-cleanup.test.ts | 184 +++++++++++++++ src/main/ipc/ephemeral-vm-runtime-handlers.ts | 18 +- src/main/ipc/ephemeral-vm.ts | 27 ++- .../startup/main-process-runtime-service.ts | 2 +- .../catalog/provisioned-root-ssh-adoption.ts | 8 +- src/preload/api/ephemeral-vm-api.ts | 11 +- src/preload/api/ephemeral-vm-bridge.ts | 1 + .../runtimes/EphemeralVmRuntimeRow.tsx | 41 +++- .../runtimes/EphemeralVmRuntimesSection.tsx | 30 ++- .../src/i18n/en-runtime-required.json | 1 + src/renderer/src/i18n/locales/en.json | 9 +- .../src/lib/ephemeral-vm-error-copy.ts | 2 + src/shared/ephemeral-vm-failure-copy.ts | 2 + .../ephemeral-vm-recipe-lifecycle-payload.ts | 8 +- src/shared/ephemeral-vm-recipe-process.ts | 9 + src/shared/ephemeral-vm-recipe-runner.ts | 4 +- src/shared/ephemeral-vm-recipe-step.ts | 2 + .../ephemeral-vm-runtime-feature-store.ts | 4 +- ...phemeral-vm-runtime-rollback-projection.ts | 21 +- ...phemeral-vm-runtime-store-rollback.test.ts | 95 ++++++++ src/shared/ephemeral-vm-runtime-store.ts | 54 ++--- .../ephemeral-vm-runtime-write-ahead-store.ts | 89 ++++++++ src/shared/ephemeral-vm-runtimes.ts | 24 +- src/shared/ephemeral-vm-workspace-runtime.ts | 21 ++ 35 files changed, 1047 insertions(+), 157 deletions(-) create mode 100644 src/shared/ephemeral-vm-runtime-write-ahead-store.ts create mode 100644 src/shared/ephemeral-vm-workspace-runtime.ts diff --git a/skill-guides/orca-per-workspace-env.md b/skill-guides/orca-per-workspace-env.md index 56c62d01d93..bb27ceabc80 100644 --- a/skill-guides/orca-per-workspace-env.md +++ b/skill-guides/orca-per-workspace-env.md @@ -304,6 +304,12 @@ timeoutSeconds: A timed-out `create` may still have started a VM, so make `create` idempotent per `ORCA_VM_INSTANCE_ID` or have it record the resource before slow work starts. +Orca records each VM before `create` runs. If `create` started but never returned a result (it +failed, timed out, or Orca quit), Settings keeps the VM listed so the user can clean it up; if it +was cancelled or rejected before starting, nothing is kept. That destroy +payload has `instanceId` but no `recipeResult`, so `destroy` should find the resource from the +instance id (for example, a name built from `ORCA_VM_INSTANCE_ID`) or exit non-zero if it can't. + The base result, which is what Orca-server mode prints: ```json diff --git a/src/cli/bundled-skill-guides.ts b/src/cli/bundled-skill-guides.ts index 4be59c4941d..27f797ad678 100644 --- a/src/cli/bundled-skill-guides.ts +++ b/src/cli/bundled-skill-guides.ts @@ -2088,6 +2088,12 @@ const ORCA_PER_WORKSPACE_ENV_MARKDOWN = [ "A timed-out `create` may still have started a VM, so make `create` idempotent per", "`ORCA_VM_INSTANCE_ID` or have it record the resource before slow work starts.", "", + "Orca records each VM before `create` runs. If `create` started but never returned a result (it", + "failed, timed out, or Orca quit), Settings keeps the VM listed so the user can clean it up; if it", + "was cancelled or rejected before starting, nothing is kept. That destroy", + "payload has `instanceId` but no `recipeResult`, so `destroy` should find the resource from the", + "instance id (for example, a name built from `ORCA_VM_INSTANCE_ID`) or exit non-zero if it can't.", + "", "The base result, which is what Orca-server mode prints:", "", "```json", diff --git a/src/main/ephemeral-vm/failed-start-cleanup.ts b/src/main/ephemeral-vm/failed-start-cleanup.ts index c27bd207f4d..7a1eaf9921d 100644 --- a/src/main/ephemeral-vm/failed-start-cleanup.ts +++ b/src/main/ephemeral-vm/failed-start-cleanup.ts @@ -24,6 +24,7 @@ import { type FailedStart = { context: EphemeralVmRecipeContext recipeResult: EphemeralVmRecipeResult + createdAt?: number } export async function cleanupFailedEphemeralVmStart( @@ -42,6 +43,7 @@ export async function cleanupFailedEphemeralVmStart( recipeId: args.recipe.id, recipe: toEphemeralVmRuntimeRecipeSnapshot(args.recipe), ...(args.repoId ? { repoId: args.repoId } : {}), + ...(args.repoPath ? { repoPath: args.repoPath } : {}), ...(args.projectId ? { projectId: args.projectId } : {}), ...(args.workspaceId ? { workspaceId: args.workspaceId } : {}), ...(args.workspaceName ? { workspaceName: args.workspaceName } : {}), @@ -51,7 +53,7 @@ export async function cleanupFailedEphemeralVmStart( ...(args.recipe.destroyDisabled ? { cleanupDisabled: true } : {}), cleanupLastAttemptAt: now, ...toCleanupLastErrorFields(cleanupError), - createdAt: now, + createdAt: start.createdAt ?? now, updatedAt: now, recipeResult: start.recipeResult } diff --git a/src/main/ephemeral-vm/runtime-attachment.ts b/src/main/ephemeral-vm/runtime-attachment.ts index 682b2bc596a..ed39f1b6570 100644 --- a/src/main/ephemeral-vm/runtime-attachment.ts +++ b/src/main/ephemeral-vm/runtime-attachment.ts @@ -15,6 +15,10 @@ export function attachEphemeralVmRuntimeToWorkspace(args: { if (!runtime) { throw new Error(`Unknown ephemeral VM runtime: ${args.runtimeId}`) } + if (!runtime.recipeResult) { + // Write-ahead rows have no VM details to attach; they must stay destroyable as they are. + throw new Error(`Ephemeral VM runtime never finished provisioning: ${args.runtimeId}`) + } if (['cleanup_pending', 'cleanup_failed', 'cleaned'].includes(runtime.status)) { throw new Error(`Cannot attach cleaned ephemeral VM runtime: ${args.runtimeId}`) } diff --git a/src/main/ephemeral-vm/runtime-provisioning-persistence.ts b/src/main/ephemeral-vm/runtime-provisioning-persistence.ts index 3faeca9ad42..957bf90b87b 100644 --- a/src/main/ephemeral-vm/runtime-provisioning-persistence.ts +++ b/src/main/ephemeral-vm/runtime-provisioning-persistence.ts @@ -5,79 +5,221 @@ import { getEphemeralVmRecipeResultConnection } from '../../shared/ephemeral-vm- import { listEphemeralVmRuntimes, removeEphemeralVmRuntime, + updateEphemeralVmRuntimeStatus, upsertEphemeralVmRuntime } from '../../shared/ephemeral-vm-runtime-store' import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes' +import { + ephemeralVmFailure, + ephemeralVmFailureFromError, + pickEphemeralVmFailureCode, + type EphemeralVmFailure +} from '../../shared/ephemeral-vm-failure-copy' +import type { EphemeralVmRecipeStartFailure } from '../../shared/ephemeral-vm-recipe-runner' import type { ProvisionEphemeralVmRuntimeArgs, ProvisionEphemeralVmRuntimeResult } from './runtime-service' import { cleanupFailedEphemeralVmStart } from './failed-start-cleanup' -export type EphemeralVmCompatibilityPersistence = { +export type EphemeralVmWriteAhead = { instanceId: string createdAt: number + /** Set once the create command's process started; before that no VM can exist. */ + launched: boolean } -export function prepareEphemeralVmCompatibilityPersistence( +// Creates owned by this process; any other `provisioning` record was orphaned by a crash or quit. +const liveProvisionInstanceIds = new Set() + +export function isEphemeralVmProvisionLive(instanceId: string): boolean { + return liveProvisionInstanceIds.has(instanceId) +} + +/** Persist the `provisioning` record before spawning create so a crash cannot leak an untracked VM. */ +export function beginEphemeralVmProvisioning( args: ProvisionEphemeralVmRuntimeArgs -): EphemeralVmCompatibilityPersistence | null { - if (!args.recipe.checkoutMode) { - return null - } - listEphemeralVmRuntimes(args.userDataPath) - const compatibility = { +): EphemeralVmWriteAhead { + const writeAhead: EphemeralVmWriteAhead = { instanceId: `orca-${randomUUID()}`, - createdAt: args.now ?? Date.now() + createdAt: args.now ?? Date.now(), + launched: false } - assertEphemeralVmRuntimeCheckoutModeCanPersist(args.userDataPath, { - id: compatibility.instanceId, - recipeId: args.recipe.id, - createdAt: compatibility.createdAt, - checkoutMode: args.recipe.checkoutMode + if (args.recipe.checkoutMode) { + assertEphemeralVmRuntimeCheckoutModeCanPersist(args.userDataPath, { + id: writeAhead.instanceId, + recipeId: args.recipe.id, + createdAt: writeAhead.createdAt, + checkoutMode: args.recipe.checkoutMode + }) + } + upsertEphemeralVmRuntime(args.userDataPath, { + ...baseRuntimeRecord(args, writeAhead), + status: 'provisioning', + updatedAt: writeAhead.createdAt }) - return compatibility + liveProvisionInstanceIds.add(writeAhead.instanceId) + return writeAhead +} + +export function endEphemeralVmProvisioning( + args: ProvisionEphemeralVmRuntimeArgs, + writeAhead: EphemeralVmWriteAhead, + error?: unknown +): void { + liveProvisionInstanceIds.delete(writeAhead.instanceId) + if (error === undefined) { + return + } + try { + const record = findRuntime(args.userDataPath, writeAhead.instanceId) + if (record?.status === 'provisioning' && !writeAhead.launched) { + removeEphemeralVmRuntime(args.userDataPath, record.id) + } else if (record?.status === 'provisioning') { + markProvisionFailed(args.userDataPath, record.id, ephemeralVmFailureFromError(error)) + } + } catch { + // Why: surface the original provisioning error; startup reconciliation retries the flip. + } +} + +export async function persistFailedEphemeralVmStart( + args: ProvisionEphemeralVmRuntimeArgs, + writeAhead: EphemeralVmWriteAhead, + start: EphemeralVmRecipeStartFailure +): Promise { + if (!start.recipeResult && !writeAhead.launched) { + // Why: create never ran (validation failed or it was cancelled first), so no VM can exist. + removeIfPresent(args.userDataPath, writeAhead.instanceId) + return + } + if (!start.recipeResult) { + // Why: create may have allocated resources before failing; keep the row so the user can destroy it. + markProvisionFailed(args.userDataPath, writeAhead.instanceId, { + error: start.error, + ...pickEphemeralVmFailureCode(start) + }) + return + } + const cleaned = await cleanupFailedEphemeralVmStart(args, { + context: start.context, + recipeResult: start.recipeResult, + createdAt: writeAhead.createdAt + }) + if (cleaned) { + removeIfPresent(args.userDataPath, writeAhead.instanceId) + } } export async function persistProvisionedEphemeralVmRuntime( args: ProvisionEphemeralVmRuntimeArgs, start: Extract['start'], - compatibility: EphemeralVmCompatibilityPersistence | null + writeAhead: EphemeralVmWriteAhead ): Promise { - const now = compatibility?.createdAt ?? args.now ?? Date.now() const connection = getEphemeralVmRecipeResultConnection(start.result) try { return upsertEphemeralVmRuntime(args.userDataPath, { - id: start.context.instanceId ?? start.context.recipeId, - recipeId: args.recipe.id, - recipe: toEphemeralVmRuntimeRecipeSnapshot(args.recipe), - ...(args.repoId ? { repoId: args.repoId } : {}), - ...(args.projectId ? { projectId: args.projectId } : {}), - ...(args.workspaceId ? { workspaceId: args.workspaceId } : {}), - ...(args.workspaceName ? { workspaceName: args.workspaceName } : {}), + ...baseRuntimeRecord(args, writeAhead), status: 'running', connectionMode: connection.type, - cleanupStatus: args.recipe.destroyDisabled ? 'disabled' : 'not_started', - ...(args.recipe.destroyDisabled ? { cleanupDisabled: true } : {}), - createdAt: now, - updatedAt: now, + updatedAt: args.now ?? Date.now(), recipeResult: start.result }) } catch (error) { - if (compatibility) { + if (args.recipe.checkoutMode) { const cleaned = await cleanupFailedEphemeralVmStart(args, { context: start.context, - recipeResult: start.result + recipeResult: start.result, + createdAt: writeAhead.createdAt }) - if ( - cleaned && - listEphemeralVmRuntimes(args.userDataPath).some( - (runtime) => runtime.id === compatibility.instanceId - ) - ) { - removeEphemeralVmRuntime(args.userDataPath, compatibility.instanceId) + if (cleaned) { + removeIfPresent(args.userDataPath, writeAhead.instanceId) } } throw error } } + +/** Flip `provisioning` records with no live create in this process to `failed` so Settings offers Destroy. */ +export function reconcileInterruptedEphemeralVmProvisions( + userDataPath: string, + now = Date.now() +): EphemeralVmRuntimeRecord[] { + return listEphemeralVmRuntimes(userDataPath) + .filter( + (runtime) => runtime.status === 'provisioning' && !isEphemeralVmProvisionLive(runtime.id) + ) + .map((runtime) => + markProvisionFailed(userDataPath, runtime.id, ephemeralVmFailure('provisionInterrupted'), now) + ) +} + +export function reconcileInterruptedEphemeralVmProvisionsOnStartup(userDataPath: string): void { + try { + reconcileInterruptedEphemeralVmProvisions(userDataPath) + } catch (error) { + console.warn('[ephemeral-vm] Could not reconcile interrupted provisions:', error) + } +} + +/** Tracked VMs must go through Destroy; only result-less write-ahead rows can be dropped by hand. */ +export function forgetInterruptedEphemeralVmProvision( + userDataPath: string, + runtimeId: string +): void { + const runtime = findRuntime(userDataPath, runtimeId) + if (!runtime) { + return + } + if ( + runtime.recipeResult || + runtime.status === 'cleanup_pending' || + (runtime.status === 'provisioning' && isEphemeralVmProvisionLive(runtime.id)) + ) { + throw new Error(`Ephemeral VM runtime must be destroyed, not removed: ${runtimeId}`) + } + removeEphemeralVmRuntime(userDataPath, runtimeId) +} + +function markProvisionFailed( + userDataPath: string, + runtimeId: string, + failure: EphemeralVmFailure, + now = Date.now() +): EphemeralVmRuntimeRecord { + // cleanupLastError is the row's displayed error, which older builds also render. + return updateEphemeralVmRuntimeStatus(userDataPath, runtimeId, { + status: 'failed', + cleanupLastError: failure, + updatedAt: now + }) +} + +function baseRuntimeRecord( + args: ProvisionEphemeralVmRuntimeArgs, + writeAhead: EphemeralVmWriteAhead +): Omit { + return { + id: writeAhead.instanceId, + recipeId: args.recipe.id, + recipe: toEphemeralVmRuntimeRecipeSnapshot(args.recipe), + ...(args.repoId ? { repoId: args.repoId } : {}), + ...(args.repoPath ? { repoPath: args.repoPath } : {}), + ...(args.projectId ? { projectId: args.projectId } : {}), + ...(args.workspaceId ? { workspaceId: args.workspaceId } : {}), + ...(args.workspaceName ? { workspaceName: args.workspaceName } : {}), + cleanupStatus: args.recipe.destroyDisabled ? 'disabled' : 'not_started', + ...(args.recipe.destroyDisabled ? { cleanupDisabled: true } : {}), + createdAt: writeAhead.createdAt + } +} + +function findRuntime(userDataPath: string, id: string): EphemeralVmRuntimeRecord | undefined { + return listEphemeralVmRuntimes(userDataPath).find((runtime) => runtime.id === id) +} + +function removeIfPresent(userDataPath: string, id: string): void { + if (findRuntime(userDataPath, id)) { + removeEphemeralVmRuntime(userDataPath, id) + } +} diff --git a/src/main/ephemeral-vm/runtime-service.test.ts b/src/main/ephemeral-vm/runtime-service.test.ts index d65a7fa99b3..8b0eb7b74a9 100644 --- a/src/main/ephemeral-vm/runtime-service.test.ts +++ b/src/main/ephemeral-vm/runtime-service.test.ts @@ -217,7 +217,42 @@ describe('ephemeral VM runtime service', () => { expect(readFileSync(countPath, 'utf8')).toBe('xx') }) - it('does not persist a runtime when recipe output cannot be parsed', async () => { + it('keeps no record when create is cancelled before its script starts', async () => { + const userDataPath = makeDir('orca-ephemeral-vm-service-user-data-') + const repoPath = makeDir('orca-ephemeral-vm-service-repo-') + const startedPath = join(repoPath, 'started.txt') + const startPath = join(repoPath, 'start.js') + writeFileSync(startPath, `require('fs').writeFileSync(${JSON.stringify(startedPath)}, 'x')\n`) + const controller = new AbortController() + controller.abort() + + const provisioned = await provisionEphemeralVmRuntime({ + userDataPath, + repoPath, + recipe: { id: 'cloud-sandbox', name: 'Cloud Sandbox', create: nodeCommand(startPath) }, + signal: controller.signal + }) + + expect(provisioned).toMatchObject({ ok: false, start: { exitCode: null } }) + expect(existsSync(startedPath)).toBe(false) + expect(listEphemeralVmRuntimes(userDataPath)).toEqual([]) + }) + + it('keeps no record when create fails validation before its script starts', async () => { + const userDataPath = makeDir('orca-ephemeral-vm-service-user-data-') + const missingRepoPath = join(makeDir('orca-ephemeral-vm-service-repo-'), 'missing') + + await expect( + provisionEphemeralVmRuntime({ + userDataPath, + repoPath: missingRepoPath, + recipe: { id: 'cloud-sandbox', name: 'Cloud Sandbox', create: 'unused' } + }) + ).rejects.toThrow() + expect(listEphemeralVmRuntimes(userDataPath)).toEqual([]) + }) + + it('keeps a result-less failed record when recipe output cannot be parsed', async () => { const userDataPath = makeDir('orca-ephemeral-vm-service-user-data-') const repoPath = makeDir('orca-ephemeral-vm-service-repo-') const startPath = join(repoPath, 'start.js') @@ -230,7 +265,8 @@ describe('ephemeral VM runtime service', () => { id: 'cloud-sandbox', name: 'Cloud Sandbox', create: nodeCommand(startPath) - } + }, + now: 1_000 }) expect(provisioned).toMatchObject({ @@ -239,7 +275,138 @@ describe('ephemeral VM runtime service', () => { error: 'Recipe stdout must be one JSON object.' } }) - expect(listEphemeralVmRuntimes(userDataPath)).toEqual([]) + const runtimes = listEphemeralVmRuntimes(userDataPath) + expect(runtimes).toEqual([ + expect.objectContaining({ + id: provisioned.start.context.instanceId, + status: 'failed', + cleanupLastError: 'Recipe stdout must be one JSON object.', + createdAt: 1_000 + }) + ]) + expect(runtimes[0]).not.toHaveProperty('recipeResult') + }) + + it('keeps a timed-out create destroyable by its instance id', async () => { + const userDataPath = makeDir('orca-ephemeral-vm-service-user-data-') + const repoPath = makeDir('orca-ephemeral-vm-service-repo-') + const startPath = join(repoPath, 'start.js') + writeFileSync(startPath, 'setInterval(() => {}, 1000)') + const recipe: OrcaVmRecipe = { + id: 'slow-sandbox', + name: 'Slow Sandbox', + create: nodeCommand(startPath), + destroy: nodeCommand(startPath), + timeoutSeconds: { create: 1 } + } + + const provisioned = await provisionEphemeralVmRuntime({ + userDataPath, + repoPath, + recipe, + now: 1_000 + }) + + expect(provisioned).toMatchObject({ ok: false, start: { errorCode: 'recipeTimedOut' } }) + expect(listEphemeralVmRuntimes(userDataPath)).toEqual([ + expect.objectContaining({ + id: provisioned.start.context.instanceId, + status: 'failed', + cleanupStatus: 'not_started', + cleanupLastErrorCode: 'recipeTimedOut', + recipe: expect.not.objectContaining({ timeoutSeconds: expect.anything() }) + }) + ]) + }) + + it('reconciles a create interrupted by a crash into a destroyable failed record', async () => { + const userDataPath = makeDir('orca-ephemeral-vm-service-user-data-') + const repoPath = makeDir('orca-ephemeral-vm-service-repo-') + const startPath = join(repoPath, 'start.js') + const destroyPath = join(repoPath, 'destroy.js') + const spawnedPath = join(repoPath, 'create-spawned.txt') + const payloadPath = join(repoPath, 'destroy-payload.json') + writeFileSync( + startPath, + [ + `require('fs').writeFileSync(${JSON.stringify(spawnedPath)}, process.env.ORCA_VM_INSTANCE_ID)`, + 'setInterval(() => {}, 1000)' + ].join('\n') + ) + writeFileSync( + destroyPath, + [ + "let input = ''", + "process.stdin.on('data', (chunk) => { input += chunk })", + `process.stdin.on('end', () => require('fs').writeFileSync(${JSON.stringify(payloadPath)}, input))` + ].join('\n') + ) + const recipe: OrcaVmRecipe = { + id: 'slow-sandbox', + name: 'Slow Sandbox', + create: nodeCommand(startPath), + destroy: nodeCommand(destroyPath) + } + const controller = new AbortController() + const provisioning = provisionEphemeralVmRuntime({ + userDataPath, + repoPath, + recipe, + repoId: 'repo-1', + workspaceName: 'Slow VM', + now: 1_000, + signal: controller.signal + }) + await vi.waitFor(() => expect(existsSync(spawnedPath)).toBe(true), { timeout: 10_000 }) + const instanceId = readFileSync(spawnedPath, 'utf8') + + // Persisted before create returned, and invisible to older builds' store schema. + expect(listEphemeralVmRuntimes(userDataPath)).toEqual([ + expect.objectContaining({ id: instanceId, status: 'provisioning', repoId: 'repo-1' }) + ]) + expect( + JSON.parse(readFileSync(getEphemeralVmRuntimeStorePath(userDataPath), 'utf8')).runtimes + ).toEqual([]) + const { reconcileInterruptedEphemeralVmProvisions: reconcileInSameProcess } = + await import('./runtime-provisioning-persistence') + expect(reconcileInSameProcess(userDataPath)).toEqual([]) + + // A relaunch has no record of the in-flight create. + vi.resetModules() + const { reconcileInterruptedEphemeralVmProvisions } = + await import('./runtime-provisioning-persistence') + const reconciled = reconcileInterruptedEphemeralVmProvisions(userDataPath, 5_000) + controller.abort() + await provisioning + + expect(reconciled).toEqual([ + { + id: instanceId, + recipeId: 'slow-sandbox', + recipe, + repoId: 'repo-1', + repoPath, + workspaceName: 'Slow VM', + status: 'failed', + cleanupStatus: 'not_started', + cleanupLastError: + 'Orca stopped while this VM was being created. Destroy it to release any provider resources.', + cleanupLastErrorCode: 'provisionInterrupted', + createdAt: 1_000, + updatedAt: 5_000 + } + ]) + + const cleanup = await cleanupEphemeralVmRuntime({ + userDataPath, + repoPath, + recipe, + runtimeId: instanceId + }) + expect(cleanup).toMatchObject({ ok: true, runtime: { status: 'cleaned' } }) + const payload = JSON.parse(readFileSync(payloadPath, 'utf8')) + expect(payload).toMatchObject({ mode: 'destroy', instanceId, recipeId: 'slow-sandbox' }) + expect(payload).not.toHaveProperty('recipeResult') }) it('rejects an unwritable feature store before a checkout-mode recipe creates resources', async () => { diff --git a/src/main/ephemeral-vm/runtime-service.ts b/src/main/ephemeral-vm/runtime-service.ts index 5a94f2ec621..4eba637eea1 100644 --- a/src/main/ephemeral-vm/runtime-service.ts +++ b/src/main/ephemeral-vm/runtime-service.ts @@ -13,10 +13,12 @@ import { type EphemeralVmRecipeStartFailure, type EphemeralVmRecipeStartSuccess } from '../../shared/ephemeral-vm-recipe-runner' -import { cleanupFailedEphemeralVmStart } from './failed-start-cleanup' import { - persistProvisionedEphemeralVmRuntime, - prepareEphemeralVmCompatibilityPersistence + beginEphemeralVmProvisioning, + endEphemeralVmProvisioning, + isEphemeralVmProvisionLive, + persistFailedEphemeralVmStart, + persistProvisionedEphemeralVmRuntime } from './runtime-provisioning-persistence' import { getProvisionedRootResumeIntegrityError } from './resume-integrity' import { runControlledEphemeralVmRuntimeCleanup } from './runtime-cleanup-control' @@ -105,38 +107,41 @@ export type ResumeEphemeralVmRuntimeResult = export async function provisionEphemeralVmRuntime( args: ProvisionEphemeralVmRuntimeArgs ): Promise { - const compatibility = prepareEphemeralVmCompatibilityPersistence(args) - const start = await runEphemeralVmRecipeStart({ - repoPath: args.repoPath, - recipe: args.recipe, - context: { - projectId: args.projectId, - workspaceId: args.workspaceId, - workspaceName: args.workspaceName, - repoUrl: args.repoUrl, - branch: args.branch, - ref: args.ref, - expectedRefHead: args.expectedRefHead, - orcaVersion: args.orcaVersion, - ...(compatibility ? { instanceId: compatibility.instanceId } : {}) - }, - signal: args.signal, - onStdout: args.onStdout, - onStderr: args.onStderr - }) - if (!start.ok) { - if (start.recipeResult) { - await cleanupFailedEphemeralVmStart(args, { - context: start.context, - recipeResult: start.recipeResult - }) + const writeAhead = beginEphemeralVmProvisioning(args) + try { + const start = await runEphemeralVmRecipeStart({ + repoPath: args.repoPath, + recipe: args.recipe, + context: { + projectId: args.projectId, + workspaceId: args.workspaceId, + workspaceName: args.workspaceName, + repoUrl: args.repoUrl, + branch: args.branch, + ref: args.ref, + expectedRefHead: args.expectedRefHead, + orcaVersion: args.orcaVersion, + instanceId: writeAhead.instanceId + }, + signal: args.signal, + onSpawn: () => { + writeAhead.launched = true + }, + onStdout: args.onStdout, + onStderr: args.onStderr + }) + if (!start.ok) { + await persistFailedEphemeralVmStart(args, writeAhead, start) + endEphemeralVmProvisioning(args, writeAhead) + return { ok: false, start } } - return { ok: false, start } + const runtime = await persistProvisionedEphemeralVmRuntime(args, start, writeAhead) + endEphemeralVmProvisioning(args, writeAhead) + return { ok: true, start, runtime } + } catch (error) { + endEphemeralVmProvisioning(args, writeAhead, error) + throw error } - - const runtime = await persistProvisionedEphemeralVmRuntime(args, start, compatibility) - - return { ok: true, start, runtime } } export function cleanupEphemeralVmRuntime( @@ -159,6 +164,9 @@ async function cleanupEphemeralVmRuntimeOnce( if (!existing) { throw new Error(`Unknown ephemeral VM runtime: ${args.runtimeId}`) } + if (existing.status === 'provisioning' && isEphemeralVmProvisionLive(existing.id)) { + throw new Error(`Ephemeral VM runtime is still provisioning: ${existing.id}`) + } if (existing.status === 'cleaned') { return { ok: true, @@ -216,6 +224,9 @@ export async function suspendEphemeralVmRuntime( if (!existing) { throw new Error(`Unknown ephemeral VM runtime: ${args.runtimeId}`) } + if (!existing.recipeResult) { + throw new Error(`Ephemeral VM runtime never finished provisioning: ${args.runtimeId}`) + } const suspend = await runEphemeralVmRecipeSuspend({ repoPath: args.repoPath, recipe: args.recipe, @@ -250,6 +261,9 @@ export async function resumeEphemeralVmRuntime( if (!existing) { throw new Error(`Unknown ephemeral VM runtime: ${args.runtimeId}`) } + if (!existing.recipeResult) { + throw new Error(`Ephemeral VM runtime never finished provisioning: ${args.runtimeId}`) + } const resume = await runEphemeralVmRecipeResume({ repoPath: args.repoPath, recipe: args.recipe, diff --git a/src/main/ephemeral-vm/workspace-resume-user-disconnect.test.ts b/src/main/ephemeral-vm/workspace-resume-user-disconnect.test.ts index 7eb5d73dd65..baeb576e45a 100644 --- a/src/main/ephemeral-vm/workspace-resume-user-disconnect.test.ts +++ b/src/main/ephemeral-vm/workspace-resume-user-disconnect.test.ts @@ -14,9 +14,11 @@ const mocks = vi.hoisted(() => ({ vi.mock('electron', () => ({ app: { getPath: () => '/user-data' } })) vi.mock('../../shared/ephemeral-vm-runtime-store', () => ({ - findWorkspaceEphemeralVmRuntime: () => ({ id: 'vm-1', status: 'suspended', repoId: 'repo-1' }), updateEphemeralVmRuntimeStatus: mocks.updateStatus })) +vi.mock('../../shared/ephemeral-vm-workspace-runtime', () => ({ + findWorkspaceEphemeralVmRuntime: () => ({ id: 'vm-1', status: 'suspended', repoId: 'repo-1' }) +})) vi.mock('../../shared/remote-runtime/host-store/runtime-environment-store', () => ({ resolveEnvironment: () => ({ id: 'env-vm', pairingRevision: 9, createdAt: 0 }), updateEnvironmentFromPairingCode: vi.fn() @@ -26,7 +28,7 @@ vi.mock('../../shared/ephemeral-vm-recipes', () => ({ getEphemeralVmRecipeResultPairingCode: () => null })) vi.mock('../ipc/ephemeral-vm-recipe-context', () => ({ - getRuntimeRecipeContext: () => ({ repo: { repo: { path: '/repo' } }, recipe: {} }) + getRuntimeRecipeContext: () => ({ repoPath: '/repo', recipe: {} }) })) vi.mock('../ipc/runtime-environments/runtime-environments', () => ({ invalidateRuntimeEnvironmentTransport: vi.fn() diff --git a/src/main/ephemeral-vm/workspace-resume.ts b/src/main/ephemeral-vm/workspace-resume.ts index a8838ffea9b..e17f536eb93 100644 --- a/src/main/ephemeral-vm/workspace-resume.ts +++ b/src/main/ephemeral-vm/workspace-resume.ts @@ -1,9 +1,7 @@ import { app } from 'electron' import type { Store } from '../persistence/loading-store/store' -import { - findWorkspaceEphemeralVmRuntime, - updateEphemeralVmRuntimeStatus -} from '../../shared/ephemeral-vm-runtime-store' +import { updateEphemeralVmRuntimeStatus } from '../../shared/ephemeral-vm-runtime-store' +import { findWorkspaceEphemeralVmRuntime } from '../../shared/ephemeral-vm-workspace-runtime' import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes' import { getEphemeralVmRecipeResultConnection, @@ -40,13 +38,16 @@ export async function resumeEphemeralVmWorkspace( const recipeContext = getRuntimeRecipeContext(store, userDataPath, runtime.id) const result = await resumeEphemeralVmRuntime({ userDataPath, - repoPath: recipeContext.repo.repo.path, + repoPath: recipeContext.repoPath, recipe: recipeContext.recipe, runtimeId: runtime.id }) if (!result.ok) { throw new Error(result.error) } + if (!result.runtime.recipeResult) { + return result.runtime + } const connection = getEphemeralVmRecipeResultConnection(result.runtime.recipeResult) // SSH servers use their target and have no pairing to refresh. if (!result.skipped && connection.type !== 'ssh' && runtime.runtimeEnvironmentId) { diff --git a/src/main/ephemeral-vm/workspace-suspend.ts b/src/main/ephemeral-vm/workspace-suspend.ts index 32c58a61f6f..7b79aa813f5 100644 --- a/src/main/ephemeral-vm/workspace-suspend.ts +++ b/src/main/ephemeral-vm/workspace-suspend.ts @@ -1,6 +1,6 @@ import { app } from 'electron' import type { Store } from '../persistence/loading-store/store' -import { findWorkspaceEphemeralVmRuntime } from '../../shared/ephemeral-vm-runtime-store' +import { findWorkspaceEphemeralVmRuntime } from '../../shared/ephemeral-vm-workspace-runtime' import { suspendEphemeralVmRuntime } from './runtime-service' import { disconnectRuntimeOwnedSshTarget } from './runtime-ssh' import { getRuntimeRecipeContext } from '../ipc/ephemeral-vm-recipe-context' @@ -17,7 +17,7 @@ export async function suspendEphemeralVmWorkspace( const recipeContext = getRuntimeRecipeContext(store, userDataPath, runtime.id) const result = await suspendEphemeralVmRuntime({ userDataPath, - repoPath: recipeContext.repo.repo.path, + repoPath: recipeContext.repoPath, recipe: recipeContext.recipe, runtimeId: runtime.id }) diff --git a/src/main/ipc/ephemeral-vm-recipe-context.ts b/src/main/ipc/ephemeral-vm-recipe-context.ts index 6f389b225d9..3990f9827bf 100644 --- a/src/main/ipc/ephemeral-vm-recipe-context.ts +++ b/src/main/ipc/ephemeral-vm-recipe-context.ts @@ -96,19 +96,30 @@ export function getRuntimeRecipeContext( runtimeId: string ): { runtime: EphemeralVmRuntimeRecord - repo: Extract + repoPath: string recipe: OrcaVmRecipe } { const runtime = listEphemeralVmRuntimes(userDataPath).find((entry) => entry.id === runtimeId) if (!runtime) { throw new EphemeralVmFailureError(ephemeralVmFailure('runtimeNotFound', { runtimeId })) } - if (!runtime.repoId) { - throw new EphemeralVmFailureError(ephemeralVmFailure('runtimeRepoMissing', { runtimeId })) - } - const repo = getRecipeRepo(store, runtime.repoId) - if (!repo.ok) { - throw new EphemeralVmFailureError(repo.failure) + const repo = runtime.repoId ? getRecipeRepo(store, runtime.repoId) : null + if (!repo?.ok) { + // Why: a re-added or removed repo must not strand the VM; the snapshot plus recorded checkout + // path are everything destroy needs (stablyai/orca#17293). Only trust the path while it still + // holds the same recipe, so a different checkout later placed there can't run its scripts. + const fallback = runtime.recipe && runtime.repoPath ? runtime.recipe : null + if (fallback && runtime.repoPath) { + const sameRecipe = (loadHooks(runtime.repoPath)?.environmentRecipes ?? []).some( + (entry) => entry.id === fallback.id && entry.create === fallback.create + ) + if (sameRecipe) { + return { runtime, repoPath: runtime.repoPath, recipe: fallback } + } + } + throw new EphemeralVmFailureError( + repo ? repo.failure : ephemeralVmFailure('runtimeRepoMissing', { runtimeId }) + ) } const repoRecipe = (loadHooks(repo.repo.path)?.environmentRecipes ?? []).find( (entry) => entry.id === runtime.recipeId @@ -127,7 +138,7 @@ export function getRuntimeRecipeContext( ephemeralVmFailure('recipeNotFound', { recipeId: runtime.recipeId }) ) } - return { runtime, repo, recipe } + return { runtime, repoPath: repo.repo.path, recipe } } export function resolveRecipeForRepo( diff --git a/src/main/ipc/ephemeral-vm-runtime-handler-cleanup.test.ts b/src/main/ipc/ephemeral-vm-runtime-handler-cleanup.test.ts index 7b1f6e6b2e5..37645f8f1d0 100644 --- a/src/main/ipc/ephemeral-vm-runtime-handler-cleanup.test.ts +++ b/src/main/ipc/ephemeral-vm-runtime-handler-cleanup.test.ts @@ -220,3 +220,187 @@ it("retires a recipe server's session partition once its VM is destroyed (#33805 expect(removeEnvironmentMock).toHaveBeenCalledWith(userDataPath, 'env-recipe') expect(removeWorkspaceSessionHost).toHaveBeenCalledWith('runtime:env-recipe') }) + +it('destroys from the recorded snapshot and repo path when the repo id no longer resolves (#17293)', async () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-vm-runtime-handler-')) + const repoPath = mkdtempSync(join(tmpdir(), 'orca-vm-runtime-repo-')) + tempDirs.push(userDataPath, repoPath) + getPathMock.mockReturnValue(userDataPath) + const destroyedPath = join(repoPath, 'destroyed.txt') + writeFileSync( + join(repoPath, 'destroy.js'), + `require('fs').writeFileSync(${JSON.stringify(destroyedPath)}, process.cwd())` + ) + const recipe = { + id: 'cloud-sandbox', + name: 'Cloud Sandbox', + create: './create.sh', + destroy: `"${process.execPath}" destroy.js` + } + // The checkout still defines the recipe the VM was created from. + writeFileSync( + join(repoPath, 'orca.yaml'), + [ + 'environmentRecipes:', + ' - id: cloud-sandbox', + ' name: Cloud Sandbox', + ' create: ./create.sh' + ].join('\n') + ) + upsertEphemeralVmRuntime(userDataPath, { + id: 'runtime-stale-repo', + recipeId: 'cloud-sandbox', + recipe, + repoId: 'removed-repo', + repoPath, + status: 'cleanup_failed', + cleanupStatus: 'failed', + createdAt: 1, + updatedAt: 1, + recipeResult: { + schemaVersion: 1, + connection: { type: 'orca-server', pairingCode: 'unused', projectRoot: '/workspace/repo' } + } + }) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these cleanup paths only read getRepo from the store. + registerEphemeralVmRuntimeHandlers({ getRepo: vi.fn(() => undefined) } as never) + + await expect( + handlers.get('ephemeralVm:getCleanupCommand')?.(null, { runtimeId: 'runtime-stale-repo' }) + ).resolves.toMatchObject({ command: expect.stringContaining('destroy.js') }) + + await expect( + handlers.get('ephemeralVm:cleanup')?.(null, { runtimeId: 'runtime-stale-repo' }) + ).resolves.toMatchObject({ status: 'cleaned', cleanupStatus: 'succeeded' }) + expect(existsSync(destroyedPath)).toBe(true) +}) + +it('refuses the recorded repo path once a different checkout sits there', async () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-vm-runtime-handler-')) + const repoPath = mkdtempSync(join(tmpdir(), 'orca-vm-runtime-repo-')) + tempDirs.push(userDataPath, repoPath) + getPathMock.mockReturnValue(userDataPath) + const ranPath = join(repoPath, 'ran.txt') + writeFileSync( + join(repoPath, 'destroy.js'), + `require('fs').writeFileSync(${JSON.stringify(ranPath)}, 'x')` + ) + writeFileSync( + join(repoPath, 'orca.yaml'), + [ + 'environmentRecipes:', + ' - id: cloud-sandbox', + ' name: Other', + ' create: ./other.sh' + ].join('\n') + ) + upsertEphemeralVmRuntime(userDataPath, { + id: 'runtime-moved-repo', + recipeId: 'cloud-sandbox', + recipe: { + id: 'cloud-sandbox', + name: 'Cloud Sandbox', + create: './create.sh', + destroy: `"${process.execPath}" destroy.js` + }, + repoId: 'removed-repo', + repoPath, + status: 'running', + cleanupStatus: 'not_started', + createdAt: 1, + updatedAt: 1, + recipeResult: { + schemaVersion: 1, + connection: { type: 'orca-server', pairingCode: 'unused', projectRoot: '/workspace/repo' } + } + }) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these cleanup paths only read getRepo from the store. + registerEphemeralVmRuntimeHandlers({ getRepo: vi.fn(() => undefined) } as never) + + await expect( + handlers.get('ephemeralVm:cleanup')?.(null, { runtimeId: 'runtime-moved-repo' }) + ).resolves.toMatchObject({ status: 'cleanup_failed', cleanupLastErrorCode: 'repoNotFound' }) + expect(existsSync(ranPath)).toBe(false) +}) + +it('still refuses cleanup when the repo id is stale and no repo path was recorded', async () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-vm-runtime-handler-')) + tempDirs.push(userDataPath) + getPathMock.mockReturnValue(userDataPath) + upsertEphemeralVmRuntime(userDataPath, { + id: 'runtime-legacy-stale-repo', + recipeId: 'cloud-sandbox', + recipe: { id: 'cloud-sandbox', name: 'Cloud Sandbox', create: 'unused', destroy: 'unused' }, + repoId: 'removed-repo', + status: 'running', + cleanupStatus: 'not_started', + createdAt: 1, + updatedAt: 1, + recipeResult: { + schemaVersion: 1, + connection: { type: 'orca-server', pairingCode: 'unused', projectRoot: '/workspace/repo' } + } + }) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these cleanup paths only read getRepo from the store. + registerEphemeralVmRuntimeHandlers({ getRepo: vi.fn(() => undefined) } as never) + + await expect( + handlers.get('ephemeralVm:cleanup')?.(null, { runtimeId: 'runtime-legacy-stale-repo' }) + ).resolves.toMatchObject({ status: 'cleanup_failed', cleanupLastErrorCode: 'repoNotFound' }) +}) + +it( + "applies the repo recipe's destroy timeout to a snapshot persisted without timeouts", + async () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-vm-runtime-handler-')) + const repoPath = mkdtempSync(join(tmpdir(), 'orca-vm-runtime-repo-')) + tempDirs.push(userDataPath, repoPath) + getPathMock.mockReturnValue(userDataPath) + writeFileSync(join(repoPath, 'destroy.js'), 'setInterval(() => {}, 1000)') + writeFileSync( + join(repoPath, 'orca.yaml'), + [ + 'environmentRecipes:', + ' - id: cloud-sandbox', + ' name: Cloud Sandbox', + ' create: unused', + ' timeoutSeconds:', + ' destroy: 1' + ].join('\n') + ) + upsertEphemeralVmRuntime(userDataPath, { + id: 'runtime-timeout', + recipeId: 'cloud-sandbox', + recipe: { + id: 'cloud-sandbox', + name: 'Cloud Sandbox', + create: 'unused', + destroy: `"${process.execPath}" destroy.js` + }, + repoId: 'repo-1', + status: 'running', + cleanupStatus: 'not_started', + createdAt: 1, + updatedAt: 1, + recipeResult: { + schemaVersion: 1, + connection: { type: 'orca-server', pairingCode: 'unused', projectRoot: '/workspace/repo' } + } + }) + registerEphemeralVmRuntimeHandlers({ + getRepo: vi.fn(() => ({ + id: 'repo-1', + path: repoPath, + displayName: 'Repo', + badgeColor: '#000', + addedAt: 0 + })) + } as never) + + await expect( + handlers.get('ephemeralVm:cleanup')?.(null, { runtimeId: 'runtime-timeout' }) + ).resolves.toMatchObject({ status: 'cleanup_failed', cleanupLastErrorCode: 'recipeTimedOut' }) + }, + // The destroy step must hit its 1s recipe timeout and then be force-stopped. + 15_000 +) diff --git a/src/main/ipc/ephemeral-vm-runtime-handlers.ts b/src/main/ipc/ephemeral-vm-runtime-handlers.ts index bbe0f63b377..95240974d55 100644 --- a/src/main/ipc/ephemeral-vm-runtime-handlers.ts +++ b/src/main/ipc/ephemeral-vm-runtime-handlers.ts @@ -22,6 +22,10 @@ import { import { invalidateRuntimeEnvironmentTransport } from './runtime-environments/runtime-environments' import { retireRemovedRuntimeEnvironment } from './runtime-environments/runtime-environment-removal-cleanup' import { attachEphemeralVmRuntimeToWorkspace } from '../ephemeral-vm/runtime-attachment' +import { + forgetInterruptedEphemeralVmProvision, + isEphemeralVmProvisionLive +} from '../ephemeral-vm/runtime-provisioning-persistence' export type EphemeralVmCleanupCommandResult = { runtimeId: string @@ -36,6 +40,7 @@ export function registerEphemeralVmRuntimeHandlers(store: Store): void { ipcMain.removeHandler('ephemeralVm:listRuntimes') ipcMain.removeHandler('ephemeralVm:cleanup') ipcMain.removeHandler('ephemeralVm:stopCleanup') + ipcMain.removeHandler('ephemeralVm:forgetRuntime') ipcMain.removeHandler('ephemeralVm:getCleanupCommand') ipcMain.handle('ephemeralVm:listRuntimes', (): EphemeralVmRuntimeRecord[] => { @@ -63,8 +68,9 @@ export function registerEphemeralVmRuntimeHandlers(store: Store): void { if (!runtime) { throw new Error(`Unknown ephemeral VM runtime: ${args.runtimeId}`) } - if (!runtime.repoId) { - throw new Error(`Ephemeral VM runtime has no repo id: ${args.runtimeId}`) + if (runtime.status === 'provisioning' && isEphemeralVmProvisionLive(runtime.id)) { + // Why: don't mark a create in flight as cleanup_failed; its own outcome settles the row. + throw new Error(`Ephemeral VM runtime is still provisioning: ${runtime.id}`) } let result if (runtime.cleanupStatus === 'succeeded') { @@ -88,7 +94,7 @@ export function registerEphemeralVmRuntimeHandlers(store: Store): void { } result = await cleanupEphemeralVmRuntime({ userDataPath, - repoPath: resolved.repo.repo.path, + repoPath: resolved.repoPath, recipe: resolved.recipe, runtimeId: runtime.id }) @@ -148,6 +154,10 @@ export function registerEphemeralVmRuntimeHandlers(store: Store): void { } ) + ipcMain.handle('ephemeralVm:forgetRuntime', (_event, args: { runtimeId: string }): void => { + forgetInterruptedEphemeralVmProvision(app.getPath('userData'), args.runtimeId) + }) + ipcMain.handle( 'ephemeralVm:getCleanupCommand', async (_event, args: { runtimeId: string }): Promise => { @@ -161,7 +171,7 @@ export function registerEphemeralVmRuntimeHandlers(store: Store): void { projectId: resolved.runtime.projectId, workspaceId: resolved.runtime.workspaceId, workspaceName: resolved.runtime.workspaceName, - repoPath: resolved.repo.repo.path + repoPath: resolved.repoPath }, recipeResult: resolved.runtime.recipeResult }) diff --git a/src/main/ipc/ephemeral-vm.ts b/src/main/ipc/ephemeral-vm.ts index 18826b2d674..e322668b02e 100644 --- a/src/main/ipc/ephemeral-vm.ts +++ b/src/main/ipc/ephemeral-vm.ts @@ -13,7 +13,10 @@ import { getProvisionedRootRecipeRepoUrl } from '../../shared/ephemeral-vm-recip // Import the Node-only doctor directly so browser consumers of the barrel do not bundle fs/path. import { doctorEphemeralVmRecipe } from '../../shared/ephemeral-vm-recipe-doctor' import { updateEphemeralVmRuntimeStatus } from '../../shared/ephemeral-vm-runtime-store' -import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes' +import { + requireEphemeralVmRecipeResult, + type EphemeralVmProvisionedRuntimeRecord +} from '../../shared/ephemeral-vm-runtimes' import { addEnvironmentFromPairingCode } from '../../shared/remote-runtime/host-store/runtime-environment-store' import { redactRuntimeEnvironment, @@ -24,6 +27,7 @@ import { provisionEphemeralVmRuntime } from '../ephemeral-vm/runtime-service' import { connectRuntimeOwnedSshTarget } from '../ephemeral-vm/runtime-ssh' +import { reconcileInterruptedEphemeralVmProvisionsOnStartup } from '../ephemeral-vm/runtime-provisioning-persistence' import { getRecipeRepo, listRecipeCatalog, @@ -48,7 +52,7 @@ export type EphemeralVmProvisionIpcResult = | { ok: true connectionType: 'orca-server' - runtime: EphemeralVmRuntimeRecord + runtime: EphemeralVmProvisionedRuntimeRecord environment: PublicKnownRuntimeEnvironment stderr: string warnings: EphemeralVmRecipeResultWarning[] @@ -56,7 +60,7 @@ export type EphemeralVmProvisionIpcResult = | { ok: true connectionType: 'ssh' - runtime: EphemeralVmRuntimeRecord + runtime: EphemeralVmProvisionedRuntimeRecord sshTargetId: string /** The managed Orca server the host runs; its workspace lives there. */ environmentId?: string @@ -77,6 +81,7 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin ipcMain.removeHandler('ephemeralVm:provision') ipcMain.removeHandler('ephemeralVm:cancelProvision') registerEphemeralVmRuntimeHandlers(store) + reconcileInterruptedEphemeralVmProvisionsOnStartup(app.getPath('userData')) ipcMain.handle('ephemeralVm:listRecipes', async (_event, args: { repoId: string }) => { return listRecipes(store, args.repoId, await getApprovedPluginVmRecipes(pluginService)) @@ -209,13 +214,11 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin runtimeId: result.runtime.id, connection }) - const runtime = updateEphemeralVmRuntimeStatus( - app.getPath('userData'), - result.runtime.id, - { + const runtime = requireEphemeralVmRecipeResult( + updateEphemeralVmRuntimeStatus(app.getPath('userData'), result.runtime.id, { sshTargetId: ssh.targetId, runtimeEnvironmentId: ssh.environmentId - } + }) ) return { ok: true, @@ -264,9 +267,11 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin stderr: redactEphemeralVmRecipeDiagnosticText(result.start.stderr) } } - const runtime = updateEphemeralVmRuntimeStatus(app.getPath('userData'), result.runtime.id, { - runtimeEnvironmentId: environment.id - }) + const runtime = requireEphemeralVmRecipeResult( + updateEphemeralVmRuntimeStatus(app.getPath('userData'), result.runtime.id, { + runtimeEnvironmentId: environment.id + }) + ) return { ok: true, connectionType: 'orca-server', diff --git a/src/main/startup/main-process-runtime-service.ts b/src/main/startup/main-process-runtime-service.ts index 3072867e660..9af01fb0f51 100644 --- a/src/main/startup/main-process-runtime-service.ts +++ b/src/main/startup/main-process-runtime-service.ts @@ -36,7 +36,7 @@ import { import { registerRecipeVmWaker } from '../runtime/workspaces/sleep/recipe-vm-waker-registry' import { openRecipeVmEnvironment } from '../ipc/runtime-environments/runtime-environment-recipe-vm-wake' import { resolveEnvironment } from '../../shared/remote-runtime/host-store/runtime-environment-store' -import { findWorkspaceEphemeralVmRuntime } from '../../shared/ephemeral-vm-runtime-store' +import { findWorkspaceEphemeralVmRuntime } from '../../shared/ephemeral-vm-workspace-runtime' import { installWslOrcadAccountShim } from './wsl-orcad-account-env-wiring' import { codexStructuredLaunchHomeResolvers, diff --git a/src/main/workspaces/catalog/provisioned-root-ssh-adoption.ts b/src/main/workspaces/catalog/provisioned-root-ssh-adoption.ts index eb9086ebc6e..a0ef6bd2213 100644 --- a/src/main/workspaces/catalog/provisioned-root-ssh-adoption.ts +++ b/src/main/workspaces/catalog/provisioned-root-ssh-adoption.ts @@ -11,7 +11,10 @@ import { getEphemeralVmRecipeResultProjectRoot } from '../../../shared/ephemeral-vm-recipes' import { listEphemeralVmRuntimes } from '../../../shared/ephemeral-vm-runtime-store' -import type { EphemeralVmRuntimeRecord } from '../../../shared/ephemeral-vm-runtimes' +import { + hasEphemeralVmRecipeResult, + type EphemeralVmProvisionedRuntimeRecord +} from '../../../shared/ephemeral-vm-runtimes' import { provisionedRootPathsEqual } from './provisioned-root-checkout' type AdoptionArgs = AdoptProvisionedRootArgs & { @@ -58,12 +61,13 @@ function requireOwnedProvisionedRootRuntime( userDataPath: string, runtimeId: string, connectionId: string -): EphemeralVmRuntimeRecord { +): EphemeralVmProvisionedRuntimeRecord { const runtime = listEphemeralVmRuntimes(userDataPath).find((entry) => entry.id === runtimeId) if (!runtime) { throw new Error(`Unknown ephemeral VM runtime: ${runtimeId}`) } if ( + !hasEphemeralVmRecipeResult(runtime) || runtime.connectionMode !== 'ssh' || runtime.sshTargetId !== connectionId || runtime.recipe?.checkoutMode !== 'provisioned-root' || diff --git a/src/preload/api/ephemeral-vm-api.ts b/src/preload/api/ephemeral-vm-api.ts index 4a644a11d6f..0b83d3f7157 100644 --- a/src/preload/api/ephemeral-vm-api.ts +++ b/src/preload/api/ephemeral-vm-api.ts @@ -2,7 +2,10 @@ import type { OrcaHooks } from '../../shared/orca-yaml-hook-types' import type { PublicKnownRuntimeEnvironment } from '../../shared/remote-runtime/host-catalog/runtime-environments' import type { EphemeralVmRecipeDoctorResult } from '../../shared/ephemeral-vm-recipes' import type { EphemeralVmRecipeResultWarning } from '../../shared/ephemeral-vm-recipe-diagnostics' -import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes' +import type { + EphemeralVmProvisionedRuntimeRecord, + EphemeralVmRuntimeRecord +} from '../../shared/ephemeral-vm-runtimes' import type { EphemeralVmFailure } from '../../shared/ephemeral-vm-failure-copy' export type EphemeralVmApi = { @@ -38,7 +41,7 @@ export type EphemeralVmApi = { | { ok: true connectionType: 'orca-server' - runtime: EphemeralVmRuntimeRecord + runtime: EphemeralVmProvisionedRuntimeRecord environment: PublicKnownRuntimeEnvironment stderr: string warnings: EphemeralVmRecipeResultWarning[] @@ -46,7 +49,7 @@ export type EphemeralVmApi = { | { ok: true connectionType: 'ssh' - runtime: EphemeralVmRuntimeRecord + runtime: EphemeralVmProvisionedRuntimeRecord sshTargetId: string /** The managed Orca server the host runs; its workspace lives there. */ environmentId?: string @@ -68,6 +71,8 @@ export type EphemeralVmApi = { resumeWorkspace: (args: { workspaceId: string }) => Promise cleanup: (args: { runtimeId: string }) => Promise stopCleanup: (args: { runtimeId: string }) => Promise + /** Drops an interrupted write-ahead record that never received a recipe result. */ + forgetRuntime: (args: { runtimeId: string }) => Promise getCleanupCommand: (args: { runtimeId: string }) => Promise<{ runtimeId: string command: string | null diff --git a/src/preload/api/ephemeral-vm-bridge.ts b/src/preload/api/ephemeral-vm-bridge.ts index 351701276ab..ee9748e499d 100644 --- a/src/preload/api/ephemeral-vm-bridge.ts +++ b/src/preload/api/ephemeral-vm-bridge.ts @@ -20,5 +20,6 @@ export const ephemeralVmApi = { resumeWorkspace: (args) => ipcRenderer.invoke('ephemeralVm:resumeWorkspace', args), cleanup: (args) => ipcRenderer.invoke('ephemeralVm:cleanup', args), stopCleanup: (args) => ipcRenderer.invoke('ephemeralVm:stopCleanup', args), + forgetRuntime: (args) => ipcRenderer.invoke('ephemeralVm:forgetRuntime', args), getCleanupCommand: (args) => ipcRenderer.invoke('ephemeralVm:getCleanupCommand', args) } satisfies PreloadApi['ephemeralVm'] diff --git a/src/renderer/src/components/settings/virtual-machines/runtimes/EphemeralVmRuntimeRow.tsx b/src/renderer/src/components/settings/virtual-machines/runtimes/EphemeralVmRuntimeRow.tsx index edec8134bcc..f2887f5ab4d 100644 --- a/src/renderer/src/components/settings/virtual-machines/runtimes/EphemeralVmRuntimeRow.tsx +++ b/src/renderer/src/components/settings/virtual-machines/runtimes/EphemeralVmRuntimeRow.tsx @@ -1,4 +1,4 @@ -import { AlertTriangle, Copy, Loader2, Square, Trash2 } from 'lucide-react' +import { AlertTriangle, Copy, Loader2, Square, Trash2, X } from 'lucide-react' import type { EphemeralVmRuntimeRecord } from '../../../../../../shared/ephemeral-vm-runtimes' import { getEphemeralVmRecipeResultProjectRoot } from '../../../../../../shared/ephemeral-vm-recipes' import { useTranslate } from '@/i18n/use-translate' @@ -16,6 +16,7 @@ type EphemeralVmRuntimeRowProps = { onCleanup: () => void onStopCleanup: () => void onCopyCleanupCommand: () => void + onForget: () => void } export function EphemeralVmRuntimeRow({ @@ -27,11 +28,15 @@ export function EphemeralVmRuntimeRow({ disabled, onCleanup, onStopCleanup, - onCopyCleanupCommand + onCopyCleanupCommand, + onForget }: EphemeralVmRuntimeRowProps): React.JSX.Element { const t = useTranslate() const hasError = cleanupFailed || runtime.status === 'failed' const cleanupError = ephemeralVmCleanupErrorText(runtime) + const isProvisioning = runtime.status === 'provisioning' + // Write-ahead rows never got a recipe result, so destroy only knows the instance id. + const isUntracked = !runtime.recipeResult && !isProvisioning return (
: null}

- {runtime.recipeId} · {getEphemeralVmRecipeResultProjectRoot(runtime.recipeResult)} + {runtime.recipeId} ·{' '} + {runtime.recipeResult + ? getEphemeralVmRecipeResultProjectRoot(runtime.recipeResult) + : runtime.id}

{cleanupError ? (

{cleanupError}

) : null} + {isUntracked ? ( +

+ {t( + 'auto.components.settings.EphemeralVmRuntimesSection.untrackedRuntimeHint', + 'Orca never received this VM’s details. Cleanup runs destroy with only instance ID {{instanceId}}; if it fails, delete the VM in your provider, then remove it from this list.', + { instanceId: runtime.id } + )} +

+ ) : null}
-
+
+ {isUntracked && !cleanupRunning ? ( + + ) : null} {runtime.cleanupStatus === 'failed' ? (
diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index a973b81ce24..bd1b52da623 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -2040,6 +2040,7 @@ "destroyExitedWithoutCode": "Destroy exited with code unknown.", "destroyFailed": "Destroy failed.", "provisionCancelled": "Provisioning cancelled.", + "provisionInterrupted": "Orca stopped while this VM was being created. Destroy it to release any provider resources.", "provisionedRootNotConfigured": "Recipe result requests provisioned-root checkout, but the recipe is not configured for it.", "provisionedRootResultRequired": "Provisioned-root recipes must return schemaVersion 2 with checkoutMode \"provisioned-root\".", "recipeExited": "Recipe exited with code {{exitCode}}.", diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 4a74bb1fb72..935f9124bc8 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -12004,7 +12004,11 @@ "stopCleanup": "Stop cleanup", "cleanupStopped": "Cleanup stopped", "stopCleanupFailed": "Couldn’t stop Cloud VM cleanup.", - "cloudVmRefreshLabel": "Refresh Cloud VMs" + "cloudVmRefreshLabel": "Refresh Cloud VMs", + "provisioning": "Provisioning", + "untrackedRuntimeHint": "Orca never received this VM’s details. Cleanup runs destroy with only instance ID {{instanceId}}; if it fails, delete the VM in your provider, then remove it from this list.", + "removeFromList": "Remove from list", + "removeFromListFailed": "Couldn’t remove Cloud VM runtime from the list." }, "ephemeralVms": { "search": { @@ -24250,7 +24254,8 @@ "suspendExitedWithoutCode": "Suspend exited with code unknown.", "resumeExited": "Resume exited with code {{exitCode}}.", "resumeExitedWithoutCode": "Resume exited with code unknown.", - "recipeTimedOut": "The recipe {{mode}} step did not finish within {{timeout}}, so Orca stopped it. Check the recipe output, or raise timeoutSeconds in orca.yaml if it needs longer." + "recipeTimedOut": "The recipe {{mode}} step did not finish within {{timeout}}, so Orca stopped it. Check the recipe output, or raise timeoutSeconds in orca.yaml if it needs longer.", + "provisionInterrupted": "Orca stopped while this VM was being created. Destroy it to release any provider resources." } }, "installDirAcl": { diff --git a/src/renderer/src/lib/ephemeral-vm-error-copy.ts b/src/renderer/src/lib/ephemeral-vm-error-copy.ts index 69309727c1d..6e169290cad 100644 --- a/src/renderer/src/lib/ephemeral-vm-error-copy.ts +++ b/src/renderer/src/lib/ephemeral-vm-error-copy.ts @@ -48,6 +48,8 @@ const COPY: Record strin }, provisionCancelled: () => translate('ephemeralVm.errors.provisionCancelled', EN.provisionCancelled), + provisionInterrupted: () => + translate('ephemeralVm.errors.provisionInterrupted', EN.provisionInterrupted), startRefUnresolved: (p) => { const ref = text(p, 'ref') return ref === null diff --git a/src/shared/ephemeral-vm-failure-copy.ts b/src/shared/ephemeral-vm-failure-copy.ts index f80bc4171c1..604ee17201b 100644 --- a/src/shared/ephemeral-vm-failure-copy.ts +++ b/src/shared/ephemeral-vm-failure-copy.ts @@ -9,6 +9,8 @@ export const EPHEMERAL_VM_FAILURE_COPY = { runtimeNotFound: 'Unknown ephemeral VM runtime: {{runtimeId}}', runtimeRepoMissing: 'Ephemeral VM runtime has no repo id: {{runtimeId}}', provisionCancelled: 'Provisioning cancelled.', + provisionInterrupted: + 'Orca stopped while this VM was being created. Destroy it to release any provider resources.', startRefUnresolved: 'Could not resolve provisioned-root start ref: {{ref}}', defaultStartRefUnresolved: 'Could not resolve a default provisioned-root start ref.', recipeExited: 'Recipe exited with code {{exitCode}}.', diff --git a/src/shared/ephemeral-vm-recipe-lifecycle-payload.ts b/src/shared/ephemeral-vm-recipe-lifecycle-payload.ts index 4cee67602c2..3022be5f833 100644 --- a/src/shared/ephemeral-vm-recipe-lifecycle-payload.ts +++ b/src/shared/ephemeral-vm-recipe-lifecycle-payload.ts @@ -13,13 +13,13 @@ export type EphemeralVmRecipeCleanupPayload = { projectId?: string workspaceId?: string workspaceName?: string - recipeResult: EphemeralVmRecipeResult + recipeResult?: EphemeralVmRecipeResult } export function buildEphemeralVmRecipeCleanupPayload(args: { recipe: Pick context: EphemeralVmRecipeContext - recipeResult: EphemeralVmRecipeResult + recipeResult?: EphemeralVmRecipeResult }): EphemeralVmRecipeCleanupPayload { return buildEphemeralVmRecipeLifecyclePayload({ ...args, mode: 'destroy' }) } @@ -28,7 +28,7 @@ export function buildEphemeralVmRecipeLifecyclePayload(args: { mode: EphemeralVmRecipeLifecycleMode recipe: Pick context: EphemeralVmRecipeContext - recipeResult: EphemeralVmRecipeResult + recipeResult?: EphemeralVmRecipeResult }): EphemeralVmRecipeCleanupPayload { return { schemaVersion: 1, @@ -38,7 +38,7 @@ export function buildEphemeralVmRecipeLifecyclePayload(args: { projectId: args.context.projectId, workspaceId: args.context.workspaceId, workspaceName: args.context.workspaceName, - recipeResult: args.recipeResult + ...(args.recipeResult ? { recipeResult: args.recipeResult } : {}) } } diff --git a/src/shared/ephemeral-vm-recipe-process.ts b/src/shared/ephemeral-vm-recipe-process.ts index ec80ba1dd9c..21d9d972f87 100644 --- a/src/shared/ephemeral-vm-recipe-process.ts +++ b/src/shared/ephemeral-vm-recipe-process.ts @@ -37,6 +37,8 @@ export async function runRecipeCommand(args: { signal?: AbortSignal /** Wall-clock limit; on expiry the process tree is stopped like a cancel and the result has timedOut. */ timeoutMs?: number + /** Called once the OS reports the process started (never when spawning fails or is skipped). */ + onSpawn?: () => void onStdout?: (chunk: string) => void onStderr?: (chunk: string) => void spawnCommand?: typeof spawn @@ -44,6 +46,10 @@ export async function runRecipeCommand(args: { const maxBytes = clampRecipeCaptureBytes(args.maxCaptureBytes) const spawnCommand = args.spawnCommand ?? spawn + if (args.signal?.aborted) { + // Why: a step cancelled before it starts must not launch anything. + return { stdout: '', stderr: '', exitCode: null, signal: null, aborted: true } + } return new Promise((resolve, reject) => { let child: ChildProcessWithoutNullStreams try { @@ -141,6 +147,9 @@ export async function runRecipeCommand(args: { // setEncoding flushes its decoder at end-of-stream; keep that final replacement character. child.stdout.on('end', () => flushDecoder(stdoutDecoder, args.onStdout)) child.stderr.on('end', () => flushDecoder(stderrDecoder, args.onStderr)) + if (args.onSpawn) { + child.once('spawn', args.onSpawn) + } child.on('error', (error) => { fail(error) }) diff --git a/src/shared/ephemeral-vm-recipe-runner.ts b/src/shared/ephemeral-vm-recipe-runner.ts index 46aef359f0b..afb5c91554b 100644 --- a/src/shared/ephemeral-vm-recipe-runner.ts +++ b/src/shared/ephemeral-vm-recipe-runner.ts @@ -40,6 +40,8 @@ export type EphemeralVmRecipeStartArgs = { env?: NodeJS.ProcessEnv maxCaptureBytes?: number signal?: AbortSignal + /** Called once the create process has actually started. */ + onSpawn?: () => void onStdout?: (chunk: string) => void onStderr?: (chunk: string) => void spawnCommand?: typeof spawn @@ -71,7 +73,7 @@ export type EphemeralVmRecipeCleanupArgs = { recipe: OrcaVmRecipe repoPath: string context: EphemeralVmRecipeContext - recipeResult: EphemeralVmRecipeResult + recipeResult?: EphemeralVmRecipeResult env?: NodeJS.ProcessEnv maxCaptureBytes?: number signal?: AbortSignal diff --git a/src/shared/ephemeral-vm-recipe-step.ts b/src/shared/ephemeral-vm-recipe-step.ts index d56d79991bf..d3be4316814 100644 --- a/src/shared/ephemeral-vm-recipe-step.ts +++ b/src/shared/ephemeral-vm-recipe-step.ts @@ -19,6 +19,7 @@ type RecipeStepArgs = Pick< | 'env' | 'maxCaptureBytes' | 'signal' + | 'onSpawn' | 'onStdout' | 'onStderr' | 'spawnCommand' @@ -41,6 +42,7 @@ export function runRecipeStep( env: args.env, maxCaptureBytes: args.maxCaptureBytes, signal: args.signal, + onSpawn: args.onSpawn, timeoutMs: toTimeoutMs(getEphemeralVmRecipeTimeoutSeconds(args.recipe, mode)), onStdout: args.onStdout, onStderr: args.onStderr, diff --git a/src/shared/ephemeral-vm-runtime-feature-store.ts b/src/shared/ephemeral-vm-runtime-feature-store.ts index a811cb0e0c6..57cb068c611 100644 --- a/src/shared/ephemeral-vm-runtime-feature-store.ts +++ b/src/shared/ephemeral-vm-runtime-feature-store.ts @@ -151,7 +151,7 @@ export function featureEntryFromRuntime( ): EphemeralVmRuntimeFeatureEntry | null { const recipeCheckoutMode = runtime.recipe?.checkoutMode const resultCheckoutMode = - runtime.recipeResult.schemaVersion === 2 ? runtime.recipeResult.checkoutMode : undefined + runtime.recipeResult?.schemaVersion === 2 ? runtime.recipeResult.checkoutMode : undefined if (!recipeCheckoutMode && !resultCheckoutMode) { return null } @@ -192,7 +192,7 @@ function restoreRuntimeFeatures( ...(runtime.recipe && feature.recipeCheckoutMode ? { recipe: { ...runtime.recipe, checkoutMode: feature.recipeCheckoutMode } } : {}), - ...(feature.resultCheckoutMode + ...(feature.resultCheckoutMode && runtime.recipeResult ? { recipeResult: { ...runtime.recipeResult, diff --git a/src/shared/ephemeral-vm-runtime-rollback-projection.ts b/src/shared/ephemeral-vm-runtime-rollback-projection.ts index b0d8dca6dae..5d977f447d3 100644 --- a/src/shared/ephemeral-vm-runtime-rollback-projection.ts +++ b/src/shared/ephemeral-vm-runtime-rollback-projection.ts @@ -1,11 +1,30 @@ import { featureIdentity, sortRuntimeFeatures } from './ephemeral-vm-runtime-feature-store' import { + hasEphemeralVmRecipeResult, RollbackEphemeralVmRuntimeRecordSchema, + type EphemeralVmProvisionedRuntimeRecord, type EphemeralVmRuntimeRecord } from './ephemeral-vm-runtimes' +/** Older builds require `recipeResult` on every record, so write-ahead records live in a sidecar they never read. */ +export function partitionRuntimesForRollback(runtimes: readonly EphemeralVmRuntimeRecord[]): { + rollback: EphemeralVmRuntimeRecord[] + writeAhead: EphemeralVmRuntimeRecord[] +} { + const rollback: EphemeralVmRuntimeRecord[] = [] + const writeAhead: EphemeralVmRuntimeRecord[] = [] + for (const runtime of runtimes) { + if (hasEphemeralVmRecipeResult(runtime)) { + rollback.push(projectRuntimeForRollback(runtime)) + } else { + writeAhead.push(runtime) + } + } + return { rollback, writeAhead } +} + export function projectRuntimeForRollback( - runtime: EphemeralVmRuntimeRecord + runtime: EphemeralVmProvisionedRuntimeRecord ): EphemeralVmRuntimeRecord { const recipe = runtime.recipe ? (({ checkoutMode: _checkoutMode, ...rollbackRecipe }) => rollbackRecipe)(runtime.recipe) diff --git a/src/shared/ephemeral-vm-runtime-store-rollback.test.ts b/src/shared/ephemeral-vm-runtime-store-rollback.test.ts index d2bd379205c..c325c49e8ce 100644 --- a/src/shared/ephemeral-vm-runtime-store-rollback.test.ts +++ b/src/shared/ephemeral-vm-runtime-store-rollback.test.ts @@ -9,6 +9,7 @@ import { import { EphemeralVmRuntimeStoreError, getEphemeralVmRuntimeStorePath, + getEphemeralVmWriteAheadRuntimeStorePath, listEphemeralVmRuntimes, updateEphemeralVmRuntimeStatus, upsertEphemeralVmRuntime @@ -18,6 +19,7 @@ import { RollbackEphemeralVmRuntimeStoreSchema, type EphemeralVmRuntimeRecord } from './ephemeral-vm-runtimes' +import { findWorkspaceEphemeralVmRuntime } from './ephemeral-vm-workspace-runtime' function runtimeRecord( overrides: Partial = {} @@ -111,6 +113,99 @@ describe('ephemeral VM runtime store rollback projection', () => { expect(listEphemeralVmRuntimes(userDataPath)).toEqual([provisioned, ordinary]) }) + it('keeps result-less write-ahead records out of the file older builds read', () => { + const userDataPath = makeUserDataPath() + const ordinary = upsertEphemeralVmRuntime(userDataPath, runtimeRecord()) + const { recipeResult: _recipeResult, ...writeAheadFields } = provisionedRootRecord() + const writeAhead = upsertEphemeralVmRuntime(userDataPath, { + ...writeAheadFields, + status: 'provisioning' + }) + + const persisted = JSON.parse(readFileSync(getEphemeralVmRuntimeStorePath(userDataPath), 'utf8')) + expect(RollbackEphemeralVmRuntimeStoreSchema.parse(persisted).runtimes).toEqual([ + expect.objectContaining({ id: 'ordinary-runtime' }) + ]) + expect( + JSON.parse(readFileSync(getEphemeralVmWriteAheadRuntimeStorePath(userDataPath), 'utf8')) + .runtimes + ).toEqual([expect.objectContaining({ id: 'provisioned-runtime', status: 'provisioning' })]) + expect(listEphemeralVmRuntimes(userDataPath)).toEqual([writeAhead, ordinary]) + + const running = updateEphemeralVmRuntimeStatus(userDataPath, 'provisioned-runtime', { + status: 'running', + recipeResult: provisionedRootRecord().recipeResult, + updatedAt: 2_000 + }) + + const promoted = JSON.parse(readFileSync(getEphemeralVmRuntimeStorePath(userDataPath), 'utf8')) + expect(RollbackEphemeralVmRuntimeStoreSchema.parse(promoted).runtimes).toEqual([ + expect.objectContaining({ + id: 'provisioned-runtime', + recipe: expect.not.objectContaining({ checkoutMode: expect.anything() }), + recipeResult: expect.objectContaining({ schemaVersion: 1 }) + }), + expect.objectContaining({ id: 'ordinary-runtime' }) + ]) + expect( + JSON.parse(readFileSync(getEphemeralVmWriteAheadRuntimeStorePath(userDataPath), 'utf8')) + .runtimes + ).toEqual([]) + expect(listEphemeralVmRuntimes(userDataPath)).toEqual([running, ordinary]) + expect(running).toEqual(provisionedRootRecord()) + }) + + it('prefers the main record when a crash leaves a stale write-ahead copy', () => { + const userDataPath = makeUserDataPath() + const running = upsertEphemeralVmRuntime(userDataPath, runtimeRecord()) + const { recipeResult: _recipeResult, ...stale } = running + writeFileSync( + getEphemeralVmWriteAheadRuntimeStorePath(userDataPath), + JSON.stringify({ version: 1, runtimes: [{ ...stale, status: 'provisioning' }] }) + ) + + expect(listEphemeralVmRuntimes(userDataPath)).toEqual([running]) + }) + + it('never overwrites a write-ahead file it cannot read', () => { + const userDataPath = makeUserDataPath() + upsertEphemeralVmRuntime(userDataPath, runtimeRecord()) + const sidecarPath = getEphemeralVmWriteAheadRuntimeStorePath(userDataPath) + const unreadable = '{"version":2,"runtimes":[{"id":"from-a-newer-build"}]}' + writeFileSync(sidecarPath, unreadable) + + // Tracked VMs keep working, and the unreadable records survive the write. + updateEphemeralVmRuntimeStatus(userDataPath, 'ordinary-runtime', { status: 'suspended' }) + expect(readFileSync(sidecarPath, 'utf8')).toBe(unreadable) + + // A new record that could not be kept is refused before its create starts. + const { recipeResult: _recipeResult, ...writeAhead } = runtimeRecord({ id: 'new-runtime' }) + expect(() => + upsertEphemeralVmRuntime(userDataPath, { ...writeAhead, status: 'provisioning' }) + ).toThrow() + expect(readFileSync(sidecarPath, 'utf8')).toBe(unreadable) + }) + + it('never offers a result-less write-ahead record to workspace suspend or resume', () => { + const userDataPath = makeUserDataPath() + const { recipeResult: _recipeResult, ...writeAhead } = runtimeRecord({ + repoId: 'repo-1', + workspaceId: 'workspace-1' + }) + upsertEphemeralVmRuntime(userDataPath, { ...writeAhead, status: 'provisioning' }) + + expect(findWorkspaceEphemeralVmRuntime(userDataPath, 'workspace-1')).toBeUndefined() + + updateEphemeralVmRuntimeStatus(userDataPath, 'ordinary-runtime', { + status: 'running', + recipeResult: runtimeRecord().recipeResult + }) + expect(findWorkspaceEphemeralVmRuntime(userDataPath, 'workspace-1')).toMatchObject({ + id: 'ordinary-runtime', + repoId: 'repo-1' + }) + }) + it('migrates current-main poisoned bytes when they are first read', () => { const userDataPath = makeUserDataPath() const poisoned = { diff --git a/src/shared/ephemeral-vm-runtime-store.ts b/src/shared/ephemeral-vm-runtime-store.ts index 7b99b1ec325..d4d21d04c3c 100644 --- a/src/shared/ephemeral-vm-runtime-store.ts +++ b/src/shared/ephemeral-vm-runtime-store.ts @@ -16,10 +16,11 @@ import { } from './ephemeral-vm-runtime-feature-store' import { mergeRuntimeFeatures, - projectRuntimeForRollback, + partitionRuntimesForRollback, runtimeFeatureListsEqual } from './ephemeral-vm-runtime-rollback-projection' import { + hasEphemeralVmRecipeResult, EphemeralVmRuntimeRecordSchema, EphemeralVmRuntimeStoreSchema, RollbackEphemeralVmRuntimeStoreSchema, @@ -28,6 +29,12 @@ import { type EphemeralVmRuntimeStatus, type EphemeralVmRuntimeStore } from './ephemeral-vm-runtimes' +import { + withWriteAheadRuntimes, + writeWriteAheadRuntimes +} from './ephemeral-vm-runtime-write-ahead-store' + +export { getEphemeralVmWriteAheadRuntimeStorePath } from './ephemeral-vm-runtime-write-ahead-store' const EPHEMERAL_VM_RUNTIMES_FILE = 'orca-ephemeral-vm-runtimes.json' export const MAX_EPHEMERAL_VM_RUNTIME_STORE_FILE_BYTES = 1024 * 1024 @@ -52,20 +59,6 @@ export function listEphemeralVmRuntimes(userDataPath: string): EphemeralVmRuntim return readEphemeralVmRuntimeStore(userDataPath).store.runtimes } -/** The live recipe VM behind a workspace, if it has one its recipe can suspend and resume. */ -export function findWorkspaceEphemeralVmRuntime( - userDataPath: string, - workspaceId: string -): (EphemeralVmRuntimeRecord & { repoId: string }) | undefined { - const runtime = listEphemeralVmRuntimes(userDataPath).find( - (entry) => - entry.workspaceId === workspaceId && - entry.status !== 'cleaned' && - entry.status !== 'cleanup_pending' - ) - return runtime?.repoId ? { ...runtime, repoId: runtime.repoId } : undefined -} - export function upsertEphemeralVmRuntime( userDataPath: string, record: EphemeralVmRuntimeRecord @@ -75,6 +68,7 @@ export function upsertEphemeralVmRuntime( const previous = loaded.store.runtimes.find((entry) => entry.id === parsed.id) if ( previous && + hasEphemeralVmRecipeResult(previous) && featureIdentity(previous) === featureIdentity(parsed) && !runtimeFeaturesEqual(previous, parsed) ) { @@ -108,9 +102,11 @@ export function upsertEphemeralVmRuntimeRollbackRecovery( path, RollbackEphemeralVmRuntimeStoreSchema.parse({ version: 1, - runtimes: [...loaded.store.runtimes.filter((entry) => entry.id !== parsed.id), parsed] - .sort(compareRuntimeRecords) - .map(projectRuntimeForRollback) + runtimes: partitionRuntimesForRollback( + [...loaded.store.runtimes.filter((entry) => entry.id !== parsed.id), parsed].sort( + compareRuntimeRecords + ) + ).rollback }), MAX_EPHEMERAL_VM_RUNTIME_STORE_FILE_BYTES, { durable: true } @@ -228,7 +224,10 @@ function readEphemeralVmRuntimeStore(userDataPath: string): LoadedEphemeralVmRun const path = getEphemeralVmRuntimeStorePath(userDataPath) if (!existsSync(path)) { return { - store: { version: 1, runtimes: [] }, + store: { + version: 1, + runtimes: withWriteAheadRuntimes(userDataPath, []).sort(compareRuntimeRecords) + }, features: readEphemeralVmRuntimeFeatureStore(userDataPath) } } @@ -243,9 +242,10 @@ function readEphemeralVmRuntimeStore(userDataPath: string): LoadedEphemeralVmRun const features = readEphemeralVmRuntimeFeatureStore(userDataPath) const store: EphemeralVmRuntimeStore = { version: 1, - runtimes: restoreRuntimeFeatureList(parsed.runtimes, features.features).sort( - compareRuntimeRecords - ) + runtimes: withWriteAheadRuntimes( + userDataPath, + restoreRuntimeFeatureList(parsed.runtimes, features.features) + ).sort(compareRuntimeRecords) } if (features.writable && !RollbackEphemeralVmRuntimeStoreSchema.safeParse(persisted).success) { try { @@ -271,9 +271,10 @@ function writeEphemeralVmRuntimeStore( const path = getEphemeralVmRuntimeStorePath(userDataPath) try { const parsed = EphemeralVmRuntimeStoreSchema.parse(store) + const { rollback, writeAhead } = partitionRuntimesForRollback(parsed.runtimes) const requiredFeatures = mergeRuntimeFeatures( [], - parsed.runtimes.flatMap((entry) => { + parsed.runtimes.filter(hasEphemeralVmRecipeResult).flatMap((entry) => { const feature = featureEntryFromRuntime(entry) return feature ? [feature] : [] }) @@ -281,13 +282,12 @@ function writeEphemeralVmRuntimeStore( const preparedFeatures = mergeRuntimeFeatures(features.features, requiredFeatures) writeSecureJsonFileWithinLimit( path, - RollbackEphemeralVmRuntimeStoreSchema.parse({ - version: 1, - runtimes: parsed.runtimes.map(projectRuntimeForRollback) - }), + RollbackEphemeralVmRuntimeStoreSchema.parse({ version: 1, runtimes: rollback }), MAX_EPHEMERAL_VM_RUNTIME_STORE_FILE_BYTES, { durable: preparedFeatures.length > 0 || features.features.length > 0 } ) + // Main first so a record gaining its recipe result is never absent from both files. + writeWriteAheadRuntimes(userDataPath, writeAhead) if (!features.writable && requiredFeatures.length > 0) { throw new EphemeralVmRuntimeStoreError( 'runtime_error', diff --git a/src/shared/ephemeral-vm-runtime-write-ahead-store.ts b/src/shared/ephemeral-vm-runtime-write-ahead-store.ts new file mode 100644 index 00000000000..0660585d407 --- /dev/null +++ b/src/shared/ephemeral-vm-runtime-write-ahead-store.ts @@ -0,0 +1,89 @@ +import { join } from 'node:path' +import { z } from 'zod' +import { readNodeFileSyncWithinLimit } from './file-content/node-bounded-file-reader' +import { writeSecureJsonFileWithinLimit } from './secure-storage/bounded-secure-json-file' +import { hardenExistingSecureFile } from './secure-storage/secure-file' +import { + EphemeralVmRuntimeRecordSchema, + hasEphemeralVmRecipeResult, + type EphemeralVmRuntimeRecord +} from './ephemeral-vm-runtimes' + +// Older builds never read this file, so result-less write-ahead records cannot break their store parse. +const EPHEMERAL_VM_WRITE_AHEAD_RUNTIMES_FILE = 'orca-ephemeral-vm-runtimes-write-ahead.json' +const MAX_WRITE_AHEAD_STORE_FILE_BYTES = 1024 * 1024 + +export function getEphemeralVmWriteAheadRuntimeStorePath(userDataPath: string): string { + return join(userDataPath, EPHEMERAL_VM_WRITE_AHEAD_RUNTIMES_FILE) +} + +const WriteAheadRuntimeStoreSchema = z.object({ + version: z.literal(1), + runtimes: z.array(EphemeralVmRuntimeRecordSchema) +}) + +/** The main file wins: a crash between flipping to running and pruning the sidecar leaves both copies. */ +export function withWriteAheadRuntimes( + userDataPath: string, + mainRuntimes: EphemeralVmRuntimeRecord[] +): EphemeralVmRuntimeRecord[] { + const mainIds = new Set(mainRuntimes.map((entry) => entry.id)) + return [ + ...mainRuntimes, + ...readWriteAheadRuntimes(userDataPath).filter((entry) => !mainIds.has(entry.id)) + ] +} + +function readWriteAheadRuntimes(userDataPath: string): EphemeralVmRuntimeRecord[] { + return readWriteAheadFile(getEphemeralVmWriteAheadRuntimeStorePath(userDataPath)).runtimes +} + +type WriteAheadFileState = 'missing' | 'readable' | 'unreadable' + +function readWriteAheadFile(path: string): { + runtimes: EphemeralVmRuntimeRecord[] + state: WriteAheadFileState +} { + let text: string + try { + text = readNodeFileSyncWithinLimit(path, MAX_WRITE_AHEAD_STORE_FILE_BYTES).buffer.toString( + 'utf8' + ) + } catch (error) { + const missing = error instanceof Error && 'code' in error && error.code === 'ENOENT' + return { runtimes: [], state: missing ? 'missing' : 'unreadable' } + } + try { + hardenExistingSecureFile(path) + const runtimes = WriteAheadRuntimeStoreSchema.parse(JSON.parse(text)).runtimes.filter( + (entry) => !hasEphemeralVmRecipeResult(entry) + ) + return { runtimes, state: 'readable' } + } catch { + // Why: an unreadable sidecar must not block cleanup of tracked VMs in the main file. + return { runtimes: [], state: 'unreadable' } + } +} + +export function writeWriteAheadRuntimes( + userDataPath: string, + runtimes: readonly EphemeralVmRuntimeRecord[] +): void { + const path = getEphemeralVmWriteAheadRuntimeStorePath(userDataPath) + const existing = readWriteAheadFile(path) + if (existing.state === 'unreadable') { + // Why: never overwrite records this build cannot read (corrupt, or from a newer build); they + // may be the only trace of a VM. Refuse new records instead, before their create starts. + if (runtimes.length > 0) { + throw new Error(`Could not update Orca ephemeral VM provisioning records at ${path}.`) + } + return + } + // Most store writes touch only tracked VMs; skip the durable write when there is nothing to change. + if (runtimes.length === 0 && existing.runtimes.length === 0) { + return + } + writeSecureJsonFileWithinLimit(path, { version: 1, runtimes }, MAX_WRITE_AHEAD_STORE_FILE_BYTES, { + durable: true + }) +} diff --git a/src/shared/ephemeral-vm-runtimes.ts b/src/shared/ephemeral-vm-runtimes.ts index f8646c0fe0e..2eb6d84dead 100644 --- a/src/shared/ephemeral-vm-runtimes.ts +++ b/src/shared/ephemeral-vm-runtimes.ts @@ -51,6 +51,8 @@ export const EphemeralVmRuntimeRecordSchema = z.object({ /** Snapshot lifecycle commands so later pack updates/disabling/removal cannot change an existing runtime’s cleanup. */ recipe: EphemeralVmRuntimeRecipeSchema.optional(), repoId: z.string().min(1).optional(), + /** Repo checkout the lifecycle commands ran from, so destroy still works if `repoId` stops resolving. */ + repoPath: z.string().min(1).optional(), projectId: z.string().min(1).optional(), workspaceId: z.string().min(1).optional(), workspaceName: z.string().min(1).optional(), @@ -67,11 +69,31 @@ export const EphemeralVmRuntimeRecordSchema = z.object({ cleanupLastErrorParams: z.record(z.string(), z.union([z.string(), z.number()])).optional(), createdAt: z.number().finite(), updatedAt: z.number().finite(), - recipeResult: EphemeralVmRecipeResultSchema + /** Absent only on write-ahead records persisted before `create` returned a result. */ + recipeResult: EphemeralVmRecipeResultSchema.optional() }) export type EphemeralVmRuntimeRecord = z.infer +export type EphemeralVmProvisionedRuntimeRecord = EphemeralVmRuntimeRecord & { + recipeResult: NonNullable +} + +export function hasEphemeralVmRecipeResult( + runtime: EphemeralVmRuntimeRecord +): runtime is EphemeralVmProvisionedRuntimeRecord { + return runtime.recipeResult !== undefined +} + +export function requireEphemeralVmRecipeResult( + runtime: EphemeralVmRuntimeRecord +): EphemeralVmProvisionedRuntimeRecord { + if (!hasEphemeralVmRecipeResult(runtime)) { + throw new Error(`Ephemeral VM runtime never finished provisioning: ${runtime.id}`) + } + return runtime +} + export const EphemeralVmRuntimeStoreSchema = z.object({ version: z.literal(1), runtimes: z.array(EphemeralVmRuntimeRecordSchema) diff --git a/src/shared/ephemeral-vm-workspace-runtime.ts b/src/shared/ephemeral-vm-workspace-runtime.ts new file mode 100644 index 00000000000..5e7e1c1f3f4 --- /dev/null +++ b/src/shared/ephemeral-vm-workspace-runtime.ts @@ -0,0 +1,21 @@ +import { listEphemeralVmRuntimes } from './ephemeral-vm-runtime-store' +import { + hasEphemeralVmRecipeResult, + type EphemeralVmProvisionedRuntimeRecord +} from './ephemeral-vm-runtimes' + +/** The live recipe VM behind a workspace, if it has one its recipe can suspend and resume. */ +export function findWorkspaceEphemeralVmRuntime( + userDataPath: string, + workspaceId: string +): (EphemeralVmProvisionedRuntimeRecord & { repoId: string }) | undefined { + // Write-ahead rows never got a recipe result, so there is no VM to suspend or resume. + const runtime = listEphemeralVmRuntimes(userDataPath).find( + (entry): entry is EphemeralVmProvisionedRuntimeRecord => + entry.workspaceId === workspaceId && + hasEphemeralVmRecipeResult(entry) && + entry.status !== 'cleaned' && + entry.status !== 'cleanup_pending' + ) + return runtime?.repoId ? { ...runtime, repoId: runtime.repoId } : undefined +} From c70e8900139caecc6e8d0490db9db8aca625cb93 Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Sun, 8 Nov 2026 19:32:05 -0500 Subject: [PATCH 02/80] fix(mobile): race Relay when a mid-session LAN redial stalls (STA-14363) (#40026) --- ...ile-endpoint-supervisor-relay-race.test.ts | 128 ++++++++++++++++++ .../connection/mobile-endpoint-supervisor.ts | 6 + 2 files changed, 134 insertions(+) create mode 100644 mobile/src/transport/connection/mobile-endpoint-supervisor-relay-race.test.ts diff --git a/mobile/src/transport/connection/mobile-endpoint-supervisor-relay-race.test.ts b/mobile/src/transport/connection/mobile-endpoint-supervisor-relay-race.test.ts new file mode 100644 index 00000000000..81ac5496981 --- /dev/null +++ b/mobile/src/transport/connection/mobile-endpoint-supervisor-relay-race.test.ts @@ -0,0 +1,128 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { + dependencies, + FakeLogicalClient, + FakeRelaySession, + FakeSession, + host, + relay +} from './mobile-endpoint-supervisor-test-fakes' +import { MobileEndpointSupervisor } from './mobile-endpoint-supervisor' +import { createStableLogicalRpcClient } from './stable-logical-rpc-client' + +vi.mock('react-native', () => ({ Platform: { OS: 'ios' } })) +vi.mock('expo-secure-store', () => ({ WHEN_UNLOCKED_THIS_DEVICE_ONLY: 'when-unlocked' })) +vi.mock('expo-crypto', () => ({ getRandomBytes: (length: number) => new Uint8Array(length) })) + +// A network-change nudge abandons the stale LAN dial and redials at once. +function redialDirect(logical: FakeLogicalClient): void { + logical.publishState('reconnecting') + logical.publishState('connecting') +} + +async function startOnLan(openRelay = vi.fn(() => new FakeRelaySession('connected'))) { + const logical = new FakeLogicalClient('connected', 'lan') + // Full-width jitter so the shared cooldown runs at its real cadence. + const deps = dependencies({ openRelay, randomBytes: () => new Uint8Array([255, 255]) }) + const supervisor = new MobileEndpointSupervisor(logical, host.id, relay, deps) + await supervisor.start() + await vi.advanceTimersByTimeAsync(0) + return { logical, openRelay, supervisor } +} + +describe('mobile endpoint supervisor relay race after a mid-session LAN loss', () => { + beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-11-08T12:00:00Z')) + }) + + afterEach(() => { + vi.useRealTimers() + }) + + it('moves to relay while network-change redials keep the LAN dial connecting', async () => { + const { logical, openRelay, supervisor } = await startOnLan() + + // Nudges every second never let the dial age out of connecting; the race fires 2.5 s into the outage. + for (let i = 0; i < 3; i++) { + redialDirect(logical) + await vi.advanceTimersByTimeAsync(1_000) + } + + expect(openRelay).toHaveBeenCalledOnce() + expect(logical.getActivePath()).toBe('relay') + expect(logical.getState()).toBe('connected') + supervisor.stop() + }) + + it('paces relay dials by the shared cooldown while relay is also unreachable', async () => { + // A relay socket that never authenticates fails its migration. + const { logical, openRelay, supervisor } = await startOnLan( + vi.fn(() => new FakeRelaySession('connecting')) + ) + + for (let i = 0; i < 60; i++) { + redialDirect(logical) + await vi.advanceTimersByTimeAsync(2_000) + } + + // 60 redials in 2 min; each attempt is a cell dial plus a director-resolved retry. + // Racing every redial would open 120 sockets; the 0.5 s→30 s backoff keeps it under 20. + expect(openRelay.mock.calls.length).toBeGreaterThan(0) + expect(openRelay.mock.calls.length).toBeLessThanOrEqual(20) + supervisor.stop() + }) + + it('races each slow LAN blip once and closes the relay socket the LAN beat', async () => { + // The real logical client owns the lost-race close: it aborts after relay authentication. + const direct = new FakeSession('connected') + const logical = createStableLogicalRpcClient(direct, 'lan') + const relaySessions: FakeRelaySession[] = [] + const openRelay = vi.fn(() => { + const session = new FakeRelaySession('connecting') + relaySessions.push(session) + return session + }) + const supervisor = new MobileEndpointSupervisor( + logical, + host.id, + relay, + dependencies({ openRelay, randomBytes: () => new Uint8Array([255, 255]) }) + ) + await supervisor.start() + + for (let flap = 0; flap < 10; flap++) { + direct.publishState('reconnecting') + direct.publishState('connecting') + await vi.advanceTimersByTimeAsync(3_000) + direct.publishState('connected') + relaySessions.at(-1)?.publishState('connected') + await vi.advanceTimersByTimeAsync(10_000) + } + + // Accepted cost: one relay dial per blip past the 2.5 s head start, each closed once it loses. + expect(openRelay).toHaveBeenCalledTimes(10) + expect(relaySessions.every((session) => session.close.mock.calls.length > 0)).toBe(true) + expect(logical.getActivePath()).toBe('lan') + supervisor.stop() + }) + + it('drops a race deferred by cooldown once the LAN comes back', async () => { + const { logical, openRelay, supervisor } = await startOnLan( + vi.fn(() => new FakeRelaySession('connecting')) + ) + for (let i = 0; i < 15; i++) { + redialDirect(logical) + await vi.advanceTimersByTimeAsync(2_000) + } + const dialsDuringOutage = openRelay.mock.calls.length + + logical.publishState('connected') + supervisor.nudge('network-change') + await vi.advanceTimersByTimeAsync(60_000) + + expect(logical.getActivePath()).toBe('lan') + expect(openRelay).toHaveBeenCalledTimes(dialsDuringOutage) + supervisor.stop() + }) +}) diff --git a/mobile/src/transport/connection/mobile-endpoint-supervisor.ts b/mobile/src/transport/connection/mobile-endpoint-supervisor.ts index 6da950ca9e4..afaaf820b14 100644 --- a/mobile/src/transport/connection/mobile-endpoint-supervisor.ts +++ b/mobile/src/transport/connection/mobile-endpoint-supervisor.ts @@ -148,6 +148,8 @@ export class MobileEndpointSupervisor { if (state === 'connected') { this.directGrace.onConnected() if (this.logical.getActivePath() !== 'relay') { + // A direct win answers any race intent deferred by cooldown; replaying it would replace a healthy LAN. + this.pendingReplace = false void this.rotateCredentialIfNeeded(this.relayReconnect.resetForDirectConnection()) } this.directProbe.schedule() @@ -159,6 +161,10 @@ export class MobileEndpointSupervisor { recoveryPresentation.onActiveFailure(this.logical, this.relayReconnect, state, this.bundle) const relayFailure = this.relayReconnect.handleStateFailure(this.logical, state) logRelayDialFailure(this.logRelay, relayFailure, 'active-session') + // Redials keep direct in connecting, which unforced recovery waits out; one timer per outage races them. + if (this.logical.getActivePath() !== 'relay') { + this.directGrace.arm() + } } }) void this.directGrace.loadLastGoodPath() From e1381cfa3e8d36bb1efc79272adf1ea6b31eccad Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:35:43 -0800 Subject: [PATCH 03/80] Rotate the server pairing identity when ORCA_INSTANCE_ID changes (#39217) * Rotate the server pairing identity when ORCA_INSTANCE_ID changes * Back up replaced identity files, keep the recorded id on reset, keep staged files once the rotating marker is published * Add identity-reset files to the sync fs baseline and document the backup folder * Report a reset honestly after finishing an interrupted rotation; keep first backups and the newest one --- config/build-plugins/cli-main-entry-names.ts | 1 + config/electron-builder.config.cjs | 1 + config/main-sync-fs-baseline.txt | 2 + docs/site/content/docs/remote-servers.mdx | 11 + src/cli/handler-group-manifest.ts | 6 + src/cli/handlers/serve-reset-identity.test.ts | 65 ++++ src/cli/handlers/serve-reset-identity.ts | 61 ++++ src/cli/specs/serve.ts | 13 + src/main/orcad/orcad-host-identity-reset.ts | 51 +++ src/main/orcad/orcad-instance-lock.ts | 4 +- .../runtime/host-instance-identity.test.ts | 299 ++++++++++++++++++ src/main/runtime/host-instance-identity.ts | 288 +++++++++++++++++ .../rpc/server/pairing/e2ee-keypair.ts | 10 +- .../rpc/server/runtime-rpc-binary-routing.ts | 29 ++ .../server/runtime-rpc-pairing-offer.test.ts | 44 ++- 15 files changed, 881 insertions(+), 4 deletions(-) create mode 100644 src/cli/handlers/serve-reset-identity.test.ts create mode 100644 src/cli/handlers/serve-reset-identity.ts create mode 100644 src/main/orcad/orcad-host-identity-reset.ts create mode 100644 src/main/runtime/host-instance-identity.test.ts create mode 100644 src/main/runtime/host-instance-identity.ts diff --git a/config/build-plugins/cli-main-entry-names.ts b/config/build-plugins/cli-main-entry-names.ts index 92a872d7a51..56905221b8e 100644 --- a/config/build-plugins/cli-main-entry-names.ts +++ b/config/build-plugins/cli-main-entry-names.ts @@ -10,6 +10,7 @@ export const CLI_MAIN_ENTRY_NAMES = [ 'skills/bundled-skill-install/bundled-skill-installation', 'filesystem/macos-tree-clone', 'orcad/orcad-stale-lock-reclaim', + 'orcad/orcad-host-identity-reset', 'plugins/plugin-authoring-checks', ...[ 'access', diff --git a/config/electron-builder.config.cjs b/config/electron-builder.config.cjs index 45e04fdcd0a..a727851532d 100644 --- a/config/electron-builder.config.cjs +++ b/config/electron-builder.config.cjs @@ -374,6 +374,7 @@ module.exports = { 'out/main/parcel-watcher-process-entry.js', 'out/main/orcad/orcad-local-serve-selection-entry.js', 'out/main/orcad/orcad-stale-lock-reclaim.js', + 'out/main/orcad/orcad-host-identity-reset.js', 'out/main/chunks/**', 'out/renderer/**', 'out/mobile-web/**', diff --git a/config/main-sync-fs-baseline.txt b/config/main-sync-fs-baseline.txt index 0eccabfb512..edeab9c15cd 100644 --- a/config/main-sync-fs-baseline.txt +++ b/config/main-sync-fs-baseline.txt @@ -355,6 +355,7 @@ 3 src/main/orcad/orcad-data-root-privacy.ts 4 src/main/orcad/orcad-host-account-launch-env-store.ts 1 src/main/orcad/orcad-host-adapters.ts +1 src/main/orcad/orcad-host-identity-reset.ts 2 src/main/orcad/orcad-idle-stop-record.ts 3 src/main/orcad/orcad-instance-lock-hold.ts 16 src/main/orcad/orcad-instance-lock-reclaim.ts @@ -474,6 +475,7 @@ 7 src/main/runtime/agent-session/records/agent-session-runtime-end-record.ts 1 src/main/runtime/agent-session/structured-agent-session-attachment-wiring.ts 1 src/main/runtime/client/navigation/desktop-chat-creation-history.ts +9 src/main/runtime/host-instance-identity.ts 4 src/main/runtime/hosts/platform-probe/host-descriptor.ts 5 src/main/runtime/mobile-cloud-relay/relay-region-preference.ts 2 src/main/runtime/mobile-cloud-relay/relay-revoke-outbox.ts diff --git a/docs/site/content/docs/remote-servers.mdx b/docs/site/content/docs/remote-servers.mdx index f402cc99f1d..866722a7fc5 100644 --- a/docs/site/content/docs/remote-servers.mdx +++ b/docs/site/content/docs/remote-servers.mdx @@ -217,6 +217,17 @@ orca-ide serve --pairing-address 100.64.1.20 This only stops Orca from turning the GPU off. Chromium may still render in software on Orca's own Xvfb, which has no GPU of its own; open `chrome://gpu` in a browser pane to check, and point `DISPLAY` at a GPU-backed X server if it reports software rendering. If you installed the server with `orca serve service install`, add the variable to the service unit yourself. If the server stops starting or browser panes crash with it set, unset it to go back to software rendering. +### Copies of a machine image + +Copies of a machine image share the server identity: the pairing key, the paired devices, and the installation id. Revoking a device on one copy leaves it working on the others, and the desktop app treats all copies as one server. Set `ORCA_INSTANCE_ID` per instance (for example the cloud-init instance id) in the environment that starts `orca serve`, or run `orca serve reset-identity` on each copy. + +- With `ORCA_INSTANCE_ID` set, Orca records the value with the server identity. If a later start sees a different value, Orca creates a new identity and removes all paired devices on that copy, so clients pair with it again. +- The first value Orca sees on an existing server is only recorded, so setting the variable later keeps existing pairings. Use a value that stays the same across restarts of one instance; a value that changes on every boot forces a new pairing each time. +- When `ORCA_INSTANCE_ID` is not set, Orca never changes the identity on its own. +- If Orca cannot write the new identity (for example on a full or read-only disk), it keeps serving the current one unchanged, logs an error, and tries again on the next start. +- `orca serve reset-identity` does the same reset on demand. Stop Orca on that machine first; the command asks for confirmation, or pass `--yes`. +- Each reset moves the replaced files into a new folder under `orca-identity-backups` in the Orca data directory (the five most recent are kept). To undo an unwanted reset, stop Orca and copy the files from that folder back into the data directory. + ### Mobile from a headless server For the Orca mobile app, request a mobile-scoped QR code and link: diff --git a/src/cli/handler-group-manifest.ts b/src/cli/handler-group-manifest.ts index f39a640858e..9cc19bf9f86 100644 --- a/src/cli/handler-group-manifest.ts +++ b/src/cli/handler-group-manifest.ts @@ -26,6 +26,12 @@ export const HANDLER_GROUPS: readonly HandlerGroup[] = [ load: async () => (await import('./handlers/core.js')).CORE_HANDLERS }, ...SERVE_PAIRING_HANDLER_GROUPS, + { + name: 'serve-reset-identity', + keys: ['serve reset-identity'], + load: async () => + (await import('./handlers/serve-reset-identity.js')).SERVE_RESET_IDENTITY_HANDLERS + }, { name: 'serve-service', keys: ['serve service install', 'serve service status', 'serve service uninstall'], diff --git a/src/cli/handlers/serve-reset-identity.test.ts b/src/cli/handlers/serve-reset-identity.test.ts new file mode 100644 index 00000000000..eb4fa8d2c89 --- /dev/null +++ b/src/cli/handlers/serve-reset-identity.test.ts @@ -0,0 +1,65 @@ +import { chmodSync, mkdtempSync, statSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { describe, expect, it } from 'vitest' +import { resetServeHostIdentity } from './serve-reset-identity' +import { loadOrCreateE2EEKeypair } from '../../main/runtime/rpc/server/pairing/e2ee-keypair' +import { DeviceRegistry } from '../../main/runtime/rpc/server/pairing/device-registry' +import { acquireOrcadInstanceLock } from '../../main/orcad/orcad-instance-lock' +import { + ensureHostInstanceIdentity, + hostInstanceIdFromEnv +} from '../../main/runtime/host-instance-identity' + +function pairedProfile() { + const dir = mkdtempSync(join(tmpdir(), 'orca-reset-identity-')) + chmodSync(dir, 0o700) + const registry = new DeviceRegistry(dir) + const { token } = registry.addDevice('Phone') + return { dir, token, publicKeyB64: loadOrCreateE2EEKeypair(dir).publicKeyB64 } +} + +describe('resetServeHostIdentity', () => { + it('rotates the key and removes paired devices, then stays put across restarts', async () => { + const before = pairedProfile() + await resetServeHostIdentity(before.dir) + + const publicKeyB64 = loadOrCreateE2EEKeypair(before.dir).publicKeyB64 + expect(publicKeyB64).not.toBe(before.publicKeyB64) + expect(new DeviceRegistry(before.dir).validateToken(before.token)).toBeNull() + expect(ensureHostInstanceIdentity(before.dir, null)).toEqual({ kind: 'unchanged' }) + // A variable adopted after the reset is only recorded. + expect( + ensureHostInstanceIdentity(before.dir, hostInstanceIdFromEnv({ ORCA_INSTANCE_ID: 'vm-a' })) + ).toEqual({ kind: 'recorded' }) + expect(loadOrCreateE2EEKeypair(before.dir).publicKeyB64).toBe(publicKeyB64) + }) + + it('refuses while an Orca holds the profile lock and changes nothing', async () => { + const before = pairedProfile() + const lock = acquireOrcadInstanceLock(before.dir) + try { + await expect(resetServeHostIdentity(before.dir)).rejects.toThrow(/Stop Orca/) + } finally { + lock.release() + } + expect(loadOrCreateE2EEKeypair(before.dir).publicKeyB64).toBe(before.publicKeyB64) + expect(new DeviceRegistry(before.dir).validateToken(before.token)).not.toBeNull() + }) + + it.skipIf(process.platform === 'win32')( + 'leaves desktop profile permissions alone when it refuses because the desktop app runs', + async () => { + const before = pairedProfile() + chmodSync(before.dir, 0o755) + const lock = acquireOrcadInstanceLock(before.dir, { role: 'desktop' }) + try { + await expect(resetServeHostIdentity(before.dir)).rejects.toThrow(/Stop Orca/) + } finally { + lock.release() + } + expect(statSync(before.dir).mode & 0o777).toBe(0o755) + expect(loadOrCreateE2EEKeypair(before.dir).publicKeyB64).toBe(before.publicKeyB64) + } + ) +}) diff --git a/src/cli/handlers/serve-reset-identity.ts b/src/cli/handlers/serve-reset-identity.ts new file mode 100644 index 00000000000..310b27eac9e --- /dev/null +++ b/src/cli/handlers/serve-reset-identity.ts @@ -0,0 +1,61 @@ +import type { CommandHandler } from '../dispatch' +import { RuntimeClientError } from '../runtime/types' +import { resolveLaunchUserDataPath } from '../runtime/launch-user-data-path' +import { pluginDevWatchConfirmFor } from '../plugin-dev-watch/plugin-dev-watch-approval' + +const RESET_WARNING = + "This gives this machine's Orca server a new pairing key and installation id and removes every " + + 'paired device. Desktops and phones paired with it must pair again.' + +export async function resetServeHostIdentity(userDataPath: string): Promise { + // Lazy: only this command needs orcad's lock code. + const { resetHostIdentityUnderLock } = + await import('../../main/orcad/orcad-host-identity-reset.js') + const result = resetHostIdentityUnderLock(userDataPath) + if (result.status === 'missing') { + throw new RuntimeClientError('not_found', `No Orca data at ${userDataPath}; nothing to reset.`) + } + if (result.status === 'running') { + throw new RuntimeClientError( + 'runtime_running', + `Stop Orca (the desktop app or \`orca serve\`) for ${userDataPath} first. ${result.message}` + ) + } + if (result.status === 'failed') { + throw new RuntimeClientError('reset_failed', `${result.message} Nothing was reset.`) + } + if (result.status === 'incomplete') { + throw new RuntimeClientError( + 'reset_incomplete', + `The reset did not finish (${result.message}). The next Orca start for ${userDataPath} finishes it before pairing is served; fix the cause first.` + ) + } +} + +export const SERVE_RESET_IDENTITY_HANDLERS: Record = { + 'serve reset-identity': async ({ flags, json }) => { + const userDataPath = resolveLaunchUserDataPath() + if (flags.get('yes') !== true) { + const confirm = pluginDevWatchConfirmFor(json) + if (!confirm) { + throw new RuntimeClientError( + 'confirmation_required', + `${RESET_WARNING} Run \`orca serve reset-identity --yes\` to continue.` + ) + } + if (!(await confirm(`${RESET_WARNING}\nReset ${userDataPath}? [y/N] `))) { + console.log('Cancelled; nothing changed.') + process.exitCode = 1 + return + } + } + await resetServeHostIdentity(userDataPath) + if (json) { + console.log(JSON.stringify({ ok: true, result: { reset: true, userDataPath } }, null, 2)) + return + } + console.log( + `Reset the pairing identity in ${userDataPath}. Start Orca again and pair clients with it.` + ) + } +} diff --git a/src/cli/specs/serve.ts b/src/cli/specs/serve.ts index 91018e91a17..3bc6b770259 100644 --- a/src/cli/specs/serve.ts +++ b/src/cli/specs/serve.ts @@ -40,6 +40,7 @@ export const SERVE_COMMAND_SPECS: CommandSpec[] = [ "--reclaim-stale-lock starts nothing. When a start refuses because this profile's lock may belong to an Orca on another machine sharing this home (for example after a reboot, on a network home), it checks that the lock was written on a host with this name in an earlier boot, was last written or refreshed at least 5 minutes ago, and that nothing answers on this machine at its holder's endpoints. Those checks cannot rule out another machine with the same host name, so it then only prints what it found (exit 2); add --yes, once you are sure no other machine sharing this home runs Orca for this profile, to clear it (exit 0). It exits 1 when a check fails and 4 when there is no lock. --data-root names the data root the refusal printed. --json prints the status (none, eligible, refused or reclaimed). Linux only.", 'On macOS, after Orca serve aborts at startup (it could not register with Launch Services), later runs from the same environment refuse for 30 minutes with exit code 78. Use --force to try anyway.', '--supervise is for service managers (launchd, systemd). Orca serve then restarts the server itself with growing delays instead of exiting when it stops, waits while an update replaces the app, and waits while another Orca serves this profile. On the orcad runtime (the default), a supervised server does not start without its terminal daemon, because terminals outside it would not survive a restart; that counts as a failure. After 6 failures in 15 minutes it gives up and exits with code 0 so the manager stops too, and later supervised runs refuse until a new build is installed or --force is passed. Not available on Windows yet.', + "Copies of a machine image share the server's pairing identity. Set ORCA_INSTANCE_ID to a value unique to each instance, or run `orca serve reset-identity` on each copy.", 'Experimental and off by default: set ORCA_TERMINAL_PROCESS_CEILING= (64 or more) before the terminal host starts to have the OS cap how many processes each terminal may run (Linux, through a systemd user scope per terminal, which needs a user manager that outlives logout; Windows, through a job per terminal). `orca serve` reports the ceiling as on or off, with the reason.', 'Off by default: set ORCA_TERMINAL_MEMORY_MAX= (256M or more) and ORCA_TERMINAL_MEMORY_SWAP_MAX= (0 for no swap) before the terminal host starts to cap the memory and swap of each terminal on Linux, through the same per-terminal systemd user scope. A terminal over its limit has its largest process OOM-killed; the host and other terminals keep running. Sizes take K, M, G or T. A limit the host cannot enforce is skipped and logged in the terminal host log. No effect on macOS or Windows.' ], @@ -64,6 +65,18 @@ export const SERVE_COMMAND_SPECS: CommandSpec[] = [ ], examples: ['orca serve restore resume', 'orca serve restore resume --json'] }, + { + path: ['serve', 'reset-identity'], + summary: "Give this machine's Orca server a new pairing identity", + usage: 'orca serve reset-identity [--yes] [--json]', + allowedFlags: [...LOCAL_COMMAND_FLAGS, 'yes'], + notes: [ + 'Replaces the pairing key and installation id and removes every paired device, so clients must pair again. Use it on each copy of a machine image that was made after Orca had run, so the copies stop sharing one identity.', + 'Asks for confirmation in a terminal; pass --yes to skip it. Stop Orca (the desktop app or `orca serve`) for this profile first; the command refuses while it runs.', + 'To have each copy rotate on its own instead, start `orca serve` with ORCA_INSTANCE_ID set to a value unique to that instance (for example the cloud-init instance id). When the value differs from the one recorded with the current identity, Orca rotates it at startup. The first value seen is only recorded, and an unset variable never rotates anything.' + ], + examples: ['orca serve reset-identity', 'orca serve reset-identity --yes'] + }, { path: ['serve', 'service', 'install'], summary: 'Run orca serve as a per-user background service that restarts itself', diff --git a/src/main/orcad/orcad-host-identity-reset.ts b/src/main/orcad/orcad-host-identity-reset.ts new file mode 100644 index 00000000000..0afe2c36631 --- /dev/null +++ b/src/main/orcad/orcad-host-identity-reset.ts @@ -0,0 +1,51 @@ +// CLI entry for `orca serve reset-identity`; plain Node, loaded from the packaged CLI. +import { existsSync } from 'node:fs' +import { acquireOrcadInstanceLock } from './orcad-instance-lock' +import { OrcadInstanceLockError } from './orcad-data-root-privacy' +import { + HostIdentityRotationNotStartedError, + rotateHostIdentity +} from '../runtime/host-instance-identity' + +export type HostIdentityResetResult = + | { status: 'reset' } + | { status: 'missing' } + | { status: 'running'; message: string } + /** Nothing changed on disk. */ + | { status: 'failed'; message: string } + /** The next Orca start finishes the rotation before it serves pairing. */ + | { status: 'incomplete'; message: string } + +const RUNNING_LOCK_CODES: ReadonlySet = new Set([ + 'orcad_instance_lock_held', + 'orcad_instance_lock_holder_unverifiable' +]) + +/** Holds the profile lock so no running Orca reads or rewrites the files mid-rotation. */ +export function resetHostIdentityUnderLock(userDataPath: string): HostIdentityResetResult { + if (!existsSync(userDataPath)) { + return { status: 'missing' } + } + let lock: ReturnType + try { + // The path may be a desktop profile, whose permissions a reset must not change. + lock = acquireOrcadInstanceLock(userDataPath, { enforcePrivateDataRoot: false }) + } catch (error) { + // Other lock errors (an unusable or shared data root) carry their own guidance. + if (error instanceof OrcadInstanceLockError && RUNNING_LOCK_CODES.has(error.code)) { + return { status: 'running', message: error.message } + } + throw error + } + try { + rotateHostIdentity(userDataPath) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + return error instanceof HostIdentityRotationNotStartedError + ? { status: 'failed', message } + : { status: 'incomplete', message } + } finally { + lock.release() + } + return { status: 'reset' } +} diff --git a/src/main/orcad/orcad-instance-lock.ts b/src/main/orcad/orcad-instance-lock.ts index bcc0eaf7168..0012cc218df 100644 --- a/src/main/orcad/orcad-instance-lock.ts +++ b/src/main/orcad/orcad-instance-lock.ts @@ -72,6 +72,8 @@ export type OrcadInstanceLockHooks = OrcadDataRootPrivacyHooks & { holderIdentity?: (record: OrcadLockRecord) => OrcadProcessIdentityVerdict /** Desktop callers must first hold Electron’s single-instance lock to reclaim desktop records. */ role?: 'orcad' | 'desktop' + /** Tighten and verify data-root permissions; defaults to on for the orcad role only. */ + enforcePrivateDataRoot?: boolean machineIdentity?: () => string | null /** Establishes locality for legacy machine-less records. */ dataRootIsOnLocalDisk?: (dataRoot: string) => boolean @@ -118,7 +120,7 @@ export function acquireOrcadInstanceLock( ) } // Desktop profiles retain existing permissions; orcad tightens its own roots. - if ((hooks.role ?? 'orcad') === 'orcad') { + if (hooks.enforcePrivateDataRoot ?? (hooks.role ?? 'orcad') === 'orcad') { assertOrcadDataRootIsPrivate(dataRoot, hooks) } diff --git a/src/main/runtime/host-instance-identity.test.ts b/src/main/runtime/host-instance-identity.test.ts new file mode 100644 index 00000000000..ffc5594c52e --- /dev/null +++ b/src/main/runtime/host-instance-identity.test.ts @@ -0,0 +1,299 @@ +import { randomUUID } from 'node:crypto' +import { cpSync, mkdtempSync, readdirSync, readFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { beforeEach, describe, expect, it } from 'vitest' +import { loadOrCreateE2EEKeypair, writeNewE2EEKeypair } from './rpc/server/pairing/e2ee-keypair' +import { DeviceRegistry } from './rpc/server/pairing/device-registry' +import { + HOST_INSTALLATION_FILENAME, + loadOrCreateHostInstallationId +} from './hosts/platform-probe/host-descriptor' +import { writeDurableSecureJsonFile } from '../../shared/secure-storage/secure-file' +import { + ensureHostInstanceIdentity, + hostInstanceIdFromEnv, + HOST_INSTANCE_FILENAME, + IDENTITY_BACKUPS_DIRNAME, + rotateHostIdentity, + type HostIdentityWrite +} from './host-instance-identity' +import { E2EE_KEYPAIR_FILENAME } from '../../shared/remote-runtime/host-store/runtime-e2ee-keypair-file' +import { DEVICE_REGISTRY_FILENAME } from './rpc/server/pairing/mobile-pairing-files' + +const vmA = hostInstanceIdFromEnv({ ORCA_INSTANCE_ID: 'vm-a' }) +const vmB = hostInstanceIdFromEnv({ ORCA_INSTANCE_ID: 'vm-b' }) + +/** What pairing serves after one startup. */ +async function identity(dir: string) { + return { + publicKeyB64: loadOrCreateE2EEKeypair(dir).publicKeyB64, + registry: new DeviceRegistry(dir), + installationId: await loadOrCreateHostInstallationId(dir) + } +} + +async function pairedHost(dir: string) { + const host = await identity(dir) + const { token } = host.registry.addDevice('Phone') + host.registry.flushPendingLastSeen() + return { ...host, token } +} + +function failingWriteOf(filename: string): HostIdentityWrite { + return (path, value) => { + if (path.endsWith(filename)) { + throw new Error(`simulated crash writing ${filename}`) + } + return writeDurableSecureJsonFile(path, value) + } +} + +describe('ensureHostInstanceIdentity', () => { + let dir: string + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'orca-host-instance-')) + }) + + it('never rotates while ORCA_INSTANCE_ID is unset, even with an id recorded', async () => { + const before = await pairedHost(dir) + expect(ensureHostInstanceIdentity(dir, null)).toEqual({ kind: 'unchanged' }) + ensureHostInstanceIdentity(dir, vmA) + expect(ensureHostInstanceIdentity(dir, null)).toEqual({ kind: 'unchanged' }) + + const after = await identity(dir) + expect(after.publicKeyB64).toBe(before.publicKeyB64) + expect(after.installationId).toBe(before.installationId) + expect(after.registry.validateToken(before.token)).not.toBeNull() + }) + + it('only records the first ORCA_INSTANCE_ID on an existing install', async () => { + const before = await pairedHost(dir) + expect(ensureHostInstanceIdentity(dir, vmA)).toEqual({ kind: 'recorded' }) + expect(ensureHostInstanceIdentity(dir, vmA)).toEqual({ kind: 'unchanged' }) + + const after = await identity(dir) + expect(after.publicKeyB64).toBe(before.publicKeyB64) + expect(after.registry.validateToken(before.token)).not.toBeNull() + expect(readFileSync(join(dir, HOST_INSTANCE_FILENAME), 'utf8')).not.toContain('vm-a') + }) + + it('rotates the identity and clears pairings when ORCA_INSTANCE_ID changes', async () => { + ensureHostInstanceIdentity(dir, vmA) + const source = await pairedHost(dir) + const clone = mkdtempSync(join(tmpdir(), 'orca-host-instance-clone-')) + cpSync(dir, clone, { recursive: true }) + + expect(ensureHostInstanceIdentity(clone, vmB)).toEqual({ + kind: 'rotated', + reason: 'instance_changed' + }) + const rotated = await identity(clone) + expect(rotated.publicKeyB64).not.toBe(source.publicKeyB64) + expect(rotated.installationId).not.toBe(source.installationId) + expect(rotated.registry.validateToken(source.token)).toBeNull() + expect(rotated.registry.listDevices()).toEqual([]) + + expect(ensureHostInstanceIdentity(clone, vmB)).toEqual({ kind: 'unchanged' }) + expect((await identity(clone)).publicKeyB64).toBe(rotated.publicKeyB64) + // The source keeps its own identity. + expect((await identity(dir)).registry.validateToken(source.token)).not.toBeNull() + }) + + it.each([ + ['the staged keypair', 'orca-e2ee-keypair.json.next'], + ['the staged registry', `${DEVICE_REGISTRY_FILENAME}.next`], + ['the rotating marker', HOST_INSTANCE_FILENAME] + ])('keeps serving the old identity when writing %s fails', async (_label, failing) => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + + expect(ensureHostInstanceIdentity(dir, vmB, failingWriteOf(failing))).toMatchObject({ + kind: 'kept' + }) + const after = await identity(dir) + expect(after.publicKeyB64).toBe(before.publicKeyB64) + expect(after.installationId).toBe(before.installationId) + expect(after.registry.validateToken(before.token)).not.toBeNull() + expect(readdirSync(dir).filter((name) => name.endsWith('.next'))).toEqual([]) + // The change is still pending, so a start that can write applies it. + expect(ensureHostInstanceIdentity(dir, vmB)).toEqual({ + kind: 'rotated', + reason: 'instance_changed' + }) + }) + + it('keeps serving the old identity when the first id cannot be recorded', async () => { + const before = await pairedHost(dir) + expect( + ensureHostInstanceIdentity(dir, vmA, failingWriteOf(HOST_INSTANCE_FILENAME)) + ).toMatchObject({ kind: 'kept' }) + expect((await identity(dir)).registry.validateToken(before.token)).not.toBeNull() + }) + + it('finishes a publish interrupted after the marker on the next start, without the variable', async () => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + + // Every staged file was moved into place; clearing `rotating` failed. + const failFinalMarker: HostIdentityWrite = (path, value) => { + if (path.endsWith(HOST_INSTANCE_FILENAME) && !JSON.stringify(value).includes('rotating')) { + throw new Error('simulated crash clearing the marker') + } + return writeDurableSecureJsonFile(path, value) + } + expect(() => ensureHostInstanceIdentity(dir, vmB, failFinalMarker)).toThrow(/simulated crash/) + + expect(ensureHostInstanceIdentity(dir, null)).toEqual({ + kind: 'rotated', + reason: 'interrupted_rotation' + }) + const after = await identity(dir) + expect(after.publicKeyB64).not.toBe(before.publicKeyB64) + expect(after.installationId).not.toBe(before.installationId) + expect(after.registry.validateToken(before.token)).toBeNull() + expect(ensureHostInstanceIdentity(dir, vmB)).toEqual({ kind: 'unchanged' }) + }) + + it('publishes the rest of a staged identity after a crash between renames', async () => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + // The keypair was already moved into place; the registry and installation id were not. + const newKey = writeNewE2EEKeypair(dir, writeDurableSecureJsonFile).publicKeyB64 + writeDurableSecureJsonFile(join(dir, `${DEVICE_REGISTRY_FILENAME}.next`), []) + const stagedInstallationId = randomUUID() + writeDurableSecureJsonFile(join(dir, `${HOST_INSTALLATION_FILENAME}.next`), { + installationId: stagedInstallationId + }) + writeDurableSecureJsonFile(join(dir, HOST_INSTANCE_FILENAME), { + v: 1, + instanceId: vmB, + rotating: true + }) + + expect(ensureHostInstanceIdentity(dir, vmB)).toEqual({ + kind: 'rotated', + reason: 'interrupted_rotation' + }) + const after = await identity(dir) + expect(after.publicKeyB64).toBe(newKey) + expect(after.installationId).toBe(stagedInstallationId) + expect(after.registry.validateToken(before.token)).toBeNull() + expect(ensureHostInstanceIdentity(dir, vmB)).toEqual({ kind: 'unchanged' }) + }) + + it('finishes the rotation when the rotating marker is published but its write still fails', async () => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + // For example the directory sync after the marker rename fails. + const failAfterMarkerRename: HostIdentityWrite = (path, value) => { + const result = writeDurableSecureJsonFile(path, value) + if (path.endsWith(HOST_INSTANCE_FILENAME) && JSON.stringify(value).includes('rotating')) { + throw new Error('simulated directory sync failure') + } + return result + } + + expect(ensureHostInstanceIdentity(dir, vmB, failAfterMarkerRename)).toEqual({ + kind: 'rotated', + reason: 'instance_changed' + }) + const after = await identity(dir) + expect(after.publicKeyB64).not.toBe(before.publicKeyB64) + expect(after.registry.validateToken(before.token)).toBeNull() + expect(ensureHostInstanceIdentity(dir, vmB)).toEqual({ kind: 'unchanged' }) + }) + + it('keeps the recorded id when a manual reset runs without ORCA_INSTANCE_ID', async () => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + rotateHostIdentity(dir, null) + const after = await identity(dir) + expect(after.publicKeyB64).not.toBe(before.publicKeyB64) + expect(ensureHostInstanceIdentity(dir, vmA)).toEqual({ kind: 'unchanged' }) + // A copy made after the reset and started with another id still rotates. + expect(ensureHostInstanceIdentity(dir, vmB)).toMatchObject({ kind: 'rotated' }) + }) + + it('finishes an interrupted rotation before a manual reset stages a new one', async () => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + const failFinalMarker: HostIdentityWrite = (path, value) => { + if (path.endsWith(HOST_INSTANCE_FILENAME) && !JSON.stringify(value).includes('rotating')) { + throw new Error('simulated crash clearing the marker') + } + return writeDurableSecureJsonFile(path, value) + } + expect(() => ensureHostInstanceIdentity(dir, vmB, failFinalMarker)).toThrow(/simulated crash/) + + rotateHostIdentity(dir, null) + const after = await identity(dir) + expect(after.registry.validateToken(before.token)).toBeNull() + expect(readdirSync(dir).filter((name) => name.endsWith('.next'))).toEqual([]) + expect(ensureHostInstanceIdentity(dir, vmB)).toEqual({ kind: 'unchanged' }) + }) + + it('moves the replaced identity into a backup folder and keeps the newest five', async () => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + rotateHostIdentity(dir, vmA, writeDurableSecureJsonFile, () => new Date('2026-01-01T00:00:00Z')) + + const backups = join(dir, IDENTITY_BACKUPS_DIRNAME) + const [first] = readdirSync(backups) + expect(first).toMatch(/^2026-01-01T00-00-00-000Z-[0-9a-f]{6}$/) + const saved = JSON.parse(readFileSync(join(backups, first, E2EE_KEYPAIR_FILENAME), 'utf8')) + expect(saved.publicKeyB64).toBe(before.publicKeyB64) + expect(readdirSync(join(backups, first)).sort()).toEqual( + [E2EE_KEYPAIR_FILENAME, DEVICE_REGISTRY_FILENAME, HOST_INSTALLATION_FILENAME].sort() + ) + + for (let day = 2; day <= 7; day++) { + rotateHostIdentity( + dir, + vmA, + writeDurableSecureJsonFile, + () => new Date(`2026-01-0${day}T00:00:00Z`) + ) + } + const kept = readdirSync(backups).sort() + expect(kept).toHaveLength(5) + expect(kept[0]).toMatch(/^2026-01-03/) + }) + + it('reports a reset as done when it finished an interrupted rotation but cannot stage another', async () => { + ensureHostInstanceIdentity(dir, vmA) + const before = await pairedHost(dir) + const failFinalMarker: HostIdentityWrite = (path, value) => { + if (path.endsWith(HOST_INSTANCE_FILENAME) && !JSON.stringify(value).includes('rotating')) { + throw new Error('simulated crash clearing the marker') + } + return writeDurableSecureJsonFile(path, value) + } + expect(() => ensureHostInstanceIdentity(dir, vmB, failFinalMarker)).toThrow(/simulated crash/) + + expect(() => + rotateHostIdentity(dir, null, failingWriteOf('orca-e2ee-keypair.json.next')) + ).not.toThrow() + expect((await identity(dir)).registry.validateToken(before.token)).toBeNull() + expect(readdirSync(dir).filter((name) => name.endsWith('.next'))).toEqual([]) + expect(ensureHostInstanceIdentity(dir, vmB)).toEqual({ kind: 'unchanged' }) + }) + + it('never prunes the backup it just made', async () => { + ensureHostInstanceIdentity(dir, vmA) + await pairedHost(dir) + for (let day = 5; day <= 9; day++) { + rotateHostIdentity( + dir, + vmA, + writeDurableSecureJsonFile, + () => new Date(`2027-01-0${day}T00:00:00Z`) + ) + } + // The clock went back (or the image carried later backups). + rotateHostIdentity(dir, vmA, writeDurableSecureJsonFile, () => new Date('2026-01-01T00:00:00Z')) + const kept = readdirSync(join(dir, IDENTITY_BACKUPS_DIRNAME)).sort() + expect(kept).toHaveLength(5) + expect(kept[0]).toMatch(/^2026-01-01/) + }) +}) diff --git a/src/main/runtime/host-instance-identity.ts b/src/main/runtime/host-instance-identity.ts new file mode 100644 index 00000000000..f327cf246b6 --- /dev/null +++ b/src/main/runtime/host-instance-identity.ts @@ -0,0 +1,288 @@ +// Copies of one machine image share the pairing identity files; ORCA_INSTANCE_ID tells the copies apart. +import { createHash, randomBytes, randomUUID } from 'node:crypto' +import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync } from 'node:fs' +import { basename, join } from 'node:path' +import { z } from 'zod' +import { + bestEffortFsyncDirectorySync, + writeDurableSecureJsonFile +} from '../../shared/secure-storage/secure-file' +import { E2EE_KEYPAIR_FILENAME } from '../../shared/remote-runtime/host-store/runtime-e2ee-keypair-file' +import { writeNewE2EEKeypair } from './rpc/server/pairing/e2ee-keypair' +import { DEVICE_REGISTRY_FILENAME } from './rpc/server/pairing/mobile-pairing-files' +import { HOST_INSTALLATION_FILENAME } from './hosts/platform-probe/host-descriptor' + +export const HOST_INSTANCE_FILENAME = 'orca-host-instance.json' +export const ORCA_INSTANCE_ID_ENV = 'ORCA_INSTANCE_ID' +const MAX_HOST_INSTANCE_FILE_BYTES = 4 * 1024 +const IDENTITY_FILENAMES = [ + E2EE_KEYPAIR_FILENAME, + DEVICE_REGISTRY_FILENAME, + HOST_INSTALLATION_FILENAME +] as const +const STAGED_SUFFIX = '.next' +/** Replaced identity files are moved here so an unwanted rotation can be undone by hand. */ +export const IDENTITY_BACKUPS_DIRNAME = 'orca-identity-backups' +const MAX_IDENTITY_BACKUPS = 5 +const BACKUP_NAME_RE = /^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z-[0-9a-f]{6}$/ + +/** `instanceId` is hashed so the file never republishes a provisioner value. `rotating` is written + * only once every new file is staged beside the live one; a start that sees it publishes them. */ +const HostInstanceMarkerSchema = z.object({ + v: z.literal(1), + instanceId: z.string().min(1).optional(), + rotating: z.literal(true).optional(), + /** Backup folder for the files a `rotating` publish replaces; stable across an interrupted publish. */ + backup: z.string().optional() +}) +type HostInstanceMarker = z.infer + +export type HostIdentityOutcome = + | { kind: 'unchanged' } + | { kind: 'recorded' } + | { kind: 'rotated'; reason: 'instance_changed' | 'interrupted_rotation' } + /** Nothing was changed on disk, so the existing identity is still whole and safe to serve. */ + | { kind: 'kept'; error: unknown } + +export type HostIdentityWrite = (path: string, value: unknown) => boolean + +/** The rotation failed before any live identity file was touched. */ +export class HostIdentityRotationNotStartedError extends Error { + constructor(cause: unknown) { + super( + `Could not write the new pairing identity; the current one is unchanged: ${cause instanceof Error ? cause.message : String(cause)}`, + { cause } + ) + this.name = 'HostIdentityRotationNotStartedError' + } +} + +export function hostInstanceIdFromEnv(env: NodeJS.ProcessEnv = process.env): string | null { + const raw = env[ORCA_INSTANCE_ID_ENV]?.trim() + return raw ? createHash('sha256').update(`orca-host-instance\0${raw}`).digest('hex') : null +} + +function isMissing(error: unknown): boolean { + return error instanceof Error && 'code' in error && error.code === 'ENOENT' +} + +function readMarker(markerPath: string): HostInstanceMarker | null { + let raw: string + try { + raw = readFileSync(markerPath, 'utf8') + } catch (error) { + if (isMissing(error)) { + return null + } + throw error + } + if (raw.length > MAX_HOST_INSTANCE_FILE_BYTES) { + return null + } + try { + const parsed = HostInstanceMarkerSchema.safeParse(JSON.parse(raw)) + return parsed.success ? parsed.data : null + } catch { + // Writes are atomic, so a bad file was edited by hand; re-record it rather than unpair. + return null + } +} + +function markerFor(instanceId: string | undefined): HostInstanceMarker { + return { v: 1, ...(instanceId ? { instanceId } : {}) } +} + +function newBackupName(now: Date): string { + return `${now.toISOString().replace(/[:.]/g, '-')}-${randomBytes(3).toString('hex')}` +} + +function stagedIdentityPaths(userDataPath: string): { live: string; staged: string }[] { + return IDENTITY_FILENAMES.map((name) => { + const live = join(userDataPath, name) + return { live, staged: `${live}${STAGED_SUFFIX}` } + }) +} + +/** Writes the new identity beside the live files, which stay untouched. */ +function stageNewIdentity(userDataPath: string, write: HostIdentityWrite): void { + const stage: HostIdentityWrite = (path, value) => write(`${path}${STAGED_SUFFIX}`, value) + writeNewE2EEKeypair(userDataPath, stage) + stage(join(userDataPath, DEVICE_REGISTRY_FILENAME), []) + stage(join(userDataPath, HOST_INSTALLATION_FILENAME), { installationId: randomUUID() }) +} + +function discardStagedIdentity(userDataPath: string): void { + for (const { staged } of stagedIdentityPaths(userDataPath)) { + try { + rmSync(staged, { force: true }) + } catch { + // A leftover staged file is ignored until a later rotation overwrites it. + } + } +} + +/** Best effort: a failed backup must not block the rotation, so the live file is then overwritten. */ +function backUpLiveFile(live: string, backupDir: string | null): void { + if (!backupDir) { + return + } + try { + mkdirSync(backupDir, { recursive: true, mode: 0o700 }) + const saved = join(backupDir, basename(live)) + // A resumed publish keeps the first backup; a live file recreated meanwhile is not the original. + if (!existsSync(saved)) { + renameSync(live, saved) + } + } catch { + // ENOENT: an earlier, interrupted publish already moved it (or it never existed). + } +} + +function pruneIdentityBackups(userDataPath: string, current: string | undefined): void { + const root = join(userDataPath, IDENTITY_BACKUPS_DIRNAME) + try { + // Never the one just made, even when copied-in backups carry later timestamps. + const names = readdirSync(root) + .filter((name) => BACKUP_NAME_RE.test(name) && name !== current) + .sort() + for (const name of names.slice(0, Math.max(0, names.length - (MAX_IDENTITY_BACKUPS - 1)))) { + rmSync(join(root, name), { recursive: true, force: true }) + } + } catch { + // Old backups are only disk space; never fail a rotation over them. + } +} + +/** Moves each staged file over its live file, after moving the live file into the backup folder. + * Staging finished before the `rotating` marker was written, so a missing staged file was already + * moved by an earlier, interrupted publish. */ +function publishStagedIdentity( + userDataPath: string, + marker: { instanceId?: string; backup?: string }, + write: HostIdentityWrite +): void { + const backupDir = + marker.backup && BACKUP_NAME_RE.test(marker.backup) + ? join(userDataPath, IDENTITY_BACKUPS_DIRNAME, marker.backup) + : null + for (const { live, staged } of stagedIdentityPaths(userDataPath)) { + if (!existsSync(staged)) { + continue + } + backUpLiveFile(live, backupDir) + try { + renameSync(staged, live) + } catch (error) { + if (!isMissing(error)) { + throw error + } + } + } + bestEffortFsyncDirectorySync(userDataPath) + write(join(userDataPath, HOST_INSTANCE_FILENAME), markerFor(marker.instanceId)) + pruneIdentityBackups(userDataPath, marker.backup) +} + +/** + * New keypair, no paired devices, new installation id. Callers must hold the profile lock. + * Without `instanceId` the recorded id is kept, so a manual reset never forgets it. + * Throws HostIdentityRotationNotStartedError when the old identity is still whole; any other + * error means the next start must finish the rotation before serving. + */ +export function rotateHostIdentity( + userDataPath: string, + instanceId: string | null = hostInstanceIdFromEnv(), + write: HostIdentityWrite = writeDurableSecureJsonFile, + now: () => Date = () => new Date() +): void { + const markerPath = join(userDataPath, HOST_INSTANCE_FILENAME) + let stored: HostInstanceMarker | null + try { + stored = readMarker(markerPath) + } catch (error) { + throw new HostIdentityRotationNotStartedError(error) + } + const finishedEarlierRotation = !!stored?.rotating + if (stored?.rotating) { + // Staging below reuses the `.next` paths, so an earlier rotation must be finished first. + publishStagedIdentity(userDataPath, stored, write) + } + // After finishing an earlier rotation the identity is already new and was never served, which + // is what a reset asks for; reporting "nothing changed" would be false. + const notStarted = (error: unknown): void => { + discardStagedIdentity(userDataPath) + if (!finishedEarlierRotation) { + throw new HostIdentityRotationNotStartedError(error) + } + } + const marker: HostInstanceMarker = { + ...markerFor(instanceId ?? stored?.instanceId), + rotating: true, + backup: newBackupName(now()) + } + try { + stageNewIdentity(userDataPath, write) + } catch (error) { + notStarted(error) + return + } + try { + write(markerPath, marker) + } catch (error) { + // The write can fail after its rename (directory sync); a published `rotating` marker + // needs the staged files, so only discard them once the marker is known not to say so. + let onDisk: HostInstanceMarker | null + try { + onDisk = readMarker(markerPath) + } catch { + throw error + } + if (!onDisk?.rotating) { + notStarted(error) + return + } + } + publishStagedIdentity(userDataPath, marker, write) +} + +/** + * Rotates only when ORCA_INSTANCE_ID is set and differs from the id recorded with this identity. + * Unset never rotates; the first id seen is recorded so adopting the variable keeps pairings. + * Throws only when a started rotation cannot be finished, so old and new files may be mixed. + */ +export function ensureHostInstanceIdentity( + userDataPath: string, + instanceId: string | null = hostInstanceIdFromEnv(), + write: HostIdentityWrite = writeDurableSecureJsonFile +): HostIdentityOutcome { + const markerPath = join(userDataPath, HOST_INSTANCE_FILENAME) + const stored = readMarker(markerPath) + if (stored?.rotating) { + publishStagedIdentity( + userDataPath, + { instanceId: instanceId ?? stored.instanceId, backup: stored.backup }, + write + ) + return { kind: 'rotated', reason: 'interrupted_rotation' } + } + if (!instanceId || stored?.instanceId === instanceId) { + return { kind: 'unchanged' } + } + if (!stored?.instanceId) { + try { + write(markerPath, markerFor(instanceId)) + } catch (error) { + return { kind: 'kept', error } + } + return { kind: 'recorded' } + } + try { + rotateHostIdentity(userDataPath, instanceId, write) + } catch (error) { + if (error instanceof HostIdentityRotationNotStartedError) { + return { kind: 'kept', error } + } + throw error + } + return { kind: 'rotated', reason: 'instance_changed' } +} diff --git a/src/main/runtime/rpc/server/pairing/e2ee-keypair.ts b/src/main/runtime/rpc/server/pairing/e2ee-keypair.ts index 2a9e7fcf539..ea948afe817 100644 --- a/src/main/runtime/rpc/server/pairing/e2ee-keypair.ts +++ b/src/main/runtime/rpc/server/pairing/e2ee-keypair.ts @@ -67,12 +67,20 @@ export function loadOrCreateE2EEKeypair(userDataPath: string): E2EEKeypair { } } + return writeNewE2EEKeypair(userDataPath) +} + +/** Atomically replaces any stored keypair with a fresh one. */ +export function writeNewE2EEKeypair( + userDataPath: string, + write: (path: string, value: unknown) => boolean = writeSecureJsonFile +): E2EEKeypair { const keypair = nacl.box.keyPair() const publicKeyB64 = Buffer.from(keypair.publicKey).toString('base64') const secretKeyB64 = Buffer.from(keypair.secretKey).toString('base64') const data: KeypairFile = { v: KEYPAIR_VERSION, publicKeyB64, secretKeyB64 } - writeSecureJsonFile(filePath, data) + write(join(userDataPath, KEYPAIR_FILENAME), data) return { publicKey: keypair.publicKey, secretKey: keypair.secretKey, publicKeyB64 } } diff --git a/src/main/runtime/rpc/server/runtime-rpc-binary-routing.ts b/src/main/runtime/rpc/server/runtime-rpc-binary-routing.ts index 7175c095868..d85122753a5 100644 --- a/src/main/runtime/rpc/server/runtime-rpc-binary-routing.ts +++ b/src/main/runtime/rpc/server/runtime-rpc-binary-routing.ts @@ -2,6 +2,7 @@ import type { WebSocket } from 'ws' import type { TerminalStreamFrame } from '../../../../shared/terminal/stream/terminal-stream-protocol' import { DeviceRegistry } from './pairing/device-registry' import { loadOrCreateE2EEKeypair, type E2EEKeypair } from './pairing/e2ee-keypair' +import { ensureHostInstanceIdentity, type HostIdentityOutcome } from '../../host-instance-identity' import { RuntimeRpcState } from './runtime-rpc-state' import { DEVICE_REGISTRY_UNAVAILABLE_GUIDANCE, @@ -86,6 +87,16 @@ export class RuntimeRpcBinaryRouting extends RuntimeRpcState { } protected initializePairingIdentity(): PairingIdentityInitialization { + try { + logHostIdentityOutcome(ensureHostInstanceIdentity(this.userDataPath)) + } catch (error) { + // A rotation that started but could not finish must not be served; the next start finishes it. + console.error('[runtime] Failed to finish rotating the pairing identity:', error) + return { + ok: false, + failure: pairingUnavailable('e2ee_key_unavailable', E2EE_KEY_UNAVAILABLE_GUIDANCE) + } + } let deviceRegistry: DeviceRegistry try { deviceRegistry = new DeviceRegistry(this.userDataPath) @@ -112,3 +123,21 @@ export class RuntimeRpcBinaryRouting extends RuntimeRpcState { return { ok: true, deviceRegistry, e2eeKeypair } } } + +function logHostIdentityOutcome(outcome: HostIdentityOutcome): void { + if (outcome.kind === 'kept') { + console.error( + '[runtime] Could not apply ORCA_INSTANCE_ID; serving the existing pairing identity unchanged. Orca retries on the next start.', + outcome.error + ) + } else if (outcome.kind === 'rotated' && outcome.reason === 'instance_changed') { + // Loud on purpose: a value that changes on every start would un-pair clients each time. + console.error( + '[runtime] ORCA_INSTANCE_ID differs from the value recorded with this pairing identity, so Orca created a new identity and removed all paired devices. Clients must pair with this host again. If this host was not copied, set ORCA_INSTANCE_ID to a value that stays the same across restarts.' + ) + } else if (outcome.kind === 'rotated') { + console.error( + '[runtime] Finished an interrupted pairing identity rotation. Clients must pair with this host again.' + ) + } +} diff --git a/src/main/runtime/rpc/server/runtime-rpc-pairing-offer.test.ts b/src/main/runtime/rpc/server/runtime-rpc-pairing-offer.test.ts index 03365ca82c6..97cb6df3fd1 100644 --- a/src/main/runtime/rpc/server/runtime-rpc-pairing-offer.test.ts +++ b/src/main/runtime/rpc/server/runtime-rpc-pairing-offer.test.ts @@ -1,13 +1,13 @@ import type * as WorktreeScanCacheModule from '../../../git/worktrees/catalog/worktree-scan-cache' import type * as WorktreeListingModule from '../../../git/worktrees/catalog/worktree-listing' import '../unused-default-rpc-methods.test-fixture' -import { mkdirSync, mkdtempSync } from 'node:fs' +import { cpSync, mkdirSync, mkdtempSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { describe, expect, it, vi } from 'vitest' import { OrcaRuntimeService } from '../../orca-runtime' import { OrcaRuntimeRpcServer } from './runtime-rpc' -import '../../../../shared/remote-runtime/pairing/pairing' +import { parsePairingCode } from '../../../../shared/remote-runtime/pairing/pairing' import { DEVICE_REGISTRY_FILENAME } from './pairing/mobile-pairing-files' import '../../../../shared/remote-runtime/host-store/runtime-e2ee-keypair-file' import '../methods/host-management/status' @@ -49,6 +49,46 @@ const { getWorktreeListingMocks } = vi.hoisted(() => { }) describe('OrcaRuntimeRpcServer', () => { + it('gives a copy started with another ORCA_INSTANCE_ID its own key and drops copied tokens', async () => { + const pairingFrom = async (userDataPath: string, instanceId: string) => { + vi.stubEnv('ORCA_INSTANCE_ID', instanceId) + const server = new OrcaRuntimeRpcServer({ + methods: [], + runtime: new OrcaRuntimeService(), + userDataPath, + enableWebSocket: true, + wsPort: 0 + }) + await server.start() + const offer = server.createPairingOffer({ name: 'Clone test' }) + return { server, parsed: offer.available ? parsePairingCode(offer.pairingUrl) : null } + } + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + const source = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-')) + const clone = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-')) + try { + const original = await pairingFrom(source, 'vm-a') + await original.server.stop() + const sourceToken = original.parsed?.deviceToken ?? '' + expect(sourceToken).toBeTruthy() + cpSync(source, clone, { recursive: true }) + + const cloned = await pairingFrom(clone, 'vm-b') + expect(cloned.parsed?.publicKeyB64).toBeTruthy() + expect(cloned.parsed?.publicKeyB64).not.toBe(original.parsed?.publicKeyB64) + expect(cloned.server.getDeviceRegistry()?.validateToken(sourceToken)).toBeNull() + await cloned.server.stop() + + const restarted = await pairingFrom(source, 'vm-a') + expect(restarted.parsed?.publicKeyB64).toBe(original.parsed?.publicKeyB64) + expect(restarted.server.getDeviceRegistry()?.validateToken(sourceToken)).not.toBeNull() + await restarted.server.stop() + } finally { + vi.unstubAllEnvs() + errorSpy.mockRestore() + } + }) + it('reports a registry persistence failure without retaining a ghost credential', async () => { const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-')) const server = new OrcaRuntimeRpcServer({ From d101312beeac1788ca081891a12fa7bb024d6377 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:37:17 -0800 Subject: [PATCH 04/80] refactor(sqlite): share one row cache across the SQLite-backed caches (#40028) The last-status, GitHub cache, notification dismissal and model catalog stores each grew their own legacy-file import, read-only startup read, changed-row writer and retry backoff. They now share src/main/sqlite/row-cache/ (KeyedJsonRowCache with load/write, the legacy JSON source, one failure classifier and backoff); each store keeps only its cadence and fencing policy. Edge cases now behave the same in every store: - a corrupt or rejected older-build file keeps the stored rows (it no longer wipes them); - an older-build file that exists but cannot be read is never treated as absent; - the import receipt is the file's content hash, without its mtime, so a copied leftover is still recognised as imported; - while the rows or that file cannot be read, the store saves nothing that session and never deletes a file it did not load (dismissals keep retrying, as before); - files are user-only and hardened everywhere. The last-status and GitHub caches move to the shared row layout, so each rebuilds empty once. --- config/main-sync-fs-baseline.txt | 8 +- .../server/last-status-legacy-file.ts | 106 ------ .../server/last-status-saved-rows.ts | 139 ++----- .../agent-hooks/server/last-status-store.ts | 218 +++-------- .../agent-hooks/server/server-persistence.ts | 9 +- .../persistence/last-status.test-fixtures.ts | 37 +- .../agent-model-catalog-persistence.ts | 69 ++-- .../github-cache-database.test.ts | 64 +--- .../loading-store/github-cache-database.ts | 227 +++++------- .../loading-store/github-cache-persistence.ts | 19 +- .../loading-store/github-cache-sidecar.ts | 56 --- .../github-cache-startup-read.ts | 100 ----- .../loading-store/loaded-state-parsing.ts | 7 +- .../loading-store/profile-preferences.ts | 1 + .../loading-store/store-runtime-state.ts | 2 + .../loading-store/write-flush-barriers.ts | 1 + .../mobile-notification-dismissal-entries.ts | 2 +- ...otification-dismissal-read-failure.test.ts | 4 +- ...obile-notification-dismissal-store.test.ts | 31 +- .../mobile-notification-dismissal-store.ts | 73 ++-- src/main/sqlite/keyed-json-row-cache.test.ts | 36 -- src/main/sqlite/keyed-json-row-cache.ts | 299 --------------- src/main/sqlite/legacy-json-source.ts | 46 --- .../row-cache/keyed-json-row-cache.test.ts | 167 +++++++++ .../sqlite/row-cache/keyed-json-row-cache.ts | 349 ++++++++++++++++++ .../sqlite/row-cache/legacy-json-source.ts | 45 +++ .../sqlite/row-cache/row-cache-failure.ts | 19 + 27 files changed, 864 insertions(+), 1270 deletions(-) delete mode 100644 src/main/agent-hooks/server/last-status-legacy-file.ts delete mode 100644 src/main/persistence/loading-store/github-cache-sidecar.ts delete mode 100644 src/main/persistence/loading-store/github-cache-startup-read.ts delete mode 100644 src/main/sqlite/keyed-json-row-cache.test.ts delete mode 100644 src/main/sqlite/keyed-json-row-cache.ts delete mode 100644 src/main/sqlite/legacy-json-source.ts create mode 100644 src/main/sqlite/row-cache/keyed-json-row-cache.test.ts create mode 100644 src/main/sqlite/row-cache/keyed-json-row-cache.ts create mode 100644 src/main/sqlite/row-cache/legacy-json-source.ts create mode 100644 src/main/sqlite/row-cache/row-cache-failure.ts diff --git a/config/main-sync-fs-baseline.txt b/config/main-sync-fs-baseline.txt index edeab9c15cd..ff70d4af49b 100644 --- a/config/main-sync-fs-baseline.txt +++ b/config/main-sync-fs-baseline.txt @@ -8,8 +8,6 @@ 11 src/main/agent-hooks/installation/configuration/hooks-json-write.ts 6 src/main/agent-hooks/installation/scripts/locked-executable-rename.ts 9 src/main/agent-hooks/installation/scripts/managed-hook-script-write.ts -2 src/main/agent-hooks/server/last-status-legacy-file.ts -1 src/main/agent-hooks/server/last-status-saved-rows.ts 1 src/main/agent-hooks/wsl-relay/wsl-hook-relay-deps.ts 3 src/main/agent-hooks/wsl-relay/wsl-hook-relay-launch.ts 1 src/main/agent-launch/trust/agent-trust-ownership-ledger.ts @@ -374,8 +372,6 @@ 2 src/main/orcad/orcad-wsl-host-claude-profile-refresh.ts 1 src/main/orcad/serve-profile-ssh-targets.ts 6 src/main/persistence/atomic-snapshot-writer.ts -1 src/main/persistence/loading-store/github-cache-sidecar.ts -1 src/main/persistence/loading-store/github-cache-startup-read.ts 5 src/main/persistence/loading-store/user-data-path.ts 9 src/main/persistence/migrating-orcad-catalog/migration-journal-directory.ts 4 src/main/persistence/migrating-orcad-catalog/orcad-migration-blob-installers.ts @@ -503,8 +499,8 @@ 1 src/main/speech/stt-worker-model-config.ts 2 src/main/speech/stt-worker-paths.ts 2 src/main/sqlite/harden-database-files.ts -1 src/main/sqlite/keyed-json-row-cache.ts -5 src/main/sqlite/legacy-json-source.ts +1 src/main/sqlite/row-cache/keyed-json-row-cache.ts +2 src/main/sqlite/row-cache/legacy-json-source.ts 1 src/main/sqlite/sync-database.ts 2 src/main/sqlite/versioned-cache-database.ts 1 src/main/ssh/authentication/ssh-auth-resolution.ts diff --git a/src/main/agent-hooks/server/last-status-legacy-file.ts b/src/main/agent-hooks/server/last-status-legacy-file.ts deleted file mode 100644 index 4917a8704e7..00000000000 --- a/src/main/agent-hooks/server/last-status-legacy-file.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { createHash } from 'node:crypto' -import { readFileSync, rmSync } from 'node:fs' - -/** - * The JSON file older builds read and write. This build only imports it: the file is removed - * once rows holding its admitted contents have committed, and only if it still has the bytes - * that were imported, so a file another build rewrote meanwhile survives for the next start. - */ -export class LegacyLastStatusFile { - private readHash: string | undefined - private removableHash: string | undefined - /** Whether the removable bytes were imported (recorded in history) or merely rejected. */ - private removableImported = false - - constructor(readonly path: string) {} - - /** Parsed contents and their hash, or undefined when absent or unreadable. */ - read(): { document: unknown; hash: string } | undefined { - const raw = this.readRaw() - if (raw === undefined) { - return undefined - } - const hash = hashContent(raw) - try { - const document: unknown = JSON.parse(raw) - this.readHash = hash - return { document, hash } - } catch { - console.warn('[agent-hooks] last-status file is not valid JSON; ignoring') - // Unreadable either way; dropping it keeps it from shadowing the rows on every start. - this.removableHash = hash - this.removableImported = false - return undefined - } - } - - /** Hydration admitted the contents read last; they may go once rows hold them. */ - confirmRead(): void { - if (this.readHash !== undefined) { - this.markImported(this.readHash) - } - } - - /** Hydration rejected the contents read last: remove them, without recording an import. */ - discardRead(): void { - if (this.readHash !== undefined) { - this.removableHash = this.readHash - this.removableImported = false - } - } - - /** Contents imported now, or earlier when the file could not be removed. */ - markImported(hash: string): void { - this.removableHash = hash - this.removableImported = true - } - - /** Hash to record as imported with the rows that hold it. */ - get pendingImportHash(): string | undefined { - return this.removableImported ? this.removableHash : undefined - } - - /** Call only after the rows holding the imported contents have committed. */ - removeIfUnchanged(): void { - const expected = this.removableHash - if (expected === undefined) { - return - } - const raw = this.readRaw() - if (raw !== undefined && hashContent(raw) !== expected) { - // Rewritten by another build since import: the next start imports it instead. - this.removableHash = undefined - return - } - try { - if (raw !== undefined) { - rmSync(this.path, { force: true }) - } - this.removableHash = undefined - } catch (err) { - console.warn('[agent-hooks] failed to remove migrated last-status file:', err) - } - } - - reset(): void { - this.readHash = undefined - this.removableHash = undefined - this.removableImported = false - } - - private readRaw(): string | undefined { - try { - return readFileSync(this.path, 'utf8') - } catch (err) { - // Why: missing file is normal (migrated or first launch); other errors degrade to the rows. - if ((err as NodeJS.ErrnoException).code !== 'ENOENT') { - console.warn('[agent-hooks] failed to read last-status file:', err) - } - return undefined - } - } -} - -export function hashContent(content: string): string { - return createHash('sha256').update(content).digest('base64') -} diff --git a/src/main/agent-hooks/server/last-status-saved-rows.ts b/src/main/agent-hooks/server/last-status-saved-rows.ts index 86a1fc6e721..9ce192497ab 100644 --- a/src/main/agent-hooks/server/last-status-saved-rows.ts +++ b/src/main/agent-hooks/server/last-status-saved-rows.ts @@ -1,139 +1,50 @@ -import { existsSync } from 'node:fs' - -import SyncDatabase from '../../sqlite/sync-database' -import { isSqliteCorruption } from '../../sqlite/sqlite-read-failure' -import { removeVersionedCacheDatabase } from '../../sqlite/versioned-cache-database' +import type { KeyedJsonRow } from '../../sqlite/row-cache/keyed-json-row-cache' +import { parseKeyedJsonRows } from '../../sqlite/row-cache/keyed-json-row-cache' import { LAST_STATUS_FILE_VERSION } from './server-constants' import type { LastStatusFile } from './server-types' -import { hashContent } from './last-status-legacy-file' -export type RowKind = 'entry' | 'commitment' | 'exits' -export type StoredRow = { kind: RowKind; key: string; payload: string } +type RowKind = 'entry' | 'commitment' | 'exits' -export const IMPORTED_LEGACY_HASH = 'imported_legacy_hash' -// Follows LAST_STATUS_FILE_VERSION: a mismatched cache rebuilds empty, as the file did. -export const LAST_STATUS_SCHEMA_VERSION = LAST_STATUS_FILE_VERSION +// Follows LAST_STATUS_FILE_VERSION (a mismatched cache rebuilds empty, as the file did); the +// low digits version the row layout. +export const LAST_STATUS_SCHEMA_VERSION = LAST_STATUS_FILE_VERSION * 100 + 2 -export const rowId = (kind: RowKind, key: string): string => `${kind}\u0000${key}` +const rowKey = (kind: RowKind, key: string): string => `${kind}\u0000${key}` -export type SavedRows = - | { kind: 'rows'; rows: StoredRow[]; importedLegacyHash: string | undefined } - /** Nothing usable is saved: no file, another schema, or a damaged file (now removed). */ - | { kind: 'empty' } - /** Saved rows exist but cannot be read now (usually another connection's lock). */ - | { kind: 'unavailable'; error: unknown } - -/** - * Read the saved rows without writing: a read-only connection needs no write lock, so a - * writer elsewhere cannot block startup the way schema initialization would. - */ -export function readSavedRows(databasePath: string, busyTimeoutMs: number): SavedRows { - if (!existsSync(databasePath)) { - return { kind: 'empty' } - } - let db: SyncDatabase | null = null - try { - db = new SyncDatabase(databasePath, { readonly: true, fileMustExist: true }) - db.pragma(`busy_timeout = ${busyTimeoutMs}`) - db.exec('BEGIN') - try { - const hasMeta = db - .prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('meta', 'rows')") - .all() - if (hasMeta.length !== 2) { - return { kind: 'empty' } - } - const version = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get() - if (version?.value !== String(LAST_STATUS_SCHEMA_VERSION)) { - return { kind: 'empty' } - } - const marker = db.prepare('SELECT value FROM meta WHERE key = ?').get(IMPORTED_LEGACY_HASH) - return { - kind: 'rows', - rows: readRows(db), - importedLegacyHash: typeof marker?.value === 'string' ? marker.value : undefined - } - } finally { - if (db.isTransaction) { - db.exec('COMMIT') - } - } - } catch (error) { - if (isSqliteCorruption(error)) { - db?.close() - db = null - // Damaged rows hold nothing recoverable; the next write rebuilds the cache. - try { - removeVersionedCacheDatabase(databasePath, 'fail-fast') - } catch (removeError) { - // A held file stays until a later start; this session reads nothing from it. - return { kind: 'unavailable', error: removeError } - } - return { kind: 'empty' } - } - return { kind: 'unavailable', error } - } finally { - db?.close() - } -} - -export function readRows(db: SyncDatabase): StoredRow[] { - return db - .prepare('SELECT kind, key, payload FROM rows') - .all() - .flatMap((row) => - isRowKind(row.kind) && typeof row.key === 'string' && typeof row.payload === 'string' - ? [{ kind: row.kind, key: row.key, payload: row.payload }] - : [] - ) -} - -export function readDataVersion(db: SyncDatabase): unknown { - return db.prepare('PRAGMA data_version').get()?.data_version -} - -export function hashRows(rows: readonly StoredRow[]): Map { - return new Map(rows.map((row) => [rowId(row.kind, row.key), hashContent(row.payload)])) -} - -export function* rowsOf(file: LastStatusFile): Generator { +/** One row per pane entry and per authority commitment, plus the exit list. */ +export function rowsOf(file: LastStatusFile): KeyedJsonRow[] { + const rows: KeyedJsonRow[] = [] for (const [key, value] of Object.entries(file.entries)) { - yield { kind: 'entry', key, payload: JSON.stringify(value) } + rows.push({ key: rowKey('entry', key), json: JSON.stringify(value) }) } for (const [key, value] of Object.entries(file.authorityCommitments ?? {})) { - yield { kind: 'commitment', key, payload: JSON.stringify(value) } + rows.push({ key: rowKey('commitment', key), json: JSON.stringify(value) }) } if (file.agentExits && file.agentExits.length > 0) { - yield { kind: 'exits', key: '', payload: JSON.stringify(file.agentExits) } + rows.push({ key: rowKey('exits', ''), json: JSON.stringify(file.agentExits) }) } + return rows } -export function assemble(rows: readonly StoredRow[]): Required { +/** The last-status document the rows hold; an unreadable row is dropped (a save deletes it). */ +export function assemble(rows: readonly KeyedJsonRow[]): Required { const file: Required = { version: LAST_STATUS_FILE_VERSION, entries: {}, authorityCommitments: {}, agentExits: [] } - for (const row of rows) { - let value: unknown - try { - value = JSON.parse(row.payload) - } catch { - // An unreadable row is dropped here; it is not in the saved document, so a save deletes it. - continue - } - if (row.kind === 'entry') { - Object.assign(file.entries, { [row.key]: value }) - } else if (row.kind === 'commitment') { - Object.assign(file.authorityCommitments, { [row.key]: value }) - } else if (Array.isArray(value)) { + for (const { key: id, value } of parseKeyedJsonRows(rows)) { + const separator = id.indexOf('\u0000') + const kind = id.slice(0, separator) + const key = id.slice(separator + 1) + if (kind === 'entry') { + Object.assign(file.entries, { [key]: value }) + } else if (kind === 'commitment') { + Object.assign(file.authorityCommitments, { [key]: value }) + } else if (kind === 'exits' && Array.isArray(value)) { file.agentExits = value } } return file } - -function isRowKind(kind: unknown): kind is RowKind { - return kind === 'entry' || kind === 'commitment' || kind === 'exits' -} diff --git a/src/main/agent-hooks/server/last-status-store.ts b/src/main/agent-hooks/server/last-status-store.ts index 28ec892d72c..1b115f13b0f 100644 --- a/src/main/agent-hooks/server/last-status-store.ts +++ b/src/main/agent-hooks/server/last-status-store.ts @@ -1,49 +1,8 @@ import { dirname, join } from 'node:path' -import type SyncDatabase from '../../sqlite/sync-database' -import { hardenSqliteDatabaseFiles } from '../../sqlite/harden-database-files' -import { isSqliteCorruption } from '../../sqlite/sqlite-read-failure' -import { - openVersionedCacheDatabase, - removeVersionedCacheDatabase -} from '../../sqlite/versioned-cache-database' +import { KeyedJsonRowCache, type KeyedJsonRow } from '../../sqlite/row-cache/keyed-json-row-cache' import type { LastStatusFile } from './server-types' -import { hashContent, LegacyLastStatusFile } from './last-status-legacy-file' -import { - assemble, - hashRows, - IMPORTED_LEGACY_HASH, - LAST_STATUS_SCHEMA_VERSION, - readDataVersion, - readRows, - readSavedRows, - rowId, - rowsOf, - type StoredRow -} from './last-status-saved-rows' - -/** - * Restart cache for agent status: one row per pane, per authority commitment, plus the exit - * list. A save rewrites only rows whose content changed, where the whole-file JSON it - * replaces was rewritten on every save. It runs on the main thread, so it never waits on - * another connection's lock beyond the caller's small bound; callers retry later instead. - */ -const SCHEMA = { - version: LAST_STATUS_SCHEMA_VERSION, - sql: ` -CREATE TABLE IF NOT EXISTS meta(key TEXT PRIMARY KEY, value TEXT NOT NULL); -CREATE TABLE IF NOT EXISTS rows( - kind TEXT NOT NULL, - key TEXT NOT NULL, - payload TEXT NOT NULL, - PRIMARY KEY(kind, key) -) WITHOUT ROWID; -`, - // Rows hold prompts and assistant messages; keep the directory user-only. - directoryMode: 0o700, - // The main thread must not sleep through Windows unlock retries; saves retry instead. - removal: 'fail-fast' as const -} +import { assemble, LAST_STATUS_SCHEMA_VERSION, rowsOf } from './last-status-saved-rows' export type SavedStatus = { document: unknown; source: 'legacy' | 'rows' } @@ -51,66 +10,70 @@ export function lastStatusDatabasePath(legacyJsonPath: string): string { return join(dirname(legacyJsonPath), 'last-status.db') } +/** + * Restart cache for agent status, one row per pane, per authority commitment, plus the exit + * list: a save rewrites only rows whose content changed. It runs on the main thread, so it + * never waits on another connection's lock beyond the caller's small bound. + */ export class LastStatusStore { - private db: SyncDatabase | null = null - /** Hashes of the rows on disk as this connection last saw them, labelled by data_version. */ - private baseline: Map | undefined - private dataVersion: unknown - private savedRows: StoredRow[] | undefined + private readonly cache: KeyedJsonRowCache + private savedRows: KeyedJsonRow[] = [] + /** The older build's file read at startup, until hydration admits or rejects it. */ + private legacyReceipt: string | undefined + private pendingImport: string | undefined /** - * Set when the saved rows (or the import receipt) could not be read at startup. The session - * then neither writes nor deletes, so an incomplete view cannot overwrite rows it never - * read, and the legacy file stays for a later start. This also means authority commitments - * are not persisted for this session, as when the JSON file was unwritable before. + * Set when the saved rows or an older build's file could not be read at startup. The session + * then neither writes nor deletes, so rows it never read and a file it never imported both + * survive for a later start. Authority commitments are then not persisted this session, as + * when the JSON file was unwritable before. */ private disabled = false - private readonly legacy: LegacyLastStatusFile - private readonly databasePath: string constructor(legacyJsonPath: string) { - this.legacy = new LegacyLastStatusFile(legacyJsonPath) - this.databasePath = lastStatusDatabasePath(legacyJsonPath) + this.cache = new KeyedJsonRowCache(lastStatusDatabasePath(legacyJsonPath), { + schemaVersion: LAST_STATUS_SCHEMA_VERSION, + busyTimeoutMs: 0, + legacyPath: legacyJsonPath + }) } - /** True for a session whose saved rows could not be read; it saves nothing. */ get isDisabled(): boolean { return this.disabled } /** The saved document (unvalidated) and where it came from, or undefined when none. */ read(busyTimeoutMs: number): SavedStatus | undefined { - const legacy = this.legacy.read() - const saved = readSavedRows(this.databasePath, busyTimeoutMs) - if (saved.kind === 'unavailable') { + const loaded = this.cache.load(parseDocument, busyTimeoutMs) + if (loaded.kind === 'unavailable') { this.disabled = true - throw saved.error + throw loaded.error } - this.savedRows = saved.kind === 'rows' ? saved.rows : [] - if (legacy) { - // Only an older build writes the file, so it is newer than the rows, unless this build - // imported exactly these bytes before and could not remove them. - if (saved.kind !== 'rows' || saved.importedLegacyHash !== legacy.hash) { - return { document: legacy.document, source: 'legacy' } - } - this.legacy.markImported(legacy.hash) + this.savedRows = loaded.rows + if (loaded.kind === 'import') { + this.legacyReceipt = loaded.receipt + return { document: loaded.value.document, source: 'legacy' } } return this.readRows() } /** The saved rows from the last read, for when hydration rejects a legacy file. */ readRows(): SavedStatus | undefined { - const rows = this.savedRows ?? [] - return rows.length > 0 ? { document: assemble(rows), source: 'rows' } : undefined + return this.savedRows.length > 0 + ? { document: assemble(this.savedRows), source: 'rows' } + : undefined } - /** Hydration admitted the legacy file read last; it may go once rows hold it. */ + /** Hydration admitted the legacy file; the next commit records it imported and removes it. */ confirmLegacyImport(): void { - this.legacy.confirmRead() + this.pendingImport = this.legacyReceipt } - /** Hydration rejected the legacy file; drop it after the next commit and keep the rows. */ + /** Hydration rejected the legacy file: remove it and keep the rows. */ rejectLegacy(): void { - this.legacy.discardRead() + if (this.legacyReceipt !== undefined) { + this.cache.discardLegacy(this.legacyReceipt) + this.legacyReceipt = undefined + } } /** Writes changed rows in one transaction; throws on lock or I/O failure for a later retry. */ @@ -118,101 +81,26 @@ export class LastStatusStore { if (this.disabled) { return false } - const next = new Map() - const rows: (StoredRow & { hash: string })[] = [] - for (const row of rowsOf(file)) { - const hash = hashContent(row.payload) - next.set(rowId(row.kind, row.key), hash) - rows.push({ ...row, hash }) - } - const db = this.open(busyTimeoutMs) - let wrote = false - try { - db.exec('BEGIN IMMEDIATE') - // Sampled under the write lock, so no other commit lands before COMMIT, and this - // connection's own commit leaves it unchanged. A changed version means stale hashes. - const version = readDataVersion(db) - const baseline = - this.baseline !== undefined && version === this.dataVersion - ? this.baseline - : hashRows(readRows(db)) - const upsert = db.prepare( - 'INSERT INTO rows(kind, key, payload) VALUES (?, ?, ?) ON CONFLICT(kind, key) DO UPDATE SET payload = excluded.payload' - ) - for (const row of rows) { - const id = rowId(row.kind, row.key) - if (baseline.get(id) !== row.hash) { - upsert.run(row.kind, row.key, row.payload) - wrote = true - } - } - const remove = db.prepare('DELETE FROM rows WHERE kind = ? AND key = ?') - for (const id of baseline.keys()) { - if (next.has(id)) { - continue - } - const [kind, key] = id.split('\u0000') - remove.run(kind, key) - wrote = true - } - const importHash = this.legacy.pendingImportHash - if (importHash !== undefined) { - db.prepare('INSERT OR REPLACE INTO meta(key, value) VALUES (?, ?)').run( - IMPORTED_LEGACY_HASH, - importHash - ) - } - db.exec(wrote || importHash !== undefined ? 'COMMIT' : 'ROLLBACK') - this.dataVersion = version - this.baseline = next - } catch (error) { - if (db.isTransaction) { - db.exec('ROLLBACK') - } - this.handleFailure(error) - throw error - } - this.legacy.removeIfUnchanged() - return wrote + this.cache.write(rowsOf(file), { importReceipt: this.pendingImport, busyTimeoutMs }) + this.pendingImport = undefined + this.legacyReceipt = undefined + return true } close(): void { - this.db?.close() - this.db = null - this.baseline = undefined - this.dataVersion = undefined - this.savedRows = undefined + this.cache.close() + this.savedRows = [] + this.legacyReceipt = undefined + this.pendingImport = undefined this.disabled = false - this.legacy.reset() } +} - private open(busyTimeoutMs: number): SyncDatabase { - if (this.db) { - this.db.pragma(`busy_timeout = ${busyTimeoutMs}`) - return this.db - } - const db = openVersionedCacheDatabase(this.databasePath, { ...SCHEMA, busyTimeoutMs }) - hardenSqliteDatabaseFiles(this.databasePath) - this.db = db - // data_version is per connection; the first write reloads the hashes under its lock. - this.baseline = undefined - this.dataVersion = undefined - return db - } - - private handleFailure(error: unknown): void { - if (!isSqliteCorruption(error)) { - return - } - // A damaged cache is rebuilt empty on the next attempt, as the helper does at open. - this.db?.close() - this.db = null - this.baseline = undefined - this.dataVersion = undefined - try { - removeVersionedCacheDatabase(this.databasePath, 'fail-fast') - } catch { - // Held by another process (Windows); the next save retries the rebuild. - } +function parseDocument(text: string): { document: unknown } | null { + try { + return { document: JSON.parse(text) } + } catch { + console.warn('[agent-hooks] last-status file is not valid JSON; ignoring') + return null } } diff --git a/src/main/agent-hooks/server/server-persistence.ts b/src/main/agent-hooks/server/server-persistence.ts index 5e2751dcf30..3e955f2cb55 100644 --- a/src/main/agent-hooks/server/server-persistence.ts +++ b/src/main/agent-hooks/server/server-persistence.ts @@ -19,6 +19,7 @@ import { authorityCommitmentsMatch } from './server-persistence-validation' import { AgentHookServerHydration } from './server-hydration' import { pruneAgentExits, serializeAgentExits } from './agent-exit-evidence' import { LastStatusStore } from './last-status-store' +import { nextRetryDelayMs } from '../../sqlite/row-cache/row-cache-failure' export abstract class AgentHookServerPersistence extends AgentHookServerHydration { private statusStore: LastStatusStore | null = null @@ -164,10 +165,10 @@ export abstract class AgentHookServerPersistence extends AgentHookServerHydratio store.write(this.buildStatusFile(), busyTimeoutMs) } catch (err) { this.reportStatusPersistFailure(err) - this.statusPersistRetryDelayMs = Math.min( - STATUS_PERSIST_RETRY_MAX_MS, - Math.max(STATUS_PERSIST_RETRY_MIN_MS, this.statusPersistRetryDelayMs * 2) - ) + this.statusPersistRetryDelayMs = nextRetryDelayMs(this.statusPersistRetryDelayMs, { + minMs: STATUS_PERSIST_RETRY_MIN_MS, + maxMs: STATUS_PERSIST_RETRY_MAX_MS + }) this.statusPersistRetryAt = Date.now() + this.statusPersistRetryDelayMs // Retry even if no later mutation arrives. this.scheduleStatusPersist() diff --git a/src/main/agent-hooks/server/testing/persistence/last-status.test-fixtures.ts b/src/main/agent-hooks/server/testing/persistence/last-status.test-fixtures.ts index 077bc6cf3c9..4a2dcda0489 100644 --- a/src/main/agent-hooks/server/testing/persistence/last-status.test-fixtures.ts +++ b/src/main/agent-hooks/server/testing/persistence/last-status.test-fixtures.ts @@ -3,36 +3,21 @@ import { dirname } from 'node:path' import SyncDatabase from '../../../../sqlite/sync-database' import { lastStatusDatabasePath } from '../../last-status-store' -import { LAST_STATUS_FILE_VERSION } from '../../server-constants' +import { assemble } from '../../last-status-saved-rows' /** What a restart restores, in the last-status file shape, read straight from the saved rows. */ export function readSavedStatus(lastStatusPath: string): string { - const path = lastStatusDatabasePath(lastStatusPath) - const db = new SyncDatabase(path, { readonly: true, fileMustExist: true }) + const db = new SyncDatabase(lastStatusDatabasePath(lastStatusPath), { + readonly: true, + fileMustExist: true + }) try { - const entries: Record = {} - const authorityCommitments: Record = {} - const file: { - version: number - entries: typeof entries - authorityCommitments: typeof authorityCommitments - agentExits?: unknown - } = { - version: LAST_STATUS_FILE_VERSION, - entries, - authorityCommitments - } - for (const row of db.prepare('SELECT kind, key, payload FROM rows').all()) { - const value: unknown = JSON.parse(String(row.payload)) - if (row.kind === 'entry') { - entries[String(row.key)] = value - } else if (row.kind === 'commitment') { - authorityCommitments[String(row.key)] = value - } else { - file.agentExits = value - } - } - return JSON.stringify(file) + const rows = db + .prepare('SELECT key, value FROM rows ORDER BY seq') + .all() + .map((row) => ({ key: String(row.key), json: String(row.value) })) + const { agentExits, ...file } = assemble(rows) + return JSON.stringify(agentExits.length > 0 ? { ...file, agentExits } : file) } finally { db.close() } diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.ts index 415e45854d9..457bda5a97e 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.ts @@ -1,9 +1,10 @@ import { join } from 'node:path' import { - readLegacyJsonSource, - removeImportedLegacyJsonSource -} from '../../sqlite/legacy-json-source' -import { KeyedJsonRowCache, type KeyedJsonRow } from '../../sqlite/keyed-json-row-cache' + KeyedJsonRowCache, + parseKeyedJsonRows, + type KeyedJsonRow +} from '../../sqlite/row-cache/keyed-json-row-cache' +import { nextRetryDelayMs } from '../../sqlite/row-cache/row-cache-failure' import type { AgentSessionModelOption, AgentSessionOptionChoice @@ -182,33 +183,27 @@ function catalogRow(entry: AgentModelCatalogEntry): KeyedJsonRow { } } -/** Rows of an older build's file; bytes it cannot use (corrupt, other version) yield none. */ -function legacyRows(text: string): KeyedJsonRow[] { - let root: Record | null - try { - root = asRecord(JSON.parse(text)) - } catch { - return [] - } +/** Entries of an older build's file; bytes it cannot use (corrupt, other version) reject it. */ +function legacyEntries(text: string): AgentModelCatalogEntry[] | null { + const root = asRecord(JSON.parse(text)) if (!root || root.version !== SCHEMA_VERSION || !Array.isArray(root.entries)) { - return [] + return null } - return root.entries - .map(parseEntry) - .filter((entry): entry is AgentModelCatalogEntry => entry !== null) - .map(catalogRow) + return parseEntries(root.entries) } /** One row per catalog entry, so a save writes only the entries whose listing changed. */ export function createAgentModelCatalogFilePersistence( directory: string ): AgentModelCatalogPersistence { - // Earlier builds rewrote this whole file (megabytes) per change; it is imported once, then removed. - const legacyPath = join(directory, 'agent-model-catalog.json') - // Stored state could not be read: this session makes no legacy-vs-stored decision and - // saves nothing, leaving both for the next start. Rediscovery refills memory meanwhile. + // Stored state or an older build's file could not be read: this session saves nothing, + // leaving both for the next start. Rediscovery refills memory meanwhile. let cold = false - const cache = new KeyedJsonRowCache(join(directory, 'agent-model-catalog.db'), SCHEMA_VERSION) + const cache = new KeyedJsonRowCache(join(directory, 'agent-model-catalog.db'), { + schemaVersion: SCHEMA_VERSION, + // Earlier builds rewrote this whole file (megabytes) per change; it is imported once, then removed. + legacyPath: join(directory, 'agent-model-catalog.json') + }) let pending: (() => readonly AgentModelCatalogEntry[]) | null = null let timer: ReturnType | null = null let retryDelayMs = SAVE_COALESCE_MS @@ -233,7 +228,10 @@ export function createAgentModelCatalogFilePersistence( // Optional bookkeeping (a lock elsewhere, a full disk): memory stays authoritative and // the latest snapshot is retried later unless a newer save replaces it. pending ??= snapshot - retryDelayMs = Math.min(retryDelayMs * 2, MAX_RETRY_DELAY_MS) + retryDelayMs = nextRetryDelayMs(retryDelayMs, { + minMs: SAVE_COALESCE_MS, + maxMs: MAX_RETRY_DELAY_MS + }) schedule(retryDelayMs) } } @@ -242,22 +240,21 @@ export function createAgentModelCatalogFilePersistence( return { async load() { - const source = readLegacyJsonSource(legacyPath) - // An unreadable file stays, as does the stored catalog; the next start tries again. - const legacy = - source.kind === 'read' - ? { rows: legacyRows(source.text), receipt: source.receipt } - : undefined - // A file still present came from an older build unless its receipt shows it was imported. - const loaded = cache.load(legacy) - if (loaded.kind === 'unreadable') { + const loaded = cache.load(legacyEntries) + if (loaded.kind === 'unavailable') { cold = true - return [] + return loaded.legacy ?? [] } - if (legacy) { - removeImportedLegacyJsonSource(legacyPath, legacy.receipt) + if (loaded.kind === 'rows') { + return parseEntries(parseKeyedJsonRows(loaded.rows).map(({ value }) => value)) } - return parseEntries(loaded.values) + try { + // An older build's file is newer than the rows: save it now, which removes the file. + cache.write(loaded.value.map(catalogRow), { importReceipt: loaded.receipt }) + } catch { + cold = true + } + return loaded.value }, save(snapshot) { pending = snapshot diff --git a/src/main/persistence/loading-store/github-cache-database.test.ts b/src/main/persistence/loading-store/github-cache-database.test.ts index 008ac4e59a7..7ce6da03f07 100644 --- a/src/main/persistence/loading-store/github-cache-database.test.ts +++ b/src/main/persistence/loading-store/github-cache-database.test.ts @@ -1,18 +1,13 @@ -import { - existsSync, - mkdtempSync, - openSync, - closeSync, - rmSync, - writeSync, - writeFileSync -} from 'node:fs' +import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' import SyncDatabase from '../../sqlite/sync-database' -import { getGithubCacheDatabaseFile, writeGitHubCacheDatabase } from './github-cache-database' -import { readGitHubCacheDatabase } from './github-cache-startup-read' +import { + getGithubCacheDatabaseFile, + readGitHubCacheDatabase, + writeGitHubCacheDatabase +} from './github-cache-database' const directories: string[] = [] @@ -55,51 +50,8 @@ describe('github cache database', () => { expect(readGitHubCacheDatabase(dataFile)).toEqual({ cache: { pr: { 'o/r#5': pr(5) }, issue: {} }, fromSidecar: false, - sidecarHash: null + sidecarHash: null, + unavailable: false }) }) - - it('never lets an already imported sidecar shadow later saves, even while locked', async () => { - const dataFile = profileDataFile() - const sidecar = JSON.stringify({ pr: { 'o/r#5': pr(5) }, issue: {} }) - writeFileSync(sidecarPath(dataFile), sidecar) - const legacy = readGitHubCacheDatabase(dataFile) - await writeGitHubCacheDatabase(dataFile, legacy.cache!, { - busyTimeoutMs: 0, - importedLegacyFileHash: legacy.sidecarHash - }) - // The same imported file survives, as when its deletion failed. - writeFileSync(sidecarPath(dataFile), sidecar) - save(dataFile, { pr: { 'o/r#6': pr(6) }, issue: {} }) - const locker = new SyncDatabase(getGithubCacheDatabaseFile(dataFile)) - locker.exec('BEGIN IMMEDIATE') - try { - expect(readGitHubCacheDatabase(dataFile).cache).toEqual({ pr: { 'o/r#6': pr(6) }, issue: {} }) - } finally { - locker.exec('ROLLBACK') - locker.close() - } - }) - - it('rebuilds a database whose entry rows are corrupt and saves again', () => { - const dataFile = profileDataFile() - save(dataFile, { pr: { 'o/r#1': pr(1) }, issue: {} }) - const path = getGithubCacheDatabaseFile(dataFile) - const db = new SyncDatabase(path) - const root = db.prepare(`SELECT rootpage FROM sqlite_master WHERE name = 'entries'`).get() - const pageSize = db.pragma('page_size', { simple: true }) - db.close() - const fd = openSync(path, 'r+') - writeSync( - fd, - Buffer.from([0xff, 0xff, 0xff, 0xff]), - 0, - 4, - (Number(root?.rootpage) - 1) * Number(pageSize) - ) - closeSync(fd) - - save(dataFile, { pr: { 'o/r#2': pr(2) }, issue: {} }) - expect(readGitHubCacheDatabase(dataFile).cache).toEqual({ pr: { 'o/r#2': pr(2) }, issue: {} }) - }) }) diff --git a/src/main/persistence/loading-store/github-cache-database.ts b/src/main/persistence/loading-store/github-cache-database.ts index 711008ceb60..eaddcf85130 100644 --- a/src/main/persistence/loading-store/github-cache-database.ts +++ b/src/main/persistence/loading-store/github-cache-database.ts @@ -1,170 +1,127 @@ import { dirname, join } from 'node:path' import type { PersistedState } from '../../../shared/persisted-state-types' -import type SyncDatabase from '../../sqlite/sync-database' import { - isUnusableDatabaseError, - openVersionedCacheDatabase, - removeVersionedCacheDatabase -} from '../../sqlite/versioned-cache-database' -import { retireGitHubCacheSidecar, sha256 } from './github-cache-sidecar' + KeyedJsonRowCache, + parseKeyedJsonRows, + type KeyedJsonRow +} from '../../sqlite/row-cache/keyed-json-row-cache' +import { getGithubCacheFile } from './user-data-path' type GitHubCache = PersistedState['githubCache'] type GitHubCacheKind = keyof GitHubCache -/** - * Next-launch copy of the PR/issue caches, one row per entry. A save rewrites only entries - * whose content changed; the JSON sidecar it replaces was rewritten whole on every flush. - * Disposable: a corrupt or mismatched database rebuilds empty and the next poll refills it. - */ -const SCHEMA_SQL = ` -CREATE TABLE IF NOT EXISTS meta(key TEXT PRIMARY KEY, value TEXT NOT NULL); -CREATE TABLE IF NOT EXISTS entries( - kind TEXT NOT NULL, - key TEXT NOT NULL, - hash TEXT NOT NULL, - payload TEXT NOT NULL, - PRIMARY KEY(kind, key) -) WITHOUT ROWID; -` - /** Main-thread callers never wait out another writer's lock; they retry later instead. */ export const GITHUB_CACHE_ROUTINE_BUSY_TIMEOUT_MS = 25 /** Quit and explicit barriers may wait briefly so a transient lock does not drop the save. */ export const GITHUB_CACHE_BARRIER_BUSY_TIMEOUT_MS = 500 -export const GITHUB_CACHE_SCHEMA_VERSION = 1 - +// 2: rows moved to the shared keyed row layout; a version-1 cache rebuilds empty once. +const GITHUB_CACHE_SCHEMA_VERSION = 2 const KINDS: readonly GitHubCacheKind[] = ['pr', 'issue'] export function getGithubCacheDatabaseFile(dataFile: string): string { return join(dirname(dataFile), 'orca-github-cache.db') } -export const IMPORTED_SIDECAR_KEY = 'imported_sidecar_sha256' +/** + * Next-launch copy of the PR/issue caches, one row per entry, so a save rewrites only entries + * whose content changed. Disposable: a corrupt or mismatched database rebuilds empty and the + * next poll refills it. No handle outlives a call (profile moves, Windows). + */ +function githubCacheRows(dataFile: string): KeyedJsonRowCache { + return new KeyedJsonRowCache(getGithubCacheDatabaseFile(dataFile), { + schemaVersion: GITHUB_CACHE_SCHEMA_VERSION, + busyTimeoutMs: GITHUB_CACHE_ROUTINE_BUSY_TIMEOUT_MS, + holdConnection: false, + legacyPath: getGithubCacheFile(dataFile) + }) +} + +export type StoredGitHubCache = { + /** What to hydrate, or null when nothing can be hydrated now. */ + cache: GitHubCache | null + /** `cache` came from an older build's sidecar, so it must be saved as rows. */ + fromSidecar: boolean + /** Receipt of an older build's sidecar to retire once rows that supersede it commit. */ + sidecarHash: string | null + /** The rows or the sidecar could not be read: this session must not save over them. */ + unavailable: boolean +} + +/** + * Startup read. An older build's sidecar is newer than the rows unless the rows record having + * imported exactly its bytes. While either cannot be read, nothing is hydrated and the caller + * saves nothing this session, so neither the rows nor an unread sidecar are lost. + */ +export function readGitHubCacheDatabase(dataFile: string): StoredGitHubCache { + const loaded = githubCacheRows(dataFile).load(parseGitHubCacheSidecar) + if (loaded.kind === 'unavailable') { + return { cache: null, fromSidecar: false, sidecarHash: null, unavailable: true } + } + if (loaded.kind === 'import') { + return { + cache: loaded.value, + fromSidecar: true, + sidecarHash: loaded.receipt, + unavailable: false + } + } + const cache: GitHubCache = { pr: {}, issue: {} } + for (const { key: id, value } of parseKeyedJsonRows(loaded.rows)) { + const separator = id.indexOf('\u0000') + const kind = KINDS.find((candidate) => candidate === id.slice(0, separator)) + if (kind) { + Object.assign(cache[kind], { [id.slice(separator + 1)]: value }) + } + } + const hasRows = Object.keys(cache.pr).length + Object.keys(cache.issue).length > 0 + return { + cache: hasRows ? cache : null, + fromSidecar: false, + sidecarHash: null, + unavailable: false + } +} /** * Save the cache, writing only rows whose content changed. `importedLegacyFileHash` names the * sidecar whose content `cache` already holds (imported or superseded): the commit records it, - * then the returned promise removes the sidecar if it is still that file. If removal never - * happens (quit, failure), the recorded fingerprint keeps it from shadowing these rows. - * Throws synchronously on a failed commit, including SQLITE_BUSY. + * then removes the sidecar if it is still that file. Throws on a failed commit, busy included. */ export function writeGitHubCacheDatabase( dataFile: string, cache: GitHubCache, options: { busyTimeoutMs: number; importedLegacyFileHash: string | null } -): Promise { - const path = getGithubCacheDatabaseFile(dataFile) - const imported = options.importedLegacyFileHash - withCacheDatabase(path, options.busyTimeoutMs, (db) => writeEntries(db, cache, imported)) - return imported === null ? Promise.resolve() : retireGitHubCacheSidecar(dataFile, imported) +): void { + githubCacheRows(dataFile).write(githubCacheRowsOf(cache), { + busyTimeoutMs: options.busyTimeoutMs, + importReceipt: options.importedLegacyFileHash ?? undefined + }) } -export function isGitHubCacheBusyError(error: unknown): boolean { - const code = error instanceof Error && 'code' in error ? String(error.code) : '' - const message = error instanceof Error ? error.message : '' - return /SQLITE_BUSY|SQLITE_LOCKED/.test(code) || /database (is|table is) locked/i.test(message) -} - -/** - * Open, run, close: no handle outlives a call (profile moves, Windows). Corruption found - * anywhere, including in the rows, rebuilds the database once; lock and permission errors - * propagate unchanged so the caller retries later. - */ -function withCacheDatabase( - path: string, - busyTimeoutMs: number, - run: (db: SyncDatabase) => T -): T { - // Fail-fast removal: a rebuild blocked by a held file throws to the caller's retry. - const schema = { - version: GITHUB_CACHE_SCHEMA_VERSION, - sql: SCHEMA_SQL, - busyTimeoutMs, - removal: 'fail-fast' as const - } - for (let attempt = 0; ; attempt++) { - let db: SyncDatabase | null = null - try { - db = openVersionedCacheDatabase(path, schema) - return run(db) - } catch (error) { - if (attempt > 0 || !isUnusableDatabaseError(error)) { - throw error - } - db?.close() - db = null - removeVersionedCacheDatabase(path, 'fail-fast') - } finally { - db?.close() - } - } -} - -export function readGitHubCacheEntries(db: SyncDatabase): GitHubCache { - const cache: GitHubCache = { pr: {}, issue: {} } - for (const row of db.prepare('SELECT kind, key, payload FROM entries').all()) { - const kind = KINDS.find((candidate) => candidate === row.kind) - if (!kind || typeof row.key !== 'string' || typeof row.payload !== 'string') { - continue - } - try { - Object.assign(cache[kind], { [row.key]: JSON.parse(row.payload) }) - } catch { - // An unreadable row is dropped; the next save rewrites or deletes it. - } - } - return cache -} - -function writeEntries(db: SyncDatabase, cache: GitHubCache, importedSidecar: string | null): void { - const upsert = db.prepare( - `INSERT INTO entries(kind, key, hash, payload) VALUES (?, ?, ?, ?) - ON CONFLICT(kind, key) DO UPDATE SET hash = excluded.hash, payload = excluded.payload` - ) - const remove = db.prepare('DELETE FROM entries WHERE kind = ? AND key = ?') - db.exec('BEGIN IMMEDIATE') - try { - // Read the baseline under the write lock, so another writer's commit cannot leave it stale. - const stored = new Map() - for (const row of db.prepare('SELECT kind, key, hash FROM entries').all()) { - if (typeof row.kind === 'string' && typeof row.key === 'string') { - stored.set(rowId(row.kind, row.key), String(row.hash)) +function githubCacheRowsOf(cache: GitHubCache): KeyedJsonRow[] { + const rows: KeyedJsonRow[] = [] + for (const kind of KINDS) { + for (const [key, entry] of Object.entries(cache[kind] ?? {})) { + const json = JSON.stringify(entry) + if (json !== undefined) { + rows.push({ key: `${kind}\u0000${key}`, json }) } } - for (const kind of KINDS) { - for (const [key, entry] of Object.entries(cache[kind] ?? {})) { - const payload = JSON.stringify(entry) - if (payload === undefined) { - continue - } - const id = rowId(kind, key) - const hash = sha256(payload) - if (stored.get(id) !== hash) { - upsert.run(kind, key, hash, payload) - } - stored.delete(id) - } - } - for (const id of stored.keys()) { - const [kind, key] = id.split('\u0000', 2) - remove.run(kind, key) - } - if (importedSidecar !== null) { - db.prepare('INSERT OR REPLACE INTO meta(key, value) VALUES (?, ?)').run( - IMPORTED_SIDECAR_KEY, - importedSidecar - ) - } - db.exec('COMMIT') - } catch (error) { - if (db.isTransaction) { - db.exec('ROLLBACK') - } - throw error } + return rows } -function rowId(kind: string, key: string): string { - return `${kind}\u0000${key}` +/** The shape check the sidecar reader always applied; unusable sidecars are rejected. */ +function parseGitHubCacheSidecar(text: string): GitHubCache | null { + const parsed: unknown = JSON.parse(text) + if (!isRecord(parsed) || !isRecord(parsed.pr) || !isRecord(parsed.issue)) { + return null + } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: same shape check the sidecar reader always applied; entries stay opaque cache payloads. + return parsed as GitHubCache +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) } diff --git a/src/main/persistence/loading-store/github-cache-persistence.ts b/src/main/persistence/loading-store/github-cache-persistence.ts index b0487845315..3370bc5ec08 100644 --- a/src/main/persistence/loading-store/github-cache-persistence.ts +++ b/src/main/persistence/loading-store/github-cache-persistence.ts @@ -2,15 +2,16 @@ import type { StoreRuntimeState } from './store-runtime-state' import { GITHUB_CACHE_BARRIER_BUSY_TIMEOUT_MS, GITHUB_CACHE_ROUTINE_BUSY_TIMEOUT_MS, - isGitHubCacheBusyError, writeGitHubCacheDatabase } from './github-cache-database' +import { classifyRowCacheFailure, nextRetryDelayMs } from '../../sqlite/row-cache/row-cache-failure' type GitHubCachePersistenceRuntime = Pick< StoreRuntimeState, | 'dataFile' | 'githubCacheDirty' | 'githubCacheLegacyFileHash' + | 'githubCachePersistenceDisabled' | 'githubCachePersistedAt' | 'githubCachePersistTimer' | 'githubCacheRetryDelayMs' @@ -39,7 +40,7 @@ export function persistGithubCache( runtime: GitHubCachePersistenceRuntime, mode: 'routine' | 'barrier' ): void { - if (!runtime.githubCacheDirty) { + if (!runtime.githubCacheDirty || runtime.githubCachePersistenceDisabled) { return } if (mode === 'barrier') { @@ -91,27 +92,25 @@ function writeGithubCacheNow(runtime: GitHubCachePersistenceRuntime, barrier: bo suspendGithubCachePersistence(runtime) try { // Synchronous: no later mutation can interleave, so the cache is clean once this returns. - const retired = writeGitHubCacheDatabase(runtime.dataFile, runtime.state.githubCache, { + writeGitHubCacheDatabase(runtime.dataFile, runtime.state.githubCache, { busyTimeoutMs: barrier ? GITHUB_CACHE_BARRIER_BUSY_TIMEOUT_MS : GITHUB_CACHE_ROUTINE_BUSY_TIMEOUT_MS, importedLegacyFileHash: runtime.githubCacheLegacyFileHash }) - // A sidecar left behind is harmless: the committed fingerprint retires it next launch. - void retired.catch(() => {}) runtime.githubCacheDirty = false runtime.githubCacheLegacyFileHash = null runtime.githubCachePersistedAt = Date.now() runtime.githubCacheRetryDelayMs = 0 } catch (error) { // Another connection holds the lock (or the disk refused): back off rather than block. - const delay = Math.min( - Math.max(GITHUB_CACHE_FIRST_RETRY_MS, runtime.githubCacheRetryDelayMs * 2), - GITHUB_CACHE_PERSIST_INTERVAL_MS - ) + const delay = nextRetryDelayMs(runtime.githubCacheRetryDelayMs, { + minMs: GITHUB_CACHE_FIRST_RETRY_MS, + maxMs: GITHUB_CACHE_PERSIST_INTERVAL_MS + }) runtime.githubCacheRetryDelayMs = delay armGithubCacheTimer(runtime, delay, 'retry') - if (!isGitHubCacheBusyError(error)) { + if (classifyRowCacheFailure(error) !== 'busy') { console.warn('[persistence] Failed to write github cache snapshot:', error) } } diff --git a/src/main/persistence/loading-store/github-cache-sidecar.ts b/src/main/persistence/loading-store/github-cache-sidecar.ts deleted file mode 100644 index 52024c94df0..00000000000 --- a/src/main/persistence/loading-store/github-cache-sidecar.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { createHash } from 'node:crypto' -import { readFileSync } from 'node:fs' -import { readFile, rm } from 'node:fs/promises' -import type { PersistedState } from '../../../shared/persisted-state-types' -import { getGithubCacheFile } from './user-data-path' - -type GitHubCache = PersistedState['githubCache'] - -export function sha256(content: string | Buffer): string { - return createHash('sha256').update(content).digest('hex') -} - -/** - * The JSON sidecar older builds write, parsed and fingerprinted from one read of its bytes, so a - * concurrent replacement can never pair one file's content with another's fingerprint. - * Missing or unparsable sidecars read as null, as they always have. - */ -export function readGitHubCacheSidecar(dataFile: string): GitHubCacheSidecar | null { - let bytes: Buffer - try { - // One-shot: the synchronous profile load reads this once at startup. - bytes = readFileSync(getGithubCacheFile(dataFile)) - } catch { - return null - } - return parseGitHubCacheSidecar(bytes) -} - -/** Remove the sidecar off the save path, only if it is still the file the rows superseded. */ -export async function retireGitHubCacheSidecar(dataFile: string, hash: string): Promise { - const path = getGithubCacheFile(dataFile) - const bytes = await readFile(path).catch(() => null) - if (bytes && parseGitHubCacheSidecar(bytes)?.hash === hash) { - await rm(path, { force: true }) - } -} - -type GitHubCacheSidecar = { cache: GitHubCache; hash: string } - -function parseGitHubCacheSidecar(bytes: Buffer): GitHubCacheSidecar | null { - let parsed: unknown - try { - parsed = JSON.parse(bytes.toString('utf-8')) - } catch { - return null - } - if (!isRecord(parsed) || !isRecord(parsed.pr) || !isRecord(parsed.issue)) { - return null - } - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: same shape check the sidecar reader always applied; entries stay opaque cache payloads. - return { cache: parsed as GitHubCache, hash: sha256(bytes) } -} - -function isRecord(value: unknown): value is Record { - return typeof value === 'object' && value !== null && !Array.isArray(value) -} diff --git a/src/main/persistence/loading-store/github-cache-startup-read.ts b/src/main/persistence/loading-store/github-cache-startup-read.ts deleted file mode 100644 index 8374d930576..00000000000 --- a/src/main/persistence/loading-store/github-cache-startup-read.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { existsSync } from 'node:fs' -import type { PersistedState } from '../../../shared/persisted-state-types' -import SyncDatabase from '../../sqlite/sync-database' -import { - isUnusableDatabaseError, - removeVersionedCacheDatabase -} from '../../sqlite/versioned-cache-database' -import { - GITHUB_CACHE_ROUTINE_BUSY_TIMEOUT_MS, - GITHUB_CACHE_SCHEMA_VERSION, - getGithubCacheDatabaseFile, - IMPORTED_SIDECAR_KEY, - readGitHubCacheEntries -} from './github-cache-database' -import { readGitHubCacheSidecar } from './github-cache-sidecar' - -type GitHubCache = PersistedState['githubCache'] - -export type StoredGitHubCache = { - /** What to hydrate, or null when nothing can be hydrated now. */ - cache: GitHubCache | null - /** `cache` came from an older build's sidecar, so it must be saved as rows. */ - fromSidecar: boolean - /** Fingerprint of a sidecar to retire once rows that supersede it commit. */ - sidecarHash: string | null -} - -type SavedRows = - | { kind: 'rows'; cache: GitHubCache; importedSidecar: string | null } - | { kind: 'empty' } - /** Rows exist but cannot be read now, usually another connection's lock. */ - | { kind: 'unavailable' } - -/** - * Startup read. An older build's sidecar is newer than the rows unless the rows record having - * imported exactly its bytes; while the rows cannot be read, that is unknown, so the sidecar is - * neither hydrated nor allowed to replace rows (the first commit supersedes it instead). - */ -export function readGitHubCacheDatabase(dataFile: string): StoredGitHubCache { - const sidecar = readGitHubCacheSidecar(dataFile) - const saved = readSavedRows(getGithubCacheDatabaseFile(dataFile)) - if (sidecar && saved.kind !== 'unavailable') { - if (saved.kind === 'empty' || saved.importedSidecar !== sidecar.hash) { - return { cache: sidecar.cache, fromSidecar: true, sidecarHash: sidecar.hash } - } - } - const rows = saved.kind === 'rows' ? saved.cache : null - const hasRows = rows !== null && Object.keys(rows.pr).length + Object.keys(rows.issue).length > 0 - return { cache: hasRows ? rows : null, fromSidecar: false, sidecarHash: sidecar?.hash ?? null } -} - -/** One read-only snapshot: no write lock, so another writer cannot block or fail startup. */ -function readSavedRows(path: string): SavedRows { - // One-shot: the synchronous profile load reads this once at startup. - if (!existsSync(path)) { - return { kind: 'empty' } - } - let db: SyncDatabase | null = null - try { - db = new SyncDatabase(path, { readonly: true, fileMustExist: true }) - db.pragma(`busy_timeout = ${GITHUB_CACHE_ROUTINE_BUSY_TIMEOUT_MS}`) - db.exec('BEGIN') - try { - const tables = db - .prepare( - "SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('meta', 'entries')" - ) - .all() - const version = - tables.length === 2 - ? db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get() - : undefined - if (version?.value !== String(GITHUB_CACHE_SCHEMA_VERSION)) { - // Another schema rebuilds empty at the next save, as a mismatched cache always has. - return { kind: 'empty' } - } - const marker = db.prepare('SELECT value FROM meta WHERE key = ?').get(IMPORTED_SIDECAR_KEY) - return { - kind: 'rows', - cache: readGitHubCacheEntries(db), - importedSidecar: typeof marker?.value === 'string' ? marker.value : null - } - } finally { - if (db.isTransaction) { - db.exec('COMMIT') - } - } - } catch (error) { - if (!isUnusableDatabaseError(error)) { - return { kind: 'unavailable' } - } - db?.close() - db = null - // Damaged rows hold nothing recoverable; the next save rebuilds the cache. - removeVersionedCacheDatabase(path, 'fail-fast') - return { kind: 'empty' } - } finally { - db?.close() - } -} diff --git a/src/main/persistence/loading-store/loaded-state-parsing.ts b/src/main/persistence/loading-store/loaded-state-parsing.ts index 9bfe817f6d7..05a523cba7d 100644 --- a/src/main/persistence/loading-store/loaded-state-parsing.ts +++ b/src/main/persistence/loading-store/loaded-state-parsing.ts @@ -18,7 +18,7 @@ import { isLegacyOpenCodeSessionCookie, isLegacySshPtyOwnerLease } from '../leasing-ssh-ptys/secret-validation' -import { readGitHubCacheDatabase } from './github-cache-startup-read' +import { readGitHubCacheDatabase } from './github-cache-database' import { retainLegacyOpenCodeGoApiKey } from './legacy-opencode-go-api-key-migration' import { gcStaleWorktreeMeta, @@ -51,6 +51,7 @@ type LoadedStateParsingOperationsRuntime = Pick< | 'dataFile' | 'githubCacheDirty' | 'githubCacheLegacyFileHash' + | 'githubCachePersistenceDisabled' | 'loadNeedsSave' | 'originalExperimentalNativeChat' | 'protectedSecrets' @@ -304,9 +305,9 @@ export class LoadedStateParsingOperations { // A sidecar import, or rows that already hold it, saves so the sidecar retires. this.runtime.githubCacheDirty ||= stored.sidecarHash !== null } - // While the rows are unreadable nothing is dirty, so no save can replace them; a later - // commit of polled state supersedes the unhydrated sidecar instead. this.runtime.githubCacheLegacyFileHash = stored?.sidecarHash ?? null + // Unread rows or an unread sidecar may hold newer entries than this session will poll. + this.runtime.githubCachePersistenceDisabled = stored?.unavailable ?? false logStartupMilestone('persistence-load-done', () => ({ repos: migrated.repos.length, diff --git a/src/main/persistence/loading-store/profile-preferences.ts b/src/main/persistence/loading-store/profile-preferences.ts index 4741d1a49f4..4be94581122 100644 --- a/src/main/persistence/loading-store/profile-preferences.ts +++ b/src/main/persistence/loading-store/profile-preferences.ts @@ -35,6 +35,7 @@ type ProfilePreferencesRuntime = Pick< | 'githubCacheDirty' | 'githubCachePersistedAt' | 'githubCacheLegacyFileHash' + | 'githubCachePersistenceDisabled' | 'githubCachePersistTimer' | 'githubCacheRetryDelayMs' | 'githubCacheWriteListeners' diff --git a/src/main/persistence/loading-store/store-runtime-state.ts b/src/main/persistence/loading-store/store-runtime-state.ts index 52c3a1ed4eb..951187da47b 100644 --- a/src/main/persistence/loading-store/store-runtime-state.ts +++ b/src/main/persistence/loading-store/store-runtime-state.ts @@ -88,6 +88,8 @@ export class StoreRuntimeState { githubCacheRetryDelayMs = 0 /** Fingerprint of a sidecar the loaded cache imported or supersedes; retired once rows commit. */ githubCacheLegacyFileHash: string | null = null + /** The saved cache or an older build's sidecar was unreadable at load: save nothing this session. */ + githubCachePersistenceDisabled = false readonly githubCacheWriteListeners = new Set<() => void>() readonly staleGithubCacheTempCleanup: Promise readonly staleProfileStateTempCleanup: Promise diff --git a/src/main/persistence/loading-store/write-flush-barriers.ts b/src/main/persistence/loading-store/write-flush-barriers.ts index 66ba9940203..629f67d748e 100644 --- a/src/main/persistence/loading-store/write-flush-barriers.ts +++ b/src/main/persistence/loading-store/write-flush-barriers.ts @@ -15,6 +15,7 @@ type WriteFlushBarrierOperationsRuntime = Pick< | 'githubCacheDirty' | 'githubCachePersistedAt' | 'githubCacheLegacyFileHash' + | 'githubCachePersistenceDisabled' | 'githubCachePersistTimer' | 'githubCacheRetryDelayMs' | 'lastDurableWriteGeneration' diff --git a/src/main/runtime/push/notifications/mobile-notification-dismissal-entries.ts b/src/main/runtime/push/notifications/mobile-notification-dismissal-entries.ts index 4ed5edbb929..03af706fe6f 100644 --- a/src/main/runtime/push/notifications/mobile-notification-dismissal-entries.ts +++ b/src/main/runtime/push/notifications/mobile-notification-dismissal-entries.ts @@ -1,7 +1,7 @@ import type { MobileNotificationEvent } from './runtime-mobile-notification-controller' import type { ExecutionHostId } from '../../../../shared/execution-host' import type { DeliveredNotificationIdentity } from '../../../../shared/mobile-notification-identity' -import type { KeyedJsonRow } from '../../../sqlite/keyed-json-row-cache' +import type { KeyedJsonRow } from '../../../sqlite/row-cache/keyed-json-row-cache' import { isStructuredAttentionOrigin, type StructuredAttentionOrigin diff --git a/src/main/runtime/push/notifications/mobile-notification-dismissal-read-failure.test.ts b/src/main/runtime/push/notifications/mobile-notification-dismissal-read-failure.test.ts index 6977386ba24..cdf2851868b 100644 --- a/src/main/runtime/push/notifications/mobile-notification-dismissal-read-failure.test.ts +++ b/src/main/runtime/push/notifications/mobile-notification-dismissal-read-failure.test.ts @@ -32,8 +32,8 @@ it('keeps history it could not read and merges dismissals recorded meanwhile', a const store = new MobileNotificationDismissalStore(dir) store.record(dismiss('old', 1)) await store.close() - // Both the read-only snapshot and the writable open fail once. - failure.remainingOpens = 2 + // The startup read fails; close() retries it and saves what was recorded meanwhile. + failure.remainingOpens = 1 vi.spyOn(console, 'warn').mockImplementation(() => {}) const restarted = new MobileNotificationDismissalStore(dir) expect(restarted.hasLiveDelivery('anything')).toBe(true) diff --git a/src/main/runtime/push/notifications/mobile-notification-dismissal-store.test.ts b/src/main/runtime/push/notifications/mobile-notification-dismissal-store.test.ts index 3270a8b70dd..9b45b703736 100644 --- a/src/main/runtime/push/notifications/mobile-notification-dismissal-store.test.ts +++ b/src/main/runtime/push/notifications/mobile-notification-dismissal-store.test.ts @@ -1,12 +1,4 @@ -import { - existsSync, - mkdirSync, - mkdtempSync, - rmSync, - statSync, - utimesSync, - writeFileSync -} from 'node:fs' +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, expect, it, vi } from 'vitest' @@ -191,24 +183,3 @@ it('a targeted old delivery cannot retire a newer replacement with the same logi ]) expect(h.store.reconcile([shown, { ...shown, notificationSeq: 13 }])).toEqual([shown]) }) - -it('does not let an imported file left behind override later dismissals', async () => { - const h = fixture() - const legacyPath = join(h.path, 'mobile-notification-dismissals.json') - const legacy = JSON.stringify([ - { ...shown, dismissedThrough: -1, expiresAt: Date.now() + 86400_000 } - ]) - writeFileSync(legacyPath, legacy) - const { atimeMs, mtimeMs } = statSync(legacyPath) - const imported = new MobileNotificationDismissalStore(h.path) - imported.record({ type: 'dismiss', ...shown, notificationEpoch: 'now', dismissedDelivery: shown }) - await imported.close() - // The same bytes reappear, as when deleting the imported file failed. - writeFileSync(legacyPath, legacy) - utimesSync(legacyPath, atimeMs / 1000, mtimeMs / 1000) - - const restarted = new MobileNotificationDismissalStore(h.path) - stores.push(restarted) - expect(restarted.reconcile([shown])).toEqual([shown]) - expect(existsSync(legacyPath)).toBe(false) -}) diff --git a/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts b/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts index 1b0581a9111..93a3b475d27 100644 --- a/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts +++ b/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts @@ -1,9 +1,9 @@ import { join } from 'node:path' -import { KeyedJsonRowCache, type KeyedJsonRowImport } from '../../../sqlite/keyed-json-row-cache' import { - readLegacyJsonSource, - removeImportedLegacyJsonSource -} from '../../../sqlite/legacy-json-source' + KeyedJsonRowCache, + parseKeyedJsonRows +} from '../../../sqlite/row-cache/keyed-json-row-cache' +import { nextRetryDelayMs } from '../../../sqlite/row-cache/row-cache-failure' import type { DeliveredNotificationIdentity } from '../../../../shared/mobile-notification-identity' import { applyDismissalEvent, @@ -21,14 +21,13 @@ export type { DeliveredNotificationRecord } from './mobile-notification-dismissa const DATABASE_FILE = 'mobile-notification-dismissals.db' // Earlier builds rewrote this whole file per alert; it is imported once, then removed. const LEGACY_FILE = 'mobile-notification-dismissals.json' -const RETRY_DELAYS_MS = [100, 1_000, 5_000, 30_000, 60_000] as const +const RETRY_BACKOFF = { minMs: 100, maxMs: 60_000 } function warnPersistFailed(error: unknown): void { console.warn('[notifications] Could not persist dismissal recovery state', error) } type PendingEvent = { event: DismissalEvent & { notificationId: string }; at: number } -type LegacyHistory = KeyedJsonRowImport & { entries: DismissalRecordEntry[] } /** * One SQLite row per delivery, so each alert or dismissal saves only the rows it changed. @@ -41,19 +40,20 @@ export class MobileNotificationDismissalStore { private readonly pending: PendingEvent[] = [] private dirty = false private retryTimer: ReturnType | undefined - private retryAttempt = 0 + private retryDelayMs = 0 private closed = false // Notification ids need no secure writer; its synchronous Windows icacls froze windows per alert (STA-9692). private readonly cache: KeyedJsonRowCache - private readonly legacyPath: string constructor(userDataPath: string) { - this.cache = new KeyedJsonRowCache(join(userDataPath, DATABASE_FILE), 1) - this.legacyPath = join(userDataPath, LEGACY_FILE) + this.cache = new KeyedJsonRowCache(join(userDataPath, DATABASE_FILE), { + schemaVersion: 1, + legacyPath: join(userDataPath, LEGACY_FILE) + }) const unread = this.hydrate() if (unread) { // Show what an older build's file says while the stored history stays unread. - this.entries = unread.legacy?.entries ?? [] + this.entries = unread.legacy ?? [] this.scheduleRetry() } } @@ -149,21 +149,25 @@ export class MobileNotificationDismissalStore { } /** Returns why history stays unread, or undefined once it is read and merged. */ - private hydrate(): { legacy?: LegacyHistory } | undefined { - const source = readLegacyJsonSource(this.legacyPath) - if (source.kind === 'unreadable') { - console.warn('[notifications] Could not read dismissal recovery state', source.error) - return {} - } - const legacy = - source.kind === 'read' ? readLegacyHistory(source.text, source.receipt) : undefined + private hydrate(): { legacy?: DismissalRecordEntry[] } | undefined { // A file still present came from an older build unless its receipt shows it was imported. - const loaded = this.cache.load(legacy) - if (loaded.kind === 'unreadable') { + const loaded = this.cache.load(readLegacyHistory) + if (loaded.kind === 'unavailable') { console.warn('[notifications] Could not read dismissal recovery state', loaded.error) - return { legacy } + return { legacy: loaded.legacy } + } + let entries: DismissalRecordEntry[] + if (loaded.kind === 'import') { + try { + this.cache.write(dismissalRows(loaded.value), { importReceipt: loaded.receipt }) + } catch (error) { + console.warn('[notifications] Could not read dismissal recovery state', error) + return { legacy: loaded.value } + } + entries = loaded.value + } else { + entries = readDismissalEntries(parseKeyedJsonRows(loaded.rows).map(({ value }) => value)) } - let entries = readDismissalEntries(loaded.values) for (const { event, at } of this.pending) { entries = applyDismissalEvent(entries, event, at) } @@ -171,9 +175,6 @@ export class MobileNotificationDismissalStore { this.pending.length = 0 this.entries = entries this.historyKnown = true - if (legacy) { - removeImportedLegacyJsonSource(this.legacyPath, legacy.receipt) - } return undefined } @@ -184,7 +185,7 @@ export class MobileNotificationDismissalStore { try { this.cache.write(dismissalRows(this.entries)) this.dirty = false - this.retryAttempt = 0 + this.retryDelayMs = 0 return true } catch (error) { warnPersistFailed(error) @@ -203,8 +204,8 @@ export class MobileNotificationDismissalStore { if (this.retryTimer || this.closed) { return } - const delay = RETRY_DELAYS_MS[Math.min(this.retryAttempt, RETRY_DELAYS_MS.length - 1)] - this.retryAttempt += 1 + this.retryDelayMs = nextRetryDelayMs(this.retryDelayMs, RETRY_BACKOFF) + const delay = this.retryDelayMs this.retryTimer = setTimeout(() => { this.retryTimer = undefined if (!this.attempt()) { @@ -215,14 +216,8 @@ export class MobileNotificationDismissalStore { } } -function readLegacyHistory(text: string, receipt: string): LegacyHistory { - let value: unknown - try { - value = JSON.parse(text) - } catch { - // Corrupt history establishes nothing, like missing history. - value = [] - } - const entries = readDismissalEntries(Array.isArray(value) ? value : []) - return { entries, rows: dismissalRows(entries), receipt } +/** History an older build wrote; corrupt bytes are rejected and never replace stored rows. */ +function readLegacyHistory(text: string): DismissalRecordEntry[] | null { + const value: unknown = JSON.parse(text) + return Array.isArray(value) ? readDismissalEntries(value) : null } diff --git a/src/main/sqlite/keyed-json-row-cache.test.ts b/src/main/sqlite/keyed-json-row-cache.test.ts deleted file mode 100644 index 0eaab068af5..00000000000 --- a/src/main/sqlite/keyed-json-row-cache.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { mkdtempSync, rmSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, expect, it } from 'vitest' -import { KeyedJsonRowCache } from './keyed-json-row-cache' - -const directories: string[] = [] -afterEach(() => { - for (const directory of directories.splice(0)) { - rmSync(directory, { recursive: true, force: true }) - } -}) - -const row = (key: string, value: string) => ({ key, json: JSON.stringify(value) }) - -it('saves exactly its own rows after another connection committed in between', () => { - const directory = mkdtempSync(join(tmpdir(), 'orca-row-cache-')) - directories.push(directory) - const path = join(directory, 'cache.db') - const first = new KeyedJsonRowCache(path, 1) - const second = new KeyedJsonRowCache(path, 1) - try { - first.write([row('a', 'a1'), row('b', 'b1')]) - expect(second.load()).toEqual({ kind: 'rows', values: ['a1', 'b1'] }) - - first.write([row('a', 'a2'), row('b', 'b1')]) - second.write([row('a', 'a1'), row('b', 'b2')]) - - const reader = new KeyedJsonRowCache(path, 1) - expect(reader.load()).toEqual({ kind: 'rows', values: ['a1', 'b2'] }) - reader.close() - } finally { - first.close() - second.close() - } -}) diff --git a/src/main/sqlite/keyed-json-row-cache.ts b/src/main/sqlite/keyed-json-row-cache.ts deleted file mode 100644 index 0a69a3eeab6..00000000000 --- a/src/main/sqlite/keyed-json-row-cache.ts +++ /dev/null @@ -1,299 +0,0 @@ -import { existsSync } from 'node:fs' -import SyncDatabase from './sync-database' -import { - isUnusableDatabaseError, - openVersionedCacheDatabase, - removeVersionedCacheDatabase -} from './versioned-cache-database' -import { hardenSqliteDatabaseFiles } from './harden-database-files' - -export type KeyedJsonRow = { key: string; json: string } - -/** Rows of an older build's file plus the receipt identifying that file. */ -export type KeyedJsonRowImport = { rows: readonly KeyedJsonRow[]; receipt: string } - -export type KeyedJsonRowCacheLoad = - | { kind: 'rows'; values: unknown[] } - | { kind: 'unreadable'; error: unknown } - -type StoredRow = { seq: number; json: string } -type Snapshot = { - receipt: unknown - stored: Map - maxSeq: number - values: unknown[] -} - -// Callers run on the main thread: a lock held elsewhere must fail fast, not freeze the app. -const BUSY_TIMEOUT_MS = 25 -const RECEIPT_KEY = 'legacy_import_receipt' - -/** - * An ordered list of JSON values kept as one SQLite row each, so a save writes only the - * rows whose value or position changed instead of rewriting the whole list. Corruption - * rebuilds the file once; busy and unreadable files throw, and callers retry later. - */ -export class KeyedJsonRowCache { - private db: SyncDatabase | null = null - private stored = new Map() - private maxSeq = 0 - private dataVersion: unknown - private closed = false - - constructor( - private readonly path: string, - private readonly schemaVersion: number - ) {} - - /** - * Opens the database only when it exists or `legacy` must be imported, so an unused - * store creates no file. `legacy` replaces the rows unless its receipt shows it was - * already imported; the receipt commits with the rows, so its source may then be removed. - */ - load(legacy?: KeyedJsonRowImport): KeyedJsonRowCacheLoad { - if (!legacy && !existsSync(this.path)) { - return { kind: 'rows', values: [] } - } - let snapshot: Snapshot | undefined - try { - // A read-only connection is not blocked by another connection's write lock. - snapshot = this.readSnapshot() - } catch (error) { - if (isBusyError(error)) { - return { kind: 'unreadable', error } - } - // Missing tables, a stale schema or corruption: the writable path repairs those. - } - try { - if (snapshot && (!legacy || snapshot.receipt === legacy.receipt)) { - this.stored = snapshot.stored - this.maxSeq = snapshot.maxSeq - // That connection's version means nothing to ours; the first write re-reads. - this.dataVersion = undefined - return { kind: 'rows', values: snapshot.values } - } - return this.transient(() => - this.withRebuild(() => { - const db = this.database() - db.exec('BEGIN') - let values: unknown[] - let imported: boolean - try { - imported = legacy !== undefined && readReceipt(db) !== legacy.receipt - values = imported ? [] : this.readBaseline(db) - db.exec('COMMIT') - } catch (error) { - rollback(db) - throw error - } - if (imported && legacy) { - this.commit(legacy.rows, legacy.receipt) - values = legacy.rows.flatMap((row) => parse(row.json)) - } - return { kind: 'rows' as const, values } - }) - ) - } catch (error) { - this.closeDatabase() - return { kind: 'unreadable', error } - } - } - - /** - * Saves `rows` in order; `importReceipt` replaces every stored row and records the import. - * Throws on failure (busy included); the stored baseline is unchanged so a retry is exact. - */ - write(rows: readonly KeyedJsonRow[], importReceipt?: string): void { - this.transient(() => this.withRebuild(() => this.commit(rows, importReceipt))) - } - - /** Later writes still save, each opening and closing the database itself. */ - close(): void { - this.closed = true - this.closeDatabase() - } - - private commit(rows: readonly KeyedJsonRow[], importReceipt: string | undefined): void { - const db = this.database() - db.exec('BEGIN IMMEDIATE') - try { - const replaceAll = importReceipt !== undefined - // Another connection committed since our baseline: diff against what is on disk. - if (!replaceAll && db.pragma('data_version', { simple: true }) !== this.dataVersion) { - this.readBaseline(db) - } - const baseline = replaceAll ? new Map() : this.stored - const next = new Map() - let maxSeq = replaceAll ? 0 : this.maxSeq - let previous = 0 - for (const { key, json } of rows) { - const old = baseline.get(key) - // Keep a row's position while the order allows it; moved rows go after the rest. - const seq = old && old.seq > previous ? old.seq : ++maxSeq - previous = seq - next.set(key, { seq, json }) - } - if (replaceAll) { - db.prepare('DELETE FROM rows').run() - db.prepare('INSERT OR REPLACE INTO meta(key, value) VALUES (?, ?)').run( - RECEIPT_KEY, - importReceipt - ) - } - const upsert = db.prepare( - `INSERT INTO rows(key, seq, value) VALUES (?, ?, ?) - ON CONFLICT(key) DO UPDATE SET seq = excluded.seq, value = excluded.value` - ) - for (const [key, row] of next) { - const old = baseline.get(key) - if (!old || old.seq !== row.seq || old.json !== row.json) { - upsert.run(key, row.seq, row.json) - } - } - const remove = db.prepare('DELETE FROM rows WHERE key = ?') - for (const key of baseline.keys()) { - if (!next.has(key)) { - remove.run(key) - } - } - // Sampled while the write lock excludes other writers, so no outside commit hides here. - const version = db.pragma('data_version', { simple: true }) - db.exec('COMMIT') - this.stored = next - this.maxSeq = maxSeq - this.dataVersion = version - } catch (error) { - rollback(db) - throw error - } - } - - /** Call inside a transaction: the version is sampled before the rows of the same snapshot. */ - private readBaseline(db: SyncDatabase): unknown[] { - const version = db.pragma('data_version', { simple: true }) - const { stored, maxSeq, values } = readRows(db) - this.stored = stored - this.maxSeq = maxSeq - this.dataVersion = version - return values - } - - /** Rows and import receipt from one read-only snapshot; throws if the schema needs work. */ - private readSnapshot(): Snapshot { - const db = new SyncDatabase(this.path, { readonly: true, fileMustExist: true }) - try { - db.pragma(`busy_timeout = ${BUSY_TIMEOUT_MS}`) - db.exec('BEGIN') - try { - const version = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get() - if (version?.value !== String(this.schemaVersion)) { - throw new Error('Cache schema needs rebuilding') - } - return { receipt: readReceipt(db), ...readRows(db) } - } finally { - rollback(db) - } - } finally { - db.close() - } - } - - /** A damaged page can surface on any statement; rebuild once and redo the operation. */ - private withRebuild(operation: () => T): T { - try { - return operation() - } catch (error) { - if (!isUnusableDatabaseError(error)) { - throw error - } - this.closeDatabase() - // Fail fast: a file held open elsewhere (Windows) throws, and the caller retries later. - removeVersionedCacheDatabase(this.path, 'fail-fast') - this.stored = new Map() - this.maxSeq = 0 - return operation() - } - } - - /** After close, each operation opens and closes the database itself. */ - private transient(operation: () => T): T { - const transient = this.closed && this.db === null - try { - return operation() - } finally { - if (transient) { - this.closeDatabase() - } - } - } - - private database(): SyncDatabase { - if (this.db) { - return this.db - } - const db = openVersionedCacheDatabase(this.path, { - version: this.schemaVersion, - busyTimeoutMs: BUSY_TIMEOUT_MS, - removal: 'fail-fast', - sql: `CREATE TABLE IF NOT EXISTS meta(key TEXT PRIMARY KEY, value TEXT NOT NULL); - CREATE TABLE IF NOT EXISTS rows(key TEXT PRIMARY KEY, seq INTEGER NOT NULL, value TEXT NOT NULL);` - }) - hardenSqliteDatabaseFiles(this.path) - this.db = db - // A fresh connection cannot vouch for a baseline read through an earlier one. - this.dataVersion = undefined - return db - } - - private closeDatabase(): void { - try { - this.db?.close() - } catch { - // Closing a failed handle must not mask the original error. - } - this.db = null - } -} - -function readRows(db: SyncDatabase): Omit { - const stored = new Map() - const values: unknown[] = [] - let maxSeq = 0 - for (const row of db.prepare('SELECT key, seq, value FROM rows ORDER BY seq').all()) { - const { key, seq, value } = row - if (typeof key === 'string' && typeof seq === 'number' && typeof value === 'string') { - stored.set(key, { seq, json: value }) - maxSeq = Math.max(maxSeq, seq) - values.push(...parse(value)) - } - } - return { stored, maxSeq, values } -} - -const BUSY_ERROR = /SQLITE_BUSY|SQLITE_LOCKED|database is locked|database table is locked/i - -function isBusyError(error: unknown): boolean { - if (!(error instanceof Error)) { - return false - } - const code = 'code' in error ? error.code : undefined - return (typeof code === 'string' && BUSY_ERROR.test(code)) || BUSY_ERROR.test(error.message) -} - -function readReceipt(db: SyncDatabase): unknown { - return db.prepare('SELECT value FROM meta WHERE key = ?').get(RECEIPT_KEY)?.value -} - -function rollback(db: SyncDatabase): void { - if (db.isTransaction) { - db.exec('ROLLBACK') - } -} - -function parse(json: string): unknown[] { - try { - return [JSON.parse(json)] - } catch { - return [] - } -} diff --git a/src/main/sqlite/legacy-json-source.ts b/src/main/sqlite/legacy-json-source.ts deleted file mode 100644 index e0cdaca8575..00000000000 --- a/src/main/sqlite/legacy-json-source.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { createHash } from 'node:crypto' -import { closeSync, fstatSync, openSync, readFileSync, rmSync } from 'node:fs' -import { isDefinitiveAbsence } from '../../shared/definitive-filesystem-absence' - -/** A JSON file an older build may have written, before its rows moved into SQLite. */ -export type LegacyJsonSource = - | { kind: 'absent' } - | { kind: 'unreadable'; error: unknown } - | { kind: 'read'; text: string; receipt: string } - -/** `receipt` identifies these exact bytes, so a finished import is recognised next start. */ -export function readLegacyJsonSource(path: string): LegacyJsonSource { - let fd: number - try { - fd = openSync(path, 'r') - } catch (error) { - return isDefinitiveAbsence(error) ? { kind: 'absent' } : { kind: 'unreadable', error } - } - try { - const { mtimeMs } = fstatSync(fd) - const bytes = readFileSync(fd) - const hash = createHash('sha256').update(bytes).digest('hex') - return { - kind: 'read', - text: bytes.toString('utf8'), - receipt: `${bytes.length}:${Math.floor(mtimeMs)}:${hash}` - } - } catch (error) { - return { kind: 'unreadable', error } - } finally { - closeSync(fd) - } -} - -/** Removes the file only while it is still the import's source, never a newer rewrite. */ -export function removeImportedLegacyJsonSource(path: string, receipt: string): void { - const current = readLegacyJsonSource(path) - if (current.kind !== 'read' || current.receipt !== receipt) { - return - } - try { - rmSync(path, { force: true }) - } catch { - // The stored receipt marks it imported, so a leftover file cannot shadow newer rows. - } -} diff --git a/src/main/sqlite/row-cache/keyed-json-row-cache.test.ts b/src/main/sqlite/row-cache/keyed-json-row-cache.test.ts new file mode 100644 index 00000000000..c23eadd9adb --- /dev/null +++ b/src/main/sqlite/row-cache/keyed-json-row-cache.test.ts @@ -0,0 +1,167 @@ +import { + chmodSync, + closeSync, + copyFileSync, + existsSync, + mkdtempSync, + openSync, + rmSync, + writeFileSync, + writeSync +} from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import SyncDatabase from '../sync-database' +import { KeyedJsonRowCache, parseKeyedJsonRows } from './keyed-json-row-cache' + +const directories: string[] = [] +afterEach(() => { + for (const directory of directories.splice(0)) { + rmSync(directory, { recursive: true, force: true }) + } +}) + +function paths() { + const directory = mkdtempSync(join(tmpdir(), 'orca-row-cache-')) + directories.push(directory) + return { path: join(directory, 'cache.db'), legacyPath: join(directory, 'cache.json') } +} + +const row = (key: string, value: string) => ({ key, json: JSON.stringify(value) }) +/** An older build's file: a JSON array of strings; anything else is rejected. */ +const parseLegacy = (text: string): string[] | null => { + const value: unknown = JSON.parse(text) + return Array.isArray(value) ? value.map(String) : null +} + +/** What a store restores at startup: imported legacy values, or the stored rows. */ +function restore(cache: KeyedJsonRowCache): unknown { + const loaded = cache.load(parseLegacy) + if (loaded.kind === 'unavailable') { + return 'unavailable' + } + if (loaded.kind === 'import') { + cache.write( + loaded.value.map((value) => row(value, value)), + { importReceipt: loaded.receipt } + ) + return loaded.value + } + return parseKeyedJsonRows(loaded.rows).map(({ value }) => value) +} + +describe('keyed JSON row cache', () => { + it('saves exactly its own rows after another connection committed in between', () => { + const { path } = paths() + const first = new KeyedJsonRowCache(path, { schemaVersion: 1 }) + const second = new KeyedJsonRowCache(path, { schemaVersion: 1 }) + try { + first.write([row('a', 'a1'), row('b', 'b1')]) + second.write([row('a', 'a1'), row('b', 'b1')]) + first.write([row('a', 'a2'), row('b', 'b1')]) + second.write([row('a', 'a1'), row('b', 'b2')]) + + expect(restore(new KeyedJsonRowCache(path, { schemaVersion: 1 }))).toEqual(['a1', 'b2']) + } finally { + first.close() + second.close() + } + }) + + it("imports an older build's file once, even when the same bytes are later restored", () => { + const { path, legacyPath } = paths() + const options = { schemaVersion: 1, holdConnection: false, legacyPath } + writeFileSync(legacyPath, '["from file"]') + copyFileSync(legacyPath, `${legacyPath}.copy`) + + expect(restore(new KeyedJsonRowCache(path, options))).toEqual(['from file']) + expect(existsSync(legacyPath)).toBe(false) + new KeyedJsonRowCache(path, options).write([row('x', 'saved later')]) + // A leftover copy (failed deletion, a backup restore) is recognised by its bytes. + copyFileSync(`${legacyPath}.copy`, legacyPath) + + expect(restore(new KeyedJsonRowCache(path, options))).toEqual(['saved later']) + expect(existsSync(legacyPath)).toBe(false) + writeFileSync(legacyPath, '["rewritten by an older build"]') + expect(restore(new KeyedJsonRowCache(path, options))).toEqual(['rewritten by an older build']) + }) + + it('recognises a file imported under the earlier length:mtime:hash receipt', () => { + const { path, legacyPath } = paths() + const options = { schemaVersion: 1, holdConnection: false, legacyPath } + new KeyedJsonRowCache(path, options).write([row('x', 'saved later')], { + importReceipt: + '13:1700000000000:526aa8236d656e8a8b8cd78bee7a16d95e1e93e99d18a37298c506cf9c789750' + }) + writeFileSync(legacyPath, '["from file"]') + + expect(restore(new KeyedJsonRowCache(path, options))).toEqual(['saved later']) + expect(existsSync(legacyPath)).toBe(false) + }) + + it('keeps the stored rows when an older build file is rejected', () => { + const { path, legacyPath } = paths() + const options = { schemaVersion: 1, holdConnection: false, legacyPath } + new KeyedJsonRowCache(path, options).write([row('a', 'stored')]) + writeFileSync(legacyPath, '{ not json') + + expect(restore(new KeyedJsonRowCache(path, options))).toEqual(['stored']) + expect(existsSync(legacyPath)).toBe(false) + }) + + it.skipIf(process.platform === 'win32' || process.getuid?.() === 0)( + 'leaves an unreadable older build file and the rows for a later start', + () => { + const { path, legacyPath } = paths() + const options = { schemaVersion: 1, holdConnection: false, legacyPath } + new KeyedJsonRowCache(path, options).write([row('a', 'stored')]) + writeFileSync(legacyPath, '["from file"]') + chmodSync(legacyPath, 0o000) + + expect(restore(new KeyedJsonRowCache(path, options))).toBe('unavailable') + chmodSync(legacyPath, 0o600) + expect(restore(new KeyedJsonRowCache(path, options))).toEqual(['from file']) + } + ) + + it('reports rows held under an exclusive lock as unavailable and keeps the older file', () => { + const { path, legacyPath } = paths() + const options = { schemaVersion: 1, holdConnection: false, legacyPath } + new KeyedJsonRowCache(path, options).write([row('a', 'stored')]) + writeFileSync(legacyPath, '["from file"]') + const locker = new SyncDatabase(path) + locker.pragma('locking_mode = EXCLUSIVE') + locker.exec('BEGIN EXCLUSIVE') + try { + expect(restore(new KeyedJsonRowCache(path, { ...options, busyTimeoutMs: 0 }))).toBe( + 'unavailable' + ) + } finally { + locker.exec('ROLLBACK') + locker.close() + } + expect(existsSync(legacyPath)).toBe(true) + }) + + it('rebuilds a database whose row pages are corrupt and saves again', () => { + const { path } = paths() + new KeyedJsonRowCache(path, { schemaVersion: 1, holdConnection: false }).write([row('a', '1')]) + const db = new SyncDatabase(path) + const root = db.prepare(`SELECT rootpage FROM sqlite_master WHERE name = 'rows'`).get() + const pageSize = Number(db.pragma('page_size', { simple: true })) + db.close() + const fd = openSync(path, 'r+') + writeSync( + fd, + Buffer.from([0xff, 0xff, 0xff, 0xff]), + 0, + 4, + (Number(root?.rootpage) - 1) * pageSize + ) + closeSync(fd) + + new KeyedJsonRowCache(path, { schemaVersion: 1, holdConnection: false }).write([row('b', '2')]) + expect(restore(new KeyedJsonRowCache(path, { schemaVersion: 1 }))).toEqual(['2']) + }) +}) diff --git a/src/main/sqlite/row-cache/keyed-json-row-cache.ts b/src/main/sqlite/row-cache/keyed-json-row-cache.ts new file mode 100644 index 00000000000..e6202151e43 --- /dev/null +++ b/src/main/sqlite/row-cache/keyed-json-row-cache.ts @@ -0,0 +1,349 @@ +import { existsSync } from 'node:fs' +import SyncDatabase from '../sync-database' +import { + openVersionedCacheDatabase, + removeVersionedCacheDatabase +} from '../versioned-cache-database' +import { hardenSqliteDatabaseFiles } from '../harden-database-files' +import { classifyRowCacheFailure } from './row-cache-failure' +import { readLegacyJsonSource, removeLegacyJsonSource } from './legacy-json-source' + +export type KeyedJsonRow = { key: string; json: string } + +export type KeyedJsonRowCacheOptions = { + schemaVersion: number + /** Lock wait per call. Main-thread callers keep it short and retry later instead. */ + busyTimeoutMs?: number + /** False opens and closes the database per call, so no handle outlives it (profile moves). */ + holdConnection?: boolean + /** JSON file older builds wrote; see load(). */ + legacyPath?: string +} + +/** + * What a store hydrates at startup. + * - rows: the stored rows (no older-build file, or one already imported or rejected). + * - import: an older build's file is newer than the rows; save `value` with `receipt`. + * - unavailable: the rows or the file cannot be read now. The store must neither hydrate a + * replacement nor save over rows it never read; `legacy` is the file's value if it was read. + */ +export type KeyedJsonRowLoad = + | { kind: 'rows'; rows: KeyedJsonRow[] } + | { kind: 'import'; value: T; receipt: string; rows: KeyedJsonRow[] } + | { kind: 'unavailable'; error: unknown; legacy?: T } + +type StoredRow = { seq: number; json: string } + +const DEFAULT_BUSY_TIMEOUT_MS = 25 +const RECEIPT_KEY = 'legacy_import_receipt' +const SCHEMA_SQL = `CREATE TABLE IF NOT EXISTS meta(key TEXT PRIMARY KEY, value TEXT NOT NULL); +CREATE TABLE IF NOT EXISTS rows(key TEXT PRIMARY KEY, seq INTEGER NOT NULL, value TEXT NOT NULL);` + +/** + * An ordered list of JSON values, one SQLite row each, so a save writes only the rows whose + * value or position changed. Corruption rebuilds the file once; busy and unreadable files + * throw from write(), and callers retry later. + */ +export class KeyedJsonRowCache { + private db: SyncDatabase | null = null + private stored = new Map() + private maxSeq = 0 + private dataVersion: unknown + private closed = false + private readonly busyTimeoutMs: number + + constructor( + readonly path: string, + private readonly options: KeyedJsonRowCacheOptions + ) { + this.busyTimeoutMs = options.busyTimeoutMs ?? DEFAULT_BUSY_TIMEOUT_MS + } + + /** + * Startup read of the rows and, when configured, the older build's file. That file is newer + * than the rows unless the rows record importing exactly its bytes; `parse` returning null + * rejects it. An imported or rejected file is removed here and never replaces the rows. + */ + load(parse: (text: string) => T | null, busyTimeoutMs?: number): KeyedJsonRowLoad { + const legacyPath = this.options.legacyPath + const source = legacyPath ? readLegacyJsonSource(legacyPath) : { kind: 'absent' as const } + const parsed = source.kind === 'read' ? parseOrNull(parse, source.text) : null + const legacy = parsed ?? undefined + const snapshot = this.readSnapshot(busyTimeoutMs ?? this.busyTimeoutMs) + if (snapshot.kind === 'unavailable') { + return { kind: 'unavailable', error: snapshot.error, ...(legacy ? { legacy } : {}) } + } + if (source.kind === 'unreadable') { + // A file that may be newer than the rows cannot be ruled out until it can be read. + return { kind: 'unavailable', error: source.error } + } + if (source.kind === 'read' && legacyPath) { + if (parsed !== null && !receiptNames(snapshot.receipt, source.receipt)) { + return { kind: 'import', value: parsed, receipt: source.receipt, rows: snapshot.rows } + } + // Already imported (its removal failed before) or rejected: the rows stay authoritative. + removeLegacyJsonSource(legacyPath, source.receipt) + } + return { kind: 'rows', rows: snapshot.rows } + } + + /** Hydration rejected an imported value after load(): remove its file, keep the rows. */ + discardLegacy(receipt: string): void { + if (this.options.legacyPath) { + removeLegacyJsonSource(this.options.legacyPath, receipt) + } + } + + /** + * Saves `rows` in order. `importReceipt` records the older build's file these rows now hold, + * in the same transaction, and removes that file once committed. Throws on failure (busy + * included); the baseline is unchanged, so a retry is exact. + */ + write( + rows: readonly KeyedJsonRow[], + options: { importReceipt?: string; busyTimeoutMs?: number } = {} + ): void { + const busyTimeoutMs = options.busyTimeoutMs ?? this.busyTimeoutMs + try { + this.withRebuild(() => this.commit(rows, options.importReceipt, busyTimeoutMs)) + } finally { + // After close() (late events at teardown), each write opens and closes its own handle. + if (this.options.holdConnection === false || this.closed) { + this.closeDatabase() + } + } + if (options.importReceipt !== undefined && this.options.legacyPath) { + removeLegacyJsonSource(this.options.legacyPath, options.importReceipt) + } + } + + close(): void { + this.closed = true + this.closeDatabase() + } + + /** One read-only snapshot: another connection's write lock cannot block it. */ + private readSnapshot( + busyTimeoutMs: number + ): + | { kind: 'rows'; rows: KeyedJsonRow[]; receipt: unknown } + | { kind: 'unavailable'; error: unknown } { + // One-shot startup probe, so an unused store creates no file. + if (!existsSync(this.path)) { + return { kind: 'rows', rows: [], receipt: undefined } + } + let db: SyncDatabase | null = null + try { + db = new SyncDatabase(this.path, { readonly: true, fileMustExist: true }) + db.pragma(`busy_timeout = ${busyTimeoutMs}`) + db.exec('BEGIN') + try { + const tables = db + .prepare( + "SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('meta', 'rows')" + ) + .all() + const version = + tables.length === 2 + ? db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get() + : undefined + if (version?.value !== String(this.options.schemaVersion)) { + // Another layout holds nothing this build reads; the first write rebuilds it. + return { kind: 'rows', rows: [], receipt: undefined } + } + const rows = [...readRows(db).stored].map(([key, { json }]) => ({ key, json })) + return { kind: 'rows', rows, receipt: readReceipt(db) } + } finally { + rollback(db) + } + } catch (error) { + if (classifyRowCacheFailure(error) !== 'corrupt') { + return { kind: 'unavailable', error } + } + db?.close() + db = null + try { + // Damaged rows hold nothing recoverable; the next write rebuilds the cache. + removeVersionedCacheDatabase(this.path, 'fail-fast') + } catch (removeError) { + return { kind: 'unavailable', error: removeError } + } + return { kind: 'rows', rows: [], receipt: undefined } + } finally { + db?.close() + } + } + + private commit( + rows: readonly KeyedJsonRow[], + importReceipt: string | undefined, + busyTimeoutMs: number + ): void { + const db = this.database(busyTimeoutMs) + db.pragma(`busy_timeout = ${busyTimeoutMs}`) + db.exec('BEGIN IMMEDIATE') + try { + // Another connection committed since our baseline: diff against what is on disk. + if (db.pragma('data_version', { simple: true }) !== this.dataVersion) { + this.readBaseline(db) + } + const baseline = this.stored + const next = new Map() + let maxSeq = this.maxSeq + let previous = 0 + for (const { key, json } of rows) { + const old = baseline.get(key) + // Keep a row's position while the order allows it; moved rows go after the rest. + const seq = old && old.seq > previous ? old.seq : ++maxSeq + previous = seq + next.set(key, { seq, json }) + } + const upsert = db.prepare( + `INSERT INTO rows(key, seq, value) VALUES (?, ?, ?) + ON CONFLICT(key) DO UPDATE SET seq = excluded.seq, value = excluded.value` + ) + for (const [key, row] of next) { + const old = baseline.get(key) + if (!old || old.seq !== row.seq || old.json !== row.json) { + upsert.run(key, row.seq, row.json) + } + } + const remove = db.prepare('DELETE FROM rows WHERE key = ?') + for (const key of baseline.keys()) { + if (!next.has(key)) { + remove.run(key) + } + } + if (importReceipt !== undefined) { + db.prepare('INSERT OR REPLACE INTO meta(key, value) VALUES (?, ?)').run( + RECEIPT_KEY, + importReceipt + ) + } + // Sampled while the write lock excludes other writers, so no outside commit hides here. + const version = db.pragma('data_version', { simple: true }) + db.exec('COMMIT') + this.stored = next + this.maxSeq = maxSeq + this.dataVersion = version + } catch (error) { + rollback(db) + throw error + } + } + + /** + * Call inside a transaction: the version is sampled with the rows of the same snapshot, and + * all three are published together, so a failed read never vouches for an older baseline. + */ + private readBaseline(db: SyncDatabase): void { + this.dataVersion = undefined + const version = db.pragma('data_version', { simple: true }) + const { stored, maxSeq } = readRows(db) + this.stored = stored + this.maxSeq = maxSeq + this.dataVersion = version + } + + /** A damaged page can surface on any statement; rebuild once and redo the operation. */ + private withRebuild(operation: () => T): T { + try { + return operation() + } catch (error) { + if (classifyRowCacheFailure(error) !== 'corrupt') { + throw error + } + this.closeDatabase() + // Fail fast: a file held open elsewhere (Windows) throws, and the caller retries later. + removeVersionedCacheDatabase(this.path, 'fail-fast') + return operation() + } + } + + private database(busyTimeoutMs: number): SyncDatabase { + if (this.db) { + return this.db + } + const db = openVersionedCacheDatabase(this.path, { + version: this.options.schemaVersion, + // The caller's budget covers opening too: schema setup writes, so it can meet the lock. + busyTimeoutMs, + removal: 'fail-fast', + // Rows can hold prompts, PR titles and notification ids; keep them user-only. + directoryMode: 0o700, + sql: SCHEMA_SQL + }) + hardenSqliteDatabaseFiles(this.path) + this.db = db + // A fresh connection cannot vouch for a baseline read through an earlier one. + this.dataVersion = undefined + this.stored = new Map() + this.maxSeq = 0 + return db + } + + private closeDatabase(): void { + try { + this.db?.close() + } catch { + // Closing a failed handle must not mask the original error. + } + this.db = null + } +} + +function readRows(db: SyncDatabase): { stored: Map; maxSeq: number } { + const stored = new Map() + let maxSeq = 0 + for (const row of db.prepare('SELECT key, seq, value FROM rows ORDER BY seq').all()) { + const { key, seq, value } = row + if (typeof key === 'string' && typeof seq === 'number' && typeof value === 'string') { + stored.set(key, { seq, json: value }) + maxSeq = Math.max(maxSeq, seq) + } + } + return { stored, maxSeq } +} + +function readReceipt(db: SyncDatabase): unknown { + return db.prepare('SELECT value FROM meta WHERE key = ?').get(RECEIPT_KEY)?.value +} + +function rollback(db: SyncDatabase): void { + if (db.isTransaction) { + db.exec('ROLLBACK') + } +} + +/** Parsed values of rows, skipping any that no longer parse (the next save rewrites them). */ +export function parseKeyedJsonRows( + rows: readonly KeyedJsonRow[] +): { key: string; value: unknown }[] { + return rows.flatMap(({ key, json }) => { + try { + const value: unknown = JSON.parse(json) + return [{ key, value }] + } catch { + return [] + } + }) +} + +function parseOrNull(parse: (text: string) => T | null, text: string): T | null { + try { + return parse(text) + } catch { + return null + } +} + +/** + * Whether a stored import receipt names these bytes. Builds before the shared row cache wrote + * `::`; its hash still identifies the same bytes. + */ +function receiptNames(stored: unknown, receipt: string): boolean { + if (typeof stored !== 'string') { + return false + } + return stored === receipt || /^\d+:\d+:([0-9a-f]{64})$/.exec(stored)?.[1] === receipt +} diff --git a/src/main/sqlite/row-cache/legacy-json-source.ts b/src/main/sqlite/row-cache/legacy-json-source.ts new file mode 100644 index 00000000000..7c5e4e2b7c6 --- /dev/null +++ b/src/main/sqlite/row-cache/legacy-json-source.ts @@ -0,0 +1,45 @@ +import { createHash } from 'node:crypto' +import { readFileSync, rmSync } from 'node:fs' +import { isDefinitiveAbsence } from '../../../shared/definitive-filesystem-absence' + +/** A JSON file an older build may have written, before its rows moved into SQLite. */ +export type LegacyJsonSource = + | { kind: 'absent' } + /** Present but not readable now (permissions, a Windows lock): neither absent nor imported. */ + | { kind: 'unreadable'; error: unknown } + | { kind: 'read'; text: string; receipt: string } + +/** + * One-shot startup read. `receipt` is the hash of these exact bytes (not their mtime, so a + * copied or restored leftover is still recognised as already imported). + */ +export function readLegacyJsonSource(path: string): LegacyJsonSource { + let bytes: Buffer + try { + bytes = readFileSync(path) + } catch (error) { + return isDefinitiveAbsence(error) ? { kind: 'absent' } : { kind: 'unreadable', error } + } + return { + kind: 'read', + text: bytes.toString('utf8'), + receipt: createHash('sha256').update(bytes).digest('hex') + } +} + +/** + * Removes the file only while it still holds the bytes `receipt` names, never a rewrite by + * another build since. One-shot: after the import commits, or when its bytes were rejected. + */ +export function removeLegacyJsonSource(path: string, receipt: string): void { + const current = readLegacyJsonSource(path) + if (current.kind !== 'read' || current.receipt !== receipt) { + return + } + try { + rmSync(path, { force: true }) + } catch (error) { + // A committed receipt marks it imported, so a leftover file cannot shadow newer rows. + console.warn('[sqlite] Could not remove a migrated legacy JSON file:', error) + } +} diff --git a/src/main/sqlite/row-cache/row-cache-failure.ts b/src/main/sqlite/row-cache/row-cache-failure.ts new file mode 100644 index 00000000000..8d6c9a0b69a --- /dev/null +++ b/src/main/sqlite/row-cache/row-cache-failure.ts @@ -0,0 +1,19 @@ +import { isSqliteCorruption, isTransientSqliteContention } from '../sqlite-read-failure' +import { isUnusableDatabaseError } from '../versioned-cache-database' + +/** busy: another connection holds the lock, retry later. corrupt: rebuild. other: report. */ +export type RowCacheFailure = 'busy' | 'corrupt' | 'other' + +export function classifyRowCacheFailure(error: unknown): RowCacheFailure { + if (isTransientSqliteContention(error)) { + return 'busy' + } + return isSqliteCorruption(error) || isUnusableDatabaseError(error) ? 'corrupt' : 'other' +} + +export type RetryBackoff = { readonly minMs: number; readonly maxMs: number } + +/** Doubling delay after `previousMs` (0 for the first failure), clamped to the range. */ +export function nextRetryDelayMs(previousMs: number, { minMs, maxMs }: RetryBackoff): number { + return Math.min(maxMs, Math.max(minMs, previousMs * 2)) +} From e7b74c031ba510f984da66e420898784d378a381 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:37:21 +0000 Subject: [PATCH 05/80] fix(worktrees): keep long-path and WSL-root Claude history names retired (#40008) * fix(worktrees): use Session History's Claude bucket rules when retiring names Retirement backfill matched Claude project buckets with the old encoder, so it missed hashed long-path buckets, root workspace folders and the \\wsl$\ share spelling. Use the shared bucket prefixes, and read the cwd of a hashed bucket to recover the workspace name its name no longer carries. * test(worktrees): drop retirement cases production cannot reach * fix(worktrees): fold case when matching a hashed bucket's cwd to the workspace root * refactor(worktrees): match each retirement bucket once and drop the old bucket rule * perf(worktrees): overlap long-bucket probes, skip rescans on denied transcripts A transcript that stays permission-denied forced a full retirement rescan after every 60 s backoff. --- .../claude/claude-project-dir-encoding.ts | 43 ++++---- .../session-scanner-scope-discovery.ts | 4 +- .../worktree-retirement-discovery-wsl.test.ts | 94 +++++++++++++++- .../worktree-retirement-discovery.test.ts | 47 ++++++++ .../worktree-retirement-discovery.ts | 103 ++++++++++++++---- 5 files changed, 243 insertions(+), 48 deletions(-) create mode 100644 src/main/workspaces/removal/retirement/worktree-retirement-discovery.test.ts diff --git a/src/main/ai-vault/scanners/claude/claude-project-dir-encoding.ts b/src/main/ai-vault/scanners/claude/claude-project-dir-encoding.ts index facf7c026e0..7d6025ebd0f 100644 --- a/src/main/ai-vault/scanners/claude/claude-project-dir-encoding.ts +++ b/src/main/ai-vault/scanners/claude/claude-project-dir-encoding.ts @@ -15,19 +15,6 @@ export function encodeClaudeProjectPath(pathValue: string): string { return trimmed.replace(/[^a-zA-Z0-9]/g, '-') } -/** Require an encoded separator boundary so orca cannot match its sibling orcadyne. */ -export function isClaudeProjectDirInScope( - projectDirName: string, - scopePrefixes: ReadonlySet | readonly string[] -): boolean { - for (const prefix of scopePrefixes) { - if (projectDirName === prefix || projectDirName.startsWith(`${prefix}-`)) { - return true - } - } - return false -} - // Claude Code names a longer encoded path's bucket `-`; older versions kept the full name. const CLAUDE_BUCKET_NAME_MAX_LENGTH = 200 @@ -40,24 +27,38 @@ export function claudeProjectBucketScopePrefixes(pathValue: string): string[] { ) } -/** Like isClaudeProjectDirInScope, but a root (`-`, `C--`) keeps its separator, so it prefixes every bucket. */ +/** Whether a bucket is a scope's own or one below it; see claudeProjectBucketNameBelowScope. */ export function isClaudeProjectBucketInScope( projectDirName: string, - scopePrefixes: ReadonlySet + scopePrefixes: Iterable ): boolean { for (const prefix of scopePrefixes) { - const isRoot = prefix === '-' || /^[A-Za-z]--$/.test(prefix) - if ( - isRoot - ? projectDirName.startsWith(prefix) - : isClaudeProjectDirInScope(projectDirName, [prefix]) - ) { + if (claudeProjectBucketNameBelowScope(projectDirName, prefix) !== null) { return true } } return false } +/** The encoded path below one scope prefix (`''` for the scope's own bucket), or null when the bucket is outside it. + * An encoded separator boundary keeps orca from matching its sibling orcadyne; a root (`-`, `C--`) keeps its separator. */ +export function claudeProjectBucketNameBelowScope( + projectDirName: string, + scopePrefix: string +): string | null { + if (projectDirName === scopePrefix) { + return '' + } + const isRoot = scopePrefix === '-' || /^[A-Za-z]--$/.test(scopePrefix) + const head = isRoot ? scopePrefix : `${scopePrefix}-` + return projectDirName.startsWith(head) ? projectDirName.slice(head.length) : null +} + +/** A name this long may be a `-` cut that no longer spells the path; legacy full names are this long too. */ +export function mayBeHashedClaudeProjectBucketName(projectDirName: string): boolean { + return projectDirName.length > CLAUDE_BUCKET_NAME_MAX_LENGTH +} + /** Transcripts sit in project buckets (depth 0); `/` folders below hold only subagent and tool output. */ export function isClaudeProjectBucketDirectory(_name: string, depth: number): boolean { return depth === 0 diff --git a/src/main/ai-vault/session-scanner-scope-discovery.ts b/src/main/ai-vault/session-scanner-scope-discovery.ts index 5aa3a3c0f1b..719b9428ddb 100644 --- a/src/main/ai-vault/session-scanner-scope-discovery.ts +++ b/src/main/ai-vault/session-scanner-scope-discovery.ts @@ -25,7 +25,7 @@ const TRANSCRIPT_EXTENSIONS = new Set(['.jsonl']) // Bucket encodings are lossy; retain Claude’s cache and verify other transcripts individually. const PROJECT_DIR_CWD_CACHE_MAX = 2048 const projectDirCwdCache = new Map() -export type IssueSink = (issue: AiVaultScanIssue) => void +export type IssueSink = (issue: AiVaultScanIssue, error?: unknown) => void export function resetProjectDirCwdCacheForTests(): void { projectDirCwdCache.clear() @@ -39,7 +39,7 @@ export function recordRefusal( agent: AiVaultAgent ): void { if (!isMissingTranscriptPathError(error)) { - issues({ agent, path, ...scanIssueTextFromError(error) }) + issues({ agent, path, ...scanIssueTextFromError(error) }, error) } } diff --git a/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts b/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts index f273f88949a..1f335efe069 100644 --- a/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts +++ b/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts @@ -1,9 +1,14 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { mkdir, mkdtemp, readdir, rm } from 'node:fs/promises' +import { mkdir, mkdtemp, readdir, rm, stat, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { MARINE_CREATURES } from '../../../../shared/marine-creatures' import { createRetiredNameLookup } from '../../../../shared/worktree/retired-name-registry' +import { encodeClaudeProjectPath } from '../../../ai-vault/scanners/claude/claude-project-dir-encoding' +import { + RETIREMENT_BACKFILL_RETRY_AFTER_FAILURE_MS, + runRetirementBackfillScan +} from './worktree-retirement-backfill-scan' const { runWslTranscriptFsTaskMock, getWslHomeAsyncMock } = vi.hoisted(() => ({ runWslTranscriptFsTaskMock: vi.fn(), @@ -26,6 +31,7 @@ const { ensureRetiredWorktreeNamesBackfilled } = await import('./worktree-name-r const { WslTranscriptFsError } = await import('../../../native-chat/wsl-transcript-fs-gate') const FIRST = MARINE_CREATURES[0].toLowerCase() +const SECOND = MARINE_CREATURES[1].toLowerCase() const DISTRO_ROOT = '\\\\wsl.localhost\\Ubuntu\\home\\ada\\orca\\workspaces' describe('retirement discovery on WSL', () => { @@ -138,6 +144,92 @@ describe('retirement discovery on WSL', () => { } }) + // A distro-root workspace folder encodes to the root prefix `-` on the Linux side. + it.each([ + ['a host bucket under the older \\\\wsl$\\ spelling', 'host', `--wsl--ubuntu-${FIRST}`], + ['a distro bucket', 'distro', `-${FIRST}`] + ])('keeps %s below a distro-root workspace folder spent', async (_case, side, bucket) => { + const homes = { + host: await mkdtemp(join(tmpdir(), 'orca-wsl-host-home-')), + distro: await mkdtemp(join(tmpdir(), 'orca-wsl-distro-home-')) + } + try { + await mkdir(join(homes[side], '.claude', 'projects', bucket), { recursive: true }) + + const retired = await discoverRetiredWorktreeNames({ + workspaceRoots: ['\\\\wsl.localhost\\Ubuntu'], + home: homes.host, + env: {}, + resolveWslHome: async () => homes.distro + }) + + expect(retired.names).toEqual(new Set([FIRST])) + } finally { + await rm(homes.host, { force: true, recursive: true }) + await rm(homes.distro, { force: true, recursive: true }) + } + }) + + // A refused permission stays refused on retry; a slow route may answer next time. + it.each([ + ['a permission refusal', Object.assign(new Error('EACCES'), { code: 'EACCES' }), 1], + ['a slow route', new WslTranscriptFsError('timeout', 'filesystem access is slow'), 2] + ])( + 'after %s on a long-path transcript, a later create rescans only if retrying can help', + async (_case, refusal, expectedProbes) => { + const distro = '\\\\wsl.localhost\\Ubuntu' + const longRoot = `/${'a'.repeat(229)}` + const distroDir = await mkdtemp(join(tmpdir(), 'orca-wsl-distro-')) + const hostHome = await mkdtemp(join(tmpdir(), 'orca-wsl-host-home-')) + try { + const projects = join(distroDir, 'home', 'ada', '.claude', 'projects') + await mkdir(join(distroDir, longRoot), { recursive: true }) + await mkdir(join(distroDir, 'home', 'ada', 'orca', 'workspaces'), { recursive: true }) + await mkdir(join(projects, `-home-ada-orca-workspaces-${FIRST}`), { recursive: true }) + const hashed = join( + projects, + `${encodeClaudeProjectPath(`${longRoot}/${SECOND}`).slice(0, 200)}-1x2y3z` + ) + await mkdir(hashed, { recursive: true }) + await writeFile(join(hashed, 'session.jsonl'), '{}\n') + let probes = 0 + runWslTranscriptFsTaskMock.mockImplementation( + async (options: { operation: string; path: string }) => { + if (options.path.endsWith('session.jsonl')) { + probes += 1 + throw refusal + } + const local = join(distroDir, options.path.slice(distro.length).replaceAll('\\', '/')) + return options.operation === 'readdir' + ? readdir(local, { withFileTypes: true }) + : stat(local) + } + ) + let now = 0 + vi.spyOn(performance, 'now').mockImplementation(() => now) + const store = {} + const create = () => + runRetirementBackfillScan(store, 'ns', () => + discoverRetiredWorktreeNames({ + workspaceRoots: [DISTRO_ROOT, `${distro}${longRoot}`], + home: hostHome, + env: {}, + resolveWslHome: async () => `${distro}\\home\\ada` + }) + ) + + await expect(create()).resolves.toEqual(new Set([FIRST])) + now += RETIREMENT_BACKFILL_RETRY_AFTER_FAILURE_MS + 1 + await expect(create()).resolves.toEqual(new Set([FIRST])) + + expect(probes).toBe(expectedProbes) + } finally { + await rm(distroDir, { force: true, recursive: true }) + await rm(hostHome, { force: true, recursive: true }) + } + } + ) + it('reaches distro discovery from the real backfill, not only when called directly', async () => { const distroHome = await mkdtemp(join(tmpdir(), 'orca-wsl-backfill-home-')) try { diff --git a/src/main/workspaces/removal/retirement/worktree-retirement-discovery.test.ts b/src/main/workspaces/removal/retirement/worktree-retirement-discovery.test.ts new file mode 100644 index 00000000000..c05caae9750 --- /dev/null +++ b/src/main/workspaces/removal/retirement/worktree-retirement-discovery.test.ts @@ -0,0 +1,47 @@ +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { MARINE_CREATURES } from '../../../../shared/marine-creatures' +import { encodeClaudeProjectPath } from '../../../ai-vault/scanners/claude/claude-project-dir-encoding' +import { discoverRetiredWorktreeNames } from './worktree-retirement-discovery' + +const FIRST = MARINE_CREATURES[0].toLowerCase() + +describe('retirement discovery of Claude buckets', () => { + let home: string + + beforeEach(async () => { + home = await mkdtemp(join(tmpdir(), 'orca-retire-claude-')) + }) + + afterEach(async () => { + await rm(home, { force: true, recursive: true }) + }) + + // Claude Code cuts a long bucket name to `-`; only the transcript cwd names the workspace. + async function addHashedBucket(cwd: string): Promise { + const bucket = join( + home, + '.claude', + 'projects', + `${encodeClaudeProjectPath(cwd).slice(0, 200)}-1x2y3z` + ) + await mkdir(bucket, { recursive: true }) + await writeFile(join(bucket, 'session.jsonl'), `${JSON.stringify({ type: 'user', cwd })}\n`) + } + + it.each([ + ['the cut falls inside the workspace name', 195, false], + ['the workspace root alone is longer than the cut', 230, false], + // Case-insensitive volumes let the transcript spell the same root differently. + ['the transcript spells the root in another case', 230, true] + ])('keeps a long workspace path spent when %s', async (_case, rootLength, upperCaseCwd) => { + const root = `/synthetic/${'a'.repeat(rootLength - '/synthetic/'.length)}` + await addHashedBucket(`${upperCaseCwd ? root.toUpperCase() : root}/${FIRST}`) + + const retired = await discoverRetiredWorktreeNames({ workspaceRoots: [root], home, env: {} }) + + expect(retired).toEqual({ names: new Set([FIRST]), complete: true }) + }) +}) diff --git a/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts b/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts index c51fe6e78ae..940318dc311 100644 --- a/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts +++ b/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts @@ -2,11 +2,19 @@ import { claudeProfileHistoryDirs } from '../../../claude-accounts/claude-profil import { homedir } from 'node:os' import { join } from 'node:path' import { isWslUncPath, parseWslUncPath } from '../../../../shared/wsl-paths' +import { relativePathInsideRoot } from '../../../../shared/cross-platform-path' import { - encodeClaudeProjectPaths, - isClaudeProjectDirInScope + claudeProjectBucketNameBelowScope, + mayBeHashedClaudeProjectBucketName } from '../../../ai-vault/scanners/claude/claude-project-dir-encoding' +import { CLAUDE_CWD_BUCKET_LAYOUT } from '../../../ai-vault/session-cwd-bucket-layouts' +import { + cachedProjectDirCwd, + cwdBucketScopePrefixes +} from '../../../ai-vault/session-scanner-scope-discovery' import { wslGatedReaddir, wslGatedStat } from '../../../native-chat/wsl-transcript-fs-access' +import { WslTranscriptFsError } from '../../../native-chat/wsl-transcript-fs-error' +import { mapWithConcurrency } from '../../../../shared/map-with-concurrency' import type { RetirementScanResult } from './worktree-retirement-backfill-scan' import { normalizeRetirableGeneratedName } from './worktree-name-retirement' import { getWslHomeAsync } from '../../../wsl/discovery/distro-catalog' @@ -29,21 +37,54 @@ export function collectRetiredNamesFromLeafNames(leafNames: Iterable): S /** Strip the encoded parent before extracting leaves; trailing-dash parsing can retire a numeric workspace's parent. */ export function extractBucketLeafCandidates( bucketName: string, - encodedParents: readonly string[] + encodedParents: Iterable ): string[] { const bucket = bucketName.toLowerCase() - for (const parent of encodedParents) { - if (!parent || bucket === parent || !isClaudeProjectDirInScope(bucket, [parent])) { - continue - } - const remainder = bucket.slice(parent.length + 1) + // Every matching parent counts: a root parent (`-`) also prefixes the bucket of a longer one. + return [...encodedParents].flatMap((parent) => { + const remainder = claudeProjectBucketNameBelowScope(bucket, parent) if (!remainder) { - continue + return [] } const firstSegment = remainder.split('-')[0] return remainder === firstSegment ? [remainder] : [remainder, firstSegment] + }) +} + +// Each probe lists a bucket and reads up to 3 transcripts. Host dirs overlap; the WSL gate still runs one scan task at a time. +const HASHED_BUCKET_PROBE_CONCURRENCY = 4 + +/** A permission refusal on one transcript or bucket stays refused on retry, so it must not keep the scan open. */ +function isPersistentFileRefusal(error: unknown): boolean { + const code = (error as NodeJS.ErrnoException | null)?.code + return !(error instanceof WslTranscriptFsError) && (code === 'EACCES' || code === 'EPERM') +} + +/** A `-` bucket name has lost the workspace name; its transcript cwd still has it. */ +async function hashedBucketLeafNames( + projectDir: string, + roots: readonly string[] +): Promise<{ names: string[]; refused: boolean }> { + let refused = false + const cwd = await cachedProjectDirCwd( + projectDir, + (_issue, error) => { + refused ||= !isPersistentFileRefusal(error) + }, + 'claude' + ) + if (!cwd) { + // Unresolved like a bucket with no cwd; only a retryable refusal leaves the scan incomplete. + return { names: [], refused } + } + // Compare paths, not names: /repo/my-app and /repo/my/app share a bucket name. Fold case like the bucket match. + const folded = cwd.toLowerCase() + return { + names: roots.flatMap( + (root) => relativePathInsideRoot(root.toLowerCase(), folded)?.split('/')[0] || [] + ), + refused } - return [] } /** Share WSL scan admission/deadlines; report refusals without discarding names readable from other sources. */ @@ -95,14 +136,18 @@ function getClaudeProjectsDir(home: string, env: NodeJS.ProcessEnv): string { type ClaudeProjectsSource = { projectsDir: string - encodedParents: string[] + /** Workspace roots as this source's Claude records its cwd. */ + roots: string[] + encodedParents: ReadonlySet } -// Fold both sides for case-insensitive filesystems; missed buckets risk reissuing used paths. -function encodeParents(paths: readonly string[]): string[] { - return paths - .filter((path) => path.length > 0) - .flatMap((path) => encodeClaudeProjectPaths(path).map((encoded) => encoded.toLowerCase())) +// Session History's bucket rules (long-path cut, roots, case, both WSL spellings); missed buckets risk reissuing used paths. +function claudeProjectsSource(projectsDir: string, roots: string[]): ClaudeProjectsSource { + return { + projectsDir, + roots, + encodedParents: cwdBucketScopePrefixes(CLAUDE_CWD_BUCKET_LAYOUT, roots) + } } /** WSL agents leave buckets in the distro home; only its default location is visible, not distro CLAUDE_CONFIG_DIR. */ @@ -114,13 +159,13 @@ async function claudeProjectsSources(args: { }): Promise<{ sources: ClaudeProjectsSource[]; complete: boolean }> { const sources: ClaudeProjectsSource[] = [] let complete = true - const hostParents = encodeParents(args.workspaceRoots) - if (hostParents.length > 0) { + const hostRoots = args.workspaceRoots.filter((root) => root.length > 0) + if (hostRoots.length > 0) { for (const projectsDir of new Set([ getClaudeProjectsDir(args.home ?? homedir(), args.env ?? process.env), ...claudeProfileHistoryDirs('projects') ])) { - sources.push({ projectsDir, encodedParents: hostParents }) + sources.push(claudeProjectsSource(projectsDir, hostRoots)) } } const linuxRootsByDistro = new Map() @@ -137,10 +182,7 @@ async function claudeProjectsSources(args: { for (const [distro, linuxRoots] of linuxRootsByDistro) { const distroHome = await resolveWslHome(distro) if (distroHome) { - sources.push({ - projectsDir: join(distroHome, '.claude', 'projects'), - encodedParents: encodeParents(linuxRoots) - }) + sources.push(claudeProjectsSource(join(distroHome, '.claude', 'projects'), linuxRoots)) continue } // Unresolved distro homes leave agent history unread, so discovery must remain retryable. @@ -170,8 +212,21 @@ export async function discoverRetiredWorktreeNames(args: { for (const source of bucketSources.sources) { const listing = await readDirectoryNames(source.projectsDir) refused ||= listing.refused + const hashedDirs: string[] = [] for (const bucket of listing.names) { - leafNames.push(...extractBucketLeafCandidates(bucket, source.encodedParents)) + const candidates = extractBucketLeafCandidates(bucket, source.encodedParents) + leafNames.push(...candidates) + // An in-scope hashed bucket always leaves a remainder (its hash), so it has candidates. + if (candidates.length > 0 && mayBeHashedClaudeProjectBucketName(bucket)) { + hashedDirs.push(join(source.projectsDir, bucket)) + } + } + const probes = await mapWithConcurrency(hashedDirs, HASHED_BUCKET_PROBE_CONCURRENCY, (dir) => + hashedBucketLeafNames(dir, source.roots) + ) + for (const probe of probes) { + leafNames.push(...probe.names) + refused ||= probe.refused } } From a353e6293e9523c78fe6ebd694d5c8dacbfbf39d Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:37:50 +0000 Subject: [PATCH 06/80] fix(session-history): show the last list at once after a restart while it rescans (#39380) * fix(session-history): show the last list at once after a restart while it rescans The renderer's list cache lived only in memory and deleted entries on every rescan or chat link change, so a restart or a forced rescan left the panel on a skeleton until the host finished scanning. Save each scope's last rows in IndexedDB (no message text), mark outdated entries stale instead of dropping them, and paint that list as refreshing until the rescan replaces it. Fixes #39308 * perf(session-history): skip the saved-list write when a rescan changed nothing * fix(session-history): skip unchanged saves across panels and keep a saved list through a cancelled publish * fix(session-history): evict saved lists by last use and save once the profile loads * test(session-history): drop an identity-only refresh test and correct the truncation comment * fix(session-history): let the React compiler handle the moved new-session rescan --- config/react-compiler-bailout-baseline.txt | 2 +- .../native-chat-composer-draft-indexeddb.ts | 64 ++--- .../scanning/ai-vault-applied-scan.ts | 27 +- .../scanning/ai-vault-new-session-rescan.ts | 59 +++++ .../ai-vault-saved-session-lists.test.ts | 235 +++++++++++++++++ .../scanning/ai-vault-saved-session-lists.ts | 245 ++++++++++++++++++ .../scanning/ai-vault-session-list-request.ts | 18 +- .../ai-vault-session-publication-gate.test.ts | 49 ++++ .../ai-vault-session-publication-gate.ts | 21 +- .../scanning/ai-vault-session-refresh.test.ts | 86 ++++++ .../scanning/ai-vault-session-refresh.ts | 114 +++----- .../scanning/ai-vault-session-result-cache.ts | 65 ++++- .../ai-vault-session-delete-action.ts | 2 + src/renderer/src/lib/indexeddb-connection.ts | 51 ++++ 14 files changed, 887 insertions(+), 151 deletions(-) create mode 100644 src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-new-session-rescan.ts create mode 100644 src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.test.ts create mode 100644 src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.ts create mode 100644 src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.test.ts create mode 100644 src/renderer/src/lib/indexeddb-connection.ts diff --git a/config/react-compiler-bailout-baseline.txt b/config/react-compiler-bailout-baseline.txt index 933ce4f0a05..b1a89fbefe0 100644 --- a/config/react-compiler-bailout-baseline.txt +++ b/config/react-compiler-bailout-baseline.txt @@ -309,7 +309,7 @@ 1 src/renderer/src/components/right-sidebar/ai-vault/panel/AiVaultPanel.tsx 2 src/renderer/src/components/right-sidebar/ai-vault/scanning/AiVaultScanProgressStrip.tsx 1 src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-association-observation.ts -9 src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.ts +8 src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.ts 1 src/renderer/src/components/right-sidebar/ai-vault/search/AiVaultPanelSearch.tsx 1 src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.ts 2 src/renderer/src/components/right-sidebar/ai-vault/session-actions/ai-vault-session-delete-action.ts diff --git a/src/renderer/src/components/native-chat/drafts/native-chat-composer-draft-indexeddb.ts b/src/renderer/src/components/native-chat/drafts/native-chat-composer-draft-indexeddb.ts index 744ea580cde..41ebdcefbd2 100644 --- a/src/renderer/src/components/native-chat/drafts/native-chat-composer-draft-indexeddb.ts +++ b/src/renderer/src/components/native-chat/drafts/native-chat-composer-draft-indexeddb.ts @@ -4,60 +4,28 @@ import type { NativeChatComposerDraftStorage, StoredNativeChatComposerDraft } from './native-chat-composer-draft-storage' +import { + committedTransaction, + indexedDbConnection, + settledRequest +} from '@/lib/indexeddb-connection' const DATABASE_NAME = 'orca-native-chat-composer-drafts' const DATABASE_VERSION = 1 const DRAFTS = 'drafts' -function settled(request: IDBRequest): Promise { - return new Promise((resolve, reject) => { - request.onsuccess = () => resolve(request.result) - request.onerror = () => reject(request.error) - }) -} - -/** Resolves once the transaction is on disk; rejects when it aborts (a full disk included). */ -function committed(transaction: IDBTransaction): Promise { - return new Promise((resolve, reject) => { - transaction.oncomplete = () => resolve() - transaction.onerror = () => reject(transaction.error) - transaction.onabort = () => reject(transaction.error ?? new Error('draft write aborted')) - }) -} - export function createIndexedDbNativeChatComposerDraftStorage( factory: IDBFactory ): NativeChatComposerDraftStorage { - let opened: Promise | null = null - const database = (): Promise => { - opened ??= new Promise((resolve, reject) => { - const request = factory.open(DATABASE_NAME, DATABASE_VERSION) - request.onupgradeneeded = () => { - if (!request.result.objectStoreNames.contains(DRAFTS)) { - request.result.createObjectStore(DRAFTS) - } - } - request.onsuccess = () => { - const db = request.result - // Close replaced/upgraded connections so later writes reopen instead of reusing a dead database. - db.onversionchange = () => db.close() - db.onclose = () => { - opened = null - } - resolve(db) - } - request.onerror = () => reject(request.error) - request.onblocked = () => reject(new Error('draft database is blocked')) - }).catch((error: unknown) => { - opened = null - throw error - }) - return opened - } + const database = indexedDbConnection(factory, DATABASE_NAME, DATABASE_VERSION, (db) => { + if (!db.objectStoreNames.contains(DRAFTS)) { + db.createObjectStore(DRAFTS) + } + }) const change = async (apply: (drafts: IDBObjectStore) => void): Promise => { const transaction = (await database()).transaction(DRAFTS, 'readwrite') - const done = committed(transaction) + const done = committedTransaction(transaction) apply(transaction.objectStore(DRAFTS)) // Why: commit now rather than when the task ends, so a quit right after still lands it. transaction.commit?.() @@ -68,8 +36,8 @@ export function createIndexedDbNativeChatComposerDraftStorage( loadAll: async () => { const drafts = (await database()).transaction(DRAFTS, 'readonly').objectStore(DRAFTS) const [keys, values] = await Promise.all([ - settled(drafts.getAllKeys()), - settled(drafts.getAll()) + settledRequest(drafts.getAllKeys()), + settledRequest(drafts.getAll()) ]) const loaded = new Map() if (Array.isArray(keys) && Array.isArray(values)) { @@ -82,7 +50,9 @@ export function createIndexedDbNativeChatComposerDraftStorage( return loaded }, read: async (scopeKey) => - settled((await database()).transaction(DRAFTS, 'readonly').objectStore(DRAFTS).get(scopeKey)), + settledRequest( + (await database()).transaction(DRAFTS, 'readonly').objectStore(DRAFTS).get(scopeKey) + ), write: (scopeKey, draft: StoredNativeChatComposerDraft) => change((drafts) => drafts.put(draft, scopeKey)), remove: (scopeKeys) => @@ -93,7 +63,7 @@ export function createIndexedDbNativeChatComposerDraftStorage( }), update: async (scopeKey, apply) => { const transaction = (await database()).transaction(DRAFTS, 'readwrite') - const done = committed(transaction) + const done = committedTransaction(transaction) const drafts = transaction.objectStore(DRAFTS) const read = drafts.get(scopeKey) // Why inside the read's callback: the write must land in the same transaction as the read. diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-applied-scan.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-applied-scan.ts index 39669f90f66..2cb35424e13 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-applied-scan.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-applied-scan.ts @@ -10,38 +10,29 @@ export type AiVaultAppliedScan = { scopeKey: string /** Workspace and Project rows hold only that scope, so they never stand in for another one. */ scopeOnly: boolean + /** The scope's last list, shown until its rescan answers. */ + stale?: true } export function useAppliedAiVaultScan(): { scan: AiVaultAppliedScan | null - applyScan: ( - published: AiVaultListResult, - limit: AiVaultSessionLimit, - scopeKey: string, - scopeOnly: boolean, - onApplied?: (result: AiVaultListResult) => void - ) => void + applyScan: (applied: AiVaultAppliedScan, onApplied?: (result: AiVaultListResult) => void) => void } { const [scan, setScan] = useState(null) const acceptedScanRef = useRef(null) // Preserve identity on unchanged republication so React still bails out. const applyScan = useCallback( - ( - published: AiVaultListResult, - limit: AiVaultSessionLimit, - scopeKey: string, - scopeOnly: boolean, - onApplied?: (result: AiVaultListResult) => void - ) => { + (applied: AiVaultAppliedScan, onApplied?: (result: AiVaultListResult) => void) => { const previous = acceptedScanRef.current - const result = reuseAiVaultListResult(previous?.result ?? null, published) + const result = reuseAiVaultListResult(previous?.result ?? null, applied.result) const next = previous !== null && previous.result === result && - previous.limit === limit && - previous.scopeKey === scopeKey + previous.limit === applied.limit && + previous.scopeKey === applied.scopeKey && + previous.stale === applied.stale ? previous - : { result, limit, scopeKey, scopeOnly } + : { ...applied, result } acceptedScanRef.current = next setScan(next) onApplied?.(result) diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-new-session-rescan.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-new-session-rescan.ts new file mode 100644 index 00000000000..0d52cda7f98 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-new-session-rescan.ts @@ -0,0 +1,59 @@ +import { useCallback, useEffect, useRef } from 'react' +import { useAppStore } from '@/store/index' +import { getAgentSessionIdsKey } from './ai-vault-agent-session-refresh-key' +import { claimAiVaultForcedRescan, aiVaultForcedRescanDelay } from './ai-vault-forced-rescan-budget' + +/** In-app launches never blur; rescan on new provider session IDs, avoiding frequent state transitions. */ +export function useAiVaultNewSessionRescan( + refresh: (args: { background: true; force: true }) => Promise +): void { + // The budget is shared across remounts so session creation cannot amplify full scans; a throttled + // rescan waits for budget rather than leaving new sessions invisible. + const forcedRescanTimerRef = useRef | null>(null) + const requestForcedRescan = useCallback(() => { + function attempt(): void { + if (claimAiVaultForcedRescan()) { + void refresh({ background: true, force: true }) + return + } + if (forcedRescanTimerRef.current !== null) { + return + } + forcedRescanTimerRef.current = setTimeout(() => { + forcedRescanTimerRef.current = null + attempt() + }, aiVaultForcedRescanDelay()) + } + attempt() + }, [refresh]) + + useEffect( + () => () => { + if (forcedRescanTimerRef.current !== null) { + clearTimeout(forcedRescanTimerRef.current) + forcedRescanTimerRef.current = null + } + }, + [] + ) + + const agentSessionIdsKey = useAppStore((s) => getAgentSessionIdsKey(s.agentStatusByPaneKey)) + const seenAgentSessionIdsRef = useRef | null>(null) + useEffect(() => { + const ids = agentSessionIdsKey === '' ? [] : agentSessionIdsKey.split('\n') + // The mount refresh already covers sessions live at mount time. + if (seenAgentSessionIdsRef.current === null) { + seenAgentSessionIdsRef.current = new Set(ids) + return + } + const seen = seenAgentSessionIdsRef.current + const freshIds = ids.filter((id) => !seen.has(id)) + if (freshIds.length === 0) { + return + } + for (const id of freshIds) { + seen.add(id) + } + requestForcedRescan() + }, [agentSessionIdsKey, requestForcedRescan]) +} diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.test.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.test.ts new file mode 100644 index 00000000000..2566ce2f079 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.test.ts @@ -0,0 +1,235 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { + AiVaultListResult, + AiVaultSession +} from '../../../../../../shared/ai-vault/contracts/ai-vault-types' + +const store = vi.hoisted(() => { + const listeners = new Set<(state: { activeOrcaProfileId: string | null }) => void>() + const state: { activeOrcaProfileId: string | null } = { activeOrcaProfileId: 'profile-1' } + return { + state, + setProfile: (id: string | null) => { + state.activeOrcaProfileId = id + listeners.forEach((listener) => listener(state)) + }, + useAppStore: { + getState: () => state, + subscribe: (listener: (next: typeof state) => void) => { + listeners.add(listener) + return () => listeners.delete(listener) + } + } + } +}) +vi.mock('@/store/index', () => ({ useAppStore: store.useAppStore })) + +type Handler = (() => void) | null +type Request = { result: unknown; onsuccess: Handler; onerror: Handler } +type Transaction = { + oncomplete: Handler + onerror: Handler + onabort: Handler + error: Error | null +} + +// A stand-in for the IndexedDB calls the module makes: one store, one savedAt index, commits on commit(). +function fakeIndexedDb(): { puts: unknown[]; refuseNextWrite: () => void } { + const rows = new Map() + const puts: unknown[] = [] + let refuse = false + const answer = (result: unknown): Request => { + const request: Request = { result, onsuccess: null, onerror: null } + queueMicrotask(() => request.onsuccess?.()) + return request + } + const database = { + objectStoreNames: { contains: () => false }, + createObjectStore: () => ({ createIndex: () => {} }), + transaction: () => { + const staged = new Map(rows) + const handlers: Transaction = { oncomplete: null, onerror: null, onabort: null, error: null } + const transaction = Object.assign(handlers, { + objectStore: () => ({ + get: (key: string) => answer(rows.get(key)), + put: (value: { savedAt: number }, key: string) => { + puts.push(value) + staged.set(key, value) + }, + delete: (key: string) => staged.delete(key), + index: () => ({ + getAllKeys: () => + answer([...staged].sort(([, a], [, b]) => a.savedAt - b.savedAt).map(([key]) => key)) + }) + }), + commit: () => + queueMicrotask(() => { + if (refuse) { + refuse = false + transaction.error = new Error('quota') + transaction.onabort?.() + return + } + rows.clear() + staged.forEach((value, key) => rows.set(key, value)) + transaction.oncomplete?.() + }) + }) + return transaction + } + } + vi.stubGlobal('indexedDB', { + open: () => { + const request: { result: unknown; onupgradeneeded: Handler; onsuccess: Handler } = { + result: database, + onupgradeneeded: null, + onsuccess: null + } + queueMicrotask(() => { + request.onupgradeneeded?.() + request.onsuccess?.() + }) + return request + } + }) + return { puts, refuseNextWrite: () => (refuse = true) } +} + +function listOf(sessions: AiVaultSession[]): AiVaultListResult { + return { sessions, issues: [], scannedAt: '2026-07-01T00:00:00.000Z' } +} + +function session(id: string): AiVaultSession { + return { + id, + executionHostId: 'local', + agent: 'claude', + sessionId: id, + title: id, + cwd: '/repo', + branch: null, + model: null, + filePath: `/sessions/${id}.jsonl`, + codexHome: null, + createdAt: null, + updatedAt: null, + modifiedAt: '2026-07-01T00:00:00.000Z', + messageCount: 2, + totalTokens: 0, + previewMessages: [ + { role: 'user', text: 'opening ask', timestamp: null }, + { role: 'assistant', text: 'latest reply', timestamp: null } + ], + firstUserPrompt: 'opening ask', + lastUserPrompt: 'opening ask', + queuedMessageCount: 0, + subagentTranscriptCount: 0, + resumeCommand: id, + subagent: null + } +} + +async function loadModule() { + vi.resetModules() + return import('./ai-vault-saved-session-lists') +} + +beforeEach(() => { + vi.useFakeTimers({ toFake: ['Date'] }) + store.state.activeOrcaProfileId = 'profile-1' +}) + +afterEach(() => { + vi.useRealTimers() + vi.unstubAllGlobals() +}) + +describe('saved Session History lists', () => { + it('keeps only the latest turn of each row and skips a rescan that changed nothing', async () => { + const { puts } = fakeIndexedDb() + const lists = await loadModule() + const result = listOf([session('a')]) + + await lists.saveAiVaultSessionList('workspace', 250, result) + await lists.saveAiVaultSessionList('workspace', 250, { ...result }) + // A remount or a return to the scope rescans into fresh copies of the same rows. + await lists.saveAiVaultSessionList('workspace', 250, listOf([session('a')])) + + expect(puts).toHaveLength(1) + const saved = await lists.readSavedAiVaultSessionList('workspace') + expect(saved?.limit).toBe(250) + expect(saved?.result.sessions).toEqual([ + expect.objectContaining({ + id: 'a', + previewMessages: [{ role: 'assistant', text: 'latest reply', timestamp: null }], + previewMessagesTruncated: true + }) + ]) + expect(saved?.result.sessions[0]).not.toHaveProperty('firstUserPrompt') + expect(saved?.result.sessions[0]).not.toHaveProperty('lastUserPrompt') + }) + + it('writes the same rows again after a refused write', async () => { + const { puts, refuseNextWrite } = fakeIndexedDb() + const lists = await loadModule() + const result = listOf([session('a')]) + + refuseNextWrite() + await lists.saveAiVaultSessionList('workspace', 250, result) + expect(await lists.readSavedAiVaultSessionList('workspace')).toBeNull() + + await lists.saveAiVaultSessionList('workspace', 250, result) + expect(puts).toHaveLength(2) + expect(await lists.readSavedAiVaultSessionList('workspace')).not.toBeNull() + }) + + it('writes the same rows again once newer lists pushed them out', async () => { + const { puts } = fakeIndexedDb() + const lists = await loadModule() + const result = listOf([session('a')]) + + await lists.saveAiVaultSessionList('workspace', 250, result) + for (let index = 0; index < 8; index++) { + vi.setSystemTime(Date.now() + 1) + await lists.saveAiVaultSessionList(`other-${index}`, 250, listOf([])) + } + expect(await lists.readSavedAiVaultSessionList('workspace')).toBeNull() + + await lists.saveAiVaultSessionList('workspace', 250, result) + expect(puts).toHaveLength(10) + expect(await lists.readSavedAiVaultSessionList('workspace')).not.toBeNull() + }) + + it('saves and reads once startup has loaded the profile', async () => { + fakeIndexedDb() + const lists = await loadModule() + store.state.activeOrcaProfileId = null + const save = lists.saveAiVaultSessionList('workspace', 250, listOf([session('a')])) + store.setProfile('profile-1') + await save + store.state.activeOrcaProfileId = null + + const read = lists.readSavedAiVaultSessionList('workspace') + store.setProfile('profile-1') + + expect((await read)?.result.sessions.map((row) => row.id)).toEqual(['a']) + }) + + it('keeps a list shown again without changes over lists last shown before it', async () => { + fakeIndexedDb() + const lists = await loadModule() + const result = listOf([session('a')]) + await lists.saveAiVaultSessionList('workspace', 250, result) + for (let index = 0; index < 7; index++) { + vi.setSystemTime(Date.now() + 1) + await lists.saveAiVaultSessionList(`other-${index}`, 250, listOf([])) + } + + await lists.saveAiVaultSessionList('workspace', 250, result) + vi.setSystemTime(Date.now() + 1) + await lists.saveAiVaultSessionList('newest', 250, listOf([])) + + expect(await lists.readSavedAiVaultSessionList('workspace')).not.toBeNull() + expect(await lists.readSavedAiVaultSessionList('other-0')).toBeNull() + }) +}) diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.ts new file mode 100644 index 00000000000..1947029598f --- /dev/null +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-saved-session-lists.ts @@ -0,0 +1,245 @@ +// Last list per scope, kept across restarts so History can paint it while the rescan runs. + +import type { + AiVaultListResult, + AiVaultSession +} from '../../../../../../shared/ai-vault/contracts/ai-vault-types' +import { latestSessionConversationTurn } from '../../../../../../shared/ai-vault/session-display/ai-vault-session-display' +import { useAppStore } from '@/store/index' +import { isWebClientLocation } from '@/lib/web-client-location' +import { + committedTransaction, + indexedDbConnection, + settledRequest +} from '@/lib/indexeddb-connection' +import { catalogRowsEqual } from '@/lib/catalog-reconciliation/catalog-row-reconciliation' +import type { AiVaultSessionLimit } from '../session-list/ai-vault-session-limit' + +const DATABASE_NAME = 'orca-ai-vault-session-lists' +const DATABASE_VERSION = 1 +const LISTS = 'lists' +const SAVED_AT = 'savedAt' +const MAX_SAVED_LISTS = 8 +// The saved copy only has to fill the first screens until the scan replaces it. +const MAX_SAVED_ROWS = 250 +// Bump when AiVaultSession changes shape, so rows saved by an older build are never painted. +const ROW_FORMAT = 1 +// Past this the scan has usually answered, and a saved list arriving later is dropped anyway. +const PROFILE_WAIT_MS = 5_000 + +export type SavedAiVaultSessionList = { limit: AiVaultSessionLimit; result: AiVaultListResult } + +type StoredList = { + format: typeof ROW_FORMAT + savedAt: number + limit: AiVaultSessionLimit + sessions: AiVaultSession[] +} + +// A web origin can be re-paired to another host, so it keeps nothing. +function savedListsAvailable(): boolean { + return typeof indexedDB !== 'undefined' && !isWebClientLocation() +} + +// Profiles share one IndexedDB, so each key names its profile. +function storageKey( + key: string, + profileId = useAppStore.getState().activeOrcaProfileId +): string | null { + return savedListsAvailable() && profileId ? JSON.stringify([profileId, key]) : null +} + +// After a restart the panel can mount, and even finish a scan, before startup has loaded the profile list. +function loadedProfileId(): Promise { + const current = useAppStore.getState().activeOrcaProfileId + if (current || !savedListsAvailable()) { + return Promise.resolve(current) + } + return new Promise((resolve) => { + const finish = (profileId: string | null) => { + clearTimeout(timer) + unsubscribe() + resolve(profileId) + } + const timer = setTimeout(() => finish(null), PROFILE_WAIT_MS) + const unsubscribe = useAppStore.subscribe((state) => { + if (state.activeOrcaProfileId) { + finish(state.activeOrcaProfileId) + } + }) + }) +} + +let connection: (() => Promise) | null = null +// What each stored key last wrote, so a rescan that changed nothing (every focus refresh) skips the +// copy and the write. Only the saved rows are kept, never the whole list. +const lastSaved = new Map() +// Save order this run. A skipped save still counts as use, so eviction stays least recently shown. +const lastUse = new Map() +let uses = 0 + +function unchangedSinceSave( + stored: string, + limit: AiVaultSessionLimit, + rows: readonly AiVaultSession[] +): boolean { + const last = lastSaved.get(stored) + if (!last || last.limit !== limit) { + return false + } + // The panel's rows keep their identity across unchanged rescans, so this is usually a pointer walk. + if (last.rows.length === rows.length && last.rows.every((row, index) => row === rows[index])) { + return true + } + // A remount or a return to this scope brings fresh copies; comparing them costs far less than a write. + if (!catalogRowsEqual(last.rows, rows)) { + return false + } + lastSaved.set(stored, { limit, rows }) + return true +} + +function database(): Promise { + connection ??= indexedDbConnection(indexedDB, DATABASE_NAME, DATABASE_VERSION, (db) => { + if (!db.objectStoreNames.contains(LISTS)) { + db.createObjectStore(LISTS).createIndex(SAVED_AT, SAVED_AT) + } + }) + return connection() +} + +// Rows keep only what the list shows: the latest turn, not the conversation or prompts. +function savedRow(session: AiVaultSession): AiVaultSession { + const { + previewMessages: _previewMessages, + previewMessagesTruncated: _previewMessagesTruncated, + firstUserPrompt: _firstUserPrompt, + lastUserPrompt: _lastUserPrompt, + antigravityOpeningPrompt: _antigravityOpeningPrompt, + ...row + } = session + const latest = latestSessionConversationTurn(session) + // Marked truncated, as the row contract asks, because the opening ask is no longer in the window. + return latest + ? { ...row, previewMessages: [latest], previewMessagesTruncated: true } + : { ...row, previewMessages: [] } +} + +function isStoredList(value: unknown): value is StoredList { + return ( + typeof value === 'object' && + value !== null && + 'format' in value && + value.format === ROW_FORMAT && + 'savedAt' in value && + typeof value.savedAt === 'number' && + 'limit' in value && + (value.limit === 'unlimited' || typeof value.limit === 'number') && + 'sessions' in value && + Array.isArray(value.sessions) + ) +} + +export async function readSavedAiVaultSessionList( + key: string +): Promise { + const stored = storageKey(key, await loadedProfileId()) + if (!stored) { + return null + } + try { + const value: unknown = await settledRequest( + (await database()).transaction(LISTS, 'readonly').objectStore(LISTS).get(stored) + ) + if (!isStoredList(value)) { + return null + } + const scannedAt = new Date(value.savedAt).toISOString() + return { limit: value.limit, result: { sessions: value.sessions, issues: [], scannedAt } } + } catch { + return null + } +} + +export async function saveAiVaultSessionList( + key: string, + limit: AiVaultSessionLimit, + result: AiVaultListResult +): Promise { + const stored = storageKey(key, await loadedProfileId()) + if (!stored) { + return + } + // Host lists are newest first, so the cut keeps the rows the panel opens on. + const rows = result.sessions.slice(0, MAX_SAVED_ROWS) + // A cut list covers no deeper than its rows. + const savedLimit = + result.sessions.length > MAX_SAVED_ROWS && (limit === 'unlimited' || limit > MAX_SAVED_ROWS) + ? MAX_SAVED_ROWS + : limit + lastUse.set(stored, ++uses) + if (unchangedSinceSave(stored, savedLimit, rows)) { + return + } + const list: StoredList = { + format: ROW_FORMAT, + savedAt: Date.now(), + limit: savedLimit, + sessions: rows.map(savedRow) + } + try { + const transaction = (await database()).transaction(LISTS, 'readwrite') + const done = committedTransaction(transaction) + const lists = transaction.objectStore(LISTS) + lists.put(list, stored) + // The index orders keys by write time; the stable sort then puts this run's uses last, in order. + const keys = lists.index(SAVED_AT).getAllKeys() + keys.onsuccess = () => { + const byUse = keys.result + .map(String) + .sort((a, b) => (lastUse.get(a) ?? 0) - (lastUse.get(b) ?? 0)) + for (const oldest of byUse.slice(0, Math.max(0, byUse.length - MAX_SAVED_LISTS))) { + lists.delete(oldest) + // Evicted keys must write again, even when their rows haven't changed. + lastSaved.delete(oldest) + lastUse.delete(oldest) + } + transaction.commit?.() + } + await done + lastSaved.set(stored, { limit: savedLimit, rows }) + } catch { + // A refused write only costs the next restart its instant list; the next scan tries again. + } +} + +/** Drops a deleted session from every saved list so no scope repaints it before its rescan. */ +export async function forgetSavedAiVaultSession(sessionId: string): Promise { + if (!savedListsAvailable()) { + return + } + // Whatever lists held the row are rewritten, so the next scan of each scope saves again. + lastSaved.clear() + try { + const transaction = (await database()).transaction(LISTS, 'readwrite') + const done = committedTransaction(transaction) + const cursorRequest = transaction.objectStore(LISTS).openCursor() + cursorRequest.onsuccess = () => { + const cursor = cursorRequest.result + if (!cursor) { + return + } + const value: unknown = cursor.value + if (isStoredList(value) && value.sessions.some((session) => session.id === sessionId)) { + cursor.update({ + ...value, + sessions: value.sessions.filter((session) => session.id !== sessionId) + }) + } + cursor.continue() + } + await done + } catch { + // The next rescan of each scope drops the row anyway. + } +} diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-list-request.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-list-request.ts index 0f6758bf179..ace3b702f1a 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-list-request.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-list-request.ts @@ -10,8 +10,10 @@ import type { AiVaultSessionLimit } from '../session-list/ai-vault-session-limit import { aiVaultSessionResultCacheKey, cacheAiVaultSessionResult, - readCachedAiVaultSessionResult + readCachedAiVaultSessionResult, + readLastAiVaultSessionResult } from './ai-vault-session-result-cache' +import type { SavedAiVaultSessionList } from './ai-vault-saved-session-lists' /** Match panel requests so readers share host/renderer caches instead of evicting one another. */ export type AiVaultSessionListRequest = { @@ -52,6 +54,20 @@ export function readCachedAiVaultSessionList( }) } +export function readLastAiVaultSessionList( + request: AiVaultSessionListRequest +): Promise { + return readLastAiVaultSessionResult({ + key: aiVaultSessionResultCacheKey( + request.executionHostScope, + request.scopePaths, + request.scopeOnly + ), + limit: request.sessionLimit, + scopePaths: request.scopePaths + }) +} + export function cacheAiVaultSessionList( request: AiVaultSessionListRequest, result: AiVaultListResult, diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.test.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.test.ts new file mode 100644 index 00000000000..f01e17da20b --- /dev/null +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.test.ts @@ -0,0 +1,49 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { + markInputQuietSchedulerInput, + resetInputQuietSchedulerForTest +} from '@/lib/input-quiet-scheduler' +import type { AiVaultListResult } from '../../../../../../shared/ai-vault/contracts/ai-vault-types' +import { AiVaultSessionPublicationGate } from './ai-vault-session-publication-gate' + +const FIRST: AiVaultListResult = { sessions: [], issues: [], scannedAt: '2026-08-09T00:00:00.000Z' } +const SECOND: AiVaultListResult = { ...FIRST, scannedAt: '2026-08-09T00:00:01.000Z' } + +afterEach(() => { + vi.useRealTimers() + resetInputQuietSchedulerForTest() +}) + +describe('AiVaultSessionPublicationGate', () => { + // A chat-link snapshot right after a restart cancels the gate; it must not drop the saved list. + it('shows a late saved list after a cancel but never over a newer published list', async () => { + const shown = vi.fn() + const gate = new AiVaultSessionPublicationGate() + gate.showUntilPublished(Promise.resolve(FIRST), shown) + gate.cancel() + await Promise.resolve() + expect(shown).toHaveBeenCalledWith(FIRST) + + const late = vi.fn() + gate.showUntilPublished(Promise.resolve(FIRST), late) + gate.publish(SECOND, vi.fn()) + await Promise.resolve() + expect(late).not.toHaveBeenCalled() + }) + + it('still shows a late saved list when the list published meanwhile was cancelled before it landed', async () => { + vi.useFakeTimers() + const shown = vi.fn() + const published = vi.fn() + const gate = new AiVaultSessionPublicationGate() + gate.showUntilPublished(Promise.resolve(FIRST), shown) + markInputQuietSchedulerInput() + gate.publish(SECOND, published) + gate.cancel() + await Promise.resolve() + vi.advanceTimersByTime(1_000) + + expect(published).not.toHaveBeenCalled() + expect(shown).toHaveBeenCalledWith(FIRST) + }) +}) diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.ts index 7cddac6fc7f..cf23b16c5d5 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-publication-gate.ts @@ -9,11 +9,28 @@ export class AiVaultSessionPublicationGate { private generation = 0 private cancelPending: (() => void) | null = null + private published = 0 + + /** Applies a list that arrives later unless a list was published meanwhile; cancels don't drop it. */ + showUntilPublished(pending: Promise, apply: (value: T) => void): void { + const published = this.published + void pending.then((value) => { + if (value !== null && published === this.published) { + startTransition(() => apply(value)) + } + }) + } + publish(result: AiVaultListResult, apply: (result: AiVaultListResult) => void): void { this.cancel() const generation = this.generation - if (hasInputBeenQuietFor(AI_VAULT_PUBLICATION_QUIET_MS)) { + // Counted when it lands: a cancelled publication must not drop a saved list still on its way. + const land = () => { + this.published += 1 startTransition(() => apply(result)) + } + if (hasInputBeenQuietFor(AI_VAULT_PUBLICATION_QUIET_MS)) { + land() return } this.cancelPending = scheduleAfterInputQuiet( @@ -22,7 +39,7 @@ export class AiVaultSessionPublicationGate { return } this.cancelPending = null - startTransition(() => apply(result)) + land() }, { delayMs: 0, diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.test.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.test.ts index 89eb1fccb6f..46215308a4b 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.test.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.test.ts @@ -20,6 +20,7 @@ import { DEFAULT_AI_VAULT_SESSION_LIMIT, type AiVaultSessionLimit } from '../session-list/ai-vault-session-limit' +import { resetAiVaultSessionResultCacheForTest } from './ai-vault-session-result-cache' const EMPTY_RESULT: AiVaultListResult = { sessions: [], @@ -27,6 +28,25 @@ const EMPTY_RESULT: AiVaultListResult = { scannedAt: '2026-07-01T00:00:00.000Z' } +// Stands in for IndexedDB, which happy-dom lacks; it outlives the in-memory cache like a restart does. +const savedLists = vi.hoisted(() => { + const state: { + lists: Map + read: ((key: string) => Promise) | null + } = { lists: new Map(), read: null } + return state +}) +vi.mock('./ai-vault-saved-session-lists', () => ({ + readSavedAiVaultSessionList: async (key: string) => { + const result = await (savedLists.read?.(key) ?? savedLists.lists.get(key) ?? null) + return result && { limit: 250, result } + }, + saveAiVaultSessionList: async (key: string, _limit: unknown, result: AiVaultListResult) => { + savedLists.lists.set(key, result) + }, + forgetSavedAiVaultSession: async () => {} +})) + const listSessionsMock = vi.fn<(args: AiVaultListArgs) => Promise>() const cancelListSessionsMock = vi.fn<(args: { requestToken: string }) => Promise>() @@ -156,6 +176,8 @@ beforeEach(() => { } } resetAiVaultForcedRescanThrottleForTest() + savedLists.lists.clear() + savedLists.read = null useAppStore.setState(initialAppState, true) }) @@ -294,4 +316,68 @@ describe('useAiVaultSessionRefresh refocus behavior', () => { }) }) +function deferredList(): { + promise: Promise + resolve: (result: AiVaultListResult) => void +} { + let resolve: (result: AiVaultListResult) => void = () => {} + const promise = new Promise((done) => (resolve = done)) + return { promise, resolve } +} + +async function restartWithScanPending(): Promise<(result: AiVaultListResult) => void> { + listSessionsMock.mockResolvedValueOnce({ + ...EMPTY_RESULT, + sessions: [makeVaultSession(1), makeVaultSession(2)] + }) + await renderHook(['/repo'], 'local', DEFAULT_AI_VAULT_SESSION_LIMIT, true) + await flushMicrotasks() + expect(latest?.sessions.map((session) => session.id)).toEqual(['session-1', 'session-2']) + roots.splice(0).forEach((root) => act(() => root.unmount())) + resetAiVaultSessionResultCacheForTest() + const scan = deferredList() + listSessionsMock.mockReturnValueOnce(scan.promise) + return scan.resolve +} + +describe('useAiVaultSessionRefresh after a restart', () => { + it('shows the last list as refreshing until the rescan replaces it', async () => { + const resolveScan = await restartWithScanPending() + + await renderHook(['/repo'], 'local', DEFAULT_AI_VAULT_SESSION_LIMIT, true) + await flushMicrotasks() + + expect(latest?.sessions.map((session) => session.id)).toEqual(['session-1', 'session-2']) + expect(latest?.loading).toBe(true) + expect(latest?.scopePending).toBe(true) + + await act(async () => { + resolveScan({ ...EMPTY_RESULT, sessions: [makeVaultSession(2)] }) + }) + await flushMicrotasks() + + expect(latest?.sessions.map((session) => session.id)).toEqual(['session-2']) + expect(latest?.loading).toBe(false) + }) + + it('keeps the rescan when the saved list answers after it', async () => { + const resolveScan = await restartWithScanPending() + const read = deferredList() + savedLists.read = () => read.promise + + await renderHook(['/repo'], 'local', DEFAULT_AI_VAULT_SESSION_LIMIT, true) + await act(async () => { + resolveScan({ ...EMPTY_RESULT, sessions: [makeVaultSession(3)] }) + }) + await flushMicrotasks() + await act(async () => { + read.resolve({ ...EMPTY_RESULT, sessions: [makeVaultSession(1)] }) + }) + await flushMicrotasks() + + expect(latest?.sessions.map((session) => session.id)).toEqual(['session-3']) + expect(latest?.loading).toBe(false) + }) +}) + // Plain HTTP hides crypto.randomUUID; fallback tokens must remain v4 UUIDs (#18096). diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.ts index cc67e934686..b5d482f2675 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-refresh.ts @@ -2,7 +2,6 @@ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react import { aiVaultScanErrorText } from './ai-vault-scan-issue-copy' import type { ExecutionHostScope } from '../../../../../../shared/execution-host' -import { useAppStore } from '@/store/index' import type { AiVaultSessionLimit } from '../session-list/ai-vault-session-limit' import { AiVaultSessionPublicationGate } from './ai-vault-session-publication-gate' import { EMPTY_AI_VAULT_SESSIONS } from '../session-list/ai-vault-session-identity' @@ -16,19 +15,16 @@ import { refreshAiVaultSessionList, type AiVaultConditionalSnapshot, cacheAiVaultSessionList, - readCachedAiVaultSessionList + readCachedAiVaultSessionList, + readLastAiVaultSessionList } from './ai-vault-session-list-request' -import { - getAgentSessionIdsKey, - resetAgentSessionIdsRefreshKeys -} from './ai-vault-agent-session-refresh-key' +import { resetAgentSessionIdsRefreshKeys } from './ai-vault-agent-session-refresh-key' +import { useAiVaultNewSessionRescan } from './ai-vault-new-session-rescan' +import { saveAiVaultSessionList } from './ai-vault-saved-session-lists' import { useAiVaultAssociationObservation } from './ai-vault-association-observation' import { createNonSecureContextUuid as createBrowserUuid } from '../../../../../../shared/non-secure-context-uuid' - -// Share the forced-rescan budget across remounts so session creation cannot amplify full scans. import { claimAiVaultForcedRescan, - aiVaultForcedRescanDelay, resetAiVaultForcedRescanBudgetForTest } from './ai-vault-forced-rescan-budget' import { @@ -121,24 +117,43 @@ export function useAiVaultSessionRefresh( executionHostScope: hostScope, sessionLimit: selectedLimit } - const baseKey = aiVaultSessionResultCacheKey(hostScope, request.scopePaths, request.scopeOnly) - const cachedResult = - args.reuseLoadedDepth === true ? readCachedAiVaultSessionList(request) : null - if (cachedResult) { - const scanKey = `${baseKey}\n${selectedLimit}` - setFailure(null) - setTerminalScopeKey(null) - publicationGateRef.current.publish(cachedResult, (published) => { - if (!mountedRef.current || scanKey !== currentScanScopeKey() || !associationCurrent()) { - return - } - applyScan(published, selectedLimit, scanKey, request.scopeOnly, (applied) => { + const { scopeOnly } = request + const baseKey = aiVaultSessionResultCacheKey(hostScope, request.scopePaths, scopeOnly) + const scanKey = `${baseKey}\n${selectedLimit}` + const applyListed = (published: AiVaultListResult, scanned = false) => + applyScan( + { result: published, limit: selectedLimit, scopeKey: scanKey, scopeOnly }, + (applied) => { conditionalSnapshotRef.current = published.fingerprint ? { scopeKey: scanKey, fingerprint: published.fingerprint, result: applied } : null - }) + // The applied rows keep their identity when a rescan changed nothing, so the save skips. + if (scanned) { + void saveAiVaultSessionList(baseKey, selectedLimit, applied) + } + } + ) + const cachedResult = + args.reuseLoadedDepth === true ? readCachedAiVaultSessionList(request) : null + if (cachedResult) { + setFailure(null) + setTerminalScopeKey(null) + publicationGateRef.current.publish(cachedResult, (published) => { + if (mountedRef.current && scanKey === currentScanScopeKey() && associationCurrent()) { + applyListed(published) + } }) setLoading(false) + } else if (args.reuseLoadedDepth === true) { + publicationGateRef.current.showUntilPublished( + readLastAiVaultSessionList(request), + // Chat links only change which rows are linked; the stale list is replaced by the rescan anyway. + ({ result, limit }) => { + if (mountedRef.current && scanKey === currentScanScopeKey()) { + applyScan({ result, limit, scopeKey: scanKey, scopeOnly, stale: true }) + } + } + ) } // Queue scope changes during an in-flight scan so the new view still refreshes. if (refreshInFlightRef.current) { @@ -165,7 +180,6 @@ export function useAiVaultSessionRefresh( setLoading(true) } setTerminalScopeKey(null) - const scanKey = `${baseKey}\n${selectedLimit}` const requestCurrent = () => mountedRef.current && refreshIdRef.current === refreshId && @@ -190,11 +204,7 @@ export function useAiVaultSessionRefresh( cacheAiVaultSessionList(request, result, { replaceHostEntries: args.force === true }) publicationGateRef.current.publish(result, (published) => { if (requestCurrent()) { - applyScan(published, selectedLimit, scanKey, request.scopeOnly, (applied) => { - conditionalSnapshotRef.current = published.fingerprint - ? { scopeKey: scanKey, fingerprint: published.fingerprint, result: applied } - : null - }) + applyListed(published, true) } }) } catch (err) { @@ -228,23 +238,6 @@ export function useAiVaultSessionRefresh( [applyScan, currentScanScopeKey] ) - // Defer throttled new-session rescans until budget frees so sessions cannot remain invisible indefinitely. - const forcedRescanTimerRef = useRef | null>(null) - const requestForcedRescan = useCallback(() => { - if (claimAiVaultForcedRescan()) { - void refresh({ background: true, force: true }) - return - } - const waitMs = aiVaultForcedRescanDelay() - if (forcedRescanTimerRef.current !== null) { - return - } - forcedRescanTimerRef.current = setTimeout(() => { - forcedRescanTimerRef.current = null - requestForcedRescan() - }, waitMs) - }, [refresh]) - useEffect(() => { mountedRef.current = true const requestToken = requestTokenRef.current @@ -260,14 +253,10 @@ export function useAiVaultSessionRefresh( void window.api.aiVault.cancelListSessions({ requestToken }) - if (forcedRescanTimerRef.current !== null) { - clearTimeout(forcedRescanTimerRef.current) - forcedRescanTimerRef.current = null - } } }, []) - // Panel entry reuses the renderer result first, then the host scan cache. + // Panel entry reuses the renderer result, else paints the last list while the host answers. useEffect(() => { publicationGateRef.current.cancel() pendingRefreshRef.current = false @@ -294,28 +283,11 @@ export function useAiVaultSessionRefresh( // Retry prior relay-readiness errors on host events; otherwise startup/reconnect failures can persist indefinitely. useAiVaultSessionHostRefreshEvents(error, refresh) - // In-app launches never blur; rescan on new provider session IDs, avoiding frequent state transitions. - const agentSessionIdsKey = useAppStore((s) => getAgentSessionIdsKey(s.agentStatusByPaneKey)) - const seenAgentSessionIdsRef = useRef | null>(null) - useEffect(() => { - const ids = agentSessionIdsKey === '' ? [] : agentSessionIdsKey.split('\n') - // The mount refresh already covers sessions live at mount time. - if (seenAgentSessionIdsRef.current === null) { - seenAgentSessionIdsRef.current = new Set(ids) - return - } - const seen = seenAgentSessionIdsRef.current - const freshIds = ids.filter((id) => !seen.has(id)) - if (freshIds.length === 0) { - return - } - for (const id of freshIds) { - seen.add(id) - } - requestForcedRescan() - }, [agentSessionIdsKey, requestForcedRescan]) + useAiVaultNewSessionRescan(refresh) - const scopePending = scan?.scopeKey !== scanScopeKey && terminalScopeKey !== scanScopeKey + // A stale list keeps the scope pending, so it shows as refreshing until the rescan answers. + const scopePending = + (scan?.scopeKey !== scanScopeKey || scan?.stale === true) && terminalScopeKey !== scanScopeKey return { error, loading: loading || scopePending, diff --git a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-result-cache.ts b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-result-cache.ts index 256f60fd512..0e11209082f 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-result-cache.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/scanning/ai-vault-session-result-cache.ts @@ -2,9 +2,15 @@ import type { AiVaultListResult } from '../../../../../../shared/ai-vault/contra import type { ExecutionHostScope } from '../../../../../../shared/execution-host' import { aiVaultCachedDepthServes, + aiVaultSessionDepthCovers, truncateAiVaultListResult } from '../../../../../../shared/ai-vault/scanning/ai-vault-session-depth' import type { AiVaultSessionLimit } from '../session-list/ai-vault-session-limit' +import { + forgetSavedAiVaultSession, + readSavedAiVaultSessionList, + type SavedAiVaultSessionList +} from './ai-vault-saved-session-lists' const MAX_CACHED_SESSION_SCOPES = 8 @@ -12,6 +18,8 @@ type CachedSessionResult = { executionHostScope: ExecutionHostScope limit: AiVaultSessionLimit result: AiVaultListResult + /** Outdated by a rescan or a chat link change: shown while refreshing, never reused as current. */ + stale: boolean } const cachedSessionResults = new Map() @@ -35,7 +43,11 @@ export function readCachedAiVaultSessionResult(args: { scopePaths: readonly string[] }): AiVaultListResult | null { const cached = cachedSessionResults.get(args.key) - if (!cached || !aiVaultCachedDepthServes(cached.limit, args.limit, args.scopePaths)) { + if ( + !cached || + cached.stale || + !aiVaultCachedDepthServes(cached.limit, args.limit, args.scopePaths) + ) { return null } cachedSessionResults.delete(args.key) @@ -43,6 +55,24 @@ export function readCachedAiVaultSessionResult(args: { return truncateAiVaultListResult(cached.result, args.limit, args.scopePaths) } +/** The last list this scope showed, current or not, from this run or a previous one, at the depth it holds. */ +export async function readLastAiVaultSessionResult(args: { + key: string + limit: AiVaultSessionLimit + scopePaths: readonly string[] +}): Promise { + const last = cachedSessionResults.get(args.key) ?? (await readSavedAiVaultSessionList(args.key)) + if (!last) { + return null + } + return aiVaultSessionDepthCovers(last.limit, args.limit) + ? { + limit: args.limit, + result: truncateAiVaultListResult(last.result, args.limit, args.scopePaths) + } + : { limit: last.limit, result: last.result } +} + export function cacheAiVaultSessionResult(args: { key: string executionHostScope: ExecutionHostScope @@ -52,17 +82,14 @@ export function cacheAiVaultSessionResult(args: { replaceHostEntries: boolean }): void { if (args.replaceHostEntries) { - for (const [key, cached] of cachedSessionResults) { - if (cached.executionHostScope === args.executionHostScope) { - cachedSessionResults.delete(key) - } - } + markAiVaultSessionResultsStale((host) => host === args.executionHostScope) } cachedSessionResults.delete(args.key) cachedSessionResults.set(args.key, { executionHostScope: args.executionHostScope, limit: args.limit, - result: args.result + result: args.result, + stale: false }) while (cachedSessionResults.size > MAX_CACHED_SESSION_SCOPES) { const oldestKey = cachedSessionResults.keys().next().value @@ -77,10 +104,26 @@ export function resetAiVaultSessionResultCacheForTest(): void { cachedSessionResults.clear() } -export function invalidateAiVaultSessionResults(host: ExecutionHostScope): void { - for (const [key, entry] of cachedSessionResults) { - if (entry.executionHostScope === host || entry.executionHostScope === 'all') { - cachedSessionResults.delete(key) +function markAiVaultSessionResultsStale(matches: (host: ExecutionHostScope) => boolean): void { + for (const entry of cachedSessionResults.values()) { + if (matches(entry.executionHostScope)) { + entry.stale = true } } } + +export function invalidateAiVaultSessionResults(host: ExecutionHostScope): void { + markAiVaultSessionResultsStale((entry) => entry === host || entry === 'all') +} + +/** Drops a deleted session from every kept list, so no scope paints it while it rescans. */ +export function forgetAiVaultSessionResult(sessionId: string): void { + for (const entry of cachedSessionResults.values()) { + const sessions = entry.result.sessions.filter((session) => session.id !== sessionId) + if (sessions.length !== entry.result.sessions.length) { + entry.result = { ...entry.result, sessions, fingerprint: undefined } + entry.stale = true + } + } + void forgetSavedAiVaultSession(sessionId) +} diff --git a/src/renderer/src/components/right-sidebar/ai-vault/session-actions/ai-vault-session-delete-action.ts b/src/renderer/src/components/right-sidebar/ai-vault/session-actions/ai-vault-session-delete-action.ts index ae78a97e420..7fb06d2e319 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/session-actions/ai-vault-session-delete-action.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/session-actions/ai-vault-session-delete-action.ts @@ -9,6 +9,7 @@ import { runtimeEnvironmentSupportsCapability } from '@/runtime/rpc/compatibilit import { lastVerifiedRuntimeStatus } from '../../../../../../shared/remote-runtime/host-status/runtime-host-status' import { resolveContinuedRuntimeEnvironmentRevision } from '@/runtime/rpc/environment-revisions/runtime-environment-revision' import { useAppStore } from '@/store/index' +import { forgetAiVaultSessionResult } from '../scanning/ai-vault-session-result-cache' import '../session-list/ai-vault-session-filters' import { agentLabel } from '../../../../../../shared/ai-vault/session-display/ai-vault-session-filters' import { @@ -135,6 +136,7 @@ export function useAiVaultSessionDeleteAction({ // Use one failure message rather than exposing raw main-side reasons. throw new Error(result.outcome) } + forgetAiVaultSessionResult(session.id) onDeleted?.(session) toast.success( translate('auto.components.right.sidebar.AiVaultPanel.sessionDeleted', 'Session deleted') diff --git a/src/renderer/src/lib/indexeddb-connection.ts b/src/renderer/src/lib/indexeddb-connection.ts new file mode 100644 index 00000000000..f085461ddf9 --- /dev/null +++ b/src/renderer/src/lib/indexeddb-connection.ts @@ -0,0 +1,51 @@ +// One cached IndexedDB connection per database, reopened after it closes, fails or is upgraded elsewhere. + +export function indexedDbConnection( + factory: IDBFactory, + name: string, + version: number, + upgrade: (database: IDBDatabase) => void +): () => Promise { + let opened: Promise | null = null + return () => { + opened ??= new Promise((resolve, reject) => { + const request = factory.open(name, version) + request.onupgradeneeded = () => upgrade(request.result) + request.onsuccess = () => { + const db = request.result + db.onversionchange = () => { + // An explicit close fires no close event, so forget the handle here too. + opened = null + db.close() + } + db.onclose = () => { + opened = null + } + resolve(db) + } + request.onerror = () => reject(request.error) + request.onblocked = () => reject(new Error(`${name} database is blocked`)) + }).catch((error: unknown) => { + opened = null + throw error + }) + return opened + } +} + +export function settledRequest(request: IDBRequest): Promise { + return new Promise((resolve, reject) => { + request.onsuccess = () => resolve(request.result) + request.onerror = () => reject(request.error) + }) +} + +/** Resolves once the transaction is on disk; rejects when it aborts (a full disk included). */ +export function committedTransaction(transaction: IDBTransaction): Promise { + return new Promise((resolve, reject) => { + transaction.oncomplete = () => resolve() + transaction.onerror = () => reject(transaction.error) + transaction.onabort = () => + reject(transaction.error ?? new Error('IndexedDB transaction aborted')) + }) +} From d5aa74110d5f7384b5091efc8d61ac793d40a002 Mon Sep 17 00:00:00 2001 From: "buf0-bot[bot]" <252831055+buf0-bot[bot]@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:42:33 +0000 Subject: [PATCH 07/80] Update README downloads badge (#40045) Co-authored-by: buf0-bot[bot] <252831055+buf0-bot[bot]@users.noreply.github.com> --- docs/assets/readme-downloads.svg | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/assets/readme-downloads.svg b/docs/assets/readme-downloads.svg index 7cb6662503d..844b8971f54 100644 --- a/docs/assets/readme-downloads.svg +++ b/docs/assets/readme-downloads.svg @@ -1,5 +1,5 @@ - - downloads: 159m + + downloads: 160m @@ -15,7 +15,7 @@ downloads downloads - 159m - 159m + 160m + 160m From 6fcaa3c813738bc0c08e201a3926bfd28c52b4e4 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:44:56 -0800 Subject: [PATCH 08/80] refactor(browser): connect commands and contracts to defining owners (#40042) --- .../cdp/commands/cdp-auxiliary-commands.ts | 21 +-- .../cdp/commands/cdp-bridge-command-module.ts | 135 ------------------ .../cdp/commands/cdp-bridge-command-set.ts | 18 +-- .../browser/cdp/commands/cdp-bridge-state.ts | 2 +- src/main/browser/cdp/commands/cdp-bridge.ts | 3 +- .../commands/cdp-element-state-commands.ts | 57 +++++--- .../cdp/commands/cdp-navigation-operations.ts | 2 +- .../cdp/commands/cdp-page-capture-commands.ts | 3 +- .../browser/cdp/commands/cdp-page-commands.ts | 126 ++++++++-------- .../commands/cdp-page-navigation-commands.ts | 4 +- .../cdp/commands/cdp-ref-resolution.ts | 43 +++--- .../browser/cdp/commands/cdp-screenshot.ts | 4 +- .../browser/cdp/commands/cdp-tab-commands.ts | 48 ++++--- .../browser/cdp/commands/cdp-tab-state.ts | 22 +++ .../cdp/debugger/cdp-command-sender.ts | 4 + .../cdp-debugger-channel.ts | 9 +- .../cdp-debugger-events.ts | 2 +- .../cdp-debugger-lifecycle.ts | 8 +- .../browser/cdp/debugger/guest-first-paint.ts | 2 +- .../browser/cdp/debugger/guest-paint-hold.ts | 2 + .../cdp/input/browser-text-insertion.ts | 2 +- .../cdp-dom-focus-replay.ts | 4 +- .../cdp-page-input-commands.ts | 12 +- .../browser/cdp/input/cdp-pointer-commands.ts | 98 ++++++++----- .../cdp/input/cdp-text-input-commands.ts | 76 +++++----- .../snapshot-cursor-interactive-elements.ts | 2 +- .../cdp/snapshots/snapshot-engine.test.ts | 3 +- .../browser/cdp/snapshots/snapshot-engine.ts | 6 +- .../cdp-client-command.ts | 0 .../cdp-synthetic-session-registry.ts | 0 .../cdp/transport/cdp-target-discovery.ts | 2 +- .../browser/cdp/transport/cdp-ws-proxy.ts | 12 +- ...runtime-browser-client-host-composition.ts | 2 +- ...-runtime-browser-client-host-route-sets.ts | 2 +- .../browser-client-page-command-admission.ts | 17 +-- ...ient-page-command-executor-dependencies.ts | 6 +- .../browser-client-page-command-executor.ts | 8 +- .../lifecycle/browser-client-page-cleanup.ts | 40 +----- .../lifecycle/browser-client-page-creation.ts | 12 +- ...client-page-execution-host-supersession.ts | 2 +- .../browser-client-page-inventory.ts | 2 +- .../browser-client-page-reconciliation.ts | 10 +- .../browser-client-page-retained-state.ts | 6 +- .../browser-client-page-renderer-bridge.ts | 2 +- .../browser-client-page-renderer-contract.ts | 28 ++++ .../browser-client-page-renderer-runtime.ts | 2 +- .../doc-preview-file-reader.ts | 9 +- .../doc-preview-runtime-read-contract.ts | 7 + .../doc-preview-terminal-file-reader.ts | 2 +- .../browser-client-network-route-registry.ts | 2 +- .../browser-client-page-network-route.ts | 10 ++ 51 files changed, 407 insertions(+), 494 deletions(-) delete mode 100644 src/main/browser/cdp/commands/cdp-bridge-command-module.ts create mode 100644 src/main/browser/cdp/commands/cdp-tab-state.ts create mode 100644 src/main/browser/cdp/debugger/cdp-command-sender.ts rename src/main/browser/cdp/{commands => debugger}/cdp-debugger-channel.ts (94%) rename src/main/browser/cdp/{commands => debugger}/cdp-debugger-events.ts (98%) rename src/main/browser/cdp/{commands => debugger}/cdp-debugger-lifecycle.ts (92%) create mode 100644 src/main/browser/cdp/debugger/guest-paint-hold.ts rename src/main/browser/cdp/{commands => input}/cdp-dom-focus-replay.ts (97%) rename src/main/browser/cdp/{commands => input}/cdp-page-input-commands.ts (94%) rename src/main/browser/cdp/{commands => transport}/cdp-client-command.ts (100%) rename src/main/browser/cdp/{commands => transport}/cdp-synthetic-session-registry.ts (100%) create mode 100644 src/main/browser/client-pages/renderer/browser-client-page-renderer-contract.ts create mode 100644 src/main/browser/document-preview/doc-preview-runtime-read-contract.ts create mode 100644 src/main/browser/network/client-routes/browser-client-page-network-route.ts diff --git a/src/main/browser/cdp/commands/cdp-auxiliary-commands.ts b/src/main/browser/cdp/commands/cdp-auxiliary-commands.ts index 358a2909ba6..fd6a308fbb8 100644 --- a/src/main/browser/cdp/commands/cdp-auxiliary-commands.ts +++ b/src/main/browser/cdp/commands/cdp-auxiliary-commands.ts @@ -1,8 +1,8 @@ +import type { CdpTabState } from './cdp-tab-state' import type { WebContents } from 'electron' import type { BrowserCaptureStartResult, BrowserCaptureStopResult, - BrowserConsoleEntry, BrowserConsoleResult, BrowserCookie, BrowserCookieDeleteResult, @@ -12,27 +12,10 @@ import type { BrowserInterceptDisableResult, BrowserInterceptEnableResult, BrowserInterceptedRequest, - BrowserNetworkEntry, BrowserNetworkLogResult, BrowserViewportResult } from '../../../../shared/runtime-browser-contracts' -import type { CdpCommandSender, SnapshotResult } from '../snapshots/snapshot-engine' - -export type CdpTabState = { - navigationId: string | null - snapshotResult: SnapshotResult | null - debuggerAttached: boolean - debuggerDetachListener: (() => void) | null - debuggerMessageListener: ((_event: unknown, method: string, params: unknown) => void) | null - iframeSessions: Map - capturing: boolean - consoleLog: BrowserConsoleEntry[] - networkLog: BrowserNetworkEntry[] - intercepting: boolean - interceptPatterns: string[] - pausedRequests: Map - networkRequestMap: Map -} +import type { CdpCommandSender } from '../debugger/cdp-command-sender' type ActiveCdpContext = { guest: WebContents diff --git a/src/main/browser/cdp/commands/cdp-bridge-command-module.ts b/src/main/browser/cdp/commands/cdp-bridge-command-module.ts deleted file mode 100644 index 30ca2a53e7b..00000000000 --- a/src/main/browser/cdp/commands/cdp-bridge-command-module.ts +++ /dev/null @@ -1,135 +0,0 @@ -import type { WebContents } from 'electron' -import type { - BrowserScreenshotResult, - BrowserScrollResult -} from '../../../../shared/runtime-browser-contracts' -import type { CdpTabState } from './cdp-auxiliary-commands' -import type { CdpCommandSender, RefEntry } from '../snapshots/snapshot-engine' -import type { CdpBridgeState, CdpQueuedCommand } from './cdp-bridge-state' -import type { CdpDebuggerLifecycle } from './cdp-debugger-lifecycle' -import type { CdpNavigationOperations } from './cdp-navigation-operations' -import type { CdpRefResolution } from './cdp-ref-resolution' - -export type CdpScrollDirection = BrowserScrollResult extends { scrolled: infer Direction } - ? Direction - : never -export type CdpScreenshotFormat = BrowserScreenshotResult extends { format: infer Format } - ? Format - : never - -export abstract class CdpBridgeCommandModule { - constructor( - private readonly bridgeState: CdpBridgeState, - private readonly debuggerLifecycle: CdpDebuggerLifecycle, - private readonly refResolution: CdpRefResolution, - private readonly navigation: CdpNavigationOperations - ) {} - - protected get activeWebContentsId(): number | null { - return this.bridgeState.activeWebContentsId - } - - protected set activeWebContentsId(webContentsId: number | null) { - this.bridgeState.activeWebContentsId = webContentsId - } - - protected get tabState(): Map { - return this.bridgeState.tabState - } - - protected get commandQueues(): Map { - return this.bridgeState.commandQueues - } - - protected getActiveGuest(): WebContents { - return this.bridgeState.getActiveGuest() - } - - protected getRegisteredTabs(): Map { - return this.bridgeState.getRegisteredTabs() - } - - protected resolveTabId(webContentsId: number): string { - return this.bridgeState.resolveTabId(webContentsId) - } - - protected resolveTabIdSafe(webContentsId: number): string | null { - return this.bridgeState.resolveTabIdSafe(webContentsId) - } - - protected getOrCreateTabState(tabId: string): CdpTabState { - return this.bridgeState.getOrCreateTabState(tabId) - } - - protected removeDebuggerListeners(guest: WebContents, state: CdpTabState): void { - this.debuggerLifecycle.removeDebuggerListeners(guest, state) - } - - protected ensureDebuggerAttached(guest: WebContents): Promise { - return this.debuggerLifecycle.ensureDebuggerAttached(guest) - } - - protected makeCdpSender(guest: WebContents, sessionId?: string): CdpCommandSender { - return this.debuggerLifecycle.makeCdpSender(guest, sessionId) - } - - protected senderForRef(guest: WebContents, ref: RefEntry): CdpCommandSender { - return this.refResolution.senderForRef(guest, ref) - } - - protected resolveRef( - guest: WebContents, - sender: CdpCommandSender, - ref: string - ): Promise { - return this.refResolution.resolveRef(guest, sender, ref) - } - - protected scrollIntoView(sender: CdpCommandSender, backendNodeId: number): Promise { - return this.refResolution.scrollIntoView(sender, backendNodeId) - } - - protected getElementCenter( - sender: CdpCommandSender, - backendNodeId: number - ): Promise<{ cx: number; cy: number }> { - return this.refResolution.getElementCenter(sender, backendNodeId) - } - - protected getPageCoordinates( - guest: WebContents, - refEntry: RefEntry, - localCx: number, - localCy: number - ): Promise<{ cx: number; cy: number }> { - return this.refResolution.getPageCoordinates(guest, refEntry, localCx, localCy) - } - - protected getNavigationId(sender: CdpCommandSender): Promise { - return this.navigation.getNavigationId(sender) - } - - protected getPreviousHistoryEntryId(sender: CdpCommandSender): Promise { - return this.navigation.getPreviousHistoryEntryId(sender) - } - - protected waitForLoad(sender: CdpCommandSender, guest: WebContents): Promise { - return this.navigation.waitForLoad(sender, guest) - } - - protected waitForNetworkIdle( - guest: WebContents, - timeoutMs: number, - idleMs: number - ): Promise { - return this.navigation.waitForNetworkIdle(guest, timeoutMs, idleMs) - } - - protected invalidateRefMap(webContentsId: number): void { - this.bridgeState.invalidateRefMap(webContentsId) - } - - protected enqueueCommand(execute: () => Promise): Promise { - return this.bridgeState.enqueueCommand(execute) - } -} diff --git a/src/main/browser/cdp/commands/cdp-bridge-command-set.ts b/src/main/browser/cdp/commands/cdp-bridge-command-set.ts index bd159a95c42..de2f3153c11 100644 --- a/src/main/browser/cdp/commands/cdp-bridge-command-set.ts +++ b/src/main/browser/cdp/commands/cdp-bridge-command-set.ts @@ -1,8 +1,9 @@ import type { WebContents } from 'electron' -import { CdpAuxiliaryCommands, type CdpTabState } from './cdp-auxiliary-commands' -import type { CdpCommandSender } from '../snapshots/snapshot-engine' +import { CdpAuxiliaryCommands } from './cdp-auxiliary-commands' +import type { CdpTabState } from './cdp-tab-state' +import type { CdpCommandSender } from '../debugger/cdp-command-sender' import type { CdpBridgeState } from './cdp-bridge-state' -import { CdpDebuggerLifecycle } from './cdp-debugger-lifecycle' +import { CdpDebuggerLifecycle } from '../debugger/cdp-debugger-lifecycle' import { CdpElementStateCommands } from './cdp-element-state-commands' import { CdpNavigationOperations } from './cdp-navigation-operations' import { CdpPageCommands } from './cdp-page-commands' @@ -23,13 +24,12 @@ export class CdpBridgeCommandSet { const debuggerLifecycle = new CdpDebuggerLifecycle(bridgeState) const navigation = new CdpNavigationOperations() const refResolution = new CdpRefResolution(bridgeState, debuggerLifecycle, navigation) - const moduleArgs = [bridgeState, debuggerLifecycle, refResolution, navigation] as const - this.elements = new CdpElementStateCommands(...moduleArgs) - this.page = new CdpPageCommands(...moduleArgs) - this.pointer = new CdpPointerCommands(...moduleArgs) - this.tabs = new CdpTabCommands(...moduleArgs) - this.textInput = new CdpTextInputCommands(...moduleArgs) + this.elements = new CdpElementStateCommands(bridgeState, debuggerLifecycle, refResolution) + this.page = new CdpPageCommands(bridgeState, debuggerLifecycle, navigation) + this.pointer = new CdpPointerCommands(bridgeState, debuggerLifecycle, refResolution) + this.tabs = new CdpTabCommands(bridgeState, debuggerLifecycle) + this.textInput = new CdpTextInputCommands(bridgeState, debuggerLifecycle, refResolution) this.auxiliary = new CdpAuxiliaryCommands({ run: ( operation: (context: { diff --git a/src/main/browser/cdp/commands/cdp-bridge-state.ts b/src/main/browser/cdp/commands/cdp-bridge-state.ts index 630bee7652b..4a9ab650837 100644 --- a/src/main/browser/cdp/commands/cdp-bridge-state.ts +++ b/src/main/browser/cdp/commands/cdp-bridge-state.ts @@ -1,6 +1,6 @@ import { webContents } from 'electron' import { BrowserError } from '../../browser-error' -import type { CdpTabState } from './cdp-auxiliary-commands' +import type { CdpTabState } from './cdp-tab-state' export type CdpQueuedCommand = { execute: () => Promise diff --git a/src/main/browser/cdp/commands/cdp-bridge.ts b/src/main/browser/cdp/commands/cdp-bridge.ts index a0083bf2220..9d28918aaa3 100644 --- a/src/main/browser/cdp/commands/cdp-bridge.ts +++ b/src/main/browser/cdp/commands/cdp-bridge.ts @@ -34,7 +34,8 @@ import type { BrowserWaitResult } from '../../../../shared/runtime-browser-contracts' import type { BrowserManager } from '../../guest-lifecycle/manager/browser-manager' -import type { CdpAuxiliaryCommands, CdpTabState } from './cdp-auxiliary-commands' +import type { CdpAuxiliaryCommands } from './cdp-auxiliary-commands' +import type { CdpTabState } from './cdp-tab-state' import { CdpBridgeCommandSet } from './cdp-bridge-command-set' import { CdpBridgeState } from './cdp-bridge-state' import type { CdpQueuedCommand } from './cdp-bridge-state' diff --git a/src/main/browser/cdp/commands/cdp-element-state-commands.ts b/src/main/browser/cdp/commands/cdp-element-state-commands.ts index 93948aa6006..010f6053860 100644 --- a/src/main/browser/cdp/commands/cdp-element-state-commands.ts +++ b/src/main/browser/cdp/commands/cdp-element-state-commands.ts @@ -3,17 +3,25 @@ import type { BrowserSelectResult, BrowserUploadResult } from '../../../../shared/runtime-browser-contracts' -import { CdpBridgeCommandModule } from './cdp-bridge-command-module' +import type { CdpBridgeState } from './cdp-bridge-state' +import type { CdpDebuggerLifecycle } from '../debugger/cdp-debugger-lifecycle' +import type { CdpRefResolution } from './cdp-ref-resolution' + +export class CdpElementStateCommands { + constructor( + private readonly bridgeState: CdpBridgeState, + private readonly debuggerLifecycle: CdpDebuggerLifecycle, + private readonly refResolution: CdpRefResolution + ) {} -export class CdpElementStateCommands extends CdpBridgeCommandModule { uploadFile(element: string, filePaths: string[]): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) await refSender('DOM.setFileInputFiles', { files: filePaths, backendNodeId: node.backendDOMNodeId @@ -24,13 +32,13 @@ export class CdpElementStateCommands extends CdpBridgeCommandModule { } select(element: string, value: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) const { nodeId } = (await refSender('DOM.requestNode', { backendNodeId: node.backendDOMNodeId })) as { nodeId: number } @@ -75,13 +83,13 @@ export class CdpElementStateCommands extends CdpBridgeCommandModule { } check(element: string, checked: boolean): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) const { nodeId } = (await refSender('DOM.requestNode', { backendNodeId: node.backendDOMNodeId @@ -97,9 +105,12 @@ export class CdpElementStateCommands extends CdpBridgeCommandModule { })) as { result: { value: boolean } } if (currentState.value !== checked) { - await this.scrollIntoView(refSender, node.backendDOMNodeId) - const localCenter = await this.getElementCenter(refSender, node.backendDOMNodeId) - const { cx, cy } = await this.getPageCoordinates( + await this.refResolution.scrollIntoView(refSender, node.backendDOMNodeId) + const localCenter = await this.refResolution.getElementCenter( + refSender, + node.backendDOMNodeId + ) + const { cx, cy } = await this.refResolution.getPageCoordinates( guest, node, localCenter.cx, diff --git a/src/main/browser/cdp/commands/cdp-navigation-operations.ts b/src/main/browser/cdp/commands/cdp-navigation-operations.ts index e74512f3269..f63c2837d5b 100644 --- a/src/main/browser/cdp/commands/cdp-navigation-operations.ts +++ b/src/main/browser/cdp/commands/cdp-navigation-operations.ts @@ -1,6 +1,6 @@ import type { WebContents } from 'electron' import { BrowserError } from '../../browser-error' -import type { CdpCommandSender } from '../snapshots/snapshot-engine' +import type { CdpCommandSender } from '../debugger/cdp-command-sender' export class CdpNavigationOperations { async getNavigationId(sender: CdpCommandSender): Promise { diff --git a/src/main/browser/cdp/commands/cdp-page-capture-commands.ts b/src/main/browser/cdp/commands/cdp-page-capture-commands.ts index a0bd4f0e525..69521766d79 100644 --- a/src/main/browser/cdp/commands/cdp-page-capture-commands.ts +++ b/src/main/browser/cdp/commands/cdp-page-capture-commands.ts @@ -1,6 +1,7 @@ import type { WebSocket } from 'ws' import type { WebContents } from 'electron' -import { captureScreenshot, type CapturePaintHold } from './cdp-screenshot' +import { captureScreenshot } from './cdp-screenshot' +import type { CapturePaintHold } from '../debugger/guest-paint-hold' import { buildPrintToPdfOptions, CdpPdfStreamStore } from './cdp-print-to-pdf' import type { CdpClientResponseWriter } from '../transport/cdp-client-response-writer' import { readRemoteCliAdmittedPage } from '../../routing/input-ownership/remote-cli-page-admission' diff --git a/src/main/browser/cdp/commands/cdp-page-commands.ts b/src/main/browser/cdp/commands/cdp-page-commands.ts index 626f504e66b..4ed98ae5bb1 100644 --- a/src/main/browser/cdp/commands/cdp-page-commands.ts +++ b/src/main/browser/cdp/commands/cdp-page-commands.ts @@ -8,29 +8,39 @@ import type { BrowserWaitResult } from '../../../../shared/runtime-browser-contracts' import { BrowserError } from '../../browser-error' -import { - CdpBridgeCommandModule, - type CdpScreenshotFormat, - type CdpScrollDirection -} from './cdp-bridge-command-module' +import type { CdpBridgeState } from './cdp-bridge-state' +import type { CdpDebuggerLifecycle } from '../debugger/cdp-debugger-lifecycle' +import type { CdpNavigationOperations } from './cdp-navigation-operations' import { buildSnapshot } from '../snapshots/snapshot-engine' -export class CdpPageCommands extends CdpBridgeCommandModule { - snapshot(): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) +type CdpScrollDirection = BrowserScrollResult extends { scrolled: infer Direction } + ? Direction + : never - const tabId = this.resolveTabId(guest.id) - const state = this.getOrCreateTabState(tabId) +type CdpScreenshotFormat = BrowserScreenshotResult extends { format: infer Format } ? Format : never + +export class CdpPageCommands { + constructor( + private readonly bridgeState: CdpBridgeState, + private readonly debuggerLifecycle: CdpDebuggerLifecycle, + private readonly navigation: CdpNavigationOperations + ) {} + + snapshot(): Promise { + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) + + const tabId = this.bridgeState.resolveTabId(guest.id) + const state = this.bridgeState.getOrCreateTabState(tabId) const result = await buildSnapshot(sender, state.iframeSessions, (sessionId) => - this.makeCdpSender(guest, sessionId) + this.debuggerLifecycle.makeCdpSender(guest, sessionId) ) state.snapshotResult = result - const navId = await this.getNavigationId(sender) + const navId = await this.navigation.getNavigationId(sender) state.navigationId = navId return { @@ -44,10 +54,10 @@ export class CdpPageCommands extends CdpBridgeCommandModule { } goto(url: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) const { errorText } = (await sender('Page.navigate', { url })) as { errorText?: string @@ -57,18 +67,18 @@ export class CdpPageCommands extends CdpBridgeCommandModule { throw new BrowserError('browser_navigation_failed', `Navigation failed: ${errorText}`) } - await this.waitForLoad(sender, guest) - this.invalidateRefMap(guest.id) + await this.navigation.waitForLoad(sender, guest) + this.bridgeState.invalidateRefMap(guest.id) return { url: guest.getURL(), title: guest.getTitle() } }) } scroll(direction: CdpScrollDirection, amount?: number): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) // JS scrolling avoids focus changes and unreliable wheel delivery in Electron webviews. const expr = amount @@ -81,19 +91,19 @@ export class CdpPageCommands extends CdpBridgeCommandModule { } wait(timeoutMs: number): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - await this.ensureDebuggerAttached(guest) - await this.waitForNetworkIdle(guest, timeoutMs, 500) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + await this.debuggerLifecycle.ensureDebuggerAttached(guest) + await this.navigation.waitForNetworkIdle(guest, timeoutMs, 500) return { waited: true } }) } pdf(): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) const { data } = (await sender('Page.printToPDF', { printBackground: true @@ -104,10 +114,10 @@ export class CdpPageCommands extends CdpBridgeCommandModule { } fullPageScreenshot(format: CdpScreenshotFormat): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) const metrics = (await sender('Page.getLayoutMetrics')) as { cssContentSize?: { width: number; height: number } @@ -136,40 +146,40 @@ export class CdpPageCommands extends CdpBridgeCommandModule { } back(): Promise<{ url: string; title: string }> { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) await sender('Page.navigateToHistoryEntry', { - entryId: await this.getPreviousHistoryEntryId(sender) + entryId: await this.navigation.getPreviousHistoryEntryId(sender) }) - await this.waitForLoad(sender, guest) - this.invalidateRefMap(guest.id) + await this.navigation.waitForLoad(sender, guest) + this.bridgeState.invalidateRefMap(guest.id) return { url: guest.getURL(), title: guest.getTitle() } }) } reload(): Promise<{ url: string; title: string }> { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) await sender('Page.reload') - await this.waitForLoad(sender, guest) - this.invalidateRefMap(guest.id) + await this.navigation.waitForLoad(sender, guest) + this.bridgeState.invalidateRefMap(guest.id) return { url: guest.getURL(), title: guest.getTitle() } }) } screenshot(format: CdpScreenshotFormat): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) const { data } = (await sender('Page.captureScreenshot', { format @@ -180,10 +190,10 @@ export class CdpPageCommands extends CdpBridgeCommandModule { } evaluate(expression: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) const { result, exceptionDetails } = (await sender('Runtime.evaluate', { expression, diff --git a/src/main/browser/cdp/commands/cdp-page-navigation-commands.ts b/src/main/browser/cdp/commands/cdp-page-navigation-commands.ts index fa0edcddabb..5db6d88cf9e 100644 --- a/src/main/browser/cdp/commands/cdp-page-navigation-commands.ts +++ b/src/main/browser/cdp/commands/cdp-page-navigation-commands.ts @@ -1,8 +1,8 @@ import type { WebSocket } from 'ws' import type { WebContents } from 'electron' import type { CdpClientResponseWriter } from '../transport/cdp-client-response-writer' -import type { CdpSyntheticSessionRegistry } from './cdp-synthetic-session-registry' -import type { CdpDebuggerChannel } from './cdp-debugger-channel' +import type { CdpSyntheticSessionRegistry } from '../transport/cdp-synthetic-session-registry' +import type { CdpDebuggerChannel } from '../debugger/cdp-debugger-channel' const LIFECYCLE_PRIMING_TIMEOUT_MS = 1_000 diff --git a/src/main/browser/cdp/commands/cdp-ref-resolution.ts b/src/main/browser/cdp/commands/cdp-ref-resolution.ts index d469296379b..6528a7162f4 100644 --- a/src/main/browser/cdp/commands/cdp-ref-resolution.ts +++ b/src/main/browser/cdp/commands/cdp-ref-resolution.ts @@ -1,9 +1,10 @@ import type { WebContents } from 'electron' import { BrowserError } from '../../browser-error' -import type { CdpTabState } from './cdp-auxiliary-commands' -import type { CdpCommandSender, RefEntry } from '../snapshots/snapshot-engine' + +import type { CdpCommandSender } from '../debugger/cdp-command-sender' +import type { RefEntry } from '../snapshots/snapshot-engine' import type { CdpBridgeState } from './cdp-bridge-state' -import type { CdpDebuggerLifecycle } from './cdp-debugger-lifecycle' +import type { CdpDebuggerLifecycle } from '../debugger/cdp-debugger-lifecycle' import type { CdpNavigationOperations } from './cdp-navigation-operations' export class CdpRefResolution { @@ -14,12 +15,14 @@ export class CdpRefResolution { ) {} senderForRef(guest: WebContents, ref: RefEntry): CdpCommandSender { - return ref.sessionId ? this.makeCdpSender(guest, ref.sessionId) : this.makeCdpSender(guest) + return ref.sessionId + ? this.debuggerLifecycle.makeCdpSender(guest, ref.sessionId) + : this.debuggerLifecycle.makeCdpSender(guest) } async resolveRef(guest: WebContents, sender: CdpCommandSender, ref: string): Promise { - const tabId = this.resolveTabId(guest.id) - const state = this.getOrCreateTabState(tabId) + const tabId = this.bridgeState.resolveTabId(guest.id) + const state = this.bridgeState.getOrCreateTabState(tabId) if (!state.snapshotResult) { throw new BrowserError( @@ -38,7 +41,7 @@ export class CdpRefResolution { // Child sessions have independent history; parent navigation IDs cannot validate their refs. if (!entry.sessionId) { - const currentNavId = await this.getNavigationId(sender) + const currentNavId = await this.navigation.getNavigationId(sender) if (state.navigationId && currentNavId !== state.navigationId) { state.snapshotResult = null state.navigationId = null @@ -49,7 +52,9 @@ export class CdpRefResolution { } } - const refSender = entry.sessionId ? this.makeCdpSender(guest, entry.sessionId) : sender + const refSender = entry.sessionId + ? this.debuggerLifecycle.makeCdpSender(guest, entry.sessionId) + : sender try { await refSender('DOM.describeNode', { backendNodeId: entry.backendDOMNodeId }) return entry @@ -94,9 +99,9 @@ export class CdpRefResolution { guest: WebContents, sessionId: string ): Promise<{ offsetX: number; offsetY: number }> { - const tabId = this.resolveTabId(guest.id) - const state = this.getOrCreateTabState(tabId) - const parentSender = this.makeCdpSender(guest) + const tabId = this.bridgeState.resolveTabId(guest.id) + const state = this.bridgeState.getOrCreateTabState(tabId) + const parentSender = this.debuggerLifecycle.makeCdpSender(guest) for (const [targetId, sid] of state.iframeSessions) { if (sid === sessionId) { @@ -201,20 +206,4 @@ export class CdpRefResolution { } return null } - - private resolveTabId(webContentsId: number): string { - return this.bridgeState.resolveTabId(webContentsId) - } - - private getOrCreateTabState(tabId: string): CdpTabState { - return this.bridgeState.getOrCreateTabState(tabId) - } - - private makeCdpSender(guest: WebContents, sessionId?: string): CdpCommandSender { - return this.debuggerLifecycle.makeCdpSender(guest, sessionId) - } - - private getNavigationId(sender: CdpCommandSender): Promise { - return this.navigation.getNavigationId(sender) - } } diff --git a/src/main/browser/cdp/commands/cdp-screenshot.ts b/src/main/browser/cdp/commands/cdp-screenshot.ts index 7371ce01765..f43f5c1e194 100644 --- a/src/main/browser/cdp/commands/cdp-screenshot.ts +++ b/src/main/browser/cdp/commands/cdp-screenshot.ts @@ -1,3 +1,4 @@ +import type { CapturePaintHold } from '../debugger/guest-paint-hold' import type { WebContents } from 'electron' import { sendGuestCdpCommand } from '../debugger/guest-cdp-command' import { @@ -5,9 +6,6 @@ import { readRemoteCliAdmittedPage } from '../../routing/input-ownership/remote-cli-page-admission' -/** Draws a hidden guest while a capture or agent input needs its frames; returns its release. */ -export type CapturePaintHold = () => () => void - export const SCREENSHOT_TIMEOUT_MS = 8000 // Probe offsets leave time for the original capture to finish before the deadline. const FRAME_PROBE_OFFSETS_MS = [250, 750, 1750, 3750] diff --git a/src/main/browser/cdp/commands/cdp-tab-commands.ts b/src/main/browser/cdp/commands/cdp-tab-commands.ts index 35e865b3d0a..92569ff3bc7 100644 --- a/src/main/browser/cdp/commands/cdp-tab-commands.ts +++ b/src/main/browser/cdp/commands/cdp-tab-commands.ts @@ -5,22 +5,28 @@ import type { BrowserTabSwitchResult } from '../../../../shared/runtime-browser-contracts' import { BrowserError } from '../../browser-error' -import { CdpBridgeCommandModule } from './cdp-bridge-command-module' +import type { CdpBridgeState } from './cdp-bridge-state' +import type { CdpDebuggerLifecycle } from '../debugger/cdp-debugger-lifecycle' + +export class CdpTabCommands { + constructor( + private readonly bridgeState: CdpBridgeState, + private readonly debuggerLifecycle: CdpDebuggerLifecycle + ) {} -export class CdpTabCommands extends CdpBridgeCommandModule { setActiveTab(webContentsId: number): void { - this.activeWebContentsId = webContentsId + this.bridgeState.activeWebContentsId = webContentsId } getActiveWebContentsId(): number | null { - return this.activeWebContentsId + return this.bridgeState.activeWebContentsId } getActivePageId(_worktreeId?: string): string | null { - if (!this.activeWebContentsId) { + if (!this.bridgeState.activeWebContentsId) { return null } - return this.resolveTabIdSafe(this.activeWebContentsId) + return this.bridgeState.resolveTabIdSafe(this.bridgeState.activeWebContentsId) } getPageInfo( @@ -31,7 +37,7 @@ export class CdpTabCommands extends CdpBridgeCommandModule { if (!resolvedPageId) { return null } - const webContentsId = this.getRegisteredTabs().get(resolvedPageId) + const webContentsId = this.bridgeState.getRegisteredTabs().get(resolvedPageId) if (webContentsId == null) { return null } @@ -50,7 +56,7 @@ export class CdpTabCommands extends CdpBridgeCommandModule { const tabs: BrowserTabInfo[] = [] let index = 0 - for (const [tabId, wcId] of this.getRegisteredTabs()) { + for (const [tabId, wcId] of this.bridgeState.getRegisteredTabs()) { const guest = webContents.fromId(wcId) if (!guest || guest.isDestroyed()) { continue @@ -60,7 +66,7 @@ export class CdpTabCommands extends CdpBridgeCommandModule { index, url: guest.getURL(), title: guest.getTitle(), - active: wcId === this.activeWebContentsId + active: wcId === this.bridgeState.activeWebContentsId }) index++ } @@ -70,7 +76,7 @@ export class CdpTabCommands extends CdpBridgeCommandModule { tabSwitch(index: number): BrowserTabSwitchResult { // Keep indices aligned with tabList(), which skips destroyed guests. - const liveEntries = [...this.getRegisteredTabs()].filter(([_tabId, wcId]) => { + const liveEntries = [...this.bridgeState.getRegisteredTabs()].filter(([_tabId, wcId]) => { const guest = webContents.fromId(wcId) return guest && !guest.isDestroyed() }) @@ -82,31 +88,31 @@ export class CdpTabCommands extends CdpBridgeCommandModule { } const [tabId, wcId] = liveEntries[index] - if (this.activeWebContentsId !== null) { - this.invalidateRefMap(this.activeWebContentsId) + if (this.bridgeState.activeWebContentsId !== null) { + this.bridgeState.invalidateRefMap(this.bridgeState.activeWebContentsId) } - this.activeWebContentsId = wcId + this.bridgeState.activeWebContentsId = wcId return { switched: index, browserPageId: tabId } } onTabClosed(webContentsId: number): void { - if (this.activeWebContentsId === webContentsId) { - this.activeWebContentsId = null + if (this.bridgeState.activeWebContentsId === webContentsId) { + this.bridgeState.activeWebContentsId = null } - const tabId = this.resolveTabIdSafe(webContentsId) + const tabId = this.bridgeState.resolveTabIdSafe(webContentsId) if (tabId) { - const state = this.tabState.get(tabId) + const state = this.bridgeState.tabState.get(tabId) const guest = webContents.fromId(webContentsId) if (state && guest) { - this.removeDebuggerListeners(guest, state) + this.debuggerLifecycle.removeDebuggerListeners(guest, state) } - this.tabState.delete(tabId) - this.commandQueues.delete(tabId) + this.bridgeState.tabState.delete(tabId) + this.bridgeState.commandQueues.delete(tabId) } } onTabChanged(webContentsId: number): void { - this.activeWebContentsId = webContentsId + this.bridgeState.activeWebContentsId = webContentsId } } diff --git a/src/main/browser/cdp/commands/cdp-tab-state.ts b/src/main/browser/cdp/commands/cdp-tab-state.ts new file mode 100644 index 00000000000..aae922be6f8 --- /dev/null +++ b/src/main/browser/cdp/commands/cdp-tab-state.ts @@ -0,0 +1,22 @@ +import type { + BrowserConsoleEntry, + BrowserInterceptedRequest, + BrowserNetworkEntry +} from '../../../../shared/runtime-browser-contracts' +import type { SnapshotResult } from '../snapshots/snapshot-engine' + +export type CdpTabState = { + navigationId: string | null + snapshotResult: SnapshotResult | null + debuggerAttached: boolean + debuggerDetachListener: (() => void) | null + debuggerMessageListener: ((_event: unknown, method: string, params: unknown) => void) | null + iframeSessions: Map + capturing: boolean + consoleLog: BrowserConsoleEntry[] + networkLog: BrowserNetworkEntry[] + intercepting: boolean + interceptPatterns: string[] + pausedRequests: Map + networkRequestMap: Map +} diff --git a/src/main/browser/cdp/debugger/cdp-command-sender.ts b/src/main/browser/cdp/debugger/cdp-command-sender.ts new file mode 100644 index 00000000000..0cff00bc4ac --- /dev/null +++ b/src/main/browser/cdp/debugger/cdp-command-sender.ts @@ -0,0 +1,4 @@ +export type CdpCommandSender = ( + method: string, + params?: Record +) => Promise diff --git a/src/main/browser/cdp/commands/cdp-debugger-channel.ts b/src/main/browser/cdp/debugger/cdp-debugger-channel.ts similarity index 94% rename from src/main/browser/cdp/commands/cdp-debugger-channel.ts rename to src/main/browser/cdp/debugger/cdp-debugger-channel.ts index 661e2bb2c2a..80172af19f0 100644 --- a/src/main/browser/cdp/commands/cdp-debugger-channel.ts +++ b/src/main/browser/cdp/debugger/cdp-debugger-channel.ts @@ -1,12 +1,9 @@ import { WebSocket } from 'ws' import type { WebContents } from 'electron' -import { - acquireElectronDebugger, - type ElectronDebuggerLease -} from '../debugger/electron-debugger-lease' +import { acquireElectronDebugger, type ElectronDebuggerLease } from './electron-debugger-lease' import type { CdpClientResponseWriter } from '../transport/cdp-client-response-writer' -import type { CdpSyntheticSessionRegistry } from './cdp-synthetic-session-registry' -import { sendGuestCdpCommand } from '../debugger/guest-cdp-command' +import type { CdpSyntheticSessionRegistry } from '../transport/cdp-synthetic-session-registry' +import { sendGuestCdpCommand } from './guest-cdp-command' import { isOrcaFrameSessionTraffic } from '../input/guest-frame-input-sessions' export class CdpDebuggerChannel { diff --git a/src/main/browser/cdp/commands/cdp-debugger-events.ts b/src/main/browser/cdp/debugger/cdp-debugger-events.ts similarity index 98% rename from src/main/browser/cdp/commands/cdp-debugger-events.ts rename to src/main/browser/cdp/debugger/cdp-debugger-events.ts index ea44c713279..cfcfcfb9721 100644 --- a/src/main/browser/cdp/commands/cdp-debugger-events.ts +++ b/src/main/browser/cdp/debugger/cdp-debugger-events.ts @@ -1,6 +1,6 @@ import type { WebContents } from 'electron' import type { BrowserNetworkEntry } from '../../../../shared/runtime-browser-contracts' -import type { CdpTabState } from './cdp-auxiliary-commands' +import type { CdpTabState } from '../commands/cdp-tab-state' const CAPTURE_LOG_LIMIT = 1000 diff --git a/src/main/browser/cdp/commands/cdp-debugger-lifecycle.ts b/src/main/browser/cdp/debugger/cdp-debugger-lifecycle.ts similarity index 92% rename from src/main/browser/cdp/commands/cdp-debugger-lifecycle.ts rename to src/main/browser/cdp/debugger/cdp-debugger-lifecycle.ts index a7bfba9cce2..c06af7287bd 100644 --- a/src/main/browser/cdp/commands/cdp-debugger-lifecycle.ts +++ b/src/main/browser/cdp/debugger/cdp-debugger-lifecycle.ts @@ -1,10 +1,10 @@ import type { WebContents } from 'electron' import { BrowserError } from '../../browser-error' -import type { CdpTabState } from './cdp-auxiliary-commands' -import type { CdpCommandSender } from '../snapshots/snapshot-engine' -import type { CdpBridgeState } from './cdp-bridge-state' +import type { CdpTabState } from '../commands/cdp-tab-state' +import type { CdpCommandSender } from './cdp-command-sender' +import type { CdpBridgeState } from '../commands/cdp-bridge-state' import { createCdpDebuggerMessageListener } from './cdp-debugger-events' -import { sendGuestCdpCommand } from '../debugger/guest-cdp-command' +import { sendGuestCdpCommand } from './guest-cdp-command' export class CdpDebuggerLifecycle { constructor(private readonly bridgeState: CdpBridgeState) {} diff --git a/src/main/browser/cdp/debugger/guest-first-paint.ts b/src/main/browser/cdp/debugger/guest-first-paint.ts index 21e84036817..b76d09fdb81 100644 --- a/src/main/browser/cdp/debugger/guest-first-paint.ts +++ b/src/main/browser/cdp/debugger/guest-first-paint.ts @@ -1,5 +1,5 @@ import type { WebContents } from 'electron' -import type { CapturePaintHold } from '../commands/cdp-screenshot' +import type { CapturePaintHold } from './guest-paint-hold' import { isEmbeddedGuest } from '../input/guest-direct-key-input' import { readRecord, diff --git a/src/main/browser/cdp/debugger/guest-paint-hold.ts b/src/main/browser/cdp/debugger/guest-paint-hold.ts new file mode 100644 index 00000000000..885a3e98a84 --- /dev/null +++ b/src/main/browser/cdp/debugger/guest-paint-hold.ts @@ -0,0 +1,2 @@ +/** Draws a hidden guest while a capture or agent input needs its frames; returns its release. */ +export type CapturePaintHold = () => () => void diff --git a/src/main/browser/cdp/input/browser-text-insertion.ts b/src/main/browser/cdp/input/browser-text-insertion.ts index b5bd0d56e84..588db4df2c8 100644 --- a/src/main/browser/cdp/input/browser-text-insertion.ts +++ b/src/main/browser/cdp/input/browser-text-insertion.ts @@ -1,6 +1,6 @@ import { iterateBrowserTextInsertionChunks } from '../../../../shared/browser-text-insertion-chunks' import { yieldToEventLoop } from '../../../../shared/event-loop-yield' -import type { CdpCommandSender } from '../snapshots/snapshot-engine' +import type { CdpCommandSender } from '../debugger/cdp-command-sender' export async function insertTextThroughCdp( sender: CdpCommandSender, diff --git a/src/main/browser/cdp/commands/cdp-dom-focus-replay.ts b/src/main/browser/cdp/input/cdp-dom-focus-replay.ts similarity index 97% rename from src/main/browser/cdp/commands/cdp-dom-focus-replay.ts rename to src/main/browser/cdp/input/cdp-dom-focus-replay.ts index 45045e5fbd3..31ffed7c213 100644 --- a/src/main/browser/cdp/commands/cdp-dom-focus-replay.ts +++ b/src/main/browser/cdp/input/cdp-dom-focus-replay.ts @@ -1,8 +1,8 @@ import type { WebSocket } from 'ws' import type { WebContents } from 'electron' import type { CdpClientResponseWriter } from '../transport/cdp-client-response-writer' -import type { CdpDebuggerChannel } from './cdp-debugger-channel' -import type { TextInsertionRoute } from '../input/guest-direct-key-input' +import type { CdpDebuggerChannel } from '../debugger/cdp-debugger-channel' +import type { TextInsertionRoute } from './guest-direct-key-input' /** Replay DOM.focus before insertion because native focusing can blur the target. */ export class CdpDomFocusReplay { diff --git a/src/main/browser/cdp/commands/cdp-page-input-commands.ts b/src/main/browser/cdp/input/cdp-page-input-commands.ts similarity index 94% rename from src/main/browser/cdp/commands/cdp-page-input-commands.ts rename to src/main/browser/cdp/input/cdp-page-input-commands.ts index dbe1ab8dc9b..372d5c21991 100644 --- a/src/main/browser/cdp/commands/cdp-page-input-commands.ts +++ b/src/main/browser/cdp/input/cdp-page-input-commands.ts @@ -1,23 +1,23 @@ import type { WebSocket } from 'ws' import type { WebContents } from 'electron' import type { CdpClientResponseWriter } from '../transport/cdp-client-response-writer' -import type { CdpDebuggerChannel } from './cdp-debugger-channel' +import type { CdpDebuggerChannel } from '../debugger/cdp-debugger-channel' import type { CdpDomFocusReplay } from './cdp-dom-focus-replay' import { enablePageFocusEmulation, isFocusRoutedCdpInput, isFocusTakingCdpInput, type AgentInputFocusGuard -} from '../input/agent-input-focus-guard' +} from './agent-input-focus-guard' import { dispatchPageKeyEvent, isEmbeddedGuest, routePageTextInsertion -} from '../input/guest-direct-key-input' -import type { GuestSessionCdpSender } from '../input/guest-frame-input-sessions' -import { withAgentInputInjection } from '../input/agent-input-injection' +} from './guest-direct-key-input' +import type { GuestSessionCdpSender } from './guest-frame-input-sessions' +import { withAgentInputInjection } from './agent-input-injection' import type { AgentPointerInput } from '../../guest-lifecycle/page-activity/browser-agent-activity' -import type { CapturePaintHold } from './cdp-screenshot' +import type { CapturePaintHold } from '../debugger/guest-paint-hold' import { waitForGuestFirstPaint } from '../debugger/guest-first-paint' /** Shows the agent's pointer at an input's point and resolves once it has landed. */ diff --git a/src/main/browser/cdp/input/cdp-pointer-commands.ts b/src/main/browser/cdp/input/cdp-pointer-commands.ts index c59348df172..67d6e4cefd5 100644 --- a/src/main/browser/cdp/input/cdp-pointer-commands.ts +++ b/src/main/browser/cdp/input/cdp-pointer-commands.ts @@ -3,21 +3,37 @@ import type { BrowserDragResult, BrowserHoverResult } from '../../../../shared/runtime-browser-contracts' -import { CdpBridgeCommandModule } from '../commands/cdp-bridge-command-module' +import type { CdpBridgeState } from '../commands/cdp-bridge-state' +import type { CdpDebuggerLifecycle } from '../debugger/cdp-debugger-lifecycle' +import type { CdpRefResolution } from '../commands/cdp-ref-resolution' + +export class CdpPointerCommands { + constructor( + private readonly bridgeState: CdpBridgeState, + private readonly debuggerLifecycle: CdpDebuggerLifecycle, + private readonly refResolution: CdpRefResolution + ) {} -export class CdpPointerCommands extends CdpBridgeCommandModule { click(element: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) - await this.scrollIntoView(refSender, node.backendDOMNodeId) - const localCenter = await this.getElementCenter(refSender, node.backendDOMNodeId) - const { cx, cy } = await this.getPageCoordinates(guest, node, localCenter.cx, localCenter.cy) + await this.refResolution.scrollIntoView(refSender, node.backendDOMNodeId) + const localCenter = await this.refResolution.getElementCenter( + refSender, + node.backendDOMNodeId + ) + const { cx, cy } = await this.refResolution.getPageCoordinates( + guest, + node, + localCenter.cx, + localCenter.cy + ) // Reveal hover-dependent targets before clicking. await sender('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx, y: cy }) @@ -41,16 +57,24 @@ export class CdpPointerCommands extends CdpBridgeCommandModule { } hover(element: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) - await this.scrollIntoView(refSender, node.backendDOMNodeId) - const localCenter = await this.getElementCenter(refSender, node.backendDOMNodeId) - const { cx, cy } = await this.getPageCoordinates(guest, node, localCenter.cx, localCenter.cy) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) + await this.refResolution.scrollIntoView(refSender, node.backendDOMNodeId) + const localCenter = await this.refResolution.getElementCenter( + refSender, + node.backendDOMNodeId + ) + const { cx, cy } = await this.refResolution.getPageCoordinates( + guest, + node, + localCenter.cx, + localCenter.cy + ) await sender('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx, y: cy }) @@ -59,21 +83,29 @@ export class CdpPointerCommands extends CdpBridgeCommandModule { } drag(fromElement: string, toElement: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const fromNode = await this.resolveRef(guest, sender, fromElement) - const toNode = await this.resolveRef(guest, sender, toElement) - const fromSender = this.senderForRef(guest, fromNode) - const toSender = this.senderForRef(guest, toNode) + const fromNode = await this.refResolution.resolveRef(guest, sender, fromElement) + const toNode = await this.refResolution.resolveRef(guest, sender, toElement) + const fromSender = this.refResolution.senderForRef(guest, fromNode) + const toSender = this.refResolution.senderForRef(guest, toNode) - await this.scrollIntoView(fromSender, fromNode.backendDOMNodeId) - const fromLocal = await this.getElementCenter(fromSender, fromNode.backendDOMNodeId) - const from = await this.getPageCoordinates(guest, fromNode, fromLocal.cx, fromLocal.cy) - const toLocal = await this.getElementCenter(toSender, toNode.backendDOMNodeId) - const to = await this.getPageCoordinates(guest, toNode, toLocal.cx, toLocal.cy) + await this.refResolution.scrollIntoView(fromSender, fromNode.backendDOMNodeId) + const fromLocal = await this.refResolution.getElementCenter( + fromSender, + fromNode.backendDOMNodeId + ) + const from = await this.refResolution.getPageCoordinates( + guest, + fromNode, + fromLocal.cx, + fromLocal.cy + ) + const toLocal = await this.refResolution.getElementCenter(toSender, toNode.backendDOMNodeId) + const to = await this.refResolution.getPageCoordinates(guest, toNode, toLocal.cx, toLocal.cy) // Intermediate drag events are required by many drag-and-drop libraries. await sender('Input.dispatchMouseEvent', { type: 'mouseMoved', x: from.cx, y: from.cy }) diff --git a/src/main/browser/cdp/input/cdp-text-input-commands.ts b/src/main/browser/cdp/input/cdp-text-input-commands.ts index 198d01b56a8..8fa20c64f77 100644 --- a/src/main/browser/cdp/input/cdp-text-input-commands.ts +++ b/src/main/browser/cdp/input/cdp-text-input-commands.ts @@ -7,17 +7,25 @@ import type { BrowserTypeResult } from '../../../../shared/runtime-browser-contracts' import { insertTextThroughCdp } from './browser-text-insertion' -import { CdpBridgeCommandModule } from '../commands/cdp-bridge-command-module' +import type { CdpBridgeState } from '../commands/cdp-bridge-state' +import type { CdpDebuggerLifecycle } from '../debugger/cdp-debugger-lifecycle' +import type { CdpRefResolution } from '../commands/cdp-ref-resolution' + +export class CdpTextInputCommands { + constructor( + private readonly bridgeState: CdpBridgeState, + private readonly debuggerLifecycle: CdpDebuggerLifecycle, + private readonly refResolution: CdpRefResolution + ) {} -export class CdpTextInputCommands extends CdpBridgeCommandModule { fill(element: string, value: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) await refSender('DOM.focus', { backendNodeId: node.backendDOMNodeId }) @@ -54,10 +62,10 @@ export class CdpTextInputCommands extends CdpBridgeCommandModule { } type(input: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) await insertTextThroughCdp(sender, input) return { typed: true } @@ -65,13 +73,13 @@ export class CdpTextInputCommands extends CdpBridgeCommandModule { } focus(element: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) await refSender('DOM.focus', { backendNodeId: node.backendDOMNodeId }) return { focused: element } @@ -79,13 +87,13 @@ export class CdpTextInputCommands extends CdpBridgeCommandModule { } clear(element: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) const { nodeId } = (await refSender('DOM.requestNode', { backendNodeId: node.backendDOMNodeId @@ -108,13 +116,13 @@ export class CdpTextInputCommands extends CdpBridgeCommandModule { } selectAll(element: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) - const node = await this.resolveRef(guest, sender, element) - const refSender = this.senderForRef(guest, node) + const node = await this.refResolution.resolveRef(guest, sender, element) + const refSender = this.refResolution.senderForRef(guest, node) await refSender('DOM.focus', { backendNodeId: node.backendDOMNodeId }) await sender('Input.dispatchKeyEvent', { @@ -133,10 +141,10 @@ export class CdpTextInputCommands extends CdpBridgeCommandModule { } keypress(key: string): Promise { - return this.enqueueCommand(async () => { - const guest = this.getActiveGuest() - const sender = this.makeCdpSender(guest) - await this.ensureDebuggerAttached(guest) + return this.bridgeState.enqueueCommand(async () => { + const guest = this.bridgeState.getActiveGuest() + const sender = this.debuggerLifecycle.makeCdpSender(guest) + await this.debuggerLifecycle.ensureDebuggerAttached(guest) const keyDef = resolveKeyDefinition(key) await sender('Input.dispatchKeyEvent', { diff --git a/src/main/browser/cdp/snapshots/snapshot-cursor-interactive-elements.ts b/src/main/browser/cdp/snapshots/snapshot-cursor-interactive-elements.ts index 997e8d9dd74..2bebb5dd7cb 100644 --- a/src/main/browser/cdp/snapshots/snapshot-cursor-interactive-elements.ts +++ b/src/main/browser/cdp/snapshots/snapshot-cursor-interactive-elements.ts @@ -1,5 +1,5 @@ import type { SnapshotEntry } from './snapshot-ax-tree-walk' -import type { CdpCommandSender } from './snapshot-engine' +import type { CdpCommandSender } from '../debugger/cdp-command-sender' // Find controls omitted from the accessibility tree, such as styled div buttons. export async function findCursorInteractiveElements( diff --git a/src/main/browser/cdp/snapshots/snapshot-engine.test.ts b/src/main/browser/cdp/snapshots/snapshot-engine.test.ts index 4369966d33f..a4aaf46753d 100644 --- a/src/main/browser/cdp/snapshots/snapshot-engine.test.ts +++ b/src/main/browser/cdp/snapshots/snapshot-engine.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it, vi } from 'vitest' -import { buildSnapshot, type CdpCommandSender } from './snapshot-engine' +import { buildSnapshot } from './snapshot-engine' +import type { CdpCommandSender } from '../debugger/cdp-command-sender' type AXNode = { nodeId: string diff --git a/src/main/browser/cdp/snapshots/snapshot-engine.ts b/src/main/browser/cdp/snapshots/snapshot-engine.ts index c8d7090ed69..344104e8b3a 100644 --- a/src/main/browser/cdp/snapshots/snapshot-engine.ts +++ b/src/main/browser/cdp/snapshots/snapshot-engine.ts @@ -1,12 +1,8 @@ +import type { CdpCommandSender } from '../debugger/cdp-command-sender' import type { BrowserSnapshotRef } from '../../../../shared/runtime-browser-contracts' import { walkTree, type AXNode, type SnapshotEntry } from './snapshot-ax-tree-walk' import { findCursorInteractiveElements } from './snapshot-cursor-interactive-elements' -export type CdpCommandSender = ( - method: string, - params?: Record -) => Promise - export type RefEntry = { backendDOMNodeId: number role: string diff --git a/src/main/browser/cdp/commands/cdp-client-command.ts b/src/main/browser/cdp/transport/cdp-client-command.ts similarity index 100% rename from src/main/browser/cdp/commands/cdp-client-command.ts rename to src/main/browser/cdp/transport/cdp-client-command.ts diff --git a/src/main/browser/cdp/commands/cdp-synthetic-session-registry.ts b/src/main/browser/cdp/transport/cdp-synthetic-session-registry.ts similarity index 100% rename from src/main/browser/cdp/commands/cdp-synthetic-session-registry.ts rename to src/main/browser/cdp/transport/cdp-synthetic-session-registry.ts diff --git a/src/main/browser/cdp/transport/cdp-target-discovery.ts b/src/main/browser/cdp/transport/cdp-target-discovery.ts index 996432f0922..75db7c36ee1 100644 --- a/src/main/browser/cdp/transport/cdp-target-discovery.ts +++ b/src/main/browser/cdp/transport/cdp-target-discovery.ts @@ -2,7 +2,7 @@ import type { WebSocket } from 'ws' import type { ServerResponse } from 'node:http' import type { WebContents } from 'electron' import type { CdpClientResponseWriter } from './cdp-client-response-writer' -import type { CdpSyntheticSessionRegistry } from '../commands/cdp-synthetic-session-registry' +import type { CdpSyntheticSessionRegistry } from './cdp-synthetic-session-registry' export class CdpTargetDiscovery { constructor( diff --git a/src/main/browser/cdp/transport/cdp-ws-proxy.ts b/src/main/browser/cdp/transport/cdp-ws-proxy.ts index 13e2f8d2090..d0ca4f49e87 100644 --- a/src/main/browser/cdp/transport/cdp-ws-proxy.ts +++ b/src/main/browser/cdp/transport/cdp-ws-proxy.ts @@ -3,18 +3,18 @@ import { createServer, type Server } from 'node:http' import { randomBytes } from 'node:crypto' import type { WebContents } from 'electron' import { CdpClientResponseWriter } from './cdp-client-response-writer' -import { CdpSyntheticSessionRegistry } from '../commands/cdp-synthetic-session-registry' +import { CdpSyntheticSessionRegistry } from './cdp-synthetic-session-registry' import { CdpTargetDiscovery } from './cdp-target-discovery' -import { CdpDebuggerChannel } from '../commands/cdp-debugger-channel' +import { CdpDebuggerChannel } from '../debugger/cdp-debugger-channel' import { CdpPageNavigationCommands } from '../commands/cdp-page-navigation-commands' -import { CdpDomFocusReplay } from '../commands/cdp-dom-focus-replay' +import { CdpDomFocusReplay } from '../input/cdp-dom-focus-replay' import { CdpElementCapture } from '../commands/cdp-element-capture' import { CdpPageCaptureCommands } from '../commands/cdp-page-capture-commands' -import type { CapturePaintHold } from '../commands/cdp-screenshot' +import type { CapturePaintHold } from '../debugger/guest-paint-hold' import type { AgentInputFocusGuard } from '../input/agent-input-focus-guard' -import { CdpPageInputCommands, type ShowAgentPointer } from '../commands/cdp-page-input-commands' +import { CdpPageInputCommands, type ShowAgentPointer } from '../input/cdp-page-input-commands' import { admitCdpProxyRequest, verifyCdpProxyUpgrade } from './cdp-loopback-request-guard' -import { parseCdpClientCommand } from '../commands/cdp-client-command' +import { parseCdpClientCommand } from './cdp-client-command' import { assertRemoteCliPageAccess, runUnderRemoteCliPageAdmission diff --git a/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-composition.ts b/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-composition.ts index adacf4a1e07..173d5844819 100644 --- a/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-composition.ts +++ b/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-composition.ts @@ -11,7 +11,7 @@ import { closeBrowserClientHostComposition, fenceBrowserClientHostComposition } from './paired-runtime-browser-client-host-teardown' -import type { BrowserClientPageNetworkRoute } from '../../client-pages/lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageNetworkRoute } from '../../network/client-routes/browser-client-page-network-route' import type { BrowserClientPageAuthorityIdentity as BrowserClientHostAuthorityTransitionInput } from '../../client-pages/execution/browser-client-page-command-executor-dependencies' import { type ComposedBrowserClientNetworkRoutes, diff --git a/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-route-sets.ts b/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-route-sets.ts index 9482333d403..9283cc429aa 100644 --- a/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-route-sets.ts +++ b/src/main/browser/client-hosts/lifecycle/paired-runtime-browser-client-host-route-sets.ts @@ -1,5 +1,5 @@ import type { BrowserClientHostLeaseAuthority } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' -import type { BrowserClientPageNetworkRoute } from '../../client-pages/lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageNetworkRoute } from '../../network/client-routes/browser-client-page-network-route' import { sameBrowserClientHostLeaseAuthority } from '../commands/browser-client-host-command-authority' export type ComposedBrowserClientNetworkRoutes = { diff --git a/src/main/browser/client-pages/execution/browser-client-page-command-admission.ts b/src/main/browser/client-pages/execution/browser-client-page-command-admission.ts index c72a404af4d..a80377935a2 100644 --- a/src/main/browser/client-pages/execution/browser-client-page-command-admission.ts +++ b/src/main/browser/client-pages/execution/browser-client-page-command-admission.ts @@ -1,21 +1,6 @@ -import type { BrowserClientHostCommandEvent } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' -import type { - BrowserClientPageRenderer, - BrowserClientPageRendererIdentity -} from '../lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageRenderer } from '../renderer/browser-client-page-renderer-contract' import { BrowserClientPageCommandError } from './browser-client-page-command-failure' -export function browserClientPageIdentity( - page: Pick, - partition: string -): BrowserClientPageRendererIdentity { - return { - partition, - browserPageId: page.browserPageId, - pageHostGeneration: page.pageHostGeneration - } -} - export function assertCurrentBrowserClientPageRenderer(renderer: BrowserClientPageRenderer): void { if ( !Number.isInteger(renderer.rendererWebContentsId) || diff --git a/src/main/browser/client-pages/execution/browser-client-page-command-executor-dependencies.ts b/src/main/browser/client-pages/execution/browser-client-page-command-executor-dependencies.ts index 48bb6dc53c3..b164589ba7a 100644 --- a/src/main/browser/client-pages/execution/browser-client-page-command-executor-dependencies.ts +++ b/src/main/browser/client-pages/execution/browser-client-page-command-executor-dependencies.ts @@ -2,10 +2,8 @@ import type { BrowserClientAutomationMethod } from '../../../../shared/browser/c import type { BrowserClientFileChannelTransport } from '../../client-hosts/file-transfers/browser-client-file-channel-transport' import type { BrowserClientPageGuestBinding } from '../lifecycle/browser-client-page-guest-binding' import type { BrowserClientUploadStaging } from '../../client-hosts/file-transfers/browser-client-upload-staging' -import type { - BrowserClientPageNetworkRoute, - BrowserClientPageRenderer -} from '../lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageNetworkRoute } from '../../network/client-routes/browser-client-page-network-route' +import type { BrowserClientPageRenderer } from '../renderer/browser-client-page-renderer-contract' import type { BrowserRouteSessionRegistry } from '../../routing/sessions/browser-route-session-registry' import type { BrowserClientPageLifecycleRegistry, diff --git a/src/main/browser/client-pages/execution/browser-client-page-command-executor.ts b/src/main/browser/client-pages/execution/browser-client-page-command-executor.ts index ac4eb302d99..34c9b6453e5 100644 --- a/src/main/browser/client-pages/execution/browser-client-page-command-executor.ts +++ b/src/main/browser/client-pages/execution/browser-client-page-command-executor.ts @@ -9,10 +9,8 @@ import { type BrowserClientHostCommandEvent } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' import type { BrowserClientAutomationResult as BrowserClientHostCommandResult } from '../../../../shared/browser/client-hosting/browser-client-automation-protocol' -import { - cleanupRetainedBrowserClientPage, - type BrowserClientPageRendererIdentity -} from '../lifecycle/browser-client-page-cleanup' +import { cleanupRetainedBrowserClientPage } from '../lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageRendererIdentity } from '../../../../shared/browser/client-hosting/browser-client-page-renderer-protocol' import { assertBrowserClientPageAdmission } from '../lifecycle/browser-client-page-admission' import { createReservedBrowserClientPage } from '../lifecycle/browser-client-page-creation' import { retireSupersededExecutionHostPages } from '../lifecycle/browser-client-page-execution-host-supersession' @@ -279,7 +277,7 @@ export class BrowserClientPageCommandExecutor { private async cleanupPage( page: BrowserClientRetainedPage, - previousRendererPage?: BrowserClientPageRendererIdentity, + previousRendererPage?: Readonly, releaseUploads = true ): Promise { page.releaseAvailabilityWatch?.() diff --git a/src/main/browser/client-pages/lifecycle/browser-client-page-cleanup.ts b/src/main/browser/client-pages/lifecycle/browser-client-page-cleanup.ts index 4d7f211c930..f0dddd8ce5b 100644 --- a/src/main/browser/client-pages/lifecycle/browser-client-page-cleanup.ts +++ b/src/main/browser/client-pages/lifecycle/browser-client-page-cleanup.ts @@ -1,47 +1,19 @@ +import type { BrowserClientPageNetworkRoute } from '../../network/client-routes/browser-client-page-network-route' +import type { BrowserClientPageRenderer } from '../renderer/browser-client-page-renderer-contract' +import type { BrowserClientPageRendererIdentity } from '../../../../shared/browser/client-hosting/browser-client-page-renderer-protocol' import type { BrowserRouteGuestLifecycleClaim } from '../../routing/guest-authority/browser-route-page-authority' import type { BrowserRouteSessionHandle } from '../../routing/sessions/browser-route-session-state' -import type { BrowserRouteProxyEndpoint } from '../../routing/sessions/browser-route-session-policy' import type { BrowserRouteWebContentsRegistry } from '../../routing/guest-authority/browser-route-webcontents-registry' -import { browserClientPageIdentity } from '../execution/browser-client-page-command-admission' +import { browserClientPageIdentity } from '../renderer/browser-client-page-renderer-contract' import type { BrowserClientRetainedPage } from './browser-client-page-retained-state' -export type BrowserClientPageNetworkRoute = { - key: string - executionHostIdentity: string - /** Pre-migration identity of the same host, so its existing partition can be adopted. */ - legacyExecutionHostIdentity: string - proxyEndpoint: BrowserRouteProxyEndpoint - release(): void | Promise -} - -export type BrowserClientPageRendererIdentity = Readonly<{ - partition: string - browserPageId: string - pageHostGeneration: number -}> - -export type BrowserClientPageRenderer = { - rendererWebContentsId: number - isCurrent(): boolean - mountPage( - page: BrowserClientPageRendererIdentity, - signal: AbortSignal - ): Promise<{ webContentsId: number }> - rekeyPage?( - previous: BrowserClientPageRendererIdentity, - next: BrowserClientPageRendererIdentity, - signal: AbortSignal - ): Promise - retirePage(page: BrowserClientPageRendererIdentity): void | Promise -} - export async function cleanupBrowserClientPage( routeWebContents: Pick, target: { guestMayExist: boolean lifecycleClaim: BrowserRouteGuestLifecycleClaim | null renderer: BrowserClientPageRenderer | null - rendererPages: readonly BrowserClientPageRendererIdentity[] + rendererPages: readonly Readonly[] routeSession: BrowserRouteSessionHandle | null route: BrowserClientPageNetworkRoute | null } @@ -94,7 +66,7 @@ export async function cleanupRetainedBrowserClientPage( }): Promise releaseUploadStaging?(): void | Promise }, - previousRendererPage?: BrowserClientPageRendererIdentity + previousRendererPage?: Readonly ): Promise { const failures: unknown[] = [] try { diff --git a/src/main/browser/client-pages/lifecycle/browser-client-page-creation.ts b/src/main/browser/client-pages/lifecycle/browser-client-page-creation.ts index 0468d4ba519..63d6a2ce0d4 100644 --- a/src/main/browser/client-pages/lifecycle/browser-client-page-creation.ts +++ b/src/main/browser/client-pages/lifecycle/browser-client-page-creation.ts @@ -1,14 +1,12 @@ import type { BrowserClientHostCommandEvent } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' -import { - cleanupBrowserClientPage, - type BrowserClientPageNetworkRoute as RetainedNetworkRoute, - type BrowserClientPageRenderer -} from './browser-client-page-cleanup' +import { cleanupBrowserClientPage } from './browser-client-page-cleanup' +import type { BrowserClientPageNetworkRoute as RetainedNetworkRoute } from '../../network/client-routes/browser-client-page-network-route' +import type { BrowserClientPageRenderer } from '../renderer/browser-client-page-renderer-contract' import { assertBrowserClientPageCommandNotAborted, - assertCurrentBrowserClientPageRenderer, - browserClientPageIdentity + assertCurrentBrowserClientPageRenderer } from '../execution/browser-client-page-command-admission' +import { browserClientPageIdentity } from '../renderer/browser-client-page-renderer-contract' import type { BrowserClientPageAuthorityIdentity } from '../execution/browser-client-page-command-executor-dependencies' import { BrowserClientPageCommandError } from '../execution/browser-client-page-command-failure' import type { BrowserClientPageGuestBinding } from './browser-client-page-guest-binding' diff --git a/src/main/browser/client-pages/lifecycle/browser-client-page-execution-host-supersession.ts b/src/main/browser/client-pages/lifecycle/browser-client-page-execution-host-supersession.ts index ce4b8c85650..8e552551e7e 100644 --- a/src/main/browser/client-pages/lifecycle/browser-client-page-execution-host-supersession.ts +++ b/src/main/browser/client-pages/lifecycle/browser-client-page-execution-host-supersession.ts @@ -1,4 +1,4 @@ -import type { BrowserClientPageNetworkRoute } from './browser-client-page-cleanup' +import type { BrowserClientPageNetworkRoute } from '../../network/client-routes/browser-client-page-network-route' import type { BrowserClientRetainedPage } from './browser-client-page-retained-state' type SupersedingRoute = Pick diff --git a/src/main/browser/client-pages/lifecycle/browser-client-page-inventory.ts b/src/main/browser/client-pages/lifecycle/browser-client-page-inventory.ts index 34882246c2c..a25911e50a4 100644 --- a/src/main/browser/client-pages/lifecycle/browser-client-page-inventory.ts +++ b/src/main/browser/client-pages/lifecycle/browser-client-page-inventory.ts @@ -8,7 +8,7 @@ import { type BrowserClientHostCommandEvent } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' import { BrowserClientPageMetadataParams } from '../../../../shared/browser/client-hosting/browser-client-page-metadata-protocol' -import type { BrowserClientPageRenderer } from './browser-client-page-cleanup' +import type { BrowserClientPageRenderer } from '../renderer/browser-client-page-renderer-contract' import { BrowserClientPageCommandError } from '../execution/browser-client-page-command-failure' import type { BrowserRouteGuestLifecycleClaim } from '../../routing/guest-authority/browser-route-page-authority' diff --git a/src/main/browser/client-pages/lifecycle/browser-client-page-reconciliation.ts b/src/main/browser/client-pages/lifecycle/browser-client-page-reconciliation.ts index 5f03d4eb3c1..e46fb501b5c 100644 --- a/src/main/browser/client-pages/lifecycle/browser-client-page-reconciliation.ts +++ b/src/main/browser/client-pages/lifecycle/browser-client-page-reconciliation.ts @@ -4,9 +4,9 @@ import type { } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' import { assertBrowserClientPageCommandNotAborted, - assertCurrentBrowserClientPageRenderer, - browserClientPageIdentity + assertCurrentBrowserClientPageRenderer } from '../execution/browser-client-page-command-admission' +import { browserClientPageIdentity } from '../renderer/browser-client-page-renderer-contract' import { sameBrowserClientPageAuthority } from '../../client-hosts/commands/browser-client-host-command-authority' import { BrowserClientPageCommandError } from '../execution/browser-client-page-command-failure' import { @@ -17,7 +17,7 @@ import type { BrowserClientPageLifecycleRegistry, BrowserClientRetainedPage } from './browser-client-page-retained-state' -import type { BrowserClientPageRendererIdentity } from './browser-client-page-cleanup' +import type { BrowserClientPageRendererIdentity } from '../../../../shared/browser/client-hosting/browser-client-page-renderer-protocol' type BrowserClientPageReconciliationContext = { pages: Map @@ -29,7 +29,7 @@ type BrowserClientPageReconciliationContext = { retirePage: (browserPageId: string, pageHostGeneration: number) => Promise cleanupPage: ( page: BrowserClientRetainedPage, - previousRendererPage?: BrowserClientPageRendererIdentity + previousRendererPage?: Readonly ) => Promise } @@ -200,7 +200,7 @@ async function failClosedRekeyedPage( context: BrowserClientPageReconciliationContext, page: BrowserClientRetainedPage, error: unknown, - previousRendererPage?: BrowserClientPageRendererIdentity + previousRendererPage?: Readonly ): Promise { page.reconciling = false page.retiring ??= context.cleanupPage(page, previousRendererPage) diff --git a/src/main/browser/client-pages/lifecycle/browser-client-page-retained-state.ts b/src/main/browser/client-pages/lifecycle/browser-client-page-retained-state.ts index 12726b4326a..4669535d9a3 100644 --- a/src/main/browser/client-pages/lifecycle/browser-client-page-retained-state.ts +++ b/src/main/browser/client-pages/lifecycle/browser-client-page-retained-state.ts @@ -1,8 +1,6 @@ import type { BrowserClientHostedPageInventory } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' -import type { - BrowserClientPageNetworkRoute, - BrowserClientPageRenderer -} from './browser-client-page-cleanup' +import type { BrowserClientPageNetworkRoute } from '../../network/client-routes/browser-client-page-network-route' +import type { BrowserClientPageRenderer } from '../renderer/browser-client-page-renderer-contract' import type { BrowserRouteGuestLifecycleClaim, BrowserRoutePageGuestIdentity diff --git a/src/main/browser/client-pages/renderer/browser-client-page-renderer-bridge.ts b/src/main/browser/client-pages/renderer/browser-client-page-renderer-bridge.ts index cdfaee7d68a..a4f2378b6de 100644 --- a/src/main/browser/client-pages/renderer/browser-client-page-renderer-bridge.ts +++ b/src/main/browser/client-pages/renderer/browser-client-page-renderer-bridge.ts @@ -8,7 +8,7 @@ import { type BrowserClientPageRendererReply as RendererReply, type BrowserClientPageRendererRequest as RendererRequest } from '../../../../shared/browser/client-hosting/browser-client-page-renderer-protocol' -import type { BrowserClientPageRenderer } from '../lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageRenderer } from './browser-client-page-renderer-contract' import { readCurrentRendererFrame, readRendererReplyRequestId, diff --git a/src/main/browser/client-pages/renderer/browser-client-page-renderer-contract.ts b/src/main/browser/client-pages/renderer/browser-client-page-renderer-contract.ts new file mode 100644 index 00000000000..e0176154708 --- /dev/null +++ b/src/main/browser/client-pages/renderer/browser-client-page-renderer-contract.ts @@ -0,0 +1,28 @@ +import type { BrowserClientHostCommandEvent } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' +import type { BrowserClientPageRendererIdentity } from '../../../../shared/browser/client-hosting/browser-client-page-renderer-protocol' + +export type BrowserClientPageRenderer = { + rendererWebContentsId: number + isCurrent(): boolean + mountPage( + page: Readonly, + signal: AbortSignal + ): Promise<{ webContentsId: number }> + rekeyPage?( + previous: Readonly, + next: Readonly, + signal: AbortSignal + ): Promise + retirePage(page: Readonly): void | Promise +} + +export function browserClientPageIdentity( + page: Pick, + partition: string +): Readonly { + return { + partition, + browserPageId: page.browserPageId, + pageHostGeneration: page.pageHostGeneration + } +} diff --git a/src/main/browser/client-pages/renderer/browser-client-page-renderer-runtime.ts b/src/main/browser/client-pages/renderer/browser-client-page-renderer-runtime.ts index 57dd5c2a4c5..776e456f414 100644 --- a/src/main/browser/client-pages/renderer/browser-client-page-renderer-runtime.ts +++ b/src/main/browser/client-pages/renderer/browser-client-page-renderer-runtime.ts @@ -1,6 +1,6 @@ import { ipcMain } from 'electron' import { BROWSER_CLIENT_PAGE_RENDERER_REPLY_CHANNEL } from '../../../../shared/browser/client-hosting/browser-client-page-renderer-protocol' -import type { BrowserClientPageRenderer } from '../lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageRenderer } from './browser-client-page-renderer-contract' import { BrowserClientPageRendererBridgeRegistry } from './browser-client-page-renderer-bridge' import type { BrowserClientPageRendererEndpoint } from './browser-client-page-renderer-reply-admission' diff --git a/src/main/browser/document-preview/doc-preview-file-reader.ts b/src/main/browser/document-preview/doc-preview-file-reader.ts index 457b19e8eef..69597092c42 100644 --- a/src/main/browser/document-preview/doc-preview-file-reader.ts +++ b/src/main/browser/document-preview/doc-preview-file-reader.ts @@ -20,6 +20,7 @@ import { } from './doc-preview-grant-registry' import { getRuntimeEnvironmentRegistryRoot } from '../../ipc/runtime-environments/runtime-environment-registry-root' import { readDocPreviewTerminalFile } from './doc-preview-terminal-file-reader' +import type { RuntimeDocPreviewReadParams } from './doc-preview-runtime-read-contract' const DOC_PREVIEW_READ_TIMEOUT_MS = 15_000 // Why: under files.readDocPreviewChunk's 512 KiB cap, and a multiple of 3 so its base64 has no padding. @@ -109,14 +110,6 @@ function toOutcome(source: PreviewFileBytes, contentType: string): DocPreviewRea : UNSUPPORTED_ASSET_OUTCOME } -export type RuntimeDocPreviewReadParams = { - worktree: string - relativePath: string - entryRelativePath: string - implicitRootRelativePath: string | null - authorizedRootRelativePaths: string[] -} - async function readRuntimeDocPreviewFile( environmentId: string, params: RuntimeDocPreviewReadParams diff --git a/src/main/browser/document-preview/doc-preview-runtime-read-contract.ts b/src/main/browser/document-preview/doc-preview-runtime-read-contract.ts new file mode 100644 index 00000000000..fcccc32afd0 --- /dev/null +++ b/src/main/browser/document-preview/doc-preview-runtime-read-contract.ts @@ -0,0 +1,7 @@ +export type RuntimeDocPreviewReadParams = { + worktree: string + relativePath: string + entryRelativePath: string + implicitRootRelativePath: string | null + authorizedRootRelativePaths: string[] +} diff --git a/src/main/browser/document-preview/doc-preview-terminal-file-reader.ts b/src/main/browser/document-preview/doc-preview-terminal-file-reader.ts index 47862cf864f..3e024512e7b 100644 --- a/src/main/browser/document-preview/doc-preview-terminal-file-reader.ts +++ b/src/main/browser/document-preview/doc-preview-terminal-file-reader.ts @@ -8,7 +8,7 @@ import { toRuntimeWorktreeRelativePath, type DocPreviewGrant } from './doc-preview-grant-registry' -import type { RuntimeDocPreviewReadParams } from './doc-preview-file-reader' +import type { RuntimeDocPreviewReadParams } from './doc-preview-runtime-read-contract' import { resolveEnvironment } from '../../../shared/remote-runtime/host-store/runtime-environment-store' const TIMEOUT_MS = 15_000 diff --git a/src/main/browser/network/client-routes/browser-client-network-route-registry.ts b/src/main/browser/network/client-routes/browser-client-network-route-registry.ts index 8bc5455aad2..f474f3fefd2 100644 --- a/src/main/browser/network/client-routes/browser-client-network-route-registry.ts +++ b/src/main/browser/network/client-routes/browser-client-network-route-registry.ts @@ -1,6 +1,6 @@ import type { BrowserHostLeaseAuthority } from '../../../../shared/browser/client-hosting/browser-client-host-protocol' import type { BrowserNetworkExecutionHost } from '../../../../shared/browser/client-hosting/browser-network-execution-host' -import type { BrowserClientPageNetworkRoute } from '../../client-pages/lifecycle/browser-client-page-cleanup' +import type { BrowserClientPageNetworkRoute } from './browser-client-page-network-route' import { assertBrowserClientNetworkRouteAddress, sameBrowserClientNetworkRouteAddress diff --git a/src/main/browser/network/client-routes/browser-client-page-network-route.ts b/src/main/browser/network/client-routes/browser-client-page-network-route.ts new file mode 100644 index 00000000000..7de7789e114 --- /dev/null +++ b/src/main/browser/network/client-routes/browser-client-page-network-route.ts @@ -0,0 +1,10 @@ +import type { BrowserRouteProxyEndpoint } from '../../routing/sessions/browser-route-session-policy' + +export type BrowserClientPageNetworkRoute = { + key: string + executionHostIdentity: string + /** Pre-migration identity of the same host, so its existing partition can be adopted. */ + legacyExecutionHostIdentity: string + proxyEndpoint: BrowserRouteProxyEndpoint + release(): void | Promise +} From fcc072c37d70da1b1d6bf91b3ba3a2c2eff616c8 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Sun, 8 Nov 2026 16:50:02 -0800 Subject: [PATCH 09/80] test: remove 12 low-value recipe, paste and worker tests (#40041) Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .../structured-worker-fleet-liveness.test.ts | 10 -- .../paste/native-chat-pasted-links.test.ts | 34 ----- .../ephemeral-vm-recipe-timeouts.test.ts | 116 ------------------ 3 files changed, 160 deletions(-) delete mode 100644 src/renderer/src/components/native-chat/paste/native-chat-pasted-links.test.ts delete mode 100644 src/shared/ephemeral-vm-recipe-timeouts.test.ts diff --git a/src/main/runtime/rpc/methods/orchestration/worker/structured-worker-fleet-liveness.test.ts b/src/main/runtime/rpc/methods/orchestration/worker/structured-worker-fleet-liveness.test.ts index 83d6b27877f..efaf9137e9d 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/structured-worker-fleet-liveness.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/structured-worker-fleet-liveness.test.ts @@ -132,16 +132,6 @@ describe('worker-list liveness for chat-mode workers', () => { }).workers[0]! } - it('reports a running Codex worker live and working, with nothing for the coordinator to do', () => { - installHost(record('codex'), true) - const worker = project([sessionStatus('working', NOW - 5 * 60 * 1000)]) - - expect(worker.liveness).toEqual({ verdict: 'live', observedAt: NOW, source: 'execution_host' }) - expect(worker.stage.activity).toBe('working') - expect(worker.evidence.lastObservedAt).toBe(NOW - 5 * 60 * 1000) - expect(worker.attention).toEqual({ categories: [], requiresAction: false }) - }) - it('keeps an idle Claude chat worker live after its last turn is hours old', () => { installHost(record('claude'), true) const worker = project([sessionStatus('done', NOW - 2 * HOUR)]) diff --git a/src/renderer/src/components/native-chat/paste/native-chat-pasted-links.test.ts b/src/renderer/src/components/native-chat/paste/native-chat-pasted-links.test.ts deleted file mode 100644 index 11eb52f9896..00000000000 --- a/src/renderer/src/components/native-chat/paste/native-chat-pasted-links.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -// @vitest-environment happy-dom -import { describe, expect, it } from 'vitest' -import { linkedSelectionText, withPastedLinks } from './native-chat-pasted-links' - -describe('withPastedLinks', () => { - it('keeps rich-text links as Markdown around the plain text', () => { - const html = - '

Hey can you take a look at this\n FR and [the] docs?

' - expect(withPastedLinks('Hey can you take a look at this FR and [the] docs?', html)).toBe( - 'Hey can you take a look at [this FR](https://github.com/stablyai/orca/issues/39150) and [\\[the\\] docs](https://example.com/a%28b%29)?' - ) - }) - - it('leaves bare URLs, unsafe schemes and existing Markdown alone', () => { - const html = - 'https://example.com/ run x' - const text = 'https://example.com/ run [x](https://x.dev)' - expect(withPastedLinks(text, html)).toBe(text) - }) -}) - -describe('linkedSelectionText', () => { - it('links the selected words to a pasted URL', () => { - expect(linkedSelectionText('external links', ' https://example.com\n')).toBe( - '[external links](https://example.com)' - ) - }) - - it('replaces instead when the selection or paste is not a word-to-URL pair', () => { - expect(linkedSelectionText('https://old.dev', 'https://new.dev')).toBeNull() - expect(linkedSelectionText('two\nlines', 'https://new.dev')).toBeNull() - expect(linkedSelectionText('words', 'see https://new.dev')).toBeNull() - }) -}) diff --git a/src/shared/ephemeral-vm-recipe-timeouts.test.ts b/src/shared/ephemeral-vm-recipe-timeouts.test.ts deleted file mode 100644 index c618bf299c3..00000000000 --- a/src/shared/ephemeral-vm-recipe-timeouts.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { describe, expect, it } from 'vitest' -import { - formatEphemeralVmRecipeTimeout, - getEphemeralVmRecipeTimeoutSeconds, - parseEphemeralVmRecipeTimeoutSeconds, - toEphemeralVmRuntimeRecipeSnapshot -} from './ephemeral-vm-recipe-timeouts' -import { ephemeralVmTimeoutFailure } from './ephemeral-vm-failure-copy' -import type { OrcaVmRecipe } from './orca-yaml-hook-types' -import { parseOrcaYaml } from './orca-yaml' - -describe('ephemeral VM recipe timeouts', () => { - it('applies no limit unless the recipe sets one', () => { - const recipe: OrcaVmRecipe = { id: 'r', name: 'R', create: './create.sh' } - for (const mode of ['create', 'suspend', 'resume', 'destroy'] as const) { - expect(getEphemeralVmRecipeTimeoutSeconds(recipe, mode)).toBeUndefined() - } - expect( - getEphemeralVmRecipeTimeoutSeconds({ ...recipe, timeoutSeconds: { create: 90 } }, 'create') - ).toBe(90) - expect( - getEphemeralVmRecipeTimeoutSeconds({ ...recipe, timeoutSeconds: { create: 90 } }, 'destroy') - ).toBeUndefined() - }) - - it('rejects values that are not whole seconds in range', () => { - for (const value of [0, -1, 1.5, 86_401, '60', [60], { create: '60' }, { create: 0 }]) { - expect(parseEphemeralVmRecipeTimeoutSeconds(value).ok).toBe(false) - } - expect(parseEphemeralVmRecipeTimeoutSeconds({})).toEqual({ ok: true }) - expect(parseEphemeralVmRecipeTimeoutSeconds(86_400)).toMatchObject({ ok: true }) - }) - - it('formats the limit compactly', () => { - expect(formatEphemeralVmRecipeTimeout(45)).toBe('45s') - expect(formatEphemeralVmRecipeTimeout(900)).toBe('15m') - expect(formatEphemeralVmRecipeTimeout(5_400)).toBe('1h 30m') - }) - - it('tells the user which step stopped and how to raise the limit', () => { - expect(ephemeralVmTimeoutFailure('create', 1_800)).toEqual({ - error: - 'The recipe create step did not finish within 30m, so Orca stopped it. Check the recipe output, or raise timeoutSeconds in orca.yaml if it needs longer.', - errorCode: 'recipeTimedOut', - errorParams: { mode: 'create', timeout: '30m', timeoutSeconds: 1_800 } - }) - }) - - it('drops timeoutSeconds from the persisted recipe snapshot', () => { - const snapshot = toEphemeralVmRuntimeRecipeSnapshot({ - id: 'r', - name: 'R', - create: './create.sh', - destroy: './destroy.sh', - timeoutSeconds: { create: 60 } - }) - expect(snapshot).toEqual({ id: 'r', name: 'R', create: './create.sh', destroy: './destroy.sh' }) - }) -}) - -describe('orca.yaml recipe timeoutSeconds', () => { - it('parses recipe timeoutSeconds as one limit or a per-step map', () => { - const yaml = [ - 'environmentRecipes:', - ' - id: all-steps', - ' name: All Steps', - ' create: ./create.sh', - ' timeoutSeconds: 120', - ' - id: per-step', - ' name: Per Step', - ' create: ./create.sh', - ' timeoutSeconds:', - ' create: 1800', - ' destroy: 60' - ].join('\n') - - expect(parseOrcaYaml(yaml)?.environmentRecipes).toEqual([ - { - id: 'all-steps', - name: 'All Steps', - create: './create.sh', - timeoutSeconds: { create: 120, suspend: 120, resume: 120, destroy: 120 } - }, - { - id: 'per-step', - name: 'Per Step', - create: './create.sh', - timeoutSeconds: { create: 1800, destroy: 60 } - } - ]) - }) - - it('keeps a recipe with an invalid timeoutSeconds without limits and reports it', () => { - const yaml = [ - 'environmentRecipes:', - ' - id: cloud-sandbox', - ' name: Cloud Sandbox', - ' create: ./create.sh', - ' timeoutSeconds:', - ' boot: 60' - ].join('\n') - - expect(parseOrcaYaml(yaml)).toEqual({ - scripts: {}, - environmentRecipes: [{ id: 'cloud-sandbox', name: 'Cloud Sandbox', create: './create.sh' }], - environmentRecipeDiagnostics: [ - { - index: 0, - field: 'timeoutSeconds', - message: - 'Recipe "cloud-sandbox" timeoutSeconds.boot is not a lifecycle step; use create, suspend, resume, or destroy. Steps keep no time limit.' - } - ] - }) - }) -}) From 74106a0dbcb1bf2b7a919364df632f71c78cf112 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:58:21 -0800 Subject: [PATCH 10/80] Add protected telemetry milestones (#40043) * Add protected telemetry milestones * Fix milestone provenance and acceptance * Fix telemetry test lint suppression * Relay remote accepted chat prompts to desktop telemetry * Honor paired desktop consent when chat host runs telemetry * Fix mobile typecheck for notification retry timer --- .../remote-accepted-prompts.md | 17 ++ docs/site/content/docs/telemetry.mdx | 1 + src/main/ipc/pty/ipc/write.ts | 8 + .../ipc/pty/pty-write-ipc-validation.test.ts | 25 +- ...ntime-environment-prompt-telemetry.test.ts | 106 +++++++ ...runtime-environment-subscription-events.ts | 1 + ...etry-protected-milestone-admission.test.ts | 62 ++++ src/main/ipc/telemetry.ts | 3 + .../structured-agent-session-create.ts | 4 +- .../structured-agent-session-host-types.ts | 4 + ...ctured-agent-session-publication-wiring.ts | 2 + ...red-agent-session-prompt-telemetry.test.ts | 250 ++++++++++++++++ ...structured-agent-session-host-mutations.ts | 29 +- .../structured-agent-session-turns.ts | 18 +- .../orca-profiles/new-profile-state.test.ts | 26 +- src/main/orca-profiles/new-profile-state.ts | 12 +- .../loaded-cohort-migrations.test.ts | 46 +++ .../loading-store/loaded-cohort-migrations.ts | 35 +++ .../mobile-notification-dismissal-store.ts | 10 +- .../client-events-prompt-telemetry.test.ts | 112 +++++++ .../methods/client-events/client-events.ts | 31 +- .../creation/structured-agent-session-gate.ts | 18 ++ src/main/telemetry/burst-cap.ts | 6 + .../telemetry/client-prompt-sampling.test.ts | 9 +- .../client-protected-milestones.test.ts | 281 ++++++++++++++++++ src/main/telemetry/client.ts | 81 ++--- src/main/telemetry/event-sampling.ts | 12 +- .../native-chat-prompt-submissions.ts | 86 ++++++ src/main/telemetry/opt-out-capture.ts | 32 ++ src/main/telemetry/protected-milestones.ts | 260 ++++++++++++++++ .../telemetry/structured-chat-dispatch.ts | 54 +++- .../structured-chat-prompt-owner.test.ts | 155 ++++++++++ src/preload/api/pty-api.ts | 2 + src/preload/api/pty-bridge-session-control.ts | 1 + .../pty-input-forward-user-activity.test.ts | 123 ++++++++ .../pty-connection/input/pty-input-forward.ts | 10 + src/renderer/src/lib/telemetry.ts | 25 ++ ...tron-remote-runtime-client-capabilities.ts | 6 +- .../structured-chat-dispatch-telemetry.ts | 14 +- src/shared/telemetry-day.ts | 3 + src/shared/telemetry-event-registry.ts | 8 + .../telemetry-protected-milestone-schemas.ts | 42 +++ src/shared/telemetry-settings-types.ts | 7 + 43 files changed, 1966 insertions(+), 71 deletions(-) create mode 100644 docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md create mode 100644 src/main/ipc/runtime-environments/runtime-environment-prompt-telemetry.test.ts create mode 100644 src/main/ipc/telemetry-protected-milestone-admission.test.ts create mode 100644 src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-telemetry.test.ts create mode 100644 src/main/runtime/rpc/methods/client-events/client-events-prompt-telemetry.test.ts create mode 100644 src/main/telemetry/client-protected-milestones.test.ts create mode 100644 src/main/telemetry/native-chat-prompt-submissions.ts create mode 100644 src/main/telemetry/opt-out-capture.ts create mode 100644 src/main/telemetry/protected-milestones.ts create mode 100644 src/main/telemetry/structured-chat-prompt-owner.test.ts create mode 100644 src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward-user-activity.test.ts create mode 100644 src/shared/telemetry-day.ts create mode 100644 src/shared/telemetry-protected-milestone-schemas.ts diff --git a/docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md b/docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md new file mode 100644 index 00000000000..d894ae0f1a5 --- /dev/null +++ b/docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md @@ -0,0 +1,17 @@ +# Remote accepted prompt observations + +The committed journal acceptance observer in `native-chat-prompt-submissions.ts` owns the evidence. A fresh personal send from a transport-proven desktop or phone retains its device class and consent-owning desktop in memory until provider acceptance. Queue conversion keeps the original draft's provenance. Reservation, handover, rejected sends, withdrawn drafts, conversation commands, unattended sends and operation replay do not emit an accepted prompt observation. + +A paired desktop owner always receives its observation through the existing live structured-chat relay on `runtime.clientEvents.subscribe`, even when the execution host also has initialized desktop telemetry. That host's installation and consent do not own the paired desktop's activity. Only sends without a paired desktop owner, such as the local desktop or its directly paired phone, use the host's initialized telemetry client; disabled consent drops them without a relay fallback. The additive `structured-chat-prompt-telemetry-v1` capability isolates the new event from older readers; older hosts simply omit it. Prompt delivery and journal storage are unchanged. + +The event contains only `type`, a fresh observation UUID, `agent_kind` and `client` (`desktop` or `mobile`). No prompt text, session/message/request identifiers, paths, fingerprints, timestamps or device identifiers cross this observation channel. The owner routing key stays on the host: a paired desktop uses its authenticated device ID, and a delegated phone uses its authenticated parent desktop ID. Only an authenticated runtime desktop stream with that owner and the new capability receives the event. Another desktop, a phone stream, the CLI or a declared client without proven identity cannot receive it. A phone paired directly to the local desktop follows the existing local telemetry path. + +Both shared-control and one-shot subscription transports use the same desktop-main subscription decoder. The decoder checks the current transport generation, strictly validates the event and deduplicates its UUID before calling the consent-owning telemetry client with `runtime: remote`. It never forwards accepted observations to the renderer. The UUID is not an analytics property. The existing protected milestone admission preserves first-interaction/day ordering, installation attribution, consent and sampling behavior. UUID deduplication retains the newest 1,024 observations across desktop subscriptions; the existing per-install milestone cursor provides additional milestone deduplication. + +## Deliberate undercount limits + +- The relay is live and best effort, matching the existing agent-start observation channel. Provider acceptance while the desktop is disconnected or before its event stream opens is missed. Reconnect registers a new listener and does not backfill historical acceptance, retry prompt delivery or infer success from send receipts. Desktop restart also clears received UUIDs, but the host does not replay observations. +- Pending prompt provenance is keyed by the live journal object and capped at 256 entries per journal. Host restart or journal replacement loses it. A queued draft or an unaccepted submission persisted before restart may later be accepted without earning a milestone; operation replay and history reads cannot restore its provenance. Older pending entries evicted at the cap also undercount. +- Persisting provenance or recovering disconnected acceptance would require a separate design for durable owner identity, acceptance ordering, retention, consent changes and reader compatibility. This change deliberately adds no outbox, journal annotation or history-derived inference. A later fresh, proven send can still earn the missing milestone. + +These limits prefer missing an observation over attributing automation, replay or an unproven user action to an installation. diff --git a/docs/site/content/docs/telemetry.mdx b/docs/site/content/docs/telemetry.mdx index 21d1d016ff9..c7c503c9901 100644 --- a/docs/site/content/docs/telemetry.mdx +++ b/docs/site/content/docs/telemetry.mdx @@ -23,6 +23,7 @@ The categories of behavior we observe: - **Agent Dashboard** — when the board opens and whether it is visible during a UTC day, labeled as in-window or pop-out. Visible-use observations are limited to one per day per surface across app restarts, so a board left visible across days is counted each day. Hidden or minimized windows pause these observations. This measures visible-use days, not time spent or human attention; no card or agent contents are included. - **Repos and workspaces** — when you add a repo or create a workspace. We record _how_ you did it (e.g. folder picker vs. clone URL; command palette vs. drag-and-drop), never the repo name, URL, path, branch name, or any free-form text. For a workspace create we also record how long it took and how that time split across its steps, whether a checkout Orca had prepared in advance was reused (and if so, what reset it needed, what prepared it, how long it took to prepare and how long it waited unused; if not, a fixed reason code), for a failed create which step it stopped in, plus coarse context: whether it came from the Orca window or from the CLI, phone or agent interface, whether Git ran locally, in WSL or over SSH, a bucketed worktree count, a bucketed count of the files the repo tracks, how many other creates and prepared-checkout jobs were running, and whether the repo has a post-checkout hook (never its contents). - **Agents** — which agents you use and their token counts. Never prompts or agent output. +- **Activation and active days** — once per install profile, that a first prompt or message to an agent was observed (which agent kind and which surface: an Orca send, chat, the phone, or a prompt seen by agent hooks), and at most once per UTC day, that you took an explicit action such as launching an agent, sending a message, creating a workspace, or typing in a terminal (only which kind of action came first). Whether the profile was new when this was first recorded is included. Never the prompt, message, or anything typed. - **Agent errors** — a coarse error category and which agent kind was involved. We never see raw error messages or stack traces; per-incident detail stays in a local diagnostic trace file on your machine and only reaches Orca if you explicitly share a diagnostic bundle. - **Settings** — when you toggle one of a small whitelisted set of feature-flag or UX preferences. We record which preference changed and whether it's a boolean or an enum, never the raw value of any free-form setting. - **Tab actions** — for a consistent 10% sample of anonymous installs, the first time per app session you choose “Close Tabs To The Left” or “Close Tabs To The Right,” limited to two events per session across all windows. For that first request in each direction, we record the selected tab's type, whether it is in a workspace or floating panel, how many unpinned tabs are eligible to close, and the sampling rate. The app-open event includes sample membership so we can measure the share of sampled sessions that use each command. Repeat requests are not recorded, so these events do not measure click frequency, overall usage by tab type or panel, or total tabs closed. This counts the request, including when you cancel a save prompt; never tab titles, file paths, or workspace identifiers. diff --git a/src/main/ipc/pty/ipc/write.ts b/src/main/ipc/pty/ipc/write.ts index 30e67581775..99d03f47301 100644 --- a/src/main/ipc/pty/ipc/write.ts +++ b/src/main/ipc/pty/ipc/write.ts @@ -3,6 +3,7 @@ import { getPtyIpc } from '../pty-host-bindings' import type { OrcaRuntimeService } from '../../../runtime/orca-runtime' import { createPtyWriteInput } from './write-input' import { installPtyTerminalAttentionIpcHandler } from './terminal-attention' +import { observeTerminalUserInput } from '../../../telemetry/protected-milestones' export function installPtyWriteIpcHandlers(deps: { mainWindow?: PtyRendererDelivery @@ -22,6 +23,13 @@ export function installPtyWriteIpcHandlers(deps: { // Input from every client lands here; agent sleep reads it back with who else is viewing. installPtyTerminalAttentionIpcHandler(deps) + ipcMain.removeAllListeners('pty:noteUserInput') + ipcMain.on('pty:noteUserInput', (event) => { + if (isPtyWriteEventFromMainWindow(event)) { + observeTerminalUserInput() + } + }) + ipcMain.on('pty:write', (event, args: unknown) => { if (!isPtyWriteEventFromMainWindow(event) || !isPtyWritePayload(args)) { return diff --git a/src/main/ipc/pty/pty-write-ipc-validation.test.ts b/src/main/ipc/pty/pty-write-ipc-validation.test.ts index 6c4eb9da679..56c13db1847 100644 --- a/src/main/ipc/pty/pty-write-ipc-validation.test.ts +++ b/src/main/ipc/pty/pty-write-ipc-validation.test.ts @@ -1,7 +1,8 @@ import { describe, expect, it, vi } from 'vitest' -import { spawnMock } from './pty-ipc-mock-registry' +import { onMock, spawnMock } from './pty-ipc-mock-registry' import { setupPtyIpcSuite } from './pty-ipc-test-harness' import { registerPtyHandlers } from './register-handlers' +import { bindSession } from '../../telemetry/protected-milestones' vi.mock('electron', () => import('./pty-ipc-mock-registry').then((m) => m.electronModuleMock())) vi.mock('fs', () => import('./pty-ipc-mock-registry').then((m) => m.fsModuleMock())) @@ -59,6 +60,28 @@ describe('registerPtyHandlers', () => { getPtyWriteListener } = setupPtyIpcSuite() + it('admits a terminal user activity observation only from the main window', () => { + const captured: string[] = [] + const settings = { telemetry: { protectedMilestones: { installCohort: 'existing' } } } + const store = { getSettings: () => settings, updateSettings: () => undefined } + bindSession( + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixture supplies all store methods read by milestone capture. + store as never, + (name) => captured.push(name), + () => true + ) + registerPtyHandlers(mainWindow as never) + const listener = onMock.mock.calls.find(([name]) => name === 'pty:noteUserInput')?.[1] + if (!listener) { + throw new Error('missing terminal user input listener') + } + + listener(foreignWindowIpcEvent) + expect(captured).toEqual([]) + listener(mainWindowIpcEvent) + expect(captured).toEqual(['meaningful_activity_day']) + }) + it('rejects malformed and cross-window pty write IPC before provider writes', async () => { const mockProc = createMockProc() spawnMock.mockReturnValue(mockProc.proc) diff --git a/src/main/ipc/runtime-environments/runtime-environment-prompt-telemetry.test.ts b/src/main/ipc/runtime-environments/runtime-environment-prompt-telemetry.test.ts new file mode 100644 index 00000000000..5ce10cfd948 --- /dev/null +++ b/src/main/ipc/runtime-environments/runtime-environment-prompt-telemetry.test.ts @@ -0,0 +1,106 @@ +import { randomUUID } from 'node:crypto' +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import { decodeRuntimeEnvironmentSubscriptionEvents } from './runtime-environment-subscription-events' +import { + cleanupTelemetryClientTest, + setupTelemetryClientTest, + type TelemetryClientTestState +} from '../../telemetry/client-test-harness' + +let state: TelemetryClientTestState +beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-11-08T23:58:00Z')) + state = setupTelemetryClientTest({ + optedIn: true, + installId: '00000000-0000-4000-8000-000000000000', + existedBeforeTelemetryRelease: false, + protectedMilestones: { installCohort: 'fresh' } + }) +}) +afterEach(() => { + cleanupTelemetryClientTest(state.envStash) + vi.useRealTimers() +}) + +const prompt = (client: 'desktop' | 'mobile' = 'desktop') => ({ + type: 'structuredChatPromptAccepted', + id: randomUUID(), + agent_kind: 'codex', + client +}) +const frame = (result: unknown) => ({ + type: 'response' as const, + response: { id: 'events-1', ok: true as const, result, _meta: { runtimeId: 'remote-runtime' } } +}) +const cursor = () => state.settings.telemetry?.protectedMilestones +const captured = (name: string) => + state.mock.capture.mock.calls.map(([event]) => event).filter((event) => event.event === name) + +it('consumes live accepted observations in desktop main once, without exposing their IDs to analytics', () => { + const delivered: unknown[] = [] + let current = false + const observe = decodeRuntimeEnvironmentSubscriptionEvents( + 'runtime.clientEvents.subscribe', + (event) => delivered.push(event), + () => current + ) + const event = prompt() + observe(frame(event)) + expect(cursor()).toEqual({ installCohort: 'fresh' }) + current = true + observe(frame(event)) + expect(cursor()).toEqual({ + installCohort: 'fresh', + firstAgentInteractionDay: 20_765, + meaningfulActivityDay: 20_765 + }) + expect(captured('first_agent_interaction')[0].properties).toMatchObject({ + source: 'native_chat', + agent_kind: 'codex' + }) + expect(captured('meaningful_activity_day')[0].properties.trigger).toBe('native_chat') + expect(delivered).toEqual([frame(event)]) + expect(JSON.stringify(state.mock.capture.mock.calls)).not.toContain(event.id) + + vi.setSystemTime(new Date('2026-11-09T00:01:00Z')) + observe(frame(event)) + expect(cursor()?.meaningfulActivityDay).toBe(20_765) + observe(frame(prompt('mobile'))) + expect(cursor()?.meaningfulActivityDay).toBe(20_766) + expect(captured('meaningful_activity_day').map((entry) => entry.properties.trigger)).toEqual([ + 'native_chat', + 'mobile_chat' + ]) +}) + +it('drops opted-out observations permanently and rejects unbounded or content-bearing facts', () => { + const observe = decodeRuntimeEnvironmentSubscriptionEvents( + 'runtime.clientEvents.subscribe', + () => {}, + () => true + ) + const event = prompt('mobile') + state.settings.telemetry!.optedIn = false + observe(frame(event)) + expect(cursor()).toEqual({ installCohort: 'fresh' }) + state.settings.telemetry!.optedIn = true + observe(frame(event)) + observe(frame({ ...prompt(), body: 'private prompt must never reach analytics' })) + observe(frame({ ...prompt(), agent_kind: 'arbitrary-agent-name' })) + const wrongMethod = decodeRuntimeEnvironmentSubscriptionEvents( + 'agentSession.subscribe', + () => {}, + () => true + ) + wrongMethod(frame(prompt())) + expect(cursor()).toEqual({ installCohort: 'fresh' }) + + observe(frame(prompt('mobile'))) + expect(cursor()).toEqual({ + installCohort: 'fresh', + firstAgentInteractionDay: 20_765, + meaningfulActivityDay: 20_765 + }) + expect(captured('first_agent_interaction')[0].properties.source).toBe('mobile_chat') +}) diff --git a/src/main/ipc/runtime-environments/runtime-environment-subscription-events.ts b/src/main/ipc/runtime-environments/runtime-environment-subscription-events.ts index 035ea30ce40..3a6ea4b428f 100644 --- a/src/main/ipc/runtime-environments/runtime-environment-subscription-events.ts +++ b/src/main/ipc/runtime-environments/runtime-environment-subscription-events.ts @@ -19,6 +19,7 @@ export function decodeRuntimeEnvironmentSubscriptionEvents( event !== null && 'type' in event && (event.type === 'structuredChatMessageDispatch' || + event.type === 'structuredChatPromptAccepted' || event.type === 'structuredChatAgentStart') && recordRemoteStructuredChatTelemetry(event) ) { diff --git a/src/main/ipc/telemetry-protected-milestone-admission.test.ts b/src/main/ipc/telemetry-protected-milestone-admission.test.ts new file mode 100644 index 00000000000..b592431459f --- /dev/null +++ b/src/main/ipc/telemetry-protected-milestone-admission.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it, vi } from 'vitest' +import type { Store } from '../persistence/loading-store/store' + +const { handleMock, trackMock } = vi.hoisted(() => ({ + handleMock: vi.fn(), + trackMock: vi.fn(() => true) +})) + +vi.mock('electron', () => ({ + BrowserWindow: { fromWebContents: vi.fn() }, + ipcMain: { handle: handleMock } +})) +vi.mock('../telemetry/client', () => ({ + hasTrackedAgentDashboardActiveToday: vi.fn(() => false), + persistBannerAcknowledgeWithoutEmitting: vi.fn(), + setOptIn: vi.fn(), + track: trackMock +})) +vi.mock('../telemetry/cohort-classifier', () => ({ getCohortAtEmit: () => ({}) })) +vi.mock('../telemetry/onboarding-cohort-classifier', () => ({ + getOnboardingCohortAtEmit: () => ({}) +})) + +import { registerTelemetryHandlers } from './telemetry' + +describe('renderer telemetry admission', () => { + it('cannot forge protected milestones but still forwards their source events', () => { + const store: Partial = {} + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the track handler never reads the store. + registerTelemetryHandlers(store as Store) + const [, trackHandler] = handleMock.mock.calls.find( + ([channel]) => channel === 'telemetry:track' + )! + const sender = { sender: {} } + + expect( + trackHandler(sender, 'first_agent_interaction', { + source: 'orca_send', + agent_kind: 'codex', + install_cohort: 'fresh' + }) + ).toBe(false) + expect( + trackHandler(sender, 'meaningful_activity_day', { + trigger: 'orca_send', + install_cohort: 'fresh' + }) + ).toBe(false) + expect( + trackHandler(sender, 'native_chat_prompt_submitted', { + agent_kind: 'codex', + runtime: 'local', + client: 'desktop' + }) + ).toBe(false) + expect(trackMock).not.toHaveBeenCalled() + + const send = { agent_kind: 'codex', launch_source: 'sidebar', request_kind: 'followup' } + expect(trackHandler(sender, 'agent_prompt_sent', send)).toBe(true) + expect(trackMock).toHaveBeenCalledWith('agent_prompt_sent', send) + }) +}) diff --git a/src/main/ipc/telemetry.ts b/src/main/ipc/telemetry.ts index f093a434105..ceb99489cd9 100644 --- a/src/main/ipc/telemetry.ts +++ b/src/main/ipc/telemetry.ts @@ -30,6 +30,9 @@ const MAIN_OWNED_TELEMETRY_EVENTS = new Set([ 'daemon_pty_cwd_readable', 'star_nag_outcome', 'feature_interaction_usage_bucket_reached', + 'first_agent_interaction', + 'meaningful_activity_day', + 'native_chat_prompt_submitted', 'plugin_installed', 'ssh_remote_runtime_resolved', 'ssh_host_server_decided', diff --git a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts index 6880f7a61cf..eedfdd4936e 100644 --- a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts +++ b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts @@ -232,7 +232,9 @@ async function foundConversation( ...(first.selection === undefined ? openingMessageOptions(candidate.record.options) : {}) - } + }, + caller.userClient, + caller.telemetryOwnerKey ).catch((error: unknown) => { if (!(error instanceof CommandReceiptExistsError)) { throw error 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 5cbacc66709..47a2ed7ede4 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 @@ -63,6 +63,10 @@ import type { AgentSessionSwitchResult } from '../../../../shared/agent-session/ export type StructuredAgentSessionCaller = { callerKey: string clientKind?: 'mobile' | 'runtime' + /** Proven by the caller's transport, never inferred from an unnamed/runtime client. */ + userClient?: 'desktop' | 'mobile' + /** Consent-owning paired desktop, including the authenticated parent of a delegated phone. */ + telemetryOwnerKey?: string readsOptionsWithoutModel?: boolean } diff --git a/src/main/native-chat/agent-session-wire/host/structured-agent-session-publication-wiring.ts b/src/main/native-chat/agent-session-wire/host/structured-agent-session-publication-wiring.ts index f9f95f85f73..7e2734eb44e 100644 --- a/src/main/native-chat/agent-session-wire/host/structured-agent-session-publication-wiring.ts +++ b/src/main/native-chat/agent-session-wire/host/structured-agent-session-publication-wiring.ts @@ -1,3 +1,4 @@ +import { observeNativeChatPromptSubmissions } from '../../../telemetry/native-chat-prompt-submissions' import { StructuredAgentSessionConversations } from './structured-agent-session-conversations' import type { StructuredAgentSessionHostDeps } from './structured-agent-session-host-types' import type { StructuredAgentSessionClientDelivery } from '../publication/subscribers/structured-agent-session-client-delivery' @@ -20,6 +21,7 @@ export function createStructuredAgentSessionPublicationWiring(input: { observe: (id, journal, rows) => { input.client().requestFailures.observe(id, journal, rows) input.observeRows()?.(id, journal, rows) + observeNativeChatPromptSubmissions(journal, rows) }, forget: (id) => input.client().requestFailures.forget(id), deliver: (id, journal, inputChanged) => { diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-telemetry.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-telemetry.test.ts new file mode 100644 index 00000000000..174e754f079 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-telemetry.test.ts @@ -0,0 +1,250 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import type { RpcContext } from '../../../../runtime/rpc/core' +import type { RpcCallerIdentity } from '../../../../runtime/rpc/rpc-caller-identity' +import { structuredCallerFor } from '../../../../runtime/rpc/methods/structured-sessions/creation/structured-agent-session-gate' +import { createTestParams } from './structured-agent-session-create-test-fixture' +import { isTelemetryInitialized, track } from '../../../../telemetry/client' +import { subscribeStructuredChatTelemetry } from '../../../../telemetry/structured-chat-dispatch' +import type { StructuredChatTelemetryEvent } from '../../../../../shared/structured-chat-dispatch-telemetry' +import { + createQueuedMessageTestRig, + type QueuedMessageTestRig +} from './structured-agent-session-queued-message-rig.test-fixture' +import { eventually } from './structured-agent-session-queued-message-rig-provider.test-fixture' +import { openRigTurnFor } from './structured-agent-session-queued-rig-turn.test-fixture' +import { + HOST_TEST_SESSION, + HOST_TEST_THREAD, + hostTestMessage, + hostTestOperationId, + hostTestSendParams +} from './structured-agent-session-host-test-data' + +vi.mock('../../../../telemetry/client', () => ({ + track: vi.fn(), + isTelemetryInitialized: vi.fn(() => true) +})) + +let rig: QueuedMessageTestRig +const prompts = () => + vi.mocked(track).mock.calls.filter(([name]) => name === 'native_chat_prompt_submitted') + +function transportCaller( + caller?: RpcCallerIdentity, + clientKind?: 'mobile' | 'runtime', + pairedDeviceParentId?: string +) { + const context: Partial = { caller, clientKind, pairedDeviceParentId } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: caller derivation reads only transport identity fields supplied here. + return structuredCallerFor(context as RpcContext) +} +const desktop = () => transportCaller({ kind: 'desktop' }) + +beforeEach(async () => { + vi.clearAllMocks() + vi.mocked(isTelemetryInitialized).mockReturnValue(true) + rig = await createQueuedMessageTestRig() +}) +afterEach(async () => rig.dispose()) + +it('counts a desktop prompt at provider acceptance, excluding handover, rejection and replay', async () => { + const rejected = { ...hostTestSendParams('private rejected prompt'), userSend: true as const } + await rig.host.send(desktop(), rejected) + const refused = rejected.envelope.clientOperationId + await eventually(async () => expect((await rig.submission(refused))?.handedOverAt).toBeDefined()) + expect(prompts()).toEqual([]) + await rig.settleRejected(refused, 'Provider refused this prompt') + expect(prompts()).toEqual([]) + + const params = { ...hostTestSendParams('private accepted prompt'), userSend: true as const } + await rig.host.send(desktop(), params) + await eventually(async () => + expect((await rig.submission(params.envelope.clientOperationId))?.handedOverAt).toBeDefined() + ) + expect(prompts()).toEqual([]) + await rig.settleAccepted(params.envelope.clientOperationId, 'accepted-desktop') + const expected = [ + ['native_chat_prompt_submitted', { agent_kind: 'codex', runtime: 'unknown', client: 'desktop' }] + ] + expect(prompts()).toEqual(expected) + + await rig.host.send(desktop(), params) + expect(prompts()).toEqual(expected) +}) + +it('keeps the phone provenance through automatic queued handoff and ignores withdrawn cards', async () => { + const working = await rig.workingSend() + const caller = transportCaller({ kind: 'paired-device', deviceId: 'phone-1' }, 'mobile') + const queue = async (text: string) => { + const id = hostTestOperationId() + const fields = { body: hostTestMessage(text), delivery: 'queue-if-active' as const } + const answer = await rig.host.send(caller, { + envelope: rig.envelope(fields, 'agentSession.send', id), + ...fields, + userSend: true + }) + expect(answer).toMatchObject({ ok: true, value: { queued: { state: 'waiting' } } }) + return id + } + const withdrawn = await queue('private withdrawn phone prompt') + await rig.deleteQueued(withdrawn) + const waiting = await queue('private queued phone prompt') + expect(prompts()).toEqual([]) + + await rig.settleRejected(working, 'End the earlier request without accepting it') + await eventually(async () => expect((await rig.handoff(waiting))?.handedOverAt).toBeDefined()) + expect(prompts()).toEqual([]) + const handedOver = await rig.handoffId(waiting) + await rig.settleAccepted(handedOver, 'accepted-phone') + expect(prompts()).toEqual([ + ['native_chat_prompt_submitted', { agent_kind: 'codex', runtime: 'unknown', client: 'mobile' }] + ]) + expect((await rig.host.history({ sessionId: HOST_TEST_SESSION, direction: 'tail' })).ok).toBe( + true + ) + expect(prompts()).toEqual([ + ['native_chat_prompt_submitted', { agent_kind: 'codex', runtime: 'unknown', client: 'mobile' }] + ]) +}) + +it('does not treat provider-accepted commands or internal messages as user prompts', async () => { + const fields = { command: 'compact' as const } + const commandId = hostTestOperationId() + expect( + await rig.host.conversationCommand(desktop(), { + envelope: rig.envelope(fields, 'agentSession.conversationCommand', commandId), + ...fields + }) + ).toMatchObject({ ok: true }) + await eventually(async () => + expect( + ((await rig.submission(commandId)) ?? (await rig.handoff(commandId)))?.dispatchState + ).toBe('accepted') + ) + rig.finishCompact() + expect(prompts()).toEqual([]) + + const internal = rig.send('private unattended agent message', undefined, { internal: true }) + await internal.result + await eventually(async () => + expect((await rig.submission(internal.id))?.handedOverAt).toBeDefined() + ) + await rig.settleAccepted(internal.id, 'accepted-internal') + expect(prompts()).toEqual([]) +}) + +it('excludes CLI and unproven runtime callers even when their send uses userSend', async () => { + for (const caller of [ + transportCaller({ kind: 'local-cli' }), + transportCaller({ kind: 'local-cli' }, 'runtime'), + transportCaller(undefined, 'runtime'), + transportCaller() + ]) { + const params = { ...hostTestSendParams('private automated prompt'), userSend: true as const } + await rig.host.send(caller, params) + await eventually(async () => + expect((await rig.submission(params.envelope.clientOperationId))?.handedOverAt).toBeDefined() + ) + await rig.settleAccepted(params.envelope.clientOperationId, params.envelope.clientOperationId) + } + expect(prompts()).toEqual([]) +}) + +it('counts the trusted desktop opening message only after its provider accepts it', async () => { + const sessionId = 'new-chat-with-first-prompt' + const firstMessage = { + clientMessageId: hostTestOperationId(), + body: hostTestMessage('private opening prompt') + } + const params = createTestParams(firstMessage, {}, sessionId) + expect(await rig.host.create(desktop(), params, { firstMessage })).toMatchObject({ ok: true }) + await eventually(async () => + expect( + (await rig.host.journalSnapshot(sessionId)).submissions.find( + (entry) => entry.clientMessageId === firstMessage.clientMessageId + )?.handedOverAt + ).toBeDefined() + ) + expect(prompts()).toEqual([]) + await rig.host.settleLateDispatch({ + sessionId, + clientMessageId: firstMessage.clientMessageId, + providerIdentity: { + provider: 'codex', + threadId: HOST_TEST_THREAD, + turnId: 'opening-turn', + ordinal: 0 + } + }) + expect(prompts()).toEqual([ + ['native_chat_prompt_submitted', { agent_kind: 'codex', runtime: 'unknown', client: 'desktop' }] + ]) +}) + +it('relays only provider acceptance with the authenticated desktop or delegated phone owner', async () => { + vi.mocked(isTelemetryInitialized).mockReturnValue(false) + const events: { event: StructuredChatTelemetryEvent; owner: string }[] = [] + const unsubscribe = subscribeStructuredChatTelemetry((event, owner) => + events.push({ event, owner }) + ) + try { + const caller = transportCaller({ kind: 'paired-device', deviceId: 'desktop-a' }, 'runtime') + const desktopSend = { ...hostTestSendParams('private desktop prompt'), userSend: true as const } + await rig.host.send(caller, desktopSend) + await eventually(async () => + expect( + (await rig.submission(desktopSend.envelope.clientOperationId))?.handedOverAt + ).toBeDefined() + ) + expect(events).toEqual([]) + + const queuedId = hostTestOperationId() + const fields = { + body: hostTestMessage('private phone prompt'), + delivery: 'queue-if-active' as const + } + const phone = transportCaller( + { kind: 'paired-device', deviceId: 'delegated-phone' }, + 'mobile', + 'desktop-a' + ) + await rig.host.send(phone, { + envelope: rig.envelope(fields, 'agentSession.send', queuedId), + ...fields, + userSend: true + }) + expect(events).toEqual([]) + await rig.settleAccepted( + desktopSend.envelope.clientOperationId, + `of-${desktopSend.envelope.clientOperationId}` + ) + await rig.host.send(caller, desktopSend) + await openRigTurnFor(rig, desktopSend.envelope.clientOperationId, 'completed', 'success') + await eventually(async () => expect((await rig.handoff(queuedId))?.handedOverAt).toBeDefined()) + await rig.settleAccepted(await rig.handoffId(queuedId), 'remote-phone-turn') + expect(events).toEqual([ + { + owner: 'desktop-a', + event: { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'codex', + client: 'desktop' + } + }, + { + owner: 'desktop-a', + event: { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'codex', + client: 'mobile' + } + } + ]) + expect(events[0].event.id).not.toBe(events[1].event.id) + expect(prompts()).toEqual([]) + } finally { + unsubscribe() + } +}) diff --git a/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts b/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts index 0fb519d98fe..e05d409093c 100644 --- a/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts +++ b/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts @@ -1,6 +1,10 @@ // Share mutation admission/planning/publication. Conversation writes need no child lease; // provider-only effects start their child before admission, sends start through delivery. +import { + forgetNativeChatPrompt, + rememberNativeChatPrompt +} from '../../../telemetry/native-chat-prompt-submissions' import { latestNativeChatUsageLimitCut } from '../../../../shared/native-chat/native-chat-usage-limit-cut' import { refuse } from '../../../../shared/agent-session/failures/agent-session-wire-refusals' import type { @@ -110,7 +114,30 @@ export function sendStructuredAgentSessionTurn( } } params.beforeRun?.() - return runQueueableStructuredAgentSessionSend(context, ctx, params, () => plan.run(ctx)) + if (params.userSend && !params.body.command) { + rememberNativeChatPrompt( + ctx.journal, + params.envelope.clientOperationId, + ctx.agent, + caller.userClient, + caller.telemetryOwnerKey + ) + } + try { + const sent = await runQueueableStructuredAgentSessionSend(context, ctx, params, () => + plan.run(ctx) + ) + if (!sent.ok) { + forgetNativeChatPrompt(ctx.journal, params.envelope.clientOperationId) + } + return sent + } catch (error) { + const id = params.envelope.clientOperationId + if (!ctx.journal.submission(id) && !ctx.journal.queuedMessages.get(id)) { + forgetNativeChatPrompt(ctx.journal, id) + } + throw error + } } }, sendPreparation(context, params.envelope, arrival, selection) diff --git a/src/main/native-chat/agent-session-wire/operations/structured-agent-session-turns.ts b/src/main/native-chat/agent-session-wire/operations/structured-agent-session-turns.ts index 21ce6eeb569..74898e1589e 100644 --- a/src/main/native-chat/agent-session-wire/operations/structured-agent-session-turns.ts +++ b/src/main/native-chat/agent-session-wire/operations/structured-agent-session-turns.ts @@ -1,6 +1,10 @@ // Admission already passed; journal before adapter effects so a crash leaves recoverable unknown work. // Calling the adapter first could lose a turn it accepted. +import { + forgetNativeChatPrompt, + rememberNativeChatPrompt +} from '../../../telemetry/native-chat-prompt-submissions' import { captureStructuredExecutionOptions } from '../options/structured-agent-session-execution-options' import type { AgentSessionFailureWordsContext } from '../../../../shared/agent-session/failures/agent-session-failure-words' import type { @@ -128,7 +132,9 @@ export async function performSend( source?: AgentSessionMessageSource options?: Readonly> selection?: Readonly> - } + }, + userClient?: 'desktop' | 'mobile', + telemetryOwnerKey?: string ): Promise> { const existing = ctx.journal.submission(input.clientMessageId) if (existing && existing.payloadFingerprint !== input.payloadFingerprint) { @@ -148,12 +154,22 @@ export async function performSend( return { ok: false, refusal: captured.refusal } } try { + if (!input.body.command) { + rememberNativeChatPrompt( + ctx.journal, + input.clientMessageId, + ctx.agent, + userClient, + telemetryOwnerKey + ) + } await ctx.journal.appendSubmission( { ...input, options: captured.options, fence: ctx.fence, handoverRecorded: true }, undefined, ctx.operationReceipt ) } catch (error) { + forgetNativeChatPrompt(ctx.journal, input.clientMessageId) return agentSessionAdmissionWriteRefusal(ctx, error) } return { diff --git a/src/main/orca-profiles/new-profile-state.test.ts b/src/main/orca-profiles/new-profile-state.test.ts index 35f854dc8e5..593ea8290ae 100644 --- a/src/main/orca-profiles/new-profile-state.test.ts +++ b/src/main/orca-profiles/new-profile-state.test.ts @@ -68,7 +68,7 @@ describe('new desktop profile initialization', () => { activeTabId: null }) expect(published.settings.telemetry).toEqual( - inherited ?? + (inherited && { ...inherited, protectedMilestones: { installCohort: 'existing' } }) ?? expect.objectContaining({ existedBeforeTelemetryRelease: false, optedIn: true, @@ -128,6 +128,30 @@ describe('new desktop profile initialization', () => { expect(readPublishedState()).toEqual(original) }) + it('starts an inherited install with its own existing-profile milestone state', () => { + initializeNewOrcaProfileState( + profileId, + { + optedIn: true, + installId: 'retained-install-id', + existedBeforeTelemetryRelease: false, + protectedMilestones: { + installCohort: 'fresh', + firstAgentInteractionDay: 20_766, + meaningfulActivityDay: 20_766 + } + }, + root + ) + + expect(readPublishedState().settings.telemetry).toEqual({ + optedIn: true, + installId: 'retained-install-id', + existedBeforeTelemetryRelease: false, + protectedMilestones: { installCohort: 'existing' } + }) + }) + it('preserves existing JSON as input for its later import', () => { const path = getOrcaProfileDataFile(profileId, root) const original = '{"settings":{"telemetry":{"installId":"older"}}}' diff --git a/src/main/orca-profiles/new-profile-state.ts b/src/main/orca-profiles/new-profile-state.ts index a55a5194a26..d4d27a6de73 100644 --- a/src/main/orca-profiles/new-profile-state.ts +++ b/src/main/orca-profiles/new-profile-state.ts @@ -30,9 +30,19 @@ export function initializeNewOrcaProfileState( } const defaults = getDefaultPersistedState(homedir()) const cohorts = new LoadedCohortMigrationOperations({ loadNeedsSave: false }) + // Inherited consent belongs to an install that already existed, so its milestone cohort is not fresh. const initial = cohorts.migrateCohortSeeds( telemetry - ? { ...defaults, settings: { ...defaults.settings, telemetry } } + ? cohorts.migrateTelemetry( + { + ...defaults, + settings: { + ...defaults.settings, + telemetry: { ...telemetry, protectedMilestones: { installCohort: 'existing' } } + } + }, + true + ) : cohorts.migrateTelemetry(defaults, false), false ) diff --git a/src/main/persistence/loading-store/loaded-cohort-migrations.test.ts b/src/main/persistence/loading-store/loaded-cohort-migrations.test.ts index 6923004f022..5fec85a3c54 100644 --- a/src/main/persistence/loading-store/loaded-cohort-migrations.test.ts +++ b/src/main/persistence/loading-store/loaded-cohort-migrations.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest' import { getDefaultPersistedState } from '../../../shared/constants' import type { PersistedState } from '../../../shared/persisted-state-types' +import type { TelemetrySettings } from '../../../shared/telemetry-settings-types' import { LoadedCohortMigrationOperations } from './loaded-cohort-migrations' const seed = (state: PersistedState, fileExistedOnLoad: boolean) => @@ -36,3 +37,48 @@ describe('Claude Code theme sync cohort', () => { }) }) }) + +describe('protected milestone install cohort', () => { + const migrate = (state: PersistedState, fileExistedOnLoad: boolean) => + new LoadedCohortMigrationOperations({ loadNeedsSave: false }).migrateTelemetry( + state, + fileExistedOnLoad + ).settings.telemetry?.protectedMilestones + + const withTelemetry = ( + protectedMilestones?: TelemetrySettings['protectedMilestones'] + ): PersistedState => { + const state = getDefaultPersistedState('/home/user') + return { + ...state, + settings: { + ...state.settings, + telemetry: { + optedIn: true, + installId: 'install-1', + existedBeforeTelemetryRelease: false, + ...(protectedMilestones ? { protectedMilestones } : {}) + } + } + } + } + + it('marks a profile created on this load fresh', () => { + expect(migrate(getDefaultPersistedState('/home/user'), false)).toEqual({ + installCohort: 'fresh' + }) + }) + + it('marks an upgraded profile existing without touching its cursors', () => { + expect(migrate(withTelemetry({ meaningfulActivityDay: 20_000 }), true)).toEqual({ + installCohort: 'existing', + meaningfulActivityDay: 20_000 + }) + }) + + it('never re-freezes a cohort already recorded', () => { + expect(migrate(withTelemetry({ installCohort: 'fresh' }), true)).toEqual({ + installCohort: 'fresh' + }) + }) +}) diff --git a/src/main/persistence/loading-store/loaded-cohort-migrations.ts b/src/main/persistence/loading-store/loaded-cohort-migrations.ts index 677556b9fae..d284106d891 100644 --- a/src/main/persistence/loading-store/loaded-cohort-migrations.ts +++ b/src/main/persistence/loading-store/loaded-cohort-migrations.ts @@ -119,6 +119,41 @@ export class LoadedCohortMigrationOperations { } migrateTelemetry(state: PersistedState, fileExistedOnLoad: boolean): PersistedState { + return this.seedMilestoneInstallCohort( + this.migrateTelemetryIdentity(state, fileExistedOnLoad), + fileExistedOnLoad + ) + } + + /** Freeze once so upgraded profiles' first post-release interaction is never counted as activation. */ + private seedMilestoneInstallCohort( + state: PersistedState, + fileExistedOnLoad: boolean + ): PersistedState { + const telemetry = state.settings?.telemetry + if (!telemetry || telemetry.protectedMilestones?.installCohort !== undefined) { + return state + } + this.runtime.loadNeedsSave = true + return { + ...state, + settings: { + ...state.settings, + telemetry: { + ...telemetry, + protectedMilestones: { + ...telemetry.protectedMilestones, + installCohort: fileExistedOnLoad ? 'existing' : 'fresh' + } + } + } + } + } + + private migrateTelemetryIdentity( + state: PersistedState, + fileExistedOnLoad: boolean + ): PersistedState { const existing = state.settings?.telemetry // Partial telemetry blocks still require migration unless all invariants hold. if ( diff --git a/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts b/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts index 93a3b475d27..be911b81694 100644 --- a/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts +++ b/src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts @@ -212,7 +212,15 @@ export class MobileNotificationDismissalStore { this.scheduleRetry() } }, delay) - this.retryTimer.unref?.() + const timer: unknown = this.retryTimer + if ( + typeof timer === 'object' && + timer !== null && + 'unref' in timer && + typeof timer.unref === 'function' + ) { + timer.unref() + } } } diff --git a/src/main/runtime/rpc/methods/client-events/client-events-prompt-telemetry.test.ts b/src/main/runtime/rpc/methods/client-events/client-events-prompt-telemetry.test.ts new file mode 100644 index 00000000000..ac6e3d0f53f --- /dev/null +++ b/src/main/runtime/rpc/methods/client-events/client-events-prompt-telemetry.test.ts @@ -0,0 +1,112 @@ +import { expect, it } from 'vitest' +import { + STRUCTURED_CHAT_PROMPT_TELEMETRY_CLIENT_CAPABILITY, + STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY +} from '../../../../../shared/structured-chat-dispatch-telemetry' +import { recordStructuredChatPromptAccepted } from '../../../../telemetry/structured-chat-dispatch' +import type { OrcaRuntimeService } from '../../../orca-runtime' +import { eraseRpcMethods, isStreamingMethod, type RpcContext } from '../../core' +// Import the registry to avoid client-events' module-init cycle through ipc/ssh. +import { ALL_RPC_METHODS } from '../rpc-method-registry' + +const subscribe = eraseRpcMethods(ALL_RPC_METHODS) + .filter(isStreamingMethod) + .find((method) => method.name === 'runtime.clientEvents.subscribe')! + +function open(overrides: Partial = {}) { + const events: unknown[] = [] + let cleanup = () => {} + const fake = { + onClientEvent: () => () => {}, + getClientSettings: () => ({}), + registerSubscriptionCleanup: (_id: string, close: () => void) => { + cleanup = close + } + } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the stream reads only these runtime members. + const runtime = fake as unknown as OrcaRuntimeService + const done = subscribe.handler( + {}, + { + runtime, + caller: { kind: 'paired-device', deviceId: 'desktop-a' }, + pairedDeviceId: 'desktop-a', + clientKind: 'runtime', + clientCapabilities: [STRUCTURED_CHAT_PROMPT_TELEMETRY_CLIENT_CAPABILITY], + ...overrides + }, + (event) => { + if ( + typeof event === 'object' && + event !== null && + 'type' in event && + event.type === 'structuredChatPromptAccepted' + ) { + events.push(event) + } + } + ) + return { events, done, close: () => cleanup() } +} + +it('addresses live observations only to capable authenticated owning desktops, with no reconnect backfill', async () => { + const owner = open() + const other = open({ + caller: { kind: 'paired-device', deviceId: 'desktop-b' }, + pairedDeviceId: 'desktop-b' + }) + const old = open({ clientCapabilities: [STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY] }) + const phone = open({ clientKind: 'mobile' }) + const unproven = open({ caller: undefined, clientId: 'desktop-a' }) + const cli = open({ caller: { kind: 'local-cli' }, clientId: 'desktop-a' }) + const streams = [owner, other, old, phone, unproven, cli] + let reconnected: ReturnType | undefined + try { + recordStructuredChatPromptAccepted( + { agent_kind: 'codex', client: 'mobile', runtime: 'unknown' }, + 'desktop-a' + ) + expect(owner.events).toEqual([ + { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'codex', + client: 'mobile' + } + ]) + expect([other.events, old.events, phone.events, unproven.events, cli.events]).toEqual([ + [], + [], + [], + [], + [] + ]) + + owner.close() + await owner.done + recordStructuredChatPromptAccepted( + { agent_kind: 'codex', client: 'desktop', runtime: 'unknown' }, + 'desktop-a' + ) + reconnected = open() + expect(reconnected.events).toEqual([]) + recordStructuredChatPromptAccepted( + { agent_kind: 'claude-code', client: 'desktop', runtime: 'unknown' }, + 'desktop-a' + ) + expect(reconnected.events).toEqual([ + { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'claude-code', + client: 'desktop' + } + ]) + expect(owner.events).toHaveLength(1) + } finally { + for (const stream of [...streams, ...(reconnected ? [reconnected] : [])]) { + stream.close() + await stream.done + } + } +}) diff --git a/src/main/runtime/rpc/methods/client-events/client-events.ts b/src/main/runtime/rpc/methods/client-events/client-events.ts index 56d6d5e5703..f78a7844631 100644 --- a/src/main/runtime/rpc/methods/client-events/client-events.ts +++ b/src/main/runtime/rpc/methods/client-events/client-events.ts @@ -10,7 +10,10 @@ import { } from '../../../../../shared/rpc-contract/client-events-params' import { BROWSER_CLIENT_HOST_REQUEST_CAPABILITY } from '../../../../../shared/browser/client-hosting/browser-client-host-request-protocol' import { getBrowserHostRequestRegistry } from '../../../browser-host/leases/browser-host-request-registry' -import { STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY } from '../../../../../shared/structured-chat-dispatch-telemetry' +import { + STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY, + STRUCTURED_CHAT_PROMPT_TELEMETRY_CLIENT_CAPABILITY +} from '../../../../../shared/structured-chat-dispatch-telemetry' import { subscribeStructuredChatTelemetry } from '../../../../telemetry/structured-chat-dispatch' import { structuredCallerFor } from '../structured-sessions/creation/structured-agent-session-gate' @@ -38,18 +41,24 @@ export const CLIENT_EVENT_METHODS = [ clientCapabilities?.includes(BROWSER_CLIENT_HOST_REQUEST_CAPABILITY) ? getBrowserHostRequestRegistry(runtime).register({ pairedDeviceId, emit }) : undefined - const unsubscribeTelemetry = clientCapabilities?.includes( + const readsStarts = clientCapabilities?.includes( STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY ) - ? subscribeStructuredChatTelemetry((event, callerKey) => { - if ( - callerKey === structuredCallerFor(ctx).callerKey && - clientCapabilities?.includes(STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY) - ) { - emit(event) - } - }) - : undefined + const readsPrompts = + ctx.caller?.kind === 'paired-device' && + clientKind === 'runtime' && + clientCapabilities?.includes(STRUCTURED_CHAT_PROMPT_TELEMETRY_CLIENT_CAPABILITY) + const unsubscribeTelemetry = + readsStarts || readsPrompts + ? subscribeStructuredChatTelemetry((event, callerKey) => { + if ( + callerKey === structuredCallerFor(ctx).callerKey && + (event.type === 'structuredChatPromptAccepted' ? readsPrompts : readsStarts) + ) { + emit(event) + } + }) + : undefined const seq = ++clientEventSubscriptionSeq const subscriptionId = `runtime-client-events-${connectionId ?? 'inproc'}-${seq}` diff --git a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-gate.ts b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-gate.ts index b06374b42a6..5fb0666abc1 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-gate.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-gate.ts @@ -132,6 +132,24 @@ export async function requireRenameStructuredHost( export function structuredCallerFor(ctx: RpcContext): StructuredAgentSessionCaller { return { clientKind: ctx.clientKind, + telemetryOwnerKey: + ctx.caller?.kind === 'paired-device' + ? ctx.clientKind === 'runtime' + ? ctx.caller.deviceId + : ctx.clientKind === 'mobile' + ? ctx.pairedDeviceParentId + : undefined + : undefined, + userClient: + ctx.caller?.kind === 'desktop' + ? 'desktop' + : ctx.caller?.kind === 'paired-device' + ? ctx.clientKind === 'mobile' + ? 'mobile' + : ctx.clientKind === 'runtime' + ? 'desktop' + : undefined + : undefined, callerKey: ctx.caller?.kind === 'paired-device' ? rpcCallerOperationKey(ctx.caller) diff --git a/src/main/telemetry/burst-cap.ts b/src/main/telemetry/burst-cap.ts index 58eec375a78..9758afdb1ab 100644 --- a/src/main/telemetry/burst-cap.ts +++ b/src/main/telemetry/burst-cap.ts @@ -1,6 +1,7 @@ // Per-event refill caps runaway producers; non-refilling session/consent ceilings bound aggregate volume. Reset per telemetry session and warn once per bucket so overflow cannot flood stderr. import { eventSchemas, type EventName } from '../../shared/telemetry-events' +import { isProtectedMilestoneEvent } from './event-sampling' const PER_EVENT_DEFAULT_CAPACITY = 30 const PER_EVENT_AGENT_ERROR_CAPACITY = 20 @@ -59,6 +60,11 @@ export function consumeBurstToken(name: EventName): boolean { } return false } + // Once-per-day milestones keep their own bucket so noisy diagnostics cannot starve them. + if (isProtectedMilestoneEvent(name)) { + bucket.tokens -= 1 + return true + } if (perSessionCount >= PER_SESSION_CEILING) { if (!perSessionWarned) { perSessionWarned = true diff --git a/src/main/telemetry/client-prompt-sampling.test.ts b/src/main/telemetry/client-prompt-sampling.test.ts index ea85823f0ea..a406f223e2b 100644 --- a/src/main/telemetry/client-prompt-sampling.test.ts +++ b/src/main/telemetry/client-prompt-sampling.test.ts @@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { EventProps } from '../../shared/telemetry-events' import { track } from './client' import { + BASE_COMMON, cleanupTelemetryClientTest, setupTelemetryClientTest, type TelemetryClientTestState @@ -18,7 +19,13 @@ describe('prompt event sampling', () => { beforeEach(() => { vi.useFakeTimers() - state = setupTelemetryClientTest() + // Milestones already earned, so only the sampled prompt stream is measured here. + state = setupTelemetryClientTest({ + optedIn: true, + installId: BASE_COMMON.install_id, + existedBeforeTelemetryRelease: false, + protectedMilestones: { firstAgentInteractionDay: 0, meaningfulActivityDay: 1_000_000 } + }) }) afterEach(() => { diff --git a/src/main/telemetry/client-protected-milestones.test.ts b/src/main/telemetry/client-protected-milestones.test.ts new file mode 100644 index 00000000000..3b0dd9e715a --- /dev/null +++ b/src/main/telemetry/client-protected-milestones.test.ts @@ -0,0 +1,281 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { GlobalSettings } from '../../shared/global-settings-types' +import type { EventProps } from '../../shared/telemetry-events' +import { _enableTransportForTests, track } from './client' +import { + BASE_COMMON, + cleanupTelemetryClientTest, + setupTelemetryClientTest, + type TelemetryClientTestState +} from './client-test-harness' +import { recordMobileChatSend, recordMobileViewOpened } from './mobile-client-telemetry' +import { observeTerminalUserInput } from './protected-milestones' + +const hookPrompt: EventProps<'agent_prompt_sent'> = { + agent_kind: 'claude-code', + launch_source: 'unknown', + request_kind: 'followup' +} +const sidebarSend: EventProps<'agent_prompt_sent'> = { ...hookPrompt, launch_source: 'sidebar' } +const sidebarLaunch: EventProps<'agent_started'> = { + agent_kind: 'codex', + launch_source: 'sidebar', + request_kind: 'new' +} + +function telemetry( + protectedMilestones: NonNullable['protectedMilestones'] = { + installCohort: 'fresh' + } +): GlobalSettings['telemetry'] { + return { + optedIn: true, + installId: BASE_COMMON.install_id, + existedBeforeTelemetryRelease: false, + protectedMilestones + } +} + +describe('protected telemetry milestones', () => { + let state: TelemetryClientTestState + + type Message = { event: string; properties: Record } + let earlier: Message[] = [] + // Thrown captures were never accepted by the SDK queue. + const accepted = (): Message[] => [ + ...earlier, + ...state.mock.capture.mock.calls + .filter((_, index) => state.mock.capture.mock.results[index]?.type !== 'throw') + .map(([message]): Message => message) + ] + const captured = (event: string) => accepted().filter((message) => message.event === event) + const milestones = () => + accepted() + .map((message) => message.event) + .filter((event) => event === 'first_agent_interaction' || event === 'meaningful_activity_day') + const restart = () => { + const saved = state.settings.telemetry + earlier = accepted() + cleanupTelemetryClientTest(state.envStash) + state = setupTelemetryClientTest(saved) + } + + beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-11-08T23:58:00Z')) + earlier = [] + state = setupTelemetryClientTest(telemetry()) + }) + + afterEach(() => { + cleanupTelemetryClientTest(state.envStash) + vi.useRealTimers() + }) + + it('emits the first observed interaction once, across restarts, with unsampled props', () => { + vi.spyOn(Math, 'random').mockReturnValue(0) + track('agent_prompt_sent', sidebarSend) + track('agent_prompt_sent', sidebarSend) + restart() + track('agent_prompt_sent', sidebarSend) + + const first = captured('first_agent_interaction') + expect(first).toHaveLength(1) + expect(first[0].properties).toEqual({ + ...BASE_COMMON, + source: 'orca_send', + agent_kind: 'claude-code', + install_cohort: 'fresh', + $process_person_profile: false + }) + expect(state.settings.telemetry?.protectedMilestones).toEqual({ + installCohort: 'fresh', + firstAgentInteractionDay: 20_765, + meaningfulActivityDay: 20_765 + }) + }) + + it('records one meaningful day per UTC day across midnight and restart', () => { + track('agent_started', sidebarLaunch) + track('workspace_created', { source: 'sidebar', from_existing_branch: false }) + vi.setSystemTime(new Date('2026-11-09T00:01:00Z')) + restart() + track('workspace_created', { source: 'sidebar', from_existing_branch: false }) + restart() + track('agent_started', sidebarLaunch) + + expect(captured('meaningful_activity_day').map((m) => m.properties.trigger)).toEqual([ + 'agent_started', + 'workspace_created' + ]) + expect(state.settings.telemetry?.protectedMilestones?.meaningfulActivityDay).toBe(20_766) + }) + + it('keeps milestones when the source prompt is sampled out', () => { + vi.spyOn(Math, 'random').mockReturnValue(0.99) + expect(track('agent_prompt_sent', sidebarSend)).toBe(false) + + expect(captured('agent_prompt_sent')).toHaveLength(0) + expect(milestones()).toEqual(['first_agent_interaction', 'meaningful_activity_day']) + expect(captured('meaningful_activity_day')[0].properties).not.toHaveProperty('sample_rate') + }) + + it('is not starved once noisy events exhaust the session budget', () => { + for (let index = 0; index < 1_000; index++) { + vi.advanceTimersByTime(3_000) + track('agent_error', { agent_kind: 'codex', error_class: 'unknown' }) + } + expect(track('agent_error', { agent_kind: 'codex', error_class: 'unknown' })).toBe(false) + + vi.spyOn(Math, 'random').mockReturnValue(0) + expect(track('agent_prompt_sent', sidebarSend)).toBe(false) + expect(milestones()).toEqual(['first_agent_interaction', 'meaningful_activity_day']) + }) + + it('retries a milestone whose capture was not accepted', () => { + state.mock.capture.mockImplementationOnce(() => { + throw new Error('queue rejected') + }) + track('native_chat_prompt_submitted', { + agent_kind: 'codex', + runtime: 'local', + client: 'desktop' + }) + expect(state.settings.telemetry?.protectedMilestones?.firstAgentInteractionDay).toBeUndefined() + + track('native_chat_prompt_submitted', { + agent_kind: 'codex', + runtime: 'local', + client: 'desktop' + }) + expect(milestones()).toEqual(['meaningful_activity_day', 'first_agent_interaction']) + expect(state.settings.telemetry?.protectedMilestones?.firstAgentInteractionDay).toBe(20_765) + }) + + it('does not consume milestones while consent or the build gate blocks transmission', () => { + state.settings.telemetry!.optedIn = false + track('agent_started', sidebarLaunch) + state.settings.telemetry!.optedIn = true + _enableTransportForTests(false) + track('agent_started', sidebarLaunch) + _enableTransportForTests(true) + expect(milestones()).toEqual([]) + expect(state.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + + track('agent_started', sidebarLaunch) + expect(milestones()).toEqual(['meaningful_activity_day']) + }) + + it('ignores invalid source payloads and renderer-unreachable raw fields', () => { + const rawPrompt = { ...sidebarSend, prompt: 'private text' } + track('agent_prompt_sent', rawPrompt) + const rawMilestone = { + source: 'orca_send', + agent_kind: 'codex', + install_cohort: 'fresh', + prompt: 'private text' + } as const + expect(track('first_agent_interaction', rawMilestone)).toBe(false) + const forged = { trigger: 'app_opened', install_cohort: 'fresh' } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: deliberately forges an out-of-enum trigger to prove runtime schema rejection. + const launchOnly = forged as EventProps<'meaningful_activity_day'> + expect(track('meaningful_activity_day', launchOnly)).toBe(false) + expect(milestones()).toEqual([]) + expect(state.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + }) + + it('does not mark activity from passive, automated or unattributed observations', () => { + track('app_opened', {}) + track('agent_started', { ...sidebarLaunch, request_kind: 'resume' }) + track('agent_started', { ...sidebarLaunch, launch_source: 'orchestration' }) + track('agent_started', { ...sidebarLaunch, launch_source: 'cli' }) + track('workspace_created', { source: 'unknown', from_existing_branch: false }) + recordMobileChatSend('mobile', 'claude', { ok: true, replayed: true }) + recordMobileChatSend('runtime', 'claude', { ok: true, replayed: false }) + recordMobileViewOpened( + { clientKind: 'mobile' }, + { + surface: 'chat', + viewKey: 'chat:1', + agent: () => 'claude' + } + ) + expect(milestones()).toEqual([]) + + for (const launch_source of ['orchestration', 'cli', 'unknown'] as const) { + track('agent_prompt_sent', { ...hookPrompt, launch_source }) + } + expect(milestones()).toEqual(['first_agent_interaction']) + expect(captured('first_agent_interaction')[0].properties.source).toBe('unattributed_prompt') + }) + + it('keeps commands and unacknowledged PTY sends from consuming chat milestones', () => { + track('native_chat_message_sent', { agent_kind: 'codex', runtime: 'local', client: 'desktop' }) + recordMobileChatSend('mobile', 'claude', { ok: true, replayed: false }) + expect(milestones()).toEqual([]) + expect(state.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + + track('native_chat_prompt_submitted', { + agent_kind: 'codex', + runtime: 'remote', + client: 'desktop' + }) + expect(captured('first_agent_interaction')[0].properties).toMatchObject({ + source: 'native_chat', + agent_kind: 'codex' + }) + expect(captured('meaningful_activity_day')[0].properties.trigger).toBe('native_chat') + }) + + it('counts a provider-accepted phone prompt as an interaction and an active day', () => { + track('native_chat_prompt_submitted', { + agent_kind: 'claude-code', + runtime: 'unknown', + client: 'mobile' + }) + + expect(captured('first_agent_interaction')[0].properties).toMatchObject({ + source: 'mobile_chat', + agent_kind: 'claude-code' + }) + expect(captured('meaningful_activity_day')[0].properties).toMatchObject({ + trigger: 'mobile_chat', + install_cohort: 'fresh' + }) + }) + + it('reports profiles without a frozen cohort as existing', () => { + state.settings.telemetry = telemetry({}) + track('agent_started', sidebarLaunch) + expect(captured('meaningful_activity_day')[0].properties.install_cohort).toBe('existing') + }) + + it('marks terminal activity once per day across restart and UTC rollover', () => { + observeTerminalUserInput() + restart() + observeTerminalUserInput() + expect(milestones()).toEqual(['meaningful_activity_day']) + + vi.setSystemTime(new Date('2026-11-09T00:01:00Z')) + observeTerminalUserInput() + expect( + captured('meaningful_activity_day').map((message) => message.properties.trigger) + ).toEqual(['terminal_input', 'terminal_input']) + expect(state.settings.telemetry?.protectedMilestones?.meaningfulActivityDay).toBe(20_766) + }) + + it('rate-limits blocked terminal checks within the day and retries immediately after UTC midnight', () => { + vi.setSystemTime(new Date('2026-11-08T23:59:59Z')) + state.settings.telemetry!.optedIn = false + observeTerminalUserInput() + state.settings.telemetry!.optedIn = true + observeTerminalUserInput() + expect(milestones()).toEqual([]) + expect(state.settings.telemetry?.protectedMilestones?.meaningfulActivityDay).toBeUndefined() + + vi.setSystemTime(new Date('2026-11-09T00:00:01Z')) + observeTerminalUserInput() + expect(milestones()).toEqual(['meaningful_activity_day']) + expect(state.settings.telemetry?.protectedMilestones?.meaningfulActivityDay).toBe(20_766) + }) +}) diff --git a/src/main/telemetry/client.ts b/src/main/telemetry/client.ts index 978f13b974e..2a9417355bb 100644 --- a/src/main/telemetry/client.ts +++ b/src/main/telemetry/client.ts @@ -7,11 +7,13 @@ import type { AgentDashboardSurface } from '../../shared/telemetry-agent-dashboa import type { Store } from '../persistence/loading-store/store' import { consumeBurstToken, resetBurstCapsForSession } from './burst-cap' import { telemetryEventSampleRate } from './event-sampling' +import * as protectedMilestones from './protected-milestones' import { experimentCohortProps } from './experiment-cohort-props' import { getCohortAtEmit } from './cohort-classifier' import { isConsentEnvOverridden, resolveConsent } from './consent' import * as postHogStart from './posthog-client-start' -import { buildCommonProps, createPostHogClient, waitForCaptureEnqueue } from './posthog-transport' +import { buildCommonProps, createPostHogClient } from './posthog-transport' +import { captureOptOutBeforeSdkOptOut } from './opt-out-capture' import { commonPropsSchema, validate } from './validator' export { shouldOptOutSdkAtInit } from './posthog-transport' @@ -42,6 +44,8 @@ let storeRef: Store | null = null let testTransportEnabled = false +type CaptureProperties = Readonly> + // Banner resolution gates first app_opened; mark and emit atomically. let appOpenedTrackedThisSession = false const trackedDashboardActiveDays = new Map() @@ -54,6 +58,7 @@ export function initTelemetry(store: Store, scheduleStart: postHogStart.StartSch // Reset banner/app-open gating each launch, not once per install. appOpenedTrackedThisSession = false trackedDashboardActiveDays.clear() + protectedMilestones.bindSession(store, enqueueCapture, isTelemetryEnabled) postHogStart.resetStart() if (!TELEMETRY_ENABLED || !IS_OFFICIAL_BUILD || BUILD_IDENTITY === null || WRITE_KEY === null) { @@ -139,11 +144,16 @@ export function track(name: N, props: EventProps): boole (!posthog && !postHogStart.isStartDeferred()) || !commonProps || !storeRef || - posthog?.optedOut || - (sampleRate < 1 && Math.random() >= sampleRate) + posthog?.optedOut ) { return false } + const sampledOut = sampleRate < 1 && Math.random() >= sampleRate + // Milestones derive from every observation, so sampling and budgets cannot hide them. + const milestoneDay = protectedMilestones.observationDay(name) + if (sampledOut && milestoneDay === null) { + return false + } const dashboardActiveSurface = name === 'agent_dashboard_active' && @@ -163,7 +173,8 @@ export function track(name: N, props: EventProps): boole } // Cap floods before expensive live consent reads, including opted-out renderers. - if (!consumeBurstToken(name)) { + const withinBudget = !sampledOut && consumeBurstToken(name) + if (!withinBudget && milestoneDay === null) { return false } @@ -179,6 +190,12 @@ export function track(name: N, props: EventProps): boole if (!result.ok) { return false } + if (milestoneDay !== null) { + protectedMilestones.record(name, result.props, settings, milestoneDay) + } + if (!withinBudget) { + return false + } if (dashboardActiveSurface) { if (settings.experimentalAgentDashboardPopout !== true) { return false @@ -198,22 +215,7 @@ export function track(name: N, props: EventProps): boole ? { sample_rate: sampleRate } : {} - // Node SDK requires per-capture person-profile suppression. - const message = { - distinctId: commonProps.install_id, - event: name, - properties: { - ...commonProps, - ...result.props, - ...mainOwnedProps, - $process_person_profile: false - } - } - if (posthog) { - posthog.capture(message) - } else { - postHogStart.holdCapture(message, ensurePostHogClient) - } + enqueueCapture(name, { ...result.props, ...mainOwnedProps }) if (dashboardActiveSurface && settings.telemetry) { trackedDashboardActiveDays.set(dashboardActiveSurface, dashboardActiveDay) try { @@ -233,6 +235,23 @@ export function track(name: N, props: EventProps): boole return true } +function enqueueCapture(name: EventName, props: CaptureProperties): void { + if (!commonProps) { + throw new Error('telemetry common props missing') + } + // Node SDK requires per-capture person-profile suppression. + const message = { + distinctId: commonProps.install_id, + event: name, + properties: { ...commonProps, ...props, $process_person_profile: false } + } + if (posthog) { + posthog.capture(message) + } else { + postHogStart.holdCapture(message, ensurePostHogClient) + } +} + export async function setOptIn(via: OptInVia, optedIn: boolean): Promise { if (!storeRef) { return @@ -263,27 +282,9 @@ export async function setOptIn(via: OptInVia, optedIn: boolean): Promise { if (!client || isConsentEnvOverridden()) { return } - // Enqueue opt-out directly before disabling SDK; track would reject it under the newly stored preference. try { - if (!shuttingDown && commonProps && consumeBurstToken('telemetry_opted_out')) { - const validated = validate('telemetry_opted_out', { via }) - if (validated.ok) { - const uuid = randomUUID() - const enqueued = waitForCaptureEnqueue(client, 'telemetry_opted_out', uuid) - client.capture({ - distinctId: commonProps.install_id, - event: 'telemetry_opted_out', - uuid, - properties: { - ...commonProps, - ...validated.props, - $process_person_profile: false - } - }) - if (!(await enqueued)) { - console.warn('[telemetry] telemetry_opted_out did not enqueue before SDK opt-out') - } - } + if (!shuttingDown && commonProps) { + await captureOptOutBeforeSdkOptOut(client, commonProps, via) } } catch (err) { console.warn('[telemetry] telemetry_opted_out capture failed before SDK opt-out:', err) diff --git a/src/main/telemetry/event-sampling.ts b/src/main/telemetry/event-sampling.ts index 91bd9b48105..2c866e465aa 100644 --- a/src/main/telemetry/event-sampling.ts +++ b/src/main/telemetry/event-sampling.ts @@ -1,7 +1,17 @@ import type { EventName } from '../../shared/telemetry-events' +// Low-volume protected milestones: never sampled and exempt from the shared session ceiling. +const PROTECTED_MILESTONE_EVENTS: ReadonlySet = new Set([ + 'first_agent_interaction', + 'meaningful_activity_day' +]) + +export function isProtectedMilestoneEvent(name: string): boolean { + return PROTECTED_MILESTONE_EVENTS.has(name) +} + export function telemetryEventSampleRate(name: EventName): 0.01 | 0.001 | 1 { - if (name === 'agent_prompt_sent') { + if (name === 'agent_prompt_sent' || name === 'native_chat_prompt_submitted') { return 0.01 } if (name === 'daemon_audit_eligibility') { diff --git a/src/main/telemetry/native-chat-prompt-submissions.ts b/src/main/telemetry/native-chat-prompt-submissions.ts new file mode 100644 index 00000000000..e1f81ed8c86 --- /dev/null +++ b/src/main/telemetry/native-chat-prompt-submissions.ts @@ -0,0 +1,86 @@ +import { tuiAgentToAgentKind } from '../../shared/agent-kind' +import { isTuiAgent } from '../../shared/agent-launch/catalog/tui-agent-config' +import type { EventProps } from '../../shared/telemetry-events' +import type { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store' +import type { JournalRow } from '../native-chat/agent-session-journal/durable-rows/journal-row-schema' +import { recordStructuredChatPromptAccepted } from './structured-chat-dispatch' + +type PromptJournal = Pick +type PromptSource = EventProps<'native_chat_prompt_submitted'> +const pendingPrompts = new WeakMap< + PromptJournal, + Map +>() +const MAX_PENDING_PROMPTS_PER_JOURNAL = 256 + +/** Fresh user sends retain device provenance through queued-message conversion, without persisting telemetry state. */ +export function rememberNativeChatPrompt( + journal: PromptJournal, + clientMessageId: string, + agent: string, + userClient: 'desktop' | 'mobile' | undefined, + ownerKey?: string +): void { + if (!userClient) { + return + } + let pending = pendingPrompts.get(journal) + if (!pending) { + pending = new Map() + pendingPrompts.set(journal, pending) + } + if (pending.has(clientMessageId)) { + return + } + if (pending.size >= MAX_PENDING_PROMPTS_PER_JOURNAL) { + const oldest = pending.keys().next().value + if (oldest !== undefined) { + pending.delete(oldest) + } + } + pending.set(clientMessageId, { + ownerKey, + source: { + agent_kind: isTuiAgent(agent) ? tuiAgentToAgentKind(agent) : 'other', + // The host cannot establish the desktop renderer's execution-target classification. + runtime: 'unknown', + client: userClient + } + }) +} + +export function forgetNativeChatPrompt(journal: PromptJournal, clientMessageId: string): void { + pendingPrompts.get(journal)?.delete(clientMessageId) +} + +/** Observe only committed transitions, never history snapshots, local reservations or handover preambles. */ +export function observeNativeChatPromptSubmissions( + journal: PromptJournal, + rows: readonly JournalRow[] | undefined +): void { + const pending = pendingPrompts.get(journal) + if (!pending?.size || !rows) { + return + } + for (const row of rows) { + if (row.kind === 'epoch') { + pending.clear() + } else if (row.kind === 'queue' && row.change.op === 'withdrawn') { + pending.delete(row.messageId) + } else if (row.kind === 'dispatch') { + const submission = journal.submission(row.clientMessageId) + const sourceId = submission?.queuedMessageId ?? row.clientMessageId + const source = pending.get(sourceId) + if (row.state === 'accepted' && source) { + pending.delete(sourceId) + try { + recordStructuredChatPromptAccepted(source.source, source.ownerKey) + } catch (error) { + console.warn('[telemetry] native chat prompt observation failed', error) + } + } else if (row.state === 'rejected' && !submission?.queuedMessageId) { + pending.delete(sourceId) + } + } + } +} diff --git a/src/main/telemetry/opt-out-capture.ts b/src/main/telemetry/opt-out-capture.ts new file mode 100644 index 00000000000..c98ab22e216 --- /dev/null +++ b/src/main/telemetry/opt-out-capture.ts @@ -0,0 +1,32 @@ +import { randomUUID } from 'node:crypto' +import type { PostHog } from 'posthog-node' +import type { CommonProps, OptInVia } from '../../shared/telemetry-events' +import { consumeBurstToken } from './burst-cap' +import { waitForCaptureEnqueue } from './posthog-transport' +import { validate } from './validator' + +/** Enqueue opt-out directly before disabling the SDK; track would reject it under the newly stored preference. */ +export async function captureOptOutBeforeSdkOptOut( + client: PostHog, + common: CommonProps, + via: OptInVia +): Promise { + if (!consumeBurstToken('telemetry_opted_out')) { + return + } + const validated = validate('telemetry_opted_out', { via }) + if (!validated.ok) { + return + } + const uuid = randomUUID() + const enqueued = waitForCaptureEnqueue(client, 'telemetry_opted_out', uuid) + client.capture({ + distinctId: common.install_id, + event: 'telemetry_opted_out', + uuid, + properties: { ...common, ...validated.props, $process_person_profile: false } + }) + if (!(await enqueued)) { + console.warn('[telemetry] telemetry_opted_out did not enqueue before SDK opt-out') + } +} diff --git a/src/main/telemetry/protected-milestones.ts b/src/main/telemetry/protected-milestones.ts new file mode 100644 index 00000000000..94a11f47bfd --- /dev/null +++ b/src/main/telemetry/protected-milestones.ts @@ -0,0 +1,260 @@ +// Derives protected milestones from already-validated, consented product events so sampling and the shared budget cannot hide them. +// Not exactly-once: cursors advance at SDK enqueue/hold acceptance (a quit before flush loses the event) and persist via the +// debounced settings save (a crash before that save can re-emit after restart), so dashboards dedupe per install_id. + +import type { GlobalSettings } from '../../shared/global-settings-types' +import { utcDayIndex } from '../../shared/telemetry-day' +import { eventSchemas, type EventName, type EventProps } from '../../shared/telemetry-events' +import type { + FirstAgentInteractionSource, + MeaningfulActivityTrigger +} from '../../shared/telemetry-protected-milestone-schemas' +import type { TelemetrySettings } from '../../shared/telemetry-settings-types' +import type { Store } from '../persistence/loading-store/store' +import { consumeBurstToken } from './burst-cap' +import { validate } from './validator' + +export type ProtectedMilestone = + | { name: 'first_agent_interaction'; props: EventProps<'first_agent_interaction'> } + | { name: 'meaningful_activity_day'; props: EventProps<'meaningful_activity_day'> } + +type ProtectedMilestoneCursor = { firstInteractionDone: boolean; activityDay: number } +type Observation = EventName | 'terminal_input' +type SourceObservation = + | { name: 'agent_started'; props: EventProps<'agent_started'> } + | { name: 'agent_prompt_sent'; props: EventProps<'agent_prompt_sent'> } + | { name: 'native_chat_prompt_submitted'; props: EventProps<'native_chat_prompt_submitted'> } + | { name: 'workspace_created'; props: EventProps<'workspace_created'> } + | { name: 'terminal_input' } +type Enqueue = (name: ProtectedMilestone['name'], props: ProtectedMilestone['props']) => void +type MilestoneSession = { store: Store; enqueue: Enqueue; isEnabled: () => boolean } + +const SOURCE_EVENTS: ReadonlySet = new Set([ + 'agent_started', + 'agent_prompt_sent', + 'native_chat_prompt_submitted', + 'workspace_created' +]) +// Automation and scriptable launches cannot prove a person acted that day. +const UNATTRIBUTED_LAUNCH_SOURCES: ReadonlySet = new Set([ + 'orchestration', + 'cli', + 'unknown' +]) + +// While a UTC day is unmarked, typing must cost at most one consent read a minute within that day. +const TERMINAL_INPUT_CHECK_INTERVAL_MS = 60_000 + +// Mirrors accepted cursors so a failed settings write cannot re-emit within the session. +let cursor: ProtectedMilestoneCursor | null = null +let session: MilestoneSession | null = null +let lastTerminalInputCheckAt: number | null = null + +/** Bind the telemetry session's store, capture path and live gates; each launch starts from persisted cursors. */ +export function bindSession(store: Store, enqueue: Enqueue, isEnabled: () => boolean): void { + session = { store, enqueue, isEnabled } + cursor = null + lastTerminalInputCheckAt = null +} + +/** Cheap pre-check before consent reads: the UTC day an observation could still earn a milestone for, else null. */ +export function observationDay(name: EventName, now = Date.now()): number | null { + if (!SOURCE_EVENTS.has(name)) { + return null + } + const day = utcDayIndex(now) + // An unknown cursor defers to the persisted one. + if (!cursor || cursor.activityDay < day) { + return day + } + const canStillInteract = + !cursor.firstInteractionDone && name !== 'agent_started' && name !== 'workspace_created' + return canStillInteract ? day : null +} + +// Re-parse with the source schema so eligibility reads typed fields; only runs while a milestone is unearned. +function parseObservation(name: Observation, props: unknown): SourceObservation | null { + if (name === 'terminal_input') { + return { name } + } + if (name === 'agent_started') { + const parsed = eventSchemas.agent_started.safeParse(props) + return parsed.success ? { name, props: parsed.data } : null + } + if (name === 'agent_prompt_sent') { + const parsed = eventSchemas.agent_prompt_sent.safeParse(props) + return parsed.success ? { name, props: parsed.data } : null + } + if (name === 'native_chat_prompt_submitted') { + const parsed = eventSchemas.native_chat_prompt_submitted.safeParse(props) + return parsed.success ? { name, props: parsed.data } : null + } + if (name === 'workspace_created') { + const parsed = eventSchemas.workspace_created.safeParse(props) + return parsed.success ? { name, props: parsed.data } : null + } + return null +} + +function interactionSource(observation: SourceObservation): FirstAgentInteractionSource | null { + if (observation.name === 'native_chat_prompt_submitted') { + return observation.props.client === 'mobile' ? 'mobile_chat' : 'native_chat' + } + if (observation.name === 'agent_prompt_sent') { + return UNATTRIBUTED_LAUNCH_SOURCES.has(observation.props.launch_source) + ? 'unattributed_prompt' + : 'orca_send' + } + return null +} + +function activityTrigger(observation: SourceObservation): MeaningfulActivityTrigger | null { + if (observation.name === 'terminal_input') { + return 'terminal_input' + } + if (observation.name === 'agent_started') { + // Resumes include background wakes of sleeping sessions. + return observation.props.request_kind === 'new' && + !UNATTRIBUTED_LAUNCH_SOURCES.has(observation.props.launch_source) + ? 'agent_started' + : null + } + if (observation.name === 'workspace_created') { + return observation.props.source === 'unknown' ? null : 'workspace_created' + } + const source = interactionSource(observation) + return source === 'unattributed_prompt' ? null : source +} + +function hydrate(saved: TelemetrySettings['protectedMilestones']): ProtectedMilestoneCursor { + const persisted = { + firstInteractionDone: saved?.firstAgentInteractionDay !== undefined, + activityDay: saved?.meaningfulActivityDay ?? -1 + } + cursor = { + firstInteractionDone: persisted.firstInteractionDone || cursor?.firstInteractionDone === true, + activityDay: Math.max(persisted.activityDay, cursor?.activityDay ?? -1) + } + return cursor +} + +/** Milestones a validated, consented observation earns. */ +function earnedProtectedMilestones( + name: Observation, + props: unknown, + telemetry: TelemetrySettings | undefined, + day: number +): ProtectedMilestone[] { + const observation = parseObservation(name, props) + if (!observation) { + return [] + } + const current = hydrate(telemetry?.protectedMilestones) + // Unknown provenance must not claim a fresh install. + const install_cohort = telemetry?.protectedMilestones?.installCohort ?? 'existing' + const earned: ProtectedMilestone[] = [] + const source = interactionSource(observation) + if ( + source && + (observation.name === 'agent_prompt_sent' || + observation.name === 'native_chat_prompt_submitted') && + !current.firstInteractionDone + ) { + earned.push({ + name: 'first_agent_interaction', + props: { source, agent_kind: observation.props.agent_kind, install_cohort } + }) + } + const trigger = activityTrigger(observation) + if (trigger && current.activityDay < day) { + earned.push({ name: 'meaningful_activity_day', props: { trigger, install_cohort } }) + } + return earned +} + +/** Advance the cursor for an accepted capture and return the settings value to persist. */ +function acceptProtectedMilestone( + milestone: ProtectedMilestone['name'], + telemetry: TelemetrySettings | undefined, + day: number +): NonNullable { + const current = hydrate(telemetry?.protectedMilestones) + const saved = telemetry?.protectedMilestones ?? {} + if (milestone === 'first_agent_interaction') { + current.firstInteractionDone = true + return { ...saved, firstAgentInteractionDay: saved.firstAgentInteractionDay ?? day } + } + current.activityDay = Math.max(current.activityDay, day) + return { ...saved, meaningfulActivityDay: current.activityDay } +} + +/** Persist each cursor only after its capture is enqueued or held, so rejected attempts retry on the next observation. */ +export function record( + name: Observation, + props: unknown, + settings: GlobalSettings, + day: number +): void { + if (!session) { + return + } + const { store, enqueue } = session + for (const milestone of earnedProtectedMilestones(name, props, settings.telemetry, day)) { + if (!consumeBurstToken(milestone.name)) { + continue + } + const validated = validate(milestone.name, milestone.props) + if (!validated.ok) { + continue + } + try { + enqueue(milestone.name, validated.props) + } catch (err) { + console.warn('[telemetry] protected milestone capture failed', err) + continue + } + const telemetry = store.getSettings().telemetry + const protectedMilestones = acceptProtectedMilestone(milestone.name, telemetry, day) + if (!telemetry) { + continue + } + try { + store.updateSettings({ telemetry: { ...telemetry, protectedMilestones } }) + } catch (err) { + console.warn('[telemetry] failed to persist protected milestone cursor', err) + } + } +} + +/** Genuine desktop xterm input admitted by a local or remote transport marks the day. */ +export function observeTerminalUserInput(now = Date.now()): void { + const day = utcDayIndex(now) + if (!session || (cursor && cursor.activityDay >= day)) { + return + } + const last = lastTerminalInputCheckAt + if ( + last !== null && + utcDayIndex(last) === day && + now >= last && + now - last < TERMINAL_INPUT_CHECK_INTERVAL_MS + ) { + return + } + try { + if (!session.isEnabled()) { + lastTerminalInputCheckAt = now + return + } + const settings = session.store.getSettings() + if (hydrate(settings.telemetry?.protectedMilestones).activityDay >= day) { + return + } + lastTerminalInputCheckAt = now + record('terminal_input', undefined, settings, day) + } catch (err) { + lastTerminalInputCheckAt = now + // Telemetry must never disturb terminal input. + console.warn('[telemetry] terminal activity milestone failed', err) + } +} diff --git a/src/main/telemetry/structured-chat-dispatch.ts b/src/main/telemetry/structured-chat-dispatch.ts index 0699b980c37..97ffb5378ad 100644 --- a/src/main/telemetry/structured-chat-dispatch.ts +++ b/src/main/telemetry/structured-chat-dispatch.ts @@ -3,21 +3,31 @@ import { randomUUID } from 'node:crypto' import { z } from 'zod' import type { StructuredChatStartTelemetryEvent, + StructuredChatPromptTelemetryEvent, StructuredChatTelemetryEvent } from '../../shared/structured-chat-dispatch-telemetry' import { agentKindSchema } from '../../shared/telemetry-property-schemas' -import { isTelemetryInitialized } from './client' +import { isTelemetryInitialized, track } from './client' +import type { EventProps } from '../../shared/telemetry-events' type TelemetryListener = (event: StructuredChatTelemetryEvent, callerKey: string) => void const remoteListeners = new Set() const receivedEvents = new Set() -const telemetryEventSchema = z.strictObject({ - type: z.literal('structuredChatAgentStart'), - id: z.uuid(), - agent_kind: agentKindSchema, - launch_source: z.literal('unknown'), - request_kind: z.enum(['new', 'resume']) -}) +const telemetryEventSchema = z.discriminatedUnion('type', [ + z.strictObject({ + type: z.literal('structuredChatAgentStart'), + id: z.uuid(), + agent_kind: agentKindSchema, + launch_source: z.literal('unknown'), + request_kind: z.enum(['new', 'resume']) + }), + z.strictObject({ + type: z.literal('structuredChatPromptAccepted'), + id: z.uuid(), + agent_kind: agentKindSchema, + client: z.enum(['desktop', 'mobile']) + }) +]) /** Live, best-effort delivery; outcomes settled without the caller connected are not replayed. */ export function subscribeStructuredChatTelemetry(listener: TelemetryListener): () => void { @@ -40,13 +50,39 @@ export function recordRemoteStructuredChatTelemetry(value: unknown): boolean { receivedEvents.delete(receivedEvents.values().next().value!) } try { - recordAgentStartTelemetry(event, 'structured') + if (event.type === 'structuredChatPromptAccepted') { + track('native_chat_prompt_submitted', { + agent_kind: event.agent_kind, + runtime: 'remote', + client: event.client + }) + } else { + recordAgentStartTelemetry(event, 'structured') + } } catch { // A telemetry failure must not interrupt runtime event delivery. } return true } +/** A paired owner retains consent authority even when this host also runs a desktop. */ +export function recordStructuredChatPromptAccepted( + source: EventProps<'native_chat_prompt_submitted'>, + ownerKey: string | undefined +): void { + if (ownerKey) { + const event: StructuredChatPromptTelemetryEvent = { + type: 'structuredChatPromptAccepted', + id: randomUUID(), + agent_kind: source.agent_kind, + client: source.client + } + relayStructuredChatTelemetry(event, ownerKey) + } else if (isTelemetryInitialized()) { + track('native_chat_prompt_submitted', source) + } +} + export function recordStructuredChatAgentStart( telemetry: Pick< StructuredChatStartTelemetryEvent, diff --git a/src/main/telemetry/structured-chat-prompt-owner.test.ts b/src/main/telemetry/structured-chat-prompt-owner.test.ts new file mode 100644 index 00000000000..ff0cdbed471 --- /dev/null +++ b/src/main/telemetry/structured-chat-prompt-owner.test.ts @@ -0,0 +1,155 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import type { RpcContext } from '../runtime/rpc/core' +import type { RpcCallerIdentity } from '../runtime/rpc/rpc-caller-identity' +import { structuredCallerFor } from '../runtime/rpc/methods/structured-sessions/creation/structured-agent-session-gate' +import type { StructuredChatTelemetryEvent } from '../../shared/structured-chat-dispatch-telemetry' +import { _setCommonPropsForTests } from './client' +import { + BASE_COMMON, + cleanupTelemetryClientTest, + setupTelemetryClientTest, + type TelemetryClientTestState +} from './client-test-harness' +import { + recordRemoteStructuredChatTelemetry, + recordStructuredChatPromptAccepted, + subscribeStructuredChatTelemetry +} from './structured-chat-dispatch' + +const HOST_INSTALL = '11111111-1111-4111-8111-111111111111' +const OWNER_INSTALL = '22222222-2222-4222-8222-222222222222' +let active: TelemetryClientTestState | undefined +let relayed: { event: StructuredChatTelemetryEvent; owner: string }[] +let unsubscribe: () => void + +beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-11-08T23:58:00Z')) + relayed = [] + unsubscribe = subscribeStructuredChatTelemetry((event, owner) => relayed.push({ event, owner })) +}) +afterEach(() => { + unsubscribe() + if (active) { + cleanupTelemetryClientTest(active.envStash) + active = undefined + } + vi.useRealTimers() +}) + +function desktop(installId: string, optedIn: boolean): TelemetryClientTestState { + if (active) { + cleanupTelemetryClientTest(active.envStash) + } + active = setupTelemetryClientTest({ + installId, + optedIn, + existedBeforeTelemetryRelease: false, + protectedMilestones: { installCohort: 'fresh' } + }) + _setCommonPropsForTests({ ...BASE_COMMON, install_id: installId }) + return active +} + +function accept( + caller: RpcCallerIdentity, + clientKind?: 'mobile' | 'runtime', + pairedDeviceParentId?: string +): void { + const context: Partial = { caller, clientKind, pairedDeviceParentId } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: caller derivation reads only the supplied transport identity fields. + const identity = structuredCallerFor(context as RpcContext) + if (!identity.userClient) { + throw new Error('expected a transport-proven user') + } + recordStructuredChatPromptAccepted( + { agent_kind: 'codex', runtime: 'unknown', client: identity.userClient }, + identity.telemetryOwnerKey + ) +} + +const captured = (state: TelemetryClientTestState, name: string) => + state.mock.capture.mock.calls.map(([event]) => event).filter((event) => event.event === name) + +it('lets the originating desktop opt in even when its initialized execution host opts out', () => { + const host = desktop(HOST_INSTALL, false) + accept({ kind: 'paired-device', deviceId: 'originating-desktop' }, 'runtime') + expect(relayed).toEqual([ + { + owner: 'originating-desktop', + event: { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'codex', + client: 'desktop' + } + } + ]) + expect(host.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + expect(host.mock.capture.mock.calls).toEqual([]) + + const owner = desktop(OWNER_INSTALL, true) + recordRemoteStructuredChatTelemetry(relayed[0].event) + expect(owner.settings.telemetry?.protectedMilestones).toEqual({ + installCohort: 'fresh', + firstAgentInteractionDay: 20_765, + meaningfulActivityDay: 20_765 + }) + expect(captured(owner, 'first_agent_interaction')[0].properties).toMatchObject({ + install_id: OWNER_INSTALL, + source: 'native_chat' + }) + expect(captured(owner, 'meaningful_activity_day')[0].properties).toMatchObject({ + install_id: OWNER_INSTALL, + trigger: 'native_chat' + }) +}) + +it('respects a delegated phone owners opt-out even when its initialized execution host opts in', () => { + const host = desktop(HOST_INSTALL, true) + accept({ kind: 'paired-device', deviceId: 'delegated-phone' }, 'mobile', 'originating-desktop') + expect(relayed).toEqual([ + { + owner: 'originating-desktop', + event: { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'codex', + client: 'mobile' + } + } + ]) + expect(host.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + expect(host.mock.capture.mock.calls).toEqual([]) + + const owner = desktop(OWNER_INSTALL, false) + recordRemoteStructuredChatTelemetry(relayed[0].event) + expect(owner.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + expect(owner.mock.capture.mock.calls).toEqual([]) +}) + +it('keeps the local desktop and its directly paired phone on host installation consent', () => { + const host = desktop(HOST_INSTALL, true) + accept({ kind: 'desktop' }) + vi.setSystemTime(new Date('2026-11-09T00:01:00Z')) + accept({ kind: 'paired-device', deviceId: 'direct-phone' }, 'mobile') + expect(relayed).toEqual([]) + expect(host.settings.telemetry?.protectedMilestones).toEqual({ + installCohort: 'fresh', + firstAgentInteractionDay: 20_765, + meaningfulActivityDay: 20_766 + }) + expect(captured(host, 'first_agent_interaction')[0].properties).toMatchObject({ + install_id: HOST_INSTALL, + source: 'native_chat' + }) + expect( + captured(host, 'meaningful_activity_day').map((event) => ({ + install_id: event.properties.install_id, + trigger: event.properties.trigger + })) + ).toEqual([ + { install_id: HOST_INSTALL, trigger: 'native_chat' }, + { install_id: HOST_INSTALL, trigger: 'mobile_chat' } + ]) +}) diff --git a/src/preload/api/pty-api.ts b/src/preload/api/pty-api.ts index e7fef744f4e..68e22c51ffc 100644 --- a/src/preload/api/pty-api.ts +++ b/src/preload/api/pty-api.ts @@ -115,6 +115,8 @@ export type PtyApi = { shellReadyArmed?: boolean }> write: (id: string, data: string, inputKind: TerminalInputKind) => void + /** Genuine xterm input admitted by a local or remote transport; carries no terminal bytes. */ + noteUserInput?: () => void /** `requireWriteSettlement` waits for the provider's acknowledgment on any provider. */ writeAccepted: ( id: string, diff --git a/src/preload/api/pty-bridge-session-control.ts b/src/preload/api/pty-bridge-session-control.ts index 6be75eff418..b57e1593f61 100644 --- a/src/preload/api/pty-bridge-session-control.ts +++ b/src/preload/api/pty-bridge-session-control.ts @@ -101,6 +101,7 @@ export const ptySessionControlApi = { write: (id: string, data: string, inputKind: TerminalInputKind): void => { ipcRenderer.send('pty:write', { id, data, inputKind }) }, + noteUserInput: (): void => ipcRenderer.send('pty:noteUserInput'), writeAccepted: ( id: string, data: string, diff --git a/src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward-user-activity.test.ts b/src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward-user-activity.test.ts new file mode 100644 index 00000000000..9304ff49792 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward-user-activity.test.ts @@ -0,0 +1,123 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('@/store', () => ({ useAppStore: { getState: () => ({}) } })) +vi.mock('./codex-pane-stale', () => ({ isCodexPaneStale: () => false })) +vi.mock('@/lib/pane-manager/mobile-driver-state', () => ({ isPtyLocked: () => false })) +vi.mock('../../snapshots/replay-guard', () => ({ isPaneReplaying: () => false })) +vi.mock('../../input/terminal-input-quarantine', () => ({ + shouldDropQuarantinedTerminalInput: () => false +})) + +describe('admitted xterm user activity', () => { + beforeEach(() => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-11-08T23:59:59Z')) + }) + + afterEach(() => { + vi.unstubAllGlobals() + vi.useRealTimers() + }) + + async function createPaneInput(ptyId: string, accepted: boolean) { + const observations: unknown[][] = [] + vi.stubGlobal('window', { + api: { pty: { noteUserInput: (...payload: unknown[]) => observations.push(payload) } } + }) + let onUserInput = (): void => undefined + let onData = (_data: string): void => undefined + const writes: string[] = [] + const transport = { + getPtyId: () => ptyId, + sendInput: (data: string) => { + writes.push(data) + return accepted + } + } + const session = { + pane: { + id: 'pane-1', + terminal: { + _core: { + coreService: { + onUserInput: (listener: () => void) => { + onUserInput = listener + return { dispose: () => undefined } + } + } + }, + onData: (listener: (data: string) => void) => { + onData = listener + return { dispose: () => undefined } + } + } + }, + deps: {}, + transport, + noteTerminalInputForPty: () => undefined, + pendingTerminalInputIntentFor: () => undefined, + inferIntentFromExactTerminalInput: () => undefined, + claimViewportForUserActivity: () => undefined, + markAcceptedTerminalInputSent: () => undefined, + observeAcceptedShellCommandInput: () => undefined, + observeAcceptedTerminalInput: () => undefined, + observeSentTerminalInputIntent: () => undefined, + clearPendingTerminalInputIntent: () => undefined, + requestRecoveryForUndeliverableInput: () => undefined + } + const { installPtyInputForward } = await import('./pty-input-forward') + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: this fixture implements every member used by the ordinary-input path. + installPtyInputForward(session as never) + return { + observations, + writes, + transport, + type: (data: string) => { + onUserInput() + onData(data) + }, + report: (data: string) => onData(data) + } + } + + it.each(['local-pty', 'remote:host@@terminal-1'])( + 'observes genuine admitted typing for %s without reporting bytes', + async (ptyId) => { + const input = await createPaneInput(ptyId, true) + input.transport.sendInput('programmatic command\r') + input.report('\x1b[O') + expect(input.observations).toEqual([]) + + input.type('private prompt\r') + input.type('more typing') + expect(input.observations).toEqual([[]]) + expect(input.writes).toEqual([ + 'programmatic command\r', + '\x1b[O', + 'private prompt\r', + 'more typing' + ]) + + vi.setSystemTime(new Date('2026-11-09T00:00:01Z')) + input.type('activity immediately after UTC midnight') + expect(input.observations).toEqual([[], []]) + } + ) + + it('does not observe rejected transport admission', async () => { + const input = await createPaneInput('local-pty', false) + input.type('blocked input') + expect(input.observations).toEqual([]) + expect(input.writes).toEqual(['blocked input']) + }) + + it('keeps typing working if the observation bridge throws', async () => { + const input = await createPaneInput('local-pty', true) + window.api.pty.noteUserInput = () => { + throw new Error('bridge unavailable') + } + expect(() => input.type('still delivered')).not.toThrow() + expect(input.writes).toEqual(['still delivered']) + }) +}) diff --git a/src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward.ts b/src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward.ts index 49311f64665..67f4023dd5b 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection/input/pty-input-forward.ts @@ -17,6 +17,7 @@ import { import { isCodexPaneStale } from './codex-pane-stale' import { installTerminalSelectionFitGuard } from '../../viewport/terminal-selection-fit-guard' import type { ConnectPanePtySession } from '../connect-pane-pty-session' +import { recordAdmittedTerminalUserInput } from '@/lib/telemetry' export function installPtyInputForward(session: ConnectPanePtySession): void { session.forwardPtyInput = (data: string, wasUserInput = false): void => { @@ -96,6 +97,9 @@ export function installPtyInputForward(session: ConnectPanePtySession): void { .sendInputAccepted(data, inputKind) .then((accepted): boolean | Promise | null => { if (accepted) { + if (wasUserInput) { + recordAdmittedTerminalUserInput() + } // Why: rejected writes use transport recovery and must not arm a parser probe. session.markAcceptedTerminalInputSent() session.observeAcceptedShellCommandInput(data) @@ -122,6 +126,9 @@ export function installPtyInputForward(session: ConnectPanePtySession): void { if (intent) { session.claimViewportForUserActivity(data) if (session.transport.sendInput(data, inputKind)) { + if (wasUserInput) { + recordAdmittedTerminalUserInput() + } session.markAcceptedTerminalInputSent() session.observeAcceptedShellCommandInput(data) session.observeAcceptedTerminalInput(data, intent) @@ -133,6 +140,9 @@ export function installPtyInputForward(session: ConnectPanePtySession): void { } session.claimViewportForUserActivity(data) if (session.transport.sendInput(data, inputKind)) { + if (wasUserInput) { + recordAdmittedTerminalUserInput() + } session.markAcceptedTerminalInputSent() session.observeAcceptedShellCommandInput(data) session.observeAcceptedTerminalInput(data) diff --git a/src/renderer/src/lib/telemetry.ts b/src/renderer/src/lib/telemetry.ts index 175e81957fd..0a22a90bed3 100644 --- a/src/renderer/src/lib/telemetry.ts +++ b/src/renderer/src/lib/telemetry.ts @@ -3,6 +3,7 @@ import type { EventName, EventProps } from '../../../shared/telemetry-events' import type { TelemetryConsentState } from '../../../shared/telemetry-consent-types' +import { utcDayIndex } from '../../../shared/telemetry-day' // Re-exported so renderer call sites can import the mapper alongside `track` (impl is shared because main needs the same mapping). export { tuiAgentToAgentKind } from '../../../shared/agent-kind' @@ -10,6 +11,30 @@ export { tuiAgentToAgentKind } from '../../../shared/agent-kind' // Single source-of-truth for the privacy doc URL so FirstLaunchBanner and PrivacyPane can't drift. export const PRIVACY_URL = 'https://www.onorca.dev/docs/telemetry' +let lastTerminalUserInputAt: number | null = null + +/** Report admitted xterm input once a minute within each UTC day; main owns consent and daily deduplication. */ +export function recordAdmittedTerminalUserInput(): void { + const now = Date.now() + if ( + lastTerminalUserInputAt !== null && + utcDayIndex(lastTerminalUserInputAt) === utcDayIndex(now) && + now >= lastTerminalUserInputAt && + now - lastTerminalUserInputAt < 60_000 + ) { + return + } + try { + const noteUserInput = window.api?.pty?.noteUserInput + if (noteUserInput) { + lastTerminalUserInputAt = now + noteUserInput() + } + } catch (err) { + console.warn('[telemetry] terminal user input IPC failed', err) + } +} + // Why: the IPC boundary is untyped at runtime, so validate before the Privacy pane trusts a payload from main. function isTelemetryConsentState(x: unknown): x is TelemetryConsentState { if (!x || typeof x !== 'object') { diff --git a/src/shared/electron-remote-runtime-client-capabilities.ts b/src/shared/electron-remote-runtime-client-capabilities.ts index d52375805cc..53815534525 100644 --- a/src/shared/electron-remote-runtime-client-capabilities.ts +++ b/src/shared/electron-remote-runtime-client-capabilities.ts @@ -3,7 +3,10 @@ import { GIT_GRAPH_TAB_CAPABILITY } from './source-control/runtime/git-graph-cap import { STRUCTURED_AGENT_SESSION_RENDERER_CLIENT_CAPABILITIES } from './agent-session/structured-agent-session-renderer-client-capabilities' import { TERMINAL_IMAGE_VIEWER_CAPABILITY } from './terminal/images/terminal-image-viewer-capability' import { BROWSER_CLIENT_HOST_REQUEST_CAPABILITY } from './browser/client-hosting/browser-client-host-request-protocol' -import { STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY } from './structured-chat-dispatch-telemetry' +import { + STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY, + STRUCTURED_CHAT_PROMPT_TELEMETRY_CLIENT_CAPABILITY +} from './structured-chat-dispatch-telemetry' import { AGENT_STATUS_RESTORED_UNCONFIRMED_RUNTIME_CAPABILITY, BROWSER_CLIENT_HOST_RUNTIME_CAPABILITY, @@ -20,6 +23,7 @@ export const ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES = [ GIT_GRAPH_TAB_CAPABILITY, GIT_REVERT_CONFLICT_CLIENT_CAPABILITY, STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY, + STRUCTURED_CHAT_PROMPT_TELEMETRY_CLIENT_CAPABILITY, ...NATIVE_REMOTE_RUNTIME_CLIENT_CAPABILITIES, TERMINAL_IMAGE_VIEWER_CAPABILITY, // The renderer that reads a paired host's structured chats is the one that reads its own. diff --git a/src/shared/structured-chat-dispatch-telemetry.ts b/src/shared/structured-chat-dispatch-telemetry.ts index 64f732cb7b2..ae01efd3a52 100644 --- a/src/shared/structured-chat-dispatch-telemetry.ts +++ b/src/shared/structured-chat-dispatch-telemetry.ts @@ -3,6 +3,9 @@ import type { AgentKind } from './telemetry-events' export const STRUCTURED_CHAT_START_TELEMETRY_CLIENT_CAPABILITY = 'structured-chat-start-telemetry-v1' +export const STRUCTURED_CHAT_PROMPT_TELEMETRY_CLIENT_CAPABILITY = + 'structured-chat-prompt-telemetry-v1' + export type StructuredChatStartTelemetryEvent = { type: 'structuredChatAgentStart' id: string @@ -11,4 +14,13 @@ export type StructuredChatStartTelemetryEvent = { request_kind: 'new' | 'resume' } -export type StructuredChatTelemetryEvent = StructuredChatStartTelemetryEvent +export type StructuredChatPromptTelemetryEvent = { + type: 'structuredChatPromptAccepted' + id: string + agent_kind: AgentKind + client: 'desktop' | 'mobile' +} + +export type StructuredChatTelemetryEvent = + | StructuredChatStartTelemetryEvent + | StructuredChatPromptTelemetryEvent diff --git a/src/shared/telemetry-day.ts b/src/shared/telemetry-day.ts new file mode 100644 index 00000000000..a86d239ad20 --- /dev/null +++ b/src/shared/telemetry-day.ts @@ -0,0 +1,3 @@ +export function utcDayIndex(now = Date.now()): number { + return Math.floor(now / 86_400_000) +} diff --git a/src/shared/telemetry-event-registry.ts b/src/shared/telemetry-event-registry.ts index 4c8f584bf6b..b1fc75c4a40 100644 --- a/src/shared/telemetry-event-registry.ts +++ b/src/shared/telemetry-event-registry.ts @@ -110,6 +110,10 @@ import { workspaceCreateFailedSchema } from './telemetry-repository-event-schemas' import { pluginInstalledSchema } from './telemetry-plugin-event-schemas' +import { + firstAgentInteractionSchema, + meaningfulActivityDaySchema +} from './telemetry-protected-milestone-schemas' import { sshRemoteRuntimeResolvedSchema } from './telemetry-ssh-runtime-event-schemas' import { wslUsageSnapshotSchema } from './telemetry-wsl-usage-event-schemas' import { @@ -134,6 +138,8 @@ export const eventSchemas = { app_starred_orca: appStarredOrcaSchema, star_nag_outcome: starNagOutcomeEventSchema, feature_interaction_usage_bucket_reached: featureInteractionUsageBucketReachedSchema, + first_agent_interaction: firstAgentInteractionSchema, + meaningful_activity_day: meaningfulActivityDaySchema, repo_added: repoAddedSchema, add_repo_setup_step_action: addRepoSetupStepActionEventSchema, @@ -174,6 +180,8 @@ export const eventSchemas = { native_chat_toggled: nativeChatToggledSchema, native_chat_message_sent: nativeChatMessageSentSchema, + // Main-owned provider-accepted prompts; legacy message_sent includes commands and queued writes. + native_chat_prompt_submitted: nativeChatMessageSentSchema, native_chat_picker_opened: nativeChatPickerOpenedSchema, native_chat_picker_item_accepted: nativeChatPickerItemAcceptedSchema, native_chat_send_classified: nativeChatSendClassifiedSchema, diff --git a/src/shared/telemetry-protected-milestone-schemas.ts b/src/shared/telemetry-protected-milestone-schemas.ts new file mode 100644 index 00000000000..52c5d967ca7 --- /dev/null +++ b/src/shared/telemetry-protected-milestone-schemas.ts @@ -0,0 +1,42 @@ +import { z } from 'zod' +import { agentKindSchema } from './telemetry-property-schemas' + +// Frozen at first profile load on a build that tracks milestones; a new profile is not proof of a new machine. +export const milestoneInstallCohortSchema = z.enum(['fresh', 'existing']) +export type MilestoneInstallCohort = z.infer + +// unattributed_prompt: a hook saw a submitted prompt but cannot prove a person, not automation, typed it. +export const firstAgentInteractionSourceSchema = z.enum([ + 'orca_send', + 'native_chat', + 'mobile_chat', + 'unattributed_prompt' +]) +export type FirstAgentInteractionSource = z.infer + +// terminal_input: genuine xterm user input admitted by a local/remote transport, including buffered input; never its bytes. +export const meaningfulActivityTriggerSchema = z.enum([ + 'agent_started', + 'orca_send', + 'native_chat', + 'mobile_chat', + 'workspace_created', + 'terminal_input' +]) +export type MeaningfulActivityTrigger = z.infer + +// Observed prompt/message submission, never inferred from a launch or provider outcome. +export const firstAgentInteractionSchema = z + .object({ + source: firstAgentInteractionSourceSchema, + agent_kind: agentKindSchema, + install_cohort: milestoneInstallCohortSchema + }) + .strict() + +export const meaningfulActivityDaySchema = z + .object({ + trigger: meaningfulActivityTriggerSchema, + install_cohort: milestoneInstallCohortSchema + }) + .strict() diff --git a/src/shared/telemetry-settings-types.ts b/src/shared/telemetry-settings-types.ts index 6aebaabdc22..d0c34178505 100644 --- a/src/shared/telemetry-settings-types.ts +++ b/src/shared/telemetry-settings-types.ts @@ -1,4 +1,5 @@ import type { AgentDashboardSurface } from './telemetry-agent-dashboard-event-schemas' +import type { MilestoneInstallCohort } from './telemetry-protected-milestone-schemas' export type ChatUiLaunchTipTargeting = | { mode: 'all' } @@ -30,4 +31,10 @@ export type TelemetrySettings = { chatUiLaunchTipTargeting?: ChatUiLaunchTipTargeting /** Local UTC-day cursors cap dashboard observations across restarts; at most two writes a day. */ agentDashboardActiveDays?: Partial> + /** UTC-day cursors written only after the SDK accepted the milestone capture; at most two writes a day. */ + protectedMilestones?: { + installCohort?: MilestoneInstallCohort + firstAgentInteractionDay?: number + meaningfulActivityDay?: number + } } From 805ec4ee21d68c5c58d46a0891b2724575331658 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:59:11 +0000 Subject: [PATCH 11/80] fix(browser): show site favicons on browser tabs running on a remote host (#40049) * fix(browser): show site favicons on remote host browser tabs Host-run browser tabs mirrored to a paired client always showed the globe: the host's session-tab snapshot carried no favicon. Hosts now publish the active page's icon URL as an optional faviconUrl, capped at 2 KB and dropped when it points at the host's loopback. Headless hosts pick it up from page-favicon-updated and publish only when it changes; desktop hosts include it in the existing graph-sync key. Clients keep their current icon when an older host omits the field. * fix(browser): publish a remote tab icon on arrival, not on every swap Read the headless favicon through the agent browser bridge so the runtime stays free of Electron imports, publish only when a page gains an icon so animated icons ride later publishes, and drop IPv4-mapped loopback icon hosts. * fix(browser): clear a remote tab icon when a cross-origin load starts A load that fails on the new origin never commits, so clearing only on did-navigate kept the old site's icon beside the failed URL. Also give the runtime tests' bridge fakes the new favicon getter. * test: give the session-tab bridge fakes the favicon getter --- .../agent-browser-bridge-tabs.ts | 3 ++ .../manager/browser-manager-queries.ts | 7 ++++ .../manager/browser-manager-state.ts | 2 ++ ...browser-manager-guest-navigation-policy.ts | 33 +++++++++++++++++++ .../browser-manager-guest-cleanup.ts | 1 + .../renderer-browser-lifecycle.test.ts | 5 ++- ...orca-runtime-serve-browser-restore.test.ts | 3 +- ...ld-headless-mobile-session-browser-tabs.ts | 1 + .../mobile-session-browser-equality.ts | 1 + .../src/components/browser-favicon.tsx | 2 +- .../browser-address-bar-suggestions.ts | 2 +- ...rowser-page-webview-navigation-handlers.ts | 2 +- .../mobile-session-browser-tabs.ts | 2 ++ .../sync-runtime-graph/sync-projections.ts | 5 ++- .../mirrored-browser-tabs.ts | 10 +++--- .../browser/browser-favicon-url.test.ts | 31 +++++++++++++++++ .../browser}/browser-favicon-url.ts | 25 ++++++++++++++ .../runtime-mobile-session-tab-contracts.ts | 2 ++ ...le-session-active-publication.unit.test.ts | 5 ++- ...ile-session-legacy-navigation.unit.test.ts | 1 + 20 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 src/shared/browser/browser-favicon-url.test.ts rename src/{renderer/src/components/browser-pane/describe-page => shared/browser}/browser-favicon-url.ts (61%) diff --git a/src/main/browser/agent-browser/session-routing/agent-browser-bridge-tabs.ts b/src/main/browser/agent-browser/session-routing/agent-browser-bridge-tabs.ts index 279663f28f2..8221270d93b 100644 --- a/src/main/browser/agent-browser/session-routing/agent-browser-bridge-tabs.ts +++ b/src/main/browser/agent-browser/session-routing/agent-browser-bridge-tabs.ts @@ -119,6 +119,9 @@ export abstract class AgentBrowserBridgeTabs extends AgentBrowserBridgeState { return null } } + getRemoteTabFaviconUrl(browserPageId: string): string | null { + return this.browserManager.getBrowserPageRemoteTabFaviconUrl(browserPageId) + } onTabChanged(webContentsId: number, worktreeId?: string): void { this.selectForPerson(webContentsId, worktreeId) this.options.onTabsChanged?.(worktreeId) diff --git a/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts b/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts index 6d58e813a9a..39148d307f6 100644 --- a/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts +++ b/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts @@ -113,6 +113,13 @@ export abstract class BrowserManagerQueries extends BrowserManagerRegistration { : (this.loadErrorsByGuestId.get(webContentsId) ?? null) } + getBrowserPageRemoteTabFaviconUrl(browserPageId: string): string | null { + const webContentsId = this.webContentsIdByTabId.get(browserPageId) + return webContentsId === undefined + ? null + : (this.remoteTabFaviconUrlByGuestId.get(webContentsId) ?? null) + } + getBrowserPageCertificateFailure(browserPageId: string): BrowserCertificateFailure | null { return this.certificateTrustController?.getFailure(browserPageId) ?? null } diff --git a/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts b/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts index 0f232699aea..70c46275235 100644 --- a/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts +++ b/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts @@ -164,6 +164,8 @@ export abstract class BrowserManagerState extends BrowserManagerViewportScrollSt protected readonly loadErrorsByGuestId = new Map() // Restore the optimistically hidden error if navigation aborts. protected readonly clearedLoadErrorsByGuestId = new Map() + // Offscreen pages have no renderer to hold their icon for session-tab rows. + protected readonly remoteTabFaviconUrlByGuestId = new Map() protected browserGuestStateChangedListener: ((worktreeId: string) => void) | null = null protected offscreenTabOpener: ((request: OffscreenBrowserTabOpenRequest) => void) | null = null protected certificateTrustController: BrowserCertificateTrustController | null = null diff --git a/src/main/browser/guest-lifecycle/manager/navigation/browser-manager-guest-navigation-policy.ts b/src/main/browser/guest-lifecycle/manager/navigation/browser-manager-guest-navigation-policy.ts index 0d39ea34f57..cf70c4ee0bc 100644 --- a/src/main/browser/guest-lifecycle/manager/navigation/browser-manager-guest-navigation-policy.ts +++ b/src/main/browser/guest-lifecycle/manager/navigation/browser-manager-guest-navigation-policy.ts @@ -11,6 +11,11 @@ import { import { allowPageUnload } from '../../navigation/browser-guest-unload-policy' import { initiatingFrameUrl } from '../../prompts/browser-external-app-launch' import { isSameExtensionNavigation } from '../../../../../shared/browser/extensions/browser-extension-url' +import { + browserNavigationLeavesFaviconOrigin, + pickDisplayableFaviconUrl, + remoteTabFaviconUrl +} from '../../../../../shared/browser/browser-favicon-url' export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManagerGuestPopupPolicy { protected installGuestNavigationPolicy(guest: Electron.WebContents): () => void { @@ -118,6 +123,10 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager if (!isMainFrame || isChromiumInternalErrorUrl(url)) { return } + // Like the tab strip: a load that fails on the new origin never commits to clear it. + if (!isInPlace && browserNavigationLeavesFaviconOrigin(guest.getURL(), url)) { + this.remoteTabFaviconUrlByGuestId.delete(guest.id) + } // getURL() still names the committed document; UA writers need the in-flight target. this.startPendingNavigation(guest.id, url) void this.presentTabIdentityAtNavigationStart(guest, url, isInPlace) @@ -138,6 +147,10 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager let publishedUrl: string | null = null const didNavigateHandler = (_event: Electron.Event, url: string): void => { + // A redirect can still land on another origin. + if (browserNavigationLeavesFaviconOrigin(publishedUrl, url)) { + this.remoteTabFaviconUrlByGuestId.delete(guest.id) + } publishedUrl = guest.getURL() this.pendingNavigationByGuestId.delete(guest.id) // Committing makes the hidden error obsolete; a later abort must not restore it. @@ -164,11 +177,30 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager } } + // Renderer webviews keep their own icon. An offscreen page's icon arriving publishes; later + // swaps (animated or status icons) ride the page's next publish instead of one per frame. + const faviconUpdatedHandler = (_event: Electron.Event, favicons: string[]): void => { + if (!this.offscreenGuestIds.has(guest.id)) { + return + } + const next = remoteTabFaviconUrl(pickDisplayableFaviconUrl(favicons)) + const hadIcon = this.remoteTabFaviconUrlByGuestId.has(guest.id) + if (next) { + this.remoteTabFaviconUrlByGuestId.set(guest.id, next) + } else { + this.remoteTabFaviconUrlByGuestId.delete(guest.id) + } + if (next && !hadIcon) { + this.notifyBrowserGuestStateChanged(guest.id) + } + } + guest.on('will-navigate', willNavigateHandler) guest.on('will-redirect', willRedirectHandler) guest.on('did-start-navigation', didStartNavigationHandler) guest.on('did-navigate', didNavigateHandler) guest.on('did-navigate-in-page', didNavigateInPageHandler) + guest.on('page-favicon-updated', faviconUpdatedHandler) guest.on('did-fail-load', didFailLoadHandler) // Why emit: an extension-blocked guest main frame commits no error page, so Electron reports // nothing; every did-fail-load reader (this policy, the pane, webNavigation) needs the failure. @@ -201,6 +233,7 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager guest.off('did-start-navigation', didStartNavigationHandler) guest.off('did-navigate', didNavigateHandler) guest.off('did-navigate-in-page', didNavigateInPageHandler) + guest.off('page-favicon-updated', faviconUpdatedHandler) guest.off('did-fail-load', didFailLoadHandler) guest.off('will-prevent-unload', allowPageUnload) } diff --git a/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts b/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts index 25787d1c159..c0812f2fa58 100644 --- a/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts +++ b/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts @@ -37,6 +37,7 @@ export abstract class BrowserManagerGuestCleanup extends BrowserManagerGuestNavi this.pendingLoadFailuresByGuestId.delete(guestWebContentsId) this.loadErrorsByGuestId.delete(guestWebContentsId) this.clearedLoadErrorsByGuestId.delete(guestWebContentsId) + this.remoteTabFaviconUrlByGuestId.delete(guestWebContentsId) this.pendingPermissionEventsByGuestId.delete(guestWebContentsId) this.pendingPopupEventsByGuestId.delete(guestWebContentsId) this.cancelPendingDownloadsForGuest(guestWebContentsId) diff --git a/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts b/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts index ec5d8f6bbc5..5b956775376 100644 --- a/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts +++ b/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts @@ -51,7 +51,10 @@ function browserRuntime(closeTab = vi.fn(async (_pageId: string) => {}), runtime ownsTab: (pageId) => livePages.has(pageId) && !desktopPageIds.has(pageId) }) // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: Only the bridge inventory is used by this lifecycle test. - runtime.setAgentBrowserBridge({ tabList: () => ({ tabs: [...livePages.values()] }) } as never) + runtime.setAgentBrowserBridge({ + tabList: () => ({ tabs: [...livePages.values()] }), + getRemoteTabFaviconUrl: () => null + } as never) electronMocks.BrowserWindow.fromId.mockImplementation((id: number) => id === 1 ? { isDestroyed: () => false } : null ) diff --git a/src/main/runtime/browser/headless/orca-runtime-serve-browser-restore.test.ts b/src/main/runtime/browser/headless/orca-runtime-serve-browser-restore.test.ts index edf1f68d464..2c2399366d0 100644 --- a/src/main/runtime/browser/headless/orca-runtime-serve-browser-restore.test.ts +++ b/src/main/runtime/browser/headless/orca-runtime-serve-browser-restore.test.ts @@ -30,7 +30,7 @@ function restartedServe(session: WorkspaceSessionState = savedServeTabs()) { ownsTab: () => true }) runtime.setAgentBrowserBridge( - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the runtime reads only tabList from the bridge on these paths. + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the runtime reads only these bridge methods on these paths. { tabList: vi.fn(() => ({ tabs: livePages.map((browserPageId, index) => ({ @@ -42,6 +42,7 @@ function restartedServe(session: WorkspaceSessionState = savedServeTabs()) { active: false })) })), + getRemoteTabFaviconUrl: () => null, getRegisteredTabs: vi.fn(() => registered()), snapshot: vi.fn(async () => ({ snapshot: '' })), getActivePageId: vi.fn(() => livePages[0] ?? null), diff --git a/src/main/runtime/session-lifecycle/projection/build-headless-mobile-session-browser-tabs.ts b/src/main/runtime/session-lifecycle/projection/build-headless-mobile-session-browser-tabs.ts index 7bc7298e4e1..a191155093d 100644 --- a/src/main/runtime/session-lifecycle/projection/build-headless-mobile-session-browser-tabs.ts +++ b/src/main/runtime/session-lifecycle/projection/build-headless-mobile-session-browser-tabs.ts @@ -67,6 +67,7 @@ export abstract class OrcaRuntimeWithBuildHeadlessMobileSessionBrowserTabs exten loading: false, canGoBack: false, canGoForward: false, + faviconUrl: this.agentBrowserBridge?.getRemoteTabFaviconUrl(tab.browserPageId) ?? null, loadError: tab.loadError ?? undefined, certificateFailure: tab.certificateFailure ?? undefined, ...(persistedProps ? { color: persistedProps.color } : {}), diff --git a/src/main/runtime/session-lifecycle/projection/mobile-session-browser-equality.ts b/src/main/runtime/session-lifecycle/projection/mobile-session-browser-equality.ts index e05ab7a166b..48e766c514a 100644 --- a/src/main/runtime/session-lifecycle/projection/mobile-session-browser-equality.ts +++ b/src/main/runtime/session-lifecycle/projection/mobile-session-browser-equality.ts @@ -19,6 +19,7 @@ export function headlessBrowserTabsUnchanged( tab.loading === prev.loading && tab.canGoBack === prev.canGoBack && tab.canGoForward === prev.canGoForward && + (tab.faviconUrl ?? null) === (prev.faviconUrl ?? null) && tab.browserProfileId === prev.browserProfileId && tab.executionHostKey === prev.executionHostKey && ((tab.placement === undefined && prev.placement === undefined) || diff --git a/src/renderer/src/components/browser-favicon.tsx b/src/renderer/src/components/browser-favicon.tsx index d8de2b5d647..86e1ba67c31 100644 --- a/src/renderer/src/components/browser-favicon.tsx +++ b/src/renderer/src/components/browser-favicon.tsx @@ -1,7 +1,7 @@ import { useState } from 'react' import { Globe, Loader2 } from 'lucide-react' import { cn } from '@/lib/utils' -import { displayableFaviconUrl } from './browser-pane/describe-page/browser-favicon-url' +import { displayableFaviconUrl } from '../../../shared/browser/browser-favicon-url' export function BrowserFavicon({ faviconUrl, diff --git a/src/renderer/src/components/browser-pane/assemble-chrome/browser-address-bar-suggestions.ts b/src/renderer/src/components/browser-pane/assemble-chrome/browser-address-bar-suggestions.ts index b783a607c7a..4ecd873eebd 100644 --- a/src/renderer/src/components/browser-pane/assemble-chrome/browser-address-bar-suggestions.ts +++ b/src/renderer/src/components/browser-pane/assemble-chrome/browser-address-bar-suggestions.ts @@ -17,7 +17,7 @@ import { matchBrowserHistory, prepareBrowserHistoryEntries } from '@/lib/browser/history/browser-history-match' -import { displayableFaviconUrl } from '../describe-page/browser-favicon-url' +import { displayableFaviconUrl } from '../../../../../shared/browser/browser-favicon-url' import { formatBrowserPaletteUrl } from '@/lib/browser/palette/browser-palette-search' import { isClipboardTextByteLengthOverLimit } from '../../../../../shared/clipboard-text' import { translate } from '@/i18n/i18n' diff --git a/src/renderer/src/components/browser-pane/host-guest/browser-page-webview-navigation-handlers.ts b/src/renderer/src/components/browser-pane/host-guest/browser-page-webview-navigation-handlers.ts index 1728de5c9ef..57893cc0f36 100644 --- a/src/renderer/src/components/browser-pane/host-guest/browser-page-webview-navigation-handlers.ts +++ b/src/renderer/src/components/browser-pane/host-guest/browser-page-webview-navigation-handlers.ts @@ -16,7 +16,7 @@ import { import { browserNavigationLeavesFaviconOrigin, pickDisplayableFaviconUrl -} from '../describe-page/browser-favicon-url' +} from '../../../../../shared/browser/browser-favicon-url' import { applyBrowserExtensionPageFavicon, isChromeExtensionPageUrl diff --git a/src/renderer/src/runtime/sync-runtime-graph/mobile-session-browser-tabs.ts b/src/renderer/src/runtime/sync-runtime-graph/mobile-session-browser-tabs.ts index e2d02ffa0fc..4ffff1aba3c 100644 --- a/src/renderer/src/runtime/sync-runtime-graph/mobile-session-browser-tabs.ts +++ b/src/renderer/src/runtime/sync-runtime-graph/mobile-session-browser-tabs.ts @@ -2,6 +2,7 @@ import type { AppState } from '@/store/types' import type { RuntimeMobileSessionBrowserTab } from '../../../../shared/runtime-types' import type { Tab } from '../../../../shared/tab-types' import { editorRowHost } from '../../../../shared/workspace-session/session-tab-file-host' +import { remoteTabFaviconUrl } from '../../../../shared/browser/browser-favicon-url' import type { MobileSessionWorktreeInputs } from './types' import { isUnifiedTabActiveInActiveGroup } from './mobile-session-surfaces' @@ -29,6 +30,7 @@ export function buildMobileBrowserTab( loading: activePage?.loading ?? workspace.loading, canGoBack: activePage?.canGoBack ?? workspace.canGoBack, canGoForward: activePage?.canGoForward ?? workspace.canGoForward, + faviconUrl: remoteTabFaviconUrl(activePage?.faviconUrl), // Null means the active page cleared its failure; do not resurrect a stale workspace error. loadError: activePage ? activePage.loadError : workspace.loadError, certificateFailure: activePage diff --git a/src/renderer/src/runtime/sync-runtime-graph/sync-projections.ts b/src/renderer/src/runtime/sync-runtime-graph/sync-projections.ts index b2894b012a0..17752835bbd 100644 --- a/src/renderer/src/runtime/sync-runtime-graph/sync-projections.ts +++ b/src/renderer/src/runtime/sync-runtime-graph/sync-projections.ts @@ -1,5 +1,6 @@ import type { AppState } from '@/store/types' import { resolveTerminalTabTitle } from '../../../../shared/tab-title-resolution' +import { remoteTabFaviconUrl } from '../../../../shared/browser/browser-favicon-url' import { stableHashString } from './editor-draft-hash' import type { TerminalTab } from '../../../../shared/workspace-layout/terminal-tab-types' import { getOpenFileIndexes } from './mobile-session-inputs' @@ -198,7 +199,9 @@ function buildBrowserPagesProjection( url: page.url, loading: page.loading, canGoBack: page.canGoBack, - canGoForward: page.canGoForward + canGoForward: page.canGoForward, + // An icon arriving resyncs; later swaps (animated icons) ride the next sync. + hasRemoteFavicon: remoteTabFaviconUrl(page.faviconUrl) !== null })) ) } diff --git a/src/renderer/src/runtime/web-session-tabs-sync/mirrored-browser-tabs.ts b/src/renderer/src/runtime/web-session-tabs-sync/mirrored-browser-tabs.ts index 78031d00c2e..4ccd080eaf8 100644 --- a/src/renderer/src/runtime/web-session-tabs-sync/mirrored-browser-tabs.ts +++ b/src/renderer/src/runtime/web-session-tabs-sync/mirrored-browser-tabs.ts @@ -116,14 +116,15 @@ export function clientHostsMirroredBrowserPage(tab: RuntimeMobileSessionBrowserT export function resolveMirroredBrowserPageContent( tab: RuntimeMobileSessionBrowserTab, existingPage: BrowserPage | undefined -): Pick { +): Pick { if (clientHostsMirroredBrowserPage(tab) && existingPage) { return { url: existingPage.url, title: existingPage.title, loading: existingPage.loading, canGoBack: existingPage.canGoBack, - canGoForward: existingPage.canGoForward + canGoForward: existingPage.canGoForward, + faviconUrl: existingPage.faviconUrl } } return { @@ -131,7 +132,9 @@ export function resolveMirroredBrowserPageContent( title: resolveMirroredBrowserTitle(tab, existingPage), loading: tab.loading, canGoBack: tab.canGoBack, - canGoForward: tab.canGoForward + canGoForward: tab.canGoForward, + // Old hosts omit the field; keep what this client already has. + faviconUrl: tab.faviconUrl === undefined ? (existingPage?.faviconUrl ?? null) : tab.faviconUrl } } @@ -187,7 +190,6 @@ export function buildMirroredBrowserTabs( workspaceId, worktreeId: snapshot.worktree, ...content, - faviconUrl: existing?.page.faviconUrl ?? null, // Guest owns client-hosted loadError; host omission must not clear it. loadError: (tab.placement?.kind === 'client' ? existing?.page.loadError : tab.loadError) ?? null, diff --git a/src/shared/browser/browser-favicon-url.test.ts b/src/shared/browser/browser-favicon-url.test.ts new file mode 100644 index 00000000000..1ee4a67e8c3 --- /dev/null +++ b/src/shared/browser/browser-favicon-url.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from 'vitest' +import { remoteTabFaviconUrl } from './browser-favicon-url' + +describe('remoteTabFaviconUrl', () => { + it('publishes public icons and small inline icons', () => { + expect(remoteTabFaviconUrl('https://www.onorca.dev/favicon.ico?v=1')).toBe( + 'https://www.onorca.dev/favicon.ico?v=1' + ) + expect(remoteTabFaviconUrl('http://192.168.1.20:3000/icon.png')).toBe( + 'http://192.168.1.20:3000/icon.png' + ) + expect(remoteTabFaviconUrl('data:image/png;base64,AAAA')).toBe('data:image/png;base64,AAAA') + }) + + it('drops icons only the host can reach', () => { + for (const url of [ + 'http://localhost:5173/favicon.ico', + 'http://127.0.0.1:3000/icon.png', + 'http://[::1]:8080/favicon.ico', + 'http://0.0.0.0:3000/favicon.ico', + 'https://app.localhost/favicon.ico', + 'http://[::ffff:127.0.0.1]:3000/favicon.ico' + ]) { + expect(remoteTabFaviconUrl(url)).toBeNull() + } + }) + + it('drops inline icons over the size cap', () => { + expect(remoteTabFaviconUrl(`data:image/png;base64,${'A'.repeat(2048)}`)).toBeNull() + }) +}) diff --git a/src/renderer/src/components/browser-pane/describe-page/browser-favicon-url.ts b/src/shared/browser/browser-favicon-url.ts similarity index 61% rename from src/renderer/src/components/browser-pane/describe-page/browser-favicon-url.ts rename to src/shared/browser/browser-favicon-url.ts index 0a29f235af0..5d4bcaa85f7 100644 --- a/src/renderer/src/components/browser-pane/describe-page/browser-favicon-url.ts +++ b/src/shared/browser/browser-favicon-url.ts @@ -1,3 +1,5 @@ +import { isEligibleLocalCertificateHost, isWildcardBindHost } from '../browser-url' + // Chromium emits icon updates only when the URL list changes; rendering and navigation listeners must share origin/icon rules. export function displayableFaviconUrl(faviconUrl: string | null | undefined): string | null { @@ -28,6 +30,29 @@ export function pickDisplayableFaviconUrl(favicons: readonly string[] | undefine return null } +// Session-tab rows resend whole on every change, so a page's inline data: icon must not grow them. +const MAX_REMOTE_TAB_FAVICON_URL_LENGTH = 2048 +// URL serializes [::ffff:127.0.0.1] as [::ffff:7f00:1]. +const IPV4_MAPPED_LOOPBACK = /^\[::ffff:7f[0-9a-f]{2}:[0-9a-f]{1,4}\]$/i + +/** The favicon a host publishes for a remote client, which loads it from its own network. */ +export function remoteTabFaviconUrl(faviconUrl: string | null | undefined): string | null { + const displayable = displayableFaviconUrl(faviconUrl) + if (!displayable || displayable.length > MAX_REMOTE_TAB_FAVICON_URL_LENGTH) { + return null + } + if (displayable.startsWith('data:')) { + return displayable + } + // The host's loopback names a different machine on the client. + const hostname = new URL(displayable).hostname + return isWildcardBindHost(hostname) || + isEligibleLocalCertificateHost(hostname) || + IPV4_MAPPED_LOOPBACK.test(hostname) + ? null + : displayable +} + function faviconOrigin(rawUrl: string | null | undefined): string | null { if (!rawUrl) { return null diff --git a/src/shared/runtime-mobile-session-tab-contracts.ts b/src/shared/runtime-mobile-session-tab-contracts.ts index 2f4379451de..df20d7d795a 100644 --- a/src/shared/runtime-mobile-session-tab-contracts.ts +++ b/src/shared/runtime-mobile-session-tab-contracts.ts @@ -98,6 +98,8 @@ export type RuntimeMobileSessionBrowserTab = { loading: boolean canGoBack: boolean canGoForward: boolean + /** From `remoteTabFaviconUrl`; null clears it. Old hosts omit it, so clients keep what they have. */ + faviconUrl?: string | null loadError?: BrowserLoadError | null certificateFailure?: BrowserCertificateFailure | null color?: string | null diff --git a/tests/e2e/session-tabs/mobile-session-active-publication.unit.test.ts b/tests/e2e/session-tabs/mobile-session-active-publication.unit.test.ts index ea51af2bc48..b98b088d157 100644 --- a/tests/e2e/session-tabs/mobile-session-active-publication.unit.test.ts +++ b/tests/e2e/session-tabs/mobile-session-active-publication.unit.test.ts @@ -656,7 +656,10 @@ describe('host active-tab publication', () => { ownsTab: () => true }) // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: Saved unstarted pages require only this bridge inventory. - runtime.setAgentBrowserBridge({ tabList: () => ({ tabs: [] }) } as never) + runtime.setAgentBrowserBridge({ + tabList: () => ({ tabs: [] }), + getRemoteTabFaviconUrl: () => null + } as never) runtime.syncWindowGraph(0, { tabs: [], leaves: [] }) expect((await list(runtime)).activeTabId).toBe('page-b') const seen: RuntimeMobileSessionTabsResult[] = [] diff --git a/tests/e2e/session-tabs/mobile-session-legacy-navigation.unit.test.ts b/tests/e2e/session-tabs/mobile-session-legacy-navigation.unit.test.ts index 7c1d1bef2dc..f09d32a878b 100644 --- a/tests/e2e/session-tabs/mobile-session-legacy-navigation.unit.test.ts +++ b/tests/e2e/session-tabs/mobile-session-legacy-navigation.unit.test.ts @@ -161,6 +161,7 @@ describe('legacy host navigation through inventory reconstruction', () => { active: index === 0 })) }), + getRemoteTabFaviconUrl: () => null, tabSwitch: vi.fn(async () => ({ switched: 1, browserPageId: 'page-b' })), getRegisteredTabs: () => new Map([ From 620a6efa5d70e383e0676077cd63129132090ea1 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Sun, 8 Nov 2026 17:03:42 -0800 Subject: [PATCH 12/80] test: remove 10 low-value push durability tests (#40053) Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .../durable-push-payload-parse-budget.test.ts | 170 ------------------ .../apps/push/src/durable-push-store.test.ts | 17 -- .../apps/push/src/durable-push-worker.test.ts | 114 +----------- .../push/src/host-challenge-store.test.ts | 9 - 4 files changed, 1 insertion(+), 309 deletions(-) delete mode 100644 cloud/apps/push/src/durable-push-payload-parse-budget.test.ts diff --git a/cloud/apps/push/src/durable-push-payload-parse-budget.test.ts b/cloud/apps/push/src/durable-push-payload-parse-budget.test.ts deleted file mode 100644 index 8e8b89443c2..00000000000 --- a/cloud/apps/push/src/durable-push-payload-parse-budget.test.ts +++ /dev/null @@ -1,170 +0,0 @@ -import { afterEach, expect, it, vi } from 'vitest' -import { DurablePushStore } from './durable-push-store.js' -import type { PushDatabase } from './push-database.js' -import { - cleanupDurablePushFixtures, - fixture, - notification -} from './durable-push-store.test-fixture.js' - -afterEach(async () => { - vi.restoreAllMocks() - await cleanupDurablePushFixtures() -}) - -function captureDatabaseErrors(database: PushDatabase, errors: unknown[]): PushDatabase { - return { - dialect: database.dialect, - query: async (sql, params) => { - try { - return await database.query(sql, params) - } catch (error) { - errors.push(error) - throw error - } - }, - transaction: (run) => database.transaction((tx) => run(captureDatabaseErrors(tx, errors))), - lockQuotaScope: (key) => database.lockQuotaScope(key), - tryLockScope: (key) => database.tryLockScope(key), - tryLockSharedScope: (key) => database.tryLockSharedScope(key), - close: () => database.close() - } -} - -it('parses each leased row once and preserves its complete serialized payload', async () => { - const { db, store } = await fixture() - const input = { - ...notification(1), - body: 'Unicode: 🐋\ud800', - extra: { first: [null, false, 3], next: { z: 'last', a: 'first' } } - } - await store.accept('host', 'phone', input) - const [row] = await db.query('SELECT * FROM push_delivery_batches') - if (!row) { - throw new Error('Missing delivery row') - } - const payload = String(row.payload_json) - const parse = vi.spyOn(JSON, 'parse') - const delivery = await store.claim() - expect(delivery).toEqual({ - id: row.batch_id, - registrationId: 'phone', - hostFingerprint: 'host', - notification: input, - expiresAt: 1_300_000, - lease: expect.any(String), - attempts: 1 - }) - expect(JSON.stringify(delivery?.notification)).toBe(payload) - expect(parse.mock.calls.filter(([value]) => value === payload)).toHaveLength(1) -}) - - - -it('reads changed retry bytes and a later writer update without carrying a parsed result across calls', async () => { - const { db, store, advance } = await fixture() - await store.accept('host', 'phone', notification(1)) - const first = await store.claim() - if (!first) { - throw new Error('Missing first delivery') - } - first.notification.body = 'provider changed this retry' - await store.finish(first, 1000) - advance(1000) - const retriedPayload = JSON.stringify(first.notification) - const parse = vi.spyOn(JSON, 'parse') - const retry = await store.claim() - expect(retry).toEqual({ ...first, lease: expect.any(String), attempts: 2 }) - expect(retry?.lease).not.toBe(first.lease) - expect(retry?.notification).not.toBe(first.notification) - expect(JSON.stringify(retry?.notification)).toBe(retriedPayload) - const retryParses = parse.mock.calls.filter(([value]) => value === retriedPayload).length - if (!retry) { - throw new Error('Missing retry delivery') - } - await store.finish(retry, 1000) - const changed = { ...notification(1), body: 'fresh database row', title: 'Changed' } - const changedPayload = JSON.stringify(changed) - await db.query('UPDATE push_delivery_batches SET payload_json = ? WHERE batch_id = ?', [ - changedPayload, - first.id - ]) - advance(1000) - const fresh = await store.claim() - expect(fresh).toEqual({ ...retry, notification: changed, lease: expect.any(String), attempts: 3 }) - expect(JSON.stringify(fresh?.notification)).toBe(changedPayload) - expect(retry.notification.body).toBe('provider changed this retry') - expect(retryParses).toBe(1) - expect(parse.mock.calls.filter(([value]) => value === changedPayload)).toHaveLength(1) -}) - - - - - -it.each(['not JSON', 'undefined', 'null', '[]'])( - 'preserves invalid payload rejection and rolls back the lease for %s', - async (payload) => { - const { db, store } = await fixture() - await store.accept('host', 'phone', notification(1)) - await db.query('UPDATE push_delivery_batches SET payload_json = ?', [payload]) - const [before] = await db.query('SELECT * FROM push_delivery_batches') - const parse = vi.spyOn(JSON, 'parse') - let caught: unknown - try { - await store.claim() - } catch (error) { - caught = error - } - expect(caught).toBeInstanceOf(Error) - const index = parse.mock.calls.findIndex(([value]) => value === payload) - expect(index).toBeGreaterThanOrEqual(0) - if (payload === 'not JSON' || payload === 'undefined') { - expect(caught).toBe(parse.mock.results[index]?.value) - expect(caught).toBeInstanceOf(SyntaxError) - } else { - expect(caught).toMatchObject({ message: 'invalid_push_delivery_payload' }) - } - expect(await db.query('SELECT * FROM push_delivery_batches')).toEqual([before]) - expect(parse.mock.calls.filter(([value]) => value === payload)).toHaveLength(1) - } -) - -it('preserves the exact database UPDATE error and retries with a fresh payload after rollback', async () => { - const { db, store, clock } = await fixture() - await store.accept('host', 'phone', notification(1)) - const [before] = await db.query('SELECT * FROM push_delivery_batches') - if (!before) { - throw new Error('Missing delivery row') - } - const originalQuery = db.query.bind(db) - const errors: unknown[] = [] - // SQLite raises a native error in the real transaction; PostgreSQL uses its real constraint. - await originalQuery( - db.dialect === 'sqlite' - ? "CREATE TRIGGER deny_lease BEFORE UPDATE ON push_delivery_batches BEGIN SELECT RAISE(FAIL, 'deny_lease'); END" - : 'ALTER TABLE push_delivery_batches ADD CONSTRAINT deny_lease CHECK (lease_until = 0)' - ) - const owner = new DurablePushStore(captureDatabaseErrors(db, errors), clock) - const parse = vi.spyOn(JSON, 'parse') - let caught: unknown - try { - await owner.claim() - } catch (error) { - caught = error - } - expect(errors).toHaveLength(1) - expect(caught).toBe(errors[0]) - expect(await originalQuery('SELECT * FROM push_delivery_batches')).toEqual([before]) - expect(parse.mock.calls.filter(([value]) => value === String(before.payload_json))).toHaveLength( - 1 - ) - await originalQuery( - db.dialect === 'sqlite' - ? 'DROP TRIGGER deny_lease' - : 'ALTER TABLE push_delivery_batches DROP CONSTRAINT deny_lease' - ) - const fresh = await owner.claim() - expect(fresh?.notification).toEqual(notification(1)) - expect(fresh?.attempts).toBe(1) -}) diff --git a/cloud/apps/push/src/durable-push-store.test.ts b/cloud/apps/push/src/durable-push-store.test.ts index b9331192e0c..d8387793d45 100644 --- a/cloud/apps/push/src/durable-push-store.test.ts +++ b/cloud/apps/push/src/durable-push-store.test.ts @@ -163,23 +163,6 @@ it('does not resurrect an in-flight alert after a dismissal and transient provid expect(await store.pendingCount('phone')).toBe(0) }) -it.each([false, true])( - 'normalizes default alert kind (explicit first: %s)', - async (explicitFirst) => { - const { db, store } = await fixture() - const { kind: _kind, ...implicit } = notification(1) - const explicit = { kind: 'alert' as const, ...implicit } - for (const event of explicitFirst ? [explicit, implicit] : [implicit, explicit]) { - expect(await store.accept('host', 'phone', event)).toBe('queued') - } - expect(await store.pendingCount('phone')).toBe(1) - expect(await db.query('SELECT event_id FROM push_events')).toHaveLength(1) - expect(await store.accept('host', 'phone', { ...explicit, body: 'changed' })).toBe('error') - expect(await store.accept('host', 'phone', { ...implicit, kind: 'dismiss' })).toBe('queued') - expect(await db.query('SELECT event_id FROM push_events')).toHaveLength(2) - } -) - it('fences late renew and finish after an expired claim is dismissed', async () => { const { db, store, advance } = await fixture() const alert = notification(1) diff --git a/cloud/apps/push/src/durable-push-worker.test.ts b/cloud/apps/push/src/durable-push-worker.test.ts index c5a77e32c29..5eb84f35110 100644 --- a/cloud/apps/push/src/durable-push-worker.test.ts +++ b/cloud/apps/push/src/durable-push-worker.test.ts @@ -8,7 +8,7 @@ import { PushDeviceRegistryStore } from './device-registry-store.js' import { openInMemoryPushDatabase } from './push-database.js' import type { PushDelivery } from './push-delivery-message.js' import type { PushProviderOutcome } from './push-provider-outcome.js' -import { WORKER_DRAINS } from './push-worker-concurrency.js' + import { pauseAfter, within } from './durable-push-store.test-fixture.js' const cleanups: (() => Promise)[] = [] @@ -69,59 +69,6 @@ async function fixture() { } } -it('sends every burst event immediately with its original content and identity', async () => { - const h = await fixture() - await h.accept(note(1)) - await h.accept( - note(2, { agentState: 'needs-input', title: 'Answer needed', body: 'Please respond' }) - ) - await h.worker.runDue() - expect(h.send).toHaveBeenCalledTimes(2) - const first = h.send.mock.calls.find(([delivery]) => delivery.orca.notificationSeq === 1)![0] - const second = h.send.mock.calls.find(([delivery]) => delivery.orca.notificationSeq === 2)![0] - expect(first).toMatchObject({ - title: 'Done', - body: 'Finished task', - orca: { - notificationId: 'note-1', - notificationSeq: 1 - } - }) - expect(second).toMatchObject({ - title: 'Answer needed', - body: 'Please respond', - orca: { notificationId: 'note-2', notificationSeq: 2 } - }) - expect(first.collapseId).not.toBe(second.collapseId) - expect(h.send.mock.calls.every(([delivery]) => !('coalescedCount' in delivery.orca))).toBe(true) - expect(h.send.mock.calls.every(([delivery]) => !('summaryMembers' in delivery.orca))).toBe(true) - expect(h.onRetry).not.toHaveBeenCalled() -}) - -it('keeps untrackable bells and per-phone deliveries individually replaceable', async () => { - const h = await fixture() - const other = await h.devices.upsert({ - hostFingerprint: 'host', - deviceId: 'phone2', - platform: 'android', - token: 'other-token' - }) - if (!other.ok) throw new Error('registration failed') - await h.accept(note(1, { notificationId: undefined, source: 'terminal-bell', agentState: null })) - await h.accept(note(2)) - await h.accept(note(3)) - await h.store.accept('host', other.registrationId, note(2)) - await h.worker.runDue() - expect(h.send).toHaveBeenCalledTimes(4) - const deliveries = h.send.mock.calls.map(([delivery]) => delivery) - const primary = deliveries.filter((delivery) => delivery.registrationId === h.registrationId) - expect(primary).toHaveLength(3) - expect(new Set(primary.map((delivery) => delivery.collapseId)).size).toBe(3) - expect( - deliveries.find((delivery) => delivery.registrationId === other.registrationId) - ).toMatchObject({ orca: { notificationId: 'note-2', notificationSeq: 2 } }) -}) - it('preserves provider retry delay when waking a restarted worker', async () => { const h = await fixture() vi.useFakeTimers() @@ -302,62 +249,3 @@ it('claims new devices while an earlier provider response is still pending', asy await h.worker.stop() } }) - - - - - -it('coalesces wakes arriving after every active drain found an empty queue', async () => { - const h = await fixture() - vi.useFakeTimers() - const entered = Promise.withResolvers() - const release = Promise.withResolvers() - const claim = h.store.claim.bind(h.store) - let empty = 0 - let holding = true - vi.spyOn(h.store, 'claim').mockImplementation(async () => { - const delivery = await claim() - if (!delivery && holding) { - if (++empty === WORKER_DRAINS) entered.resolve() - await release.promise - } - return delivery - }) - h.worker.start() - const startedAt = Date.now() - const pending = h.worker.runDue() - try { - await entered.promise - await h.accept(note(1)) - for (let count = 0; count < 10; count++) h.worker.wake() - expect(h.store.claim).toHaveBeenCalledTimes(WORKER_DRAINS) - holding = false - release.resolve() - await pending - await vi.waitFor(() => expect(h.send).toHaveBeenCalledOnce()) - await vi.waitFor(() => expect(h.store.claim).toHaveBeenCalledTimes(WORKER_DRAINS + 2)) - expect(Date.now() - startedAt).toBeLessThan(1000) - expect(h.send.mock.calls[0]?.[0].orca.notificationSeq).toBe(1) - } finally { - holding = false - release.resolve() - } -}) - - - -it('recovers from a failed automatic wake without leaving another timer', async () => { - const h = await fixture() - vi.useFakeTimers() - vi.spyOn(h.store, 'claim').mockRejectedValueOnce(new Error('database unavailable')) - h.worker.start() - h.worker.wake() - await vi.waitFor(() => - expect(console.warn).toHaveBeenCalledWith(JSON.stringify({ event: 'orca_push_worker_failed' })) - ) - await h.accept(note(1)) - h.worker.wake() - await vi.waitFor(() => expect(h.send).toHaveBeenCalledOnce()) - await h.worker.stop() - expect(vi.getTimerCount()).toBe(0) -}) diff --git a/cloud/apps/push/src/host-challenge-store.test.ts b/cloud/apps/push/src/host-challenge-store.test.ts index 62dd815f52e..f73a4f53ea8 100644 --- a/cloud/apps/push/src/host-challenge-store.test.ts +++ b/cloud/apps/push/src/host-challenge-store.test.ts @@ -75,14 +75,5 @@ describe('push host challenge store', () => { }) }) - it('rejects an unknown challenge id and a malformed public key', async () => { - await expect(store.verify('missing', Buffer.alloc(32, 9).toString('base64'))).resolves.toEqual({ - ok: false, - reason: 'unknown_challenge' - }) - await expect(store.issue('not-base64!!')).resolves.toBeNull() - await expect(store.issue(Buffer.alloc(31, 1).toString('base64'))).resolves.toBeNull() - }) - }) From bf7dfff664f3956647d9985516a01caf4d65393f Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:08:24 -0800 Subject: [PATCH 13/80] Keep Session History responsive on large histories: move session search SQLite to worker threads (#38945) * Move session search SQLite into dedicated workers * test: spawn the session search test worker with the shared worker helper * fix: pass only drain promises to the channel's drain wait * Repair session search failure and lifecycle boundaries * Stop indexing tool output and reclaim existing rows * test: say why the session search worker tests need a longer timeout * Bound search cleanup and preserve failure and digest semantics * Use the database row type for tool purge batches * test: drop the same-connection mid-page write case now that each page reads one snapshot * test: prove a mid-page delete from the writer can't change that page's hits * Repair transcript cancellation cleanup and search test runtime admission * fix: cancel active transcript finalization and enrichment waits * fix: close transcript handles when cancellation wins delivery * fix: preserve scan cancellation errors across parser waits * test: prove cold session search bypasses an active writer batch * fix: preserve search continuation through background tool cleanup * fix: preserve search pages during orphan reclamation * fix: bound typo repair posting visibility probes * fix: preserve exact search terms when visibility is inconclusive * test: drop orphan-reclaim cursor staleness case superseded by frozen continuation * Bound session search work before ranking candidates * Preserve the limit notice for empty search windows * Bound session eligibility lookups and reuse FTS scoring statistics * Index session scopes for population and candidate searches * Derive search status from the live index after clear * Preserve disabled search status during settings transitions * Share planned expressions between spelling probes and search * Keep identifier fragments out of spelling presence checks * Report search initialization through its pending transition * Bound spelling vocabulary work with the search posting budget * Preserve search fallback while bounding distinct vocabulary seeks * fix(session-search): drain removals before the worker queue fills; keep aborted close waits from failing teardown * fix(session-search): hold writer commands when the worker queue is full instead of stopping the writer * chore(ratchets): baseline the session search worker's one-shot entry check --- .../build-plugins/main-build-entry-points.ts | 1 + .../build-plugins/plain-node-entry-guard.ts | 15 +- .../worker-thread-entry-names.ts | 14 + config/electron-builder.config.cjs | 1 + config/main-sync-fs-baseline.txt | 1 + .../server-bundle/orcad-entry-build.mjs | 1 + .../vitest-sqlite-runtime-scan.mjs | 6 +- skill-guides/orca-cli.md | 2 +- src/cli/bundled-skill-guides.ts | 2 +- src/cli/specs/search.ts | 2 +- .../session-search-child-service.ts | 11 +- .../session-search-cursor-snapshot.ts | 64 ++++ .../session-search-deleted-sources.test.ts | 1 + .../session-search-deleted-sources.ts | 9 +- .../session-search-engine-types.ts | 2 +- .../ai-vault-search/session-search-engine.ts | 143 +++++++-- .../session-search-file-write.test.ts | 6 +- .../session-search-host-registration.test.ts | 1 + .../session-search-index-consumer.test.ts | 21 ++ .../session-search-index-consumer.ts | 58 +++- .../session-search-index-generation.ts | 20 ++ .../session-search-index-pass.ts | 8 +- .../session-search-index-writer.test.ts | 45 --- .../session-search-index-writer.ts | 198 +++++++----- .../session-search-indexer-test-fixture.ts | 1 + .../session-search-indexer.test.ts | 54 +++- .../ai-vault-search/session-search-indexer.ts | 35 ++- .../session-search-instance.test.ts | 119 ++++++- .../session-search-instance.ts | 183 ++++++----- .../session-search-lag-recovery.test.ts | 53 ++++ .../session-search-lifecycle-matrix.test.ts | 5 +- .../session-search-live-transcript.test.ts | 8 +- .../session-search-merged-roots.test.ts | 3 +- .../session-search-message-rows.test.ts | 27 +- .../session-search-message-rows.ts | 12 +- ...ession-search-native-chat-indexing.test.ts | 3 +- .../session-search-opencode-index.test.ts | 9 +- .../session-search-orphan-rows.test.ts | 29 -- .../session-search-paging.test.ts | 69 ++-- .../ai-vault-search/session-search-pass.ts | 6 +- .../session-search-posting-window.test.ts | 170 ++++++++++ .../session-search-postings.ts | 87 +++++ .../session-search-ranked-cursor.ts | 88 ++++++ .../session-search-reader-snapshot.test.ts | 42 +++ .../session-search-readers.test.ts | 68 ++++ .../ai-vault-search/session-search-readers.ts | 92 ++++++ .../session-search-request-deadline.ts | 20 ++ .../session-search-restart-reindex.test.ts | 5 +- .../session-search-retention-delete.test.ts | 43 ++- .../session-search-retention-delete.ts | 22 +- .../session-search-retrieval.ts | 174 +++++++--- .../session-search-root-refresh.test.ts | 5 +- .../session-search-row-filter.ts | 69 +++- .../ai-vault-search/session-search-schema.ts | 16 + .../session-search-service.test.ts | 4 +- .../session-search-store-types.ts | 48 +++ .../ai-vault-search/session-search-store.ts | 86 +++-- .../session-search-synthetic-corpus.test.ts | 2 +- .../session-search-tool-purge.test.ts | 124 ++++++++ .../session-search-tool-purge.ts | 129 ++++++++ .../ai-vault-search/session-search-trace.ts | 31 ++ .../session-search-typo-repair.ts | 122 +++++--- .../session-search-typo-scope.test.ts | 43 ++- .../session-search-worker-client.test.ts | 38 +++ .../session-search-worker-client.ts | 124 ++++++++ .../session-search-worker-entry.ts | 296 ++++++++++++++++++ .../session-search-worker-protocol.ts | 60 ++++ .../session-search-worker-store-base.ts | 169 ++++++++++ .../session-search-worker-store.ts | 206 ++++++++++++ .../session-search-worker.test-fixture.ts | 28 ++ .../session-search-worker.test.ts | 216 +++++++++++++ .../session-search-write-buffer.test.ts | 33 +- .../session-search-write-buffer.ts | 49 ++- .../ai-vault/ai-vault-scan-cancellation.ts | 11 + .../session-parse-cache-database.ts | 5 + .../session-parse-file-lane.test.ts | 37 +++ .../parse-cache/session-parse-file-lane.ts | 33 +- .../session-scanner-parse-cache.ts | 30 +- .../session-scanner-service-client-state.ts | 10 +- .../session-scanner-service-client.ts | 2 +- .../session-scanner-service-entry.test.ts | 22 ++ .../session-scanner-service-entry.ts | 25 +- .../session-scanner-service-protocol.ts | 4 + ...n-scanner-service-search-lifecycle.test.ts | 68 ++++ ...ssion-scanner-service-search-roots.test.ts | 2 +- .../session-scanner-service-search.test.ts | 2 + .../session-scanner-service-search.ts | 69 +++- .../claude/session-scanner-primary-parsers.ts | 4 +- .../session-scanner-subagent-transcripts.ts | 12 +- .../cline/session-scanner-cline-parser.ts | 6 +- .../codex/session-scanner-codex-parser.ts | 7 +- .../session-scanner-cursor-chat-meta.ts | 5 +- .../devin/session-scanner-devin-parser.ts | 3 +- .../gemini/session-scanner-gemini-parsers.ts | 3 +- .../grok/session-scanner-grok-parser.ts | 23 +- .../hermes/session-scanner-hermes-parser.ts | 3 +- .../jcode/session-scanner-jcode-parser.ts | 31 +- .../kimi/session-scanner-kimi-parser.ts | 37 ++- .../kimi/session-scanner-kimi-paths.ts | 44 +-- .../kiro/session-scanner-kiro-parser.ts | 8 +- .../muse/session-scanner-muse-parser.ts | 25 +- ...ession-scanner-omp-subagent-transcripts.ts | 17 +- .../session-scanner-opencode-parser.ts | 35 ++- .../scanners/session-scanner-graph-parsers.ts | 46 +-- .../scanners/session-scanner-rovo-metadata.ts | 25 ++ .../ai-vault/session-scanner-agent-parser.ts | 29 +- .../ai-vault/session-scanner-jsonl-reader.ts | 125 ++++---- .../session-scanner-parse-wsl-stall.test.ts | 59 ++++ src/main/ai-vault/session-scanner-types.ts | 1 + .../transcripts/session-document-stream.ts | 16 +- .../transcripts/session-transcript-channel.ts | 11 + .../session-transcript-consumers.ts | 4 + .../transcripts/session-transcript-reader.ts | 34 +- ...-vault-search-all-hosts-real-index.test.ts | 19 +- .../ai-vault-search-all-hosts.test.ts | 53 ++++ .../ipc/ai-vault/ai-vault-search-all-hosts.ts | 33 +- .../ai-vault/ai-vault-search-merged-cursor.ts | 32 +- src/main/ipc/ai-vault/ai-vault-search.test.ts | 16 + .../native-chat/wsl-transcript-fs-access.ts | 51 ++- src/main/observability/tracer.ts | 9 + .../worker-threads/lazy-worker-thread-host.ts | 4 + .../ai-vault/search/AiVaultPanelSearch.tsx | 4 +- .../search/use-ai-vault-search.test.tsx | 25 ++ .../ai-vault/search/use-ai-vault-search.ts | 14 +- .../SessionHistorySettingsPane.tsx | 2 +- src/renderer/src/i18n/locales/de.json | 2 - src/renderer/src/i18n/locales/en.json | 4 +- src/renderer/src/i18n/locales/es.json | 2 - src/renderer/src/i18n/locales/fr.json | 2 - src/renderer/src/i18n/locales/ja.json | 2 - src/renderer/src/i18n/locales/ko.json | 2 - src/renderer/src/i18n/locales/pt.json | 2 - src/renderer/src/i18n/locales/ru.json | 2 - src/renderer/src/i18n/locales/tr.json | 2 - src/renderer/src/i18n/locales/zh-Hant.json | 2 - src/renderer/src/i18n/locales/zh.json | 2 - .../ai-vault/search/ai-vault-search-types.ts | 4 +- 137 files changed, 4274 insertions(+), 906 deletions(-) create mode 100644 config/build-plugins/worker-thread-entry-names.ts create mode 100644 src/main/ai-vault-search/session-search-cursor-snapshot.ts create mode 100644 src/main/ai-vault-search/session-search-lag-recovery.test.ts create mode 100644 src/main/ai-vault-search/session-search-posting-window.test.ts create mode 100644 src/main/ai-vault-search/session-search-postings.ts create mode 100644 src/main/ai-vault-search/session-search-ranked-cursor.ts create mode 100644 src/main/ai-vault-search/session-search-reader-snapshot.test.ts create mode 100644 src/main/ai-vault-search/session-search-readers.test.ts create mode 100644 src/main/ai-vault-search/session-search-readers.ts create mode 100644 src/main/ai-vault-search/session-search-request-deadline.ts create mode 100644 src/main/ai-vault-search/session-search-store-types.ts create mode 100644 src/main/ai-vault-search/session-search-tool-purge.test.ts create mode 100644 src/main/ai-vault-search/session-search-tool-purge.ts create mode 100644 src/main/ai-vault-search/session-search-trace.ts create mode 100644 src/main/ai-vault-search/session-search-worker-client.test.ts create mode 100644 src/main/ai-vault-search/session-search-worker-client.ts create mode 100644 src/main/ai-vault-search/session-search-worker-entry.ts create mode 100644 src/main/ai-vault-search/session-search-worker-protocol.ts create mode 100644 src/main/ai-vault-search/session-search-worker-store-base.ts create mode 100644 src/main/ai-vault-search/session-search-worker-store.ts create mode 100644 src/main/ai-vault-search/session-search-worker.test-fixture.ts create mode 100644 src/main/ai-vault-search/session-search-worker.test.ts create mode 100644 src/main/ai-vault/parse-cache/session-parse-file-lane.test.ts create mode 100644 src/main/ai-vault/scan-service/session-scanner-service-search-lifecycle.test.ts create mode 100644 src/main/ai-vault/scanners/session-scanner-rovo-metadata.ts diff --git a/config/build-plugins/main-build-entry-points.ts b/config/build-plugins/main-build-entry-points.ts index de85496fb88..1521adc3dc3 100644 --- a/config/build-plugins/main-build-entry-points.ts +++ b/config/build-plugins/main-build-entry-points.ts @@ -20,6 +20,7 @@ export const MAIN_BUILD_ENTRY_POINTS: Record = { ), 'stt-worker': resolve('src/main/speech/stt-worker.ts'), 'warp-theme-parser-worker': resolve('src/main/warp-themes/warp-theme-parser-worker.ts'), + 'session-search-worker-entry': resolve('src/main/ai-vault-search/session-search-worker-entry.ts'), 'foreign-sqlite-reader-entry': resolve( 'src/main/foreign-sqlite-readers/foreign-sqlite-reader-entry.ts' ), diff --git a/config/build-plugins/plain-node-entry-guard.ts b/config/build-plugins/plain-node-entry-guard.ts index 69f7c8114c1..1a031934482 100644 --- a/config/build-plugins/plain-node-entry-guard.ts +++ b/config/build-plugins/plain-node-entry-guard.ts @@ -1,3 +1,4 @@ +import { WORKER_THREAD_ENTRY_NAMES } from './worker-thread-entry-names' import { spawn } from 'node:child_process' import { isBuiltin } from 'node:module' import { join } from 'node:path' @@ -28,20 +29,6 @@ const PLAIN_NODE_ENTRY_NAMES = [ ...CLI_MAIN_ENTRY_NAMES ] as const -// Electron is unavailable in main-process worker threads too (verified Electron 43); scan their graphs rather than trusting entry comments. -const WORKER_THREAD_ENTRY_NAMES = [ - 'warp-theme-parser-worker', - 'foreign-sqlite-reader-entry', - 'main-thread-hang-watchdog-entry', - 'port-scan-command-worker-entry', - 'command-path-directory-worker-entry', - 'worker-thread-child-process-entry', - ...(['usage-scan', 'workspace-space-scan'] as const).map((n) => `${n}-worker-entry` as const), - 'claude-profile-setup-worker-entry', - ...(['backup', 'writer'] as const).map((kind) => `profile-state-${kind}-worker-entry` as const), - 'plugin-source-transpile-worker-entry' -] as const - export const GUARDED_ENTRY_NAMES = [ ...PLAIN_NODE_ENTRY_NAMES, ...WORKER_THREAD_ENTRY_NAMES diff --git a/config/build-plugins/worker-thread-entry-names.ts b/config/build-plugins/worker-thread-entry-names.ts new file mode 100644 index 00000000000..3f72cea6df2 --- /dev/null +++ b/config/build-plugins/worker-thread-entry-names.ts @@ -0,0 +1,14 @@ +// Electron is unavailable in main-process worker threads too (verified Electron 43); scan their graphs rather than trusting entry comments. +export const WORKER_THREAD_ENTRY_NAMES = [ + 'warp-theme-parser-worker', + 'foreign-sqlite-reader-entry', + 'session-search-worker-entry', + 'main-thread-hang-watchdog-entry', + 'port-scan-command-worker-entry', + 'command-path-directory-worker-entry', + 'worker-thread-child-process-entry', + ...(['usage-scan', 'workspace-space-scan'] as const).map((n) => `${n}-worker-entry` as const), + 'claude-profile-setup-worker-entry', + ...(['backup', 'writer'] as const).map((kind) => `profile-state-${kind}-worker-entry` as const), + 'plugin-source-transpile-worker-entry' +] as const diff --git a/config/electron-builder.config.cjs b/config/electron-builder.config.cjs index a727851532d..face51a2e73 100644 --- a/config/electron-builder.config.cjs +++ b/config/electron-builder.config.cjs @@ -368,6 +368,7 @@ module.exports = { 'out/main/session-scanner-service-entry.js', 'out/main/wsl-transcript-fs-process-entry.js', 'out/main/foreign-sqlite-reader-entry.js', + 'out/main/session-search-worker-entry.js', 'out/main/plugin-host-entry.js', 'out/main/browser-extension-native-host-supervisor-entry.js', 'out/main/computer-sidecar.js', diff --git a/config/main-sync-fs-baseline.txt b/config/main-sync-fs-baseline.txt index ff70d4af49b..9868d39cfa7 100644 --- a/config/main-sync-fs-baseline.txt +++ b/config/main-sync-fs-baseline.txt @@ -14,6 +14,7 @@ 14 src/main/agent-launch/trust/agent-trust-presets.ts 2 src/main/agent-launch/trust/antigravity-workspace-trust-ownership.ts 1 src/main/agent-launch/trust/execution-host-workspace-trust.ts +1 src/main/ai-vault-search/session-search-worker-client.ts 4 src/main/ai-vault/parse-cache/session-parse-cache-legacy-json.ts 2 src/main/ai-vault/scan-service/session-scanner-service-entry-path.ts 1 src/main/ai-vault/scan-service/session-scanner-service-spawn.ts diff --git a/config/scripts/orcad-distribution/server-bundle/orcad-entry-build.mjs b/config/scripts/orcad-distribution/server-bundle/orcad-entry-build.mjs index ad7a1da8c5a..cc412c5eb09 100644 --- a/config/scripts/orcad-distribution/server-bundle/orcad-entry-build.mjs +++ b/config/scripts/orcad-distribution/server-bundle/orcad-entry-build.mjs @@ -20,6 +20,7 @@ export const ORCAD_CHILD_ENTRY_POINTS = { daemon: 'src/main/daemon/daemon-entry.ts', writer: 'src/main/persistence/profile-state/profile-state-writer-worker-entry.ts', backup: 'src/main/persistence/profile-state/profile-state-backup-worker-entry.ts', + sessionSearch: 'src/main/ai-vault-search/session-search-worker-entry.ts', foreignSqliteReader: 'src/main/foreign-sqlite-readers/foreign-sqlite-reader-entry.ts', portScanCommandWorker: 'src/main/ports/port-scan-command-worker-entry.ts', commandPathDirectoryWorker: 'src/main/ipc/command-path/command-path-directory-worker-entry.ts', diff --git a/config/scripts/test-runtime/sqlite-admission/vitest-sqlite-runtime-scan.mjs b/config/scripts/test-runtime/sqlite-admission/vitest-sqlite-runtime-scan.mjs index a79aea4f639..2259db54cb3 100644 --- a/config/scripts/test-runtime/sqlite-admission/vitest-sqlite-runtime-scan.mjs +++ b/config/scripts/test-runtime/sqlite-admission/vitest-sqlite-runtime-scan.mjs @@ -22,10 +22,14 @@ const SQLITE_HARNESSES = new Set([ 'terminal-topology-profile-fixture', 'acknowledged-terminal-tab-retirement-fixture', // Opens the index read-only after the indexer closes; Bun's macOS SQLite refuses a WAL file then. - 'session-search-indexer-test-fixture' + 'session-search-indexer-test-fixture', + 'session-search-index-test-fixture', + 'session-search-engine-test-fixture' ]) export const SQLITE_MODULES = [ 'src/main/sqlite/sync-database', + 'src/main/ai-vault-search/session-search-schema', + 'src/main/ai-vault-search/session-search-store', 'src/main/persistence/profile-state/profile-state-sqlite-authority', 'src/main/native-chat/agent-session-journal/database/journal-host-database', 'src/main/runtime/orchestration/db', diff --git a/skill-guides/orca-cli.md b/skill-guides/orca-cli.md index 36d41f69743..523f0ec335b 100644 --- a/skill-guides/orca-cli.md +++ b/skill-guides/orca-cli.md @@ -344,7 +344,7 @@ Search rules: - Quote a multi-word query; unquoted words are read as command names. - Search for a distinctive phrase or identifier, not a description of the topic. An exact sentence matches as a phrase first, then as all of its words, then as any of them. -- `--scope all` (the default) covers conversation turns, commands, and tool output; `--scope conversation` keeps user and assistant turns only. +- `--scope all` (the default) and `--scope conversation` search user and assistant turns; tool output is excluded. - Each hit carries the session, a snippet with the matched text marked, and a `resumeCommand`. `--debug` adds the route the host used. - Check `--index-status --json` first. Search runs only where a human turned it on under Settings → Agent Session History; when `enabled` is false, say so and stop. There is no CLI way to turn it on. - While `phase` is `indexing`, results can be incomplete. `--fresh` waits up to five seconds for the host to catch up, then searches anyway. diff --git a/src/cli/bundled-skill-guides.ts b/src/cli/bundled-skill-guides.ts index 27f797ad678..c5d92714523 100644 --- a/src/cli/bundled-skill-guides.ts +++ b/src/cli/bundled-skill-guides.ts @@ -572,7 +572,7 @@ const ORCA_CLI_MARKDOWN = [ "", "- Quote a multi-word query; unquoted words are read as command names.", "- Search for a distinctive phrase or identifier, not a description of the topic. An exact sentence matches as a phrase first, then as all of its words, then as any of them.", - "- `--scope all` (the default) covers conversation turns, commands, and tool output; `--scope conversation` keeps user and assistant turns only.", + "- `--scope all` (the default) and `--scope conversation` search user and assistant turns; tool output is excluded.", "- Each hit carries the session, a snippet with the matched text marked, and a `resumeCommand`. `--debug` adds the route the host used.", "- Check `--index-status --json` first. Search runs only where a human turned it on under Settings → Agent Session History; when `enabled` is false, say so and stop. There is no CLI way to turn it on.", "- While `phase` is `indexing`, results can be incomplete. `--fresh` waits up to five seconds for the host to catch up, then searches anyway.", diff --git a/src/cli/specs/search.ts b/src/cli/specs/search.ts index 314132a81cc..36d8a3a0e5d 100644 --- a/src/cli/specs/search.ts +++ b/src/cli/specs/search.ts @@ -27,7 +27,7 @@ export const SEARCH_COMMAND_SPECS: CommandSpec[] = [ 'Searches one host: this machine, or the paired Orca server named by --environment / --pairing-code. There is no all-computers search.', 'In an Orca SSH terminal, the forwarded CLI searches the controlling Orca runtime by default. Use --environment / --pairing-code to select a paired server; --path only filters results on the selected runtime.', 'Quote a multi-word query, or pass it as --query ""; unquoted words are read as command names.', - '--scope conversation searches user and assistant turns only; --scope all (the default) also searches commands and tool output.', + '--scope conversation and --scope all (the default) search user and assistant turns; tool output is excluded.', '--fresh waits up to five seconds for the host to reconcile its index before searching, then searches anyway.', '--agent and --path may be repeated. --path is a literal execution-host path and is not expanded or resolved against the current directory.', '--since takes an ISO 8601 timestamp with an offset, for example 2026-08-01T00:00:00Z.', diff --git a/src/main/ai-vault-search/session-search-child-service.ts b/src/main/ai-vault-search/session-search-child-service.ts index 47c32a34819..68099b7fd69 100644 --- a/src/main/ai-vault-search/session-search-child-service.ts +++ b/src/main/ai-vault-search/session-search-child-service.ts @@ -7,7 +7,7 @@ import { } from '../ai-vault/scan-service/session-scanner-service-spawn' import type { SessionSearchService } from './session-search-service' -/** Forward to the database-owning child; failed searches return not-ready while logging the error. */ +/** Forward to the database-owning child, preserving query failures for the caller. */ export function createChildSessionSearchService( calls = { search: searchSessionsInService, @@ -16,14 +16,7 @@ export function createChildSessionSearchService( } ): SessionSearchService { return { - search: async (request, hostScope) => { - try { - return await calls.search(request, hostScope) - } catch (error) { - console.warn('[ai-vault-search] search failed', error) - return { kind: 'unavailable', reason: 'not-ready' } - } - }, + search: (request, hostScope) => calls.search(request, hostScope), status: async (): Promise => { try { return await calls.status() diff --git a/src/main/ai-vault-search/session-search-cursor-snapshot.ts b/src/main/ai-vault-search/session-search-cursor-snapshot.ts new file mode 100644 index 00000000000..71e0a3debb9 --- /dev/null +++ b/src/main/ai-vault-search/session-search-cursor-snapshot.ts @@ -0,0 +1,64 @@ +import type { SessionFileCandidate } from '../ai-vault/session-scanner-types' +import type { SessionSearchFileIdentity } from './session-search-file-cursor' +import type { SessionSearchSnapshotRow } from './session-search-worker-protocol' + +export abstract class SessionSearchCursorSnapshot { + protected readonly committed = new Map() + protected readonly speculative = new Map() + abstract get acceptingReads(): boolean + files() { + return [...this.committed.values()] + } + protected row(path: string, identity: SessionSearchFileIdentity, speculative = true) { + if (!this.acceptingReads) { + return null + } + const row = (speculative && this.speculative.get(path)) || this.committed.get(path) + return row && + (!identity || + !row.identity || + (identity.dev === row.identity.dev && identity.ino === row.identity.ino)) + ? row + : null + } + indexedFile(path: string, identity: SessionSearchFileIdentity) { + const row = this.row(path, identity) + return row + ? { byteOffset: row.byteOffset, sizeBytes: row.sizeBytes, mtimeMs: row.mtimeMs } + : null + } + coversTranscript(candidate: SessionFileCandidate): boolean { + const { file } = candidate + const row = this.committed.get(file.path) + return ( + this.acceptingReads && + !!row && + row.state === 'current' && + row.mtimeMs === file.mtimeMs && + typeof file.sizeBytes === 'number' && + row.sizeBytes === file.sizeBytes && + row.byteOffset === file.sizeBytes && + (typeof file.dev !== 'number' || + typeof file.ino !== 'number' || + (row.identity?.dev === file.dev && row.identity.ino === file.ino)) + ) + } + resumableCursor(candidate: SessionFileCandidate) { + const { file } = candidate + const row = this.row( + file.path, + typeof file.dev === 'number' && typeof file.ino === 'number' + ? { dev: file.dev, ino: file.ino } + : null + ) + return row && + row.state === 'current' && + row.prefixHash && + row.byteOffset !== null && + row.byteOffset > 0 && + typeof file.sizeBytes === 'number' && + row.byteOffset < file.sizeBytes + ? { byteOffset: row.byteOffset, prefixHash: row.prefixHash } + : null + } +} diff --git a/src/main/ai-vault-search/session-search-deleted-sources.test.ts b/src/main/ai-vault-search/session-search-deleted-sources.test.ts index 853b23c7ace..77e37e041a1 100644 --- a/src/main/ai-vault-search/session-search-deleted-sources.test.ts +++ b/src/main/ai-vault-search/session-search-deleted-sources.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { mkdir, rm, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { afterEach, beforeEach, expect, it } from 'vitest' diff --git a/src/main/ai-vault-search/session-search-deleted-sources.ts b/src/main/ai-vault-search/session-search-deleted-sources.ts index 553b2a226d4..c62916c6bf6 100644 --- a/src/main/ai-vault-search/session-search-deleted-sources.ts +++ b/src/main/ai-vault-search/session-search-deleted-sources.ts @@ -3,10 +3,12 @@ import type { SessionSearchDegradedRoot } from './session-search-degraded-roots' import type { SessionSearchDirectoryReader } from './session-search-directory-listings' import { isUnderScanRoot } from './session-search-scan-roots' import { splitSyntheticSessionSource } from './session-search-synthetic-sources' -import type { SessionSearchStore } from './session-search-store' +import type { SessionSearchIndexStore } from './session-search-store' // Walk missing directories upward to seek a listing that proves which component disappeared. const MISSING_DIRECTORY = new Set(['ENOENT', 'ENOTDIR']) +// Cached listings retire without awaiting; drain held removals so a huge sweep stays bounded. +const REMOVALS_PER_FLUSH = 256 export type SessionSearchRetirement = { retired: string[] @@ -19,7 +21,7 @@ export type SessionSearchRetirement = { } export type SessionSearchRetirementArgs = { - store: SessionSearchStore + store: SessionSearchIndexStore /** Indexed paths absent from this pass’s discovery. */ paths: readonly string[] /** Longest containing root bounds each path’s proof walk. */ @@ -82,6 +84,9 @@ export async function retireDeletedSessionSearchSources( if (proof.verdict === 'gone') { store.removeFile(path) retirement.retired.push(path) + if (retirement.retired.length % REMOVALS_PER_FLUSH === 0) { + await store.flush?.() + } continue } if (proof.verdict === 'present') { diff --git a/src/main/ai-vault-search/session-search-engine-types.ts b/src/main/ai-vault-search/session-search-engine-types.ts index 166ed5b3764..14bc6c16cd1 100644 --- a/src/main/ai-vault-search/session-search-engine-types.ts +++ b/src/main/ai-vault-search/session-search-engine-types.ts @@ -8,7 +8,7 @@ export const SESSION_SEARCH_QUERY_MAX_LENGTH = 512 export const SESSION_SEARCH_SNIPPET_MARK_OPEN = '[[' export const SESSION_SEARCH_SNIPPET_MARK_CLOSE = ']]' -/** Conversation searches user/assistant columns; all also searches tools and identifier tokens. */ +/** Conversation searches prose; all also searches conversation identifier tokens. */ export type SessionSearchScope = 'conversation' | 'all' export type SessionSearchSort = 'relevance' | 'newest' diff --git a/src/main/ai-vault-search/session-search-engine.ts b/src/main/ai-vault-search/session-search-engine.ts index 0757466fcac..3dd603b5ced 100644 --- a/src/main/ai-vault-search/session-search-engine.ts +++ b/src/main/ai-vault-search/session-search-engine.ts @@ -16,7 +16,11 @@ import { type SessionSearchSourcePresence } from './session-search-engine-types' import { resolveSessionSearchLimit } from '../../shared/ai-vault/search/ai-vault-search-limit' -import { readIndexGeneration, readIndexIncarnation } from './session-search-index-generation' +import { + readIndexContentRevision, + readIndexGeneration, + readIndexIncarnation +} from './session-search-index-generation' import { rankSessionHits, type MessageRow, @@ -24,11 +28,16 @@ import { type SessionRow } from './session-search-hit-ranking' import { - SessionSearchCursorError, decodeSessionSearchCursor, encodeSessionSearchCursor, sessionSearchPageKey } from './session-search-page-cursor' +import { + SESSION_SEARCH_MAX_CANDIDATES, + decodeRankedSessionSearchCursor, + encodeRankedSessionSearchCursor, + type SessionSearchRankedCursor +} from './session-search-ranked-cursor' import { planSessionSearchQuery } from './session-search-query-planner' import { SessionSearchRetrieval, @@ -43,6 +52,8 @@ import { sessionSourcePresence } from './session-search-source-presence' /** Candidate-limit tuning and measurements: docs/reference/agent-session-search-query-tuning.md. */ export const SESSION_SEARCH_CANDIDATE_LIMIT_DEFAULT = 600 +type SnippetPlan = Pick + type RankedPage = { ranked: RankedSession[] retrieved: Retrieved | null @@ -56,7 +67,7 @@ export type SessionSearchEngineOptions = { retentionCutoffMs?: number | null } -/** Use independent statements to avoid pinning WAL; reject pages whose content generation changes. */ +/** One page reads one WAL snapshot; later page requests still validate their generation. */ export class SessionSearchEngine { private readonly retrieval: SessionSearchRetrieval private readonly candidateLimit: number @@ -65,7 +76,10 @@ export class SessionSearchEngine { private readonly db: SyncDatabase, private readonly options: SessionSearchEngineOptions = {} ) { - this.candidateLimit = options.sessionCandidateLimit ?? SESSION_SEARCH_CANDIDATE_LIMIT_DEFAULT + this.candidateLimit = Math.min( + SESSION_SEARCH_MAX_CANDIDATES, + options.sessionCandidateLimit ?? SESSION_SEARCH_CANDIDATE_LIMIT_DEFAULT + ) // Install tables and generation triggers before preparing retrieval or accepting writes. ensureSessionSearchQuerySchema(this.db) this.retrieval = new SessionSearchRetrieval(this.db) @@ -76,6 +90,18 @@ export class SessionSearchEngine { } search(request: SessionSearchRequest): SessionSearchResponse { + this.db.exec('BEGIN') + try { + const result = this.readPage(request) + this.db.exec('COMMIT') + return result + } catch (error) { + this.db.exec('ROLLBACK') + throw error + } + } + + private readPage(request: SessionSearchRequest): SessionSearchResponse { const startedAt = performance.now() ensureSessionSearchQuerySchema(this.db) const generation = readIndexGeneration(this.db) @@ -93,6 +119,23 @@ export class SessionSearchEngine { } // Reject invalid cursors before paying for retrieval. const pageKey = sessionSearchPageKey(request) + const frozen = request.cursor + ? decodeRankedSessionSearchCursor( + request.cursor, + generation, + readIndexContentRevision(this.db, generation), + pageKey, + incarnation + ) + : null + if (frozen) { + const limit = resolveSessionSearchLimit(request.limit) + const page = this.retrieval.hydrate( + frozen.hits.slice(frozen.o, frozen.o + limit), + retrievalScope + ) + return this.frozenPage(frozen, page, scope, limit, startedAt) + } const offset = request.cursor ? decodeSessionSearchCursor(request.cursor, generation, pageKey, incarnation) : 0 @@ -104,36 +147,75 @@ export class SessionSearchEngine { : this.text(plan, retrievalScope, sort) const limit = resolveSessionSearchLimit(request.limit) - const page = ranked.slice(offset, offset + limit) - const hits = this.hits(page, scope, retrieved) - const actualGeneration = readIndexGeneration(this.db) - if (actualGeneration !== generation) { - throw new SessionSearchCursorError('stale-generation', actualGeneration, generation) + const snapshot: SessionSearchRankedCursor = { + v: 2, + i: incarnation, + g: generation, + r: readIndexContentRevision(this.db, generation), + k: pageKey, + o: offset, + hits: ranked.map((entry) => ({ + s: entry.session.id, + m: entry.message?.rowid ?? null, + score: entry.score, + d: entry.duplicateCount + })), + retrieval: retrieved + ? { + plan: retrieved.plan, + route: retrieved.route, + ...(retrieved.repairedTerms ? { repairedTerms: retrieved.repairedTerms } : {}) + } + : null, + incomplete, + queryTruncated: capped.length < request.query.length || plan.truncated } - const hasMore = ranked.length > offset + limit - const response: SessionSearchResponse = { + return this.frozenPage( + snapshot, + ranked.slice(offset, offset + limit), + scope, + limit, + startedAt, + !!request.cursor + ) + } + + private frozenPage( + frozen: SessionSearchRankedCursor, + page: RankedSession[], + scope: SessionSearchScope, + limit: number, + startedAt: number, + legacy = false + ): SessionSearchResponse { + const hits = this.hits(page, scope, frozen.retrieval) + const next = frozen.o + limit + const hasMore = next < frozen.hits.length + return { hits, planner: { - route: retrieved?.route ?? 'or', + route: frozen.retrieval?.route ?? 'or', tier: scope, - ...(retrieved?.repairedTerms ? { repairedTerms: retrieved.repairedTerms } : {}) + ...(frozen.retrieval?.repairedTerms + ? { repairedTerms: frozen.retrieval.repairedTerms } + : {}) }, page: { hasMore, cursor: hasMore - ? encodeSessionSearchCursor(generation, offset + limit, pageKey, incarnation) + ? legacy + ? encodeSessionSearchCursor(frozen.g, next, frozen.k, frozen.i) + : encodeRankedSessionSearchCursor({ ...frozen, o: next }) : null }, truncated: { - // Retrieval alone can distinguish an exhausted scan budget from no matching rows. - candidates: incomplete, - snippets: hits.filter((hit) => hit.evidence?.snippetTruncated).length, - query: capped.length < request.query.length || plan.truncated + candidates: frozen.incomplete, + query: frozen.queryTruncated, + snippets: hits.filter((hit) => hit.evidence?.snippetTruncated).length }, - generation, + generation: frozen.g, durationMs: performance.now() - startedAt } - return response } /** Operator-only queries use newest order and the same fork folding as text queries. */ @@ -151,11 +233,22 @@ export class SessionSearchEngine { sort: 'relevance' | 'newest' ): RankedPage { const retrieved = this.retrieval.run(plan, scope) - const best = new Map(retrieved.rows.map((row) => [row.session_row_id, row])) + const best = new Map() + for (const row of retrieved.rows) { + const previous = best.get(row.session_row_id) + if ( + !previous || + row.score > previous.score || + (row.score === previous.score && row.rowid < previous.rowid) + ) { + best.set(row.session_row_id, row) + } + } + const ranked = rankSessionHits(retrieved.sessions, best, sort) return { - ranked: rankSessionHits(retrieved.sessions, best, sort), + ranked: ranked.slice(0, this.candidateLimit), retrieved, - incomplete: retrieved.incomplete + incomplete: retrieved.incomplete || ranked.length >= this.candidateLimit } } @@ -163,7 +256,7 @@ export class SessionSearchEngine { private hits( page: readonly RankedSession[], scope: SessionSearchScope, - retrieved: Retrieved | null + retrieved: SnippetPlan | null ): SessionSearchHit[] { const presence = sessionSourcePresence( this.db, @@ -175,7 +268,7 @@ export class SessionSearchEngine { private hit( entry: RankedSession, scope: SessionSearchScope, - retrieved: Retrieved | null, + retrieved: SnippetPlan | null, presence: ReadonlyMap ): SessionSearchHit { const { session, message } = entry diff --git a/src/main/ai-vault-search/session-search-file-write.test.ts b/src/main/ai-vault-search/session-search-file-write.test.ts index b0b69b3e883..3892dfdeeb9 100644 --- a/src/main/ai-vault-search/session-search-file-write.test.ts +++ b/src/main/ai-vault-search/session-search-file-write.test.ts @@ -71,7 +71,7 @@ function counts(db: SyncDatabase): Record { } } -it('files every row in one FTS table, under the column its role owns', () => { +it('indexes conversation rows and excludes tool output', () => { replayTranscriptRead({ messages: [ { role: 'user', text: 'alpha question', timestamp: null }, @@ -80,8 +80,8 @@ it('files every row in one FTS table, under the column its role owns', () => { ] }) - expect(counts(index.db).full).toBe(3) - expect(matches(index.db, 'messages_fts', 'gamma')).toBe(1) + expect(counts(index.db).full).toBe(2) + expect(matches(index.db, 'messages_fts', 'gamma')).toBe(0) expect(matches(index.db, 'messages_fts', '{user_text assistant_text}: gamma')).toBe(0) expect(matches(index.db, 'messages_fts', '{user_text assistant_text}: beta')).toBe(1) }) diff --git a/src/main/ai-vault-search/session-search-host-registration.test.ts b/src/main/ai-vault-search/session-search-host-registration.test.ts index 3e2ffbab840..a863e95d0cf 100644 --- a/src/main/ai-vault-search/session-search-host-registration.test.ts +++ b/src/main/ai-vault-search/session-search-host-registration.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { join } from 'node:path' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { resetSessionParseCacheForTests } from '../ai-vault/parse-cache/session-parse-cache-store' diff --git a/src/main/ai-vault-search/session-search-index-consumer.test.ts b/src/main/ai-vault-search/session-search-index-consumer.test.ts index 167a7c3e340..417e3422c9b 100644 --- a/src/main/ai-vault-search/session-search-index-consumer.test.ts +++ b/src/main/ai-vault-search/session-search-index-consumer.test.ts @@ -51,3 +51,24 @@ it('keeps the session list running when the index write fails', async () => { expect(errors.length).toBeGreaterThan(0) expect(owed()?.state).not.toBe('current') }) + +it('leaves a recoverable lagging prefix current when the list cursor has advanced', () => { + replayTranscriptRead({ + messages: userMessages('original', 1), + outcome: { byteOffset: 100, prefixHash: 'known-prefix' } + }) + replayTranscriptRead({ + mode: 'append', + previousByteOffset: 200, + messages: userMessages('lagging', 1), + outcome: { byteOffset: 300 } + }) + expect(owed()).toEqual({ state: 'current', fail_count: 0 }) + expect( + store.resumableCursor({ + agent: 'claude', + codexHome: null, + file: { path: SYNTHETIC_TRANSCRIPT, mtimeMs: 1740000000000, sizeBytes: 4096, modifiedAt: '' } + }) + ).toEqual({ byteOffset: 100, prefixHash: 'known-prefix' }) +}) diff --git a/src/main/ai-vault-search/session-search-index-consumer.ts b/src/main/ai-vault-search/session-search-index-consumer.ts index d7d18805ced..7e2001fc429 100644 --- a/src/main/ai-vault-search/session-search-index-consumer.ts +++ b/src/main/ai-vault-search/session-search-index-consumer.ts @@ -1,3 +1,6 @@ +import { inSessionParseFileLane } from '../ai-vault/parse-cache/session-parse-file-lane' +import { requestWholeTranscriptRead } from '../ai-vault/transcripts/session-transcript-reader' +import { traceSessionSearch } from './session-search-trace' import type { AiVaultAgent } from '../../shared/ai-vault/contracts/ai-vault-types' import { registerTranscriptConsumer, @@ -10,7 +13,7 @@ import { import type { SessionFileCandidate } from '../ai-vault/session-scanner-types' import { fileIdentity } from './session-search-file-cursor' import type { SessionSearchFileWrite } from './session-search-index-writer' -import type { SessionSearchResumableCursor, SessionSearchStore } from './session-search-store' +import type { SessionSearchResumableCursor, SessionSearchIndexStore } from './session-search-store' export const SELF_CONTAINED_TRANSCRIPT_AGENTS = { claude: true, @@ -40,9 +43,9 @@ export const SELF_CONTAINED_TRANSCRIPT_AGENTS = { kiro: false // A .jsonl sidecar supplies messages. } satisfies Record -/** Persist index progress independently: declined appends become due, failed reads retry against their stat. */ +/** Proven index gaps need whole reads; declined transfers leave committed coverage unchanged. */ export class SessionSearchIndexConsumer implements TranscriptConsumer { - constructor(private readonly store: SessionSearchStore) {} + constructor(private readonly store: SessionSearchIndexStore) {} resumableOffset(candidate: SessionFileCandidate): number | null { return this.store.resumableCursor(candidate)?.byteOffset ?? null @@ -50,6 +53,9 @@ export class SessionSearchIndexConsumer implements TranscriptConsumer { beginRead(start: TranscriptReadStart): TranscriptReadConsumer | null { const { candidate } = start + if (this.store.acceptingReads === false) { + return null + } // A fully covered self-contained transcript has nothing new to publish, even under another agent’s fold. if ( SELF_CONTAINED_TRANSCRIPT_AGENTS[candidate.agent] && @@ -61,6 +67,16 @@ export class SessionSearchIndexConsumer implements TranscriptConsumer { if (start.mode === 'append') { const cursor = this.store.indexedFile(candidate.file.path, fileIdentity(candidate.file)) if (!cursor || cursor.byteOffset !== start.previousByteOffset) { + const recoverable = this.store.resumableCursor(candidate) + if (recoverable && recoverable.byteOffset < start.previousByteOffset) { + if (start.indexing) { + // Why: invalidate after this lane saves its resume point, before the next read acquires it. + void inSessionParseFileLane(candidate.file.path, async () => + requestWholeTranscriptRead(candidate.file.path) + ).catch(() => undefined) + } + return null + } // A missing span or partial cursor requires a whole read; appending would preserve an index hole. this.store.setFileState(candidate.file.path, 'due') return null @@ -69,10 +85,23 @@ export class SessionSearchIndexConsumer implements TranscriptConsumer { // A restart dropped the parser's resume point; append past our cursor once the reread proves the prefix. const resume = start.mode === 'replace' ? this.provableCursor(start) : null const write = resume - ? this.store.beginWrite(candidate, 'append', resume.byteOffset, start.identity) - : this.store.beginWrite(candidate, start.mode, start.previousByteOffset, start.identity) + ? this.store.beginWrite( + candidate, + 'append', + resume.byteOffset, + start.identity, + start.indexing, + start.signal + ) + : this.store.beginWrite( + candidate, + start.mode, + start.previousByteOffset, + start.identity, + start.indexing, + start.signal + ) if (!write) { - this.store.setFileState(candidate.file.path, 'due') return null } return new SessionSearchReadConsumer(this.store, start, write, resume) @@ -92,12 +121,16 @@ class SessionSearchReadConsumer implements TranscriptReadConsumer { private prefixProven = false constructor( - private readonly store: SessionSearchStore, + private readonly store: SessionSearchIndexStore, private readonly start: TranscriptReadStart, private readonly write: SessionSearchFileWrite, private readonly resume: SessionSearchResumableCursor | null = null ) {} + async drain(): Promise { + await this.write.drain?.() + } + prefixReached(offset: number, digest: string): void { if (this.resume?.byteOffset === offset) { this.prefixProven = digest === this.resume.prefixHash @@ -120,7 +153,7 @@ class SessionSearchReadConsumer implements TranscriptReadConsumer { } finish(outcome: TranscriptReadOutcome): void { - if (outcome.cancelled) { + if (outcome.cancelled || this.write.cancelled?.() || this.write.failed?.()) { // Cancellation must not count toward the failure limit for a healthy file. this.write.discard() return @@ -129,7 +162,12 @@ class SessionSearchReadConsumer implements TranscriptReadConsumer { if (this.resume && !this.prefixProven && !outcome.incomplete) { // A rewritten or unreached prefix leaves stale rows; the next pass replaces them. this.write.discard() - this.store.setFileState(candidate.file.path, 'due') + traceSessionSearch('commit', () => this.store.setFileState(candidate.file.path, 'due'), { + append: 0, + replace: 0, + rescueRejections: 1, + rescueRejection: 'prefix-mismatch-or-unreached' + }) return } let committed = false @@ -150,6 +188,6 @@ class SessionSearchReadConsumer implements TranscriptReadConsumer { } } -export function registerSessionSearchIndexConsumer(store: SessionSearchStore): () => void { +export function registerSessionSearchIndexConsumer(store: SessionSearchIndexStore): () => void { return registerTranscriptConsumer(new SessionSearchIndexConsumer(store)) } diff --git a/src/main/ai-vault-search/session-search-index-generation.ts b/src/main/ai-vault-search/session-search-index-generation.ts index 63329f42def..ce08c96b903 100644 --- a/src/main/ai-vault-search/session-search-index-generation.ts +++ b/src/main/ai-vault-search/session-search-index-generation.ts @@ -2,6 +2,7 @@ import type SyncDatabase from '../sqlite/sync-database' const GENERATION_KEY = 'index_generation' const INCARNATION_KEY = 'index_incarnation' +const MAINTENANCE_KEY = 'index_maintenance_generation' export const SESSION_SEARCH_GENERATION_TRIGGERS = [ 'search_generation_file_insert', @@ -39,6 +40,25 @@ export function readIndexGeneration(db: SyncDatabase): number { return Number.isInteger(parsed) && parsed >= 0 ? parsed : 0 } +/** Invisible-row cleanup changes raw generation for legacy clients without invalidating frozen hits. */ +export function readIndexContentRevision( + db: SyncDatabase, + generation = readIndexGeneration(db) +): number { + const row = db.prepare('SELECT value FROM meta WHERE key = ?').get(MAINTENANCE_KEY) + const maintenance = Number(row?.value ?? 0) + return generation - (Number.isSafeInteger(maintenance) && maintenance >= 0 ? maintenance : 0) +} + +export function recordIndexMaintenance(db: SyncDatabase, before: number): void { + const delta = readIndexGeneration(db) - before + db.prepare(`INSERT INTO meta(key, value) VALUES (?, ?) + ON CONFLICT(key) DO UPDATE SET value = CAST(value AS INTEGER) + excluded.value`).run( + MAINTENANCE_KEY, + String(delta) + ) +} + export function readIndexIncarnation(db: SyncDatabase): string { const row: unknown = db.prepare('SELECT value FROM meta WHERE key = ?').get(INCARNATION_KEY) if (!row || typeof row !== 'object' || !('value' in row) || typeof row.value !== 'string') { diff --git a/src/main/ai-vault-search/session-search-index-pass.ts b/src/main/ai-vault-search/session-search-index-pass.ts index 5ba71a668ca..ac8840cb669 100644 --- a/src/main/ai-vault-search/session-search-index-pass.ts +++ b/src/main/ai-vault-search/session-search-index-pass.ts @@ -7,7 +7,7 @@ import { import type { SessionFileCandidate } from '../ai-vault/session-scanner-types' import { fileIdentity } from './session-search-file-cursor' import { sessionSearchReadDecision } from './session-search-read-decision' -import type { SessionSearchFileRow, SessionSearchStore } from './session-search-store' +import type { SessionSearchFileRow, SessionSearchIndexStore } from './session-search-store' export type SessionSearchIndexPassOptions = { signal?: AbortSignal @@ -19,7 +19,7 @@ export type SessionSearchIndexPassOptions = { /** Deferred candidates stay owed by their stored state; left counts backlog outside the due-row count. */ export async function runSessionSearchIndexPass( - store: SessionSearchStore, + store: SessionSearchIndexStore, candidates: readonly SessionFileCandidate[], options: SessionSearchIndexPassOptions ): Promise<{ stats: SessionParseStats; outOfTime: boolean; left: number }> { @@ -60,8 +60,10 @@ export async function runSessionSearchIndexPass( process.platform, stats, decision, - options.signal + options.signal, + true ) + await store.flush?.() } catch (error) { throwIfAiVaultScanCancelled(options.signal) console.warn( diff --git a/src/main/ai-vault-search/session-search-index-writer.test.ts b/src/main/ai-vault-search/session-search-index-writer.test.ts index c28c11d02a9..8fce10ff555 100644 --- a/src/main/ai-vault-search/session-search-index-writer.test.ts +++ b/src/main/ai-vault-search/session-search-index-writer.test.ts @@ -55,51 +55,6 @@ function indexRead(previousByteOffset: number, byteOffset: number, text: string) }) } -it('buffers capped tool rows without retaining their large parent strings', () => { - if (!('gc' in globalThis) || typeof globalThis.gc !== 'function') { - throw new Error('The test runner must enable --expose-gc') - } - const gc = globalThis.gc - const heapAfterGc = (): number => { - gc() - gc() - return process.memoryUsage().heapUsed - } - const write = store.beginWrite({ ...syntheticCandidate(), agent: 'codex' }, 'replace', 0) - if (!write) { - throw new Error('expected a write') - } - const prefix = (i: number): string => `retentionneedle ${i} 漢字 😀 ` - const rows = 70 - const before = heapAfterGc() - for (let i = 0; i < rows; i++) { - write.add({ - role: 'tool', - text: prefix(i) + 'x'.repeat(2 * 1024 * 1024), - timestamp: null - }) - } - // 210 Ki characters must not keep hundreds of MiB of discarded tool output alive. - expect(heapAfterGc() - before).toBeLessThan(16 * 1024 * 1024) - expect(count('messages')).toBe(0) - expect( - write.commit({ - session: syntheticSession({ agent: 'codex' }), - byteOffset: 140 * 1024 * 1024, - incomplete: false - }) - ).toBe(true) - expect(count('messages')).toBe(rows) - expect( - index.db - .prepare( - "SELECT tool_text FROM messages_fts WHERE messages_fts MATCH 'tool_text:retentionneedle' ORDER BY rowid" - ) - .all() - ).toEqual(Array.from({ length: rows }, (_, i) => ({ tool_text: prefix(i).padEnd(3072, 'x') }))) - expect(errors).toEqual([]) -}) - it('refuses an append whose predecessor offset is not the committed cursor', () => { expect(indexRead(0, 100, 'first')).toBe(true) diff --git a/src/main/ai-vault-search/session-search-index-writer.ts b/src/main/ai-vault-search/session-search-index-writer.ts index cc2ef85cea1..228e57af9ba 100644 --- a/src/main/ai-vault-search/session-search-index-writer.ts +++ b/src/main/ai-vault-search/session-search-index-writer.ts @@ -1,3 +1,4 @@ +import { traceSessionSearch } from './session-search-trace' import type SyncDatabase from '../sqlite/sync-database' import type { SessionFileCandidate } from '../ai-vault/session-scanner-types' import type { @@ -5,8 +6,12 @@ import type { TranscriptReadOutcome, TranscriptSessionIdentity } from '../ai-vault/transcripts/session-transcript-consumers' -import { SessionSearchWriteBuffer } from './session-search-write-buffer' -import { EMPTY_CONTENT_HASH, foldContentHash } from './session-search-content-hash' +import { SessionSearchWriteBuffer, SessionSearchStagingBudget } from './session-search-write-buffer' +import { + EMPTY_CONTENT_HASH, + foldContentHash, + type SessionContentHash +} from './session-search-content-hash' import type { SessionSearchFileIdentity, SessionSearchIndexedFile @@ -38,6 +43,10 @@ type FileCursor = Pick export type SessionSearchFileWrite = { /** Commit named chunks at the ceiling; spill anonymous reads privately until atomic publication. */ add(message: TranscriptMessage): void + failed?(): boolean + cancelled?(): boolean + drain?(): Promise + addPrepared?(rows: Iterable, hash: SessionContentHash): void /** Atomically publish final rows, metadata, and cursor; return false if the source changed meanwhile. */ commit(outcome: TranscriptReadOutcome): boolean /** Ends an incomplete or failed read without publishing its buffered rows. */ @@ -48,13 +57,15 @@ export class SessionSearchIndexWriter { private readonly records: SessionSearchFileRecords private readonly activeWrites = new Map() private readonly pendingBuffers = new Set() + private readonly staging = new SessionSearchStagingBudget() private closed = false constructor( private readonly db: SyncDatabase, private readonly commitChars: number = SESSION_SEARCH_COMMIT_CHARS, /** Schedule bounded orphan reclamation after commit; synchronous cleanup would enlarge the transaction. */ - private readonly onOrphanedRows: () => void = () => undefined + private readonly onOrphanedRows: () => void = () => undefined, + private readonly onMessagesChanged: (delta: number) => void = () => undefined ) { this.records = new SessionSearchFileRecords(db) } @@ -90,7 +101,9 @@ export class SessionSearchIndexWriter { candidate: SessionFileCandidate, mode: 'replace' | 'append', previousByteOffset: number, - identity?: () => TranscriptSessionIdentity | null + identity?: () => TranscriptSessionIdentity | null, + _indexing?: boolean, + _signal?: AbortSignal ): SessionSearchFileWrite | null { if (this.closed) { return null @@ -119,9 +132,10 @@ export class SessionSearchIndexWriter { const cursor = this.cursor(path) this.db.exec('BEGIN IMMEDIATE') try { - this.dropSession(cursor?.session_row_id ?? null) + const removed = this.dropSession(cursor?.session_row_id ?? null) this.db.prepare('DELETE FROM files WHERE path = ?').run(path) this.db.exec('COMMIT') + this.onMessagesChanged(-removed) } catch (error) { this.db.exec('ROLLBACK') throw error @@ -131,7 +145,7 @@ export class SessionSearchIndexWriter { close(): void { this.closed = true for (const buffer of this.pendingBuffers) { - buffer.clear() + buffer.release() } this.pendingBuffers.clear() for (const active of this.activeWrites.values()) { @@ -154,7 +168,7 @@ export class SessionSearchIndexWriter { ): SessionSearchFileWrite { const db = this.db const path = candidate.file.path - const buffer = new SessionSearchWriteBuffer(this.commitChars) + const buffer = new SessionSearchWriteBuffer(this.commitChars, this.staging) this.pendingBuffers.add(buffer) // Recheck inside each transaction to prevent stale chunks resurrecting deleted or superseded rows. let expected = opened @@ -169,7 +183,7 @@ export class SessionSearchIndexWriter { return } released = true - buffer.clear() + buffer.release() this.pendingBuffers.delete(buffer) lifetime.readers-- if (lifetime.readers === 0 && this.activeWrites.get(path) === lifetime) { @@ -197,90 +211,101 @@ export class SessionSearchIndexWriter { const write = ( outcome: TranscriptReadOutcome | null, named: TranscriptSessionIdentity | null - ): boolean => { - const decoded = outcome?.session ?? null - db.exec('BEGIN IMMEDIATE') - try { - if (!current()) { - db.exec('ROLLBACK') - return false - } - if (outcome && !decoded) { - // Advance the cursor even without a decoded session to avoid rereading the same empty content. - this.dropSession(session) - session = null - this.records.upsertFile(candidate, outcome.byteOffset, null, outcome.prefixHash) - } else { - if (replaced) { - session ??= this.records.createSessionRow(candidate) - } else { - const previous = session - session = this.records.createSessionRow(candidate) - if (previous !== null) { - db.prepare('DELETE FROM sessions WHERE id = ?').run(previous) - orphaned = true + ): boolean => + traceSessionSearch( + append ? 'commit' : 'replace', + () => { + const decoded = outcome?.session ?? null + db.exec('BEGIN IMMEDIATE') + let delta = 0 + try { + if (!current()) { + db.exec('ROLLBACK') + return false } - replaced = true + if (outcome && !decoded) { + // Advance the cursor even without a decoded session to avoid rereading the same empty content. + delta -= this.dropSession(session) + session = null + this.records.upsertFile(candidate, outcome.byteOffset, null, outcome.prefixHash) + } else { + if (replaced) { + session ??= this.records.createSessionRow(candidate) + } else { + const previous = session + session = this.records.createSessionRow(candidate) + if (previous !== null) { + db.prepare('DELETE FROM sessions WHERE id = ?').run(previous) + orphaned = true + } + replaced = true + } + for (const row of buffer.rows()) { + insertSearchMessage(db, session, row) + delta++ + } + if (decoded) { + this.records.updateSession(decoded, session, hash) + } else if (named) { + // Committed chunks need searchable identity even if the read never finishes. + this.records.updateProvisionalSession(session, named, candidate) + } + this.records.upsertFile( + candidate, + outcome ? outcome.byteOffset : PARTIAL_FILE_CURSOR, + session, + outcome?.prefixHash + ) + } + db.exec('COMMIT') + this.onMessagesChanged(delta) + } catch (error) { + db.exec('ROLLBACK') + throw error } - for (const row of buffer.rows()) { - insertSearchMessage(db, session, row) + // Schedule reclamation only after commit; rolled-back generations remain live. + if (orphaned) { + orphaned = false + this.onOrphanedRows() } - if (decoded) { - this.records.updateSession(decoded, session, hash) - } else if (named) { - // Committed chunks need searchable identity even if the read never finishes. - this.records.updateProvisionalSession(session, named, candidate) + expected = { + session_row_id: session, + byte_offset: outcome ? outcome.byteOffset : PARTIAL_FILE_CURSOR } - this.records.upsertFile( - candidate, - outcome ? outcome.byteOffset : PARTIAL_FILE_CURSOR, - session, - outcome?.prefixHash - ) - } - db.exec('COMMIT') - } catch (error) { - db.exec('ROLLBACK') - throw error - } - // Schedule reclamation only after commit; rolled-back generations remain live. - if (orphaned) { - orphaned = false - this.onOrphanedRows() - } - expected = { - session_row_id: session, - byte_offset: outcome ? outcome.byteOffset : PARTIAL_FILE_CURSOR - } - buffer.clear() - return true - } + buffer.clear() + return true + }, + { append: append ? 1 : 0, replace: append ? 0 : 1, rescueRejections: 0 } + ) - return { - add: (message) => { - if (released || fenced || this.closed) { + const stage = (rows: Iterable): void => { + if (released || fenced || this.closed) { + discard() + return + } + for (const row of rows) { + buffer.add(row) + if (!buffer.ready) { + continue + } + const named = identity?.() ?? null + if (!named && buffer.spillReady) { + buffer.spill() + } else if (named && !write(null, named)) { + fenced = true discard() return } + } + } + return { + add: (message) => { hash = foldContentHash(hash, [message]) - // Split before checking the ceiling so one huge message cannot force an oversized named chunk. - for (const row of searchMessageRows([message])) { - buffer.add(row) - if (!buffer.ready && (!identity || !buffer.hasSpilled)) { - continue - } - const named = identity?.() ?? null - if (!buffer.ready && !named) { - continue - } - if (!named) { - buffer.spill() - } else if (!write(null, named)) { - fenced = true - discard() - return - } - } + stage(searchMessageRows([message])) + }, + addPrepared: (rows, preparedHash) => { + hash = preparedHash + stage(rows) }, commit: (outcome) => { try { @@ -293,11 +318,12 @@ export class SessionSearchIndexWriter { } } - private dropSession(sessionRowId: number | null): void { + private dropSession(sessionRowId: number | null): number { if (sessionRowId === null) { - return + return 0 } - deleteSearchMessages(this.db, sessionRowId) + const removed = deleteSearchMessages(this.db, sessionRowId) this.db.prepare('DELETE FROM sessions WHERE id = ?').run(sessionRowId) + return removed } } diff --git a/src/main/ai-vault-search/session-search-indexer-test-fixture.ts b/src/main/ai-vault-search/session-search-indexer-test-fixture.ts index 013e1d1e97c..2753651ddb7 100644 --- a/src/main/ai-vault-search/session-search-indexer-test-fixture.ts +++ b/src/main/ai-vault-search/session-search-indexer-test-fixture.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { mkdir, mkdtemp, rename, rm, stat, utimes, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { dirname, join } from 'node:path' diff --git a/src/main/ai-vault-search/session-search-indexer.test.ts b/src/main/ai-vault-search/session-search-indexer.test.ts index f16b38bb6a8..aacbb8a6a75 100644 --- a/src/main/ai-vault-search/session-search-indexer.test.ts +++ b/src/main/ai-vault-search/session-search-indexer.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { mkdirSync, rmSync } from 'node:fs' import { appendFile, chmod, mkdir, rm, stat, utimes } from 'node:fs/promises' import { join } from 'node:path' @@ -39,7 +40,7 @@ beforeEach(async () => { }) afterEach(async () => { - indexer?.close() + await indexer?.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() await harness.cleanup() @@ -203,11 +204,11 @@ it('widens history by constructing a new instance and narrows by purging on its expect(sessionsMatching('recent')).toEqual([SESSION_ID]) expect(sessionsMatching('ancient')).toEqual([]) - indexer?.close() + await indexer?.close() await newIndexer({ historyDays: null, recentPerAgent: 1 }).start() expect(sessionsMatching('ancient')).toEqual([OTHER_SESSION_ID]) - indexer?.close() + await indexer?.close() await newIndexer({ historyDays: 30, recentPerAgent: 1 }).start() expect(sessionsMatching('ancient')).toEqual([]) expect(sessionsMatching('recent')).toEqual([SESSION_ID]) @@ -288,13 +289,13 @@ it.skipIf(!CAN_DENY_READ)('stops re-reading a transcript it cannot read', async } }) -it('releases the database path when the open itself throws', () => { +it('releases the database path when the open itself throws', async () => { // A directory at the database path makes opening fail without leaving an ownership claim. mkdirSync(harness.databasePath, { recursive: true }) - expect(() => newIndexer()).toThrow() + await expect(newIndexer().initialize()).rejects.toThrow() rmSync(harness.databasePath, { recursive: true, force: true }) - expect(() => newIndexer()).not.toThrow() + await expect(newIndexer().initialize()).resolves.toBeUndefined() }) it('re-reads a file a chunked read left half written, and settles it in one pass', async () => { @@ -303,7 +304,7 @@ it('re-reads a file a chunked read left half written, and settles it in one pass await newIndexer().start() const whole = (await stat(path)).size expect(indexedByteOffset(path)).toBe(whole) - indexer?.close() + await indexer?.close() plantPartialCursor(path) await newIndexer().start() @@ -311,7 +312,7 @@ it('re-reads a file a chunked read left half written, and settles it in one pass expect(indexedByteOffset(path)).toBe(whole) expect(fileState(path)).toBe('current') expect(indexer?.status().phase).toBe('current') - indexer?.close() + await indexer?.close() // Growing a partially indexed file must force a whole read rather than offer a declined append. plantPartialCursor(path) @@ -351,7 +352,7 @@ it('reads a declined file at its current stat, not the one it was recorded with' await newIndexer().start() // Retain the session-list cursor while deleting the index to force an append the consumer must decline. - indexer?.close() + await indexer?.close() removeSessionSearchDatabase(harness.databasePath) newIndexer() await appendFile(path, `${claudeLines(['declined turn'], SESSION_ID, 10).join('\n')}\n`) @@ -391,7 +392,7 @@ it.skipIf(!CAN_DENY_READ)( it('keeps a root that is gone at the first sweep after a restart', async () => { await writeClaudeTranscript(transcriptPath(), ['a session on a removable volume'], SESSION_ID) await newIndexer().start() - indexer?.close() + await indexer?.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() @@ -429,7 +430,7 @@ it('keeps rows under no configured root, and retires them only when gone', async const moved = transcriptPath() await writeClaudeTranscript(moved, ['a session in the old profile'], SESSION_ID) await newIndexer().start() - indexer?.close() + await indexer?.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() @@ -448,7 +449,7 @@ it('stops everything on close, including work already queued', async () => { await newIndexer().start() const queued = indexer?.reconcile({ full: true }) - indexer?.close() + await indexer?.close() await queued expect(errors).toEqual([]) @@ -492,7 +493,7 @@ it.skipIf(!CAN_DENY_READ)('retires a deleted file behind a block of unreadable r await writeClaudeTranscript(transcriptPath(), ['a live conversation'], SESSION_ID) const locked = join(harness.roots.claudeProjectsDir ?? '', 'locked') await mkdir(locked, { recursive: true }) - newIndexer() + await newIndexer().initialize() // Put 520 unreadable rows ahead of the deleted row to catch limits counted by rows instead of directories. harness.write((db: SyncDatabase) => { @@ -519,3 +520,30 @@ it.skipIf(!CAN_DENY_READ)('retires a deleted file behind a block of unreadable r await chmod(locked, 0o700) } }) + +it('holds out a repeatedly rejected write while other files continue indexing', async () => { + const broken = transcriptPath() + await writeClaudeTranscript(broken, ['rejectedneedle'], SESSION_ID) + await writeClaudeTranscript(transcriptPath(OTHER_SESSION_ID), ['healthyneedle'], OTHER_SESSION_ID) + const subject = newIndexer() + await subject.initialize() + harness.write((db) => + db.exec(`CREATE TRIGGER reject_one_file BEFORE INSERT ON messages + WHEN (SELECT file_path FROM sessions WHERE id=NEW.session_row_id) LIKE '%${SESSION_ID}%' + BEGIN SELECT RAISE(ABORT, 'rejected fixture write'); END`) + ) + await subject.start() + for (let pass = 0; pass < 3; pass++) { + await subject.reconcile({ full: true }) + } + expect(rowFor(broken)).toEqual({ state: 'failed', failCount: 3 }) + expect(sessionsMatching('healthyneedle')).toEqual([OTHER_SESSION_ID]) + expect(sessionsMatching('rejectedneedle')).toEqual([]) + await appendFile( + transcriptPath(OTHER_SESSION_ID), + `${claudeLines(['continuedneedle'], OTHER_SESSION_ID, 10).join('\n')}\n` + ) + await subject.reconcile({ full: true }) + expect(sessionsMatching('continuedneedle')).toEqual([OTHER_SESSION_ID]) + expect(rowFor(broken)).toEqual({ state: 'failed', failCount: 3 }) +}) diff --git a/src/main/ai-vault-search/session-search-indexer.ts b/src/main/ai-vault-search/session-search-indexer.ts index 0ef64e691ca..7cef202dd5e 100644 --- a/src/main/ai-vault-search/session-search-indexer.ts +++ b/src/main/ai-vault-search/session-search-indexer.ts @@ -10,7 +10,8 @@ import { SessionSearchDirectoryListings } from './session-search-directory-listi import { registerSessionSearchIndexConsumer } from './session-search-index-consumer' import { runSessionSearchPass } from './session-search-pass' import { sessionSearchHistoryCutoffMs } from './session-search-retention-policy' -import { SessionSearchStore, type SessionSearchStateCounts } from './session-search-store' +import type { SessionSearchStateCounts } from './session-search-store' +import { SessionSearchWorkerStore } from './session-search-worker-store' import type { SessionSearchDegradedRoot } from './session-search-degraded-roots' import { SessionSearchWorkLoop } from './session-search-work-loop' @@ -47,7 +48,7 @@ export class SessionSearchIndexer { private readonly onError: (error: unknown) => void private readonly loop: SessionSearchWorkLoop - private readonly store: SessionSearchStore + private readonly store: SessionSearchWorkerStore private readonly unregister: () => void /** Null means unobserved; an empty set records a real pass. */ private previousRootsWithFiles: ReadonlySet | null = null @@ -87,8 +88,10 @@ export class SessionSearchIndexer { `SessionSearchIndexer: ${options.databasePath} already has a live indexer; close it first` ) } - // Claim ownership only after opening succeeds; a failed constructor cannot release a claim. - this.store = new SessionSearchStore(options.databasePath, onError) + // Claim ownership before async initialization so another indexer cannot race the writer open. + this.store = new SessionSearchWorkerStore(options.databasePath, onError, () => { + this.sweepNext = true + }) liveIndexerPaths.add(this.ownershipPath) this.store.setRetentionCutoffMs(this.cutoffMs()) this.unregister = registerSessionSearchIndexConsumer(this.store) @@ -116,6 +119,10 @@ export class SessionSearchIndexer { return this.tick() } + generation(): number { + return this.store.generation + } + status(): SessionSearchIndexStatus { // Use cached counts after close to avoid reading a shut database. const settled = (this.closed ? this.lastCounts : this.readCounts()) ?? { @@ -138,9 +145,9 @@ export class SessionSearchIndexer { } } - close(): void { + close(): Promise { if (this.closed) { - return + return this.store.retired() } // Preserve final counts for status calls after the store closes. this.lastCounts = this.readCounts() ?? this.lastCounts @@ -150,10 +157,24 @@ export class SessionSearchIndexer { this.unregister() this.store.close() liveIndexerPaths.delete(this.ownershipPath) + return this.store.retired() + } + + async initialize(): Promise { + try { + await this.store.prepare() + } catch (error) { + await this.close() + throw error + } + } + + retired(): Promise { + return this.store.retired() } settled(): Promise { - return this.loop.settled + return this.loop.settled.then(() => this.store.flush()) } private readCounts(): SessionSearchStateCounts | null { diff --git a/src/main/ai-vault-search/session-search-instance.test.ts b/src/main/ai-vault-search/session-search-instance.test.ts index aae1088fb81..b55320d7f63 100644 --- a/src/main/ai-vault-search/session-search-instance.test.ts +++ b/src/main/ai-vault-search/session-search-instance.test.ts @@ -1,10 +1,15 @@ +import './session-search-worker.test-fixture' import { existsSync } from 'node:fs' -import { utimes } from 'node:fs/promises' +import { unlink, utimes } from 'node:fs/promises' +import * as fileRemoval from '../../shared/windows-transient-lock-removal' import { join } from 'node:path' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { resetSessionParseCacheForTests } from '../ai-vault/parse-cache/session-parse-cache-store' import { resetTranscriptConsumersForTests } from '../ai-vault/transcripts/session-transcript-consumers' +import * as searchSchema from './session-search-schema' +import { SessionSearchReaders } from './session-search-readers' import { SessionSearchInstance } from './session-search-instance' +import { SessionSearchIndexer } from './session-search-indexer' import { openSessionSearchIndexerHarness, writeClaudeTranscript, @@ -28,7 +33,7 @@ beforeEach(async () => { afterEach(async () => { vi.restoreAllMocks() - instance?.close() + await instance?.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() await harness.cleanup() @@ -71,7 +76,7 @@ it('constructs nothing and touches no disk while the setting is off', async () = kind: 'unavailable', reason: 'disabled' }) - expect(subject.status()).toMatchObject({ enabled: false, phase: 'idle', generation: 0 }) + expect(await subject.status()).toMatchObject({ enabled: false, phase: 'idle', generation: 0 }) expect(errors).toEqual([]) }) @@ -110,19 +115,74 @@ it('removes the database on clear and rebuilds only while consent stands', async await subject.settled() expect(await searchFor('distinctive')).toEqual([RECENT_SESSION_ID]) - subject.clear() + await subject.clear() expect(subject.running).toBe(true) expect(existsSync(harness.databasePath)).toBe(true) await subject.settled() expect(await searchFor('distinctive')).toEqual([RECENT_SESSION_ID]) subject.apply({ enabled: false, historyDays: null }) - subject.clear() + await subject.clear() expect(subject.running).toBe(false) expect(existsSync(harness.databasePath)).toBe(false) expect(errors).toEqual([]) }) +it('never reports the deleted index while re-enabling a cleared search', async () => { + for (const id of [RECENT_SESSION_ID, ANCIENT_SESSION_ID]) { + await writeClaudeTranscript(transcriptPath(id), ['searchable conversation'], id) + } + const subject = newInstance() + const initialEnable = subject.apply({ enabled: true, historyDays: null }) + expect(await subject.status()).toMatchObject({ + enabled: true, + phase: 'indexing', + filesIndexed: 0, + messagesIndexed: 0 + }) + await initialEnable + await subject.settled() + expect(await subject.status()).toMatchObject({ filesIndexed: 2, messagesIndexed: 4 }) + + await subject.apply({ enabled: false, historyDays: null }) + await subject.clear() + expect(existsSync(harness.databasePath)).toBe(false) + await unlink(transcriptPath(ANCIENT_SESSION_ID)) + + let release!: () => void + let opening!: () => void + const blocked = new Promise((resolve) => { + release = resolve + }) + const started = new Promise((resolve) => { + opening = resolve + }) + const initialize = SessionSearchIndexer.prototype.initialize + vi.spyOn(SessionSearchIndexer.prototype, 'initialize').mockImplementationOnce(async function ( + this: SessionSearchIndexer + ) { + opening() + await blocked + await initialize.call(this) + }) + const enabling = subject.apply({ enabled: true, historyDays: null }) + try { + await started + expect(await subject.status()).toMatchObject({ + enabled: true, + phase: 'indexing', + filesIndexed: 0, + messagesIndexed: 0 + }) + } finally { + release() + await enabling + } + await subject.settled() + expect(await subject.status()).toMatchObject({ filesIndexed: 1, messagesIndexed: 2 }) + expect(errors).toEqual([]) +}) + it('refuses a page cursor minted before clear even when the rebuilt generation matches', async () => { for (const id of [RECENT_SESSION_ID, ANCIENT_SESSION_ID]) { await writeClaudeTranscript(transcriptPath(id), [`shared clear fence ${id}`], id) @@ -135,9 +195,9 @@ it('refuses a page cursor minted before clear even when the rebuilt generation m throw new Error('expected a paged result') } - subject.clear() + await subject.clear() await subject.settled() - expect(subject.status().generation).toBe(first.generation) + expect((await subject.status()).generation).toBe(first.generation) expect( await subject.search({ query: 'shared clear fence', limit: 1, cursor: first.page.cursor }) ).toMatchObject({ kind: 'stale-cursor', generation: first.generation }) @@ -170,3 +230,48 @@ it('keeps pagination stable when the clock crosses retention before a purge', as expect(second.hits[0].sessionId).not.toBe(first.hits[0].sessionId) expect(errors).toEqual([]) }) + +it('propagates query failures while status remains available without a reader', async () => { + const subject = newInstance() + await subject.apply({ enabled: true, historyDays: null }) + await subject.settled() + const before = await subject.status() + vi.spyOn(SessionSearchReaders.prototype, 'query').mockRejectedValue(new Error('reader failed')) + await expect(subject.search({ query: 'needle' })).rejects.toThrow('reader failed') + expect(await subject.status()).toEqual(before) +}) + +it('rejects a failed clear and keeps the transition chain usable for another clear', async () => { + const subject = newInstance() + const remove = vi + .spyOn(searchSchema, 'removeSessionSearchDatabaseAsync') + .mockRejectedValueOnce(new Error('unlink denied')) + await expect(subject.clear()).rejects.toThrow('unlink denied') + await expect(subject.clear()).resolves.toBeUndefined() + expect(remove).toHaveBeenCalledTimes(2) +}) + +it('keeps serving while an asynchronous clear deletion outlives its deadline', async () => { + const subject = newInstance() + let reject!: (error: Error) => void + const blocked = new Promise((_resolve, fail) => { + reject = fail + }) + const remove = vi.spyOn(fileRemoval, 'removeTree').mockReturnValue(blocked) + vi.useFakeTimers() + try { + const clear = expect(subject.clear()).rejects.toThrow('Could not complete') + await vi.advanceTimersByTimeAsync(0) + expect(remove).toHaveBeenCalled() + expect(await subject.status()).toMatchObject({ enabled: false }) + await vi.advanceTimersByTimeAsync(10_000) + await clear + expect(await subject.search({ query: 'responsive' })).toMatchObject({ reason: 'disabled' }) + reject(new Error('file remains locked')) + await subject.close() + expect(errors).toContainEqual(new Error('file remains locked')) + } finally { + vi.useRealTimers() + remove.mockRestore() + } +}) diff --git a/src/main/ai-vault-search/session-search-instance.ts b/src/main/ai-vault-search/session-search-instance.ts index 36db3025b6f..af3242e1c91 100644 --- a/src/main/ai-vault-search/session-search-instance.ts +++ b/src/main/ai-vault-search/session-search-instance.ts @@ -1,4 +1,4 @@ -import type SyncDatabase from '../sqlite/sync-database' +import { withinSessionSearchDeadline } from './session-search-request-deadline' import type { AiVaultSearchRequest, AiVaultSearchResponse, @@ -6,17 +6,13 @@ import type { } from '../../shared/ai-vault/search/ai-vault-search-types' import { unavailableSessionSearchStatus } from '../../shared/ai-vault/search/ai-vault-search-client' import type { AiVaultSearchSettings } from '../../shared/ai-vault/search/ai-vault-search-settings' -import { SessionSearchEngine } from './session-search-engine' import { SessionSearchIndexer } from './session-search-indexer' +import { SessionSearchReaders } from './session-search-readers' import { sessionSearchHistoryCutoffMs } from './session-search-retention-policy' -import { openSessionSearchDatabase, removeSessionSearchDatabase } from './session-search-schema' +import { removeSessionSearchDatabaseAsync } from './session-search-schema' import type { SessionSearchScanRoots } from './session-search-scan-roots' import type { SessionSearchIndexerOptions } from './session-search-indexer-options' -import { - createSessionSearchService, - type SessionSearchHostScope, - type SessionSearchService -} from './session-search-service' +import type { SessionSearchHostScope } from './session-search-service' export type SessionSearchInstanceOptions = { databasePath: string @@ -25,128 +21,127 @@ export type SessionSearchInstanceOptions = { onError?: (error: unknown) => void reconcileIntervalMs?: number } +type LiveIndex = { indexer: SessionSearchIndexer; readers: SessionSearchReaders } -type LiveIndex = { - indexer: SessionSearchIndexer - engine: SessionSearchEngine - /** Reader connection; the indexer owns a separate writer connection. */ - db: SyncDatabase - service: SessionSearchService -} - -/** Replace the indexer and reader together when settings change, closing the old pair first. */ +/** Replace the writer and readers together, awaiting their exits before reopening or unlinking. */ export class SessionSearchInstance { private live: LiveIndex | null = null private settings: AiVaultSearchSettings = { enabled: false, historyDays: null } + private transition: Promise | null = null private readonly onError: (error: unknown) => void - constructor(private readonly options: SessionSearchInstanceOptions) { this.onError = options.onError ?? ((error) => console.warn('[ai-vault-search]', error)) } - get running(): boolean { return this.live !== null } - - /** Even unchanged settings restart the indexer. */ - apply(next: AiVaultSearchSettings): void { + apply(next: AiVaultSearchSettings): Promise { this.settings = next - this.closeLive() - this.construct() + return this.queue(next, false).catch(() => undefined) } - - clear(): void { - this.closeLive() - removeSessionSearchDatabase(this.options.databasePath) - this.construct() + clear(signal?: AbortSignal): Promise { + return withinSessionSearchDeadline(this.queue(this.settings, true), signal) } - - close(): void { - this.closeLive() + close(): Promise { + return this.trackTransition((this.transition ?? Promise.resolve()).then(() => this.closeLive())) + } + private trackTransition(operation: Promise): Promise { + const pending = operation.catch(this.onError).finally(() => { + if (this.transition === pending) { + this.transition = null + } + }) + this.transition = pending + return pending + } + private queue(settings: AiVaultSearchSettings, clear: boolean): Promise { + const old = this.live + this.live = null + old?.indexer.close() + const retired = old + ? Promise.all([old.indexer.retired(), old.readers.close()]) + : Promise.resolve() + const operation = (this.transition ?? Promise.resolve()).then(async () => { + await retired + await this.closeLive() + if (clear) { + await removeSessionSearchDatabaseAsync(this.options.databasePath) + } + if (!settings.enabled) { + return + } + const indexer = new SessionSearchIndexer({ + ...this.options, + historyDays: settings.historyDays + }) + try { + await indexer.initialize() + } catch (error) { + indexer.close() + await indexer.retired() + throw error + } + const readers = new SessionSearchReaders( + this.options.databasePath, + sessionSearchHistoryCutoffMs(settings.historyDays, Date.now()) + ) + this.live = { indexer, readers } + void indexer.start().catch(this.onError) + }) + this.trackTransition(operation) + return operation } - async search( request: AiVaultSearchRequest, - hostScope?: SessionSearchHostScope + scope?: SessionSearchHostScope, + signal?: AbortSignal ): Promise { const live = this.live - // Report disabled or not-ready before checking scope so the client can offer the right recovery. if (!live) { return { kind: 'unavailable', reason: this.settings.enabled ? 'not-ready' : 'disabled' } } - return live.service.search(request, hostScope) + const response = await live.readers.query(request, scope, signal) + if (!response.result) { + throw new Error('Could not search sessions') + } + return response.result } - - status(): AiVaultSearchStatus { + async status(): Promise { const live = this.live - if (!live) { - return { ...unavailableSessionSearchStatus(), enabled: this.settings.enabled } + if (!this.settings.enabled || !live) { + return { + ...unavailableSessionSearchStatus(), + enabled: this.settings.enabled, + ...(this.settings.enabled && this.transition + ? { phase: 'indexing', messagesIndexed: 0 } + : {}) + } } return { enabled: true, ...live.indexer.status(), - generation: live.engine.generation() + generation: live.indexer.generation() } } - - async reconcile(): Promise { - await this.live?.service.reconcile() + reconcile(signal?: AbortSignal): Promise { + return withinSessionSearchDeadline( + (this.transition ?? Promise.resolve()).then(() => + this.live?.indexer.reconcile({ full: true }) + ), + signal + ) } - - settled(): Promise { - return this.live?.indexer.settled() ?? Promise.resolve() + async settled(): Promise { + await this.transition + await this.live?.indexer.settled() } - - private construct(): void { - if (!this.settings.enabled) { - return - } - const { historyDays } = this.settings - let indexer: SessionSearchIndexer | null = null - let db: SyncDatabase | null = null - try { - indexer = new SessionSearchIndexer({ - databasePath: this.options.databasePath, - roots: this.options.roots, - resolveRoots: this.options.resolveRoots, - historyDays, - onError: this.onError, - ...(this.options.reconcileIntervalMs === undefined - ? {} - : { reconcileIntervalMs: this.options.reconcileIntervalMs }) - }) - db = openSessionSearchDatabase(this.options.databasePath) - // Keep pagination stable until an indexer purge expires rows and invalidates cursors. - const engineOptions = { - retentionCutoffMs: sessionSearchHistoryCutoffMs(historyDays, Date.now()) - } - const engine = new SessionSearchEngine(db, engineOptions) - this.live = { - indexer, - engine, - db, - service: createSessionSearchService({ engine, indexer }) - } - void indexer.start().catch(this.onError) - } catch (error) { - // Release the writer’s database-path claim if reader construction fails. - db?.close() - indexer?.close() - this.live = null - this.onError(error) - } - } - - private closeLive(): void { + private async closeLive(): Promise { const live = this.live this.live = null if (!live) { return } - try { - live.indexer.close() - } finally { - live.db.close() - } + live.indexer.close() + await Promise.all([live.indexer.retired(), live.readers.close()]) } } diff --git a/src/main/ai-vault-search/session-search-lag-recovery.test.ts b/src/main/ai-vault-search/session-search-lag-recovery.test.ts new file mode 100644 index 00000000000..55559668c49 --- /dev/null +++ b/src/main/ai-vault-search/session-search-lag-recovery.test.ts @@ -0,0 +1,53 @@ +import { appendFile, writeFile } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { expect, it } from 'vitest' +import { resetSessionParseCacheForTests } from '../ai-vault/parse-cache/session-parse-cache-store' +import { resetTranscriptConsumersForTests } from '../ai-vault/transcripts/session-transcript-consumers' +import { registerSessionSearchIndexConsumer } from './session-search-index-consumer' +import { runSessionSearchIndexPass } from './session-search-index-pass' +import { openSessionSearchIndexFile } from './session-search-index-test-fixture' +import { SessionSearchStore } from './session-search-store' +import { parseTranscript, sessionCandidate, userRecord } from './session-search-transcript-fixtures' + +it('recovers a declined append through its proven prefix while the transcript keeps growing', async () => { + const index = await openSessionSearchIndexFile('ss-lag-recovery') + const store = new SessionSearchStore(index.path, (error) => { + throw error + }) + const path = join(dirname(index.path), 'transcript.jsonl') + let unregister = registerSessionSearchIndexConsumer(store) + try { + await writeFile(path, `${userRecord(0, 'originalneedle')}\n`) + await parseTranscript(path) + // Let listing advance its resume point while the index consumer declines the read. + unregister() + await appendFile(path, `${userRecord(1, 'missedneedle')}\n`) + await parseTranscript(path) + unregister = registerSessionSearchIndexConsumer(store) + const pass = async () => { + const candidate = await sessionCandidate('claude', path) + await runSessionSearchIndexPass(store, [candidate], { + rows: new Map(store.files().map((row) => [row.path, row])) + }) + return candidate + } + await appendFile(path, `${userRecord(2, 'stillgrowingneedle')}\n`) + await pass() + expect(store.files()[0].state).toBe('current') + await appendFile(path, `${userRecord(3, 'recoveredneedle')}\n`) + const candidate = await pass() + expect(store.coversTranscript(candidate)).toBe(true) + expect(index.db.prepare('SELECT user_text FROM messages_fts ORDER BY rowid').all()).toEqual([ + { user_text: 'originalneedle' }, + { user_text: 'missedneedle' }, + { user_text: 'stillgrowingneedle' }, + { user_text: 'recoveredneedle' } + ]) + } finally { + unregister() + resetTranscriptConsumersForTests() + resetSessionParseCacheForTests() + store.close() + await index.close() + } +}) diff --git a/src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts b/src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts index a9e8cdb3900..866d7c0fe65 100644 --- a/src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts +++ b/src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { chmod, mkdir, rename, rm } from 'node:fs/promises' import { delimiter, dirname, join } from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' @@ -182,7 +183,7 @@ beforeEach(async () => { }) afterEach(async () => { - indexer?.close() + await indexer?.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() await harness.cleanup() @@ -262,7 +263,7 @@ for (const roots of ROOT_LAYOUTS) { await operation.run({ indexer: () => indexer as SessionSearchIndexer, reopen: async (args = {}) => { - indexer?.close() + await indexer?.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() if (args.removeDatabase === true) { diff --git a/src/main/ai-vault-search/session-search-live-transcript.test.ts b/src/main/ai-vault-search/session-search-live-transcript.test.ts index 5d28c0b0ee3..832af6e6e5f 100644 --- a/src/main/ai-vault-search/session-search-live-transcript.test.ts +++ b/src/main/ai-vault-search/session-search-live-transcript.test.ts @@ -89,7 +89,7 @@ it('indexes a Claude transcript through the reader and resumes on append', async }) }) -it('keeps a tool result searchable but out of the conversation half', async () => { +it('keeps the prompt searchable while excluding tool results', async () => { const root = await makeTempDir() const codexHome = await makeTempDir() const path = join(root, CODEX_ROLLOUT_FILE) @@ -104,10 +104,10 @@ it('keeps a tool result searchable but out of the conversation half', async () = await parseTranscript(path, 'codex', codexHome) expect(errors).toEqual([]) - expect(sessionsMatching('pericardium')).toHaveLength(1) - expect(sessionsMatching('outputonly')).toHaveLength(1) + expect(sessionsMatching('pericardium')).toHaveLength(0) + expect(sessionsMatching('outputonly')).toHaveLength(0) expect(sessionsMatching('tailonly')).toHaveLength(0) - expect(sessionsMatching('rg')).toHaveLength(1) + expect(sessionsMatching('rg')).toHaveLength(0) expect(sessionsMatching('{user_text assistant_text}: promptonly')).toHaveLength(1) expect(sessionsMatching('{user_text assistant_text}: outputonly')).toHaveLength(0) expect(sessionsMatching('{user_text assistant_text}: rg')).toHaveLength(0) diff --git a/src/main/ai-vault-search/session-search-merged-roots.test.ts b/src/main/ai-vault-search/session-search-merged-roots.test.ts index 242a475f7d5..bfad130fcc9 100644 --- a/src/main/ai-vault-search/session-search-merged-roots.test.ts +++ b/src/main/ai-vault-search/session-search-merged-roots.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { chmod, rm } from 'node:fs/promises' import { delimiter, join } from 'node:path' import { afterEach, beforeEach, expect, it } from 'vitest' @@ -29,7 +30,7 @@ beforeEach(async () => { }) afterEach(async () => { - indexer.close() + await indexer.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() await harness.cleanup() diff --git a/src/main/ai-vault-search/session-search-message-rows.test.ts b/src/main/ai-vault-search/session-search-message-rows.test.ts index b51830570ca..a33de16385b 100644 --- a/src/main/ai-vault-search/session-search-message-rows.test.ts +++ b/src/main/ai-vault-search/session-search-message-rows.test.ts @@ -91,26 +91,9 @@ it('cuts at punctuation when the window holds no whitespace at all', async () => } }) -it('files a tool row under the tool column alone', async () => { - const index = await openSessionSearchIndexFile('ss-message-rows-tool') - try { - for (const row of searchMessageRows([ - { role: 'tool', text: 'rg pericardium', timestamp: null } - ])) { - insertSearchMessage(index.db, 1, row) - } - expect(index.db.prepare('SELECT count(*) AS n FROM messages_fts').get()).toEqual({ n: 1 }) - expect( - index.db - .prepare('SELECT count(*) AS n FROM messages_fts WHERE messages_fts MATCH ?') - .get('{user_text assistant_text}: pericardium') - ).toEqual({ n: 0 }) - expect( - index.db - .prepare('SELECT count(*) AS n FROM messages_fts WHERE messages_fts MATCH ?') - .get('{tool_text}: pericardium') - ).toEqual({ n: 1 }) - } finally { - await index.close() - } +it('drops tool output and preserves the full conversation', () => { + const output = `pericardium ${'padding '.repeat(140_000)}` + expect([...searchMessageRows([{ role: 'tool', text: output, timestamp: null }])]).toEqual([]) + const rows = [...searchMessageRows([{ role: 'assistant', text: output, timestamp: null }])] + expect(rows.map((row) => row.text).join('')).toBe(output) }) diff --git a/src/main/ai-vault-search/session-search-message-rows.ts b/src/main/ai-vault-search/session-search-message-rows.ts index d497bdd7a35..43265fe4a15 100644 --- a/src/main/ai-vault-search/session-search-message-rows.ts +++ b/src/main/ai-vault-search/session-search-message-rows.ts @@ -1,13 +1,9 @@ import type SyncDatabase from '../sqlite/sync-database' import type { TranscriptMessage } from '../ai-vault/transcripts/session-transcript-consumers' -import { sliceAtCodeUnitLimit } from '../ai-vault/session-scanner-text-normalization' import { identifierShadowText } from './session-search-identifier-split' const CHUNK_TARGET_CHARS = 8000 -// Cap tool-output prefixes to keep logs from dominating index size; retain all conversation text. -const TOOL_ROW_CHARS = 3072 - // Folded diacritics remain inside tokens; see sqlite3Fts5UnicodeIsdiacritic in fts5_unicode2.c. const FOLDED_DIACRITIC = /[\u0300-\u0304\u0306-\u030c\u030f\u0311\u031b\u0323-\u0328\u032d-\u032e\u0330-\u0331]/ @@ -43,18 +39,12 @@ function* textChunks(text: string): Generator { } } -/** Chunk conversation text for FTS ranking; tool output gets one capped row. */ +/** Chunk conversation text for FTS ranking; tool output is not indexed. */ export function* searchMessageRows( messages: Iterable ): Generator { for (const message of messages) { if (message.role === 'tool') { - const text = sliceAtCodeUnitLimit(message.text, TOOL_ROW_CHARS) - yield { - ...message, - text, - ...(text.length < message.text.length ? { textTruncated: true as const } : {}) - } continue } for (const text of textChunks(message.text)) { diff --git a/src/main/ai-vault-search/session-search-native-chat-indexing.test.ts b/src/main/ai-vault-search/session-search-native-chat-indexing.test.ts index 8b4248015c7..c7eda7f3e66 100644 --- a/src/main/ai-vault-search/session-search-native-chat-indexing.test.ts +++ b/src/main/ai-vault-search/session-search-native-chat-indexing.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { appendFile, mkdir, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { afterEach, beforeEach, expect, it } from 'vitest' @@ -27,7 +28,7 @@ beforeEach(async () => { }) afterEach(async () => { - indexer.close() + await indexer.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() await harness.cleanup() diff --git a/src/main/ai-vault-search/session-search-opencode-index.test.ts b/src/main/ai-vault-search/session-search-opencode-index.test.ts index 0b649bf6859..72250fd7ff8 100644 --- a/src/main/ai-vault-search/session-search-opencode-index.test.ts +++ b/src/main/ai-vault-search/session-search-opencode-index.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { join } from 'node:path' import { afterEach, beforeEach, expect, it, vi } from 'vitest' @@ -75,7 +76,7 @@ beforeEach(async () => { }) afterEach(async () => { - indexer?.close() + await indexer?.close() for (const db of engineDbs) { db.close() } @@ -150,14 +151,12 @@ function writeVault(): void { ]) } -it('searches tool output under the all scope and not under conversation', async () => { +it('excludes tool output from both search scopes', async () => { writeVault() await startIndexer() const all = openEngine().search({ query: TOOL_ONLY, scope: 'all' }) - expect(all.hits).toHaveLength(1) - expect(all.hits[0]).toMatchObject({ agent: 'opencode', sessionId: SESSION }) - expect(all.hits[0]?.evidence?.role).toBe('tool') + expect(all.hits).toEqual([]) expect(openEngine().search({ query: TOOL_ONLY, scope: 'conversation' }).hits).toEqual([]) }) diff --git a/src/main/ai-vault-search/session-search-orphan-rows.test.ts b/src/main/ai-vault-search/session-search-orphan-rows.test.ts index 650aacb3460..4a2a48c9003 100644 --- a/src/main/ai-vault-search/session-search-orphan-rows.test.ts +++ b/src/main/ai-vault-search/session-search-orphan-rows.test.ts @@ -6,10 +6,8 @@ import { type SessionSearchHarness } from './session-search-engine-test-fixture' import { identifierShadowText } from './session-search-identifier-split' -import { readIndexGeneration } from './session-search-index-generation' import { planSessionSearchQuery } from './session-search-query-planner' import { sessionSearchSnippet } from './session-search-snippet' -import type { SessionSearchCursorError } from './session-search-page-cursor' import { SessionSearchTypoRepair } from './session-search-typo-repair' // Purges leave orphaned FTS rows until reclamation; retrieval, snippets and typo repair must exclude them. @@ -90,33 +88,6 @@ it('snippets nothing for an orphaned row, even asked for it by rowid', async () }) describe('a purge reclaiming rows nothing can reach', () => { - async function withReclaimable(): Promise { - harness = await openSessionSearchHarness('ss-orphan-drain') - // Typo repair needs at least two live occurrences. - addSyntheticSession(harness.db, { id: 1, text: 'the marmoset lives here', rows: 2 }) - plantOrphans(harness.db) - return harness - } - - it('moves the generation anyway, so no cursor spans it', async () => { - // Reclamation can change which vocabulary candidates fit the scan limit, so it must fence page cursors. - const open = await withReclaimable() - addSyntheticSession(open.db, { id: 2, text: 'the marmoset again', rows: 2 }) - const page = open.engine.search({ query: 'marmoset', limit: 1 }) - expect(page.page.cursor).not.toBeNull() - const before = readIndexGeneration(open.db) - - await open.store.purgeOlderThan(null) - - expect(readIndexGeneration(open.db)).toBeGreaterThan(before) - try { - open.engine.search({ query: 'marmoset', limit: 1, cursor: page.page.cursor! }) - expect.unreachable('a cursor must not span a purge') - } catch (error) { - expect((error as SessionSearchCursorError).rejection).toBe('stale-generation') - } - }) - it('picks the same repair when an unreachable spelling was the more common one', async () => { // Add orphaned occurrences to the less common live spelling to test that repair ranks only live rows. harness = await openSessionSearchHarness('ss-orphan-drain-tie') diff --git a/src/main/ai-vault-search/session-search-paging.test.ts b/src/main/ai-vault-search/session-search-paging.test.ts index e72516f5c80..50437ff5fe0 100644 --- a/src/main/ai-vault-search/session-search-paging.test.ts +++ b/src/main/ai-vault-search/session-search-paging.test.ts @@ -1,15 +1,16 @@ -import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterEach, describe, expect, it } from 'vitest' import type { SessionSearchRequest } from './session-search-engine-types' +import type { SessionSearchRankedCursor } from './session-search-ranked-cursor' import { addSyntheticSession, openSessionSearchHarness, type SessionSearchHarness } from './session-search-engine-test-fixture' -import { readIndexGeneration } from './session-search-index-generation' import { decodeSessionSearchCursor, encodeSessionSearchCursor, - SessionSearchCursorError + SessionSearchCursorError, + sessionSearchPageKey } from './session-search-page-cursor' let harness: SessionSearchHarness | null = null @@ -208,32 +209,40 @@ describe('the candidate limit is a tunable default, and says when it cut', () => }) }) -it.each([false, true])('rejects a write during page assembly (cursor: %s)', async (withCursor) => { - const { db, engine, store } = await open('ss-concurrent-page') - for (let id = 1; id <= 3; id++) { - addSyntheticSession(db, { id, text: 'needle' }) +it('round-trips an existing offset cursor through the engine with its raw generation fence', async () => { + const { db, engine } = await withSessions(4) + const request = { query: 'needle', limit: 1 } + const first = engine.search(request) + const incarnation = String( + db.prepare("SELECT value FROM meta WHERE key = 'index_incarnation'").get()!.value + ) + const key = sessionSearchPageKey(request) + const cursor = encodeSessionSearchCursor(first.generation, 1, key, incarnation) + const second = engine.search({ ...request, cursor }) + expect(second.hits[0]?.sessionId).not.toBe(first.hits[0]?.sessionId) + expect(decodeSessionSearchCursor(second.page.cursor!, first.generation, key, incarnation)).toBe(2) +}) + +it('validates frozen cursor bounds, query scopes and message ownership before exposing a hit', async () => { + const { engine, db } = await withSessions(4) + addSyntheticSession(db, { id: 9, cwd: '/private', text: 'hiddenneedle' }) + const request = { query: 'needle', limit: 1, filters: { scopePaths: ['/repo'] } } + const first = engine.search(request) + const raw = first.page.cursor! + const payload = JSON.parse(Buffer.from(raw.slice(4), 'base64url').toString('utf8')) + const forged = (patch: Partial) => + `ss2.${Buffer.from(JSON.stringify({ ...payload, ...patch })).toString('base64url')}` + for (const patch of [ + { o: -1 }, + { hits: Array.from({ length: 601 }, () => payload.hits[0]) }, + { hits: [{ ...payload.hits[0], s: 9 }], o: 0 }, + { hits: [{ ...payload.hits[0], m: 5 }], o: 0 } + ]) { + expect(() => engine.search({ ...request, cursor: forged(patch) })).toThrow( + SessionSearchCursorError + ) } - const first = engine.search({ query: 'needle', limit: 1 }) - const prepare = db.prepare.bind(db) - let committed = false - const hook = vi.spyOn(db, 'prepare').mockImplementation((sql) => { - if (!committed && sql.includes('SELECT DISTINCT session_row_id FROM files')) { - committed = true - store.removeFile('/synthetic/1.jsonl') - } - return prepare(sql) - }) - try { - expect(() => - engine.search({ - query: 'needle', - limit: 1, - ...(withCursor ? { cursor: first.page.cursor! } : {}) - }) - ).toThrow(SessionSearchCursorError) - expect(committed).toBe(true) - expect(readIndexGeneration(db)).toBeGreaterThan(first.generation) - } finally { - hook.mockRestore() - } + expect(() => engine.search({ ...request, cursor: `ss2.${'x'.repeat(256 * 1024)}` })).toThrow( + SessionSearchCursorError + ) }) diff --git a/src/main/ai-vault-search/session-search-pass.ts b/src/main/ai-vault-search/session-search-pass.ts index 6681643e8e4..c6a2f4af744 100644 --- a/src/main/ai-vault-search/session-search-pass.ts +++ b/src/main/ai-vault-search/session-search-pass.ts @@ -20,7 +20,7 @@ import { sessionSearchRootListings, type SessionSearchScanRoots } from './session-search-scan-roots' -import type { SessionSearchFileRow, SessionSearchStore } from './session-search-store' +import type { SessionSearchFileRow, SessionSearchIndexStore } from './session-search-store' import { sessionSearchEnumeratedContainers } from './session-search-synthetic-sources' // Bound recent-cycle retirement work and prioritize rows within the recency window. @@ -30,7 +30,7 @@ const RETIREMENT_ROWS_PER_CYCLE = 512 const RETIREMENT_DIRECTORIES_PER_PASS = 512 export type SessionSearchPassArgs = { - store: SessionSearchStore + store: SessionSearchIndexStore roots: SessionSearchScanRoots /** A sweep lists every root; a cycle lists the newest N per agent. */ full: boolean @@ -60,6 +60,7 @@ export async function runSessionSearchPass( args: SessionSearchPassArgs ): Promise { const { store, signal } = args + await store.prepare?.() if (args.full) { // Apply narrowed retention before the new instance’s first discovery. await store.purgeOlderThan(store.retentionCutoff, signal) @@ -131,6 +132,7 @@ export async function runSessionSearchPass( ) : [] + await store.flush?.() return { rootsWithFiles, degradedRoots: mergeDegradedRoots( diff --git a/src/main/ai-vault-search/session-search-posting-window.test.ts b/src/main/ai-vault-search/session-search-posting-window.test.ts new file mode 100644 index 00000000000..eb8105429b1 --- /dev/null +++ b/src/main/ai-vault-search/session-search-posting-window.test.ts @@ -0,0 +1,170 @@ +import { expect, it } from 'vitest' +import { addSyntheticSession, openSessionSearchHarness } from './session-search-engine-test-fixture' + +it('freezes the newest index window before ranking and pages its truncated result once', async () => { + const index = await openSessionSearchHarness('ss-posting-window') + try { + index.db.exec('BEGIN') + for (let id = 1; id <= 12; id++) { + addSyntheticSession(index.db, { + id, + text: 'windowword', + rows: 1000, + updatedAt: `2026-09-${String(20 - id).padStart(2, '0')}T00:00:00.000Z` + }) + } + index.db.exec('COMMIT') + const request = { query: 'windowword', limit: 1 } + const first = index.engine.search(request) + expect(first.hits.map((hit) => hit.sessionId)).toEqual(['11']) + expect(first.truncated.candidates).toBe(true) + expect(first.page.hasMore).toBe(true) + const next = index.engine.search({ ...request, cursor: first.page.cursor! }) + expect(next.hits.map((hit) => hit.sessionId)).toEqual(['12']) + expect(next.truncated.candidates).toBe(true) + expect(next.page).toEqual({ cursor: null, hasMore: false }) + } finally { + await index.close() + } +}) + +it('charges tool postings to scan work but leaves all candidate slots for conversations', async () => { + const index = await openSessionSearchHarness('ss-posting-tools') + try { + index.db.exec('BEGIN') + addSyntheticSession(index.db, { id: 1, text: 'crowded', rows: 1000 }) + addSyntheticSession(index.db, { id: 2, text: 'crowded', rows: 2000 }) + addSyntheticSession(index.db, { id: 3, text: 'crowded', role: 'tool', rows: 8500 }) + index.db.exec('COMMIT') + const result = index.engine.search({ query: 'crowded' }) + expect(result.hits.map((hit) => hit.sessionId)).toEqual(['2']) + expect(result.truncated.candidates).toBe(true) + const filtered = index.engine.search({ query: 'crowded repo:absent' }) + expect(filtered.hits).toEqual([]) + expect(filtered.truncated.candidates).toBe(true) + } finally { + await index.close() + } +}) + +it('shares examined postings across phrase and AND without rewalking filtered matches', async () => { + const index = await openSessionSearchHarness('ss-posting-routes') + try { + index.db.exec('BEGIN') + addSyntheticSession(index.db, { id: 1, text: 'alpha x beta', rows: 2 }) + addSyntheticSession(index.db, { id: 2, text: 'alpha beta', role: 'tool', rows: 6000 }) + index.db.exec('COMMIT') + const result = index.engine.search({ query: '"alpha beta"' }) + expect(result.hits).toEqual([]) + expect(result.truncated.candidates).toBe(true) + } finally { + await index.close() + } +}) + +it('keeps an inconclusive own spelling and reports even an empty budget-limited result', async () => { + const index = await openSessionSearchHarness('ss-posting-own-term') + try { + index.db.exec('BEGIN') + addSyntheticSession(index.db, { id: 1, text: 'coalesces', rows: 2 }) + addSyntheticSession(index.db, { id: 2, text: 'coalesce', rows: 2 }) + addSyntheticSession(index.db, { id: 3, text: 'coalesces', role: 'tool', rows: 10001 }) + index.db.exec('COMMIT') + const result = index.engine.search({ query: 'coalesces' }) + expect(result.planner.repairedTerms).toBeUndefined() + expect(result.hits).toEqual([]) + expect(result.truncated.candidates).toBe(true) + } finally { + await index.close() + } +}) + +it('keeps capitalized hits when the spelling probe and OR share a crowded window', async () => { + const index = await openSessionSearchHarness('ss-posting-capitalized') + try { + index.db.exec('BEGIN') + addSyntheticSession(index.db, { id: 1, text: 'Timeout' }) + addSyntheticSession(index.db, { id: 2, text: 'Timeout', rows: 64 }) + addSyntheticSession(index.db, { id: 3, text: 'Timeout', role: 'tool', rows: 5000 }) + index.db.exec('COMMIT') + for (const query of ['Timeout', 'TIMEOUT']) { + const result = index.engine.search({ query }) + expect(result.hits.map((hit) => hit.sessionId).sort()).toEqual(['1', '2']) + expect(result.truncated.candidates).toBe(false) + } + } finally { + await index.close() + } +}) + +it('keeps original OR hits beside a misspelling and crowded tool vocabulary', async () => { + const index = await openSessionSearchHarness('ss-posting-vocabulary') + try { + index.db.exec('BEGIN') + addSyntheticSession(index.db, { id: 1, text: 'deploy', rows: 2 }) + addSyntheticSession(index.db, { id: 2, text: 'request', role: 'tool', rows: 10001 }) + index.db.exec('COMMIT') + const result = index.engine.search({ query: 'recieve deploy' }) + expect(result.hits.map((hit) => hit.sessionId)).toEqual(['1']) + expect(result.planner.route).toBe('or') + expect(result.planner.repairedTerms).toBeUndefined() + } finally { + await index.close() + } +}) + +it('repairs a common spelling without spending postings on its vocabulary occurrences', async () => { + const index = await openSessionSearchHarness('ss-posting-common-repair') + try { + index.db.exec('BEGIN') + addSyntheticSession(index.db, { id: 1, text: 'coalesces', rows: 10001 }) + addSyntheticSession(index.db, { id: 2, text: 'terminal', rows: 2 }) + index.db.exec('COMMIT') + const single = index.engine.search({ query: 'coalescs' }) + expect(single.planner.repairedTerms).toEqual(['coalesces']) + expect(single.hits.map((hit) => hit.sessionId)).toEqual(['1']) + const mixed = index.engine.search({ query: 'coalescs terminal' }) + expect(mixed.planner.repairedTerms).toEqual(['coalesces', 'terminal']) + expect(mixed.hits.map((hit) => hit.sessionId).sort()).toEqual(['1', '2']) + } finally { + await index.close() + } +}) + +it('keeps original hits and truncation when the shared vocabulary seek allowance runs out', async () => { + const index = await openSessionSearchHarness('ss-posting-vocabulary-limit') + try { + addSyntheticSession(index.db, { id: 1, text: 'deploy' }) + addSyntheticSession(index.db, { + id: 2, + text: Array.from({ length: 4001 }, (_, n) => `aa${String(n).padStart(5, '0')}`).join(' '), + role: 'tool' + }) + const result = index.engine.search({ query: 'aargh deploy' }) + expect(result.hits.map((hit) => hit.sessionId)).toEqual(['1']) + expect(result.planner.route).toBe('or') + expect(result.planner.repairedTerms).toBeUndefined() + expect(result.truncated.candidates).toBe(true) + } finally { + await index.close() + } +}) + +it('seeks past a whole vocabulary term while retaining its extensions', async () => { + const index = await openSessionSearchHarness('ss-posting-vocabulary-successor') + try { + addSyntheticSession(index.db, { id: 1, text: 'coalesce coalesces coalescing', rows: 2 }) + const seek = index.db.prepare( + 'SELECT term FROM temp.session_search_vocab WHERE term >= ? ORDER BY term LIMIT 1' + ) + expect(seek.get('coal')).toEqual({ term: 'coalesce' }) + expect(seek.get('coalesce\u0000')).toEqual({ term: 'coalesces' }) + expect(seek.get('coalesces\u0000')).toEqual({ term: 'coalescing' }) + expect(seek.get('coalescing\u0000')).toBeUndefined() + const result = index.engine.search({ query: 'coalescs' }) + expect(result.planner.repairedTerms).toEqual(['coalesces']) + expect(result.hits.map((hit) => hit.sessionId)).toEqual(['1']) + } finally { + await index.close() + } +}) diff --git a/src/main/ai-vault-search/session-search-postings.ts b/src/main/ai-vault-search/session-search-postings.ts new file mode 100644 index 00000000000..f1a77244c28 --- /dev/null +++ b/src/main/ai-vault-search/session-search-postings.ts @@ -0,0 +1,87 @@ +import type SyncDatabase from '../sqlite/sync-database' +import type { SessionSearchScope } from './session-search-engine-types' +import type { MessageRow } from './session-search-hit-ranking' +import { scopedExpression } from './session-search-query-planner' + +const EXAMINED_POSTINGS_LIMIT = 10_000 +export const SESSION_SEARCH_MESSAGE_CANDIDATES = 2000 + +type PostingWindow = { + rows: MessageRow[] + after: number | null + complete: boolean +} + +/** One request shares its index work across retrieval routes and spelling probes. */ +export class SessionSearchPostings { + private remaining = EXAMINED_POSTINGS_LIMIT + private readonly windows = new Map() + exhausted = false + + constructor( + private readonly db: SyncDatabase, + private readonly scope: SessionSearchScope, + private readonly acceptsSession: (id: number) => boolean = () => true + ) {} + + private examinePosting(): boolean { + // The first row beyond the shared budget is the request's only exhaustion probe. + if (this.remaining === 0) { + this.exhausted = true + return false + } + this.remaining-- + return true + } + + *matching(expression: string): Generator { + let window = this.windows.get(expression) + if (!window) { + window = { rows: [], after: null, complete: false } + this.windows.set(expression, window) + } + // Why: an own-term probe and its later retrieval must share the already-examined window. + yield* window.rows + if (window.complete || this.exhausted) { + return + } + const statement = this.db.prepare( + `SELECT rowid FROM messages_fts WHERE messages_fts MATCH ? + ${window.after === null ? '' : 'AND rowid < CAST(? AS INTEGER)'} ORDER BY rowid DESC` + ) + const values = window.after === null ? [expression] : [expression, window.after] + const message = this.db.prepare( + `SELECT m.id AS rowid, m.session_row_id, m.role, m.ts FROM messages m + JOIN sessions s ON s.id = m.session_row_id WHERE m.id = ? AND m.role != 'tool'` + ) + const scoped = this.db.prepare( + 'SELECT rowid FROM messages_fts WHERE messages_fts MATCH ? AND rowid IN (SELECT ?)' + ) + for (const posting of statement.iterate(...values)) { + if (!this.examinePosting()) { + return + } + window.after = Number(posting.rowid) + const row = message.get(window.after) + if (!row || !this.acceptsSession(Number(row.session_row_id))) { + continue + } + if ( + this.scope === 'conversation' && + !scoped.get(scopedExpression(this.scope, expression), window.after) + ) { + continue + } + const match: MessageRow = { + rowid: window.after, + session_row_id: Number(row.session_row_id), + role: String(row.role), + ts: typeof row.ts === 'string' ? row.ts : null, + score: 0 + } + window.rows.push(match) + yield match + } + window.complete = true + } +} diff --git a/src/main/ai-vault-search/session-search-ranked-cursor.ts b/src/main/ai-vault-search/session-search-ranked-cursor.ts new file mode 100644 index 00000000000..e2728698155 --- /dev/null +++ b/src/main/ai-vault-search/session-search-ranked-cursor.ts @@ -0,0 +1,88 @@ +import { z } from 'zod' +import { SessionSearchCursorError } from './session-search-page-cursor' + +export const SESSION_SEARCH_MAX_CANDIDATES = 600 +const PREFIX = 'ss2.' +const MAX_CURSOR_CHARS = 256 * 1024 +const integer = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER) +const tokens = z.array(z.string().max(512)).max(64) +const rankedCursorSchema = z.object({ + v: z.literal(2), + i: z.string().min(1).max(128), + g: integer, + r: z.number().int().min(-Number.MAX_SAFE_INTEGER).max(Number.MAX_SAFE_INTEGER), + k: z.string().max(128), + o: integer, + hits: z + .array( + z.object({ + s: integer.positive(), + m: integer.positive().nullable(), + score: z.number().finite(), + d: integer.positive().max(SESSION_SEARCH_MAX_CANDIDATES) + }) + ) + .max(SESSION_SEARCH_MAX_CANDIDATES), + retrieval: z + .object({ + route: z.enum(['phrase', 'and', 'or', 'typo+phrase', 'typo+and', 'typo+or']), + plan: z.object({ + literal: z.boolean(), + truncated: z.boolean(), + terms: tokens, + body: tokens, + phrase: tokens + }), + repairedTerms: tokens.optional() + }) + .nullable(), + incomplete: z.boolean(), + queryTruncated: z.boolean() +}) + +export type SessionSearchRankedCursor = z.infer +export type SessionSearchFrozenHit = SessionSearchRankedCursor['hits'][number] + +export function encodeRankedSessionSearchCursor(cursor: SessionSearchRankedCursor): string { + return `${PREFIX}${Buffer.from(JSON.stringify(cursor), 'utf8').toString('base64url')}` +} + +/** Legacy cursors retain raw-generation fencing and offset interpretation. */ +export function decodeRankedSessionSearchCursor( + raw: string, + generation: number, + revision: number, + key: string, + incarnation: string +): SessionSearchRankedCursor | null { + if (!raw.startsWith(PREFIX)) { + return null + } + let parsed: unknown + try { + if (raw.length > MAX_CURSOR_CHARS) { + throw new Error('Cursor too large') + } + parsed = JSON.parse(Buffer.from(raw.slice(PREFIX.length), 'base64url').toString('utf8')) + } catch { + throw new SessionSearchCursorError('malformed', generation) + } + const result = rankedCursorSchema.safeParse(parsed) + if (!result.success) { + throw new SessionSearchCursorError('malformed', generation) + } + const cursor = result.data + if ( + cursor.o >= cursor.hits.length || + new Set(cursor.hits.map((hit) => hit.s)).size !== cursor.hits.length + ) { + throw new SessionSearchCursorError('malformed', generation, cursor.g) + } + if (cursor.r !== revision || cursor.i !== incarnation) { + throw new SessionSearchCursorError('stale-generation', generation, cursor.g) + } + if (cursor.k !== key) { + throw new SessionSearchCursorError('different-query', generation, cursor.g) + } + return cursor +} diff --git a/src/main/ai-vault-search/session-search-reader-snapshot.test.ts b/src/main/ai-vault-search/session-search-reader-snapshot.test.ts new file mode 100644 index 00000000000..44280d52502 --- /dev/null +++ b/src/main/ai-vault-search/session-search-reader-snapshot.test.ts @@ -0,0 +1,42 @@ +import { expect, it, vi } from 'vitest' +import { openSessionSearchHarness, addSyntheticSession } from './session-search-engine-test-fixture' + +it('keeps a page on one read snapshot while commits invalidate later page requests', async () => { + const harness = await openSessionSearchHarness('ss-reader-snapshot') + const { db, store, engine } = harness + addSyntheticSession(db, { id: 1, text: 'snapshotneedle' }) + addSyntheticSession(db, { id: 2, text: 'snapshotneedle' }) + const baseline = engine.search({ query: 'snapshotneedle', limit: 1 }) + const baselineSessions = baseline.hits.map((hit) => hit.sessionId) + const prepare = db.prepare.bind(db) + let changed = false + const boundary = vi.spyOn(db, 'prepare').mockImplementation((sql) => { + const statement = prepare(sql) + if (sql.includes('SELECT value FROM meta')) { + const get = statement.get.bind(statement) + statement.get = (...args) => { + const result = get(...args) + if (!changed) { + changed = true + // Why: the writer's separate connection removes the page's own hit mid-assembly; the page must not see it. + store.removeFile(`/synthetic/${baselineSessions[0]}.jsonl`) + } + return result + } + } + return statement + }) + try { + const first = engine.search({ query: 'snapshotneedle', limit: 1 }) + expect(changed).toBe(true) + expect(first.hits.map((hit) => hit.sessionId)).toEqual(baselineSessions) + expect(first.page.cursor).toBeTruthy() + boundary.mockRestore() + expect(() => + engine.search({ query: 'snapshotneedle', limit: 1, cursor: first.page.cursor! }) + ).toThrow() + } finally { + boundary.mockRestore() + await harness.close() + } +}) diff --git a/src/main/ai-vault-search/session-search-readers.test.ts b/src/main/ai-vault-search/session-search-readers.test.ts new file mode 100644 index 00000000000..84271b51014 --- /dev/null +++ b/src/main/ai-vault-search/session-search-readers.test.ts @@ -0,0 +1,68 @@ +import { EventEmitter } from 'node:events' +import { afterEach, expect, it, vi } from 'vitest' +import type * as WorkerClient from './session-search-worker-client' +import { SessionSearchReaders } from './session-search-readers' +import type { SearchWorkerRequest } from './session-search-worker-protocol' + +let createWorker: () => BlockedReader +vi.mock('./session-search-worker-client', async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + SessionSearchWorkerClient: class extends actual.SessionSearchWorkerClient { + constructor() { + super(undefined, () => createWorker()) + } + } + } +}) + +class BlockedReader extends EventEmitter { + private release!: (code: number) => void + private readonly exit = new Promise((resolve) => { + this.release = resolve + }) + unref(): void {} + postMessage(request: SearchWorkerRequest): void { + if (request.command.op === 'open') { + queueMicrotask(() => this.emit('message', { id: request.id })) + } + } + terminate(): Promise { + return this.exit + } + exitNativeCall(): void { + this.release(0) + } +} + +afterEach(() => vi.useRealTimers()) +it('fails timed-out searches before native retirement and keeps clear waiting for both blocked readers', async () => { + vi.useFakeTimers() + const native: BlockedReader[] = [] + createWorker = () => { + const worker = new BlockedReader() + native.push(worker) + return worker + } + const readers = new SessionSearchReaders('blocked-index.sqlite', null) + for (let i = 0; i < 2; i++) { + const result = expect(readers.query({ query: 'common' })).rejects.toThrow( + 'Could not search sessions' + ) + await vi.advanceTimersByTimeAsync(10_000) + await result + } + await expect(readers.query({ query: 'common' })).rejects.toThrow('Could not search sessions') + let cleared = false + const retired = readers.close().then(() => { + cleared = true + }) + await Promise.resolve() + expect(cleared).toBe(false) + for (const worker of native) { + worker.exitNativeCall() + } + await retired + expect(cleared).toBe(true) +}) diff --git a/src/main/ai-vault-search/session-search-readers.ts b/src/main/ai-vault-search/session-search-readers.ts new file mode 100644 index 00000000000..2ad90b8be8d --- /dev/null +++ b/src/main/ai-vault-search/session-search-readers.ts @@ -0,0 +1,92 @@ +import type { AiVaultSearchRequest } from '../../shared/ai-vault/search/ai-vault-search-types' +import type { SessionSearchHostScope } from './session-search-service' +import { SessionSearchWorkerClient } from './session-search-worker-client' +import type { SearchWorkerCommand, SearchWorkerResponse } from './session-search-worker-protocol' + +/** Two fixed slots count retiring native readers against capacity until their exit. */ +export class SessionSearchReaders { + private readonly slots = Array.from( + { length: 2 }, + (): { + client: SessionSearchWorkerClient + opened: number + opening: Promise | null + } => ({ + client: new SessionSearchWorkerClient(), + opened: -1, + opening: null + }) + ) + private closed = false + constructor( + private readonly path: string, + private readonly cutoff: number | null + ) {} + query(request: AiVaultSearchRequest, scope?: SessionSearchHostScope, signal?: AbortSignal) { + return this.execute({ op: 'query', request, scope }, signal) + } + private execute( + command: SearchWorkerCommand, + signal?: AbortSignal + ): Promise { + const slot = + this.slots.find((s) => !s.client.retiring && !s.client.busy) ?? + this.slots.find((s) => !s.client.retiring) + if (this.closed || signal?.aborted || !slot) { + return Promise.reject(new Error('Could not search sessions')) + } + return new Promise((resolve, reject) => { + let finished = false + const finish = (error?: unknown, value?: SearchWorkerResponse) => { + if (finished) { + return + } + finished = true + clearTimeout(timer) + signal?.removeEventListener('abort', abort) + if (error || !value) { + reject(error ?? new Error('Could not search sessions')) + } else { + resolve(value) + } + } + const abort = () => { + // Why: send failure before termination; a native SQLite call can delay worker exit. + finish(new Error('Could not search sessions')) + slot.client.stop() + } + const timer = setTimeout(abort, 10_000) + signal?.addEventListener('abort', abort, { once: true }) + void (async () => { + if (slot.opened !== slot.client.epoch) { + slot.opening ??= (async () => { + const epoch = slot.client.epoch + await slot.client.request({ + op: 'open', + path: this.path, + reader: true, + cutoff: this.cutoff + }) + if (slot.client.epoch !== epoch) { + throw new Error('Could not search sessions') + } + slot.opened = epoch + })().finally(() => { + slot.opening = null + }) + await slot.opening + } + if (!finished) { + finish(undefined, await slot.client.request(command)) + } + })().catch((error) => finish(error)) + }) + } + close(): Promise { + this.closed = true + for (const slot of this.slots) { + slot.client.stop() + } + return Promise.all(this.slots.map((s) => s.client.retired())).then(() => undefined) + } +} diff --git a/src/main/ai-vault-search/session-search-request-deadline.ts b/src/main/ai-vault-search/session-search-request-deadline.ts new file mode 100644 index 00000000000..41ac7abddc3 --- /dev/null +++ b/src/main/ai-vault-search/session-search-request-deadline.ts @@ -0,0 +1,20 @@ +import { abandonRemoteSessionScanOnCancel } from '../ai-vault/ai-vault-scan-cancellation' + +/** Cleanup continues after a caller leaves; native retirement must never extend an IPC deadline. */ +export async function withinSessionSearchDeadline( + work: Promise, + signal?: AbortSignal +): Promise { + let timer: ReturnType | undefined + const deadline = new Promise((_resolve, reject) => { + timer = setTimeout( + () => reject(new Error('Could not complete session search operation')), + 10_000 + ) + }) + try { + return await abandonRemoteSessionScanOnCancel(Promise.race([work, deadline]), signal) + } finally { + clearTimeout(timer) + } +} diff --git a/src/main/ai-vault-search/session-search-restart-reindex.test.ts b/src/main/ai-vault-search/session-search-restart-reindex.test.ts index 82399d9540c..a4e5deb8997 100644 --- a/src/main/ai-vault-search/session-search-restart-reindex.test.ts +++ b/src/main/ai-vault-search/session-search-restart-reindex.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { appendFile, chmod, mkdir, stat, writeFile } from 'node:fs/promises' import { dirname, join } from 'node:path' import { afterEach, beforeEach, expect, it } from 'vitest' @@ -31,7 +32,7 @@ beforeEach(async () => { }) afterEach(async () => { - indexer?.close() + await indexer?.close() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() await harness.cleanup() @@ -106,7 +107,7 @@ async function indexThenRestart(path: string, content: string): Promise { + const index = await openSessionSearchHarness('ss-orphan-paging') + try { + for (let id = 1; id <= 7; id++) { + addSyntheticSession(index.db, { id, text: 'needle' }) + } + addSyntheticSession(index.db, { id: 20, text: 'needle', rows: 1537 }) + index.db.exec('DELETE FROM sessions WHERE id = 20; DELETE FROM files WHERE session_row_id = 20') + const request = { query: 'needle', limit: 1 } + const first = index.engine.search(request) + const before = readIndexContentRevision(index.db) + const seen = first.hits.map((hit) => hit.sessionId) + let cursor = first.page.cursor + await drainOrphanedMessages( + index.db, + () => false, + async () => { + if (cursor) { + const page = index.engine.search({ ...request, cursor }) + seen.push(...page.hits.map((hit) => hit.sessionId)) + cursor = page.page.cursor + } + } + ) + expect(seen).toEqual(['7', '6', '5', '4', '3', '2', '1']) + expect(cursor).toBeNull() + expect(readIndexContentRevision(index.db)).toBe(before) + expect(readIndexGeneration(index.db)).toBeGreaterThan(first.generation) + } finally { + await index.close() + } +}) + it('finishes an interrupted deletion after reopening', async () => { const index = await openSessionSearchIndexFile('ss-retention-resume') let store = new SessionSearchStore(index.path) @@ -68,7 +103,7 @@ it('finishes an interrupted deletion after reopening', async () => { closed = false // Orphaned rows must drain even after retention becomes unlimited. await store.purgeOlderThan(null) - expect(count(index.db, 'messages')).toBe(0) + await vi.waitFor(() => expect(count(index.db, 'messages')).toBe(0)) expect(count(index.db, 'messages_fts')).toBe(0) } finally { if (!closed) { @@ -92,7 +127,7 @@ it('cancels retention between batches and resumes without exposing a partial ses expect(remaining).toBeLessThan(1025) expect(visibleSessionIds(index.db)).toEqual([]) await store.purgeOlderThan(null) - expect(count(index.db, 'messages')).toBe(0) + await vi.waitFor(() => expect(count(index.db, 'messages')).toBe(0)) } finally { store.close() await index.close() diff --git a/src/main/ai-vault-search/session-search-retention-delete.ts b/src/main/ai-vault-search/session-search-retention-delete.ts index 09222aa16a5..1257b33c670 100644 --- a/src/main/ai-vault-search/session-search-retention-delete.ts +++ b/src/main/ai-vault-search/session-search-retention-delete.ts @@ -1,6 +1,8 @@ +import { traceSessionSearch } from './session-search-trace' import { setImmediate as yieldToEventLoop } from 'node:timers/promises' import type SyncDatabase from '../sqlite/sync-database' import { deleteSearchMessages } from './session-search-message-rows' +import { readIndexGeneration, recordIndexMaintenance } from './session-search-index-generation' export const RETENTION_DELETE_ROWS_PER_STEP = 256 // Reclaim freed pages incrementally to avoid a long vacuum after the purge. @@ -13,7 +15,9 @@ export async function deleteExpiredSearchFiles( db: SyncDatabase, cutoffMs: number | null, closed: () => boolean, - yieldStep: () => Promise = yieldToEventLoop + yieldStep: () => Promise = yieldToEventLoop, + onMessagesChanged: (delta: number) => void = () => undefined, + reclaim = true ): Promise { if (cutoffMs !== null) { const expired = db @@ -41,14 +45,17 @@ export async function deleteExpiredSearchFiles( await yieldStep() } } - await drainOrphanedMessages(db, closed, yieldStep) + if (reclaim) { + await drainOrphanedMessages(db, closed, yieldStep, onMessagesChanged) + } } /** Drain orphaned rows left by retention, replacement, or interruption; the rows record unfinished work. */ export async function drainOrphanedMessages( db: SyncDatabase, closed: () => boolean, - yieldStep: () => Promise = yieldToEventLoop + yieldStep: () => Promise = yieldToEventLoop, + onMessagesChanged: (delta: number) => void = () => undefined ): Promise { // Finish one session before scanning for the next orphan to avoid repeating that scan per batch. const nextOrphan = db.prepare( @@ -60,8 +67,13 @@ export async function drainOrphanedMessages( db.exec('BEGIN IMMEDIATE') let deleted = 0 try { - deleted = deleteSearchMessages(db, orphan, RETENTION_DELETE_ROWS_PER_STEP) + const before = readIndexGeneration(db) + deleted = traceSessionSearch('drain', () => + deleteSearchMessages(db, orphan!, RETENTION_DELETE_ROWS_PER_STEP) + ) + recordIndexMaintenance(db, before) db.exec('COMMIT') + onMessagesChanged(-deleted) } catch (error) { db.exec('ROLLBACK') throw error @@ -79,6 +91,6 @@ export async function drainOrphanedMessages( function reclaimFreePages(db: SyncDatabase): void { const free = Number(db.pragma('freelist_count', { simple: true })) if (free > Number(db.pragma('page_count', { simple: true })) * RECLAIM_FREE_PAGE_SHARE) { - db.pragma(`incremental_vacuum(${RECLAIM_PAGES_PER_STEP})`) + traceSessionSearch('vacuum', () => db.pragma(`incremental_vacuum(${RECLAIM_PAGES_PER_STEP})`)) } } diff --git a/src/main/ai-vault-search/session-search-retrieval.ts b/src/main/ai-vault-search/session-search-retrieval.ts index 24ae58f6f10..b2996785434 100644 --- a/src/main/ai-vault-search/session-search-retrieval.ts +++ b/src/main/ai-vault-search/session-search-retrieval.ts @@ -1,6 +1,9 @@ import type SyncDatabase from '../sqlite/sync-database' import type { SessionSearchRoute, SessionSearchScope } from './session-search-engine-types' -import type { MessageRow, SessionRow } from './session-search-hit-ranking' +import type { SessionSearchFrozenHit } from './session-search-ranked-cursor' +import { SessionSearchCursorError } from './session-search-page-cursor' +import { readIndexGeneration } from './session-search-index-generation' +import type { RankedSession, MessageRow, SessionRow } from './session-search-hit-ranking' import { andExpression, orExpression, @@ -10,7 +13,11 @@ import { type SessionSearchQueryPlan } from './session-search-query-planner' import type { SessionRowFilter } from './session-search-row-filter' -import { SessionSearchTypoRepair } from './session-search-typo-repair' +import { + SessionSearchTypoRepair, + SessionSearchVocabularyBudget +} from './session-search-typo-repair' +import { SessionSearchPostings, SESSION_SEARCH_MESSAGE_CANDIDATES } from './session-search-postings' const RECENT_PAGE_ROWS = 512 // Leave SQLite parameter capacity for filter values as well as session IDs. @@ -21,7 +28,7 @@ const RECENT_SCAN_FACTOR = 20 const FULL_WEIGHTS = '3.0, 2.0, 1.0, 1.0' const CONVERSATION_WEIGHTS = '3.0, 2.0, 0.0, 0.0' -// Keep SQL text stable for statement-cache reuse; tests pin the projected schema. +// Keep SQL text stable for statement-cache reuse. const SESSION_COLUMN_LIST = ( [ 'id', @@ -73,39 +80,52 @@ export class SessionSearchRetrieval { this.typoRepair = new SessionSearchTypoRepair(db) } - /** Try typo repair before OR so common words cannot mask a misspelled term. */ + /** Preserve original OR hits before spending the remaining allowance on optional repair. */ run(plan: SessionSearchQueryPlan, scope: RetrievalScope): Retrieved { let incomplete = false - let sessions: SessionRow[] = [] + const sessionCache = new Map() + const postings = new SessionSearchPostings(this.db, scope.scope, (id) => { + if (!sessionCache.has(id)) { + sessionCache.set(id, this.loadSessions([id], scope)[0] ?? null) + } + return sessionCache.get(id) !== null + }) const match = (expression: string): MessageRow[] => { - const rows = this.match(expression, scope) - // Only the route returned to the caller determines whether candidates were truncated. - incomplete = rows.length >= scope.candidateLimit - sessions = this.loadSessions( - rows.map((row) => row.session_row_id), - scope - ) - const eligible = new Set(sessions.map((row) => row.id)) - return rows.filter((row) => eligible.has(row.session_row_id)) + const rows = this.match(expression, scope, postings) + incomplete ||= rows.length >= SESSION_SEARCH_MESSAGE_CANDIDATES + return rows } + const sessionsFor = (rows: MessageRow[]): SessionRow[] => + [...new Set(rows.map((row) => row.session_row_id))].flatMap( + (id) => sessionCache.get(id) ?? [] + ) const exact = this.phraseThenAnd(plan, match) if (exact) { - return { ...exact, plan, incomplete, sessions } - } - const repaired = this.repair(plan, scope.scope) - const effective = repaired ?? plan - const literal = repaired ? this.phraseThenAnd(repaired, match) : null - const found = literal ?? { - rows: match(orExpression(effective.terms)), - route: 'or' as const + return { + ...exact, + plan, + incomplete: incomplete || postings.exhausted, + sessions: sessionsFor(exact.rows) + } } + const original = { rows: match(orExpression(plan.terms)), route: 'or' as const } + const vocabulary = new SessionSearchVocabularyBudget() + const repaired = this.repair(plan, scope.scope, postings, vocabulary) + const attempted = repaired + ? (this.phraseThenAnd(repaired, match) ?? { + rows: match(orExpression(repaired.terms)), + route: 'or' as const + }) + : null + const successfulRepair = attempted && attempted.rows.length > 0 ? repaired : null + const found = successfulRepair && attempted ? attempted : original return { - sessions, + sessions: sessionsFor(found.rows), rows: found.rows, - incomplete, - route: repaired ? (`typo+${found.route}` as SessionSearchRoute) : found.route, - plan: effective, - ...(repaired ? { repairedTerms: repaired.body } : {}) + incomplete: incomplete || postings.exhausted || vocabulary.exhausted, + route: successfulRepair ? `typo+${found.route}` : found.route, + plan: successfulRepair ?? plan, + ...(successfulRepair ? { repairedTerms: successfulRepair.body } : {}) } } @@ -141,16 +161,55 @@ export class SessionSearchRetrieval { return { sessions, incomplete: incomplete || sessions.length >= scope.candidateLimit } } + /** Hydrate only the frozen slice; reapply scope and message ownership to untrusted cursor IDs. */ + hydrate(hits: readonly SessionSearchFrozenHit[], scope: RetrievalScope): RankedSession[] { + const sessions = new Map( + this.loadSessions( + hits.map((hit) => hit.s), + scope + ).map((row) => [row.id, row]) + ) + const ids = hits.flatMap((hit) => (hit.m === null ? [] : [hit.m])) + const messages = new Map() + if (ids.length) { + const rows = this.db + .prepare(`SELECT id AS rowid, session_row_id, role, ts FROM messages + WHERE id IN (${ids.map(() => '?').join(',')}) AND role != 'tool'`) + .all(...ids) + for (const row of rows) { + messages.set(Number(row.rowid), { + rowid: Number(row.rowid), + session_row_id: Number(row.session_row_id), + role: String(row.role), + ts: typeof row.ts === 'string' ? row.ts : null, + score: 0 + }) + } + } + return hits.map((hit) => { + const session = sessions.get(hit.s) + const message = hit.m === null ? null : messages.get(hit.m) + if (!session || message === undefined || (message && message.session_row_id !== hit.s)) { + throw new SessionSearchCursorError('malformed', readIndexGeneration(this.db)) + } + return { session, message, score: hit.score, duplicateCount: hit.d } + }) + } + /** Bound SQL parameters independently of the configurable candidate limit. */ private loadSessions(ids: readonly number[], scope: RetrievalScope): SessionRow[] { const rows: SessionRow[] = [] + const { conditions: predicates, values } = scope.filter.candidate for (let start = 0; start < ids.length; start += SESSION_ID_BATCH) { const batch = ids.slice(start, start + SESSION_ID_BATCH) - const conditions = [`id IN (${batch.map(() => '?').join(',')})`, ...scope.filter.conditions] + const conditions = [`id IN (${batch.map(() => '?').join(',')})`, ...predicates] const sessionRows = this.db .prepare(`SELECT ${SESSION_COLUMN_LIST} FROM sessions WHERE ${conditions.join(' AND ')}`) - .all(...batch, ...scope.filter.values) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: The private sessions schema defines SessionRow; tests compare every projected column and value with that schema. + .all(...batch, ...values) + .filter((row) => + scope.filter.matchesScope(typeof row.cwd_key === 'string' ? row.cwd_key : null) + ) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: The selected columns match the private sessions schema defining SessionRow. rows.push(...(sessionRows as SessionRow[])) } return rows.filter((row) => scope.matchesOperators(row)) @@ -158,7 +217,9 @@ export class SessionSearchRetrieval { private repair( plan: SessionSearchQueryPlan, - scope: SessionSearchScope + scope: SessionSearchScope, + postings: SessionSearchPostings, + vocabulary: SessionSearchVocabularyBudget ): SessionSearchQueryPlan | null { const typoRepair = this.typoRepair let changed = false @@ -169,7 +230,7 @@ export class SessionSearchRetrieval { return token } // Tool-only vocabulary must neither suppress nor supply a conversation-scoped repair. - const fix = typoRepair.correct(token, scope) + const fix = typoRepair.correct(token, scope, postings, vocabulary) if (fix && fix !== token.toLowerCase()) { changed = true return fix @@ -201,24 +262,35 @@ export class SessionSearchRetrieval { return and.length > 0 ? { rows: and, route: 'and' } : null } - private match(expression: string, scope: RetrievalScope): MessageRow[] { - const { filter, sort, candidateLimit } = scope - const eligible = filter.conditions.length - ? ` AND m.session_row_id IN (SELECT id FROM sessions WHERE ${filter.conditions.join(' AND ')})` - : '' - const matched = `SELECT messages_fts.rowid AS rowid, - -bm25(messages_fts, ${scopedWeights(scope.scope)}) AS score, - m.session_row_id, m.role, m.ts, s.updated_at - FROM messages_fts JOIN messages m ON m.id = messages_fts.rowid - JOIN sessions s ON s.id = m.session_row_id WHERE messages_fts MATCH ?${eligible}` - // Group before limiting so one long session cannot consume the candidate budget. - // SQLite’s max(score) selects the best matching row for the other projected columns. - const order = sort === 'newest' ? 'updated_at DESC, score DESC' : 'score DESC' - const sql = `WITH matched AS MATERIALIZED (${matched}) - SELECT rowid, max(score) AS score, session_row_id, role, ts FROM matched - GROUP BY session_row_id ORDER BY ${order} LIMIT ${candidateLimit}` - return this.db - .prepare(sql) - .all(scopedExpression(scope.scope, expression), ...filter.values) as MessageRow[] + private match( + expression: string, + scope: RetrievalScope, + postings: SessionSearchPostings + ): MessageRow[] { + const rows: MessageRow[] = [] + for (const row of postings.matching(expression)) { + rows.push(row) + if (rows.length >= SESSION_SEARCH_MESSAGE_CANDIDATES) { + break + } + } + if (!rows.length) { + return [] + } + const admitted = new Map(rows.map((row) => [row.rowid, row])) + const ids = [...admitted.keys()] + // Why: one range cursor retains BM25's corpus statistics across all admitted rows. + const scores = this.db + .prepare(`SELECT rowid, -bm25(messages_fts, ${scopedWeights(scope.scope)}) AS score + FROM messages_fts WHERE messages_fts MATCH ? + AND rowid >= CAST(? AS INTEGER) AND rowid <= CAST(? AS INTEGER)`) + .iterate(scopedExpression(scope.scope, expression), Math.min(...ids), Math.max(...ids)) + for (const score of scores) { + const row = admitted.get(Number(score.rowid)) + if (row) { + row.score = Number(score.score) + } + } + return rows } } diff --git a/src/main/ai-vault-search/session-search-root-refresh.test.ts b/src/main/ai-vault-search/session-search-root-refresh.test.ts index 29db33f8747..4d77eca8317 100644 --- a/src/main/ai-vault-search/session-search-root-refresh.test.ts +++ b/src/main/ai-vault-search/session-search-root-refresh.test.ts @@ -1,3 +1,4 @@ +import './session-search-worker.test-fixture' import { join } from 'node:path' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { SessionSearchIndexer } from './session-search-indexer' @@ -18,7 +19,7 @@ beforeEach(async () => { harness = await openSessionSearchIndexerHarness('search-root-refresh') }) afterEach(async () => { - indexer?.close() + await indexer?.close() indexer = undefined resetSessionParseCacheForTests() resetTranscriptConsumersForTests() @@ -70,7 +71,7 @@ it('does not access a closed store when pending discovery completes', async () = }) const start = indexer.start() await vi.waitFor(() => expect(resolver).toHaveBeenCalledTimes(1)) - indexer.close() + await indexer.close() pending.resolve(harness.roots) await start expect(errors).not.toHaveBeenCalled() diff --git a/src/main/ai-vault-search/session-search-row-filter.ts b/src/main/ai-vault-search/session-search-row-filter.ts index f3d047d47de..1d105e91ab0 100644 --- a/src/main/ai-vault-search/session-search-row-filter.ts +++ b/src/main/ai-vault-search/session-search-row-filter.ts @@ -1,12 +1,17 @@ import { cwdKey } from './session-search-file-records' import type { SessionSearchFilters } from './session-search-engine-types' -/** ANDed conditions and bound values for the sessions WHERE clause. */ -export type SessionRowFilter = { +type SessionRowPredicate = { conditions: string[] values: (string | number)[] } +/** Population SQL and point eligibility share all predicates and scope normalization. */ +export type SessionRowFilter = SessionRowPredicate & { + candidate: SessionRowPredicate + matchesScope: (key: string | null) => boolean +} + // Use the sidebar’s cwd identity so indexed sessions and scopes share path normalization. const CWD = 'cwd_key' @@ -15,22 +20,34 @@ export function sessionRowFilter( filters: SessionSearchFilters, cutoffMs: number | null = null ): SessionRowFilter { - const filter: SessionRowFilter = { conditions: [], values: [] } + const candidate: SessionRowPredicate = { conditions: [], values: [] } if (cutoffMs !== null) { - filter.conditions.push('id IN (SELECT session_row_id FROM files WHERE mtime_ms >= ?)') - filter.values.push(cutoffMs) + candidate.conditions.push( + 'EXISTS (SELECT 1 FROM files WHERE session_row_id = sessions.id AND mtime_ms >= ?)' + ) + candidate.values.push(cutoffMs) } if (filters.agents && filters.agents.length > 0) { - filter.conditions.push(`agent IN (${filters.agents.map(() => '?').join(',')})`) - filter.values.push(...filters.agents) + candidate.conditions.push(`agent IN (${filters.agents.map(() => '?').join(',')})`) + candidate.values.push(...filters.agents) } if (filters.since) { - filter.conditions.push('updated_at >= ?') - filter.values.push(filters.since) + candidate.conditions.push('updated_at >= ?') + candidate.values.push(filters.since) + } + const filter: SessionRowFilter = { + conditions: [...candidate.conditions], + values: [...candidate.values], + candidate, + matchesScope: () => true } if (filters.scopePaths && filters.scopePaths.length > 0) { // Unkeyable scopes must not widen a scoped query to every session. - const keys = filters.scopePaths.map(cwdKey).filter((key) => key !== null) + const keys = filters.scopePaths + .map(cwdKey) + .filter((key) => key !== null) + .map((key) => key.toWellFormed()) + filter.matchesScope = scopeMatcher(keys) if (keys.length === 0) { filter.conditions.push('0 = 1') } else { @@ -41,18 +58,40 @@ export function sessionRowFilter( return filter } -// One JSON parameter avoids OR-depth limits; exact-key and child-prefix ranges seek sessions_cwd_key. +// Why: population queries must seek the scope once, before counting accepted rows for LIMIT. const SCOPE_CONDITION = `id IN ( SELECT scoped.id FROM json_each(?) AS scope JOIN sessions AS scoped ON scoped.${CWD} = scope.value ->> 0 OR (scoped.${CWD} >= scope.value ->> 1 AND scoped.${CWD} < scope.value ->> 2) )` +function scopeMatcher(keys: readonly string[]): SessionRowFilter['matchesScope'] { + const exact = new Set(keys) + const children = new Set(keys.map(childPrefix)) + return (key) => { + if (key === null) { + return false + } + if (exact.has(key)) { + return true + } + // Why: ancestor-prefix lookups scale with this path's depth, not the number of scopes. + for (let slash = key.indexOf('/'); slash !== -1; slash = key.indexOf('/', slash + 1)) { + if (children.has(key.slice(0, slash + 1))) { + return true + } + } + return false + } +} + +function childPrefix(key: string): string { + return key.endsWith('/') ? key : `${key}/` +} + function scopeRange(scopeKey: string): [string, string, string] { - // Match SQLite’s U+FFFD encoding of lone surrogates before serializing keys through JSON. - const key = scopeKey.toWellFormed() - const children = key.endsWith('/') ? key : `${key}/` - return [key, children, nextAfterPrefix(children)] + const children = childPrefix(scopeKey) + return [scopeKey, children, nextAfterPrefix(children)] } /** Upper bound for every string starting with prefix. */ diff --git a/src/main/ai-vault-search/session-search-schema.ts b/src/main/ai-vault-search/session-search-schema.ts index 5c8ec720068..9c5e46ea64e 100644 --- a/src/main/ai-vault-search/session-search-schema.ts +++ b/src/main/ai-vault-search/session-search-schema.ts @@ -1,3 +1,4 @@ +import { removeTree } from '../../shared/windows-transient-lock-removal' import { randomUUID } from 'node:crypto' import type SyncDatabase from '../sqlite/sync-database' import { @@ -56,6 +57,7 @@ CREATE TABLE IF NOT EXISTS files( prefix_hash TEXT ); CREATE INDEX IF NOT EXISTS files_mtime ON files(mtime_ms); +CREATE INDEX IF NOT EXISTS files_session_mtime ON files(session_row_id, mtime_ms); CREATE TABLE IF NOT EXISTS messages( id INTEGER PRIMARY KEY, session_row_id INTEGER NOT NULL, @@ -79,6 +81,10 @@ export function openSessionSearchDatabase(path: string): SyncDatabase { 'index_incarnation', randomUUID() ) + db.prepare('INSERT OR IGNORE INTO meta(key, value) VALUES (?, ?)').run( + 'index_maintenance_generation', + '0' + ) // Each small commit adds an FTS segment; wider merges mean fewer levels rewriting it (#36666). db.prepare("INSERT INTO messages_fts(messages_fts, rank) VALUES ('automerge', 8)").run() // Older Cursor rows omitted the file-time fallback; repair metadata without rereading transcripts. @@ -114,3 +120,13 @@ export function addSessionSearchPrefixHashColumn(db: SyncDatabase): void { export function removeSessionSearchDatabase(path: string): void { removeVersionedCacheDatabase(path) } + +/** Async lock retries keep scanner/server deadlines responsive after all workers have retired. */ +export async function removeSessionSearchDatabaseAsync(path: string): Promise { + if (path === ':memory:') { + return + } + for (const suffix of ['', '-wal', '-shm', '-journal']) { + await removeTree(`${path}${suffix}`) + } +} diff --git a/src/main/ai-vault-search/session-search-service.test.ts b/src/main/ai-vault-search/session-search-service.test.ts index 97b43b16381..e4895fb99e1 100644 --- a/src/main/ai-vault-search/session-search-service.test.ts +++ b/src/main/ai-vault-search/session-search-service.test.ts @@ -90,7 +90,7 @@ describe('real index to public service adapter', () => { expect(AiVaultSearchStatusSchema.parse(status)).toEqual(status) expect(status.generation).toBeGreaterThan(0) }) - it('keeps tool text out of conversation scope and reports query truncation', async () => { + it('keeps tool rows out of both scopes and reports query truncation', async () => { const { db, service } = await fixture() addSyntheticSession(db, { id: 1, role: 'tool', text: 'needle' }) expect(await service.search({ query: 'needle', scope: 'conversation' })).toMatchObject({ @@ -99,7 +99,7 @@ describe('real index to public service adapter', () => { }) expect(await service.search({ query: 'needle' })).toMatchObject({ kind: 'results', - hits: [expect.anything()] + hits: [] }) expect(await service.search({ query: 'needle '.repeat(100) })).toMatchObject({ kind: 'results', diff --git a/src/main/ai-vault-search/session-search-store-types.ts b/src/main/ai-vault-search/session-search-store-types.ts new file mode 100644 index 00000000000..49e7bcbd30c --- /dev/null +++ b/src/main/ai-vault-search/session-search-store-types.ts @@ -0,0 +1,48 @@ +import type { SessionSearchStore } from './session-search-store' +import type { SessionSearchFileIdentity } from './session-search-file-cursor' + +/** Due files need a full reread; failed reads track counts and mtime to bound retries. */ +export type SessionSearchFileState = 'current' | 'due' | 'failed' + +export type SessionSearchFileRow = { + path: string + identity: SessionSearchFileIdentity + mtimeMs: number + sizeBytes: number | null + state: SessionSearchFileState + failCount: number + failedMtimeMs: number | null +} + +export type SessionSearchResumableCursor = { byteOffset: number; prefixHash: string } + +export type SessionSearchStateCounts = { + current: number + due: number + failed: number + /** Session counts distinguish indexed content from files that have only been discovered. */ + sessionsByAgent: Record + messages: number +} + +export type SessionSearchIndexStore = Pick< + SessionSearchStore, + | 'indexedFile' + | 'coversTranscript' + | 'resumableCursor' + | 'beginWrite' + | 'writeCommitted' + | 'reportWriteFailure' + | 'files' + | 'setFileState' + | 'stateCounts' + | 'removeFile' + | 'purgeOlderThan' + | 'close' + | 'retentionCutoff' + | 'setRetentionCutoffMs' +> & { + readonly acceptingReads?: boolean + prepare?(): Promise + flush?(): Promise +} diff --git a/src/main/ai-vault-search/session-search-store.ts b/src/main/ai-vault-search/session-search-store.ts index 0ce14166df1..07b99bd622d 100644 --- a/src/main/ai-vault-search/session-search-store.ts +++ b/src/main/ai-vault-search/session-search-store.ts @@ -1,3 +1,17 @@ +import { ensureSessionSearchQuerySchema } from './session-search-query-schema' +import type { + SessionSearchFileRow, + SessionSearchFileState, + SessionSearchResumableCursor, + SessionSearchStateCounts +} from './session-search-store-types' +export type { + SessionSearchFileRow, + SessionSearchFileState, + SessionSearchResumableCursor, + SessionSearchStateCounts, + SessionSearchIndexStore +} from './session-search-store-types' import type SyncDatabase from '../sqlite/sync-database' import { asRecord } from '../ai-vault/session-scanner-record-value' import type { SessionFileCandidate } from '../ai-vault/session-scanner-types' @@ -13,32 +27,9 @@ import { type SessionSearchFileWrite } from './session-search-index-writer' import { deleteExpiredSearchFiles, drainOrphanedMessages } from './session-search-retention-delete' +import { reportSessionSearchBackgroundError } from './session-search-trace' import { openSessionSearchDatabase } from './session-search-schema' -/** Due files need a full reread; failed reads track counts and mtime to bound retries. */ -export type SessionSearchFileState = 'current' | 'due' | 'failed' - -export type SessionSearchFileRow = { - path: string - identity: SessionSearchFileIdentity - mtimeMs: number - sizeBytes: number | null - state: SessionSearchFileState - failCount: number - failedMtimeMs: number | null -} - -export type SessionSearchResumableCursor = { byteOffset: number; prefixHash: string } - -export type SessionSearchStateCounts = { - current: number - due: number - failed: number - /** Session counts distinguish indexed content from files that have only been discovered. */ - sessionsByAgent: Record - messages: number -} - export class SessionSearchStore { private readonly db: SyncDatabase private readonly writer: SessionSearchIndexWriter @@ -54,11 +45,16 @@ export class SessionSearchStore { console.warn( '[ai-vault-search] index write failed:', error instanceof Error ? error.name : 'IndexError' - ) + ), + private readonly onMessagesChanged: (delta: number) => void = () => undefined ) { this.db = openSessionSearchDatabase(path) - this.writer = new SessionSearchIndexWriter(this.db, SESSION_SEARCH_COMMIT_CHARS, () => - this.scheduleOrphanDrain() + ensureSessionSearchQuerySchema(this.db) + this.writer = new SessionSearchIndexWriter( + this.db, + SESSION_SEARCH_COMMIT_CHARS, + () => this.scheduleOrphanDrain(), + onMessagesChanged ) } @@ -77,11 +73,11 @@ export class SessionSearchStore { try { while (this.drainRequested && !this.closed) { this.drainRequested = false - await drainOrphanedMessages(this.db, () => this.closed) + await drainOrphanedMessages(this.db, () => this.closed, undefined, this.onMessagesChanged) } } catch (error) { if (!this.closed) { - this.onError(error) + reportSessionSearchBackgroundError(this.onError, error) } } finally { this.draining = false @@ -101,11 +97,6 @@ export class SessionSearchStore { return this.retentionCutoffMs } - /** Sidebar reads also reach the consumer, so enforce retention beyond the indexer’s own decisions. */ - private withinRetention(candidate: SessionFileCandidate): boolean { - return this.retentionCutoffMs === null || candidate.file.mtimeMs >= this.retentionCutoffMs - } - indexedFile(path: string, identity: SessionSearchFileIdentity): SessionSearchIndexedFile | null { try { return this.writer.indexedFile(path, identity) @@ -183,9 +174,14 @@ export class SessionSearchStore { candidate: SessionFileCandidate, mode: 'replace' | 'append', previousByteOffset: number, - identity?: () => TranscriptSessionIdentity | null + identity?: () => TranscriptSessionIdentity | null, + _indexing?: boolean, + _signal?: AbortSignal ): SessionSearchFileWrite | null { - if (this.closed || !this.withinRetention(candidate)) { + if ( + this.closed || + (this.retentionCutoffMs !== null && candidate.file.mtimeMs < this.retentionCutoffMs) + ) { return null } try { @@ -274,7 +270,9 @@ export class SessionSearchStore { for (const row of rows) { counts[row.state] = Number(row.n) } - counts.messages = this.messageCount() + counts.messages = Number( + asRecord(this.db.prepare('SELECT count(*) AS n FROM messages').get())?.n ?? 0 + ) return counts } @@ -292,14 +290,6 @@ export class SessionSearchStore { return counts } - private messageCount(): number { - const row: unknown = this.db.prepare('SELECT count(*) AS n FROM messages').get() - if (row && typeof row === 'object' && 'n' in row && typeof row.n === 'number') { - return row.n - } - return 0 - } - /** Only proven deletion permits removal; unreadable sources retain their indexed rows. */ removeFile(path: string): void { try { @@ -314,8 +304,12 @@ export class SessionSearchStore { await deleteExpiredSearchFiles( this.db, cutoffMs, - () => this.closed || signal?.aborted === true + () => this.closed || signal?.aborted === true, + undefined, + this.onMessagesChanged, + false ) + this.scheduleOrphanDrain() } catch (error) { if (!this.closed) { this.onError(error) diff --git a/src/main/ai-vault-search/session-search-synthetic-corpus.test.ts b/src/main/ai-vault-search/session-search-synthetic-corpus.test.ts index c75fcb97196..a1ff461b6e5 100644 --- a/src/main/ai-vault-search/session-search-synthetic-corpus.test.ts +++ b/src/main/ai-vault-search/session-search-synthetic-corpus.test.ts @@ -31,7 +31,7 @@ it.each([0, 200, 2000])( await parseTranscript(corpus.files[0]!) expect(corpus.messageCount).toBe(toolResultWords === 0 ? 3 : 4) expect(store.connection.prepare('SELECT count(*) AS n FROM messages').get()).toEqual({ - n: corpus.messageCount + n: 2 }) } finally { unregister() diff --git a/src/main/ai-vault-search/session-search-tool-purge.test.ts b/src/main/ai-vault-search/session-search-tool-purge.test.ts new file mode 100644 index 00000000000..2ef3ea2bb62 --- /dev/null +++ b/src/main/ai-vault-search/session-search-tool-purge.test.ts @@ -0,0 +1,124 @@ +import { expect, it } from 'vitest' +import { addSyntheticSession, openSessionSearchHarness } from './session-search-engine-test-fixture' +import { readIndexContentRevision, readIndexGeneration } from './session-search-index-generation' +import { encodeSessionSearchCursor, sessionSearchPageKey } from './session-search-page-cursor' +import { SessionSearchToolPurge, readSessionSearchToolPurgePage } from './session-search-tool-purge' + +it('purges legacy tool rows across batches while preserving conversations and raw generation fences', async () => { + const index = await openSessionSearchHarness('ss-tool-purge') + try { + addSyntheticSession(index.db, { id: 1, text: 'userkept' }) + addSyntheticSession(index.db, { id: 2, role: 'assistant', text: 'assistantkept' }) + addSyntheticSession(index.db, { id: 3, role: 'tool', text: 'toolremoved', rows: 600 }) + const before = readIndexGeneration(index.db) + expect(index.engine.search({ query: 'toolremoved' }).hits).toHaveLength(0) + let messages = 602 + const errors: unknown[] = [] + const purge = new SessionSearchToolPurge( + index.db, + () => false, + (delta) => { + messages += delta + }, + (error) => errors.push(error) + ) + await purge.request() + expect(errors).toEqual([]) + expect(index.engine.search({ query: 'toolremoved' }).hits).toEqual([]) + expect(index.engine.search({ query: 'userkept' }).hits[0]?.sessionId).toBe('1') + expect(index.engine.search({ query: 'assistantkept' }).hits[0]?.sessionId).toBe('2') + expect(messages).toBe(2) + expect(readIndexGeneration(index.db)).toBeGreaterThan(before) + addSyntheticSession(index.db, { id: 4, role: 'tool', text: 'newlegacytool' }) + await purge.request() + expect(index.engine.search({ query: 'newlegacytool' }).hits).toEqual([]) + } finally { + await index.close() + } +}) + +it('bounds empty conversation pages and seeks across sparse rowid gaps', async () => { + const index = await openSessionSearchHarness('ss-tool-purge-sparse') + try { + addSyntheticSession(index.db, { id: 1, text: 'kept', rows: 300 }) + addSyntheticSession(index.db, { id: 2, text: 'legacytool', role: 'tool' }) + index.db.exec('UPDATE messages SET id = 1000000 WHERE id = 301') + const first = readSessionSearchToolPurgePage(index.db, 0) + expect(first).toEqual({ cursor: 256, ids: [] }) + expect(readSessionSearchToolPurgePage(index.db, 256)).toEqual({ cursor: 512, ids: [] }) + expect(readSessionSearchToolPurgePage(index.db, 512)).toEqual({ + cursor: 1000255, + ids: [{ id: 1000000 }] + }) + expect(readSessionSearchToolPurgePage(index.db, 1000255)).toBeNull() + } finally { + await index.close() + } +}) + +it.each(['relevance', 'newest'] as const)( + 'continues frozen %s ordering and fork folding throughout tool cleanup', + async (sort) => { + const index = await openSessionSearchHarness('ss-frozen-purge') + try { + for (let id = 1; id <= 7; id++) { + addSyntheticSession(index.db, { + id, + text: 'needle', + messageCount: id, + updatedAt: `2026-09-0${id}T00:00:00.000Z` + }) + } + index.db.exec( + "UPDATE sessions SET content_hash = 'shared', content_hash_count = 8 WHERE id IN (2,7)" + ) + addSyntheticSession(index.db, { + id: 20, + role: 'tool', + text: 'needle noisy words', + rows: 2500 + }) + // Orphan deletes fire generation triggers in addition to the purge's explicit bump. + index.db.exec('DELETE FROM sessions WHERE id = 20') + const before = readIndexContentRevision(index.db) + const request = { query: 'needle', limit: 1, filters: { sort } } + const first = index.engine.search(request) + const incarnation = String( + index.db.prepare("SELECT value FROM meta WHERE key = 'index_incarnation'").get()!.value + ) + const legacy = encodeSessionSearchCursor( + first.generation, + 1, + sessionSearchPageKey(request), + incarnation + ) + const seen = [...first.hits] + let cursor = first.page.cursor + const errors: unknown[] = [] + await new SessionSearchToolPurge( + index.db, + () => false, + () => { + if (cursor) { + const page = index.engine.search({ ...request, cursor }) + expect(page.generation).toBe(first.generation) + seen.push(...page.hits) + cursor = page.page.cursor + } + }, + (error) => errors.push(error) + ).request() + expect(errors).toEqual([]) + expect(cursor).toBeNull() + expect(seen.map((hit) => hit.sessionId)).toEqual( + sort === 'newest' ? ['7', '6', '5', '4', '3', '1'] : ['1', '3', '4', '5', '6', '7'] + ) + expect(seen.find((hit) => hit.sessionId === '7')?.duplicateCount).toBe(2) + expect(readIndexContentRevision(index.db)).toBe(before) + expect(readIndexGeneration(index.db)).toBeGreaterThan(first.generation) + expect(() => index.engine.search({ ...request, cursor: legacy })).toThrow('stale-generation') + } finally { + await index.close() + } + } +) diff --git a/src/main/ai-vault-search/session-search-tool-purge.ts b/src/main/ai-vault-search/session-search-tool-purge.ts new file mode 100644 index 00000000000..a29eb585580 --- /dev/null +++ b/src/main/ai-vault-search/session-search-tool-purge.ts @@ -0,0 +1,129 @@ +import { readIndexGeneration, recordIndexMaintenance } from './session-search-index-generation' +import { setImmediate as yieldToEventLoop } from 'node:timers/promises' +import type SyncDatabase from '../sqlite/sync-database' +import type { SqliteRow } from '../sqlite/sqlite-statement' +import { reportSessionSearchBackgroundError, traceSessionSearch } from './session-search-trace' + +/** Seek over gaps, then examine at most 256 rowids, including conversation rows in the bound. */ +export function readSessionSearchToolPurgePage(db: SyncDatabase, after: number) { + const first = db.prepare('SELECT id FROM messages WHERE id > ? ORDER BY id LIMIT 1').get(after) + if (!first) { + return null + } + const cursor = Number(first.id) + 255 + const ids = db + .prepare("SELECT id FROM messages WHERE id > ? AND id <= ? AND role = 'tool' ORDER BY id") + .all(Number(first.id) - 1, cursor) + return { cursor, ids } +} + +/** The rows themselves record unfinished work; no persisted migration cursor is needed. */ +export class SessionSearchToolPurge { + private requested = false + private running: Promise | null = null + + constructor( + private readonly db: SyncDatabase, + private readonly closed: () => boolean, + private readonly onMessagesChanged: (delta: number) => void, + private readonly onError: (error: unknown) => void + ) {} + + request(): Promise { + this.requested = true + if (!this.running) { + this.running = this.run().finally(() => { + this.running = null + }) + } + return this.running + } + + private async run(): Promise { + try { + // Why: opening the writer and publishing its snapshot must not wait for maintenance. + await yieldToEventLoop() + while (this.requested && !this.closed()) { + this.requested = false + let cursor = 0 + let changed = false + while (!this.closed()) { + // Why: sparse/empty pages must yield too, without acquiring the write lock to scan. + const page = readSessionSearchToolPurgePage(this.db, cursor) + if (!page) { + break + } + if (page.ids.length) { + const deleted = traceSessionSearch('purge', () => this.deletePage(page.ids)) + this.onMessagesChanged(-deleted) + changed ||= deleted > 0 + } + cursor = page.cursor + await yieldToEventLoop() + } + if (changed && !this.closed()) { + await this.compact() + } + } + } catch (error) { + if (!this.closed()) { + reportSessionSearchBackgroundError(this.onError, error) + } + } + } + + private deletePage(ids: SqliteRow[]): number { + this.db.exec('BEGIN IMMEDIATE') + try { + const before = readIndexGeneration(this.db) + const full = this.db.prepare('DELETE FROM messages_fts WHERE rowid = ?') + const message = this.db.prepare("DELETE FROM messages WHERE id = ? AND role = 'tool'") + let deleted = 0 + for (const { id } of ids) { + // A foreign writer may have replaced a row after the deferred scan. + if (message.run(Number(id)).changes) { + full.run(Number(id)) + deleted++ + } + } + if (deleted) { + this.db.exec(`INSERT INTO meta(key, value) VALUES ('index_generation', '1') + ON CONFLICT(key) DO UPDATE SET value = CAST(value AS INTEGER) + 1`) + } + recordIndexMaintenance(this.db, before) + this.db.exec('COMMIT') + return deleted + } catch (error) { + this.db.exec('ROLLBACK') + throw error + } + } + + private async compact(): Promise { + const changes = this.db.prepare('SELECT total_changes() AS n') + const merge = this.db.prepare("INSERT INTO messages_fts(messages_fts, rank) VALUES('merge', ?)") + // Why: negative starts a two-segment merge; positive steps continue it within the page budget. + let pages = -64 + while (!this.closed()) { + const before = Number(changes.get()!.n) + traceSessionSearch('merge', () => merge.run(pages)) + const done = Number(changes.get()!.n) - before < 2 + await yieldToEventLoop() + if (done) { + break + } + pages = 64 + } + // Reclaim pages freed by merging only after the final merge step has yielded. + let free = this.closed() ? 0 : Number(this.db.pragma('freelist_count', { simple: true })) + while (free > 0 && !this.closed()) { + traceSessionSearch('vacuum', () => this.db.pragma('incremental_vacuum(2000)')) + const remaining = Number(this.db.pragma('freelist_count', { simple: true })) + await yieldToEventLoop() + if (remaining >= free) { + break + } + free = remaining + } + } +} diff --git a/src/main/ai-vault-search/session-search-trace.ts b/src/main/ai-vault-search/session-search-trace.ts new file mode 100644 index 00000000000..56126aa45a0 --- /dev/null +++ b/src/main/ai-vault-search/session-search-trace.ts @@ -0,0 +1,31 @@ +import { startSpan } from '../observability/tracer' + +export function traceSessionSearch( + name: string, + run: () => T, + attributes?: Record +): T { + const span = startSpan(`aiVault.search.${name}`, { attributes }) + try { + const result = run() + if (typeof result === 'boolean') { + span.setAttribute('committed', result) + } + span.end() + return result + } catch (error) { + span.fail(error instanceof Error ? error : String(error)) + throw error + } +} + +export function reportSessionSearchBackgroundError( + report: (error: unknown) => void, + error: unknown +): void { + try { + report(error) + } catch { + // Why: reporting a background failure must not terminate the writer. + } +} diff --git a/src/main/ai-vault-search/session-search-typo-repair.ts b/src/main/ai-vault-search/session-search-typo-repair.ts index 8981c69a630..7577d83121f 100644 --- a/src/main/ai-vault-search/session-search-typo-repair.ts +++ b/src/main/ai-vault-search/session-search-typo-repair.ts @@ -1,17 +1,34 @@ import type SyncDatabase from '../sqlite/sync-database' import type { SessionSearchScope } from './session-search-engine-types' -import { quoteFtsTerm, scopedExpression } from './session-search-query-planner' +import { orExpression, planSessionSearchQuery, quoteFtsTerm } from './session-search-query-planner' +import { SessionSearchPostings } from './session-search-postings' const MIN_TERM_LENGTH = 4 const MAX_TERM_LENGTH = 40 const LENGTH_SLACK = 2 const MIN_DOC_FREQUENCY = 2 const MIN_SIMILARITY = 0.82 -const MAX_CANDIDATES = 4000 +const MAX_VOCABULARY_SEEKS = 4000 const MAX_VISIBILITY_PROBES = 8 // Cap frequency tie-break counts so common terms cannot make repair unbounded. const MAX_COUNTED_ROWS = 64 +export class SessionSearchVocabularyBudget { + private remaining = MAX_VOCABULARY_SEEKS + + get exhausted(): boolean { + return this.remaining === 0 + } + + take(): boolean { + if (this.exhausted) { + return false + } + this.remaining-- + return true + } +} + function commonSubsequenceLength(a: string, b: string): number { let previous = Array.from({ length: b.length + 1 }).fill(0) let current = Array.from({ length: b.length + 1 }).fill(0) @@ -35,46 +52,48 @@ function similarity(a: string, b: string): number { /** Validate against live scoped rows: vocabulary includes other columns and orphans. */ export class SessionSearchTypoRepair { - private readonly liveRows: ReturnType private readonly candidatesByPrefix: ReturnType - constructor(db: SyncDatabase) { - this.liveRows = db.prepare( - `SELECT count(*) AS rows FROM ( - SELECT m.id FROM messages_fts - JOIN messages m ON m.id = messages_fts.rowid - JOIN sessions s ON s.id = m.session_row_id - WHERE messages_fts MATCH ? LIMIT ${MAX_COUNTED_ROWS})` + constructor(private readonly db: SyncDatabase) { + // Why: row vocabulary computes full doclist counts before returning even one term. + db.exec( + "CREATE VIRTUAL TABLE IF NOT EXISTS temp.session_search_vocab USING fts5vocab(main,messages_fts,'instance')" ) - // Term order uses the vocab index and keeps the candidate cutoff independent of orphan counts. this.candidatesByPrefix = db.prepare( - `SELECT term FROM messages_vocab - WHERE term >= ? AND term < ? AND length(term) BETWEEN ? AND ? - ORDER BY term LIMIT ?` + `SELECT term FROM temp.session_search_vocab WHERE term >= ? ORDER BY term LIMIT 1` ) } - private countRows(term: string, scope: SessionSearchScope): number { - const row = this.liveRows.get(scopedExpression(scope, quoteFtsTerm(term))) as { rows: number } - return row.rows + private countRows(expression: string, postings: SessionSearchPostings): number { + let rows = 0 + for (const _row of postings.matching(expression)) { + if (++rows >= MAX_COUNTED_ROWS) { + break + } + } + return rows } - hasPostings(term: string, scope: SessionSearchScope): boolean { - return this.countRows(term, scope) > 0 - } - - correct(term: string, scope: SessionSearchScope): string | null { + correct( + term: string, + scope: SessionSearchScope, + postings = new SessionSearchPostings(this.db, scope), + vocabulary = new SessionSearchVocabularyBudget() + ): string | null { const lowered = term.toLowerCase() if (lowered.length < MIN_TERM_LENGTH || lowered.length > MAX_TERM_LENGTH) { return null } - if (this.hasPostings(lowered, scope)) { + const ownExpression = orExpression( + planSessionSearchQuery(term).terms.filter((spelling) => spelling.toLowerCase() === lowered) + ) + if (this.countRows(ownExpression, postings) > 0 || postings.exhausted) { return null } // Widen cautiously: common prefix, transposed pair, then first letter. const prefixes = [lowered.slice(0, 2), lowered[1] + lowered[0], lowered[0]] for (const prefix of prefixes) { - const best = this.bestVisible(lowered, prefix, scope) + const best = this.bestVisible(lowered, prefix, postings, vocabulary) if (best) { return best } @@ -83,10 +102,18 @@ export class SessionSearchTypoRepair { } /** Probe several ranked candidates so an invisible best match does not suppress a live repair. */ - private bestVisible(lowered: string, prefix: string, scope: SessionSearchScope): string | null { - const counted = this.ranked(lowered, prefix) + private bestVisible( + lowered: string, + prefix: string, + postings: SessionSearchPostings, + vocabulary: SessionSearchVocabularyBudget + ): string | null { + const counted = this.ranked(lowered, prefix, vocabulary) .slice(0, MAX_VISIBILITY_PROBES) - .map((candidate) => ({ ...candidate, rows: this.countRows(candidate.term, scope) })) + .map((candidate) => ({ + ...candidate, + rows: this.countRows(quoteFtsTerm(candidate.term), postings) + })) .filter((candidate) => candidate.rows >= MIN_DOC_FREQUENCY) if (counted.length === 0) { return null @@ -96,22 +123,39 @@ export class SessionSearchTypoRepair { .term } - private ranked(lowered: string, prefix: string): { term: string; score: number }[] { - return this.candidates(prefix, lowered.length) + private ranked( + lowered: string, + prefix: string, + vocabulary: SessionSearchVocabularyBudget + ): { term: string; score: number }[] { + return this.candidates(prefix, lowered.length, vocabulary) .map((row) => ({ term: row.term, score: similarity(lowered, row.term) })) .filter((candidate) => candidate.score >= MIN_SIMILARITY) .sort((left, right) => right.score - left.score || (left.term < right.term ? -1 : 1)) } - private candidates(prefix: string, length: number): { term: string }[] { - const last = prefix.charCodeAt(prefix.length - 1) - const upper = prefix.slice(0, -1) + String.fromCharCode(last + 1) - return this.candidatesByPrefix.all( - prefix, - upper, - Math.max(MIN_TERM_LENGTH - 1, length - LENGTH_SLACK), - length + LENGTH_SLACK, - MAX_CANDIDATES - ) as { term: string }[] + private candidates( + prefix: string, + length: number, + vocabulary: SessionSearchVocabularyBudget + ): { term: string }[] { + const terms: { term: string }[] = [] + let next = prefix + while (vocabulary.take()) { + const term = this.candidatesByPrefix.get(next)?.term + if (typeof term !== 'string' || !term.startsWith(prefix)) { + break + } + // Why: strict > filters occurrences; the TEXT successor seeks past the whole doclist. + next = `${term}\u0000` + const termLength = Array.from(term).length + if ( + termLength >= Math.max(MIN_TERM_LENGTH - 1, length - LENGTH_SLACK) && + termLength <= length + LENGTH_SLACK + ) { + terms.push({ term }) + } + } + return terms } } diff --git a/src/main/ai-vault-search/session-search-typo-scope.test.ts b/src/main/ai-vault-search/session-search-typo-scope.test.ts index 14343abb303..c5cf48b530e 100644 --- a/src/main/ai-vault-search/session-search-typo-scope.test.ts +++ b/src/main/ai-vault-search/session-search-typo-scope.test.ts @@ -15,12 +15,12 @@ afterEach(async () => { control = null }) -it('repairs a conversation query the same way with or without a tool row', async () => { +it('repairs whole identifiers despite visible fragments and tool spellings', async () => { harness = await openSessionSearchHarness('ss-typo-scope-suppress') addSyntheticSession(harness.db, { id: 1, text: 'we changed resolveTerminalPath today', rows: 2 }) addSyntheticSession(harness.db, { id: 2, - text: 'ran the linter', + text: 'terminal', toolText: 'warning: unknown symbol resolveterminalpth in build log', rows: 2, role: 'assistant' @@ -28,16 +28,16 @@ it('repairs a conversation query the same way with or without a tool row', async control = await openSessionSearchHarness('ss-typo-scope-control') addSyntheticSession(control.db, { id: 1, text: 'we changed resolveTerminalPath today', rows: 2 }) - addSyntheticSession(control.db, { id: 2, text: 'ran the linter' }) + addSyntheticSession(control.db, { id: 2, text: 'terminal' }) - const request = { query: 'resolveterminalpth', scope: 'conversation' } as const - const withTool = harness.engine.search(request) - const clean = control.engine.search(request) - - expect(clean.planner.repairedTerms).toEqual(['resolveterminalpath']) - expect(clean.hits.map((hit) => hit.sessionId)).toEqual(['1']) - expect(withTool.planner.repairedTerms).toEqual(clean.planner.repairedTerms) - expect(withTool.hits.map((hit) => hit.sessionId)).toEqual(clean.hits.map((hit) => hit.sessionId)) + for (const query of ['resolveterminalpth', 'resolveTerminalPth']) { + const withTool = harness.engine.search({ query, scope: 'conversation' }) + const clean = control.engine.search({ query, scope: 'all' }) + for (const result of [withTool, clean]) { + expect(result.planner.repairedTerms).toEqual(['resolveterminalpath']) + expect(result.hits.map((hit) => hit.sessionId)).toEqual(['1']) + } + } }) it('never repairs a conversation query onto a word only tool output holds', async () => { @@ -58,3 +58,24 @@ it('never repairs a conversation query onto a word only tool output holds', asyn expect(wide.planner.repairedTerms).toEqual(['quicksilverfox']) expect(wide.hits.map((hit) => hit.sessionId)).toEqual(['1']) }) + +it('excludes tool-only spellings before typo visibility and candidate selection in all scope', async () => { + harness = await openSessionSearchHarness('ss-typo-tool-exclusion') + addSyntheticSession(harness.db, { id: 1, text: 'quicksilverfox', rows: 2 }) + addSyntheticSession(harness.db, { id: 2, role: 'tool', text: 'quicksilverfx', rows: 10 }) + const page = harness.engine.search({ query: 'quicksilverfx', scope: 'all' }) + expect(page.planner.repairedTerms).toEqual(['quicksilverfox']) + expect(page.hits.map((hit) => hit.sessionId)).toEqual(['1']) +}) + +it('keeps an exact spelling in a tool-heavy vocabulary', async () => { + harness = await openSessionSearchHarness('ss-typo-exhausted-exact') + addSyntheticSession(harness.db, { id: 1, role: 'tool', text: 'coalesces', rows: 1023 }) + addSyntheticSession(harness.db, { id: 2, text: 'coalesces', rows: 2 }) + addSyntheticSession(harness.db, { id: 3, text: 'coalesce', rows: 2 }) + for (const scope of ['all', 'conversation'] as const) { + const page = harness.engine.search({ query: 'coalesces', scope }) + expect(page.planner.repairedTerms).toBeUndefined() + expect(page.hits.map((hit) => hit.sessionId)).toEqual(['2']) + } +}) diff --git a/src/main/ai-vault-search/session-search-worker-client.test.ts b/src/main/ai-vault-search/session-search-worker-client.test.ts new file mode 100644 index 00000000000..89753c38b24 --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker-client.test.ts @@ -0,0 +1,38 @@ +import { EventEmitter } from 'node:events' +import { expect, it } from 'vitest' +import { SessionSearchWorkerClient } from './session-search-worker-client' +import type { SearchWorkerRequest } from './session-search-worker-protocol' + +class UnansweredWorker extends EventEmitter { + readonly received: SearchWorkerRequest[] = [] + unref(): void {} + postMessage(request: SearchWorkerRequest): void { + this.received.push(request) + } + terminate(): Promise { + return Promise.resolve(0) + } + answerAll(): void { + for (const { id } of this.received.splice(0)) { + this.emit('message', { id }) + } + } +} + +it('holds writer commands past the in-flight cap and sends them in order once the worker answers', async () => { + const worker = new UnansweredWorker() + const client = new SessionSearchWorkerClient(undefined, () => worker, true) + const replies = Array.from({ length: 1100 }, (_, index) => + client.request({ op: 'remove', path: `/sessions/${index}.jsonl` }) + ) + expect(worker.received).toHaveLength(1024) + worker.answerAll() + expect(worker.received.map(({ command }) => command)).toEqual( + Array.from({ length: 76 }, (_, index) => ({ + op: 'remove', + path: `/sessions/${1024 + index}.jsonl` + })) + ) + worker.answerAll() + await expect(Promise.all(replies)).resolves.toHaveLength(1100) +}) diff --git a/src/main/ai-vault-search/session-search-worker-client.ts b/src/main/ai-vault-search/session-search-worker-client.ts new file mode 100644 index 00000000000..fb4a9655c1b --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker-client.ts @@ -0,0 +1,124 @@ +import { recordRelayedSpan } from '../observability/tracer' +import { existsSync } from 'node:fs' +import { LazyWorkerThreadHost } from '../worker-threads/lazy-worker-thread-host' +import { spawnWorkerThread } from '../worker-threads/worker-thread-spawn' +import { + currentWorkerEntryLayout, + resolveWorkerThreadEntryPath +} from '../worker-threads/worker-thread-entry-path' +import type { WorkerThreadFactory } from '../worker-threads/worker-thread-transport' +import type { SearchWorkerCommand, SearchWorkerResponse } from './session-search-worker-protocol' + +export function sessionSearchWorkerFactory() { + const path = resolveWorkerThreadEntryPath( + currentWorkerEntryLayout(__dirname), + 'session-search-worker-entry.js' + ) + if (!existsSync(path)) { + throw new Error(`Session search worker entry not found: ${path}`) + } + return spawnWorkerThread(path, { resourceLimits: { maxOldGenerationSizeMb: 256 } }) +} + +export class SessionSearchOperationError extends Error {} + +const MAX_PENDING_REQUESTS = 1024 + +type PendingCall = { resolve(value: SearchWorkerResponse): void; reject(error: Error): void } + +/** Why: a failed port invalidates every staged handle; none can resume on a replacement. */ +export class SessionSearchWorkerClient { + private nextId = 0 + private readonly pending = new Map() + // Writer commands wait for room instead of failing, so a burst can't stop the writer. + private readonly held: { command: SearchWorkerCommand; call: PendingCall }[] = [] + private readonly host: LazyWorkerThreadHost + private retirement = Promise.resolve() + epoch = 0 + + constructor( + private readonly onChange: (value: SearchWorkerResponse) => void = () => undefined, + factory: WorkerThreadFactory = sessionSearchWorkerFactory, + private readonly holdWhenFull = false + ) { + this.host = new LazyWorkerThreadHost({ + factory, + idleTeardownMs: 0, + awaitRetirement: true, + isIdle: () => this.pending.size === 0 && this.held.length === 0, + onMessage: (value) => { + if (value.trace) { + recordRelayedSpan(value.trace) + return + } + this.onChange(value) + const call = this.pending.get(value.id) + if (!call) { + return + } + this.pending.delete(value.id) + this.postHeld() + if (value.error && !value.failure) { + call.reject(new SessionSearchOperationError(value.error)) + } else { + call.resolve(value) + } + }, + onError: (error) => this.stop(error), + onExit: () => this.stop(new Error('Session search worker exited')), + onUnavailable: () => undefined + }) + } + get retiring(): boolean { + return this.host.isRetiring + } + get busy(): boolean { + return this.pending.size > 0 || this.held.length > 0 + } + async retired(): Promise { + await this.retirement + } + request(command: SearchWorkerCommand): Promise { + if (this.holdWhenFull && (this.held.length || this.pending.size >= MAX_PENDING_REQUESTS)) { + return new Promise((resolve, reject) => + this.held.push({ command, call: { resolve, reject } }) + ) + } + if (this.pending.size >= MAX_PENDING_REQUESTS) { + return Promise.reject(new Error('Session search worker queue is full')) + } + return new Promise((resolve, reject) => this.post(command, { resolve, reject })) + } + private postHeld(): void { + while (this.held.length && this.pending.size < MAX_PENDING_REQUESTS) { + const next = this.held.shift()! + this.post(next.command, next.call) + } + } + private post(command: SearchWorkerCommand, call: PendingCall): void { + const worker = this.host.ensure() + if (!worker) { + call.reject(new Error('Session search worker is unavailable')) + return + } + const id = ++this.nextId + this.pending.set(id, call) + try { + worker.postMessage({ id, command }) + } catch (error) { + this.stop(error instanceof Error ? error : new Error('Session search transfer failed')) + } + } + stop(error = new Error('Session search read cancelled')): void { + this.epoch++ + for (const call of this.pending.values()) { + call.reject(error) + } + this.pending.clear() + for (const { call } of this.held.splice(0)) { + call.reject(error) + } + this.host.destroy() + this.retirement = this.host.retired() + } +} diff --git a/src/main/ai-vault-search/session-search-worker-entry.ts b/src/main/ai-vault-search/session-search-worker-entry.ts new file mode 100644 index 00000000000..665eece97b2 --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker-entry.ts @@ -0,0 +1,296 @@ +import { SessionSearchToolPurge } from './session-search-tool-purge' +import { readIndexGeneration } from './session-search-index-generation' +import { parentPort } from 'node:worker_threads' +import SyncDatabase from '../sqlite/sync-database' +import { withSpan, setActiveSink } from '../observability/tracer' +import { SessionSearchStore, type SessionSearchStateCounts } from './session-search-store' +import { SessionSearchEngine } from './session-search-engine' +import { createSessionSearchService } from './session-search-service' +import type { SessionSearchFileWrite } from './session-search-index-writer' +import type { TranscriptSessionIdentity } from '../ai-vault/transcripts/session-transcript-consumers' +import type { + SearchWorkerRequest, + SearchWorkerResponse, + SessionSearchSnapshotRow +} from './session-search-worker-protocol' + +setActiveSink({ + push: (trace) => parentPort?.postMessage({ id: 0, trace }), + flush() {}, + close() {} +}) + +let store: SessionSearchStore | null = null +let toolPurge: SessionSearchToolPurge | null = null +let reader: SyncDatabase | null = null +let engine: SessionSearchEngine | null = null +let dataVersion: unknown +let counts: SessionSearchStateCounts = { + current: 0, + due: 0, + failed: 0, + messages: 0, + sessionsByAgent: {} +} +const files = new Map() +const writes = new Map< + number, + { write: SessionSearchFileWrite; identity: TranscriptSessionIdentity | null } +>() + +function rows(path?: string): SessionSearchSnapshotRow[] { + if (!store) { + return [] + } + return store.connection + .prepare(`SELECT f.path, CAST(f.dev AS REAL) AS dev, CAST(f.ino AS REAL) AS ino, + f.mtime_ms, f.size_bytes, f.state, f.fail_count, f.failed_mtime_ms, f.byte_offset, f.prefix_hash, + s.agent, s.content_hash, s.content_hash_count FROM files f LEFT JOIN sessions s ON s.id=f.session_row_id + ${path === undefined ? '' : 'WHERE f.path = ?'}`) + .all(...(path === undefined ? [] : [path])) + .map((row) => ({ + path: String(row.path), + identity: + typeof row.dev === 'number' && typeof row.ino === 'number' + ? { dev: row.dev, ino: row.ino } + : null, + mtimeMs: Number(row.mtime_ms), + sizeBytes: row.size_bytes === null ? null : Number(row.size_bytes), + state: row.state === 'due' ? 'due' : row.state === 'failed' ? 'failed' : 'current', + failCount: Number(row.fail_count), + failedMtimeMs: row.failed_mtime_ms === null ? null : Number(row.failed_mtime_ms), + byteOffset: row.byte_offset === -1 ? null : Number(row.byte_offset), + prefixHash: typeof row.prefix_hash === 'string' ? row.prefix_hash : null, + agent: typeof row.agent === 'string' ? row.agent : null, + contentHash: { + hash: typeof row.content_hash === 'string' ? row.content_hash : null, + count: Number(row.content_hash_count ?? 0) + } + })) +} +function snapshot(): SessionSearchSnapshotRow[] { + if (!store) { + return [] + } + const db = store.connection + const observed = db.pragma('data_version', { simple: true }) + db.exec('BEGIN') + try { + const snapshot = rows() + counts = store.stateCounts() + db.exec('COMMIT') + files.clear() + for (const row of snapshot) { + files.set(row.path, row) + } + dataVersion = observed + return snapshot + } catch (error) { + db.exec('ROLLBACK') + throw error + } +} +function delta(path: string) { + const previous = files.get(path) + const row = rows(path)[0] ?? null + if (previous) { + counts[previous.state]-- + if (previous.agent) { + counts.sessionsByAgent[previous.agent]-- + } + } + if (row) { + counts[row.state]++ + if (row.agent) { + counts.sessionsByAgent[row.agent] = (counts.sessionsByAgent[row.agent] ?? 0) + 1 + } + files.set(path, row) + } else { + files.delete(path) + } + for (const agent of Object.keys(counts.sessionsByAgent)) { + if (!counts.sessionsByAgent[agent]) { + delete counts.sessionsByAgent[agent] + } + } + return { path, row } +} +async function execute({ id, command: c }: SearchWorkerRequest): Promise { + if (c.op === 'open') { + if (c.reader) { + reader = new SyncDatabase(c.path, { readonly: true, fileMustExist: true }) + engine = new SessionSearchEngine(reader, { retentionCutoffMs: c.cutoff }) + reader.pragma('query_only = ON') + return { id } + } + const onMessagesChanged = (n: number): void => { + counts.messages += n + parentPort?.postMessage({ + id: 0, + counts, + generation: readIndexGeneration(store!.connection) + }) + } + const onError = (error: unknown): never => { + throw error + } + store = new SessionSearchStore(c.path, onError, onMessagesChanged) + // Why: worker retirement closes the connection and its maintenance together. + toolPurge = new SessionSearchToolPurge( + store.connection, + () => false, + onMessagesChanged, + onError + ) + void toolPurge.request() + store.setRetentionCutoffMs(c.cutoff) + return { id, snapshot: snapshot(), counts, generation: readIndexGeneration(store.connection) } + } + if (c.op === 'query') { + if (!engine) { + throw new Error('Search reader is not open') + } + const service = createSessionSearchService({ + engine, + indexer: { + status: () => { + throw new Error('Reader has no indexer') + }, + reconcile: async () => undefined + } + }) + return { + id, + result: await withSpan('aiVault.search.query', () => service.search(c.request, c.scope)) + } + } + if (!store) { + throw new Error('Search writer is not open') + } + let reset: SessionSearchSnapshotRow[] | undefined + // Why: SQLite data_version changes only for foreign commits, including legacy writers' generation bumps. + if (dataVersion !== store.connection.pragma('data_version', { simple: true })) { + for (const { write } of writes.values()) { + write.discard() + } + writes.clear() + handlePaths.clear() + reset = snapshot() + void toolPurge?.request() + } + let path: string | undefined + let accepted = true + switch (c.op) { + case 'begin': { + handlePaths.set(c.handle, c.candidate.file.path) + let identity: TranscriptSessionIdentity | null = null + const write = store.beginWrite(c.candidate, c.mode, c.offset, () => identity) + if (write) { + writes.set(c.handle, { + write, + get identity() { + return identity + }, + set identity(value) { + identity = value + } + }) + } + accepted = write !== null + break + } + case 'batch': { + const entry = writes.get(c.handle) + accepted = !!entry + if (entry) { + entry.identity = c.identity + entry.write.addPrepared!(c.rows, c.hash) + } + break + } + case 'finish': { + const entry = writes.get(c.handle) + entry?.write.addPrepared!([], c.hash) + accepted = !!entry && entry.write.commit(c.outcome) + writes.delete(c.handle) + if (accepted) { + const path = handlePaths.get(c.handle) + if (path) { + store.setFileState(path, 'current') + } + } + break + } + case 'discard': + writes.get(c.handle)?.write.discard() + writes.delete(c.handle) + break + case 'state': + store.setFileState(c.path, c.state, c.mtime) + path = c.path + break + case 'remove': + store.removeFile(c.path) + path = c.path + break + case 'purge': { + store.setRetentionCutoffMs(c.cutoff) + const before = [...files.keys()] + await withSpan('aiVault.search.purge', () => store!.purgeOlderThan(c.cutoff)) + const remaining = new Map(rows().map((row) => [row.path, row])) + const deltas = before.filter((p) => !remaining.has(p)).map(delta) + return { + id, + snapshot: reset, + deltas, + counts, + generation: readIndexGeneration(store.connection) + } + } + case 'sync': + break + } + if (c.op === 'batch' || c.op === 'finish') { + // A named chunk can commit before EOF; publish its partial cursor immediately. + path = handlePaths.get(c.handle) + } + if (c.op === 'begin') { + handlePaths.set(c.handle, c.candidate.file.path) + } + if (c.op === 'finish' || c.op === 'discard') { + handlePaths.delete(c.handle) + } + return { + id, + accepted, + snapshot: reset, + deltas: path ? [delta(path)] : undefined, + counts, + generation: readIndexGeneration(store.connection) + } +} +const handlePaths = new Map() +let lane = Promise.resolve() +parentPort?.on('message', (request: SearchWorkerRequest) => { + lane = lane.then(async () => { + try { + parentPort?.postMessage(await execute(request)) + } catch (error) { + const command = request.command + if ('handle' in command) { + writes.get(command.handle)?.write.discard() + writes.delete(command.handle) + handlePaths.delete(command.handle) + } + parentPort?.postMessage({ + id: request.id, + accepted: false, + failure: + command.op === 'begin' || command.op === 'batch' || command.op === 'finish' + ? 'write' + : undefined, + error: error instanceof Error ? error.message : 'Session search failed' + }) + } + }) +}) diff --git a/src/main/ai-vault-search/session-search-worker-protocol.ts b/src/main/ai-vault-search/session-search-worker-protocol.ts new file mode 100644 index 00000000000..a3db6b6c3f8 --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker-protocol.ts @@ -0,0 +1,60 @@ +import type { SessionFileCandidate } from '../ai-vault/session-scanner-types' +import type { + TranscriptMessage, + TranscriptReadOutcome, + TranscriptSessionIdentity +} from '../ai-vault/transcripts/session-transcript-consumers' +import type { + AiVaultSearchRequest, + AiVaultSearchResponse +} from '../../shared/ai-vault/search/ai-vault-search-types' +import type { SessionSearchHostScope } from './session-search-service' +import type { + SessionSearchFileRow, + SessionSearchFileState, + SessionSearchStateCounts +} from './session-search-store' +import type { SessionContentHash } from './session-search-content-hash' + +export type SessionSearchSnapshotRow = SessionSearchFileRow & { + byteOffset: number | null + prefixHash: string | null + agent: string | null + contentHash: SessionContentHash +} +export type SearchWorkerCommand = + | { op: 'open'; path: string; reader: boolean; cutoff: number | null } + | { + op: 'begin' + handle: number + candidate: SessionFileCandidate + mode: 'append' | 'replace' + offset: number + } + | { + op: 'batch' + handle: number + rows: TranscriptMessage[] + hash: SessionContentHash + identity: TranscriptSessionIdentity | null + } + | { op: 'finish'; handle: number; outcome: TranscriptReadOutcome; hash: SessionContentHash } + | { op: 'discard'; handle: number } + | { op: 'state'; path: string; state: SessionSearchFileState; mtime?: number } + | { op: 'remove'; path: string } + | { op: 'purge'; cutoff: number | null } + | { op: 'sync' } + | { op: 'query'; request: AiVaultSearchRequest; scope?: SessionSearchHostScope } +export type SearchWorkerRequest = { id: number; command: SearchWorkerCommand } +export type SearchWorkerResponse = { + id: number + trace?: unknown + failure?: 'write' + error?: string + accepted?: boolean + snapshot?: SessionSearchSnapshotRow[] + deltas?: { path: string; row: SessionSearchSnapshotRow | null }[] + counts?: SessionSearchStateCounts + generation?: number + result?: AiVaultSearchResponse +} diff --git a/src/main/ai-vault-search/session-search-worker-store-base.ts b/src/main/ai-vault-search/session-search-worker-store-base.ts new file mode 100644 index 00000000000..01d23522f3e --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker-store-base.ts @@ -0,0 +1,169 @@ +import { + createAiVaultScanCancelledError, + abandonRemoteSessionScanOnCancel +} from '../ai-vault/ai-vault-scan-cancellation' +import { + SessionSearchWorkerClient, + SessionSearchOperationError +} from './session-search-worker-client' +import { reportSessionSearchBackgroundError } from './session-search-trace' +import { SessionSearchCursorSnapshot } from './session-search-cursor-snapshot' +import type { SessionSearchFileState, SessionSearchStateCounts } from './session-search-store' +import type { SearchWorkerCommand, SearchWorkerResponse } from './session-search-worker-protocol' + +/** Scanner-side cursor facts only; SQLite and durable staging belong to the writer thread. */ +export class SessionSearchWorkerStoreBase extends SessionSearchCursorSnapshot { + protected readonly client: SessionSearchWorkerClient + protected readonly transfers = new Set>() + protected readonly credits = new Set>() + protected readonly invalidations = new Map() + protected initialization: Promise | null = null + protected initializedEpoch = -1 + protected snapshotEpoch = 0 + protected nextHandle = 0 + protected bytes = 0 + protected closed = false + protected counts: SessionSearchStateCounts = { + current: 0, + due: 0, + failed: 0, + messages: 0, + sessionsByAgent: {} + } + generation = 0 + protected cutoff: number | null = null + + constructor( + protected readonly path: string, + protected readonly onError: (error: unknown) => void, + private readonly onInvalidated: () => void = () => undefined + ) { + super() + this.client = new SessionSearchWorkerClient( + (response) => this.update(response), + undefined, + true + ) + } + get acceptingReads(): boolean { + return !this.closed && this.initializedEpoch === this.client.epoch + } + get retentionCutoff(): number | null { + return this.cutoff + } + setRetentionCutoffMs(cutoff: number | null): void { + this.cutoff = cutoff + } + async prepare(): Promise { + if (this.closed) { + throw createAiVaultScanCancelledError() + } + if (this.acceptingReads) { + await this.send({ op: 'sync' }) + return + } + this.initialization ??= (async () => { + await this.client.retired() + const epoch = this.client.epoch + await this.client.request({ op: 'open', path: this.path, reader: false, cutoff: this.cutoff }) + this.initializedEpoch = epoch + })() + .catch((error) => { + this.client.stop() + throw error + }) + .finally(() => { + this.initialization = null + }) + await this.initialization + } + protected update(response: SearchWorkerResponse): void { + if (response.snapshot) { + if (this.invalidations.size) { + this.onInvalidated() + } + for (const owner of this.invalidations.keys()) { + owner.abort() + } + this.invalidations.clear() + this.snapshotEpoch++ + this.committed.clear() + this.speculative.clear() + for (const row of response.snapshot) { + this.committed.set(row.path, row) + } + } + for (const { path, row } of response.deltas ?? []) { + if (row) { + this.committed.set(path, row) + } else { + this.committed.delete(path) + } + } + if (response.generation !== undefined) { + this.generation = response.generation + } + if (response.counts) { + this.counts = response.counts + } + } + protected send(command: SearchWorkerCommand): Promise { + const promise = this.client.request(command) + this.transfers.add(promise) + void promise + .catch((error: unknown) => { + if (error instanceof SessionSearchOperationError) { + reportSessionSearchBackgroundError(this.onError, error) + return + } + this.onInvalidated() + for (const owner of this.invalidations.keys()) { + owner.abort() + } + this.invalidations.clear() + this.client.stop(createAiVaultScanCancelledError()) + this.speculative.clear() + }) + .finally(() => this.transfers.delete(promise)) + return promise + } + async flush(): Promise { + while (this.transfers.size) { + await Promise.allSettled(this.transfers) + } + } + stateCounts(): SessionSearchStateCounts { + return this.counts + } + writeCommitted(): void {} + reportWriteFailure(error: unknown): void { + this.onError(error) + } + setFileState(path: string, state: SessionSearchFileState, mtime?: number): void { + if (this.acceptingReads) { + void this.send({ op: 'state', path, state, mtime }) + } + } + removeFile(path: string): void { + if (this.acceptingReads) { + void this.send({ op: 'remove', path }) + } + } + async purgeOlderThan(cutoff: number | null, signal?: AbortSignal): Promise { + await this.prepare() + await abandonRemoteSessionScanOnCancel(this.send({ op: 'purge', cutoff }), signal) + } + close(): void { + this.closed = true + for (const owner of this.invalidations.keys()) { + owner.abort() + } + this.invalidations.clear() + this.client.stop() + this.committed.clear() + this.speculative.clear() + } + retired(): Promise { + return this.client.retired() + } +} diff --git a/src/main/ai-vault-search/session-search-worker-store.ts b/src/main/ai-vault-search/session-search-worker-store.ts new file mode 100644 index 00000000000..4a9b92e82ec --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker-store.ts @@ -0,0 +1,206 @@ +import { ownRetainedString } from '../../shared/own-retained-string' +import type { SessionFileCandidate } from '../ai-vault/session-scanner-types' +import type { + TranscriptMessage, + TranscriptReadOutcome, + TranscriptSessionIdentity +} from '../ai-vault/transcripts/session-transcript-consumers' +import { + createAiVaultScanCancelledError, + abandonRemoteSessionScanOnCancel +} from '../ai-vault/ai-vault-scan-cancellation' +import { EMPTY_CONTENT_HASH, foldContentHash } from './session-search-content-hash' +import { searchMessageRows } from './session-search-message-rows' +import type { SessionSearchFileWrite } from './session-search-index-writer' +import type { + SearchWorkerCommand, + SessionSearchSnapshotRow +} from './session-search-worker-protocol' +import { SessionSearchWorkerStoreBase } from './session-search-worker-store-base' + +const TRANSFER_BYTES = 8 * 1024 * 1024 +const BATCH_BYTES = 64 * 1024 + +export class SessionSearchWorkerStore extends SessionSearchWorkerStoreBase { + beginWrite( + candidate: SessionFileCandidate, + mode: 'append' | 'replace', + offset: number, + identity?: () => TranscriptSessionIdentity | null, + indexing = false, + signal?: AbortSignal + ): SessionSearchFileWrite | null { + if ( + !this.acceptingReads || + (!indexing && this.bytes >= TRANSFER_BYTES) || + (this.cutoff !== null && candidate.file.mtimeMs < this.cutoff) + ) { + return null + } + const handle = ++this.nextHandle + const epoch = this.client.epoch + const snapshotEpoch = this.snapshotEpoch + const owner = new AbortController() + this.invalidations.set(owner, candidate.file.path) + const readSignal = signal ? AbortSignal.any([owner.signal, signal]) : owner.signal + let ended = false + let cancelled = false + let failed = false + let rows: TranscriptMessage[] = [] + let size = 0 + let hash = + mode === 'append' + ? (this.row(candidate.file.path, null)?.contentHash ?? EMPTY_CONTENT_HASH) + : EMPTY_CONTENT_HASH + const active = () => + !cancelled && + this.acceptingReads && + epoch === this.client.epoch && + snapshotEpoch === this.snapshotEpoch && + !readSignal.aborted + const valid = () => active() && !failed + const post = (command: SearchWorkerCommand) => { + if (!valid()) { + return Promise.resolve({ id: 0, accepted: false }) + } + return this.send(command) + .then((response) => { + if (response.failure === 'write' && !failed) { + failed = true + this.setFileState(candidate.file.path, 'failed', candidate.file.mtimeMs) + for (const [dependent, path] of this.invalidations) { + if (dependent !== owner && path === candidate.file.path) { + dependent.abort() + } + } + this.speculative.delete(candidate.file.path) + } else if (response.accepted === false && !failed) { + cancelled = true + owner.abort() + this.speculative.delete(candidate.file.path) + } + return response + }) + .catch(() => { + cancelled = true + return { id: 0, accepted: false } + }) + } + void post({ op: 'begin', handle, candidate, mode, offset }) + const discard = () => { + cancelled = true + this.bytes -= size + rows = [] + size = 0 + if (this.acceptingReads && epoch === this.client.epoch && !ended) { + ended = true + void this.send({ op: 'discard', handle }).catch(() => undefined) + } + this.speculative.delete(candidate.file.path) + this.invalidations.delete(owner) + } + owner.signal.addEventListener('abort', discard, { once: true }) + const batch = () => { + if (!rows.length || !valid()) { + return + } + const payload = rows + const bytes = size + rows = [] + size = 0 + const sent = post({ + op: 'batch', + handle, + rows: payload, + hash, + identity: identity?.() ?? null + }) + this.credits.add(sent) + void sent + .finally(() => { + this.bytes -= bytes + this.credits.delete(sent) + }) + .catch(() => undefined) + } + return { + failed: () => failed, + cancelled: () => !active(), + drain: async () => { + batch() + if (!active()) { + throw createAiVaultScanCancelledError() + } + if (this.bytes >= TRANSFER_BYTES) { + await abandonRemoteSessionScanOnCancel(Promise.allSettled(this.credits), readSignal) + } + if (!active()) { + throw createAiVaultScanCancelledError() + } + }, + add: (message) => { + if (!valid()) { + return + } + hash = foldContentHash(hash, [message]) + for (const row of searchMessageRows([message])) { + row.text = ownRetainedString(row.text) + rows.push(row) + const bytes = row.text.length * 2 + 256 + size += bytes + this.bytes += bytes + if (!indexing && this.bytes >= TRANSFER_BYTES) { + discard() + return + } + if (size >= BATCH_BYTES) { + batch() + } + } + }, + addPrepared: () => { + throw new Error('Prepared rows belong to the writer') + }, + commit: (outcome: TranscriptReadOutcome) => { + if (!valid()) { + discard() + return false + } + batch() + ended = true + const old = this.committed.get(candidate.file.path) + const speculative: SessionSearchSnapshotRow = { + path: candidate.file.path, + identity: + typeof candidate.file.dev === 'number' && typeof candidate.file.ino === 'number' + ? { dev: candidate.file.dev, ino: candidate.file.ino } + : null, + mtimeMs: candidate.file.mtimeMs, + sizeBytes: candidate.file.sizeBytes ?? null, + state: 'current', + failCount: 0, + failedMtimeMs: null, + byteOffset: outcome.byteOffset, + prefixHash: outcome.prefixHash ?? null, + agent: old?.agent ?? candidate.agent, + contentHash: hash + } + this.speculative.set(candidate.file.path, speculative) + void post({ op: 'finish', handle, outcome, hash }) + .then(() => { + if (this.speculative.get(candidate.file.path) === speculative) { + this.speculative.delete(candidate.file.path) + } + this.invalidations.delete(owner) + }) + .catch(() => undefined) + return true + }, + discard: () => { + if (!ended) { + discard() + } + } + } + } +} diff --git a/src/main/ai-vault-search/session-search-worker.test-fixture.ts b/src/main/ai-vault-search/session-search-worker.test-fixture.ts new file mode 100644 index 00000000000..3a3ef931776 --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker.test-fixture.ts @@ -0,0 +1,28 @@ +import type * as WorkerClient from './session-search-worker-client' +import { createRequire } from 'node:module' +import { resolve } from 'node:path' +import { vi } from 'vitest' +import { spawnWorkerThread } from '../worker-threads/worker-thread-spawn' + +vi.mock('./session-search-worker-client', async (importOriginal) => { + const actual = await importOriginal() + const require = createRequire(import.meta.url) + const loader = require.resolve('tsx/cjs') + const entry = resolve('src/main/ai-vault-search/session-search-worker-entry.ts') + return { + ...actual, + SessionSearchWorkerClient: class extends actual.SessionSearchWorkerClient { + constructor(onChange?: ConstructorParameters[0]) { + super(onChange, () => + spawnWorkerThread( + `require(${JSON.stringify(loader)}); require(${JSON.stringify(entry)})`, + { + eval: true, + execArgv: ['--conditions=orca-source'] + } + ) + ) + } + } + } +}) diff --git a/src/main/ai-vault-search/session-search-worker.test.ts b/src/main/ai-vault-search/session-search-worker.test.ts new file mode 100644 index 00000000000..139c28d3ce4 --- /dev/null +++ b/src/main/ai-vault-search/session-search-worker.test.ts @@ -0,0 +1,216 @@ +import './session-search-worker.test-fixture' +import { mkdtemp, writeFile, rm, stat } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, expect, it } from 'vitest' +import { SessionSearchWorkerStore } from './session-search-worker-store' +import { SessionSearchReaders } from './session-search-readers' +import { registerSessionSearchIndexConsumer } from './session-search-index-consumer' +import { syntheticCandidate } from './session-search-index-test-fixture' +import { readWholeTranscript } from '../ai-vault/transcripts/session-transcript-reader' +import { resetTranscriptConsumersForTests } from '../ai-vault/transcripts/session-transcript-consumers' +import { createHash } from 'node:crypto' +import SyncDatabase from '../sqlite/sync-database' + +let root: string | undefined +let store: SessionSearchWorkerStore | undefined +let readers: SessionSearchReaders | undefined + +afterEach(async () => { + resetTranscriptConsumersForTests() + store?.close() + await Promise.all([store?.retired(), readers?.close()]) + if (root) { + await rm(root, { recursive: true, force: true }) + } +}) + +// Real workers import the parser graph; allow cold module loading plus SQLite publication. +it( + 'indexes a non-JSONL pass larger than transfer credits without losing rows or original-message hashes', + async () => { + root = await mkdtemp(join(tmpdir(), 'ss-worker-flow-')) + const path = join(root, 'index.sqlite') + const file = join(root, 'conversation.json') + const text = 'observable payload '.repeat(7000) + await writeFile( + file, + JSON.stringify({ + id: 'worker-session', + working_dir: root, + messages: [ + ...Array.from({ length: 40 }, () => ({ role: 'user', content: text })), + { role: 'assistant', content: 'workerneedle reached the end' } + ] + }) + ) + const info = await stat(file) + const candidate = { + ...syntheticCandidate({ path: file, mtimeMs: info.mtimeMs, sizeBytes: info.size }), + agent: 'jcode' as const + } + store = new SessionSearchWorkerStore(path, (error) => { + throw error + }) + await store.prepare() + registerSessionSearchIndexConsumer(store) + const listed = await readWholeTranscript({ candidate, platform: process.platform }) + await store.flush() + expect(listed?.messageCount).toBe(41) + expect(store.stateCounts()).toMatchObject({ current: 0, due: 0, failed: 0, messages: 0 }) + expect(store.coversTranscript(candidate)).toBe(false) + await readWholeTranscript({ candidate, platform: process.platform, indexing: true }) + await store.flush() + readers = new SessionSearchReaders(path, null) + expect((await readers.query({ query: 'workerneedle' })).result).toMatchObject({ + kind: 'results', + hits: [{ sessionId: 'worker-session' }] + }) + expect(store.coversTranscript(candidate)).toBe(true) + expect(store.stateCounts().failed).toBe(0) + const db = new SyncDatabase(path, { readonly: true }) + try { + let digest = '' + for (let i = 0; i < 8; i++) { + digest = createHash('sha256').update(digest).update('\0user\0').update(text).digest('hex') + } + expect( + db.prepare('SELECT content_hash, content_hash_count FROM sessions').get() + ).toMatchObject({ content_hash: digest, content_hash_count: 8 }) + } finally { + db.close() + } + }, + // Why: these start real writer and reader worker threads (tsx-loaded) and stream multi-MiB transcripts. + 30_000 +) + +it('cancels a dead writer handle and lets a fresh pass recover without failing the transcript', async () => { + root = await mkdtemp(join(tmpdir(), 'ss-worker-recovery-')) + const path = join(root, 'index.sqlite') + class RestartableStore extends SessionSearchWorkerStore { + stopWriter(): void { + this.client.stop() + } + } + const subject = new RestartableStore(path, (error) => { + throw error + }) + store = subject + await subject.prepare() + const candidate = syntheticCandidate() + const interrupted = subject.beginWrite(candidate, 'replace', 0, undefined, true)! + interrupted.add({ role: 'user', text: 'interrupted payload', timestamp: null }) + subject.stopWriter() + await expect(interrupted.drain!()).rejects.toMatchObject({ name: 'AbortError' }) + expect(interrupted.cancelled?.()).toBe(true) + await subject.prepare() + expect(subject.stateCounts().failed).toBe(0) + const recovered = subject.beginWrite(candidate, 'replace', 0, undefined, true)! + recovered.add({ role: 'user', text: 'recoveredneedle', timestamp: null }) + const { syntheticSession } = await import('./session-search-index-test-fixture') + recovered.commit({ session: syntheticSession(), byteOffset: 4096, incomplete: false }) + await subject.flush() + readers = new SessionSearchReaders(path, null) + expect((await readers.query({ query: 'recoveredneedle' })).result).toMatchObject({ + kind: 'results', + hits: [{ sessionId: 'fixture' }] + }) + expect(subject.stateCounts().failed).toBe(0) +}) + +it( + 'drains a near-10-MiB JSONL record even when one synchronous emission exceeds transfer credits', + async () => { + root = await mkdtemp(join(tmpdir(), 'ss-worker-burst-')) + const path = join(root, 'index.sqlite') + const file = join(root, 'burst.jsonl') + const text = `largestburstneedle ${'x '.repeat(5 * 1024 * 1024 - 1024)}` + const { writeClaudeTranscript } = await import('./session-search-indexer-test-fixture') + await writeClaudeTranscript(file, [text], 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee') + const info = await stat(file) + const candidate = syntheticCandidate({ + path: file, + mtimeMs: info.mtimeMs, + sizeBytes: info.size + }) + store = new SessionSearchWorkerStore(path, (error) => { + throw error + }) + await store.prepare() + registerSessionSearchIndexConsumer(store) + const { readResumableTranscript } = + await import('../ai-vault/transcripts/session-transcript-reader') + const { createClaudeSessionResumeState } = + await import('../ai-vault/scanners/claude/session-scanner-primary-parsers') + await readResumableTranscript({ + candidate, + platform: process.platform, + resume: null, + indexing: true, + stateFactory: (channel) => createClaudeSessionResumeState(candidate.file, channel) + }) + await store.flush() + expect(store.coversTranscript(candidate)).toBe(true) + expect(store.stateCounts().failed).toBe(0) + readers = new SessionSearchReaders(path, null) + expect((await readers.query({ query: 'largestburstneedle' })).result).toMatchObject({ + kind: 'results', + hits: [{ sessionId: 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee' }] + }) + }, + // Why: these start real writer and reader worker threads (tsx-loaded) and stream multi-MiB transcripts. + 30_000 +) + +it('commits the original-message digest when a drained conversation ends with only tool rows', async () => { + root = await mkdtemp(join(tmpdir(), 'ss-worker-hash-tail-')) + const path = join(root, 'index.sqlite') + store = new SessionSearchWorkerStore(path, (error) => { + throw error + }) + await store.prepare() + const write = store.beginWrite(syntheticCandidate(), 'replace', 0, undefined, true)! + write.add({ role: 'user', text: 'prompt', timestamp: null }) + await write.drain!() + await store.flush() + write.add({ role: 'tool', text: 'tooltail', timestamp: null }) + const { syntheticSession } = await import('./session-search-index-test-fixture') + expect(write.commit({ session: syntheticSession(), byteOffset: 4096, incomplete: false })).toBe( + true + ) + await store.flush() + const db = new SyncDatabase(path, { readonly: true }) + try { + expect(db.prepare('SELECT content_hash, content_hash_count FROM sessions').get()).toEqual({ + content_hash: '20d617e013f1dbefe8ce6afe43ad44ec558ae2e087d44804ce2f789cc04538a8', + content_hash_count: 2 + }) + expect(db.prepare('SELECT count(*) AS n FROM messages').get()).toEqual({ n: 1 }) + } finally { + db.close() + } +}) + +it('opens a cold reader and searches committed rows while a writer batch remains uncommitted', async () => { + root = await mkdtemp(join(tmpdir(), 'ss-reader-independent-')) + const path = join(root, 'index.sqlite') + const { SessionSearchStore } = await import('./session-search-store') + const { addSyntheticSession } = await import('./session-search-engine-test-fixture') + const writer = new SessionSearchStore(path) + try { + addSyntheticSession(writer.connection, { id: 1, text: 'independentneedle' }) + writer.connection.exec('BEGIN IMMEDIATE') + writer.connection.prepare("UPDATE sessions SET title = 'uncommitted' WHERE id = 1").run() + readers = new SessionSearchReaders(path, null) + expect((await readers.query({ query: 'independentneedle' })).result).toMatchObject({ + kind: 'results', + hits: [{ sessionId: '1', title: 'fixture' }] + }) + } finally { + if (writer.connection.isTransaction) { + writer.connection.exec('ROLLBACK') + } + writer.close() + } +}) diff --git a/src/main/ai-vault-search/session-search-write-buffer.test.ts b/src/main/ai-vault-search/session-search-write-buffer.test.ts index 874b57440b7..ce3128c0a4f 100644 --- a/src/main/ai-vault-search/session-search-write-buffer.test.ts +++ b/src/main/ai-vault-search/session-search-write-buffer.test.ts @@ -1,6 +1,6 @@ import { expect, it, vi } from 'vitest' import SyncDatabase from '../sqlite/sync-database' -import { SessionSearchWriteBuffer } from './session-search-write-buffer' +import { SessionSearchWriteBuffer, SessionSearchStagingBudget } from './session-search-write-buffer' import { SessionSearchIndexWriter } from './session-search-index-writer' import { openSessionSearchIndexFile, @@ -65,8 +65,7 @@ it.each([1, 1_000_000])( ).toEqual([ { role: 'user', text_truncated: 1 }, { role: 'user', text_truncated: 1 }, - { role: 'assistant', text_truncated: 0 }, - { role: 'tool', text_truncated: 1 } + { role: 'assistant', text_truncated: 0 } ]) } finally { write.discard() @@ -119,3 +118,31 @@ it.each(['discard', 'close', 'removed', 'rollback'] as const)( } } ) + +it('shares resident staging across handles without changing publication readiness or row order', () => { + const budget = new SessionSearchStagingBudget() + const buffers = Array.from( + { length: 4 }, + () => new SessionSearchWriteBuffer(32 * 1024 * 1024, budget) + ) + const text = '界'.repeat(8000) + try { + for (const buffer of buffers) { + for (let row = 0; row < 200; row++) { + buffer.add({ role: 'user', text: `${row}:${text}`, timestamp: null }) + } + } + expect(budget.bytes).toBeLessThanOrEqual(8 * 1024 * 1024) + for (const buffer of buffers) { + expect(buffer.ready).toBe(false) + expect([...buffer.rows()].map((row) => row.text)).toEqual( + Array.from({ length: 200 }, (_, row) => `${row}:${text}`) + ) + } + } finally { + for (const buffer of buffers) { + buffer.release() + } + } + expect(budget.bytes).toBe(0) +}) diff --git a/src/main/ai-vault-search/session-search-write-buffer.ts b/src/main/ai-vault-search/session-search-write-buffer.ts index a8b7e519d9b..21bfb85bdb7 100644 --- a/src/main/ai-vault-search/session-search-write-buffer.ts +++ b/src/main/ai-vault-search/session-search-write-buffer.ts @@ -2,18 +2,43 @@ import SyncDatabase from '../sqlite/sync-database' import type { TranscriptMessage } from '../ai-vault/transcripts/session-transcript-consumers' import { ownRetainedString } from '../../shared/own-retained-string' +/** Shared by all handles; a capped row can overshoot the resident budget by at most 16,256 bytes. */ +export class SessionSearchStagingBudget { + readonly buffers = new Set() + bytes = 0 + retain(bytes: number): void { + this.bytes += bytes + if (this.bytes > 8 * 1024 * 1024) { + for (const buffer of this.buffers) { + buffer.spill() + } + } + } +} + /** Anonymous reads stay private until commit; large reads spill to a disposable SQLite file. */ export class SessionSearchWriteBuffer { private readonly pending: TranscriptMessage[] = [] private characters = 0 + private spilledCharacters = 0 + private residentBytes = 0 private spilled: SyncDatabase | null = null - constructor(private readonly commitChars: number) {} + constructor( + private readonly commitChars: number, + private readonly budget = new SessionSearchStagingBudget() + ) { + budget.buffers.add(this) + } get ready(): boolean { return this.characters >= this.commitChars } + get spillReady(): boolean { + return this.characters - this.spilledCharacters >= this.commitChars + } + get hasSpilled(): boolean { return this.spilled !== null } @@ -22,13 +47,21 @@ export class SessionSearchWriteBuffer { message.text = ownRetainedString(message.text) this.pending.push(message) this.characters += message.text.length + const bytes = message.text.length * 2 + 256 + this.residentBytes += bytes + this.budget.retain(bytes) } spill(): void { + if (!this.pending.length) { + return + } if (!this.spilled) { // An empty filename is a private temporary database deleted when its connection closes. this.spilled = new SyncDatabase('') - this.spilled.exec(`PRAGMA temp_store = FILE; + // Why: per-handle native page caches must not grow with staged transcript size. + this.spilled.exec(`PRAGMA temp_store = FILE; PRAGMA cache_size = -64; + PRAGMA mmap_size = 0; CREATE TABLE messages(role TEXT NOT NULL, text TEXT NOT NULL, ts TEXT, text_truncated INTEGER NOT NULL)`) } const db = this.spilled @@ -46,7 +79,9 @@ export class SessionSearchWriteBuffer { throw error } this.pending.length = 0 - this.characters = 0 + this.spilledCharacters = this.characters + this.budget.bytes -= this.residentBytes + this.residentBytes = 0 } *rows(): Generator { @@ -73,9 +108,17 @@ export class SessionSearchWriteBuffer { yield* this.pending } + release(): void { + this.clear() + this.budget.buffers.delete(this) + } + clear(): void { + this.budget.bytes -= this.residentBytes + this.residentBytes = 0 this.pending.length = 0 this.characters = 0 + this.spilledCharacters = 0 const spilled = this.spilled this.spilled = null spilled?.close() diff --git a/src/main/ai-vault/ai-vault-scan-cancellation.ts b/src/main/ai-vault/ai-vault-scan-cancellation.ts index 4c86222d013..98a77712109 100644 --- a/src/main/ai-vault/ai-vault-scan-cancellation.ts +++ b/src/main/ai-vault/ai-vault-scan-cancellation.ts @@ -1,3 +1,4 @@ +import { waitForPromiseWithSignal } from '../../shared/abort-signal-reason' import { AI_VAULT_SCAN_CANCELLED_MESSAGE } from '../../shared/ai-vault/contracts/ai-vault-types' export function throwIfAiVaultScanCancelled(signal?: AbortSignal): void { @@ -7,6 +8,16 @@ export function throwIfAiVaultScanCancelled(signal?: AbortSignal): void { throw createAiVaultScanCancelledError() } +export function waitForAiVaultScan(promise: Promise, signal?: AbortSignal): Promise { + if (!signal) { + return promise + } + return waitForPromiseWithSignal(promise, signal).catch((error: unknown) => { + throwIfAiVaultScanCancelled(signal) + throw error + }) +} + // RPC cannot abort in-flight work; cancellation releases its waiter while the request settles separately. export function abandonRemoteSessionScanOnCancel( promise: Promise, diff --git a/src/main/ai-vault/parse-cache/session-parse-cache-database.ts b/src/main/ai-vault/parse-cache/session-parse-cache-database.ts index 83b92c435d0..26cffa36fa3 100644 --- a/src/main/ai-vault/parse-cache/session-parse-cache-database.ts +++ b/src/main/ai-vault/parse-cache/session-parse-cache-database.ts @@ -1,3 +1,4 @@ +import { startSpan } from '../../observability/tracer' // Scanner-owned per-transcript rows persist changes without rewriting or capping the full working set. import { isSqliteCorruption } from '../../sqlite/sqlite-read-failure' import { hardenSqliteDatabaseFiles } from '../../sqlite/harden-database-files' @@ -194,9 +195,11 @@ export class SessionParseCacheDatabase { if (db === null) { return fallback } + const span = startSpan('aiVault.parseCache.sqlite') try { return operation(db) } catch (error) { + span.fail(error instanceof Error ? error : String(error)) console.debug('[ai-vault] session parse cache database failed', error) if (isSqliteCorruption(error)) { this.close() @@ -211,6 +214,8 @@ export class SessionParseCacheDatabase { } } return fallback + } finally { + span.end() } } } diff --git a/src/main/ai-vault/parse-cache/session-parse-file-lane.test.ts b/src/main/ai-vault/parse-cache/session-parse-file-lane.test.ts new file mode 100644 index 00000000000..4ffcf3460ef --- /dev/null +++ b/src/main/ai-vault/parse-cache/session-parse-file-lane.test.ts @@ -0,0 +1,37 @@ +import { expect, it } from 'vitest' +import { inSessionParseFileLane } from './session-parse-file-lane' + +it('cancels a queued list promptly while retaining its link behind the draining pass', async () => { + let release!: () => void + let entered!: () => void + const holding = new Promise((resolve) => { + release = resolve + }) + const started = new Promise((resolve) => { + entered = resolve + }) + const pass = inSessionParseFileLane('cancelled-waiter', async () => { + entered() + await holding + }) + await started + const controller = new AbortController() + const cancelled = inSessionParseFileLane( + 'cancelled-waiter', + async () => { + throw new Error('Cancelled waiter ran') + }, + controller.signal + ) + controller.abort() + await expect(cancelled).rejects.toMatchObject({ name: 'AbortError' }) + let thirdEntered = false + const third = inSessionParseFileLane('cancelled-waiter', async () => { + thirdEntered = true + }) + await Promise.resolve() + expect(thirdEntered).toBe(false) + release() + await Promise.all([pass, third]) + expect(thirdEntered).toBe(true) +}) diff --git a/src/main/ai-vault/parse-cache/session-parse-file-lane.ts b/src/main/ai-vault/parse-cache/session-parse-file-lane.ts index 3b8a6e902e7..1e1a519e661 100644 --- a/src/main/ai-vault/parse-cache/session-parse-file-lane.ts +++ b/src/main/ai-vault/parse-cache/session-parse-file-lane.ts @@ -1,18 +1,33 @@ +import { + abandonRemoteSessionScanOnCancel, + throwIfAiVaultScanCancelled +} from '../ai-vault-scan-cancellation' + const pending = new Map>() /** Serialize by path: overlapping reads corrupt shared message consumers and can rewind the cached cursor. */ -export async function inSessionParseFileLane(path: string, parse: () => Promise): Promise { +export function inSessionParseFileLane( + path: string, + parse: () => Promise, + signal?: AbortSignal +): Promise { const previous = pending.get(path) + const acquired = Promise.resolve(previous).catch(() => undefined) + const admission = abandonRemoteSessionScanOnCancel(acquired, signal) const run = (async () => { - await previous?.catch(() => undefined) + await acquired + throwIfAiVaultScanCancelled(signal) return parse() })() pending.set(path, run) - try { - return await run - } finally { - if (pending.get(path) === run) { - pending.delete(path) - } - } + // Why: cancelling the caller must retain its chain link until the holder exits. + void run + .finally(() => { + if (pending.get(path) === run) { + pending.delete(path) + } + }) + .catch(() => undefined) + // Why: an active parser must finish cancelling its consumers before its caller returns. + return admission.then(() => run) } diff --git a/src/main/ai-vault/parse-cache/session-scanner-parse-cache.ts b/src/main/ai-vault/parse-cache/session-scanner-parse-cache.ts index 57088d7c767..b7d6e266e18 100644 --- a/src/main/ai-vault/parse-cache/session-scanner-parse-cache.ts +++ b/src/main/ai-vault/parse-cache/session-scanner-parse-cache.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../ai-vault-scan-cancellation' import { createQoderSessionResumeState } from '../scanners/qoder/session-scanner-qoder-parser' import type { AiVaultSession } from '../../../shared/ai-vault/contracts/ai-vault-types' import { inSessionParseFileLane } from './session-parse-file-lane' @@ -109,11 +110,14 @@ export async function parseAgentSessionFileCached( platform: NodeJS.Platform, stats?: SessionParseStats, requireRead?: SessionParseReadRequirement, - signal?: AbortSignal + signal?: AbortSignal, + indexing = false ): Promise { // Serialize lookup/read/store because overlapping parses share a resume point and message channel. - return inSessionParseFileLane(candidate.file.path, () => - parseCachedInLane(candidate, platform, stats, requireRead, signal) + return inSessionParseFileLane( + candidate.file.path, + () => parseCachedInLane(candidate, platform, stats, requireRead, signal, indexing), + signal ) } @@ -151,7 +155,8 @@ async function parseCachedInLane( platform: NodeJS.Platform, stats?: SessionParseStats, requireRead?: SessionParseReadRequirement, - signal?: AbortSignal + signal?: AbortSignal, + indexing = false ): Promise { const { file } = candidate const key = sessionParseCacheKey(candidate.agent, file.path) @@ -166,7 +171,7 @@ async function parseCachedInLane( if (entry !== undefined && sessionParseCacheCoversTranscript(candidate, key, platform)) { if (sidecarUnchanged(entry.sidecar, file.sidecar)) { - return reuseCachedSession(candidate, key, entry, stats) + return reuseCachedSession(candidate, key, entry, stats, signal) } // Metadata-only sidecar changes can merge onto the saved fold without rereading the transcript. if (sidecarEnrichesWithoutReparse(candidate) && entry.foldSession !== undefined) { @@ -176,6 +181,7 @@ async function parseCachedInLane( platform, signal ) + throwIfAiVaultScanCancelled(signal) const session = reuseUnchangedSidecarSession(entry.session, enriched.session) storeSessionParseCacheEntry(key, { ...entry, @@ -205,7 +211,8 @@ async function parseCachedInLane( stateFactory, stats, signal, - sessionForConsumers + sessionForConsumers, + indexing }) storeSessionParseCacheEntry(key, { agent: candidate.agent, @@ -228,6 +235,7 @@ async function parseCachedInLane( stats, signal, sessionForConsumers, + indexing, onSkippedRecords: (records) => { skippedRecords = mergeSkippedTranscriptRecords([], records) } @@ -250,7 +258,8 @@ async function reuseCachedSession( candidate: SessionFileCandidate, key: string, entry: SessionParseCacheEntry, - stats?: SessionParseStats + stats?: SessionParseStats, + signal?: AbortSignal ): Promise { if (stats) { stats.reused++ @@ -260,9 +269,9 @@ async function reuseCachedSession( if (session && session.messageCount === 0) { const subagentTranscriptCount = session.agent === 'claude' - ? await countSubagentTranscripts(candidate.file.path) + ? await countSubagentTranscripts(candidate.file.path, signal) : session.agent === 'omp' - ? await countOmpSubagentTranscripts(candidate.file.path) + ? await countOmpSubagentTranscripts(candidate.file.path, signal) : null if ( subagentTranscriptCount !== null && @@ -273,8 +282,9 @@ async function reuseCachedSession( } // Index titles can change independently of the transcript signature. if (session && candidate.agent === 'codex') { - session = await refreshCachedCodexTitle(candidate, session) + session = await waitForAiVaultScan(refreshCachedCodexTitle(candidate, session), signal) } + throwIfAiVaultScanCancelled(signal) storeSessionParseCacheEntry(key, { ...entry, session }) return session } diff --git a/src/main/ai-vault/scan-service/session-scanner-service-client-state.ts b/src/main/ai-vault/scan-service/session-scanner-service-client-state.ts index 851958757f9..d9a9b09f961 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-client-state.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-client-state.ts @@ -1,9 +1,11 @@ +import { recordRelayedSpan } from '../../observability/tracer' import type { SessionSearchScanRoots } from '../../ai-vault-search/session-search-scan-roots' import type { ChildProcess } from 'node:child_process' import { createAiVaultScanCancelledError } from '../ai-vault-scan-cancellation' import type { AiVaultServiceScanListener } from './session-scanner-service-request-deadline' import { AI_VAULT_SERVICE_PROTOCOL_VERSION, + isAiVaultServiceChildMessage, aiVaultServiceLane, type AiVaultServiceInit, type AiVaultServiceLane, @@ -213,7 +215,13 @@ export function attachAiVaultServiceChild( onStderr?: (text: string) => void } ): void { - child.on('message', handlers.onMessage) + child.on('message', (message: unknown) => { + if (isAiVaultServiceChildMessage(message) && message.type === 'trace') { + recordRelayedSpan(message.record) + } else { + handlers.onMessage(message) + } + }) child.on('error', handlers.onFault) child.on('disconnect', () => handlers.onFault(new Error('AI Vault service disconnected.'))) child.on('exit', (code) => handlers.onFault(new Error(`AI Vault service exited (${code}).`))) diff --git a/src/main/ai-vault/scan-service/session-scanner-service-client.ts b/src/main/ai-vault/scan-service/session-scanner-service-client.ts index d84b951a665..4e08d0d65ba 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-client.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-client.ts @@ -218,7 +218,7 @@ export class AiVaultScannerServiceClient { } private onMessage(message: unknown): void { - if (!isAiVaultServiceChildMessage(message)) { + if (!isAiVaultServiceChildMessage(message) || message.type === 'trace') { this.onFault(new Error('AI Vault service sent a malformed message.')) return } diff --git a/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts b/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts index f433a9f4f89..fec29a49870 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts @@ -1,4 +1,5 @@ import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' +import { listLocalAiVaultSubagentSessions } from '../session-subagent-reader' import { noteAiVaultScanWorkChanged } from '../ai-vault-scan-progress' import { AI_VAULT_SERVICE_PROTOCOL_VERSION } from './session-scanner-service-protocol' @@ -111,4 +112,25 @@ describe('AI Vault service entry cache invalidation', () => { clock.mockRestore() } }) + it('answers search status at admission while the interactive lane is occupied', async () => { + let release!: () => void + vi.mocked(listLocalAiVaultSubagentSessions).mockImplementationOnce( + () => + new Promise((resolve) => { + release = () => resolve({ sessions: [], issues: [] }) + }) + ) + emit({ type: 'request', id: 30, operation: 'subagents', request: {} }) + await vi.waitFor(() => expect(release).toBeDefined()) + try { + emit({ type: 'request', id: 31, operation: 'searchStatus' }) + await vi.waitFor(() => + expect(sent).toContainEqual(expect.objectContaining({ type: 'result', id: 31 })) + ) + expect(sent.some((message) => message.id === 30)).toBe(false) + } finally { + release() + await vi.waitFor(() => expect(sent.some((message) => message.id === 30)).toBe(true)) + } + }) }) diff --git a/src/main/ai-vault/scan-service/session-scanner-service-entry.ts b/src/main/ai-vault/scan-service/session-scanner-service-entry.ts index 30f09c6093b..728e1b15a2f 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-entry.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-entry.ts @@ -1,3 +1,4 @@ +import { setActiveSink } from '../../observability/tracer' import { requestSessionSearchRoots } from './session-scanner-service-root-request' import { aiVaultScanWork, trackAiVaultScanProgress } from '../ai-vault-scan-progress' import type { AiVaultScanWork } from '../../../shared/ai-vault/scanning/ai-vault-scan-progress' @@ -35,6 +36,12 @@ if (!process.send) { throw new Error('AI Vault service requires a parent IPC channel.') } +setActiveSink({ + push: (record) => process.send?.({ type: 'trace', record }), + flush() {}, + close() {} +}) + const controllers = new Map() const cancelled = new Set() const pending = new Set() @@ -70,20 +77,15 @@ async function scanReportingProgress( } async function executeRequest(request: AiVaultServiceRequest): Promise { - if (sessionSearch.handles(request)) { - try { - return await sessionSearch.execute(request) - } finally { - // Search has no controller to consume cancellation; discard its id here. - cancelled.delete(request.id) - } - } const controller = new AbortController() controllers.set(request.id, controller) try { if (cancelled.delete(request.id)) { controller.abort() } + if (sessionSearch.handles(request)) { + return await sessionSearch.execute(request, controller.signal) + } if (request.operation === 'titles') { const requests = await resolveHostReadableAiVaultTitleRequests( request.requests, @@ -170,6 +172,11 @@ function queueRequest(request: AiVaultServiceRequest): void { return } pending.add(request.id) + // Why: search deadlines and cancellation start at admission, independent of the interactive lane. + if (sessionSearch.handles(request)) { + void handleRequest(request) + return + } if (aiVaultServiceLane(request.operation) === 'interactive') { interactiveLane = interactiveLane.then(() => handleRequest(request)) return @@ -185,7 +192,7 @@ async function shutdown(): Promise { for (const controller of controllers.values()) { controller.abort() } - sessionSearch.close() + await sessionSearch.close() await Promise.allSettled([cacheLane, interactiveLane]) await flushSessionParseCachePersist() process.disconnect?.() diff --git a/src/main/ai-vault/scan-service/session-scanner-service-protocol.ts b/src/main/ai-vault/scan-service/session-scanner-service-protocol.ts index 24783ab845c..b69788d7ec6 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-protocol.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-protocol.ts @@ -130,6 +130,7 @@ export type AiVaultServiceResultValue = | { operation: 'searchClear'; value: null } export type AiVaultServiceChildMessage = + | { type: 'trace'; record: unknown } | { type: 'sessionSearchRoots'; id: number } | { type: 'ready' @@ -166,6 +167,9 @@ export function isAiVaultServiceChildMessage(value: unknown): value is AiVaultSe return false } const message = value as Record + if (message.type === 'trace') { + return true + } if (message.type === 'ready') { return message.protocol === AI_VAULT_SERVICE_PROTOCOL_VERSION && Number.isInteger(message.pid) } diff --git a/src/main/ai-vault/scan-service/session-scanner-service-search-lifecycle.test.ts b/src/main/ai-vault/scan-service/session-scanner-service-search-lifecycle.test.ts new file mode 100644 index 00000000000..e4f1b667fa4 --- /dev/null +++ b/src/main/ai-vault/scan-service/session-scanner-service-search-lifecycle.test.ts @@ -0,0 +1,68 @@ +import '../../ai-vault-search/session-search-worker.test-fixture' +import { afterEach, expect, it, vi } from 'vitest' +import { SessionSearchIndexer } from '../../ai-vault-search/session-search-indexer' +import { SessionSearchReaders } from '../../ai-vault-search/session-search-readers' +import { openSessionSearchIndexerHarness } from '../../ai-vault-search/session-search-indexer-test-fixture' +import { SessionScannerServiceSearch } from './session-scanner-service-search' + +afterEach(() => { + vi.useRealTimers() + vi.restoreAllMocks() +}) + +it.each(['initialization', 'retirement'] as const)( + 'answers search/status and settles cancelled or timed-out maintenance during slow %s', + async (phase) => { + const harness = await openSessionSearchIndexerHarness('ss-transition-deadline') + let release!: () => void + const blocked = new Promise((resolve) => { + release = resolve + }) + const initialize = vi.spyOn(SessionSearchIndexer.prototype, 'initialize').mockResolvedValue() + vi.spyOn(SessionSearchIndexer.prototype, 'start').mockResolvedValue() + const subject = new SessionScannerServiceSearch() + const init = { + databasePath: harness.databasePath, + roots: harness.roots, + settings: { enabled: true, historyDays: null } + } + if (phase === 'retirement') { + await subject.apply(init) + vi.spyOn(SessionSearchReaders.prototype, 'close').mockImplementationOnce(() => blocked) + } else { + initialize.mockImplementationOnce(() => blocked) + } + const applying = subject.apply(init) + try { + vi.useFakeTimers() + expect( + await subject.execute({ + type: 'request', + id: 1, + operation: 'searchSessions', + request: { query: 'needle' } + }) + ).toMatchObject({ value: { kind: 'unavailable', reason: 'not-ready' } }) + expect( + await subject.execute({ type: 'request', id: 2, operation: 'searchStatus' }) + ).toMatchObject({ value: { enabled: true } }) + for (const operation of ['searchReconcile', 'searchClear'] as const) { + const controller = new AbortController() + const pending = subject.execute({ type: 'request', id: 3, operation }, controller.signal) + controller.abort() + await expect(pending).rejects.toMatchObject({ name: 'AbortError' }) + } + const clear = expect( + subject.execute({ type: 'request', id: 4, operation: 'searchClear' }) + ).rejects.toThrow('Could not complete') + await vi.advanceTimersByTimeAsync(10_000) + await clear + } finally { + release() + vi.useRealTimers() + await applying + await subject.close() + await harness.cleanup() + } + } +) diff --git a/src/main/ai-vault/scan-service/session-scanner-service-search-roots.test.ts b/src/main/ai-vault/scan-service/session-scanner-service-search-roots.test.ts index b5532224ce3..a893f67fffc 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-search-roots.test.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-search-roots.test.ts @@ -32,7 +32,7 @@ beforeEach(async () => { }) afterEach(async () => { - subject.close() + await subject.close() vi.restoreAllMocks() resetTranscriptConsumersForTests() resetSessionParseCacheForTests() diff --git a/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts b/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts index 31cb6081698..098c43e1369 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts @@ -105,6 +105,7 @@ beforeAll(async () => { afterAll(async () => { emit({ type: 'sessionSearch', init: searchInit(false) }) + await searchStatus() process.send = originalSend await harness.cleanup() }) @@ -160,5 +161,6 @@ it('answers disabled once consent is withdrawn, without a respawn', async () => expect(await searchSessions('distinctive')).toEqual({ kind: 'unavailable', reason: 'disabled' }) expect(await searchStatus()).toMatchObject({ enabled: false, phase: 'idle' }) emit({ type: 'sessionSearch', init: searchInit(true) }) + await call({ type: 'request', operation: 'searchReconcile' }) expect((await searchSessions('distinctive')).kind).toBe('results') }) diff --git a/src/main/ai-vault/scan-service/session-scanner-service-search.ts b/src/main/ai-vault/scan-service/session-scanner-service-search.ts index 7ca8e3f913a..39cf574abeb 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-search.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-search.ts @@ -1,3 +1,4 @@ +import { withinSessionSearchDeadline } from '../../ai-vault-search/session-search-request-deadline' import type { SessionSearchIndexerOptions } from '../../ai-vault-search/session-search-indexer-options' import { unavailableSessionSearchStatus } from '../../../shared/ai-vault/search/ai-vault-search-client' import { AiVaultSearchRequestSchema } from '../../../shared/ai-vault/search/ai-vault-search-contract' @@ -20,22 +21,37 @@ type SearchOperation = Extract< /** Keep indexing with transcript readers; callers access the database through service IPC. */ export class SessionScannerServiceSearch { + private transition = Promise.resolve() + private applying = 0 + private enabled = false private instance: SessionSearchInstance | null = null private databasePath: string | null = null private roots: SessionSearchScanRoots | null = null constructor(private readonly resolveRoots?: SessionSearchIndexerOptions['resolveRoots']) {} - apply(init: AiVaultSessionSearchInit): void { + apply(init: AiVaultSessionSearchInit): Promise { + this.enabled = init.settings.enabled + this.applying++ + const operation = this.transition.then(() => this.applySettings(init)) + this.transition = operation + .catch((error: unknown) => console.warn('[ai-vault-search]', error)) + .finally(() => { + this.applying-- + }) + return this.transition + } + + private async applySettings(init: AiVaultSessionSearchInit): Promise { if (!sessionSearchSqliteAvailable()) { return } if (this.instance && this.databasePath !== init.databasePath) { - this.close() + await this.closeInstance() } if (this.instance && this.roots && !sameSessionSearchRoots(this.roots, init.roots)) { // Refresh fallback roots for callers without a resolver. - this.close() + await this.closeInstance() } this.databasePath = init.databasePath this.roots = init.roots @@ -44,7 +60,7 @@ export class SessionScannerServiceSearch { roots: init.roots, resolveRoots: this.resolveRoots }) - this.instance.apply(init.settings) + await this.instance.apply(init.settings) } handles(request: AiVaultServiceRequest): request is SearchOperation { @@ -56,40 +72,59 @@ export class SessionScannerServiceSearch { ) } - async execute(request: SearchOperation): Promise { + async execute( + request: SearchOperation, + signal?: AbortSignal + ): Promise { const instance = this.instance if (request.operation === 'searchStatus') { return { operation: 'searchStatus', - value: instance?.status() ?? unavailableSessionSearchStatus() + value: this.enabled + ? { ...((await instance?.status()) ?? unavailableSessionSearchStatus()), enabled: true } + : unavailableSessionSearchStatus() } } if (request.operation === 'searchReconcile') { - await instance?.reconcile() + await withinSessionSearchDeadline( + this.transition.then(() => this.instance?.reconcile(signal)), + signal + ) return { operation: 'searchReconcile', value: null } } if (request.operation === 'searchClear') { - if (!instance) { + if (!instance && !this.applying) { throw new Error('Agent Session History search is not available.') } - instance.clear() + await withinSessionSearchDeadline( + this.transition.then(() => this.instance?.clear(signal)), + signal + ) return { operation: 'searchClear', value: null } } return { operation: 'searchSessions', - value: instance - ? await instance.search( - AiVaultSearchRequestSchema.parse(request.request), - request.hostScope - ) - : { kind: 'unavailable', reason: 'disabled' } + value: !this.enabled + ? { kind: 'unavailable', reason: 'disabled' } + : this.applying || !instance + ? { kind: 'unavailable', reason: 'not-ready' } + : await instance.search( + AiVaultSearchRequestSchema.parse(request.request), + request.hostScope, + signal + ) } } - close(): void { - this.instance?.close() + close(): Promise { + return (this.transition = this.transition.then(() => this.closeInstance())) + } + + private async closeInstance(): Promise { + const retired = this.instance?.close() this.instance = null this.databasePath = null this.roots = null + await retired } } diff --git a/src/main/ai-vault/scanners/claude/session-scanner-primary-parsers.ts b/src/main/ai-vault/scanners/claude/session-scanner-primary-parsers.ts index 815c82b9c46..1039bee33e8 100644 --- a/src/main/ai-vault/scanners/claude/session-scanner-primary-parsers.ts +++ b/src/main/ai-vault/scanners/claude/session-scanner-primary-parsers.ts @@ -30,6 +30,7 @@ import { extractMessageText, normalizeTitleText } from '../../session-scanner-te import { extractString, parseJsonObject } from '../../session-scanner-values' type ParserSessionOptions = { + signal?: AbortSignal executionHostId?: ExecutionHostId executionHostPlatform?: NodeJS.Platform | null } @@ -173,7 +174,8 @@ export async function finalizeClaudeSessionParseState( !options.executionHostId || options.executionHostId === LOCAL_EXECUTION_HOST_ID if (ownsTranscriptDisk) { snapshot.accumulator.subagentTranscriptCount = await countSubagentTranscripts( - snapshot.accumulator.filePath + snapshot.accumulator.filePath, + options.signal ) } // A rename (custom-title) or Claude's generated name is the session's own; prompts and metadata are stand-ins. diff --git a/src/main/ai-vault/scanners/claude/session-scanner-subagent-transcripts.ts b/src/main/ai-vault/scanners/claude/session-scanner-subagent-transcripts.ts index adc9dc3bef0..80743356828 100644 --- a/src/main/ai-vault/scanners/claude/session-scanner-subagent-transcripts.ts +++ b/src/main/ai-vault/scanners/claude/session-scanner-subagent-transcripts.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { wslGatedReaddir } from '../../../native-chat/wsl-transcript-fs-access' import { basename, dirname, extname, join } from 'node:path' @@ -20,11 +21,18 @@ export function subagentTranscriptsDirFor(transcriptFilePath: string): string { return join(dirname(transcriptFilePath), stem, SUBAGENT_DIR_NAME) } -export async function countSubagentTranscripts(transcriptFilePath: string): Promise { +export async function countSubagentTranscripts( + transcriptFilePath: string, + signal?: AbortSignal +): Promise { let entries try { - entries = await wslGatedReaddir(subagentTranscriptsDirFor(transcriptFilePath), 'scan') + entries = await waitForAiVaultScan( + wslGatedReaddir(subagentTranscriptsDirFor(transcriptFilePath), 'scan', signal), + signal + ) } catch { + throwIfAiVaultScanCancelled(signal) return 0 } return entries.filter((entry) => isSubagentTranscriptFileName(entry.name, entry.isFile())).length diff --git a/src/main/ai-vault/scanners/cline/session-scanner-cline-parser.ts b/src/main/ai-vault/scanners/cline/session-scanner-cline-parser.ts index ad1e9311d79..e140bcec951 100644 --- a/src/main/ai-vault/scanners/cline/session-scanner-cline-parser.ts +++ b/src/main/ai-vault/scanners/cline/session-scanner-cline-parser.ts @@ -135,7 +135,8 @@ export async function parseClineSessionDocuments( fields: ['session_id', 'cwd', 'workspace_root', 'model', 'started_at', 'prompt'], create: () => null, consume: () => {}, - signal + signal, + drain: () => messages?.drain?.() ?? Promise.resolve() }) if (!parsed) { return null @@ -169,7 +170,8 @@ export async function parseClineSessionDocuments( fields: ['updated_at'], create, consume: consumeClineSessionMessage, - signal + signal, + drain: () => messages?.drain?.() ?? Promise.resolve() }) if (parsed) { accumulator = parsed.state diff --git a/src/main/ai-vault/scanners/codex/session-scanner-codex-parser.ts b/src/main/ai-vault/scanners/codex/session-scanner-codex-parser.ts index 43b79f26acd..246b642f591 100644 --- a/src/main/ai-vault/scanners/codex/session-scanner-codex-parser.ts +++ b/src/main/ai-vault/scanners/codex/session-scanner-codex-parser.ts @@ -1,3 +1,4 @@ +import { waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { openTranscriptReadStream } from '../../../native-chat/wsl-transcript-fs-access' import { createInterface } from 'node:readline' import type { @@ -211,6 +212,7 @@ async function finalizeCodexParseState( args: { codexHome: string | null titleReader?: (sessionId: string) => Promise + signal?: AbortSignal executionHostId?: ExecutionHostId executionHostPlatform?: NodeJS.Platform | null } @@ -224,7 +226,10 @@ async function finalizeCodexParseState( let titleKind: AiVaultSessionTitleKind = snapshot.titleSource === 'meta' && snapshot.accumulator.title ? 'named' : 'fallback' if (snapshot.sawSessionMeta) { - const indexedTitle = await args.titleReader?.(snapshot.accumulator.sessionId) + const indexedTitle = await waitForAiVaultScan( + Promise.resolve(args.titleReader?.(snapshot.accumulator.sessionId)), + args.signal + ) if (indexedTitle) { snapshot.accumulator.title = indexedTitle titleKind = 'named' diff --git a/src/main/ai-vault/scanners/cursor/session-scanner-cursor-chat-meta.ts b/src/main/ai-vault/scanners/cursor/session-scanner-cursor-chat-meta.ts index 0c8b0ef17bc..4e930b1990b 100644 --- a/src/main/ai-vault/scanners/cursor/session-scanner-cursor-chat-meta.ts +++ b/src/main/ai-vault/scanners/cursor/session-scanner-cursor-chat-meta.ts @@ -1,3 +1,4 @@ +import { waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { AsyncLocalStorage } from 'node:async_hooks' import { basename, dirname, join } from 'node:path' import { wslGatedReaddir, wslGatedStat } from '../../../native-chat/wsl-transcript-fs-access' @@ -123,13 +124,13 @@ export async function readCursorChatMeta( transcriptPath: string, signal?: AbortSignal ): Promise { - const metaPath = await cursorChatMetaPath(transcriptPath) + const metaPath = await waitForAiVaultScan(cursorChatMetaPath(transcriptPath), signal) if (!metaPath) { return null } let record: Record | null try { - record = await readJsonObjectIfExists(metaPath, signal) + record = await waitForAiVaultScan(readJsonObjectIfExists(metaPath, signal), signal) } catch (error) { if (!(error instanceof WslTranscriptFsError)) { throw error diff --git a/src/main/ai-vault/scanners/devin/session-scanner-devin-parser.ts b/src/main/ai-vault/scanners/devin/session-scanner-devin-parser.ts index df5bfa3215f..946347b6315 100644 --- a/src/main/ai-vault/scanners/devin/session-scanner-devin-parser.ts +++ b/src/main/ai-vault/scanners/devin/session-scanner-devin-parser.ts @@ -198,7 +198,8 @@ export async function parseDevinSessionDocument( }) }, consume: consumeDevinSessionStep, - signal + signal, + drain: () => messages?.drain?.() ?? Promise.resolve() }) if (!parsed) { return null diff --git a/src/main/ai-vault/scanners/gemini/session-scanner-gemini-parsers.ts b/src/main/ai-vault/scanners/gemini/session-scanner-gemini-parsers.ts index 6dcdbacc7f6..0ab09bf5c6e 100644 --- a/src/main/ai-vault/scanners/gemini/session-scanner-gemini-parsers.ts +++ b/src/main/ai-vault/scanners/gemini/session-scanner-gemini-parsers.ts @@ -194,7 +194,8 @@ export async function parseGeminiSessionDocument( }) }, consume: (state, value) => consumeGeminiMessage(state, asRecord(value)), - signal + signal, + drain: () => messages?.drain?.() ?? Promise.resolve() }) if (!parsed) { return null diff --git a/src/main/ai-vault/scanners/grok/session-scanner-grok-parser.ts b/src/main/ai-vault/scanners/grok/session-scanner-grok-parser.ts index ce287c944ca..e0d375a0ffc 100644 --- a/src/main/ai-vault/scanners/grok/session-scanner-grok-parser.ts +++ b/src/main/ai-vault/scanners/grok/session-scanner-grok-parser.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { openTranscriptReadStream, wslGatedReadFile @@ -30,9 +31,13 @@ const GROK_PREVIEW_SCAN_LIMIT = 4096 export async function parseGrokSessionFile( file: FileWithMtime, platform: NodeJS.Platform = process.platform, - messages?: TranscriptMessageSink + messages?: TranscriptMessageSink, + signal?: AbortSignal ): Promise { - const record = asRecord(JSON.parse(await wslGatedReadFile(file.path, 'utf-8', 'scan')) as unknown) + const parsed: unknown = JSON.parse( + await waitForAiVaultScan(wslGatedReadFile(file.path, 'utf-8', 'scan', signal), signal) + ) + const record = asRecord(parsed) if (!record) { return null } @@ -50,7 +55,7 @@ export async function parseGrokSessionFile( updateTimeline(accumulator, extractString(record.created_at)) updateTimeline(accumulator, extractString(record.updated_at)) updateTimeline(accumulator, extractString(record.last_active_at)) - await consumeGrokChatHistory(accumulator, grokTranscriptPathForMetadata(file.path)) + await consumeGrokChatHistory(accumulator, grokTranscriptPathForMetadata(file.path), signal) return finalizeSession(accumulator, platform) } @@ -61,12 +66,14 @@ export function grokSessionCwd(summary: Record): string | null async function consumeGrokChatHistory( accumulator: SessionAccumulator, - transcriptPath: string + transcriptPath: string, + signal?: AbortSignal ): Promise { - const input = openTranscriptReadStream(transcriptPath, { encoding: 'utf-8' }, 'scan') + const input = openTranscriptReadStream(transcriptPath, { encoding: 'utf-8' }, 'scan', signal) const lines = createInterface({ input, crlfDelay: Infinity }) try { for await (const line of lines) { + await waitForAiVaultScan(accumulator.messages.drain?.() ?? Promise.resolve(), signal) const record = parseJsonObject(line) if (!record) { continue @@ -101,8 +108,12 @@ async function consumeGrokChatHistory( ) } } catch (error) { + throwIfAiVaultScanCancelled(signal) // Summary-only sessions can list; WSL refusal must propagate so incomplete history is not cached as current. - if (error instanceof WslTranscriptFsError) { + if ( + error instanceof WslTranscriptFsError || + (error instanceof Error && error.name === 'AbortError') + ) { throw error } } finally { diff --git a/src/main/ai-vault/scanners/hermes/session-scanner-hermes-parser.ts b/src/main/ai-vault/scanners/hermes/session-scanner-hermes-parser.ts index 9fe0ccd6b5f..05e437da29b 100644 --- a/src/main/ai-vault/scanners/hermes/session-scanner-hermes-parser.ts +++ b/src/main/ai-vault/scanners/hermes/session-scanner-hermes-parser.ts @@ -113,7 +113,8 @@ export async function parseHermesSessionDocument( }) }, consume: consumeHermesSessionMessage, - signal + signal, + drain: () => messages?.drain?.() ?? Promise.resolve() }) if (!parsed) { return null diff --git a/src/main/ai-vault/scanners/jcode/session-scanner-jcode-parser.ts b/src/main/ai-vault/scanners/jcode/session-scanner-jcode-parser.ts index bea7e71c468..b20e7cf988f 100644 --- a/src/main/ai-vault/scanners/jcode/session-scanner-jcode-parser.ts +++ b/src/main/ai-vault/scanners/jcode/session-scanner-jcode-parser.ts @@ -1,3 +1,4 @@ +import { waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { wslGatedReadFile } from '../../../native-chat/wsl-transcript-fs-access' import type { AiVaultSession } from '../../../../shared/ai-vault/contracts/ai-vault-types' import type { ExecutionHostId } from '../../../../shared/execution-host' @@ -47,14 +48,21 @@ function jcodeMessageTokens(message: Record): number { export async function parseJcodeSessionFile( file: FileWithMtime, platform: NodeJS.Platform = process.platform, - messages?: TranscriptMessageSink + messages?: TranscriptMessageSink, + signal?: AbortSignal ): Promise { - return parseJcodeSessionContent( + const fold = foldJcodeSessionContent( file, - await wslGatedReadFile(file.path, 'utf-8', 'scan'), + await waitForAiVaultScan(wslGatedReadFile(file.path, 'utf-8', 'scan', signal), signal), platform, - messages ? { messages } : undefined + { messages } ) + let step = fold.next() + while (!step.done) { + await waitForAiVaultScan(messages?.drain?.() ?? Promise.resolve(), signal) + step = fold.next() + } + return step.value } export function parseJcodeSessionContent( @@ -63,6 +71,20 @@ export function parseJcodeSessionContent( platform: NodeJS.Platform = process.platform, options: ParserSessionOptions = {} ): AiVaultSession | null { + const fold = foldJcodeSessionContent(file, content, platform, options) + let step = fold.next() + while (!step.done) { + step = fold.next() + } + return step.value +} + +function* foldJcodeSessionContent( + file: FileWithMtime, + content: string, + platform: NodeJS.Platform, + options: ParserSessionOptions +): Generator { let parsed: unknown try { parsed = JSON.parse(content) @@ -94,6 +116,7 @@ export function parseJcodeSessionContent( // Ignore the live journal: its appends do not change the document signature used by the parse cache. for (const message of arrayValue(record.messages)) { consumeJcodeMessage(accumulator, message) + yield } return finalizeSession(accumulator, platform, options) diff --git a/src/main/ai-vault/scanners/kimi/session-scanner-kimi-parser.ts b/src/main/ai-vault/scanners/kimi/session-scanner-kimi-parser.ts index 60ffd90b07f..3b9d3053664 100644 --- a/src/main/ai-vault/scanners/kimi/session-scanner-kimi-parser.ts +++ b/src/main/ai-vault/scanners/kimi/session-scanner-kimi-parser.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { openTranscriptReadStream, wslGatedReadFile @@ -28,16 +29,22 @@ import { numberValue } from '../../session-scanner-token-values' export async function parseKimiSessionFile( file: FileWithMtime, platform: NodeJS.Platform = process.platform, - messages?: TranscriptMessageSink + messages?: TranscriptMessageSink, + signal?: AbortSignal ): Promise { let stateRecord: Record | null try { - stateRecord = asRecord( - JSON.parse(await wslGatedReadFile(file.path, 'utf-8', 'scan')) as unknown + const parsed: unknown = JSON.parse( + await waitForAiVaultScan(wslGatedReadFile(file.path, 'utf-8', 'scan', signal), signal) ) + stateRecord = asRecord(parsed) } catch (error) { + throwIfAiVaultScanCancelled(signal) // Propagate WSL refusal so an unreachable state file is not cached as no session. - if (error instanceof WslTranscriptFsError) { + if ( + error instanceof WslTranscriptFsError || + (error instanceof Error && error.name === 'AbortError') + ) { throw error } return null @@ -51,7 +58,8 @@ export async function parseKimiSessionFile( // Kimi resume requires the indexed work directory; session IDs alone are not sufficient. const workDirBySessionId = await readKimiWorkDirBySessionId( - kimiSessionIndexPathFromStatePath(file.path) + kimiSessionIndexPathFromStatePath(file.path), + signal ) accumulator.cwd = workDirBySessionId.get(sessionId) ?? null @@ -60,14 +68,19 @@ export async function parseKimiSessionFile( updateTimeline(accumulator, extractString(stateRecord.createdAt)) updateTimeline(accumulator, extractString(stateRecord.updatedAt)) - await consumeKimiWireTranscript(accumulator, kimiPrimaryAgentWirePath(file.path, stateRecord)) + await consumeKimiWireTranscript( + accumulator, + kimiPrimaryAgentWirePath(file.path, stateRecord), + signal + ) return finalizeSession(accumulator, platform) } async function consumeKimiWireTranscript( accumulator: SessionAccumulator, - wirePath: string + wirePath: string, + signal?: AbortSignal ): Promise { let pendingAssistantText: string[] = [] const flushAssistant = (): void => { @@ -79,10 +92,11 @@ async function consumeKimiWireTranscript( } } - const input = openTranscriptReadStream(wirePath, { encoding: 'utf-8' }, 'scan') + const input = openTranscriptReadStream(wirePath, { encoding: 'utf-8' }, 'scan', signal) const lines = createInterface({ input, crlfDelay: Infinity }) try { for await (const line of lines) { + await waitForAiVaultScan(accumulator.messages.drain?.() ?? Promise.resolve(), signal) const record = parseJsonObject(line) if (!record) { continue @@ -106,8 +120,12 @@ async function consumeKimiWireTranscript( } } } catch (error) { + throwIfAiVaultScanCancelled(signal) // Metadata-only sessions may list, but WSL refusal must propagate so missing wire content remains retryable. - if (error instanceof WslTranscriptFsError) { + if ( + error instanceof WslTranscriptFsError || + (error instanceof Error && error.name === 'AbortError') + ) { throw error } } finally { @@ -116,6 +134,7 @@ async function consumeKimiWireTranscript( input.destroy() } flushAssistant() + await waitForAiVaultScan(accumulator.messages.drain?.() ?? Promise.resolve(), signal) } function consumeKimiUserMessage(accumulator: SessionAccumulator, value: unknown): void { diff --git a/src/main/ai-vault/scanners/kimi/session-scanner-kimi-paths.ts b/src/main/ai-vault/scanners/kimi/session-scanner-kimi-paths.ts index ec8cffa90d1..44f8cabd5a7 100644 --- a/src/main/ai-vault/scanners/kimi/session-scanner-kimi-paths.ts +++ b/src/main/ai-vault/scanners/kimi/session-scanner-kimi-paths.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { homedir } from 'node:os' import { basename, dirname, join } from 'node:path' import { createInterface } from 'node:readline' @@ -63,33 +64,40 @@ export function hasKimiSessionIndexCacheEntryForTests(indexPath: string): boolea return workDirCacheByIndexPath.has(indexPath) } -export async function readKimiWorkDirBySessionId(indexPath: string): Promise> { +export async function readKimiWorkDirBySessionId( + indexPath: string, + signal?: AbortSignal +): Promise> { const generation = workDirCacheByIndexPath.beginRead() let identity: Awaited> try { - identity = await wslGatedStat(indexPath, 'scan') + identity = await waitForAiVaultScan(wslGatedStat(indexPath, 'scan', signal), signal) } catch { + throwIfAiVaultScanCancelled(signal) // Failed index stats leave sessions listed without cwd; discard cached enrichment for retry. workDirCacheByIndexPath.delete(indexPath, generation) return new Map() } - return workDirCacheByIndexPath.get( - indexPath, - { - changeTimeMs: identity.ctimeMs, - mtimeMs: identity.mtimeMs, - sizeBytes: identity.size - }, - generation, - () => - parseKimiSessionIndex(indexPath).then(({ map, refused }) => { - // Evict inside the shared loader so every concurrent caller retries refused reads without a signature change. - if (refused) { - workDirCacheByIndexPath.delete(indexPath, generation) - } - return map - }) + return waitForAiVaultScan( + workDirCacheByIndexPath.get( + indexPath, + { + changeTimeMs: identity.ctimeMs, + mtimeMs: identity.mtimeMs, + sizeBytes: identity.size + }, + generation, + () => + parseKimiSessionIndex(indexPath).then(({ map, refused }) => { + // Evict inside the shared loader so every concurrent caller retries refused reads without a signature change. + if (refused) { + workDirCacheByIndexPath.delete(indexPath, generation) + } + return map + }) + ), + signal ) } diff --git a/src/main/ai-vault/scanners/kiro/session-scanner-kiro-parser.ts b/src/main/ai-vault/scanners/kiro/session-scanner-kiro-parser.ts index 0af3592c2ce..993ea6296ed 100644 --- a/src/main/ai-vault/scanners/kiro/session-scanner-kiro-parser.ts +++ b/src/main/ai-vault/scanners/kiro/session-scanner-kiro-parser.ts @@ -1,3 +1,4 @@ +import { waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { basename } from 'node:path' import { wslGatedReadFile } from '../../../native-chat/wsl-transcript-fs-access' import { WslTranscriptFsError } from '../../../native-chat/wsl-transcript-fs-error' @@ -126,11 +127,12 @@ export async function parseKiroSessionFile( file: FileWithMtime, platform: NodeJS.Platform = process.platform, messages?: TranscriptMessageSink, - onSkippedRecords?: (records: SkippedTranscriptRecord[]) => void + onSkippedRecords?: (records: SkippedTranscriptRecord[]) => void, + signal?: AbortSignal ): Promise { const accumulator = createKiroAccumulator( file, - await wslGatedReadFile(file.path, 'utf-8', 'scan'), + await waitForAiVaultScan(wslGatedReadFile(file.path, 'utf-8', 'scan', signal), signal), messages ) if (!accumulator) { @@ -141,6 +143,8 @@ export async function parseKiroSessionFile( const { trailingPartialLine, skippedRecords } = await consumeCompleteJsonlLines({ path: kiroTranscriptPathForMetadata(file.path), start: 0, + signal, + drain: () => messages?.drain?.() ?? Promise.resolve(), onLine: (line) => consumeKiroTranscriptLine(accumulator, line) }) onSkippedRecords?.(skippedRecords) diff --git a/src/main/ai-vault/scanners/muse/session-scanner-muse-parser.ts b/src/main/ai-vault/scanners/muse/session-scanner-muse-parser.ts index e0da113e6ec..3ab7dfe9603 100644 --- a/src/main/ai-vault/scanners/muse/session-scanner-muse-parser.ts +++ b/src/main/ai-vault/scanners/muse/session-scanner-muse-parser.ts @@ -1,3 +1,4 @@ +import { waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { wslGatedReadFile } from '../../../native-chat/wsl-transcript-fs-access' import type { AiVaultSession } from '../../../../shared/ai-vault/contracts/ai-vault-types' import { unwrapMuseLogRecords } from '../../../../shared/muse-session-log' @@ -217,15 +218,25 @@ function foldMuseLines( export async function parseMuseSessionFile( file: FileWithMtime, platform: NodeJS.Platform = process.platform, - messages?: TranscriptMessageSink + messages?: TranscriptMessageSink, + signal?: AbortSignal ): Promise { - return parseMuseSessionContent( - file, - await wslGatedReadFile(file.path, 'utf-8', 'scan'), - platform, - {}, - messages + const content = await waitForAiVaultScan( + wslGatedReadFile(file.path, 'utf-8', 'scan', signal), + signal ) + const accumulator = createAccumulator({ + agent: 'muse', + file, + sessionId: museSessionIdFromFilePath(file.path), + messages + }) + const dedupe: MuseDedupeState = { text: null, ms: null } + for (const line of content.split('\n')) { + foldMuseLines(accumulator, [line], dedupe) + await waitForAiVaultScan(messages?.drain?.() ?? Promise.resolve(), signal) + } + return finalizeSession(accumulator, platform) } export function parseMuseSessionContent( diff --git a/src/main/ai-vault/scanners/omp/session-scanner-omp-subagent-transcripts.ts b/src/main/ai-vault/scanners/omp/session-scanner-omp-subagent-transcripts.ts index bcc3a2476d9..69a39a9dc7b 100644 --- a/src/main/ai-vault/scanners/omp/session-scanner-omp-subagent-transcripts.ts +++ b/src/main/ai-vault/scanners/omp/session-scanner-omp-subagent-transcripts.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { wslGatedReaddir } from '../../../native-chat/wsl-transcript-fs-access' import { basename, dirname, extname, join } from 'node:path' import { LOCAL_EXECUTION_HOST_ID } from '../../../../shared/execution-host' @@ -31,11 +32,18 @@ export function isOmpSubagentTranscriptFileName(name: string, isFile: boolean): } /** Count direct task transcripts only; nested artifacts and grandchildren belong to their own parents. */ -export async function countOmpSubagentTranscripts(transcriptFilePath: string): Promise { +export async function countOmpSubagentTranscripts( + transcriptFilePath: string, + signal?: AbortSignal +): Promise { let entries try { - entries = await wslGatedReaddir(ompArtifactDirFor(transcriptFilePath), 'scan') + entries = await waitForAiVaultScan( + wslGatedReaddir(ompArtifactDirFor(transcriptFilePath), 'scan', signal), + signal + ) } catch { + throwIfAiVaultScanCancelled(signal) return 0 } return entries.filter((entry) => isOmpSubagentTranscriptFileName(entry.name, entry.isFile())) @@ -84,7 +92,10 @@ export function withOmpSubagentTranscriptCount( } return { ...session, - subagentTranscriptCount: await countOmpSubagentTranscripts(transcriptFilePath) + subagentTranscriptCount: await countOmpSubagentTranscripts( + transcriptFilePath, + options?.signal + ) } } } diff --git a/src/main/ai-vault/scanners/opencode/session-scanner-opencode-parser.ts b/src/main/ai-vault/scanners/opencode/session-scanner-opencode-parser.ts index 8535a291a12..30acc251a8c 100644 --- a/src/main/ai-vault/scanners/opencode/session-scanner-opencode-parser.ts +++ b/src/main/ai-vault/scanners/opencode/session-scanner-opencode-parser.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../../ai-vault-scan-cancellation' import { wslGatedReaddir, wslGatedReadFile } from '../../../native-chat/wsl-transcript-fs-access' import { WslTranscriptFsError } from '../../../native-chat/wsl-transcript-fs-error' import { join } from 'node:path' @@ -29,9 +30,13 @@ export function openCodeSessionCwd(record: Record): string | nu export async function parseOpenCodeSessionFile( file: FileWithMtime, platform: NodeJS.Platform = process.platform, - messages?: TranscriptMessageSink + messages?: TranscriptMessageSink, + signal?: AbortSignal ): Promise { - const record = asRecord(JSON.parse(await wslGatedReadFile(file.path, 'utf-8', 'scan')) as unknown) + const parsed: unknown = JSON.parse( + await waitForAiVaultScan(wslGatedReadFile(file.path, 'utf-8', 'scan', signal), signal) + ) + const record = asRecord(parsed) if (!record) { return null } @@ -41,16 +46,20 @@ export async function parseOpenCodeSessionFile( accumulator.cwd = openCodeSessionCwd(record) updateTimeline(accumulator, timeObjectValue(record.time, 'created')) updateTimeline(accumulator, timeObjectValue(record.time, 'updated')) - await consumeOpenCodeMessages(accumulator, findOpenCodeStorageRoot(file.path), sessionId) + await consumeOpenCodeMessages(accumulator, findOpenCodeStorageRoot(file.path), sessionId, signal) return finalizeSession(accumulator, platform) } // Rebuild arbitrary directory order by creation time; undated files sort last so they cannot displace the first prompt. -async function readOpenCodeMessagesInOrder(messageDir: string): Promise[]> { +async function readOpenCodeMessagesInOrder( + messageDir: string, + signal?: AbortSignal +): Promise[]> { let entries try { - entries = await wslGatedReaddir(messageDir, 'scan') + entries = await waitForAiVaultScan(wslGatedReaddir(messageDir, 'scan', signal), signal) } catch (error) { + throwIfAiVaultScanCancelled(signal) // Propagate WSL refusal so an unreachable message directory is not cached as an empty transcript. if (error instanceof WslTranscriptFsError) { throw error @@ -64,10 +73,15 @@ async function readOpenCodeMessagesInOrder(messageDir: string): Promise | null = null try { - message = asRecord( - JSON.parse(await wslGatedReadFile(join(messageDir, entry.name), 'utf-8', 'scan')) as unknown + const parsed: unknown = JSON.parse( + await waitForAiVaultScan( + wslGatedReadFile(join(messageDir, entry.name), 'utf-8', 'scan', signal), + signal + ) ) + message = asRecord(parsed) } catch (error) { + throwIfAiVaultScanCancelled(signal) // Skip malformed live writes, but propagate WSL refusal so incomplete transcripts remain retryable. if (error instanceof WslTranscriptFsError) { throw error @@ -92,14 +106,17 @@ async function readOpenCodeMessagesInOrder(messageDir: string): Promise { if (!storageRoot) { return } for (const message of await readOpenCodeMessagesInOrder( - join(storageRoot, 'message', sessionId) + join(storageRoot, 'message', sessionId), + signal )) { + await waitForAiVaultScan(accumulator.messages.drain?.() ?? Promise.resolve(), signal) const role = extractString(message.role) if (role === 'user' || role === 'assistant') { accumulator.messageCount++ diff --git a/src/main/ai-vault/scanners/session-scanner-graph-parsers.ts b/src/main/ai-vault/scanners/session-scanner-graph-parsers.ts index db7b9b6c8ac..f2d1d81bd5e 100644 --- a/src/main/ai-vault/scanners/session-scanner-graph-parsers.ts +++ b/src/main/ai-vault/scanners/session-scanner-graph-parsers.ts @@ -1,3 +1,6 @@ +import { waitForAiVaultScan } from '../ai-vault-scan-cancellation' +import { rovoSessionCwd, rovoRoleFromKind } from './session-scanner-rovo-metadata' +export { rovoSessionCwd, rovoRoleFromKind } from './session-scanner-rovo-metadata' import { foldOmpTranscriptTitle, type OmpTranscriptTitle } from './omp/session-scanner-omp-title' import { isOmpWrittenRecord } from './omp/session-scanner-omp-written-record' import { @@ -45,28 +48,16 @@ type ParserSessionOptions = { executionHostPlatform?: NodeJS.Platform | null } -/** Scope discovery reads cwd from metadata without opening the context file. */ -export function rovoSessionCwd(metadata: Record): string | null { - return firstString(metadata, [ - 'workspace_path', - 'workspacePath', - 'workspace', - 'cwd', - 'working_directory', - 'workingDirectory', - 'project_path', - 'projectPath' - ]) -} - export async function parseRovoSessionFile( file: FileWithMtime, platform: NodeJS.Platform = process.platform, - messages?: TranscriptMessageSink + messages?: TranscriptMessageSink, + signal?: AbortSignal ): Promise { - const metadata = asRecord( - JSON.parse(await wslGatedReadFile(file.path, 'utf-8', 'scan')) as unknown + const parsed: unknown = JSON.parse( + await waitForAiVaultScan(wslGatedReadFile(file.path, 'utf-8', 'scan', signal), signal) ) + const metadata = asRecord(parsed) if (!metadata) { return null } @@ -88,9 +79,16 @@ export async function parseRovoSessionFile( ) const contextPath = rovoTranscriptPathForMetadata(file.path) - const context = await readJsonObjectIfExists(contextPath) + const context = await waitForAiVaultScan(readJsonObjectIfExists(contextPath, signal), signal) if (context) { - consumeRovoSessionContext(accumulator, context) + for (const message of arrayValue(context.messages)) { + consumeRovoSessionContext(accumulator, { messages: [message] }) + await waitForAiVaultScan(messages?.drain?.() ?? Promise.resolve(), signal) + } + for (const entry of arrayValue(context.message_history)) { + consumeRovoSessionContext(accumulator, { message_history: [entry] }) + await waitForAiVaultScan(messages?.drain?.() ?? Promise.resolve(), signal) + } } return finalizeSession(accumulator, platform) @@ -141,16 +139,6 @@ export function consumeRovoHistoryEntry( addSessionContent(accumulator, role, content, record.timestamp) } -export function rovoRoleFromKind(value: unknown): 'user' | 'assistant' | null { - if (value === 'request') { - return 'user' - } - if (value === 'response') { - return 'assistant' - } - return null -} - function rovoTextParts(parts: unknown[], role: 'user' | 'assistant'): string[] { const textParts: string[] = [] for (const part of parts) { diff --git a/src/main/ai-vault/scanners/session-scanner-rovo-metadata.ts b/src/main/ai-vault/scanners/session-scanner-rovo-metadata.ts new file mode 100644 index 00000000000..679f69ea2ec --- /dev/null +++ b/src/main/ai-vault/scanners/session-scanner-rovo-metadata.ts @@ -0,0 +1,25 @@ +import { firstString } from '../session-scanner-values' + +/** Scope discovery reads cwd from metadata without opening the context file. */ +export function rovoSessionCwd(metadata: Record): string | null { + return firstString(metadata, [ + 'workspace_path', + 'workspacePath', + 'workspace', + 'cwd', + 'working_directory', + 'workingDirectory', + 'project_path', + 'projectPath' + ]) +} + +export function rovoRoleFromKind(value: unknown): 'user' | 'assistant' | null { + if (value === 'request') { + return 'user' + } + if (value === 'response') { + return 'assistant' + } + return null +} diff --git a/src/main/ai-vault/session-scanner-agent-parser.ts b/src/main/ai-vault/session-scanner-agent-parser.ts index 349bfbe3ff1..087723ea6fb 100644 --- a/src/main/ai-vault/session-scanner-agent-parser.ts +++ b/src/main/ai-vault/session-scanner-agent-parser.ts @@ -1,7 +1,7 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from './ai-vault-scan-cancellation' import { parseQoderSessionFile } from './scanners/qoder/session-scanner-qoder-parser' import { parseCodebuddySessionFile } from './scanners/codebuddy/session-scanner-codebuddy-parser' import type { AiVaultSession } from '../../shared/ai-vault/contracts/ai-vault-types' -import { throwIfSignalAborted } from '../../shared/abort-signal-reason' import { parseDevinSessionFile } from './scanners/devin/session-scanner-devin-parser' import { parseAntigravitySessionFile } from './scanners/antigravity/session-scanner-antigravity-parser' import { parseDroidSessionFile } from './scanners/droid/session-scanner-droid-parser' @@ -43,7 +43,7 @@ async function readOpenCodeSqliteCandidate( signal?: AbortSignal, agent?: 'opencode2' | 'zcode' ): Promise { - throwIfSignalAborted(signal) + throwIfAiVaultScanCancelled(signal) const request = { ...sqliteCandidate, platform, signal } if (!messages?.active) { const parse = @@ -52,8 +52,8 @@ async function readOpenCodeSqliteCandidate( : agent === 'zcode' ? parseZcodeSqliteSessionViaWorker : parseOpenCodeSqliteSessionViaWorker - const session = await parse(request) - throwIfSignalAborted(signal) + const session = await waitForAiVaultScan(parse(request), signal) + throwIfAiVaultScanCancelled(signal) return session } const capture = @@ -62,12 +62,13 @@ async function readOpenCodeSqliteCandidate( : agent === 'zcode' ? captureZcodeSqliteSessionViaWorker : captureOpenCodeSqliteSessionViaWorker - const result = await capture(request) + const result = await waitForAiVaultScan(capture(request), signal) for (const message of result.messages) { - throwIfSignalAborted(signal) + throwIfAiVaultScanCancelled(signal) messages.push(message) + await waitForAiVaultScan(messages.drain?.() ?? Promise.resolve(), signal) } - throwIfSignalAborted(signal) + throwIfAiVaultScanCancelled(signal) return result.session } @@ -107,7 +108,7 @@ export async function parseAgentSessionFile( if (sqliteCandidate) { return readOpenCodeSqliteCandidate(sqliteCandidate, platform, messages, signal) } - return parseOpenCodeSessionFile(candidate.file, platform, messages) + return parseOpenCodeSessionFile(candidate.file, platform, messages, signal) } case 'opencode2': { const sqliteCandidate = splitOpenCodeSqliteCandidate(candidate.file.path) @@ -123,11 +124,11 @@ export async function parseAgentSessionFile( : null } case 'grok': - return parseGrokSessionFile(candidate.file, platform, messages) + return parseGrokSessionFile(candidate.file, platform, messages, signal) case 'hermes': return parseHermesSessionFile(candidate.file, platform, messages, signal) case 'rovo': - return parseRovoSessionFile(candidate.file, platform, messages) + return parseRovoSessionFile(candidate.file, platform, messages, signal) case 'openclaw': return parseMessageGraphSessionFile('openclaw', candidate.file, platform, messages) case 'pi': @@ -143,12 +144,12 @@ export async function parseAgentSessionFile( case 'devin': return parseDevinSessionFile(candidate.file, platform, messages, signal) case 'kimi': - return parseKimiSessionFile(candidate.file, platform, messages) + return parseKimiSessionFile(candidate.file, platform, messages, signal) case 'muse': - return parseMuseSessionFile(candidate.file, platform, messages) + return parseMuseSessionFile(candidate.file, platform, messages, signal) case 'jcode': - return parseJcodeSessionFile(candidate.file, platform, messages) + return parseJcodeSessionFile(candidate.file, platform, messages, signal) case 'kiro': - return parseKiroSessionFile(candidate.file, platform, messages, onSkippedRecords) + return parseKiroSessionFile(candidate.file, platform, messages, onSkippedRecords, signal) } } diff --git a/src/main/ai-vault/session-scanner-jsonl-reader.ts b/src/main/ai-vault/session-scanner-jsonl-reader.ts index d11ecd89cd3..6af54b2fce4 100644 --- a/src/main/ai-vault/session-scanner-jsonl-reader.ts +++ b/src/main/ai-vault/session-scanner-jsonl-reader.ts @@ -1,6 +1,6 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from './ai-vault-scan-cancellation' import type { Hash } from 'node:crypto' import { openTranscriptReadStream } from '../native-chat/wsl-transcript-fs-access' -import { throwIfAiVaultScanCancelled } from './ai-vault-scan-cancellation' import { MAX_SESSION_TRANSCRIPT_RECORD_BYTES, type SkippedTranscriptRecord @@ -29,6 +29,7 @@ export async function consumeCompleteJsonlLines(args: { path: string start: number onLine: (line: string) => void + drain?: () => Promise onLineBytes?: (line: Buffer) => void /** Covers [0, start); extended through each complete line and returned for the new end. */ prefixHash?: Hash | null @@ -61,75 +62,81 @@ export async function consumeCompleteJsonlLines(args: { const stream = openTranscriptReadStream(args.path, { start: args.start }, 'scan', args.signal) let chunkStart = args.start - for await (const chunk of stream as AsyncIterable) { - // Check per chunk so cancellation also interrupts giant records and releases the stream. - throwIfAiVaultScanCancelled(args.signal) - bytesRead += chunk.length - const chunkEnd = chunkStart + chunk.length - let lineStart = 0 - let hashedThrough = 0 - let newlineIndex = chunk.indexOf(NEWLINE_BYTE, lineStart) - while (newlineIndex !== -1) { - const newlineOffset = chunkStart + newlineIndex - const recordLength = newlineOffset - consumedThrough - if (discarding || recordLength > MAX_SESSION_TRANSCRIPT_RECORD_BYTES) { - skippedRecords.push({ byteOffset: consumedThrough, approximateBytes: recordLength }) - remainderParts = [] - remainderLength = 0 - discarding = false - } else { - let line = chunk.subarray(lineStart, newlineIndex) - // Only the first line carries a prefix; reset here to avoid allocations for complete lines. - if (remainderLength > 0) { - line = Buffer.concat([...remainderParts, line], recordLength) + try { + for await (const chunk of stream as AsyncIterable) { + // Check per chunk so cancellation also interrupts giant records and releases the stream. + throwIfAiVaultScanCancelled(args.signal) + bytesRead += chunk.length + const chunkEnd = chunkStart + chunk.length + let lineStart = 0 + let hashedThrough = 0 + let newlineIndex = chunk.indexOf(NEWLINE_BYTE, lineStart) + while (newlineIndex !== -1) { + const newlineOffset = chunkStart + newlineIndex + const recordLength = newlineOffset - consumedThrough + if (discarding || recordLength > MAX_SESSION_TRANSCRIPT_RECORD_BYTES) { + skippedRecords.push({ byteOffset: consumedThrough, approximateBytes: recordLength }) remainderParts = [] remainderLength = 0 - } - const lineEnd = line.at(-1) === CARRIAGE_RETURN_BYTE ? line.length - 1 : line.length - if (args.onLineBytes) { - args.onLineBytes(line.subarray(0, lineEnd)) + discarding = false } else { - args.onLine(line.toString('utf-8', 0, lineEnd)) + let line = chunk.subarray(lineStart, newlineIndex) + // Only the first line carries a prefix; reset here to avoid allocations for complete lines. + if (remainderLength > 0) { + line = Buffer.concat([...remainderParts, line], recordLength) + remainderParts = [] + remainderLength = 0 + } + const lineEnd = line.at(-1) === CARRIAGE_RETURN_BYTE ? line.length - 1 : line.length + if (args.onLineBytes) { + args.onLineBytes(line.subarray(0, lineEnd)) + } else { + args.onLine(line.toString('utf-8', 0, lineEnd)) + } } - } - consumedThrough = newlineOffset + 1 - lineStart = newlineIndex + 1 - if (hash) { - hash.update(chunk.subarray(hashedThrough, lineStart)) - hashedThrough = lineStart - committedHash = null - if (args.prefixCheckpoints?.offsets.has(consumedThrough)) { - args.prefixCheckpoints.reached(consumedThrough, hash.copy().digest('hex')) + consumedThrough = newlineOffset + 1 + lineStart = newlineIndex + 1 + if (hash) { + hash.update(chunk.subarray(hashedThrough, lineStart)) + hashedThrough = lineStart + committedHash = null + if (args.prefixCheckpoints?.offsets.has(consumedThrough)) { + args.prefixCheckpoints.reached(consumedThrough, hash.copy().digest('hex')) + } } + if (args.shouldStop?.()) { + stopped = true + break + } + newlineIndex = chunk.indexOf(NEWLINE_BYTE, lineStart) } - if (args.shouldStop?.()) { - stopped = true - break + if (hash && !stopped && hashedThrough < chunk.length) { + committedHash ??= hash.copy() + hash.update(chunk.subarray(hashedThrough)) } - newlineIndex = chunk.indexOf(NEWLINE_BYTE, lineStart) - } - if (hash && !stopped && hashedThrough < chunk.length) { - committedHash ??= hash.copy() - hash.update(chunk.subarray(hashedThrough)) - } - if (stopped) { - remainderParts = [] - remainderLength = 0 - break - } - if (lineStart < chunk.length && !discarding) { - // Include carried prefixes when budgeting an unterminated record. - if (chunkEnd - consumedThrough > MAX_SESSION_TRANSCRIPT_RECORD_BYTES) { + if (stopped) { remainderParts = [] remainderLength = 0 - discarding = true - } else { - // Copy a partial tail so retaining it does not pin the whole chunk buffer. - remainderParts.push(lineStart === 0 ? chunk : Buffer.from(chunk.subarray(lineStart))) - remainderLength = chunkEnd - consumedThrough + break } + if (lineStart < chunk.length && !discarding) { + // Include carried prefixes when budgeting an unterminated record. + if (chunkEnd - consumedThrough > MAX_SESSION_TRANSCRIPT_RECORD_BYTES) { + remainderParts = [] + remainderLength = 0 + discarding = true + } else { + // Copy a partial tail so retaining it does not pin the whole chunk buffer. + remainderParts.push(lineStart === 0 ? chunk : Buffer.from(chunk.subarray(lineStart))) + remainderLength = chunkEnd - consumedThrough + } + } + await waitForAiVaultScan(args.drain?.() ?? Promise.resolve(), args.signal) + chunkStart = chunkEnd } - chunkStart = chunkEnd + } catch (error) { + throwIfAiVaultScanCancelled(args.signal) + throw error } if (discarding) { diff --git a/src/main/ai-vault/session-scanner-parse-wsl-stall.test.ts b/src/main/ai-vault/session-scanner-parse-wsl-stall.test.ts index 708d45c7a3e..8249a546c6f 100644 --- a/src/main/ai-vault/session-scanner-parse-wsl-stall.test.ts +++ b/src/main/ai-vault/session-scanner-parse-wsl-stall.test.ts @@ -13,7 +13,12 @@ vi.mock('node:fs/promises', async (importOriginal) => ({ readdir: mocks.readdir })) +import * as transcriptFs from '../native-chat/wsl-transcript-fs-access' import { parseAgentSessionFileCached } from './parse-cache/session-scanner-parse-cache' +import { + registerTranscriptConsumer, + resetTranscriptConsumersForTests +} from './transcripts/session-transcript-consumers' import { resetSessionParseCacheForTests } from './parse-cache/session-parse-cache-store' import { resetWslTranscriptFsGateForTests, @@ -84,6 +89,7 @@ async function releaseAndSettle(): Promise { beforeEach(() => { resetWslTranscriptFsGateForTests() resetSessionParseCacheForTests() + resetTranscriptConsumersForTests() mocks.open.mockReset() mocks.readdir.mockReset() mocks.readdir.mockResolvedValue([]) @@ -94,10 +100,63 @@ beforeEach(() => { afterEach(async () => { await releaseAndSettle() + resetTranscriptConsumersForTests() + vi.restoreAllMocks() vi.useRealTimers() }) describe('AI Vault session parse with a stalled WSL transcript body read', () => { + it('cancels stalled EOF finalization, cleans up, and admits the next read', async () => { + const bytes = body(2) + const file = candidate(STALLED_PATH.replace('a.jsonl', 'finalization.jsonl'), bytes, 1) + mocks.open.mockResolvedValue(servingHandle(bytes)) + let entered!: () => void + const finalizing = new Promise((resolve) => { + entered = resolve + }) + const readdir = vi.spyOn(transcriptFs, 'wslGatedReaddir').mockImplementationOnce(() => { + entered() + return new Promise(() => {}) + }) + const finishes = vi.fn() + registerTranscriptConsumer({ + beginRead: () => ({ message: () => {}, finish: finishes }) + }) + const controller = new AbortController() + const first = parseAgentSessionFileCached( + file, + 'linux', + undefined, + undefined, + controller.signal + ) + const cancelled = first.catch((error) => { + expect(error).toMatchObject({ + name: 'AbortError', + message: 'Agent Session History scan was cancelled' + }) + return 'cancelled' + }) + await finalizing + let nextMessageCount: number | undefined + const next = parseAgentSessionFileCached(file, 'linux').then((session) => { + nextMessageCount = session?.messageCount + return session + }) + let result: string | undefined + void cancelled.then((value) => { + result = typeof value === 'string' ? value : 'completed' + }) + controller.abort() + await vi.advanceTimersByTimeAsync(100) + expect(result).toBe('cancelled') + expect(finishes).toHaveBeenNthCalledWith(1, expect.objectContaining({ cancelled: true })) + expect(nextMessageCount).toBe(2) + await next + expect(finishes).toHaveBeenCalledTimes(2) + readdir.mockRestore() + }) + it('caches no resume point when the newline probe is refused mid-append', async () => { const first = body(2) mocks.open.mockResolvedValue(servingHandle(first)) diff --git a/src/main/ai-vault/session-scanner-types.ts b/src/main/ai-vault/session-scanner-types.ts index 54b7eadcb08..3fafdc435af 100644 --- a/src/main/ai-vault/session-scanner-types.ts +++ b/src/main/ai-vault/session-scanner-types.ts @@ -104,6 +104,7 @@ export type SessionParseResult = { } export type ResumableParseFinalizeOptions = { + signal?: AbortSignal executionHostId?: ExecutionHostId executionHostPlatform?: NodeJS.Platform | null } diff --git a/src/main/ai-vault/transcripts/session-document-stream.ts b/src/main/ai-vault/transcripts/session-document-stream.ts index 4fc710ef6f3..b6a186082e9 100644 --- a/src/main/ai-vault/transcripts/session-document-stream.ts +++ b/src/main/ai-vault/transcripts/session-document-stream.ts @@ -1,8 +1,8 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../ai-vault-scan-cancellation' import { StringDecoder } from 'node:string_decoder' import { FlexAssembler, arrayRule, objectRule } from 'stream-json/core/utils/flex-assembler.js' import { setImmediate as yieldToEventLoop } from 'node:timers/promises' import { createJsonTokenReader } from '../../../shared/json-stream/json-token-reader' -import { throwIfAiVaultScanCancelled } from '../ai-vault-scan-cancellation' /** Fold one root array while retaining only the root fields the agent parser uses. */ export async function readStreamedSessionDocument(args: { @@ -13,6 +13,7 @@ export async function readStreamedSessionDocument(args: { create: () => T consume: (state: T, value: unknown) => void signal?: AbortSignal + drain?: () => Promise }): Promise<{ record: Record; state: T } | null> { const record: Record = Object.create(null) const fields = new Set(args.fields) @@ -122,16 +123,23 @@ export async function readStreamedSessionDocument(args: { } ) const decoder = new StringDecoder('utf8') - for await (const chunk of args.bytes) { + try { + for await (const chunk of args.bytes) { + throwIfAiVaultScanCancelled(args.signal) + parser.write(decoder.write(chunk)) + await waitForAiVaultScan(args.drain?.() ?? Promise.resolve(), args.signal) + await waitForAiVaultScan(yieldToEventLoop(undefined, { signal: args.signal }), args.signal) + } + } catch (error) { throwIfAiVaultScanCancelled(args.signal) - parser.write(decoder.write(chunk)) - await yieldToEventLoop() + throw error } const tail = decoder.end() if (tail) { parser.write(tail) } parser.end() + await waitForAiVaultScan(args.drain?.() ?? Promise.resolve(), args.signal) throwIfAiVaultScanCancelled(args.signal) if (consumeFailure) { throw consumeFailure.error diff --git a/src/main/ai-vault/transcripts/session-transcript-channel.ts b/src/main/ai-vault/transcripts/session-transcript-channel.ts index c26b27428d5..ccf17eecd1a 100644 --- a/src/main/ai-vault/transcripts/session-transcript-channel.ts +++ b/src/main/ai-vault/transcripts/session-transcript-channel.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../ai-vault-scan-cancellation' import { hasTranscriptConsumers, transcriptConsumers, @@ -67,6 +68,16 @@ export class TranscriptMessageChannel implements TranscriptMessageSink { } } + async drain(): Promise { + throwIfAiVaultScanCancelled(this.start?.signal) + if (this.start?.indexing) { + await waitForAiVaultScan( + Promise.all(this.readers.flatMap((reader) => (reader.drain ? [reader.drain()] : []))), + this.start.signal + ) + } + } + reset(): void { const start = this.start if (!start || !this.emitted) { diff --git a/src/main/ai-vault/transcripts/session-transcript-consumers.ts b/src/main/ai-vault/transcripts/session-transcript-consumers.ts index cf97ab28829..6a23a76a59a 100644 --- a/src/main/ai-vault/transcripts/session-transcript-consumers.ts +++ b/src/main/ai-vault/transcripts/session-transcript-consumers.ts @@ -19,6 +19,7 @@ export type TranscriptMessageSink = { readonly active: boolean push(message: TranscriptMessage): void /** A later duplicate JSON array replaces every message emitted from the earlier array. */ + drain?: () => Promise reset?: () => void } @@ -38,6 +39,8 @@ export type TranscriptSessionIdentity = { export type TranscriptReadStart = { candidate: SessionFileCandidate + indexing?: boolean + signal?: AbortSignal /** `replace`: the whole file is being re-read; `append`: a resumed read. */ mode: 'replace' | 'append' /** Byte offset the messages of this read continue from. */ @@ -63,6 +66,7 @@ export type TranscriptReadOutcome = { export type TranscriptReadConsumer = { message(message: TranscriptMessage): void + drain?(): Promise finish(outcome: TranscriptReadOutcome): void /** The read has consumed exactly [0, offset), whose bytes hash to digest. */ prefixReached?(offset: number, digest: string): void diff --git a/src/main/ai-vault/transcripts/session-transcript-reader.ts b/src/main/ai-vault/transcripts/session-transcript-reader.ts index 8321d0361c0..c61449a5104 100644 --- a/src/main/ai-vault/transcripts/session-transcript-reader.ts +++ b/src/main/ai-vault/transcripts/session-transcript-reader.ts @@ -1,3 +1,4 @@ +import { throwIfAiVaultScanCancelled, waitForAiVaultScan } from '../ai-vault-scan-cancellation' import { createHash } from 'node:crypto' import { readTranscriptSlice } from '../../native-chat/wsl-transcript-fs-access' import type { AiVaultSession } from '../../../shared/ai-vault/contracts/ai-vault-types' @@ -14,10 +15,10 @@ import { mergeSkippedTranscriptRecords, type SkippedTranscriptRecord } from './session-transcript-record-budget' -import { throwIfAiVaultScanCancelled } from '../ai-vault-scan-cancellation' type TranscriptSessionEnrichment = { /** Complete consumer metadata before committing; the returned fold stays unmodified. */ + indexing?: boolean sessionForConsumers?: (session: AiVaultSession | null) => Promise } @@ -89,6 +90,8 @@ export async function readResumableTranscript( !canResume && prefixHash ? nonEmpty(transcriptResumableOffsets(args.candidate)) : undefined channel.beginRead({ candidate: args.candidate, + indexing: args.indexing, + signal: args.signal, mode: canResume ? 'append' : 'replace', previousByteOffset: startOffset, identity: () => state.identity?.() ?? null, @@ -104,6 +107,7 @@ export async function readResumableTranscript( reached: (offset, digest) => channel.prefixReached(offset, digest) }, onLine: (line) => state.consumeLine(line), + drain: () => channel.drain(), // Preserve `this` for parsers that implement these hooks as methods. onLineBytes: state.consumeLineBytes?.bind(state), shouldStop: state.shouldStop?.bind(state), @@ -123,9 +127,10 @@ export async function readResumableTranscript( channel.mute(() => displayState.consumeLine(partialLine)) } - const session = await displayState.finalize(args.platform) + throwIfAiVaultScanCancelled(args.signal) + const session = await displayState.finalize(args.platform, { signal: args.signal }) const consumerSession = args.sessionForConsumers - ? await args.sessionForConsumers(session) + ? await waitForAiVaultScan(args.sessionForConsumers(session), args.signal) : session throwIfAiVaultScanCancelled(args.signal) channel.finishRead({ @@ -155,8 +160,10 @@ export async function readResumableTranscript( session: null, byteOffset: startOffset, incomplete: true, - cancelled: args.signal?.aborted === true + cancelled: + args.signal?.aborted === true || (error instanceof Error && error.name === 'AbortError') }) + throwIfAiVaultScanCancelled(args.signal) throw error } } @@ -177,7 +184,13 @@ export async function readWholeTranscript( args.stats.bytesRead += file.sizeBytes ?? 0 } const channel = new TranscriptMessageChannel() - channel.beginRead({ candidate: args.candidate, mode: 'replace', previousByteOffset: 0 }) + channel.beginRead({ + candidate: args.candidate, + mode: 'replace', + previousByteOffset: 0, + indexing: args.indexing, + signal: args.signal + }) try { const session = await parseAgentSessionFile( args.candidate, @@ -187,7 +200,7 @@ export async function readWholeTranscript( args.onSkippedRecords ) const consumerSession = args.sessionForConsumers - ? await args.sessionForConsumers(session) + ? await waitForAiVaultScan(args.sessionForConsumers(session), args.signal) : session throwIfAiVaultScanCancelled(args.signal) channel.finishRead({ @@ -201,8 +214,10 @@ export async function readWholeTranscript( session: null, byteOffset: 0, incomplete: true, - cancelled: args.signal?.aborted === true + cancelled: + args.signal?.aborted === true || (error instanceof Error && error.name === 'AbortError') }) + throwIfAiVaultScanCancelled(args.signal) throw error } } @@ -217,6 +232,9 @@ async function endsWithNewlineAt( offset: number, signal?: AbortSignal ): Promise { - const slice = await readTranscriptSlice(path, offset - 1, 1, 'scan', signal) + const slice = await waitForAiVaultScan( + readTranscriptSlice(path, offset - 1, 1, 'scan', signal), + signal + ) return slice.length === 1 && slice[0] === NEWLINE_BYTE } diff --git a/src/main/ipc/ai-vault/ai-vault-search-all-hosts-real-index.test.ts b/src/main/ipc/ai-vault/ai-vault-search-all-hosts-real-index.test.ts index b64d0911e23..8ad7e9e9ff4 100644 --- a/src/main/ipc/ai-vault/ai-vault-search-all-hosts-real-index.test.ts +++ b/src/main/ipc/ai-vault/ai-vault-search-all-hosts-real-index.test.ts @@ -1,3 +1,4 @@ +import '../../ai-vault-search/session-search-worker.test-fixture' import { mkdir, rm, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { afterEach, beforeEach, expect, it } from 'vitest' @@ -139,7 +140,7 @@ it('hands out every hit on every host exactly once, at limit 5 and limit 20', as } }) -it('fences the purged host and keeps the other two paginating', async () => { +it('replays snapshot tails then fences the changed host at its next read while healthy hosts finish', async () => { const request = { query: 'needle', limit: 5 } const first = resultsOf(await searchAllExecutionHosts(request, legs())) expect(first.hosts?.every((host) => host.outcome === 'searched')).toBe(true) @@ -151,25 +152,27 @@ it('fences the purged host and keeps the other two paginating', async () => { const second = resultsOf( await searchAllExecutionHosts({ ...request, cursor: first.page.cursor! }, legs()) ) - expect(second.hosts).toContainEqual({ - executionHostId: 'ssh:beta', - outcome: 'stale' - }) - expect(second.hits.some((hit) => hit.executionHostId === 'ssh:beta')).toBe(false) + expect(second.hosts).toContainEqual({ executionHostId: 'ssh:beta', outcome: 'searched' }) + expect(second.hits.some((hit) => hit.executionHostId === 'ssh:beta')).toBe(true) const seen = [...keysOf(first), ...keysOf(second)] let cursor = second.page.cursor let pages = 0 + let stale = false while (cursor !== null) { const page = resultsOf(await searchAllExecutionHosts({ ...request, cursor }, legs())) + stale ||= + page.hosts?.some((host) => host.executionHostId === 'ssh:beta' && host.outcome === 'stale') ?? + false seen.push(...keysOf(page)) cursor = page.page.cursor // Fail boundedly if a host is never retired. expect((pages += 1)).toBeLessThan(40) } - // Purged beta retains only previously emitted hits; healthy hosts finish without duplicates. + expect(stale).toBe(true) + // The first host page was already hydrated, so its carried tail survives the write. expect(new Set(seen).size).toBe(seen.length) - const betaEmitted = keysOf(first).filter((key) => key.startsWith('ssh:beta/')) + const betaEmitted = [39, 38, 37, 36, 35].map((id) => `ssh:beta/${sessionIdFor(id)}`) expect([...seen].sort()).toEqual( [ ...hosts[0]!.sessionIds.map((id) => `local/${id}`), diff --git a/src/main/ipc/ai-vault/ai-vault-search-all-hosts.test.ts b/src/main/ipc/ai-vault/ai-vault-search-all-hosts.test.ts index 5a2e394c738..694e5315755 100644 --- a/src/main/ipc/ai-vault/ai-vault-search-all-hosts.test.ts +++ b/src/main/ipc/ai-vault/ai-vault-search-all-hosts.test.ts @@ -162,3 +162,56 @@ it('reads at most three pages from one host per merged request', async () => { 'a-11' ]) }) + +it('replays bounded partial and terminal host pages without refetching or inheriting another query scope', async () => { + const a = new StubHost('local', sessions('a', 2)) + const b = new StubHost('ssh:b', sessions('b', 2)) + const request = { query: 'needle', limit: 2, filters: { scopePaths: ['/repo'] } } + const first = resultsOf(await searchAllExecutionHosts(request, [a.leg(), b.leg()])) + expect(first.hits.map((hit) => hit.sessionId)).toEqual(['a-0', 'b-0']) + // A new request would fail; the terminal tails have already been read and need no validation call. + const fail = (executionHostId: ExecutionHostId): SessionSearchHostLeg => ({ + executionHostId, + search: () => { + throw new Error('must not refetch a captured page') + } + }) + const next = resultsOf( + await searchAllExecutionHosts({ ...request, cursor: first.page.cursor! }, [ + fail('local'), + fail('ssh:b') + ]) + ) + expect(next.hits.map((hit) => hit.sessionId)).toEqual(['a-1', 'b-1']) + expect(next.page).toEqual({ hasMore: false, cursor: null }) + expect( + await searchAllExecutionHosts({ ...request, query: 'other', cursor: first.page.cursor! }, [ + a.leg(), + b.leg() + ]) + ).toEqual({ kind: 'malformed-cursor' }) + expect( + await searchAllExecutionHosts( + { ...request, filters: { scopePaths: ['/private'] }, cursor: first.page.cursor! }, + [a.leg(), b.leg()] + ) + ).toEqual({ kind: 'malformed-cursor' }) +}) + +it('accepts an existing merged cursor and continues using an older host opaque cursor', async () => { + const host = new StubHost('local', sessions('a', 4)) + const cursor = Buffer.from( + JSON.stringify({ l: 2, s: 'relevance', h: { local: { c: null, e: 1, g: 1 } } }) + ).toString('base64url') + const page = resultsOf( + await searchAllExecutionHosts({ query: 'needle', limit: 2, cursor }, [host.leg()]) + ) + expect(page.hits.map((hit) => hit.sessionId)).toEqual(['a-1', 'a-2']) + const tail = resultsOf( + await searchAllExecutionHosts({ query: 'needle', limit: 2, cursor: page.page.cursor! }, [ + host.leg() + ]) + ) + expect(tail.hits.map((hit) => hit.sessionId)).toEqual(['a-3']) + expect(tail.page.hasMore).toBe(false) +}) diff --git a/src/main/ipc/ai-vault/ai-vault-search-all-hosts.ts b/src/main/ipc/ai-vault/ai-vault-search-all-hosts.ts index de51683c059..595908e8c1c 100644 --- a/src/main/ipc/ai-vault/ai-vault-search-all-hosts.ts +++ b/src/main/ipc/ai-vault/ai-vault-search-all-hosts.ts @@ -1,3 +1,5 @@ +import { createHash } from 'node:crypto' +import { sessionSearchPageKey } from '../../ai-vault-search/session-search-page-cursor' import { resolveSessionSearchLimit } from '../../../shared/ai-vault/search/ai-vault-search-limit' import type { AiVaultSearchHit, @@ -55,12 +57,22 @@ export async function searchAllExecutionHosts( const startedAt = Date.now() const limit = resolveSessionSearchLimit(request.limit) const sort: MergedSort = request.filters?.sort ?? 'relevance' + const key = createHash('sha256') + .update( + JSON.stringify([ + sessionSearchPageKey(request), + request.within ?? null, + legs.map((leg) => leg.executionHostId).sort() + ]) + ) + .digest('base64url') const resumed = request.cursor === undefined ? null : decodeMergedSearchCursor(request.cursor) if (request.cursor !== undefined) { const known = new Set(legs.map((leg) => leg.executionHostId)) // A cursor belongs to one query over one host set; anything else is not ours. if ( !resumed || + (resumed.key !== undefined && resumed.key !== key) || resumed.limit !== limit || resumed.sort !== sort || Object.keys(resumed.hosts).some((executionHostId) => !known.has(executionHostId)) @@ -77,7 +89,7 @@ export async function searchAllExecutionHosts( walks.map((walk) => fetchHostPage(walk, resumed?.hosts[walk.executionHostId] ?? null)) ) const hits = await drainMergedPage(walks, limit, sort) - return mergedSearchResponse(walks, { limit, sort }, hits, Date.now() - startedAt) + return mergedSearchResponse(walks, { limit, sort, key }, hits, Date.now() - startedAt) } /** Every leg answers in the merged order; the cursor and debug are this merge's own. */ @@ -109,6 +121,17 @@ function newHostWalk(leg: SessionSearchHostLeg, request: AiVaultSearchRequest): } async function fetchHostPage(walk: HostWalk, entry: MergedSearchCursorEntry | null): Promise { + if (entry?.p !== undefined) { + // Why: these hits were already hydrated; only the next host read validates new content. + walk.pending = stampExecutionHost(entry.p, walk.executionHostId) + walk.nextCursor = entry.c + walk.generation = entry.g + walk.outcome = 'searched' + if (entry.t) { + walk.truncated = { ...entry.t } + } + return + } walk.cursor = entry?.c ?? null walk.emitted = entry?.e ?? 0 walk.generation = entry?.g ?? 0 @@ -217,7 +240,7 @@ function mostRecentWalk(walks: readonly HostWalk[]): HostWalk | null { function mergedSearchResponse( walks: readonly HostWalk[], - query: { limit: number; sort: MergedSort }, + query: { limit: number; sort: MergedSort; key: string }, hits: AiVaultSearchHit[], durationMs: number ): AiVaultSearchResponse { @@ -256,13 +279,13 @@ function mergedSearchResponse( } } -/** Resume where this request stopped: mid-page by skip count, else the unread page. */ +/** Carry already-read hits verbatim; the next cursor starts after that captured page. */ function nextCursorEntry(walk: HostWalk): MergedSearchCursorEntry | null { if (walk.pending.length > 0) { - return { c: walk.cursor, e: walk.emitted, g: walk.generation } + return { c: walk.nextCursor, e: 0, g: walk.generation, p: walk.pending, t: walk.truncated } } if (walk.nextCursor !== null) { - return { c: walk.nextCursor, e: 0, g: walk.generation } + return { c: walk.nextCursor, e: 0, g: walk.generation, p: [], t: walk.truncated } } return walk.carry ? { c: walk.cursor, e: walk.emitted, g: walk.generation } : null } diff --git a/src/main/ipc/ai-vault/ai-vault-search-merged-cursor.ts b/src/main/ipc/ai-vault/ai-vault-search-merged-cursor.ts index e5c1efabd80..140f5e24768 100644 --- a/src/main/ipc/ai-vault/ai-vault-search-merged-cursor.ts +++ b/src/main/ipc/ai-vault/ai-vault-search-merged-cursor.ts @@ -1,20 +1,31 @@ +import { + AiVaultSearchHitSchema, + AiVaultSearchTruncationSchema +} from '../../../shared/ai-vault/search/ai-vault-search-contract' +import { SESSION_SEARCH_LIMIT_MAX } from '../../../shared/ai-vault/search/ai-vault-search-limit' +import type { AiVaultSearchHit } from '../../../shared/ai-vault/search/ai-vault-search-types' import { z } from 'zod' -/** Refetch host cursor c, skip e emitted hits within generation g; null c starts that host's walk. */ +/** Legacy entries refetch c and skip e; carried entries replay p, then read c without an offset. */ export type MergedSearchCursorEntry = { c: string | null e: number g: number + p?: AiVaultSearchHit[] + t?: z.infer } export type MergedSearchCursor = { /** Page size and sort the cursor was minted for; a cursor belongs to one query. */ limit: number sort: 'relevance' | 'newest' + key?: string hosts: Record } const mergedCursorPayloadSchema = z.object({ + v: z.literal(2).optional(), + k: z.string().max(128).optional(), l: z.number().int().positive(), s: z.enum(['relevance', 'newest']), h: z.record( @@ -22,18 +33,23 @@ const mergedCursorPayloadSchema = z.object({ z.object({ c: z.string().nullable(), e: z.number().int().nonnegative(), - g: z.number().int().nonnegative() + g: z.number().int().nonnegative(), + p: z.array(AiVaultSearchHitSchema).max(SESSION_SEARCH_LIMIT_MAX).optional(), + t: AiVaultSearchTruncationSchema.optional() }) ) }) export function encodeMergedSearchCursor(cursor: MergedSearchCursor): string { - const payload = { l: cursor.limit, s: cursor.sort, h: cursor.hosts } + const payload = { v: 2, k: cursor.key, l: cursor.limit, s: cursor.sort, h: cursor.hosts } return Buffer.from(JSON.stringify(payload), 'utf8').toString('base64url') } /** Reject malformed cursor shapes. */ export function decodeMergedSearchCursor(raw: string): MergedSearchCursor | null { + if (raw.length > 16 * 1024 * 1024) { + return null + } let parsed: unknown try { parsed = JSON.parse(Buffer.from(raw, 'base64url').toString('utf8')) @@ -41,8 +57,14 @@ export function decodeMergedSearchCursor(raw: string): MergedSearchCursor | null return null } const result = mergedCursorPayloadSchema.safeParse(parsed) - if (!result.success) { + if ( + !result.success || + (result.data.v === 2 && !result.data.k) || + (result.success && + result.data.v !== 2 && + Object.values(result.data.h).some((entry) => entry.p !== undefined)) + ) { return null } - return { limit: result.data.l, sort: result.data.s, hosts: result.data.h } + return { key: result.data.k, limit: result.data.l, sort: result.data.s, hosts: result.data.h } } diff --git a/src/main/ipc/ai-vault/ai-vault-search.test.ts b/src/main/ipc/ai-vault/ai-vault-search.test.ts index e91e519834d..0970ae51edb 100644 --- a/src/main/ipc/ai-vault/ai-vault-search.test.ts +++ b/src/main/ipc/ai-vault/ai-vault-search.test.ts @@ -22,6 +22,7 @@ vi.mock('../ssh/ssh', () => ({ getActiveSshAiVaultHostInfos: sshHostInfos })) +import { createChildSessionSearchService } from '../../ai-vault-search/session-search-child-service' import { registerAiVaultSearchHandlers } from './ai-vault-search' import { aiVaultApi } from '../../../preload/api/ai-vault-bridge' import { setSessionSearchService } from '../../ai-vault-search/session-search-service-registry' @@ -102,3 +103,18 @@ describe('desktop IPC and preload search boundary', () => { expect(sshSearch).not.toHaveBeenCalled() }) }) + +it('preserves a child reader failure through registry, IPC and preload', async () => { + const service = fakeSearchService() + service.search.mockRejectedValue(new Error('reader deadline exceeded')) + setSessionSearchService( + createChildSessionSearchService({ + search: service.search, + status: service.status, + reconcile: service.reconcile + }) + ) + await expect(aiVaultApi.searchSessions({ query: 'needle' }, 'local')).rejects.toThrow( + 'reader deadline exceeded' + ) +}) diff --git a/src/main/native-chat/wsl-transcript-fs-access.ts b/src/main/native-chat/wsl-transcript-fs-access.ts index 82607b9017c..31e20cb81c0 100644 --- a/src/main/native-chat/wsl-transcript-fs-access.ts +++ b/src/main/native-chat/wsl-transcript-fs-access.ts @@ -2,6 +2,7 @@ import { createReadStream, type Dirent, type Stats } from 'node:fs' import { access, lstat, open, readdir, readFile, stat, type FileHandle } from 'node:fs/promises' import { Readable } from 'node:stream' import { StringDecoder } from 'node:string_decoder' +import { throwIfSignalAborted, waitForPromiseWithSignal } from '../../shared/abort-signal-reason' import { isWslUncPath } from '../../shared/wsl-paths' import { runWslTranscriptFsTask, type WslTranscriptFsTaskPriority } from './wsl-transcript-fs-gate' import { @@ -41,13 +42,14 @@ function runReusableFsOperation( signal: AbortSignal | undefined, localTask: () => Promise ): Promise { - return isWslUncPath(request.path) + throwIfSignalAborted(signal) + const pending = isWslUncPath(request.path) ? admitWslTranscriptPath(request.path).then(() => runWslTranscriptFsTask( { operation: request.operation, path: request.path, priority, signal }, async (taskSignal) => { // Queued work runs later: ask again right before the touch. - await admitWslTranscriptPath(request.path) + await waitForPromiseWithSignal(admitWslTranscriptPath(request.path), taskSignal) return runWslTranscriptFsProcess( request, taskSignal, @@ -57,6 +59,7 @@ function runReusableFsOperation( ) ) : localTask() + return waitForPromiseWithSignal(pending, signal) } export function wslGatedAccess( @@ -84,7 +87,9 @@ export function transcriptFileStat( signal?: AbortSignal ): Promise { const ssh = parseSshTranscriptPath(path) - return ssh ? statSshTranscript(ssh) : wslGatedStat(path, priority, signal) + return ssh + ? waitForPromiseWithSignal(statSshTranscript(ssh), signal) + : wslGatedStat(path, priority, signal) } export function wslGatedLstat( @@ -127,7 +132,17 @@ export function openTranscriptFile( return Promise.resolve({ sshTranscript: ssh }) } if (!isWslUncPath(path)) { - return open(path, 'r') + throwIfSignalAborted(signal) + const pending = open(path, 'r') + return waitForPromiseWithSignal(pending, signal).catch((error: unknown) => { + void pending + .then( + (handle) => handle.close(), + () => undefined + ) + .catch(() => undefined) + throw error + }) } return openAdmittedWslTranscriptFile(path, priority, signal) } @@ -137,7 +152,7 @@ async function openAdmittedWslTranscriptFile( priority: WslTranscriptFsTaskPriority, signal?: AbortSignal ): Promise { - await admitWslTranscriptPath(path) + await waitForPromiseWithSignal(admitWslTranscriptPath(path), signal) return runWslTranscriptFsTask( { operation: 'open', @@ -148,7 +163,7 @@ async function openAdmittedWslTranscriptFile( onAbandonedResult: (handle) => void closeTranscriptHandle(handle, path) }, async (taskSignal) => { - await admitWslTranscriptPath(path) + await waitForPromiseWithSignal(admitWslTranscriptPath(path), taskSignal) return openWslTranscriptFsProcess(path, taskSignal, wslTranscriptFsLaneKey(path, priority)) } ) @@ -167,17 +182,20 @@ export function readTranscriptFile( signal?: AbortSignal ): Promise<{ bytesRead: number; buffer: Buffer }> { if (isSshTranscriptHandle(handle)) { - return readSshTranscript(handle) + return waitForPromiseWithSignal(readSshTranscript(handle), signal) } // Process handle identity overrides path spelling; never route it into native FileHandle reads. if (!isWslUncPath(path) && !isWslTranscriptFsProcessHandle(handle)) { - return (handle as FileHandle).read(buffer, offset, length, position) + return waitForPromiseWithSignal( + (handle as FileHandle).read(buffer, offset, length, position), + signal + ) } return runWslTranscriptFsTask( { operation: 'read', path, priority, signal, dedupe: false }, async (taskSignal) => { // An open 9P handle still reconnects (and boots) a distro that stopped since the open. - await admitWslTranscriptPath(path) + await waitForPromiseWithSignal(admitWslTranscriptPath(path), taskSignal) if (!isWslTranscriptFsProcessHandle(handle)) { return (handle as FileHandle).read(buffer, offset, length, position) } @@ -188,6 +206,17 @@ export function readTranscriptFile( ) } +/** An aborted close wait must not replace the read's own outcome or fail stream teardown. */ +function closeTranscriptHandleWithSignal( + handle: TranscriptFileHandle, + path: string, + signal?: AbortSignal +): Promise { + return waitForPromiseWithSignal(closeTranscriptHandle(handle, path), signal).catch( + (error: unknown) => (signal?.aborted ? undefined : Promise.reject(error)) + ) +} + /** Never gated; process-owned handles retire on the client's bounded deadline. */ export function closeTranscriptHandle(handle: TranscriptFileHandle, path: string): Promise { if (isSshTranscriptHandle(handle)) { @@ -228,7 +257,7 @@ export async function readTranscriptSlice( ) return buffer.subarray(0, bytesRead) } finally { - await closeTranscriptHandle(handle, path) + await closeTranscriptHandleWithSignal(handle, path, signal) } } @@ -289,7 +318,7 @@ async function* gatedChunks( } } finally { // Readable.destroy calls generator return, closing handles even after aborted head reads. - await closeTranscriptHandle(handle, path) + await closeTranscriptHandleWithSignal(handle, path, signal) } } diff --git a/src/main/observability/tracer.ts b/src/main/observability/tracer.ts index f159ef05a1b..1bf4b45b4ec 100644 --- a/src/main/observability/tracer.ts +++ b/src/main/observability/tracer.ts @@ -210,3 +210,12 @@ export function _resetTracerForTests(): void { activeSink = null // Context tests should create their own withSpan scope. } + +/** Worker spans are already redacted before crossing the internal transport. */ +export function recordRelayedSpan(record: unknown): void { + try { + activeSink?.push(record) + } catch { + /* Diagnostics cannot fail a request. */ + } +} diff --git a/src/main/worker-threads/lazy-worker-thread-host.ts b/src/main/worker-threads/lazy-worker-thread-host.ts index a3a0f5ea464..1f41cf8d4a0 100644 --- a/src/main/worker-threads/lazy-worker-thread-host.ts +++ b/src/main/worker-threads/lazy-worker-thread-host.ts @@ -85,6 +85,10 @@ export class LazyWorkerThreadHost { } } + retired(): Promise { + return this.retiring ?? Promise.resolve() + } + scheduleIdleTeardown(): void { this.clearIdleTimer() if (!this.worker) { diff --git a/src/renderer/src/components/right-sidebar/ai-vault/search/AiVaultPanelSearch.tsx b/src/renderer/src/components/right-sidebar/ai-vault/search/AiVaultPanelSearch.tsx index f23431773e4..9730dba00e1 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/search/AiVaultPanelSearch.tsx +++ b/src/renderer/src/components/right-sidebar/ai-vault/search/AiVaultPanelSearch.tsx @@ -101,7 +101,7 @@ export function AiVaultPanelSearch({ if (needsLocalConsent) { message = t( 'sessionSearch.panel.consent', - 'Enable full-text search? Orca builds an index on this computer from local agent transcripts, including full conversations and up to 3,072 characters per tool output. Content is not redacted. Authenticated paired clients can search it.' + 'Enable full-text search? Orca builds an index on this computer from local agent transcripts, including full conversations. Content is not redacted. Authenticated paired clients can search it.' ) } else if (noAgents) { message = t('auto.components.right.sidebar.AiVaultPanel.noAgentsSelected', 'No agents selected') @@ -157,7 +157,7 @@ export function AiVaultPanelSearch({ ) } } - if (response?.kind === 'results' && search.hits.length === 0) { + if (response?.kind === 'results' && search.hits.length === 0 && !message) { message = t( 'sessionSearch.panel.noMatches', 'No matching sessions in the indexed history. Try another query or scope.' diff --git a/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.test.tsx b/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.test.tsx index 29491600d01..96afcbb7a58 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.test.tsx +++ b/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.test.tsx @@ -135,6 +135,31 @@ it('does not revive old results or cursors before debounce when returning from a unmount() }) +it('keeps loaded rows while a continuation is pending and appends its captured page', async () => { + searchSessions.mockResolvedValueOnce({ + ...searchResults(), + hits: [{ ...searchHit(), sessionId: 'first' }], + page: { cursor: 'next', hasMore: true } + }) + const request = { query: 'needle' } + const { result, unmount } = renderHook(() => useAiVaultSearch(request, 'local', '')) + await debounce() + let resolve!: (response: AiVaultSearchResponse) => void + searchSessions.mockImplementationOnce( + () => + new Promise((done) => { + resolve = done + }) + ) + act(() => result.current.loadMore()) + expect(result.current.hits.map((hit) => hit.sessionId)).toEqual(['first']) + await act(async () => + resolve({ ...searchResults(), hits: [{ ...searchHit(), sessionId: 'second' }] }) + ) + expect(result.current.hits.map((hit) => hit.sessionId)).toEqual(['first', 'second']) + unmount() +}) + it('keeps the last hits on screen until a newer query answers, ignoring the older reply', async () => { const hit = (sessionId: string) => ({ ...searchHit(), sessionId }) let answerOld = (_response: AiVaultSearchResponse): void => {} diff --git a/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.ts b/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.ts index 9811b908091..89ddefa2a42 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault/search/use-ai-vault-search.ts @@ -72,11 +72,9 @@ export function useAiVaultSearch( started = true const revision = associations.getSnapshot() const isCurrent = () => !cancelled && revision === associations.getSnapshot() - // Only a rerun of the same request touches its page; a new request's page waits for its reply. + // Only a rerun of the same request touches its page, keeping its reply so its cursor survives revalidation. setPage((previous) => - previous?.identity === identity - ? { ...previous, response: null, error: false, loading: true } - : previous + previous?.identity === identity ? { ...previous, error: false, loading: true } : previous ) try { let response = await window.api.aiVault.searchSessions({ ...request, cursor }, scope) @@ -106,7 +104,13 @@ export function useAiVaultSearch( })) } catch { if (isCurrent()) { - setPage({ identity, hits: [], response: null, error: true, loading: false }) + setPage((previous) => ({ + identity, + hits: previous?.identity === identity ? previous.hits : [], + response: previous?.identity === identity ? previous.response : null, + error: true, + loading: false + })) } } finally { pending = false diff --git a/src/renderer/src/components/settings/session-history/SessionHistorySettingsPane.tsx b/src/renderer/src/components/settings/session-history/SessionHistorySettingsPane.tsx index feca1509edb..eb0aa7859e9 100644 --- a/src/renderer/src/components/settings/session-history/SessionHistorySettingsPane.tsx +++ b/src/renderer/src/components/settings/session-history/SessionHistorySettingsPane.tsx @@ -190,7 +190,7 @@ export function SessionHistorySettingsPane({ ) : t( 'sessionHistory.settings.computersConsent', - 'Each computer keeps a searchable copy of its own agent conversations and tool output. Nothing leaves that computer.' + 'Each computer keeps a searchable copy of its own agent conversations. Nothing leaves that computer.' )}

diff --git a/src/renderer/src/i18n/locales/de.json b/src/renderer/src/i18n/locales/de.json index 558e628ce53..f3a4b0cf91c 100644 --- a/src/renderer/src/i18n/locales/de.json +++ b/src/renderer/src/i18n/locales/de.json @@ -21397,7 +21397,6 @@ "sortRelevance": "Relevanteste", "sortNewest": "Neueste", "sortResultsAriaLabel": "Ergebnisse sortieren: {{value0}}", - "consent": "Volltextsuche aktivieren? Orca erstellt auf diesem Computer einen Index aus lokalen Agentenprotokollen, einschließlich vollständiger Unterhaltungen und bis zu 3.072 Zeichen pro Tool-Ausgabe. Inhalte werden nicht geschwärzt. Authentifizierte, gekoppelte Clients können den Index durchsuchen.", "remoteDisabled": "Die Suche ist auf diesem Computer deaktiviert. Dort die Indexierung von Protokollen aktivieren, um seine Sitzungen zu durchsuchen.", "titleOnly": "Die Volltextsuche ist auf diesem Computer deaktiviert. Daher werden nur Sitzungstitel durchsucht.", "notReady": "Der Suchindex ist noch nicht bereit. In Kürze erneut versuchen.", @@ -21457,7 +21456,6 @@ "description": "Alles durchsuchen, was Agenten auf diesem Computer und auf gekoppelten Orca-Servern gesagt und getan haben.", "searchDescription": "Die Sitzungssuche für diesen Computer und gekoppelte Server aktivieren oder Suchdaten löschen.", "indexComputers": "Innerhalb von Sitzungen suchen", - "computersConsent": "Jeder Computer speichert eine durchsuchbare Kopie seiner eigenen Agentenunterhaltungen und Tool-Ausgaben. Nichts verlässt diesen Computer.", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "Sitzungen auf {{host}} durchsuchen", "serverOffline": "Offline", diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 935f9124bc8..294a0232133 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -22117,7 +22117,7 @@ "sortRelevance": "Most relevant", "sortNewest": "Newest", "sortResultsAriaLabel": "Sort results: {{value0}}", - "consent": "Enable full-text search? Orca builds an index on this computer from local agent transcripts, including full conversations and up to 3,072 characters per tool output. Content is not redacted. Authenticated paired clients can search it.", + "consent": "Enable full-text search? Orca builds an index on this computer from local agent transcripts, including full conversations. Content is not redacted. Authenticated paired clients can search it.", "remoteDisabled": "Search is disabled on this computer. Enable transcript indexing on that computer to search its sessions.", "titleOnly": "Full-text search is off on this computer, so only session titles are matched.", "notReady": "The search index is not ready yet. Try again shortly.", @@ -22177,7 +22177,7 @@ "description": "Search everything your agents have said and done, on this computer and on any paired Orca server.", "searchDescription": "Turn on session search for this computer and paired servers, or clear search data.", "indexComputers": "Search inside sessions", - "computersConsent": "Each computer keeps a searchable copy of its own agent conversations and tool output. Nothing leaves that computer.", + "computersConsent": "Each computer keeps a searchable copy of its own agent conversations. Nothing leaves that computer.", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "Search sessions on {{host}}", "serverOffline": "Offline", diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index de16ce36a27..cd9fe9525d0 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -21626,7 +21626,6 @@ "sortRelevance": "Más relevantes", "sortNewest": "Más recientes", "sortResultsAriaLabel": "Ordenar resultados: {{value0}}", - "consent": "¿Activar la búsqueda de texto completo? Orca crea un índice en esta computadora a partir de las transcripciones locales de los agentes, incluidas conversaciones completas y hasta 3.072 caracteres por salida de herramienta. El contenido no se censura. Los clientes emparejados autenticados pueden buscarlo.", "remoteDisabled": "La búsqueda está desactivada en esta computadora. Activa la indexación de transcripciones en esa computadora para buscar sus sesiones.", "titleOnly": "La búsqueda de texto completo está desactivada en esta computadora, así que solo se buscan los títulos de las sesiones.", "notReady": "El índice de búsqueda aún no está listo. Reintenta en un momento.", @@ -21691,7 +21690,6 @@ "description": "Busca todo lo que tus agentes han dicho y hecho, en esta computadora y en cualquier servidor de Orca emparejado.", "searchDescription": "Activa la búsqueda de sesiones para esta computadora y los servidores emparejados, o borra los datos de búsqueda.", "indexComputers": "Buscar dentro de las sesiones", - "computersConsent": "Cada computadora guarda una copia de búsqueda de sus propias conversaciones de agente y salidas de herramientas. Nada sale de esa computadora.", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "Buscar sesiones en {{host}}", "serverOffline": "Sin conexión", diff --git a/src/renderer/src/i18n/locales/fr.json b/src/renderer/src/i18n/locales/fr.json index 89ff347f348..820994f51ac 100644 --- a/src/renderer/src/i18n/locales/fr.json +++ b/src/renderer/src/i18n/locales/fr.json @@ -21715,7 +21715,6 @@ "sortRelevance": "Le plus pertinent", "sortNewest": "Le plus récent", "sortResultsAriaLabel": "Trier les résultats : {{value0}}", - "consent": "Activer la recherche en texte intégral ? Orca crée un index sur cet ordinateur à partir des transcriptions des agents locaux, comprenant des conversations complètes et jusqu'à 3 072 caractères par sortie d'outil. Le contenu n'est pas expurgé. Les clients couplés authentifiés peuvent le rechercher.", "remoteDisabled": "La recherche est désactivée sur cet ordinateur. Activez l’indexation des transcriptions sur cet ordinateur pour rechercher ses sessions.", "titleOnly": "La recherche en texte intégral est désactivée sur cet ordinateur ; seuls les titres des sessions sont pris en compte.", "notReady": "L'index de recherche n'est pas encore prêt. Réessayez sous peu.", @@ -21778,7 +21777,6 @@ "description": "Recherchez tout ce que vos agents ont dit et fait, sur cet ordinateur et sur n'importe quel serveur Orca couplé.", "searchDescription": "Activez la recherche de session pour cet ordinateur et les serveurs associés, ou effacez les données de recherche.", "indexComputers": "Rechercher dans les sessions", - "computersConsent": "Chaque ordinateur conserve une copie consultable de ses propres conversations d'agent et des résultats de ses outils. Rien ne quitte cet ordinateur.", "serverVersion": "Orque v{{version}}", "rowSwitchLabel": "Sessions de recherche le {{host}}", "serverOffline": "Hors ligne", diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index a71d0ef7834..3eb7af10b9c 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -21068,7 +21068,6 @@ "sortRelevance": "最も関連性の高い", "sortNewest": "最新", "sortResultsAriaLabel": "ソート結果: {{value0}}", - "consent": "全文検索を有効にしますか? Orca は、完全な会話とツール出力ごとに最大 3,072 文字を含む、ローカル Agent のトランスクリプトからこのコンピュータ上にインデックスを作成します。内容は編集されていません。認証されたペアリングされたクライアントは検索できます。", "remoteDisabled": "このコンピュータでは検索が無効になっています。そのコンピュータ上でトランスクリプトのインデックス作成を有効にして、セッションを検索します。", "titleOnly": "このコンピュータでは全文検索がオフのため、セッションのタイトルのみが照合されます。", "notReady": "検索インデックスはまだ準備ができていません。しばらくしてからもう一度お試しください。", @@ -21125,7 +21124,6 @@ "description": "このコンピュータおよびペアになっている Orca サーバー上で、Agent の言動をすべて検索します。", "searchDescription": "このコンピュータとペアリングされたサーバーのセッション検索をオンにするか、検索データをクリアします。", "indexComputers": "セッション内を検索", - "computersConsent": "各コンピュータは、自身の Agent の会話とツールの出力の検索可能なコピーを保持します。そのコンピュータからは何も残りません。", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "{{host}} のセッションを検索", "serverOffline": "オフライン", diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index 196f742f9ee..fc16b0f3419 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -21068,7 +21068,6 @@ "sortRelevance": "관련성이 가장 높은", "sortNewest": "최신", "sortResultsAriaLabel": "결과 정렬: {{value0}}", - "consent": "전체 텍스트 검색을 활성화하시겠습니까? Orca는 전체 대화와 도구 출력당 최대 3,072자를 포함하여 로컬 에이전트 기록에서 이 컴퓨터에 인덱스를 구축합니다. 내용은 편집되지 않습니다. 인증된 페어링 클라이언트는 검색이 가능합니다.", "remoteDisabled": "이 컴퓨터에서는 검색이 비활성화되어 있습니다. 세션을 검색하려면 해당 컴퓨터에서 기록 색인화를 활성화하십시오.", "titleOnly": "이 컴퓨터에서는 전체 텍스트 검색이 꺼져 있어 세션 제목만 일치시킵니다.", "notReady": "검색 색인이 아직 준비되지 않았습니다. 잠시 후 다시 시도하세요.", @@ -21125,7 +21124,6 @@ "description": "이 컴퓨터와 페어링된 Orca 서버에서 에이전트가 말하고 행한 모든 내용을 검색하세요.", "searchDescription": "이 컴퓨터와 페어링된 서버에 대한 세션 검색을 켜거나 검색 데이터를 지웁니다.", "indexComputers": "세션 내부 검색", - "computersConsent": "각 컴퓨터는 자체 에이전트 대화와 도구 출력의 검색 가능한 사본을 보관합니다. 이 데이터는 해당 컴퓨터 밖으로 전송되지 않습니다.", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "{{host}}의 세션 검색", "serverOffline": "오프라인", diff --git a/src/renderer/src/i18n/locales/pt.json b/src/renderer/src/i18n/locales/pt.json index 8d82c4b94e0..b9919f279fb 100644 --- a/src/renderer/src/i18n/locales/pt.json +++ b/src/renderer/src/i18n/locales/pt.json @@ -794,7 +794,6 @@ "loadMore": "Carregar mais resultados", "indexedHistory": "Histórico indexado · melhores resultados", "hostsSkipped": "Não pesquisado: {{value0}}", - "consent": "Ativar a busca em texto completo? O Orca cria um índice neste computador a partir das transcrições locais dos agentes, incluindo conversas completas e até 3.072 caracteres por saída de ferramenta. O conteúdo não é ocultado. Clientes pareados autenticados podem pesquisá-lo.", "remoteDisabled": "A busca está desativada neste computador. Ative a indexação de transcrições nesse computador para pesquisar suas sessões.", "notReady": "O índice de busca ainda não está pronto. Tente novamente em breve.", "noService": "A busca não está disponível neste computador. Pode ser necessário atualizar o Orca ou usar um runtime com suporte a busca.", @@ -894,7 +893,6 @@ "description": "Pesquise tudo o que seus agentes disseram e fizeram, neste computador e em qualquer servidor Orca pareado.", "searchDescription": "Ative a busca de sessões para este computador e servidores pareados, ou limpe os dados de busca.", "indexComputers": "Pesquisar dentro das sessões", - "computersConsent": "Cada computador mantém uma cópia pesquisável das próprias conversas de agente e saídas de ferramentas. Nada sai desse computador.", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "Pesquisar sessões em {{host}}", "serverOffline": "Off-line", diff --git a/src/renderer/src/i18n/locales/ru.json b/src/renderer/src/i18n/locales/ru.json index 66c2bfac9ad..c9b7d4d159a 100644 --- a/src/renderer/src/i18n/locales/ru.json +++ b/src/renderer/src/i18n/locales/ru.json @@ -22095,7 +22095,6 @@ "sortRelevance": "По соответствию", "sortNewest": "Сначала новые", "sortResultsAriaLabel": "Сортировка результатов: {{value0}}", - "consent": "Включить полнотекстовый поиск? Orca создаёт на этом компьютере индекс локальных историй агентов, включая полные разговоры и до 3072 символов из каждого вывода инструмента. Конфиденциальные данные в содержимом не скрываются. Сопряжённые клиенты, прошедшие аутентификацию, смогут выполнять поиск по этому индексу.", "remoteDisabled": "Поиск на этом компьютере выключен. Включите на нём индексацию историй, чтобы искать в его сессиях.", "titleOnly": "Полнотекстовый поиск на этом компьютере выключен. Поиск идёт только по названиям сессий.", "notReady": "Поисковый индекс ещё не готов. Повторите попытку немного позже.", @@ -22161,7 +22160,6 @@ "description": "Ищите всё, что говорили и делали агенты, на этом компьютере и любом сопряжённом сервере Orca.", "searchDescription": "Включите поиск сессий для этого компьютера и сопряжённых серверов или очистите данные поиска.", "indexComputers": "Поиск внутри сессий", - "computersConsent": "Каждый компьютер хранит поисковую копию собственных разговоров агентов и вывода инструментов. Ничего не передаётся на другие компьютеры.", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "Поиск сессий на {{host}}", "serverOffline": "Не в сети", diff --git a/src/renderer/src/i18n/locales/tr.json b/src/renderer/src/i18n/locales/tr.json index 0be9067070f..1a4410c50d5 100644 --- a/src/renderer/src/i18n/locales/tr.json +++ b/src/renderer/src/i18n/locales/tr.json @@ -21378,7 +21378,6 @@ "sortRelevance": "En alakalı", "sortNewest": "En yeni", "sortResultsAriaLabel": "Sonuçları sırala: {{value0}}", - "consent": "Tam metin arama etkinleştirilsin mi? Orca, tam konuşmalar ve araç çıktısı başına 3.072 karaktere kadar dahil olmak üzere yerel ajan dökümlerinden bu bilgisayarda bir dizin oluşturur. İçerik sansürlenmez. Kimliği doğrulanmış eşlenmiş istemciler bu dizinde arama yapabilir.", "remoteDisabled": "Arama bu bilgisayarda devre dışı. Oturumlarında arama yapmak için o bilgisayarda döküm dizinlemeyi etkinleştirin.", "titleOnly": "Bu bilgisayarda tam metin araması kapalı olduğu için yalnızca oturum başlıklarında eşleşme aranır.", "notReady": "Arama dizini henüz hazır değil. Birazdan tekrar deneyin.", @@ -21438,7 +21437,6 @@ "description": "Bu bilgisayarda ve eşlenmiş herhangi bir Orca sunucusunda ajanlarınızın söylediği ve yaptığı her şeyi arayın.", "searchDescription": "Bu bilgisayar ve eşlenmiş sunucular için oturum aramasını açın veya arama verilerini temizleyin.", "indexComputers": "Oturumların içinde ara", - "computersConsent": "Her bilgisayar, kendi ajan konuşmalarının ve araç çıktısının aranabilir bir kopyasını tutar. Hiçbir şey o bilgisayardan dışarı çıkmaz.", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "{{host}} üzerindeki oturumları ara", "serverOffline": "Çevrimdışı", diff --git a/src/renderer/src/i18n/locales/zh-Hant.json b/src/renderer/src/i18n/locales/zh-Hant.json index 27c0ac4d71b..5d545f01c37 100644 --- a/src/renderer/src/i18n/locales/zh-Hant.json +++ b/src/renderer/src/i18n/locales/zh-Hant.json @@ -21078,7 +21078,6 @@ "sortRelevance": "最相關", "sortNewest": "最新", "sortResultsAriaLabel": "排序結果:{{value0}}", - "consent": "要啟用全文搜尋嗎?Orca 會在此電腦上,根據本機代理程式對話紀錄建立索引,包括完整對話及每筆工具輸出最多 3,072 個字元。內容不會經過遮蔽。已驗證的配對用戶端可以搜尋此索引。", "remoteDisabled": "此電腦已停用搜尋。請在該電腦啟用對話紀錄索引,以搜尋其工作階段。", "titleOnly": "此電腦上的全文搜尋已關閉,因此僅比對工作階段標題。", "notReady": "搜尋索引尚未就緒。請稍後重試。", @@ -21135,7 +21134,6 @@ "description": "搜尋此電腦及任何已配對 Orca 伺服器上代理程式說過或做過的事。", "searchDescription": "啟用此電腦與已配對伺服器的工作階段搜尋,或清除搜尋資料。", "indexComputers": "搜尋工作階段內容", - "computersConsent": "每台電腦會在本機儲存自己的代理程式對話與工具輸出的可搜尋副本。資料不會離開該電腦。", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "搜尋 {{host}} 上的工作階段", "serverOffline": "離線", diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index aa5a22b0d91..be1ebb988a9 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -21068,7 +21068,6 @@ "sortRelevance": "最相关的", "sortNewest": "最新", "sortResultsAriaLabel": "排序结果:{{value0}}", - "consent": "启用全文搜索吗? Orca 根据本地智能体记录在此计算机上构建索引,包括完整对话和每个工具输出最多 3,072 个字符。内容未经过编辑。经过身份验证的配对客户端可以搜索它。", "remoteDisabled": "此计算机上已禁用搜索。在该计算机上启用转录索引以搜索其会话。", "titleOnly": "此计算机上的全文搜索已关闭,因此仅匹配会话标题。", "notReady": "搜索索引尚未准备好。稍后再试。", @@ -21125,7 +21124,6 @@ "description": "在此计算机和任何配对的 Orca 服务器上搜索您的智能体所说和所做的一切。", "searchDescription": "打开此计算机和配对服务器的会话搜索,或清除搜索数据。", "indexComputers": "在会话内搜索", - "computersConsent": "每台计算机都保留自己的智能体对话和工具输出的可搜索副本。没有任何东西离开那台计算机。", "serverVersion": "Orca v{{version}}", "rowSwitchLabel": "搜索 {{host}} 上的会话", "serverOffline": "离线", diff --git a/src/shared/ai-vault/search/ai-vault-search-types.ts b/src/shared/ai-vault/search/ai-vault-search-types.ts index 41ecd4f67c5..415a45b1672 100644 --- a/src/shared/ai-vault/search/ai-vault-search-types.ts +++ b/src/shared/ai-vault/search/ai-vault-search-types.ts @@ -9,8 +9,8 @@ import type { } from './ai-vault-search-contract' /** - * Tool output beyond 3,072 characters per row is not indexed and not searchable; user and assistant text is indexed in full. - * A page cursor outstanding during a retention purge is refused once as `stale-cursor`; the client re-issues page 1. + * Tool output is not indexed or searchable; user and assistant text is indexed in full. + * Content changes invalidate outstanding cursors; background tool cleanup preserves frozen continuation. * A phrase match across a chunk boundary of a long message is not supported. */ export type AiVaultSearchRequest = z.input From bec57632a4bd3a8507dede381f3dd48d8c35d53a Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:25:35 -0800 Subject: [PATCH 14/80] refactor(settings): place shared host features and terminal catalogs with owners (#40051) * refactor(settings): place shared host features and terminal catalogs with owners * refactor(settings): retain browser host adapter policy --- config/localization-coverage-allowlist.json | 24 ++++++++--- config/react-compiler-bailout-baseline.txt | 4 +- .../scripts/ci/sharding/ci-shard-timings.json | 8 ++-- .../feature-tips/SessionSearchTipDialog.tsx | 2 +- .../use-session-search-tip-setup.ts | 4 +- .../feature-wall/BrowserUseSkillSetupCard.tsx | 2 +- ....test.tsx => HostListSearchField.test.tsx} | 0 .../host-picker/SshTargetSelect.tsx | 28 ------------- .../onboarding-feature-setup.test.ts | 2 +- .../onboarding/onboarding-feature-setup.ts | 4 +- .../session-search-count-format.ts | 0 .../session-search-status.ts} | 2 +- .../use-session-search-status.test.tsx | 40 +++---------------- .../use-session-search-status.ts | 6 +-- .../network/NodeDistMirrorDialog.test.tsx | 2 +- .../browser/passwords/BrowserPasswordList.tsx | 2 +- .../passwords/BrowserPasswordNeverList.tsx | 2 +- .../passwords/BrowserPasswordsSection.tsx | 7 +--- .../browser-password-import-toast.ts | 8 ---- .../passwords/browser-passwords-copy.ts | 8 ++++ .../settings/browser/use/BrowserUsePane.tsx | 2 +- .../ServerListRow.tsx} | 8 ++-- ...rts.tsx => managed-server-row-content.tsx} | 0 .../runtime-install-server-dialog.tsx | 6 +-- .../use-managed-server-catalog.ts | 7 +--- .../pairing/RuntimePairingUrlGenerator.tsx | 9 ++++- .../pairing/runtime-pairing-link-state.ts | 6 --- .../runtime-servers-header-actions.tsx | 2 +- .../server-rows/runtime-server-list.tsx | 2 +- .../runtime-server-row-presentation.ts | 2 +- .../server-rows/runtime-server-row.tsx | 4 +- .../ssh-access/RuntimeSshAccessControl.tsx | 6 +-- .../SessionHistoryServerRow.tsx | 4 +- .../SessionHistorySettingsPane.tsx | 4 +- .../components/settings/ssh/SshPane.test.tsx | 2 +- .../src/components/settings/ssh/SshPane.tsx | 2 +- .../SshHostKeepServerVersionDialog.tsx | 2 +- .../SshHostRecoverServerAction.tsx | 2 +- .../SshHostRestartServerAction.tsx | 2 +- .../server-actions/SshTargetServerStatus.tsx | 9 +++-- .../ssh/targets/SshTargetForm.test.tsx | 2 +- .../settings/ssh/targets/SshTargetForm.tsx | 4 +- .../ssh/targets/ssh-target-save-payload.ts | 5 ++- .../settings/terminal/TerminalPane.tsx | 2 +- .../appearance/TerminalAppearanceSection.tsx | 6 +-- .../TerminalWindowSection.tsx | 16 ++++---- .../terminal-window-color-groups.tsx | 2 +- .../terminal-window-search.ts} | 31 +------------- .../terminal-config-import-search.ts | 37 +++++++++++++++++ .../terminal-advanced-platform-search.ts | 34 ---------------- .../ManageSessionsSection.tsx | 2 +- .../manage-sessions-search.ts | 32 +++++++++++++++ .../settings/terminal/terminal-search.ts | 8 ++-- .../updates/GeneralRemoteServerUpdates.tsx | 2 +- .../updates/GeneralUpdateSettingsSection.tsx | 2 +- .../settings/wsl/hosts/WslHostsSection.tsx | 6 +-- .../ssh/AddRemoteHostDialog.tsx | 2 +- .../ssh/AddRemoteHostFields.tsx | 4 +- .../ssh/AddRemoteHostSshFormPanel.tsx | 2 +- .../ssh/AddServerSshHostDialog.tsx | 2 +- .../ssh/add-remote-host-ssh-actions.ts | 2 +- ...AddRepoHostSelector.large-config.test.tsx} | 4 +- .../SshHostAdvancedFields.fixture.tsx | 2 +- .../SshHostAdvancedFields.tsx | 2 +- .../ssh-target-draft.test.ts | 0 .../ssh-target-draft.ts | 4 +- .../SshHostMoveAnywayAction.tsx | 8 ++-- .../SshHostNodeMirrorAction.tsx | 4 +- .../ssh/TerminalSshReconnectOverlay.tsx | 4 +- .../browser}/browser-use-setup-state.ts | 0 .../managed-orcad-api-availability.ts | 7 ++++ 71 files changed, 217 insertions(+), 257 deletions(-) rename src/renderer/src/components/host-picker/{bounded-host-list.test.tsx => HostListSearchField.test.tsx} (100%) delete mode 100644 src/renderer/src/components/host-picker/SshTargetSelect.tsx rename src/renderer/src/components/{settings/session-history => session-search}/session-search-count-format.ts (100%) rename src/renderer/src/components/{settings/session-history/session-history-status-copy.ts => session-search/session-search-status.ts} (97%) rename src/renderer/src/components/{settings/session-history => session-search}/use-session-search-status.test.tsx (56%) rename src/renderer/src/components/{settings/session-history => session-search}/use-session-search-status.ts (91%) rename src/renderer/src/components/settings/{runtime-environments/server-rows/runtime-server-list-row.tsx => host-rows/ServerListRow.tsx} (96%) rename src/renderer/src/components/settings/runtime-environments/managed-servers/{managed-server-row-parts.tsx => managed-server-row-content.tsx} (100%) rename src/renderer/src/components/settings/terminal/{ => appearance}/TerminalWindowSection.tsx (94%) rename src/renderer/src/components/settings/terminal/{ => appearance}/terminal-window-color-groups.tsx (98%) rename src/renderer/src/components/settings/terminal/{terminal-window-setup-search.ts => appearance/terminal-window-search.ts} (71%) create mode 100644 src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts create mode 100644 src/renderer/src/components/settings/terminal/session-management/manage-sessions-search.ts rename src/renderer/src/components/settings/{runtime-environments => }/updates/GeneralRemoteServerUpdates.tsx (98%) rename src/renderer/src/components/{host-picker/host-list-surfaces.large-config.test.tsx => sidebar/project-admission/host-selection/AddRepoHostSelector.large-config.test.tsx} (88%) rename src/renderer/src/components/{settings/ssh/targets => ssh-host-form}/SshHostAdvancedFields.fixture.tsx (87%) rename src/renderer/src/components/{settings/ssh/targets => ssh-host-form}/SshHostAdvancedFields.tsx (99%) rename src/renderer/src/components/{settings/ssh/targets => ssh-host-form}/ssh-target-draft.test.ts (100%) rename src/renderer/src/components/{settings/ssh/targets => ssh-host-form}/ssh-target-draft.ts (98%) rename src/renderer/src/components/{settings/ssh/server-actions => ssh-host-recovery}/SshHostMoveAnywayAction.tsx (95%) rename src/renderer/src/components/{settings/ssh/server-actions => ssh-host-recovery}/SshHostNodeMirrorAction.tsx (88%) rename src/renderer/src/{components/settings/browser/use => lib/browser}/browser-use-setup-state.ts (100%) create mode 100644 src/renderer/src/runtime/hosts/installation/managed-orcad-api-availability.ts diff --git a/config/localization-coverage-allowlist.json b/config/localization-coverage-allowlist.json index 18549199187..4222e7a5add 100644 --- a/config/localization-coverage-allowlist.json +++ b/config/localization-coverage-allowlist.json @@ -60,31 +60,45 @@ "kind": "object-property:keywords", "text": "ghostty", "dynamic": false, - "count": 2 + "count": 1 + }, + { + "filePath": "src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts", + "kind": "object-property:keywords", + "text": "ghostty", + "dynamic": false, + "count": 1 }, { "filePath": "src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts", "kind": "object-property:keywords", "text": "Ghostty", "dynamic": false, - "count": 2 + "count": 1 }, { - "filePath": "src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts", + "filePath": "src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts", + "kind": "object-property:keywords", + "text": "Ghostty", + "dynamic": false, + "count": 1 + }, + { + "filePath": "src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts", "kind": "object-property:keywords", "text": "Kitty", "dynamic": false, "count": 1 }, { - "filePath": "src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts", + "filePath": "src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts", "kind": "object-property:keywords", "text": "kitty", "dynamic": false, "count": 1 }, { - "filePath": "src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts", + "filePath": "src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts", "kind": "object-property:keywords", "text": "kitty.conf", "dynamic": false, diff --git a/config/react-compiler-bailout-baseline.txt b/config/react-compiler-bailout-baseline.txt index b1a89fbefe0..7ba46d39928 100644 --- a/config/react-compiler-bailout-baseline.txt +++ b/config/react-compiler-bailout-baseline.txt @@ -441,7 +441,7 @@ 1 src/renderer/src/components/settings/session-history/SessionHistoryServerRow.tsx 2 src/renderer/src/components/settings/session-history/SessionHistorySettingsPane.tsx 1 src/renderer/src/components/settings/session-history/SessionSearchAdvancedSection.tsx -1 src/renderer/src/components/settings/session-history/use-session-search-status.ts +1 src/renderer/src/components/session-search/use-session-search-status.ts 1 src/renderer/src/components/settings/shortcuts/ShortcutRecorderButton.tsx 2 src/renderer/src/components/settings/source-control/AutoRenameBranchFromWorkSetting.tsx 1 src/renderer/src/components/settings/source-control/CommitMessageAiPane.tsx @@ -449,7 +449,7 @@ 1 src/renderer/src/components/settings/ssh/SshPane.tsx 1 src/renderer/src/components/settings/ssh/server-actions/SshHostDeltaMoveDialog.tsx 1 src/renderer/src/components/settings/ssh/server-actions/SshHostKeepServerVersionDialog.tsx -1 src/renderer/src/components/settings/ssh/server-actions/SshHostMoveAnywayAction.tsx +1 src/renderer/src/components/ssh-host-recovery/SshHostMoveAnywayAction.tsx 1 src/renderer/src/components/settings/ssh/server-actions/SshHostRecoverServerAction.tsx 1 src/renderer/src/components/settings/ssh/server-actions/SshHostRestartServerAction.tsx 1 src/renderer/src/components/settings/ssh/ssh-hosts-header-slot.tsx diff --git a/config/scripts/ci/sharding/ci-shard-timings.json b/config/scripts/ci/sharding/ci-shard-timings.json index 2a25d38facd..1c66127fe10 100644 --- a/config/scripts/ci/sharding/ci-shard-timings.json +++ b/config/scripts/ci/sharding/ci-shard-timings.json @@ -5190,8 +5190,8 @@ "src/renderer/src/components/github/github-work-item-mutation-target.test.ts": 348, "src/renderer/src/components/github/use-image-input.test.ts": 2619, "src/renderer/src/components/gitlab-item-dialog/use-gitlab-primary-actions.test.ts": 2141, - "src/renderer/src/components/host-picker/bounded-host-list.test.tsx": 667, - "src/renderer/src/components/host-picker/host-list-surfaces.large-config.test.tsx": 2963, + "src/renderer/src/components/host-picker/HostListSearchField.test.tsx": 667, + "src/renderer/src/components/sidebar/project-admission/host-selection/AddRepoHostSelector.large-config.test.tsx": 2963, "src/renderer/src/components/jira-attachment-image.test.tsx": 2117, "src/renderer/src/components/jira-connect-scoped-token.test.tsx": 1047, "src/renderer/src/components/jira-issue-assignee-control.test.tsx": 2875, @@ -5643,7 +5643,7 @@ "src/renderer/src/components/settings/navigation/settings-project-list.test.ts": 99, "src/renderer/src/components/settings/source-control/source-control-action-card-layout.test.tsx": 4551, "src/renderer/src/components/settings/source-control/source-control-action-permission-draft.test.tsx": 6938, - "src/renderer/src/components/settings/ssh/targets/ssh-target-draft.test.ts": 72, + "src/renderer/src/components/ssh-host-form/ssh-target-draft.test.ts": 72, "src/renderer/src/components/settings/ssh/targets/ssh-target-list-model.test.ts": 56, "src/renderer/src/components/integrations/task-trackers/linear-integration-card.test.tsx": 1414, "src/renderer/src/components/settings/appearance/themes/use-catalog-app-themes.test.ts": 2159, @@ -5652,7 +5652,7 @@ "src/renderer/src/components/settings/repository/hooks/use-repository-issue-command-wsl-stopped.test.ts": 523, "src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-catalog.test.tsx": 2252, "src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-mutation-actions.test.tsx": 2069, - "src/renderer/src/components/settings/session-history/use-session-search-status.test.tsx": 477, + "src/renderer/src/components/session-search/use-session-search-status.test.tsx": 477, "src/renderer/src/components/settings/accounts/usage/use-zcode-plan-credentials.test.tsx": 880, "src/renderer/src/lib/directory-path-suggestion-filter.scan-budget.test.ts": 68, "src/renderer/src/lib/directory-path-suggestion-filter.test.ts": 51, diff --git a/src/renderer/src/components/feature-tips/SessionSearchTipDialog.tsx b/src/renderer/src/components/feature-tips/SessionSearchTipDialog.tsx index fac94e4fb0e..21de52052fb 100644 --- a/src/renderer/src/components/feature-tips/SessionSearchTipDialog.tsx +++ b/src/renderer/src/components/feature-tips/SessionSearchTipDialog.tsx @@ -4,7 +4,7 @@ import type { FeatureTip } from '../../../../shared/feature-tips' import { sessionSearchStatusDetails, sessionSearchStatusMessage -} from '@/components/settings/session-history/session-history-status-copy' +} from '@/components/session-search/session-search-status' import { DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { translate } from '@/i18n/i18n' import { useIntlLocale, useTranslate, type Translate } from '@/i18n/use-translate' diff --git a/src/renderer/src/components/feature-tips/use-session-search-tip-setup.ts b/src/renderer/src/components/feature-tips/use-session-search-tip-setup.ts index e23a6e608cb..b489a3157a4 100644 --- a/src/renderer/src/components/feature-tips/use-session-search-tip-setup.ts +++ b/src/renderer/src/components/feature-tips/use-session-search-tip-setup.ts @@ -6,8 +6,8 @@ import { LOCAL_EXECUTION_HOST_ID } from '../../../../shared/execution-host' import { isSessionSearchIndexReady, sessionSearchStatusMessage -} from '@/components/settings/session-history/session-history-status-copy' -import { useSessionSearchStatus } from '@/components/settings/session-history/use-session-search-status' +} from '@/components/session-search/session-search-status' +import { useSessionSearchStatus } from '@/components/session-search/use-session-search-status' import { translate } from '@/i18n/i18n' import { useTranslate } from '@/i18n/use-translate' import { useAppStore } from '@/store' diff --git a/src/renderer/src/components/feature-wall/BrowserUseSkillSetupCard.tsx b/src/renderer/src/components/feature-wall/BrowserUseSkillSetupCard.tsx index 191567772f8..79117be2416 100644 --- a/src/renderer/src/components/feature-wall/BrowserUseSkillSetupCard.tsx +++ b/src/renderer/src/components/feature-wall/BrowserUseSkillSetupCard.tsx @@ -1,6 +1,6 @@ import type { JSX } from 'react' import { ORCA_CLI_SKILL_NAME } from '@/lib/agent-feature-skill-names' -import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/components/settings/browser/use/browser-use-setup-state' +import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/lib/browser/browser-use-setup-state' import type { InstalledAgentSkillState } from '@/hooks/useInstalledAgentSkills' import { useActiveProjectSkillRuntime } from '@/hooks/useActiveProjectSkillRuntime' import { AgentSkillSetupPanel } from '@/components/skills/setup/AgentSkillSetupPanel' diff --git a/src/renderer/src/components/host-picker/bounded-host-list.test.tsx b/src/renderer/src/components/host-picker/HostListSearchField.test.tsx similarity index 100% rename from src/renderer/src/components/host-picker/bounded-host-list.test.tsx rename to src/renderer/src/components/host-picker/HostListSearchField.test.tsx diff --git a/src/renderer/src/components/host-picker/SshTargetSelect.tsx b/src/renderer/src/components/host-picker/SshTargetSelect.tsx deleted file mode 100644 index 409d1ce7a44..00000000000 --- a/src/renderer/src/components/host-picker/SshTargetSelect.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import type { SshTarget } from '../../../../shared/ssh-types' -import { HostCombobox } from './HostCombobox' - -type SshTargetSelectProps = { - id?: string - targets: SshTarget[] - value: string - onChange: (targetId: string) => void - placeholder: string -} - -export function SshTargetSelect({ - id, - targets, - value, - onChange, - placeholder -}: SshTargetSelectProps): React.JSX.Element { - return ( - - ) -} diff --git a/src/renderer/src/components/onboarding/onboarding-feature-setup.test.ts b/src/renderer/src/components/onboarding/onboarding-feature-setup.test.ts index aeefb3780ee..8b3c9095065 100644 --- a/src/renderer/src/components/onboarding/onboarding-feature-setup.test.ts +++ b/src/renderer/src/components/onboarding/onboarding-feature-setup.test.ts @@ -4,7 +4,7 @@ import type { ComputerUsePermissionStatusResult } from '../../../../shared/computer-use-permissions-types' import { ORCHESTRATION_SKILL_NAME } from '@/lib/agent-feature-skill-names' -import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/components/settings/browser/use/browser-use-setup-state' +import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/lib/browser/browser-use-setup-state' import { ORCHESTRATION_ENABLED_STORAGE_KEY } from '@/lib/orchestration-setup-state' import { runOnboardingFeatureSetup, diff --git a/src/renderer/src/components/onboarding/onboarding-feature-setup.ts b/src/renderer/src/components/onboarding/onboarding-feature-setup.ts index 0e671ad7313..fb7b19c77b5 100644 --- a/src/renderer/src/components/onboarding/onboarding-feature-setup.ts +++ b/src/renderer/src/components/onboarding/onboarding-feature-setup.ts @@ -8,14 +8,14 @@ import { ORCA_CLI_SKILL_NAME, ORCHESTRATION_SKILL_NAME } from '@/lib/agent-feature-skill-names' -import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/components/settings/browser/use/browser-use-setup-state' +import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/lib/browser/browser-use-setup-state' import { e2eConfig } from '@/lib/e2e-config' import { ORCHESTRATION_ENABLED_STORAGE_KEY, ORCHESTRATION_SETUP_DISMISSED_STORAGE_KEY, notifyOrchestrationSetupStateChanged } from '@/lib/orchestration-setup-state' -import type { EventProps } from '../../../../shared/telemetry-events' +import type { EventProps } from '../../../../shared/telemetry-event-classification' import type { BundledSkillInstallState } from '@/hooks/use-bundled-skill-install' export type OnboardingFeatureSetupId = diff --git a/src/renderer/src/components/settings/session-history/session-search-count-format.ts b/src/renderer/src/components/session-search/session-search-count-format.ts similarity index 100% rename from src/renderer/src/components/settings/session-history/session-search-count-format.ts rename to src/renderer/src/components/session-search/session-search-count-format.ts diff --git a/src/renderer/src/components/settings/session-history/session-history-status-copy.ts b/src/renderer/src/components/session-search/session-search-status.ts similarity index 97% rename from src/renderer/src/components/settings/session-history/session-history-status-copy.ts rename to src/renderer/src/components/session-search/session-search-status.ts index 55867312942..a9d62a1bd07 100644 --- a/src/renderer/src/components/settings/session-history/session-history-status-copy.ts +++ b/src/renderer/src/components/session-search/session-search-status.ts @@ -1,4 +1,4 @@ -import type { AiVaultSearchStatus } from '../../../../../shared/ai-vault/search/ai-vault-search-types' +import type { AiVaultSearchStatus } from '../../../../shared/ai-vault/search/ai-vault-search-types' import { translate } from '@/i18n/i18n' import type { Translate } from '@/i18n/use-translate' import { formatMessageCount, formatSessionCount } from './session-search-count-format' diff --git a/src/renderer/src/components/settings/session-history/use-session-search-status.test.tsx b/src/renderer/src/components/session-search/use-session-search-status.test.tsx similarity index 56% rename from src/renderer/src/components/settings/session-history/use-session-search-status.test.tsx rename to src/renderer/src/components/session-search/use-session-search-status.test.tsx index 7373e48231b..d68cac03fbf 100644 --- a/src/renderer/src/components/settings/session-history/use-session-search-status.test.tsx +++ b/src/renderer/src/components/session-search/use-session-search-status.test.tsx @@ -2,9 +2,9 @@ import '@testing-library/jest-dom/vitest' import { act, cleanup, renderHook } from '@testing-library/react' import { afterEach, beforeEach, expect, it, vi } from 'vitest' -import { unavailableSessionSearchStatus } from '../../../../../shared/ai-vault/search/ai-vault-search-client' -import type { AiVaultSearchStatus } from '../../../../../shared/ai-vault/search/ai-vault-search-types' -import { LOCAL_EXECUTION_HOST_ID } from '../../../../../shared/execution-host' +import { unavailableSessionSearchStatus } from '../../../../shared/ai-vault/search/ai-vault-search-client' +import type { AiVaultSearchStatus } from '../../../../shared/ai-vault/search/ai-vault-search-types' +import { LOCAL_EXECUTION_HOST_ID } from '../../../../shared/execution-host' import { useSessionSearchStatus } from './use-session-search-status' @@ -39,7 +39,6 @@ function poll(active = true, refresh = 0) { } beforeEach(() => { - vi.useFakeTimers() mocks.visible = true mocks.status.mockReset().mockResolvedValue(current) vi.stubGlobal('api', undefined) @@ -50,44 +49,15 @@ beforeEach(() => { }) afterEach(() => { cleanup() - vi.useRealTimers() vi.unstubAllGlobals() }) -it('does not overlap slow status requests and stops polling on unmount', async () => { - let answer: (value: AiVaultSearchStatus) => void = () => undefined - mocks.status.mockReturnValue( - new Promise((resolve) => { - answer = resolve - }) - ) - const view = poll() - await act(async () => { - await vi.advanceTimersByTimeAsync(30_000) - }) - expect(mocks.status).toHaveBeenCalledTimes(1) - await act(async () => { - answer({ ...current, phase: 'indexing', filesDue: 2 }) - }) - const beforeUnmount = mocks.status.mock.calls.length - view.unmount() - await act(async () => { - await vi.advanceTimersByTimeAsync(60_000) - }) - expect(mocks.status).toHaveBeenCalledTimes(beforeUnmount) -}) - -it('marks the host too old on a host-too-old rejection and stops polling', async () => { +it('marks the host too old on a host-too-old rejection', async () => { mocks.status.mockRejectedValue(new Error('Error invoking remote method: host-too-old')) const { result } = poll() await act(async () => { - await vi.advanceTimersByTimeAsync(0) + await Promise.resolve() }) expect(result.current.hostTooOld).toBe(true) expect(result.current.failed).toBe(true) - const calls = mocks.status.mock.calls.length - await act(async () => { - await vi.advanceTimersByTimeAsync(30_000) - }) - expect(mocks.status.mock.calls.length).toBe(calls) }) diff --git a/src/renderer/src/components/settings/session-history/use-session-search-status.ts b/src/renderer/src/components/session-search/use-session-search-status.ts similarity index 91% rename from src/renderer/src/components/settings/session-history/use-session-search-status.ts rename to src/renderer/src/components/session-search/use-session-search-status.ts index 1423b9ba1a5..65859a980f8 100644 --- a/src/renderer/src/components/settings/session-history/use-session-search-status.ts +++ b/src/renderer/src/components/session-search/use-session-search-status.ts @@ -1,9 +1,9 @@ import { useCallback, useEffect, useState } from 'react' -import type { AiVaultSearchStatus } from '../../../../../shared/ai-vault/search/ai-vault-search-types' -import type { ExecutionHostId } from '../../../../../shared/execution-host' +import type { AiVaultSearchStatus } from '../../../../shared/ai-vault/search/ai-vault-search-types' +import type { ExecutionHostId } from '../../../../shared/execution-host' import { useWindowStreamVisible } from '@/hooks/use-window-stream-visibility' import { installWindowVisibilityInterval } from '@/lib/window-visibility-interval' -import { isHostTooOldError, sessionSearchPollIntervalMs } from './session-history-status-copy' +import { isHostTooOldError, sessionSearchPollIntervalMs } from './session-search-status' export type SessionSearchStatusRead = { status: AiVaultSearchStatus | null diff --git a/src/renderer/src/components/settings/advanced/network/NodeDistMirrorDialog.test.tsx b/src/renderer/src/components/settings/advanced/network/NodeDistMirrorDialog.test.tsx index b83fc88f330..911d77ad263 100644 --- a/src/renderer/src/components/settings/advanced/network/NodeDistMirrorDialog.test.tsx +++ b/src/renderer/src/components/settings/advanced/network/NodeDistMirrorDialog.test.tsx @@ -11,7 +11,7 @@ const toastError = vi.hoisted(() => vi.fn()) vi.mock('sonner', () => ({ toast: { error: toastError } })) import NodeDistMirrorDialog from './NodeDistMirrorDialog' -import { SshHostNodeMirrorAction } from '../../ssh/server-actions/SshHostNodeMirrorAction' +import { SshHostNodeMirrorAction } from '../../../ssh-host-recovery/SshHostNodeMirrorAction' import { toastSshConnectFailure } from '@/ssh/ssh-connect-failure-toast' import { getDefaultSettings } from '../../../../../../shared/constants' diff --git a/src/renderer/src/components/settings/browser/passwords/BrowserPasswordList.tsx b/src/renderer/src/components/settings/browser/passwords/BrowserPasswordList.tsx index 5d2f04ea740..52fa9d02a0e 100644 --- a/src/renderer/src/components/settings/browser/passwords/BrowserPasswordList.tsx +++ b/src/renderer/src/components/settings/browser/passwords/BrowserPasswordList.tsx @@ -10,7 +10,7 @@ import { translate } from '@/i18n/i18n' import { useIntlLocale, useTranslate, type Translate } from '@/i18n/use-translate' import { formatUiRelativeTime } from '@/i18n/relative-time-format' import type { BrowserPasswordEntrySummary } from '../../../../../../shared/browser-passwords-types' -import { browserPasswordHostOf } from './browser-password-import-toast' +import { browserPasswordHostOf } from './browser-passwords-copy' export type BrowserPasswordRowActions = { onCopy: (entry: BrowserPasswordEntrySummary) => void diff --git a/src/renderer/src/components/settings/browser/passwords/BrowserPasswordNeverList.tsx b/src/renderer/src/components/settings/browser/passwords/BrowserPasswordNeverList.tsx index 85b16e87585..49e29781af4 100644 --- a/src/renderer/src/components/settings/browser/passwords/BrowserPasswordNeverList.tsx +++ b/src/renderer/src/components/settings/browser/passwords/BrowserPasswordNeverList.tsx @@ -2,7 +2,7 @@ import { ChevronRight } from 'lucide-react' import { Button } from '@/components/ui/button' import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible' import { useTranslate } from '@/i18n/use-translate' -import { browserPasswordHostOf } from './browser-password-import-toast' +import { browserPasswordHostOf } from './browser-passwords-copy' /** Sites the user told the save offer to skip; removing one lets Orca offer again there. */ export function BrowserPasswordNeverList({ diff --git a/src/renderer/src/components/settings/browser/passwords/BrowserPasswordsSection.tsx b/src/renderer/src/components/settings/browser/passwords/BrowserPasswordsSection.tsx index c2d5782d8b7..999fcd4edab 100644 --- a/src/renderer/src/components/settings/browser/passwords/BrowserPasswordsSection.tsx +++ b/src/renderer/src/components/settings/browser/passwords/BrowserPasswordsSection.tsx @@ -25,11 +25,8 @@ import { } from './BrowserPasswordImportMenu' import { BrowserPasswordList } from './BrowserPasswordList' import { BrowserPasswordNeverList } from './BrowserPasswordNeverList' -import { - browserPasswordHostOf, - toastBrowserPasswordImportResult -} from './browser-password-import-toast' -import { getBrowserPasswordsSearchEntry } from './browser-passwords-copy' +import { toastBrowserPasswordImportResult } from './browser-password-import-toast' +import { browserPasswordHostOf, getBrowserPasswordsSearchEntry } from './browser-passwords-copy' type VaultView = { status: BrowserPasswordsStatus | null diff --git a/src/renderer/src/components/settings/browser/passwords/browser-password-import-toast.ts b/src/renderer/src/components/settings/browser/passwords/browser-password-import-toast.ts index dd72fbba211..4831ee6e675 100644 --- a/src/renderer/src/components/settings/browser/passwords/browser-password-import-toast.ts +++ b/src/renderer/src/components/settings/browser/passwords/browser-password-import-toast.ts @@ -46,11 +46,3 @@ export function toastBrowserPasswordImportResult(result: BrowserPasswordImportRe skipped.length > 0 ? { description: skipped.join(' · ') } : undefined ) } - -export function browserPasswordHostOf(origin: string): string { - try { - return new URL(origin).host - } catch { - return origin - } -} diff --git a/src/renderer/src/components/settings/browser/passwords/browser-passwords-copy.ts b/src/renderer/src/components/settings/browser/passwords/browser-passwords-copy.ts index cb868e0738e..41ff392999b 100644 --- a/src/renderer/src/components/settings/browser/passwords/browser-passwords-copy.ts +++ b/src/renderer/src/components/settings/browser/passwords/browser-passwords-copy.ts @@ -70,3 +70,11 @@ export function getBrowserPasswordsSearchEntry(t?: Translate): SettingsSearchEnt ] } } + +export function browserPasswordHostOf(origin: string): string { + try { + return new URL(origin).host + } catch { + return origin + } +} diff --git a/src/renderer/src/components/settings/browser/use/BrowserUsePane.tsx b/src/renderer/src/components/settings/browser/use/BrowserUsePane.tsx index bc7fb27e33b..cdfb200b9a4 100644 --- a/src/renderer/src/components/settings/browser/use/BrowserUsePane.tsx +++ b/src/renderer/src/components/settings/browser/use/BrowserUsePane.tsx @@ -1,6 +1,6 @@ import { useEffect, useState } from 'react' import { ORCA_CLI_SKILL_NAME } from '@/lib/agent-feature-skill-names' -import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/components/settings/browser/use/browser-use-setup-state' +import { BROWSER_USE_ENABLED_STORAGE_KEY } from '@/lib/browser/browser-use-setup-state' import { GLOBAL_AGENT_SKILL_SOURCE_KINDS, useInstalledAgentSkill diff --git a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-list-row.tsx b/src/renderer/src/components/settings/host-rows/ServerListRow.tsx similarity index 96% rename from src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-list-row.tsx rename to src/renderer/src/components/settings/host-rows/ServerListRow.tsx index 928f86b4c92..80dc0993e23 100644 --- a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-list-row.tsx +++ b/src/renderer/src/components/settings/host-rows/ServerListRow.tsx @@ -2,10 +2,10 @@ import type React from 'react' import { MoreHorizontal, Server } from 'lucide-react' import { useTranslate } from '@/i18n/use-translate' import { cn } from '@/lib/utils' -import { LoadingSpinner } from '../../../LoadingSpinner' -import { Button } from '../../../ui/button' -import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '../../../ui/dropdown-menu' -import { Tooltip, TooltipContent, TooltipTrigger } from '../../../ui/tooltip' +import { LoadingSpinner } from '../../LoadingSpinner' +import { Button } from '../../ui/button' +import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '../../ui/dropdown-menu' +import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip' export type ServerRowStatusTone = 'success' | 'warning' | 'destructive' | 'muted' diff --git a/src/renderer/src/components/settings/runtime-environments/managed-servers/managed-server-row-parts.tsx b/src/renderer/src/components/settings/runtime-environments/managed-servers/managed-server-row-content.tsx similarity index 100% rename from src/renderer/src/components/settings/runtime-environments/managed-servers/managed-server-row-parts.tsx rename to src/renderer/src/components/settings/runtime-environments/managed-servers/managed-server-row-content.tsx diff --git a/src/renderer/src/components/settings/runtime-environments/managed-servers/runtime-install-server-dialog.tsx b/src/renderer/src/components/settings/runtime-environments/managed-servers/runtime-install-server-dialog.tsx index b80121cfe49..755381861bb 100644 --- a/src/renderer/src/components/settings/runtime-environments/managed-servers/runtime-install-server-dialog.tsx +++ b/src/renderer/src/components/settings/runtime-environments/managed-servers/runtime-install-server-dialog.tsx @@ -18,7 +18,7 @@ import { import { Input } from '../../../ui/input' import { Label } from '../../../ui/label' import { managedServerOutcomeLabel } from './managed-server-copy' -import { SshTargetSelect } from '../../../host-picker/SshTargetSelect' +import { HostCombobox } from '../../../host-picker/HostCombobox' type RuntimeInstallServerDialogProps = { open: boolean @@ -145,9 +145,9 @@ export function RuntimeInstallServerDialog({ - Promise } -/** Web fallback stubs cannot manage SSH hosts. */ -export function getManagedOrcadApi(): ManagedOrcadPreloadApi | undefined { - return isWebClientLocation() ? undefined : window.api.runtimeEnvironments.managedOrcad -} - export function useManagedServerCatalog( environments: PublicKnownRuntimeEnvironment[], onChanged: () => void diff --git a/src/renderer/src/components/settings/runtime-environments/pairing/RuntimePairingUrlGenerator.tsx b/src/renderer/src/components/settings/runtime-environments/pairing/RuntimePairingUrlGenerator.tsx index 59cd75d96b9..df7886a42dd 100644 --- a/src/renderer/src/components/settings/runtime-environments/pairing/RuntimePairingUrlGenerator.tsx +++ b/src/renderer/src/components/settings/runtime-environments/pairing/RuntimePairingUrlGenerator.tsx @@ -14,12 +14,17 @@ import { runtimePairingLinkCache, runtimePairingReachForIntent, selectRuntimePairingIntent, - type RuntimePairingIntent, - type RuntimePairingUrlGeneratorProps + type RuntimePairingIntent } from './runtime-pairing-link-state' import { useRuntimePairingLinkExpiry } from './use-runtime-pairing-link-expiry' import { useRuntimePairingUrlCopy } from './use-runtime-pairing-url-copy' +type RuntimePairingUrlGeneratorProps = { + framed?: boolean + showHeader?: boolean + showGeneratorForm?: boolean +} + export function RuntimePairingUrlGenerator({ framed = true, showHeader = true, diff --git a/src/renderer/src/components/settings/runtime-environments/pairing/runtime-pairing-link-state.ts b/src/renderer/src/components/settings/runtime-environments/pairing/runtime-pairing-link-state.ts index e67ae35628a..c2d9b2cf0d1 100644 --- a/src/renderer/src/components/settings/runtime-environments/pairing/runtime-pairing-link-state.ts +++ b/src/renderer/src/components/settings/runtime-environments/pairing/runtime-pairing-link-state.ts @@ -9,12 +9,6 @@ export function runtimePairingReachForIntent(intent: RuntimePairingIntent): Runt return intent === 'local' ? 'this-computer' : 'network' } -export type RuntimePairingUrlGeneratorProps = { - framed?: boolean - showHeader?: boolean - showGeneratorForm?: boolean -} - // Main retains pairing tokens; cache navigation state without persisting credential material. export const runtimePairingLinkCache: { selectedAddress: string diff --git a/src/renderer/src/components/settings/runtime-environments/runtime-servers-header-actions.tsx b/src/renderer/src/components/settings/runtime-environments/runtime-servers-header-actions.tsx index bd155ab5752..29f33e127d9 100644 --- a/src/renderer/src/components/settings/runtime-environments/runtime-servers-header-actions.tsx +++ b/src/renderer/src/components/settings/runtime-environments/runtime-servers-header-actions.tsx @@ -12,7 +12,7 @@ import { } from '../../ui/dropdown-menu' import { useRuntimeServersPage } from './runtime-servers-page-context' import { RuntimeShareComputerButton } from './pairing/runtime-share-computer-button' -import { getManagedOrcadApi } from './managed-servers/use-managed-server-catalog' +import { getManagedOrcadApi } from '../../../runtime/hosts/installation/managed-orcad-api-availability' function AddServerMenuItem({ icon, diff --git a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-list.tsx b/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-list.tsx index 51015bec6dd..4607f616df8 100644 --- a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-list.tsx +++ b/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-list.tsx @@ -14,7 +14,7 @@ import { Button } from '../../../ui/button' import { migrationPhaseLabel } from '../managed-servers/managed-server-copy' import type { RuntimeHostDetails } from '../catalog/runtime-environment-host-details' import { RuntimeServerRow } from './runtime-server-row' -import { ServerListRow } from './runtime-server-list-row' +import { ServerListRow } from '../../host-rows/ServerListRow' import type { ManagedServerCatalog } from '../managed-servers/use-managed-server-catalog' import { useManagedServerLifecycle } from '../managed-servers/use-managed-server-lifecycle' diff --git a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row-presentation.ts b/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row-presentation.ts index 52efdc5de6c..ea384f1953f 100644 --- a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row-presentation.ts +++ b/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row-presentation.ts @@ -12,7 +12,7 @@ import type { import { getRuntimeServerConnectionLabel } from '../catalog/runtime-environment-host-details' import { getRemoteServerUpdatePhaseLabel } from '../updates/RemoteServerUpdateStatus' -import type { ServerRowStatus } from './runtime-server-list-row' +import type { ServerRowStatus } from '../../host-rows/ServerListRow' const UPDATING_PHASES: ReadonlySet = new Set([ 'queued', diff --git a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row.tsx b/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row.tsx index 57c42429054..d636925097d 100644 --- a/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row.tsx +++ b/src/renderer/src/components/settings/runtime-environments/server-rows/runtime-server-row.tsx @@ -30,7 +30,7 @@ import { ServerRowChip, ServerRowDetailsToggle, type ServerRowStatus -} from './runtime-server-list-row' +} from '../../host-rows/ServerListRow' import { RuntimeSshAccessControl } from '../ssh-access/RuntimeSshAccessControl' import { RuntimeSshAccessDialog } from '../ssh-access/runtime-ssh-access-dialog' import { @@ -42,7 +42,7 @@ import { managedServerHasPrimaryAction, managedServerForgettable, ManagedServerPrimaryAction -} from '../managed-servers/managed-server-row-parts' +} from '../managed-servers/managed-server-row-content' import type { ManagedServerLifecycle } from '../managed-servers/use-managed-server-lifecycle' const NO_SSH_TARGETS: SshTarget[] = [] diff --git a/src/renderer/src/components/settings/runtime-environments/ssh-access/RuntimeSshAccessControl.tsx b/src/renderer/src/components/settings/runtime-environments/ssh-access/RuntimeSshAccessControl.tsx index a8c07e9df5e..8c95f3c97d5 100644 --- a/src/renderer/src/components/settings/runtime-environments/ssh-access/RuntimeSshAccessControl.tsx +++ b/src/renderer/src/components/settings/runtime-environments/ssh-access/RuntimeSshAccessControl.tsx @@ -9,7 +9,7 @@ import { createNonSecureContextUuid as createBrowserUuid } from '../../../../../ import { Button } from '../../../ui/button' import { Input } from '../../../ui/input' import { Label } from '../../../ui/label' -import { SshTargetSelect } from '../../../host-picker/SshTargetSelect' +import { HostCombobox } from '../../../host-picker/HostCombobox' const DEFAULT_REMOTE_PORT = '6768' @@ -161,9 +161,9 @@ export function RuntimeSshAccessControl({ - ({ useSshConnectFailureToast: () => vi.fn() })) vi.mock('./targets/SshTargetForm', () => ({ SshTargetForm: () => null })) -vi.mock('./targets/ssh-target-draft', () => ({ EMPTY_FORM: {} })) +vi.mock('../../ssh-host-form/ssh-target-draft', () => ({ EMPTY_FORM: {} })) vi.mock('./targets/SshTargetDestructiveActions', () => ({ SshTargetDestructiveActions: ({ children diff --git a/src/renderer/src/components/settings/ssh/SshPane.tsx b/src/renderer/src/components/settings/ssh/SshPane.tsx index 6d3a01a62c2..6f1f5c025e8 100644 --- a/src/renderer/src/components/settings/ssh/SshPane.tsx +++ b/src/renderer/src/components/settings/ssh/SshPane.tsx @@ -26,7 +26,7 @@ import { EMPTY_FORM, type EditingTarget, getEditingTargetForSshTarget -} from './targets/ssh-target-draft' +} from '../../ssh-host-form/ssh-target-draft' import { buildSshTargetSavePayload } from './targets/ssh-target-save-payload' import { HostRemoveDialog } from '../../sidebar/HostRemoveDialog' diff --git a/src/renderer/src/components/settings/ssh/server-actions/SshHostKeepServerVersionDialog.tsx b/src/renderer/src/components/settings/ssh/server-actions/SshHostKeepServerVersionDialog.tsx index fcbe49a9997..62521d91638 100644 --- a/src/renderer/src/components/settings/ssh/server-actions/SshHostKeepServerVersionDialog.tsx +++ b/src/renderer/src/components/settings/ssh/server-actions/SshHostKeepServerVersionDialog.tsx @@ -11,7 +11,7 @@ import { DialogHeader, DialogTitle } from '../../../ui/dialog' -import { getManagedOrcadApi } from '../../runtime-environments/managed-servers/use-managed-server-catalog' +import { getManagedOrcadApi } from '../../../../runtime/hosts/installation/managed-orcad-api-availability' /** Offer migration of changes made by an older build or retain the managed server’s version. */ export function hasSshHostSourceChanged(target: SshTarget): boolean { diff --git a/src/renderer/src/components/settings/ssh/server-actions/SshHostRecoverServerAction.tsx b/src/renderer/src/components/settings/ssh/server-actions/SshHostRecoverServerAction.tsx index a9bab8a23e5..77a5928661a 100644 --- a/src/renderer/src/components/settings/ssh/server-actions/SshHostRecoverServerAction.tsx +++ b/src/renderer/src/components/settings/ssh/server-actions/SshHostRecoverServerAction.tsx @@ -22,7 +22,7 @@ import { DialogTitle } from '../../../ui/dialog' import { Label } from '../../../ui/label' -import { getManagedOrcadApi } from '../../runtime-environments/managed-servers/use-managed-server-catalog' +import { getManagedOrcadApi } from '../../../../runtime/hosts/installation/managed-orcad-api-availability' /** Offered only when the slot's own refusal named the command, so that build supports it. */ export function canRecoverSshHostServer(status: SshManagedServerStatus | undefined): boolean { diff --git a/src/renderer/src/components/settings/ssh/server-actions/SshHostRestartServerAction.tsx b/src/renderer/src/components/settings/ssh/server-actions/SshHostRestartServerAction.tsx index 3332845233d..d010b9315a5 100644 --- a/src/renderer/src/components/settings/ssh/server-actions/SshHostRestartServerAction.tsx +++ b/src/renderer/src/components/settings/ssh/server-actions/SshHostRestartServerAction.tsx @@ -13,7 +13,7 @@ import { DialogHeader, DialogTitle } from '../../../ui/dialog' -import { getManagedOrcadApi } from '../../runtime-environments/managed-servers/use-managed-server-catalog' +import { getManagedOrcadApi } from '../../../../runtime/hosts/installation/managed-orcad-api-availability' /** Offered only when the launching SSH session proved ended. */ export function canRestartSshHostServer(status: SshManagedServerStatus | undefined): boolean { diff --git a/src/renderer/src/components/settings/ssh/server-actions/SshTargetServerStatus.tsx b/src/renderer/src/components/settings/ssh/server-actions/SshTargetServerStatus.tsx index a4b57a323dc..66f582d6fb3 100644 --- a/src/renderer/src/components/settings/ssh/server-actions/SshTargetServerStatus.tsx +++ b/src/renderer/src/components/settings/ssh/server-actions/SshTargetServerStatus.tsx @@ -9,14 +9,17 @@ import { SshHostKeepServerVersionDialog } from './SshHostKeepServerVersionDialog' import { SshHostDeltaMoveDialog } from './SshHostDeltaMoveDialog' -import { SshHostNodeMirrorAction } from './SshHostNodeMirrorAction' +import { SshHostNodeMirrorAction } from '../../../ssh-host-recovery/SshHostNodeMirrorAction' import { OpenPlainSshTerminalButton } from '../../../plain-ssh-terminal/OpenPlainSshTerminalButton' -import { canMoveSshHostAnyway, SshHostMoveAnywayDialog } from './SshHostMoveAnywayAction' +import { + canMoveSshHostAnyway, + SshHostMoveAnywayDialog +} from '../../../ssh-host-recovery/SshHostMoveAnywayAction' import { canRestartSshHostServer, SshHostRestartServerDialog } from './SshHostRestartServerAction' import { canRecoverSshHostServer, SshHostRecoverServerDialog } from './SshHostRecoverServerAction' import { Button } from '../../../ui/button' import { isConnectingSshStatus } from '../../../../ssh/ssh-connection-recoverability' -import { getManagedOrcadApi } from '../../runtime-environments/managed-servers/use-managed-server-catalog' +import { getManagedOrcadApi } from '../../../../runtime/hosts/installation/managed-orcad-api-availability' export type SshTargetServerDialog = | 'details' diff --git a/src/renderer/src/components/settings/ssh/targets/SshTargetForm.test.tsx b/src/renderer/src/components/settings/ssh/targets/SshTargetForm.test.tsx index 2e288d7cfb1..f66fb767a9c 100644 --- a/src/renderer/src/components/settings/ssh/targets/SshTargetForm.test.tsx +++ b/src/renderer/src/components/settings/ssh/targets/SshTargetForm.test.tsx @@ -4,7 +4,7 @@ import { act } from 'react' import { createRoot, type Root } from 'react-dom/client' import { afterEach, describe, expect, it, vi } from 'vitest' -import { EMPTY_FORM, type EditingTarget } from './ssh-target-draft' +import { EMPTY_FORM, type EditingTarget } from '../../../ssh-host-form/ssh-target-draft' import { SshTargetForm } from './SshTargetForm' afterEach(() => { diff --git a/src/renderer/src/components/settings/ssh/targets/SshTargetForm.tsx b/src/renderer/src/components/settings/ssh/targets/SshTargetForm.tsx index b6c2a5d036b..406eee532ee 100644 --- a/src/renderer/src/components/settings/ssh/targets/SshTargetForm.tsx +++ b/src/renderer/src/components/settings/ssh/targets/SshTargetForm.tsx @@ -11,13 +11,13 @@ import { } from '../../../ui/dialog' import { Input } from '../../../ui/input' import { Label } from '../../../ui/label' -import { SshHostAdvancedFields } from './SshHostAdvancedFields' +import { SshHostAdvancedFields } from '../../../ssh-host-form/SshHostAdvancedFields' import { applyParsedSshHostInput, hasAdvancedConnectionValues, isSshTargetFormDirty, type EditingTarget -} from './ssh-target-draft' +} from '../../../ssh-host-form/ssh-target-draft' import { useTranslate } from '@/i18n/use-translate' import { preventOutsideDismissWhenDirty } from '@/lib/outside-dismiss-guard' diff --git a/src/renderer/src/components/settings/ssh/targets/ssh-target-save-payload.ts b/src/renderer/src/components/settings/ssh/targets/ssh-target-save-payload.ts index 3c4416e6c58..f3d4f0854a5 100644 --- a/src/renderer/src/components/settings/ssh/targets/ssh-target-save-payload.ts +++ b/src/renderer/src/components/settings/ssh/targets/ssh-target-save-payload.ts @@ -1,5 +1,8 @@ import type { SshTargetCreateInput, SshTargetUpdateInput } from '../../../../../../shared/ssh-types' -import { getSshTargetDraftConnectionFields, type EditingTarget } from './ssh-target-draft' +import { + getSshTargetDraftConnectionFields, + type EditingTarget +} from '../../../ssh-host-form/ssh-target-draft' import { translate } from '../../../../i18n/i18n' type SshTargetSavePayload = { diff --git a/src/renderer/src/components/settings/terminal/TerminalPane.tsx b/src/renderer/src/components/settings/terminal/TerminalPane.tsx index f43ee3d121d..10132ea19e5 100644 --- a/src/renderer/src/components/settings/terminal/TerminalPane.tsx +++ b/src/renderer/src/components/settings/terminal/TerminalPane.tsx @@ -9,7 +9,7 @@ import { SettingsSectionStack } from '../section-layout/SettingsSectionStack' import { matchesSettingsSearch } from '../search/settings-search' import { useAppStore } from '../../../store/index' import { isMacUserAgent, isWindowsUserAgent } from '@/lib/user-agent-platform' -import { getManageSessionsSearchEntries } from './terminal-window-setup-search' +import { getManageSessionsSearchEntries } from './session-management/manage-sessions-search' import { getTerminalDefaultShellSearchEntry } from './terminal-default-shell-search' import { getTerminalAdvancedSearchEntries, diff --git a/src/renderer/src/components/settings/terminal/appearance/TerminalAppearanceSection.tsx b/src/renderer/src/components/settings/terminal/appearance/TerminalAppearanceSection.tsx index 6c6c2d5d6a6..3c6be4eea38 100644 --- a/src/renderer/src/components/settings/terminal/appearance/TerminalAppearanceSection.tsx +++ b/src/renderer/src/components/settings/terminal/appearance/TerminalAppearanceSection.tsx @@ -21,9 +21,9 @@ import { getTerminalWarpImportSearchEntries, getTerminalYamlImportSearchEntries } from '../themes/terminal-theme-search' -import { getTerminalConfigImportSearchEntries } from '../platform/terminal-advanced-platform-search' +import { getTerminalConfigImportSearchEntries } from '../configuration/terminal-config-import-search' import { getTerminalPaneAppearanceSearchEntries } from './terminal-pane-appearance-search' -import { getTerminalWindowSearchEntries } from '../terminal-window-setup-search' +import { getTerminalWindowSearchEntries } from './terminal-window-search' import { Button } from '../../../ui/button' import { SettingsRow, SettingsSubsectionHeader } from '../../form-controls/SettingsFormControls' import { FontAutocomplete } from '../../appearance/fonts/FontAutocomplete' @@ -32,7 +32,7 @@ import { TerminalFontSizeSetting } from './TerminalFontSizeSetting' import { TerminalAdvancedTypographyControls } from './TerminalAdvancedTypographyControls' import { TerminalThemeCatalogSection } from '../themes/TerminalThemeSections' import { ClaudeCodeThemeSyncSetting } from '../themes/ClaudeCodeThemeSyncSetting' -import { TerminalWindowSection } from '../TerminalWindowSection' +import { TerminalWindowSection } from './TerminalWindowSection' import { TerminalCursorAppearanceSection } from './TerminalCursorAppearanceSection' import { TerminalPaneAppearanceSection } from './TerminalPaneAppearanceSection' import { AppearanceAdvancedDisclosure } from '../../appearance/AppearanceAdvancedDisclosure' diff --git a/src/renderer/src/components/settings/terminal/TerminalWindowSection.tsx b/src/renderer/src/components/settings/terminal/appearance/TerminalWindowSection.tsx similarity index 94% rename from src/renderer/src/components/settings/terminal/TerminalWindowSection.tsx rename to src/renderer/src/components/settings/terminal/appearance/TerminalWindowSection.tsx index 51b46eae19b..3a4f740904e 100644 --- a/src/renderer/src/components/settings/terminal/TerminalWindowSection.tsx +++ b/src/renderer/src/components/settings/terminal/appearance/TerminalWindowSection.tsx @@ -1,18 +1,18 @@ import { useState } from 'react' -import type { GlobalSettings } from '../../../../../shared/global-settings-types' +import type { GlobalSettings } from '../../../../../../shared/global-settings-types' import { TERMINAL_COLOR_OVERRIDES_SETTING, terminalColorOverridesForSlot, type TerminalThemeSlot -} from '../../../../../shared/terminal-themes/terminal-color-overrides' -import { Button } from '../../ui/button' -import { Label } from '../../ui/label' -import { Switch } from '../../ui/switch' -import { ColorField, NumberField } from '../form-controls/SettingsFormControls' -import { SearchableSetting } from '../section-layout/SearchableSetting' +} from '../../../../../../shared/terminal-themes/terminal-color-overrides' +import { Button } from '../../../ui/button' +import { Label } from '../../../ui/label' +import { Switch } from '../../../ui/switch' +import { ColorField, NumberField } from '../../form-controls/SettingsFormControls' +import { SearchableSetting } from '../../section-layout/SearchableSetting' import { clampNumber } from '@/lib/terminal-theme' import { isWebClientLocation } from '@/lib/web-client-location' -import { WindowMaterialSetting } from '../appearance/WindowMaterialSetting' +import { WindowMaterialSetting } from '../../appearance/WindowMaterialSetting' import { useTranslate } from '@/i18n/use-translate' import { COLOR_OVERRIDE_GROUPS } from './terminal-window-color-groups' diff --git a/src/renderer/src/components/settings/terminal/terminal-window-color-groups.tsx b/src/renderer/src/components/settings/terminal/appearance/terminal-window-color-groups.tsx similarity index 98% rename from src/renderer/src/components/settings/terminal/terminal-window-color-groups.tsx rename to src/renderer/src/components/settings/terminal/appearance/terminal-window-color-groups.tsx index 128a865d2cf..32cf6070cf5 100644 --- a/src/renderer/src/components/settings/terminal/terminal-window-color-groups.tsx +++ b/src/renderer/src/components/settings/terminal/appearance/terminal-window-color-groups.tsx @@ -1,4 +1,4 @@ -import type { TerminalColorOverrides } from '../../../../../shared/terminal-themes/terminal-color-overrides' +import type { TerminalColorOverrides } from '../../../../../../shared/terminal-themes/terminal-color-overrides' import { translate } from '@/i18n/i18n' export const COLOR_OVERRIDE_GROUPS: { diff --git a/src/renderer/src/components/settings/terminal/terminal-window-setup-search.ts b/src/renderer/src/components/settings/terminal/appearance/terminal-window-search.ts similarity index 71% rename from src/renderer/src/components/settings/terminal/terminal-window-setup-search.ts rename to src/renderer/src/components/settings/terminal/appearance/terminal-window-search.ts index 0d7a5cb1605..16737becbd4 100644 --- a/src/renderer/src/components/settings/terminal/terminal-window-setup-search.ts +++ b/src/renderer/src/components/settings/terminal/appearance/terminal-window-search.ts @@ -1,36 +1,7 @@ import { translate } from '@/i18n/i18n' -import { translateSearchKeyword } from '../search/settings-search-keywords' +import { translateSearchKeyword } from '../../search/settings-search-keywords' import { createLocalizedCatalog } from '@/i18n/localized-catalog' -export const getManageSessionsSearchEntries = createLocalizedCatalog(() => [ - { - title: translate('auto.components.settings.terminal.search.6f5d486a68', 'Manage Sessions'), - description: translate( - 'auto.components.settings.terminal.search.f72abc493c', - 'Recover from frozen terminals by killing sessions, clearing saved scrollback, or restarting the daemon.' - ), - keywords: [ - ...translateSearchKeyword('auto.components.settings.terminal.search.f35400f7e8', 'daemon'), - ...translateSearchKeyword('auto.components.settings.terminal.search.9f2dda133c', 'pty'), - ...translateSearchKeyword('auto.components.settings.terminal.search.d802a578bf', 'sessions'), - ...translateSearchKeyword('auto.components.settings.terminal.search.a8d2784214', 'manage'), - ...translateSearchKeyword('auto.components.settings.terminal.search.a3e5297c10', 'kill'), - ...translateSearchKeyword('auto.components.settings.terminal.search.920573d65b', 'kill all'), - ...translateSearchKeyword('auto.components.settings.terminal.search.456da64d4d', 'clear'), - ...translateSearchKeyword('auto.components.settings.terminal.search.3982d88725', 'history'), - ...translateSearchKeyword( - 'auto.components.settings.terminal.search.cde233f5da', - 'scrollback' - ), - ...translateSearchKeyword('auto.components.settings.terminal.search.6892fb1019', 'restart'), - ...translateSearchKeyword('auto.components.settings.terminal.search.f66a7cf715', 'terminal'), - ...translateSearchKeyword('auto.components.settings.terminal.search.0a05629060', 'recover'), - ...translateSearchKeyword('auto.components.settings.terminal.search.88561b3499', 'frozen'), - ...translateSearchKeyword('auto.components.settings.terminal.search.d4daf4f612', 'unfreeze') - ] - } -]) - export const getTerminalWindowSearchEntries = createLocalizedCatalog(() => [ { title: translate('auto.components.settings.terminal.search.b36fd2416d', 'Background Opacity'), diff --git a/src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts b/src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts new file mode 100644 index 00000000000..8221bc5fd37 --- /dev/null +++ b/src/renderer/src/components/settings/terminal/configuration/terminal-config-import-search.ts @@ -0,0 +1,37 @@ +import { translate } from '@/i18n/i18n' +import { translateSearchKeyword } from '../../search/settings-search-keywords' +import { createLocalizedCatalog } from '@/i18n/localized-catalog' + +export const getTerminalConfigImportSearchEntries = createLocalizedCatalog(() => [ + { + title: translate('auto.components.settings.terminal.search.a979df0083', 'Import from Ghostty'), + description: translate( + 'auto.components.settings.terminal.search.73e9422f19', + 'One-time import of supported Ghostty terminal settings.' + ), + keywords: [ + // Keep product names searchable without translation. + 'Ghostty', + 'ghostty', + ...translateSearchKeyword('auto.components.settings.terminal.search.fd752b3cac', 'import'), + ...translateSearchKeyword('auto.components.settings.terminal.search.f66a7cf715', 'terminal'), + ...translateSearchKeyword('auto.components.settings.terminal.search.2ade3ea490', 'config'), + ...translateSearchKeyword('auto.components.settings.terminal.search.10f9fb6fea', 'settings') + ] + }, + { + title: translate('auto.components.settings.terminal.search.kitty_import', 'Import from Kitty'), + description: translate( + 'auto.components.settings.terminal.search.kitty_import_description', + 'One-time import of supported settings from kitty.conf.' + ), + keywords: [ + // Keep product names searchable without translation. + 'Kitty', + 'kitty', + 'kitty.conf', + ...translateSearchKeyword('auto.components.settings.terminal.search.fd752b3cac', 'import'), + ...translateSearchKeyword('auto.components.settings.terminal.search.2ade3ea490', 'config') + ] + } +]) diff --git a/src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts b/src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts index 254a43b8b68..d1f97b6c5f0 100644 --- a/src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts +++ b/src/renderer/src/components/settings/terminal/platform/terminal-advanced-platform-search.ts @@ -116,37 +116,3 @@ export const getTerminalMacYenSearchEntries = createLocalizedCatalog(() => [ ] } ]) - -export const getTerminalConfigImportSearchEntries = createLocalizedCatalog(() => [ - { - title: translate('auto.components.settings.terminal.search.a979df0083', 'Import from Ghostty'), - description: translate( - 'auto.components.settings.terminal.search.73e9422f19', - 'One-time import of supported Ghostty terminal settings.' - ), - keywords: [ - // Keep product names searchable without translation. - 'Ghostty', - 'ghostty', - ...translateSearchKeyword('auto.components.settings.terminal.search.fd752b3cac', 'import'), - ...translateSearchKeyword('auto.components.settings.terminal.search.f66a7cf715', 'terminal'), - ...translateSearchKeyword('auto.components.settings.terminal.search.2ade3ea490', 'config'), - ...translateSearchKeyword('auto.components.settings.terminal.search.10f9fb6fea', 'settings') - ] - }, - { - title: translate('auto.components.settings.terminal.search.kitty_import', 'Import from Kitty'), - description: translate( - 'auto.components.settings.terminal.search.kitty_import_description', - 'One-time import of supported settings from kitty.conf.' - ), - keywords: [ - // Keep product names searchable without translation. - 'Kitty', - 'kitty', - 'kitty.conf', - ...translateSearchKeyword('auto.components.settings.terminal.search.fd752b3cac', 'import'), - ...translateSearchKeyword('auto.components.settings.terminal.search.2ade3ea490', 'config') - ] - } -]) diff --git a/src/renderer/src/components/settings/terminal/session-management/ManageSessionsSection.tsx b/src/renderer/src/components/settings/terminal/session-management/ManageSessionsSection.tsx index 250e793ee3f..ac8bd54a34a 100644 --- a/src/renderer/src/components/settings/terminal/session-management/ManageSessionsSection.tsx +++ b/src/renderer/src/components/settings/terminal/session-management/ManageSessionsSection.tsx @@ -3,7 +3,7 @@ import { toast } from 'sonner' import type { PtyManagementSession } from '../../../../../../preload/api/pty-management-api' import { SearchableSetting } from '../../section-layout/SearchableSetting' import { SettingsGroup } from '../../section-layout/SettingsGroup' -import { getManageSessionsSearchEntries } from '../terminal-window-setup-search' +import { getManageSessionsSearchEntries } from './manage-sessions-search' import { useAppStore } from '../../../../store/index' import { activateAndRevealWorktree } from '@/lib/workspace/activation/worktree-activation' import { activateTabAndFocusPane } from '@/lib/activate-tab-and-focus-pane' diff --git a/src/renderer/src/components/settings/terminal/session-management/manage-sessions-search.ts b/src/renderer/src/components/settings/terminal/session-management/manage-sessions-search.ts new file mode 100644 index 00000000000..6c3941c6467 --- /dev/null +++ b/src/renderer/src/components/settings/terminal/session-management/manage-sessions-search.ts @@ -0,0 +1,32 @@ +import { translate } from '@/i18n/i18n' +import { translateSearchKeyword } from '../../search/settings-search-keywords' +import { createLocalizedCatalog } from '@/i18n/localized-catalog' + +export const getManageSessionsSearchEntries = createLocalizedCatalog(() => [ + { + title: translate('auto.components.settings.terminal.search.6f5d486a68', 'Manage Sessions'), + description: translate( + 'auto.components.settings.terminal.search.f72abc493c', + 'Recover from frozen terminals by killing sessions, clearing saved scrollback, or restarting the daemon.' + ), + keywords: [ + ...translateSearchKeyword('auto.components.settings.terminal.search.f35400f7e8', 'daemon'), + ...translateSearchKeyword('auto.components.settings.terminal.search.9f2dda133c', 'pty'), + ...translateSearchKeyword('auto.components.settings.terminal.search.d802a578bf', 'sessions'), + ...translateSearchKeyword('auto.components.settings.terminal.search.a8d2784214', 'manage'), + ...translateSearchKeyword('auto.components.settings.terminal.search.a3e5297c10', 'kill'), + ...translateSearchKeyword('auto.components.settings.terminal.search.920573d65b', 'kill all'), + ...translateSearchKeyword('auto.components.settings.terminal.search.456da64d4d', 'clear'), + ...translateSearchKeyword('auto.components.settings.terminal.search.3982d88725', 'history'), + ...translateSearchKeyword( + 'auto.components.settings.terminal.search.cde233f5da', + 'scrollback' + ), + ...translateSearchKeyword('auto.components.settings.terminal.search.6892fb1019', 'restart'), + ...translateSearchKeyword('auto.components.settings.terminal.search.f66a7cf715', 'terminal'), + ...translateSearchKeyword('auto.components.settings.terminal.search.0a05629060', 'recover'), + ...translateSearchKeyword('auto.components.settings.terminal.search.88561b3499', 'frozen'), + ...translateSearchKeyword('auto.components.settings.terminal.search.d4daf4f612', 'unfreeze') + ] + } +]) diff --git a/src/renderer/src/components/settings/terminal/terminal-search.ts b/src/renderer/src/components/settings/terminal/terminal-search.ts index 47b56a98152..587e0a426d5 100644 --- a/src/renderer/src/components/settings/terminal/terminal-search.ts +++ b/src/renderer/src/components/settings/terminal/terminal-search.ts @@ -1,10 +1,10 @@ import type { SettingsSearchEntry } from '../search/settings-search' import { getTerminalAdvancedSearchEntries, - getTerminalConfigImportSearchEntries, getTerminalMacOptionSearchEntries, getTerminalMacYenSearchEntries } from './platform/terminal-advanced-platform-search' +import { getTerminalConfigImportSearchEntries } from './configuration/terminal-config-import-search' import { getTerminalPaneAppearanceSearchEntries, getTerminalPaneInteractionSearchEntries @@ -28,10 +28,8 @@ import { getTerminalWindowsShellSearchEntry, getTerminalWindowsAdministratorSearchEntry } from './platform/terminal-windows-search' -import { - getManageSessionsSearchEntries, - getTerminalWindowSearchEntries -} from './terminal-window-setup-search' +import { getManageSessionsSearchEntries } from './session-management/manage-sessions-search' +import { getTerminalWindowSearchEntries } from './appearance/terminal-window-search' import { createLocalizedCatalog } from '@/i18n/localized-catalog' import { translate } from '@/i18n/i18n' import type { Translate } from '@/i18n/use-translate' diff --git a/src/renderer/src/components/settings/runtime-environments/updates/GeneralRemoteServerUpdates.tsx b/src/renderer/src/components/settings/updates/GeneralRemoteServerUpdates.tsx similarity index 98% rename from src/renderer/src/components/settings/runtime-environments/updates/GeneralRemoteServerUpdates.tsx rename to src/renderer/src/components/settings/updates/GeneralRemoteServerUpdates.tsx index b26da6b9e86..2f6c6752394 100644 --- a/src/renderer/src/components/settings/runtime-environments/updates/GeneralRemoteServerUpdates.tsx +++ b/src/renderer/src/components/settings/updates/GeneralRemoteServerUpdates.tsx @@ -5,7 +5,7 @@ import { Button } from '@/components/ui/button' import { useAppStore } from '@/store/index' import { useTranslate } from '@/i18n/use-translate' import { getUpdateCheckClickOptions, getUpdateCheckHint } from '@/lib/update-check-click-options' -import { SearchableSetting } from '../../section-layout/SearchableSetting' +import { SearchableSetting } from '../section-layout/SearchableSetting' export function GeneralRemoteServerUpdates(): React.JSX.Element | null { const t = useTranslate() diff --git a/src/renderer/src/components/settings/updates/GeneralUpdateSettingsSection.tsx b/src/renderer/src/components/settings/updates/GeneralUpdateSettingsSection.tsx index 1034992d27d..481a40399dc 100644 --- a/src/renderer/src/components/settings/updates/GeneralUpdateSettingsSection.tsx +++ b/src/renderer/src/components/settings/updates/GeneralUpdateSettingsSection.tsx @@ -10,7 +10,7 @@ import { SettingsGroup } from '../section-layout/SettingsGroup' import { useIntlLocale, useTranslate } from '@/i18n/use-translate' import { updaterMessageText } from '@/lib/updater-message-copy' import { getUpdateCheckClickOptions, getUpdateCheckHint } from '@/lib/update-check-click-options' -import { GeneralRemoteServerUpdates } from '../runtime-environments/updates/GeneralRemoteServerUpdates' +import { GeneralRemoteServerUpdates } from './GeneralRemoteServerUpdates' import { ReleaseChannelSection } from './ReleaseChannelSection' import { UpdateDelaySetting } from './UpdateDelaySetting' import { formatUpdateDelayDate } from './update-delay-copy' diff --git a/src/renderer/src/components/settings/wsl/hosts/WslHostsSection.tsx b/src/renderer/src/components/settings/wsl/hosts/WslHostsSection.tsx index 3ba5c7ff506..a374d19028d 100644 --- a/src/renderer/src/components/settings/wsl/hosts/WslHostsSection.tsx +++ b/src/renderer/src/components/settings/wsl/hosts/WslHostsSection.tsx @@ -40,11 +40,7 @@ import { Badge } from '../../../ui/badge' import { Button } from '../../../ui/button' import { DropdownMenuItem } from '../../../ui/dropdown-menu' import { managedServerOutcomeLabel } from '../../runtime-environments/managed-servers/managed-server-copy' -import { - ServerListRow, - ServerRowChip, - type ServerRowStatus -} from '../../runtime-environments/server-rows/runtime-server-list-row' +import { ServerListRow, ServerRowChip, type ServerRowStatus } from '../../host-rows/ServerListRow' import { WslMoveOrchestratorStatusLine } from '../../../wsl-move/WslMoveOrchestratorStatusLine' import { wslMoveOrchestratorLine } from '../../../wsl-move/wsl-move-orchestrator-copy' import { diff --git a/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostDialog.tsx b/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostDialog.tsx index 9636e321a49..4213e182a3a 100644 --- a/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostDialog.tsx +++ b/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostDialog.tsx @@ -3,7 +3,7 @@ import { toast } from 'sonner' import { Dialog, DialogContent } from '@/components/ui/dialog' import { useTranslate } from '@/i18n/use-translate' import { useAppStore } from '@/store/index' -import { EMPTY_FORM, type EditingTarget } from '../../../settings/ssh/targets/ssh-target-draft' +import { EMPTY_FORM, type EditingTarget } from '../../../ssh-host-form/ssh-target-draft' import type { SshConfigHostSummary } from '../../../../../../shared/ssh-types' import { parseHostAccessLink } from '../../../../../../shared/remote-runtime/pairing/remote-pairing-address' import { diff --git a/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostFields.tsx b/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostFields.tsx index 059694824e1..ef4ed4d5ca2 100644 --- a/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostFields.tsx +++ b/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostFields.tsx @@ -12,8 +12,8 @@ import type { ParseHostAccessLinkResult } from '../../../../../../shared/remote- import { applyParsedSshHostInput, type EditingTarget -} from '../../../settings/ssh/targets/ssh-target-draft' -import { SshHostAdvancedFields } from '../../../settings/ssh/targets/SshHostAdvancedFields' +} from '../../../ssh-host-form/ssh-target-draft' +import { SshHostAdvancedFields } from '../../../ssh-host-form/SshHostAdvancedFields' export function SshHostFields({ form, diff --git a/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostSshFormPanel.tsx b/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostSshFormPanel.tsx index 04ac91802f5..b6c4eeb9ef3 100644 --- a/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostSshFormPanel.tsx +++ b/src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostSshFormPanel.tsx @@ -1,7 +1,7 @@ import { DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { useTranslate } from '@/i18n/use-translate' -import type { EditingTarget } from '../../../settings/ssh/targets/ssh-target-draft' +import type { EditingTarget } from '../../../ssh-host-form/ssh-target-draft' import { SshHostFields } from './AddRemoteHostFields' export function AddRemoteHostSshFormPanel({ diff --git a/src/renderer/src/components/sidebar/host-admission/ssh/AddServerSshHostDialog.tsx b/src/renderer/src/components/sidebar/host-admission/ssh/AddServerSshHostDialog.tsx index a222447847d..7f2957e364d 100644 --- a/src/renderer/src/components/sidebar/host-admission/ssh/AddServerSshHostDialog.tsx +++ b/src/renderer/src/components/sidebar/host-admission/ssh/AddServerSshHostDialog.tsx @@ -1,7 +1,7 @@ import { useState } from 'react' import { Dialog, DialogContent } from '@/components/ui/dialog' import { useAppStore } from '@/store/index' -import { EMPTY_FORM, type EditingTarget } from '../../../settings/ssh/targets/ssh-target-draft' +import { EMPTY_FORM, type EditingTarget } from '../../../ssh-host-form/ssh-target-draft' import { addRuntimeSshTarget, listRuntimeEditableSshTargets diff --git a/src/renderer/src/components/sidebar/host-admission/ssh/add-remote-host-ssh-actions.ts b/src/renderer/src/components/sidebar/host-admission/ssh/add-remote-host-ssh-actions.ts index 87435877ee2..8652abb3b2d 100644 --- a/src/renderer/src/components/sidebar/host-admission/ssh/add-remote-host-ssh-actions.ts +++ b/src/renderer/src/components/sidebar/host-admission/ssh/add-remote-host-ssh-actions.ts @@ -5,7 +5,7 @@ import { getSshTargetDraftConnectionFields, hasAdvancedConnectionValues, type EditingTarget -} from '../../../settings/ssh/targets/ssh-target-draft' +} from '../../../ssh-host-form/ssh-target-draft' import { SSH_CONFIG_HOST_RESULT_LIMIT, type SshConfigHostSummary, diff --git a/src/renderer/src/components/host-picker/host-list-surfaces.large-config.test.tsx b/src/renderer/src/components/sidebar/project-admission/host-selection/AddRepoHostSelector.large-config.test.tsx similarity index 88% rename from src/renderer/src/components/host-picker/host-list-surfaces.large-config.test.tsx rename to src/renderer/src/components/sidebar/project-admission/host-selection/AddRepoHostSelector.large-config.test.tsx index 359510978ee..f054086afa6 100644 --- a/src/renderer/src/components/host-picker/host-list-surfaces.large-config.test.tsx +++ b/src/renderer/src/components/sidebar/project-admission/host-selection/AddRepoHostSelector.large-config.test.tsx @@ -2,9 +2,9 @@ import { cleanup, fireEvent, render, screen } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' -import type { SidebarHostOption } from '../sidebar/worktree-list/controls/sidebar-host-options' +import type { SidebarHostOption } from '../../worktree-list/controls/sidebar-host-options' -import { AddRepoHostSelector } from '../sidebar/project-admission/host-selection/AddRepoHostSelector' +import { AddRepoHostSelector } from './AddRepoHostSelector' // Why 13,692: the host count from the generated ~/.ssh/config in the renderer crash this guards. const HOST_COUNT = 13_692 diff --git a/src/renderer/src/components/settings/ssh/targets/SshHostAdvancedFields.fixture.tsx b/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.fixture.tsx similarity index 87% rename from src/renderer/src/components/settings/ssh/targets/SshHostAdvancedFields.fixture.tsx rename to src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.fixture.tsx index 41b8d139d7e..c99a4f877c9 100644 --- a/src/renderer/src/components/settings/ssh/targets/SshHostAdvancedFields.fixture.tsx +++ b/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.fixture.tsx @@ -1,4 +1,4 @@ -import { defineFixture } from '../../../../component-fixtures/component-fixture' +import { defineFixture } from '../../component-fixtures/component-fixture' import { SshHostAdvancedFields } from './SshHostAdvancedFields' import { EMPTY_FORM } from './ssh-target-draft' diff --git a/src/renderer/src/components/settings/ssh/targets/SshHostAdvancedFields.tsx b/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.tsx similarity index 99% rename from src/renderer/src/components/settings/ssh/targets/SshHostAdvancedFields.tsx rename to src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.tsx index 519961fec74..8c41ec05206 100644 --- a/src/renderer/src/components/settings/ssh/targets/SshHostAdvancedFields.tsx +++ b/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.tsx @@ -4,7 +4,7 @@ import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/component import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { cn } from '@/lib/utils' -import { SettingsSwitch } from '../../form-controls/SettingsFormControls' +import { SettingsSwitch } from '../settings/form-controls/SettingsFormControls' import type { EditingTarget } from './ssh-target-draft' import { useTranslate } from '@/i18n/use-translate' diff --git a/src/renderer/src/components/settings/ssh/targets/ssh-target-draft.test.ts b/src/renderer/src/components/ssh-host-form/ssh-target-draft.test.ts similarity index 100% rename from src/renderer/src/components/settings/ssh/targets/ssh-target-draft.test.ts rename to src/renderer/src/components/ssh-host-form/ssh-target-draft.test.ts diff --git a/src/renderer/src/components/settings/ssh/targets/ssh-target-draft.ts b/src/renderer/src/components/ssh-host-form/ssh-target-draft.ts similarity index 98% rename from src/renderer/src/components/settings/ssh/targets/ssh-target-draft.ts rename to src/renderer/src/components/ssh-host-form/ssh-target-draft.ts index 4a651a8974e..479b82dbcb7 100644 --- a/src/renderer/src/components/settings/ssh/targets/ssh-target-draft.ts +++ b/src/renderer/src/components/ssh-host-form/ssh-target-draft.ts @@ -1,5 +1,5 @@ -import type { SshConfigHostResolution, SshTarget } from '../../../../../../shared/ssh-types' -import { isRemoteCliControlAllowed } from '../../../../../../shared/ssh-remote-cli-control' +import type { SshConfigHostResolution, SshTarget } from '../../../../shared/ssh-types' +import { isRemoteCliControlAllowed } from '../../../../shared/ssh-remote-cli-control' export type EditingTarget = { label: string diff --git a/src/renderer/src/components/settings/ssh/server-actions/SshHostMoveAnywayAction.tsx b/src/renderer/src/components/ssh-host-recovery/SshHostMoveAnywayAction.tsx similarity index 95% rename from src/renderer/src/components/settings/ssh/server-actions/SshHostMoveAnywayAction.tsx rename to src/renderer/src/components/ssh-host-recovery/SshHostMoveAnywayAction.tsx index c553c1c1553..f4d5d5a5c7e 100644 --- a/src/renderer/src/components/settings/ssh/server-actions/SshHostMoveAnywayAction.tsx +++ b/src/renderer/src/components/ssh-host-recovery/SshHostMoveAnywayAction.tsx @@ -1,10 +1,10 @@ import { useState } from 'react' -import type { SshManagedServerStatus, SshTarget } from '../../../../../../shared/ssh-types' +import type { SshManagedServerStatus, SshTarget } from '../../../../shared/ssh-types' import { useMountedRef } from '@/hooks/useMountedRef' import { useTranslate } from '@/i18n/use-translate' import { sshHostServerReassurance } from '@/ssh/ssh-host-server-setup-copy' import { trackSshConnect } from '@/ssh/ssh-connect-in-flight' -import { Button } from '../../../ui/button' +import { Button } from '../ui/button' import { Dialog, DialogContent, @@ -12,8 +12,8 @@ import { DialogFooter, DialogHeader, DialogTitle -} from '../../../ui/dialog' -import { getManagedOrcadApi } from '../../runtime-environments/managed-servers/use-managed-server-catalog' +} from '../ui/dialog' +import { getManagedOrcadApi } from '../../runtime/hosts/installation/managed-orcad-api-availability' /** Offered only for a refusal that saved state alone causes. */ export function canMoveSshHostAnyway(status: SshManagedServerStatus | undefined): boolean { diff --git a/src/renderer/src/components/settings/ssh/server-actions/SshHostNodeMirrorAction.tsx b/src/renderer/src/components/ssh-host-recovery/SshHostNodeMirrorAction.tsx similarity index 88% rename from src/renderer/src/components/settings/ssh/server-actions/SshHostNodeMirrorAction.tsx rename to src/renderer/src/components/ssh-host-recovery/SshHostNodeMirrorAction.tsx index 9735ee424b1..a7c59eb98f4 100644 --- a/src/renderer/src/components/settings/ssh/server-actions/SshHostNodeMirrorAction.tsx +++ b/src/renderer/src/components/ssh-host-recovery/SshHostNodeMirrorAction.tsx @@ -1,11 +1,11 @@ -import type { SshManagedServerStatus } from '../../../../../../shared/ssh-types' +import type { SshManagedServerStatus } from '../../../../shared/ssh-types' import { useAppStore } from '@/store/index' import { nodeMirrorRecoveryLabel } from '@/ssh/ssh-host-node-download-copy' import { openNodeMirrorRecovery, thisComputerNodeMirrorRecovery } from '@/ssh/node-dist-mirror-recovery' -import { Button } from '../../../ui/button' +import { Button } from '../ui/button' import { useTranslate } from '@/i18n/use-translate' export function SshHostNodeMirrorAction({ diff --git a/src/renderer/src/components/terminal-pane/pty-transport/recovery/ssh/TerminalSshReconnectOverlay.tsx b/src/renderer/src/components/terminal-pane/pty-transport/recovery/ssh/TerminalSshReconnectOverlay.tsx index 07a86189157..89952cfb84e 100644 --- a/src/renderer/src/components/terminal-pane/pty-transport/recovery/ssh/TerminalSshReconnectOverlay.tsx +++ b/src/renderer/src/components/terminal-pane/pty-transport/recovery/ssh/TerminalSshReconnectOverlay.tsx @@ -2,8 +2,8 @@ import { useCallback } from 'react' import { Loader2, Server, ServerOff } from 'lucide-react' import { toastSshConnectFailure } from '@/ssh/ssh-connect-failure-toast' import { Button } from '@/components/ui/button' -import { SshHostNodeMirrorAction } from '@/components/settings/ssh/server-actions/SshHostNodeMirrorAction' -import { SshHostMoveAnywayButton } from '@/components/settings/ssh/server-actions/SshHostMoveAnywayAction' +import { SshHostNodeMirrorAction } from '@/components/ssh-host-recovery/SshHostNodeMirrorAction' +import { SshHostMoveAnywayButton } from '@/components/ssh-host-recovery/SshHostMoveAnywayAction' import { OpenPlainSshTerminalButton } from '@/components/plain-ssh-terminal/OpenPlainSshTerminalButton' import { useAppStore } from '@/store/index' import type { SshConnectionStatus } from '../../../../../../../shared/ssh-types' diff --git a/src/renderer/src/components/settings/browser/use/browser-use-setup-state.ts b/src/renderer/src/lib/browser/browser-use-setup-state.ts similarity index 100% rename from src/renderer/src/components/settings/browser/use/browser-use-setup-state.ts rename to src/renderer/src/lib/browser/browser-use-setup-state.ts diff --git a/src/renderer/src/runtime/hosts/installation/managed-orcad-api-availability.ts b/src/renderer/src/runtime/hosts/installation/managed-orcad-api-availability.ts new file mode 100644 index 00000000000..e211613cd01 --- /dev/null +++ b/src/renderer/src/runtime/hosts/installation/managed-orcad-api-availability.ts @@ -0,0 +1,7 @@ +import type { ManagedOrcadPreloadApi } from '../../../../../preload/api/managed-orcad-api' +import { isWebClientLocation } from '@/lib/web-client-location' + +/** Web fallback stubs cannot manage SSH hosts. */ +export function getManagedOrcadApi(): ManagedOrcadPreloadApi | undefined { + return isWebClientLocation() ? undefined : window.api.runtimeEnvironments.managedOrcad +} From 5ff53133b59ed17f34e4ddda7f20a02669c7a530 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:30:54 +0000 Subject: [PATCH 15/80] fix(native-chat): hide the transcript as soon as /clear is sent (#40054) The host stops the old agent (about 0.6 s for Claude, up to 3 s with slow MCP servers) before it writes the divider, so the old transcript lingered. Linear: STA-14361 --- .../mobile-structured-composer-command.ts | 8 + .../native-chat/MobileNativeChatOverlay.tsx | 1 + .../native-chat/MobileNativeChatView.tsx | 10 +- ...le-structured-agent-session.clear.test.tsx | 160 ++++++++++++++++++ .../mobile-native-chat-render-data.ts | 8 +- .../use-mobile-native-chat-session.ts | 2 + .../use-mobile-structured-agent-session.ts | 11 +- ...use-mobile-structured-send-with-outcome.ts | 4 + .../use-mobile-structured-transcript.ts | 52 +++++- .../NativeChatStructuredSession.tsx | 9 +- .../projection/NativeChatContextClearFade.tsx | 18 +- ...e-native-chat-context-clear-floor.test.tsx | 56 +++++- .../use-native-chat-context-clear-floor.ts | 64 +++++-- .../use-structured-agent-session.ts | 1 + .../structured-conversation-command-send.ts | 10 ++ .../agent-session-context-clear.ts | 12 ++ 16 files changed, 379 insertions(+), 47 deletions(-) create mode 100644 mobile/src/session/native-chat/agent-session/use-mobile-structured-agent-session.clear.test.tsx diff --git a/mobile/src/session/mobile-structured-composer-command.ts b/mobile/src/session/mobile-structured-composer-command.ts index b2c75949dff..04d80c271fb 100644 --- a/mobile/src/session/mobile-structured-composer-command.ts +++ b/mobile/src/session/mobile-structured-composer-command.ts @@ -46,6 +46,8 @@ export async function dispatchMobileStructuredCommand(input: { waitsInLine: (command: AgentSessionConversationCommand) => boolean /** `refusedWhile`: what the phone showed the refused command waiting on. */ onError: MobileNativeChatSendErrorReporter + /** Called as a /clear goes out; the returned restore runs unless the host accepts it. */ + onClearSent?: () => () => void timeoutMs: number }): Promise { if ( @@ -98,6 +100,8 @@ export async function dispatchMobileStructuredCommand(input: { } } input.pending.current = true + const restoreClear = command === 'clear' ? input.onClearSent?.() : undefined + let accepted = false try { const result = await requestStructuredAgentSessionMutation({ @@ -133,6 +137,7 @@ export async function dispatchMobileStructuredCommand(input: { startFailures: input.startFailures(), say: sayMobileAgentSessionFailure }) + accepted = reply.accepted // A refusal names its cause only when the phone showed it, as on desktop. return { accepted: reply.accepted, @@ -146,6 +151,9 @@ export async function dispatchMobileStructuredCommand(input: { } } finally { input.pending.current = false + if (!accepted) { + restoreClear?.() + } } } }) diff --git a/mobile/src/session/native-chat/MobileNativeChatOverlay.tsx b/mobile/src/session/native-chat/MobileNativeChatOverlay.tsx index 434cdbfddd3..649307a056f 100644 --- a/mobile/src/session/native-chat/MobileNativeChatOverlay.tsx +++ b/mobile/src/session/native-chat/MobileNativeChatOverlay.tsx @@ -285,6 +285,7 @@ function MobileNativeChatOverlayContent({ status={session.status} error={session.error} readFailedFinally={session.readFailedFinally === true} + clearing={session.clearing === true} agent={controller.nativeChatAgent} sessionLive={controller.nativeChatSessionLive} agentWorking={controller.nativeChatTurnActive} diff --git a/mobile/src/session/native-chat/MobileNativeChatView.tsx b/mobile/src/session/native-chat/MobileNativeChatView.tsx index a98345f4ea4..b3eead5e801 100644 --- a/mobile/src/session/native-chat/MobileNativeChatView.tsx +++ b/mobile/src/session/native-chat/MobileNativeChatView.tsx @@ -77,6 +77,8 @@ type ChatViewProps = MobileNativeChatSendProps & error?: string // Permanent read failure replaces the pane because further chat actions cannot succeed. readFailedFinally?: boolean + /** A sent /clear hid the old rows: show a blank list until its divider lands, not the new-chat copy. */ + clearing?: boolean agent?: string | null sessionLive?: boolean /** The turn is live: working, or waiting on the reader. */ @@ -138,6 +140,7 @@ export function MobileNativeChatView({ status, error, readFailedFinally = false, + clearing = false, agent, sessionLive, agentWorking, @@ -235,7 +238,6 @@ export function MobileNativeChatView({ : (ask ?? permission ?? question ?? collapsedPrompt ?? awaitingInput) != null, send: onSend }) - const { lockReason } = offlineQueue const handleSend = useCallback( async (...send: Parameters): Promise => { // Retire the previous notice before this action can report a new one. @@ -313,10 +315,10 @@ export function MobileNativeChatView({ // Changing text scale relays out every row, so preserve the page’s reading anchor. const layoutKey = `${fontScale}` const footer = mobileNativeChatListFooter(liveStatus, turns.waitingRows, renderItem) - const emptyState = mobileNativeChatEmptyState(status, agent ?? null, error, sessionLive) + const emptyState = mobileNativeChatEmptyState(status, agent, error, sessionLive, clearing) const composerPlaceholder = mobileNativeChatComposerPlaceholder( - lockReason, + offlineQueue.lockReason, turnIndicator?.afterStop ) const promptOwnsInput = useMobileNativeChatPromptInputOwner( @@ -346,7 +348,7 @@ export function MobileNativeChatView({ collapsedPrompt={collapsedPrompt} /> diff --git a/mobile/src/session/native-chat/agent-session/use-mobile-structured-agent-session.clear.test.tsx b/mobile/src/session/native-chat/agent-session/use-mobile-structured-agent-session.clear.test.tsx new file mode 100644 index 00000000000..ba720a54a09 --- /dev/null +++ b/mobile/src/session/native-chat/agent-session/use-mobile-structured-agent-session.clear.test.tsx @@ -0,0 +1,160 @@ +import { createElement } from 'react' +import { act, type ReactTestRenderer } from 'react-test-renderer' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { AgentJournalRenderItem } from '../../../../../src/shared/agent-session/journal/agent-session-journal-types' +import type { AgentSessionSubscribeEvent } from '../../../../../src/shared/agent-session/agent-session-wire' +import { createWithQueryClient } from '../../../test-support/query-client-test-renderer' +import type { RpcClient } from '../../../transport/rpc/rpc-client-contract' +import type { RpcResponse } from '../../../transport/rpc/rpc-wire-types' +import { useMobileStructuredAgentSession } from '../../use-mobile-structured-agent-session' +import { + SESSION_ID, + mutationOk, + ok, + snapshotEvent +} from './use-mobile-structured-agent-session-queued.test-fixture' + +vi.mock('@react-native-async-storage/async-storage', () => ({ + default: { getItem: vi.fn(async () => null), setItem: vi.fn(), removeItem: vi.fn() } +})) + +const row = ( + sequence: number, + role: 'user' | 'assistant', + text: string +): AgentJournalRenderItem => ({ + itemId: `row-${sequence}`, + sequence, + revision: 1, + observedAt: sequence, + body: { kind: 'message', role, blocks: [{ type: 'text', text }] } +}) +const divider = (sequence: number): AgentJournalRenderItem => ({ + ...row(sequence, 'assistant', ''), + body: { + kind: 'status', + text: 'Context cleared', + presentation: 'context-cleared', + contextClear: { operationId: `clear-${sequence}`, afterFence: 3, clearedAt: sequence } + } +}) +const dividerLands = (sequence: number): AgentSessionSubscribeEvent => ({ + type: 'batch', + sessionId: SESSION_ID, + batch: { + cursor: { epoch: 'epoch-1', sequence }, + items: [divider(sequence)], + removedItemIds: [], + submissions: [] + } +}) + +describe('useMobileStructuredAgentSession /clear', () => { + let renderer: ReactTestRenderer | null = null + afterEach(() => { + act(() => renderer?.unmount()) + renderer = null + }) + + async function mount() { + let listener: ((value: unknown) => void) | null = null + let reply: ((response: RpcResponse) => void) | null = null + const client: Pick = { + getState: () => 'connected', + onStateChange: () => () => {}, + sendRequest: vi.fn(async (method: string): Promise => { + if (method === 'agentSession.options') { + return ok({ models: [], current: {}, conversationCommands: ['clear'] }) + } + if (method === 'agentSession.conversationCommand') { + return new Promise((resolve) => { + reply = resolve + }) + } + return ok({}) + }), + subscribe: vi.fn((method: string, _params: unknown, onData: (value: unknown) => void) => { + if (method === 'agentSession.subscribe') { + listener = onData + } + return () => {} + }) + } + const rendered: { hook: ReturnType | null } = { + hook: null + } + function Harness(): null { + rendered.hook = useMobileStructuredAgentSession({ + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the hook's request, subscription and connection members are provided. + client: client as RpcClient, + pairedHostClient: null, + sessionId: SESSION_ID, + enabled: true, + connected: true, + hostSupport: null, + agent: 'claude', + onSendError: vi.fn() + }) + return null + } + act(() => { + renderer = createWithQueryClient(createElement(Harness)) + }) + await vi.waitFor(() => expect(listener).toEqual(expect.any(Function))) + act(() => + listener?.( + snapshotEvent({ items: [row(1, 'user', 'question'), row(2, 'assistant', 'reply')] }) + ) + ) + await vi.waitFor(() => expect(rendered.hook?.conversationCommands).toEqual(['clear'])) + const shown = () => ({ + text: rendered.hook!.session.messages.flatMap((message) => + message.blocks.flatMap((block) => (block.type === 'text' ? [block.text] : [])) + ), + clearing: rendered.hook!.session.clearing + }) + const sendClear = async () => { + let outcome: Promise | undefined + act(() => { + outcome = rendered.hook!.sendWithOutcome('/clear') + }) + await vi.waitFor(() => expect(reply).toEqual(expect.any(Function))) + // Wrapped so awaiting the press does not wait for the reply. + return { outcome: outcome! } + } + return { + shown, + sendClear, + push: (event: AgentSessionSubscribeEvent) => act(() => listener?.(event)), + reply: async (value: unknown) => { + await act(async () => reply?.(mutationOk(value))) + } + } + } + + it('hides the old transcript from the press until the divider lands', async () => { + const chat = await mount() + expect(chat.shown()).toEqual({ text: ['question', 'reply'], clearing: false }) + + const { outcome } = await chat.sendClear() + expect(chat.shown()).toEqual({ text: [], clearing: true }) + + // The host's reply can reach the phone before the divider it already wrote. + await chat.reply({ command: 'clear', state: 'completed' }) + await expect(outcome).resolves.toBe('accepted') + expect(chat.shown()).toEqual({ text: [], clearing: true }) + + chat.push(dividerLands(3)) + expect(chat.shown()).toEqual({ text: ['Context cleared'], clearing: false }) + }) + + it('shows the old transcript again when the host refuses the /clear', async () => { + const chat = await mount() + const { outcome } = await chat.sendClear() + expect(chat.shown()).toEqual({ text: [], clearing: true }) + + await chat.reply({ command: 'clear', state: 'completed', error: 'Could not clear.' }) + await expect(outcome).resolves.toBe('rejected') + expect(chat.shown()).toEqual({ text: ['question', 'reply'], clearing: false }) + }) +}) diff --git a/mobile/src/session/native-chat/mobile-native-chat-render-data.ts b/mobile/src/session/native-chat/mobile-native-chat-render-data.ts index d8f5893538b..9065b441ab8 100644 --- a/mobile/src/session/native-chat/mobile-native-chat-render-data.ts +++ b/mobile/src/session/native-chat/mobile-native-chat-render-data.ts @@ -62,9 +62,11 @@ function mobileNativeChatEmptyStateCopy( export function mobileNativeChatEmptyState( status: MobileNativeChatStatus, - agent: string | null, + agent: string | null | undefined, error?: string, - sessionLive = false + sessionLive = false, + /** A sent /clear hid the old rows: a blank list until its divider lands, not new-chat copy. */ + clearing = false ): NativeChatEmptyStateCopy | null { const agentLabel = agent ? formatAgentTypeLabel(agent) @@ -77,7 +79,7 @@ export function mobileNativeChatEmptyState( case 'awaiting-transcript': return mobileNativeChatEmptyStateCopy(sessionLive ? 'empty' : 'notFound', agentLabel) case 'ready': - return mobileNativeChatEmptyStateCopy('empty', agentLabel) + return clearing ? null : mobileNativeChatEmptyStateCopy('empty', agentLabel) case 'error': { const copy = mobileNativeChatEmptyStateCopy('error', agentLabel) return error ? { ...copy, subtitle: describeLoadError(error, translate).message } : copy diff --git a/mobile/src/session/native-chat/use-mobile-native-chat-session.ts b/mobile/src/session/native-chat/use-mobile-native-chat-session.ts index 93f6956dc93..c1952510c32 100644 --- a/mobile/src/session/native-chat/use-mobile-native-chat-session.ts +++ b/mobile/src/session/native-chat/use-mobile-native-chat-session.ts @@ -39,6 +39,8 @@ export type MobileNativeChatSession = { /** The read failed for good (damage, or a newer Orca's chat): the error says why, once, and * there is nothing to send into. */ readFailedFinally?: boolean + /** This phone's /clear is out and its divider has not landed: the old rows are hidden. */ + clearing?: boolean hasMore: boolean /** `olderPages === 'loading'`, kept because recorded RPC fixtures read it. */ loadingEarlier: boolean diff --git a/mobile/src/session/use-mobile-structured-agent-session.ts b/mobile/src/session/use-mobile-structured-agent-session.ts index d0011546821..f15182b48eb 100644 --- a/mobile/src/session/use-mobile-structured-agent-session.ts +++ b/mobile/src/session/use-mobile-structured-agent-session.ts @@ -128,11 +128,8 @@ export function useMobileStructuredAgentSession( onSendError, confirmRecorded: useMobileStructuredSendRecorded(client, sessionId, hostSupport) }) - const { transcriptItems, messages, receipts } = useMobileStructuredTranscript( - agent, - state, - sends.optimistic - ) + const transcript = useMobileStructuredTranscript(agent, state, sends.optimistic, sessionKey) + const { clearing, transcriptItems, messages, receipts } = transcript const turnTiming = useMobileStructuredAgentTurnTiming( { ...state, items: transcriptItems }, turnId @@ -190,6 +187,7 @@ export function useMobileStructuredAgentSession( stopping: turnIndicator.stopping, ...sends, commandPending: commandPendingRef, + onClearSent: transcript.onClearSent, sendsOut, controller: goal.commands, sessionCommands: args.sessionCommands, @@ -273,7 +271,8 @@ export function useMobileStructuredAgentSession( transcriptLoading: status === 'loading', error: state.error, readFailedFinally: status === 'error' && isFinalAgentSessionReadRefusal(state.readRefusal), - hasMore: state.hasOlder && olderPages !== 'unavailable', + clearing, + hasMore: state.hasOlder && olderPages !== 'unavailable' && !clearing, loadingEarlier: olderPages === 'loading', olderPages, loadEarlier diff --git a/mobile/src/session/use-mobile-structured-send-with-outcome.ts b/mobile/src/session/use-mobile-structured-send-with-outcome.ts index 0f3273fc4e6..be4dd2ea649 100644 --- a/mobile/src/session/use-mobile-structured-send-with-outcome.ts +++ b/mobile/src/session/use-mobile-structured-send-with-outcome.ts @@ -63,6 +63,7 @@ export function useMobileStructuredSendWithOutcome(args: { reject: MobileStructuredSends['reject'] holdUnconfirmed: MobileStructuredSends['holdUnconfirmed'] commandPending: { current: boolean } + onClearSent: () => () => void /** This chat's message sends still out, which a command waits behind as on desktop. */ sendsOut: MobileStructuredSendsOut controller: Pick< @@ -99,6 +100,7 @@ export function useMobileStructuredSendWithOutcome(args: { commandsWait, controller, enabled, + onClearSent, reject, holdUnconfirmed, onSendError, @@ -193,6 +195,7 @@ export function useMobileStructuredSendWithOutcome(args: { selectAgentSessionCurrentRequests(stateRef.current) ), onError: onSendError, + onClearSent, timeoutMs }) if (commandOutcome !== null) { @@ -275,6 +278,7 @@ export function useMobileStructuredSendWithOutcome(args: { commandsWait, controller, enabled, + onClearSent, reject, holdUnconfirmed, onSendError, diff --git a/mobile/src/session/use-mobile-structured-transcript.ts b/mobile/src/session/use-mobile-structured-transcript.ts index 992e9a9495f..f4fa032ca3c 100644 --- a/mobile/src/session/use-mobile-structured-transcript.ts +++ b/mobile/src/session/use-mobile-structured-transcript.ts @@ -1,5 +1,9 @@ -import { useMemo } from 'react' -import { agentSessionTranscriptRows } from '../../../src/shared/agent-session/agent-session-context-clear' +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' +import { + agentSessionContextClearOutstanding, + agentSessionTranscriptRows, + latestAgentSessionContextClearSequence +} from '../../../src/shared/agent-session/agent-session-context-clear' import type { StructuredAgentSessionState } from '../../../src/shared/agent-session/structured-agent-session-reducer' import { projectStructuredAgentSessionMessages } from '../../../src/shared/agent-session/view/structured-agent-session-message-projection' import { tuiAgentDisplayName } from '../../../src/shared/agent-launch/catalog/tui-agent-display-names' @@ -12,18 +16,50 @@ const mobileStructuredTranscript = createStructuredQuestionProjection( projectStructuredAgentStartItem ) -/** The chat's transcript rows, its messages with this phone's sends in flight, and the prompts they resolved. */ +/** The chat's transcript rows, its messages with this phone's sends in flight, and the prompts they resolved. + * From this phone's /clear press until its divider lands, the rows drawn at the press stay hidden: + * the host stops the old agent before it writes the divider. */ export function useMobileStructuredTranscript( agent: string | null, - state: Pick, - optimistic: Parameters[1] + state: Pick, + optimistic: Parameters[1], + sessionKey: string ) { + const [clearSent, setClearSent] = useState<{ + sessionKey: string + epoch: string | null + tail: number + } | null>(null) + // The newest row the last commit drew: the rows a press hides. + const tail = state.items.at(-1)?.sequence ?? 0 + const drawnTail = useRef(tail) + useEffect(() => { + drawnTail.current = tail + }, [tail]) + const onClearSent = useCallback(() => { + const sent = { sessionKey, epoch: state.epoch, tail: drawnTail.current } + setClearSent(sent) + return () => setClearSent((current) => (current === sent ? null : current)) + }, [sessionKey, state.epoch]) + // A rewind's new epoch restarts sequences, so the press's rows are not in it. + const clearSentTail = + clearSent?.sessionKey === sessionKey && clearSent.epoch === state.epoch ? clearSent.tail : null + const latestClear = useMemo( + () => latestAgentSessionContextClearSequence(state.items), + [state.items] + ) + const clearing = agentSessionContextClearOutstanding(state.items, latestClear, clearSentTail) + // Retire a press that hides nothing (its divider landed, or its rows left), so a later trim cannot re-arm it. + if (clearSent?.sessionKey === sessionKey && !clearing) { + setClearSent(null) + } + const floor = clearing ? clearSentTail + 1 : latestClear const transcriptItems = useMemo( () => - withNativeChatCutTurnNotices(agentSessionTranscriptRows(state.items), { + withNativeChatCutTurnNotices(agentSessionTranscriptRows(state.items, floor), { agentName: agent ? (tuiAgentDisplayName(agent) ?? agent) : undefined }), - [agent, state.items] + [agent, floor, state.items] ) const messages = useMemo( () => @@ -39,6 +75,8 @@ export function useMobileStructuredTranscript( [transcriptItems, optimistic, state.submissions] ) return { + clearing, + onClearSent, transcriptItems, messages, receipts: mobileStructuredTranscript.transcript(transcriptItems).receipts diff --git a/src/renderer/src/components/native-chat/NativeChatStructuredSession.tsx b/src/renderer/src/components/native-chat/NativeChatStructuredSession.tsx index 77fb10fbf0c..3a2b4dc1196 100644 --- a/src/renderer/src/components/native-chat/NativeChatStructuredSession.tsx +++ b/src/renderer/src/components/native-chat/NativeChatStructuredSession.tsx @@ -273,8 +273,13 @@ export function NativeChatStructuredSession(props: Omit} > - - {find.isOpen ? : null} + : null + } + > {viewState.kind === 'loading' || (viewState.kind === 'error' && !readFailure) ? ( historyPhase !== 'unread' && ) : viewState.kind === 'error' ? ( diff --git a/src/renderer/src/components/native-chat/conversation/projection/NativeChatContextClearFade.tsx b/src/renderer/src/components/native-chat/conversation/projection/NativeChatContextClearFade.tsx index 57cf67d3775..a23d8c23a23 100644 --- a/src/renderer/src/components/native-chat/conversation/projection/NativeChatContextClearFade.tsx +++ b/src/renderer/src/components/native-chat/conversation/projection/NativeChatContextClearFade.tsx @@ -3,17 +3,25 @@ import type { ReactNode } from 'react' /** Swap the transcript while hidden during the live /clear fade, then reveal it again. */ export function NativeChatContextClearFade({ clearing, + overlay, children }: { clearing: boolean + /** Stays visible and usable over the hidden transcript, e.g. the find bar. */ + overlay?: ReactNode children: ReactNode }): React.JSX.Element { return ( -
- {children} +
+ {overlay} +
) } diff --git a/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.test.tsx b/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.test.tsx index f9c1a56020a..ac46e8db302 100644 --- a/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.test.tsx +++ b/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.test.tsx @@ -26,13 +26,22 @@ const clear = (sequence: number): AgentJournalRenderItem => ({ }) function render(epoch: string | null, items: AgentJournalRenderItem[]) { - return renderHook( + let restore = (): void => {} + const hook = renderHook( ({ epoch, items }) => { - const { rows, clearing, clears } = useNativeChatContextClearFloor(epoch, items) - return { shown: rows.map((row) => row.sequence), clearing, clears } + const { rows, clearing, clears, onClearSent } = useNativeChatContextClearFloor(epoch, items) + return { view: { shown: rows.map((row) => row.sequence), clearing, clears }, onClearSent } }, { initialProps: { epoch, items } } ) + return { + rerender: hook.rerender, + get view() { + return hook.result.current.view + }, + sendClear: () => act(() => void (restore = hook.result.current.onClearSent())), + refuseClear: () => act(() => restore()) + } } describe('useNativeChatContextClearFloor', () => { @@ -41,24 +50,55 @@ describe('useNativeChatContextClearFloor', () => { it('fades a live /clear out before the transcript moves to its divider', () => { const hook = render('epoch-a', [message(1), message(2)]) - expect(hook.result.current).toEqual({ shown: [1, 2], clearing: false, clears: 0 }) + expect(hook.view).toEqual({ shown: [1, 2], clearing: false, clears: 0 }) hook.rerender({ epoch: 'epoch-a', items: [message(1), message(2), clear(3)] }) - expect(hook.result.current).toEqual({ shown: [1, 2, 3], clearing: true, clears: 0 }) + expect(hook.view).toEqual({ shown: [1, 2, 3], clearing: true, clears: 0 }) act(() => vi.advanceTimersByTime(NATIVE_CHAT_CONTEXT_CLEAR_FADE_MS)) - expect(hook.result.current).toEqual({ shown: [3], clearing: false, clears: 1 }) + expect(hook.view).toEqual({ shown: [3], clearing: false, clears: 1 }) + }) + + it('hides the transcript from the moment a /clear is sent until its divider lands', () => { + const hook = render('epoch-a', [message(1), message(2)]) + hook.sendClear() + expect(hook.view).toEqual({ shown: [1, 2], clearing: true, clears: 0 }) + + // Already faded out by the time the divider lands, so it swaps without a second fade. + act(() => vi.advanceTimersByTime(NATIVE_CHAT_CONTEXT_CLEAR_FADE_MS)) + hook.rerender({ epoch: 'epoch-a', items: [message(1), message(2), clear(3)] }) + act(() => vi.advanceTimersByTime(0)) + expect(hook.view).toEqual({ shown: [3], clearing: false, clears: 1 }) + + // The answered press hides nothing once later history trims its divider out. + hook.rerender({ epoch: 'epoch-a', items: [message(4), message(5)] }) + expect(hook.view.clearing).toBe(false) + }) + + it('shows the transcript again when a sent /clear is refused', () => { + const hook = render('epoch-a', [message(1), message(2)]) + hook.sendClear() + expect(hook.view.clearing).toBe(true) + hook.refuseClear() + expect(hook.view).toEqual({ shown: [1, 2], clearing: false, clears: 0 }) + }) + + it('stops hiding once a snapshot past the divider holds none of the rows the press hid', () => { + const hook = render('epoch-a', [message(1), message(2)]) + hook.sendClear() + hook.rerender({ epoch: 'epoch-a', items: [message(9), message(10)] }) + expect(hook.view).toEqual({ shown: [9, 10], clearing: false, clears: 0 }) }) it('moves to a divider paged in from history without fading or replacing the list', () => { const hook = render('epoch-a', [message(40), message(41)]) hook.rerender({ epoch: 'epoch-a', items: [message(10), clear(11), message(40), message(41)] }) - expect(hook.result.current).toEqual({ shown: [11, 40, 41], clearing: false, clears: 0 }) + expect(hook.view).toEqual({ shown: [11, 40, 41], clearing: false, clears: 0 }) }) it('takes the divider a first read or a rewind loads without fading', () => { const hook = render(null, []) hook.rerender({ epoch: 'epoch-a', items: [message(1), clear(2), message(3)] }) - expect(hook.result.current).toEqual({ shown: [2, 3], clearing: false, clears: 0 }) + expect(hook.view).toEqual({ shown: [2, 3], clearing: false, clears: 0 }) }) }) diff --git a/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.ts b/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.ts index 6e883a84420..f4a0ee3e3e4 100644 --- a/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.ts +++ b/src/renderer/src/components/native-chat/conversation/projection/use-native-chat-context-clear-floor.ts @@ -1,6 +1,7 @@ -import { useEffect, useMemo, useRef, useState } from 'react' +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import type { AgentJournalRenderItem } from '../../../../../../shared/agent-session/journal/agent-session-journal-types' import { + agentSessionContextClearOutstanding, agentSessionTranscriptRows, latestAgentSessionContextClearSequence } from '../../../../../../shared/agent-session/agent-session-context-clear' @@ -14,21 +15,34 @@ type ClearFloorState = { floor: number /** A clear that landed live, waiting for the old transcript to fade out. */ fading: number | null + /** When the fade that `fading` waits on began, if this view's press began it. */ + fadedSince: number | null /** Bumped per live clear; keys the list so nothing the cleared transcript held carries over. */ clears: number + /** This view's /clear awaiting its divider: its press time and the newest row drawn then. */ + sent: { at: number; tail: number } | null } -/** Show history after the latest clear; fade live clears before swapping, but preserve scroll for historical dividers. */ +/** Show history after the latest clear; fade live clears before swapping, but preserve scroll for historical dividers. + * A sent /clear fades the transcript at once: the host stops the old agent before its divider lands. */ export function useNativeChatContextClearFloor( epoch: string | null, items: readonly AgentJournalRenderItem[] -): { rows: readonly AgentJournalRenderItem[]; clearing: boolean; clears: number } { +): { + rows: readonly AgentJournalRenderItem[] + clearing: boolean + clears: number + /** Call as this view's /clear goes out; the returned restore shows the transcript again if refused. */ + onClearSent: () => () => void +} { const latest = useMemo(() => latestAgentSessionContextClearSequence(items), [items]) const [state, setState] = useState(() => ({ epoch, floor: latest, fading: null, - clears: 0 + fadedSince: null, + clears: 0, + sent: null })) // The newest row the last commit drew: a divider past it arrived live, not from an older page. const tail = items.at(-1)?.sequence ?? 0 @@ -36,36 +50,62 @@ export function useNativeChatContextClearFloor( useEffect(() => { drawnTail.current = tail }, [tail]) + const onClearSent = useCallback(() => { + const sent = { at: Date.now(), tail: drawnTail.current } + setState((prior) => ({ ...prior, sent })) + return () => setState((prior) => (prior.sent === sent ? { ...prior, sent: null } : prior)) + }, []) let current = state if (epoch !== state.epoch) { - // A new journal (first read or a rewind) remounts the list on its own; nothing to fade. - current = { ...state, epoch, floor: latest, fading: null } + // A new journal (first read or a rewind) remounts the list on its own; nothing to fade or wait on. + current = { ...state, epoch, floor: latest, fading: null, fadedSince: null, sent: null } setState(current) } else if (latest > (state.fading ?? state.floor)) { + // A divider past the press answers it, so the press stops hiding anything. + const { sent } = state + const answered = sent !== null && latest > sent.tail + const waiting = answered ? null : sent current = - latest > drawnTail.current ? { ...state, fading: latest } : { ...state, floor: latest } + latest > drawnTail.current + ? { ...state, fading: latest, fadedSince: answered ? sent.at : null, sent: waiting } + : { ...state, floor: latest, sent: waiting } + setState(current) + } else if ( + state.sent !== null && + !agentSessionContextClearOutstanding(items, latest, state.sent.tail) + ) { + // No row the press hid is loaded any more (a snapshot past its divider). + current = { ...state, sent: null } setState(current) } - const fading = current.fading + const { fading, fadedSince } = current + // Held hidden until a divider newer than the press lands; a refused clear fades back in. + const awaitingDivider = agentSessionContextClearOutstanding( + items, + latest, + current.sent?.tail ?? null + ) useEffect(() => { if (fading === null) { return } + // A press that already faded the transcript out swaps as soon as its divider lands. + const elapsed = fadedSince === null ? 0 : Date.now() - fadedSince const timer = setTimeout( () => setState((prior) => prior.fading === fading - ? { ...prior, floor: fading, fading: null, clears: prior.clears + 1 } + ? { ...prior, floor: fading, fading: null, fadedSince: null, clears: prior.clears + 1 } : prior ), - NATIVE_CHAT_CONTEXT_CLEAR_FADE_MS + Math.max(0, NATIVE_CHAT_CONTEXT_CLEAR_FADE_MS - elapsed) ) return () => clearTimeout(timer) - }, [fading]) + }, [fadedSince, fading]) const floor = current.floor const rows = useMemo(() => agentSessionTranscriptRows(items, floor), [floor, items]) - return { rows, clearing: fading !== null, clears: current.clears } + return { rows, clearing: fading !== null || awaitingDivider, clears: current.clears, onClearSent } } diff --git a/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts b/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts index 352e8cf64b9..6ab811b914e 100644 --- a/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts +++ b/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts @@ -248,6 +248,7 @@ export function useStructuredAgentSession(args: StructuredAgentSessionViewArgs) rewindInFlight: rewind.blockedRef, sends: pending, items: () => stateRef.current.items, + onClearSent: contextClear.onClearSent, send: commandWrite }), journalItems: transcriptItems, diff --git a/src/renderer/src/components/native-chat/session/structured/writes/structured-conversation-command-send.ts b/src/renderer/src/components/native-chat/session/structured/writes/structured-conversation-command-send.ts index 1fd31b30b80..32b424b24f6 100644 --- a/src/renderer/src/components/native-chat/session/structured/writes/structured-conversation-command-send.ts +++ b/src/renderer/src/components/native-chat/session/structured/writes/structured-conversation-command-send.ts @@ -40,6 +40,8 @@ export async function sendStructuredConversationCommand(input: { causes: StructuredConversationCommandCauses /** What the chat's loaded start-failure rows state, read when the reply lands. */ startFailures: () => readonly AgentSessionFailureFact[] + /** Called as a /clear goes out; the returned restore runs unless the host accepts it. */ + onClearSent?: () => () => void send: ( command: AgentSessionConversationCommand ) => Promise> @@ -70,6 +72,8 @@ export async function sendStructuredConversationCommand(input: { ) } input.pending.current = true + const restoreClear = input.command === 'clear' ? input.onClearSent?.() : undefined + let accepted = false try { const outcome = await input.send(input.command) if (outcome.kind === 'not-done') { @@ -84,9 +88,13 @@ export async function sendStructuredConversationCommand(input: { startFailures: input.startFailures(), say: sayAgentSessionFailureTranslated }) + accepted = reply.accepted return reply.accepted ? { accepted: true, error: null } : refused(reply.error, reply.cause) } finally { input.pending.current = false + if (!accepted) { + restoreClear?.() + } } } @@ -109,6 +117,7 @@ export function structuredConversationCommandRunner(args: { sends: readonly StructuredAgentSessionPendingSend[] /** The loaded rows, read when the reply lands, for the start failures they state. */ items: () => readonly AgentJournalRenderItem[] + onClearSent?: () => () => void send: ( command: AgentSessionConversationCommand, delivery?: 'queue-if-active', @@ -152,6 +161,7 @@ export function structuredConversationCommandRunner(args: { }), causes, startFailures: () => agentSessionVisibleFailureFacts(args.items()), + onClearSent: args.onClearSent, send: (command) => args.send( command, diff --git a/src/shared/agent-session/agent-session-context-clear.ts b/src/shared/agent-session/agent-session-context-clear.ts index 1a2ad24ddcb..4794f7ac51c 100644 --- a/src/shared/agent-session/agent-session-context-clear.ts +++ b/src/shared/agent-session/agent-session-context-clear.ts @@ -49,6 +49,18 @@ export function latestAgentSessionContextClearSequence( return sequence } +/** A /clear sent while `sentTail` was the newest row drawn hides the rows up to it until a divider past it lands. + * Once none of those rows is loaded (a tail snapshot past the divider), nothing is left to hide. */ +export function agentSessionContextClearOutstanding( + items: readonly Pick[], + latestClear: number, + sentTail: number | null +): sentTail is number { + return ( + sentTail !== null && latestClear <= sentTail && (items[0]?.sequence ?? Infinity) <= sentTail + ) +} + /** Show rows from the /clear divider onward; retain earlier journal rows for workers and archives. */ export function agentSessionTranscriptRows< T extends Pick From 5ad35ad5e5df9b072c0b04ac24cfad0c1a7ce27c Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:37:53 -0800 Subject: [PATCH 16/80] perf(terminal): index sleeping-record and launch-agent tabs once per parking pass (#40009) goneSessionsKeepTabMounted scanned every sleeping record per eligible tab, so a workspace switch with thousands of tabs did tabs x records work on the renderer thread. Build both owner sets once per source identity instead. --- .../parked-tab-agent-owner-index.ts | 44 +++++++++++++++++++ .../terminal-parked-tab-watchers.ts | 7 ++- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/renderer/src/components/terminal-pane/parking/output-watchers/parked-tab-agent-owner-index.ts diff --git a/src/renderer/src/components/terminal-pane/parking/output-watchers/parked-tab-agent-owner-index.ts b/src/renderer/src/components/terminal-pane/parking/output-watchers/parked-tab-agent-owner-index.ts new file mode 100644 index 00000000000..57f9f976d00 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/parking/output-watchers/parked-tab-agent-owner-index.ts @@ -0,0 +1,44 @@ +/** Agent-owner tab sets for gone-session parking, built once per source identity so a pass over every tab stays linear. */ +import type { TerminalTab } from '../../../../../../shared/workspace-layout/terminal-tab-types' +import type { SleepingAgentSessionRecord } from '../../../../../../shared/agent-session/lifecycle/agent-session-resume' + +const launchAgentTabIdsByTabs = new WeakMap>() +const NO_TAB_IDS: ReadonlySet = new Set() + +export function launchAgentTabIds(tabs: readonly TerminalTab[] | undefined): ReadonlySet { + if (!tabs) { + return NO_TAB_IDS + } + let tabIds = launchAgentTabIdsByTabs.get(tabs) + if (!tabIds) { + tabIds = new Set(tabs.flatMap((tab) => (tab.launchAgent ? [tab.id] : []))) + launchAgentTabIdsByTabs.set(tabs, tabIds) + } + return tabIds +} + +let sleepingRecordTabIdsCache: { + records: Record + tabIds: ReadonlySet +} | null = null + +/** Tabs a sleeping record names by tabId or by a `${tabId}:` pane-key prefix. */ +export function sleepingRecordTabIds( + records: Record +): ReadonlySet { + if (sleepingRecordTabIdsCache?.records === records) { + return sleepingRecordTabIdsCache.tabIds + } + const tabIds = new Set() + for (const paneKey in records) { + const recordTabId = records[paneKey]?.tabId + if (recordTabId !== undefined) { + tabIds.add(recordTabId) + } + for (let colon = paneKey.indexOf(':'); colon !== -1; colon = paneKey.indexOf(':', colon + 1)) { + tabIds.add(paneKey.slice(0, colon)) + } + } + sleepingRecordTabIdsCache = { records, tabIds } + return tabIds +} diff --git a/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-tab-watchers.ts b/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-tab-watchers.ts index 5703e61e4f2..2c6c05ac509 100644 --- a/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-tab-watchers.ts +++ b/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-tab-watchers.ts @@ -16,6 +16,7 @@ import { resolveParkedTerminalPaneCandidates, type ParkableTerminalTabModel } from './terminal-parked-watcher-reconciliation' +import { launchAgentTabIds, sleepingRecordTabIds } from './parked-tab-agent-owner-index' import { collapseParkedExitedLeaf, startParkedPtyWatcher } from './terminal-parked-pty-watcher' import { capturedPanesByTabId, @@ -84,10 +85,8 @@ function goneSessionsKeepTabMounted( } return ( goneCount < panes.length || - state.tabsByWorktree[worktreeId]?.some((t) => t.id === tab.id && t.launchAgent) === true || - Object.entries(state.sleepingAgentSessionsByPaneKey).some( - ([paneKey, record]) => record.tabId === tab.id || paneKey.startsWith(`${tab.id}:`) - ) + launchAgentTabIds(state.tabsByWorktree[worktreeId]).has(tab.id) || + sleepingRecordTabIds(state.sleepingAgentSessionsByPaneKey).has(tab.id) ) } From 8bf5e95249047c60144d56d84d69551f33424ec8 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:38:25 -0800 Subject: [PATCH 17/80] perf(terminal): group park-exempt sleeping records once per map; index captured panes by leaf (#40014) * perf(terminal): group park-exempt sleeping records once per map; index captured panes by leaf Each mounted worktree's exemption selector rescanned every sleeping record after a record-map change (worktrees x records), and pane reconciliation compared every captured pane against every fallback pane (panes^2 per tab). * perf(terminal): skip the pane index on empty fallbacks; reuse the accumulating leaf traversal --- .../layout/terminal-layout-leaf-traversal.ts | 12 +--- .../terminal-parked-watcher-reconciliation.ts | 36 ++++++++-- .../sleeping-record-park-exemption.ts | 67 ++++++++++++------- 3 files changed, 73 insertions(+), 42 deletions(-) diff --git a/src/renderer/src/components/terminal-pane/layout/terminal-layout-leaf-traversal.ts b/src/renderer/src/components/terminal-pane/layout/terminal-layout-leaf-traversal.ts index 7613e116e64..dd41bd6e2de 100644 --- a/src/renderer/src/components/terminal-pane/layout/terminal-layout-leaf-traversal.ts +++ b/src/renderer/src/components/terminal-pane/layout/terminal-layout-leaf-traversal.ts @@ -1,14 +1,8 @@ import type { TerminalPaneLayoutNode } from '../../../../../shared/workspace-layout/terminal-tab-types' +import { collectLayoutLeafIdsInOrder } from '../../../../../shared/workspace-layout/terminal-pane-tree' -export function collectLeafIdsInOrder(node: TerminalPaneLayoutNode | null | undefined): string[] { - if (!node) { - return [] - } - if (node.type === 'leaf') { - return [node.leafId] - } - return [...collectLeafIdsInOrder(node.first), ...collectLeafIdsInOrder(node.second)] -} +// Accumulate instead of spreading per split, which copies quadratically on skewed layouts. +export const collectLeafIdsInOrder = collectLayoutLeafIdsInOrder export function getLeftmostLeafId(node: TerminalPaneLayoutNode): string { return node.type === 'leaf' ? node.leafId : getLeftmostLeafId(node.first) diff --git a/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts b/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts index 5ffb2b35cff..5b761dcff8e 100644 --- a/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts +++ b/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts @@ -38,28 +38,50 @@ export function fallbackParkedPaneCandidates( })) } +function leafPtyKey(leafId: string, ptyId: string | null): string { + return `${leafId}\u0000${ptyId ?? ''}\u0000${ptyId === null ? 0 : 1}` +} + +// Index captured panes so a many-split tab stays linear in its pane count. +function capturedCoversFallback( + captured: readonly ParkedTerminalPaneCapture[], + fallback: readonly ParkedTerminalPaneCapture[] +): boolean { + const capturedLeafPtyKeys = new Set( + captured.map((candidate) => leafPtyKey(candidate.leafId, candidate.ptyId)) + ) + return fallback.every((pane) => capturedLeafPtyKeys.has(leafPtyKey(pane.leafId, pane.ptyId))) +} + export function resolveParkedTerminalPaneCandidates( tab: ParkableTerminalTabModel, state: ParkedPaneFallbackState ): ParkedTerminalPaneCapture[] { const captured = capturedPanesByTabId.get(tab.id) const fallback = fallbackParkedPaneCandidates(tab, state) + if (!captured) { + return fallback + } const capturedIsCurrent = - captured !== undefined && captured.panes.length > 0 && (tab.ptyId === null || captured.panes.some((pane) => pane.ptyId === tab.ptyId)) && (fallback.length === 0 || (captured.panes.length === fallback.length && - fallback.every((pane) => - captured.panes.some( - (candidate) => candidate.leafId === pane.leafId && candidate.ptyId === pane.ptyId - ) - ))) + capturedCoversFallback(captured.panes, fallback))) if (capturedIsCurrent) { return captured.panes } + if (fallback.length === 0) { + return fallback + } + const priorByLeafId = new Map() + for (const candidate of captured.panes) { + if (!priorByLeafId.has(candidate.leafId)) { + priorByLeafId.set(candidate.leafId, candidate) + } + } return fallback.map((pane) => { - const prior = captured?.panes.find((candidate) => candidate.leafId === pane.leafId) + const prior = priorByLeafId.get(pane.leafId) return prior ? { ...pane, diff --git a/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts b/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts index 5039efbd75e..6894156d452 100644 --- a/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts +++ b/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts @@ -12,36 +12,51 @@ type SleepingRecordParkExemptionState = { sleepingAgentSessionsByPaneKey?: Record } -/** Exempt only consumable sleeping records: parked panes cannot cold-restore, but finished notes would pin panes forever. Memoize by record-map identity to avoid rescanning every worktree on unrelated writes; iterate without allocating Object.values. */ +let exemptTabIdsByWorktreeCache: { + records: Record + byWorktreeId: ReadonlyMap> +} | null = null + +/** Group consumable records by worktree once per record map; each mounted worktree then reads its bucket instead of rescanning every record. */ +function exemptTabIdsByWorktree( + records: Record +): ReadonlyMap> { + if (exemptTabIdsByWorktreeCache?.records === records) { + return exemptTabIdsByWorktreeCache.byWorktreeId + } + const byWorktreeId = new Map>() + for (const paneKey in records) { + const record = records[paneKey] + if (!record || activationTreatsNoteAsFinished(record)) { + continue + } + // Why: malformed pane keys must yield no owner instead of a truncated tab id. + const tabId = + record.tabId ?? + parsePaneKey(record.paneKey)?.tabId ?? + parseLegacyNumericPaneKey(record.paneKey)?.tabId + if (tabId) { + let owned = byWorktreeId.get(record.worktreeId) + if (!owned) { + owned = new Set() + byWorktreeId.set(record.worktreeId, owned) + } + owned.add(tabId) + } + } + exemptTabIdsByWorktreeCache = { records, byWorktreeId } + return byWorktreeId +} + +/** Exempt only consumable sleeping records: parked panes cannot cold-restore, but finished notes would pin panes forever. Memoize by record-map identity to avoid rescanning every worktree on unrelated writes. */ export const selectSleepingRecordParkExemptTabIds = createWorktreeRecordSelector< SleepingRecordParkExemptionState, ReadonlySet >({ readSources: (state) => [state.sleepingAgentSessionsByPaneKey], empty: EMPTY_TAB_IDS, - build: ({ sleepingAgentSessionsByPaneKey }, worktreeId) => { - if (!sleepingAgentSessionsByPaneKey) { - return EMPTY_TAB_IDS - } - let owned: Set | null = null - for (const paneKey in sleepingAgentSessionsByPaneKey) { - const record = sleepingAgentSessionsByPaneKey[paneKey] - if (!record || record.worktreeId !== worktreeId) { - continue - } - if (activationTreatsNoteAsFinished(record)) { - continue - } - // Why: malformed pane keys must yield no owner instead of a truncated tab id. - const tabId = - record.tabId ?? - parsePaneKey(record.paneKey)?.tabId ?? - parseLegacyNumericPaneKey(record.paneKey)?.tabId - if (tabId) { - owned ??= new Set() - owned.add(tabId) - } - } - return owned ?? EMPTY_TAB_IDS - } + build: ({ sleepingAgentSessionsByPaneKey }, worktreeId) => + sleepingAgentSessionsByPaneKey + ? (exemptTabIdsByWorktree(sleepingAgentSessionsByPaneKey).get(worktreeId) ?? EMPTY_TAB_IDS) + : EMPTY_TAB_IDS }) From c192f04dba23807d8efaa6393bfc9235f26a1c02 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:44:46 -0800 Subject: [PATCH 18/80] chore(claude): update the Claude Agent SDK to 0.3.318 (#39940) * chore(claude): update the Claude Agent SDK to 0.3.318 Includes the upstream fix for omitted permissionMode (claude-agent-sdk-typescript#519), so the 'use my Claude settings' permission choice honors settings defaultMode. * test(orcad): expect the SDK's joined --permission-prompt-tool=stdio argument Claude Agent SDK 0.3.295+ passes named option values in the same argument as their flag. * refactor(claude): drop the permission flag SDK 0.3.318 no longer reads The SDK leaves an omitted permissionMode to the CLI, so the 'own' choice needs no opt-in option. --- package.json | 2 +- pnpm-lock.yaml | 12 ++++++------ .../launch/claude-structured-launch-resolution.ts | 8 +++----- ...rcad-packaged-structured-chat.integration.test.ts | 3 +-- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 4014ada5610..604f23dd467 100644 --- a/package.json +++ b/package.json @@ -232,7 +232,7 @@ "check:import-boundaries": "bun config/scripts/static-analysis/process-boundaries/check-import-boundaries.mjs" }, "dependencies": { - "@anthropic-ai/claude-agent-sdk": "0.3.284", + "@anthropic-ai/claude-agent-sdk": "0.3.318", "@floating-ui/dom": "1.8.0", "@linear/sdk": "^97.0.0", "@orca/process-host": "workspace:*", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a2e0691a065..228db2cb82d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -227,8 +227,8 @@ importers: .: dependencies: '@anthropic-ai/claude-agent-sdk': - specifier: 0.3.284 - version: 0.3.284(@anthropic-ai/sdk@0.122.0(zod@4.6.7))(@modelcontextprotocol/sdk@1.32.1(supports-color@7.2.0)(zod@4.6.7))(zod@4.6.7) + specifier: 0.3.318 + version: 0.3.318(@anthropic-ai/sdk@0.122.0(zod@4.6.7))(@modelcontextprotocol/sdk@1.32.1(supports-color@7.2.0)(zod@4.6.7))(zod@4.6.7) '@floating-ui/dom': specifier: 1.8.0 version: 1.8.0 @@ -763,9 +763,9 @@ packages: '@antfu/install-pkg@1.1.0': resolution: {integrity: sha512-MGQsmw10ZyI+EJo45CdSER4zEb+p31LpDAFp2Z3gkSd1yqVZGi0Ebx++YTEMonJy4oChEMLsxZ64j8FH6sSqtQ==} - '@anthropic-ai/claude-agent-sdk@0.3.284': - resolution: {integrity: sha512-NSoJwEq6nFSf8dtaacYx37QdGgqApI3eHFUlxclMLYi8irb6ZJwEaUnPnLUCyAyg9W/tMkgZC0GXWLRCc01I0w==} - engines: {node: '>=18.0.0'} + '@anthropic-ai/claude-agent-sdk@0.3.318': + resolution: {integrity: sha512-z5WHCUeW52SKQ3rnvI3zt8X5h2QQ5o3Cgg5xarO5Rimfe67f3pOJt2aOh00S8t/nRzqfs+8nCb9o1EoXe+yccA==} + engines: {node: '>=18.14.0'} peerDependencies: '@anthropic-ai/sdk': '>=0.93.0' '@modelcontextprotocol/sdk': ^1.29.0 @@ -8342,7 +8342,7 @@ snapshots: package-manager-detector: 1.6.0 tinyexec: 1.1.2 - '@anthropic-ai/claude-agent-sdk@0.3.284(@anthropic-ai/sdk@0.122.0(zod@4.6.7))(@modelcontextprotocol/sdk@1.32.1(supports-color@7.2.0)(zod@4.6.7))(zod@4.6.7)': + '@anthropic-ai/claude-agent-sdk@0.3.318(@anthropic-ai/sdk@0.122.0(zod@4.6.7))(@modelcontextprotocol/sdk@1.32.1(supports-color@7.2.0)(zod@4.6.7))(zod@4.6.7)': dependencies: '@anthropic-ai/sdk': 0.122.0(zod@4.6.7) '@modelcontextprotocol/sdk': 1.32.1(supports-color@7.2.0)(zod@4.6.7) diff --git a/src/main/claude/structured/launch/claude-structured-launch-resolution.ts b/src/main/claude/structured/launch/claude-structured-launch-resolution.ts index 096321a9716..e462e89a305 100644 --- a/src/main/claude/structured/launch/claude-structured-launch-resolution.ts +++ b/src/main/claude/structured/launch/claude-structured-launch-resolution.ts @@ -55,9 +55,7 @@ export function withClaudeArtifactDefault( return chosen ? env : { ...env, [CLAUDE_ARTIFACT_ENV]: '1' } } -type ClaudePermissionOptions = Pick & { - resolvePermissionModeInCli?: true -} +type ClaudePermissionOptions = Pick export type ClaudeStructuredSdkOptions = Pick< ClaudeAgentSdkOptions, @@ -100,8 +98,8 @@ export function claudeStructuredPermissionOptions( mode: AgentChatPermissionChoice ): ClaudePermissionOptions { if (mode === 'own') { - // upstream request: https://github.com/anthropics/claude-agent-sdk-typescript/issues/519 - return { resolvePermissionModeInCli: true } + // An omitted mode lets the CLI apply the user's settings defaultMode. + return {} } return mode === 'full-access' ? { extraArgs: { 'dangerously-skip-permissions': null } } diff --git a/src/main/orcad/orcad-packaged-structured-chat.integration.test.ts b/src/main/orcad/orcad-packaged-structured-chat.integration.test.ts index 65ad62692d8..3fe20170630 100644 --- a/src/main/orcad/orcad-packaged-structured-chat.integration.test.ts +++ b/src/main/orcad/orcad-packaged-structured-chat.integration.test.ts @@ -326,8 +326,7 @@ describe.skipIf(skip || process.platform === 'win32')('packaged plain-Node struc '--output-format', '--include-partial-messages', '--replay-user-messages', - '--permission-prompt-tool', - 'stdio', + '--permission-prompt-tool=stdio', `--session-id=${claudeSessionIdForOrcaSession(sessionId)}`, '--setting-sources=user,project,local', '--dangerously-skip-permissions' From b27c91012891fa52ba919b94dc345a47bd65f3f6 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:44:59 -0800 Subject: [PATCH 19/80] fix(codex): recognize the 16 app-server notifications added through Codex 0.167 (#39943) * fix(codex): recognize the 16 app-server notifications added through Codex 0.167 Users run their own Codex, so 0.167 frames already reach Orca. Unlisted notifications fell back to visible raw rows in the chat timeline; classify each one from the 0.167.0 schema instead. * test(codex): journal no row for app-server notifications that carry provider text --- .../codex-app-server-notification-schema.ts | 18 +++++++++++++++++- ...ctured-journal-translation-streams.test.ts | 19 +++++++++++++++++++ .../providers/provider-frame-disposition.ts | 16 ++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/codex/app-server/codex-app-server-notification-schema.ts b/src/main/codex/app-server/codex-app-server-notification-schema.ts index a7319273cdb..4c84ff59b71 100644 --- a/src/main/codex/app-server/codex-app-server-notification-schema.ts +++ b/src/main/codex/app-server/codex-app-server-notification-schema.ts @@ -1,16 +1,24 @@ -// Stable ServerNotification method discriminators generated by codex-cli 0.147.0. +// Stable ServerNotification method discriminators generated by codex-cli 0.167.0. export const CODEX_APP_SERVER_NOTIFICATION_METHODS = [ 'error', 'thread/started', 'thread/status/changed', + 'thread/readState/changed', 'thread/archived', 'thread/deleted', 'thread/unarchived', 'thread/closed', + 'thread/reverted', 'skills/changed', 'thread/name/updated', + 'thread/section/changed', + 'thread/attachment/updated', 'thread/goal/updated', + 'thread/prediction/updated', 'thread/goal/cleared', + 'thread/queue/changed', + 'project/changed', + 'thread/project/updated', 'thread/environment/connected', 'thread/environment/disconnected', 'thread/settings/updated', @@ -24,6 +32,7 @@ export const CODEX_APP_SERVER_NOTIFICATION_METHODS = [ 'item/started', 'item/autoApprovalReview/started', 'item/autoApprovalReview/completed', + 'autoApprovalReview/strictReviewRequired', 'item/completed', 'rawResponseItem/completed', 'rawResponse/completed', @@ -40,7 +49,9 @@ export const CODEX_APP_SERVER_NOTIFICATION_METHODS = [ 'item/mcpToolCall/progress', 'mcpServer/oauthLogin/completed', 'mcpServer/startupStatus/updated', + 'mcpServer/event/stream/notification', 'account/updated', + 'account/gatewayOAuth/changed', 'account/rateLimits/updated', 'app/list/updated', 'remoteControl/status/changed', @@ -53,6 +64,8 @@ export const CODEX_APP_SERVER_NOTIFICATION_METHODS = [ 'thread/compacted', 'model/rerouted', 'model/verification', + 'modelProvider/authRecoveryStarted', + 'modelProvider/authRecoveryCompleted', 'turn/moderationMetadata', 'model/safetyBuffering/updated', 'warning', @@ -63,6 +76,9 @@ export const CODEX_APP_SERVER_NOTIFICATION_METHODS = [ 'fuzzyFileSearch/sessionCompleted', 'thread/realtime/started', 'thread/realtime/itemAdded', + 'thread/realtime/item/started', + 'thread/realtime/item/transcript/delta', + 'thread/realtime/item/completed', 'thread/realtime/transcript/delta', 'thread/realtime/transcript/done', 'thread/realtime/outputAudio/delta', diff --git a/src/main/codex/structured/journal/codex-structured-journal-translation-streams.test.ts b/src/main/codex/structured/journal/codex-structured-journal-translation-streams.test.ts index 67df104b1d1..74ed29258d7 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-translation-streams.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-translation-streams.test.ts @@ -198,6 +198,25 @@ describe('codex journal translation', () => { expect(tap.rows.filter((row) => row.key.includes('provider-frame-suppressed'))).toHaveLength(1) }) + it('journals no row for app-server notifications that carry provider text', () => { + const { translator, tap, window } = translatorWith() + + translator.handle(TURN_STARTED) + translator.handle( + notification('thread/prediction/updated', { threadId: THREAD_ID, text: 'Predicted reply' }) + ) + translator.handle( + notification('modelProvider/authRecoveryStarted', { message: 'Reconnecting to provider' }) + ) + translator.handle( + notification('modelProvider/authRecoveryCompleted', { message: 'Reconnected' }) + ) + translator.handle(notification('account/gatewayOAuth/changed', { error: 'Sign-in expired' })) + window.fire() + + expect(tap.rows.filter((row) => row.body.kind === 'status')).toEqual([]) + }) + it('writes nothing more after dispose', () => { const { translator, tap, window } = translatorWith() diff --git a/src/main/native-chat/agent-session-wire/providers/provider-frame-disposition.ts b/src/main/native-chat/agent-session-wire/providers/provider-frame-disposition.ts index 85bb374034c..cc31e15d12d 100644 --- a/src/main/native-chat/agent-session-wire/providers/provider-frame-disposition.ts +++ b/src/main/native-chat/agent-session-wire/providers/provider-frame-disposition.ts @@ -24,8 +24,16 @@ export const PROVIDER_FRAME_CLASSIFICATIONS = { 'thread/deleted': 'status-chrome', 'thread/unarchived': 'status-chrome', 'thread/closed': 'status-chrome', + 'thread/readState/changed': 'suppressed-benign', + 'thread/reverted': 'status-chrome', 'skills/changed': 'status-chrome', 'thread/name/updated': 'status-chrome', + 'thread/section/changed': 'suppressed-benign', + 'thread/attachment/updated': 'status-chrome', + 'thread/prediction/updated': 'suppressed-benign', + 'thread/queue/changed': 'status-chrome', + 'project/changed': 'suppressed-benign', + 'thread/project/updated': 'suppressed-benign', // Goal frames are the only authoritative evidence because goal calls are never emitted as items. 'thread/goal/updated': 'timeline-substantive', 'thread/goal/cleared': 'timeline-substantive', @@ -43,6 +51,7 @@ export const PROVIDER_FRAME_CLASSIFICATIONS = { 'item/started': 'timeline-substantive', 'item/autoApprovalReview/started': 'status-chrome', 'item/autoApprovalReview/completed': 'status-chrome', + 'autoApprovalReview/strictReviewRequired': 'status-chrome', 'item/completed': 'timeline-substantive', 'rawResponseItem/completed': 'suppressed-benign', 'rawResponse/completed': 'suppressed-benign', @@ -59,7 +68,9 @@ export const PROVIDER_FRAME_CLASSIFICATIONS = { 'item/mcpToolCall/progress': 'status-chrome', 'mcpServer/oauthLogin/completed': 'status-chrome', 'mcpServer/startupStatus/updated': 'status-chrome', + 'mcpServer/event/stream/notification': 'suppressed-benign', 'account/updated': 'status-chrome', + 'account/gatewayOAuth/changed': 'status-chrome', 'account/rateLimits/updated': 'suppressed-benign', 'app/list/updated': 'status-chrome', 'remoteControl/status/changed': 'status-chrome', @@ -72,6 +83,8 @@ export const PROVIDER_FRAME_CLASSIFICATIONS = { 'thread/compacted': 'timeline-substantive', 'model/rerouted': 'status-chrome', 'model/verification': 'status-chrome', + 'modelProvider/authRecoveryStarted': 'status-chrome', + 'modelProvider/authRecoveryCompleted': 'status-chrome', 'turn/moderationMetadata': 'suppressed-benign', 'model/safetyBuffering/updated': 'status-chrome', warning: 'error-surface', @@ -82,6 +95,9 @@ export const PROVIDER_FRAME_CLASSIFICATIONS = { 'fuzzyFileSearch/sessionCompleted': 'suppressed-benign', 'thread/realtime/started': 'status-chrome', 'thread/realtime/itemAdded': 'timeline-substantive', + 'thread/realtime/item/started': 'status-chrome', + 'thread/realtime/item/transcript/delta': 'stream-into-item', + 'thread/realtime/item/completed': 'timeline-substantive', 'thread/realtime/transcript/delta': 'stream-into-item', 'thread/realtime/transcript/done': 'timeline-substantive', 'thread/realtime/outputAudio/delta': 'stream-into-item', From 7d5e5dc2acabb6a8c4a32a3cda9a014e3677fdc6 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Sun, 8 Nov 2026 17:47:32 -0800 Subject: [PATCH 20/80] test: remove low-value custom smoke workflows and stale benchmark (#40060) Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .github/workflows/pr.yml | 6 - config/audit-tools/README.md | 1 - config/path-literal-baseline.txt | 1 - .../audit-tools/verify-locked-audit-tools.mjs | 118 ---- .../session-search-file-id-e2e/README.md | 80 --- .../session-search-file-id-e2e/client.cjs | 30 - .../session-search-file-id-e2e/host.cjs | 325 --------- .../scripts/session-search-file-id-e2e/run.ts | 194 ------ .../session-search-file-id-e2e/verify.cjs | 62 -- package.json | 1 - .../worktree-deletion-branch-fixture.mjs | 69 -- .../worktree-deletion-dev-bench.mjs | 631 ------------------ .../worktree-deletion-timing-samples.mjs | 21 - .../omp-child-history-rendered/README.md | 5 - .../tools/omp-child-session-resume-smoke.mjs | 82 --- .../tools/omp-child-session-runtime-smoke.mjs | 146 ---- .../tools/omp-fresh-session-runtime-smoke.mjs | 75 --- tests/tools/omp-history-title-smoke.mjs | 92 --- .../omp-resume-transcript-locator-smoke.mjs | 81 --- .../tools/omp-session-root-upstream-smoke.mjs | 86 --- .../omp-source-control-runtime-smoke.mjs | 33 - tests/tools/omp-source-control-ui/README.md | 4 - tests/tools/omp-startup-keyboard-smoke.mjs | 99 --- .../omp-status-input-redaction-smoke.mjs | 101 --- tests/tools/pi-ui-prompt-cdp-smoke.mjs | 61 -- tests/tools/pi-ui-prompt-extension.mjs | 43 -- tests/tools/pi-ui-prompt-runtime-smoke.mjs | 155 ----- tests/tools/pi-ui-prompt-verification.md | 42 -- 28 files changed, 2644 deletions(-) delete mode 100644 config/scripts/dependency-security/audit-tools/verify-locked-audit-tools.mjs delete mode 100644 config/scripts/session-search-file-id-e2e/README.md delete mode 100644 config/scripts/session-search-file-id-e2e/client.cjs delete mode 100644 config/scripts/session-search-file-id-e2e/host.cjs delete mode 100644 config/scripts/session-search-file-id-e2e/run.ts delete mode 100644 config/scripts/session-search-file-id-e2e/verify.cjs delete mode 100644 tests/tools/benchmarks/worktree-deletion-branch-fixture.mjs delete mode 100644 tests/tools/benchmarks/worktree-deletion-dev-bench.mjs delete mode 100644 tests/tools/benchmarks/worktree-deletion-timing-samples.mjs delete mode 100644 tests/tools/omp-child-session-resume-smoke.mjs delete mode 100644 tests/tools/omp-child-session-runtime-smoke.mjs delete mode 100644 tests/tools/omp-fresh-session-runtime-smoke.mjs delete mode 100644 tests/tools/omp-history-title-smoke.mjs delete mode 100644 tests/tools/omp-resume-transcript-locator-smoke.mjs delete mode 100644 tests/tools/omp-session-root-upstream-smoke.mjs delete mode 100644 tests/tools/omp-source-control-runtime-smoke.mjs delete mode 100644 tests/tools/omp-startup-keyboard-smoke.mjs delete mode 100644 tests/tools/omp-status-input-redaction-smoke.mjs delete mode 100644 tests/tools/pi-ui-prompt-cdp-smoke.mjs delete mode 100644 tests/tools/pi-ui-prompt-extension.mjs delete mode 100644 tests/tools/pi-ui-prompt-runtime-smoke.mjs delete mode 100644 tests/tools/pi-ui-prompt-verification.md diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index edc22bc6505..c88dc0f782f 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -342,12 +342,6 @@ jobs: if [ "$PREFLIGHT_PHASE_SELECTED" != true ] || [ "$PREFLIGHT_PRIOR_SUCCESS" != true ]; then exit 0; fi node config/scripts/preflight/changed-checks/check-changed-code-quality.mjs "${{ github.event.pull_request.base.sha }}" - - name: Verify locked audit tools - if: needs.code_paths.outputs.static_analysis == 'true' - env: - ORCA_BACKGROUND_LAUNCH: '1' - run: node config/scripts/dependency-security/audit-tools/verify-locked-audit-tools.mjs - - name: Enforce React Doctor on changed lines if: needs.code_paths.outputs.static_analysis == 'true' run: node config/scripts/static-analysis/react-doctor/check-react-doctor-changed.mjs "${{ github.event.pull_request.base.sha }}" diff --git a/config/audit-tools/README.md b/config/audit-tools/README.md index 7ae8738106c..9c060fa55a2 100644 --- a/config/audit-tools/README.md +++ b/config/audit-tools/README.md @@ -13,4 +13,3 @@ To update a tool, change its exact version in this package's manifest, run `pnpm --dir config/audit-tools install --ignore-scripts`, and review both the manifest and generated lockfile. The workspace enforces a three-day release delay and disables install scripts. Keep its package-manager pin aligned with the root. -Run `node config/scripts/verify-locked-audit-tools.mjs` to exercise both tools. diff --git a/config/path-literal-baseline.txt b/config/path-literal-baseline.txt index a0a0ede85ea..3d31a05c9e5 100644 --- a/config/path-literal-baseline.txt +++ b/config/path-literal-baseline.txt @@ -6,7 +6,6 @@ config/scripts/performance/session-search/session-search-tool-heavy-corpus.ts src/main/ai-vault/session-transcript-reader.ts config/scripts/runtime-verification/desktop-transport/run-linux-lid-sleep-assertion-docker.mjs src/main/linux-lid-sleep-assertion.ts config/scripts/runtime-verification/desktop-transport/run-ssh-staged-upload-reliability.mjs src/main/ssh/ssh-relay-install-namespace.test.ts -config/scripts/session-search-file-id-e2e/run.ts src/main/ai-vault/session-scanner-service-entry.ts config/scripts/static-analysis/ratchets/renderer-state/check-layout-writer-ratchets.mjs src/renderer/src/runtime/remote-workspace-frozen/ config/scripts/workflow/source-routing/pr-code-change-scope.mjs src/main/runtime/runtime-worktree-agent- config/scripts/workflow/source-routing/pr-code-change-scope.mjs src/shared/agent-session-mutation-envelope diff --git a/config/scripts/dependency-security/audit-tools/verify-locked-audit-tools.mjs b/config/scripts/dependency-security/audit-tools/verify-locked-audit-tools.mjs deleted file mode 100644 index 400546950fb..00000000000 --- a/config/scripts/dependency-security/audit-tools/verify-locked-audit-tools.mjs +++ /dev/null @@ -1,118 +0,0 @@ -import assert from 'node:assert/strict' -import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' -import { join, resolve } from 'node:path' -import { parse } from 'yaml' -import { runProcessSync } from '@orca/process-host' -import { auditToolEntry, installAuditTools } from './run-audit-tool.mjs' - -const root = resolve(import.meta.dirname, '../../../..') -const installed = installAuditTools(root) -assert.equal(installed.code, 0, 'Frozen, script-free audit tool install failed') - -const scratch = join(root, 'tmp') -mkdirSync(scratch, { recursive: true }) -const fixture = mkdtempSync(join(scratch, 'locked audit tools ')) -const toolManifest = JSON.parse(readFileSync(join(root, 'config/audit-tools/package.json'), 'utf8')) - -function command(program, args, expectedCode = 0) { - const result = runProcessSync({ program, args, cwd: fixture, timeoutMs: 60_000 }) - assert.equal(result.timedOut, false, `${program} timed out`) - assert.equal(result.signal, null, `${program} received ${result.signal}`) - assert.equal(result.code, expectedCode, `${result.stdout}\n${result.stderr}`) - return result.stdout -} - -function tool(name, args, expectedCode = 0) { - return command(process.execPath, [auditToolEntry(name, root), ...args], expectedCode) -} - -function write(file, value) { - writeFileSync(join(fixture, file), value) -} - -const cleanComponent = 'export default function App() { return }\n' -const indexKeyComponent = - 'export default function App() { return
{["one"].map((text, index) => {text})}
}\n' -const doctorArgs = [ - '.', - '--yes', - '--no-dead-code', - '--no-supply-chain', - '--no-telemetry', - '--no-cache', - '--json', - '--blocking', - 'warning' -] - -try { - command('git', ['init', '--quiet']) - command('git', ['config', 'core.hooksPath', join(fixture, '.git', 'disabled-hooks')]) - command('git', ['config', 'commit.gpgSign', 'false']) - write( - 'package.json', - JSON.stringify({ name: 'locked-audit-smoke', private: true, dependencies: { react: '19.2.8' } }) - ) - write('App.tsx', cleanComponent) - write('knip.json', JSON.stringify({ entry: ['App.tsx'], project: ['*.tsx'] })) - - for (const name of ['react-doctor', 'knip']) { - assert.equal(tool(name, ['--version']).trim(), toolManifest.dependencies[name]) - } - - const clean = JSON.parse(tool('react-doctor', doctorArgs)) - assert.equal(clean.ok, true) - assert.ok(clean.projects.some((project) => project.analyzedFiles.includes('App.tsx'))) - tool('knip', ['--config', 'knip.json', '--files']) - - write('App.tsx', indexKeyComponent) - command('git', ['add', '.']) - command('git', [ - '-c', - 'user.name=Audit tool smoke', - '-c', - 'user.email=audit-smoke@example.invalid', - 'commit', - '--quiet', - '-m', - 'Baseline' - ]) - write('Changed.tsx', indexKeyComponent.replace('function App', 'function Changed')) - command('git', ['add', 'Changed.tsx']) - const changed = JSON.parse( - tool('react-doctor', [...doctorArgs, '--scope', 'lines', '--base', 'HEAD'], 1) - ) - assert.ok(changed.diagnostics.some((item) => item.filePath.endsWith('Changed.tsx'))) - assert.ok(changed.diagnostics.every((item) => !item.filePath.endsWith('App.tsx'))) - - write('Orphan.tsx', 'export const orphan = true\n') - assert.match(tool('knip', ['--config', 'knip.json', '--files'], 1), /Orphan\.tsx/) - - const stale = join(fixture, 'config', 'audit-tools') - mkdirSync(stale, { recursive: true }) - mkdirSync(join(fixture, 'config', 'patches'), { recursive: true }) - const toolPolicy = parse( - readFileSync(join(root, 'config/audit-tools/pnpm-workspace.yaml'), 'utf8') - ) - for (const patch of Object.values(toolPolicy.patchedDependencies ?? {})) { - copyFileSync(join(root, 'config/audit-tools', patch), join(stale, patch)) - } - for (const file of ['pnpm-lock.yaml', 'pnpm-workspace.yaml']) { - copyFileSync(join(root, 'config', 'audit-tools', file), join(stale, file)) - } - writeFileSync( - join(stale, 'package.json'), - JSON.stringify({ - ...toolManifest, - dependencies: { ...toolManifest.dependencies, knip: '0.0.0' } - }) - ) - const rejected = installAuditTools(fixture, (spec) => runProcessSync({ ...spec, stdio: 'pipe' })) - assert.notEqual(rejected.code, 0, 'A stale tool lockfile was accepted') - assert.match(`${rejected.stdout}\n${rejected.stderr}`, /OUTDATED_LOCKFILE/) - console.log( - 'Locked audit tools: real analysis, changed-line filtering, findings and stale-lock rejection passed' - ) -} finally { - rmSync(fixture, { recursive: true, force: true }) -} diff --git a/config/scripts/session-search-file-id-e2e/README.md b/config/scripts/session-search-file-id-e2e/README.md deleted file mode 100644 index ae1de7fd994..00000000000 --- a/config/scripts/session-search-file-id-e2e/README.md +++ /dev/null @@ -1,80 +0,0 @@ -# Windows session-search file-ID integration reproduction - -Opt-in Windows/NTFS harness for #20551. Run from the fix workspace root with -dependencies, the Electron binary, and Git object `9845bef63a6` available. -Everything generated stays under ignored `notes/search-ipc/`; each run gets fresh -roots and a profile. No real transcripts, WSL discovery, desktop windows, or user -profile are used. The synthetic file is recreated until NTFS assigns a real inode -above `Number.MAX_SAFE_INTEGER`; failure to obtain one fails the test. - -```powershell -$env:ORCA_BACKGROUND_LAUNCH='1' -pnpm exec esbuild config/scripts/session-search-file-id-e2e/run.ts --bundle --platform=node --format=esm --packages=external --outfile=notes/search-ipc/run.mjs -node notes/search-ipc/run.mjs red -# Expected exit 1: initial indexing/query succeeds, subsequent reconciliation fails. -node notes/search-ipc/run.mjs green -# Expected exit 0 for both the lifecycle and fresh-host restart phases. -node config/scripts/session-search-file-id-e2e/verify.cjs -``` - -Run red and green sequentially: their build step reuses the exported source tree. -The verifier checks the expected red failure, green reports, identical oracle -expressions after formatting normalization, and that exactly the two file-ID -SELECT source files differ. It does not turn an arbitrary red failure into success. - -## Exact topology - -The runner exports **LOCAL integration overlay** #20516 head -`9845bef63a6d0b80ec37af82b5b275287b8e17df` with `git archive`, then bundles the real -scanner entry and parent modules with esbuild. It does not check out, stage, or -push overlay sources. Green changes only the two SELECT projections to the CAST -expressions in #20551; red retains the original projections. CLI #20514 is unused. - -An isolated, windowless Electron host installs the production child search -enablement using fixture settings. The production shared scanner client invokes -`spawnAiVaultServiceProcess`, which forks Electron with `ELECTRON_RUN_AS_NODE=1` -and the real `session-scanner-service-entry`. That child owns the production -instance, indexer, SQLite search engine, and IPC request handling. - -Marker queries come from **separate Node client processes over a real Windows -named pipe**, through production `UnixSocketTransport`, production `RpcDispatcher` -and `AI_VAULT_METHODS`, then the search registry/service and scanner IPC. Status -polls and default-off/disabled responses also exercise registered handlers directly. -An independent read-only SQLite connection verifies active FTS rows and committed -file metadata against filesystem size/mtime. The transcript-consumer observation -logs actual replace/append modes without supplying messages or search results. - -Labelled fixture seams, identical in both variants: - -- Parent root resolution returns the existing `isolatedScanRoots` fixture. -- `getSettings` supplies the isolated JSON policy; real enablement/settings-change functions apply it. -- The child's allowlisted environment includes `ORCA_BACKGROUND_LAUNCH=1`. -- Passive child PID, IPC, stderr, and transcript-read observations are added. -- The unused default RPC method catalog is excluded; the dispatcher receives the production `AI_VAULT_METHODS` explicitly. Its runtime context supplies only a fixture runtime ID. - -## Oracle and evidence - -Default-off creates no DB and returns disabled. Enablement indexes the marker; -two **real default 20-second recent timer cycles** and two explicit full passes -preserve it. An append becomes searchable; a rename replacement with equal byte -length and exactly restored mtime removes the old marker and adds the new one. -The scanner restarts, then a second Electron host opens the same profile/DB and -queries again. Disablement followed by an append, explicit reconcile, and 21-second -wait leaves the DB unchanged and the new marker absent. - -`red-latest.json` / `green-latest.json` locate each run. Reports contain parent and -scanner PIDs, executable/Node/Electron versions, IPC events, raw unsafe inode, -queries, DB metadata, read modes, and scanner exit codes. `*-stages.jsonl` gives -live progress; `process-*.log` preserves complete output. `comparison.json` records -the two-source-only comparison. The production scanner shutdown protocol closes -only recorded children; the runner waits for its own Electron hosts and external -clients. Profiles/DBs remain as evidence, not running services. - -## Boundaries not exercised - -This is not a packaged/full Orca desktop launch. The host and scanner are bundled -from production sources by this harness, not electron-vite's complete app build. -The actual runtime authentication/metadata server, CLI routing, remote SSH/relay -transport, and UI are not exercised. Named-pipe framing and dispatcher are real, -but the harness connects them directly with a fixture token; it makes no claim -about production authentication. No session-search UI exists in this topology. diff --git a/config/scripts/session-search-file-id-e2e/client.cjs b/config/scripts/session-search-file-id-e2e/client.cjs deleted file mode 100644 index 1922547f6ac..00000000000 --- a/config/scripts/session-search-file-id-e2e/client.cjs +++ /dev/null @@ -1,30 +0,0 @@ -const { connect } = require('node:net') -const assert = require('node:assert/strict') -assert.equal(process.env.ORCA_BACKGROUND_LAUNCH, '1') -const socket = connect(process.argv[2]) -let buffer = '' -socket.setEncoding('utf8') -socket.setTimeout(10_000, () => socket.destroy(new Error('RPC client timeout'))) -socket.on('connect', () => - socket.write( - `${JSON.stringify({ - id: 'file-id-query', - authToken: 'isolated-fixture', - method: 'aiVault.searchSessions', - params: JSON.parse(process.argv[3]) - })}\n` - ) -) -socket.on('data', (chunk) => { - buffer += String(chunk) - const newline = buffer.indexOf('\n') - if (newline === -1) { - return - } - console.log(JSON.stringify({ pid: process.pid, response: JSON.parse(buffer.slice(0, newline)) })) - socket.end() -}) -socket.on('error', (error) => { - console.error(error) - process.exitCode = 1 -}) diff --git a/config/scripts/session-search-file-id-e2e/host.cjs b/config/scripts/session-search-file-id-e2e/host.cjs deleted file mode 100644 index 17ddbb21ff8..00000000000 --- a/config/scripts/session-search-file-id-e2e/host.cjs +++ /dev/null @@ -1,325 +0,0 @@ -const { app } = require('electron') -const assert = require('node:assert/strict') -const fs = require('node:fs/promises') -const { existsSync, appendFileSync } = require('node:fs') -const { join } = require('node:path') -const { DatabaseSync } = require('node:sqlite') -const output = process.argv[2] -const phase = process.argv[3] -const profile = join(output, 'profile') -app.setPath('userData', profile) -app.setPath('sessionData', join(profile, 'chromium')) -app.disableHardwareAcceleration() -globalThis.__fileIdChildren = [] -const report = { - phase, - pid: process.pid, - execPath: process.execPath, - versions: process.versions, - stages: [], - children: [], - stderr: [], - ipc: [] -} -globalThis.__fileIdObserveChild = (child) => { - globalThis.__fileIdChildren.push(child) - child.stderr.on('data', (chunk) => report.stderr.push(String(chunk))) - child.on('message', (message) => - report.ipc.push({ pid: child.pid, type: message.type, operation: message.operation }) - ) -} -const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) -const sessionId = 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee' -const markers = { - initial: 'quartzinitialmarker', - append: 'cobaltappendedmarker', - replacement: 'velvetreplacedmarker', - disabled: 'tungstendisabledmarker' -} -let api -let registration -let transport -const endpoint = `\\\\.\\pipe\\orca-file-id-${process.pid}` -let settings = {} -let transcript -const database = join(profile, 'ai-vault', 'session-search.sqlite') -function record(stage, data = {}) { - const entry = { stage, at: Date.now(), ...data } - report.stages.push(entry) - appendFileSync(join(output, `${phase}-stages.jsonl`), `${JSON.stringify(entry)}\n`) - console.log(stage, JSON.stringify(data)) -} -function inspect(marker) { - const db = new DatabaseSync(database, { readOnly: true }) - try { - const query = db.prepare( - 'SELECT CAST(ino AS TEXT) AS ino, byte_offset, size_bytes, mtime_ms FROM files WHERE path = ?' - ) - const rows = db - .prepare(`SELECT s.session_id FROM messages_fts f JOIN messages m ON m.id=f.rowid - JOIN sessions s ON s.id=m.session_row_id WHERE messages_fts MATCH ?`) - .all(marker) - return { file: query.get(transcript), sessions: rows.map((row) => row.session_id) } - } finally { - db.close() - } -} -async function query(marker, present) { - const result = await api.runProcess({ - program: process.env.ORCA_FILE_ID_NODE, - args: [ - join(output, 'client.cjs'), - endpoint, - JSON.stringify({ query: `"${marker}"`, scope: 'conversation', freshness: 'indexed' }) - ], - env: process.env, - timeoutMs: 15_000 - }) - assert.equal(result.code, 0, result.stderr) - const external = JSON.parse(result.stdout) - assert.equal(external.response.ok, true, JSON.stringify(external)) - const response = external.response.result - assert.equal(response.kind, 'results', JSON.stringify(response)) - assert.equal( - response.hits.some((hit) => hit.sessionId === sessionId), - present, - `RPC ${marker}` - ) - const disk = inspect(marker) - assert.equal(disk.sessions.includes(sessionId), present, `DB ${marker}`) - const actual = await fs.stat(transcript) - assert.equal( - disk.file.byte_offset, - actual.size, - 'Committed cursor covers actual transcript bytes' - ) - assert.equal(disk.file.size_bytes, actual.size) - assert.equal(disk.file.mtime_ms, actual.mtimeMs) - record('query', { marker, present, clientPid: external.pid, endpoint, response, disk }) -} -async function status() { - return api.rpc('aiVault.searchStatus', {}) -} -async function waitFor(predicate, label, timeout = 30_000) { - const until = Date.now() + timeout - while (Date.now() < until) { - const current = await status() - if (predicate(current)) { - return current - } - await sleep(200) - } - throw new Error(`Timed out: ${label}`) -} -async function policy(next) { - const before = settings - settings = next - await fs.writeFile(join(profile, 'fixture-settings.json'), JSON.stringify(settings)) - api.applySessionSearchSettingsChange(before, settings) -} -async function stopChild() { - const children = [...globalThis.__fileIdChildren] - api.resetAiVaultScannerServiceForTests() - for (const child of children) { - const until = Date.now() + 5000 - while (child.exitCode === null && child.signalCode === null && Date.now() < until) { - await sleep(50) - } - assert.ok( - child.exitCode !== null || child.signalCode !== null, - `Child ${child.pid} did not exit` - ) - if (!report.children.some((row) => row.pid === child.pid)) { - report.children.push({ - pid: child.pid, - spawnfile: child.spawnfile, - spawnargs: child.spawnargs, - exitCode: child.exitCode, - signalCode: child.signalCode - }) - } - } -} -async function run() { - assert.equal(process.env.ORCA_BACKGROUND_LAUNCH, '1') - await fs.mkdir(profile, { recursive: true }) - api = require('./production.js') - api.setAppEnvironment({ - getPath: (name) => (name === 'userData' ? profile : app.getPath(name)), - getAppPath: () => output, - getVersion: () => app.getVersion(), - isPackaged: () => false, - onWillQuit: (handler) => app.on('will-quit', handler), - exit: (code) => app.exit(code), - getAppMetrics: () => app.getAppMetrics() - }) - const dispatcher = new api.RpcDispatcher({ - methods: api.AI_VAULT_METHODS, - runtime: { getRuntimeId: () => `file-id-${process.pid}` } - }) - transport = new api.UnixSocketTransport({ endpoint, kind: 'named-pipe' }) - transport.onMessage((message, reply) => { - void dispatcher - .dispatch(JSON.parse(message)) - .then((response) => reply(JSON.stringify(response))) - }) - await transport.start() - record('rpc-transport', { - endpoint, - auth: 'fixture; production auth/metadata server not booted', - methods: 'production AI_VAULT_METHODS', - dispatcher: 'production RpcDispatcher' - }) - const roots = { - ...api.isolatedScanRoots(join(output, 'roots')), - wslHomeDirs: [], - additionalCodexSessionsDirs: [], - executionHostId: 'local' - } - process.env.ORCA_FILE_ID_ROOTS = JSON.stringify(roots) - await fs.mkdir(roots.claudeProjectsDir, { recursive: true }) - transcript = join(roots.claudeProjectsDir, `${sessionId}.jsonl`) - if (phase === 'restart') { - settings = JSON.parse(await fs.readFile(join(profile, 'fixture-settings.json'), 'utf8')) - assert.equal(settings.aiVaultSearch.enabled, true) - const beforeRestart = inspect(markers.replacement) - registration = api.installChildSessionSearchService({ - dataRoot: profile, - getSettings: () => settings - }) - await waitFor((s) => s.filesIndexed === 1 && s.phase === 'current', 'Electron host restart') - await query(markers.append, false) - await query(markers.replacement, true) - assert.deepEqual(inspect(markers.replacement), beforeRestart) - record('app-host-restart-same-db') - await disableAndCheck() - return - } - const body = `${api.claudeLines([markers.initial], sessionId, 0).join('\n')}\n` - await fs.writeFile(transcript, body) - let raw = await fs.stat(transcript, { bigint: true }) - for (let n = 0; raw.ino <= BigInt(Number.MAX_SAFE_INTEGER) && n < 512; n++) { - await fs.unlink(transcript) - await fs.writeFile(transcript, body) - raw = await fs.stat(transcript, { bigint: true }) - } - assert.ok(raw.ino > BigInt(Number.MAX_SAFE_INTEGER), 'NTFS must supply an actual unsafe inode') - record('fixture', { - transcript, - rawIno: raw.ino.toString(), - numericIno: BigInt(Number(raw.ino)).toString() - }) - registration = api.installChildSessionSearchService({ - dataRoot: profile, - getSettings: () => settings - }) - assert.ok(registration) - assert.equal((await status()).enabled, false) - assert.deepEqual(await api.rpc('aiVault.searchSessions', { query: markers.initial }), { - kind: 'unavailable', - reason: 'disabled' - }) - assert.equal(existsSync(database), false) - record('default-off') - await policy({ aiVaultSearch: { enabled: true, historyDays: null } }) - const initial = await waitFor( - (s) => s.filesIndexed === 1 && s.phase === 'current', - 'initial indexing' - ) - assert.equal(inspect(markers.initial).file.ino, BigInt(Number(raw.ino)).toString()) - await query(markers.initial, true) - record('initial-indexing', { status: initial }) - let recent = initial - for (let n = 0; n < 2; n++) { - const previous = recent - recent = await waitFor( - (s) => s.lastReconcileAt > previous.lastReconcileAt, - 'recent timer reconciliation' - ) - assert.equal( - recent.lastSweepCompletedAt, - initial.lastSweepCompletedAt, - 'Expected a recent pass' - ) - await query(markers.initial, true) - record('recent-pass', { status: recent }) - } - for (let n = 0; n < 2; n++) { - await api.reconcileSessionSearchInService() - await query(markers.initial, true) - record('full-pass', { status: await status() }) - } - await fs.appendFile( - transcript, - `${api.claudeLines([markers.append], sessionId, 10).join('\n')}\n` - ) - const frozen = new Date(Math.floor((await fs.stat(transcript)).mtimeMs)) - await fs.utimes(transcript, frozen, frozen) - await api.reconcileSessionSearchInService() - await query(markers.append, true) - const before = await fs.stat(transcript) - const replacement = `${transcript}.replacement` - const replacementBody = (await fs.readFile(transcript, 'utf8')).replaceAll( - markers.append, - markers.replacement - ) - assert.equal(Buffer.byteLength(replacementBody), before.size) - await fs.writeFile(replacement, replacementBody) - await fs.rename(replacement, transcript) - await fs.utimes(transcript, before.atime, before.mtime) - const after = await fs.stat(transcript) - assert.equal(after.size, before.size) - assert.equal(after.mtimeMs, before.mtimeMs) - assert.notEqual(after.ino, before.ino) - await api.reconcileSessionSearchInService() - await query(markers.append, false) - await query(markers.replacement, true) - record('replacement', { beforeIno: before.ino, afterIno: after.ino }) - await stopChild() - await waitFor((s) => s.filesIndexed === 1 && s.phase === 'current', 'scanner restart') - await query(markers.append, false) - await query(markers.replacement, true) - record('service-restart') -} -async function disableAndCheck() { - await policy({ aiVaultSearch: { enabled: false, historyDays: null } }) - await waitFor((s) => !s.enabled, 'disable') - const snapshot = inspect(markers.replacement) - await fs.appendFile( - transcript, - `${api.claudeLines([markers.disabled], sessionId, 20).join('\n')}\n` - ) - await api.reconcileSessionSearchInService() - await sleep(21_000) - assert.deepEqual(await api.rpc('aiVault.searchSessions', { query: markers.disabled }), { - kind: 'unavailable', - reason: 'disabled' - }) - assert.deepEqual(inspect(markers.replacement), snapshot) - assert.deepEqual(inspect(markers.disabled).sessions, []) - record('disabled-no-indexing') -} -app.whenReady().then(async () => { - try { - await run() - report.ok = true - } catch (error) { - report.ok = false - report.error = error.stack - console.error(error) - } finally { - registration?.dispose() - await transport?.stop() - if (api) { - await stopChild().catch((error) => { - report.cleanupError = String(error) - report.ok = false - }) - } - if (report.stderr.some((text) => text.includes('[ai-vault-search]'))) { - report.ok = false - } - await fs.writeFile(join(output, `report-${phase}.json`), JSON.stringify(report, null, 2)) - app.exit(report.ok ? 0 : 1) - } -}) diff --git a/config/scripts/session-search-file-id-e2e/run.ts b/config/scripts/session-search-file-id-e2e/run.ts deleted file mode 100644 index 3ab6b5ae15d..00000000000 --- a/config/scripts/session-search-file-id-e2e/run.ts +++ /dev/null @@ -1,194 +0,0 @@ -import assert from 'node:assert/strict' -import { mkdir, readFile, writeFile, copyFile } from 'node:fs/promises' -import { resolve, join, relative } from 'node:path' -import { createHash } from 'node:crypto' -import { build } from 'esbuild' -import { runProcess } from '@orca/process-host' - -const root = process.cwd() -const evidence = resolve('notes/search-ipc') -const overlay = join(evidence, 'overlay') -const wiring = '9845bef63a6' -const mode = process.argv[2] -assert.equal(process.platform, 'win32', 'This harness requires native Windows and NTFS') -assert.ok(mode === 'red' || mode === 'green', 'Pass red or green') -assert.equal(process.env.ORCA_BACKGROUND_LAUNCH, '1') -const output = join(evidence, mode, String(Date.now())) -await mkdir(output, { recursive: true }) - -async function command(program: string, args: string[]) { - const result = await runProcess({ program, args, cwd: root, timeoutMs: 120_000 }) - assert.equal(result.code, 0, result.stderr) - return result.stdout.trim() -} - -// The overlay is exported, never checked out or staged on the fix branch. -await mkdir(overlay, { recursive: true }) -await command('git', [ - 'archive', - '--format=tar', - `--output=${join(evidence, 'overlay.tar')}`, - wiring, - 'src' -]) -await command('tar', [ - '-xf', - relative(root, join(evidence, 'overlay.tar')), - '-C', - relative(root, overlay) -]) -const edits: { path: string; sha256: string }[] = [] -function replaceOnce(source: string, before: string, after: string) { - assert.equal(source.split(before).length, 2, `Expected exactly one injection: ${before}`) - return source.replace(before, after) -} - -const adapter = join(output, 'adapter.mjs') -const source = (path: string) => JSON.stringify(join(overlay, 'src', path)) -await writeFile( - adapter, - ` -export { installChildSessionSearchService, applySessionSearchSettingsChange } from ${source('main/ai-vault-search/session-search-enablement.ts')}; -export { resetAiVaultScannerServiceForTests, reconcileSessionSearchInService } from ${source('main/ai-vault/session-scanner-service-spawn.ts')}; -export { setAppEnvironment } from ${source('shared/app-environment.ts')}; -export { isolatedScanRoots } from ${source('main/ai-vault/session-scanner-test-fixtures.ts')}; -export { claudeLines } from ${source('main/ai-vault-search/session-search-indexer-test-fixture.ts')}; -export { runProcess } from '@orca/process-host'; -export { UnixSocketTransport } from ${source('main/runtime/rpc/unix-socket-transport.ts')}; -export { RpcDispatcher } from ${source('main/runtime/rpc/dispatcher.ts')}; -export { AI_VAULT_METHODS } from ${source('main/runtime/rpc/methods/ai-vault.ts')}; -import { buildRegistry } from ${source('main/runtime/rpc/core.ts')}; -import { AI_VAULT_METHODS } from ${source('main/runtime/rpc/methods/ai-vault.ts')}; -const registry = buildRegistry(AI_VAULT_METHODS); -export async function rpc(name, params) { - const method = registry.get(name); - if (!method || !name.startsWith('aiVault.search')) throw new Error('Unexpected method'); - return method.handler(method.params.parse(params), {}); -} -` -) - -await build({ - entryPoints: { - production: adapter, - 'session-scanner-service-entry': join( - overlay, - 'src/main/ai-vault/session-scanner-service-entry.ts' - ) - }, - outdir: output, - platform: 'node', - format: 'cjs', - bundle: true, - packages: 'external', - define: { ORCA_FEATURE_WALL_ENABLED: 'true' }, - plugins: [ - { - name: 'labelled-local-fixtures', - setup(api) { - api.onLoad({ filter: /\.ts$/ }, async (args) => { - let contents = await readFile(args.path, 'utf8') - const path = args.path.replaceAll('\\', '/') - if (path.endsWith('/runtime/rpc/methods/rpc-method-registry.ts')) { - // DispatcherOptions explicitly supplies AI_VAULT_METHODS; don't load unrelated default methods. - contents = 'export const ALL_RPC_METHODS = []' - } - if (path.endsWith('/cached-session-list.ts')) { - contents = replaceOnce( - contents, - ' const [additionalCodexHomes, wslHomeDirs] = await Promise.all([', - ' return JSON.parse(process.env.ORCA_FILE_ID_ROOTS!);\n const [additionalCodexHomes, wslHomeDirs] = await Promise.all([' - ) - } - if (path.endsWith('/session-scanner-service-env.ts')) { - contents = replaceOnce( - contents, - " env.ELECTRON_RUN_AS_NODE = '1'", - " env.ORCA_BACKGROUND_LAUNCH = '1'\n env.ELECTRON_RUN_AS_NODE = '1'" - ) - } - if (path.endsWith('/session-scanner-service-spawn.ts')) { - contents = replaceOnce( - contents, - ' lowerAiVaultServicePriority(child.pid)', - ' globalThis.__fileIdObserveChild?.(child)\n lowerAiVaultServicePriority(child.pid)' - ) - } - if (path.endsWith('/session-scanner-service-entry.ts')) { - contents = `import { registerTranscriptConsumer } from './session-transcript-consumers'; -registerTranscriptConsumer({beginRead: start => { console.error('[file-id-read]', JSON.stringify({mode:start.mode,path:start.candidate.file.path})); return null }}); -console.error('[file-id-child]', JSON.stringify({pid:process.pid,execPath:process.execPath,versions:process.versions,background:process.env.ORCA_BACKGROUND_LAUNCH}));\n${contents}` - } - if (mode === 'green' && path.endsWith('/session-search-store.ts')) { - contents = replaceOnce( - contents, - 'SELECT path, dev, ino, mtime_ms', - 'SELECT path, CAST(dev AS REAL) AS dev, CAST(ino AS REAL) AS ino, mtime_ms' - ) - } - if (mode === 'green' && path.endsWith('/session-search-index-writer.ts')) { - contents = replaceOnce( - contents, - 'SELECT dev, ino, byte_offset', - 'SELECT CAST(dev AS REAL) AS dev, CAST(ino AS REAL) AS ino, byte_offset' - ) - } - edits.push({ - path: path.replace(overlay.replaceAll('\\', '/'), ''), - sha256: createHash('sha256').update(contents).digest('hex') - }) - return { contents, loader: 'ts' } - }) - } - } - ] -}) -await copyFile( - join(root, 'config/scripts/session-search-file-id-e2e/host.cjs'), - join(output, 'host.cjs') -) -await copyFile( - join(root, 'config/scripts/session-search-file-id-e2e/client.cjs'), - join(output, 'client.cjs') -) -await writeFile( - join(output, 'topology.json'), - JSON.stringify( - { - wiring: await command('git', ['rev-parse', wiring]), - fix: await command('git', ['rev-parse', 'HEAD']), - mode, - injections: [ - 'isolated root resolver', - 'background child env', - 'child PID/error/read observation', - 'unused default RPC catalog excluded; explicit production AI_VAULT_METHODS' - ], - files: edits.sort((a, b) => a.path.localeCompare(b.path)) - }, - null, - 2 - ) -) -await writeFile(join(evidence, `${mode}-latest.json`), JSON.stringify({ output })) -for (const phase of ['lifecycle', 'restart']) { - console.log(JSON.stringify({ mode, phase, output })) - const result = await runProcess({ - program: join(root, 'node_modules/electron/dist/electron.exe'), - args: [join(output, 'host.cjs'), output, phase], - cwd: output, - env: { - ...process.env, - ORCA_BACKGROUND_LAUNCH: '1', - ELECTRON_RUN_AS_NODE: undefined, - ORCA_FILE_ID_NODE: process.execPath - }, - timeoutMs: 180_000 - }) - await writeFile(join(output, `process-${phase}.log`), result.stdout + result.stderr) - console.log(JSON.stringify({ mode, phase, code: result.code, timedOut: result.timedOut, output })) - process.exitCode = result.code ?? 1 - if (process.exitCode !== 0) { - break - } -} diff --git a/config/scripts/session-search-file-id-e2e/verify.cjs b/config/scripts/session-search-file-id-e2e/verify.cjs deleted file mode 100644 index 180bf4cf154..00000000000 --- a/config/scripts/session-search-file-id-e2e/verify.cjs +++ /dev/null @@ -1,62 +0,0 @@ -const assert = require('node:assert/strict') -const { readFileSync, writeFileSync } = require('node:fs') -const { join, resolve } = require('node:path') -const { createHash } = require('node:crypto') -const { transformSync } = require('esbuild') -const evidence = resolve('notes/search-ipc') -const json = (path) => JSON.parse(readFileSync(path, 'utf8')) -const red = json(join(evidence, 'red-latest.json')).output -const green = json(join(evidence, 'green-latest.json')).output -const before = json(join(red, 'topology.json')) -const after = json(join(green, 'topology.json')) -assert.equal(before.wiring, after.wiring) -assert.deepEqual(before.injections, after.injections) -const oldFiles = new Map(before.files.map((row) => [row.path, row.sha256])) -assert.equal(oldFiles.size, after.files.length) -const changed = after.files - .filter((row) => oldFiles.get(row.path) !== row.sha256) - .map((row) => row.path) - .sort() -assert.deepEqual(changed, [ - '/src/main/ai-vault-search/session-search-index-writer.ts', - '/src/main/ai-vault-search/session-search-store.ts' -]) -for (const name of ['host.cjs', 'client.cjs']) { - // Ignore formatter-only differences; preserve every expression and assertion. - const hash = (root) => - createHash('sha256') - .update( - transformSync(readFileSync(join(root, name), 'utf8'), { - loader: 'js', - minifyWhitespace: true - }).code - ) - .digest('hex') - assert.equal(hash(red), hash(green), `Identical oracle: ${name}`) -} -const failed = json(join(red, 'report-lifecycle.json')) -assert.equal(failed.ok, false) -assert.match(failed.stderr.join(''), /RangeError: Value is too large/) -assert.ok(failed.stages.some((row) => row.stage === 'initial-indexing')) -const reports = [ - json(join(green, 'report-lifecycle.json')), - json(join(green, 'report-restart.json')) -] -for (const report of reports) { - assert.equal(report.ok, true) - assert.ok(report.ipc.some((row) => row.operation === 'searchSessions')) - assert.ok(report.stages.some((row) => row.stage === 'query' && row.clientPid !== report.pid)) - assert.ok(report.children.every((child) => child.exitCode === 0)) -} -assert.notEqual(reports[0].pid, reports[1].pid) -const result = { - ok: true, - wiring: before.wiring, - changed, - red, - green, - parentPids: [failed.pid, ...reports.map((row) => row.pid)], - childPids: [failed, ...reports].flatMap((report) => report.children.map((child) => child.pid)) -} -writeFileSync(join(evidence, 'comparison.json'), JSON.stringify(result, null, 2)) -console.log(JSON.stringify(result)) diff --git a/package.json b/package.json index 604f23dd467..faf04c0f277 100644 --- a/package.json +++ b/package.json @@ -207,7 +207,6 @@ "bench:wsl-git-shell": "node config/scripts/performance/wsl-git/wsl-git-shell-benchmark.mjs", "bench:hang-watchdog-memory": "pnpm run ensure:electron-runtime && node config/scripts/diagnostics/hang-watchdog/hang-watchdog-memory-benchmark.mjs", "bench:main-thread-jank": "pnpm run ensure:electron-runtime && node tests/tools/benchmarks/main-thread-jank-bench.mjs", - "bench:worktree-deletion": "node tests/tools/benchmarks/worktree-deletion-dev-bench.mjs", "bench:zustand-selector-fanout": "node config/scripts/performance/renderer-projection/zustand-selector-fanout-benchmark.mjs", "bench:agent-inspection-cadence": "node config/scripts/performance/agent-lineage/agent-inspection-cadence-batching-benchmark.mjs", "bench:renderer-quadratic-scans": "node config/scripts/performance/renderer-markdown/renderer-quadratic-scan-benchmark.mjs", diff --git a/tests/tools/benchmarks/worktree-deletion-branch-fixture.mjs b/tests/tools/benchmarks/worktree-deletion-branch-fixture.mjs deleted file mode 100644 index c655e059b5d..00000000000 --- a/tests/tools/benchmarks/worktree-deletion-branch-fixture.mjs +++ /dev/null @@ -1,69 +0,0 @@ -import { spawnSync } from 'node:child_process' -import net from 'node:net' -import path from 'node:path' - -function runGit(args, cwd) { - const result = spawnSync('git', args, { cwd, encoding: 'utf8' }) - if (result.status !== 0) { - throw new Error(`git ${args.join(' ')} failed (${result.status})\n${result.stderr}`) - } - return result.stdout.trim() -} - -export function initializeBranchCleanupRemote(fixtureRoot, repoPath) { - const remotePath = path.join(fixtureRoot, 'remote.git') - runGit(['init', '--bare', remotePath], fixtureRoot) - runGit(['symbolic-ref', 'HEAD', 'refs/heads/main'], remotePath) - runGit(['remote', 'add', 'origin', remotePath], repoPath) - runGit(['push', 'origin', 'main'], repoPath) -} - -export function seedBranchCleanupRepro(repoPath, worktreePath) { - runGit( - ['commit', '--allow-empty', '-m', 'Trigger safe branch cleanup', '--no-gpg-sign'], - worktreePath - ) - const branch = runGit(['symbolic-ref', '--short', 'HEAD'], worktreePath) - runGit(['config', `branch.${branch}.base`, 'refs/remotes/origin/main'], repoPath) -} - -export async function startDelayedFetchServer(delayMs) { - const sockets = new Set() - const timers = new Set() - const server = net.createServer((socket) => { - sockets.add(socket) - const timer = setTimeout(() => { - timers.delete(timer) - socket.end('HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n') - }, delayMs) - timers.add(timer) - socket.on('close', () => { - clearTimeout(timer) - timers.delete(timer) - sockets.delete(socket) - }) - socket.on('error', () => undefined) - }) - await new Promise((resolve, reject) => { - server.once('error', reject) - server.listen(0, '127.0.0.1', resolve) - }) - const address = server.address() - if (!address || typeof address === 'string') { - throw new Error('Delayed fetch server did not expose a TCP port') - } - return { - url: `http://127.0.0.1:${address.port}/remote.git`, - close: () => - new Promise((resolve) => { - for (const timer of timers) { - clearTimeout(timer) - } - timers.clear() - for (const socket of sockets) { - socket.destroy() - } - server.close(resolve) - }) - } -} diff --git a/tests/tools/benchmarks/worktree-deletion-dev-bench.mjs b/tests/tools/benchmarks/worktree-deletion-dev-bench.mjs deleted file mode 100644 index 17ac22e3fe1..00000000000 --- a/tests/tools/benchmarks/worktree-deletion-dev-bench.mjs +++ /dev/null @@ -1,631 +0,0 @@ -#!/usr/bin/env node -/** - * A/B benchmark for worktree deletion against real Orca dev instances. - * - * Usage: - * pnpm bench:worktree-deletion -- --instance baseline=/path/to/main \ - * --instance candidate=. --iterations 3 --history-files 10000 - */ -import { spawn, spawnSync } from 'node:child_process' -import { createHash } from 'node:crypto' -import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from 'node:fs' -import { rm } from 'node:fs/promises' -import net from 'node:net' -import os from 'node:os' -import path from 'node:path' -// Use declared @playwright/test’s re-export instead of relying on a hoisted transitive playwright install. -import { chromium } from '@playwright/test' -import * as branchFixture from './worktree-deletion-branch-fixture.mjs' -import { round, summarize } from './worktree-deletion-timing-samples.mjs' - -const DEFAULT_ITERATIONS = 3 -const DEFAULT_HISTORY_FILES = 10_000 -const DEFAULT_FETCH_DELAY_MS = 1_500 -const CDP_START_PORT = 9_700 -const START_TIMEOUT_MS = 180_000 -const IPC_TIMEOUT_MS = 90_000 -const resultsRoot = path.resolve(import.meta.dirname, 'results') - -function parseArgs(argv) { - const options = { - instances: [], - iterations: DEFAULT_ITERATIONS, - historyFiles: DEFAULT_HISTORY_FILES, - fetchDelayMs: DEFAULT_FETCH_DELAY_MS, - keepFixture: false - } - for (let index = 2; index < argv.length; index += 1) { - const value = argv[index] - if (value === '--') { - continue - } - if (value === '--help' || value === '-h') { - printHelp() - process.exit(0) - } - if (value === '--keep-fixture') { - options.keepFixture = true - continue - } - const next = () => { - const result = argv[++index] - if (!result) { - throw new Error(`${value} needs a value`) - } - return result - } - if (value === '--instance') { - const entry = next() - const separator = entry.indexOf('=') - if (separator <= 0) { - throw new Error('--instance must use label=/absolute/or/relative/path') - } - options.instances.push({ - label: entry.slice(0, separator), - repoRoot: path.resolve(entry.slice(separator + 1)) - }) - } else if (value === '--iterations') { - options.iterations = readPositiveInteger(value, next()) - } else if (value === '--history-files') { - options.historyFiles = readPositiveInteger(value, next()) - } else if (value === '--fetch-delay-ms') { - options.fetchDelayMs = readPositiveInteger(value, next()) - } else { - throw new Error(`Unknown argument: ${value}`) - } - } - if (options.instances.length === 0) { - options.instances.push({ label: 'candidate', repoRoot: process.cwd() }) - } - return options -} - -function readPositiveInteger(flag, value) { - const number = Number(value) - if (!Number.isInteger(number) || number < 1) { - throw new Error(`${flag} must be a positive integer`) - } - return number -} - -function printHelp() { - console.log(`Usage: - pnpm bench:worktree-deletion -- [options] - -Options: - --instance Dev checkout to launch; repeat for A/B comparison - --iterations Deletions per instance (default: ${DEFAULT_ITERATIONS}) - --history-files Files seeded in each worktree history (default: ${DEFAULT_HISTORY_FILES}) - --fetch-delay-ms Slow-remote delay for branch cleanup (default: ${DEFAULT_FETCH_DELAY_MS}) - --keep-fixture Keep disposable profiles and repos for inspection`) -} - -function run(command, args, cwd) { - const result = spawnSync(command, args, { cwd, encoding: 'utf8' }) - if (result.status !== 0) { - throw new Error( - `${command} ${args.join(' ')} failed (${result.status})\n${result.stderr || result.stdout}` - ) - } -} - -function createFixture(instanceLabel) { - const root = mkdtempSync(path.join(os.tmpdir(), `orca-delete-bench-${instanceLabel}-`)) - const repoPath = path.join(root, 'repo') - const userDataPath = path.join(root, 'user-data') - mkdirSync(repoPath) - mkdirSync(userDataPath) - // Git 2.25 lacks `git init --initial-branch`; rename after the first commit below. - run('git', ['init'], repoPath) - run('git', ['config', 'user.email', 'worktree-delete-bench@orca.invalid'], repoPath) - run('git', ['config', 'user.name', 'Orca Worktree Delete Bench'], repoPath) - writeFileSync(path.join(repoPath, 'README.md'), '# Orca worktree deletion benchmark\n') - run('git', ['add', 'README.md'], repoPath) - run('git', ['commit', '-m', 'Initialize benchmark fixture', '--no-gpg-sign'], repoPath) - run('git', ['branch', '-m', 'main'], repoPath) - branchFixture.initializeBranchCleanupRemote(root, repoPath) - return { root, repoPath, userDataPath } -} - -function launchDevInstance({ label, repoRoot }, fixture, port) { - if (!existsSync(path.join(repoRoot, 'package.json'))) { - throw new Error(`${label}: package.json not found under ${repoRoot}`) - } - const env = { - ...process.env, - ORCA_DEV_USER_DATA_PATH: fixture.userDataPath, - REMOTE_DEBUGGING_PORT: String(port) - } - delete env.ELECTRON_RUN_AS_NODE - const child = spawn( - process.execPath, - ['config/scripts/development/launch/run-electron-vite-dev.mjs'], - { - cwd: repoRoot, - env, - stdio: ['ignore', 'pipe', 'pipe'], - // Why: own process group so teardown can signal the Electron/Vite grandchildren, not just the launcher. - detached: process.platform !== 'win32', - windowsHide: true - } - ) - const log = { stdout: '', stderr: '' } - child.stdout?.on('data', (chunk) => { - log.stdout = appendLog(log.stdout, chunk) - }) - child.stderr?.on('data', (chunk) => { - log.stderr = appendLog(log.stderr, chunk) - }) - return { child, endpoint: `http://127.0.0.1:${port}`, label, log } -} - -function appendLog(current, chunk) { - return `${current}${String(chunk)}`.slice(-30_000) -} - -async function connectToOrca(instance) { - const deadline = Date.now() + START_TIMEOUT_MS - let lastError = null - while (Date.now() < deadline) { - if (instance.child.exitCode !== null) { - throw new Error( - `${instance.label}: dev process exited (${instance.child.exitCode})\n${instance.log.stderr}` - ) - } - try { - const browser = await chromium.connectOverCDP(instance.endpoint) - try { - // Wait for __store before accepting CDP; findOrcaPage retries otherwise leak browser handles. - const page = await findOrcaPage(browser) - return { browser, page } - } catch (error) { - await browser.close().catch(() => undefined) - throw error - } - } catch (error) { - lastError = error - await delay(500) - } - } - throw new Error( - `${instance.label}: CDP did not become ready: ${String(lastError)}\n${instance.log.stderr}` - ) -} - -async function findOrcaPage(browser) { - const deadline = Date.now() + 30_000 - while (Date.now() < deadline) { - for (const context of browser.contexts()) { - for (const page of context.pages()) { - const ready = await page - .evaluate(() => Boolean(window.__store && window.api?.repos?.list)) - .catch(() => false) - if (ready) { - return page - } - } - } - await delay(250) - } - throw new Error('Orca renderer with window.__store was not found') -} - -async function addFixtureRepo(page, repoPath) { - const addedRepo = await page.evaluate(async (fixturePath) => { - const store = window.__store - if (!store) { - throw new Error('window.__store is unavailable') - } - const repo = await store.getState().addRepoPath(fixturePath) - if (!repo) { - throw new Error(`Could not add fixture repo ${fixturePath}`) - } - return { id: repo.id, path: repo.path } - }, repoPath) - return page.evaluate( - async ({ fixtureRepoId, fixturePath }) => { - const store = window.__store - if (!store) { - throw new Error('window.__store is unavailable') - } - let rootWorktree - for (let attempt = 0; attempt < 60 && !rootWorktree; attempt += 1) { - await store.getState().fetchWorktrees(fixtureRepoId) - await new Promise((resolve) => window.setTimeout(resolve, 250)) - rootWorktree = store - .getState() - .worktreesByRepo[fixtureRepoId]?.find((worktree) => worktree.path === fixturePath) - } - if (!rootWorktree) { - throw new Error('Fixture root worktree did not load') - } - const state = store.getState() - state.setSidebarOpen(true) - state.setShowActiveOnly(false) - state.setShowSleepingWorkspaces(true) - state.setHideDefaultBranchWorkspace(false) - state.setFilterRepoIds([]) - state.setActiveRepo(fixtureRepoId) - state.setActiveWorktree(rootWorktree.id) - return { repoId: fixtureRepoId, rootWorktreeId: rootWorktree.id } - }, - { fixtureRepoId: addedRepo.id, fixturePath: addedRepo.path } - ) -} - -async function createMeasuredWorktree(page, repoId, iteration) { - return page.evaluate( - async ({ fixtureRepoId, sequence }) => { - const store = window.__store - if (!store) { - throw new Error('window.__store is unavailable') - } - const result = await store - .getState() - .createWorktree(fixtureRepoId, `delete-bench-${sequence}-${Date.now()}`) - await store.getState().fetchWorktrees(fixtureRepoId) - const state = store.getState() - const tab = state.createTab(result.worktree.id) - state.createBrowserTab(result.worktree.id, 'about:blank', { - title: 'worktree deletion regression probe', - activate: false - }) - state.setActiveView('terminal') - state.setActiveWorktree(result.worktree.id) - state.setActiveTab(tab.id) - state.setRightSidebarTab('explorer') - state.setRightSidebarOpen(true) - state.revealWorktreeInSidebar(result.worktree.id, { behavior: 'auto' }) - // Let the terminal and explorer install their real PTY/watcher resources before deletion. - await new Promise((resolve) => window.setTimeout(resolve, 500)) - return { id: result.worktree.id, path: result.worktree.path } - }, - { fixtureRepoId: repoId, sequence: iteration } - ) -} - -function seedTerminalHistory(userDataPath, worktreeId, fileCount) { - const hash = createHash('sha256').update(worktreeId).digest('hex').slice(0, 16) - const historyPath = path.join(userDataPath, 'terminal-history', hash) - const payload = 'worktree deletion benchmark history\n'.repeat(4) - for (let index = 0; index < fileCount; index += 1) { - const bucket = path.join(historyPath, String(Math.floor(index / 250))) - if (index % 250 === 0) { - mkdirSync(bucket, { recursive: true }) - } - writeFileSync(path.join(bucket, `${index}.history`), payload) - } - return historyPath -} - -async function assertWorktreeRowVisible(page, worktreeId) { - await page.waitForFunction( - (id) => - [...document.querySelectorAll('[data-worktree-id]')].some( - (element) => element.getAttribute('data-worktree-id') === id - ), - worktreeId - ) -} - -async function measureDeletion(page, worktreeId, rootWorktreeId) { - return page.evaluate( - async ({ targetId, fallbackId, ipcTimeoutMs }) => { - const store = window.__store - if (!store) { - throw new Error('window.__store is unavailable') - } - store.getState().setActiveWorktree(fallbackId) - let settled = false - let failure = null - const startedAt = performance.now() - const deletion = store - .getState() - .removeWorktree(targetId, true) - .then((result) => { - settled = true - return result - }) - .catch((error) => { - // Surface rejected delete before polling so the regression cannot become an infinite wait. - settled = true - failure = error - return null - }) - const ipcSamplesMs = [] - let maxRendererTimerDriftMs = 0 - let expectedTimerAt = performance.now() + 10 - while (!settled) { - await new Promise((resolve) => window.setTimeout(resolve, 10)) - const now = performance.now() - maxRendererTimerDriftMs = Math.max(maxRendererTimerDriftMs, now - expectedTimerAt) - const ipcStartedAt = performance.now() - await Promise.race([ - window.api.repos.list(), - new Promise((_, reject) => - window.setTimeout(() => reject(new Error('repos:list IPC timed out')), ipcTimeoutMs) - ) - ]) - ipcSamplesMs.push(performance.now() - ipcStartedAt) - // Re-read clock after IPC so its separately recorded round-trip does not inflate the next drift sample. - expectedTimerAt = performance.now() + 10 - } - const result = await deletion - if (failure) { - throw new Error(`removeWorktree rejected: ${String(failure)}`) - } - const postDeleteIpcStartedAt = performance.now() - await window.api.repos.list() - const finalState = store.getState() - return { - result, - totalMs: performance.now() - startedAt, - ipcSamplesMs, - postDeleteIpcMs: performance.now() - postDeleteIpcStartedAt, - maxRendererTimerDriftMs, - worktreeStillInStore: Object.values(finalState.worktreesByRepo) - .flat() - .some((worktree) => worktree.id === targetId), - residualTabCount: finalState.tabsByWorktree[targetId]?.length ?? 0, - residualBrowserTabCount: finalState.browserTabsByWorktree[targetId]?.length ?? 0, - residualOpenFileCount: finalState.openFiles.filter((file) => file.worktreeId === targetId) - .length - } - }, - { targetId: worktreeId, fallbackId: rootWorktreeId, ipcTimeoutMs: IPC_TIMEOUT_MS } - ) -} - -async function verifyDeletion(page, worktree, historyPath, measurement) { - if (!measurement.result?.ok) { - throw new Error(`removeWorktree failed: ${measurement.result?.error ?? 'unknown error'}`) - } - if (measurement.worktreeStillInStore) { - throw new Error('Deleted worktree remains in the renderer store') - } - if ( - measurement.residualTabCount || - measurement.residualBrowserTabCount || - measurement.residualOpenFileCount - ) { - throw new Error( - `Deleted worktree retained UI state: ${JSON.stringify({ - tabs: measurement.residualTabCount, - browserTabs: measurement.residualBrowserTabCount, - openFiles: measurement.residualOpenFileCount - })}` - ) - } - if (existsSync(worktree.path)) { - throw new Error(`Deleted worktree directory remains at ${worktree.path}`) - } - if (existsSync(historyPath)) { - throw new Error(`Live terminal history remains at ${historyPath}`) - } - await page.waitForFunction( - (id) => - ![...document.querySelectorAll('[data-worktree-id]')].some( - (element) => element.getAttribute('data-worktree-id') === id - ), - worktree.id - ) -} - -async function runIteration(page, fixture, repoState, iteration, historyFiles) { - const worktree = await createMeasuredWorktree(page, repoState.repoId, iteration) - await assertWorktreeRowVisible(page, worktree.id) - branchFixture.seedBranchCleanupRepro(fixture.repoPath, worktree.path) - const historyPath = seedTerminalHistory(fixture.userDataPath, worktree.id, historyFiles) - const measurement = await measureDeletion(page, worktree.id, repoState.rootWorktreeId) - await verifyDeletion(page, worktree, historyPath, measurement) - return { - iteration, - totalMs: round(measurement.totalMs), - ipcLatencyMs: summarize(measurement.ipcSamplesMs), - postDeleteIpcMs: round(measurement.postDeleteIpcMs), - maxRendererTimerDriftMs: round(measurement.maxRendererTimerDriftMs) - } -} - -async function verifyRestart(instanceConfig, fixture, port, repoId) { - const relaunched = launchDevInstance(instanceConfig, fixture, port) - let browser = null - try { - const connection = await connectToOrca(relaunched) - browser = connection.browser - const { page } = connection - const state = await page.evaluate(async (fixtureRepoId) => { - const store = window.__store - if (!store) { - throw new Error('window.__store is unavailable after restart') - } - await window.api.repos.list() - let recovery = { repoPresent: false, worktreeCount: 0 } - for (let attempt = 0; attempt < 60; attempt += 1) { - const owner = store.getState().settings?.activeRuntimeEnvironmentId ?? null - await store.getState().fetchRepos({ runtimeEnvironmentId: owner }) - if (store.getState().repos.some((repo) => repo.id === fixtureRepoId)) { - await store.getState().fetchWorktrees(fixtureRepoId) - } - await new Promise((resolve) => window.setTimeout(resolve, 250)) - recovery = { - repoPresent: store.getState().repos.some((repo) => repo.id === fixtureRepoId), - worktreeCount: store.getState().worktreesByRepo[fixtureRepoId]?.length ?? 0 - } - if (recovery.repoPresent && recovery.worktreeCount > 0) { - break - } - } - return recovery - }, repoId) - if (!state.repoPresent || state.worktreeCount < 1) { - throw new Error(`Fixture repo did not recover after restart: ${JSON.stringify(state)}`) - } - await page.evaluate(async (fixtureRepoId) => { - await window.__store?.getState().removeProject(fixtureRepoId) - }, repoId) - } finally { - await browser?.close().catch(() => undefined) - await stopDevInstance(relaunched) - } -} - -async function benchmarkInstance(instanceConfig, index, options) { - run( - process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm', - ['run', 'ensure:electron-runtime'], - instanceConfig.repoRoot - ) - const fixture = createFixture(instanceConfig.label) - let delayedFetchServer, instance, browser - try { - delayedFetchServer = await branchFixture.startDelayedFetchServer(options.fetchDelayMs) - run('git', ['remote', 'set-url', 'origin', delayedFetchServer.url], fixture.repoPath) - const port = await findAvailablePort(CDP_START_PORT + index) - instance = launchDevInstance(instanceConfig, fixture, port) - const { browser: connectedBrowser, page } = await connectToOrca(instance) - browser = connectedBrowser - const repoState = await addFixtureRepo(page, fixture.repoPath) - const iterations = [] - for (let iteration = 1; iteration <= options.iterations; iteration += 1) { - const result = await runIteration(page, fixture, repoState, iteration, options.historyFiles) - iterations.push(result) - console.log( - `[${instanceConfig.label}] ${iteration}/${options.iterations}: ${result.totalMs}ms total, ` + - `${result.ipcLatencyMs.max}ms max main IPC` - ) - } - await browser.close() - browser = null - await stopDevInstance(instance) - // Use fresh CDP ports so teardown cannot leave retries attaching to a dying endpoint. - await verifyRestart( - instanceConfig, - fixture, - await findAvailablePort(port + 1), - repoState.repoId - ) - return { - label: instanceConfig.label, - repoRoot: instanceConfig.repoRoot, - historyFiles: options.historyFiles, - fetchDelayMs: options.fetchDelayMs, - iterations, - summary: summarize(iterations.map((entry) => entry.totalMs)), - ipcSummary: summarize(iterations.flatMap((entry) => entry.ipcLatencyMs.samples)), - restartPassed: true, - fixtureRoot: options.keepFixture ? fixture.root : undefined - } - } finally { - await browser?.close().catch(() => undefined) - if (instance) { - await stopDevInstance(instance) - } - await delayedFetchServer?.close() - if (!options.keepFixture) { - await rm(fixture.root, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 250 - }) - } - } -} - -async function findAvailablePort(startPort) { - for (let port = startPort; port < startPort + 100; port += 1) { - if (await isPortAvailable(port)) { - return port - } - } - throw new Error(`No free CDP port found in ${startPort}-${startPort + 99}`) -} - -function isPortAvailable(port) { - return new Promise((resolve) => { - const server = net.createServer() - server.once('error', () => resolve(false)) - server.once('listening', () => server.close(() => resolve(true))) - server.listen(port, '127.0.0.1') - }) -} - -async function stopDevInstance(instance) { - // Check signalCode too: signal-exited children retain null exitCode and otherwise burn another 8s kill race. - if (instance.child.exitCode !== null || instance.child.signalCode !== null) { - return - } - if (process.platform === 'win32') { - spawnSync('taskkill.exe', ['/PID', String(instance.child.pid), '/T', '/F'], { stdio: 'ignore' }) - return - } - killDevInstanceTree(instance.child, 'SIGINT') - const exited = await Promise.race([ - new Promise((resolve) => instance.child.once('exit', () => resolve(true))), - delay(8_000).then(() => false) - ]) - if (!exited) { - killDevInstanceTree(instance.child, 'SIGKILL') - } -} - -/** POSIX: signal the whole group, or the launcher's Electron/Vite grandchildren survive and keep the - * CDP port and fixture directory busy. */ -function killDevInstanceTree(child, signal) { - try { - process.kill(-child.pid, signal) - } catch { - child.kill(signal) - } -} - -function delay(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)) -} - -function printComparison(results) { - console.log('\nWorktree deletion results') - console.table( - results.map((result) => ({ - instance: result.label, - 'delete median ms': result.summary.median, - 'delete p95 ms': result.summary.p95, - 'main IPC p95 ms': result.ipcSummary.p95, - 'main IPC max ms': result.ipcSummary.max, - restart: result.restartPassed ? 'pass' : 'fail' - })) - ) - if (results.length === 2) { - const [baseline, candidate] = results - const percent = round( - ((baseline.summary.median - candidate.summary.median) / baseline.summary.median) * 100 - ) - console.log( - `${candidate.label} median deletion is ${Math.abs(percent)}% ${ - percent >= 0 ? 'faster' : 'slower' - } than ${baseline.label}.` - ) - } -} - -const options = parseArgs(process.argv) -mkdirSync(resultsRoot, { recursive: true }) -const results = [] -for (const [index, instance] of options.instances.entries()) { - results.push(await benchmarkInstance(instance, index, options)) -} -const artifact = { - benchmark: 'worktree-deletion-dev', - createdAt: new Date().toISOString(), - platform: process.platform, - arch: process.arch, - results -} -const artifactPath = path.join( - resultsRoot, - `worktree-deletion-${new Date().toISOString().replaceAll(/[:.]/g, '-')}.json` -) -writeFileSync(artifactPath, `${JSON.stringify(artifact, null, 2)}\n`) -printComparison(results) -console.log(`Artifact: ${artifactPath}`) diff --git a/tests/tools/benchmarks/worktree-deletion-timing-samples.mjs b/tests/tools/benchmarks/worktree-deletion-timing-samples.mjs deleted file mode 100644 index 5f4c6d88d5a..00000000000 --- a/tests/tools/benchmarks/worktree-deletion-timing-samples.mjs +++ /dev/null @@ -1,21 +0,0 @@ -export function summarize(values) { - const samples = values.map((value) => round(value)).sort((a, b) => a - b) - if (samples.length === 0) { - return { count: 0, median: 0, p95: 0, max: 0, samples } - } - return { - count: samples.length, - median: percentile(samples, 0.5), - p95: percentile(samples, 0.95), - max: samples.at(-1), - samples - } -} - -function percentile(sorted, fraction) { - return sorted[Math.min(sorted.length - 1, Math.ceil(sorted.length * fraction) - 1)] -} - -export function round(value) { - return Math.round(value * 100) / 100 -} diff --git a/tests/tools/omp-child-history-rendered/README.md b/tests/tools/omp-child-history-rendered/README.md index 52cd99a8371..dde1c3f0d1d 100644 --- a/tests/tools/omp-child-history-rendered/README.md +++ b/tests/tools/omp-child-history-rendered/README.md @@ -8,8 +8,3 @@ It verifies parent-row measurement avoids overlap, indentation stops growing, sc away/back restores expansion, and collapse removes descendants. CDP screenshots and native hidden/unfocused window assertions are saved in `.bench-fixtures`. This exercises production rendering and callbacks, not the complete terminal launch UI. - -Run `ORCA_BACKGROUND_LAUNCH=1 bun tests/tools/omp-child-session-resume-smoke.mjs /path/to/oh-my-pi` -for a zero-model-call check against real OMP session storage and CLI parsing. The smoke -builds Orca's path-based resume command, creates parent, child and grandchild transcripts, -and verifies OMP selects each descendant's distinct identity in a folder workspace. diff --git a/tests/tools/omp-child-session-resume-smoke.mjs b/tests/tools/omp-child-session-resume-smoke.mjs deleted file mode 100644 index f39bf2968d3..00000000000 --- a/tests/tools/omp-child-session-resume-smoke.mjs +++ /dev/null @@ -1,82 +0,0 @@ -// Bun; argv[2] is a read-only OMP checkout. No model requests. -import assert from 'node:assert/strict' -import { mkdtemp, mkdir, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { buildAiVaultResumeCommand } from '../../src/shared/ai-vault/resume/ai-vault-resume-command.ts' -import { tokenizeStartupCommand } from '../../src/shared/agent-launch/startup/tui-agent-startup-shell.ts' - -assert.ok(process.argv[2], 'Pass a read-only OMP checkout path') -const scratch = await mkdtemp(join(tmpdir(), 'orca-omp-child-resume-')) -process.env.HOME = join(scratch, 'home') -process.env.USERPROFILE = process.env.HOME -process.env.XDG_CONFIG_HOME = join(scratch, 'config') -process.env.XDG_DATA_HOME = join(scratch, 'data') -process.env.XDG_STATE_HOME = join(scratch, 'state') -process.env.PI_CODING_AGENT_DIR = join(scratch, 'agent') -delete process.env.OMP_CODING_AGENT_DIR -delete process.env.PI_CONFIG_DIR -delete process.env.OMP_PROFILE -delete process.env.PI_PROFILE -delete process.env.PI_CONFIG_FILES -const source = (name) => - pathToFileURL(join(resolve(process.argv[2]), 'packages/coding-agent/src', name)).href -const managers = [] -try { - await mkdir(process.env.HOME, { recursive: true }) - const { SessionManager } = await import(source('session/session-manager.ts')) - const { Settings } = await import(source('config/settings.ts')) - const { createSessionManager } = await import(source('main.ts')) - const { parseArgs } = await import(source('cli/args.ts')) - const cwd = join(scratch, 'folder workspace') - await mkdir(cwd) - const parent = SessionManager.create(cwd) - managers.push(parent) - parent.appendMessage({ role: 'user', content: 'coordinate work', timestamp: Date.now() }) - await parent.ensureOnDisk() - await parent.flush() - const child = SessionManager.create(cwd, parent.getSessionFile().replace(/\.jsonl$/, '')) - managers.push(child) - child.appendMessage({ role: 'user', content: 'child task', timestamp: Date.now() }) - await child.ensureOnDisk() - await child.flush() - const grandchild = SessionManager.create(cwd, child.getSessionFile().replace(/\.jsonl$/, '')) - managers.push(grandchild) - grandchild.appendMessage({ role: 'user', content: 'grandchild research', timestamp: Date.now() }) - await grandchild.ensureOnDisk() - await grandchild.flush() - for (const target of [child, grandchild]) { - const command = buildAiVaultResumeCommand({ - agent: 'omp', - sessionId: target.getSessionId(), - resumeFilePath: target.getSessionFile(), - cwd: null, - platform: process.platform, - shell: 'posix' - }) - const tokens = tokenizeStartupCommand(command, 'posix') - assert.ok(tokens.ok) - const args = parseArgs(tokens.tokens.slice(1)) - assert.equal(args.resume, target.getSessionFile()) - const settings = await Settings.init({ cwd }) - const resumed = await createSessionManager(args, cwd, settings) - managers.push(resumed) - assert.equal(resumed.getSessionId(), target.getSessionId()) - assert.notEqual(resumed.getSessionId(), parent.getSessionId()) - } - console.log( - JSON.stringify({ - childPathResumed: true, - grandchildPathResumed: true, - distinctFromParent: true, - folderWorkspace: true, - modelCalls: 0 - }) - ) -} finally { - for (const manager of managers) { - await manager?.close() - } - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/omp-child-session-runtime-smoke.mjs b/tests/tools/omp-child-session-runtime-smoke.mjs deleted file mode 100644 index 3f09904bdcb..00000000000 --- a/tests/tools/omp-child-session-runtime-smoke.mjs +++ /dev/null @@ -1,146 +0,0 @@ -// Run with Bun and a read-only OMP checkout path as the first argument. -import assert from 'node:assert/strict' -import { mkdtemp, writeFile, rm } from 'node:fs/promises' -import { createServer } from 'node:http' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { build } from 'esbuild' - -const reference = process.argv[2] -assert.ok(reference, 'Pass the read-only oh-my-pi source checkout path') -const source = (path) => - pathToFileURL(join(resolve(reference), 'packages/coding-agent/src', path)).href -const { loadExtensions } = await import(source('extensibility/extensions/loader.ts')) -const { ExtensionRunner } = await import(source('extensibility/extensions/runner.ts')) -const { EventBus } = await import(source('utils/event-bus.ts')) -const { SessionManager } = await import(source('session/session-manager.ts')) -const scratch = await mkdtemp(join(tmpdir(), 'orca-omp-child-status-')) -const posts = [] -const errors = [] -const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) -const server = createServer(async (request, response) => { - try { - let body = '' - for await (const chunk of request) { - body += chunk - } - posts.push(JSON.parse(body).payload) - response.writeHead(200).end() - } catch (error) { - errors.push(error) - response.writeHead(500).end() - } -}) -await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) -try { - await build({ - entryPoints: ['src/main/pi/agent-status-extension-source.ts'], - bundle: true, - platform: 'node', - format: 'esm', - outfile: join(scratch, 'generator.mjs') - }) - const { getPiAgentStatusExtensionSource } = await import( - pathToFileURL(join(scratch, 'generator.mjs')).href - ) - const extensionPath = join(scratch, 'orca-agent-status.ts') - await writeFile(extensionPath, getPiAgentStatusExtensionSource('omp')) - process.env.ORCA_PANE_KEY = 'test-parent-pane' - process.env.ORCA_AGENT_LAUNCH_TOKEN = 'test-parent-launch' - process.env.ORCA_AGENT_HOOK_PORT = String(server.address().port) - process.env.ORCA_AGENT_HOOK_TOKEN = 'test-token' - delete process.env.ORCA_AGENT_HOOK_ENDPOINT - delete process.env.ORCA_PI_STATUS_OWNED - const load = async (manager) => { - const result = await loadExtensions([extensionPath], scratch, new EventBus()) - assert.deepEqual(result.errors, []) - assert.equal(result.extensions.length, 1) - // These lifecycle handlers never query models or invoke agent actions. - const runner = new ExtensionRunner(result.extensions, result.runtime, scratch, manager, {}) - runner.onError((error) => errors.push(error)) - return runner - } - const emit = async (runner, type, expectedCount) => { - await runner.emit({ type }) - if (expectedCount !== undefined) { - const deadline = Date.now() + 2000 - while (posts.length < expectedCount && errors.length === 0 && Date.now() < deadline) { - await delay(10) - } - assert.equal(posts.length, expectedCount, `HTTP delivery after ${type}`) - } - assert.deepEqual(errors, []) - } - const assertQuiet = async (expectedCount) => { - // Suppressed events have no completion callback; allow local HTTP dispatch to settle. - await delay(100) - assert.deepEqual(errors, []) - assert.equal(posts.length, expectedCount, 'child lifecycle must not post pane status') - } - const rootManager = SessionManager.create(scratch, join(scratch, 'sessions')) - const childManager = SessionManager.inMemory(scratch) - const root = await load(rootManager) - await emit(root, 'session_start') - await emit(root, 'agent_start', 1) - const child = await load(childManager) - assert.notEqual(root, child) - assert.notEqual(rootManager, childManager) - await emit(child, 'session_start') - await emit(child, 'agent_start') - await emit(child, 'agent_end') - await assertQuiet(1) - await emit(root, 'agent_end', 2) - await writeFile(extensionPath, `${getPiAgentStatusExtensionSource('omp')}\n// Reloaded module\n`) - const reloaded = await load(rootManager) - await emit(reloaded, 'session_start') - const previousId = rootManager.getSessionId() - await rootManager.newSession() - assert.notEqual(rootManager.getSessionId(), previousId) - await emit(reloaded, 'agent_start', 3) - await emit(reloaded, 'agent_end', 4) - assert.equal(posts.at(-1).session_id, rootManager.getSessionId()) - - const resumedPath = join(scratch, 'resume-target.jsonl') - await writeFile( - resumedPath, - `${JSON.stringify({ - type: 'session', - version: 3, - id: 'runtime-resume-target', - timestamp: new Date().toISOString(), - cwd: scratch - })}\n` - ) - // Resume/reopen the transcript through the same manager. - for (const expectedCount of [5, 7]) { - await rootManager.setSessionFile(resumedPath) - assert.equal(rootManager.getSessionId(), 'runtime-resume-target') - await emit(reloaded, 'session_switch') - await emit(reloaded, 'agent_start', expectedCount) - await emit(reloaded, 'agent_end', expectedCount + 1) - assert.equal(posts.at(-1).session_id, 'runtime-resume-target') - } - await emit(reloaded, 'session_shutdown') - await emit(child, 'agent_start') - await emit(child, 'agent_end') - await emit(child, 'session_shutdown') - await assertQuiet(8) - assert.deepEqual( - posts.map((post) => post.hook_event_name), - Array.from({ length: 4 }, () => ['agent_start', 'agent_end']).flat() - ) - console.log( - JSON.stringify({ - platform: process.platform, - posts: posts.map((post) => post.hook_event_name), - distinctManagers: rootManager !== childManager, - scope: - 'Actual OMP loader, ExtensionRunner and SessionManager; controlled lifecycle order; real native HTTP' - }) - ) -} finally { - server.closeAllConnections() - await new Promise((resolve) => server.close(resolve)) - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/omp-fresh-session-runtime-smoke.mjs b/tests/tools/omp-fresh-session-runtime-smoke.mjs deleted file mode 100644 index 24488bcf4ca..00000000000 --- a/tests/tools/omp-fresh-session-runtime-smoke.mjs +++ /dev/null @@ -1,75 +0,0 @@ -// Bun, with a read-only OMP source checkout as argv[2]. No model requests. -import assert from 'node:assert/strict' -import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { OMP_FRESH_CONFIG_SOURCE } from '../../src/shared/agent-launch/startup/omp-fresh-launch.ts' - -assert.ok(process.argv[2], 'Pass a read-only OMP checkout path') -const scratch = await mkdtemp(join(tmpdir(), 'orca-omp-fresh-proof-')) -process.env.HOME = join(scratch, 'home') -process.env.USERPROFILE = process.env.HOME -process.env.XDG_CONFIG_HOME = join(scratch, 'xdg-config') -process.env.XDG_DATA_HOME = join(scratch, 'xdg-data') -process.env.XDG_STATE_HOME = join(scratch, 'xdg-state') -process.env.OMP_CODING_AGENT_DIR = join(scratch, 'agent') -delete process.env.PI_CONFIG_FILES -const source = (path) => - pathToFileURL(join(resolve(process.argv[2]), 'packages/coding-agent/src', path)).href -const managers = [] -try { - await mkdir(process.env.HOME, { recursive: true }) - const { SessionManager } = await import(source('session/session-manager.ts')) - const { Settings, resetSettingsForTest } = await import(source('config/settings.ts')) - const { createSessionManager } = await import(source('main.ts')) - const cwd = join(scratch, 'project') - await mkdir(cwd) - const previous = SessionManager.create(cwd) - managers.push(previous) - previous.appendMessage({ role: 'user', content: 'previous task', timestamp: Date.now() }) - await previous.ensureOnDisk() - await previous.flush() - const config = join(scratch, 'fresh.yml') - const userConfig = join(scratch, 'user.yml') - await writeFile(config, OMP_FRESH_CONFIG_SOURCE) - await writeFile(userConfig, 'autoResume: true\n') - const settings = await Settings.init({ cwd, configFiles: [userConfig] }) - const resumed = await createSessionManager({}, cwd, settings) - managers.push(resumed) - assert.equal(resumed.getSessionId(), previous.getSessionId()) - resetSettingsForTest() - const freshSettings = await Settings.init({ cwd, configFiles: [userConfig, config] }) - assert.equal(freshSettings.get('autoResume'), false) - assert.equal(settings.get('autoResume'), true) - const defaultSelection = await createSessionManager({}, cwd, freshSettings) - assert.equal(defaultSelection, undefined, 'SDK creates a fresh session after undefined selection') - const fresh = SessionManager.create(cwd) - managers.push(fresh) - assert.notEqual(fresh.getSessionId(), previous.getSessionId()) - assert.equal(fresh.getSessionDir(), previous.getSessionDir()) - const explicit = await createSessionManager( - { resume: previous.getSessionFile() }, - cwd, - freshSettings - ) - managers.push(explicit) - assert.equal(explicit.getSessionId(), previous.getSessionId()) - console.log( - JSON.stringify({ - platform: process.platform, - autoResumeReproduced: true, - freshSelected: true, - storageDirectoryPreserved: true, - explicitResumePreserved: true, - modelCalls: 0, - scope: - 'Actual OMP Settings overlays, persistent SessionManager and createSessionManager; no rendered UI' - }) - ) -} finally { - for (const manager of managers) { - await manager?.close() - } - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/omp-history-title-smoke.mjs b/tests/tools/omp-history-title-smoke.mjs deleted file mode 100644 index 21c4a088347..00000000000 --- a/tests/tools/omp-history-title-smoke.mjs +++ /dev/null @@ -1,92 +0,0 @@ -import assert from 'node:assert/strict' -import { mkdtemp, mkdir, rm, stat } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { fileURLToPath, pathToFileURL } from 'node:url' -assert.ok(process.argv[2], 'Pass a read-only OMP checkout') -const orcaRoot = fileURLToPath(new URL('../../', import.meta.url)) -const scratch = await mkdtemp(join(tmpdir(), 'orca-omp-history-title-')) -process.env.HOME = join(scratch, 'home') -process.env.USERPROFILE = process.env.HOME -for (const [key, value] of Object.entries({ - XDG_CONFIG_HOME: 'config', - XDG_DATA_HOME: 'data', - XDG_STATE_HOME: 'state', - XDG_CACHE_HOME: 'cache' -})) { - process.env[key] = join(scratch, value) -} -for (const key of [ - 'OMP_CODING_AGENT_DIR', - 'PI_CODING_AGENT_DIR', - 'OMP_PROFILE', - 'PI_PROFILE', - 'PI_CONFIG_DIR', - 'PI_CONFIG_FILES' -]) { - delete process.env[key] -} -await mkdir(process.env.HOME, { recursive: true }) -const source = (root, path) => pathToFileURL(join(resolve(root), path)).href -const { SessionManager } = await import( - source(process.argv[2], 'packages/coding-agent/src/session/session-manager.ts') -) -const { parseMessageGraphSessionFile } = await import( - source(orcaRoot, 'src/main/ai-vault/scanners/session-scanner-graph-parsers.ts') -) -const { createSessionParseStats, parseAgentSessionFileCached } = await import( - source(orcaRoot, 'src/main/ai-vault/parse-cache/session-scanner-parse-cache.ts') -) -const stats = createSessionParseStats() -const manager = SessionManager.create(scratch, join(scratch, 'sessions')) -try { - manager.appendMessage({ role: 'user', content: 'Original first prompt', timestamp: Date.now() }) - await manager.ensureOnDisk() - await manager.flush() - const candidate = async () => { - const details = await stat(manager.getSessionFile()) - return { - agent: 'omp', - codexHome: null, - file: { - path: manager.getSessionFile(), - mtimeMs: details.mtimeMs, - modifiedAt: details.mtime.toISOString(), - sizeBytes: details.size - } - } - } - const initial = await parseAgentSessionFileCached(await candidate(), process.platform, stats) - assert.equal(initial.title, 'Original first prompt') - await manager.setSessionName('Explicit renamed conversation', 'user') - await manager.flush() - const path = manager.getSessionFile() - const details = await stat(path) - const parsed = await parseMessageGraphSessionFile( - 'omp', - { path, mtimeMs: details.mtimeMs, modifiedAt: details.mtime.toISOString() }, - process.platform - ) - const refreshed = await parseAgentSessionFileCached(await candidate(), process.platform, stats) - assert.equal(parsed?.title, manager.getSessionName()) - assert.equal(refreshed?.title, manager.getSessionName()) - assert.equal(stats.fullParses, 1) - assert.equal(stats.incremental, 1) - assert.equal(initial.title, 'Original first prompt') - const reused = await parseAgentSessionFileCached(await candidate(), process.platform, stats) - assert.equal(reused, refreshed) - console.log( - JSON.stringify({ - actualOmpPersistence: true, - renamedTitlePreserved: true, - cachedRenamePreserved: true, - unchangedSnapshotReused: true, - fullParses: stats.fullParses, - incrementalParses: stats.incremental, - modelCalls: 0 - }) - ) -} finally { - await manager.close() - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/omp-resume-transcript-locator-smoke.mjs b/tests/tools/omp-resume-transcript-locator-smoke.mjs deleted file mode 100644 index 3341db99eb1..00000000000 --- a/tests/tools/omp-resume-transcript-locator-smoke.mjs +++ /dev/null @@ -1,81 +0,0 @@ -// Bun; argv[2] is a read-only OMP checkout. No model requests. -import assert from 'node:assert/strict' -import { mkdtemp, mkdir, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { getAgentResumeArgv } from '../../src/shared/agent-launch/startup/agent-resume-argv' - -assert.ok(process.argv[2], 'Pass a read-only OMP checkout path') -const scratch = await mkdtemp(join(tmpdir(), 'orca-omp-resume-locator-')) -process.env.HOME = join(scratch, 'home') -process.env.USERPROFILE = process.env.HOME -for (const key of [ - 'OMP_CODING_AGENT_DIR', - 'PI_CODING_AGENT_DIR', - 'PI_CONFIG_DIR', - 'OMP_PROFILE', - 'PI_PROFILE', - 'PI_CONFIG_FILES' -]) { - delete process.env[key] -} -process.env.XDG_CONFIG_HOME = join(scratch, 'config') -process.env.XDG_DATA_HOME = join(scratch, 'data') -process.env.XDG_STATE_HOME = join(scratch, 'state') -const source = (name) => - pathToFileURL(join(resolve(process.argv[2]), 'packages/coding-agent/src', name)).href -const managers = [] -try { - await mkdir(process.env.HOME, { recursive: true }) - const { SessionManager } = await import(source('session/session-manager.ts')) - const { Settings } = await import(source('config/settings.ts')) - const { createSessionManager } = await import(source('main.ts')) - const { parseArgs } = await import(source('cli/args.ts')) - const cwd = join(scratch, 'folder workspace') - await mkdir(cwd) - const original = SessionManager.create(cwd, join(scratch, 'custom sessions')) - managers.push(original) - original.appendMessage({ - role: 'user', - content: 'task in a custom session root', - timestamp: Date.now() - }) - await original.ensureOnDisk() - await original.flush() - const settings = await Settings.init({ cwd }) - await assert.rejects( - createSessionManager({ resume: original.getSessionId() }, cwd, settings), - /not found/ - ) - const direct = await createSessionManager({ resume: original.getSessionFile() }, cwd, settings) - managers.push(direct) - assert.equal(direct.getSessionId(), original.getSessionId()) - const argv = getAgentResumeArgv('omp', { - key: 'session_id', - id: original.getSessionId(), - transcriptPath: original.getSessionFile() - }) - assert.ok(argv) - assert.equal( - argv[2], - original.getSessionFile(), - 'Orca must retain the recorded transcript locator' - ) - const resumed = await createSessionManager(parseArgs(argv.slice(1)), cwd, settings) - managers.push(resumed) - assert.equal(resumed.getSessionId(), original.getSessionId()) - console.log( - JSON.stringify({ - customRootUuidMisses: true, - absolutePathResumes: true, - generatedArgvResumes: true, - modelCalls: 0 - }) - ) -} finally { - for (const manager of managers) { - await manager?.close() - } - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/omp-session-root-upstream-smoke.mjs b/tests/tools/omp-session-root-upstream-smoke.mjs deleted file mode 100644 index d3d9f415903..00000000000 --- a/tests/tools/omp-session-root-upstream-smoke.mjs +++ /dev/null @@ -1,86 +0,0 @@ -// Bun; argv[2] is a read-only OMP checkout. Resolves paths without model requests. -import assert from 'node:assert/strict' -import { mkdir, mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { resolveOmpSessionsDir } from '../../src/main/ai-vault/scanners/omp/omp-session-root.ts' - -assert.ok(process.argv[2], 'Pass a read-only OMP checkout path') -const root = await mkdtemp(join(tmpdir(), 'orca-omp-root-parity-')) -const home = join(root, 'home') -const xdg = join(root, 'data') -const keys = [ - 'OMP_CODING_AGENT_DIR', - 'OMP_PROFILE', - 'PI_PROFILE', - 'PI_CONFIG_DIR', - 'PI_CODING_AGENT_DIR', - 'XDG_DATA_HOME', - 'XDG_STATE_HOME', - 'XDG_CACHE_HOME' -] -process.env.HOME = home -process.env.USERPROFILE = home -for (const key of keys) { - delete process.env[key] -} -const passed = [] -try { - await mkdir(join(home, '.omp', 'agent', 'sessions'), { recursive: true }) - const upstream = await import( - pathToFileURL(join(resolve(process.argv[2]), 'packages/utils/src/dirs.ts')).href - ) - const check = (label, env) => { - for (const key of keys) { - delete process.env[key] - } - Object.assign(process.env, env) - upstream.__resetDirsFromEnvForTests() - assert.equal(resolveOmpSessionsDir(), upstream.getSessionsDir(), label) - passed.push(label) - } - check('legacy without XDG', {}) - await mkdir(join(home, '.local', 'share', 'omp', 'sessions'), { recursive: true }) - check('no implicit XDG fallback', {}) - check('missing XDG app root', { XDG_DATA_HOME: xdg }) - await mkdir(join(xdg, 'omp'), { recursive: true }) - check('existing XDG app wins over legacy sessions', { XDG_DATA_HOME: xdg }) - check('named profile stays in config until its own XDG root exists', { - XDG_DATA_HOME: xdg, - OMP_PROFILE: 'work', - PI_CONFIG_DIR: '.config/omp' - }) - await mkdir(join(xdg, 'omp', 'profiles', 'work'), { recursive: true }) - check('named migrated profile', { XDG_DATA_HOME: xdg, OMP_PROFILE: 'work' }) - check('legacy profile fallback', { XDG_DATA_HOME: xdg, PI_PROFILE: 'work' }) - check('canonical empty profile ignores fallback', { - XDG_DATA_HOME: xdg, - OMP_PROFILE: '', - PI_PROFILE: 'work' - }) - check('relative agent override', { PI_CODING_AGENT_DIR: '.' }) - check('custom agent disables XDG', { - XDG_DATA_HOME: xdg, - PI_CODING_AGENT_DIR: join(root, 'custom') - }) - check('named profile ignores custom agent', { - XDG_DATA_HOME: xdg, - OMP_PROFILE: 'work', - PI_CODING_AGENT_DIR: join(root, 'custom') - }) - check('default agent explicit override permits XDG', { - XDG_DATA_HOME: xdg, - PI_CODING_AGENT_DIR: join(home, '.omp', 'agent') - }) - check('canonical default discards inherited profile agent', { - OMP_PROFILE: '', - PI_PROFILE: 'work', - PI_CODING_AGENT_DIR: join(home, '.omp', 'profiles', 'work', 'agent') - }) - console.log( - JSON.stringify({ platform: process.platform, upstreamParityCases: passed, modelCalls: 0 }) - ) -} finally { - await rm(root, { recursive: true, force: true }) -} diff --git a/tests/tools/omp-source-control-runtime-smoke.mjs b/tests/tools/omp-source-control-runtime-smoke.mjs deleted file mode 100644 index 73b7f2603a7..00000000000 --- a/tests/tools/omp-source-control-runtime-smoke.mjs +++ /dev/null @@ -1,33 +0,0 @@ -import assert from 'node:assert/strict' -import { pathToFileURL } from 'node:url' -import { join, resolve } from 'node:path' -import { getCommitMessageAgentSpec } from '../../src/shared/source-control/ai/generation/commit-message-agent-spec.ts' -const reference = process.argv[2] -assert.ok(reference, 'Pass the read-only source checkout path') -const { parseArgs } = await import( - pathToFileURL(join(resolve(reference), 'packages/coding-agent/src/cli/args.ts')).href -) -const spec = getCommitMessageAgentSpec('omp') -assert.ok(spec) -for (const model of ['default', 'provider/model']) { - const args = spec.buildArgs({ prompt: 'Generated entirely from stdin', model }) - const parsed = parseArgs(args) - assert.equal(parsed.print, true) - assert.equal(parsed.noSession, true) - assert.equal(parsed.noTools, true) - assert.equal(parsed.noExtensions, true) - assert.equal(parsed.noSkills, true) - assert.equal(parsed.noRules, true) - assert.equal(parsed.mode, 'text') - assert.equal(parsed.model, model === 'default' ? undefined : model) - assert.deepEqual(parsed.messages, []) -} -console.log( - JSON.stringify({ - actualArgumentParser: true, - promptDelivery: spec.promptDelivery, - configDefault: true, - explicitModel: true, - modelCalls: 0 - }) -) diff --git a/tests/tools/omp-source-control-ui/README.md b/tests/tools/omp-source-control-ui/README.md index 740686f0e10..5066d32e140 100644 --- a/tests/tools/omp-source-control-ui/README.md +++ b/tests/tools/omp-source-control-ui/README.md @@ -19,7 +19,3 @@ generator is called or mocked. Current Source Control AI settings expose action recipes with CLI arguments; they do not have a Discover Models button. The separate `omp-source-control-discovery.test.ts` regression covers production generation selection with cached models, including SSH host keys. - -`tests/tools/omp-source-control-runtime-smoke.mjs` separately validates argv against -the actual OMP argument parser in a read-only reference checkout. It makes no model -call and must not be described as real generated output. diff --git a/tests/tools/omp-startup-keyboard-smoke.mjs b/tests/tools/omp-startup-keyboard-smoke.mjs deleted file mode 100644 index 96818f45650..00000000000 --- a/tests/tools/omp-startup-keyboard-smoke.mjs +++ /dev/null @@ -1,99 +0,0 @@ -// Bun: isolated protocol harness with actual read-only OMP ProcessTerminal. -import assert from 'node:assert/strict' -import { mkdtemp, mkdir, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { PtyStartupIngress } from '../../src/shared/terminal/pty/ingress/pty-startup-ingress.ts' - -assert.ok(process.argv[2], 'Pass the read-only OMP checkout') -const scratch = await mkdtemp(join(tmpdir(), 'orca-omp-keyboard-')) -process.env.HOME = join(scratch, 'home') -await mkdir(process.env.HOME) -const { setTerminalHeadless } = await import( - pathToFileURL(join(resolve(process.argv[2]), 'packages/utils/src/env.ts')).href -) -const { ProcessTerminal } = await import( - pathToFileURL(join(resolve(process.argv[2]), 'packages/tui/src/terminal.ts')).href -) -const originalWrite = process.stdout.write -const originalRawMode = process.stdin.setRawMode -const originalResume = process.stdin.resume -process.stdin.setRawMode = () => process.stdin -process.stdin.resume = () => process.stdin -const emitted = [] -const replies = [] -let terminal -let ingress -try { - assert.ok(!process.stdout.isTTY, 'Run with piped stdout to keep native terminal effects isolated') - ingress = new PtyStartupIngress({ - intent: { - colors: { foreground: '#ffffff', background: '#000000' }, - deadlineMs: 5000, - kittyKeyboardProtocol: true - }, - write: (data) => { - replies.push(data) - queueMicrotask(() => process.stdin.emit('data', data)) - }, - onEmission: (span) => emitted.push(span) - }) - process.stdout.write = (data) => { - ingress.accept(String(data)) - return true - } - terminal = new ProcessTerminal() - terminal.start( - () => {}, - () => {}, - () => {}, - { deferInput: true } - ) - Object.defineProperty(process.stdout, 'isTTY', { value: true, configurable: true }) - terminal.enableInput() - await new Promise((resolve) => setTimeout(resolve, 50)) - assert.equal(terminal.kittyProtocolActive, true) - assert.equal(replies.filter((reply) => reply === '\x1b[?0u').length, 1) - assert.ok( - emitted - .map((span) => span.data) - .join('') - .includes(terminal.kittyEnableSequence) - ) - assert.ok( - !emitted - .map((span) => span.data) - .join('') - .includes('\x1b[?u') - ) - terminal.stop() - ingress.drainAndClose() - let end = 0 - for (const span of emitted) { - assert.equal(span.rawStartSeq, end) - end = span.rawEndSeq - } - Object.defineProperty(process.stdout, 'isTTY', { value: false, configurable: true }) - process.stdout.write = originalWrite - console.log( - JSON.stringify({ - actualOmpProcessTerminal: true, - kittyActive: true, - singleReply: true, - modePushPreserved: true, - sequenceCoverage: end, - rendererAttached: false, - modelCalls: 0 - }) - ) -} finally { - terminal?.stop() - setTerminalHeadless(true) - ingress?.drainAndClose() - Object.defineProperty(process.stdout, 'isTTY', { value: false, configurable: true }) - process.stdout.write = originalWrite - process.stdin.setRawMode = originalRawMode - process.stdin.resume = originalResume - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/omp-status-input-redaction-smoke.mjs b/tests/tools/omp-status-input-redaction-smoke.mjs deleted file mode 100644 index 7a7a79b0699..00000000000 --- a/tests/tools/omp-status-input-redaction-smoke.mjs +++ /dev/null @@ -1,101 +0,0 @@ -// Bun; argv[2] is a read-only OMP checkout. Synthetic events, no model calls. -import assert from 'node:assert/strict' -import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises' -import { createServer } from 'node:http' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { getPiAgentStatusExtensionSource } from '../../src/main/pi/agent-status-extension-source.ts' - -assert.ok(process.argv[2], 'Pass a read-only OMP checkout path') -const scratch = await mkdtemp(join(tmpdir(), 'orca-omp-input-redaction-')) -process.env.HOME = join(scratch, 'home') -process.env.USERPROFILE = process.env.HOME -process.env.XDG_CONFIG_HOME = join(scratch, 'config') -process.env.XDG_DATA_HOME = join(scratch, 'data') -process.env.XDG_STATE_HOME = join(scratch, 'state') -process.env.XDG_CACHE_HOME = join(scratch, 'cache') -for (const key of [ - 'OMP_CODING_AGENT_DIR', - 'PI_CODING_AGENT_DIR', - 'PI_CONFIG_DIR', - 'OMP_PROFILE', - 'PI_PROFILE', - 'PI_CONFIG_FILES', - 'ORCA_AGENT_HOOK_ENDPOINT', - 'ORCA_PI_STATUS_OWNED' -]) { - delete process.env[key] -} -await mkdir(process.env.HOME, { recursive: true }) -const source = (path) => - pathToFileURL(join(resolve(process.argv[2]), 'packages/coding-agent/src', path)).href -const { loadExtensions } = await import(source('extensibility/extensions/loader.ts')) -const { EventBus } = await import(source('utils/event-bus.ts')) -const { SessionManager } = await import(source('session/session-manager.ts')) -const posts = [] -const server = createServer(async (request, response) => { - let body = '' - for await (const chunk of request) { - body += chunk - } - posts.push(JSON.parse(body).payload) - response.writeHead(200).end() -}) -await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) -const manager = SessionManager.inMemory(scratch) -try { - process.env.ORCA_PANE_KEY = 'redaction-test-pane' - process.env.ORCA_AGENT_HOOK_PORT = String(server.address().port) - process.env.ORCA_AGENT_HOOK_TOKEN = 'synthetic-test-token' - const extensionPath = join(scratch, 'orca-agent-status.ts') - await writeFile(extensionPath, getPiAgentStatusExtensionSource('omp')) - const loaded = await loadExtensions([extensionPath], scratch, new EventBus()) - assert.deepEqual(loaded.errors, []) - const extension = loaded.extensions[0] - let serializerCalls = 0 - const cases = [ - { input: { command: 'cat ~/.ssh/id_rsa' }, expected: { redacted: true } }, - { - input: { - path: '/tmp/safe', - toJSON() { - serializerCalls++ - return { path: '~/.ssh/id_rsa' } - } - }, - expected: { redacted: true } - }, - { input: { questions: [{ question: 'Choose', options: ['one', 'two'] }] } } - ] - for (const [index, entry] of cases.entries()) { - for (const handler of extension.handlers.get('tool_call') ?? []) { - await handler( - { type: 'tool_call', toolName: 'custom', input: entry.input }, - { sessionManager: manager, hasUI: false } - ) - } - const deadline = Date.now() + 2000 - while (posts.length <= index && Date.now() < deadline) { - await new Promise((resolve) => setTimeout(resolve, 10)) - } - assert.equal(posts.length, index + 1) - assert.deepEqual(posts[index].tool_input, entry.expected ?? entry.input) - } - assert.equal(serializerCalls, 0) - console.log( - JSON.stringify({ - actualOmpLoader: true, - actualHttpPosts: posts.length, - sensitiveInputsRedacted: 2, - ordinaryQuestionsPreserved: true, - serializerCalls, - modelCalls: 0 - }) - ) -} finally { - await manager.close() - server.closeAllConnections() - await new Promise((resolve) => server.close(resolve)) - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/pi-ui-prompt-cdp-smoke.mjs b/tests/tools/pi-ui-prompt-cdp-smoke.mjs deleted file mode 100644 index 27bf971781f..00000000000 --- a/tests/tools/pi-ui-prompt-cdp-smoke.mjs +++ /dev/null @@ -1,61 +0,0 @@ -// Run against an isolated Orca dev instance with Pi and pi-ui-prompt-extension.mjs loaded. -// Usage: node tests/tools/pi-ui-prompt-cdp-smoke.mjs http://127.0.0.1:9333 /path/to/proof -import assert from 'node:assert/strict' -import { mkdir } from 'node:fs/promises' -import { join, resolve } from 'node:path' -import { chromium, expect } from '@stablyai/playwright-test' - -const [endpoint, outputDirectory] = process.argv.slice(2) -assert.ok(endpoint && outputDirectory, 'Pass the CDP endpoint and screenshot directory') -const output = resolve(outputDirectory) -await mkdir(output, { recursive: true }) -const browser = await chromium.connectOverCDP(endpoint) -try { - const page = browser.contexts().flatMap((context) => context.pages())[0] - assert.ok(page, 'Orca renderer must be open') - const identity = await page.evaluate(() => window.api.app.getIdentity()) - assert.equal(identity.isDev, true, 'Use an isolated development instance') - console.log(JSON.stringify(identity)) - const terminals = page.locator('[data-pty-id]') - await expect(terminals).toHaveCount(1) - const terminal = terminals.first() - const input = page.getByRole('textbox', { name: 'Terminal input' }) - const attention = page.getByLabel('Needs attention', { exact: true }) - const waitForState = (state) => - expect - .poll(() => - page.evaluate(() => - Object.values(window.__store.getState().agentStatusByPaneKey) - .filter((entry) => entry.agentType === 'pi') - .map((entry) => entry.state) - ) - ) - .toEqual([state]) - - for (const kind of ['select', 'confirm', 'input', 'editor', 'custom']) { - for (const ending of kind === 'select' ? ['answer', 'cancel'] : ['cancel']) { - await input.pressSequentially(`/orca-modal ${kind}`, { delay: 10 }) - await input.press('Enter') - await waitForState('waiting') - await expect(attention).toBeVisible() - await expect(terminal).toBeVisible() - await page.screenshot({ path: join(output, `${kind}-${ending}-waiting.png`) }) - await (kind === 'custom' - ? page.evaluate(() => { - const id = document.querySelector('[data-pty-id]')?.getAttribute('data-pty-id') - if (!id) { - throw new Error('Terminal lost its PTY') - } - window.api.pty.write(id, '\u001b') - }) - : input.press(ending === 'answer' ? 'Enter' : 'Escape')) - await waitForState('done') - await expect(attention).toHaveCount(0) - await expect(page.getByLabel('Done', { exact: true })).toBeVisible() - await page.screenshot({ path: join(output, `${kind}-${ending}-done.png`) }) - console.log(`PASS: ${kind}/${ending}: waiting -> done, visible icon agrees`) - } - } -} finally { - await browser.close() -} diff --git a/tests/tools/pi-ui-prompt-extension.mjs b/tests/tools/pi-ui-prompt-extension.mjs deleted file mode 100644 index 561c361446d..00000000000 --- a/tests/tools/pi-ui-prompt-extension.mjs +++ /dev/null @@ -1,43 +0,0 @@ -// Load with Pi's -e flag; /orca-modal exercises real dialogs without a model or API key. -export default function (pi) { - pi.registerCommand('orca-modal', { - description: 'Verify Orca status: select, confirm, input, editor, or custom', - handler: async (args, ctx) => { - const kind = args.trim() || 'select' - const title = `Orca verification: ${kind}` - let answer - switch (kind) { - case 'select': - answer = await ctx.ui.select(title, ['Continue verification', 'Second option']) - break - case 'confirm': - answer = await ctx.ui.confirm(title, 'Continue verification?') - break - case 'input': - answer = await ctx.ui.input(title, 'Type a test answer') - break - case 'editor': - answer = await ctx.ui.editor(title, 'Test answer') - break - case 'custom': - answer = await ctx.ui.custom((_tui, _theme, keys, done) => ({ - render: () => [title, 'Press Enter to answer or Escape to cancel.'], - invalidate() {}, - handleInput: (data) => { - if (keys.matches(data, 'tui.select.confirm')) { - done('answered') - } - if (keys.matches(data, 'tui.select.cancel')) { - done(undefined) - } - } - })) - break - default: - ctx.ui.notify('Use select, confirm, input, editor, or custom', 'error') - return - } - ctx.ui.notify(`Orca verification: ${kind} ${answer === undefined ? 'cancelled' : 'answered'}`) - } - }) -} diff --git a/tests/tools/pi-ui-prompt-runtime-smoke.mjs b/tests/tools/pi-ui-prompt-runtime-smoke.mjs deleted file mode 100644 index 7fb43f9635a..00000000000 --- a/tests/tools/pi-ui-prompt-runtime-smoke.mjs +++ /dev/null @@ -1,155 +0,0 @@ -// Run with: node tests/tools/pi-ui-prompt-runtime-smoke.mjs /path/to/pi-coding-agent -import assert from 'node:assert/strict' -import { mkdtemp, readFile, rm } from 'node:fs/promises' -import { createRequire } from 'node:module' -import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' -import { pathToFileURL } from 'node:url' -import { runInNewContext } from 'node:vm' -import { build } from 'esbuild' -import ts from 'typescript-api' - -const piRoot = process.argv[2] -assert.ok(piRoot, 'Pass the installed pi-coding-agent package directory (Pi >= 0.84.4)') -const cwd = process.cwd() -const require = createRequire(join(cwd, 'package.json')) -const scratch = await mkdtemp(join(tmpdir(), 'orca-pi-ui-prompt-')) - -try { - const bundle = join(scratch, 'orca-status.cjs') - await build({ - stdin: { - contents: [ - "export { getPiAgentStatusExtensionSource } from './src/main/pi/agent-status-extension-source';", - "export { normalizeHookPayload } from './src/shared/agent-hook-listener/normalization/normalize-hook-payload';", - "export { createHookListenerState } from './src/shared/agent-hook-listener/listener-state';" - ].join('\n'), - resolveDir: cwd - }, - bundle: true, - platform: 'node', - format: 'cjs', - outfile: bundle, - packages: 'external' - }) - const { - getPiAgentStatusExtensionSource, - normalizeHookPayload, - createHookListenerState - } = require(bundle) - const { ExtensionRunner } = await import( - pathToFileURL(resolve(piRoot, 'dist/core/extensions/runner.js')).href - ) - const handlers = new Map() - const state = createHookListenerState() - const snapshots = [] - const errors = [] - const module = { exports: {} } - const source = ts.transpileModule(getPiAgentStatusExtensionSource('pi'), { - compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } - }).outputText - runInNewContext(source, { - module, - exports: module.exports, - require, - process: { - pid: 4242, - title: 'pi', - argv: ['node', 'pi'], - env: { - ORCA_PANE_KEY: 'tab-1:11111111-1111-4111-8111-111111111111', - ORCA_AGENT_HOOK_PORT: '4321', - ORCA_AGENT_HOOK_TOKEN: 'test', - ORCA_AGENT_HOOK_ENV: 'production' - } - }, - fetch: async (_url, init) => { - const result = normalizeHookPayload(state, 'pi', JSON.parse(init.body), 'production') - snapshots.push(result?.payload) - return { ok: true } - }, - console, - Promise, - Buffer, - URL, - AbortController, - setTimeout, - clearTimeout - }) - module.exports.default({ on: (name, handler) => handlers.set(name, [handler]) }) - const runner = new ExtensionRunner([{ path: 'orca-status', handlers }], {}, cwd, {}, {}) - runner.onError((error) => errors.push(error)) - let idle = false - runner.isIdleFn = () => idle - const flush = async () => { - for (let i = 0; i < 80; i++) { - await Promise.resolve() - } - } - const last = () => snapshots.at(-1)?.state - let checks = 0 - - // Only UI promises are controlled; the real Pi runner must produce the lifecycle events. - for (const kind of ['select', 'confirm', 'input', 'editor', 'custom']) { - for (const ending of ['answer', 'cancel', 'error']) { - for (const wasIdle of [false, true]) { - idle = wasIdle - let finish, fail - const pending = new Promise((yes, no) => { - finish = yes - fail = no - }) - runner.setUIContext({ [kind]: () => pending }, 'interactive') - const promise = runner.getUIContext()[kind]('Sensitive title', [], {}) - const observed = promise.catch(() => undefined) - await flush() - assert.equal(last(), 'waiting', `${kind}/${ending}/idle=${idle}: start`) - await runner.emit({ type: 'tool_execution_end', toolName: 'bash' }) - await flush() - assert.equal(last(), 'waiting', 'Unrelated work must not clear the modal') - if (ending === 'error') { - fail(new Error('UI fixture failure')) - } else { - finish(ending === 'cancel' ? undefined : 'answer') - } - await observed - await flush() - assert.equal(last(), idle ? 'done' : 'working', `${kind}/${ending}/idle=${idle}: end`) - checks++ - } - } - } - - let finishA, finishB - runner.setUIContext( - { - custom: () => - new Promise((done) => { - finishA = done - }), - input: () => - new Promise((done) => { - finishB = done - }) - }, - 'interactive' - ) - const a = runner.getUIContext().custom(() => {}) - const b = runner.getUIContext().input('Input') - await flush() - assert.equal(last(), 'waiting') - finishA() - await a - await flush() - assert.equal(last(), 'waiting', 'The remaining prompt still needs input') - finishB() - await b - await flush() - assert.equal(last(), 'done') - checks++ - assert.deepEqual(errors, []) - const { version } = JSON.parse(await readFile(resolve(piRoot, 'package.json'), 'utf8')) - console.log(`PASS: Pi ${version}, ${checks} scenarios, ${snapshots.length} status snapshots`) -} finally { - await rm(scratch, { recursive: true, force: true }) -} diff --git a/tests/tools/pi-ui-prompt-verification.md b/tests/tools/pi-ui-prompt-verification.md deleted file mode 100644 index 1aa128cec49..00000000000 --- a/tests/tools/pi-ui-prompt-verification.md +++ /dev/null @@ -1,42 +0,0 @@ -# Real Pi dialog verification - -Use Pi 0.84.4 or newer. Older Pi does not emit `ui_prompt_start` / `ui_prompt_end`. -The checked-in extension only opens dialogs; it does not call a model or send synthetic -Orca hook events. - -1. Launch an isolated Orca development instance with CDP using the Electron skill. -2. Open one terminal in a git worktree or folder workspace. Start Pi with Orca's - generated status extension and this additional extension: - - ```sh - pi --offline --no-session -e /absolute/path/to/orca/tests/tools/pi-ui-prompt-extension.mjs - ``` - - If launching Pi directly through `node` or disabling extension discovery, explicitly - load Orca's generated `orca-agent-status.ts` with another `-e` argument. - -3. Leave Pi at its input editor, then run from the Orca repository: - - ```sh - node tests/tools/pi-ui-prompt-cdp-smoke.mjs http://127.0.0.1:9333 /path/to/proof - ``` - -The smoke check requires one terminal and one Pi status entry in the isolated instance. -It opens all five real Pi dialogs, answers the selector, and cancels each dialog. -It asserts backend `waiting` plus the terminal tab's visible **Needs attention** icon, -then backend `done` plus the visible completion icon. Screenshots are saved for both -states. Custom-dialog cancellation sends a plain Escape through the real PTY; -the standard dialogs use browser keyboard events. - -For manual verification, run `/orca-modal select`, `/orca-modal confirm`, -`/orca-modal input`, `/orca-modal editor`, or `/orca-modal custom` inside Pi. - -The separate runtime test covers active-agent close (`working`), idle close (`done`), -overlap, unrelated tool events, and rejected dialog promises using Pi's actual runner: - -```sh -node tests/tools/pi-ui-prompt-runtime-smoke.mjs /path/to/installed/pi-coding-agent -``` - -These local checks do not prove live SSH/network-failure behavior, Windows/WSL, -mobile rendering, or startup selectors created before Pi's extension runner exists. From 60df70e7cd8af083887fbaa5b73542cdad752ef5 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:49:14 -0800 Subject: [PATCH 21/80] refactor: organize provider and repository settings owners (#40059) * refactor: organize provider and repository settings owners * refactor: define repository action recipe specialization once --- config/host-list-render-baseline.txt | 2 +- config/react-compiler-bailout-baseline.txt | 8 +- .../scripts/ci/sharding/ci-shard-timings.json | 8 +- .../agent/AgentLaunchSetupPanel.tsx | 2 +- .../launch-settings/AgentArgsChoiceChips.tsx | 125 ++++++ .../AgentChatChoiceControls.tsx | 10 +- .../launch-settings/AgentChoiceControls.tsx | 4 +- .../AgentChoiceOptionField.tsx | 15 - .../AgentLaunchChoiceEditor.tsx | 5 +- .../AgentLaunchDefaultsEditor.tsx | 3 +- .../AgentPermissionChoiceControls.tsx | 386 ------------------ .../launch-settings/AgentPermissionMenu.tsx | 107 +++++ .../AgentPermissionMenuOptions.tsx | 2 +- .../launch-settings/agent-args-choice.ts | 7 + .../agent-choice-route-hint.ts | 15 + .../agent-model-choice-chips.ts | 2 +- .../agent-permission-choice-label.ts | 20 + .../agent-permission-choice-row.ts | 18 + .../AntigravityChatAuthForm.tsx | 14 +- .../AntigravityChatAuthSettingRow.tsx | 2 +- .../AntigravityChatSignInAction.tsx | 2 +- .../AntigravityChatSignInRow.tsx | 179 +------- .../AntigravityProviderSignInArea.tsx | 18 +- ...antigravity-chat-runtime-sign-in-policy.ts | 29 ++ .../use-antigravity-chat-sign-in.ts | 161 ++++++++ .../ai/source-control-action-model-copy.ts | 4 +- .../settings/accounts/AccountsPane.tsx | 20 +- .../accounts/accounts-pane-account-actions.ts | 13 +- .../settings/accounts/accounts-pane-types.ts | 19 +- .../providers/AntigravityAccountsSection.tsx | 8 +- .../accounts-pane-gemini-section.tsx | 82 ++++ ...tsx => accounts-pane-opencode-section.tsx} | 85 +--- .../scope/accounts-pane-location-section.tsx | 2 +- .../accounts/scope/accounts-pane-runtime.ts | 18 +- .../AgentWorkspaceTrustSetting.fixture.tsx | 2 +- .../settings/agents/AgentsPane.test.tsx | 79 ---- .../components/settings/agents/AgentsPane.tsx | 24 +- .../behavior/AgentWorkspaceTrustSetting.tsx | 27 ++ .../agents/launch/AgentCatalogRow.tsx | 9 +- .../launch/AgentLaunchSettingsCatalog.tsx | 8 +- .../launch/AgentLaunchSettingsLoading.tsx | 2 +- .../AllAgentsPermissionSetting.tsx | 124 ++++++ .../agent-permission-mode-label.ts | 2 +- .../settings/repository/RepositoryPane.tsx | 18 +- .../execution/sparse-preset-draft-editor.tsx | 31 -- .../RepositoryHookPolicySettings.tsx | 0 .../RepositoryHookScriptSetting.tsx | 0 .../RepositoryHooksSection.tsx | 0 .../RepositoryHooksYamlStatus.tsx | 0 .../RepositoryIssueCommandSetting.tsx | 0 .../repository-git-hooks-search-entries.ts | 0 .../repository-hook-defaults.ts | 0 .../repository-hook-settings-draft.ts | 0 ...se-repository-hook-settings-draft.test.tsx | 0 .../use-repository-hook-settings-draft.ts | 0 ...pository-issue-command-wsl-stopped.test.ts | 2 +- .../use-repository-issue-command.ts | 0 .../RepositoryHostSetupActions.tsx | 0 ...yHostSetupsSection.settings-entry.test.tsx | 0 .../RepositoryHostSetupsSection.tsx | 0 .../repository-host-add-project-steps.tsx | 0 .../repository-host-setup-options.ts | 0 .../repository/mcp/mcp-config-search.ts | 45 ++ .../settings/repository/repository-search.ts | 8 +- .../ProjectWindowsRuntimeSetting.tsx | 0 .../RepositoryWindowsRuntimeSection.tsx | 0 .../repository-runtime-session-summary.ts | 0 .../RepositoryForkSyncSection.tsx | 0 .../RepositoryGitHubAccountSection.tsx | 21 +- .../RepositorySourceControlAiActionRows.tsx | 11 +- ...itorySourceControlAiActionSaveControls.tsx | 2 +- .../repository-git-author-search-entries.ts | 37 +- .../repository-source-control-ai-draft.ts | 26 +- .../repository-source-control-ai-global-ux.ts | 2 +- .../repository-source-control-ai-labels.ts | 36 +- ...ory-source-control-ai-recipe-completion.ts | 52 +++ ...repository-source-control-ai-text-draft.ts | 5 +- ...RepositoryWorktreeDefaultsSection.test.tsx | 0 .../RepositoryWorktreeDefaultsSection.tsx | 0 .../SparsePresetSettingsSection.test.tsx | 0 .../SparsePresetSettingsSection.tsx | 9 +- .../WorktreeSymlinksSection.tsx | 0 .../repository-git-worktree-search-entries.ts | 0 .../sparse-preset-date.ts | 0 .../sparse-preset-directory-preview.tsx | 0 .../sparse-preset-operation-error.ts | 0 .../sparse-preset-settings-row.tsx | 0 .../SourceControlActionAgentPicker.tsx | 12 +- .../SourceControlActionArguments.tsx | 2 +- .../SourceControlTextActionAgentPicker.tsx | 6 +- ...eSetup.tsx => TaskSourceCodeHostSetup.tsx} | 65 +-- .../settings/tasks/TaskSourceJiraSetup.tsx | 54 +++ .../tasks/TaskSourceSimpleSetup.test.tsx | 43 -- .../components/settings/tasks/TasksPane.tsx | 3 +- .../settings/tasks/task-source-setup-state.ts | 15 + .../status-bar/ClaudeSwitcherForOwner.tsx | 2 +- .../status-bar/status-bar-runtime-targets.ts | 2 +- .../agent-settings-update-queue.test.ts | 75 ++++ .../native-chat-provider-choice.test.tsx | 2 +- .../src/lib/claude-account-sign-in.ts | 2 +- .../account-runtime-label.ts | 21 + .../provider-account-action-errors.ts} | 4 +- 102 files changed, 1124 insertions(+), 1161 deletions(-) create mode 100644 src/renderer/src/components/agent/launch-settings/AgentArgsChoiceChips.tsx delete mode 100644 src/renderer/src/components/agent/launch-settings/AgentPermissionChoiceControls.tsx create mode 100644 src/renderer/src/components/agent/launch-settings/AgentPermissionMenu.tsx create mode 100644 src/renderer/src/components/agent/launch-settings/agent-args-choice.ts create mode 100644 src/renderer/src/components/agent/launch-settings/agent-choice-route-hint.ts create mode 100644 src/renderer/src/components/agent/launch-settings/agent-permission-choice-label.ts create mode 100644 src/renderer/src/components/agent/launch-settings/agent-permission-choice-row.ts create mode 100644 src/renderer/src/components/provider-accounts/antigravity-chat-runtime-sign-in-policy.ts create mode 100644 src/renderer/src/components/provider-accounts/use-antigravity-chat-sign-in.ts create mode 100644 src/renderer/src/components/settings/accounts/providers/accounts-pane-gemini-section.tsx rename src/renderer/src/components/settings/accounts/providers/{accounts-pane-provider-setting-sections.tsx => accounts-pane-opencode-section.tsx} (66%) delete mode 100644 src/renderer/src/components/settings/agents/AgentsPane.test.tsx create mode 100644 src/renderer/src/components/settings/agents/behavior/AgentWorkspaceTrustSetting.tsx create mode 100644 src/renderer/src/components/settings/agents/permissions/AllAgentsPermissionSetting.tsx delete mode 100644 src/renderer/src/components/settings/repository/execution/sparse-preset-draft-editor.tsx rename src/renderer/src/components/settings/repository/{execution => hooks}/RepositoryHookPolicySettings.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/RepositoryHookScriptSetting.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/RepositoryHooksSection.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/RepositoryHooksYamlStatus.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/RepositoryIssueCommandSetting.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/repository-git-hooks-search-entries.ts (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/repository-hook-defaults.ts (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/repository-hook-settings-draft.ts (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/use-repository-hook-settings-draft.test.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/use-repository-hook-settings-draft.ts (100%) rename src/renderer/src/components/settings/repository/{execution => hooks}/use-repository-issue-command.ts (100%) rename src/renderer/src/components/settings/repository/{execution => hosts}/RepositoryHostSetupActions.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hosts}/RepositoryHostSetupsSection.settings-entry.test.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hosts}/RepositoryHostSetupsSection.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hosts}/repository-host-add-project-steps.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => hosts}/repository-host-setup-options.ts (100%) create mode 100644 src/renderer/src/components/settings/repository/mcp/mcp-config-search.ts rename src/renderer/src/components/settings/repository/{execution => runtime}/ProjectWindowsRuntimeSetting.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => runtime}/RepositoryWindowsRuntimeSection.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => runtime}/repository-runtime-session-summary.ts (100%) rename src/renderer/src/components/settings/repository/{execution => source-control}/RepositoryForkSyncSection.tsx (100%) rename src/renderer/src/components/settings/repository/{ => source-control}/RepositoryGitHubAccountSection.tsx (94%) rename src/renderer/src/components/settings/{ => repository}/source-control/RepositorySourceControlAiActionSaveControls.tsx (97%) rename src/renderer/src/components/settings/repository/{ => source-control}/repository-git-author-search-entries.ts (56%) create mode 100644 src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts rename src/renderer/src/components/settings/repository/{execution => worktrees}/RepositoryWorktreeDefaultsSection.test.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/RepositoryWorktreeDefaultsSection.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/SparsePresetSettingsSection.test.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/SparsePresetSettingsSection.tsx (98%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/WorktreeSymlinksSection.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/repository-git-worktree-search-entries.ts (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/sparse-preset-date.ts (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/sparse-preset-directory-preview.tsx (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/sparse-preset-operation-error.ts (100%) rename src/renderer/src/components/settings/repository/{execution => worktrees}/sparse-preset-settings-row.tsx (100%) rename src/renderer/src/components/settings/tasks/{TaskSourceSimpleSetup.tsx => TaskSourceCodeHostSetup.tsx} (51%) create mode 100644 src/renderer/src/components/settings/tasks/TaskSourceJiraSetup.tsx delete mode 100644 src/renderer/src/components/settings/tasks/TaskSourceSimpleSetup.test.tsx create mode 100644 src/renderer/src/lib/provider-accounts/account-runtime-label.ts rename src/renderer/src/{components/settings/accounts/accounts-pane-action-errors.ts => lib/provider-accounts/provider-account-action-errors.ts} (92%) diff --git a/config/host-list-render-baseline.txt b/config/host-list-render-baseline.txt index b1a39632401..09d3a029300 100644 --- a/config/host-list-render-baseline.txt +++ b/config/host-list-render-baseline.txt @@ -7,7 +7,7 @@ 1 src/renderer/src/components/settings/browser/profiles/BrowserSessionCookiesSection.tsx 1 src/renderer/src/components/settings/browser/routing/BrowserSshWorkspaceRoutingSetting.tsx 1 src/renderer/src/components/settings/quick-commands/QuickCommandsToolbar.tsx -1 src/renderer/src/components/settings/repository/execution/RepositoryHostSetupsSection.tsx +1 src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupsSection.tsx 1 src/renderer/src/components/sidebar/host-admission/ssh/AddRemoteHostSshConfigPicker.tsx 4 src/renderer/src/components/status-bar/SshStatusSegment.tsx 1 src/renderer/src/components/status-bar/resource-usage-popover-body.tsx diff --git a/config/react-compiler-bailout-baseline.txt b/config/react-compiler-bailout-baseline.txt index 7ba46d39928..c4cd51da520 100644 --- a/config/react-compiler-bailout-baseline.txt +++ b/config/react-compiler-bailout-baseline.txt @@ -422,11 +422,11 @@ 1 src/renderer/src/components/settings/privacy/PrivacyPane.tsx 1 src/renderer/src/components/settings/quick-commands/QuickCommandsPane.tsx 1 src/renderer/src/components/settings/repository/RepositoryFormatterSection.tsx -2 src/renderer/src/components/settings/repository/RepositoryGitHubAccountSection.tsx +2 src/renderer/src/components/settings/repository/source-control/RepositoryGitHubAccountSection.tsx 3 src/renderer/src/components/settings/repository/RepositoryJiraProjectSection.tsx -2 src/renderer/src/components/settings/repository/execution/RepositoryForkSyncSection.tsx -3 src/renderer/src/components/settings/repository/execution/RepositoryHostSetupActions.tsx -2 src/renderer/src/components/settings/repository/execution/SparsePresetSettingsSection.tsx +2 src/renderer/src/components/settings/repository/source-control/RepositoryForkSyncSection.tsx +3 src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupActions.tsx +2 src/renderer/src/components/settings/repository/worktrees/SparsePresetSettingsSection.tsx 2 src/renderer/src/components/settings/repository/identity/RepositoryIconPicker.tsx 1 src/renderer/src/components/settings/repository/mcp/McpConfigSection.tsx 9 src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-global-ux.ts diff --git a/config/scripts/ci/sharding/ci-shard-timings.json b/config/scripts/ci/sharding/ci-shard-timings.json index 1c66127fe10..da79a66c8cf 100644 --- a/config/scripts/ci/sharding/ci-shard-timings.json +++ b/config/scripts/ci/sharding/ci-shard-timings.json @@ -5606,7 +5606,7 @@ "src/renderer/src/components/settings/form-controls/SettingsFormControls.segmented-control.test.tsx": 730, "src/renderer/src/components/settings/shortcuts/ShortcutFilterRail.test.ts": 273, "src/renderer/src/components/settings/shortcuts/ShortcutsPane.provenance.test.tsx": 2302, - "src/renderer/src/components/settings/repository/execution/SparsePresetSettingsSection.test.tsx": 744, + "src/renderer/src/components/settings/repository/worktrees/SparsePresetSettingsSection.test.tsx": 744, "src/renderer/src/components/settings/ssh/server-actions/SshHostDeltaMoveDialog.test.tsx": 438, "src/renderer/src/components/settings/ssh/server-actions/SshHostRecoverServerAction.test.tsx": 669, "src/renderer/src/components/settings/ssh/SshPane.test.tsx": 1699, @@ -5636,8 +5636,8 @@ "src/renderer/src/components/settings/notifications/notification-host-pending-save.test.tsx": 2029, "src/renderer/src/components/settings/repository/RepositoryJiraProjectSection.test.tsx": 463, "src/renderer/src/components/settings/repository/RepositoryPaneDraftInput.test.tsx": 314, - "src/renderer/src/components/settings/repository/execution/RepositoryHostSetupsSection.settings-entry.test.tsx": 2007, - "src/renderer/src/components/settings/repository/execution/RepositoryWorktreeDefaultsSection.test.tsx": 495, + "src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupsSection.settings-entry.test.tsx": 2007, + "src/renderer/src/components/settings/repository/worktrees/RepositoryWorktreeDefaultsSection.test.tsx": 495, "src/renderer/src/components/settings/repository/identity/RepositoryIconPicker.github-avatar-refresh.test.tsx": 615, "src/renderer/src/components/settings/runtime-environments/pairing/runtime-share-computer-button.test.tsx": 2092, "src/renderer/src/components/settings/navigation/settings-project-list.test.ts": 99, @@ -5648,7 +5648,7 @@ "src/renderer/src/components/integrations/task-trackers/linear-integration-card.test.tsx": 1414, "src/renderer/src/components/settings/appearance/themes/use-catalog-app-themes.test.ts": 2159, "src/renderer/src/components/settings/form-controls/use-debounced-settings-text-draft.test.ts": 308, - "src/renderer/src/components/settings/repository/execution/use-repository-hook-settings-draft.test.tsx": 586, + "src/renderer/src/components/settings/repository/hooks/use-repository-hook-settings-draft.test.tsx": 586, "src/renderer/src/components/settings/repository/hooks/use-repository-issue-command-wsl-stopped.test.ts": 523, "src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-catalog.test.tsx": 2252, "src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-mutation-actions.test.tsx": 2069, diff --git a/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx b/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx index 7e420a90701..f1674e82ec5 100644 --- a/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx +++ b/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx @@ -27,7 +27,7 @@ import { AgentDefaultArgsInput } from './launch-settings/AgentLaunchDefaultsEdit import { NativeChatSessionOptionRows } from '../native-chat/session/options/controls/NativeChatSessionOptionRows' import { Collapsible, CollapsibleTrigger, CollapsibleContent } from '@/components/ui/collapsible' import type { SessionOptionDescriptor } from '../../../../shared/native-chat/input/native-chat-session-options' -import { AgentPermissionMenu } from './launch-settings/AgentPermissionChoiceControls' +import { AgentPermissionMenu } from './launch-settings/AgentPermissionMenu' import { AgentLaunchSetupFooter, agentLaunchSetupArgumentsNote, diff --git a/src/renderer/src/components/agent/launch-settings/AgentArgsChoiceChips.tsx b/src/renderer/src/components/agent/launch-settings/AgentArgsChoiceChips.tsx new file mode 100644 index 00000000000..e5ea2e81420 --- /dev/null +++ b/src/renderer/src/components/agent/launch-settings/AgentArgsChoiceChips.tsx @@ -0,0 +1,125 @@ +import type { Ref } from 'react' +import { X } from 'lucide-react' +import { useTranslate } from '../../../i18n/use-translate' +import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip' +import type { AgentArgsChoice } from './agent-args-choice' + +function AgentArgsChoiceChip({ + label, + flag, + onRemove, + disabled, + chipRef, + buttonRef, + choiceKey, + source +}: { + label: string + flag: string + onRemove: () => void + disabled?: boolean + chipRef?: Ref + buttonRef?: Ref + choiceKey: string + source?: 'arguments' +}): React.JSX.Element { + const t = useTranslate() + return ( + + + + {flag} + + + + + {source === 'arguments' + ? t('agentPicker.argumentsHint', 'Applies only to CLI launches.') + : choiceKey !== 'permissionMode' + ? t( + 'settings.agents.choiceChipTooltip', + 'Added by {{choice}}. Remove it to use the default.', + { + choice: + choiceKey === 'model' + ? t('settings.sourceControl.actionModel.model', 'Model') + : t('settings.sourceControl.actionModel.effort', 'Effort') + } + ) + : t( + 'settings.agents.permissionChipTooltip', + 'Added by Permissions. Remove it to use {{agent}}’s own settings.', + { agent: label } + )} + + + ) +} + +export function AgentArgsChoiceChips({ + chips, + disabled, + chipRef, + buttonRef, + onRemove +}: { + chips: readonly AgentArgsChoice[] + disabled?: boolean + chipRef?: Ref + buttonRef?: Ref + onRemove: (chip: AgentArgsChoice) => void +}): React.JSX.Element { + return ( + + {chips.map((chip, index) => ( + onRemove(chip)} + /> + ))} + + ) +} diff --git a/src/renderer/src/components/agent/launch-settings/AgentChatChoiceControls.tsx b/src/renderer/src/components/agent/launch-settings/AgentChatChoiceControls.tsx index 8d325ff20ec..10c739f54dd 100644 --- a/src/renderer/src/components/agent/launch-settings/AgentChatChoiceControls.tsx +++ b/src/renderer/src/components/agent/launch-settings/AgentChatChoiceControls.tsx @@ -4,15 +4,13 @@ import { useTranslate } from '@/i18n/use-translate' import { antigravityChatSignInRunning } from '@/runtime/provider-accounts/sign-in/antigravity-chat-sign-in-status' import { useManagedRuntimeInstall } from '@/runtime/hosts/installation/use-managed-runtime-install' import { ManagedRuntimeInstallRow } from '../../managed-runtime/ManagedRuntimeInstallRow' -import { useAntigravityChatSignIn } from '../../provider-accounts/AntigravityChatSignInRow' -import { - AntigravityProviderSignInArea, - antigravityProviderSignInShown -} from '../../provider-accounts/AntigravityProviderSignInArea' +import { useAntigravityChatSignIn } from '../../provider-accounts/use-antigravity-chat-sign-in' +import { AntigravityProviderSignInArea } from '../../provider-accounts/AntigravityProviderSignInArea' +import { antigravityProviderSignInShown } from '../../provider-accounts/antigravity-chat-runtime-sign-in-policy' import { getHostRuntimeLabel, getRuntimeSentenceLabel -} from '../../settings/accounts/scope/accounts-pane-runtime' +} from '../../../lib/provider-accounts/account-runtime-label' import type { AgentOpensAs } from '../../../lib/agent-session/launch/native-chat-provider-choice' import { SettingsSegmentedControl } from '../../settings/form-controls/SettingsFormControls' import { structuredChatUnavailableOnPlatform } from '../../../../../shared/agent-session/structured-chat-platform-support' diff --git a/src/renderer/src/components/agent/launch-settings/AgentChoiceControls.tsx b/src/renderer/src/components/agent/launch-settings/AgentChoiceControls.tsx index 0f5d09aecd0..f79f0fc0178 100644 --- a/src/renderer/src/components/agent/launch-settings/AgentChoiceControls.tsx +++ b/src/renderer/src/components/agent/launch-settings/AgentChoiceControls.tsx @@ -2,13 +2,11 @@ import type { AgentChoicePatch, AgentDedicatedChoices } from '../../../../../shared/agent-launch/choices/agent-launch-choices' - import { reconcileAgentChoiceArgs } from '../../../../../shared/agent-launch/choices/agent-choice-reconciliation' import { findCatalogModel, getAgentSessionOptionLaunchCatalog } from '../../../../../shared/agent-session/options/agent-session-option-catalog' - import { sourceControlActionAgentPicksModel, sourceControlActionOptions @@ -16,7 +14,7 @@ import { import { encodeCatalogOptionValue } from '../../../../../shared/agent-session/options/structured-agent-session-option-codec' import type { TuiAgent } from '../../../../../shared/agent-launch/catalog/tui-agent' import { AgentChoiceMenu } from './AgentChoiceMenu' -import { sourceControlActionRouteHint } from './AgentChoiceOptionField' +import { sourceControlActionRouteHint } from './agent-choice-route-hint' import { useTranslate } from '@/i18n/use-translate' const AGENT_DEFAULT_VALUE = '__agent_default__' diff --git a/src/renderer/src/components/agent/launch-settings/AgentChoiceOptionField.tsx b/src/renderer/src/components/agent/launch-settings/AgentChoiceOptionField.tsx index 6d1c2c9a4f3..476815e0c6b 100644 --- a/src/renderer/src/components/agent/launch-settings/AgentChoiceOptionField.tsx +++ b/src/renderer/src/components/agent/launch-settings/AgentChoiceOptionField.tsx @@ -2,9 +2,6 @@ import type { ReactNode } from 'react' import { Label } from '../../ui/label' import { SelectTrigger, SelectValue } from '../../ui/select' import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip' -import { translate } from '@/i18n/i18n' -import type { Translate } from '@/i18n/use-translate' -import type { SourceControlActionChoiceRoute } from '../../../../../shared/source-control/ai/recipes/source-control-action-options' export function SourceControlActionOptionField({ label, @@ -24,18 +21,6 @@ export function SourceControlActionOptionField({ ) } -/** Says which launches a saved choice reaches when it is not both. */ -export function sourceControlActionRouteHint( - route: SourceControlActionChoiceRoute, - t: Translate = translate -): string | null { - return route === 'chats' - ? t('settings.sourceControl.actionModel.chatsOnly', 'Applies to chats only') - : route === 'terminals' - ? t('settings.sourceControl.actionModel.terminalsOnly', 'Applies to terminals only') - : null -} - export function SourceControlActionOptionTrigger({ label, disabled diff --git a/src/renderer/src/components/agent/launch-settings/AgentLaunchChoiceEditor.tsx b/src/renderer/src/components/agent/launch-settings/AgentLaunchChoiceEditor.tsx index 578f84149b1..6e95245b19c 100644 --- a/src/renderer/src/components/agent/launch-settings/AgentLaunchChoiceEditor.tsx +++ b/src/renderer/src/components/agent/launch-settings/AgentLaunchChoiceEditor.tsx @@ -5,13 +5,12 @@ import { type AgentChoicePatch } from '../../../../../shared/agent-launch/choices/agent-launch-choices' import { AgentDefaultArgsInput } from './AgentLaunchDefaultsEditor' -import { AgentPermissionMenu, type AgentPermissionChoiceRow } from './AgentPermissionChoiceControls' +import { AgentPermissionMenu } from './AgentPermissionMenu' +import type { AgentPermissionChoiceRow } from './agent-permission-choice-row' import { agentModelChoiceChips } from './agent-model-choice-chips' import { useAgentLaunchFieldWrite } from './use-agent-launch-field-write' import { reconcileAgentChoiceArgs } from '../../../../../shared/agent-launch/choices/agent-choice-reconciliation' - import type { TuiAgent } from '../../../../../shared/agent-launch/catalog/tui-agent' - import { useTranslate } from '@/i18n/use-translate' import { AgentChoiceFields } from './AgentChoiceControls' diff --git a/src/renderer/src/components/agent/launch-settings/AgentLaunchDefaultsEditor.tsx b/src/renderer/src/components/agent/launch-settings/AgentLaunchDefaultsEditor.tsx index f6ec4790271..2880c1a64ed 100644 --- a/src/renderer/src/components/agent/launch-settings/AgentLaunchDefaultsEditor.tsx +++ b/src/renderer/src/components/agent/launch-settings/AgentLaunchDefaultsEditor.tsx @@ -9,7 +9,8 @@ import { type AgentDefaultEnvDraftInvalidReason } from './agent-default-env-draft' import { useAgentLaunchFieldWrite } from './use-agent-launch-field-write' -import { AgentArgsChoiceChips, type AgentArgsChoice } from './AgentPermissionChoiceControls' +import { AgentArgsChoiceChips } from './AgentArgsChoiceChips' +import type { AgentArgsChoice } from './agent-args-choice' export function AgentCommandOverrideInput({ defaultCmd, diff --git a/src/renderer/src/components/agent/launch-settings/AgentPermissionChoiceControls.tsx b/src/renderer/src/components/agent/launch-settings/AgentPermissionChoiceControls.tsx deleted file mode 100644 index e0e630ca7cb..00000000000 --- a/src/renderer/src/components/agent/launch-settings/AgentPermissionChoiceControls.tsx +++ /dev/null @@ -1,386 +0,0 @@ -import { AgentPermissionMenuOptions } from './AgentPermissionMenuOptions' -import { ChevronDown, X } from 'lucide-react' -import type { Ref } from 'react' -import type { AgentChatPermissionMode } from '../../../../../shared/agent-launch/permissions/agent-chat-permission-mode' -import type { AgentPermissionChoice } from '../../../../../shared/agent-launch/permissions/tui-agent-permission-choice' -import { translate } from '@/i18n/i18n' -import { useTranslate } from '@/i18n/use-translate' -import type { Translate } from '@/i18n/use-translate' -import { cn } from '@/lib/utils' -import { Button } from '../../ui/button' -import { Label } from '../../ui/label' -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuRadioGroup, - DropdownMenuTrigger -} from '../../ui/dropdown-menu' -import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip' -import { nativeChatPermissionModeLabel } from '../../native-chat/session/options/controls/native-chat-permission-mode-labels' -import { AgentPermissionChoiceItems, AgentPermissionChoiceName } from './AgentPermissionChoiceItems' - -/** Use chat-mode wording; own names the agent when available. */ -export function agentPermissionChoiceLabel( - choice: AgentPermissionChoice, - agentLabel: string | null, - t: Translate = translate -): string { - if (choice !== 'own') { - return nativeChatPermissionModeLabel(choice, t) - } - return agentLabel - ? t('settings.agents.permissionOwnAgent', "Use {{agent}}'s own settings", { - agent: agentLabel - }) - : t('settings.agents.permissionOwnAll', "Agents' own settings") -} - -export function AllAgentsPermissionSetting({ - chatOwnSupported = false, - choice, - withoutAutoChoice, - onChange, - onChangeWithoutAuto -}: { - chatOwnSupported?: boolean - choice: AgentPermissionChoice | null - /** What the agents without Approve for me share; null when they differ. */ - withoutAutoChoice: AgentPermissionChoice | null - onChange: (choice: AgentPermissionChoice) => void - onChangeWithoutAuto: (choice: AgentPermissionChoice) => void -}): React.JSX.Element { - const t = useTranslate() - const title = t('settings.agents.permissionsTitle', 'Permissions') - const ownLabel = t('settings.agents.permissionEachOwn', "Each agent's own settings") - const withoutAutoTitle = t( - 'settings.agents.otherAgentsPermissionsLabel', - 'Permissions for other agents' - ) - return ( -
-
-

{title}

- - - - - - - - - - -
-

- {t( - 'settings.agents.permissionsChoiceExplanation', - 'What every agent may do without asking. You can set one agent differently below.' - )} -

-
- {choice === 'auto' ? ( -
- - {t('settings.agents.permissionWithoutAuto', 'Agents without Approve for me')} - - - - - - - - - - - -
- ) : null} -
-
- ) -} - -export type AgentPermissionChoiceRow = { - chatOwnSupported?: boolean - choices: readonly AgentPermissionChoice[] - choice: AgentPermissionChoice - source: 'agent' | 'all' - terminalChoice?: AgentPermissionChoice - chatChoice?: AgentChatPermissionMode | null - /** What this agent gets from the all-agents choice. */ - defaultChoice: AgentPermissionChoice - onSelect: (choice: AgentPermissionChoice | 'default') => void - /** The flag terminals add, shown as a chip in Arguments; empty when none. */ - flag: string - /** The agent's own Arguments set permissions, so terminals use those instead. */ - argsSetPermission: boolean -} - -export function AgentPermissionMenu({ - agentId, - label, - permission, - inheritLabel, - onOpenChange, - disabled, - size = 'xs', - layout = 'inline', - showInherited = true, - rawValue -}: { - agentId: string - label: string - permission: AgentPermissionChoiceRow - inheritLabel?: string - onOpenChange?: (open: boolean) => void - disabled?: boolean - size?: 'xs' | 'sm' - /** Row: muted label left and the menu right, like the chat composer's option rows. */ - layout?: 'inline' | 'stacked' | 'row' - showInherited?: boolean - rawValue?: string -}): React.JSX.Element { - const t = useTranslate() - const followAll = inheritLabel ?? t('settings.agents.permissionSameAsAll', 'Same as all agents') - const ownLabel = agentPermissionChoiceLabel('own', label, t) - return ( -
- {layout === 'row' ? ( - - {t('settings.agents.rowPermissions', 'Permissions')} - - ) : layout === 'stacked' ? ( - - ) : ( - - {t('settings.agents.rowPermissions', 'Permissions')} - - )} - - - - - - - - - {permission.terminalChoice === 'own' && permission.chatChoice ? ( - - {t( - 'settings.agents.permissionSurfaceDifference', - 'Terminals use the agent’s own settings; new chats use {{mode}}.', - { mode: nativeChatPermissionModeLabel(permission.chatChoice, t) } - )} - - ) : null} -
- ) -} - -function AgentArgsChoiceChip({ - label, - flag, - onRemove, - disabled, - chipRef, - buttonRef, - choiceKey, - source -}: { - label: string - flag: string - onRemove: () => void - disabled?: boolean - chipRef?: Ref - buttonRef?: Ref - choiceKey: string - source?: 'arguments' -}): React.JSX.Element { - const t = useTranslate() - return ( - - - - {flag} - - - - - {source === 'arguments' - ? t('agentPicker.argumentsHint', 'Applies only to CLI launches.') - : choiceKey !== 'permissionMode' - ? t( - 'settings.agents.choiceChipTooltip', - 'Added by {{choice}}. Remove it to use the default.', - { - choice: - choiceKey === 'model' - ? t('settings.sourceControl.actionModel.model', 'Model') - : t('settings.sourceControl.actionModel.effort', 'Effort') - } - ) - : t( - 'settings.agents.permissionChipTooltip', - 'Added by Permissions. Remove it to use {{agent}}’s own settings.', - { agent: label } - )} - - - ) -} - -export type AgentArgsChoice = { - key: string - label: string - flag: string - onRemove: (text: string) => string - source?: 'arguments' -} - -export function AgentArgsChoiceChips({ - chips, - disabled, - chipRef, - buttonRef, - onRemove -}: { - chips: readonly AgentArgsChoice[] - disabled?: boolean - chipRef?: Ref - buttonRef?: Ref - onRemove: (chip: AgentArgsChoice) => void -}): React.JSX.Element { - return ( - - {chips.map((chip, index) => ( - onRemove(chip)} - /> - ))} - - ) -} diff --git a/src/renderer/src/components/agent/launch-settings/AgentPermissionMenu.tsx b/src/renderer/src/components/agent/launch-settings/AgentPermissionMenu.tsx new file mode 100644 index 00000000000..5755c37a089 --- /dev/null +++ b/src/renderer/src/components/agent/launch-settings/AgentPermissionMenu.tsx @@ -0,0 +1,107 @@ +import { useTranslate } from '../../../i18n/use-translate' +import { cn } from '../../../lib/utils' +import { ChevronDown } from 'lucide-react' +import { Button } from '../../ui/button' +import { Label } from '../../ui/label' +import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '../../ui/dropdown-menu' +import { nativeChatPermissionModeLabel } from '../../native-chat/session/options/controls/native-chat-permission-mode-labels' +import { AgentPermissionChoiceName } from './AgentPermissionChoiceItems' +import { AgentPermissionMenuOptions } from './AgentPermissionMenuOptions' +import type { AgentPermissionChoiceRow } from './agent-permission-choice-row' +import { agentPermissionChoiceLabel } from './agent-permission-choice-label' + +export function AgentPermissionMenu({ + agentId, + label, + permission, + inheritLabel, + onOpenChange, + disabled, + size = 'xs', + layout = 'inline', + showInherited = true, + rawValue +}: { + agentId: string + label: string + permission: AgentPermissionChoiceRow + inheritLabel?: string + onOpenChange?: (open: boolean) => void + disabled?: boolean + size?: 'xs' | 'sm' + /** Row: muted label left and the menu right, like the chat composer's option rows. */ + layout?: 'inline' | 'stacked' | 'row' + showInherited?: boolean + rawValue?: string +}): React.JSX.Element { + const t = useTranslate() + const followAll = inheritLabel ?? t('settings.agents.permissionSameAsAll', 'Same as all agents') + const ownLabel = agentPermissionChoiceLabel('own', label, t) + return ( +
+ {layout === 'row' ? ( + + {t('settings.agents.rowPermissions', 'Permissions')} + + ) : layout === 'stacked' ? ( + + ) : ( + + {t('settings.agents.rowPermissions', 'Permissions')} + + )} + + + + + + + + + {permission.terminalChoice === 'own' && permission.chatChoice ? ( + + {t( + 'settings.agents.permissionSurfaceDifference', + 'Terminals use the agent’s own settings; new chats use {{mode}}.', + { mode: nativeChatPermissionModeLabel(permission.chatChoice, t) } + )} + + ) : null} +
+ ) +} diff --git a/src/renderer/src/components/agent/launch-settings/AgentPermissionMenuOptions.tsx b/src/renderer/src/components/agent/launch-settings/AgentPermissionMenuOptions.tsx index 952fcc138b4..c5469c2802a 100644 --- a/src/renderer/src/components/agent/launch-settings/AgentPermissionMenuOptions.tsx +++ b/src/renderer/src/components/agent/launch-settings/AgentPermissionMenuOptions.tsx @@ -1,4 +1,4 @@ -import type { AgentPermissionChoiceRow } from './AgentPermissionChoiceControls' +import type { AgentPermissionChoiceRow } from './agent-permission-choice-row' import { AgentPermissionChoiceItems, AgentPermissionChoiceName } from './AgentPermissionChoiceItems' import { DropdownMenuRadioGroup, diff --git a/src/renderer/src/components/agent/launch-settings/agent-args-choice.ts b/src/renderer/src/components/agent/launch-settings/agent-args-choice.ts new file mode 100644 index 00000000000..1ce39193b46 --- /dev/null +++ b/src/renderer/src/components/agent/launch-settings/agent-args-choice.ts @@ -0,0 +1,7 @@ +export type AgentArgsChoice = { + key: string + label: string + flag: string + onRemove: (text: string) => string + source?: 'arguments' +} diff --git a/src/renderer/src/components/agent/launch-settings/agent-choice-route-hint.ts b/src/renderer/src/components/agent/launch-settings/agent-choice-route-hint.ts new file mode 100644 index 00000000000..bbdb0704342 --- /dev/null +++ b/src/renderer/src/components/agent/launch-settings/agent-choice-route-hint.ts @@ -0,0 +1,15 @@ +import { translate } from '../../../i18n/i18n' +import type { Translate } from '../../../i18n/use-translate' +import type { SourceControlActionChoiceRoute } from '../../../../../shared/source-control/ai/recipes/source-control-action-options' + +/** Says which launches a saved choice reaches when it is not both. */ +export function sourceControlActionRouteHint( + route: SourceControlActionChoiceRoute, + t: Translate = translate +): string | null { + return route === 'chats' + ? t('settings.sourceControl.actionModel.chatsOnly', 'Applies to chats only') + : route === 'terminals' + ? t('settings.sourceControl.actionModel.terminalsOnly', 'Applies to terminals only') + : null +} diff --git a/src/renderer/src/components/agent/launch-settings/agent-model-choice-chips.ts b/src/renderer/src/components/agent/launch-settings/agent-model-choice-chips.ts index b3b66c6a36a..6a7f68d79bf 100644 --- a/src/renderer/src/components/agent/launch-settings/agent-model-choice-chips.ts +++ b/src/renderer/src/components/agent/launch-settings/agent-model-choice-chips.ts @@ -10,7 +10,7 @@ import type { } from '../../../../../shared/agent-launch/choices/agent-launch-choices' import type { AgentArgsProjectionContext } from '../../../../../shared/agent-launch/picker/agent-launch-picker-arguments' import type { TuiAgent } from '../../../../../shared/agent-launch/catalog/tui-agent' -import type { AgentArgsChoice } from './AgentPermissionChoiceControls' +import type { AgentArgsChoice } from './agent-args-choice' export function agentModelChoiceChips( agent: TuiAgent | null | undefined, diff --git a/src/renderer/src/components/agent/launch-settings/agent-permission-choice-label.ts b/src/renderer/src/components/agent/launch-settings/agent-permission-choice-label.ts new file mode 100644 index 00000000000..5ebe2a5a566 --- /dev/null +++ b/src/renderer/src/components/agent/launch-settings/agent-permission-choice-label.ts @@ -0,0 +1,20 @@ +import type { AgentPermissionChoice } from '../../../../../shared/agent-launch/permissions/tui-agent-permission-choice' +import { translate } from '../../../i18n/i18n' +import type { Translate } from '../../../i18n/use-translate' +import { nativeChatPermissionModeLabel } from '../../native-chat/session/options/controls/native-chat-permission-mode-labels' + +/** Use chat-mode wording; own names the agent when available. */ +export function agentPermissionChoiceLabel( + choice: AgentPermissionChoice, + agentLabel: string | null, + t: Translate = translate +): string { + if (choice !== 'own') { + return nativeChatPermissionModeLabel(choice, t) + } + return agentLabel + ? t('settings.agents.permissionOwnAgent', "Use {{agent}}'s own settings", { + agent: agentLabel + }) + : t('settings.agents.permissionOwnAll', "Agents' own settings") +} diff --git a/src/renderer/src/components/agent/launch-settings/agent-permission-choice-row.ts b/src/renderer/src/components/agent/launch-settings/agent-permission-choice-row.ts new file mode 100644 index 00000000000..9e282eb6f39 --- /dev/null +++ b/src/renderer/src/components/agent/launch-settings/agent-permission-choice-row.ts @@ -0,0 +1,18 @@ +import type { AgentChatPermissionMode } from '../../../../../shared/agent-launch/permissions/agent-chat-permission-mode' +import type { AgentPermissionChoice } from '../../../../../shared/agent-launch/permissions/tui-agent-permission-choice' + +export type AgentPermissionChoiceRow = { + chatOwnSupported?: boolean + choices: readonly AgentPermissionChoice[] + choice: AgentPermissionChoice + source: 'agent' | 'all' + terminalChoice?: AgentPermissionChoice + chatChoice?: AgentChatPermissionMode | null + /** What this agent gets from the all-agents choice. */ + defaultChoice: AgentPermissionChoice + onSelect: (choice: AgentPermissionChoice | 'default') => void + /** The flag terminals add, shown as a chip in Arguments; empty when none. */ + flag: string + /** The agent's own Arguments set permissions, so terminals use those instead. */ + argsSetPermission: boolean +} diff --git a/src/renderer/src/components/provider-accounts/AntigravityChatAuthForm.tsx b/src/renderer/src/components/provider-accounts/AntigravityChatAuthForm.tsx index 2ad72f75cb7..8da4de826dc 100644 --- a/src/renderer/src/components/provider-accounts/AntigravityChatAuthForm.tsx +++ b/src/renderer/src/components/provider-accounts/AntigravityChatAuthForm.tsx @@ -16,7 +16,7 @@ import { type AntigravityChatAuthConfig } from '../../../../shared/antigravity-chat-auth-config' import { Input } from '../ui/input' -import { SettingRow } from './AntigravityChatAuthSettingRow' +import { AntigravityChatAuthSettingRow } from './AntigravityChatAuthSettingRow' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select' export type AntigravityChatAuthAction = { @@ -296,7 +296,7 @@ function AuthForm({ {heading} {!unavailable && (
- {methodLabel('oauth-business', t)} - + {method === 'oauth-business' && ( <> - @@ -352,8 +352,8 @@ function AuthForm({ onBlur={commitText} onKeyDown={commitOnEnter} /> - - + @@ -370,7 +370,7 @@ function AuthForm({ onBlur={commitText} onKeyDown={commitOnEnter} /> - + )}
diff --git a/src/renderer/src/components/provider-accounts/AntigravityChatAuthSettingRow.tsx b/src/renderer/src/components/provider-accounts/AntigravityChatAuthSettingRow.tsx index be70802fde1..8ee426a34ff 100644 --- a/src/renderer/src/components/provider-accounts/AntigravityChatAuthSettingRow.tsx +++ b/src/renderer/src/components/provider-accounts/AntigravityChatAuthSettingRow.tsx @@ -2,7 +2,7 @@ import type { ReactNode } from 'react' import { Label } from '../ui/label' /** Label and short description on the left, its control on the right. */ -export function SettingRow(props: { +export function AntigravityChatAuthSettingRow(props: { htmlFor: string label: string description?: string | null diff --git a/src/renderer/src/components/provider-accounts/AntigravityChatSignInAction.tsx b/src/renderer/src/components/provider-accounts/AntigravityChatSignInAction.tsx index 116ff387d3c..2b257e8d745 100644 --- a/src/renderer/src/components/provider-accounts/AntigravityChatSignInAction.tsx +++ b/src/renderer/src/components/provider-accounts/AntigravityChatSignInAction.tsx @@ -8,7 +8,7 @@ import { Badge } from '../ui/badge' import { Button } from '../ui/button' import { Input } from '../ui/input' import type { AntigravityChatAuthAction } from './AntigravityChatAuthForm' -import type { AntigravityChatSignInControls } from './AntigravityChatSignInRow' +import type { AntigravityChatSignInControls } from './use-antigravity-chat-sign-in' export function AntigravityChatSignInBadge({ signIn diff --git a/src/renderer/src/components/provider-accounts/AntigravityChatSignInRow.tsx b/src/renderer/src/components/provider-accounts/AntigravityChatSignInRow.tsx index d9da2125f18..48da9809e04 100644 --- a/src/renderer/src/components/provider-accounts/AntigravityChatSignInRow.tsx +++ b/src/renderer/src/components/provider-accounts/AntigravityChatSignInRow.tsx @@ -1,185 +1,12 @@ import { AntigravityChatAuthForm } from './AntigravityChatAuthForm' -import { useEffect, useState, useSyncExternalStore, type ReactNode } from 'react' -import { translate } from '@/i18n/i18n' -import { useTranslate, type Translate } from '@/i18n/use-translate' -import { useClipboardTextCopyFeedback } from '@/hooks/use-clipboard-text-copy-feedback' -import { - antigravityChatSignInRunning as running, - cancelAntigravityChatSignIn, - completeAntigravityChatSignIn, - observeAntigravityChatSignIn, - readAntigravityChatSignIn, - startAntigravityChatSignIn, - subscribeAntigravityChatSignIns, - type AntigravityChatSignInView -} from '@/runtime/provider-accounts/sign-in/antigravity-chat-sign-in-status' +import type { ReactNode } from 'react' +import { useTranslate } from '../../i18n/use-translate' import type { RuntimeClientTarget } from '../../../../shared/runtime-client-target' -import type { - AntigravityChatAuth, - AntigravityChatSignInState -} from '../../../../shared/antigravity-account-types' -import type { ManagedRuntimeInstallObservation } from '@/runtime/hosts/installation/use-managed-runtime-install' import { AntigravityChatSignInAction, AntigravityChatSignInBadge } from './AntigravityChatSignInAction' - -type Flow = AntigravityChatSignInState - -/** A running flow another client started: the host tells only its starter the id. */ -const otherClients = (flow: Flow | null): boolean => running(flow) && flow.flowId === null - -/** Progress of a sign-in flow; signed-in or signed-out state shows in the status badge instead. */ -function chatSignInLine( - flow: Flow | null, - auth: AntigravityChatAuth, - openFailed: boolean, - t: Translate = translate -): string | null { - if (otherClients(flow)) { - return t('accounts.antigravity.chatOtherClient', 'Sign-in is in progress in another client.') - } - switch (flow?.phase) { - case 'starting': - return t('accounts.antigravity.chatStarting', 'Starting sign-in…') - case 'waiting': - return openFailed - ? t('accounts.antigravity.chatOpenFailed', 'Copy the link and open it in your browser.') - : t('accounts.antigravity.chatWaiting', 'Finish signing in in your browser.') - case 'verifying': - return t('accounts.antigravity.chatVerifying', 'Finishing sign-in…') - // A settled flow only stands in until the host answers whether it is signed in. - case 'cancelled': - return auth === 'unknown' - ? t('accounts.antigravity.chatCancelled', 'Sign-in cancelled.') - : null - case 'expired': - return auth === 'unknown' - ? t('accounts.antigravity.chatExpired', 'Sign-in timed out. Try again.') - : null - case 'failed': - case 'signedIn': - case undefined: - return null - } -} - -type SignInAction = 'retry' | 'changeAccount' | 'signIn' - -function signInAction( - flow: Flow | null, - auth: AntigravityChatAuth, - error: string | null -): SignInAction { - if ( - error || - flow?.phase === 'failed' || - flow?.phase === 'cancelled' || - flow?.phase === 'expired' - ) { - return 'retry' - } - return auth === 'signedIn' ? 'changeAccount' : 'signIn' -} - -const SETTLED_OPERATION_PHASES = new Set(['succeeded', 'failed', 'cancelled']) - -/** Sign-in needs an installed server and no removal; updates keep the old copy usable, while unreported status defers to the host. */ -export function antigravityChatSignInBlocked( - install: Extract -): boolean { - const { snapshot } = install - if (snapshot.kind !== 'known') { - return snapshot.kind === 'unknown' - } - const { source, operation } = snapshot.state - return !source || (operation?.kind === 'remove' && !SETTLED_OPERATION_PHASES.has(operation.phase)) -} - -const NOT_FOLLOWED: AntigravityChatSignInView = { - status: 'unknown', - flow: null, - auth: 'unknown', - error: null, - openFailed: false -} - -export function useAntigravityChatSignIn( - owner: RuntimeClientTarget | null -): AntigravityChatSignInView { - const shown = owner !== null - const environmentId = owner?.kind === 'environment' ? owner.environmentId : null - useEffect( - () => - shown - ? observeAntigravityChatSignIn( - environmentId === null ? { kind: 'local' } : { kind: 'environment', environmentId } - ) - : undefined, - [shown, environmentId] - ) - const read = (): AntigravityChatSignInView => - owner ? readAntigravityChatSignIn(owner) : NOT_FOLLOWED - return useSyncExternalStore(subscribeAntigravityChatSignIns, read, read) -} - -/** Share host auth across Settings layouts; auth stays unknown until a chat or sign-in asks the host. */ -export type AntigravityChatSignInControls = ReturnType - -export function useAntigravityChatSignInControls(props: { - owner: RuntimeClientTarget - label: string - disabled: boolean -}) { - const t = useTranslate() - const { owner, label } = props - const { status, flow, auth, openFailed, ...view } = useAntigravityChatSignIn(owner) - const [pending, setPending] = useState(false) - const act = (request: () => Promise): void => { - setPending(true) - void request().finally(() => setPending(false)) - } - const copy = useClipboardTextCopyFeedback(flow?.url ?? '') - const failed = t('accounts.antigravity.chatFailed', 'Sign-in failed. Try again.') - const error = view.error === null ? null : view.error || failed - const copyFailed = t('accounts.antigravity.chatCopyFailed', 'Could not copy the link.') - const readFailed = t( - 'accounts.antigravity.chatReadFailed', - 'Could not check the sign-in on {{host}}.', - { host: label } - ) - const ownFlow = running(flow) && flow.flowId !== null - const action = signInAction(flow, auth, error) - const line = status === 'known' ? chatSignInLine(flow, auth, openFailed, t) : null - return { - flow, - auth: status === 'known' ? auth : 'unknown', - signingIn: running(flow), - ownFlow, - url: ownFlow && flow?.phase === 'waiting' ? (flow.url ?? null) : null, - line, - alert: - error ?? - (status === 'readFailed' ? readFailed : null) ?? - (flow?.phase === 'failed' ? (flow.error ?? failed) : null) ?? - (copy.status === 'failed' ? copyFailed : null), - action, - pending, - copy, - copyLabel: t('accounts.antigravity.chatCopyLink', 'Copy sign-in link'), - signInDisabled: props.disabled || pending || status !== 'known' || otherClients(flow), - recoveryDisabled: props.disabled || pending || otherClients(flow), - start: () => act(() => startAntigravityChatSignIn(owner)), - cancel: () => act(() => cancelAntigravityChatSignIn(owner)), - send: (callbackUrl: string, refused: (message: string) => void) => - act(async () => { - const refusal = await completeAntigravityChatSignIn(owner, callbackUrl) - if (refusal !== null) { - refused(refusal || failed) - } - }) - } -} +import { useAntigravityChatSignInControls } from './use-antigravity-chat-sign-in' /** All rows for an execution host share its server’s sign-in, including running flows. */ export function AntigravityChatSignInRow(props: { diff --git a/src/renderer/src/components/provider-accounts/AntigravityProviderSignInArea.tsx b/src/renderer/src/components/provider-accounts/AntigravityProviderSignInArea.tsx index 43c45978528..7d467b96505 100644 --- a/src/renderer/src/components/provider-accounts/AntigravityProviderSignInArea.tsx +++ b/src/renderer/src/components/provider-accounts/AntigravityProviderSignInArea.tsx @@ -6,25 +6,11 @@ import { AntigravityChatSignInAction, AntigravityChatSignInBadge } from './AntigravityChatSignInAction' -import { - antigravityChatSignInBlocked, - useAntigravityChatSignInControls -} from './AntigravityChatSignInRow' +import { antigravityChatSignInBlocked } from './antigravity-chat-runtime-sign-in-policy' +import { useAntigravityChatSignInControls } from './use-antigravity-chat-sign-in' type ActiveInstall = Extract -const CHANGING_PHASES = new Set(['downloading', 'extracting', 'verifying', 'removing']) - -/** Require an installed server outside install/removal; retain space while host status is unknown. */ -export function antigravityProviderSignInShown(install: ActiveInstall): boolean { - const { snapshot } = install - if (snapshot.kind !== 'known') { - return true - } - const { source, operation } = snapshot.state - return source !== null && !(operation && CHANGING_PHASES.has(operation.phase)) -} - export function AntigravityProviderSignInArea(props: { owner: RuntimeClientTarget label: string diff --git a/src/renderer/src/components/provider-accounts/antigravity-chat-runtime-sign-in-policy.ts b/src/renderer/src/components/provider-accounts/antigravity-chat-runtime-sign-in-policy.ts new file mode 100644 index 00000000000..6acc801a8f3 --- /dev/null +++ b/src/renderer/src/components/provider-accounts/antigravity-chat-runtime-sign-in-policy.ts @@ -0,0 +1,29 @@ +import type { ManagedRuntimeInstallObservation } from '../../runtime/hosts/installation/use-managed-runtime-install' + +const SETTLED_OPERATION_PHASES = new Set(['succeeded', 'failed', 'cancelled']) + +/** Sign-in needs an installed server and no removal; updates keep the old copy usable, while unreported status defers to the host. */ +export function antigravityChatSignInBlocked( + install: Extract +): boolean { + const { snapshot } = install + if (snapshot.kind !== 'known') { + return snapshot.kind === 'unknown' + } + const { source, operation } = snapshot.state + return !source || (operation?.kind === 'remove' && !SETTLED_OPERATION_PHASES.has(operation.phase)) +} + +const CHANGING_PHASES = new Set(['downloading', 'extracting', 'verifying', 'removing']) + +/** Require an installed server outside install/removal; retain space while host status is unknown. */ +export function antigravityProviderSignInShown( + install: Extract +): boolean { + const { snapshot } = install + if (snapshot.kind !== 'known') { + return true + } + const { source, operation } = snapshot.state + return source !== null && !(operation && CHANGING_PHASES.has(operation.phase)) +} diff --git a/src/renderer/src/components/provider-accounts/use-antigravity-chat-sign-in.ts b/src/renderer/src/components/provider-accounts/use-antigravity-chat-sign-in.ts new file mode 100644 index 00000000000..a476f14331a --- /dev/null +++ b/src/renderer/src/components/provider-accounts/use-antigravity-chat-sign-in.ts @@ -0,0 +1,161 @@ +import { useEffect, useState, useSyncExternalStore } from 'react' +import { translate } from '../../i18n/i18n' +import { useTranslate, type Translate } from '../../i18n/use-translate' +import { useClipboardTextCopyFeedback } from '../../hooks/use-clipboard-text-copy-feedback' +import { + antigravityChatSignInRunning as running, + cancelAntigravityChatSignIn, + completeAntigravityChatSignIn, + observeAntigravityChatSignIn, + readAntigravityChatSignIn, + startAntigravityChatSignIn, + subscribeAntigravityChatSignIns, + type AntigravityChatSignInView +} from '../../runtime/provider-accounts/sign-in/antigravity-chat-sign-in-status' +import type { RuntimeClientTarget } from '../../../../shared/runtime-client-target' +import type { + AntigravityChatAuth, + AntigravityChatSignInState +} from '../../../../shared/antigravity-account-types' + +/** A running flow another client started: the host tells only its starter the id. */ +const otherClients = (flow: AntigravityChatSignInState | null): boolean => + running(flow) && flow.flowId === null + +/** Progress of a sign-in flow; signed-in or signed-out state shows in the status badge instead. */ +function chatSignInLine( + flow: AntigravityChatSignInState | null, + auth: AntigravityChatAuth, + openFailed: boolean, + t: Translate = translate +): string | null { + if (otherClients(flow)) { + return t('accounts.antigravity.chatOtherClient', 'Sign-in is in progress in another client.') + } + switch (flow?.phase) { + case 'starting': + return t('accounts.antigravity.chatStarting', 'Starting sign-in…') + case 'waiting': + return openFailed + ? t('accounts.antigravity.chatOpenFailed', 'Copy the link and open it in your browser.') + : t('accounts.antigravity.chatWaiting', 'Finish signing in in your browser.') + case 'verifying': + return t('accounts.antigravity.chatVerifying', 'Finishing sign-in…') + // A settled flow only stands in until the host answers whether it is signed in. + case 'cancelled': + return auth === 'unknown' + ? t('accounts.antigravity.chatCancelled', 'Sign-in cancelled.') + : null + case 'expired': + return auth === 'unknown' + ? t('accounts.antigravity.chatExpired', 'Sign-in timed out. Try again.') + : null + case 'failed': + case 'signedIn': + case undefined: + return null + } +} + +type SignInAction = 'retry' | 'changeAccount' | 'signIn' + +function signInAction( + flow: AntigravityChatSignInState | null, + auth: AntigravityChatAuth, + error: string | null +): SignInAction { + if ( + error || + flow?.phase === 'failed' || + flow?.phase === 'cancelled' || + flow?.phase === 'expired' + ) { + return 'retry' + } + return auth === 'signedIn' ? 'changeAccount' : 'signIn' +} + +const NOT_FOLLOWED: AntigravityChatSignInView = { + status: 'unknown', + flow: null, + auth: 'unknown', + error: null, + openFailed: false +} + +export function useAntigravityChatSignIn( + owner: RuntimeClientTarget | null +): AntigravityChatSignInView { + const shown = owner !== null + const environmentId = owner?.kind === 'environment' ? owner.environmentId : null + useEffect( + () => + shown + ? observeAntigravityChatSignIn( + environmentId === null ? { kind: 'local' } : { kind: 'environment', environmentId } + ) + : undefined, + [shown, environmentId] + ) + const read = (): AntigravityChatSignInView => + owner ? readAntigravityChatSignIn(owner) : NOT_FOLLOWED + return useSyncExternalStore(subscribeAntigravityChatSignIns, read, read) +} + +/** Share host auth across Settings layouts; auth stays unknown until a chat or sign-in asks the host. */ +export type AntigravityChatSignInControls = ReturnType + +export function useAntigravityChatSignInControls(props: { + owner: RuntimeClientTarget + label: string + disabled: boolean +}) { + const t = useTranslate() + const { owner, label } = props + const { status, flow, auth, openFailed, ...view } = useAntigravityChatSignIn(owner) + const [pending, setPending] = useState(false) + const act = (request: () => Promise): void => { + setPending(true) + void request().finally(() => setPending(false)) + } + const copy = useClipboardTextCopyFeedback(flow?.url ?? '') + const failed = t('accounts.antigravity.chatFailed', 'Sign-in failed. Try again.') + const error = view.error === null ? null : view.error || failed + const copyFailed = t('accounts.antigravity.chatCopyFailed', 'Could not copy the link.') + const readFailed = t( + 'accounts.antigravity.chatReadFailed', + 'Could not check the sign-in on {{host}}.', + { host: label } + ) + const ownFlow = running(flow) && flow.flowId !== null + const action = signInAction(flow, auth, error) + const line = status === 'known' ? chatSignInLine(flow, auth, openFailed, t) : null + return { + flow, + auth: status === 'known' ? auth : 'unknown', + signingIn: running(flow), + ownFlow, + url: ownFlow && flow?.phase === 'waiting' ? (flow.url ?? null) : null, + line, + alert: + error ?? + (status === 'readFailed' ? readFailed : null) ?? + (flow?.phase === 'failed' ? (flow.error ?? failed) : null) ?? + (copy.status === 'failed' ? copyFailed : null), + action, + pending, + copy, + copyLabel: t('accounts.antigravity.chatCopyLink', 'Copy sign-in link'), + signInDisabled: props.disabled || pending || status !== 'known' || otherClients(flow), + recoveryDisabled: props.disabled || pending || otherClients(flow), + start: () => act(() => startAntigravityChatSignIn(owner)), + cancel: () => act(() => cancelAntigravityChatSignIn(owner)), + send: (callbackUrl: string, refused: (message: string) => void) => + act(async () => { + const refusal = await completeAntigravityChatSignIn(owner, callbackUrl) + if (refusal !== null) { + refused(refusal || failed) + } + }) + } +} diff --git a/src/renderer/src/components/right-sidebar/source-control/ai/source-control-action-model-copy.ts b/src/renderer/src/components/right-sidebar/source-control/ai/source-control-action-model-copy.ts index a64a2eb3ff2..0e7636a2b6e 100644 --- a/src/renderer/src/components/right-sidebar/source-control/ai/source-control-action-model-copy.ts +++ b/src/renderer/src/components/right-sidebar/source-control/ai/source-control-action-model-copy.ts @@ -11,8 +11,8 @@ import { import { sourceControlActionSelectedPreferences } from '../../../../../../shared/source-control/ai/recipes/source-control-action-launch-preferences' import { translate } from '@/i18n/i18n' import { AGENT_CATALOG } from '@/lib/agent-catalog' -import { agentPermissionChoiceLabel } from '../../../agent/launch-settings/AgentPermissionChoiceControls' -import { sourceControlActionRouteHint } from '../../../agent/launch-settings/AgentChoiceOptionField' +import { agentPermissionChoiceLabel } from '../../../agent/launch-settings/agent-permission-choice-label' +import { sourceControlActionRouteHint } from '../../../agent/launch-settings/agent-choice-route-hint' /** Describes the saved choices and which launches they reach. */ export function describeSourceControlActionModel( diff --git a/src/renderer/src/components/settings/accounts/AccountsPane.tsx b/src/renderer/src/components/settings/accounts/AccountsPane.tsx index be9aa57d2b7..09131bc6fb5 100644 --- a/src/renderer/src/components/settings/accounts/AccountsPane.tsx +++ b/src/renderer/src/components/settings/accounts/AccountsPane.tsx @@ -47,15 +47,11 @@ import { ZcodePlanAccountsSection } from './usage/ZcodePlanAccountsSection' import type { AccountsPaneProps, AccountsPaneSectionModel, - ClaudeAccountAction, - CodexAccountAction, + ProviderAccountAction, RemoveAccountTarget } from './accounts-pane-types' -import { - EMPTY_WSL_DISTROS, - getRuntimeSentenceLabel, - getSelectedAccountRuntime -} from './scope/accounts-pane-runtime' +import { EMPTY_WSL_DISTROS, getSelectedAccountRuntime } from './scope/accounts-pane-runtime' +import { getRuntimeSentenceLabel } from '../../../lib/provider-accounts/account-runtime-label' import { watchCodexConfigSyncStatus } from '../../../runtime/provider-accounts/codex-config-sync-observer' import { createClaudeAccountActionRunner, @@ -64,10 +60,8 @@ import { import { renderAccountsLocationSection } from './scope/accounts-pane-location-section' import { renderClaudeAccountsSection } from './claude/accounts-pane-claude-section' import { renderCodexAccountsSection } from './codex/accounts-pane-codex-section' -import { - renderGeminiAccountsSection, - renderOpenCodeAccountsSection -} from './providers/accounts-pane-provider-setting-sections' +import { renderGeminiAccountsSection } from './providers/accounts-pane-gemini-section' +import { renderOpenCodeAccountsSection } from './providers/accounts-pane-opencode-section' import { MiniMaxAccountsSection } from './usage/MiniMaxAccountsSection' import { useUsageHostSettings } from './usage/use-usage-host-settings' import { ManagedDataAccountsSection } from './providers/ManagedDataAccountsSection' @@ -148,10 +142,10 @@ export function AccountsPane({ const [codexAccounts, setCodexAccounts] = useState(emptyCodexAccountsState) const [codexAccountsLoaded, setCodexAccountsLoaded] = useState(false) - const [codexAction, setCodexAction] = useState('idle') + const [codexAction, setCodexAction] = useState('idle') const [claudeAccounts, setClaudeAccounts] = useState(emptyClaudeAccountsState) - const [claudeAction, setClaudeAction] = useState('idle') + const [claudeAction, setClaudeAction] = useState('idle') // Capture runtime slots at dialog opening; roster updates could remove the slot needed for restart diffs. const [removeCodexTarget, setRemoveCodexTarget] = useState(null) const [removeClaudeTarget, setRemoveClaudeTarget] = useState(null) diff --git a/src/renderer/src/components/settings/accounts/accounts-pane-account-actions.ts b/src/renderer/src/components/settings/accounts/accounts-pane-account-actions.ts index e6cc3880eef..b7956134520 100644 --- a/src/renderer/src/components/settings/accounts/accounts-pane-account-actions.ts +++ b/src/renderer/src/components/settings/accounts/accounts-pane-account-actions.ts @@ -17,18 +17,17 @@ import { getProviderAccountRuntime } from '../../provider-accounts/provider-account-visibility' import type { - ClaudeAccountAction, + ProviderAccountAction, ClaudeAccountActionRunner, - CodexAccountAction, - CodexAccountActionRunner, - LocalAccountRuntime + CodexAccountActionRunner } from './accounts-pane-types' +import type { LocalAccountRuntime } from '../../../lib/provider-accounts/account-runtime-label' import { getClaudeAccountErrorDescription, getCodexAccountErrorDescription, isClaudeAccountCancellation, isCodexAccountCancellation -} from './accounts-pane-action-errors' +} from '../../../lib/provider-accounts/provider-account-action-errors' import { getClaudeAccountLabel } from '@/lib/claude-account-label' import { handleRunningClaudeSessionsAfterSwitch } from '@/lib/claude-account-switch-sessions' @@ -41,7 +40,7 @@ type CodexActionContext = { codexAccounts: CodexRateLimitAccountsState setCodexAccounts: Dispatch> setCodexAccountsLoaded: Dispatch> - setCodexAction: Dispatch> + setCodexAction: Dispatch> fetchSettings: () => Promise recordFeatureInteraction: (featureId: FeatureInteractionId) => void /** False once the pane shows a different account owner than this action targeted. */ @@ -147,7 +146,7 @@ type ClaudeActionContext = { isRemoteAccountScope: boolean claudeAccounts: ClaudeRateLimitAccountsState setClaudeAccounts: Dispatch> - setClaudeAction: Dispatch> + setClaudeAction: Dispatch> fetchSettings: () => Promise recordFeatureInteraction: (featureId: FeatureInteractionId) => void /** False once the pane shows a different account owner than this action targeted. */ diff --git a/src/renderer/src/components/settings/accounts/accounts-pane-types.ts b/src/renderer/src/components/settings/accounts/accounts-pane-types.ts index dd37ec0d132..2456c828c47 100644 --- a/src/renderer/src/components/settings/accounts/accounts-pane-types.ts +++ b/src/renderer/src/components/settings/accounts/accounts-pane-types.ts @@ -13,6 +13,7 @@ import type { FeatureInteractionId } from '../../../../../shared/feature-interac import type { ProviderAccountRuntimeView } from '../../provider-accounts/provider-account-visibility' import type { RuntimeClientTarget } from '../../../../../shared/runtime-client-target' import type { SettingsHostScope } from '../host-scope/settings-host-scope' +import type { LocalAccountRuntime } from '../../../lib/provider-accounts/account-runtime-label' export type AccountsPaneProps = { settings: GlobalSettings @@ -30,21 +31,13 @@ export type AccountsPaneProps = { wslHostDistro?: string | null } -export type LocalAccountRuntime = { - runtime: 'host' | 'wsl' - wslDistro?: string | null - label: string -} - -export type CodexAccountAction = +export type ProviderAccountAction = | 'idle' | 'adding' | `reauth:${string}` | `remove:${string}` | `select:${string}` -export type ClaudeAccountAction = CodexAccountAction - export type RemoveAccountTarget = { id: string runtime: ProviderAccountRuntimeView @@ -56,13 +49,13 @@ export type ProviderAccountVisibilityOptions = { } export type CodexAccountActionRunner = ( - action: CodexAccountAction, + action: ProviderAccountAction, operation: () => Promise, actionRuntime?: ProviderAccountRuntimeView ) => Promise export type ClaudeAccountActionRunner = ( - action: ClaudeAccountAction, + action: ProviderAccountAction, operation: () => Promise, actionRuntime?: ProviderAccountRuntimeView ) => Promise @@ -90,13 +83,13 @@ export type AccountsPaneSectionModel = { accountRuntimeUnavailable: boolean accountVisibilityOptions: ProviderAccountVisibilityOptions claudeAccounts: ClaudeRateLimitAccountsState - claudeAction: ClaudeAccountAction + claudeAction: ProviderAccountAction visibleClaudeAccounts: ClaudeRateLimitAccountsState['accounts'] systemClaudeActive: boolean setRemoveClaudeTarget: Dispatch> runClaudeAccountAction: ClaudeAccountActionRunner codexAccounts: CodexRateLimitAccountsState - codexAction: CodexAccountAction + codexAction: ProviderAccountAction visibleCodexAccounts: CodexRateLimitAccountsState['accounts'] systemCodexActive: boolean systemCodexNeedsSignIn: boolean diff --git a/src/renderer/src/components/settings/accounts/providers/AntigravityAccountsSection.tsx b/src/renderer/src/components/settings/accounts/providers/AntigravityAccountsSection.tsx index 6a08f0d9675..0b963470af2 100644 --- a/src/renderer/src/components/settings/accounts/providers/AntigravityAccountsSection.tsx +++ b/src/renderer/src/components/settings/accounts/providers/AntigravityAccountsSection.tsx @@ -17,11 +17,9 @@ import { Button } from '../../../ui/button' import { Badge } from '../../../ui/badge' import { ManagedRuntimeInstallRow } from '../../../managed-runtime/ManagedRuntimeInstallRow' import { antigravityChatSignInRunning } from '@/runtime/provider-accounts/sign-in/antigravity-chat-sign-in-status' -import { - AntigravityChatSignInRow, - antigravityChatSignInBlocked, - useAntigravityChatSignIn -} from '../../../provider-accounts/AntigravityChatSignInRow' +import { AntigravityChatSignInRow } from '../../../provider-accounts/AntigravityChatSignInRow' +import { antigravityChatSignInBlocked } from '../../../provider-accounts/antigravity-chat-runtime-sign-in-policy' +import { useAntigravityChatSignIn } from '../../../provider-accounts/use-antigravity-chat-sign-in' import { openAppHttpLinkFromAnchor } from '@/lib/browser/navigation/app-http-link-open' import { formatTimeAgo } from '../../../status-bar/tooltip' import { ChatUnavailableOnWindowsNote } from '../../../agent/launch-settings/AgentChatChoiceControls' diff --git a/src/renderer/src/components/settings/accounts/providers/accounts-pane-gemini-section.tsx b/src/renderer/src/components/settings/accounts/providers/accounts-pane-gemini-section.tsx new file mode 100644 index 00000000000..7f3125d44ee --- /dev/null +++ b/src/renderer/src/components/settings/accounts/providers/accounts-pane-gemini-section.tsx @@ -0,0 +1,82 @@ +import { translate } from '../../../../i18n/i18n' +import type { Translate } from '../../../../i18n/use-translate' +import { Label } from '../../../ui/label' +import { Switch } from '../../../ui/switch' +import { GeminiIcon } from '../../../status-bar/icons' +import { SearchableSetting } from '../../section-layout/SearchableSetting' +import { SettingsGroup } from '../../section-layout/SettingsGroup' +import type { AccountsPaneSectionModel } from '../accounts-pane-types' + +export function renderGeminiAccountsSection( + model: AccountsPaneSectionModel, + t: Translate = translate +): React.JSX.Element { + const { localAccountRuntimeSentenceLabel, recordFeatureInteraction, settings, updateSettings } = + model + return ( +
+ + + {t('auto.components.settings.AccountsPane.0c64dc2a64', 'Gemini CLI (legacy)')} + + } + description={t( + 'auto.components.settings.AccountsPane.973741a871', + 'Keep existing Gemini CLI sessions available. Gemini credentials do not sign you into Antigravity.' + )} + > + +
+ +

+ {t( + 'auto.components.settings.AccountsPane.c2aee76420', + 'Extracts OAuth credentials from your local Gemini CLI installation to authenticate with Google for {{value0}}. This uses credentials issued to the Gemini CLI app, not Orca. May break if Google updates the CLI. Use at your own risk.', + { value0: localAccountRuntimeSentenceLabel } + )} +

+
+ { + recordFeatureInteraction('usage-tracking') + updateSettings({ + geminiCliOAuthEnabled: checked + }) + }} + /> +
+
+
+ ) +} diff --git a/src/renderer/src/components/settings/accounts/providers/accounts-pane-provider-setting-sections.tsx b/src/renderer/src/components/settings/accounts/providers/accounts-pane-opencode-section.tsx similarity index 66% rename from src/renderer/src/components/settings/accounts/providers/accounts-pane-provider-setting-sections.tsx rename to src/renderer/src/components/settings/accounts/providers/accounts-pane-opencode-section.tsx index 3b5c99a6b22..3c166ba7143 100644 --- a/src/renderer/src/components/settings/accounts/providers/accounts-pane-provider-setting-sections.tsx +++ b/src/renderer/src/components/settings/accounts/providers/accounts-pane-opencode-section.tsx @@ -1,88 +1,13 @@ -import { translate } from '@/i18n/i18n' -import type { Translate } from '@/i18n/use-translate' +import { translate } from '../../../../i18n/i18n' +import type { Translate } from '../../../../i18n/use-translate' import { Button } from '../../../ui/button' import { Label } from '../../../ui/label' -import { Switch } from '../../../ui/switch' -import { GeminiIcon, OpenCodeGoIcon } from '../../../status-bar/icons' +import { OpenCodeGoIcon } from '../../../status-bar/icons' import { SearchableSetting } from '../../section-layout/SearchableSetting' +import { SettingsGroup } from '../../section-layout/SettingsGroup' +import { DebouncedSettingsTextInput } from '../../form-controls/DebouncedSettingsTextInput' import type { AccountsPaneSectionModel } from '../accounts-pane-types' import { OpenCodeGoCredentials } from '../usage/accounts-pane-opencode-credentials' -import { DebouncedSettingsTextInput } from '../../form-controls/DebouncedSettingsTextInput' -import { SettingsGroup } from '../../section-layout/SettingsGroup' - -export function renderGeminiAccountsSection( - model: AccountsPaneSectionModel, - t: Translate = translate -): React.JSX.Element { - const { localAccountRuntimeSentenceLabel, recordFeatureInteraction, settings, updateSettings } = - model - return ( -
- - - {t('auto.components.settings.AccountsPane.0c64dc2a64', 'Gemini CLI (legacy)')} - - } - description={t( - 'auto.components.settings.AccountsPane.973741a871', - 'Keep existing Gemini CLI sessions available. Gemini credentials do not sign you into Antigravity.' - )} - > - -
- -

- {t( - 'auto.components.settings.AccountsPane.c2aee76420', - 'Extracts OAuth credentials from your local Gemini CLI installation to authenticate with Google for {{value0}}. This uses credentials issued to the Gemini CLI app, not Orca. May break if Google updates the CLI. Use at your own risk.', - { value0: localAccountRuntimeSentenceLabel } - )} -

-
- { - recordFeatureInteraction('usage-tracking') - updateSettings({ - geminiCliOAuthEnabled: checked - }) - }} - /> -
-
-
- ) -} export function renderOpenCodeAccountsSection( model: AccountsPaneSectionModel, diff --git a/src/renderer/src/components/settings/accounts/scope/accounts-pane-location-section.tsx b/src/renderer/src/components/settings/accounts/scope/accounts-pane-location-section.tsx index 087f9dd9b2a..965cfe923c9 100644 --- a/src/renderer/src/components/settings/accounts/scope/accounts-pane-location-section.tsx +++ b/src/renderer/src/components/settings/accounts/scope/accounts-pane-location-section.tsx @@ -4,7 +4,7 @@ import type { Translate } from '@/i18n/use-translate' import { SearchableSetting } from '../../section-layout/SearchableSetting' import { SettingsRow, SettingsSegmentedControl } from '../../form-controls/SettingsFormControls' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../../../ui/select' -import { getHostRuntimeLabel } from './accounts-pane-runtime' +import { getHostRuntimeLabel } from '../../../../lib/provider-accounts/account-runtime-label' import { WSL_DEFAULT_DISTRO_KEY } from '../../../provider-accounts/provider-account-visibility' import type { GlobalSettings } from '../../../../../../shared/global-settings-types' import type { AccountsPaneSectionModel } from '../accounts-pane-types' diff --git a/src/renderer/src/components/settings/accounts/scope/accounts-pane-runtime.ts b/src/renderer/src/components/settings/accounts/scope/accounts-pane-runtime.ts index 8d0fee372f0..363eaf415dc 100644 --- a/src/renderer/src/components/settings/accounts/scope/accounts-pane-runtime.ts +++ b/src/renderer/src/components/settings/accounts/scope/accounts-pane-runtime.ts @@ -8,16 +8,13 @@ import { resolveLocalAccountRuntimeTarget } from '../../../../../../shared/local import { getRendererAppPlatform } from '../../../../lib/renderer-app-platform' import { getIntlLocale, translate } from '@/i18n/i18n' import type { Translate } from '@/i18n/use-translate' -import type { LocalAccountRuntime } from '../accounts-pane-types' +import { + type LocalAccountRuntime, + getHostRuntimeLabel +} from '../../../../lib/provider-accounts/account-runtime-label' export const EMPTY_WSL_DISTROS: string[] = [] -export function getHostRuntimeLabel(t: Translate = translate): string { - return navigator.userAgent.includes('Windows') - ? 'Windows' - : t('auto.components.settings.AccountsPane.9baf45d071', 'This device') -} - // System default has no saved identity; show live host login or custom-provider detail, falling back when signed out. export function getCodexSystemDefaultSubtitle( identity: CodexSystemDefaultIdentity | undefined, @@ -98,13 +95,6 @@ export function getSelectedAccountRuntime( return { runtime: 'host', label: getHostRuntimeLabel(t) } } -// Standalone runtime labels need sentence casing when interpolated into prose. -export function getRuntimeSentenceLabel(runtime: LocalAccountRuntime): string { - return runtime.runtime === 'host' && !navigator.userAgent.includes('Windows') - ? `${runtime.label.charAt(0).toLocaleLowerCase()}${runtime.label.slice(1)}` - : runtime.label -} - export function formatAccountTimestamp( timestamp: number, intlLocale: string = getIntlLocale() diff --git a/src/renderer/src/components/settings/agents/AgentWorkspaceTrustSetting.fixture.tsx b/src/renderer/src/components/settings/agents/AgentWorkspaceTrustSetting.fixture.tsx index 35f2f8019fa..23bd8b64870 100644 --- a/src/renderer/src/components/settings/agents/AgentWorkspaceTrustSetting.fixture.tsx +++ b/src/renderer/src/components/settings/agents/AgentWorkspaceTrustSetting.fixture.tsx @@ -1,6 +1,6 @@ import { getDefaultSettings } from '../../../../../shared/constants' import { defineFixture } from '../../../component-fixtures/component-fixture' -import { AgentWorkspaceTrustSetting } from './AgentsPane' +import { AgentWorkspaceTrustSetting } from './behavior/AgentWorkspaceTrustSetting' export const on = defineFixture({ width: 640, diff --git a/src/renderer/src/components/settings/agents/AgentsPane.test.tsx b/src/renderer/src/components/settings/agents/AgentsPane.test.tsx deleted file mode 100644 index de70d9e9a3b..00000000000 --- a/src/renderer/src/components/settings/agents/AgentsPane.test.tsx +++ /dev/null @@ -1,79 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import { getDefaultSettings } from '../../../../../shared/constants' -import type { GlobalSettings } from '../../../../../shared/global-settings-types' -import { useAppStore } from '../../../store/index' -import { createAgentAvailabilityUpdateQueue } from '../../../lib/agent-session/launch/agent-settings-update-queue' - -type Deferred = { - promise: Promise - resolve: () => void -} - -function createDeferred(): Deferred { - let resolve!: () => void - const promise = new Promise((next) => { - resolve = next - }) - return { promise, resolve } -} - -async function flushPromiseQueue(): Promise { - await Promise.resolve() - await Promise.resolve() -} - -describe('AgentsPane', () => { - it('serializes rapid availability writes against the latest settings snapshot', async () => { - const queueAvailabilityUpdate = createAgentAvailabilityUpdateQueue() - const settings: GlobalSettings = { - ...getDefaultSettings('/tmp'), - defaultTuiAgent: null, - disabledTuiAgents: [] - } - const writes: Deferred[] = [] - const updates: Partial[] = [] - - useAppStore.setState({ settings }) - const updateSettings = vi.fn((update: Partial) => { - updates.push(update) - const nextSettings = { - ...(useAppStore.getState().settings ?? settings), - ...update - } - const write = createDeferred() - writes.push(write) - return write.promise.then(() => { - useAppStore.setState({ settings: nextSettings }) - }) - }) - - const firstWrite = queueAvailabilityUpdate({ - getSettings: () => useAppStore.getState().settings, - fallbackSettings: settings, - updateSettings, - agentId: 'claude', - enabled: false - }) - const secondWrite = queueAvailabilityUpdate({ - getSettings: () => useAppStore.getState().settings, - fallbackSettings: settings, - updateSettings, - agentId: 'codex', - enabled: false - }) - - await flushPromiseQueue() - expect(updateSettings).toHaveBeenCalledTimes(1) - expect(updates[0]).toMatchObject({ disabledTuiAgents: ['claude'] }) - - writes[0].resolve() - await firstWrite - await flushPromiseQueue() - - expect(updateSettings).toHaveBeenCalledTimes(2) - expect(updates[1]).toMatchObject({ disabledTuiAgents: ['claude', 'codex'] }) - - writes[1].resolve() - await secondWrite - }) -}) diff --git a/src/renderer/src/components/settings/agents/AgentsPane.tsx b/src/renderer/src/components/settings/agents/AgentsPane.tsx index 300cb30f862..906f6307b86 100644 --- a/src/renderer/src/components/settings/agents/AgentsPane.tsx +++ b/src/renderer/src/components/settings/agents/AgentsPane.tsx @@ -18,13 +18,8 @@ import { getAgentStatusHooksDescription, getAgentStatusHooksTitle } from './behavior/agent-status-hooks-copy' -import { - getAgentWorkspaceTrustDescription, - getAgentWorkspaceTrustTitle -} from './behavior/agent-workspace-trust-copy' import { SettingsSwitchRow } from '../form-controls/SettingsFormControls' import { isPairedWebClientWindow } from '@/lib/desktop-window-chrome' - import { AgentLaunchSettingsSection } from './launch/AgentLaunchSettingsSection' import { AgentPermissionsSetting, @@ -33,6 +28,7 @@ import { import type { SettingsHostScope } from '../host-scope/settings-host-scope' import { SettingsGroup } from '../section-layout/SettingsGroup' import { useTranslate } from '@/i18n/use-translate' +import { AgentWorkspaceTrustSetting } from './behavior/AgentWorkspaceTrustSetting' type AgentSettingProps = { settings: GlobalSettings @@ -120,7 +116,7 @@ function renderAgentPermissions(props: AgentPermissionsSettingProps): React.Reac return } -export function AgentStatusHooksSetting({ settings, updateSettings }: AgentSettingProps) { +function AgentStatusHooksSetting({ settings, updateSettings }: AgentSettingProps) { const t = useTranslate() const enabled = settings.agentStatusHooksEnabled !== false return ( @@ -134,21 +130,7 @@ export function AgentStatusHooksSetting({ settings, updateSettings }: AgentSetti ) } -export function AgentWorkspaceTrustSetting({ settings, updateSettings }: AgentSettingProps) { - const t = useTranslate() - const enabled = settings.agentWorkspaceTrustEnabled !== false - return ( - updateSettings({ agentWorkspaceTrustEnabled: !enabled })} - ariaLabel={getAgentWorkspaceTrustTitle(t)} - /> - ) -} - -export function AgentGeneratedTabTitlesSetting({ settings, updateSettings }: AgentSettingProps) { +function AgentGeneratedTabTitlesSetting({ settings, updateSettings }: AgentSettingProps) { const t = useTranslate() const enabled = settings.tabAutoGenerateTitle === true return ( diff --git a/src/renderer/src/components/settings/agents/behavior/AgentWorkspaceTrustSetting.tsx b/src/renderer/src/components/settings/agents/behavior/AgentWorkspaceTrustSetting.tsx new file mode 100644 index 00000000000..b0cb3692c2f --- /dev/null +++ b/src/renderer/src/components/settings/agents/behavior/AgentWorkspaceTrustSetting.tsx @@ -0,0 +1,27 @@ +import type { GlobalSettings } from '../../../../../../shared/global-settings-types' +import { useTranslate } from '../../../../i18n/use-translate' +import { SettingsSwitchRow } from '../../form-controls/SettingsFormControls' +import { + getAgentWorkspaceTrustTitle, + getAgentWorkspaceTrustDescription +} from './agent-workspace-trust-copy' + +export function AgentWorkspaceTrustSetting({ + settings, + updateSettings +}: { + settings: GlobalSettings + updateSettings: (updates: Partial) => void | Promise +}) { + const t = useTranslate() + const enabled = settings.agentWorkspaceTrustEnabled !== false + return ( + updateSettings({ agentWorkspaceTrustEnabled: !enabled })} + ariaLabel={getAgentWorkspaceTrustTitle(t)} + /> + ) +} diff --git a/src/renderer/src/components/settings/agents/launch/AgentCatalogRow.tsx b/src/renderer/src/components/settings/agents/launch/AgentCatalogRow.tsx index 0f8cfa8ce07..414878167e2 100644 --- a/src/renderer/src/components/settings/agents/launch/AgentCatalogRow.tsx +++ b/src/renderer/src/components/settings/agents/launch/AgentCatalogRow.tsx @@ -6,7 +6,6 @@ import { cn } from '@/lib/utils' import { useTranslate } from '@/i18n/use-translate' import { Button } from '../../../ui/button' import { SettingsBadge, SettingsSegmentedControl } from '../../form-controls/SettingsFormControls' - import { type AgentSessionSourceHomeControl, AgentSessionSourceHomeInput @@ -24,11 +23,9 @@ import { AgentDefaultArgsInput, AgentDefaultEnvInput } from '../../../agent/launch-settings/AgentLaunchDefaultsEditor' -import { - agentPermissionChoiceLabel, - AgentPermissionMenu, - type AgentPermissionChoiceRow -} from '../../../agent/launch-settings/AgentPermissionChoiceControls' +import { agentPermissionChoiceLabel } from '../../../agent/launch-settings/agent-permission-choice-label' +import { AgentPermissionMenu } from '../../../agent/launch-settings/AgentPermissionMenu' +import type { AgentPermissionChoiceRow } from '../../../agent/launch-settings/agent-permission-choice-row' import { openAppHttpLinkFromAnchor } from '@/lib/browser/navigation/app-http-link-open' import type { AgentOpensAs } from '../../../../lib/agent-session/launch/native-chat-provider-choice' import { AgentOpensAsControl } from '../../../agent/launch-settings/AgentChatChoiceControls' diff --git a/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsCatalog.tsx b/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsCatalog.tsx index a889724094f..ec8218f5abe 100644 --- a/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsCatalog.tsx +++ b/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsCatalog.tsx @@ -23,11 +23,9 @@ import { } from '../../../../../../shared/agent-launch/permissions/tui-agent-permission-choice' import { agentArgsSetPermission } from '../../../../../../shared/agent-launch/permissions/agent-permission-args' import { liftAgentPermissionArgs } from '../../../../../../shared/agent-launch/permissions/tui-agent-permission-lift' -import { - agentPermissionChoiceLabel, - AllAgentsPermissionSetting, - type AgentPermissionChoiceRow -} from '@/components/agent/launch-settings/AgentPermissionChoiceControls' +import { agentPermissionChoiceLabel } from '../../../agent/launch-settings/agent-permission-choice-label' +import { AllAgentsPermissionSetting } from '../permissions/AllAgentsPermissionSetting' +import type { AgentPermissionChoiceRow } from '../../../agent/launch-settings/agent-permission-choice-row' import type { AgentPermissionsSettingProps } from '@/components/settings/agents/permissions/AgentPermissionsSetting' import { buildAgentRowPermissions, diff --git a/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsLoading.tsx b/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsLoading.tsx index 8af60e1f1c2..994faf53d0b 100644 --- a/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsLoading.tsx +++ b/src/renderer/src/components/settings/agents/launch/AgentLaunchSettingsLoading.tsx @@ -1,6 +1,6 @@ import { useTranslate } from '@/i18n/use-translate' import { AgentDefaultSetting } from './AgentDefaultSetting' -import { AllAgentsPermissionSetting } from '../../../agent/launch-settings/AgentPermissionChoiceControls' +import { AllAgentsPermissionSetting } from '../permissions/AllAgentsPermissionSetting' export function AgentLaunchSettingsLoading(): React.JSX.Element { const t = useTranslate() diff --git a/src/renderer/src/components/settings/agents/permissions/AllAgentsPermissionSetting.tsx b/src/renderer/src/components/settings/agents/permissions/AllAgentsPermissionSetting.tsx new file mode 100644 index 00000000000..639f376e960 --- /dev/null +++ b/src/renderer/src/components/settings/agents/permissions/AllAgentsPermissionSetting.tsx @@ -0,0 +1,124 @@ +import { useTranslate } from '../../../../i18n/use-translate' +import type { AgentPermissionChoice } from '../../../../../../shared/agent-launch/permissions/tui-agent-permission-choice' +import { ChevronDown } from 'lucide-react' +import { Button } from '../../../ui/button' +import { + DropdownMenu, + DropdownMenuTrigger, + DropdownMenuContent, + DropdownMenuRadioGroup +} from '../../../ui/dropdown-menu' +import { + AgentPermissionChoiceItems, + AgentPermissionChoiceName +} from '../../../agent/launch-settings/AgentPermissionChoiceItems' + +export function AllAgentsPermissionSetting({ + chatOwnSupported = false, + choice, + withoutAutoChoice, + onChange, + onChangeWithoutAuto +}: { + chatOwnSupported?: boolean + choice: AgentPermissionChoice | null + /** What the agents without Approve for me share; null when they differ. */ + withoutAutoChoice: AgentPermissionChoice | null + onChange: (choice: AgentPermissionChoice) => void + onChangeWithoutAuto: (choice: AgentPermissionChoice) => void +}): React.JSX.Element { + const t = useTranslate() + const title = t('settings.agents.permissionsTitle', 'Permissions') + const ownLabel = t('settings.agents.permissionEachOwn', "Each agent's own settings") + const withoutAutoTitle = t( + 'settings.agents.otherAgentsPermissionsLabel', + 'Permissions for other agents' + ) + return ( +
+
+

{title}

+ + + + + + + + + + +
+

+ {t( + 'settings.agents.permissionsChoiceExplanation', + 'What every agent may do without asking. You can set one agent differently below.' + )} +

+
+ {choice === 'auto' ? ( +
+ + {t('settings.agents.permissionWithoutAuto', 'Agents without Approve for me')} + + + + + + + + + + + +
+ ) : null} +
+
+ ) +} diff --git a/src/renderer/src/components/settings/agents/permissions/agent-permission-mode-label.ts b/src/renderer/src/components/settings/agents/permissions/agent-permission-mode-label.ts index 6960cd5e03a..9411eff69d9 100644 --- a/src/renderer/src/components/settings/agents/permissions/agent-permission-mode-label.ts +++ b/src/renderer/src/components/settings/agents/permissions/agent-permission-mode-label.ts @@ -1,5 +1,5 @@ import type { AgentPermissionMode } from '../../../../../../shared/agent-launch/permissions/tui-agent-permissions' -import { agentPermissionChoiceLabel } from '../../../agent/launch-settings/AgentPermissionChoiceControls' +import { agentPermissionChoiceLabel } from '../../../agent/launch-settings/agent-permission-choice-label' import type { Translate } from '../../../../i18n/use-translate' export function agentPermissionModeChoice(mode: Exclude) { diff --git a/src/renderer/src/components/settings/repository/RepositoryPane.tsx b/src/renderer/src/components/settings/repository/RepositoryPane.tsx index 50871a8077f..0cdad62a893 100644 --- a/src/renderer/src/components/settings/repository/RepositoryPane.tsx +++ b/src/renderer/src/components/settings/repository/RepositoryPane.tsx @@ -8,10 +8,10 @@ import { getRepoKindLabel, isFolderRepo } from '../../../../../shared/repo-kind' import { getRepoExecutionHostId, type ExecutionHostId } from '../../../../../shared/execution-host' import { Label } from '../../ui/label' import { useShallow } from 'zustand/react/shallow' -import { RepositoryHooksSection } from './execution/RepositoryHooksSection' +import { RepositoryHooksSection } from './hooks/RepositoryHooksSection' import { McpConfigSection } from './mcp/McpConfigSection' -import { WorktreeSymlinksSection } from './execution/WorktreeSymlinksSection' -import { SparsePresetSettingsSection } from './execution/SparsePresetSettingsSection' +import { WorktreeSymlinksSection } from './worktrees/WorktreeSymlinksSection' +import { SparsePresetSettingsSection } from './worktrees/SparsePresetSettingsSection' import { RepositorySourceControlAiSection } from './source-control/RepositorySourceControlAiSection' import { SearchableSetting } from '../section-layout/SearchableSetting' import { SettingsGroup } from '../section-layout/SettingsGroup' @@ -22,20 +22,20 @@ import { useAppStore } from '@/store/index' import { getRepositoryIconSectionId } from './repository-settings-targets' import { RepositoryIconPicker } from './identity/RepositoryIconPicker' import { getRepositoryPaneSearchEntries } from './repository-search' -import { RepositoryHostSetupsSection } from './execution/RepositoryHostSetupsSection' +import { RepositoryHostSetupsSection } from './hosts/RepositoryHostSetupsSection' import { RepoSettingsDraftInput } from './RepositorySettingsDraftInput' -import { RepositoryForkSyncSection } from './execution/RepositoryForkSyncSection' +import { RepositoryForkSyncSection } from './source-control/RepositoryForkSyncSection' import { getRepositoryAgentTrustSearchEntry, RepositoryAgentTrustSection } from './RepositoryAgentTrustSection' -import { RepositoryGitHubAccountSection } from './RepositoryGitHubAccountSection' +import { RepositoryGitHubAccountSection } from './source-control/RepositoryGitHubAccountSection' import { useTranslate } from '@/i18n/use-translate' -import { RepositoryWindowsRuntimeSection } from './execution/RepositoryWindowsRuntimeSection' +import { RepositoryWindowsRuntimeSection } from './runtime/RepositoryWindowsRuntimeSection' import { matchesRepositoryIdentitySearch } from './identity/repository-identity-search' -import { RepositoryWorktreeDefaultsSection } from './execution/RepositoryWorktreeDefaultsSection' +import { RepositoryWorktreeDefaultsSection } from './worktrees/RepositoryWorktreeDefaultsSection' import { RepositoryJiraProjectSection } from './RepositoryJiraProjectSection' -import { getProjectRuntimeSessionSummary } from './execution/repository-runtime-session-summary' +import { getProjectRuntimeSessionSummary } from './runtime/repository-runtime-session-summary' import { getRepoOwnerWorktreeVisibilityDefaults } from '../../../store/slices/worktrees/listing/worktree-visibility-defaults-by-host' import { RepositoryFormatterSection, diff --git a/src/renderer/src/components/settings/repository/execution/sparse-preset-draft-editor.tsx b/src/renderer/src/components/settings/repository/execution/sparse-preset-draft-editor.tsx deleted file mode 100644 index 87e00d40473..00000000000 --- a/src/renderer/src/components/settings/repository/execution/sparse-preset-draft-editor.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import type { SparsePresetDirectoryParseResult } from '@/lib/sparse-preset-draft' -import { SparsePresetInlineEditor } from '../../../sparse/SparsePresetInlineEditor' -import type { SparsePresetDraft } from '../../../sparse/SparseCheckoutPresetDraftForm' - -type SparsePresetDraftEditorProps = { - draft: SparsePresetDraft - setDraft: (draft: SparsePresetDraft | null) => void - nameError: string | null - parsedDirectories: SparsePresetDirectoryParseResult | null - canSaveDraft: boolean - submitting: boolean - onSave: () => void - operationError?: string | null - repoRootPath?: string - repoConnectionId?: string -} - -export function SparsePresetDraftEditor({ - setDraft, - canSaveDraft, - ...props -}: SparsePresetDraftEditorProps): React.JSX.Element { - return ( - setDraft(null)} - /> - ) -} diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryHookPolicySettings.tsx b/src/renderer/src/components/settings/repository/hooks/RepositoryHookPolicySettings.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryHookPolicySettings.tsx rename to src/renderer/src/components/settings/repository/hooks/RepositoryHookPolicySettings.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryHookScriptSetting.tsx b/src/renderer/src/components/settings/repository/hooks/RepositoryHookScriptSetting.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryHookScriptSetting.tsx rename to src/renderer/src/components/settings/repository/hooks/RepositoryHookScriptSetting.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryHooksSection.tsx b/src/renderer/src/components/settings/repository/hooks/RepositoryHooksSection.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryHooksSection.tsx rename to src/renderer/src/components/settings/repository/hooks/RepositoryHooksSection.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryHooksYamlStatus.tsx b/src/renderer/src/components/settings/repository/hooks/RepositoryHooksYamlStatus.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryHooksYamlStatus.tsx rename to src/renderer/src/components/settings/repository/hooks/RepositoryHooksYamlStatus.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryIssueCommandSetting.tsx b/src/renderer/src/components/settings/repository/hooks/RepositoryIssueCommandSetting.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryIssueCommandSetting.tsx rename to src/renderer/src/components/settings/repository/hooks/RepositoryIssueCommandSetting.tsx diff --git a/src/renderer/src/components/settings/repository/execution/repository-git-hooks-search-entries.ts b/src/renderer/src/components/settings/repository/hooks/repository-git-hooks-search-entries.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/repository-git-hooks-search-entries.ts rename to src/renderer/src/components/settings/repository/hooks/repository-git-hooks-search-entries.ts diff --git a/src/renderer/src/components/settings/repository/execution/repository-hook-defaults.ts b/src/renderer/src/components/settings/repository/hooks/repository-hook-defaults.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/repository-hook-defaults.ts rename to src/renderer/src/components/settings/repository/hooks/repository-hook-defaults.ts diff --git a/src/renderer/src/components/settings/repository/execution/repository-hook-settings-draft.ts b/src/renderer/src/components/settings/repository/hooks/repository-hook-settings-draft.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/repository-hook-settings-draft.ts rename to src/renderer/src/components/settings/repository/hooks/repository-hook-settings-draft.ts diff --git a/src/renderer/src/components/settings/repository/execution/use-repository-hook-settings-draft.test.tsx b/src/renderer/src/components/settings/repository/hooks/use-repository-hook-settings-draft.test.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/use-repository-hook-settings-draft.test.tsx rename to src/renderer/src/components/settings/repository/hooks/use-repository-hook-settings-draft.test.tsx diff --git a/src/renderer/src/components/settings/repository/execution/use-repository-hook-settings-draft.ts b/src/renderer/src/components/settings/repository/hooks/use-repository-hook-settings-draft.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/use-repository-hook-settings-draft.ts rename to src/renderer/src/components/settings/repository/hooks/use-repository-hook-settings-draft.ts diff --git a/src/renderer/src/components/settings/repository/hooks/use-repository-issue-command-wsl-stopped.test.ts b/src/renderer/src/components/settings/repository/hooks/use-repository-issue-command-wsl-stopped.test.ts index ed19fd07680..9ee0c301bb8 100644 --- a/src/renderer/src/components/settings/repository/hooks/use-repository-issue-command-wsl-stopped.test.ts +++ b/src/renderer/src/components/settings/repository/hooks/use-repository-issue-command-wsl-stopped.test.ts @@ -13,7 +13,7 @@ vi.mock('@/runtime/repo-hooks/runtime-hooks-client', () => ({ })) vi.mock('sonner', () => ({ toast: { error: vi.fn() } })) -import { useRepositoryIssueCommand } from '../execution/use-repository-issue-command' +import { useRepositoryIssueCommand } from './use-repository-issue-command' const distroRunningListeners = new Set<(distro: string) => void>() const REFUSED = { diff --git a/src/renderer/src/components/settings/repository/execution/use-repository-issue-command.ts b/src/renderer/src/components/settings/repository/hooks/use-repository-issue-command.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/use-repository-issue-command.ts rename to src/renderer/src/components/settings/repository/hooks/use-repository-issue-command.ts diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryHostSetupActions.tsx b/src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupActions.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryHostSetupActions.tsx rename to src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupActions.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryHostSetupsSection.settings-entry.test.tsx b/src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupsSection.settings-entry.test.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryHostSetupsSection.settings-entry.test.tsx rename to src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupsSection.settings-entry.test.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryHostSetupsSection.tsx b/src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupsSection.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryHostSetupsSection.tsx rename to src/renderer/src/components/settings/repository/hosts/RepositoryHostSetupsSection.tsx diff --git a/src/renderer/src/components/settings/repository/execution/repository-host-add-project-steps.tsx b/src/renderer/src/components/settings/repository/hosts/repository-host-add-project-steps.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/repository-host-add-project-steps.tsx rename to src/renderer/src/components/settings/repository/hosts/repository-host-add-project-steps.tsx diff --git a/src/renderer/src/components/settings/repository/execution/repository-host-setup-options.ts b/src/renderer/src/components/settings/repository/hosts/repository-host-setup-options.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/repository-host-setup-options.ts rename to src/renderer/src/components/settings/repository/hosts/repository-host-setup-options.ts diff --git a/src/renderer/src/components/settings/repository/mcp/mcp-config-search.ts b/src/renderer/src/components/settings/repository/mcp/mcp-config-search.ts new file mode 100644 index 00000000000..32b25bc9e79 --- /dev/null +++ b/src/renderer/src/components/settings/repository/mcp/mcp-config-search.ts @@ -0,0 +1,45 @@ +import type { Repo } from '../../../../../../shared/repo-types' +import type { SettingsSearchEntry } from '../../search/settings-search' +import { translate } from '@/i18n/i18n' +import type { Translate } from '@/i18n/use-translate' +import { searchKeywordTranslator } from '../../search/settings-search-keywords' + +export function getRepositoryMcpConfigSearchEntries( + repo: Repo, + t: Translate = translate +): SettingsSearchEntry[] { + const translateSearchKeyword = searchKeywordTranslator(t) + return [ + { + title: t('auto.components.settings.repository.search.31bd0a2420', 'MCP Configs'), + description: t( + 'auto.components.settings.repository.search.3c31801626', + 'Inspect project-level MCP server config files.' + ), + keywords: [ + repo.displayName, + ...translateSearchKeyword('auto.components.settings.repository.search.343f0a508c', 'mcp'), + ...translateSearchKeyword( + 'auto.components.settings.repository.search.16dc7a4637', + 'model context protocol' + ), + ...translateSearchKeyword( + 'auto.components.settings.repository.search.e760e3fae7', + '.mcp.json' + ), + ...translateSearchKeyword( + 'auto.components.settings.repository.search.26f42fe773', + '.cursor/mcp.json' + ), + ...translateSearchKeyword( + 'auto.components.settings.repository.search.db11b337c4', + '.claude.json' + ), + ...translateSearchKeyword( + 'auto.components.settings.repository.search.d73fb47b45', + '.claude/mcp.json' + ) + ] + } + ] +} diff --git a/src/renderer/src/components/settings/repository/repository-search.ts b/src/renderer/src/components/settings/repository/repository-search.ts index 4e47547207f..03b9a313d47 100644 --- a/src/renderer/src/components/settings/repository/repository-search.ts +++ b/src/renderer/src/components/settings/repository/repository-search.ts @@ -8,9 +8,10 @@ import type { SettingsSearchEntry } from '../search/settings-search' import { translate } from '@/i18n/i18n' import type { Translate } from '@/i18n/use-translate' import { searchKeywordTranslator } from '../search/settings-search-keywords' -import { getRepositoryGitAuthorSearchEntries } from './repository-git-author-search-entries' -import { getRepositoryGitHooksSearchEntries } from './execution/repository-git-hooks-search-entries' -import { getRepositoryGitWorktreeSearchEntries } from './execution/repository-git-worktree-search-entries' +import { getRepositoryGitAuthorSearchEntries } from './source-control/repository-git-author-search-entries' +import { getRepositoryMcpConfigSearchEntries } from './mcp/mcp-config-search' +import { getRepositoryGitHooksSearchEntries } from './hooks/repository-git-hooks-search-entries' +import { getRepositoryGitWorktreeSearchEntries } from './worktrees/repository-git-worktree-search-entries' import { getRepositoryFormatterSearchEntry } from './RepositoryFormatterSection' import { getRepositoryAgentTrustSearchEntry } from './RepositoryAgentTrustSection' @@ -249,6 +250,7 @@ export function getRepositoryPaneSearchEntries( ? [] : [ ...getRepositoryGitAuthorSearchEntries(repo, t), + ...getRepositoryMcpConfigSearchEntries(repo, t), ...getRepositoryGitHooksSearchEntries(repo, t) ]) ] diff --git a/src/renderer/src/components/settings/repository/execution/ProjectWindowsRuntimeSetting.tsx b/src/renderer/src/components/settings/repository/runtime/ProjectWindowsRuntimeSetting.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/ProjectWindowsRuntimeSetting.tsx rename to src/renderer/src/components/settings/repository/runtime/ProjectWindowsRuntimeSetting.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryWindowsRuntimeSection.tsx b/src/renderer/src/components/settings/repository/runtime/RepositoryWindowsRuntimeSection.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryWindowsRuntimeSection.tsx rename to src/renderer/src/components/settings/repository/runtime/RepositoryWindowsRuntimeSection.tsx diff --git a/src/renderer/src/components/settings/repository/execution/repository-runtime-session-summary.ts b/src/renderer/src/components/settings/repository/runtime/repository-runtime-session-summary.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/repository-runtime-session-summary.ts rename to src/renderer/src/components/settings/repository/runtime/repository-runtime-session-summary.ts diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryForkSyncSection.tsx b/src/renderer/src/components/settings/repository/source-control/RepositoryForkSyncSection.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryForkSyncSection.tsx rename to src/renderer/src/components/settings/repository/source-control/RepositoryForkSyncSection.tsx diff --git a/src/renderer/src/components/settings/repository/RepositoryGitHubAccountSection.tsx b/src/renderer/src/components/settings/repository/source-control/RepositoryGitHubAccountSection.tsx similarity index 94% rename from src/renderer/src/components/settings/repository/RepositoryGitHubAccountSection.tsx rename to src/renderer/src/components/settings/repository/source-control/RepositoryGitHubAccountSection.tsx index b7a26640f98..a12d6e8256b 100644 --- a/src/renderer/src/components/settings/repository/RepositoryGitHubAccountSection.tsx +++ b/src/renderer/src/components/settings/repository/source-control/RepositoryGitHubAccountSection.tsx @@ -1,21 +1,24 @@ import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react' import { RefreshCw } from 'lucide-react' -import type { GhAccountBinding } from '../../../../../shared/github/account-binding' -import type { Repo } from '../../../../../shared/repo-types' -import type { GhAccountBindingInventory } from '../../../../../shared/github/auth-types' -import { Button } from '../../ui/button' -import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../../ui/select' -import { SearchableSetting } from '../section-layout/SearchableSetting' +import type { GhAccountBinding } from '../../../../../../shared/github/account-binding' +import type { Repo } from '../../../../../../shared/repo-types' +import type { GhAccountBindingInventory } from '../../../../../../shared/github/auth-types' +import { Button } from '../../../ui/button' +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../../../ui/select' +import { SearchableSetting } from '../../section-layout/SearchableSetting' import { useAppStore } from '@/store/index' import { runtimeTargetForOwnerEnvironment } from '@/runtime/rpc/targets/runtime-client-target' -import { getRepoExecutionHostId, parseExecutionHostId } from '../../../../../shared/execution-host' +import { + getRepoExecutionHostId, + parseExecutionHostId +} from '../../../../../../shared/execution-host' import { useTranslate } from '@/i18n/use-translate' -import { searchKeywords } from '../search/settings-search-keywords' +import { searchKeywords } from '../../search/settings-search-keywords' import { isGhAccountBindingEnforced, listRepositoryGhBindableAccounts, validateRepositoryGhAccountBinding -} from '../../../runtime/repository-github/repository-github-account' +} from '../../../../runtime/repository-github/repository-github-account' type RepositoryGitHubAccountSectionProps = { repo: Repo diff --git a/src/renderer/src/components/settings/repository/source-control/RepositorySourceControlAiActionRows.tsx b/src/renderer/src/components/settings/repository/source-control/RepositorySourceControlAiActionRows.tsx index a2454cb8c50..c79cdf6c962 100644 --- a/src/renderer/src/components/settings/repository/source-control/RepositorySourceControlAiActionRows.tsx +++ b/src/renderer/src/components/settings/repository/source-control/RepositorySourceControlAiActionRows.tsx @@ -23,11 +23,12 @@ import { isCustomAgentId } from '../../../../../../shared/source-control/ai/gene import { type SourceControlActionId, SOURCE_CONTROL_ACTION_IDS, - SOURCE_CONTROL_ACTION_LABELS + SOURCE_CONTROL_ACTION_LABELS, + resolveSourceControlActionCommandTemplate } from '../../../../../../shared/source-control/ai/recipes/source-control-ai-actions' import { Input } from '../../../ui/input' import { Label } from '../../../ui/label' -import { RepositorySourceControlAiActionSaveControls } from '../../source-control/RepositorySourceControlAiActionSaveControls' +import { RepositorySourceControlAiActionSaveControls } from './RepositorySourceControlAiActionSaveControls' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../../../ui/select' import { SourceControlActionVariableChips } from '../../../source-control/SourceControlActionVariableChips' import { @@ -45,7 +46,6 @@ import { agentArgsStateLabel, commandTemplateStateLabel, readInheritedAgentArgs, - readInheritedCommandTemplate, resolveAgentArgsPlaceholderAgent } from './repository-source-control-ai-labels' import { getRepositorySourceControlAiActionRecipeSectionId } from '../repository-settings-targets' @@ -129,7 +129,10 @@ const SelectedRepositoryActionRows = memo( {SOURCE_CONTROL_ACTION_IDS.map((actionId) => { const hasOverride = hasOwnActionOverride(repoAi.actionOverrides, actionId) const override = repoAi.actionOverrides?.[actionId] - const inheritedTemplate = readInheritedCommandTemplate(source, actionId) + const inheritedTemplate = resolveSourceControlActionCommandTemplate( + source.actions, + actionId + ) const inheritedAgentArgs = readInheritedAgentArgs(source, actionId) const templateValue = hasOverride && typeof override?.commandInputTemplate === 'string' diff --git a/src/renderer/src/components/settings/source-control/RepositorySourceControlAiActionSaveControls.tsx b/src/renderer/src/components/settings/repository/source-control/RepositorySourceControlAiActionSaveControls.tsx similarity index 97% rename from src/renderer/src/components/settings/source-control/RepositorySourceControlAiActionSaveControls.tsx rename to src/renderer/src/components/settings/repository/source-control/RepositorySourceControlAiActionSaveControls.tsx index d5bdcf3d533..26f40724727 100644 --- a/src/renderer/src/components/settings/source-control/RepositorySourceControlAiActionSaveControls.tsx +++ b/src/renderer/src/components/settings/repository/source-control/RepositorySourceControlAiActionSaveControls.tsx @@ -1,4 +1,4 @@ -import { Button } from '../../ui/button' +import { Button } from '../../../ui/button' import { useTranslate } from '@/i18n/use-translate' type RepositorySourceControlAiActionSaveControlsProps = { diff --git a/src/renderer/src/components/settings/repository/repository-git-author-search-entries.ts b/src/renderer/src/components/settings/repository/source-control/repository-git-author-search-entries.ts similarity index 56% rename from src/renderer/src/components/settings/repository/repository-git-author-search-entries.ts rename to src/renderer/src/components/settings/repository/source-control/repository-git-author-search-entries.ts index 9e2621fa58f..a1783a431b6 100644 --- a/src/renderer/src/components/settings/repository/repository-git-author-search-entries.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-git-author-search-entries.ts @@ -1,8 +1,8 @@ -import type { Repo } from '../../../../../shared/repo-types' -import type { SettingsSearchEntry } from '../search/settings-search' +import type { Repo } from '../../../../../../shared/repo-types' +import type { SettingsSearchEntry } from '../../search/settings-search' import { translate } from '@/i18n/i18n' import type { Translate } from '@/i18n/use-translate' -import { searchKeywordTranslator } from '../search/settings-search-keywords' +import { searchKeywordTranslator } from '../../search/settings-search-keywords' export function getRepositoryGitAuthorSearchEntries( repo: Repo, @@ -43,37 +43,6 @@ export function getRepositoryGitAuthorSearchEntries( ...translateSearchKeyword('auto.components.settings.repository.search.fa3131f223', 'model'), ...translateSearchKeyword('auto.components.settings.repository.search.fff8834983', 'prompt') ] - }, - { - title: t('auto.components.settings.repository.search.31bd0a2420', 'MCP Configs'), - description: t( - 'auto.components.settings.repository.search.3c31801626', - 'Inspect project-level MCP server config files.' - ), - keywords: [ - repo.displayName, - ...translateSearchKeyword('auto.components.settings.repository.search.343f0a508c', 'mcp'), - ...translateSearchKeyword( - 'auto.components.settings.repository.search.16dc7a4637', - 'model context protocol' - ), - ...translateSearchKeyword( - 'auto.components.settings.repository.search.e760e3fae7', - '.mcp.json' - ), - ...translateSearchKeyword( - 'auto.components.settings.repository.search.26f42fe773', - '.cursor/mcp.json' - ), - ...translateSearchKeyword( - 'auto.components.settings.repository.search.db11b337c4', - '.claude.json' - ), - ...translateSearchKeyword( - 'auto.components.settings.repository.search.d73fb47b45', - '.claude/mcp.json' - ) - ] } ] } diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-draft.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-draft.ts index 6a78a4379ce..d8e8dfe8c17 100644 --- a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-draft.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-draft.ts @@ -1,3 +1,7 @@ +import { + readCompleteRecipeForDraft, + type RepoActionRecipe +} from './repository-source-control-ai-recipe-completion' import { readActionRecipeTextDraft, sameDraftLaunchPreferences, @@ -6,7 +10,6 @@ import { } from './repository-source-control-ai-recipe-text' import { normalizeRepoSourceControlAiOverrides } from '../../../../../../shared/source-control/ai/settings/source-control-ai-repo-settings' -import { resolveSourceControlActionRecipe } from '../../../../../../shared/source-control/ai/source-control-ai-operation-policy' import { SOURCE_CONTROL_ACTION_IDS, SOURCE_CONTROL_TEXT_ACTION_IDS, @@ -15,13 +18,8 @@ import { import type { RepoSourceControlAiOverrides } from '../../../../../../shared/source-control/ai/settings/source-control-ai-settings-types' import { withRepoTextModelEdits } from '../../source-control/text-actions/source-control-text-action-model-replay' import type { GlobalSettings } from '../../../../../../shared/global-settings-types' -import { completeRepoActionRecipe } from './repository-source-control-ai-labels' import { SOURCE_CONTROL_TEXT_ACTION_ID_SET } from '../../source-control/source-control-action-recipe-options' -type RepoActionRecipe = NonNullable< - NonNullable[SourceControlActionId] -> - export function hasOwnActionOverride( overrides: RepoSourceControlAiOverrides['actionOverrides'], actionId: SourceControlActionId @@ -61,22 +59,6 @@ export function dropRepoLegacyInstructionForAction( } } -/** The action an edit starts from: the complete recipe it resolves to, owned or partial, keeping a - * saved explicit args clear and never the agent only detected to run a default selection. */ -export function readCompleteRecipeForDraft( - current: RepoSourceControlAiOverrides, - settings: Pick | null, - actionId: SourceControlActionId -): RepoActionRecipe { - const recipe = resolveSourceControlActionRecipe({ - settings, - repo: { sourceControlAi: current }, - actionId - }) - const cleared = current.actionOverrides?.[actionId]?.agentArgs === null - return { ...completeRepoActionRecipe(recipe, actionId), ...(cleared ? { agentArgs: null } : {}) } -} - export function setActionOverride( current: RepoSourceControlAiOverrides, actionId: SourceControlActionId, diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-global-ux.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-global-ux.ts index 399dade1c93..ba21cd67737 100644 --- a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-global-ux.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-global-ux.ts @@ -1,3 +1,4 @@ +import { readCompleteRecipeForDraft } from './repository-source-control-ai-recipe-completion' import type { HostedReviewDefaultKey, UseRepositorySourceControlAiGlobalUxArgs @@ -19,7 +20,6 @@ import { composeDisplayRepoAi, computeActionDirtyById, hasOwnActionOverride, - readCompleteRecipeForDraft, retainCustomCommandDraft, retainDivergentActionTextDrafts, withRepoAiActionMode, diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-labels.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-labels.ts index dbde6d29730..900082ed622 100644 --- a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-labels.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-labels.ts @@ -1,7 +1,4 @@ -import { - lowerActionArgumentChoices, - demoteActionArgumentChoices -} from '../../../../../../shared/source-control/ai/recipes/source-control-action-argument-document' +import { lowerActionArgumentChoices } from '../../../../../../shared/source-control/ai/recipes/source-control-action-argument-document' import { sourceControlActionSelectedPreferences, sourceControlActionExecutionArgs @@ -10,7 +7,6 @@ import type { TuiAgent } from '../../../../../../shared/agent-launch/catalog/tui import { readActionModelSelection } from '../../../../../../shared/source-control/ai/recipes/source-control-action-model-selection' import { DEFAULT_SOURCE_CONTROL_ACTION_COMMAND_TEMPLATES, - resolveSourceControlActionCommandTemplate, type SourceControlActionId, type SourceControlActionRecipe } from '../../../../../../shared/source-control/ai/recipes/source-control-ai-actions' @@ -27,13 +23,6 @@ export const DEFAULT_AGENT_VALUE = '__default_agent__' export const CUSTOM_COMMAND_MODE_INHERIT = 'inherit' export const CUSTOM_COMMAND_MODE_REPO = 'repo' -export function readInheritedCommandTemplate( - source: SourceControlAiSettings, - actionId: SourceControlActionId -): string { - return resolveSourceControlActionCommandTemplate(source.actions, actionId) -} - export function readInheritedAgentArgs( source: SourceControlAiSettings, actionId: SourceControlActionId @@ -60,29 +49,6 @@ export function resolveAgentArgsPlaceholderAgent( return defaultTuiAgent && defaultTuiAgent !== 'blank' ? defaultTuiAgent : null } -export function completeRepoActionRecipe( - recipe: SourceControlActionRecipe, - actionId: SourceControlActionId -): NonNullable< - NonNullable[SourceControlActionId] -> { - if (recipe.argumentChoices && recipe.argumentChoices.syntaxAgent !== recipe.agentId) { - recipe = demoteActionArgumentChoices(recipe) - } - const commandInputTemplate = - typeof recipe.commandInputTemplate === 'string' - ? recipe.commandInputTemplate - : DEFAULT_SOURCE_CONTROL_ACTION_COMMAND_TEMPLATES[actionId] - return { - agentId: recipe.agentId ?? null, - commandInputTemplate, - ...(typeof recipe.agentArgs === 'string' ? { agentArgs: recipe.agentArgs } : {}), - ...(recipe.argumentChoices ? { argumentChoices: recipe.argumentChoices } : {}), - ...(recipe.launchPreferences ? { launchPreferences: recipe.launchPreferences } : {}), - ...(recipe.surface ? { surface: recipe.surface } : {}) - } -} - export function actionScopeLabel(hasOverride: boolean): string { return hasOverride ? 'Customized for this repository' : 'Using global settings' } diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts new file mode 100644 index 00000000000..db7d5e42d0f --- /dev/null +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts @@ -0,0 +1,52 @@ +import { demoteActionArgumentChoices } from '../../../../../../shared/source-control/ai/recipes/source-control-action-argument-document' +import { resolveSourceControlActionRecipe } from '../../../../../../shared/source-control/ai/source-control-ai-operation-policy' +import { + DEFAULT_SOURCE_CONTROL_ACTION_COMMAND_TEMPLATES, + type SourceControlActionRecipe, + type SourceControlActionId +} from '../../../../../../shared/source-control/ai/recipes/source-control-ai-actions' +import type { RepoSourceControlAiOverrides } from '../../../../../../shared/source-control/ai/settings/source-control-ai-settings-types' +import type { GlobalSettings } from '../../../../../../shared/global-settings-types' + +export type RepoActionRecipe = NonNullable< + NonNullable[SourceControlActionId] +> + +function completeRepoActionRecipe( + recipe: SourceControlActionRecipe, + actionId: SourceControlActionId +): NonNullable< + NonNullable[SourceControlActionId] +> { + if (recipe.argumentChoices && recipe.argumentChoices.syntaxAgent !== recipe.agentId) { + recipe = demoteActionArgumentChoices(recipe) + } + const commandInputTemplate = + typeof recipe.commandInputTemplate === 'string' + ? recipe.commandInputTemplate + : DEFAULT_SOURCE_CONTROL_ACTION_COMMAND_TEMPLATES[actionId] + return { + agentId: recipe.agentId ?? null, + commandInputTemplate, + ...(typeof recipe.agentArgs === 'string' ? { agentArgs: recipe.agentArgs } : {}), + ...(recipe.argumentChoices ? { argumentChoices: recipe.argumentChoices } : {}), + ...(recipe.launchPreferences ? { launchPreferences: recipe.launchPreferences } : {}), + ...(recipe.surface ? { surface: recipe.surface } : {}) + } +} + +/** The action an edit starts from: the complete recipe it resolves to, owned or partial, keeping a + * saved explicit args clear and never the agent only detected to run a default selection. */ +export function readCompleteRecipeForDraft( + current: RepoSourceControlAiOverrides, + settings: Pick | null, + actionId: SourceControlActionId +): RepoActionRecipe { + const recipe = resolveSourceControlActionRecipe({ + settings, + repo: { sourceControlAi: current }, + actionId + }) + const cleared = current.actionOverrides?.[actionId]?.agentArgs === null + return { ...completeRepoActionRecipe(recipe, actionId), ...(cleared ? { agentArgs: null } : {}) } +} diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-text-draft.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-text-draft.ts index 87d1c2e44cc..5ad80a5a6cf 100644 --- a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-text-draft.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-text-draft.ts @@ -21,7 +21,7 @@ import { type ActionRecipeTextDraft } from './repository-source-control-ai-recipe-text' import { retireActionLaunchPreferences } from '../../source-control/source-control-ai-action-recipe-draft' -import { readInheritedCommandTemplate } from './repository-source-control-ai-labels' +import { resolveSourceControlActionCommandTemplate } from '../../../../../../shared/source-control/ai/recipes/source-control-ai-actions' export function resetActionDraftLaunchPreferences( current: Partial>, @@ -136,7 +136,8 @@ export function createRepoActionTextDraftEditors( const draft = current[actionId] ?? readActionRecipeTextDraft(immediateRepoAiRef.current, actionId) const currentTemplate = - draft.commandInputTemplate || readInheritedCommandTemplate(source, actionId) + draft.commandInputTemplate || + resolveSourceControlActionCommandTemplate(source.actions, actionId) const separator = currentTemplate.endsWith('\n') || currentTemplate.length === 0 ? '' : ' ' return patchActionTextDraft(current, immediateRepoAiRef.current, actionId, { commandInputTemplate: `${currentTemplate}${separator}{${variable}}` diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryWorktreeDefaultsSection.test.tsx b/src/renderer/src/components/settings/repository/worktrees/RepositoryWorktreeDefaultsSection.test.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryWorktreeDefaultsSection.test.tsx rename to src/renderer/src/components/settings/repository/worktrees/RepositoryWorktreeDefaultsSection.test.tsx diff --git a/src/renderer/src/components/settings/repository/execution/RepositoryWorktreeDefaultsSection.tsx b/src/renderer/src/components/settings/repository/worktrees/RepositoryWorktreeDefaultsSection.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/RepositoryWorktreeDefaultsSection.tsx rename to src/renderer/src/components/settings/repository/worktrees/RepositoryWorktreeDefaultsSection.tsx diff --git a/src/renderer/src/components/settings/repository/execution/SparsePresetSettingsSection.test.tsx b/src/renderer/src/components/settings/repository/worktrees/SparsePresetSettingsSection.test.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/SparsePresetSettingsSection.test.tsx rename to src/renderer/src/components/settings/repository/worktrees/SparsePresetSettingsSection.test.tsx diff --git a/src/renderer/src/components/settings/repository/execution/SparsePresetSettingsSection.tsx b/src/renderer/src/components/settings/repository/worktrees/SparsePresetSettingsSection.tsx similarity index 98% rename from src/renderer/src/components/settings/repository/execution/SparsePresetSettingsSection.tsx rename to src/renderer/src/components/settings/repository/worktrees/SparsePresetSettingsSection.tsx index b081c32e6ae..b15a6e36e5f 100644 --- a/src/renderer/src/components/settings/repository/execution/SparsePresetSettingsSection.tsx +++ b/src/renderer/src/components/settings/repository/worktrees/SparsePresetSettingsSection.tsx @@ -6,7 +6,7 @@ import { parseSparsePresetDirectories, validateSparsePresetName } from '@/lib/sp import { useMountedRef } from '@/hooks/useMountedRef' import { Button } from '../../../ui/button' import { getSparsePresetOperationErrorMessage } from './sparse-preset-operation-error' -import { SparsePresetDraftEditor } from './sparse-preset-draft-editor' +import { SparsePresetInlineEditor } from '../../../sparse/SparsePresetInlineEditor' import type { SparsePresetDraft } from '../../../sparse/SparseCheckoutPresetDraftForm' import { SparsePresetSettingsRow } from './sparse-preset-settings-row' import { useTranslate } from '@/i18n/use-translate' @@ -183,10 +183,10 @@ export function SparsePresetSettingsSection({ } const draftEditor = draft ? ( - { + onDraftChange={(nextDraft) => { if (nextDraft) { setDraft(nextDraft) } else { @@ -195,7 +195,8 @@ export function SparsePresetSettingsSection({ }} nameError={nameError} parsedDirectories={parsedDirectories} - canSaveDraft={canSaveDraft} + canSave={canSaveDraft} + onCancel={closeDraft} submitting={submitting} onSave={() => void handleSaveDraft()} repoRootPath={repo?.path} diff --git a/src/renderer/src/components/settings/repository/execution/WorktreeSymlinksSection.tsx b/src/renderer/src/components/settings/repository/worktrees/WorktreeSymlinksSection.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/WorktreeSymlinksSection.tsx rename to src/renderer/src/components/settings/repository/worktrees/WorktreeSymlinksSection.tsx diff --git a/src/renderer/src/components/settings/repository/execution/repository-git-worktree-search-entries.ts b/src/renderer/src/components/settings/repository/worktrees/repository-git-worktree-search-entries.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/repository-git-worktree-search-entries.ts rename to src/renderer/src/components/settings/repository/worktrees/repository-git-worktree-search-entries.ts diff --git a/src/renderer/src/components/settings/repository/execution/sparse-preset-date.ts b/src/renderer/src/components/settings/repository/worktrees/sparse-preset-date.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/sparse-preset-date.ts rename to src/renderer/src/components/settings/repository/worktrees/sparse-preset-date.ts diff --git a/src/renderer/src/components/settings/repository/execution/sparse-preset-directory-preview.tsx b/src/renderer/src/components/settings/repository/worktrees/sparse-preset-directory-preview.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/sparse-preset-directory-preview.tsx rename to src/renderer/src/components/settings/repository/worktrees/sparse-preset-directory-preview.tsx diff --git a/src/renderer/src/components/settings/repository/execution/sparse-preset-operation-error.ts b/src/renderer/src/components/settings/repository/worktrees/sparse-preset-operation-error.ts similarity index 100% rename from src/renderer/src/components/settings/repository/execution/sparse-preset-operation-error.ts rename to src/renderer/src/components/settings/repository/worktrees/sparse-preset-operation-error.ts diff --git a/src/renderer/src/components/settings/repository/execution/sparse-preset-settings-row.tsx b/src/renderer/src/components/settings/repository/worktrees/sparse-preset-settings-row.tsx similarity index 100% rename from src/renderer/src/components/settings/repository/execution/sparse-preset-settings-row.tsx rename to src/renderer/src/components/settings/repository/worktrees/sparse-preset-settings-row.tsx diff --git a/src/renderer/src/components/settings/source-control/SourceControlActionAgentPicker.tsx b/src/renderer/src/components/settings/source-control/SourceControlActionAgentPicker.tsx index 3cc5738e4e7..20b3767f7d6 100644 --- a/src/renderer/src/components/settings/source-control/SourceControlActionAgentPicker.tsx +++ b/src/renderer/src/components/settings/source-control/SourceControlActionAgentPicker.tsx @@ -43,17 +43,7 @@ type PickerProps = { onPick: (agent: TuiAgent | null, choice: Pick) => void } -export function SourceControlActionAgentPicker( - props: PickerProps | { text: ComponentProps } -) { - return 'text' in props ? ( - - ) : ( - - ) -} - -function ObservedSourceControlActionAgentPicker(props: PickerProps) { +export function SourceControlActionAgentPicker(props: PickerProps) { const repo = useAppStore((state) => state.repos.find((repo) => repo.id === props.repoId) ?? null) const rows = props.repoId ? sourceControlRepoAgentRows(repo) diff --git a/src/renderer/src/components/settings/source-control/SourceControlActionArguments.tsx b/src/renderer/src/components/settings/source-control/SourceControlActionArguments.tsx index 2e8a28d45b9..86d23419198 100644 --- a/src/renderer/src/components/settings/source-control/SourceControlActionArguments.tsx +++ b/src/renderer/src/components/settings/source-control/SourceControlActionArguments.tsx @@ -27,7 +27,7 @@ import type { TuiAgent } from '../../../../../shared/agent-launch/catalog/tui-ag import { useAppStore } from '@/store/index' import { useTranslate } from '@/i18n/use-translate' import { AgentDefaultArgsInput } from '../../agent/launch-settings/AgentLaunchDefaultsEditor' -import { AgentPermissionMenu } from '../../agent/launch-settings/AgentPermissionChoiceControls' +import { AgentPermissionMenu } from '../../agent/launch-settings/AgentPermissionMenu' export function SourceControlActionArguments({ agent, diff --git a/src/renderer/src/components/settings/source-control/text-actions/SourceControlTextActionAgentPicker.tsx b/src/renderer/src/components/settings/source-control/text-actions/SourceControlTextActionAgentPicker.tsx index a676ce71a09..469f6f0d646 100644 --- a/src/renderer/src/components/settings/source-control/text-actions/SourceControlTextActionAgentPicker.tsx +++ b/src/renderer/src/components/settings/source-control/text-actions/SourceControlTextActionAgentPicker.tsx @@ -43,7 +43,7 @@ import { type AgentPickerEntry, type AgentPickerDraft } from '../../../../../../shared/agent-launch/picker/agent-launch-picker' -import { SourceControlActionAgentPicker } from '../SourceControlActionAgentPicker' +import { AgentLaunchPicker } from '@/components/agent/AgentLaunchPicker' import { getAgentCatalogForAction } from '../source-control-action-recipe-options' import { DropdownMenuItem } from '../../../ui/dropdown-menu' @@ -308,8 +308,8 @@ const TextActionPicker = memo( .filter(Boolean) .join(' · ') return ( - void - connected: boolean - checking: boolean -} - -function getConnectStepState(props: ConnectStepProps): 'in-progress' | 'done' | 'pending' { - if (props.checking) { - return 'in-progress' - } - return props.connected ? 'done' : 'pending' -} +import { getConnectStepState, type ConnectStepProps } from './task-source-setup-state' export function CodeHostSetupSteps( props: ConnectStepProps & { @@ -78,50 +62,3 @@ export function CodeHostSetupSteps( ) } - -export function JiraSetupSteps( - props: ConnectStepProps & { onConnected: () => void; onOpenIntegrations: () => void } -): React.JSX.Element { - const t = useTranslate() - const [dialogOpen, setDialogOpen] = useState(false) - - return ( - <> -
    - setDialogOpen(true)} - > - {props.connected - ? t('auto.components.settings.TasksPane.manageJira', 'Manage keys') - : t('auto.components.settings.TasksPane.addJira', 'Add Jira access')} - - } - /> - -
- - - ) -} diff --git a/src/renderer/src/components/settings/tasks/TaskSourceJiraSetup.tsx b/src/renderer/src/components/settings/tasks/TaskSourceJiraSetup.tsx new file mode 100644 index 00000000000..a8b247dd674 --- /dev/null +++ b/src/renderer/src/components/settings/tasks/TaskSourceJiraSetup.tsx @@ -0,0 +1,54 @@ +import { useState } from 'react' +import { JiraConnectDialog } from '@/components/jira-connect-dialog' +import { Button } from '@/components/ui/button' +import { TaskSourceShowInTasksStep } from './TaskSourceShowInTasksStep' +import { TaskSourceStepRow } from './TaskSourceStepRow' +import { useTranslate } from '@/i18n/use-translate' +import { getConnectStepState, type ConnectStepProps } from './task-source-setup-state' + +export function JiraSetupSteps( + props: ConnectStepProps & { onConnected: () => void; onOpenIntegrations: () => void } +): React.JSX.Element { + const t = useTranslate() + const [dialogOpen, setDialogOpen] = useState(false) + + return ( + <> +
    + setDialogOpen(true)} + > + {props.connected + ? t('auto.components.settings.TasksPane.manageJira', 'Manage keys') + : t('auto.components.settings.TasksPane.addJira', 'Add Jira access')} + + } + /> + +
+ + + ) +} diff --git a/src/renderer/src/components/settings/tasks/TaskSourceSimpleSetup.test.tsx b/src/renderer/src/components/settings/tasks/TaskSourceSimpleSetup.test.tsx deleted file mode 100644 index c85146cb6af..00000000000 --- a/src/renderer/src/components/settings/tasks/TaskSourceSimpleSetup.test.tsx +++ /dev/null @@ -1,43 +0,0 @@ -// @vitest-environment happy-dom - -import { cleanup, fireEvent, render } from '@testing-library/react' -import { afterEach, describe, expect, it, vi } from 'vitest' -import { JiraSetupSteps } from './TaskSourceSimpleSetup' - -const mocks = vi.hoisted(() => ({ - dialogOpen: [] as boolean[] -})) - -vi.mock('@/components/jira-connect-dialog', () => ({ - JiraConnectDialog: ({ open }: { open: boolean }) => { - mocks.dialogOpen.push(open) - return null - } -})) - -afterEach(() => { - cleanup() - mocks.dialogOpen = [] -}) - -describe('JiraSetupSteps', () => { - it('routes connected credential management to Integrations', () => { - const onOpenIntegrations = vi.fn() - const rendered = render( - - ) - - fireEvent.click(rendered.getByRole('button', { name: 'Manage keys' })) - - expect(onOpenIntegrations).toHaveBeenCalledOnce() - expect(mocks.dialogOpen.at(-1)).toBe(false) - }) -}) diff --git a/src/renderer/src/components/settings/tasks/TasksPane.tsx b/src/renderer/src/components/settings/tasks/TasksPane.tsx index 7b549bac428..946912f85fe 100644 --- a/src/renderer/src/components/settings/tasks/TasksPane.tsx +++ b/src/renderer/src/components/settings/tasks/TasksPane.tsx @@ -13,7 +13,8 @@ import { LinearIcon } from '@/components/icons/LinearIcon' import { Button } from '@/components/ui/button' import { useAppStore } from '@/store/index' import { SearchableSetting } from '../section-layout/SearchableSetting' -import { CodeHostSetupSteps, JiraSetupSteps } from './TaskSourceSimpleSetup' +import { CodeHostSetupSteps } from './TaskSourceCodeHostSetup' +import { JiraSetupSteps } from './TaskSourceJiraSetup' import { TaskSourceLinearSetup } from './TaskSourceLinearSetup' import { TaskSourceProviderCard } from './TaskSourceProviderCard' import { diff --git a/src/renderer/src/components/settings/tasks/task-source-setup-state.ts b/src/renderer/src/components/settings/tasks/task-source-setup-state.ts index 70d126106c9..31a79cce3cc 100644 --- a/src/renderer/src/components/settings/tasks/task-source-setup-state.ts +++ b/src/renderer/src/components/settings/tasks/task-source-setup-state.ts @@ -145,3 +145,18 @@ export function resolveStickyAutoExpandedTaskProvider({ }): TaskProvider | null { return previousAutoExpanded ?? getAutoExpandedTaskProvider(providers, readinessByProvider) } + +export type ConnectStepProps = { + visible: boolean + canHide: boolean + onToggleVisible: () => void + connected: boolean + checking: boolean +} + +export function getConnectStepState(props: ConnectStepProps): 'in-progress' | 'done' | 'pending' { + if (props.checking) { + return 'in-progress' + } + return props.connected ? 'done' : 'pending' +} diff --git a/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx b/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx index 10565b005cb..08582d13463 100644 --- a/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx +++ b/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx @@ -11,7 +11,7 @@ import { emptyClaudeAccountsState } from '@/runtime/provider-accounts/observations/runtime-provider-accounts-client' import { toast } from 'sonner' -import { getClaudeAccountErrorDescription } from '../settings/accounts/accounts-pane-action-errors' +import { getClaudeAccountErrorDescription } from '../../lib/provider-accounts/provider-account-action-errors' import { useTranslate } from '@/i18n/use-translate' import { getWindowsTerminalCapabilityOwnerKey, diff --git a/src/renderer/src/components/status-bar/status-bar-runtime-targets.ts b/src/renderer/src/components/status-bar/status-bar-runtime-targets.ts index 45493c345ba..41f07440a0f 100644 --- a/src/renderer/src/components/status-bar/status-bar-runtime-targets.ts +++ b/src/renderer/src/components/status-bar/status-bar-runtime-targets.ts @@ -3,7 +3,7 @@ import type { RateLimitRuntimeTarget } from '../../../../shared/rate-limit-types import { resolveLocalAccountRuntimeTarget } from '../../../../shared/local-account-runtime' import { getRendererAppPlatform } from '../../lib/renderer-app-platform' import { translate } from '@/i18n/i18n' -import { getHostRuntimeLabel } from '../settings/accounts/scope/accounts-pane-runtime' +import { getHostRuntimeLabel } from '../../lib/provider-accounts/account-runtime-label' export type CodexStatusRuntimeTarget = { runtime: 'host' | 'wsl' diff --git a/src/renderer/src/lib/agent-session/launch/agent-settings-update-queue.test.ts b/src/renderer/src/lib/agent-session/launch/agent-settings-update-queue.test.ts index 71af9925394..ec15942fc1a 100644 --- a/src/renderer/src/lib/agent-session/launch/agent-settings-update-queue.test.ts +++ b/src/renderer/src/lib/agent-session/launch/agent-settings-update-queue.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it, vi } from 'vitest' import { getDefaultSettings } from '../../../../../shared/constants' import type { GlobalSettings } from '../../../../../shared/global-settings-types' +import { useAppStore } from '../../../store/index' import { createAgentAvailabilityUpdateQueue } from './agent-settings-update-queue' describe('agent availability settings', () => { @@ -40,3 +41,77 @@ describe('agent availability settings', () => { expect(updateSettings.mock.calls[1][0]).toMatchObject({ disabledTuiAgents: ['codex'] }) }) }) + +type Deferred = { + promise: Promise + resolve: () => void +} + +function createDeferred(): Deferred { + let resolve!: () => void + const promise = new Promise((next) => { + resolve = next + }) + return { promise, resolve } +} + +async function flushPromiseQueue(): Promise { + await Promise.resolve() + await Promise.resolve() +} + +describe('AgentsPane', () => { + it('serializes rapid availability writes against the latest settings snapshot', async () => { + const queueAvailabilityUpdate = createAgentAvailabilityUpdateQueue() + const settings: GlobalSettings = { + ...getDefaultSettings('/tmp'), + defaultTuiAgent: null, + disabledTuiAgents: [] + } + const writes: Deferred[] = [] + const updates: Partial[] = [] + + useAppStore.setState({ settings }) + const updateSettings = vi.fn((update: Partial) => { + updates.push(update) + const nextSettings = { + ...(useAppStore.getState().settings ?? settings), + ...update + } + const write = createDeferred() + writes.push(write) + return write.promise.then(() => { + useAppStore.setState({ settings: nextSettings }) + }) + }) + + const firstWrite = queueAvailabilityUpdate({ + getSettings: () => useAppStore.getState().settings, + fallbackSettings: settings, + updateSettings, + agentId: 'claude', + enabled: false + }) + const secondWrite = queueAvailabilityUpdate({ + getSettings: () => useAppStore.getState().settings, + fallbackSettings: settings, + updateSettings, + agentId: 'codex', + enabled: false + }) + + await flushPromiseQueue() + expect(updateSettings).toHaveBeenCalledTimes(1) + expect(updates[0]).toMatchObject({ disabledTuiAgents: ['claude'] }) + + writes[0].resolve() + await firstWrite + await flushPromiseQueue() + + expect(updateSettings).toHaveBeenCalledTimes(2) + expect(updates[1]).toMatchObject({ disabledTuiAgents: ['claude', 'codex'] }) + + writes[1].resolve() + await secondWrite + }) +}) diff --git a/src/renderer/src/lib/agent-session/launch/native-chat-provider-choice.test.tsx b/src/renderer/src/lib/agent-session/launch/native-chat-provider-choice.test.tsx index 6b007b10351..815e93a2caf 100644 --- a/src/renderer/src/lib/agent-session/launch/native-chat-provider-choice.test.tsx +++ b/src/renderer/src/lib/agent-session/launch/native-chat-provider-choice.test.tsx @@ -79,7 +79,7 @@ vi.mock('@/runtime/hosts/installation/managed-runtime-install-status', async (im vi.mock('@/runtime/hosts/installation/use-managed-runtime-install', () => ({ useManagedRuntimeInstall: () => runtime.current })) -vi.mock('../../../components/provider-accounts/AntigravityChatSignInRow', () => ({ +vi.mock('../../../components/provider-accounts/use-antigravity-chat-sign-in', () => ({ useAntigravityChatSignIn: () => ({ flow: null }) })) vi.mock( diff --git a/src/renderer/src/lib/claude-account-sign-in.ts b/src/renderer/src/lib/claude-account-sign-in.ts index 169b41fff9e..85148154eb7 100644 --- a/src/renderer/src/lib/claude-account-sign-in.ts +++ b/src/renderer/src/lib/claude-account-sign-in.ts @@ -4,7 +4,7 @@ import { useAppStore } from '@/store' import { getClaudeAccountErrorDescription, isClaudeAccountCancellation -} from '@/components/settings/accounts/accounts-pane-action-errors' +} from './provider-accounts/provider-account-action-errors' /** Sign in again via hidden claude auth login in the account folder; toast failures and keep cancellation silent. */ export async function signInToClaudeAccount(accountId: string): Promise { diff --git a/src/renderer/src/lib/provider-accounts/account-runtime-label.ts b/src/renderer/src/lib/provider-accounts/account-runtime-label.ts new file mode 100644 index 00000000000..8ed6cb5b416 --- /dev/null +++ b/src/renderer/src/lib/provider-accounts/account-runtime-label.ts @@ -0,0 +1,21 @@ +import { translate } from '../../i18n/i18n' +import type { Translate } from '../../i18n/use-translate' + +export type LocalAccountRuntime = { + runtime: 'host' | 'wsl' + wslDistro?: string | null + label: string +} + +export function getHostRuntimeLabel(t: Translate = translate): string { + return navigator.userAgent.includes('Windows') + ? 'Windows' + : t('auto.components.settings.AccountsPane.9baf45d071', 'This device') +} + +// Standalone runtime labels need sentence casing when interpolated into prose. +export function getRuntimeSentenceLabel(runtime: LocalAccountRuntime): string { + return runtime.runtime === 'host' && !navigator.userAgent.includes('Windows') + ? `${runtime.label.charAt(0).toLocaleLowerCase()}${runtime.label.slice(1)}` + : runtime.label +} diff --git a/src/renderer/src/components/settings/accounts/accounts-pane-action-errors.ts b/src/renderer/src/lib/provider-accounts/provider-account-action-errors.ts similarity index 92% rename from src/renderer/src/components/settings/accounts/accounts-pane-action-errors.ts rename to src/renderer/src/lib/provider-accounts/provider-account-action-errors.ts index 20c10fa5d6f..0f30e9ab077 100644 --- a/src/renderer/src/components/settings/accounts/accounts-pane-action-errors.ts +++ b/src/renderer/src/lib/provider-accounts/provider-account-action-errors.ts @@ -1,5 +1,5 @@ -import { CLAUDE_SIGN_IN_FAILED_MESSAGE } from '../../../../../shared/claude-sign-in-link' -import { CODEX_LOGIN_CANCELLED_MESSAGE } from '../../../../../shared/codex-auth-errors' +import { CLAUDE_SIGN_IN_FAILED_MESSAGE } from '../../../../shared/claude-sign-in-link' +import { CODEX_LOGIN_CANCELLED_MESSAGE } from '../../../../shared/codex-auth-errors' export function getCodexAccountErrorDescription(error: unknown): string { const message = String((error as Error)?.message ?? error) From a13c51fb3dc0d5a8cdf53b2574415b119b62f20f Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:54:53 -0800 Subject: [PATCH 22/80] Let Continue in Chat UI switch when Orca can't prove the terminal stopped, and start unsaved conversations fresh (#40033) * fix(native-chat): let Continue in Chat UI switch when Orca can't prove the terminal stopped Continue in Chat UI closed the agent's terminal and then refused the chat unless the host proved a clean exit, so an idle Claude on a slow or remote host failed with "couldn't confirm Claude stopped". Any settled exit wait now lets the chat start; the host's create still refuses a conversation another terminal holds, using the hook rows and foreground-process checks it already has. The wait outlasts the host's explicit-close stop. A conversation the agent never saved (nothing sent yet) has no history to open, so every Retry failed. After the terminal closes, that chat now restarts in the same tab as a new conversation instead. Tracked in #39085. * fix(native-chat): read a Claude transcript nothing was sent in as empty, and let an unknown writer check switch A Claude session that never got a message can leave a metadata-only transcript. Its import found no turns and refused as unreadable, so Continue in Chat UI still failed. A Claude file with no user or assistant record now reads as empty, the same refusal a missing transcript gets, which the handoff answers by starting the chat as a new conversation. Records Orca can't decode still count as a conversation, so unreadable history keeps failing. A host that couldn't say whether it checked other terminals no longer stops the switch before the close; the create checks them again. * fix(native-chat): read an empty Claude transcript as empty through adoption, and a broken record as a possible turn Adoption checks a transcript's identity before using it, and a metadata-only file carries no working directory, so the empty file still refused as unreadable. A file with no conversation adopts nothing, so it skips that check. A record too broken to parse may be a turn, so it keeps the history's own failure instead of reading as empty. * fix(native-chat): only a typed record with no message reads as Claude metadata A record shaped like a turn but missing a known type could read as metadata, so a transcript holding a real prompt could be taken for one nothing was sent in. Now only a JSON object with a non-empty type other than user or assistant and no message counts as metadata; anything else may be a turn and keeps the history's own failure. * refactor(native-chat): narrow the Claude record type without a cast * fix(native-chat): read a Claude meta user record as metadata when it shows nothing A Claude session where only a local command ran, such as /rename, saves a user record Claude flags as meta and no turn, so Continue in Chat UI still failed for it. A meta user record the decoder can't show now counts as metadata; one it shows, such as a tool result, still counts as a turn. --- .../journal-legacy-import.test.ts | 80 +++++++++++++++++++ .../history-import/journal-legacy-import.ts | 16 ++-- .../transcript-line-decoders-claude.ts | 23 ++++++ .../cli-chat-resume-close-admission.ts | 6 +- .../cli-chat-resume-close-terminal.test.ts | 11 ++- .../tab-bar/cli-chat-resume-close-terminal.ts | 28 ++----- .../tab-bar/cli-chat-resume-feedback.ts | 12 +-- .../tab-bar/cli-chat-resume-run-close.test.ts | 19 +++++ .../components/tab-bar/cli-chat-resume-run.ts | 28 ++++--- src/renderer/src/i18n/locales/en.json | 1 - .../launch/structured-agent-session-launch.ts | 16 +++- .../lib/launch-structured-agent-session.ts | 6 +- 12 files changed, 190 insertions(+), 56 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.test.ts b/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.test.ts index 32458ce43ee..57d579197a4 100644 --- a/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.test.ts +++ b/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.test.ts @@ -73,3 +73,83 @@ it.each([true, false])( }) } ) + +it.each([ + { tail: '', sourceEmpty: true, items: 0 }, + { tail: '{"type":"future-metadata"}', sourceEmpty: true, items: 0 }, + { tail: '{"message":{"role":"user","content":"existing prompt"}}', sourceEmpty: false, items: 0 }, + { + tail: '{"type":"future-turn","message":{"role":"user","content":"existing prompt"}}', + sourceEmpty: false, + items: 0 + }, + { tail: '{"type":"summary","message":null}', sourceEmpty: false, items: 0 }, + { tail: '{"type":"user","uuid":"u1"}', sourceEmpty: false, items: 0 }, + { tail: '{"type":7}', sourceEmpty: false, items: 0 }, + { tail: '{"type":""}', sourceEmpty: false, items: 0 }, + { tail: '{}', sourceEmpty: false, items: 0 }, + { tail: 'null', sourceEmpty: false, items: 0 }, + { tail: '[]', sourceEmpty: false, items: 0 }, + { tail: '{"type":"user","message":{"role":"us', sourceEmpty: false, items: 0 }, + { + tail: '{"type":"user","isMeta":true,"message":{"role":"user","content":"Local command"}}', + sourceEmpty: true, + items: 0 + }, + { + tail: '{"type":"user","isMeta":true,"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"saved output"}]}}', + sourceEmpty: false, + items: 1, + shows: 'saved output' + }, + { + tail: '{"type":"user","message":{"role":"user","content":"/rename"}}', + sourceEmpty: false, + items: 1, + shows: '/rename' + }, + { + tail: '{"type":"user","message":{"role":"user","content":[{"type":"future-content","text":"existing prompt"}]}}', + sourceEmpty: false, + items: 0 + } +])( + 'reads a Claude file nothing was sent in as empty, unlike one with a turn it cannot decode: %j', + async ({ + tail, + sourceEmpty, + items, + shows + }: { + tail: string + sourceEmpty: boolean + items: number + shows?: string + }) => { + const filePath = join(root, 'claude-unused.jsonl') + const metadata = [ + { type: 'file-history-snapshot', messageId: 'm1', snapshot: { trackedFileBackups: {} } }, + { + type: 'summary', + summary: 'New session', + leafUuid: 'l1', + cwd: root, + sessionId: 'claude-unused' + } + ] + await writeFile(filePath, `${[...metadata.map((m) => JSON.stringify(m)), tail].join('\n')}\n`) + // Adoption always names the conversation it expects, as Continue in Chat's create does. + const source = { providerSessionId: 'claude-unused', workspaceCwd: root } + const prepared = await prepareLegacyTranscriptImport({ + agent: 'claude', + sessionId: 'claude-unused', + options: { filePath, source } + }) + expect(prepared.ok).toBe(true) + expect(prepared.ok && prepared.items.length).toBe(items) + if (shows) { + expect(JSON.stringify(prepared.ok && prepared.items)).toContain(shows) + } + expect(prepared.ok && prepared.sourceEmpty).toBe(sourceEmpty) + } +) diff --git a/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.ts b/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.ts index 671148e67c4..bfc020a501c 100644 --- a/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.ts +++ b/src/main/native-chat/agent-session-journal/history-import/journal-legacy-import.ts @@ -16,6 +16,7 @@ import { resolveNativeChatTranscriptAgent } from '../../../../shared/native-chat import { resolveSessionFilePath, type ResolveSessionFileOptions } from '../../session-file-resolver' import { readLegacyImportTranscript, TRANSCRIPT_DECODERS } from './journal-legacy-transcript-stream' import { readOpenCodeNativeChatTranscriptFull } from '../../transcript-opencode' +import { mayBeClaudeConversationRecord } from '../../transcript-line-decoders-claude' import { createLegacyIdentityTracker } from './journal-legacy-identity' import { createPiTranscriptCollector } from './journal-pi-active-branch' import { createNewestItemWindow } from './journal-newest-item-window' @@ -107,7 +108,7 @@ export async function prepareLegacyTranscriptImport(input: { } try { - const { items, consumedBytes, forkTipItemIds } = await decodeReplacementItems({ + const { items, sourceEmpty, forkTipItemIds } = await decodeReplacementItems({ filePath, transcriptAgent, agent: input.agent, @@ -120,7 +121,7 @@ export async function prepareLegacyTranscriptImport(input: { decodedMessageIdentities: options.decodedMessageIdentities, limits }) - return { ok: true, items, sourceEmpty: consumedBytes === 0, forkTipItemIds } + return { ok: true, items, sourceEmpty, forkTipItemIds } } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) } } @@ -213,7 +214,7 @@ async function decodeReplacementItems(input: { codexSessionsDirs?: string[] signal?: AbortSignal limits: JournalPayloadLimits -}): Promise<{ items: JournalReplacementItem[]; consumedBytes: number; forkTipItemIds: string[] }> { +}): Promise<{ items: JournalReplacementItem[]; sourceEmpty: boolean; forkTipItemIds: string[] }> { const source = input.source ? adoptedTranscriptSourceObserver({ agent: input.agent, @@ -241,6 +242,8 @@ async function decodeReplacementItems(input: { : null const forkTip = createImportedForkTip() let lineIndex = 0 + // A Claude file of metadata alone is one nothing was sent in; an undecodable turn still counts. + let conversation = input.transcriptAgent !== 'claude' try { const { consumedBytes } = await readLegacyImportTranscript( { @@ -261,6 +264,7 @@ async function decodeReplacementItems(input: { return null } const message = TRANSCRIPT_DECODERS[input.transcriptAgent](line, fallbackId) + conversation ||= !!message || mayBeClaudeConversationRecord(line) if (message) { const identity = recordId || input.decodedMessageIdentities @@ -285,13 +289,15 @@ async function decodeReplacementItems(input: { } : undefined ) - if (consumedBytes > 0) { + // A file with no conversation adopts nothing, so it has no identity to prove. + if (consumedBytes > 0 && conversation) { await source?.validate() } if (pi) { await pi.project(retained.push) } - return { items: retained.items(), consumedBytes, forkTipItemIds: forkTip.settledItems() } + const sourceEmpty = consumedBytes === 0 || !conversation + return { items: retained.items(), sourceEmpty, forkTipItemIds: forkTip.settledItems() } } finally { pi?.close() } diff --git a/src/main/native-chat/transcript-line-decoders-claude.ts b/src/main/native-chat/transcript-line-decoders-claude.ts index 00290c78a84..2e256af18e4 100644 --- a/src/main/native-chat/transcript-line-decoders-claude.ts +++ b/src/main/native-chat/transcript-line-decoders-claude.ts @@ -209,3 +209,26 @@ function parseTimestamp(value: unknown): number | null { const parsed = timestampMs(value) return Number.isFinite(parsed) ? parsed : null } + +/** Anything but a typed record with no message, or a user record Claude flags as meta, may be a + * turn: what a transcript nothing was sent in lacks. */ +export function mayBeClaudeConversationRecord(line: string): boolean { + let record: unknown + try { + record = JSON.parse(line) + } catch { + return true + } + if (!record || typeof record !== 'object' || Array.isArray(record)) { + return true + } + // A local command's caveat or output; the decoder still keeps any meta record it can show. + if ('isMeta' in record && record.isMeta === true && 'type' in record && record.type === 'user') { + return false + } + if ('message' in record) { + return true + } + const type = 'type' in record ? record.type : undefined + return typeof type !== 'string' || !type.trim() || type === 'user' || type === 'assistant' +} diff --git a/src/renderer/src/components/tab-bar/cli-chat-resume-close-admission.ts b/src/renderer/src/components/tab-bar/cli-chat-resume-close-admission.ts index 7c09376c649..522ec0b93be 100644 --- a/src/renderer/src/components/tab-bar/cli-chat-resume-close-admission.ts +++ b/src/renderer/src/components/tab-bar/cli-chat-resume-close-admission.ts @@ -161,9 +161,9 @@ export async function admitCliChatResumeClose( toast.error(structuredChatUnavailableText(support.reason)) return null } - // Closing is irreversible: a host that could not say whether it checked the other writers has - // not admitted it. - if (support.kind !== 'admitted' || support.writers === 'unknown') { + // A host that could not say whether it checked the other writers still lets the switch go on: + // the create judges them again. + if (support.kind !== 'admitted') { throw new Error('chat unavailable') } const permission = await readStructuredChatHostPermissionVerdict(target, agent) diff --git a/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.test.ts b/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.test.ts index 62c78f06669..4ee8af6e867 100644 --- a/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.test.ts +++ b/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.test.ts @@ -126,19 +126,22 @@ it.each([ exitCode: -1, exitCause: { kind: 'unknown', reason: 'stop_unverified' } }, - { satisfied: true, status: 'unknown', exitCode: null } -])('rejects unproven exit evidence: %j', async (wait) => { + { satisfied: true, status: 'unknown', exitCode: null }, + new Error('timeout') +])('goes on once an exit the host could not prove settles: %j', async (wait) => { mocks.call.mockImplementation(async (_target, method, params) => { const leaf = params.paneKey?.endsWith(':b') ? 'b' : 'a' return method === 'terminal.resolvePane' ? { terminal: { handle: `handle-${leaf}`, ptyId: `pty-${leaf}`, incarnationId: `inc-${leaf}` } } - : { wait } + : wait instanceof Error + ? Promise.reject(wait) + : { wait } }) await expect( closeCliChatResumeTerminal(source, () => true, hooks(), new AbortController().signal) - ).rejects.toThrow('terminal exit unproven') + ).resolves.toBe(true) }) it('rechecks the source incarnation after an existing close dialog and says the close was dropped', async () => { mocks.close.mockImplementation((_tab, options) => { diff --git a/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.ts b/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.ts index 72972b75d71..035dd5aaaf1 100644 --- a/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.ts +++ b/src/renderer/src/components/tab-bar/cli-chat-resume-close-terminal.ts @@ -86,7 +86,7 @@ export async function closeCliChatResumeTerminal( }) ) hooks.prepared() - return await new Promise((resolve, reject) => { + return await new Promise((resolve) => { let started = false const cancel = () => { if (started) { @@ -133,7 +133,8 @@ export async function closeCliChatResumeTerminal( started = true signal.removeEventListener('abort', cancel) // Skip the already-checked compatibility await so every handle's wait is sent before close. - const exited = Promise.all( + // Any settled wait proceeds: the create's own writer check refuses a terminal still holding it. + const exited = Promise.allSettled( terminals.map((terminal) => callRuntimeRpc<{ wait: RuntimeTerminalWait }>( target, @@ -141,30 +142,15 @@ export async function closeCliChatResumeTerminal( { terminal: terminal.handle, for: 'exit', - timeoutMs: 15_000 + // Outlasts the host's explicit-close stop, so a slow stop isn't read as running. + timeoutMs: 30_000 }, - { signal, timeoutMs: 16_000, skipCompatibilityCheck: true } + { signal, timeoutMs: 31_000, skipCompatibilityCheck: true } ) ) ) hooks.closing() - void exited - .then((results) => { - if ( - results.some( - ({ wait }) => - !wait.satisfied || - wait.status !== 'exited' || - ((wait.exitCode === null || wait.exitCode < 0) && - wait.exitCause?.kind !== 'operator_close' && - wait.exitCause?.kind !== 'signaled') - ) - ) { - throw new Error('terminal exit unproven') - } - resolve(true) - }) - .catch(reject) + void exited.then(() => resolve(!signal.aborted)) return true } }) diff --git a/src/renderer/src/components/tab-bar/cli-chat-resume-feedback.ts b/src/renderer/src/components/tab-bar/cli-chat-resume-feedback.ts index 1b3fe65ef9a..b7d3f727ebb 100644 --- a/src/renderer/src/components/tab-bar/cli-chat-resume-feedback.ts +++ b/src/renderer/src/components/tab-bar/cli-chat-resume-feedback.ts @@ -56,12 +56,12 @@ export async function reportCliChatResumeFailureInChat( } } -/** Said in the chat that took the terminal's place, since that terminal is gone. */ -export function cliChatResumeExitUnprovenText(source: CliChatResumeSource): string { - return translate( - 'components.native-chat.resumeInNewChat.terminalExitUnprovenInChat', - "Orca closed {{agent}}'s terminal but couldn't confirm {{agent}} stopped. Make sure it has stopped, then try again.", - { agent: agentLabel(source.agent) } +/** The host found no history to open for the conversation: none saved, or none it can reach. */ +export function canRestartMissingConversation(error: unknown): boolean { + const refusal = structuredLaunchFailure(error) + return ( + refusal?.code === 'agent_session_identity_required' && + refusal.details?.reason === 'transcriptNotFound' ) } diff --git a/src/renderer/src/components/tab-bar/cli-chat-resume-run-close.test.ts b/src/renderer/src/components/tab-bar/cli-chat-resume-run-close.test.ts index 5b3fc7fcfa5..e38473b574a 100644 --- a/src/renderer/src/components/tab-bar/cli-chat-resume-run-close.test.ts +++ b/src/renderer/src/components/tab-bar/cli-chat-resume-run-close.test.ts @@ -98,6 +98,7 @@ import { runCliChatResume } from './cli-chat-resume-run' import { SOURCE, + refuse, resetCliChatResumeRun, showAgentInSource, tabs @@ -142,6 +143,24 @@ describe('close terminal then continue on capable hosts', () => { expect(resolveWebSessionVisibleTabId(useAppStore.getState(), 'wt-1')).toBe('picked') }) + it('starts the chat anew when the closed terminal never saved its conversation', async () => { + const answer = mocks.call.getMockImplementation() + mocks.call.mockImplementationOnce(async (_target, method) => { + expect(method).toBe('agentSession.create') + return { ok: false, refusal: refuse('transcriptNotFound') } + }) + await runCliChatResume(SOURCE) + await vi.waitFor(() => expect(creates()).toHaveLength(2)) + const [first, second] = creates().map(([, , params]) => params) + expect(first.resumeFrom?.providerSessionId).toBe(SOURCE.providerSessionId) + expect(second.resumeFrom).toBeUndefined() + expect(second.envelope.sessionId).toBe(first.envelope.sessionId) + mocks.call.mockImplementation(answer!) + await main?.settle() + expect(tabs().map((tab) => tab.entityId)).toEqual([first.envelope.sessionId]) + expect(mocks.toast).not.toHaveBeenCalled() + }) + // Joining it would re-send an unconfirmed chat's create beside the still-running CLI. it.each(['pending', 'visibility-unknown'])( 'shows a %s chat already opening this conversation and keeps the terminal', diff --git a/src/renderer/src/components/tab-bar/cli-chat-resume-run.ts b/src/renderer/src/components/tab-bar/cli-chat-resume-run.ts index 2388b6b4328..8460ff12a40 100644 --- a/src/renderer/src/components/tab-bar/cli-chat-resume-run.ts +++ b/src/renderer/src/components/tab-bar/cli-chat-resume-run.ts @@ -10,10 +10,8 @@ import { useSyncExternalStore } from 'react' import { useAppStore } from '@/store' import { newAgentLaunchRequestId } from '@/lib/agent-session/launch/routing/agent-launch-request-id' import { adoptAgentSessionLaunchVerdict } from '@/lib/agent-session/launch/routing/agent-session-launch-plan' -import { - StructuredAgentSessionCreateSourceFailedError, - type StructuredAgentSessionCreateGate -} from '@/lib/agent-session/launch/structured-agent-session-launch-errors' +import type { StructuredAgentSessionCreateGate } from '@/lib/agent-session/launch/structured-agent-session-launch-errors' +import { retryStructuredAgentSessionLaunch } from '@/lib/agent-session/launch/structured-agent-session-launch' import { beginStructuredAgentSessionProvisionalLaunch } from '@/lib/agent-session/structured-agent-session-provisional-tab' import { supportsStructuredAgentSessionCliSource } from '@/runtime/native-chat/host-capabilities/structured-agent-session-host-support' import { structuredAgentSessionTargetForHost } from '@/runtime/native-chat/session-ownership/structured-agent-session-owner' @@ -22,7 +20,7 @@ import { CLI_CONVERSATION_ADOPTING_AGENTS } from '../../../../shared/structured- import { toHostSessionPaneKey } from '../../../../shared/workspace-layout/terminal-surface-id' import { resolveCliChatResumeSource, type CliChatResumeSource } from './cli-chat-resume-source' import { - cliChatResumeExitUnprovenText, + canRestartMissingConversation, reportCliChatResumeFailure, reportCliChatResumeFailureInChat, reportCliChatResumeTerminalChanged @@ -269,7 +267,8 @@ export async function runCliChatResume( if (!closed) { return } - releaseCreate?.(true) + // A replacement chat the user closed while the terminal stopped starts nothing. + releaseCreate?.(!chatClosed()) } // A chat that ended before its tab opened has no terminal to close; its failure is said here. const result = await waitForPromiseWithSignal(launch.settlement, controller.signal) @@ -277,6 +276,17 @@ export async function runCliChatResume( return } if (closed) { + // The terminal is gone and the host has no history for it, so the chat starts a new conversation. + const chatSessionId = ( + useAppStore.getState().unifiedTabsByWorktree[source.workspaceId] ?? [] + ).find((tab) => tab.id === chatTabId)?.entityId + if ( + chatSessionId && + canRestartMissingConversation(result.error) && + retryStructuredAgentSessionLaunch(source.workspaceId, chatSessionId, { fresh: true }) + ) { + return + } await reportCliChatResumeFailureInChat(result.error, openOwner, target, dropChat) return } @@ -285,10 +295,8 @@ export async function runCliChatResume( } } catch (error) { if (closing && chatTabId && !chatClosed()) { - // The terminal is gone, so the chat that took its place says why, without a toast on top. - releaseCreate?.( - new StructuredAgentSessionCreateSourceFailedError(cliChatResumeExitUnprovenText(source)) - ) + // The terminal is gone, so the chat that took its place goes on; its create judges writers. + releaseCreate?.(true) } else if (pending.get(key)?.controller === controller) { await reportCliChatResumeFailure(source, error, false, target, closing) } diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 294a0232133..a4e8442aab0 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -20329,7 +20329,6 @@ "notSavedYet": "{{agent}} hasn't saved this conversation yet. Try again after it replies.", "terminalChanged": "The terminal running {{agent}} changed before it closed, so Orca left it open. Try again.", "terminalExitUnproven": "Orca closed {{agent}}'s terminal but couldn't confirm it stopped, so the chat didn't open. Check the host connection, then open the conversation from Session History.", - "terminalExitUnprovenInChat": "Orca closed {{agent}}'s terminal but couldn't confirm {{agent}} stopped. Make sure it has stopped, then try again.", "resumeFailed": "{{agent}} couldn't continue in chat. Check this terminal and the host connection, then try again." }, "state": { diff --git a/src/renderer/src/lib/agent-session/launch/structured-agent-session-launch.ts b/src/renderer/src/lib/agent-session/launch/structured-agent-session-launch.ts index f0301d1d189..386caf77d29 100644 --- a/src/renderer/src/lib/agent-session/launch/structured-agent-session-launch.ts +++ b/src/renderer/src/lib/agent-session/launch/structured-agent-session-launch.ts @@ -103,12 +103,16 @@ function resetStructuredLaunchCallers( state.callers.onSettled = () => maybeCleanupLaunchState(state) } -function restartStructuredLaunchState(state: StructuredLaunchState, requested = false): void { +function restartStructuredLaunchState( + state: StructuredLaunchState, + requested = false, + fresh = false +): void { const wasVisibilityUnknown = state.visibilityUnknown const retainedOpening = wasVisibilityUnknown && !!state.intent.params.firstMessage if (!wasVisibilityUnknown || (retainedOpening && requested)) { const surfaceAddress = state.intent.surfaceAddress - state.intent = retryStructuredAgentSessionLaunchIntent(state.intent, retainedOpening) + state.intent = retryStructuredAgentSessionLaunchIntent(state.intent, retainedOpening, fresh) state.intent.surfaceAddress = surfaceAddress } resetStructuredLaunchCallers(state, retainedOpening) @@ -286,7 +290,11 @@ export function startStructuredAgentLaunch( } } -export function retryStructuredAgentSessionLaunch(worktreeId: string, sessionId: string): boolean { +export function retryStructuredAgentSessionLaunch( + worktreeId: string, + sessionId: string, + { fresh = false }: { fresh?: boolean } = {} +): boolean { const state = getStructuredLaunchStateBySessionId(sessionId) ?? restorePersistedStructuredLaunchState(worktreeId, sessionId) @@ -298,7 +306,7 @@ export function retryStructuredAgentSessionLaunch(worktreeId: string, sessionId: ) { return false } - restartStructuredLaunchState(state, true) + restartStructuredLaunchState(state, true, fresh) return true } diff --git a/src/renderer/src/lib/launch-structured-agent-session.ts b/src/renderer/src/lib/launch-structured-agent-session.ts index 61a54d29480..aa59d16dcad 100644 --- a/src/renderer/src/lib/launch-structured-agent-session.ts +++ b/src/renderer/src/lib/launch-structured-agent-session.ts @@ -160,7 +160,9 @@ function buildStructuredAgentSessionLaunchIntent( /** A definitive refusal consumed its operation id, but the provisional tab still owns its session. */ export function retryStructuredAgentSessionLaunchIntent( intent: StructuredAgentSessionLaunchIntent, - retainOpening = false + retainOpening = false, + /** Starts a new conversation instead of resuming one the agent never saved. */ + fresh = false ): StructuredAgentSessionLaunchIntent { if (retainOpening) { const { beforeCreate: _beforeCreate, createAfter: _createAfter, ...retained } = intent @@ -181,7 +183,7 @@ export function retryStructuredAgentSessionLaunchIntent( intent, intent.agent, intent.sessionId, - intent.params.resumeFrom, + fresh ? undefined : intent.params.resumeFrom, intent.seedOptions, intent.params.forkFrom ), From 5ddfd7bcaf3383265786ae257fa134076e414ec5 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:56:18 -0800 Subject: [PATCH 23/80] An unanswered model or effort change fails only its message, and each agent process end is logged with its cause (#40038) * fix(native-chat): an unanswered model or effort change fails only its message, and each agent process end is logged with its cause A model or effort write the agent didn't answer within 30 s, or a send withdrawn while its write was pending, closed the agent's stdin. OpenCode exits 0 on that, so the chat showed "OpenCode stopped while this response was in progress (code 0)" and its background work died, with nothing logged. Session setters now carry a call deadline that sends $/cancel_request and never touches the stream; later controls and prompts wait for a withdrawn write to settle. Every agent process end logs who ended it and why. * fix(native-chat): name Orca as the one ending an agent it stopped after its own failure The exit record now reads endedBy orca for a host force-close and for an ACP agent Orca stopped after losing its connection or refusing a journal write, with that reason leading. The withdrawn-write test no longer races its deadline. * fix(native-chat): keep who ended an agent when its last journal write is refused Ruled by the independent decider: the failure-close cause is recorded before any publish that can re-enter cleanup, an ended or requested ending never takes a new one, and the ended event reads it from before publishing. A test drives a real refused journal write on an agent's own exit and on a lost connection. * fix(native-chat): redact the agent process end record's reason before it is logged The reason can carry the agent's stderr tail, and the console line is not redacted downstream. --- .../acp/options/acp-session-option-write.ts | 78 ++++++++-- .../acp/options/acp-structured-options.ts | 20 +++ src/main/acp/protocol/acp-session-call.ts | 7 +- src/main/acp/protocol/acp-session-runtime.ts | 34 ++++- src/main/acp/session/acp-session-ending.ts | 10 +- .../acp/session/acp-structured-acquire.ts | 9 +- ...p-structured-option-write-deadline.test.ts | 143 ++++++++++++++++++ .../session/acp-structured-session-adapter.ts | 10 +- .../acp-structured-session-contract.ts | 2 + .../acp-structured-child-lifecycle.ts | 18 ++- .../session/children/acp-structured-child.ts | 1 + .../acp/session/start/acp-structured-start.ts | 2 +- src/main/acp/transport/acp-errors.ts | 11 ++ src/main/acp/transport/acp-json-rpc-peer.ts | 21 +-- .../acp/transport/acp-response-settlement.ts | 3 + src/main/acp/turns/acp-model-command.ts | 3 +- .../structured-agent-session-adapter.ts | 2 + ...uctured-agent-session-child-exit-record.ts | 29 ++++ .../structured-agent-session-child-exit.ts | 47 ++---- ...uctured-agent-session-provider-exit-log.ts | 57 +++++++ 20 files changed, 419 insertions(+), 88 deletions(-) create mode 100644 src/main/acp/session/acp-structured-option-write-deadline.test.ts create mode 100644 src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit-record.ts create mode 100644 src/main/native-chat/agent-session-wire/termination/structured-agent-session-provider-exit-log.ts diff --git a/src/main/acp/options/acp-session-option-write.ts b/src/main/acp/options/acp-session-option-write.ts index 6600f26026a..d04bdb01779 100644 --- a/src/main/acp/options/acp-session-option-write.ts +++ b/src/main/acp/options/acp-session-option-write.ts @@ -9,18 +9,22 @@ export type AcpOptionWriteBound = { onUncertain?: (error: Error) => void } +/** A native setter carries its own call deadline, so an unanswered one fails only its send. */ export function writeAcpSessionOption( connection: Pick, options: AcpStructuredOptions, write: AcpOptionWrite, - bound: AcpOptionWriteBound & { key: string; value: string } + { onUncertain: _retire, ...bound }: AcpOptionWriteBound & { key: string; value: string } ): Promise { + const { timeoutMs } = bound return boundAcpOptionWrite(options, bound, () => write.method === 'config' ? connection - .setConfigOption(write.configId, write.value) + .setConfigOption(write.configId, write.value, undefined, undefined, timeoutMs) .then((result) => () => options.adoptConfigOptions(result.configOptions)) - : connection.setModel(write.modelId).then(() => () => options.adoptModel(write.modelId)) + : connection + .setModel(write.modelId, undefined, timeoutMs) + .then(() => () => options.adoptModel(write.modelId)) ) } @@ -38,26 +42,51 @@ export function boundAcpOptionWrite( const revision = ++options.writeRevision options.invalidateInput(bound.key) options.writing += 1 + let failed = false + let native: Promise<() => string | void> | null = null let completed = false - const applied = send().then((apply) => { - completed = true - if (options.writeRevision !== revision) { - throw new Error(`${agent} option write was superseded`) - } - const resolved = apply() - options.adoptOwnWrite(bound.key, bound.value, resolved ?? undefined) - }) + // A withdrawn control may still land, so this one is sent only after it settles. + const applied = options + .whenControlsSettle() + .then(() => { + if (failed) { + throw new Error(`${agent} option write was withdrawn before it was sent`) + } + native = send() + // Orca's own write stays in flight until the agent answers, so its echo is not drift. + const released = (): void => { + options.writing -= 1 + } + native.then(released, released) + return native + }) + .then((apply) => { + completed = true + if (options.writeRevision !== revision) { + throw new Error(`${agent} option write was superseded`) + } + const resolved = apply() + options.adoptOwnWrite(bound.key, bound.value, resolved ?? undefined) + }) void applied.catch(() => { completed = true }) return new Promise((resolve, reject) => { + let settled = false const fail = (error: Error): void => { + if (settled) { + return + } if (options.writeRevision === revision) { options.writeRevision += 1 } + failed = true settle() - // A local deadline/abort cannot cancel an already-sent native control. - if (!completed) { + if (native === null) { + options.writing -= 1 + } else if (!completed) { + // A local deadline/abort cannot cancel an already-sent native control. + options.holdUntilSettled(native, bound.key) bound.onUncertain?.(error) } reject(error) @@ -71,7 +100,6 @@ export function boundAcpOptionWrite( () => fail(new Error(`${agent} did not answer an option write within ${timeoutMs}ms`)), timeoutMs ) - let settled = false const settle = (): void => { if (settled) { return @@ -79,7 +107,6 @@ export function boundAcpOptionWrite( settled = true clearTimeout(timer) signal?.removeEventListener('abort', onAbort) - options.writing -= 1 } signal?.addEventListener('abort', onAbort, { once: true }) applied.then(() => { @@ -88,3 +115,24 @@ export function boundAcpOptionWrite( }, fail) }) } + +/** A prompt waits for withdrawn controls, each bounded by its call deadline, or for its own abort. */ +export async function acpControlsSettled( + session: { options: AcpStructuredOptions }, + signal?: AbortSignal +): Promise { + if (!signal) { + return session.options.whenControlsSettle() + } + signal.throwIfAborted() + let onAbort = (): void => {} + const aborted = new Promise((_resolve, reject) => { + onAbort = () => reject(abortSignalReason(signal)) + signal.addEventListener('abort', onAbort, { once: true }) + }) + try { + await Promise.race([session.options.whenControlsSettle(), aborted]) + } finally { + signal.removeEventListener('abort', onAbort) + } +} diff --git a/src/main/acp/options/acp-structured-options.ts b/src/main/acp/options/acp-structured-options.ts index 8fe1895e87b..5327bde5116 100644 --- a/src/main/acp/options/acp-structured-options.ts +++ b/src/main/acp/options/acp-structured-options.ts @@ -64,6 +64,26 @@ export class AcpStructuredOptions { // selector, so a concrete change away from this retires it; ordinary control proof never does. private selectorProof: Readonly> | null = null + // Native controls a withdrawn send stopped waiting for; their call deadline bounds each one. + private readonly unsettled = new Set>() + + /** Later controls and prompts wait for it, and its key is re-sent once it settles. */ + holdUntilSettled(native: Promise, key: string): void { + this.unsettled.add(native) + const settled = (): void => { + this.unsettled.delete(native) + this.invalidateInput(key) + } + native.then(settled, settled) + } + + /** Resolves once no withdrawn control can still land at the agent. */ + async whenControlsSettle(): Promise { + while (this.unsettled.size > 0) { + await Promise.allSettled(this.unsettled) + } + } + invalidateInput(key: string): void { this.appliedInput.delete(key) if (key === 'model') { diff --git a/src/main/acp/protocol/acp-session-call.ts b/src/main/acp/protocol/acp-session-call.ts index 3057ddd33e6..1d330a49345 100644 --- a/src/main/acp/protocol/acp-session-call.ts +++ b/src/main/acp/protocol/acp-session-call.ts @@ -13,10 +13,13 @@ export function callAcpSession( params: unknown, schema: z.ZodType, answered: ((signedIn: boolean) => void) | undefined, - received?: (response: T) => void + received?: (response: T) => void, + timeoutMs: number | null = null ): Promise { return peer.request(method, params, { - timeoutMs: null, + timeoutMs, + // A bounded session control fails only itself; the agent is asked to drop it. + ...(timeoutMs === null ? {} : { onTimeout: 'cancel' as const }), settle: (response) => { if ('error' in response) { const error = response.error diff --git a/src/main/acp/protocol/acp-session-runtime.ts b/src/main/acp/protocol/acp-session-runtime.ts index 3b8959705ab..517142f647c 100644 --- a/src/main/acp/protocol/acp-session-runtime.ts +++ b/src/main/acp/protocol/acp-session-runtime.ts @@ -201,19 +201,31 @@ export class AcpSessionRuntime { } return this.peer.request(method, params, { timeoutMs }) } - async setModel(modelId: string, meta?: Meta): Promise { + /** `timeoutMs` drops an unanswered call from the pending set; null waits for the answer. */ + async setModel( + modelId: string, + meta?: Meta, + timeoutMs: number | null = null + ): Promise { const params: SetSessionModelRequest = { sessionId: this.sessionId(), modelId, ...withMeta(meta) } - return this.call('session/set_model', params, SetSessionModelResponseSchema) + return this.call( + 'session/set_model', + params, + SetSessionModelResponseSchema, + undefined, + timeoutMs + ) } async setConfigOption( configId: SetSessionConfigOptionRequest['configId'], value: SetSessionConfigOptionRequest['value'], meta?: Meta, - received?: (response: SetSessionConfigOptionResponse) => void + received?: (response: SetSessionConfigOptionResponse) => void, + timeoutMs: number | null = null ): Promise { const params = { configId, sessionId: this.sessionId(), ...withMeta(meta) } const request = @@ -224,7 +236,8 @@ export class AcpSessionRuntime { 'session/set_config_option', request, SetSessionConfigOptionResponseSchema, - received + received, + timeoutMs ) } @@ -248,9 +261,18 @@ export class AcpSessionRuntime { method: string, params: unknown, schema: z.ZodType, - received?: (response: T) => void + received?: (response: T) => void, + timeoutMs: number | null = null ): Promise { - return callAcpSession(this.peer, method, params, schema, this.options.onAuthAnswered, received) + return callAcpSession( + this.peer, + method, + params, + schema, + this.options.onAuthAnswered, + received, + timeoutMs + ) } private handleRequest(method: string, params: unknown, context: AcpRequestContext): unknown { diff --git a/src/main/acp/session/acp-session-ending.ts b/src/main/acp/session/acp-session-ending.ts index 3a5a6ef7eb6..690f2a7ba15 100644 --- a/src/main/acp/session/acp-session-ending.ts +++ b/src/main/acp/session/acp-session-ending.ts @@ -41,16 +41,15 @@ export function endAcpStructuredSession( if (session.ended) { return } + // Publishing the ending can re-enter cleanup, so attribution comes from before it. + const failureClose = session.failureClose session.ended = true session.events?.observeExit?.(undefined, observedAt) session.exitObservedAt = observedAt const stderr = session.connection.stderrTail() const how = providerExitStatusText(session.connection.exitStatus) - // Process exit can follow stream loss; keep that earlier reason when stderr is absent. - const lead = - session.journalClosed !== null && !stderr - ? session.journalClosed - : `${session.spec.agent} ACP agent exited` + // Orca's own close (stream loss, refusal) names the cause; the agent's stderr stays beside it. + const lead = failureClose ?? session.journalClosed ?? `${session.spec.agent} ACP agent exited` const reason = session.closeRequested ? `${session.spec.agent} ACP agent closed by Orca` : `${lead}${how ? ` ${how}` : ''}${stderr ? `: ${stderr}` : ''}` @@ -77,6 +76,7 @@ export function endAcpStructuredSession( ? agentSessionFailureFact('hostFault') : agentSessionFailureFact('providerExited', detail ? { detail } : {}), cause: session.closeRequested ? 'requested-close' : 'unexpected-exit', + ...(failureClose !== null && !session.closeRequested ? { closedByOrca: true as const } : {}), fence: session.fence, acquisitionGeneration: session.acquisitionGeneration, observedAt diff --git a/src/main/acp/session/acp-structured-acquire.ts b/src/main/acp/session/acp-structured-acquire.ts index 88d8a8163a0..8cb822ebf5f 100644 --- a/src/main/acp/session/acp-structured-acquire.ts +++ b/src/main/acp/session/acp-structured-acquire.ts @@ -46,7 +46,7 @@ export async function acquireAcpStructuredSession(input: { /** Protocol loss may precede process exit; null means startup has not produced a session. */ onConnectionLost: (session: AcpStructuredChild | null, error: Error) => void onSettled: AcpStructuredTurnsDeps['settle'] - forceClose: (sessionId: string) => void + forceClose: (sessionId: string, reason: string) => void }): Promise<{ acquisition: AgentSessionAcquisition session: AcpStructuredChild @@ -214,7 +214,11 @@ export async function acquireAcpStructuredSession(input: { session.turns?.accept(clientMessageId) } }, - onFailed: () => input.forceClose(sessionId) + onFailed: (reason) => + input.forceClose( + sessionId, + `Orca stopped ${agentName} after its chat journal refused a write (${reason})` + ) }) slot.lane = lane slot.reattaching = attaching @@ -260,6 +264,7 @@ export async function acquireAcpStructuredSession(input: { prompts, childStops: undefined, closeRequested: false, + failureClose: null, journalClosed: null, ended: false, exitObservedAt: null, diff --git a/src/main/acp/session/acp-structured-option-write-deadline.test.ts b/src/main/acp/session/acp-structured-option-write-deadline.test.ts new file mode 100644 index 00000000000..55fdf7193fa --- /dev/null +++ b/src/main/acp/session/acp-structured-option-write-deadline.test.ts @@ -0,0 +1,143 @@ +import { afterEach, expect, it } from 'vitest' +import { + closeProviderTimelineRigs, + SESSION +} from '../../native-chat/agent-session-timeline/provider-timeline-assembler-test-support' +import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types' +import type { FakeFrame } from '../process/testing/acp-scripted-agent.test-support' +import { + GROK_CONFIG_OPTIONS, + openAcpAdapterRig, + waitFor, + type AcpAdapterRig +} from './testing/acp-structured-adapter.test-support' + +afterEach(closeProviderTimelineRigs) + +const HELLO: AgentJournalMessageItem = { + kind: 'message', + role: 'user', + blocks: [{ type: 'text', text: 'hello' }] +} + +function send( + rig: AcpAdapterRig, + clientMessageId: string, + executionOptions: { model?: string; effort?: string }, + signal?: AbortSignal +) { + return rig.adapter.dispatch({ + sessionId: SESSION, + clientMessageId, + body: HELLO, + fence: 1, + executionOptions, + ...(signal ? { signal } : {}) + }) +} + +/** Holds the first model/effort write unanswered; answers every later one. */ +async function openWithHeldFirstWrite( + optionWriteTimeoutMs: number +): Promise<{ rig: AcpAdapterRig; held: () => FakeFrame }> { + let held: FakeFrame | undefined + const rig = await openAcpAdapterRig({ + deps: { optionWriteTimeoutMs }, + script: (agent) => { + agent.on('session/set_config_option', (frame) => { + if (!held) { + held = frame + return + } + agent.reply(frame, { configOptions: GROK_CONFIG_OPTIONS }) + }) + } + }) + await rig.acquire() + return { + rig, + held: () => { + if (!held) { + throw new Error('no option write held yet') + } + return held + } + } +} + +it('refuses only the message whose model write went unanswered, keeping the agent running', async () => { + const { rig, held } = await openWithHeldFirstWrite(100) + + await expect(send(rig, 'send-1', { model: 'grok-4.6' })).rejects.toThrow() + + // The unanswered call is withdrawn at the agent, and the agent process is never ended for it. + const cancel = await rig.frame('$/cancel_request') + expect(cancel.params).toEqual({ requestId: held().id }) + await rig.settle() + expect(rig.ended).toEqual([]) + expect(rig.child().closes).toBe(0) + expect(rig.spawned.filter((step) => step === 'spawn')).toHaveLength(1) + + await expect(send(rig, 'send-2', { model: 'grok-4.6' })).resolves.toEqual({ state: 'admitted' }) + expect(rig.sent('session/set_config_option')).toHaveLength(2) + await rig.frame('session/prompt') +}) + +it('holds later controls and prompts until a withdrawn write is answered', async () => { + // Long enough that no deadline expires; the held write settles only when answered below. + const { rig, held } = await openWithHeldFirstWrite(10_000) + const withdrawn = new AbortController() + const first = send(rig, 'send-1', { effort: 'low' }, withdrawn.signal) + await waitFor(() => held()) + withdrawn.abort(new Error('replaced by a newer message')) + await expect(first).rejects.toThrow('replaced by a newer message') + + const second = send(rig, 'send-2', { model: 'grok-4.6' }) + await rig.settle() + // The withdrawn effort write may still land, so nothing follows it to the agent yet. + expect(rig.sent('session/set_config_option')).toHaveLength(1) + expect(rig.sent('session/prompt')).toEqual([]) + + rig.child().agent.reply(held(), { configOptions: GROK_CONFIG_OPTIONS }) + await expect(second).resolves.toEqual({ state: 'admitted' }) + expect(rig.sent('session/set_config_option')).toHaveLength(2) + await rig.frame('session/prompt') + expect(rig.ended).toEqual([]) +}) + +it.each([ + { + end: 'the agent exits on its own', + trigger: (rig: AcpAdapterRig) => rig.child().exit(), + closedByOrca: undefined, + reason: /^grok ACP agent exited/ + }, + { + end: 'Orca loses its connection to the agent', + trigger: (rig: AcpAdapterRig) => rig.child().agent.stdin.destroy(), + closedByOrca: true, + reason: /^grok ACP connection closed/ + } +])( + 'keeps who ended the agent when its final journal write is refused: $end', + async ({ trigger, closedByOrca, reason }) => { + const rig = await openAcpAdapterRig() + let refusing = false + const events = rig.rig.eventSink + const append = events.tryAppendTransition?.bind(events) + rig.rig.eventSink = { + ...events, + tryAppendTransition: (transition) => + refusing ? { accepted: false, reason: 'failed' } : append!(transition) + } + await rig.acquire() + refusing = true + trigger(rig) + const [ended] = await waitFor(() => { + expect(rig.ended).toHaveLength(1) + return rig.ended + }) + expect(ended?.closedByOrca).toBe(closedByOrca) + expect(ended?.reason).toMatch(reason) + } +) diff --git a/src/main/acp/session/acp-structured-session-adapter.ts b/src/main/acp/session/acp-structured-session-adapter.ts index 77fb871ea49..c7c4c31a36a 100644 --- a/src/main/acp/session/acp-structured-session-adapter.ts +++ b/src/main/acp/session/acp-structured-session-adapter.ts @@ -23,7 +23,7 @@ import { type AcpStructuredSessionAdapterDeps } from './acp-structured-session-adapter-deps' import { readAcpLiveOptions } from '../options/acp-live-options' -import { writeAcpSessionOption } from '../options/acp-session-option-write' +import { acpControlsSettled, writeAcpSessionOption } from '../options/acp-session-option-write' import { acpOptionWriteBound, withAcpModelFallback } from '../turns/acp-model-command' import { stopAcpChildren, acpChildStopCapabilities } from '../turns/acp-structured-child-stop' import * as childState from './children/acp-structured-child' @@ -125,6 +125,7 @@ export class AcpStructuredSessionAdapter implements SessionAdapter { if (!optionsApplied) { return rejectedAcpDispatch(session) } + await acpControlsSettled(session, signal) input.signal?.throwIfAborted() await input.beforeDispatch?.() input.signal?.throwIfAborted() @@ -141,8 +142,11 @@ export class AcpStructuredSessionAdapter implements SessionAdapter { }) } - compact: NonNullable = async (input) => - this.live(input.sessionId).turns.compact(input.command) + compact: NonNullable = async (input) => { + const session = this.live(input.sessionId) + await acpControlsSettled(session) + return session.turns.compact(input.command) + } cancelTurn: SessionAdapter['cancelTurn'] = async (input) => { const session = this.live(input.sessionId) diff --git a/src/main/acp/session/acp-structured-session-contract.ts b/src/main/acp/session/acp-structured-session-contract.ts index 5e3388a4b69..5af81feb4e2 100644 --- a/src/main/acp/session/acp-structured-session-contract.ts +++ b/src/main/acp/session/acp-structured-session-contract.ts @@ -24,6 +24,8 @@ export type AcpStructuredSession = { options: AcpStructuredOptions turns: AcpStructuredTurns closeRequested: boolean + /** Why Orca stopped this agent after its own failure (stream loss, a refused journal write). */ + failureClose: string | null /** Null while provider events can still reach the journal. */ journalClosed: string | null /** Exit was proven and reported to the host. */ diff --git a/src/main/acp/session/children/acp-structured-child-lifecycle.ts b/src/main/acp/session/children/acp-structured-child-lifecycle.ts index 73d4f023feb..1a0a6a4f4f1 100644 --- a/src/main/acp/session/children/acp-structured-child-lifecycle.ts +++ b/src/main/acp/session/children/acp-structured-child-lifecycle.ts @@ -60,7 +60,12 @@ export class AcpStructuredChildren { (pending() || lane.hasPendingDiagnostic(input.clientMessageId)) } - stopAfterFailure(sessionId: string): void { + stopAfterFailure(sessionId: string, reason: string): void { + const session = this.sessions.get(sessionId) + // An ending already under way (agent exit, requested close) keeps its own attribution. + if (session && !session.ended && !session.closeRequested) { + session.failureClose ??= reason + } void this.stop(sessionId, false).catch((error: unknown) => { const logger = neverThrowingStructuredAgentSessionLogger( this.deps.logger ?? createStructuredAgentSessionLogger() @@ -228,12 +233,11 @@ export class AcpStructuredChildren { session.journalClosed = error.message || error.name return } - closeAcpSessionJournal( - session, - `${session.spec.agent} ACP connection closed: ${error.message || error.name}`, - error - ) - this.stopAfterFailure(session.sessionId) + const reason = `${session.spec.agent} ACP connection closed: ${error.message || error.name}` + // Record the cause first: publishing the close can re-enter cleanup through a refused write. + session.failureClose ??= reason + closeAcpSessionJournal(session, reason, error) + this.stopAfterFailure(session.sessionId, reason) } private stopGraceMs(): number { diff --git a/src/main/acp/session/children/acp-structured-child.ts b/src/main/acp/session/children/acp-structured-child.ts index 5da21afe726..0f26e9ba4b2 100644 --- a/src/main/acp/session/children/acp-structured-child.ts +++ b/src/main/acp/session/children/acp-structured-child.ts @@ -15,6 +15,7 @@ export type AcpStructuredChild = Pick< | 'permissionModes' | 'connection' | 'closeRequested' + | 'failureClose' | 'journalClosed' | 'ended' | 'exitObservedAt' diff --git a/src/main/acp/session/start/acp-structured-start.ts b/src/main/acp/session/start/acp-structured-start.ts index e4fb2f687ef..d148ff64f8e 100644 --- a/src/main/acp/session/start/acp-structured-start.ts +++ b/src/main/acp/session/start/acp-structured-start.ts @@ -60,7 +60,7 @@ export async function startAcpStructuredSession( }, onConnectionLost: (session, error) => children.connectionLost(session, error), onSettled: (settlement) => deps.onDispatchSettledLate?.({ sessionId, ...settlement }), - forceClose: (id) => children.stopAfterFailure(id) + forceClose: (id, reason) => children.stopAfterFailure(id, reason) }) if (attempt.signal.aborted) { session.dispose() diff --git a/src/main/acp/transport/acp-errors.ts b/src/main/acp/transport/acp-errors.ts index 6474ceac2b0..d167f0fd609 100644 --- a/src/main/acp/transport/acp-errors.ts +++ b/src/main/acp/transport/acp-errors.ts @@ -102,3 +102,14 @@ export class AcpRequestTimeoutError extends Error { withProviderRequestTimeout(this, { method, timeoutMs }) } } + +/** Null waits for the answer; otherwise `expire` runs once the call goes unanswered. */ +export function startAcpRequestDeadline( + method: string, + timeoutMs: number | null, + expire: (error: AcpRequestTimeoutError) => void +): ReturnType | undefined { + return timeoutMs === null + ? undefined + : setTimeout(() => expire(new AcpRequestTimeoutError(method, timeoutMs)), timeoutMs) +} diff --git a/src/main/acp/transport/acp-json-rpc-peer.ts b/src/main/acp/transport/acp-json-rpc-peer.ts index b8196da7c8b..9615cc88484 100644 --- a/src/main/acp/transport/acp-json-rpc-peer.ts +++ b/src/main/acp/transport/acp-json-rpc-peer.ts @@ -10,7 +10,7 @@ import { createIncrementalNdjsonFramer, encodeNdjson } from '../../../shared/main-process-ndjson-framer' -import { AcpConnectionClosedError, AcpRequestTimeoutError } from './acp-errors' +import { AcpConnectionClosedError, startAcpRequestDeadline } from './acp-errors' import { AcpIncomingRequests } from './acp-incoming-requests' import { requestTimeout, resolveAcpPeerOptions, type AcpPeerOptions } from './acp-peer-limits' @@ -136,15 +136,16 @@ export class AcpJsonRpcPeer { reject(error) } } - const timer = - timeoutMs === null - ? undefined - : setTimeout(() => { - this.pending.delete(id) - const error = new AcpRequestTimeoutError(method, timeoutMs) - controller.abort(error) - reject(error) - }, timeoutMs) + const timer = startAcpRequestDeadline(method, timeoutMs, (error) => { + this.pending.delete(id) + if (options.onTimeout === 'cancel') { + // Cutting a line mid-write ends the stream, so the cancel follows the call instead. + void this.notify('$/cancel_request', { requestId: id }).catch(() => {}) + } else { + controller.abort(error) + } + reject(error) + }) this.pending.set(id, { method, resolve: (value) => settle({ value }), diff --git a/src/main/acp/transport/acp-response-settlement.ts b/src/main/acp/transport/acp-response-settlement.ts index 4e31227e31a..4c420c3a54b 100644 --- a/src/main/acp/transport/acp-response-settlement.ts +++ b/src/main/acp/transport/acp-response-settlement.ts @@ -2,6 +2,9 @@ export type AcpResponse = { value: unknown } | { error: Error } export type AcpRequestOptions = { timeoutMs?: number | null + /** 'cancel': a timeout asks the agent to drop the call and never touches the stream, so an + * unanswered control cannot end the connection. Default withdraws the write. */ + onTimeout?: 'withdraw' | 'cancel' settle: (response: AcpResponse) => T } diff --git a/src/main/acp/turns/acp-model-command.ts b/src/main/acp/turns/acp-model-command.ts index 2e303b99248..304ba71c035 100644 --- a/src/main/acp/turns/acp-model-command.ts +++ b/src/main/acp/turns/acp-model-command.ts @@ -74,7 +74,8 @@ async function applyAcpModelCommand( }) } -/** Option writes and the model command share one deadline, and an unanswered one retires the child. */ +/** Option writes and the model command share one deadline; only an unanswered command, a hidden + * prompt Orca cannot cancel, retires the child. */ export function acpOptionWriteBound( deps: Pick, session: Pick, 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 6d787d22af4..6b5e43543df 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 @@ -109,6 +109,8 @@ export type StructuredAgentSessionEndedEvent = { /** Absent means provider exit without extra detail; host faults retain their own classification. */ failure?: SubmissionRejectionFact cause: 'unexpected-exit' | 'requested-close' + /** An unexpected exit Orca caused by stopping the agent after its own failure; log only. */ + closedByOrca?: true deathEvidenceKind?: 'kill-delivered' /** Execution-host observations from a requested provider close, when available. */ processExit?: { diff --git a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit-record.ts b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit-record.ts new file mode 100644 index 00000000000..c77b1f82789 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit-record.ts @@ -0,0 +1,29 @@ +import type { SubmissionRejectionFact } from '../../../../shared/agent-session/failures/agent-session-failure' +import type { StructuredAgentSessionEndedEvent } from '../providers/structured-agent-session-adapter' + +export type StructuredAgentSessionChildExit = { + deathEvidenceKind?: 'kill-delivered' + heldSends?: ReadonlySet + expected: boolean + /** Log only; failure supplies user-facing wording. */ + reason: string + failure?: SubmissionRejectionFact + observedAt?: number + startupUnanswered?: true + /** Orca stopped the agent after its own failure; the exit stays unexpected for the turn. */ + closedByOrca?: true +} + +export function structuredAgentSessionEndedEventExit( + event: StructuredAgentSessionEndedEvent +): StructuredAgentSessionChildExit { + return { + expected: event.cause === 'requested-close', + ...(event.deathEvidenceKind ? { deathEvidenceKind: event.deathEvidenceKind } : {}), + reason: event.reason, + ...(event.failure ? { failure: event.failure } : {}), + ...(event.observedAt === undefined ? {} : { observedAt: event.observedAt }), + ...(event.startupUnanswered ? { startupUnanswered: event.startupUnanswered } : {}), + ...(event.closedByOrca ? { closedByOrca: true as const } : {}) + } +} diff --git a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit.ts b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit.ts index fe82ef80f6c..66b8e459c31 100644 --- a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit.ts +++ b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-child-exit.ts @@ -1,10 +1,7 @@ // First-hand writer death evidence ends the child record in finally. Attempt/report every bookkeeping step; // expected changes presentation, never whether the dead child remains recorded. -import { - agentSessionFailureFact, - type SubmissionRejectionFact -} from '../../../../shared/agent-session/failures/agent-session-failure' +import { agentSessionFailureFact } from '../../../../shared/agent-session/failures/agent-session-failure' import { PROVIDER_EXIT_ROW_PREFIX } from '../../../../shared/agent-session/lifecycle/agent-session-stop-row-identity' import { isQueuedAgentJournalSubmission } from '../../../../shared/agent-session/requests/agent-session-queued-submission' import { structuredAgentSessionFailureWordsContext } from '../operations/structured-agent-session-send-preparation' @@ -33,21 +30,13 @@ import { } from './structured-agent-session-unfinished-work' import type { StructuredAgentSessionLogger } from '../host/structured-agent-session-logger' import { evictStructuredAgentSession } from './structured-agent-session-eviction' +import type { StructuredAgentSessionChildExit } from './structured-agent-session-child-exit-record' +import { structuredAgentSessionEndedEventExit } from './structured-agent-session-child-exit-record' +import { logExitFailure, logProviderChildExit } from './structured-agent-session-provider-exit-log' import type { StructuredAgentSessionHostRuntimeState } from '../host/structured-agent-session-host-runtime-state' import type { StructuredAgentSessionStartFlights } from '../startup/structured-agent-session-start-flight' import type { StructuredAgentSessionStartupAttempts } from '../startup/structured-agent-session-startup-attempt' -export type StructuredAgentSessionChildExit = { - deathEvidenceKind?: 'kill-delivered' - heldSends?: ReadonlySet - expected: boolean - /** Log only; failure supplies user-facing wording. */ - reason: string - failure?: SubmissionRejectionFact - observedAt?: number - startupUnanswered?: true -} - export type StructuredAgentSessionChildExitSession = Pick< StructuredAgentSessionHostSession, 'child' | 'lastEndedChild' @@ -117,18 +106,7 @@ export function settleStructuredAgentSessionChildExit< }) } -export function structuredAgentSessionEndedEventExit( - event: StructuredAgentSessionEndedEvent -): StructuredAgentSessionChildExit { - return { - expected: event.cause === 'requested-close', - ...(event.deathEvidenceKind ? { deathEvidenceKind: event.deathEvidenceKind } : {}), - reason: event.reason, - ...(event.failure ? { failure: event.failure } : {}), - ...(event.observedAt === undefined ? {} : { observedAt: event.observedAt }), - ...(event.startupUnanswered ? { startupUnanswered: event.startupUnanswered } : {}) - } -} +export { structuredAgentSessionEndedEventExit, type StructuredAgentSessionChildExit } export async function endExitedStructuredAgentSessionChildUnderSerialize< TSession extends StructuredAgentSessionChildExitSession @@ -189,6 +167,12 @@ export async function endExitedStructuredAgentSessionChildUnderSerialize< ...(exit.deathEvidenceKind ? { deathEvidenceKind: exit.deathEvidenceKind } : {}), ...(expected && close ? { endedAt: close.requestedAt } : {}) }) + logProviderChildExit(context.logger, sessionId, child, { + expected, + failure, + exit, + duringStartup: exitedDuringStartup + }) context.generationEnded(sessionId) const endChild = (): void => { context.startupAttempts?.childEnded(sessionId, child, { @@ -321,12 +305,3 @@ export async function endExitedStructuredAgentSessionChildUnderSerialize< wakeQueuedDelivery() } } - -function logExitFailure( - context: Pick, - sessionId: string, - scope: string, - error: unknown -): void { - context.logger.warn('settling a provider exit did not finish', { scope, sessionId, error }) -} diff --git a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-provider-exit-log.ts b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-provider-exit-log.ts new file mode 100644 index 00000000000..9a7955b01f6 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-provider-exit-log.ts @@ -0,0 +1,57 @@ +import type { SubmissionRejectionFact } from '../../../../shared/agent-session/failures/agent-session-failure' +import { redactString } from '../../../observability/redactor' +import { startSpan } from '../../../observability/tracer' +import type { StructuredAgentSessionProviderChild } from '../host/structured-agent-session-host-types' +import type { StructuredAgentSessionLogger } from '../host/structured-agent-session-logger' +import type { StructuredAgentSessionChildExit } from './structured-agent-session-child-exit-record' + +/** One record per ended agent process naming who ended it, so a bare exit code is attributable. */ +export function logProviderChildExit( + logger: StructuredAgentSessionLogger, + sessionId: string, + child: Pick, + end: { + expected: boolean + exit: Pick + failure: SubmissionRejectionFact | undefined + duringStartup: boolean + } +): void { + const { close } = child + const fields = { + scope: 'provider-exit', + sessionId, + generation: child.generation, + // A close Orca made after its own failure still reads as unexpected to the turn. + endedBy: end.expected || close || end.exit.closedByOrca ? 'orca' : 'agent', + // The reason can carry the agent's stderr tail; the console line is not redacted downstream. + reason: redactString(end.exit.reason), + duringStartup: end.duringStartup, + ...(close + ? { closeCause: close.cause, closeReason: close.reason && redactString(close.reason) } + : {}), + ...(close?.quit ? { quit: true } : {}), + ...(end.failure ? { failure: end.failure.kind } : {}) + } + // A host-fault close counts as unexpected upstream, so it warns too. + if (!end.expected) { + logger.warn('agent process ended', fields) + return + } + // A requested end is normal; record it without a failed span. + try { + startSpan('agentSession.provider-exit', { attributes: fields }).end() + console.info('[agent-session] provider-exit: agent process ended by Orca', fields) + } catch { + // Diagnostics never fail the exit they describe. + } +} + +export function logExitFailure( + context: { logger: StructuredAgentSessionLogger }, + sessionId: string, + scope: string, + error: unknown +): void { + context.logger.warn('settling a provider exit did not finish', { scope, sessionId, error }) +} From be059c443d944de458e2339c6eeae3dd9442d08b Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Sun, 8 Nov 2026 18:00:57 -0800 Subject: [PATCH 24/80] test: share a worker's module cache across unit files that can be reset between files (#40067) Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .github/CONTRIBUTING.md | 6 + .../vitest-shared-worker-file-reset.mjs | 201 ++++++++++++++++++ .../vitest-shared-worker-files.mjs | 94 ++++++++ config/vitest.config.ts | 44 +++- 4 files changed, 343 insertions(+), 2 deletions(-) create mode 100644 config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs create mode 100644 config/scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index d0b6347805d..4d52e46d4a1 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -123,6 +123,12 @@ graph. Local affected tests do not replace CI. Use `pnpm test`, not raw `vitest`: only it keeps the transform cache that makes repeat runs fast. +Most unit files share a worker and its module cache with the files before them. After each file +the worker undoes mocks, fake timers, stubs, env, cwd, and leftover timers, listeners, and globals. +Files using `vi.doMock`, `vi.resetModules`, a DOM environment, or direct `globalThis` writes keep +their own worker. If a file passes alone but fails after others, add `// @orca-isolated-worker` +to it; `ORCA_VITEST_ISOLATE_EVERY_FILE=1` isolates every file to confirm a leak. + To reduce main-process rebuild CPU and memory when you do not need its source maps, set `ORCA_DEV_MAIN_SOURCEMAPS=0` before `pnpm dev`. In Bash/zsh: diff --git a/config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs b/config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs new file mode 100644 index 00000000000..dff75d916d4 --- /dev/null +++ b/config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs @@ -0,0 +1,201 @@ +// Shared-worker files run back to back in one module cache; undo each file's mocks and process state. +// A file that leaves timers, listeners or globals behind also drops the module cache, as isolation would. +// Plain JS: it rewires Vitest's mocker and the timer globals, which have no public types. +import { promisify } from 'node:util' +import { afterAll, vi } from 'vitest' + +const VITEST_INTERNAL = /\/vitest\/dist\/|\/@vitest\/|vitest-virtual-/ +// Registries their owners create on first use and look up again on every access. +const LAZY_GLOBAL_REGISTRY = /^(?:Symbol\(orca\.|__zod_global)/ +const PROCESS_KEYS = ['platform', 'arch', 'execPath', 'argv', 'exitCode'] + +function resetNode(node) { + node.evaluated = false + node.promise = undefined + node.exports = undefined +} + +// Same reset Vitest's isolation applies, limited to modules that saw the mocked one. +function resetImportersOf(modules, id) { + const pending = [...(modules.getModuleById(id)?.importers ?? [])] + const seen = new Set() + while (pending.length > 0) { + const importer = pending.pop() + if (seen.has(importer) || VITEST_INTERNAL.test(importer)) { + continue + } + seen.add(importer) + const node = modules.getModuleById(importer) + if (node) { + resetNode(node) + pending.push(...node.importers) + } + } +} + +function resetAllModules(modules) { + for (const [id, node] of modules.idToModuleMap) { + if (!VITEST_INTERNAL.test(id)) { + resetNode(node) + node.importers.clear() + } + } +} + +function trackTimers(state) { + const real = { setTimeout, setInterval, clearTimeout, clearInterval } + function trackedSetTimeout(callback, ms, ...args) { + if (typeof callback !== 'function') { + return real.setTimeout(callback, ms, ...args) + } + const handle = real.setTimeout( + (...callbackArgs) => { + state.pendingTimers.delete(handle) + callback(...callbackArgs) + }, + ms, + ...args + ) + state.pendingTimers.add(handle) + return handle + } + trackedSetTimeout[promisify.custom] = real.setTimeout[promisify.custom] + globalThis.setTimeout = trackedSetTimeout + globalThis.setInterval = (...args) => { + const handle = real.setInterval(...args) + state.pendingTimers.add(handle) + return handle + } + globalThis.clearTimeout = (handle) => { + state.pendingTimers.delete(handle) + real.clearTimeout(handle) + } + globalThis.clearInterval = (handle) => { + state.pendingTimers.delete(handle) + real.clearInterval(handle) + } + state.clearTimer = (handle) => { + real.clearTimeout(handle) + real.clearInterval(handle) + } +} + +// The mocker lives across files, unlike this module, so it carries the once-per-worker state. +function installOnce(mocker) { + if (mocker.orcaSharedWorkerState) { + return mocker.orcaSharedWorkerState + } + const mockPath = mocker.mockPath.bind(mocker) + // Modules cached by an earlier file hold the real binding; re-run them against this file's mock. + mocker.mockPath = (originalId, id, ...rest) => { + mockPath(originalId, id, ...rest) + resetImportersOf(mocker.evaluatedModules, id) + } + const reset = mocker.reset.bind(mocker) + // And re-run them against the real module before the next file imports them. + mocker.reset = () => { + for (const registry of mocker.registries.values()) { + for (const url of registry.keys()) { + resetImportersOf(mocker.evaluatedModules, registry.get(url).id) + } + } + reset() + } + const state = { pendingTimers: new Set(), clearTimer: () => {} } + trackTimers(state) + mocker.orcaSharedWorkerState = state + return state +} + +const mocker = globalThis.__vitest_mocker__ +const state = mocker ? installOnce(mocker) : undefined +state?.pendingTimers.clear() + +const initialGlobals = Object.getOwnPropertyDescriptors(globalThis) +const initialProcess = Object.fromEntries( + PROCESS_KEYS.map((key) => [key, Object.getOwnPropertyDescriptor(process, key)]) +) +const initialListeners = new Map( + process.eventNames().map((event) => [event, new Set(process.listeners(event))]) +) +const initialEnv = { ...process.env } +const initialCwd = process.cwd() +const evaluatedBefore = new Set( + [...(mocker?.evaluatedModules.idToModuleMap ?? [])] + .filter(([, node]) => node.evaluated) + .map(([id]) => id) +) + +/** Puts process state back and reports what this file had left behind. */ +function restoreProcessState() { + const leaks = { globals: [], process: [] } + for (const key of Reflect.ownKeys(globalThis)) { + if (!(key in initialGlobals) && !LAZY_GLOBAL_REGISTRY.test(String(key))) { + leaks.globals.push(String(key)) + Reflect.deleteProperty(globalThis, key) + } + } + for (const [key, descriptor] of Object.entries(initialGlobals)) { + const current = Object.getOwnPropertyDescriptor(globalThis, key) + if (!Object.is(current?.value, descriptor.value) || current?.get !== descriptor.get) { + leaks.globals.push(key) + try { + Object.defineProperty(globalThis, key, descriptor) + } catch { + // Non-configurable globals cannot be put back; the module reset below still applies. + } + } + } + for (const key of PROCESS_KEYS) { + const descriptor = initialProcess[key] + if (descriptor) { + Object.defineProperty(process, key, descriptor) + } else { + Reflect.deleteProperty(process, key) + } + } + for (const event of process.eventNames()) { + const before = initialListeners.get(event) + for (const listener of process.listeners(event)) { + if (!before?.has(listener)) { + leaks.process.push(`${String(event)} listener`) + process.removeListener(event, listener) + } + } + } + for (const handle of state?.pendingTimers ?? []) { + leaks.process.push('timer') + state.clearTimer(handle) + } + state?.pendingTimers.clear() + for (const key of Object.keys(process.env)) { + if (!(key in initialEnv)) { + delete process.env[key] + } + } + Object.assign(process.env, initialEnv) + if (process.cwd() !== initialCwd) { + process.chdir(initialCwd) + } + return leaks +} + +afterAll(() => { + // Runs Vitest's end-of-file spy restore now: a spy restored by hand would otherwise be re-applied after this hook. + vi.restoreAllMocks() + vi.useRealTimers() + vi.unstubAllEnvs() + vi.unstubAllGlobals() + const leaks = restoreProcessState() + if (mocker && leaks.process.length > 0) { + // Any cached module may own a cleared timer or listener. + resetAllModules(mocker.evaluatedModules) + } else if (mocker && leaks.globals.length > 0) { + // A module this file loaded may have installed the global it now expects. + for (const [id, node] of mocker.evaluatedModules.idToModuleMap) { + if (node.evaluated && !evaluatedBefore.has(id) && !VITEST_INTERNAL.test(id)) { + resetNode(node) + } + } + } +}) diff --git a/config/scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs b/config/scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs new file mode 100644 index 00000000000..e436fafecca --- /dev/null +++ b/config/scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs @@ -0,0 +1,94 @@ +import { readFileSync, statSync } from 'node:fs' +import { dirname, join, relative, resolve } from 'node:path' + +// vitest-shared-worker-file-reset.mjs undoes mocks, timers, stubs, env and cwd after each file. +// These can't be undone between files, so a file that uses them keeps its own worker. +const ISOLATION_REQUIRING_API = new RegExp( + [ + // Opt-out for a file whose state leaks in ways the reset cannot see. + String.raw`@orca-isolated-worker\b`, + String.raw`\bvi\.(?:doMock|unmock|doUnmock|resetModules)\(`, + String.raw`@vitest-environment\s+(?!node\b)`, + String.raw`\bglobalThis(?:\.\w+|\[[^\]]+\])\s*=[^=]`, + String.raw`\bObject\.defineProperty\(\s*(?:globalThis|process|navigator|window|document)\b` + ].join('|') +) +const IMPORT_SPECIFIER = /(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)['"]([^'"]+)['"]/g +const SOURCE_EXTENSIONS = ['.ts', '.tsx', '.mts', '.mjs', '.js'] + +function isFile(path) { + try { + return statSync(path).isFile() + } catch { + return false + } +} + +function resolveLocal(fromFile, specifier, aliases) { + let base = null + if (specifier.startsWith('./') || specifier.startsWith('../')) { + base = resolve(dirname(fromFile), specifier) + } else { + const alias = aliases.find(({ find }) => specifier === find || specifier.startsWith(`${find}/`)) + if (!alias) { + return null + } + base = join(alias.replacement, specifier.slice(alias.find.length)) + } + const stem = base.replace(/\.[cm]?jsx?$/, '') + const candidates = [ + base, + ...SOURCE_EXTENSIONS.map((extension) => stem + extension), + ...SOURCE_EXTENSIONS.map((extension) => join(base, `index${extension}`)) + ] + return ( + candidates.find( + (candidate) => !/[\\/]node_modules[\\/]/.test(candidate) && isFile(candidate) + ) ?? null + ) +} + +/** + * Returns the test files (root-relative, forward slashes) that can share a worker's module cache. + * Only the test file and the test-support files it imports are scanned, not production modules. + */ +export function filesSafeToShareWorker(root, files, aliases = []) { + const sources = new Map() + const read = (file) => { + let source = sources.get(file) + if (source === undefined) { + source = readFileSync(file, 'utf8') + sources.set(file, source) + } + return source + } + const isTestSupport = (file) => /\bfrom\s+['"]vitest['"]/.test(read(file)) + const memo = new Map() + const visiting = new Set() + const requiresIsolation = (file) => { + const known = memo.get(file) + if (known !== undefined) { + return known + } + if (visiting.has(file)) { + return false + } + visiting.add(file) + const source = read(file) + let result = ISOLATION_REQUIRING_API.test(source) + for (const match of result ? [] : source.matchAll(IMPORT_SPECIFIER)) { + const target = resolveLocal(file, match[1], aliases) + if (target && isTestSupport(target) && requiresIsolation(target)) { + result = true + break + } + } + visiting.delete(file) + memo.set(file, result) + return result + } + return files + .map((file) => resolve(root, file)) + .filter((file) => isFile(file) && !requiresIsolation(file)) + .map((file) => relative(root, file).replaceAll('\\', '/')) +} diff --git a/config/vitest.config.ts b/config/vitest.config.ts index 6202566aa32..2c2d9316496 100644 --- a/config/vitest.config.ts +++ b/config/vitest.config.ts @@ -1,4 +1,4 @@ -import { readFileSync } from 'node:fs' +import { globSync, readFileSync } from 'node:fs' import { createRequire } from 'node:module' import { resolve } from 'node:path' import { normalizePath, transformWithOxc, type Plugin } from 'vite' @@ -9,6 +9,7 @@ import { UNIT_SETUP_FILES } from './scripts/ci/unit-selection/dependency-graph/ci-unit-files.mjs' import TimingSequencer from './scripts/ci/sharding/ci-unit-sequencer.mjs' +import { filesSafeToShareWorker } from './scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs' import RuntimeSequencer from './scripts/test-runtime/execution-policy/vitest-runtime-sequencer.mjs' import { getNodeRuntimeInclude } from './scripts/test-runtime/sqlite-admission/vitest-node-runtime-files.mjs' import { nodeRuntimePool } from './scripts/test-runtime/node-worker/vitest-node-runtime-pool' @@ -121,7 +122,46 @@ const bunProject = { sequence: { groupOrder: 1 } } } -const projects = [...(process.versions.bun ? [bunProject] : []), nodeProject] +// Files whose state the per-file reset can undo share one worker's module cache instead of +// re-importing thousands of modules each. ORCA_VITEST_ISOLATE_EVERY_FILE=1 turns it off. +const runtimeProject = process.versions.bun ? bunProject : nodeProject +const nodeRuntimeFiles = new Set(nodeRuntimeInclude.map((file) => file.replaceAll('\\', '/'))) +const sharedWorkerFiles = + process.env.ORCA_VITEST_ISOLATE_EVERY_FILE === '1' + ? [] + : filesSafeToShareWorker( + process.cwd(), + globSync(UNIT_INCLUDE) + .map((file) => file.replaceAll('\\', '/')) + .filter((file) => !nodeRuntimeFiles.has(file)), + [ + { find: '@renderer', replacement: resolve('src/renderer/src') }, + { find: '@', replacement: resolve('src/renderer/src') } + ] + ) +const sharedProjects = + sharedWorkerFiles.length > 0 + ? [ + { + ...runtimeProject, + test: { + ...runtimeProject.test, + name: `${runtimeProject.test.name}-shared`, + include: sharedWorkerFiles, + isolate: false, + setupFiles: [ + ...runtimeProject.test.setupFiles, + resolve('config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs') + ] + } + } + ] + : [] +runtimeProject.test.exclude = [ + ...(runtimeProject.test.exclude ?? testOptions.exclude), + ...sharedWorkerFiles +] +const projects = [...(process.versions.bun ? [bunProject] : []), nodeProject, ...sharedProjects] export default defineConfig({ ...transforms, From da76713d825b1dac78541fb1476409f7b2305c54 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:04:16 +0000 Subject: [PATCH 25/80] refactor(mobile): move the shell's WebView and its fences into a document object (#39946) The native shell view now hosts a MobileWebShellDocument that owns the WebView, its scheme handler or request intercept, the network block, the bridge and the navigation policy. The view keeps layout, the back-edge swipe and its React events. Nothing changes for a page; this is the seam a prewarmed spare page needs. Based on Jinwoo's page split in #32202. Co-authored-by: Jinwoo-H <73622457+Jinwoo-H@users.noreply.github.com> --- .../MobileWebShellDocument.kt | 405 ++++++++++++ .../orcamobilewebshell/MobileWebShellView.kt | 384 +----------- .../ios/MobileWebShellDocument.swift | 586 ++++++++++++++++++ .../ios/MobileWebShellView.swift | 568 +---------------- 4 files changed, 1022 insertions(+), 921 deletions(-) create mode 100644 mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt create mode 100644 mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt new file mode 100644 index 00000000000..8292fb9ca2c --- /dev/null +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt @@ -0,0 +1,405 @@ +package expo.modules.orcamobilewebshell + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Color +import android.net.Uri +import android.os.Message +import android.view.View +import android.view.ViewGroup +import android.webkit.RenderProcessGoneDetail +import android.webkit.WebChromeClient +import android.webkit.WebResourceError +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebSettings +import android.webkit.WebView +import android.webkit.WebViewClient +import androidx.core.graphics.Insets +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.webkit.JavaScriptReplyProxy +import androidx.webkit.ScriptHandler +import androidx.webkit.WebMessageCompat +import androidx.webkit.WebViewCompat +import androidx.webkit.WebViewFeature +import expo.modules.kotlin.exception.CodedException +import java.io.ByteArrayInputStream + +/** Publish host and manifest map atomically/volatily: main-thread writes race Chromium’s interceptor. */ +private class MobileWebShellServed( + val generation: MobileWebShellGeneration, + val originHost: String +) + +/** Where a document's reports go: the view showing it. */ +internal interface MobileWebShellDocumentSink { + fun documentLoadState(payload: Map) + fun documentBridgeMessage(json: String) + fun documentExternalNavigation(url: String) +} + +/** One WebView and every fence on it, apart from React. The view hosts it and lays it out. */ +@SuppressLint("SetJavaScriptEnabled") +internal class MobileWebShellDocument(private val context: Context) { + var sink: MobileWebShellDocumentSink? = null + private var bridgeInstalled = false + private val bridgeGate = MobileWebShellBridgeGate() + // The page sends ready before native replies; volatile proxy state crosses interceptor/UI threads. + @Volatile private var replyProxy: JavaScriptReplyProxy? = null + private var applied: MobileWebShellAppliedProps? = null + private val loadState = MobileWebShellLoadStateMachine() + // Failure state crosses main-thread callbacks and Chromium’s background interceptor. + @Volatile private var documentFailed = false + @Volatile private var served: MobileWebShellServed? = null + private var blocker: ScriptHandler? = null + // Read by the interceptor thread when it reports a failed main-frame load. + @Volatile var webView: WebView? = createWebView() + private set + + /** Apply unordered props together; identical triples do not retry. Recovery requires a remount. */ + fun apply(generationDirectory: String, sessionId: String, bridgeEnabled: Boolean) { + val next = MobileWebShellAppliedProps(generationDirectory, sessionId, bridgeEnabled) + if (applied?.matches(next) == true) return + applied = next + documentFailed = false + loadState.reset() + val view = webView + if (view == null) { + // A dead renderer requires a remount; changed props must still report failure. + emit(loadState.rendererStillGone()) + return + } + view.stopLoading() + emit(loadState.started()) + + val origin = mobileWebShellOrigin(sessionId) + val host = mobileWebShellOriginHost(sessionId) + if (origin == null || host == null) { + // The private origin is the isolation primitive; a malformed session id leaves us without one. + failPropUpdate(MobileWebShellFailureReason.ISOLATION_UNAVAILABLE) + return + } + val loaded = MobileWebShellGeneration.load(generationDirectory) + if (loaded == null) { + failPropUpdate(MobileWebShellFailureReason.GENERATION_UNREADABLE) + return + } + blocker?.remove() + blocker = installMobileWebShellNetworkApiBlocker(view, origin) + if (blocker == null) { + failPropUpdate(MobileWebShellFailureReason.ISOLATION_UNAVAILABLE) + return + } + if (!applyBridgeListener(view, origin, bridgeEnabled)) { + failPropUpdate(MobileWebShellFailureReason.ISOLATION_UNAVAILABLE) + return + } + served = MobileWebShellServed(loaded, host) + view.visibility = View.VISIBLE + view.loadUrl("$origin/") + } + + /** Retire the old visible generation even when the replacement fails. */ + private fun failPropUpdate(reason: MobileWebShellFailureReason) { + // Remove the old listener: stopLoading leaves its same-origin document alive and able to rearm replies. + removeBridgeListener() + served = null + webView?.visibility = View.INVISIBLE + emit(loadState.failed(reason)) + } + + /** addWebMessageListener injects before scripts and enforces origins; false means unsupported provider. */ + private fun applyBridgeListener(view: WebView, origin: String, bridgeEnabled: Boolean): Boolean { + removeBridgeListener() + val outcome = mobileWebShellBridgeInstall( + bridgeEnabled, + WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) + ) + if (outcome != MobileWebShellBridgeInstall.INSTALL) { + return outcome == MobileWebShellBridgeInstall.SKIP + } + return runCatching { + WebViewCompat.addWebMessageListener( + view, + MOBILE_WEB_SHELL_BRIDGE_OBJECT, + setOf(origin), + bridgeListener + ) + bridgeInstalled = true + }.isSuccess + } + + /** The one way the bridge goes away, so no disable path can leave a listener behind. */ + private fun removeBridgeListener() { + val view = webView + if (bridgeInstalled && view != null) { + WebViewCompat.removeWebMessageListener(view, MOBILE_WEB_SHELL_BRIDGE_OBJECT) + } + bridgeInstalled = false + replyProxy = null + } + + /** Chromium calls this on the UI thread, which is also the only thread that may reply. */ + private val bridgeListener = WebViewCompat.WebMessageListener { + _, message, _, isMainFrame, proxy -> + val isStringMessage = message.type == WebMessageCompat.TYPE_STRING + val json = if (isStringMessage) message.data else null + if ( + acceptsMobileWebShellBridgeFrame( + isMainFrame, + isStringMessage, + loadState.hasCommittedDocument + ) && json != null && + bridgeGate.accepts(json.toByteArray(Charsets.UTF_8).size) + ) { + replyProxy = proxy + sink?.documentBridgeMessage(json) + } + } + + /** Throw so a host chunking bug rejects rather than leaving its request unsettled. */ + fun postBridgeMessage(json: String) { + val proxy = replyProxy ?: throw MobileWebShellBridgeUnavailableException() + val byteCount = json.toByteArray(Charsets.UTF_8).size + if (!acceptsMobileWebShellBridgeByteCount(byteCount)) { + throw MobileWebShellBridgeMessageTooLargeException(byteCount) + } + proxy.postMessage(json) + } + + /** The view's attach and detach: off-window, the page reads hidden, as a buried WKWebView does on iOS. */ + fun resume() { + webView?.onResume() + } + + fun pause() { + webView?.onPause() + } + + /** The view is done with it, or onRenderProcessGone. */ + fun destroyWebView() { + val view = webView ?: return + removeBridgeListener() + loadState.documentEnded() + webView = null + blocker?.remove() + blocker = null + served = null + documentFailed = false + view.stopLoading() + (view.parent as? ViewGroup)?.removeView(view) + view.destroy() + } + + // Keep explicit settings for the Chromium 83 floor, even where newer WebViews ignore deprecated options. + // B4 requires device proofs: no network, persistent state, file/content access, cleartext or navigation escape. + @Suppress("DEPRECATION") + private fun createWebView(): WebView { + val view = MobileWebShellSelectionView(context) + view.setBackgroundColor(Color.TRANSPARENT) + view.settings.apply { + javaScriptEnabled = true + domStorageEnabled = false + databaseEnabled = false + allowFileAccess = false + allowFileAccessFromFileURLs = false + allowUniversalAccessFromFileURLs = false + allowContentAccess = false + javaScriptCanOpenWindowsAutomatically = false + setSupportMultipleWindows(false) + mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW + cacheMode = WebSettings.LOAD_NO_CACHE + blockNetworkLoads = true + mediaPlaybackRequiresUserGesture = true + setGeolocationEnabled(false) + } + // clearCache(true) is process-global, including terminals; use per-view LOAD_NO_CACHE and no-store. + view.webViewClient = ShellWebViewClient() + view.webChromeClient = object : WebChromeClient() { + override fun onCreateWindow( + view: WebView?, + isDialog: Boolean, + isUserGesture: Boolean, + resultMsg: Message? + ): Boolean = false + } + view.setDownloadListener { _, _, _, _, _ -> } + // Replace WebView inset handling on P–R/S+; forward zeroed insets for S+ parity. + ViewCompat.setOnApplyWindowInsetsListener(view) { target, insets -> + ViewCompat.onApplyWindowInsets(target, insetsForShellPage(insets)) + } + return view + } + + private fun emit(emission: MobileWebShellLoadEmission?) { + if (emission != null) sink?.documentLoadState(emission.toPayload()) + } + + /** Post hiding past Chromium’s error-document commit; interception may also run off the main thread. */ + private fun reportDocumentFailure() { + replyProxy = null + // Drop replies synchronously before Chromium’s error document commits and the posted hide runs. + loadState.documentEnded() + // Set before posting: intervening onPageFinished must not restore ready or expose the error page. + documentFailed = true + val epoch = loadState.epoch + // The WebView's queue, like the view it sits in: a detached document reports once it attaches. + webView?.post { + if (!documentFailed) return@post + val emission = loadState.failedDuring( + epoch, + MobileWebShellFailureReason.DOCUMENT_LOAD_FAILED + ) ?: return@post + webView?.visibility = View.INVISIBLE + emit(emission) + } + } + + private fun isDocumentUrl(url: Uri): Boolean { + val host = served?.originHost ?: return false + return resolveMobileWebShellRequestPath(requestParts(url), host) == "/" + } + + private fun requestParts( + url: Uri, + method: String = "GET", + hasRangeHeader: Boolean = false + ): MobileWebShellRequestParts = MobileWebShellRequestParts( + method = method, + hasRangeHeader = hasRangeHeader, + scheme = url.scheme, + host = url.host, + port = url.port, + userInfo = url.userInfo, + query = url.query, + fragment = url.fragment, + encodedPath = url.encodedPath, + urlLength = url.toString().length + ) + + private fun serveRequest(request: WebResourceRequest): WebResourceResponse? { + val current = served ?: return null + val parts = requestParts( + request.url, + method = request.method, + hasRangeHeader = request.requestHeaders.keys.any { it.equals("Range", ignoreCase = true) } + ) + val path = resolveMobileWebShellRequestPath(parts, current.originHost) ?: return null + val asset = current.generation.entries[path] ?: return null + val bytes = runCatching { asset.file.readBytes() }.getOrNull() ?: return null + val headers = mobileWebShellResponseHeaders(path, bytes.size) + val (mimeType, charset) = splitMobileWebShellContentType(asset.contentType) + return WebResourceResponse(mimeType, charset, 200, "OK", headers, ByteArrayInputStream(bytes)) + } + + private fun refusedResponse(): WebResourceResponse = WebResourceResponse( + MOBILE_WEB_SHELL_REFUSAL_MIME_TYPE, + MOBILE_WEB_SHELL_REFUSAL_CHARSET, + MOBILE_WEB_SHELL_REFUSAL_STATUS, + MOBILE_WEB_SHELL_REFUSAL_REASON, + MOBILE_WEB_SHELL_REFUSAL_HEADERS, + ByteArrayInputStream(mobileWebShellRefusalBody()) + ) + + private inner class ShellWebViewClient : WebViewClient() { + /** Never null, so no request can fall through to the network. */ + override fun shouldInterceptRequest( + view: WebView, + request: WebResourceRequest + ): WebResourceResponse { + val response = serveRequest(request) + if (response != null) return response + if (request.isForMainFrame) reportDocumentFailure() + return refusedResponse() + } + + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { + // hasGesture gates only opener offers: human requests may omit it and subframes can target top without it. + val verdict = mobileWebShellNavigationVerdict( + url = request.url?.toString(), + isForMainFrame = request.isForMainFrame, + // Not reported here, unlike WKNavigationAction.sourceFrame on iOS. + isFromSubframe = false, + isDocumentUrl = !mobileWebShellDropsNavigation( + requestParts(request.url), + served?.originHost, + request.isForMainFrame + ), + // loadUrl bypasses this callback by WebViewClient contract; no callback action is our own load. + isShellLoad = false, + hasGesture = request.hasGesture(), + isDownload = false + ) + if (verdict is MobileWebShellNavigationVerdict.CancelAndOffer) { + sink?.documentExternalNavigation(verdict.url) + } + return verdict !is MobileWebShellNavigationVerdict.Allow + } + + override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { + // Discard the replaced document’s reply proxy; the next page must send ready. + replyProxy = null + loadState.documentEnded() + if (documentFailed || !isDocumentUrl(Uri.parse(url))) return + // Arm at commit before page script runs. + loadState.committed() + emit(loadState.started()) + } + + // history.replaceState changes the document path before finish; commit state identifies the load. + override fun onPageFinished(view: WebView, url: String) { + if (documentFailed || !loadState.hasCommittedDocument) return + view.visibility = View.VISIBLE + // Remove history so the page cannot return to a replaced document. + view.clearHistory() + emit(loadState.finished()) + } + + override fun onReceivedError( + view: WebView, + request: WebResourceRequest, + error: WebResourceError + ) { + if (request.isForMainFrame) reportDocumentFailure() + } + + override fun onReceivedHttpError( + view: WebView, + request: WebResourceRequest, + errorResponse: WebResourceResponse + ) { + if (request.isForMainFrame) reportDocumentFailure() + } + + /** Return true to avoid killing the app; destroy the dead WebView and leave retry policy to the caller. */ + override fun onRenderProcessGone(view: WebView, detail: RenderProcessGoneDetail): Boolean { + // Android ends every WebView's renderer together; off-screen or low-memory losses are the OS's doing, not the page's. + val host = view.parent as? View ?: view + val background = !host.isShown || host.windowVisibility != View.VISIBLE + destroyWebView() + emit(loadState.rendererGone(background, detail.didCrash())) + return true + } + } +} + +internal class MobileWebShellBridgeUnavailableException : + CodedException("The mobile web shell bridge is not installed on this view") + +internal class MobileWebShellBridgeMessageTooLargeException(byteCount: Int) : CodedException( + "A bridge message of $byteCount bytes exceeds the " + + "$MOBILE_WEB_SHELL_BRIDGE_MAX_MESSAGE_BYTES byte cap" +) + +/** The shell already pads bars/keyboard; zero (never consume) insets to prevent WebView M144+ bar and M139+ IME double-padding. */ +private fun insetsForShellPage(insets: WindowInsetsCompat): WindowInsetsCompat = + WindowInsetsCompat.Builder(insets) + .setInsets(SHELL_OWNED_INSET_TYPES, Insets.NONE) + .build() + +private val SHELL_OWNED_INSET_TYPES = + WindowInsetsCompat.Type.systemBars() or + WindowInsetsCompat.Type.displayCutout() or + WindowInsetsCompat.Type.ime() diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt index 32f824dff4c..6e86c878f4e 100644 --- a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt @@ -2,44 +2,16 @@ package expo.modules.orcamobilewebshell import android.annotation.SuppressLint import android.content.Context -import android.graphics.Bitmap -import android.graphics.Color -import android.net.Uri -import android.os.Message -import android.view.View -import android.webkit.RenderProcessGoneDetail -import android.webkit.WebChromeClient -import android.webkit.WebResourceError -import android.webkit.WebResourceRequest -import android.webkit.WebResourceResponse -import android.webkit.WebSettings -import android.webkit.WebView -import android.webkit.WebViewClient -import androidx.core.graphics.Insets -import androidx.core.view.ViewCompat -import androidx.core.view.WindowInsetsCompat -import androidx.webkit.JavaScriptReplyProxy -import androidx.webkit.ScriptHandler -import androidx.webkit.WebMessageCompat -import androidx.webkit.WebViewCompat -import androidx.webkit.WebViewFeature import expo.modules.kotlin.AppContext -import expo.modules.kotlin.exception.CodedException import expo.modules.kotlin.viewevent.EventDispatcher import expo.modules.kotlin.views.ExpoView -import java.io.ByteArrayInputStream -/** Publish host and manifest map atomically/volatily: main-thread writes race Chromium’s interceptor. */ -private class MobileWebShellServed( - val generation: MobileWebShellGeneration, - val originHost: String -) - -@SuppressLint("ViewConstructor", "SetJavaScriptEnabled") +/** React Native's handle on one document: it lays the WebView out and forwards what it reports. */ +@SuppressLint("ViewConstructor") internal class OrcaMobileWebShellView( context: Context, appContext: AppContext -) : ExpoView(context, appContext) { +) : ExpoView(context, appContext), MobileWebShellDocumentSink { private val onLoadState by EventDispatcher>() private val onBridgeMessage by EventDispatcher>() private val onExternalNavigation by EventDispatcher>() @@ -47,22 +19,13 @@ internal class OrcaMobileWebShellView( private var generationDirectory = "" private var sessionId = "" private var bridgeEnabled = false - private var bridgeInstalled = false - private val bridgeGate = MobileWebShellBridgeGate() - // The page sends ready before native replies; volatile proxy state crosses interceptor/UI threads. - @Volatile private var replyProxy: JavaScriptReplyProxy? = null - private var applied: MobileWebShellAppliedProps? = null - private val loadState = MobileWebShellLoadStateMachine() - // Failure state crosses main-thread callbacks and Chromium’s background interceptor. - @Volatile private var documentFailed = false - @Volatile private var served: MobileWebShellServed? = null - private var blocker: ScriptHandler? = null - private var webView: WebView? = createWebView() + private val document = MobileWebShellDocument(context) init { - addView(webView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)) + document.sink = this + addView(document.webView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)) // Why: Chromium reports an unpaused, never-attached WebView as visible, and a screen pushed under another never attaches. - webView?.onPause() + document.pause() } fun setGenerationDirectory(value: String) { @@ -77,348 +40,33 @@ internal class OrcaMobileWebShellView( bridgeEnabled = value } - /** Apply unordered props together; identical triples do not retry. Recovery requires a remount. */ fun propsDidUpdate() { - val next = MobileWebShellAppliedProps(generationDirectory, sessionId, bridgeEnabled) - if (applied?.matches(next) == true) return - applied = next - documentFailed = false - loadState.reset() - val view = webView - if (view == null) { - // A dead renderer requires a remount; changed props must still report failure. - emit(loadState.rendererStillGone()) - return - } - view.stopLoading() - emit(loadState.started()) - - val origin = mobileWebShellOrigin(sessionId) - val host = mobileWebShellOriginHost(sessionId) - if (origin == null || host == null) { - // The private origin is the isolation primitive; a malformed session id leaves us without one. - failPropUpdate(MobileWebShellFailureReason.ISOLATION_UNAVAILABLE) - return - } - val loaded = MobileWebShellGeneration.load(generationDirectory) - if (loaded == null) { - failPropUpdate(MobileWebShellFailureReason.GENERATION_UNREADABLE) - return - } - blocker?.remove() - blocker = installMobileWebShellNetworkApiBlocker(view, origin) - if (blocker == null) { - failPropUpdate(MobileWebShellFailureReason.ISOLATION_UNAVAILABLE) - return - } - if (!applyBridgeListener(view, origin)) { - failPropUpdate(MobileWebShellFailureReason.ISOLATION_UNAVAILABLE) - return - } - served = MobileWebShellServed(loaded, host) - view.visibility = View.VISIBLE - view.loadUrl("$origin/") + document.apply(generationDirectory, sessionId, bridgeEnabled) } - /** Retire the old visible generation even when the replacement fails. */ - private fun failPropUpdate(reason: MobileWebShellFailureReason) { - // Remove the old listener: stopLoading leaves its same-origin document alive and able to rearm replies. - removeBridgeListener() - served = null - webView?.visibility = View.INVISIBLE - emit(loadState.failed(reason)) - } - - /** addWebMessageListener injects before scripts and enforces origins; false means unsupported provider. */ - private fun applyBridgeListener(view: WebView, origin: String): Boolean { - removeBridgeListener() - val outcome = mobileWebShellBridgeInstall( - bridgeEnabled, - WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) - ) - if (outcome != MobileWebShellBridgeInstall.INSTALL) { - return outcome == MobileWebShellBridgeInstall.SKIP - } - return runCatching { - WebViewCompat.addWebMessageListener( - view, - MOBILE_WEB_SHELL_BRIDGE_OBJECT, - setOf(origin), - bridgeListener - ) - bridgeInstalled = true - }.isSuccess - } - - /** The one way the bridge goes away, so no disable path can leave a listener behind. */ - private fun removeBridgeListener() { - val view = webView - if (bridgeInstalled && view != null) { - WebViewCompat.removeWebMessageListener(view, MOBILE_WEB_SHELL_BRIDGE_OBJECT) - } - bridgeInstalled = false - replyProxy = null - } - - /** Chromium calls this on the UI thread, which is also the only thread that may reply. */ - private val bridgeListener = WebViewCompat.WebMessageListener { - _, message, _, isMainFrame, proxy -> - val isStringMessage = message.type == WebMessageCompat.TYPE_STRING - val json = if (isStringMessage) message.data else null - if ( - acceptsMobileWebShellBridgeFrame( - isMainFrame, - isStringMessage, - loadState.hasCommittedDocument - ) && json != null && - bridgeGate.accepts(json.toByteArray(Charsets.UTF_8).size) - ) { - replyProxy = proxy - onBridgeMessage(mapOf("json" to json)) - } - } - - /** Throw so a host chunking bug rejects rather than leaving its request unsettled. */ fun postBridgeMessage(json: String) { - val proxy = replyProxy ?: throw MobileWebShellBridgeUnavailableException() - val byteCount = json.toByteArray(Charsets.UTF_8).size - if (!acceptsMobileWebShellBridgeByteCount(byteCount)) { - throw MobileWebShellBridgeMessageTooLargeException(byteCount) - } - proxy.postMessage(json) + document.postBridgeMessage(json) } override fun onAttachedToWindow() { super.onAttachedToWindow() - webView?.onResume() + document.resume() } // Stay paused whenever off-window so the page reads hidden, as a buried WKWebView does on iOS. override fun onDetachedFromWindow() { - webView?.onPause() + document.pause() super.onDetachedFromWindow() } - /** Expo calls this once React Native is done with the view, and onRenderProcessGone calls it. */ + /** Expo calls this once React Native is done with the view. */ fun destroyWebView() { - val view = webView ?: return - removeBridgeListener() - loadState.documentEnded() - webView = null - blocker?.remove() - blocker = null - served = null - documentFailed = false - view.stopLoading() - removeView(view) - view.destroy() + document.destroyWebView() } - // Keep explicit settings for the Chromium 83 floor, even where newer WebViews ignore deprecated options. - // B4 requires device proofs: no network, persistent state, file/content access, cleartext or navigation escape. - @Suppress("DEPRECATION") - private fun createWebView(): WebView { - val view = MobileWebShellSelectionView(context) - view.setBackgroundColor(Color.TRANSPARENT) - view.settings.apply { - javaScriptEnabled = true - domStorageEnabled = false - databaseEnabled = false - allowFileAccess = false - allowFileAccessFromFileURLs = false - allowUniversalAccessFromFileURLs = false - allowContentAccess = false - javaScriptCanOpenWindowsAutomatically = false - setSupportMultipleWindows(false) - mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW - cacheMode = WebSettings.LOAD_NO_CACHE - blockNetworkLoads = true - mediaPlaybackRequiresUserGesture = true - setGeolocationEnabled(false) - } - // clearCache(true) is process-global, including terminals; use per-view LOAD_NO_CACHE and no-store. - view.webViewClient = ShellWebViewClient() - view.webChromeClient = object : WebChromeClient() { - override fun onCreateWindow( - view: WebView?, - isDialog: Boolean, - isUserGesture: Boolean, - resultMsg: Message? - ): Boolean = false - } - view.setDownloadListener { _, _, _, _, _ -> } - // Replace WebView inset handling on P–R/S+; forward zeroed insets for S+ parity. - ViewCompat.setOnApplyWindowInsetsListener(view) { target, insets -> - ViewCompat.onApplyWindowInsets(target, insetsForShellPage(insets)) - } - return view - } + override fun documentLoadState(payload: Map) = onLoadState(payload) - private fun emit(emission: MobileWebShellLoadEmission?) { - if (emission != null) onLoadState(emission.toPayload()) - } + override fun documentBridgeMessage(json: String) = onBridgeMessage(mapOf("json" to json)) - /** Post hiding past Chromium’s error-document commit; interception may also run off the main thread. */ - private fun reportDocumentFailure() { - replyProxy = null - // Drop replies synchronously before Chromium’s error document commits and the posted hide runs. - loadState.documentEnded() - // Set before posting: intervening onPageFinished must not restore ready or expose the error page. - documentFailed = true - val epoch = loadState.epoch - post { - if (!documentFailed) return@post - val emission = loadState.failedDuring( - epoch, - MobileWebShellFailureReason.DOCUMENT_LOAD_FAILED - ) ?: return@post - webView?.visibility = View.INVISIBLE - emit(emission) - } - } - - private fun isDocumentUrl(url: Uri): Boolean { - val host = served?.originHost ?: return false - return resolveMobileWebShellRequestPath(requestParts(url), host) == "/" - } - - private fun requestParts( - url: Uri, - method: String = "GET", - hasRangeHeader: Boolean = false - ): MobileWebShellRequestParts = MobileWebShellRequestParts( - method = method, - hasRangeHeader = hasRangeHeader, - scheme = url.scheme, - host = url.host, - port = url.port, - userInfo = url.userInfo, - query = url.query, - fragment = url.fragment, - encodedPath = url.encodedPath, - urlLength = url.toString().length - ) - - private fun serveRequest(request: WebResourceRequest): WebResourceResponse? { - val current = served ?: return null - val parts = requestParts( - request.url, - method = request.method, - hasRangeHeader = request.requestHeaders.keys.any { it.equals("Range", ignoreCase = true) } - ) - val path = resolveMobileWebShellRequestPath(parts, current.originHost) ?: return null - val asset = current.generation.entries[path] ?: return null - val bytes = runCatching { asset.file.readBytes() }.getOrNull() ?: return null - val headers = mobileWebShellResponseHeaders(path, bytes.size) - val (mimeType, charset) = splitMobileWebShellContentType(asset.contentType) - return WebResourceResponse(mimeType, charset, 200, "OK", headers, ByteArrayInputStream(bytes)) - } - - private fun refusedResponse(): WebResourceResponse = WebResourceResponse( - MOBILE_WEB_SHELL_REFUSAL_MIME_TYPE, - MOBILE_WEB_SHELL_REFUSAL_CHARSET, - MOBILE_WEB_SHELL_REFUSAL_STATUS, - MOBILE_WEB_SHELL_REFUSAL_REASON, - MOBILE_WEB_SHELL_REFUSAL_HEADERS, - ByteArrayInputStream(mobileWebShellRefusalBody()) - ) - - private inner class ShellWebViewClient : WebViewClient() { - /** Never null, so no request can fall through to the network. */ - override fun shouldInterceptRequest( - view: WebView, - request: WebResourceRequest - ): WebResourceResponse { - val response = serveRequest(request) - if (response != null) return response - if (request.isForMainFrame) reportDocumentFailure() - return refusedResponse() - } - - override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { - // hasGesture gates only opener offers: human requests may omit it and subframes can target top without it. - val verdict = mobileWebShellNavigationVerdict( - url = request.url?.toString(), - isForMainFrame = request.isForMainFrame, - // Not reported here, unlike WKNavigationAction.sourceFrame on iOS. - isFromSubframe = false, - isDocumentUrl = !mobileWebShellDropsNavigation( - requestParts(request.url), - served?.originHost, - request.isForMainFrame - ), - // loadUrl bypasses this callback by WebViewClient contract; no callback action is our own load. - isShellLoad = false, - hasGesture = request.hasGesture(), - isDownload = false - ) - if (verdict is MobileWebShellNavigationVerdict.CancelAndOffer) { - onExternalNavigation(mapOf("url" to verdict.url)) - } - return verdict !is MobileWebShellNavigationVerdict.Allow - } - - override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { - // Discard the replaced document’s reply proxy; the next page must send ready. - replyProxy = null - loadState.documentEnded() - if (documentFailed || !isDocumentUrl(Uri.parse(url))) return - // Arm at commit before page script runs. - loadState.committed() - emit(loadState.started()) - } - - // history.replaceState changes the document path before finish; commit state identifies the load. - override fun onPageFinished(view: WebView, url: String) { - if (documentFailed || !loadState.hasCommittedDocument) return - view.visibility = View.VISIBLE - // Remove history so the page cannot return to a replaced document. - view.clearHistory() - emit(loadState.finished()) - } - - override fun onReceivedError( - view: WebView, - request: WebResourceRequest, - error: WebResourceError - ) { - if (request.isForMainFrame) reportDocumentFailure() - } - - override fun onReceivedHttpError( - view: WebView, - request: WebResourceRequest, - errorResponse: WebResourceResponse - ) { - if (request.isForMainFrame) reportDocumentFailure() - } - - /** Return true to avoid killing the app; destroy the dead WebView and leave retry policy to the caller. */ - override fun onRenderProcessGone(view: WebView, detail: RenderProcessGoneDetail): Boolean { - // Android ends every WebView's renderer together; off-screen or low-memory losses are the OS's doing, not the page's. - val background = !isShown || windowVisibility != View.VISIBLE - destroyWebView() - emit(loadState.rendererGone(background, detail.didCrash())) - return true - } - } + override fun documentExternalNavigation(url: String) = onExternalNavigation(mapOf("url" to url)) } - -internal class MobileWebShellBridgeUnavailableException : - CodedException("The mobile web shell bridge is not installed on this view") - -internal class MobileWebShellBridgeMessageTooLargeException(byteCount: Int) : CodedException( - "A bridge message of $byteCount bytes exceeds the " + - "$MOBILE_WEB_SHELL_BRIDGE_MAX_MESSAGE_BYTES byte cap" -) - -/** The shell already pads bars/keyboard; zero (never consume) insets to prevent WebView M144+ bar and M139+ IME double-padding. */ -private fun insetsForShellPage(insets: WindowInsetsCompat): WindowInsetsCompat = - WindowInsetsCompat.Builder(insets) - .setInsets(SHELL_OWNED_INSET_TYPES, Insets.NONE) - .build() - -private val SHELL_OWNED_INSET_TYPES = - WindowInsetsCompat.Type.systemBars() or - WindowInsetsCompat.Type.displayCutout() or - WindowInsetsCompat.Type.ime() diff --git a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift new file mode 100644 index 00000000000..b23b2efaf75 --- /dev/null +++ b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift @@ -0,0 +1,586 @@ +import ExpoModulesCore +import WebKit + +private let networkBlockIdentifier = "dev.orca.mobile-web-shell.network-block-v1" + +/// Block http(s)/ws(s) beneath CSP; compilation failure is terminal and must load nothing. +private let networkBlockRules = """ + [ + { "trigger": { "url-filter": "^https?://" }, "action": { "type": "block" } }, + { "trigger": { "url-filter": "^wss?://" }, "action": { "type": "block" } } + ] + """ + +/// Compiled once per launch and shared by every view, so later mounts load without waiting; main thread only. +private enum NetworkBlockRuleList { + private static var compiled: WKContentRuleList? + + /// Synchronous once compiled in this process; nil means isolation is unavailable. + static func load(_ completion: @escaping (WKContentRuleList?) -> Void) { + if let compiled { + completion(compiled) + return + } + // Optional chaining would skip completion when the store is absent, leaving loading forever; fail closed instead. + guard let store = WKContentRuleListStore.default() else { + completion(nil) + return + } + store.compileContentRuleList( + forIdentifier: networkBlockIdentifier, + encodedContentRuleList: networkBlockRules + ) { ruleList, _ in + DispatchQueue.main.async { + if let ruleList { + compiled = ruleList + } + completion(ruleList) + } + } + } +} + +/// CSP fences fetch/XHR; document-start script blocks unobservable WebSocket and service-worker APIs. +/// Match Android; both non-configurable and non-writable prevent page replacement. +private let networkApiBlocker = """ + (function(){ + var deny=function(){throw new TypeError('Network access is disabled')}; + try{Object.defineProperty(globalThis,'WebSocket',{value:deny,configurable:false,writable:false})}catch(_){} + try{Object.defineProperty(Navigator.prototype,'serviceWorker',{get:function(){return undefined},configurable:false})}catch(_){} + try{Object.defineProperty(navigator,'serviceWorker',{value:undefined,configurable:false,writable:false})}catch(_){} + })(); + """ + +/// Inject the same orcaBridge surface as Android before scripts, alongside the network blocker. +/// Capture the sink so deleting window.webkit cannot break it; freeze properties against replacement. +private let bridgeInstaller = """ + (function(){ + var sink=window.webkit.messageHandlers.orcaBridge; + var handler=null; + var bridge={}; + Object.defineProperty(bridge,'postMessage',{value:function(json){ + if(typeof json!=='string'){throw new TypeError('orcaBridge.postMessage expects a string')} + sink.postMessage(json)},configurable:false,writable:false,enumerable:true}); + Object.defineProperty(bridge,'onmessage',{get:function(){return handler}, + set:function(value){handler=typeof value==='function'?value:null},configurable:false,enumerable:true}); + Object.defineProperty(bridge,'__deliver',{value:function(json){if(handler){handler({data:json})}}, + configurable:false,writable:false,enumerable:false}); + Object.defineProperty(globalThis,'orcaBridge',{value:bridge,configurable:false,writable:false,enumerable:true}); + })(); + """ + +/// Bind payload as JS value m, never script text. A missing installer must throw to reject undelivered posts. +private let bridgeDeliver = """ + globalThis.orcaBridge.__deliver(m) + """ + +private final class MobileWebShellSchemeHandler: NSObject, WKURLSchemeHandler { + /// Read up-to-10-MiB assets off WebKit’s main-thread scheme callbacks. + private let readQueue = DispatchQueue(label: "dev.orca.mobile-web-shell.read") + /// Main-thread live-task set prevents stopped-task delivery’s uncatchable Objective-C exception. + private var liveTasks: Set = [] + + var sessionId: String? + var generation: MobileWebShellGeneration? + + func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) { + let key = ObjectIdentifier(urlSchemeTask) + liveTasks.insert(key) + guard + let sessionId, + let generation, + let url = urlSchemeTask.request.url, + let parts = MobileWebShellRequestParts(request: urlSchemeTask.request), + let path = MobileWebShellOrigin.resolveRequestPath(parts, sessionId: sessionId), + let asset = generation.entries[path] + else { + fail(urlSchemeTask, key) + return + } + readQueue.async { [weak self] in + let data = try? Data(contentsOf: asset.file) + DispatchQueue.main.async { + guard let self, self.liveTasks.contains(key) else { return } + guard + let data, + let response = Self.makeResponse( + url: url, + asset: asset, + byteCount: data.count, + path: path + ) + else { + self.fail(urlSchemeTask, key) + return + } + self.liveTasks.remove(key) + urlSchemeTask.didReceive(response) + urlSchemeTask.didReceive(data) + urlSchemeTask.didFinish() + } + } + } + + func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) { + liveTasks.remove(ObjectIdentifier(urlSchemeTask)) + } + + private func fail(_ urlSchemeTask: WKURLSchemeTask, _ key: ObjectIdentifier) { + guard liveTasks.remove(key) != nil else { return } + urlSchemeTask.didFailWithError(URLError(.resourceUnavailable)) + } + + private static func makeResponse( + url: URL, + asset: MobileWebShellAsset, + byteCount: Int, + path: String + ) -> HTTPURLResponse? { + HTTPURLResponse( + url: url, + statusCode: 200, + httpVersion: "HTTP/1.1", + headerFields: MobileWebShellResponseHeaders.forPath( + path, + contentType: asset.contentType, + byteCount: byteCount + ) + ) + } +} + +/// WKUserContentController retains handlers; a weak document reference prevents a React-unmount leak. +private final class MobileWebShellBridgeReceiver: NSObject, WKScriptMessageHandler { + weak var document: MobileWebShellDocument? + + func userContentController( + _ controller: WKUserContentController, + didReceive message: WKScriptMessage + ) { + document?.receiveBridgeMessage(message) + } +} + +/// The RN host sees this, never the page: it is the difference between a request that failed and +/// one that never settles. +internal final class MobileWebShellBridgeDeliveryFailedException: GenericException, + @unchecked Sendable { + override var reason: String { + "The mobile web shell bridge could not deliver a message: \(param)" + } +} + +internal final class MobileWebShellBridgeUnavailableException: Exception, @unchecked Sendable { + override var reason: String { + "The mobile web shell bridge is not installed on this view" + } +} + +/// Throw so a host chunking bug rejects rather than leaving its request unsettled. +internal final class MobileWebShellBridgeMessageTooLargeException: GenericException, + @unchecked Sendable { + override var reason: String { + "A bridge message of \(param) bytes exceeds the \(MobileWebShellBridge.maxMessageByteCount) byte cap" + } +} + +/// Where a document's reports go: the view showing it. +protocol MobileWebShellDocumentSink: AnyObject { + func documentLoadState(_ payload: [String: Any]) + func documentBridgeMessage(_ json: String) + func documentExternalNavigation(_ url: String) +} + +/// One WKWebView and every fence on it, apart from React. The view hosts it and lays it out. +final class MobileWebShellDocument: NSObject, WKNavigationDelegate, WKUIDelegate { + weak var sink: MobileWebShellDocumentSink? + let webView: WKWebView + + private let schemeHandler = MobileWebShellSchemeHandler() + private let bridgeReceiver = MobileWebShellBridgeReceiver() + private let bridgeGate = MobileWebShellBridgeGate() + private var bridgeInstalled = false + private var bridgeTarget = MobileWebShellBridgeTarget() + private var applied: MobileWebShellAppliedProps? + private var appliedSessionId: String? { applied?.sessionId } + private var pendingDocumentUrl: URL? + private var isolationReady = false + private var isolationFailed = false + private let loadState = MobileWebShellLoadStateMachine() + private var keyboardPreparationPending = false + private var windowObservers: [NSObjectProtocol] = [] + + override init() { + let configuration = WKWebViewConfiguration() + configuration.allowsInlineMediaPlayback = true + configuration.mediaTypesRequiringUserActionForPlayback = .all + // WebKit cannot disable DOM storage/databases; isolate with a non-persistent store, per-session origin and unmount destruction. + configuration.websiteDataStore = .nonPersistent() + configuration.preferences.javaScriptCanOpenWindowsAutomatically = false + // Disabling text interaction suppresses text after keydown; page user-select handles long-press selection. + configuration.setURLSchemeHandler(schemeHandler, forURLScheme: MobileWebShellOrigin.scheme) + configuration.userContentController.addUserScript(Self.makeBlockerScript()) + webView = WKWebView(frame: .zero, configuration: configuration) + super.init() + bridgeReceiver.document = self + webView.navigationDelegate = self + webView.uiDelegate = self + webView.allowsBackForwardNavigationGestures = false + // WKWebView’s opaque white default flashes during dark-page boot; transparency exposes the shell’s themed frame. + webView.isOpaque = false + webView.backgroundColor = .clear + webView.scrollView.backgroundColor = .clear + webView.scrollView.contentInsetAdjustmentBehavior = .never + hideKeyboardAccessoryBar(of: webView) + ignoreKeyboardNotifications(in: webView) + for name in [ + UIWindow.didBecomeKeyNotification, UIScene.didActivateNotification, + UIApplication.didBecomeActiveNotification, + UIResponder.keyboardDidHideNotification, UITextField.textDidEndEditingNotification, + UITextView.textDidEndEditingNotification + ] { + windowObservers.append(NotificationCenter.default.addObserver( + forName: name, object: nil, queue: .main + ) { [weak self] _ in + self?.loadWhenIsolated() + }) + } + installNetworkBlock(into: configuration.userContentController) + } + + deinit { + for observer in windowObservers { + NotificationCenter.default.removeObserver(observer) + } + } + + /// Apply unordered props together; identical triples do not retry (remount required). + /// Include bridgeEnabled: document-start scripts require a reload to change. + func apply(generationDirectory: String, sessionId: String, bridgeEnabled: Bool) { + let next = MobileWebShellAppliedProps( + generationDirectory: generationDirectory, + sessionId: sessionId, + bridgeEnabled: bridgeEnabled + ) + guard applied?.matches(next) != true else { return } + applied = next + clearBridgeTarget() + loadState.reset() + pendingDocumentUrl = nil + webView.stopLoading() + webView.isHidden = false + emit(loadState.started()) + guard + MobileWebShellOrigin.isValidSessionId(sessionId), + let documentUrl = MobileWebShellOrigin.documentUrl(sessionId: sessionId) + else { + // The private origin is the isolation primitive; a malformed session id leaves us without one. + failPropUpdate(.isolationUnavailable) + return + } + guard + let generation = try? MobileWebShellGeneration.load(directoryPath: generationDirectory) + else { + failPropUpdate(.generationUnreadable) + return + } + schemeHandler.sessionId = sessionId + schemeHandler.generation = generation + applyBridgeInstallation(bridgeEnabled) + if isolationFailed { + failPropUpdate(.isolationUnavailable) + return + } + pendingDocumentUrl = documentUrl + loadWhenIsolated() + } + + /// Retire the old visible generation even when the replacement fails. + private func failPropUpdate(_ reason: MobileWebShellFailureReason) { + clearBridgeTarget() + schemeHandler.sessionId = nil + schemeHandler.generation = nil + pendingDocumentUrl = nil + webView.stopLoading() + webView.isHidden = true + emit(loadState.failed(reason)) + } + + /// removeAllUserScripts is WebKit’s only removal API, so disabling the bridge must reinstall the blocker. + private static func makeBlockerScript() -> WKUserScript { + WKUserScript( + source: networkApiBlocker, + injectionTime: .atDocumentStart, + forMainFrameOnly: false + ) + } + + /// Nothing here runs while the prop stays false, which is what keeps Phase B byte-identical. + private func applyBridgeInstallation(_ bridgeEnabled: Bool) { + guard bridgeEnabled != bridgeInstalled else { return } + clearBridgeTarget() + let controller = webView.configuration.userContentController + if bridgeEnabled { + controller.add(bridgeReceiver, name: MobileWebShellBridge.handlerName) + controller.addUserScript( + WKUserScript( + source: bridgeInstaller, + injectionTime: .atDocumentStart, + // Main-frame injection is convenience; accepts must still refuse subframes that reach the handler. + forMainFrameOnly: true + ) + ) + } else { + controller.removeScriptMessageHandler(forName: MobileWebShellBridge.handlerName) + controller.removeAllUserScripts() + controller.addUserScript(Self.makeBlockerScript()) + } + bridgeInstalled = bridgeEnabled + } + + /// Use the loaded session, not latest props; the replaced same-origin document survives until commit. + fileprivate func receiveBridgeMessage(_ message: WKScriptMessage) { + guard bridgeInstalled, let json = message.body as? String else { return } + let origin = message.frameInfo.securityOrigin + let source = MobileWebShellBridgeSource( + isOurWebView: message.webView === webView, + isMainFrame: message.frameInfo.isMainFrame, + hasCommittedDocument: loadState.hasCommittedDocument, + originProtocol: origin.`protocol`, + originHost: origin.host + ) + guard + MobileWebShellBridge.accepts(source, sessionId: appliedSessionId ?? ""), + bridgeGate.accepts(byteCount: json.utf8.count) + else { return } + bridgeTarget.arm(frame: message.frameInfo, originHost: origin.host) + sink?.documentBridgeMessage(json) + } + + /// Anything that ends the document the page spoke from ends the only target native has. + private func clearBridgeTarget() { + bridgeTarget.clear() + } + + /// Reject posts without a live committed frame that spoke; resolve only after page execution so host requests cannot hang. + func postBridgeMessage(_ json: String, promise: Promise) throws { + guard + MobileWebShellBridge.canPost( + toFrameOriginHost: bridgeTarget.originHost, + sessionId: appliedSessionId ?? "", + hasCommittedDocument: loadState.hasCommittedDocument + ), + let frame = bridgeTarget.frame + else { + throw MobileWebShellBridgeUnavailableException() + } + let byteCount = json.utf8.count + guard MobileWebShellBridge.acceptsByteCount(byteCount) else { + throw MobileWebShellBridgeMessageTooLargeException(byteCount) + } + // Use the two in: labels plus completionHandler to select the callback overload and reply to the frame that spoke. + webView.callAsyncJavaScript( + bridgeDeliver, + arguments: ["m": json], + in: frame, + in: .page + ) { result in + switch result { + case .success: + promise.resolve() + case .failure(let error): + promise.reject(MobileWebShellBridgeDeliveryFailedException(error.localizedDescription)) + } + } + } + + private func installNetworkBlock(into controller: WKUserContentController) { + NetworkBlockRuleList.load { [weak self] ruleList in + guard let self else { return } + guard let ruleList else { + self.isolationFailed = true + self.pendingDocumentUrl = nil + // Rule compilation can finish after refusal; the state machine prevents a second terminal reason. + if self.appliedSessionId != nil { + self.failPropUpdate(.isolationUnavailable) + } + return + } + controller.add(ruleList) + self.isolationReady = true + self.loadWhenIsolated() + } + } + + /// Load only in a foreground-active key window; the view calls this again when it moves to one. + func loadWhenIsolated() { + guard isolationReady, pendingDocumentUrl != nil, !keyboardPreparationPending, + let window = webView.window, window.isKeyWindow, + window.windowScene?.activationState == .foregroundActive else { return } + keyboardPreparationPending = true + DispatchQueue.main.async { [weak self, weak window] in + guard let self else { return } + guard let window, self.webView.window === window, window.isKeyWindow, + window.windowScene?.activationState == .foregroundActive else { + self.keyboardPreparationPending = false + self.loadWhenIsolated() + return + } + let delay: TimeInterval + switch MobileWebShellKeyboardWarmup.prepare(in: window) { + case .ready(let remainingDelay): + delay = remainingDelay + case .waitingForTextInput: + DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { [weak self] in + guard let self else { return } + self.keyboardPreparationPending = false + self.loadWhenIsolated() + } + return + case .failed: + self.keyboardPreparationPending = false + self.failPropUpdate(.documentLoadFailed) + return + } + DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self, weak window] in + guard let self else { return } + self.keyboardPreparationPending = false + guard let window, self.webView.window === window, window.isKeyWindow, + window.windowScene?.activationState == .foregroundActive else { + self.loadWhenIsolated() + return + } + guard self.isolationReady, let url = self.pendingDocumentUrl else { return } + self.pendingDocumentUrl = nil + self.loadState.shellLoadStarted() + self.webView.load(URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData)) + } + } + } + + private func emit(_ emission: MobileWebShellLoadEmission?) { + guard let emission else { return } + sink?.documentLoadState(emission.payload) + } + + private func reportDocumentFailure() { + clearBridgeTarget() + emit(loadState.failed(.documentLoadFailed)) + } + + /// A cancelled navigation is our own doing, not the document's; see MobileWebShellNavigationError. + private func reportNavigationFailure(_ error: Error) { + let error = error as NSError + guard !MobileWebShellNavigationError.isIgnorable(domain: error.domain, code: error.code) else { + return + } + reportDocumentFailure() + } + + private func isDocumentUrl(_ url: URL?) -> Bool { + guard let url, let parts = MobileWebShellRequestParts(url: url) else { return false } + return MobileWebShellOrigin.resolveRequestPath(parts, sessionId: appliedSessionId ?? "") == "/" + } + + func webView( + _ webView: WKWebView, + decidePolicyFor navigationAction: WKNavigationAction, + decisionHandler: @escaping (WKNavigationActionPolicy) -> Void + ) { + var isDownload = false + if #available(iOS 14.5, *) { + isDownload = navigationAction.shouldPerformDownload + } + let isMainFrame = navigationAction.targetFrame?.isMainFrame == true + // linkActivated gates only opener offers; gesture-free subframe top navigation must never authorize a load. + let verdict = MobileWebShellNavigationPolicy.verdict( + url: navigationAction.request.url?.absoluteString, + isMainFrame: isMainFrame, + isDocumentUrl: isDocumentUrl(navigationAction.request.url), + isShellLoad: loadState.isShellLoad, + hasGesture: navigationAction.navigationType == .linkActivated, + isDownload: isDownload + ) + if case let .cancelAndOffer(url) = verdict { + sink?.documentExternalNavigation(url) + } + if verdict == .allow && isMainFrame { + // Spend before handing back the decision so another main-frame action cannot reuse the allowance. + loadState.shellLoadConsumed() + } + decisionHandler(verdict == .allow ? .allow : .cancel) + } + + func webView( + _ webView: WKWebView, + decidePolicyFor navigationResponse: WKNavigationResponse, + decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void + ) { + // Only srcdoc subframes pass the action policy; refusing one must not fail the shell document. + guard navigationResponse.isForMainFrame else { + let url = navigationResponse.response.url?.absoluteString + decisionHandler(url == MobileWebShellNavigationPolicy.inlineDocumentUrl ? .allow : .cancel) + return + } + let allowed = navigationResponse.canShowMIMEType && + isDocumentUrl(navigationResponse.response.url) + if !allowed { + reportDocumentFailure() + } + decisionHandler(allowed ? .allow : .cancel) + } + + func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { + // Clear replies and acceptance when the document is replaced; its successor must commit and send ready. + clearBridgeTarget() + loadState.documentEnded() + guard appliedSessionId != nil else { return } + emit(loadState.started()) + } + + /// Arm at commit, before document-start messages and didFinish. + func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { + guard isDocumentUrl(webView.url) else { return } + // Commit rearms acceptance, so clear the replaced frame here as well as at provisional start. + clearBridgeTarget() + loadState.committed() + } + + /// history.replaceState changes the path before finish; finished() uses commit state instead of URL equality. + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { + MobileWebShellWarmUp.release() + emit(loadState.finished()) + } + + func webView( + _ webView: WKWebView, + didFailProvisionalNavigation navigation: WKNavigation!, + withError error: Error + ) { + MobileWebShellWarmUp.release() + reportNavigationFailure(error) + } + + func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { + MobileWebShellWarmUp.release() + reportNavigationFailure(error) + } + + /// Report renderer death; memory pressure/provider updates look alike, so the caller owns retry policy. + func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { + MobileWebShellWarmUp.release() + clearBridgeTarget() + // Jetsam takes WebContent while Orca is backgrounded; the report can arrive as the app resumes, before it is active. + let background = webView.window == nil || UIApplication.shared.applicationState != .active + emit(loadState.failed(.renderProcessGone, background: background)) + } + + func webView( + _ webView: WKWebView, + createWebViewWith configuration: WKWebViewConfiguration, + for navigationAction: WKNavigationAction, + windowFeatures: WKWindowFeatures + ) -> WKWebView? { + nil + } +} diff --git a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift index d341acab6bc..86c45218151 100644 --- a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift +++ b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift @@ -1,190 +1,8 @@ import ExpoModulesCore import WebKit -private let networkBlockIdentifier = "dev.orca.mobile-web-shell.network-block-v1" - -/// Block http(s)/ws(s) beneath CSP; compilation failure is terminal and must load nothing. -private let networkBlockRules = """ - [ - { "trigger": { "url-filter": "^https?://" }, "action": { "type": "block" } }, - { "trigger": { "url-filter": "^wss?://" }, "action": { "type": "block" } } - ] - """ - -/// Compiled once per launch and shared by every view, so later mounts load without waiting; main thread only. -private enum NetworkBlockRuleList { - private static var compiled: WKContentRuleList? - - /// Synchronous once compiled in this process; nil means isolation is unavailable. - static func load(_ completion: @escaping (WKContentRuleList?) -> Void) { - if let compiled { - completion(compiled) - return - } - // Optional chaining would skip completion when the store is absent, leaving loading forever; fail closed instead. - guard let store = WKContentRuleListStore.default() else { - completion(nil) - return - } - store.compileContentRuleList( - forIdentifier: networkBlockIdentifier, - encodedContentRuleList: networkBlockRules - ) { ruleList, _ in - DispatchQueue.main.async { - if let ruleList { - compiled = ruleList - } - completion(ruleList) - } - } - } -} - -/// CSP fences fetch/XHR; document-start script blocks unobservable WebSocket and service-worker APIs. -/// Match Android; both non-configurable and non-writable prevent page replacement. -private let networkApiBlocker = """ - (function(){ - var deny=function(){throw new TypeError('Network access is disabled')}; - try{Object.defineProperty(globalThis,'WebSocket',{value:deny,configurable:false,writable:false})}catch(_){} - try{Object.defineProperty(Navigator.prototype,'serviceWorker',{get:function(){return undefined},configurable:false})}catch(_){} - try{Object.defineProperty(navigator,'serviceWorker',{value:undefined,configurable:false,writable:false})}catch(_){} - })(); - """ - -/// Inject the same orcaBridge surface as Android before scripts, alongside the network blocker. -/// Capture the sink so deleting window.webkit cannot break it; freeze properties against replacement. -private let bridgeInstaller = """ - (function(){ - var sink=window.webkit.messageHandlers.orcaBridge; - var handler=null; - var bridge={}; - Object.defineProperty(bridge,'postMessage',{value:function(json){ - if(typeof json!=='string'){throw new TypeError('orcaBridge.postMessage expects a string')} - sink.postMessage(json)},configurable:false,writable:false,enumerable:true}); - Object.defineProperty(bridge,'onmessage',{get:function(){return handler}, - set:function(value){handler=typeof value==='function'?value:null},configurable:false,enumerable:true}); - Object.defineProperty(bridge,'__deliver',{value:function(json){if(handler){handler({data:json})}}, - configurable:false,writable:false,enumerable:false}); - Object.defineProperty(globalThis,'orcaBridge',{value:bridge,configurable:false,writable:false,enumerable:true}); - })(); - """ - -/// Bind payload as JS value m, never script text. A missing installer must throw to reject undelivered posts. -private let bridgeDeliver = """ - globalThis.orcaBridge.__deliver(m) - """ - -private final class MobileWebShellSchemeHandler: NSObject, WKURLSchemeHandler { - /// Read up-to-10-MiB assets off WebKit’s main-thread scheme callbacks. - private let readQueue = DispatchQueue(label: "dev.orca.mobile-web-shell.read") - /// Main-thread live-task set prevents stopped-task delivery’s uncatchable Objective-C exception. - private var liveTasks: Set = [] - - var sessionId: String? - var generation: MobileWebShellGeneration? - - func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) { - let key = ObjectIdentifier(urlSchemeTask) - liveTasks.insert(key) - guard - let sessionId, - let generation, - let url = urlSchemeTask.request.url, - let parts = MobileWebShellRequestParts(request: urlSchemeTask.request), - let path = MobileWebShellOrigin.resolveRequestPath(parts, sessionId: sessionId), - let asset = generation.entries[path] - else { - fail(urlSchemeTask, key) - return - } - readQueue.async { [weak self] in - let data = try? Data(contentsOf: asset.file) - DispatchQueue.main.async { - guard let self, self.liveTasks.contains(key) else { return } - guard - let data, - let response = Self.makeResponse( - url: url, - asset: asset, - byteCount: data.count, - path: path - ) - else { - self.fail(urlSchemeTask, key) - return - } - self.liveTasks.remove(key) - urlSchemeTask.didReceive(response) - urlSchemeTask.didReceive(data) - urlSchemeTask.didFinish() - } - } - } - - func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) { - liveTasks.remove(ObjectIdentifier(urlSchemeTask)) - } - - private func fail(_ urlSchemeTask: WKURLSchemeTask, _ key: ObjectIdentifier) { - guard liveTasks.remove(key) != nil else { return } - urlSchemeTask.didFailWithError(URLError(.resourceUnavailable)) - } - - private static func makeResponse( - url: URL, - asset: MobileWebShellAsset, - byteCount: Int, - path: String - ) -> HTTPURLResponse? { - HTTPURLResponse( - url: url, - statusCode: 200, - httpVersion: "HTTP/1.1", - headerFields: MobileWebShellResponseHeaders.forPath( - path, - contentType: asset.contentType, - byteCount: byteCount - ) - ) - } -} - -/// WKUserContentController retains handlers; a weak view reference prevents a React-unmount leak. -private final class MobileWebShellBridgeReceiver: NSObject, WKScriptMessageHandler { - weak var view: OrcaMobileWebShellView? - - func userContentController( - _ controller: WKUserContentController, - didReceive message: WKScriptMessage - ) { - view?.receiveBridgeMessage(message) - } -} - -/// The RN host sees this, never the page: it is the difference between a request that failed and -/// one that never settles. -internal final class MobileWebShellBridgeDeliveryFailedException: GenericException, - @unchecked Sendable { - override var reason: String { - "The mobile web shell bridge could not deliver a message: \(param)" - } -} - -internal final class MobileWebShellBridgeUnavailableException: Exception, @unchecked Sendable { - override var reason: String { - "The mobile web shell bridge is not installed on this view" - } -} - -/// Throw so a host chunking bug rejects rather than leaving its request unsettled. -internal final class MobileWebShellBridgeMessageTooLargeException: GenericException, - @unchecked Sendable { - override var reason: String { - "A bridge message of \(param) bytes exceeds the \(MobileWebShellBridge.maxMessageByteCount) byte cap" - } -} - -final class OrcaMobileWebShellView: ExpoView, WKNavigationDelegate, WKUIDelegate, +/// React Native's handle on one document: it lays the WebView out and owns the back-edge swipe. +final class OrcaMobileWebShellView: ExpoView, MobileWebShellDocumentSink, UIGestureRecognizerDelegate { let onLoadState = EventDispatcher() let onBridgeMessage = EventDispatcher() @@ -193,58 +11,20 @@ final class OrcaMobileWebShellView: ExpoView, WKNavigationDelegate, WKUIDelegate // The page's own Back: the stack's swipe is off while the page claims Back, and a strip overlay would eat taps. private let backEdgePan = UIPanGestureRecognizer() - private let schemeHandler = MobileWebShellSchemeHandler() - private let bridgeReceiver = MobileWebShellBridgeReceiver() - private let bridgeGate = MobileWebShellBridgeGate() - private var bridgeEnabled = false - private var bridgeInstalled = false - private var bridgeTarget = MobileWebShellBridgeTarget() - private var webView: WKWebView! + private let document = MobileWebShellDocument() private var generationDirectory = "" private var sessionId = "" - private var applied: MobileWebShellAppliedProps? - private var appliedSessionId: String? { applied?.sessionId } - private var pendingDocumentUrl: URL? - private var isolationReady = false - private var isolationFailed = false - private let loadState = MobileWebShellLoadStateMachine() - private var keyboardPreparationPending = false - private var windowObservers: [NSObjectProtocol] = [] + private var bridgeEnabled = false override func didMoveToWindow() { super.didMoveToWindow() - loadWhenIsolated() - } - - deinit { - for observer in windowObservers { - NotificationCenter.default.removeObserver(observer) - } + document.loadWhenIsolated() } required init(appContext: AppContext? = nil) { super.init(appContext: appContext) - let configuration = WKWebViewConfiguration() - configuration.allowsInlineMediaPlayback = true - configuration.mediaTypesRequiringUserActionForPlayback = .all - // WebKit cannot disable DOM storage/databases; isolate with a non-persistent store, per-session origin and unmount destruction. - configuration.websiteDataStore = .nonPersistent() - configuration.preferences.javaScriptCanOpenWindowsAutomatically = false - // Disabling text interaction suppresses text after keydown; page user-select handles long-press selection. - configuration.setURLSchemeHandler(schemeHandler, forURLScheme: MobileWebShellOrigin.scheme) - configuration.userContentController.addUserScript(Self.makeBlockerScript()) - bridgeReceiver.view = self - webView = WKWebView(frame: bounds, configuration: configuration) - webView.navigationDelegate = self - webView.uiDelegate = self - webView.allowsBackForwardNavigationGestures = false - // WKWebView’s opaque white default flashes during dark-page boot; transparency exposes the shell’s themed frame. - webView.isOpaque = false - webView.backgroundColor = .clear - webView.scrollView.backgroundColor = .clear - webView.scrollView.contentInsetAdjustmentBehavior = .never - hideKeyboardAccessoryBar(of: webView) - ignoreKeyboardNotifications(in: webView) + document.sink = self + let webView = document.webView webView.translatesAutoresizingMaskIntoConstraints = false addSubview(webView) NSLayoutConstraint.activate([ @@ -253,19 +33,6 @@ final class OrcaMobileWebShellView: ExpoView, WKNavigationDelegate, WKUIDelegate webView.leadingAnchor.constraint(equalTo: leadingAnchor), webView.trailingAnchor.constraint(equalTo: trailingAnchor) ]) - for name in [ - UIWindow.didBecomeKeyNotification, UIScene.didActivateNotification, - UIApplication.didBecomeActiveNotification, - UIResponder.keyboardDidHideNotification, UITextField.textDidEndEditingNotification, - UITextView.textDidEndEditingNotification - ] { - windowObservers.append(NotificationCenter.default.addObserver( - forName: name, object: nil, queue: .main - ) { [weak self] _ in - self?.loadWhenIsolated() - }) - } - installNetworkBlock(into: configuration.userContentController) backEdgePan.isEnabled = false backEdgePan.addTarget(self, action: #selector(handleBackEdgePan(_:))) backEdgePan.delegate = self @@ -310,332 +77,27 @@ final class OrcaMobileWebShellView: ExpoView, WKNavigationDelegate, WKUIDelegate bridgeEnabled = value } - /// Apply unordered props together; identical triples do not retry (remount required). - /// Include bridgeEnabled: document-start scripts require a reload to change. func propsDidUpdate() { - let next = MobileWebShellAppliedProps( + document.apply( generationDirectory: generationDirectory, sessionId: sessionId, bridgeEnabled: bridgeEnabled ) - guard applied?.matches(next) != true else { return } - applied = next - clearBridgeTarget() - loadState.reset() - pendingDocumentUrl = nil - webView.stopLoading() - webView.isHidden = false - emit(loadState.started()) - guard - MobileWebShellOrigin.isValidSessionId(sessionId), - let documentUrl = MobileWebShellOrigin.documentUrl(sessionId: sessionId) - else { - // The private origin is the isolation primitive; a malformed session id leaves us without one. - failPropUpdate(.isolationUnavailable) - return - } - guard - let generation = try? MobileWebShellGeneration.load(directoryPath: generationDirectory) - else { - failPropUpdate(.generationUnreadable) - return - } - schemeHandler.sessionId = sessionId - schemeHandler.generation = generation - applyBridgeInstallation() - if isolationFailed { - failPropUpdate(.isolationUnavailable) - return - } - pendingDocumentUrl = documentUrl - loadWhenIsolated() } - /// Retire the old visible generation even when the replacement fails. - private func failPropUpdate(_ reason: MobileWebShellFailureReason) { - clearBridgeTarget() - schemeHandler.sessionId = nil - schemeHandler.generation = nil - pendingDocumentUrl = nil - webView.stopLoading() - webView.isHidden = true - emit(loadState.failed(reason)) + func postBridgeMessage(_ json: String, promise: Promise) throws { + try document.postBridgeMessage(json, promise: promise) } - /// removeAllUserScripts is WebKit’s only removal API, so disabling the bridge must reinstall the blocker. - private static func makeBlockerScript() -> WKUserScript { - WKUserScript( - source: networkApiBlocker, - injectionTime: .atDocumentStart, - forMainFrameOnly: false - ) + func documentLoadState(_ payload: [String: Any]) { + onLoadState(payload) } - /// Nothing here runs while the prop stays false, which is what keeps Phase B byte-identical. - private func applyBridgeInstallation() { - guard bridgeEnabled != bridgeInstalled else { return } - clearBridgeTarget() - let controller = webView.configuration.userContentController - if bridgeEnabled { - controller.add(bridgeReceiver, name: MobileWebShellBridge.handlerName) - controller.addUserScript( - WKUserScript( - source: bridgeInstaller, - injectionTime: .atDocumentStart, - // Main-frame injection is convenience; accepts must still refuse subframes that reach the handler. - forMainFrameOnly: true - ) - ) - } else { - controller.removeScriptMessageHandler(forName: MobileWebShellBridge.handlerName) - controller.removeAllUserScripts() - controller.addUserScript(Self.makeBlockerScript()) - } - bridgeInstalled = bridgeEnabled - } - - /// Use the loaded session, not latest props; the replaced same-origin document survives until commit. - fileprivate func receiveBridgeMessage(_ message: WKScriptMessage) { - guard bridgeInstalled, let json = message.body as? String else { return } - let origin = message.frameInfo.securityOrigin - let source = MobileWebShellBridgeSource( - isOurWebView: message.webView === webView, - isMainFrame: message.frameInfo.isMainFrame, - hasCommittedDocument: loadState.hasCommittedDocument, - originProtocol: origin.`protocol`, - originHost: origin.host - ) - guard - MobileWebShellBridge.accepts(source, sessionId: appliedSessionId ?? ""), - bridgeGate.accepts(byteCount: json.utf8.count) - else { return } - bridgeTarget.arm(frame: message.frameInfo, originHost: origin.host) + func documentBridgeMessage(_ json: String) { onBridgeMessage(["json": json]) } - /// Anything that ends the document the page spoke from ends the only target native has. - private func clearBridgeTarget() { - bridgeTarget.clear() - } - - /// Reject posts without a live committed frame that spoke; resolve only after page execution so host requests cannot hang. - func postBridgeMessage(_ json: String, promise: Promise) throws { - guard - MobileWebShellBridge.canPost( - toFrameOriginHost: bridgeTarget.originHost, - sessionId: appliedSessionId ?? "", - hasCommittedDocument: loadState.hasCommittedDocument - ), - let frame = bridgeTarget.frame - else { - throw MobileWebShellBridgeUnavailableException() - } - let byteCount = json.utf8.count - guard MobileWebShellBridge.acceptsByteCount(byteCount) else { - throw MobileWebShellBridgeMessageTooLargeException(byteCount) - } - // Use the two in: labels plus completionHandler to select the callback overload and reply to the frame that spoke. - webView.callAsyncJavaScript( - bridgeDeliver, - arguments: ["m": json], - in: frame, - in: .page - ) { result in - switch result { - case .success: - promise.resolve() - case .failure(let error): - promise.reject(MobileWebShellBridgeDeliveryFailedException(error.localizedDescription)) - } - } - } - - private func installNetworkBlock(into controller: WKUserContentController) { - NetworkBlockRuleList.load { [weak self] ruleList in - guard let self else { return } - guard let ruleList else { - self.isolationFailed = true - self.pendingDocumentUrl = nil - // Rule compilation can finish after refusal; the state machine prevents a second terminal reason. - if self.appliedSessionId != nil { - self.failPropUpdate(.isolationUnavailable) - } - return - } - controller.add(ruleList) - self.isolationReady = true - self.loadWhenIsolated() - } - } - - private func loadWhenIsolated() { - guard isolationReady, pendingDocumentUrl != nil, !keyboardPreparationPending, - let window, window.isKeyWindow, - window.windowScene?.activationState == .foregroundActive else { return } - keyboardPreparationPending = true - DispatchQueue.main.async { [weak self, weak window] in - guard let self else { return } - guard let window, self.window === window, window.isKeyWindow, - window.windowScene?.activationState == .foregroundActive else { - self.keyboardPreparationPending = false - self.loadWhenIsolated() - return - } - let delay: TimeInterval - switch MobileWebShellKeyboardWarmup.prepare(in: window) { - case .ready(let remainingDelay): - delay = remainingDelay - case .waitingForTextInput: - DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { [weak self] in - guard let self else { return } - self.keyboardPreparationPending = false - self.loadWhenIsolated() - } - return - case .failed: - self.keyboardPreparationPending = false - self.failPropUpdate(.documentLoadFailed) - return - } - DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self, weak window] in - guard let self else { return } - self.keyboardPreparationPending = false - guard let window, self.window === window, window.isKeyWindow, - window.windowScene?.activationState == .foregroundActive else { - self.loadWhenIsolated() - return - } - guard self.isolationReady, let url = self.pendingDocumentUrl else { return } - self.pendingDocumentUrl = nil - self.loadState.shellLoadStarted() - self.webView.load(URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData)) - } - } - } - - private func emit(_ emission: MobileWebShellLoadEmission?) { - guard let emission else { return } - onLoadState(emission.payload) - } - - private func reportDocumentFailure() { - clearBridgeTarget() - emit(loadState.failed(.documentLoadFailed)) - } - - /// A cancelled navigation is our own doing, not the document's; see MobileWebShellNavigationError. - private func reportNavigationFailure(_ error: Error) { - let error = error as NSError - guard !MobileWebShellNavigationError.isIgnorable(domain: error.domain, code: error.code) else { - return - } - reportDocumentFailure() - } - - private func isDocumentUrl(_ url: URL?) -> Bool { - guard let url, let parts = MobileWebShellRequestParts(url: url) else { return false } - return MobileWebShellOrigin.resolveRequestPath(parts, sessionId: sessionId) == "/" - } - - func webView( - _ webView: WKWebView, - decidePolicyFor navigationAction: WKNavigationAction, - decisionHandler: @escaping (WKNavigationActionPolicy) -> Void - ) { - var isDownload = false - if #available(iOS 14.5, *) { - isDownload = navigationAction.shouldPerformDownload - } - let isMainFrame = navigationAction.targetFrame?.isMainFrame == true - // linkActivated gates only opener offers; gesture-free subframe top navigation must never authorize a load. - let verdict = MobileWebShellNavigationPolicy.verdict( - url: navigationAction.request.url?.absoluteString, - isMainFrame: isMainFrame, - isDocumentUrl: isDocumentUrl(navigationAction.request.url), - isShellLoad: loadState.isShellLoad, - hasGesture: navigationAction.navigationType == .linkActivated, - isDownload: isDownload - ) - if case let .cancelAndOffer(url) = verdict { - onExternalNavigation(["url": url]) - } - if verdict == .allow && isMainFrame { - // Spend before handing back the decision so another main-frame action cannot reuse the allowance. - loadState.shellLoadConsumed() - } - decisionHandler(verdict == .allow ? .allow : .cancel) - } - - func webView( - _ webView: WKWebView, - decidePolicyFor navigationResponse: WKNavigationResponse, - decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void - ) { - // Only srcdoc subframes pass the action policy; refusing one must not fail the shell document. - guard navigationResponse.isForMainFrame else { - let url = navigationResponse.response.url?.absoluteString - decisionHandler(url == MobileWebShellNavigationPolicy.inlineDocumentUrl ? .allow : .cancel) - return - } - let allowed = navigationResponse.canShowMIMEType && - isDocumentUrl(navigationResponse.response.url) - if !allowed { - reportDocumentFailure() - } - decisionHandler(allowed ? .allow : .cancel) - } - - func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { - // Clear replies and acceptance when the document is replaced; its successor must commit and send ready. - clearBridgeTarget() - loadState.documentEnded() - guard appliedSessionId != nil else { return } - emit(loadState.started()) - } - - /// Arm at commit, before document-start messages and didFinish. - func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { - guard isDocumentUrl(webView.url) else { return } - // Commit rearms acceptance, so clear the replaced frame here as well as at provisional start. - clearBridgeTarget() - loadState.committed() - } - - /// history.replaceState changes the path before finish; finished() uses commit state instead of URL equality. - func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { - MobileWebShellWarmUp.release() - emit(loadState.finished()) - } - - func webView( - _ webView: WKWebView, - didFailProvisionalNavigation navigation: WKNavigation!, - withError error: Error - ) { - MobileWebShellWarmUp.release() - reportNavigationFailure(error) - } - - func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { - MobileWebShellWarmUp.release() - reportNavigationFailure(error) - } - - /// Report renderer death; memory pressure/provider updates look alike, so the caller owns retry policy. - func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { - MobileWebShellWarmUp.release() - clearBridgeTarget() - // Jetsam takes WebContent while Orca is backgrounded; the report can arrive as the app resumes, before it is active. - let background = window == nil || UIApplication.shared.applicationState != .active - emit(loadState.failed(.renderProcessGone, background: background)) - } - - func webView( - _ webView: WKWebView, - createWebViewWith configuration: WKWebViewConfiguration, - for navigationAction: WKNavigationAction, - windowFeatures: WKWindowFeatures - ) -> WKWebView? { - nil + func documentExternalNavigation(_ url: String) { + onExternalNavigation(["url": url]) } } From 041b542a935286519c4c4eadc384f7b811e658e5 Mon Sep 17 00:00:00 2001 From: ggpepperstech Date: Sun, 8 Nov 2026 20:08:29 -0600 Subject: [PATCH 26/80] feat(cli): list and reload installed browser extensions (#39613) * feat(cli): list and reload installed browser extensions * docs(browser): document extension command contracts * docs(browser): describe extension test fixture factory * refactor(cli): validate extension reload once and trim list output The RPC params schema owns the ID-or-path and ID format checks. Reload drops --profile and the constant scope field, since it always reloads every profile where the extension is enabled. List no longer returns icon data URLs or permission lists. * test(browser): keep five extension reload tests at the command owner * fix(cli): route orca extension through the packaged Linux launcher * fix(browser): refresh Settings after an extension reload from the CLI * test(browser): type the reload fixture list without a cast --------- Co-authored-by: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> --- src/cli/browser/browser-extension.ts | 47 +++++++++ src/cli/browser/browser-handler-groups.ts | 5 + src/cli/command-scoped-flag-help.ts | 5 + src/cli/root-help-text-primary.ts | 2 + src/cli/specs/browser-advanced.ts | 19 ++++ src/cli/specs/browser-page-target.ts | 2 + .../browser-extension-commands.test.ts | 95 +++++++++++++++++++ .../extensions/browser-extension-commands.ts | 91 ++++++++++++++++++ .../browser-extension-management.ts | 4 + src/main/host/electron-browser-commands.ts | 4 +- .../runtime-browser-commands-factory.ts | 28 ++++++ .../rpc/methods/browser/browser-core.ts | 2 + .../browser/browser-extension-rpc-methods.ts | 21 ++++ src/main/startup/cli-command-names.ts | 1 + .../extensions/browser-extension-types.ts | 14 +++ src/shared/orca-browser-cli-commands.ts | 1 + .../rpc-contract/browser-extension-params.ts | 12 +++ .../rpc-params-catalog.generated.ts | 3 + 18 files changed, 355 insertions(+), 1 deletion(-) create mode 100644 src/cli/browser/browser-extension.ts create mode 100644 src/main/browser/extensions/browser-extension-commands.test.ts create mode 100644 src/main/browser/extensions/browser-extension-commands.ts create mode 100644 src/main/runtime/rpc/methods/browser/browser-extension-rpc-methods.ts create mode 100644 src/shared/rpc-contract/browser-extension-params.ts diff --git a/src/cli/browser/browser-extension.ts b/src/cli/browser/browser-extension.ts new file mode 100644 index 00000000000..8a95b6c7a4a --- /dev/null +++ b/src/cli/browser/browser-extension.ts @@ -0,0 +1,47 @@ +import type { + BrowserExtensionSummaryListResult, + BrowserExtensionReloadResult +} from '../../shared/browser/extensions/browser-extension-types' +import type { CommandHandler } from '../dispatch' +import { getPresentStringFlag } from '../flags' +import { printResult } from '../format' +import { resolveRepoPathArgument } from '../repo-path-arguments' + +export const BROWSER_EXTENSION_HANDLERS: Record = { + 'extension list': async ({ flags, client, json }) => { + const profileId = getPresentStringFlag(flags, 'profile') + const result = await client.call( + 'browser.extensionList', + profileId !== undefined ? { profileId } : {} + ) + printResult(result, json, ({ active, extensions }) => + !active + ? 'Browser extensions are disabled in Settings › Experimental.' + : extensions.length === 0 + ? 'No browser extensions installed.' + : extensions + .map( + (extension) => + `${extension.id}\t${extension.name}\t${extension.version}\t${extension.enabled ? 'enabled' : 'disabled'}\t${extension.source}\t${extension.path}` + ) + .join('\n') + ) + }, + 'extension reload': async ({ flags, client, cwd, json }) => { + const extensionId = getPresentStringFlag(flags, 'extension-id') + const path = getPresentStringFlag(flags, 'path') + const result = await client.call('browser.extensionReload', { + ...(extensionId !== undefined ? { extensionId } : {}), + ...(path !== undefined + ? { + path: resolveRepoPathArgument(path, cwd, client.isRemote, 'Remote extension source') + } + : {}) + }) + printResult( + result, + json, + (value) => `Reloaded extension ${value.extensionId} across all profiles where it is enabled.` + ) + } +} diff --git a/src/cli/browser/browser-handler-groups.ts b/src/cli/browser/browser-handler-groups.ts index 48b89c4d065..356fcc787f7 100644 --- a/src/cli/browser/browser-handler-groups.ts +++ b/src/cli/browser/browser-handler-groups.ts @@ -2,6 +2,11 @@ import { ORCA_BROWSER_CLI_COMMAND_KEYS } from '../../shared/orca-browser-cli-com import type { HandlerGroup } from '../handler-group-manifest' export const BROWSER_HANDLER_GROUPS: readonly HandlerGroup[] = [ + { + name: 'browser-extension', + keys: ORCA_BROWSER_CLI_COMMAND_KEYS['browser-extension'], + load: async () => (await import('./browser-extension.js')).BROWSER_EXTENSION_HANDLERS + }, { name: 'browser-identity', keys: ORCA_BROWSER_CLI_COMMAND_KEYS['browser-identity'], diff --git a/src/cli/command-scoped-flag-help.ts b/src/cli/command-scoped-flag-help.ts index 0c9a294dcaa..fc5c6c56006 100644 --- a/src/cli/command-scoped-flag-help.ts +++ b/src/cli/command-scoped-flag-help.ts @@ -3,6 +3,11 @@ const FILE_OPEN_FOCUS_HELP = "--focus Bring the user to the file (switches Orca's window to its worktree)" const COMMAND_SCOPED_FLAG_HELP: Record> = { + 'extension reload': { + 'extension-id': + '--extension-id Installed unpacked extension ID; also accepted positionally', + path: '--path Source folder of exactly one installed unpacked extension' + }, 'reference add': { url: '--url Full review/issue URL or Jira key; use positionals or repeated --url', 'reference-site': '--reference-site Jira site for bare keys; see orca jira connection' diff --git a/src/cli/root-help-text-primary.ts b/src/cli/root-help-text-primary.ts index 569ae4564f5..6ad30800fca 100644 --- a/src/cli/root-help-text-primary.ts +++ b/src/cli/root-help-text-primary.ts @@ -216,6 +216,8 @@ export const ROOT_HELP_TEXT_PRIMARY = [ ' tab profile clone Clone a browser tab into another profile', ' tab switch Switch the active browser tab by --index or --page', ' tab close Close a browser tab by --index/--page or the current tab', + ' extension list List installed browser extensions', + ' extension reload Reload an unpacked extension across enabled profiles', ' snapshot Accessibility snapshot with element refs (e.g. @e1, @e2)', ' goto Navigate the active tab to --url', ' click Click element by --element ref', diff --git a/src/cli/specs/browser-advanced.ts b/src/cli/specs/browser-advanced.ts index 229c0e54c3b..39cbcee9105 100644 --- a/src/cli/specs/browser-advanced.ts +++ b/src/cli/specs/browser-advanced.ts @@ -3,6 +3,25 @@ import { GLOBAL_FLAGS } from '../args' import { withBrowserPageTarget } from './browser-page-target' export const BROWSER_ADVANCED_COMMAND_SPECS: CommandSpec[] = withBrowserPageTarget([ + { + path: ['extension', 'list'], + summary: 'List installed browser extensions and their enabled state', + usage: 'orca extension list [--profile ] [--json]', + allowedFlags: [...GLOBAL_FLAGS, 'profile'], + notes: ['Desktop only. Defaults to the default browser profile on this Orca host.'] + }, + { + path: ['extension', 'reload'], + summary: 'Reload an installed unpacked browser extension', + usage: + 'orca extension reload [--json] | orca extension reload --path [--json]', + positionalArgs: ['extension-id'], + allowedFlags: [...GLOBAL_FLAGS, 'extension-id', 'path'], + notes: [ + 'Desktop only. --path selects an already installed unpacked source folder; it never installs one.', + 'Like the Settings reload button, this restarts the extension in every profile where it is enabled.' + ] + }, { path: ['cookie', 'get'], summary: 'Get cookies for the active tab (optionally filter by URL)', diff --git a/src/cli/specs/browser-page-target.ts b/src/cli/specs/browser-page-target.ts index 175b5f58333..ab303ca29f7 100644 --- a/src/cli/specs/browser-page-target.ts +++ b/src/cli/specs/browser-page-target.ts @@ -2,6 +2,8 @@ import type { CommandSpec } from '../args' // These open, list or configure pages, so there is no one page to target. const NOT_PAGE_TARGETED = new Set([ + 'extension list', + 'extension reload', 'open-url', 'tab list', 'tab current', diff --git a/src/main/browser/extensions/browser-extension-commands.test.ts b/src/main/browser/extensions/browser-extension-commands.test.ts new file mode 100644 index 00000000000..a405dbf4ca1 --- /dev/null +++ b/src/main/browser/extensions/browser-extension-commands.test.ts @@ -0,0 +1,95 @@ +import { mkdtemp, mkdir, rm, symlink } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { + BrowserExtensionActionResult, + InstalledBrowserExtension +} from '../../../shared/browser/extensions/browser-extension-types' + +const mocks = vi.hoisted(() => { + const installed: InstalledBrowserExtension[] = [] + return { + installed, + reload: vi.fn(async (): Promise => ({ ok: true })) + } +}) +vi.mock('../sessions/browser-session-registry', () => ({ browserSessionRegistry: {} })) +vi.mock('./installation/browser-extension-install-store', () => ({ + getBrowserExtensionInstallStore: () => ({ list: () => mocks.installed }) +})) +vi.mock('./browser-extension-management', () => ({ + reloadUnpackedBrowserExtension: mocks.reload +})) +import { desktopBrowserExtensions } from './browser-extension-commands' + +const ID = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +let directory: string + +function extension(overrides: Partial = {}): InstalledBrowserExtension { + return { + id: ID, + name: 'Dev', + version: '1', + path: directory, + source: 'unpacked', + iconPath: null, + permissions: [], + hostPermissions: [], + optionsPage: null, + ...overrides + } +} + +beforeEach(async () => { + vi.clearAllMocks() + mocks.installed = [] + directory = await mkdtemp(join(tmpdir(), 'orca-extension-command-')) +}) +afterEach(async () => rm(directory, { recursive: true, force: true })) + +describe('desktop extension reload', () => { + it('reloads the installed unpacked extension whose source folder matches through an alias', async () => { + const source = join(directory, 'source') + const alias = join(directory, 'alias') + await mkdir(source) + await symlink(source, alias, 'junction') + mocks.installed = [extension({ path: source })] + await expect(desktopBrowserExtensions.reload({ path: alias })).resolves.toEqual({ + extensionId: ID + }) + expect(mocks.reload).toHaveBeenCalledWith(ID) + }) + + it('rejects a source folder shared by two installs without reloading either', async () => { + mocks.installed = [extension(), extension({ id: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' })] + await expect(desktopBrowserExtensions.reload({ path: directory })).rejects.toMatchObject({ + code: 'invalid_argument' + }) + expect(mocks.reload).not.toHaveBeenCalled() + }) + + it('never installs or reloads a folder that is not installed', async () => { + await expect(desktopBrowserExtensions.reload({ path: directory })).rejects.toMatchObject({ + code: 'invalid_argument' + }) + expect(mocks.reload).not.toHaveBeenCalled() + }) + + it('refuses Web Store extensions', async () => { + mocks.installed = [extension({ source: 'webstore' })] + await expect(desktopBrowserExtensions.reload({ extensionId: ID })).rejects.toMatchObject({ + code: 'invalid_argument' + }) + expect(mocks.reload).not.toHaveBeenCalled() + }) + + it('surfaces a manager failure instead of reporting success', async () => { + mocks.installed = [extension()] + mocks.reload.mockResolvedValueOnce({ ok: false, error: 'Extensions disabled' }) + await expect(desktopBrowserExtensions.reload({ extensionId: ID })).rejects.toMatchObject({ + code: 'runtime_error', + message: 'Extensions disabled' + }) + }) +}) diff --git a/src/main/browser/extensions/browser-extension-commands.ts b/src/main/browser/extensions/browser-extension-commands.ts new file mode 100644 index 00000000000..e3d3f751cc5 --- /dev/null +++ b/src/main/browser/extensions/browser-extension-commands.ts @@ -0,0 +1,91 @@ +import { realpath, stat } from 'node:fs/promises' +import { isAbsolute } from 'node:path' +import type { + BrowserExtensionListParams, + BrowserExtensionReloadParams, + BrowserExtensionReloadResult, + BrowserExtensionSummaryListResult, + InstalledBrowserExtension +} from '../../../shared/browser/extensions/browser-extension-types' +import { BrowserError } from '../browser-error' +import { browserSessionRegistry } from '../sessions/browser-session-registry' +import { getBrowserExtensionInstallStore } from './installation/browser-extension-install-store' +import { + listBrowserExtensionsForProfile, + reloadUnpackedBrowserExtension +} from './browser-extension-management' + +/** Finds the one installed unpacked extension whose canonical source folder is `path`. */ +async function extensionIdForSourceFolder( + path: string, + installed: InstalledBrowserExtension[] +): Promise { + if (!isAbsolute(path)) { + throw new BrowserError('invalid_argument', 'Extension source folder must be an absolute path') + } + const directory = await realpath(path).catch(() => null) + if (!directory || !(await stat(directory).catch(() => null))?.isDirectory()) { + throw new BrowserError('invalid_argument', 'Extension source folder was not found') + } + const matches = await Promise.all( + installed + .filter((extension) => extension.source === 'unpacked') + .map(async (extension) => + (await realpath(extension.path).catch(() => null)) === directory ? extension.id : null + ) + ) + const ids = matches.filter((id) => id !== null) + if (ids.length !== 1) { + throw new BrowserError( + 'invalid_argument', + ids.length === 0 + ? 'No installed unpacked extension matches this source folder' + : 'Multiple installed unpacked extensions match this source folder; use an extension ID' + ) + } + return ids[0] +} + +/** Selection shape and ID format are checked by the RPC params schema. */ +export const desktopBrowserExtensions = { + async list({ + profileId = 'default' + }: BrowserExtensionListParams): Promise { + if (!browserSessionRegistry.getProfile(profileId)) { + throw new BrowserError('invalid_argument', `Browser profile ${profileId} was not found`) + } + const { active, extensions } = await listBrowserExtensionsForProfile(profileId) + return { + active, + extensions: extensions.map(({ id, name, version, source, path, enabled, loadError }) => ({ + id, + name, + version, + source, + path, + enabled, + loadError + })) + } + }, + /** Reloads every profile where the extension is enabled, like the Settings reload button. */ + async reload(params: BrowserExtensionReloadParams): Promise { + const installed = getBrowserExtensionInstallStore().list() + const extensionId = + params.path !== undefined + ? await extensionIdForSourceFolder(params.path, installed) + : params.extensionId + const extension = installed.find((record) => record.id === extensionId) + if (!extension) { + throw new BrowserError('invalid_argument', 'Extension is not installed') + } + if (extension.source !== 'unpacked') { + throw new BrowserError('invalid_argument', 'Only unpacked extensions can be reloaded') + } + const result = await reloadUnpackedBrowserExtension(extension.id) + if (!result.ok) { + throw new BrowserError('runtime_error', result.error) + } + return { extensionId: extension.id } + } +} diff --git a/src/main/browser/extensions/browser-extension-management.ts b/src/main/browser/extensions/browser-extension-management.ts index dbd4c9e178d..fda54bc4fde 100644 --- a/src/main/browser/extensions/browser-extension-management.ts +++ b/src/main/browser/extensions/browser-extension-management.ts @@ -58,6 +58,7 @@ async function settle(action: () => Promise): Promise { @@ -72,6 +73,7 @@ export async function listBrowserExtensionsForProfile( name: extension.name, version: extension.version, source: extension.source, + path: extension.path, enabled: enabled.has(extension.id), iconDataUrl: await readBrowserExtensionIconDataUrl(extension.path, extension.iconPath), hasOptionsPage: extension.optionsPage !== null, @@ -182,6 +184,8 @@ export function reloadUnpackedBrowserExtension( } store.save(record, true) await reloadBrowserExtensionWhereEnabled(record, { refresh: true }) + // Why: a CLI reload can change name, version or options page while Settings is open. + notifyBrowserExtensionListChanged() }) ) } diff --git a/src/main/host/electron-browser-commands.ts b/src/main/host/electron-browser-commands.ts index ae3a0858879..5bb39288dcb 100644 --- a/src/main/host/electron-browser-commands.ts +++ b/src/main/host/electron-browser-commands.ts @@ -1,3 +1,4 @@ +import { desktopBrowserExtensions } from '../browser/extensions/browser-extension-commands' import { RuntimeBrowserCommands } from '../runtime/browser/commands/browser-commands' import { setRuntimeBrowserCommandsFactory } from '../runtime/browser/installation/runtime-browser-commands-factory' import { setBrowserHostClientProfileResolver } from '../runtime/browser-host/profiles/browser-host-client-profile-metadata' @@ -9,7 +10,8 @@ import { deleteManagedBrowserProfile } from '../browser/sessions/profiles/browse export function installElectronBrowserCommands(): void { setRuntimeBrowserCommandsFactory((host) => new RuntimeBrowserCommands(host), { supportsGoogleCookieClear: () => true, - clientHosting: true + clientHosting: true, + extensions: desktopBrowserExtensions }) setBrowserHostClientProfileResolver((browserProfileId) => { const profile = browserSessionRegistry.getProfile(browserProfileId) diff --git a/src/main/runtime/browser/installation/runtime-browser-commands-factory.ts b/src/main/runtime/browser/installation/runtime-browser-commands-factory.ts index 610c7811ad8..addff04997f 100644 --- a/src/main/runtime/browser/installation/runtime-browser-commands-factory.ts +++ b/src/main/runtime/browser/installation/runtime-browser-commands-factory.ts @@ -1,3 +1,9 @@ +import type { + BrowserExtensionListParams, + BrowserExtensionReloadParams, + BrowserExtensionReloadResult, + BrowserExtensionSummaryListResult +} from '../../../../shared/browser/extensions/browser-extension-types' import { BrowserError } from '../../../browser/browser-error' import '../../../../shared/runtime-types' import { BROWSER_UNAVAILABLE_ERROR_CODE } from '../../../../shared/runtime-session-contracts' @@ -16,6 +22,11 @@ export type RuntimeBrowserCommandsFactory = ( export type RuntimeBrowserCommandsFactoryOptions = { /** The provider creates pages against this runtime's client-host lease registry. */ clientHosting?: boolean + /** Only the desktop installs access to its local extension manager. */ + extensions?: { + list(params: BrowserExtensionListParams): Promise + reload(params: BrowserExtensionReloadParams): Promise + } supportsGoogleCookieClear?: () => boolean /** Client pages live in this runtime's own registries, so they need no healthy engine. */ clientHostingWithoutEngine?: boolean @@ -117,3 +128,20 @@ export function browserUnavailableError(): BrowserError { 'Browser automation is unavailable on this host.' ) } + +/** Requires an available desktop extension provider; headless and remote browser providers are unsupported. */ +export function getRuntimeBrowserExtensions(): NonNullable< + RuntimeBrowserCommandsFactoryOptions['extensions'] +> { + if ( + !runtimeBrowserCommandsFactoryIsAvailable() || + currentOptions.headless || + !currentOptions.extensions + ) { + throw new BrowserError( + 'browser_unavailable', + 'Browser extension management requires the Orca desktop on this host; it is unavailable in headless or remote browser providers.' + ) + } + return currentOptions.extensions +} diff --git a/src/main/runtime/rpc/methods/browser/browser-core.ts b/src/main/runtime/rpc/methods/browser/browser-core.ts index 9e4ab2379e3..d9d6d14ed1f 100644 --- a/src/main/runtime/rpc/methods/browser/browser-core.ts +++ b/src/main/runtime/rpc/methods/browser/browser-core.ts @@ -1,3 +1,4 @@ +import { BROWSER_EXTENSION_METHODS } from './browser-extension-rpc-methods' import { defineMethod } from '../../core' import { BrowserTarget, @@ -145,6 +146,7 @@ export const BROWSER_CORE_METHODS = [ handler: async (params, { runtime }) => runtime.browserTabClose(params) }), ...BROWSER_PROFILE_METHODS, + ...BROWSER_EXTENSION_METHODS, defineMethod({ name: 'browser.hover', permission: 'workspace', diff --git a/src/main/runtime/rpc/methods/browser/browser-extension-rpc-methods.ts b/src/main/runtime/rpc/methods/browser/browser-extension-rpc-methods.ts new file mode 100644 index 00000000000..0ccb61c2dca --- /dev/null +++ b/src/main/runtime/rpc/methods/browser/browser-extension-rpc-methods.ts @@ -0,0 +1,21 @@ +import { defineMethod } from '../../core' +import { + ExtensionList, + ExtensionReload +} from '../../../../../shared/rpc-contract/browser-extension-params' +import { getRuntimeBrowserExtensions } from '../../../browser/installation/runtime-browser-commands-factory' + +export const BROWSER_EXTENSION_METHODS = [ + defineMethod({ + name: 'browser.extensionList', + permission: 'workspace', + params: ExtensionList, + handler: async (params) => getRuntimeBrowserExtensions().list(params) + }), + defineMethod({ + name: 'browser.extensionReload', + permission: 'workspace', + params: ExtensionReload, + handler: async (params) => getRuntimeBrowserExtensions().reload(params) + }) +] diff --git a/src/main/startup/cli-command-names.ts b/src/main/startup/cli-command-names.ts index 867e15b0fa0..9a09163faf7 100644 --- a/src/main/startup/cli-command-names.ts +++ b/src/main/startup/cli-command-names.ts @@ -28,6 +28,7 @@ export const CLI_COMMAND_NAMES = [ 'environment', 'eval', 'exec', + 'extension', 'file', 'fill', 'find', diff --git a/src/shared/browser/extensions/browser-extension-types.ts b/src/shared/browser/extensions/browser-extension-types.ts index 06bf127eafe..92c4b64d037 100644 --- a/src/shared/browser/extensions/browser-extension-types.ts +++ b/src/shared/browser/extensions/browser-extension-types.ts @@ -25,6 +25,7 @@ export type BrowserExtensionListing = { name: string version: string source: BrowserExtensionSource + path: string enabled: boolean /** Why data URL: the renderer CSP does not load chrome-extension: images. */ iconDataUrl: string | null @@ -46,3 +47,16 @@ export type BrowserExtensionActionResult = { ok: true } | { ok: false; error: st /** Main → trusted renderer: the installed list, a load error or a held update changed; Settings refetches. */ export const BROWSER_EXTENSIONS_CHANGED_CHANNEL = 'browserExtensions:changed' + +export type BrowserExtensionListParams = { profileId?: string } +/** What the CLI lists: no icon data or permission lists, which would bloat agent context. */ +export type BrowserExtensionSummary = Pick< + BrowserExtensionListing, + 'id' | 'name' | 'version' | 'source' | 'path' | 'enabled' | 'loadError' +> +export type BrowserExtensionSummaryListResult = { + active: boolean + extensions: BrowserExtensionSummary[] +} +export type BrowserExtensionReloadParams = { extensionId?: string; path?: string } +export type BrowserExtensionReloadResult = { extensionId: string } diff --git a/src/shared/orca-browser-cli-commands.ts b/src/shared/orca-browser-cli-commands.ts index 1cae56cadcb..24b933a00e5 100644 --- a/src/shared/orca-browser-cli-commands.ts +++ b/src/shared/orca-browser-cli-commands.ts @@ -3,6 +3,7 @@ import { orcaCliInvocations } from './orca-cli-invocations' /** The browser CLI's command keys per handler group. Kept apart from the CLI's * lazy handler imports so the renderer and mobile can read them too. */ export const ORCA_BROWSER_CLI_COMMAND_KEYS = { + 'browser-extension': ['extension list', 'extension reload'], 'browser-identity': ['browser identity get', 'browser identity set'], 'browser-nav': [ 'snapshot', diff --git a/src/shared/rpc-contract/browser-extension-params.ts b/src/shared/rpc-contract/browser-extension-params.ts new file mode 100644 index 00000000000..3e03d82aefe --- /dev/null +++ b/src/shared/rpc-contract/browser-extension-params.ts @@ -0,0 +1,12 @@ +import { z } from 'zod' +import { isChromeExtensionId } from '../browser/extensions/browser-extension-url' + +export const ExtensionList = z.object({ profileId: z.string().min(1).optional() }) +export const ExtensionReload = z + .object({ + extensionId: z.string().refine(isChromeExtensionId, 'Invalid Chrome extension ID').optional(), + path: z.string().min(1).optional() + }) + .refine(({ extensionId, path }) => (extensionId === undefined) !== (path === undefined), { + message: 'Pass either an extension ID or --path ' + }) diff --git a/src/shared/rpc-contract/rpc-params-catalog.generated.ts b/src/shared/rpc-contract/rpc-params-catalog.generated.ts index 761f3ca6eb2..3ae0db066f2 100644 --- a/src/shared/rpc-contract/rpc-params-catalog.generated.ts +++ b/src/shared/rpc-contract/rpc-params-catalog.generated.ts @@ -103,6 +103,7 @@ import { AutomationUpdate } from './automation-params' import { CertificateProceed } from './browser-core-params' +import { ExtensionList, ExtensionReload } from './browser-extension-params' import { MouseClick } from './browser-extras-params' import { BrowserIdentitySet, ProfileCreate } from './browser-identity-params' import { Screenshot, Snapshot } from './browser-observation-params' @@ -917,6 +918,8 @@ export const RPC_PARAMS_BY_METHOD = { 'browser.drag': Drag, 'browser.eval': Eval, 'browser.exec': Exec, + 'browser.extensionList': ExtensionList, + 'browser.extensionReload': ExtensionReload, 'browser.fill': Fill, 'browser.find': Find, 'browser.focus': Element, From 49a2163e072096071a6b236b498286e6a7c4fb60 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:08:34 +0000 Subject: [PATCH 27/80] feat(viewer): fit PDF previews to pane width (#39966) * feat(viewer): fit PDF previews to pane width - Apply the saved scale on pdf.js pagesinit; a 'page-width' preset set before page views exist was dropped, so PDFs opened at 100%. - Refit 'page-width' when the viewer container width changes. - Ctrl+wheel / pinch zoom stops on the fit-width scale when crossing it. * fix(viewer): refit PDF page width once a resize settles - Refit after the container stops resizing (150 ms) instead of every frame of a split drag; each refit re-renders the visible pages. - Drop the fitted-width cache: Fit to width, the wheel stop and pagesinit fit without updating it, so returning to an old width skipped the refit. pdf.js already no-ops a same-scale refit. - Observe the border box so a scrollbar the refit toggles can't loop. - Apply the latest scale preference on pagesinit, so a zoom made while the first page loads isn't undone. - Record the wheel stop's 'page-width' before update() can move the current page, and read the fit scale only for zoom wheels. * test(viewer): cover PDF wheel zoom stopping at fit width * fix(viewer): apply the PDF wheel stop as the page-width preset --------- Co-authored-by: stmoon --- .../src/components/editor/PdfViewer.tsx | 2 +- .../editor/pdf-scale-preference.test.ts | 32 ++++++++++++ .../components/editor/pdf-scale-preference.ts | 47 ++++++++++++++---- .../components/editor/pdf-viewer-session.ts | 49 ++++++++++++++++--- 4 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 src/renderer/src/components/editor/pdf-scale-preference.test.ts diff --git a/src/renderer/src/components/editor/PdfViewer.tsx b/src/renderer/src/components/editor/PdfViewer.tsx index d9c0118e538..36da94c441a 100644 --- a/src/renderer/src/components/editor/PdfViewer.tsx +++ b/src/renderer/src/components/editor/PdfViewer.tsx @@ -113,7 +113,7 @@ function PdfDocumentViewer({ viewerDiv, doc, scrollCacheKey, - scalePreference: scalePreferenceRef.current, + getScalePreference: () => scalePreferenceRef.current, scaleBounds: SCALE_BOUNDS, onScaleChanging: setScale, onWheelZoom: recordScalePreference diff --git a/src/renderer/src/components/editor/pdf-scale-preference.test.ts b/src/renderer/src/components/editor/pdf-scale-preference.test.ts new file mode 100644 index 00000000000..75ff14ba4c1 --- /dev/null +++ b/src/renderer/src/components/editor/pdf-scale-preference.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from 'vitest' +import { zoomPdfViewerWithWheel } from './pdf-scale-preference' + +function fakeViewer(currentScale: number) { + return { + currentScale, + currentScaleValue: '', + container: { scrollLeft: 0, scrollTop: 0, getBoundingClientRect: () => ({ left: 0, top: 0 }) }, + update: () => {} + } +} + +const zoomInEvent = { + ctrlKey: true, + deltaY: -100, + deltaMode: 0, + clientX: 0, + clientY: 0, + preventDefault: () => {} +} +const bounds = { min: 0.25, max: 5 } + +describe('zoomPdfViewerWithWheel', () => { + it('stops on the fit-width scale when a step would pass it, then leaves it', () => { + const below = fakeViewer(1) + expect(zoomPdfViewerWithWheel(below, zoomInEvent, bounds, () => 1.5)).toBe('page-width') + expect(below.currentScaleValue).toBe('page-width') + + const atFit = fakeViewer(1.5) + expect(zoomPdfViewerWithWheel(atFit, zoomInEvent, bounds, () => 1.5)).toBeGreaterThan(1.5) + }) +}) diff --git a/src/renderer/src/components/editor/pdf-scale-preference.ts b/src/renderer/src/components/editor/pdf-scale-preference.ts index 3eabad2dfac..546b55b6e2f 100644 --- a/src/renderer/src/components/editor/pdf-scale-preference.ts +++ b/src/renderer/src/components/editor/pdf-scale-preference.ts @@ -30,6 +30,22 @@ export function stepPdfScalePreference( return { scale: next, preference: next } } +// Half a percent; a scale this close to fit-width already is fit-width. +const FIT_STOP_MARGIN = 0.005 + +/** A zoom that would pass over fit-width stops on it; the next one leaves it. */ +function stopAtPdfFitScale(previous: number, next: number, fitScale: number | null): number { + if (fitScale === null) { + return next + } + const before = previous / fitScale - 1 + const after = next / fitScale - 1 + const crosses = + (before < -FIT_STOP_MARGIN && after > -FIT_STOP_MARGIN) || + (before > FIT_STOP_MARGIN && after < FIT_STOP_MARGIN) + return crosses ? fitScale : next +} + type PdfWheelZoomEvent = Pick< WheelEvent, 'ctrlKey' | 'deltaY' | 'deltaMode' | 'clientX' | 'clientY' | 'preventDefault' @@ -39,6 +55,7 @@ type PdfWheelZoomEvent = Pick< export function zoomPdfViewerWithWheel( viewer: { currentScale: number + currentScaleValue: string container: { scrollLeft: number scrollTop: number @@ -47,30 +64,42 @@ export function zoomPdfViewerWithWheel( update: () => void }, event: PdfWheelZoomEvent, - bounds: { min: number; max: number } -): number | null { + bounds: { min: number; max: number }, + getFitScale: () => number | null +): PdfScalePreference | null { if (!shouldHandleImageZoomWheel(event)) { return null } event.preventDefault() const previous = viewer.currentScale - const next = clampPdfScale( - previous * getPinchZoomFactor(event.deltaY, event.deltaMode), - bounds.min, - bounds.max + const fitScale = getFitScale() + const next = stopAtPdfFitScale( + previous, + clampPdfScale( + previous * getPinchZoomFactor(event.deltaY, event.deltaMode), + bounds.min, + bounds.max + ), + fitScale ) if (next === previous) { return null } const { container } = viewer const rect = container.getBoundingClientRect() - // Why: an absolute scale avoids updateScale's 0.01 rounding, which swallows slow pinches. - viewer.currentScale = next + const fitted = next === fitScale + if (fitted) { + // Why: the preset is what resizes refit to; a numeric set first could move pdf.js to a page of another width. + viewer.currentScaleValue = 'page-width' + } else { + // Why: an absolute scale avoids updateScale's 0.01 rounding, which swallows slow pinches. + viewer.currentScale = next + } // Why: pdf.js keeps the viewport's top-left fixed, so shift by the pointer's grown offset. const growth = next / previous - 1 container.scrollLeft += (event.clientX - rect.left) * growth container.scrollTop += (event.clientY - rect.top) * growth // Why: pdf.js re-reads its location on the next frame; a faster pinch event would anchor to the old one. viewer.update() - return next + return fitted ? 'page-width' : next } diff --git a/src/renderer/src/components/editor/pdf-viewer-session.ts b/src/renderer/src/components/editor/pdf-viewer-session.ts index 52b14b3eefe..ce439e92ac3 100644 --- a/src/renderer/src/components/editor/pdf-viewer-session.ts +++ b/src/renderer/src/components/editor/pdf-viewer-session.ts @@ -17,6 +17,8 @@ import { createPdfViewPositionRecorder } from './pdf-view-position' +const PAGE_WIDTH_REFIT_SETTLE_MS = 150 + const USER_SCROLL_INPUT_EVENTS = ['wheel', 'touchstart', 'keydown', 'pointerdown'] as const function watchUserScrollInput(container: HTMLElement, onInput: () => void): () => void { @@ -35,7 +37,7 @@ export function createPdfViewerSession({ viewerDiv, doc, scrollCacheKey, - scalePreference, + getScalePreference, scaleBounds, onScaleChanging, onWheelZoom @@ -44,10 +46,10 @@ export function createPdfViewerSession({ viewerDiv: HTMLDivElement doc: pdfjsLib.PDFDocumentProxy scrollCacheKey: string | null - scalePreference: PdfScalePreference + getScalePreference: () => PdfScalePreference scaleBounds: { min: number; max: number } onScaleChanging: (scale: number) => void - onWheelZoom: (scale: number) => void + onWheelZoom: (scale: PdfScalePreference) => void }): { viewer: InstanceType eventBus: InstanceType @@ -87,9 +89,13 @@ export function createPdfViewerSession({ container.addEventListener( 'wheel', (event) => { - const scale = zoomPdfViewerWithWheel(viewer, event, scaleBounds) - if (scale !== null) { - onWheelZoom(scale) + const preference = zoomPdfViewerWithWheel(viewer, event, scaleBounds, () => { + const page = viewer.getPageView(viewer.currentPageNumber - 1) + // Why: pdf.js's 'page-width' scale, given removePageBorders (no padding) and no spreads. + return page ? (container.clientWidth / page.width) * page.scale : null + }) + if (preference !== null) { + onWheelZoom(preference) } }, { passive: false, signal: abortController.signal } @@ -117,6 +123,8 @@ export function createPdfViewerSession({ } const handlePagesInit = (): void => { + // Why: pdf.js drops a preset scale set before its page views exist, leaving the PDF at 100%. + applyPdfScalePreference(viewer, getScalePreference(), scaleBounds) const cached = scrollCacheKey ? pdfViewPositionCache.get(scrollCacheKey) : undefined const clamped = cached ? clampPdfViewPosition(cached, viewer.pagesCount) : null if (!clamped) { @@ -173,6 +181,29 @@ export function createPdfViewerSession({ recorder?.arm() } + // Why: pdf.js computes 'page-width' once; split resizes and maximize must refit it. + let refitTimer: ReturnType | null = null + const refitPageWidth = (): void => { + refitTimer = null + // Why: a hidden pane measures zero wide and would fit the pages to nothing. + if ( + container.clientWidth > 0 && + viewer.pagesCount > 0 && + viewer.currentScaleValue === 'page-width' + ) { + viewer.currentScaleValue = 'page-width' + } + } + const fitWidthObserver = new ResizeObserver(() => { + // Why: each refit re-renders the visible pages, so wait for a split drag to settle. + if (refitTimer !== null) { + clearTimeout(refitTimer) + } + refitTimer = setTimeout(refitPageWidth, PAGE_WIDTH_REFIT_SETTLE_MS) + }) + // Why: border-box ignores the scrollbar a refit can toggle, which would refit again in a loop. + fitWidthObserver.observe(container, { box: 'border-box' }) + eventBus.on('pagesinit', handlePagesInit) eventBus.on('pagesloaded', handlePagesLoaded) eventBus.on('updateviewarea', handleUpdateViewArea) @@ -187,6 +218,10 @@ export function createPdfViewerSession({ // Why: teardown events must not overwrite the reader's final position. detachInputWatcher?.() disconnectVisibilityObserver() + fitWidthObserver.disconnect() + if (refitTimer !== null) { + clearTimeout(refitTimer) + } recorder?.dispose() eventBus.off('pagesinit', handlePagesInit) eventBus.off('pagesloaded', handlePagesLoaded) @@ -208,7 +243,7 @@ export function createPdfViewerSession({ viewer.setDocument(doc) linkService.setDocument(doc) findController.setDocument(doc) - applyPdfScalePreference(viewer, scalePreference, scaleBounds) + applyPdfScalePreference(viewer, getScalePreference(), scaleBounds) } catch (error) { dispose() throw error From a44cb9ac3ed19baff5413dbe0ff43c88f7bfc7dc Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:14:05 +0000 Subject: [PATCH 28/80] perf(mobile): prewarm one page while Home is shown so opening a page skips the WebView start (#40018) * perf(mobile): answer a page's ready that arrived before its bridge host A page's ready can reach the shell before the screen has built its bridge host (the host waits on the client and the host profile read). It used to be dropped, so init waited for the page's next retry, up to 2 s later. Keep the latest ready per session and answer it once the host exists. * perf(mobile): keep one prewarmed page document while Home is shown While native Home is on screen, boot one page document for the most recently connected host with a cached build, parked invisibly in the window. Opening a page from that build adopts it instead of building a WebView, which removes the WebView start and script parse from the tap. The spare is dropped when Home is left without a claim (2 s grace), on background, memory pressure, its own failure and before the cache deletes its files. It is never kept beside a shown page, and only phones with more than 3 GiB of RAM (4 GB phones) get one. A parked page is told to stop asking for init; the adopting view answers the ready it sent. Builds on Jinwoo's spare pool and replay work in #32203 and #32204. Co-authored-by: Jinwoo-H <73622457+Jinwoo-H@users.noreply.github.com> * fix(mobile): unregister the shell's memory callbacks from the context that registered them --------- Co-authored-by: Jinwoo-H <73622457+Jinwoo-H@users.noreply.github.com> --- .../MobileWebShellBridge.kt | 3 + .../MobileWebShellDocument.kt | 10 +- .../MobileWebShellLoadState.kt | 1 + .../MobileWebShellSparePool.kt | 169 ++++++++++++++++++ .../orcamobilewebshell/MobileWebShellView.kt | 30 ++-- .../OrcaMobileWebShellModule.kt | 58 ++++++ .../ios/MobileWebShellBridge.swift | 3 + .../ios/MobileWebShellDocument.swift | 22 ++- .../ios/MobileWebShellLoadState.swift | 2 +- .../ios/MobileWebShellSparePool.swift | 150 ++++++++++++++++ .../ios/MobileWebShellView.swift | 22 ++- .../ios/OrcaMobileWebShellModule.swift | 41 +++++ .../src/spare-document.ts | 27 +++ mobile/src/home/MobileHomeScreen.tsx | 2 + mobile/src/home/use-home-spare-page.ts | 48 +++++ .../bridge/bridge-client-inbound-frames.ts | 4 + .../bridge/bridge-envelope.ts | 2 + .../bridge/bridge-rpc-client.ts | 2 + .../generation-host-directories.ts | 27 +++ .../mobile-web-shell/generation-store.test.ts | 18 ++ .../src/mobile-web-shell/generation-store.ts | 37 ++-- .../src/mobile-web-shell/home-spare-page.ts | 91 ++++++++++ ...e-web-shell-failed-update-fallback.test.ts | 1 + .../mobile-web-shell-runtime.ts | 12 +- .../mobile-web-shell-session-effects.test.ts | 1 + .../mobile-web-shell-session-effects.ts | 7 +- .../page-frames-without-host.ts | 40 +++++ .../process-generation-store.ts | 6 +- .../src/mobile-web-shell/shell-session-id.ts | 9 + .../use-mobile-web-shell-bridge.test.ts | 31 +++- .../use-mobile-web-shell-bridge.ts | 35 ++-- .../use-mobile-web-shell-session.test.ts | 18 +- .../src/test-support/spare-document-node.ts | 4 + mobile/vitest.config.ts | 6 + 34 files changed, 865 insertions(+), 74 deletions(-) create mode 100644 mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellSparePool.kt create mode 100644 mobile/modules/orca-mobile-web-shell/ios/MobileWebShellSparePool.swift create mode 100644 mobile/modules/orca-mobile-web-shell/src/spare-document.ts create mode 100644 mobile/src/home/use-home-spare-page.ts create mode 100644 mobile/src/mobile-web-shell/generation-host-directories.ts create mode 100644 mobile/src/mobile-web-shell/home-spare-page.ts create mode 100644 mobile/src/mobile-web-shell/page-frames-without-host.ts create mode 100644 mobile/src/mobile-web-shell/shell-session-id.ts create mode 100644 mobile/src/test-support/spare-document-node.ts diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellBridge.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellBridge.kt index d3df9fffb59..045cce54908 100644 --- a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellBridge.kt +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellBridge.kt @@ -3,6 +3,9 @@ package expo.modules.orcamobilewebshell /** Shared page/native bridge contract; framework-free for off-device checks. */ internal const val MOBILE_WEB_SHELL_BRIDGE_OBJECT = "orcaBridge" +/** Tells a page booted ahead of its screen to stop asking for init; the screen that adopts it answers. */ +internal const val MOBILE_WEB_SHELL_PARKED_FRAME = """{"v":1,"type":"parked"}""" + /** Enforce the shared UTF-8 JSON byte ceiling natively, before parsing. */ internal const val MOBILE_WEB_SHELL_BRIDGE_MAX_MESSAGE_BYTES = 640 * 1024 diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt index 8292fb9ca2c..af1aff6a343 100644 --- a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellDocument.kt @@ -33,7 +33,7 @@ private class MobileWebShellServed( val originHost: String ) -/** Where a document's reports go: the view showing it. */ +/** Where a document's reports go: the view showing it, or the spare pool while it is parked. */ internal interface MobileWebShellDocumentSink { fun documentLoadState(payload: Map) fun documentBridgeMessage(json: String) @@ -58,6 +58,9 @@ internal class MobileWebShellDocument(private val context: Context) { @Volatile var webView: WebView? = createWebView() private set + /** Still showable: not torn down, and no terminal load failure or renderer loss. */ + val isUsable: Boolean get() = webView != null && !loadState.isTerminal + /** Apply unordered props together; identical triples do not retry. Recovery requires a remount. */ fun apply(generationDirectory: String, sessionId: String, bridgeEnabled: Boolean) { val next = MobileWebShellAppliedProps(generationDirectory, sessionId, bridgeEnabled) @@ -169,6 +172,11 @@ internal class MobileWebShellDocument(private val context: Context) { proxy.postMessage(json) } + /** A frame from the shell itself, to the frame that spoke. */ + fun postHostFrame(json: String) { + replyProxy?.postMessage(json) + } + /** The view's attach and detach: off-window, the page reads hidden, as a buried WKWebView does on iOS. */ fun resume() { webView?.onResume() diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellLoadState.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellLoadState.kt index 493e50b9b5b..93737f6dd66 100644 --- a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellLoadState.kt +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellLoadState.kt @@ -26,6 +26,7 @@ internal data class MobileWebShellLoadEmission( /** Failure is terminal and duplicate reports are dropped: late error-document/rule-list callbacks must never restore ready. Cross-thread readers require volatile state. */ internal class MobileWebShellLoadStateMachine { private var terminal = false + val isTerminal: Boolean get() = terminal private var last: MobileWebShellLoadEmission? = null private var rendererLoss: Pair? = null diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellSparePool.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellSparePool.kt new file mode 100644 index 00000000000..791dbba21cf --- /dev/null +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellSparePool.kt @@ -0,0 +1,169 @@ +package expo.modules.orcamobilewebshell + +import android.app.Activity +import android.app.ActivityManager +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import java.util.Collections +import java.util.WeakHashMap + +/** More than 3 GiB can only come from a phone with 4 GB or more; those report about 3.5 to 3.9 GiB. */ +private const val MINIMUM_TOTAL_MEMORY_BYTES = 3L shl 30 + +/** A claim is adopted on the claiming screen's first commit; this only bounds a screen that never mounts. */ +private const val CLAIM_EXPIRY_MS = 10_000L + +/** Holds a parked WebView under the app's content: invisible, untouchable and out of accessibility. */ +private class MobileWebShellParking(context: Context) : FrameLayout(context) { + init { + alpha = 0f + isFocusable = false + descendantFocusability = ViewGroup.FOCUS_BLOCK_DESCENDANTS + importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS + } + + override fun dispatchTouchEvent(event: MotionEvent): Boolean = false +} + +/** + * A booted document waiting for a screen. It keeps what the page reported, and the page's ready, for + * the view that adopts it: a parked page stops asking for init, so its ready is answered from here. + */ +internal class MobileWebShellSpare( + activity: Activity, + val directory: String, + val sessionId: String +) : MobileWebShellDocumentSink { + val document = MobileWebShellDocument(activity) + val parking: ViewGroup = MobileWebShellParking(activity) + private val loadStates = mutableListOf>() + private var ready: String? = null + + init { + document.sink = this + } + + override fun documentLoadState(payload: Map) { + loadStates += payload + if (payload["state"] == "failed") MobileWebShellSparePool.discard(this) + } + + /** Before init a page only sends ready, from its first document or one it reloaded. */ + override fun documentBridgeMessage(json: String) { + ready = json + document.postHostFrame(MOBILE_WEB_SHELL_PARKED_FRAME) + } + + // A parked page has no screen to have been tapped on. + override fun documentExternalNavigation(url: String) = Unit + + /** The adopting view sees what a fresh document would have reported, ready last. */ + fun handOver(sink: MobileWebShellDocumentSink) { + document.sink = sink + loadStates.forEach(sink::documentLoadState) + ready?.let(sink::documentBridgeMessage) + } + + fun destroy() { + document.destroyWebView() + (parking.parent as? ViewGroup)?.removeView(parking) + } +} + +/** + * At most one document booted ahead of its screen, only while no page document is shown. Main thread only. + * Parked attached under the content, not detached: a detached WebView took 0.3–1.9 s to paint after 30 s, vs 0.2 s. + */ +internal object MobileWebShellSparePool { + private val main = Handler(Looper.getMainLooper()) + private var parked: MobileWebShellSpare? = null + private var claimed: MobileWebShellSpare? = null + private val shown: MutableSet = + Collections.newSetFromMap(WeakHashMap()) + + /** Whether a spare for [directory] is parked after the call; never on a small phone or beside a page. */ + fun prepare(activity: Activity, directory: String, sessionId: String): Boolean { + if (!inForeground() || cannotAffordSpare(activity) || shown.any { it.webView != null }) return false + val current = parked + if (current != null && current.directory == directory && current.document.isUsable) return true + val decor = activity.window?.decorView as? ViewGroup ?: return false + // A claim no view adopted was abandoned; it must not outlive the spare that replaces it. + discardAll() + val spare = MobileWebShellSpare(activity, directory, sessionId) + val webView = spare.document.webView ?: return false + spare.parking.addView(webView, FrameLayout.LayoutParams(MATCH, MATCH)) + decor.addView(spare.parking, 0, FrameLayout.LayoutParams(MATCH, MATCH)) + parked = spare + spare.document.apply(directory, sessionId, bridgeEnabled = true) + // A load that failed at once has already discarded it. + return parked === spare + } + + /** The parked spare's session id for a screen opening [directory], handed out once. */ + fun claim(directory: String): String? { + val spare = parked ?: return null + if (spare.directory != directory || !spare.document.isUsable) return null + parked = null + claimed = spare + main.postDelayed({ if (claimed === spare) discard(spare) }, CLAIM_EXPIRY_MS) + return spare.sessionId + } + + /** The claimed spare when [sessionId] names it, else a fresh document, which leaves no room for a spare. */ + fun document( + context: Context, + sessionId: String, + sink: MobileWebShellDocumentSink + ): MobileWebShellDocument { + val spare = claimed + val document = if (spare != null && spare.sessionId == sessionId && spare.document.isUsable) { + claimed = null + spare.document.webView?.let { spare.parking.removeView(it) } + (spare.parking.parent as? ViewGroup)?.removeView(spare.parking) + spare.handOver(sink) + spare.document + } else { + discardAll() + MobileWebShellDocument(context).also { it.sink = sink } + } + shown += document + return document + } + + /** Memory pressure, the app leaving the foreground, files going away, or Home without a claim. */ + fun discardAll() { + listOfNotNull(parked, claimed).forEach(MobileWebShellSpare::destroy) + parked = null + claimed = null + } + + fun discard(spare: MobileWebShellSpare) { + when { + parked === spare -> parked = null + claimed === spare -> claimed = null + else -> return + } + spare.destroy() + } + + /** React Native keeps its activity after it stops, so ask the process's own importance. */ + private fun inForeground(): Boolean { + val state = ActivityManager.RunningAppProcessInfo() + ActivityManager.getMyMemoryState(state) + return state.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND + } + + private fun cannotAffordSpare(activity: Activity): Boolean { + val manager = activity.getSystemService(ActivityManager::class.java) ?: return true + val memory = ActivityManager.MemoryInfo().also(manager::getMemoryInfo) + // Android 14 stopped sending RUNNING_LOW trims to foreground apps, so check pressure before booting. + return manager.isLowRamDevice || memory.totalMem <= MINIMUM_TOTAL_MEMORY_BYTES || memory.lowMemory + } +} + +private const val MATCH = ViewGroup.LayoutParams.MATCH_PARENT diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt index 6e86c878f4e..506c6e03202 100644 --- a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/MobileWebShellView.kt @@ -19,14 +19,8 @@ internal class OrcaMobileWebShellView( private var generationDirectory = "" private var sessionId = "" private var bridgeEnabled = false - private val document = MobileWebShellDocument(context) - - init { - document.sink = this - addView(document.webView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)) - // Why: Chromium reports an unpaused, never-attached WebView as visible, and a screen pushed under another never attaches. - document.pause() - } + /** Taken on the first prop commit, not in init: the session id says whether a spare was booted for it. */ + private var document: MobileWebShellDocument? = null fun setGenerationDirectory(value: String) { generationDirectory = value @@ -41,27 +35,37 @@ internal class OrcaMobileWebShellView( } fun propsDidUpdate() { - document.apply(generationDirectory, sessionId, bridgeEnabled) + val current = document ?: hostDocument().also { document = it } + current.apply(generationDirectory, sessionId, bridgeEnabled) + } + + private fun hostDocument(): MobileWebShellDocument { + val document = MobileWebShellSparePool.document(context, sessionId, this) + addView(document.webView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)) + // Why: Chromium reports an unpaused, never-attached WebView as visible, and a screen pushed under another never attaches. + if (!isAttachedToWindow) document.pause() + return document } fun postBridgeMessage(json: String) { - document.postBridgeMessage(json) + val current = document ?: throw MobileWebShellBridgeUnavailableException() + current.postBridgeMessage(json) } override fun onAttachedToWindow() { super.onAttachedToWindow() - document.resume() + document?.resume() } // Stay paused whenever off-window so the page reads hidden, as a buried WKWebView does on iOS. override fun onDetachedFromWindow() { - document.pause() + document?.pause() super.onDetachedFromWindow() } /** Expo calls this once React Native is done with the view. */ fun destroyWebView() { - document.destroyWebView() + document?.destroyWebView() } override fun documentLoadState(payload: Map) = onLoadState(payload) diff --git a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/OrcaMobileWebShellModule.kt b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/OrcaMobileWebShellModule.kt index c3d036b0416..81e5a16133a 100644 --- a/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/OrcaMobileWebShellModule.kt +++ b/mobile/modules/orca-mobile-web-shell/android/src/main/java/expo/modules/orcamobilewebshell/OrcaMobileWebShellModule.kt @@ -1,17 +1,75 @@ package expo.modules.orcamobilewebshell import android.content.ActivityNotFoundException +import android.content.ComponentCallbacks2 +import android.content.Context import android.content.Intent +import android.content.res.Configuration import android.net.Uri +import android.os.Handler +import android.os.Looper import expo.modules.kotlin.exception.CodedException import expo.modules.kotlin.exception.Exceptions +import expo.modules.kotlin.functions.Queues import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition class OrcaMobileWebShellModule : Module() { + private val main = Handler(Looper.getMainLooper()) + // Kept so teardown unregisters from the same context after React has let go of it. + private var callbacksContext: Context? = null + + // A spare is the first thing to give back: nothing is shown by it. UI_HIDDEN, above RUNNING_LOW, is + // the app leaving the foreground; an activity pause is not, since a notification tap or deep link + // pauses it on the way to the screen that would claim the spare. + private val memoryCallbacks = object : ComponentCallbacks2 { + override fun onTrimMemory(level: Int) { + if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) discardSpare() + } + + override fun onConfigurationChanged(newConfig: Configuration) = Unit + + @Deprecated("Deprecated in Java") + override fun onLowMemory() = discardSpare() + } + + // Callbacks and OnDestroy can run off the main thread, and the pool's WebViews must not. + private fun discardSpare() { + main.post { MobileWebShellSparePool.discardAll() } + } + override fun definition() = ModuleDefinition { Name("OrcaMobileWebShell") + OnCreate { + callbacksContext = appContext.reactContext?.applicationContext?.also { + it.registerComponentCallbacks(memoryCallbacks) + } + } + + OnDestroy { + callbacksContext?.unregisterComponentCallbacks(memoryCallbacks) + callbacksContext = null + discardSpare() + } + + OnActivityDestroys { + discardSpare() + } + + AsyncFunction("prepareSpareDocument") { directory: String, sessionId: String -> + val activity = appContext.currentActivity ?: return@AsyncFunction false + MobileWebShellSparePool.prepare(activity, directory, sessionId) + }.runOnQueue(Queues.MAIN) + + AsyncFunction("claimSpareDocument") { directory: String -> + MobileWebShellSparePool.claim(directory) + }.runOnQueue(Queues.MAIN) + + AsyncFunction("discardSpareDocument") { + MobileWebShellSparePool.discardAll() + }.runOnQueue(Queues.MAIN) + // Takes a FileProvider content:// URI; the grant lets the chosen viewer read Orca's cache file. AsyncFunction("presentPdf") { uri: String -> val activity = appContext.currentActivity ?: throw Exceptions.MissingActivity() diff --git a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellBridge.swift b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellBridge.swift index 6a8fb35df39..f3d6ebb5cb4 100644 --- a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellBridge.swift +++ b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellBridge.swift @@ -5,6 +5,9 @@ enum MobileWebShellBridge { /// Use the same injected global/handler name on Android and iOS. static let handlerName = "orcaBridge" + /// Tells a page booted ahead of its screen to stop asking for init; the screen that adopts it answers. + static let parkedFrame = #"{"v":1,"type":"parked"}"# + /// Enforce the shared UTF-8 JSON byte ceiling natively, before parsing. static let maxMessageByteCount = 640 * 1024 diff --git a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift index b23b2efaf75..ae372997bf4 100644 --- a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift +++ b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellDocument.swift @@ -184,7 +184,7 @@ internal final class MobileWebShellBridgeMessageTooLargeException: GenericExcept } } -/// Where a document's reports go: the view showing it. +/// Where a document's reports go: the view showing it, or the spare pool while it is parked. protocol MobileWebShellDocumentSink: AnyObject { func documentLoadState(_ payload: [String: Any]) func documentBridgeMessage(_ json: String) @@ -206,6 +206,7 @@ final class MobileWebShellDocument: NSObject, WKNavigationDelegate, WKUIDelegate private var pendingDocumentUrl: URL? private var isolationReady = false private var isolationFailed = false + private var destroyed = false private let loadState = MobileWebShellLoadStateMachine() private var keyboardPreparationPending = false private var windowObservers: [NSObjectProtocol] = [] @@ -254,6 +255,19 @@ final class MobileWebShellDocument: NSObject, WKNavigationDelegate, WKUIDelegate } } + /// Still showable: not torn down, and no terminal load failure or content-process loss. + var isUsable: Bool { !destroyed && !loadState.isTerminal } + + /// The spare pool's teardown; the content process goes with the last reference. + func destroy() { + destroyed = true + sink = nil + clearBridgeTarget() + pendingDocumentUrl = nil + webView.stopLoading() + webView.removeFromSuperview() + } + /// Apply unordered props together; identical triples do not retry (remount required). /// Include bridgeEnabled: document-start scripts require a reload to change. func apply(generationDirectory: String, sessionId: String, bridgeEnabled: Bool) { @@ -394,6 +408,12 @@ final class MobileWebShellDocument: NSObject, WKNavigationDelegate, WKUIDelegate } } + /// A frame from the shell itself, to the frame that spoke; nothing waits on its delivery. + func postHostFrame(_ json: String) { + guard let frame = bridgeTarget.frame else { return } + webView.callAsyncJavaScript(bridgeDeliver, arguments: ["m": json], in: frame, in: .page) { _ in } + } + private func installNetworkBlock(into controller: WKUserContentController) { NetworkBlockRuleList.load { [weak self] ruleList in guard let self else { return } diff --git a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellLoadState.swift b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellLoadState.swift index 5fb8544ef73..b3e1fef7005 100644 --- a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellLoadState.swift +++ b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellLoadState.swift @@ -28,7 +28,7 @@ struct MobileWebShellLoadEmission: Equatable { /// Failure is terminal and duplicate reports are dropped: late error-document/rule-list callbacks must never restore ready. final class MobileWebShellLoadStateMachine { - private var isTerminal = false + private(set) var isTerminal = false private var last: MobileWebShellLoadEmission? /// Accept messages only after the current props’ document commits; the replaced same-origin document can still speak before then. diff --git a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellSparePool.swift b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellSparePool.swift new file mode 100644 index 00000000000..92e9ff89ffc --- /dev/null +++ b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellSparePool.swift @@ -0,0 +1,150 @@ +import UIKit +import WebKit + +/// More than 3 GiB can only come from a phone with 4 GB or more; those report about 3.5 to 3.9 GiB. +private let minimumPhysicalMemory: UInt64 = 3 << 30 + +/// A claim is adopted on the claiming screen's first commit; this only bounds a screen that never mounts. +private let claimExpirySeconds: TimeInterval = 10 + +/// A booted document waiting for a screen. It keeps what the page reported, and the page's ready, for +/// the view that adopts it: a parked page stops asking for init, so its ready is answered from here. +final class MobileWebShellSpare: MobileWebShellDocumentSink { + let document = MobileWebShellDocument() + let directory: String + let sessionId: String + private var loadStates: [[String: Any]] = [] + private var ready: String? + + init(directory: String, sessionId: String) { + self.directory = directory + self.sessionId = sessionId + document.sink = self + } + + func documentLoadState(_ payload: [String: Any]) { + loadStates.append(payload) + if payload["state"] as? String == "failed" { + MobileWebShellSparePool.shared.discard(self) + } + } + + /// Before init a page only sends ready, from its first document or one it reloaded. + func documentBridgeMessage(_ json: String) { + ready = json + document.postHostFrame(MobileWebShellBridge.parkedFrame) + } + + /// A parked page has no screen to have been tapped on. + func documentExternalNavigation(_ url: String) {} + + /// The adopting view sees what a fresh document would have reported, ready last. + func handOver(to sink: MobileWebShellDocumentSink) { + document.sink = sink + loadStates.forEach(sink.documentLoadState) + if let ready { + sink.documentBridgeMessage(ready) + } + } +} + +/// At most one document booted ahead of its screen, only while no page document is shown. Main thread only. +/// Parked in the key window at alpha 0: WebKit suspends an off-window view (0.5–1.3 s to paint after 30 s, vs 0.1 s). +final class MobileWebShellSparePool { + static let shared = MobileWebShellSparePool() + + private var parked: MobileWebShellSpare? + private var claimed: MobileWebShellSpare? + private let shown = NSHashTable.weakObjects() + + /// Whether a spare for `directory` is parked after the call; never on a small phone, inactive app or shown page. + func prepare(directory: String, sessionId: String) -> Bool { + guard + ProcessInfo.processInfo.physicalMemory > minimumPhysicalMemory, + UIApplication.shared.applicationState == .active, + shown.allObjects.isEmpty, + let window = keyWindow() + else { return false } + if let parked, parked.directory == directory, parked.document.isUsable { + return true + } + // A claim no view adopted was abandoned; it must not outlive the spare that replaces it. + discardAll() + let spare = MobileWebShellSpare(directory: directory, sessionId: sessionId) + let webView = spare.document.webView + webView.alpha = 0 + webView.isUserInteractionEnabled = false + webView.accessibilityElementsHidden = true + webView.frame = window.bounds + webView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + window.insertSubview(webView, at: 0) + parked = spare + spare.document.apply(generationDirectory: directory, sessionId: sessionId, bridgeEnabled: true) + // A load that failed at once has already discarded it. + return parked === spare + } + + /// The parked spare's session id for a screen opening `directory`, handed out once. + func claim(directory: String) -> String? { + guard let spare = parked, spare.directory == directory, spare.document.isUsable else { + return nil + } + parked = nil + claimed = spare + DispatchQueue.main.asyncAfter(deadline: .now() + claimExpirySeconds) { [weak self, weak spare] in + if let self, let spare, self.claimed === spare { + self.discard(spare) + } + } + return spare.sessionId + } + + /// The claimed spare when `sessionId` names it, else a fresh document, which leaves no room for a spare. + func document(for sessionId: String, sink: MobileWebShellDocumentSink) -> MobileWebShellDocument { + let document: MobileWebShellDocument + if let spare = claimed, spare.sessionId == sessionId, spare.document.isUsable { + claimed = nil + document = spare.document + let webView = document.webView + webView.removeFromSuperview() + webView.alpha = 1 + webView.isUserInteractionEnabled = true + webView.accessibilityElementsHidden = false + webView.autoresizingMask = [] + spare.handOver(to: sink) + } else { + discardAll() + document = MobileWebShellDocument() + document.sink = sink + } + shown.add(document) + return document + } + + /// Memory pressure, the app leaving the foreground, files going away, or Home without a claim. + func discardAll() { + for spare in [parked, claimed].compactMap({ $0 }) { + spare.document.destroy() + } + parked = nil + claimed = nil + } + + fileprivate func discard(_ spare: MobileWebShellSpare) { + if parked === spare { + parked = nil + } else if claimed === spare { + claimed = nil + } else { + return + } + spare.document.destroy() + } + + private func keyWindow() -> UIWindow? { + UIApplication.shared.connectedScenes + .compactMap { $0 as? UIWindowScene } + .flatMap(\.windows) + .first(where: \.isKeyWindow) + } +} diff --git a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift index 86c45218151..91aa028d7fa 100644 --- a/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift +++ b/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellView.swift @@ -11,19 +11,27 @@ final class OrcaMobileWebShellView: ExpoView, MobileWebShellDocumentSink, // The page's own Back: the stack's swipe is off while the page claims Back, and a strip overlay would eat taps. private let backEdgePan = UIPanGestureRecognizer() - private let document = MobileWebShellDocument() + /// Taken on the first prop commit, not in init: the session id says whether a spare was booted for it. + private var document: MobileWebShellDocument? private var generationDirectory = "" private var sessionId = "" private var bridgeEnabled = false override func didMoveToWindow() { super.didMoveToWindow() - document.loadWhenIsolated() + document?.loadWhenIsolated() } required init(appContext: AppContext? = nil) { super.init(appContext: appContext) - document.sink = self + backEdgePan.isEnabled = false + backEdgePan.addTarget(self, action: #selector(handleBackEdgePan(_:))) + backEdgePan.delegate = self + addGestureRecognizer(backEdgePan) + } + + private func hostDocument() -> MobileWebShellDocument { + let document = MobileWebShellSparePool.shared.document(for: sessionId, sink: self) let webView = document.webView webView.translatesAutoresizingMaskIntoConstraints = false addSubview(webView) @@ -33,11 +41,8 @@ final class OrcaMobileWebShellView: ExpoView, MobileWebShellDocumentSink, webView.leadingAnchor.constraint(equalTo: leadingAnchor), webView.trailingAnchor.constraint(equalTo: trailingAnchor) ]) - backEdgePan.isEnabled = false - backEdgePan.addTarget(self, action: #selector(handleBackEdgePan(_:))) - backEdgePan.delegate = self - addGestureRecognizer(backEdgePan) webView.scrollView.panGestureRecognizer.require(toFail: backEdgePan) + return document } func setBackEdgeSwipe(_ value: Bool) { @@ -78,6 +83,8 @@ final class OrcaMobileWebShellView: ExpoView, MobileWebShellDocumentSink, } func propsDidUpdate() { + let document = self.document ?? hostDocument() + self.document = document document.apply( generationDirectory: generationDirectory, sessionId: sessionId, @@ -86,6 +93,7 @@ final class OrcaMobileWebShellView: ExpoView, MobileWebShellDocumentSink, } func postBridgeMessage(_ json: String, promise: Promise) throws { + guard let document else { throw MobileWebShellBridgeUnavailableException() } try document.postBridgeMessage(json, promise: promise) } diff --git a/mobile/modules/orca-mobile-web-shell/ios/OrcaMobileWebShellModule.swift b/mobile/modules/orca-mobile-web-shell/ios/OrcaMobileWebShellModule.swift index 0f3bcaa5ca7..ed5e81770d1 100644 --- a/mobile/modules/orca-mobile-web-shell/ios/OrcaMobileWebShellModule.swift +++ b/mobile/modules/orca-mobile-web-shell/ios/OrcaMobileWebShellModule.swift @@ -1,12 +1,53 @@ import ExpoModulesCore import QuickLook +import UIKit public class OrcaMobileWebShellModule: Module { private var pdfPreview: MobileWebShellPdfPreview? + private var memoryWarningObserver: NSObjectProtocol? public func definition() -> ModuleDefinition { Name("OrcaMobileWebShell") + // A spare is the first thing to give back: nothing is shown by it. + OnCreate { + memoryWarningObserver = NotificationCenter.default.addObserver( + forName: UIApplication.didReceiveMemoryWarningNotification, + object: nil, + queue: .main + ) { _ in + MobileWebShellSparePool.shared.discardAll() + } + } + + OnDestroy { + if let memoryWarningObserver { + NotificationCenter.default.removeObserver(memoryWarningObserver) + } + DispatchQueue.main.async { + MobileWebShellSparePool.shared.discardAll() + } + } + + // Home boots the next one after its next foreground idle. + OnAppEntersBackground { + DispatchQueue.main.async { + MobileWebShellSparePool.shared.discardAll() + } + } + + AsyncFunction("prepareSpareDocument") { (directory: String, sessionId: String) -> Bool in + MobileWebShellSparePool.shared.prepare(directory: directory, sessionId: sessionId) + }.runOnQueue(.main) + + AsyncFunction("claimSpareDocument") { (directory: String) -> String? in + MobileWebShellSparePool.shared.claim(directory: directory) + }.runOnQueue(.main) + + AsyncFunction("discardSpareDocument") { + MobileWebShellSparePool.shared.discardAll() + }.runOnQueue(.main) + // Home calls this once it has a paired host, so a user with nothing to open pays nothing. Function("warmUp") { // A second's delay keeps the warm-up off the launch's first frames. diff --git a/mobile/modules/orca-mobile-web-shell/src/spare-document.ts b/mobile/modules/orca-mobile-web-shell/src/spare-document.ts new file mode 100644 index 00000000000..ca9ba5c1f85 --- /dev/null +++ b/mobile/modules/orca-mobile-web-shell/src/spare-document.ts @@ -0,0 +1,27 @@ +import { requireOptionalNativeModule } from 'expo-modules-core' + +type SpareDocumentModule = { + prepareSpareDocument?: (directory: string, sessionId: string) => Promise + claimSpareDocument?: (directory: string) => Promise + discardSpareDocument?: () => Promise +} + +// A binary built before the spare existed has the module without these functions. +const spareModule = (): SpareDocumentModule | null => + requireOptionalNativeModule('OrcaMobileWebShell') + +// Boot a page from `directory` under `sessionId` and park it, invisible, until a screen claims it. +// False when native declined: a phone under 4 GB, the app not active, or a page document shown. +export async function prepareSpareDocument(directory: string, sessionId: string): Promise { + return (await spareModule()?.prepareSpareDocument?.(directory, sessionId)) ?? false +} + +// The parked spare's session id when it was booted from `directory`, handed out once. The view +// mounted under that id adopts the booted page. +export async function claimSpareDocument(directory: string): Promise { + return (await spareModule()?.claimSpareDocument?.(directory)) ?? null +} + +export async function discardSpareDocument(): Promise { + await spareModule()?.discardSpareDocument?.() +} diff --git a/mobile/src/home/MobileHomeScreen.tsx b/mobile/src/home/MobileHomeScreen.tsx index fadfbfcd5aa..11c0274f3a1 100644 --- a/mobile/src/home/MobileHomeScreen.tsx +++ b/mobile/src/home/MobileHomeScreen.tsx @@ -29,6 +29,7 @@ import { useOpenMobileHostEdit } from '../host-screen/use-open-mobile-host-edit' import { mobileConnectionPathLabel } from '../transport/connection/mobile-connection-path-label' import { newWorkspaceOverlayHref } from '../new-workspace/new-workspace-overlay-route' import { useMobileHomeData } from './use-mobile-home-data' +import { useHomeSparePage } from './use-home-spare-page' import { HOME_TABS, type HomeTabId } from './tabs/home-tabs' import { MobileHomeAgentsTab } from './tabs/MobileHomeAgentsTab' import { MobileHomeAutomationsTab } from './tabs/MobileHomeAutomationsTab' @@ -45,6 +46,7 @@ export function MobileHomeScreen() { const t = useMobileTranslation() useMobileColorScheme() const data = useMobileHomeData() + useHomeSparePage(data.hostCatalogLoaded ? data.sortedHosts.map((host) => host.id) : []) const insets = useSafeAreaInsets() const openMobileHostEdit = useOpenMobileHostEdit() const openMobileTasks = useOpenMobileTasks() diff --git a/mobile/src/home/use-home-spare-page.ts b/mobile/src/home/use-home-spare-page.ts new file mode 100644 index 00000000000..58aa55953c6 --- /dev/null +++ b/mobile/src/home/use-home-spare-page.ts @@ -0,0 +1,48 @@ +import { useFocusEffect } from 'expo-router' +import { useCallback } from 'react' +import { AppState } from 'react-native' +import { setForegroundTimer } from '../mobile-web-shell/foreground-timer' +import { + enterHomeSparePage, + leaveHomeSparePage, + prepareHomeSparePage +} from '../mobile-web-shell/home-spare-page' +import { processGenerationStore } from '../mobile-web-shell/process-generation-store' + +// Past Home's first paint and its first reads, so the boot never competes with them. +const HOME_SPARE_IDLE_MS = 1000 + +const HOST_ID_SEPARATOR = '\n' + +// While Home is shown, keep one page booted for the most recently connected host with a cached build. +// Native drops it when the app leaves the foreground, so every return to it arms again. +export function useHomeSparePage(hostIds: readonly string[]): void { + const hosts = hostIds.join(HOST_ID_SEPARATOR) + useFocusEffect( + useCallback(() => { + if (hosts === '') { + return undefined + } + enterHomeSparePage() + let cancelTimer = (): void => undefined + const arm = (): void => { + cancelTimer() + cancelTimer = setForegroundTimer( + () => void prepareHomeSparePage(processGenerationStore(), hosts.split(HOST_ID_SEPARATOR)), + HOME_SPARE_IDLE_MS + ) + } + arm() + const subscription = AppState.addEventListener('change', (state) => { + if (state === 'active') { + arm() + } + }) + return () => { + subscription.remove() + cancelTimer() + leaveHomeSparePage() + } + }, [hosts]) + ) +} diff --git a/mobile/src/mobile-web-shell/bridge/bridge-client-inbound-frames.ts b/mobile/src/mobile-web-shell/bridge/bridge-client-inbound-frames.ts index f36beea592f..57c5300af27 100644 --- a/mobile/src/mobile-web-shell/bridge/bridge-client-inbound-frames.ts +++ b/mobile/src/mobile-web-shell/bridge/bridge-client-inbound-frames.ts @@ -20,6 +20,7 @@ export type BridgeInboundFramePort = { acceptState: (connection: BridgeConnectionSnapshot) => void acceptConnectionPath: (connection: BridgeConnectionSnapshot) => void acceptBack: () => void + acceptParked: () => void acceptTerminalKey: (key: BridgeTerminalKeyId, inputOwner?: string) => void } @@ -66,6 +67,9 @@ export function createBridgeInboundFrameReader( case 'back': port.acceptBack() return + case 'parked': + port.acceptParked() + return case 'terminalKey': port.acceptTerminalKey(message.key, message.inputOwner) return diff --git a/mobile/src/mobile-web-shell/bridge/bridge-envelope.ts b/mobile/src/mobile-web-shell/bridge/bridge-envelope.ts index 2cef127bfe6..31458b9f69d 100644 --- a/mobile/src/mobile-web-shell/bridge/bridge-envelope.ts +++ b/mobile/src/mobile-web-shell/bridge/bridge-envelope.ts @@ -236,6 +236,8 @@ const BridgeHostMessageSchema = z.union([ }), // Back names no destination; only the native stack knows what screen lies beneath. z.object({ v: versionSchema, type: z.literal(BRIDGE_BACK_FRAME) }), + // Native booted this page ahead of its screen and keeps its ready for the screen that adopts it. + z.object({ v: versionSchema, type: z.literal('parked') }), // One press on the shell's key rail. Sent only to a page granted the rail, which knows the frame. z.object({ v: versionSchema, diff --git a/mobile/src/mobile-web-shell/bridge/bridge-rpc-client.ts b/mobile/src/mobile-web-shell/bridge/bridge-rpc-client.ts index 11cc2190538..e9a62282aa2 100644 --- a/mobile/src/mobile-web-shell/bridge/bridge-rpc-client.ts +++ b/mobile/src/mobile-web-shell/bridge/bridge-rpc-client.ts @@ -198,6 +198,8 @@ export function createBridgeRpcClient(options: BridgeRpcClientOptions): BridgeRp acceptState: (connection) => acceptState(cache.apply(connection)), acceptConnectionPath: (connection) => acceptState(cache.applyPath(connection)), acceptBack: back.press, + // Only a page booted before its screen, with no init yet, is parked; the shell answers its ready on adoption. + acceptParked: handshake.stop, acceptTerminalKey: (key, inputOwner) => terminalKeys.accept(key, inputOwner) }) diff --git a/mobile/src/mobile-web-shell/generation-host-directories.ts b/mobile/src/mobile-web-shell/generation-host-directories.ts new file mode 100644 index 00000000000..fa56421bd66 --- /dev/null +++ b/mobile/src/mobile-web-shell/generation-host-directories.ts @@ -0,0 +1,27 @@ +import { joinUri } from './generation-cache-uri' +import type { GenerationDirectoryEntry, GenerationFileSystem } from './generation-store-file-system' +import { isHostCacheKey } from './host-cache-key' + +export const GENERATIONS_DIRECTORY_NAME = 'generations' + +// One directory per host under the cache root; anything else there is not the store's. +export async function listGenerationHostDirectories( + fs: GenerationFileSystem +): Promise { + const entries = await fs.list(fs.rootUri) + return entries.filter((entry) => entry.isDirectory && isHostCacheKey(entry.name)) +} + +// Count/evict only hosts with generations; evicting staged-only hosts would delete a tree awaiting commit. Sweeps still visit them. +export async function listActivatedGenerationHosts( + fs: GenerationFileSystem +): Promise { + const activated: string[] = [] + for (const host of await listGenerationHostDirectories(fs)) { + const generations = await fs.list(joinUri(fs.rootUri, host.name, GENERATIONS_DIRECTORY_NAME)) + if (generations.some((entry) => entry.isDirectory)) { + activated.push(host.name) + } + } + return activated +} diff --git a/mobile/src/mobile-web-shell/generation-store.test.ts b/mobile/src/mobile-web-shell/generation-store.test.ts index 257aeb9f58f..4d381f688ba 100644 --- a/mobile/src/mobile-web-shell/generation-store.test.ts +++ b/mobile/src/mobile-web-shell/generation-store.test.ts @@ -544,4 +544,22 @@ describe('an update staged behind a live page', () => { expect((await relaunched.readActiveGeneration(HOST))?.buildId).toBe(SECOND_BUILD) expect(await fs.fileExists(`${ROOT}/${HOST}/generations/${SECOND_BUILD}/index.html`)).toBe(true) }) + + it('lets a reader of a host’s build go before a commit or a removal deletes it', async () => { + const fs = createFakeFileSystem() + const onDisk: boolean[] = [] + let reading = BUILD + const store = createGenerationStore({ + fileSystem: fs, + beforeHostFilesDeleted: async (hostKey) => { + onDisk.push(await fs.fileExists(`${ROOT}/${hostKey}/generations/${reading}/manifest.json`)) + } + }) + await activate(store, HOST) + onDisk.length = 0 + await activate(store, HOST, buildResult(SECOND)) + reading = SECOND_BUILD + await store.deleteHostCache(HOST) + expect(onDisk).toEqual([true, true]) + }) }) diff --git a/mobile/src/mobile-web-shell/generation-store.ts b/mobile/src/mobile-web-shell/generation-store.ts index 73d2d0bcf91..67da4168d49 100644 --- a/mobile/src/mobile-web-shell/generation-store.ts +++ b/mobile/src/mobile-web-shell/generation-store.ts @@ -10,7 +10,12 @@ import { MANIFEST_FILE_NAME } from './generation-asset-path' import { parseGenerationManifest, readWaitingStage } from './generation-manifest-read' import { reusableAssetsOf, reusableAssetsOfHosts } from './generation-reuse' import { stagingAssetsOf } from './generation-staging-assets' -import type { GenerationDirectoryEntry, GenerationFileSystem } from './generation-store-file-system' +import type { GenerationFileSystem } from './generation-store-file-system' +import { + GENERATIONS_DIRECTORY_NAME, + listActivatedGenerationHosts, + listGenerationHostDirectories +} from './generation-host-directories' import { isHostCacheKey } from './host-cache-key' import type { MobileWebShellUpdateFailure } from './mobile-web-shell-update-failure' import { @@ -19,7 +24,6 @@ import { recordUpdateFailureIn } from './mobile-web-shell-update-failure-log' -const GENERATIONS_DIRECTORY_NAME = 'generations' const STAGING_DIRECTORY_NAME = 'tmp' export const MAX_CACHED_HOSTS = 6 @@ -66,6 +70,8 @@ export type GenerationStore = { export function createGenerationStore(options: { fileSystem: GenerationFileSystem + // Runs before a host's built files are deleted, so nothing still reading them outlives them. + beforeHostFilesDeleted?: (hostKey: string) => Promise }): GenerationStore { const fs = options.fileSystem const hostOrder = createGenerationHostOrder(fs) @@ -87,31 +93,15 @@ export function createGenerationStore(options: { const stagingRoot = (hostKey: string): string => joinUri(hostRoot(hostKey), STAGING_DIRECTORY_NAME) - async function listHostDirectories(): Promise { - const entries = await fs.list(fs.rootUri) - return entries.filter((entry) => entry.isDirectory && isHostCacheKey(entry.name)) - } - - // Count/evict only hosts with generations; evicting staged-only hosts would delete a tree awaiting commit. Sweeps still visit them. - async function listActivatedHosts(): Promise { - const activated: string[] = [] - for (const host of await listHostDirectories()) { - const generations = await fs.list(joinUri(fs.rootUri, host.name, GENERATIONS_DIRECTORY_NAME)) - if (generations.some((entry) => entry.isDirectory)) { - activated.push(host.name) - } - } - return activated - } - async function dropHostTree(hostKey: string): Promise { activeMemo.delete(hostKey) deferred.drop(hostKey) + await options.beforeHostFilesDeleted?.(hostKey) await fs.delete(hostRoot(hostKey)) } async function enforceHostLimit(order: readonly string[]): Promise { - const hosts = await listActivatedHosts() + const hosts = await listActivatedGenerationHosts(fs) // Unlisted hosts sort first after index loss; keep the just-activated host last so it survives eviction. const evicted = new Set( [...hosts] @@ -238,6 +228,7 @@ export function createGenerationStore(options: { if (!(await fs.fileExists(joinUri(staged.directory, MANIFEST_FILE_NAME)))) { throw new Error(`staged generation ${staged.buildId} is no longer on disk`) } + await options.beforeHostFilesDeleted?.(staged.hostKey) // Delete old generations before rename: a crash then leaves no activation, whereas renaming first can leave two with no way to choose. for (const entry of entries) { await fs.delete(joinUri(generations, entry.name)) @@ -276,7 +267,9 @@ export function createGenerationStore(options: { async function openReusable(hostKey: string): Promise { const hosts = [ hostKey, - ...(await listHostDirectories()).map((host) => host.name).filter((key) => key !== hostKey) + ...(await listGenerationHostDirectories(fs)) + .map((host) => host.name) + .filter((key) => key !== hostKey) ] // Reuse discovery must not clean up another shell's active or staged files. const reusable = await reusableAssetsOfHosts(fs, hosts, (key) => readActive(key, false)) @@ -290,7 +283,7 @@ export function createGenerationStore(options: { return } // Interrupted downloads may belong to a host this launch never opens, so visit every host's tmp tree. - for (const host of await listHostDirectories()) { + for (const host of await listGenerationHostDirectories(fs)) { // Unreadable (undefined) is not corrupt: leave that tree for a later launch rather than fail every read. const waiting = await readWaitingStage(fs, host.name, stagingRoot(host.name)).catch( () => undefined diff --git a/mobile/src/mobile-web-shell/home-spare-page.ts b/mobile/src/mobile-web-shell/home-spare-page.ts new file mode 100644 index 00000000000..1456e5909b1 --- /dev/null +++ b/mobile/src/mobile-web-shell/home-spare-page.ts @@ -0,0 +1,91 @@ +import { + claimSpareDocument, + discardSpareDocument, + prepareSpareDocument +} from '../../modules/orca-mobile-web-shell/src/spare-document' +import type { GenerationStore } from './generation-store' +import { generationDirectoryPath } from './generation-store-file-system' +import { deriveHostCacheKey } from './host-cache-key' +import { mintShellSessionId } from './shell-session-id' + +// A Home entry claims the spare about 0.2 s after the tap on a simulator; a native screen keeps it no longer. +const HOME_SPARE_CLAIM_GRACE_MS = 2000 + +// What JS last asked native to park, until a claim or a drop. Native may since have dropped it on +// its own (background, memory); this only saves the claim's native round trip when nothing can match. +let parkedDirectory: string | null = null +let dropTimer: ReturnType | null = null +// A prepare still reading the store when Home goes must not park a spare after the drop ran. +let homeShown = false + +function cancelDrop(): void { + if (dropTimer !== null) { + clearTimeout(dropTimer) + dropTimer = null + } +} + +// Boot one page from the first of `hostIds` (most recently connected first) that has a cached build. +export async function prepareHomeSparePage( + store: GenerationStore, + hostIds: readonly string[] +): Promise { + try { + for (const hostId of hostIds) { + const hostKey = deriveHostCacheKey(hostId) + const active = await store.readActiveGeneration(hostKey) + if (active === null) { + continue + } + if (!homeShown) { + return + } + const directory = generationDirectoryPath(active.directory) + const prepared = await prepareSpareDocument(directory, mintShellSessionId()) + if (prepared && !homeShown) { + await dropHomeSparePage() + } else if (prepared) { + parkedDirectory = directory + } + return + } + } catch { + // A spare only saves time; the next entry boots its own page without one. + } +} + +// The spare's session id when Home booted it from `directory`; the view mounted with it adopts that page. +export async function claimHomeSparePage(directory: string): Promise { + if (parkedDirectory !== directory) { + return null + } + parkedDirectory = null + cancelDrop() + try { + return await claimSpareDocument(directory) + } catch { + return null + } +} + +// Home is shown; a spare still inside the grace after leaving it stays. +export function enterHomeSparePage(): void { + homeShown = true + cancelDrop() +} + +// Home lost focus: a page opening from it claims the spare within the grace, anything else drops it. +export function leaveHomeSparePage(): void { + homeShown = false + cancelDrop() + dropTimer = setTimeout(() => { + dropTimer = null + void dropHomeSparePage() + }, HOME_SPARE_CLAIM_GRACE_MS) +} + +// Also runs before any host's built files go: that is rare, and a spare must not outlive its files. +export function dropHomeSparePage(): Promise { + parkedDirectory = null + return discardSpareDocument().catch(() => undefined) +} diff --git a/mobile/src/mobile-web-shell/mobile-web-shell-failed-update-fallback.test.ts b/mobile/src/mobile-web-shell/mobile-web-shell-failed-update-fallback.test.ts index 1d7d38cdab3..d4c7b198524 100644 --- a/mobile/src/mobile-web-shell/mobile-web-shell-failed-update-fallback.test.ts +++ b/mobile/src/mobile-web-shell/mobile-web-shell-failed-update-fallback.test.ts @@ -165,6 +165,7 @@ async function mountRoute(fileSystem: FakeGenerationFileSystem): Promise createGenerationStore({ fileSystem }), mintSessionId: () => 'session-id', + claimSpareSessionId: async () => null, now: () => 0, setForegroundTimer: () => () => {} } diff --git a/mobile/src/mobile-web-shell/mobile-web-shell-runtime.ts b/mobile/src/mobile-web-shell/mobile-web-shell-runtime.ts index e9aff1ac061..44e53bfd614 100644 --- a/mobile/src/mobile-web-shell/mobile-web-shell-runtime.ts +++ b/mobile/src/mobile-web-shell/mobile-web-shell-runtime.ts @@ -1,12 +1,9 @@ -import * as ExpoCrypto from 'expo-crypto' -import { encodeBase64Url } from '../transport/encryption/mobile-session-id-encoding' import { BRIDGE_READY_RETRY_MAX_MS } from './bridge/bridge-client-init-handshake' import type { GenerationStore } from './generation-store' import { processGenerationStore } from './process-generation-store' import { setForegroundTimer } from './foreground-timer' - -// Use 32 random base64url bytes: each mount/remount needs a fresh id for the view’s private origin. -const SESSION_ID_BYTES = 32 +import { claimHomeSparePage } from './home-spare-page' +import { mintShellSessionId } from './shell-session-id' // Allow five ready-backoff ceilings before treating a loaded document as silent; startup import failures may prevent every ready attempt. export const PAGE_READY_DEADLINE_MS = BRIDGE_READY_RETRY_MAX_MS * 5 @@ -17,6 +14,8 @@ export const DOCUMENT_LOAD_DEADLINE_MS = 20_000 export type MobileWebShellRuntime = { createStore(): GenerationStore mintSessionId(): string + // The session id of a page Home booted from this directory, so the view adopts it instead of a new one. + claimSpareSessionId(directory: string): Promise now(): number // Count only foreground time and return cancellation; tests replace this seam instead of waiting on wall time. setForegroundTimer(run: () => void, delayMs: number): () => void @@ -25,7 +24,8 @@ export type MobileWebShellRuntime = { export function createMobileWebShellRuntime(): MobileWebShellRuntime { return { createStore: processGenerationStore, - mintSessionId: () => encodeBase64Url(ExpoCrypto.getRandomBytes(SESSION_ID_BYTES)), + mintSessionId: mintShellSessionId, + claimSpareSessionId: claimHomeSparePage, now: Date.now, setForegroundTimer } diff --git a/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.test.ts b/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.test.ts index 5af588e13bc..b023ae70955 100644 --- a/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.test.ts +++ b/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.test.ts @@ -103,6 +103,7 @@ describe('a download that reuses the cached generation', () => { runtime: { createStore: () => store, mintSessionId: () => 's', + claimSpareSessionId: async () => null, now: () => 0, setForegroundTimer: () => () => {} }, diff --git a/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts b/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts index 05605735d45..c5995d21023 100644 --- a/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts +++ b/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts @@ -264,17 +264,20 @@ export async function runShellEffect( context.manifest.current = null await (effect.kind === 'download' ? download : stageUpdate)({ ...context, opened }) return - case 'open-generation': + case 'open-generation': { + // A remount mints its own id below; only a first open adopts a page Home booted. + const spare = await runtime.claimSpareSessionId(effect.directory) send({ type: 'activated', flow, generationDirectory: effect.directory, - sessionId: runtime.mintSessionId(), + sessionId: spare ?? runtime.mintSessionId(), buildId: effect.buildId, totalBytes: effect.totalBytes, elapsedMs: runtime.now() - context.startedAt }) return + } case 'remount': { const remount = (): void => send({ type: 'remounted', flow, sessionId: runtime.mintSessionId() }) diff --git a/mobile/src/mobile-web-shell/page-frames-without-host.ts b/mobile/src/mobile-web-shell/page-frames-without-host.ts new file mode 100644 index 00000000000..86e6c55ce88 --- /dev/null +++ b/mobile/src/mobile-web-shell/page-frames-without-host.ts @@ -0,0 +1,40 @@ +import { readBridgeClientMessage } from './bridge/bridge-envelope' +import { frameWithoutHostLine } from './shell-bridge-log-lines' +import { recordShellLifecycleLine } from './shell-lifecycle-line-recorder' + +export type FrameWithoutHostFacts = { hostId: string; clientReady: boolean; snapshotReady: boolean } + +export type PageFramesWithoutHost = { + /** A frame no host serves yet; `facts` is null when a retired session's host is still mounted. */ + receive: (sessionId: string, json: string, facts: FrameWithoutHostFacts | null) => void + /** Hands the page's latest ready for this session to the host just built for it, once. */ + replayReady: (sessionId: string, host: { receive: (json: string) => void }) => void +} + +// Keep the ready instead of dropping it, so init does not wait on the page's 50 ms to 2 s backoff. +export function createPageFramesWithoutHost(): PageFramesWithoutHost { + let loggedSessionId: string | null = null + let ready: { sessionId: string; json: string } | null = null + return { + receive: (sessionId, json, facts) => { + if (facts !== null && loggedSessionId !== sessionId) { + loggedSessionId = sessionId + recordShellLifecycleLine(facts.hostId, frameWithoutHostLine(facts)) + } + const read = readBridgeClientMessage(json) + if (read.ok && read.message.type === 'ready') { + ready = { sessionId, json } + } else if (read.ok && read.message.type === 'close') { + // The document that asked is gone, so its ready must not be answered. + ready = null + } + }, + replayReady: (sessionId, host) => { + const kept = ready + ready = null + if (kept?.sessionId === sessionId) { + host.receive(kept.json) + } + } + } +} diff --git a/mobile/src/mobile-web-shell/process-generation-store.ts b/mobile/src/mobile-web-shell/process-generation-store.ts index 205abf1145c..1087603b3e0 100644 --- a/mobile/src/mobile-web-shell/process-generation-store.ts +++ b/mobile/src/mobile-web-shell/process-generation-store.ts @@ -1,11 +1,15 @@ import { createGenerationStore, type GenerationStore } from './generation-store' import { createExpoGenerationFileSystem } from './generation-store-file-system' +import { dropHomeSparePage } from './home-spare-page' let processStore: GenerationStore | null = null // Share one store queue across native callers so host removal cannot interleave with generation/index/failure-log writes. export function processGenerationStore(): GenerationStore { - processStore ??= createGenerationStore({ fileSystem: createExpoGenerationFileSystem() }) + processStore ??= createGenerationStore({ + fileSystem: createExpoGenerationFileSystem(), + beforeHostFilesDeleted: dropHomeSparePage + }) return processStore } diff --git a/mobile/src/mobile-web-shell/shell-session-id.ts b/mobile/src/mobile-web-shell/shell-session-id.ts new file mode 100644 index 00000000000..3b28baf87e4 --- /dev/null +++ b/mobile/src/mobile-web-shell/shell-session-id.ts @@ -0,0 +1,9 @@ +import * as ExpoCrypto from 'expo-crypto' +import { encodeBase64Url } from '../transport/encryption/mobile-session-id-encoding' + +// Use 32 random base64url bytes: each mount/remount needs a fresh id for the view’s private origin. +const SESSION_ID_BYTES = 32 + +export function mintShellSessionId(): string { + return encodeBase64Url(ExpoCrypto.getRandomBytes(SESSION_ID_BYTES)) +} diff --git a/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.test.ts b/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.test.ts index d18449d42b9..95ce881b9e4 100644 --- a/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.test.ts +++ b/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.test.ts @@ -14,6 +14,7 @@ import { import type { BridgeErrorCapture } from './bridge/bridge-error-capture' import type { MobileWebShellSessionState } from './mobile-web-shell-session-contract' import type { FakeRpcClient } from './bridge-host-test-fakes' +import type { PageHostSnapshot } from './use-page-host-snapshot' const doubles = vi.hoisted((): { client: FakeRpcClient | null } => ({ client: null })) @@ -82,6 +83,7 @@ function FakeShellView(props: { function Harness(props: { session: MobileWebShellSessionState sessionEstablished?: boolean + snapshot?: PageHostSnapshot | null posted: PostedFrame[] probe: Probe faults: BridgeErrorCapture[] @@ -121,7 +123,7 @@ function Harness(props: { props.probe.backPops += 1 return 'popped' }, - snapshot: SNAPSHOT, + snapshot: props.snapshot === undefined ? SNAPSHOT : props.snapshot, readStorage: () => ({ storage: STORAGE, storageOversize: [] }), onStorageWrite: (key, value) => props.probe.storageWrites.push({ key, value }), // Use fresh callbacks each render; rebuilding for them would settle pending requests. @@ -198,7 +200,8 @@ afterEach(async () => { async function mount( session: MobileWebShellSessionState, - sessionEstablished?: boolean + sessionEstablished?: boolean, + snapshot?: PageHostSnapshot | null ): Promise { const posted: PostedFrame[] = [] const probe: Probe = { @@ -219,6 +222,7 @@ async function mount( createElement(Harness, { session: next, ...(sessionEstablished === undefined ? {} : { sessionEstablished }), + ...(snapshot === undefined ? {} : { snapshot }), posted, probe, faults, @@ -312,3 +316,26 @@ describe('a session that handshook before this host existed', () => { expect(fakeClient().requests).toEqual([]) }) }) + +describe('a ready the page sent before its host existed', () => { + it('is answered with one init as soon as the host snapshot arrives', async () => { + const mounted = await mount(readyState('session-one'), undefined, null) + await mounted.deliver(clientFrame({ type: 'ready' })) + expect(mounted.posted).toEqual([]) + await act(async () => { + mounted.tree.update( + createElement(Harness, { + session: readyState('session-one'), + snapshot: SNAPSHOT, + posted: mounted.posted, + probe: mounted.probe, + faults: mounted.faults, + readies: mounted.readies + }) + ) + }) + expect(mounted.frames('session-one')).toEqual([ + expect.objectContaining({ type: 'init', sessionId: 'session-one', buildId: 'build-a' }) + ]) + }) +}) diff --git a/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.ts b/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.ts index 04bfaa84363..07a5f2cba67 100644 --- a/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.ts +++ b/mobile/src/mobile-web-shell/use-mobile-web-shell-bridge.ts @@ -5,14 +5,14 @@ import type { BridgeTerminalKeyRail } from './bridge/bridge-terminal-key' import type { BridgePageGeometry } from './bridge/bridge-page-geometry' -import { useCallback, useEffect, useLayoutEffect, useRef } from 'react' +import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react' import type { MobileWebShellBridgeMessagePayload, OrcaMobileWebShellViewHandle } from '../../modules/orca-mobile-web-shell/src' import { useHostClient } from '../transport/connection/host-client-hooks' import { createBridgeDiagnosticReporter } from './bridge-diagnostic-log' -import { bridgeDiagnosticLine, frameWithoutHostLine } from './shell-bridge-log-lines' +import { bridgeDiagnosticLine } from './shell-bridge-log-lines' import { recordShellLifecycleLine } from './shell-lifecycle-line-recorder' import type { BridgeColorScheme, BridgeInitRoute, BridgeUiLocale } from './bridge/bridge-envelope' import type { BridgeClearableRouteParam } from './bridge/bridge-route-update' @@ -27,6 +27,7 @@ import type { BridgeErrorCapture } from './bridge/bridge-error-capture' import type { MobileWebShellSessionState } from './mobile-web-shell-session-contract' import type { PageHostSnapshot } from './use-page-host-snapshot' import type { PageStorageForInit } from './page-storage-keys' +import { createPageFramesWithoutHost } from './page-frames-without-host' class BridgeViewGoneError extends Error { constructor() { @@ -135,7 +136,7 @@ export function useMobileWebShellBridge(args: MobileWebShellBridgeArgs): MobileW // Read identity lazily without rebuilding a live host just to refresh a string. const clientIdRef = useRef(clientId) const clientRef = useRef(client) - const droppedFrameLoggedRef = useRef(null) + const [framesWithoutHost] = useState(createPageFramesWithoutHost) // Update refs on every commit before host construction; native frames may arrive before passive effects. useLayoutEffect(() => { argsRef.current = args @@ -207,13 +208,14 @@ export function useMobileWebShellBridge(args: MobileWebShellBridgeArgs): MobileW hostRef.current = { sessionId, host } // Reset host-owned drop totals on rebuild so a new lower count cannot look like frames being recovered. latest.onBinaryFramesDropped(0) + framesWithoutHost.replayReady(sessionId, host) return () => { // Capture Back before disposal so the next host for this document inherits it. establishedBackRef.current = { sessionId, back: host.readSessionBack() } hostRef.current = null host.dispose() } - }, [buildId, client, sessionId, snapshot]) + }, [buildId, client, framesWithoutHost, sessionId, snapshot]) useEffect(() => { const mounted = hostRef.current @@ -246,25 +248,24 @@ export function useMobileWebShellBridge(args: MobileWebShellBridgeArgs): MobileW (event: MobileWebShellBridgeMessageEvent) => { const mounted = hostRef.current if (mounted === null || mounted.sessionId !== sessionId) { - if ( - mounted === null && - sessionId !== null && - droppedFrameLoggedRef.current !== sessionId - ) { - droppedFrameLoggedRef.current = sessionId - recordShellLifecycleLine( - argsRef.current.hostId, - frameWithoutHostLine({ - clientReady: clientRef.current !== null, - snapshotReady: argsRef.current.snapshot !== null - }) + if (sessionId !== null) { + framesWithoutHost.receive( + sessionId, + event.nativeEvent.json, + mounted === null + ? { + hostId: argsRef.current.hostId, + clientReady: clientRef.current !== null, + snapshotReady: argsRef.current.snapshot !== null + } + : null ) } return } mounted.host.receive(event.nativeEvent.json) }, - [sessionId] + [framesWithoutHost, sessionId] ), // Fenced on the session the same way inbound frames are: a host left over from a session this // render has moved past must not be handed this one's route. diff --git a/mobile/src/mobile-web-shell/use-mobile-web-shell-session.test.ts b/mobile/src/mobile-web-shell/use-mobile-web-shell-session.test.ts index 65f65113c20..c5b70b1822f 100644 --- a/mobile/src/mobile-web-shell/use-mobile-web-shell-session.test.ts +++ b/mobile/src/mobile-web-shell/use-mobile-web-shell-session.test.ts @@ -264,7 +264,8 @@ afterEach(async () => { async function mount( store: GenerationStore, - setForegroundTimer?: MobileWebShellRuntime['setForegroundTimer'] + setForegroundTimer?: MobileWebShellRuntime['setForegroundTimer'], + claimSpareSessionId: MobileWebShellRuntime['claimSpareSessionId'] = async () => null ): Promise { const timers = createTimerSeam() // Mint distinct ids so a remount's replacement view can be told from the one it retires. @@ -297,6 +298,7 @@ async function mount( runtime: { createStore: () => store, mintSessionId: () => `session-${++minted}`, + claimSpareSessionId, now: () => 0, setForegroundTimer: setForegroundTimer ?? timers.setTimer } @@ -374,6 +376,19 @@ describe('the wait for the page to speak', () => { expect(mounted.states().at(-1)?.kind).toBe('ready') }) + it('opens a page Home booted under its session id, and remounts under a fresh one', async () => { + const fake = createFakeStore() + const mounted = await mount(fake.store, undefined, async () => 'home-spare') + fake.settleCacheRead(activeGeneration()) + await flush() + expect(mounted.states().at(-1)).toMatchObject({ kind: 'ready', sessionId: 'home-spare' }) + await act(async () => { + mounted.shellFailed('document-load-failed') + }) + await flush() + expect(mounted.states().at(-1)).toMatchObject({ kind: 'ready', sessionId: 'session-1' }) + }) + it('stays ready when the page speaks after document load and the timers fire', async () => { const mounted = await ready() await act(async () => { @@ -401,6 +416,7 @@ describe('the wait for the page to speak', () => { runtime: { createStore: () => fake.store, mintSessionId: () => 'session-id', + claimSpareSessionId: async () => null, now: () => 0, setForegroundTimer: createTimerSeam().setTimer } diff --git a/mobile/src/test-support/spare-document-node.ts b/mobile/src/test-support/spare-document-node.ts new file mode 100644 index 00000000000..13723e207eb --- /dev/null +++ b/mobile/src/test-support/spare-document-node.ts @@ -0,0 +1,4 @@ +// Alias the native spare pool for Vitest: no spare is ever parked, so every entry boots its own page. +export const prepareSpareDocument = (): Promise => Promise.resolve(false) +export const claimSpareDocument = (): Promise => Promise.resolve(null) +export const discardSpareDocument = (): Promise => Promise.resolve() diff --git a/mobile/vitest.config.ts b/mobile/vitest.config.ts index e9861769f75..53fb31a4987 100644 --- a/mobile/vitest.config.ts +++ b/mobile/vitest.config.ts @@ -22,6 +22,12 @@ export default defineConfig({ new URL('./src/test-support/expo-crypto-node.ts', import.meta.url) ) }, + { + find: /^.*\/orca-mobile-web-shell\/src\/spare-document$/, + replacement: fileURLToPath( + new URL('./src/test-support/spare-document-node.ts', import.meta.url) + ) + }, { find: 'expo-haptics', replacement: fileURLToPath( From a086693d78d5d49e4cfa56a80b9870f3f360fb85 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:25:15 +0000 Subject: [PATCH 29/80] fix(native-chat): show newly installed skills in the Codex $ menu before the first message (#40012) * fix(native-chat): show newly installed skills in the Codex $ menu before the first message Resting Codex chats served the background probe's skills/list from a 5-minute cache, so a skill installed in that window showed "No matching skills", even in a new chat. Each menu open now compares a cheap stamp of Codex's skill folders and skill/plugin config with the one taken before the last scan, and rescans only when it changed. Probe requests always force a reload, and probe pools are keyed by the catalog's launch generation instead of the whole env, whose 10 s refresh started a second Codex on every rescan. Fixes STA-14359 * fix(native-chat): keep the structured session hook within its line limit --- ...codex-managed-home-resource-copy-marker.ts | 28 ++- .../codex-at-rest-skills-probe-pools.ts | 1 - .../catalog/codex-at-rest-skills-probe.ts | 35 ++-- .../catalog/codex-skill-sources-stamp.test.ts | 123 +++++++++++ .../catalog/codex-skill-sources-stamp.ts | 196 ++++++++++++++++++ .../options/at-rest-command-catalog.ts | 32 ++- .../options/at-rest-command-stamp.ts | 61 ++++++ .../at-rest-command-catalog-stamp.test.ts | 77 +++++++ .../skills/discovery/skill-root-file-walk.ts | 45 +++- .../filesystem/skill-candidate-concurrency.ts | 7 +- .../pickers/NativeChatAutocompleteMenus.tsx | 31 ++- ...gent-session-command-menu.test-fixture.tsx | 2 +- .../pickers/native-chat-picker-empty-text.ts | 21 ++ ...tructured-agent-session-command-refresh.ts | 61 +++++- ...use-native-chat-composer-command-picker.ts | 2 + .../pickers/use-native-chat-picker-state.ts | 11 +- ...tive-chat-structured-composer-transport.ts | 2 + ...tive-chat-structured-composer-transport.ts | 1 + .../use-structured-agent-session.ts | 3 +- src/renderer/src/i18n/locales/de.json | 1 + src/renderer/src/i18n/locales/en.json | 1 + src/renderer/src/i18n/locales/es.json | 1 + src/renderer/src/i18n/locales/fr.json | 1 + src/renderer/src/i18n/locales/ja.json | 1 + src/renderer/src/i18n/locales/ko.json | 1 + src/renderer/src/i18n/locales/pt.json | 1 + src/renderer/src/i18n/locales/ru.json | 1 + src/renderer/src/i18n/locales/tr.json | 1 + src/renderer/src/i18n/locales/zh-Hant.json | 1 + src/renderer/src/i18n/locales/zh.json | 1 + .../native-chat-composer-autocomplete.ts | 2 + 31 files changed, 682 insertions(+), 70 deletions(-) create mode 100644 src/main/codex/structured/catalog/codex-skill-sources-stamp.test.ts create mode 100644 src/main/codex/structured/catalog/codex-skill-sources-stamp.ts create mode 100644 src/main/native-chat/agent-session-wire/options/at-rest-command-stamp.ts create mode 100644 src/main/native-chat/agent-session-wire/options/testing/at-rest-command-catalog-stamp.test.ts create mode 100644 src/renderer/src/components/native-chat/composer/pickers/native-chat-picker-empty-text.ts diff --git a/src/main/codex/configuration/homes/codex-managed-home-resource-copy-marker.ts b/src/main/codex/configuration/homes/codex-managed-home-resource-copy-marker.ts index c8486c164e7..a5cd6fbf1ff 100644 --- a/src/main/codex/configuration/homes/codex-managed-home-resource-copy-marker.ts +++ b/src/main/codex/configuration/homes/codex-managed-home-resource-copy-marker.ts @@ -1,4 +1,5 @@ import { existsSync, lstatSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { readFile } from 'node:fs/promises' import { dirname, join } from 'node:path' // Mark fallback copies so sync can refresh/remove them without touching user-created resources. @@ -19,11 +20,9 @@ export function markCopiedResource( }) } -function readCopiedResourceSourcePath(managedHomePath: string, entryName: string): string | null { +function parseCopiedResourceSourcePath(text: string): string | null { try { - const parsed: unknown = JSON.parse( - readFileSync(getResourceCopyMarkerPath(managedHomePath, entryName), 'utf-8') - ) + const parsed: unknown = JSON.parse(text) if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { return null } @@ -34,6 +33,27 @@ function readCopiedResourceSourcePath(managedHomePath: string, entryName: string } } +function readCopiedResourceSourcePath(managedHomePath: string, entryName: string): string | null { + try { + return parseCopiedResourceSourcePath( + readFileSync(getResourceCopyMarkerPath(managedHomePath, entryName), 'utf-8') + ) + } catch { + return null + } +} + +/** The source an Orca fallback copy mirrors; null when the entry is a link or the user's own. */ +export async function readCopiedResourceSource( + managedHomePath: string, + entryName: string +): Promise { + const text = await readFile(getResourceCopyMarkerPath(managedHomePath, entryName), 'utf-8').catch( + () => null + ) + return text === null ? null : parseCopiedResourceSourcePath(text) +} + export function clearCopiedResourceMarker(managedHomePath: string, entryName: string): void { // Malformed marker directories must not block resource repair or Codex launch. rmSync(getResourceCopyMarkerPath(managedHomePath, entryName), { diff --git a/src/main/codex/structured/catalog/codex-at-rest-skills-probe-pools.ts b/src/main/codex/structured/catalog/codex-at-rest-skills-probe-pools.ts index d7d23978c4f..30055b42bca 100644 --- a/src/main/codex/structured/catalog/codex-at-rest-skills-probe-pools.ts +++ b/src/main/codex/structured/catalog/codex-at-rest-skills-probe-pools.ts @@ -7,7 +7,6 @@ const FAILED_START_BACKOFF_MS = [10_000, 30_000, 90_000, 5 * 60_000] as const export type FolderRequest = { cwd: string - generation: number isCurrent: () => boolean admitted: () => void answer: PromiseWithResolvers diff --git a/src/main/codex/structured/catalog/codex-at-rest-skills-probe.ts b/src/main/codex/structured/catalog/codex-at-rest-skills-probe.ts index 6c05e8ca12f..070f724816b 100644 --- a/src/main/codex/structured/catalog/codex-at-rest-skills-probe.ts +++ b/src/main/codex/structured/catalog/codex-at-rest-skills-probe.ts @@ -1,6 +1,5 @@ import { delayUnlessAborted } from '../../../../shared/abort-aware-delay' import { waitForPromiseWithSignal } from '../../../../shared/abort-signal-reason' -import { createHash } from 'node:crypto' import * as Pools from './codex-at-rest-skills-probe-pools' import { isLegacyAgentSessionAccountHome, @@ -26,6 +25,7 @@ import { codexReportCommandRows, isCodexSkillsListUnsupported } from './codex-skill-list' +import { createCodexSkillSourcesStamp } from './codex-skill-sources-stamp' import { isNativeLocalLocation as supportsCodexStructuredLocation } from '../../../../shared/execution-host' import { resolveHiddenProbeCwd } from '../../../rate-limits/hidden-probe-cwd' import { @@ -64,6 +64,7 @@ function definedEnv(env: NodeJS.ProcessEnv | undefined): Record export function createCodexAtRestSkillCatalog(deps: CodexAtRestSkillsDeps): AtRestCommandCatalog { const now = deps.now ?? Date.now const { pools, start, expire, dispose } = Pools.createCodexSkillsProbePools(deps, probe) + const stampSources = createCodexSkillSourcesStamp(deps.now ? { now: deps.now } : {}) return new AtRestCommandCatalog({ label: 'codex', @@ -73,6 +74,8 @@ export function createCodexAtRestSkillCatalog(deps: CodexAtRestSkillsDeps): AtRe isLegacyAgentSessionAccountHome(record.accountHome), resolveWorkspacePath: deps.resolveWorkspacePath, dispose, + stamp: (record, cwd, signal) => + stampSources(requireLegacyAgentSessionAccountHome(record.accountHome).path, cwd, signal), scan: async (record, cwd, isCurrent, generation, signal, admitted) => { const accountHomePath = requireLegacyAgentSessionAccountHome(record.accountHome).path const { command, prefixArgs, environment } = await resolveCodexStructuredInvocation({ @@ -90,14 +93,10 @@ export function createCodexAtRestSkillCatalog(deps: CodexAtRestSkillsDeps): AtRe return NO_SCAN } const env = definedEnv(environment) - const key = JSON.stringify([ - accountHomePath, - command, - args, - createHash('sha256') - .update(JSON.stringify(Object.entries(env).sort())) - .digest('hex') - ]) + // Not the env itself: the shell capture refreshes every 10 s with volatile values (prompt + // session keys), which would start a second probe. Settings and account env changes retire + // the generation; a running probe keeps the env it started with for its ~70 s life. + const key = JSON.stringify([accountHomePath, command, args, generation]) const pool = pools.get(key) ?? { failedStarts: 0, retryAt: 0, lastDemandAt: now() } pools.set(key, pool) pool.lastDemandAt = now() @@ -117,7 +116,6 @@ export function createCodexAtRestSkillCatalog(deps: CodexAtRestSkillsDeps): AtRe } const request: Pools.FolderRequest = { cwd, - generation, isCurrent, admitted, answer: Promise.withResolvers(), @@ -161,7 +159,6 @@ export async function probe( onStarted: () => void ): Promise { const now = deps.now ?? Date.now - let reloaded = Math.max(...[...pool.pending].map((request) => request.generation)) await (deps.runSession ?? runCodexAppServerSession)( { command: pool.command, @@ -210,13 +207,10 @@ export async function probe( pool.cursor = undefined } if (batch.length > 0) { - const forceReload = batch.some((request) => request.generation > reloaded) try { deps.prepareHome?.(pool.accountHomePath) batch.forEach((request) => request.admitted()) - if (await answer(rpc, batch, forceReload, signal)) { - reloaded = Math.max(reloaded, ...batch.map((request) => request.generation)) - } + await answer(rpc, batch, signal) } catch (error) { batch.forEach((request) => Pools.settle(request, error instanceof Error ? error : new Error(String(error))) @@ -249,27 +243,27 @@ export async function probe( async function answer( rpc: CodexAppServerRpc, batch: Pools.FolderRequest[], - forceReload: boolean, signal: AbortSignal -): Promise { +): Promise { let reply: unknown try { reply = await rpc.request( CODEX_SKILLS_LIST_METHOD, - { cwds: batch.map((request) => request.cwd), ...(forceReload ? { forceReload: true } : {}) }, + // A probe answers several menus in its lifetime; without this Codex can repeat its cached list. + { cwds: batch.map((request) => request.cwd), forceReload: true }, { timeoutMs: ANSWER_TIMEOUT_MS } ) } catch (error) { if (isCodexSkillsListUnsupported(error)) { // An older Codex has no list to give; the client keeps its folder scan. batch.forEach((request) => Pools.settle(request, { commands: undefined, fromAgent: true })) - return false + return } // The process still settles: ending it early would interrupt plugin sync, including after a list failure. batch.forEach((request) => Pools.settle(request, error instanceof Error ? error : new Error(String(error))) ) - return false + return } signal.throwIfAborted() for (const request of batch) { @@ -290,5 +284,4 @@ async function answer( : new Error(`codex listed no skills entry for ${request.cwd}`) ) } - return true } diff --git a/src/main/codex/structured/catalog/codex-skill-sources-stamp.test.ts b/src/main/codex/structured/catalog/codex-skill-sources-stamp.test.ts new file mode 100644 index 00000000000..56782bd37ef --- /dev/null +++ b/src/main/codex/structured/catalog/codex-skill-sources-stamp.test.ts @@ -0,0 +1,123 @@ +import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { createCodexSkillSourcesStamp } from './codex-skill-sources-stamp' + +let root: string +let clock = 0 + +beforeEach(async () => { + root = await mkdtemp(join(tmpdir(), 'codex-skill-stamp-')) + await mkdir(join(root, 'home', '.agents', 'skills'), { recursive: true }) + await mkdir(join(root, 'codex-home'), { recursive: true }) + await mkdir(join(root, 'repo', '.git'), { recursive: true }) + await writeFile(join(root, 'codex-home', 'config.toml'), TRUSTED) +}) + +const TRUSTED = '[projects."/tmp/a"]\ntrust_level = "trusted"\n' + +afterEach(async () => { + await rm(root, { recursive: true, force: true }) +}) + +async function writeSkill(directory: string, name: string): Promise { + await mkdir(directory, { recursive: true }) + await writeFile(join(directory, 'SKILL.md'), `---\nname: ${name}\ndescription: d\n---\n`) +} + +function stamper(accountHome = join(root, 'codex-home')) { + const stamp = createCodexSkillSourcesStamp({ + now: () => clock, + homeDir: () => join(root, 'home'), + systemCodexHome: () => join(root, 'codex-home'), + managedCodexHome: () => join(root, 'managed-home') + }) + return async () => { + // Step past the shared per-root reuse window between reads. + clock += 10_000 + return stamp(accountHome, join(root, 'repo'), new AbortController().signal) + } +} + +describe('Codex skill sources stamp', () => { + it.each([ + ['a user skill', () => writeSkill(join(root, 'home', '.agents', 'skills', 'perf-scan'), 'p')], + [ + 'a linked user skill', + async () => { + await writeSkill(join(root, 'elsewhere', 'perf-scan'), 'p') + await symlink( + join(root, 'elsewhere', 'perf-scan'), + join(root, 'home', '.agents', 'skills', 'perf-scan'), + 'dir' + ) + } + ], + [ + 'a nested user skill', + () => writeSkill(join(root, 'home', '.agents', 'skills', 'synced', 'x', 'perf-scan'), 'p') + ], + ['a repo skill', () => writeSkill(join(root, 'repo', '.agents', 'skills', 'perf-scan'), 'p')], + [ + 'a project plugin', + async () => { + await mkdir(join(root, 'repo', '.codex'), { recursive: true }) + await writeFile( + join(root, 'repo', '.codex', 'config.toml'), + '[plugins."docs@openai"]\nenabled = true\n' + ) + } + ], + [ + 'a quoted plugin table', + () => + writeFile( + join(root, 'codex-home', 'config.toml'), + `${TRUSTED}['plugins'.'demo@local']\nenabled = true\n` + ) + ], + [ + 'a dotted plugin key', + () => + writeFile( + join(root, 'codex-home', 'config.toml'), + `plugins."demo@local".enabled = true\n${TRUSTED}` + ) + ], + [ + 'a disabled skill', + () => + writeFile( + join(root, 'codex-home', 'config.toml'), + `${TRUSTED}[[skills.config]]\npath = "/s/SKILL.md"\nenabled = false\n` + ) + ] + ])('changes when %s is added', async (_label, change) => { + const read = stamper() + const before = await read() + await change() + const after = await read() + expect(before).toBeDefined() + expect(after).not.toBe(before) + }) + + it('ignores config edits outside skills and plugins', async () => { + const read = stamper() + const before = await read() + await writeFile( + join(root, 'codex-home', 'config.toml'), + `${TRUSTED}[projects."/tmp/new-worktree"]\ntrust_level = "trusted"\n` + ) + expect(await read()).toBe(before) + }) + + it("ignores the managed home's config copy, which the probe rewrites from the source", async () => { + const managed = join(root, 'managed-home') + await mkdir(managed, { recursive: true }) + const read = stamper(managed) + const before = await read() + await writeFile(join(managed, 'config.toml'), '[plugins."docs@openai"]\nenabled = true\n') + expect(await read()).toBe(before) + }) +}) diff --git a/src/main/codex/structured/catalog/codex-skill-sources-stamp.ts b/src/main/codex/structured/catalog/codex-skill-sources-stamp.ts new file mode 100644 index 00000000000..01f2272866d --- /dev/null +++ b/src/main/codex/structured/catalog/codex-skill-sources-stamp.ts @@ -0,0 +1,196 @@ +import { createHash } from 'node:crypto' +import { readFile, stat } from 'node:fs/promises' +import { homedir } from 'node:os' +import { dirname, join } from 'node:path' +import { findSkillFiles } from '../../../skills/discovery/skill-root-file-walk' +import { runSkillCandidateTasks } from '../../../skills/filesystem/skill-candidate-concurrency' +import { + getSystemCodexHomePath, + resolveOrcaManagedCodexHomePath +} from '../../configuration/homes/codex-home-paths' +import { readCopiedResourceSource } from '../../configuration/homes/codex-managed-home-resource-copy-marker' +import { parse as parseToml } from 'smol-toml' +import { waitForPromiseWithSignal } from '../../../../shared/abort-signal-reason' +import { normalizeRuntimePathForComparison } from '../../../../shared/cross-platform-path' + +// Mirrors Codex's skill walk (codex-rs ext/skills loader: MAX_SCAN_DEPTH). +const CODEX_SKILL_SCAN_DEPTH = 6 +/** Menu bursts across chats and phones share one walk per root. */ +const ROOT_STAMP_REUSE_MS = 1_000 +/** A slow read is reused longer, keeping a huge root's I/O near 2% of wall time. */ +const ROOT_STAMP_REUSE_PER_WALK_MS = 50 +/** A read that timed out (hung network drive) is not retried until this passes. */ +const TIMED_OUT_ROOT_RETRY_MS = 60_000 +/** Half the libuv pool, so a stamp never starves other main-process I/O. */ +const STAT_CONCURRENCY = 2 +/** Per read and overall: a slow root (network drive) yields no stamp; the TTL rescan covers it. */ +const STAMP_TIMEOUT_MS = 2_000 + +type Remembered = { until: number; value: Promise } + +// Every filesystem read goes through one of these: a hung mount parks at most one libuv worker per key. +function createTimedMemo(now: () => number) { + const entries = new Map>() + return (key: string, caller: AbortSignal, read: (signal: AbortSignal) => Promise) => { + const at = now() + let entry = entries.get(key) + if (!entry || at >= entry.until) { + for (const [path, old] of entries) { + if (at >= old.until) { + entries.delete(path) + } + } + // Shared work owns only its time limit, so one chat's cancel never fails another's read. + const limit = AbortSignal.timeout(STAMP_TIMEOUT_MS) + // A hung readdir or stat never settles on abort; racing lets the timed-out mark replace it. + const value = waitForPromiseWithSignal(read(limit), limit) + // In flight: concurrent menus join this read. + const created: Remembered = { until: Number.POSITIVE_INFINITY, value } + value.then( + () => { + const done = now() + created.until = + done + Math.max(ROOT_STAMP_REUSE_MS, (done - at) * ROOT_STAMP_REUSE_PER_WALK_MS) + }, + () => { + created.until = now() + TIMED_OUT_ROOT_RETRY_MS + } + ) + entries.set(key, created) + entry = created + } + return waitForPromiseWithSignal(entry.value, caller) + } +} + +/** Fingerprint of everything Codex reads skills from; a changed value means skills/list may differ. */ +export function createCodexSkillSourcesStamp( + options: { + now?: () => number + homeDir?: () => string + systemCodexHome?: () => string + managedCodexHome?: () => string + } = {} +) { + const now = options.now ?? Date.now + const home = options.homeDir ?? homedir + const systemCodexHome = options.systemCodexHome ?? getSystemCodexHomePath + const managedCodexHome = options.managedCodexHome ?? resolveOrcaManagedCodexHomePath + const roots = createTimedMemo(now) + const chains = createTimedMemo(now) + const markers = createTimedMemo(now) + const configReads = createTimedMemo(now) + const configs = new Map() + + const rootStamp = (root: string, caller: AbortSignal): Promise => + roots(root, caller, async (signal) => { + const files = await findSkillFiles(root, CODEX_SKILL_SCAN_DEPTH, signal, { + skipHiddenDirectories: true, + stopAtSkillDirectories: true + }) + const facts = await runSkillCandidateTasks( + files.map((file) => async () => { + signal.throwIfAborted() + // ctime catches copies that preserve mtime (cp -p, unpacked archives). + const info = await stat(file).catch(() => null) + return info ? `${file}:${info.ino}:${info.ctimeMs}:${info.size}` : `${file}:-` + }), + STAT_CONCURRENCY + ) + return facts.join('\n') + }) + + // Only skill and plugin tables matter: Orca adds a trust entry each time a chat opens in a new folder. + const configStamp = async (path: string): Promise => { + const info = await stat(path).catch(() => null) + if (!info) { + return `${path}:-` + } + const key = `${info.ino}:${info.mtimeMs}:${info.size}` + const cached = configs.get(path) + if (cached?.key === key) { + return cached.digest + } + const text = await readFile(path, 'utf8').catch(() => '') + let kept: string + try { + // Parsed, so headers, dotted keys and inline tables all count. + const { skills, plugins } = parseToml(text) + kept = JSON.stringify([skills ?? null, plugins ?? null]) + } catch { + kept = text + } + const digest = `${path}:${createHash('sha1').update(kept).digest('hex')}` + configs.set(path, { key, digest }) + return digest + } + + return async ( + accountHomePath: string, + cwd: string, + signal: AbortSignal + ): Promise => { + const bounded = AbortSignal.any([signal, AbortSignal.timeout(STAMP_TIMEOUT_MS)]) + const work = async (): Promise => { + // Orca's managed home holds copies the probe rewrites; stamping them would rescan forever. + const mirrored = + normalizeRuntimePathForComparison(accountHomePath) === + normalizeRuntimePathForComparison(managedCodexHome()) + const copiedFrom = mirrored + ? await markers(accountHomePath, signal, () => + readCopiedResourceSource(accountHomePath, 'skills') + ) + : null + const skills = copiedFrom ?? join(accountHomePath, 'skills') + const project = await chains(cwd, signal, (limit) => projectDirectories(cwd, limit)) + const sources = [ + skills, + join(skills, '.system'), + join(home(), '.agents', 'skills'), + ...(process.platform === 'win32' ? [] : ['/etc/codex/skills']), + ...project.flatMap((directory) => [ + join(directory, '.agents', 'skills'), + join(directory, '.codex', 'skills') + ]) + ] + const parts: string[] = [] + // One root at a time: each walk already keeps up to three reads in flight. + for (const root of sources) { + parts.push(root, await rootStamp(root, signal)) + } + // Trusted projects can enable plugins in their own config too. + for (const config of new Set([ + ...(mirrored ? [] : [join(accountHomePath, 'config.toml')]), + join(systemCodexHome(), 'config.toml'), + ...project.map((directory) => join(directory, '.codex', 'config.toml')) + ])) { + parts.push(await configReads(config, signal, () => configStamp(config))) + } + return createHash('sha1').update(parts.join('\n')).digest('hex') + } + // Caps the menu's wait even while a joined root walk is still running. + return waitForPromiseWithSignal(work(), bounded).catch(() => undefined) + } +} + +/** Codex reads project skills and config in each folder from the git root down to cwd. */ +async function projectDirectories(cwd: string, signal: AbortSignal): Promise { + const chain: string[] = [] + for (let directory = cwd; ; directory = dirname(directory)) { + signal.throwIfAborted() + chain.push(directory) + if ( + await stat(join(directory, '.git')).then( + () => true, + () => false + ) + ) { + break + } + if (dirname(directory) === directory) { + chain.splice(1) + break + } + } + return chain +} diff --git a/src/main/native-chat/agent-session-wire/options/at-rest-command-catalog.ts b/src/main/native-chat/agent-session-wire/options/at-rest-command-catalog.ts index e5a15bf3bd8..cb6e01cd3ee 100644 --- a/src/main/native-chat/agent-session-wire/options/at-rest-command-catalog.ts +++ b/src/main/native-chat/agent-session-wire/options/at-rest-command-catalog.ts @@ -11,6 +11,12 @@ import { atRestCommandContextReady } from './at-rest-command-context' import { stat } from 'node:fs/promises' import { isDefinitiveAbsence } from '../../../../shared/definitive-filesystem-absence' import { waitForPromiseWithSignal } from '../../../../shared/abort-signal-reason' +import { + type AtRestCommandStamp, + type AtRestCommandStamped, + rescanIfStampChanged, + stampBeforeScan +} from './at-rest-command-stamp' /** Running chats supply their own lists; rescan resting folders/accounts after this TTL. */ export const AT_REST_COMMANDS_TTL_MS = 5 * 60_000 @@ -40,6 +46,8 @@ export type AtRestCommandSource = { admitted: () => void, demand: AtRestCommandDemand ) => Promise + /** A menu open rescans before the TTL once this changes. */ + stamp?: AtRestCommandStamp scanTimeoutMs?: number maxFailedScans?: number | null evictUnreadMs?: number | null @@ -66,6 +74,8 @@ type Entry = { attempt?: AbortController lastReadAt: number settled: Set<() => void> + stamped?: AtRestCommandStamped + restamping?: Promise } /** Cache commands from each resting chat’s launch folder; publish changed scan results. */ @@ -79,7 +89,10 @@ export class AtRestCommandCatalog implements StructuredAgentSessionAtRestCommand constructor(private readonly source: AtRestCommandSource) {} retireContext = (workspaceId?: string): void => { - this.generation += 1 + // One folder's retirement keeps the launch context other folders' probes share. + if (workspaceId === undefined) { + this.generation += 1 + } for (const [key, entry] of this.entries) { if (workspaceId !== undefined && entry.workspaceId !== workspaceId) { continue @@ -127,6 +140,15 @@ export class AtRestCommandCatalog implements StructuredAgentSessionAtRestCommand settled = async (record: AgentSessionRecord, budgetMs: number, independent = false) => { const deadline = Date.now() + budgetMs + // Menu opens only; dispose empties entries, so a disposed catalog finds none. + const opened = + independent && this.source.accepts(record) ? this.entries.get(this.key(record)) : undefined + if (opened && this.source.stamp) { + const owned = (): boolean => this.entries.get(opened.key) === opened + await rescanIfStampChanged(opened, this.source.stamp, owned, () => + this.scanInto(opened, record, true) + ) + } for (;;) { const entry = this.readEntry(record, independent) if (!entry) { @@ -229,7 +251,7 @@ export class AtRestCommandCatalog implements StructuredAgentSessionAtRestCommand admittedAt ??= this.now() } entry.scanning = true - void (async () => { + void (async (): Promise => { const wait = (work: Promise): Promise => waitForPromiseWithSignal(work, attempt.signal) await wait(Promise.resolve(atRestCommandContextReady())) @@ -252,19 +274,21 @@ export class AtRestCommandCatalog implements StructuredAgentSessionAtRestCommand } return { commands: undefined, fromAgent: false } } + const stamped = await wait(stampBeforeScan(this.source.stamp, chat, cwd, attempt.signal)) const answer = await wait( this.source.scan(chat, cwd, isCurrent, entry.generation, attempt.signal, admitted, demand) ) attempt.signal.throwIfAborted() - return answer + return { ...answer, stamped } })() .then( - ({ commands, fromAgent }) => { + ({ commands, fromAgent, stamped }) => { if (!isCurrent()) { return } entry.scannedAt = admittedAt ?? this.now() entry.failures = 0 + entry.stamped = stamped // Refresh settles even when fallback cannot supersede an agent-provided list. if (entry.fromAgent && !fromAgent) { return diff --git a/src/main/native-chat/agent-session-wire/options/at-rest-command-stamp.ts b/src/main/native-chat/agent-session-wire/options/at-rest-command-stamp.ts new file mode 100644 index 00000000000..a06092b51f6 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/options/at-rest-command-stamp.ts @@ -0,0 +1,61 @@ +import type { AgentSessionRecord } from '../../../../shared/agent-session/lifecycle/agent-session-record' + +/** Cheap fingerprint of what a scan reads; undefined when it can't be read in time. */ +export type AtRestCommandStamp = ( + record: AgentSessionRecord, + cwd: string, + signal: AbortSignal +) => Promise + +/** What the last good scan read, so a menu open can tell whether its sources changed since. */ +export type AtRestCommandStamped = { record: AgentSessionRecord; cwd: string; stamp: string } + +type StampedEntry = { + stamped?: AtRestCommandStamped + restamping?: Promise + scanning: boolean + failures: number +} + +/** Finished before the scan reads, so a change made while it runs still differs next time. */ +export async function stampBeforeScan( + stamp: AtRestCommandStamp | undefined, + record: AgentSessionRecord, + cwd: string, + signal: AbortSignal +): Promise { + // Never rejects: the scan may fail first and leave this unawaited. + const value = await stamp?.(record, cwd, signal).catch(() => undefined) + return value === undefined ? undefined : { record, cwd, stamp: value } +} + +/** Unchanged sources keep the cached list without starting a scan; concurrent opens share one check. */ +export function rescanIfStampChanged( + entry: StampedEntry, + stamp: AtRestCommandStamp, + owned: () => boolean, + rescan: () => void +): Promise | undefined { + const last = entry.stamped + // After a failed scan the catalog's retry schedule decides, not every menu open. + if (!last || entry.scanning || entry.failures > 0) { + return undefined + } + entry.restamping ??= stamp(last.record, last.cwd, new AbortController().signal) + .catch(() => undefined) + .then((value) => { + if ( + value !== undefined && + value !== last.stamp && + entry.stamped === last && + !entry.scanning && + owned() + ) { + rescan() + } + }) + .finally(() => { + entry.restamping = undefined + }) + return entry.restamping +} diff --git a/src/main/native-chat/agent-session-wire/options/testing/at-rest-command-catalog-stamp.test.ts b/src/main/native-chat/agent-session-wire/options/testing/at-rest-command-catalog-stamp.test.ts new file mode 100644 index 00000000000..c62864170c5 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/options/testing/at-rest-command-catalog-stamp.test.ts @@ -0,0 +1,77 @@ +import { tmpdir } from 'node:os' +import { describe, expect, it, vi } from 'vitest' +import { hostTestRestingOptionsRecord } from '../../host/testing/structured-agent-session-host-test-data' +import { AtRestCommandCatalog } from '../at-rest-command-catalog' + +function catalogWithSkills() { + let stamp = 'a' + let skills = ['old-skill'] + let scans = 0 + let failing = false + const catalog = new AtRestCommandCatalog({ + label: 'test', + accepts: () => true, + resolveWorkspacePath: async () => tmpdir(), + stamp: async () => stamp, + scan: async () => { + scans += 1 + if (failing) { + throw new Error('skills/list timed out') + } + return { + commands: skills.map((name) => ({ name, kind: 'skill' as const, path: `/s/${name}` })), + fromAgent: true + } + } + }) + return { + catalog, + scans: () => scans, + install: (name: string, nextStamp: string) => { + skills = [...skills, name] + stamp = nextStamp + }, + failScans: () => { + failing = true + } + } +} + +const names = (commands: { name: string }[] | undefined) => commands?.map(({ name }) => name) + +describe('at-rest commands before the TTL', () => { + it('rescans on a menu open once the skill sources changed', async () => { + const { catalog, scans, install } = catalogWithSkills() + const record = hostTestRestingOptionsRecord() + expect(names(await catalog.settled(record, 1_000, true))).toEqual(['old-skill']) + + install('perf-scan', 'b') + expect(names(await catalog.settled(record, 1_000, true))).toEqual(['old-skill', 'perf-scan']) + expect(scans()).toBe(2) + catalog.dispose() + }) + + it('keeps the cached list without a scan while the sources are unchanged', async () => { + const { catalog, scans } = catalogWithSkills() + const record = hostTestRestingOptionsRecord() + await catalog.settled(record, 1_000, true) + await catalog.settled(record, 1_000, true) + await catalog.settled(record, 1_000, true) + expect(scans()).toBe(1) + catalog.dispose() + }) + + it('leaves a failed rescan to the retry schedule instead of rescanning on every open', async () => { + const { catalog, scans, install, failScans } = catalogWithSkills() + const record = hostTestRestingOptionsRecord() + vi.spyOn(console, 'warn').mockImplementation(() => {}) + await catalog.settled(record, 1_000, true) + failScans() + install('perf-scan', 'b') + await catalog.settled(record, 1_000, true) + await catalog.settled(record, 1_000, true) + await catalog.settled(record, 1_000, true) + expect(scans()).toBe(2) + catalog.dispose() + }) +}) diff --git a/src/main/skills/discovery/skill-root-file-walk.ts b/src/main/skills/discovery/skill-root-file-walk.ts index edd7bb19b29..7fbf87d4fa2 100644 --- a/src/main/skills/discovery/skill-root-file-walk.ts +++ b/src/main/skills/discovery/skill-root-file-walk.ts @@ -18,20 +18,38 @@ function isWithinDepth(rootPath: string, childPath: string, maxDepth: number): b return rel.split(sep).length <= maxDepth } +/** Codex's own walk skips hidden folders; stopping at a skill folder skips its references and scripts. */ +export type SkillFileWalkOptions = { + skipHiddenDirectories?: boolean + stopAtSkillDirectories?: boolean +} + +function entersDirectory(name: string, options: SkillFileWalkOptions): boolean { + return ( + name !== SKILL_FILE_NAME && + !isSkillStagingEntryName(name) && + !(options.skipHiddenDirectories && name.startsWith('.')) + ) +} + +function holdsSkillFile(entries: readonly Dirent[]): boolean { + return entries.some((entry) => entry.name === SKILL_FILE_NAME && !entry.isDirectory()) +} + /** Entry indexes of the plain child directories a walk enters. */ function enteredChildIndexes( rootPath: string, maxDepth: number, dirPath: string, - entries: readonly Dirent[] + entries: readonly Dirent[], + options: SkillFileWalkOptions ): number[] { + if (options.stopAtSkillDirectories && holdsSkillFile(entries)) { + return [] + } const indexes: number[] = [] for (const [index, entry] of entries.entries()) { - if ( - !entry.isDirectory() || - entry.name === SKILL_FILE_NAME || - isSkillStagingEntryName(entry.name) - ) { + if (!entry.isDirectory() || !entersDirectory(entry.name, options)) { continue } // Directory entry names add one segment, so siblings share the depth verdict. @@ -47,14 +65,15 @@ function enteredChildIndexes( export async function findSkillFiles( rootPath: string, maxDepth: number, - signal?: AbortSignal + signal?: AbortSignal, + options: SkillFileWalkOptions = {} ): Promise { const out: string[] = [] const visitedDirectoryPaths = new Set() const readAhead = createDirectoryReadAhead({ signal, enteredChildIndexes: (dirPath, entries) => - enteredChildIndexes(rootPath, maxDepth, dirPath, entries) + enteredChildIndexes(rootPath, maxDepth, dirPath, entries, options) }) async function visit(dirPath: string, order: WalkOrder): Promise { // Reject on abort; a cached truncated listing would falsely report skills removed. @@ -68,7 +87,8 @@ export async function findSkillFiles( if (!entries) { return } - const childIndexes = new Set(enteredChildIndexes(rootPath, maxDepth, dirPath, entries)) + const childIndexes = new Set(enteredChildIndexes(rootPath, maxDepth, dirPath, entries, options)) + const leaf = options.stopAtSkillDirectories === true && holdsSkillFile(entries) for (const [index, entry] of entries.entries()) { signal?.throwIfAborted() // Hide transaction siblings so in-flight skills cannot appear twice or be deleted again. @@ -98,7 +118,12 @@ export async function findSkillFiles( } continue } - if (entry.isSymbolicLink() && isWithinDepth(rootPath, entryPath, maxDepth)) { + if ( + !leaf && + entry.isSymbolicLink() && + entersDirectory(entry.name, options) && + isWithinDepth(rootPath, entryPath, maxDepth) + ) { // Follow provider directory links but guard realpaths against cycles. let linksToDirectory = false try { diff --git a/src/main/skills/filesystem/skill-candidate-concurrency.ts b/src/main/skills/filesystem/skill-candidate-concurrency.ts index c81f1942ce9..8f2a714470f 100644 --- a/src/main/skills/filesystem/skill-candidate-concurrency.ts +++ b/src/main/skills/filesystem/skill-candidate-concurrency.ts @@ -1,7 +1,8 @@ const MAX_CONCURRENT_SKILL_CANDIDATES = 4 export async function runSkillCandidateTasks( - tasks: readonly (() => Promise)[] + tasks: readonly (() => Promise)[], + limit = MAX_CONCURRENT_SKILL_CANDIDATES ): Promise { const results = Array.from({ length: tasks.length }) let nextIndex = 0 @@ -17,8 +18,6 @@ export async function runSkillCandidateTasks( } } - await Promise.all( - Array.from({ length: Math.min(MAX_CONCURRENT_SKILL_CANDIDATES, tasks.length) }, () => worker()) - ) + await Promise.all(Array.from({ length: Math.min(limit, tasks.length) }, () => worker())) return results } diff --git a/src/renderer/src/components/native-chat/composer/pickers/NativeChatAutocompleteMenus.tsx b/src/renderer/src/components/native-chat/composer/pickers/NativeChatAutocompleteMenus.tsx index b91dbb9cd5d..ab15d6451f0 100644 --- a/src/renderer/src/components/native-chat/composer/pickers/NativeChatAutocompleteMenus.tsx +++ b/src/renderer/src/components/native-chat/composer/pickers/NativeChatAutocompleteMenus.tsx @@ -14,6 +14,7 @@ import type { NativeChatPickerItem } from '../../../../../../shared/native-chat/ import type { NativeChatMentionFiles } from './use-native-chat-mention-files' import { LoadingSpinner } from '@/components/LoadingSpinner' import { localizeSlashCommandDescription } from './native-chat-slash-command-descriptions' +import { nativeChatPickerEmptyText } from './native-chat-picker-empty-text' export const NativeChatPickerMenu = memo(function NativeChatPickerMenu({ autocomplete, @@ -48,7 +49,8 @@ export const NativeChatPickerMenu = memo(function NativeChatPickerMenu({ const showSkillsHeading = autocomplete.grouped && (skills.length > 0 || hasSkillStatus) const noMatches = autocomplete.skillStatus === 'ready' && commands.length === 0 && skills.length === 0 - const emptyText = noMatches ? getPickerEmptyText(autocomplete, t) : null + const checkingSkills = noMatches && autocomplete.skillsChecking && autocomplete.skillsEnabled + const emptyText = noMatches ? nativeChatPickerEmptyText(autocomplete, t, checkingSkills) : null const collision = commands.find((item) => item.skillCollision) const duplicate = skills.find((item) => item.sources.length > 1) // One sentence for the line and the screen reader, so the two never disagree. @@ -80,8 +82,7 @@ export const NativeChatPickerMenu = memo(function NativeChatPickerMenu({ })} {showSkillsHeading ? : null} {autocomplete.skillStatus === 'loading' ? ( - - + {t('components.native-chat.composer.loadingSkills', 'Loading skills...')} ) : null} @@ -115,7 +116,7 @@ export const NativeChatPickerMenu = memo(function NativeChatPickerMenu({ /> ) })} - {noMatches ? {emptyText} : null} + {noMatches ? {emptyText} : null}
{autocomplete.skillStatus === 'loading' ? t('components.native-chat.composer.loadingSkills', 'Loading skills...') @@ -140,19 +141,6 @@ export const NativeChatPickerMenu = memo(function NativeChatPickerMenu({ ) }) -function getPickerEmptyText( - autocomplete: Extract, - t: Translate = translate -): string { - if (!autocomplete.commandsEnabled) { - return t('components.native-chat.composer.noSkills', 'No matching skills') - } - if (autocomplete.skillsEnabled) { - return t('components.native-chat.composer.noCommandsOrSkills', 'No matching commands or skills') - } - return t('components.native-chat.composer.noCommands', 'No matching commands') -} - function PickerGroupHeading({ kind }: { kind: 'commands' | 'skills' }): React.JSX.Element { const t = useTranslate() return ( @@ -164,9 +152,16 @@ function PickerGroupHeading({ kind }: { kind: 'commands' | 'skills' }): React.JS ) } -function PickerStatus({ children }: { children: React.ReactNode }): React.JSX.Element { +function PickerStatus({ + busy = false, + children +}: { + busy?: boolean + children: React.ReactNode +}): React.JSX.Element { return (
+ {busy ? : null} {children}
) diff --git a/src/renderer/src/components/native-chat/composer/pickers/agent-session-command-menu.test-fixture.tsx b/src/renderer/src/components/native-chat/composer/pickers/agent-session-command-menu.test-fixture.tsx index ce2ca9fafbb..3ea31f03a3f 100644 --- a/src/renderer/src/components/native-chat/composer/pickers/agent-session-command-menu.test-fixture.tsx +++ b/src/renderer/src/components/native-chat/composer/pickers/agent-session-command-menu.test-fixture.tsx @@ -13,7 +13,7 @@ export const renderCommandMenu: CommandMenuRendererFixtureModule['renderCommandM })) return { commands: () => hook.result.current.read.state.commands, - setOpen: (open) => hook.result.current.menu.setOpen(open), + setOpen: (open) => hook.result.current.menu.setCommandsMenuOpen(open), unmount: hook.unmount } } diff --git a/src/renderer/src/components/native-chat/composer/pickers/native-chat-picker-empty-text.ts b/src/renderer/src/components/native-chat/composer/pickers/native-chat-picker-empty-text.ts new file mode 100644 index 00000000000..3f457c98a0e --- /dev/null +++ b/src/renderer/src/components/native-chat/composer/pickers/native-chat-picker-empty-text.ts @@ -0,0 +1,21 @@ +import { translate } from '@/i18n/i18n' +import type { Translate } from '@/i18n/use-translate' +import type { ComposerAutocomplete } from '../../../../../../shared/native-chat/input/native-chat-composer-autocomplete' + +/** While the host checks for new skills, an empty filter says so instead of reporting no match. */ +export function nativeChatPickerEmptyText( + autocomplete: Extract, + t: Translate = translate, + checking = false +): string { + if (checking) { + return t('components.native-chat.composer.checkingSkills', 'Checking for new skills...') + } + if (!autocomplete.commandsEnabled) { + return t('components.native-chat.composer.noSkills', 'No matching skills') + } + if (autocomplete.skillsEnabled) { + return t('components.native-chat.composer.noCommandsOrSkills', 'No matching commands or skills') + } + return t('components.native-chat.composer.noCommands', 'No matching commands') +} diff --git a/src/renderer/src/components/native-chat/composer/pickers/structured-agent-session-command-refresh.ts b/src/renderer/src/components/native-chat/composer/pickers/structured-agent-session-command-refresh.ts index 6f56509f275..eb1402f737c 100644 --- a/src/renderer/src/components/native-chat/composer/pickers/structured-agent-session-command-refresh.ts +++ b/src/renderer/src/components/native-chat/composer/pickers/structured-agent-session-command-refresh.ts @@ -5,22 +5,67 @@ import type { AgentSessionCommandsResult } from '../../../../../../shared/agent- import { startAgentSessionCommandMenuQuery } from '../../../../../../shared/agent-session/agent-session-command-menu-query' import { getStructuredAgentSessionReadOwner } from '../../session/structured/read/structured-agent-session-read-owner' +const CHECKING_SHOWN_AFTER_MS = 150 +const CHECKING_HELD_AFTER_MS = 250 + +async function readMenuCommands( + target: RuntimeClientTarget, + sessionId: string, + started: () => void, + finished: () => void +): Promise { + started() + try { + return await callStructuredAgentSession( + target, + 'agentSession.commands', + { sessionId } + ) + } finally { + finished() + } +} + export function useStructuredAgentSessionCommandMenu( target: RuntimeClientTarget, sessionId: string, enabled: boolean ) { const [open, setOpen] = useState(false) + // The host answers after any rescan it started, so a slow read means the list may still grow. + const [checking, setChecking] = useState(false) const query = useRef | null>(null) useEffect(() => { if (!enabled || !open) { return } + let shown: ReturnType | undefined + let hidden: ReturnType | undefined + let visible = false + const show = (next: boolean): void => { + visible = next + setChecking(next) + } const owned = startAgentSessionCommandMenuQuery( () => - callStructuredAgentSession(target, 'agentSession.commands', { - sessionId - }), + readMenuCommands( + target, + sessionId, + () => { + clearTimeout(hidden) + if (!visible) { + // Unchanged skills answer well inside this, so the common open re-renders nothing. + shown = setTimeout(() => show(true), CHECKING_SHOWN_AFTER_MS) + } + }, + () => { + clearTimeout(shown) + // The new list is pushed separately and lands just after this answer. + if (visible) { + hidden = setTimeout(() => show(false), CHECKING_HELD_AFTER_MS) + } + } + ), () => {} ) query.current = owned @@ -31,8 +76,16 @@ export function useStructuredAgentSessionCommandMenu( stop() query.current = null owned.dispose() + clearTimeout(shown) + clearTimeout(hidden) + show(false) } }, [enabled, open, sessionId, target]) const refresh = useCallback(() => query.current?.invalidate(), []) - return { setOpen, refresh } + // Named as the session controller exposes them. + return { + setCommandsMenuOpen: setOpen, + refreshCommands: refresh, + commandsChecking: enabled && open && checking + } } diff --git a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-composer-command-picker.ts b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-composer-command-picker.ts index bb66481bde6..66606d8c160 100644 --- a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-composer-command-picker.ts +++ b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-composer-command-picker.ts @@ -22,6 +22,7 @@ export function useNativeChatComposerCommandPicker( | 'sessionSkills' | 'onPickerOpened' | 'onPickerDemand' + | 'skillsChecking' | 'launchWorktreeId' | keyof ResumeArgs['editor'] > & @@ -55,6 +56,7 @@ export function useNativeChatComposerCommandPicker( commandAvailable, sessionSkills, onPickerDemand: structuredTransport?.setCommandsMenuOpen, + skillsChecking: structuredTransport?.commandsChecking === true, ...(!structuredTransport?.setCommandsMenuOpen && structuredTransport?.refreshCommands ? { onPickerOpened: structuredTransport.refreshCommands } : {}) diff --git a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts index c56cd09bac7..b5c57300341 100644 --- a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts +++ b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts @@ -73,6 +73,7 @@ export function useNativeChatPickerState(args: { /** Refresh host catalogs on every / or $ menu opening, even in resting chats. */ onPickerOpened?: () => void onPickerDemand?: (open: boolean) => void + skillsChecking?: boolean textareaRef: RefObject setDraft: (value: string) => void setCaret: Dispatch> @@ -93,6 +94,7 @@ export function useNativeChatPickerState(args: { commandAvailable, onPickerOpened, onPickerDemand, + skillsChecking = false, textareaRef, setDraft, setCaret, @@ -119,7 +121,7 @@ export function useNativeChatPickerState(args: { const [dismissed, setDismissed] = useState<{ context: string; triggerKey: string } | null>(null) const skillOriginRef = useRef(null) const lastOpenKeyRef = useRef(null) - const autocomplete = useMemo( + const derivedAutocomplete = useMemo( () => recalledFromHistory || disabled ? { mode: 'none' } @@ -148,6 +150,13 @@ export function useNativeChatPickerState(args: { commandAvailable ] ) + const autocomplete = useMemo( + () => + skillsChecking && derivedAutocomplete.mode === 'slash' + ? { ...derivedAutocomplete, skillsChecking } + : derivedAutocomplete, + [derivedAutocomplete, skillsChecking] + ) useEffect(() => { // Reset dismissal/origin on context changes so a reused composer cannot retain a prior pane’s suppression. diff --git a/src/renderer/src/components/native-chat/composer/submit/native-chat-structured-composer-transport.ts b/src/renderer/src/components/native-chat/composer/submit/native-chat-structured-composer-transport.ts index ee48bfc01d9..f55055ad62d 100644 --- a/src/renderer/src/components/native-chat/composer/submit/native-chat-structured-composer-transport.ts +++ b/src/renderer/src/components/native-chat/composer/submit/native-chat-structured-composer-transport.ts @@ -38,6 +38,8 @@ export type NativeChatStructuredComposerTransport = { /** Request command refresh; updated choices arrive through sessionCommands. */ refreshCommands?: () => void setCommandsMenuOpen?: (open: boolean) => void + /** The open menu's host read is in flight; an empty filter may still fill in. */ + commandsChecking?: boolean /** False when the agent takes no image input, as its host registered it. */ acceptsImages?: boolean /** The session's context usage; null until the journal can state it. */ diff --git a/src/renderer/src/components/native-chat/composer/submit/use-native-chat-structured-composer-transport.ts b/src/renderer/src/components/native-chat/composer/submit/use-native-chat-structured-composer-transport.ts index 1ed8e1b5d77..ed1c441b3fc 100644 --- a/src/renderer/src/components/native-chat/composer/submit/use-native-chat-structured-composer-transport.ts +++ b/src/renderer/src/components/native-chat/composer/submit/use-native-chat-structured-composer-transport.ts @@ -174,6 +174,7 @@ export function useNativeChatStructuredComposerTransport(args: { sessionCommands: controller.sessionCommands, refreshCommands: controller.refreshCommands, setCommandsMenuOpen: controller.setCommandsMenuOpen, + commandsChecking: controller.commandsChecking, acceptsImages, contextUsage: controller.contextUsage, worktreeId, diff --git a/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts b/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts index 6ab811b914e..f476b79f90a 100644 --- a/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts +++ b/src/renderer/src/components/native-chat/session/structured/use-structured-agent-session.ts @@ -314,8 +314,7 @@ export function useStructuredAgentSession(args: StructuredAgentSessionViewArgs) optionSnapshot, optionSurface, sessionCommands: transportEnabled ? (state.commands ?? undefined) : undefined, - refreshCommands: commandMenu.refresh, - setCommandsMenuOpen: commandMenu.setOpen, + ...commandMenu, setStructuredOption, /** Why the host's catalog probe says no chat can start here; the chat shows it as a notice. */ unavailable, diff --git a/src/renderer/src/i18n/locales/de.json b/src/renderer/src/i18n/locales/de.json index f3a4b0cf91c..b4bdcb17e1b 100644 --- a/src/renderer/src/i18n/locales/de.json +++ b/src/renderer/src/i18n/locales/de.json @@ -19484,6 +19484,7 @@ "noCommands": "Keine passenden Befehle", "commands": "Befehle", "skills": "Skills", + "checkingSkills": "Nach neuen Skills wird gesucht…", "loadingSkills": "Skills werden geladen…", "skillsLoaded": "Skills geladen", "clearDescription": "Unterhaltungskontext leeren", diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index a4e8442aab0..319c6d1814e 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -20044,6 +20044,7 @@ "noCommands": "No matching commands", "commands": "Commands", "skills": "Skills", + "checkingSkills": "Checking for new skills...", "loadingSkills": "Loading skills...", "skillsLoaded": "Skills loaded", "clearDescription": "Clear conversation context", diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index cd9fe9525d0..b64fd3a94c9 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -19231,6 +19231,7 @@ "noCommandsOrSkills": "No hay comandos o habilidades coincidentes", "commands": "Comandos", "skills": "Habilidades", + "checkingSkills": "Buscando habilidades nuevas…", "loadingSkills": "Cargando habilidades…", "clearDescription": "Borrar el contexto de la conversación", "skillsUnavailableHost": "Las habilidades no se pueden listar en los chats SSH", diff --git a/src/renderer/src/i18n/locales/fr.json b/src/renderer/src/i18n/locales/fr.json index 820994f51ac..6cf39d933a0 100644 --- a/src/renderer/src/i18n/locales/fr.json +++ b/src/renderer/src/i18n/locales/fr.json @@ -19401,6 +19401,7 @@ "noCommands": "Aucune commande correspondante", "commands": "Commandes", "skills": "Skills", + "checkingSkills": "Recherche de nouvelles compétences…", "loadingSkills": "Chargement des skills...", "skillsLoaded": "Skills chargées", "clearDescription": "Effacer le contexte de la conversation", diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index 3eb7af10b9c..85177fb5d92 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -18808,6 +18808,7 @@ "noCommandsOrSkills": "一致するコマンドやスキルがありません", "commands": "コマンド", "skills": "スキル", + "checkingSkills": "新しいスキルを確認しています…", "loadingSkills": "スキルを読み込み中…", "clearDescription": "会話のコンテキストをクリア", "skillsUnavailableHost": "SSH チャットではスキルを一覧表示できません", diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index fc16b0f3419..184de529643 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -18808,6 +18808,7 @@ "noCommandsOrSkills": "일치하는 명령어 또는 스킬이 없습니다", "commands": "명령어", "skills": "스킬", + "checkingSkills": "새 스킬을 확인하는 중…", "loadingSkills": "스킬 로딩 중…", "clearDescription": "대화 컨텍스트 지우기", "skillsUnavailableHost": "SSH 채팅에서는 스킬 목록을 표시할 수 없습니다", diff --git a/src/renderer/src/i18n/locales/pt.json b/src/renderer/src/i18n/locales/pt.json index b9919f279fb..a09ee3f3ed1 100644 --- a/src/renderer/src/i18n/locales/pt.json +++ b/src/renderer/src/i18n/locales/pt.json @@ -20219,6 +20219,7 @@ "noCommands": "Nenhum comando correspondente", "commands": "Comandos", "skills": "Skills", + "checkingSkills": "Procurando novas habilidades…", "loadingSkills": "Carregando skills...", "skillsLoaded": "Skills carregadas", "skillsUnavailableHost": "As skills não estão disponíveis para este host", diff --git a/src/renderer/src/i18n/locales/ru.json b/src/renderer/src/i18n/locales/ru.json index c9b7d4d159a..d130f088ef4 100644 --- a/src/renderer/src/i18n/locales/ru.json +++ b/src/renderer/src/i18n/locales/ru.json @@ -20062,6 +20062,7 @@ "noCommands": "Подходящих команд нет", "commands": "Команды", "skills": "Навыки", + "checkingSkills": "Поиск новых навыков…", "loadingSkills": "Загрузка навыков…", "skillsLoaded": "Навыки загружены", "clearDescription": "Очистить контекст разговора", diff --git a/src/renderer/src/i18n/locales/tr.json b/src/renderer/src/i18n/locales/tr.json index 1a4410c50d5..a4a78b58241 100644 --- a/src/renderer/src/i18n/locales/tr.json +++ b/src/renderer/src/i18n/locales/tr.json @@ -19469,6 +19469,7 @@ "noCommands": "Eşleşen komut yok", "commands": "Komutlar", "skills": "Beceriler", + "checkingSkills": "Yeni beceriler denetleniyor…", "loadingSkills": "Beceriler yükleniyor...", "skillsLoaded": "Beceriler yüklendi", "clearDescription": "Konuşma bağlamını temizle", diff --git a/src/renderer/src/i18n/locales/zh-Hant.json b/src/renderer/src/i18n/locales/zh-Hant.json index 5d545f01c37..8c73ced2e77 100644 --- a/src/renderer/src/i18n/locales/zh-Hant.json +++ b/src/renderer/src/i18n/locales/zh-Hant.json @@ -19223,6 +19223,7 @@ "noCommands": "沒有符合的命令", "commands": "命令", "skills": "技能", + "checkingSkills": "正在檢查新技能…", "loadingSkills": "正在載入技能…", "skillsLoaded": "技能已載入", "clearDescription": "清除對話上下文", diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index be1ebb988a9..60049e1fe35 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -18750,6 +18750,7 @@ "noCommandsOrSkills": "没有匹配的命令或技能", "commands": "命令", "skills": "技能", + "checkingSkills": "正在检查新技能…", "loadingSkills": "正在加载技能…", "clearDescription": "清除对话上下文", "skillsUnavailableHost": "SSH 聊天中无法列出技能", diff --git a/src/shared/native-chat/input/native-chat-composer-autocomplete.ts b/src/shared/native-chat/input/native-chat-composer-autocomplete.ts index 51a96379a15..3794b4e2ae0 100644 --- a/src/shared/native-chat/input/native-chat-composer-autocomplete.ts +++ b/src/shared/native-chat/input/native-chat-composer-autocomplete.ts @@ -26,6 +26,8 @@ type PickerAutocomplete = { skillsEnabled: boolean skillStatus: NativeChatSkillDiscoverySnapshot['status'] skillErrorKind?: NativeChatSkillDiscoverySnapshot['errorKind'] + /** The host is checking for skills added since its last list, so an empty filter may fill in. */ + skillsChecking?: boolean } export type ComposerAutocomplete = From 74c1e942183a74dbefe5b8e96d330a0c66d6df3a Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:26:42 +0000 Subject: [PATCH 30/80] fix(mobile): translate the Markdown reader's status labels and delete the unused reader (#40068) --- .../MobileMarkdownReader.tsx | 188 ------------------ .../markdown-floating-actions-layout.ts | 16 -- .../mobile-markdown-reader-status.ts | 15 +- mobile/src/i18n/locales/de.json | 3 +- mobile/src/i18n/locales/en.json | 3 +- mobile/src/i18n/locales/es.json | 3 +- mobile/src/i18n/locales/fr.json | 3 +- mobile/src/i18n/locales/ja.json | 3 +- mobile/src/i18n/locales/ko.json | 3 +- mobile/src/i18n/locales/pt.json | 3 +- mobile/src/i18n/locales/ru.json | 3 +- mobile/src/i18n/locales/tr.json | 3 +- mobile/src/i18n/locales/zh-Hant.json | 3 +- mobile/src/i18n/locales/zh.json | 3 +- 14 files changed, 22 insertions(+), 230 deletions(-) delete mode 100644 mobile/src/files/session-documents/MobileMarkdownReader.tsx diff --git a/mobile/src/files/session-documents/MobileMarkdownReader.tsx b/mobile/src/files/session-documents/MobileMarkdownReader.tsx deleted file mode 100644 index 5dcecdbb970..00000000000 --- a/mobile/src/files/session-documents/MobileMarkdownReader.tsx +++ /dev/null @@ -1,188 +0,0 @@ -import { useRef, useState } from 'react' -import { ActivityIndicator, Pressable, Text, View } from 'react-native' -import { ChevronDown, Keyboard as KeyboardIcon, RefreshCw } from 'lucide-react-native' -import { - MobileRichMarkdownEditor, - type MobileRichMarkdownEditorHandle -} from '../../components/MobileRichMarkdownEditor' -import { colors, spacing, pressedStyle } from '../../theme/mobile-theme' -import { useMobileColorScheme } from '../../theme/use-mobile-color-scheme' -import { - resolveMarkdownFloatingActionsBottom, - shouldShowMarkdownFloatingActions -} from './markdown-floating-actions-layout' -import type { MarkdownDocState } from './session-document-state' -import { styles } from '../../session/mobile-session-styles' -import { MobileLoadErrorState, MobileStateView } from '../../components/MobileStateView' -import { useMobileTranslation } from '../../i18n/use-mobile-translation' - -type Props = { - documentId: string - doc: MarkdownDocState | undefined - onRefresh: () => void - onChange: (content: string) => void - onSave: () => void - onCopy: () => void - onDiscard: () => void - keyboardLift: number -} - -export function MobileMarkdownReader({ - documentId, - doc, - onRefresh, - onChange, - onSave, - onCopy, - onDiscard, - keyboardLift -}: Props) { - useMobileColorScheme() - const t = useMobileTranslation() - const editorRef = useRef(null) - // Native Keyboard events under-report the WebView editor's covered area, so prefer the larger WebView-measured inset. - const [webviewKeyboardInset, setWebviewKeyboardInset] = useState(0) - const effectiveKeyboardLift = Math.max(keyboardLift, webviewKeyboardInset) - const keyboardOpen = effectiveKeyboardLift > 0 - - if (!doc || doc.status === 'loading') { - return ( - - ) - } - if (doc.status === 'error') { - return - } - - const statusText = doc.saveError - ? doc.saveError - : doc.readOnlyReason - ? t('session.markdownReader.readOnly', 'Read only') - : doc.stale - ? t('session.markdownReader.changedOnDesktop', 'Changed on desktop') - : null - const showRefresh = Boolean((doc.stale && !doc.isDirty) || !doc.editable) - const showCopy = Boolean(doc.saveError || !doc.editable) - const showSave = Boolean(doc.isDirty || doc.saving) - const showFloatingActions = shouldShowMarkdownFloatingActions({ - keyboardLift: effectiveKeyboardLift, - hasStatus: statusText != null, - showRefresh, - showCopy, - showSave - }) - - return ( - - - {showFloatingActions ? ( - - {statusText ? ( - - {statusText} - - ) : null} - - {keyboardOpen ? ( - [ - styles.markdownFloatingButton, - styles.markdownKeyboardDismissButton, - pressed && pressedStyle - ]} - onPress={() => editorRef.current?.dismissKeyboard()} - hitSlop={8} - accessibilityRole="button" - accessibilityLabel={t('session.markdownReader.dismissKeyboard', 'Dismiss keyboard')} - accessibilityHint={t( - 'session.markdownReader.dismissKeyboardHint', - 'Hides the software keyboard and keeps the markdown editor open.' - )} - > - - - - - - ) : null} - {showCopy ? ( - [styles.markdownFloatingButton, pressed && pressedStyle]} - onPress={onCopy} - > - {t('common.copy', 'Copy')} - - ) : null} - {showRefresh ? ( - [styles.markdownFloatingButton, pressed && pressedStyle]} - onPress={onRefresh} - > - - - {t('common.refresh', 'Refresh')} - - - ) : null} - {doc.isDirty ? ( - [styles.markdownFloatingButton, pressed && pressedStyle]} - onPress={onDiscard} - > - - {t('session.markdownReader.discard', 'Discard')} - - - ) : null} - {showSave ? ( - [ - styles.markdownFloatingButton, - styles.markdownSaveButton, - (!doc.editable || !doc.isDirty || doc.saving) && styles.markdownButtonDisabled, - pressed && pressedStyle - ]} - disabled={!doc.editable || !doc.isDirty || doc.saving} - onPress={onSave} - > - {doc.saving ? ( - - ) : ( - {t('common.save', 'Save')} - )} - - ) : null} - - - ) : null} - - ) -} diff --git a/mobile/src/files/session-documents/markdown-floating-actions-layout.ts b/mobile/src/files/session-documents/markdown-floating-actions-layout.ts index bc506a88a9d..06aa5b9223f 100644 --- a/mobile/src/files/session-documents/markdown-floating-actions-layout.ts +++ b/mobile/src/files/session-documents/markdown-floating-actions-layout.ts @@ -9,19 +9,3 @@ export function resolveMarkdownFloatingActionsBottom({ }): number { return keyboardLift > 0 ? keyboardLift + liftedClearance : restingBottom } - -export function shouldShowMarkdownFloatingActions({ - keyboardLift, - hasStatus, - showRefresh, - showCopy, - showSave -}: { - keyboardLift: number - hasStatus: boolean - showRefresh: boolean - showCopy: boolean - showSave: boolean -}): boolean { - return keyboardLift > 0 || hasStatus || showRefresh || showCopy || showSave -} diff --git a/mobile/src/files/session-documents/mobile-markdown-reader-status.ts b/mobile/src/files/session-documents/mobile-markdown-reader-status.ts index 9e79d7adce9..e238cda5922 100644 --- a/mobile/src/files/session-documents/mobile-markdown-reader-status.ts +++ b/mobile/src/files/session-documents/mobile-markdown-reader-status.ts @@ -1,3 +1,4 @@ +import { translate } from '../../i18n/mobile-i18n' import { formatPreviewByteLength } from '../mobile-file-preview-response' import type { MarkdownDocState } from './session-document-state' @@ -9,11 +10,17 @@ export function markdownReaderStatusText( } if (doc.truncated) { return doc.byteLength === undefined - ? 'Preview truncated.' - : `Preview truncated. File size: ${formatPreviewByteLength(doc.byteLength)}.` + ? translate('session.markdownReader.previewTruncated', 'Preview truncated.') + : translate( + 'files.filePreviewSourceText.truncatedNote', + 'Preview truncated. File size: {{size}}.', + { size: formatPreviewByteLength(doc.byteLength) } + ) } if (doc.readOnlyReason) { - return 'Read only' + return translate('session.markdownReader.readOnly', 'Read only') } - return doc.stale ? 'Changed on desktop' : null + return doc.stale + ? translate('session.markdownReader.changedOnDesktop', 'Changed on desktop') + : null } diff --git a/mobile/src/i18n/locales/de.json b/mobile/src/i18n/locales/de.json index a2da39a8e47..2a9c0de8508 100644 --- a/mobile/src/i18n/locales/de.json +++ b/mobile/src/i18n/locales/de.json @@ -2193,8 +2193,7 @@ "copyPathLabel": "Pfad der Markdown-Datei kopieren", "copySource": "Markdown-Quelltext kopieren", "discard": "Verwerfen", - "dismissKeyboard": "Tastatur ausblenden", - "dismissKeyboardHint": "Blendet die Bildschirmtastatur aus und lässt den Markdown-Editor geöffnet.", + "previewTruncated": "Vorschau gekürzt.", "readOnly": "Schreibgeschützt" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/en.json b/mobile/src/i18n/locales/en.json index 92a11862440..fc4eaba6cd2 100644 --- a/mobile/src/i18n/locales/en.json +++ b/mobile/src/i18n/locales/en.json @@ -1962,8 +1962,7 @@ "markdownReader": { "readOnly": "Read only", "changedOnDesktop": "Changed on desktop", - "dismissKeyboard": "Dismiss keyboard", - "dismissKeyboardHint": "Hides the software keyboard and keeps the markdown editor open.", + "previewTruncated": "Preview truncated.", "discard": "Discard", "copyLoadedSource": "Copy loaded Markdown source", "copySource": "Copy Markdown source", diff --git a/mobile/src/i18n/locales/es.json b/mobile/src/i18n/locales/es.json index 615bc3008fa..2afe3233ac1 100644 --- a/mobile/src/i18n/locales/es.json +++ b/mobile/src/i18n/locales/es.json @@ -2260,8 +2260,7 @@ "copyPathLabel": "Copiar la ruta del archivo Markdown", "copySource": "Copiar el código fuente Markdown", "discard": "Descartar", - "dismissKeyboard": "Ocultar teclado", - "dismissKeyboardHint": "Oculta el teclado en pantalla y mantiene abierto el editor de Markdown.", + "previewTruncated": "Vista previa truncada.", "readOnly": "Solo lectura" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/fr.json b/mobile/src/i18n/locales/fr.json index b4968d0c4b4..fbfb961983b 100644 --- a/mobile/src/i18n/locales/fr.json +++ b/mobile/src/i18n/locales/fr.json @@ -2260,8 +2260,7 @@ "copyPathLabel": "Copier le chemin du fichier Markdown", "copySource": "Copier la source Markdown", "discard": "Abandonner", - "dismissKeyboard": "Masquer le clavier", - "dismissKeyboardHint": "Masque le clavier virtuel et laisse l'éditeur Markdown ouvert.", + "previewTruncated": "Aperçu tronqué.", "readOnly": "Lecture seule" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/ja.json b/mobile/src/i18n/locales/ja.json index 41b59f00545..954f8547baf 100644 --- a/mobile/src/i18n/locales/ja.json +++ b/mobile/src/i18n/locales/ja.json @@ -2189,8 +2189,7 @@ "copyPathLabel": "Markdown ファイルのパスをコピー", "copySource": "Markdown ソースをコピー", "discard": "破棄", - "dismissKeyboard": "キーボードを閉じる", - "dismissKeyboardHint": "Markdown エディターを開いたまま、ソフトウェアキーボードを非表示にします。", + "previewTruncated": "プレビューは一部省略されています。", "readOnly": "読み取り専用" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/ko.json b/mobile/src/i18n/locales/ko.json index 7c34395812a..826352f43aa 100644 --- a/mobile/src/i18n/locales/ko.json +++ b/mobile/src/i18n/locales/ko.json @@ -2189,8 +2189,7 @@ "copyPathLabel": "Markdown 경로 복사", "copySource": "Markdown 소스 복사", "discard": "버리기", - "dismissKeyboard": "키보드 닫기", - "dismissKeyboardHint": "소프트웨어 키보드를 숨기고 Markdown 편집기를 열린 상태로 유지합니다.", + "previewTruncated": "미리보기가 일부만 표시됩니다.", "readOnly": "읽기 전용" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/pt.json b/mobile/src/i18n/locales/pt.json index fb83a84674d..ffa929cb422 100644 --- a/mobile/src/i18n/locales/pt.json +++ b/mobile/src/i18n/locales/pt.json @@ -2260,8 +2260,7 @@ "copyPathLabel": "Copiar caminho do arquivo Markdown", "copySource": "Copiar código-fonte Markdown", "discard": "Descartar", - "dismissKeyboard": "Ocultar teclado", - "dismissKeyboardHint": "Oculta o teclado virtual e mantém o editor Markdown aberto.", + "previewTruncated": "Pré-visualização truncada.", "readOnly": "Somente leitura" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/ru.json b/mobile/src/i18n/locales/ru.json index 9ced7c69b59..946c5ce0a27 100644 --- a/mobile/src/i18n/locales/ru.json +++ b/mobile/src/i18n/locales/ru.json @@ -2327,8 +2327,7 @@ "copyPathLabel": "Скопировать путь к файлу Markdown", "copySource": "Скопировать исходный текст Markdown", "discard": "Отменить изменения", - "dismissKeyboard": "Скрыть клавиатуру", - "dismissKeyboardHint": "Скрывает экранную клавиатуру и оставляет редактор Markdown открытым.", + "previewTruncated": "Показана часть файла.", "readOnly": "Только чтение" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/tr.json b/mobile/src/i18n/locales/tr.json index f103d17aec0..5e3a4d21824 100644 --- a/mobile/src/i18n/locales/tr.json +++ b/mobile/src/i18n/locales/tr.json @@ -2193,8 +2193,7 @@ "copyPathLabel": "Markdown yolunu kopyala", "copySource": "Markdown kaynağını kopyala", "discard": "Vazgeç", - "dismissKeyboard": "Klavyeyi gizle", - "dismissKeyboardHint": "Ekran klavyesini gizler ve Markdown düzenleyicisini açık tutar.", + "previewTruncated": "Önizleme kısaltıldı.", "readOnly": "Salt okunur" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/zh-Hant.json b/mobile/src/i18n/locales/zh-Hant.json index b41f7765fec..9c74b7b9ccd 100644 --- a/mobile/src/i18n/locales/zh-Hant.json +++ b/mobile/src/i18n/locales/zh-Hant.json @@ -2189,8 +2189,7 @@ "copyPathLabel": "複製 Markdown 路徑", "copySource": "複製 Markdown 原始碼", "discard": "放棄", - "dismissKeyboard": "收起鍵盤", - "dismissKeyboardHint": "隱藏螢幕鍵盤,並保持 Markdown 編輯器開啟。", + "previewTruncated": "預覽內容已截斷。", "readOnly": "唯讀" }, "nativeChatAttachActions": { diff --git a/mobile/src/i18n/locales/zh.json b/mobile/src/i18n/locales/zh.json index b99d10462d5..0e08b7072b4 100644 --- a/mobile/src/i18n/locales/zh.json +++ b/mobile/src/i18n/locales/zh.json @@ -2189,8 +2189,7 @@ "copyPathLabel": "复制 Markdown 文件路径", "copySource": "复制 Markdown 源码", "discard": "放弃", - "dismissKeyboard": "关闭键盘", - "dismissKeyboardHint": "隐藏软键盘,并保持 Markdown 编辑器打开。", + "previewTruncated": "预览已截断。", "readOnly": "只读" }, "nativeChatAttachActions": { From d821a0a78260d13900e0ffd347df9e50e0f80e7f Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:38:02 +0000 Subject: [PATCH 31/80] fix(mobile): lift the file preview restore bar above the keyboard (#40075) --- mobile/src/files/MobileFilePreviewEditDraft.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mobile/src/files/MobileFilePreviewEditDraft.tsx b/mobile/src/files/MobileFilePreviewEditDraft.tsx index 13ede3827b2..bea1c2ab81b 100644 --- a/mobile/src/files/MobileFilePreviewEditDraft.tsx +++ b/mobile/src/files/MobileFilePreviewEditDraft.tsx @@ -6,9 +6,11 @@ import { } from '../drafts/use-document-edit-draft' import { useClipboardWriter } from '../platform/clipboard' import { triggerError, triggerSuccess } from '../platform/haptics' +import { useKeyboardOcclusion } from '../platform/keyboard-occlusion' import { spacing } from '../theme/mobile-theme' import { useSafeAreaInsets } from 'react-native-safe-area-context' import type { MobileFilePreviewSource } from './mobile-file-preview-request' +import { resolveMarkdownFloatingActionsBottom } from './session-documents/markdown-floating-actions-layout' /** Only terminal artifacts are editable in the file preview. */ export function filePreviewEditDraftKey( @@ -43,10 +45,15 @@ function OfferBar(props: { }) { const clipboard = useClipboardWriter() const insets = useSafeAreaInsets() + const keyboardHeight = useKeyboardOcclusion() return ( void clipboard.writeText(props.offer.content).then(triggerSuccess, triggerError) } From e290270a738a30acd4aa4e66f89dca7a683f2837 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:58:10 +0000 Subject: [PATCH 32/80] fix: repair main's session registry import and isolate the popup Electron fixture (#40074) * fix(browser): import the session registry from its new profiles folder #40002 moved browser-session-registry into sessions/profiles, and #39613 landed after it with the old path, so main no longer typechecks or builds. * fix(test): keep the popup Electron fixture in its own worker #40067 lets unit files share a worker's module cache. The popup compatibility fixture keeps its Electron app, mock server and temp root at module level, so after the first file the others reuse a closed browser. --- src/main/browser/extensions/browser-extension-commands.test.ts | 2 +- src/main/browser/extensions/browser-extension-commands.ts | 2 +- .../compatibility/browser-popup-compatibility-cdp-fixture.ts | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/browser/extensions/browser-extension-commands.test.ts b/src/main/browser/extensions/browser-extension-commands.test.ts index a405dbf4ca1..659a8f021cf 100644 --- a/src/main/browser/extensions/browser-extension-commands.test.ts +++ b/src/main/browser/extensions/browser-extension-commands.test.ts @@ -14,7 +14,7 @@ const mocks = vi.hoisted(() => { reload: vi.fn(async (): Promise => ({ ok: true })) } }) -vi.mock('../sessions/browser-session-registry', () => ({ browserSessionRegistry: {} })) +vi.mock('../sessions/profiles/browser-session-registry', () => ({ browserSessionRegistry: {} })) vi.mock('./installation/browser-extension-install-store', () => ({ getBrowserExtensionInstallStore: () => ({ list: () => mocks.installed }) })) diff --git a/src/main/browser/extensions/browser-extension-commands.ts b/src/main/browser/extensions/browser-extension-commands.ts index e3d3f751cc5..04d4916a17d 100644 --- a/src/main/browser/extensions/browser-extension-commands.ts +++ b/src/main/browser/extensions/browser-extension-commands.ts @@ -8,7 +8,7 @@ import type { InstalledBrowserExtension } from '../../../shared/browser/extensions/browser-extension-types' import { BrowserError } from '../browser-error' -import { browserSessionRegistry } from '../sessions/browser-session-registry' +import { browserSessionRegistry } from '../sessions/profiles/browser-session-registry' import { getBrowserExtensionInstallStore } from './installation/browser-extension-install-store' import { listBrowserExtensionsForProfile, diff --git a/src/main/browser/guest-lifecycle/popups/compatibility/browser-popup-compatibility-cdp-fixture.ts b/src/main/browser/guest-lifecycle/popups/compatibility/browser-popup-compatibility-cdp-fixture.ts index b52c38e018e..742e76bdbed 100644 --- a/src/main/browser/guest-lifecycle/popups/compatibility/browser-popup-compatibility-cdp-fixture.ts +++ b/src/main/browser/guest-lifecycle/popups/compatibility/browser-popup-compatibility-cdp-fixture.ts @@ -1,3 +1,4 @@ +// @orca-isolated-worker: the Electron app, mock server and temp root live at module level, one per test file. import type { PopupCompatibilityStatus } from './browser-popup-compatibility-status' import { spawnProcess } from '@orca/process-host' import { createServer } from 'node:http' From 57d45efe002173680d43ffe567df313df55c8327 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:02:45 +0000 Subject: [PATCH 33/80] fix(mobile): paint host labels and relay server rows from the phone's store on relaunch (#40086) * fix(mobile): paint host labels and relay server rows from the phone's store on relaunch * refactor(mobile): reuse the SSH target summary schema for stored host labels --- ...tings-repo-metadata-refuse-after-data.json | 47 +++++------------ mobile/src/host-screen/host-list-queries.ts | 52 +++++++++++++++++-- .../host-screen/host-screen-reply-schema.ts | 21 ++++---- .../src/host-screen/use-host-repo-metadata.ts | 2 +- .../host-screen/use-host-screen-controller.ts | 4 +- .../host-screen/use-host-worktree-catalog.ts | 18 ++++--- mobile/src/query/first-paint-query-keys.ts | 16 ++++++ 7 files changed, 101 insertions(+), 59 deletions(-) diff --git a/mobile/rpc-foundation/goldens/settings-repo-metadata-refuse-after-data.json b/mobile/rpc-foundation/goldens/settings-repo-metadata-refuse-after-data.json index 3b737599abd..1ccd86c7a3e 100644 --- a/mobile/rpc-foundation/goldens/settings-repo-metadata-refuse-after-data.json +++ b/mobile/rpc-foundation/goldens/settings-repo-metadata-refuse-after-data.json @@ -35,17 +35,6 @@ "ordinal": 9, "json": "{\"id\":\"frame-3\",\"deviceToken\":\"recording-device\",\"method\":\"ssh.listTargetSummaries\"}" }, - "18c2b8e092d7": { - "name": "streams-registered-at-teardown", - "ordinal": 26, - "value": [ - { - "cancelled": true, - "method": "runtime.clientEvents.subscribe", - "payload": "runtime.clientEvents.subscribe#1" - } - ] - }, "20200b8feac4": { "name": "host.platform#1", "ordinal": 6, @@ -106,6 +95,17 @@ "startedAt": 0 } }, + "44eb39507b75": { + "name": "streams-registered-at-teardown", + "ordinal": 23, + "value": [ + { + "cancelled": true, + "method": "runtime.clientEvents.subscribe", + "payload": "runtime.clientEvents.subscribe#1" + } + ] + }, "564f54c8d902": { "name": "host.platform#1", "ordinal": 6, @@ -137,11 +137,6 @@ "ordinal": 13, "value": [["ssh:ssh-1", "SSH"]] }, - "59ab92fbdbc0": { - "name": "hostPlatform", - "ordinal": 25, - "value": "linux" - }, "6770aa1912e4": { "name": "repo.list#2", "ordinal": 15, @@ -411,11 +406,6 @@ "status": "pending", "startedAt": 0 }, - "9c290301a4bc": { - "name": "hostHealthById", - "ordinal": 23, - "value": [] - }, "b2746491ac07": { "name": "repo.list#1", "ordinal": 2, @@ -642,11 +632,6 @@ "name": "hostPlatform", "ordinal": 14, "value": "linux" - }, - "fb797b9761e1": { - "name": "hostLabelById", - "ordinal": 24, - "value": [["ssh:ssh-1", "SSH"]] } }, "recording": { @@ -795,10 +780,7 @@ "8cfdd18c5fe6", "84c1dde54c62", "56ab81cb67dc", - "f9193cae5824", - "9c290301a4bc", - "fb797b9761e1", - "59ab92fbdbc0" + "f9193cae5824" ] } }, @@ -838,10 +820,7 @@ "84c1dde54c62", "56ab81cb67dc", "f9193cae5824", - "9c290301a4bc", - "fb797b9761e1", - "59ab92fbdbc0", - "18c2b8e092d7" + "44eb39507b75" ] } } diff --git a/mobile/src/host-screen/host-list-queries.ts b/mobile/src/host-screen/host-list-queries.ts index c65ded233ea..caa752201fd 100644 --- a/mobile/src/host-screen/host-list-queries.ts +++ b/mobile/src/host-screen/host-list-queries.ts @@ -1,5 +1,10 @@ import * as z from 'zod' -import { getRepoExecutionHostId, parseExecutionHostId } from '../../../src/shared/execution-host' +import { + getRepoExecutionHostId, + parseExecutionHostId, + type ExecutionHostId +} from '../../../src/shared/execution-host' +import { MOBILE_RELAY_HOST_WORKTREE_DELTAS_RUNTIME_CAPABILITY } from '../../../src/shared/mobile-desktop-relay-contract' import type { MobileRelayHost } from '../../../src/shared/remote-runtime/relay/mobile-relay-hosts-contract' import type { Query, QueryClient } from '@tanstack/react-query' import { defineHostQuery } from '../query/host-query' @@ -18,8 +23,13 @@ import { } from '../worktree/worktree-catalog-snapshot-client' import type { Worktree } from '../worktree/workspace-list-types' import { getCachedWorktrees } from '../cache/worktree-cache' -import { hostRepoCatalogRepos, type HostRepoCatalogRow } from './host-screen-reply-schema' +import { + hostRepoCatalogRepos, + hostSshTargetSummarySchema, + type HostRepoCatalogRow +} from './host-screen-reply-schema' import { hostPlatformRead, hostRepoCatalogSnapshotRead } from './host-screen-operations' +import { NODE_PLATFORM_NAMES } from '../transport/connection/mobile-runtime-host-platform' import { acceptedMetadata, readHostSshTargetLabels, @@ -119,6 +129,18 @@ const serverRowSnapshots = { full: new ServerRowSnapshots(false) } +/** + * The server rows' input. Before the host's status is read it names the key a current desktop stores them + * under, so they paint from the store: the page came from that desktop, which sends the deltas capability. + */ +export function hostServerWorkspacesInput(hostCapabilities: readonly string[] | null): { + deltas: boolean +} { + return { + deltas: hostCapabilities?.includes(MOBILE_RELAY_HOST_WORKTREE_DELTAS_RUNTIME_CAPABILITY) ?? true + } +} + // Workspaces on the servers this desktop relays to; a server whose read fails keeps its rows. export const hostServerWorkspacesQuery = defineHostQuery<{ deltas: boolean }, ServerWorkspaces>({ family: 'relayHosts.workspaces', @@ -187,10 +209,18 @@ export function catalogNeedsHostLabels(catalog: readonly HostRepoCatalogRow[]): return hostIds.size > 1 || [...hostIds].some((id) => parseExecutionHostId(id)?.kind === 'ssh') } -export type HostRepoLabels = Awaited> & { +export type HostRepoLabels = { + sshTargetSummaries: z.output[] + // Entries rather than a Map, so the record can be stored. + hostLabels: [ExecutionHostId, string][] platform: NodeJS.Platform | null } +const storedHostLabelSchema = z.tuple([ + z.custom((id) => typeof id === 'string' && parseExecutionHostId(id) !== null), + z.string() +]) + export const hostRepoLabelsQuery = defineHostQuery({ family: 'repo.hostLabels', fetch: async (client) => { @@ -198,13 +228,25 @@ export const hostRepoLabelsQuery = defineHostQuery hostPlatformRead.request(client)) ]) - return { ...labels, platform: acceptedMetadata(platform, hostPlatformRead.interpret) ?? null } + return { + sshTargetSummaries: labels.sshTargetSummaries, + hostLabels: [...labels.hostLabelById], + platform: acceptedMetadata(platform, hostPlatformRead.interpret) ?? null + } }, staleTime: 60_000, gcTime: 30 * 60_000, refetchOnWindowFocus: true, // SSH health and the host's own label overrides. - signal: { events: ['reposChanged', 'sshStateChanged'] } + signal: { events: ['reposChanged', 'sshStateChanged'] }, + persist: { + version: 1, + dataSchema: z.object({ + sshTargetSummaries: z.array(hostSshTargetSummarySchema), + hostLabels: z.array(storedHostLabelSchema), + platform: z.enum(NODE_PLATFORM_NAMES).nullable() + }) + } }) // The host list's own reads, which pull to refresh repeats; project.sections lives with its hook. diff --git a/mobile/src/host-screen/host-screen-reply-schema.ts b/mobile/src/host-screen/host-screen-reply-schema.ts index 7afeb0e3cea..171400b27eb 100644 --- a/mobile/src/host-screen/host-screen-reply-schema.ts +++ b/mobile/src/host-screen/host-screen-reply-schema.ts @@ -83,20 +83,17 @@ export const hostRepoCatalogSnapshotSchema = z.union([ ]) // Keep target parsing total: absent/malformed targets yield [] and labels degrade to host ids on older hosts. A throw here would also skip the subsequent platform write. +export const hostSshTargetSummarySchema = z.looseObject({ + id: z.string(), + label: z.string(), + // Read raw: an arm a newer desktop adds must leave the badge as it is, not read as a verdict. + connected: salvagedOptional('connected', z.boolean()), + connectionStatus: salvagedOptional('connectionStatus', z.string()) +}) + export const hostSshTargetSummariesSchema = z .looseObject({ - targets: salvagedOptional( - 'targets', - salvagingArray( - z.looseObject({ - id: z.string(), - label: z.string(), - // Read raw: an arm a newer desktop adds must leave the badge as it is, not read as a verdict. - connected: salvagedOptional('connected', z.boolean()), - connectionStatus: salvagedOptional('connectionStatus', z.string()) - }) - ) - ) + targets: salvagedOptional('targets', salvagingArray(hostSshTargetSummarySchema)) }) .transform((reply) => reply.targets ?? []) .catch(() => []) diff --git a/mobile/src/host-screen/use-host-repo-metadata.ts b/mobile/src/host-screen/use-host-repo-metadata.ts index 6a7475b1dd3..2d306455441 100644 --- a/mobile/src/host-screen/use-host-repo-metadata.ts +++ b/mobile/src/host-screen/use-host-repo-metadata.ts @@ -66,7 +66,7 @@ export function useHostRepoMetadata(args: { useEffect(() => { if (needsLabels && labels.data) { setHostHealthById(buildSshHostHealthById(labels.data.sshTargetSummaries)) - setHostLabelById(labels.data.hostLabelById) + setHostLabelById(new Map(labels.data.hostLabels)) setHostPlatform(labels.data.platform) } else if (repos.data && !needsLabels) { // Clear labels for departed hosts so stale metadata cannot badge remaining rows. diff --git a/mobile/src/host-screen/use-host-screen-controller.ts b/mobile/src/host-screen/use-host-screen-controller.ts index 8d9b449a25e..983fa99eb96 100644 --- a/mobile/src/host-screen/use-host-screen-controller.ts +++ b/mobile/src/host-screen/use-host-screen-controller.ts @@ -60,7 +60,8 @@ export function useHostScreenController({ const relayRecovery = useRelayRecoveryStatus(hostId) const forgetHostClient = useForgetHostClient() const forceReconnectHost = useForceReconnect() - const { hostCapabilities, floatingWorkspaceEnabled, hostInstallationId } = useHostProtocolGates() + const { hostCapabilities, statusReadable, floatingWorkspaceEnabled, hostInstallationId } = + useHostProtocolGates() const state = useHostScreenState(hostId, peekStoredHost(hostId)) const settings = useHostViewSettings({ client, connState, state }) const connectionVerdict = classifyConnection({ @@ -83,6 +84,7 @@ export function useHostScreenController({ connState, embedded, hostCapabilities, + hostStatusReadable: statusReadable, hostId, sidebarFeed: sidebarFeed?.triggers ?? null, state, diff --git a/mobile/src/host-screen/use-host-worktree-catalog.ts b/mobile/src/host-screen/use-host-worktree-catalog.ts index c4cd1db1da9..ca3715baef2 100644 --- a/mobile/src/host-screen/use-host-worktree-catalog.ts +++ b/mobile/src/host-screen/use-host-worktree-catalog.ts @@ -1,4 +1,3 @@ -import { MOBILE_RELAY_HOST_WORKTREE_DELTAS_RUNTIME_CAPABILITY } from '../../../src/shared/mobile-desktop-relay-contract' import { WORKTREE_ROWS_CHANGED_EVENT_RUNTIME_CAPABILITY } from '../../../src/shared/worktree-runtime-capabilities' import { readWorktreeFirstPage } from '../worktree/worktree-catalog-first-page' import type { Worktree } from '../worktree/workspace-list-types' @@ -32,6 +31,7 @@ import { HOST_LIST_BACKSTOP_POLL_MS, HOST_LIST_POLL_MS, HOST_LIST_TARGET, + hostServerWorkspacesInput, hostServerWorkspacesQuery, hostWorktreeCatalogKey, hostWorktreeCatalogQuery, @@ -54,6 +54,8 @@ export function useHostWorktreeCatalog(args: { connState: ConnectionState embedded: boolean hostCapabilities: readonly string[] + // Whether the host answered its status: capabilities are unknown, not absent, until it has. + hostStatusReadable: boolean hostId: string | undefined sidebarFeed: HostSidebarFeedTriggers | null state: HostScreenState @@ -64,6 +66,7 @@ export function useHostWorktreeCatalog(args: { connState, embedded, hostCapabilities, + hostStatusReadable, hostId, sidebarFeed, state, @@ -117,11 +120,13 @@ export function useHostWorktreeCatalog(args: { ) // An older desktop or shell lists no server rows, rather than rows that could not open. const relays = client !== null && relaysToServers(client, hostCapabilities) + // Until the host's status is read, stored server rows paint and nothing reads them. + const relaysRuledOut = hostStatusReadable && !relays const servers = useHostQuery( hostServerWorkspacesQuery, host, HOST_LIST_TARGET, - { deltas: hostCapabilities.includes(MOBILE_RELAY_HOST_WORKTREE_DELTAS_RUNTIME_CAPABILITY) }, + hostServerWorkspacesInput(hostStatusReadable ? hostCapabilities : null), // Execution hosts' notices are not relayed, so server rows keep the full poll. { enabled: !!hostId && relays, subscribed, refetchInterval: pollMs } ) @@ -210,12 +215,13 @@ export function useHostWorktreeCatalog(args: { }, [catalog.status, catalog.errorUpdatedAt]) useEffect(() => { - if (!relays) { - setServerWorkspaces(NO_SERVER_WORKSPACES) - } else if (servers.data) { + if (servers.data && !relaysRuledOut) { setServerWorkspaces(servers.data) + } else if (!relays) { + // Also drops a previous host's rows when the sidebar switches to one with none stored. + setServerWorkspaces(NO_SERVER_WORKSPACES) } - }, [relays, servers.data]) + }, [relays, relaysRuledOut, servers.data]) const fetchWorktrees = useCallback( async (options: { allowDuringModal?: boolean } = {}) => { diff --git a/mobile/src/query/first-paint-query-keys.ts b/mobile/src/query/first-paint-query-keys.ts index 609962e2284..aee6d07b4c5 100644 --- a/mobile/src/query/first-paint-query-keys.ts +++ b/mobile/src/query/first-paint-query-keys.ts @@ -2,6 +2,9 @@ import { normalizeExecutionHostId } from '../../../src/shared/execution-host' import { HOST_LIST_TARGET, hostRepoCatalogQuery, + hostRepoLabelsQuery, + hostServerWorkspacesInput, + hostServerWorkspacesQuery, hostWorktreeCatalogKey, hostWorktreeCatalogQuery, NO_HOST_LIST_INPUT @@ -42,10 +45,23 @@ export function firstPaintQueryKeys(hostId: string, route: URL): PersistedHostQu ? [ persistedHostQueryKey(hostWorktreeCatalogQuery, hostWorktreeCatalogKey(hostId)), persistedHostQueryKey(hostViewFamily, hostViewKey(hostId)), + persistedHostQueryKey( + hostServerWorkspacesQuery, + hostQueryKey( + hostId, + hostServerWorkspacesQuery.family, + HOST_LIST_TARGET, + hostServerWorkspacesInput(null) + ) + ), persistedHostQueryKey( hostRepoCatalogQuery, hostQueryKey(hostId, hostRepoCatalogQuery.family, HOST_LIST_TARGET, NO_HOST_LIST_INPUT) ), + persistedHostQueryKey( + hostRepoLabelsQuery, + hostQueryKey(hostId, hostRepoLabelsQuery.family, HOST_LIST_TARGET, NO_HOST_LIST_INPUT) + ), persistedHostQueryKey( hostProjectSectionsQuery, hostQueryKey( From 353f4c5e281734a3cea1306d76f471f0523143ab Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:04:19 +0000 Subject: [PATCH 34/80] fix(mobile): refresh Review's branch compare when the desktop reports a git change (#40088) Review rebuilt only when a git.status read changed, so a base move that left this worktree's status as it was kept the old compare (also cached 30 s). The compare entries now take git.status's gitStatusChanged signal. A status read that lands after the signal marked the shown compare rebuilds Review without re-keying worktree diffs, and a compare read that lands marked is read again. --- mobile/src/query/host-query.ts | 22 +++++ ...mobile-diff-review-branch-compare-cache.ts | 53 +++++++++++ .../use-mobile-diff-review-snapshot.test.ts | 89 +++++++++++++++++++ .../review/use-mobile-diff-review-snapshot.ts | 69 +++++++++----- 4 files changed, 211 insertions(+), 22 deletions(-) create mode 100644 mobile/src/source-control/review/mobile-diff-review-branch-compare-cache.ts create mode 100644 mobile/src/source-control/review/use-mobile-diff-review-snapshot.test.ts diff --git a/mobile/src/query/host-query.ts b/mobile/src/query/host-query.ts index 4d65785430e..209ad828d2e 100644 --- a/mobile/src/query/host-query.ts +++ b/mobile/src/query/host-query.ts @@ -141,6 +141,28 @@ export async function readHostQueryLive( return data } +// A cached read through the screen's `read`: reuses a fresh answer and joins a read already out. An answer +// an event marked stale as it landed predates that change, so it is read again. +export async function fetchHostQuery( + queryClient: QueryClient, + query: HostQuery, + host: { hostId: string; client: RpcClient }, + target: HostQueryTarget, + input: TInput, + read: () => Promise +): Promise { + const options = { + ...hostQueryOptions(query, host.client, host.hostId, target, input), + queryFn: read + } + let data = await queryClient.fetchQuery(options) + // Each pass needs a newer event, so this ends with the event burst. + while (queryClient.getQueryState(options.queryKey)?.isInvalidated) { + data = await queryClient.fetchQuery(options) + } + return data +} + // A read ahead of the user: skipped while the key holds current data, joins a read already out. export function prefetchHostQuery( queryClient: QueryClient, diff --git a/mobile/src/source-control/review/mobile-diff-review-branch-compare-cache.ts b/mobile/src/source-control/review/mobile-diff-review-branch-compare-cache.ts new file mode 100644 index 00000000000..1a85fde8f6e --- /dev/null +++ b/mobile/src/source-control/review/mobile-diff-review-branch-compare-cache.ts @@ -0,0 +1,53 @@ +import type { QueryClient } from '@tanstack/react-query' +import { defineHostQuery, fetchHostQuery } from '../../query/host-query' +import { hostQueryKey, type HostQueryTarget } from '../../query/host-query-key' +import type { RpcClient } from '../../transport/rpc/rpc-client-contract' +import { gitStatusQuery } from '../mobile-git-status-query' +import type { BranchCompareLoadResult } from './mobile-diff-review-loaders' + +export type ReviewBranchCompareKey = { baseRef: string; head: string } + +// Read by Review with the base ref it resolved. A desktop git change can move the base and leave status as it was, +// so the change that re-reads status also marks these stale. +const reviewBranchCompareQuery = defineHostQuery({ + family: 'git.branchCompare', + signal: gitStatusQuery.signal +}) + +export async function readCachedReviewBranchCompare( + queryClient: QueryClient, + host: { hostId: string; client: RpcClient }, + target: HostQueryTarget, + key: ReviewBranchCompareKey, + read: () => Promise +): Promise { + const loaded = await fetchHostQuery( + queryClient, + reviewBranchCompareQuery, + host, + target, + key, + read + ) + // Only a compare that landed for this head is worth reusing; cached status can trail a commit. + if (loaded.result?.summary.headOid !== key.head) { + queryClient.removeQueries({ + queryKey: hostQueryKey(host.hostId, reviewBranchCompareQuery.family, target, key), + exact: true + }) + } + return loaded +} + +// Marked by a git change and not being read again, so only a new build brings the current compare. +export function reviewBranchCompareNeedsRead( + queryClient: QueryClient, + hostId: string, + target: HostQueryTarget, + key: ReviewBranchCompareKey +): boolean { + const state = queryClient.getQueryState( + hostQueryKey(hostId, reviewBranchCompareQuery.family, target, key) + ) + return state?.isInvalidated === true && state.fetchStatus === 'idle' +} diff --git a/mobile/src/source-control/review/use-mobile-diff-review-snapshot.test.ts b/mobile/src/source-control/review/use-mobile-diff-review-snapshot.test.ts new file mode 100644 index 00000000000..0ea1695e085 --- /dev/null +++ b/mobile/src/source-control/review/use-mobile-diff-review-snapshot.test.ts @@ -0,0 +1,89 @@ +import { createElement } from 'react' +import { act, create, type ReactTestRenderer } from 'react-test-renderer' +import { afterEach, expect, it, vi } from 'vitest' +import { documentQueryClient } from '../../test-support/rpc-recording/hook-mount' +import { stubRpcClient } from '../../transport/rpc/rpc-client-test-stub' +import type { RpcResponse } from '../../transport/rpc/rpc-wire-types' +import { useMobileDiffReviewSnapshot } from './use-mobile-diff-review-snapshot' + +vi.mock('react-native', () => ({ Platform: { OS: 'ios' } })) + +const WORKTREE = 'repo-m::/r/wt' +let renderer: ReactTestRenderer | undefined +const queries = documentQueryClient() + +afterEach(async () => { + act(() => renderer?.unmount()) + renderer = undefined + await queries.end() +}) + +it('re-reads the branch compare when a desktop git change moves the base but not status', async () => { + let branchFiles = ['a.ts', 'b.ts'] + let emit: (event: unknown) => void = () => {} + const results: Record = { + 'git.status': { + entries: [], + conflictOperation: 'unknown', + head: 'h1', + branch: 'refs/heads/wt' + }, + 'worktree.show': { worktree: { baseRef: 'main', branch: 'refs/heads/wt', head: 'h1' } }, + 'repo.list': { repos: [] }, + 'settings.get': { settings: {} } + } + const client = stubRpcClient({ + getLastConnectedAt: () => 1, + onStateChange: () => () => {}, + subscribe: (_method, _params, onData) => { + emit = onData + return () => {} + }, + sendRequest: async (method): Promise => ({ + id: 'r', + ok: true, + result: + method === 'git.branchCompare' + ? { + summary: { + baseRef: 'main', + compareRef: 'HEAD', + changedFiles: branchFiles.length, + status: 'ready', + headOid: 'h1', + mergeBase: branchFiles.length === 2 ? 'm1' : 'm2' + }, + entries: branchFiles.map((path) => ({ path, status: 'modified' })) + } + : results[method] + }) + }) + let shown: string[] | undefined + function Review(): null { + const { screenState } = useMobileDiffReviewSnapshot({ + client, + connState: 'connected', + hostId: 'h', + worktreeId: WORKTREE, + commitOid: null, + filter: 'all', + setActionError: () => {} + }) + shown = + screenState.kind === 'ready' + ? screenState.branchCompare?.entries?.map((entry) => entry.path) + : undefined + return null + } + await act(async () => { + renderer = create(queries.wrap(createElement(Review))) + }) + act(() => emit({ type: 'ready', subscriptionId: 's' })) + await vi.waitFor(() => expect(shown).toEqual(['a.ts', 'b.ts'])) + + // a.ts was merged into main from another checkout; this worktree's status is unchanged. + branchFiles = ['b.ts'] + act(() => emit({ type: 'gitStatusChanged', repoId: 'repo-m' })) + + await vi.waitFor(() => expect(shown).toEqual(['b.ts']), { timeout: 3_000 }) +}) diff --git a/mobile/src/source-control/review/use-mobile-diff-review-snapshot.ts b/mobile/src/source-control/review/use-mobile-diff-review-snapshot.ts index 1a724609276..10fe03182a0 100644 --- a/mobile/src/source-control/review/use-mobile-diff-review-snapshot.ts +++ b/mobile/src/source-control/review/use-mobile-diff-review-snapshot.ts @@ -10,6 +10,11 @@ import type { RpcClient } from '../../transport/rpc/rpc-client-contract' import type { ConnectionState } from '../../transport/connection/connection-state' import { loadMobileDiffReviewCommitSnapshot } from './mobile-diff-review-commit-loader' import type { MobileBranchCompareCache } from './mobile-diff-review-loaders' +import { + readCachedReviewBranchCompare, + reviewBranchCompareNeedsRead, + type ReviewBranchCompareKey +} from './mobile-diff-review-branch-compare-cache' import type { MobileDiffReviewQueueFilter } from './mobile-diff-review-queue' import type { ReviewScreenState } from './mobile-diff-review-screen-model' import { @@ -59,11 +64,17 @@ export function useMobileDiffReviewSnapshot(input: SnapshotInput) { ) // The same git.status entry Changes shows, so Review opened from it starts from those rows. const worktreeReview = worktreeId !== '' && commitOid === null && filter !== 'commit' - const { data: statusRead } = useHostQuery(gitStatusQuery, { hostId, client }, target, NO_INPUT, { - enabled: worktreeReview - }) + const { data: statusRead, dataUpdatedAt: statusReadAt } = useHostQuery( + gitStatusQuery, + { hostId, client }, + target, + NO_INPUT, + { enabled: worktreeReview } + ) // The status read the shown snapshot was built from; AWAITING while a build waits for its first read. const builtFromRef = useRef(undefined) + // The compare the current build read, so a git change that marked it stale rebuilds even when status held. + const builtCompareRef = useRef(null) // Reloads after a write wait for the rebuild they cause, so the screen's buttons match the new files. const buildWaitersRef = useRef<(() => void)[]>([]) const settleWaiters = () => { @@ -72,23 +83,12 @@ export function useMobileDiffReviewSnapshot(input: SnapshotInput) { } } - const cacheBranchCompare = useCallback( - async (key, read) => { - const queryKey = hostQueryKey(hostId, 'git.branchCompare', target, key) - const loaded = await queryClient.fetchQuery({ queryKey, queryFn: read }) - // Only a compare that landed for this head is worth reusing; cached status can trail a commit. - if (loaded.result?.summary.headOid !== key.head) { - queryClient.removeQueries({ queryKey, exact: true }) - } - return loaded - }, - [hostId, queryClient, target] - ) - - const buildSnapshot = useEffectEvent(async (status: GitStatusRead | undefined) => { + // `sameStatus`: only the compare moved, so worktree diffs keyed to the shown status read still hold. + const buildSnapshot = useEffectEvent(async (status?: GitStatusRead, sameStatus = false) => { const generation = loadGenerationRef.current + 1 loadGenerationRef.current = generation const isCurrent = () => generation === loadGenerationRef.current + builtCompareRef.current = null try { if (!worktreeId) { setScreenState({ @@ -170,6 +170,14 @@ export function useMobileDiffReviewSnapshot(input: SnapshotInput) { throw err } ) + const cacheBranchCompare: MobileBranchCompareCache = async (key, read) => { + // A superseded build's compare is never shown. + if (!isCurrent()) { + return { result: null } + } + builtCompareRef.current = key + return readCachedReviewBranchCompare(queryClient, { hostId, client }, target, key, read) + } const { snapshot, branch } = startMobileDiffReviewSnapshot(client, worktreeId, { status: statusInput, compareAgainstUpstream, @@ -183,7 +191,9 @@ export function useMobileDiffReviewSnapshot(input: SnapshotInput) { if (!isCurrent()) { return } - setStatusRevision(queryClient.getQueryState(statusKey)?.dataUpdatedAt ?? 0) + if (!sameStatus) { + setStatusRevision(queryClient.getQueryState(statusKey)?.dataUpdatedAt ?? 0) + } if (loaded) { setScreenState(withMobileDiffReviewBranch(state, loaded, worktreeId)) setActionError(loaded.error ?? null) @@ -224,12 +234,27 @@ export function useMobileDiffReviewSnapshot(input: SnapshotInput) { }, [client, commitOid, connState, queryClient, reloadNonce, statusKey, worktreeReview]) // A status read that changed what Changes shows rebuilds the snapshot; one in flight already brings its own. - useEffect(() => { + // A read that lands unchanged rebuilds too once a git change marked the shown compare stale; a compare + // still out is read again by its own build when it lands marked. + const rebuildOnStatusRead = useEffectEvent((read: GitStatusRead) => { const built = builtFromRef.current - if (worktreeReview && statusRead !== undefined && built !== AWAITING && statusRead !== built) { - void buildSnapshot(statusRead) + if (built === AWAITING) { + return } - }, [statusRead, worktreeReview]) + const compare = builtCompareRef.current + const compareMarked = + compare !== null && reviewBranchCompareNeedsRead(queryClient, hostId, target, compare) + if (read !== built) { + void buildSnapshot(read) + } else if (compareMarked) { + void buildSnapshot(read, true) + } + }) + useEffect(() => { + if (worktreeReview && statusRead !== undefined) { + rebuildOnStatusRead(statusRead) + } + }, [statusRead, statusReadAt, worktreeReview]) // The compare base is read when a snapshot is built; a later setting change rebuilds without re-reading status. const shownCompareSettingRef = useRef(compareAgainstUpstream) From b41ab35b723341f7be4c30ad3495dd6a94b2c2ce Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:14:06 +0000 Subject: [PATCH 35/80] fix(clone): let a clone use its own folder when the repo's name is taken (#40066) Cloning always went into /, so a same-named repository from another owner (or any non-empty folder of that name) could not be cloned into the same parent. Before cloning, Orca now checks the target on the clone host. If it is taken, the clone forms show a Folder name field with a free suggestion (-, then -2...) and clone into that instead. RPC repo.clone and `orca project setup-clone` accept the folder name too. --- src/cli/handlers/project.ts | 1 + src/cli/specs/project.ts | 13 +- src/main/git/clone-target-check.test.ts | 93 ++++ src/main/git/clone-target-check.ts | 87 ++++ src/main/git/repo-clone-path.ts | 56 ++- src/main/git/saved-clone-target.ts | 2 +- src/main/ipc/repos.ts | 1 + src/main/ipc/repos/repo-clone-lifecycle.ts | 399 +++++++++--------- .../construction/runtime-host-services.ts | 2 +- .../runtime-project-host-setup-controller.ts | 4 +- .../runtime-repository-clone-controller.ts | 52 ++- .../runtime-repository-command-surface.ts | 2 + src/main/runtime/rpc/methods/projects/repo.ts | 13 +- src/preload/api/repos-bridge.ts | 2 + src/preload/api/repository-api.ts | 4 +- .../SetProjectLocationDialog.tsx | 12 +- .../new-workspace/SetProjectLocationForms.tsx | 20 +- .../hosts/RepositoryHostSetupActions.tsx | 14 +- .../repository-host-add-project-steps.tsx | 21 +- .../clone-folder/CloneFolderNameField.tsx | 46 ++ .../clone-folder/use-clone-folder-override.ts | 91 ++++ .../dialog/AddRepoCloneStep.tsx | 27 +- .../dialog/AddRepoDialog.tsx | 18 +- .../dialog/AddRepoDialogStepContent.tsx | 7 + .../dialog/useAddRepoCloneFlow.ts | 63 ++- src/renderer/src/i18n/locales/de.json | 5 +- src/renderer/src/i18n/locales/en.json | 5 +- 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 | 5 +- src/renderer/src/i18n/locales/zh.json | 5 +- .../store/projects/project-host-routing.ts | 7 + .../projects/project-host-setup-actions.ts | 6 +- .../web/preload-api/web-repositories-api.ts | 7 +- src/shared/git/clone/clone-target-check.ts | 11 + .../git/remotes/git-clone-failure-message.ts | 2 +- src/shared/project-types.ts | 2 + .../rpc-contract/project-runtime-params.ts | 1 + src/shared/rpc-contract/repo-params.ts | 3 +- .../rpc-params-catalog.generated.ts | 1 + 45 files changed, 855 insertions(+), 290 deletions(-) create mode 100644 src/main/git/clone-target-check.test.ts create mode 100644 src/main/git/clone-target-check.ts create mode 100644 src/renderer/src/components/sidebar/project-admission/clone-folder/CloneFolderNameField.tsx create mode 100644 src/renderer/src/components/sidebar/project-admission/clone-folder/use-clone-folder-override.ts create mode 100644 src/shared/git/clone/clone-target-check.ts diff --git a/src/cli/handlers/project.ts b/src/cli/handlers/project.ts index 324f8ec660c..798eaa8bdb6 100644 --- a/src/cli/handlers/project.ts +++ b/src/cli/handlers/project.ts @@ -156,6 +156,7 @@ export const PROJECT_HANDLERS: Record = { client.isRemote, 'Project setup clone' ), + folderName: getOptionalStringFlag(flags, 'folder-name'), displayName: getOptionalStringFlag(flags, 'display-name'), projectId: await resolveProjectIdSelector(client, getRequiredStringFlag(flags, 'project')) } diff --git a/src/cli/specs/project.ts b/src/cli/specs/project.ts index 2de579cc117..1f7d989b8ba 100644 --- a/src/cli/specs/project.ts +++ b/src/cli/specs/project.ts @@ -48,10 +48,19 @@ export const PROJECT_COMMAND_SPECS: CommandSpec[] = [ path: ['project', 'setup-clone'], summary: 'Make a project available on a host by cloning a repository', usage: - 'orca project setup-clone --project --host --url --destination [--display-name ] [--json]', - allowedFlags: [...GLOBAL_FLAGS, 'project', 'host', 'url', 'destination', 'display-name'], + 'orca project setup-clone --project --host --url --destination [--folder-name ] [--display-name ] [--json]', + allowedFlags: [ + ...GLOBAL_FLAGS, + 'project', + 'host', + 'url', + 'destination', + 'folder-name', + 'display-name' + ], notes: [ 'For remote runtimes, --destination must be an absolute parent directory on the remote server.', + '--folder-name sets the folder created inside --destination; it defaults to the repository name from the URL.', '--host takes local, ssh:, runtime:, or the bare host name `orca host list` prints for any of them.', 'SSH targets are cloned through the desktop UI because the desktop client owns SSH connections.' ], diff --git a/src/main/git/clone-target-check.test.ts b/src/main/git/clone-target-check.test.ts new file mode 100644 index 00000000000..53ec030409c --- /dev/null +++ b/src/main/git/clone-target-check.test.ts @@ -0,0 +1,93 @@ +import { afterEach, describe, expect, it } from 'vitest' +import { execFileSync } from 'node:child_process' +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { LOCAL_EXECUTION_HOST_ID } from '../../shared/execution-host' +import type { Repo } from '../../shared/repo-types' +import { checkCloneTarget } from './clone-target-check' +import { getClonePathComparisonKey } from './repo-clone-path' + +const roots: string[] = [] + +function parentFolder(): string { + const root = mkdtempSync(join(tmpdir(), 'orca-clone-target-')) + roots.push(root) + return root +} + +function savedClone(path: string, originUrl: string): Repo { + execFileSync('git', ['init', '-q', path]) + execFileSync('git', ['-C', path, 'remote', 'add', 'origin', originUrl]) + execFileSync('git', [ + '-C', + path, + '-c', + 'user.name=t', + '-c', + 'user.email=t@t.invalid', + 'commit', + '-q', + '--allow-empty', + '-m', + 'init' + ]) + return { + id: path, + path, + displayName: 'orca-plugins', + badgeColor: '#fff', + addedAt: 1, + kind: 'git' + } +} + +const check = (repos: Repo[], url: string, destination: string) => + checkCloneTarget( + { url, destination }, + (keys) => repos.find((repo) => keys.includes(getClonePathComparisonKey(repo.path))), + LOCAL_EXECUTION_HOST_ID + ) + +afterEach(() => { + for (const root of roots.splice(0)) { + rmSync(root, { recursive: true, force: true }) + } +}) + +describe('checkCloneTarget', () => { + it('suggests an owner-named folder when a same-named repository from another owner is there', async () => { + const parent = parentFolder() + const official = savedClone( + join(parent, 'orca-plugins'), + 'https://github.com/stablyai/orca-plugins.git' + ) + + await expect( + check([official], 'https://github.com/aamonnaig4/orca-plugins', parent) + ).resolves.toEqual({ + status: 'taken', + folderName: 'orca-plugins', + reason: 'other-repository', + suggestedFolderName: 'orca-plugins-aamonnaig4' + }) + + mkdirSync(join(parent, 'orca-plugins-aamonnaig4')) + writeFileSync(join(parent, 'orca-plugins-aamonnaig4', 'notes.txt'), '') + await expect( + check([official], 'git@github.com:aamonnaig4/orca-plugins.git', parent) + ).resolves.toMatchObject({ status: 'taken', suggestedFolderName: 'orca-plugins-2' }) + }) + + it('leaves the default folder alone when it already holds this repository', async () => { + const parent = parentFolder() + const official = savedClone( + join(parent, 'orca-plugins'), + 'https://github.com/stablyai/orca-plugins.git' + ) + + await expect( + check([official], 'git@github.com:stablyai/orca-plugins.git', parent) + ).resolves.toEqual({ status: 'free', folderName: 'orca-plugins' }) + }) +}) diff --git a/src/main/git/clone-target-check.ts b/src/main/git/clone-target-check.ts new file mode 100644 index 00000000000..dbd2ac5b305 --- /dev/null +++ b/src/main/git/clone-target-check.ts @@ -0,0 +1,87 @@ +import { basename, join } from 'node:path' +import { lstat, readdir } from 'node:fs/promises' +import type { ExecutionHostId } from '../../shared/execution-host' +import type { CloneTargetArgs, CloneTargetCheck } from '../../shared/git/clone/clone-target-check' +import { isFolderRepo } from '../../shared/repo-kind' +import type { Repo } from '../../shared/repo-types' +import { + deriveCloneOwnerFromUrl, + deriveValidatedClonePath, + getSavedProjectPathKeys +} from './repo-clone-path' +import { reuseSavedCloneTarget } from './saved-clone-target' + +/** Finds the project saved at any of these path keys on the clone's host. */ +export type FindSavedCloneProject = (pathKeys: string[]) => Repo | undefined + +const MAX_NUMBERED_SUGGESTIONS = 20 + +/** Git refuses a non-empty destination, and reuseSavedCloneTarget refuses another repository's project. */ +async function findCloneTargetConflict( + clonePath: string, + url: string, + findSaved: FindSavedCloneProject, + hostId: ExecutionHostId +): Promise<'other-repository' | 'not-empty' | null> { + const pathKeys = await getSavedProjectPathKeys(clonePath) + const saved = findSaved(pathKeys) + if (saved && !isFolderRepo(saved)) { + try { + await reuseSavedCloneTarget(() => findSaved(pathKeys), url, hostId) + return null + } catch { + return 'other-repository' + } + } + return (await isNonEmptyPath(clonePath)) ? 'not-empty' : null +} + +async function isNonEmptyPath(path: string): Promise { + try { + const stats = await lstat(path) + return !stats.isDirectory() || (await readdir(path)).length > 0 + } catch { + // Missing or unreadable: the clone itself reports anything other than a free folder. + return false + } +} + +async function suggestFreeFolderName( + args: CloneTargetArgs, + folderName: string, + findSaved: FindSavedCloneProject, + hostId: ExecutionHostId +): Promise { + const owner = deriveCloneOwnerFromUrl(args.url) + const candidates = [ + ...(owner ? [`${folderName}-${owner}`] : []), + ...Array.from({ length: MAX_NUMBERED_SUGGESTIONS }, (_, i) => `${folderName}-${i + 2}`) + ] + for (const candidate of candidates) { + // A candidate already holding this repository's project is fine: the clone reuses it. + const path = join(args.destination, candidate) + if (!(await findCloneTargetConflict(path, args.url, findSaved, hostId))) { + return candidate + } + } + return null +} + +export async function checkCloneTarget( + args: CloneTargetArgs, + findSaved: FindSavedCloneProject, + hostId: ExecutionHostId +): Promise { + const clonePath = deriveValidatedClonePath(args) + const folderName = basename(clonePath) + const reason = await findCloneTargetConflict(clonePath, args.url, findSaved, hostId) + if (!reason) { + return { status: 'free', folderName } + } + return { + status: 'taken', + folderName, + reason, + suggestedFolderName: await suggestFreeFolderName(args, folderName, findSaved, hostId) + } +} diff --git a/src/main/git/repo-clone-path.ts b/src/main/git/repo-clone-path.ts index 1186656f6dd..726ae5f026a 100644 --- a/src/main/git/repo-clone-path.ts +++ b/src/main/git/repo-clone-path.ts @@ -15,25 +15,46 @@ export type ClaimedCloneTarget = { type CloneDirectoryIdentity = Pick -export function deriveCloneRepoNameFromUrl(url: string): string { - // Reject dot destinations before cleanup could remove a parent or unrelated folder. +/** Path part of a clone source: scp-style host prefixes removed, URL schemes kept. */ +function cloneSourcePath(url: string): { path: string; windows: boolean } { const source = url.replace(/\.git\/?$/, '') - const isWindowsLocalSource = /^[A-Za-z]:[\\/]/.test(source) || source.startsWith('\\\\') - const scpPath = - !isWindowsLocalSource && !source.includes('://') - ? source.match(/^(?:[^@/:]+@)?(?:\[[^\]]+\]|[^:/\s]+):(.+)$/)?.[1] - : undefined - const repoName = isWindowsLocalSource ? win32.basename(source) : posix.basename(scpPath ?? source) - if (!repoName || repoName === '.' || repoName === '..') { - throw new Error('Invalid repository name derived from URL') + if (/^[A-Za-z]:[\\/]/.test(source) || source.startsWith('\\\\')) { + return { path: source, windows: true } } - if (repoName.includes('/') || repoName.includes('\\')) { - throw new Error('Invalid repository name derived from URL') - } - return repoName + const scpPath = !source.includes('://') + ? source.match(/^(?:[^@/:]+@)?(?:\[[^\]]+\]|[^:/\s]+):(.+)$/)?.[1] + : undefined + return { path: scpPath ?? source, windows: false } } -export function deriveValidatedClonePath(args: { url: string; destination: string }): string { +function assertCloneFolderName(name: string, message: string): string { + if (!name || name === '.' || name === '..' || name.includes('/') || name.includes('\\')) { + throw new Error(message) + } + return name +} + +export function deriveCloneRepoNameFromUrl(url: string): string { + // Reject dot destinations before cleanup could remove a parent or unrelated folder. + const { path, windows } = cloneSourcePath(url) + const repoName = windows ? win32.basename(path) : posix.basename(path) + return assertCloneFolderName(repoName, 'Invalid repository name derived from URL') +} + +/** The segment above the repository (GitHub owner, GitLab group), or null when there is none. */ +export function deriveCloneOwnerFromUrl(url: string): string | null { + const { path, windows } = cloneSourcePath(url) + const pathOps = windows ? win32 : posix + const repoPath = windows ? path : path.replace(/^[a-z][\w+.-]*:\/\/[^/]*/i, '') + const owner = pathOps.basename(pathOps.dirname(repoPath)) + return owner && owner !== '.' && owner !== '..' && !owner.includes(':') ? owner : null +} + +export function deriveValidatedClonePath(args: { + url: string + destination: string + folderName?: string +}): string { if ( !args.destination || !isAbsolute(args.destination) || @@ -42,7 +63,10 @@ export function deriveValidatedClonePath(args: { url: string; destination: strin throw new Error('Clone destination must be an absolute path') } - const repoName = deriveCloneRepoNameFromUrl(args.url) + const requestedFolderName = args.folderName?.trim() + const repoName = requestedFolderName + ? assertCloneFolderName(requestedFolderName, 'Invalid clone folder name') + : deriveCloneRepoNameFromUrl(args.url) const clonePath = join(args.destination, repoName) const resolvedDestination = resolve(args.destination) diff --git a/src/main/git/saved-clone-target.ts b/src/main/git/saved-clone-target.ts index f264f3c173f..998c69e009d 100644 --- a/src/main/git/saved-clone-target.ts +++ b/src/main/git/saved-clone-target.ts @@ -138,6 +138,6 @@ export async function reuseSavedCloneTarget( storedOrigin ? 'Orca has that project recorded as a different repository' : 'Orca has no record of which repository that project holds, so it cannot tell whether the folder is missing or holds something else' - }. Remove the project from Orca or choose another folder.` + }. Clone into a different folder name, or remove the project from Orca.` ) } diff --git a/src/main/ipc/repos.ts b/src/main/ipc/repos.ts index 434e9207253..af8b9c5fe8f 100644 --- a/src/main/ipc/repos.ts +++ b/src/main/ipc/repos.ts @@ -52,6 +52,7 @@ export function registerRepoHandlers( ipcMain.removeHandler('repos:pickFolders') ipcMain.removeHandler('repos:pickDirectory') ipcMain.removeHandler('repos:clone') + ipcMain.removeHandler('repos:checkCloneTarget') ipcMain.removeHandler('repos:cloneAbort') ipcMain.removeHandler('repos:isGitAvailable') ipcMain.removeHandler('repos:getDefaultCreateProjectParent') diff --git a/src/main/ipc/repos/repo-clone-lifecycle.ts b/src/main/ipc/repos/repo-clone-lifecycle.ts index 1cadfe8e428..bd68750b60e 100644 --- a/src/main/ipc/repos/repo-clone-lifecycle.ts +++ b/src/main/ipc/repos/repo-clone-lifecycle.ts @@ -10,6 +10,8 @@ import { DEFAULT_REPO_BADGE_COLOR } from '../../../shared/constants' import { getGitCloneFailureMessage } from '../../../shared/git/remotes/git-clone-failure-message' import { gitSpawnAfterWindowsEnvironmentReady, promptGuardGitEnv } from '../../git/runner' import { reuseSavedCloneTarget } from '../../git/saved-clone-target' +import { checkCloneTarget } from '../../git/clone-target-check' +import type { CloneTargetArgs } from '../../../shared/git/clone/clone-target-check' import { getRepoName, inspectGitRepoForRegistrationAsync } from '../../git/repo' import type { ClaimedCloneTarget } from '../../git/repo-clone-path' import { @@ -115,214 +117,217 @@ export function registerRepoCloneHandlers(mainWindow: BrowserWindow, store: Stor } }) - ipcMain.handle( - 'repos:clone', - async (_event, args: { url: string; destination: string }): Promise => { - const clonePath = deriveValidatedClonePath(args) - const clonePathKey = getClonePathComparisonKey(clonePath) - let savedPathKeys: string[] = [] - // Remote projects can share this path string without belonging to this clone host. - const findSaved = (): Repo | undefined => - store - .getRepos() - .find( - (r) => - savedPathKeys.includes(getClonePathComparisonKey(r.path)) && - getRepoExecutionHostId(r) === LOCAL_EXECUTION_HOST_ID - ) - return runWithClonePathLock(clonePathKey, async () => { - await pendingAbortCleanupByPath.get(clonePathKey) - // Read after the lock so lookup sees what a queued clone of this path registered. - savedPathKeys = await getSavedProjectPathKeys(clonePath) - const cloneMetadataRef: { current: ActiveCloneMetadata | null } = { current: null } - let claimedTarget: ClaimedCloneTarget - let proc: Awaited> - // Register before saved-project reads so Cancel can abort those Git calls too. - const pendingController = new AbortController() - pendingLocalCloneControllers.add(pendingController) - try { - const reused = await reuseSavedCloneTarget( - findSaved, - args.url, - LOCAL_EXECUTION_HOST_ID, - pendingController.signal - ) - if (reused) { - emitRepoAdded('clone_url', true, true) - return reused - } - // Fresh installs may lack the destination used as the clone process cwd. - await mkdir(args.destination, { recursive: true }) - claimedTarget = await claimCloneTarget(clonePath) - // Stream progress to avoid execFile buffer limits; --progress also works without a TTY. - try { - // Destination cwd enables WSL routing; '--' prevents URL content from becoming Git flags. - proc = await gitSpawnAfterWindowsEnvironmentReady( - ['clone', '--progress', '--', args.url, clonePath], - { - cwd: args.destination, - // Host Git on purpose: the clone makes a new repo no project owns, even inside one. - hostGit: true, - admissionTier: 'interactive', - // Suppress Windows Git Credential Manager OAuth windows during noninteractive cloning (#7652). - env: promptGuardGitEnv(), - signal: pendingController.signal, - stdio: ['ignore', 'ignore', 'pipe'] - } - ) - } catch (err) { - await cleanupClaimedCloneTarget(clonePath, claimedTarget) - const message = err instanceof Error ? err.message : String(err) - throw new Error(`Clone failed: ${message}`) - } - } finally { - pendingLocalCloneControllers.delete(pendingController) + // Remote projects can share this path string without belonging to this clone host. + const findLocalSaved = (pathKeys: string[]): Repo | undefined => + store + .getRepos() + .find( + (r) => + pathKeys.includes(getClonePathComparisonKey(r.path)) && + getRepoExecutionHostId(r) === LOCAL_EXECUTION_HOST_ID + ) + + ipcMain.handle('repos:checkCloneTarget', (_event, args: CloneTargetArgs) => + checkCloneTarget(args, findLocalSaved, LOCAL_EXECUTION_HOST_ID) + ) + + ipcMain.handle('repos:clone', async (_event, args: CloneTargetArgs): Promise => { + const clonePath = deriveValidatedClonePath(args) + const clonePathKey = getClonePathComparisonKey(clonePath) + let savedPathKeys: string[] = [] + const findSaved = (): Repo | undefined => findLocalSaved(savedPathKeys) + return runWithClonePathLock(clonePathKey, async () => { + await pendingAbortCleanupByPath.get(clonePathKey) + // Read after the lock so lookup sees what a queued clone of this path registered. + savedPathKeys = await getSavedProjectPathKeys(clonePath) + const cloneMetadataRef: { current: ActiveCloneMetadata | null } = { current: null } + let claimedTarget: ClaimedCloneTarget + let proc: Awaited> + // Register before saved-project reads so Cancel can abort those Git calls too. + const pendingController = new AbortController() + pendingLocalCloneControllers.add(pendingController) + try { + const reused = await reuseSavedCloneTarget( + findSaved, + args.url, + LOCAL_EXECUTION_HOST_ID, + pendingController.signal + ) + if (reused) { + emitRepoAdded('clone_url', true, true) + return reused } - await new Promise((resolve, reject) => { - const generation = nextCloneGeneration++ - latestCloneGenerationByPath.set(clonePathKey, generation) - const metadata: ActiveCloneMetadata = { - path: clonePath, - pathKey: clonePathKey, - claimedTarget, - process: proc, - abortRequested: false, - generation, - pendingAbortCleanup: null, - resolvePendingAbortCleanup: null - } - cloneMetadataRef.current = metadata - activeClone = metadata - - let stderrTail = '' - let settled = false - proc.stderr!.on('data', (chunk: Buffer) => { - const text = chunk.toString() - stderrTail = (stderrTail + text).slice(-4096) - - // Git progress uses carriage-return fragments, matching the SSH clone parser. - emitCloneProgressFromText(mainWindow, text) - }) - - const finishClone = async ( - code: number | null, - signal: NodeJS.Signals | null, - err?: Error - ) => { - if (settled) { - return - } - settled = true - // Abort-and-retry may already have replaced the active process. - if (activeClone?.process === proc) { - activeClone = null + // Fresh installs may lack the destination used as the clone process cwd. + await mkdir(args.destination, { recursive: true }) + claimedTarget = await claimCloneTarget(clonePath) + // Stream progress to avoid execFile buffer limits; --progress also works without a TTY. + try { + // Destination cwd enables WSL routing; '--' prevents URL content from becoming Git flags. + proc = await gitSpawnAfterWindowsEnvironmentReady( + ['clone', '--progress', '--', args.url, clonePath], + { + cwd: args.destination, + // Host Git on purpose: the clone makes a new repo no project owns, even inside one. + hostGit: true, + admissionTier: 'interactive', + // Suppress Windows Git Credential Manager OAuth windows during noninteractive cloning (#7652). + env: promptGuardGitEnv(), + signal: pendingController.signal, + stdio: ['ignore', 'ignore', 'pipe'] } + ) + } catch (err) { + await cleanupClaimedCloneTarget(clonePath, claimedTarget) + const message = err instanceof Error ? err.message : String(err) + throw new Error(`Clone failed: ${message}`) + } + } finally { + pendingLocalCloneControllers.delete(pendingController) + } + await new Promise((resolve, reject) => { + const generation = nextCloneGeneration++ + latestCloneGenerationByPath.set(clonePathKey, generation) + const metadata: ActiveCloneMetadata = { + path: clonePath, + pathKey: clonePathKey, + claimedTarget, + process: proc, + abortRequested: false, + generation, + pendingAbortCleanup: null, + resolvePendingAbortCleanup: null + } + cloneMetadataRef.current = metadata + activeClone = metadata - const cloneSucceeded = !err && code === 0 && !signal - if (!cloneSucceeded) { - // Failed-clone cleanup belongs only to the process that created this target. - await cleanupOwnedCloneTarget(metadata) - } - if (metadata.abortRequested && !cloneSucceeded) { - settleCloneAbortCleanup(metadata) - } - if (latestCloneGenerationByPath.get(metadata.pathKey) === metadata.generation) { - latestCloneGenerationByPath.delete(metadata.pathKey) - } + let stderrTail = '' + let settled = false + proc.stderr!.on('data', (chunk: Buffer) => { + const text = chunk.toString() + stderrTail = (stderrTail + text).slice(-4096) - if (err) { - reject(new Error(`Clone failed: ${err.message}`)) - } else if (signal === 'SIGTERM') { - reject(new Error('Clone aborted')) - } else if (code === 0) { - resolve() - } else { - reject( - new Error(`Clone failed: ${getGitCloneFailureMessage(stderrTail, { clonePath })}`) - ) - } - } - - proc.on('error', (err) => { - void finishClone(null, null, err) - }) - - proc.on('close', (code, signal) => { - void finishClone(code, signal) - }) + // Git progress uses carriage-return fragments, matching the SSH clone parser. + emitCloneProgressFromText(mainWindow, text) }) - try { - // Clone may land in an existing folder project; upgrade it instead of duplicating registration. - savedPathKeys = await getSavedProjectPathKeys(clonePath) - const existing = findSaved() - if (existing) { - const conversion = await resolveClonedFolderConversion(store, existing) - const current = findSaved() - if ( - current && - conversion && - findExtraFolderWorkspaceIds(current, store.getAllWorktreeMeta()).length === 0 && - current.path === existing.path && - isFolderRepo(current) && - getRepoExecutionHostId(current) === getRepoExecutionHostId(existing) - ) { - const updated = store.updateRepo(current, { - kind: 'git', - ...conversion, - projectHostSetupMethod: 'cloned' - }) - if (updated) { - await prepareLocalWorktreeRootForRepo(store, updated) - invalidateAuthorizedRootsCache() - notifyReposChanged(mainWindow) - // Folder-to-Git conversion counts as new Git provisioning. - emitRepoAdded('clone_url', false, true) - return updated - } - } - // Recreating the folder loses its worktree root and invalidates cached authorization. - if (current) { - await prepareLocalWorktreeRootForRepo(store, current) - invalidateAuthorizedRootsCache() - notifyReposChanged(mainWindow) - emitRepoAdded('clone_url', true, true) - return current - } + const finishClone = async ( + code: number | null, + signal: NodeJS.Signals | null, + err?: Error + ) => { + if (settled) { + return + } + settled = true + // Abort-and-retry may already have replaced the active process. + if (activeClone?.process === proc) { + activeClone = null } - // Register Git's checkout path so the clone stays its own selected checkout. - const repoPath = (await inspectGitRepoForRegistrationAsync(clonePath)).rootPath - const detected = await detectRepoIconAndUpstream({ - repoPath, - kind: 'git', - executionHostId: LOCAL_EXECUTION_HOST_ID - }) - const repo: Repo = { - id: randomUUID(), - path: repoPath, - displayName: getRepoName(clonePath), - badgeColor: DEFAULT_REPO_BADGE_COLOR, - ...detected, - addedAt: Date.now(), - kind: 'git', - externalWorktreeVisibilityLegacy: false, - projectHostSetupMethod: 'cloned' + const cloneSucceeded = !err && code === 0 && !signal + if (!cloneSucceeded) { + // Failed-clone cleanup belongs only to the process that created this target. + await cleanupOwnedCloneTarget(metadata) } - - store.addRepo(repo) - await prepareLocalWorktreeRootForRepo(store, repo) - invalidateAuthorizedRootsCache() - notifyReposChanged(mainWindow) - emitRepoAdded('clone_url', false, true) - return repo - } finally { - const metadata = cloneMetadataRef.current - if (metadata?.abortRequested) { + if (metadata.abortRequested && !cloneSucceeded) { settleCloneAbortCleanup(metadata) } + if (latestCloneGenerationByPath.get(metadata.pathKey) === metadata.generation) { + latestCloneGenerationByPath.delete(metadata.pathKey) + } + + if (err) { + reject(new Error(`Clone failed: ${err.message}`)) + } else if (signal === 'SIGTERM') { + reject(new Error('Clone aborted')) + } else if (code === 0) { + resolve() + } else { + reject( + new Error(`Clone failed: ${getGitCloneFailureMessage(stderrTail, { clonePath })}`) + ) + } } + + proc.on('error', (err) => { + void finishClone(null, null, err) + }) + + proc.on('close', (code, signal) => { + void finishClone(code, signal) + }) }) - } - ) + + try { + // Clone may land in an existing folder project; upgrade it instead of duplicating registration. + savedPathKeys = await getSavedProjectPathKeys(clonePath) + const existing = findSaved() + if (existing) { + const conversion = await resolveClonedFolderConversion(store, existing) + const current = findSaved() + if ( + current && + conversion && + findExtraFolderWorkspaceIds(current, store.getAllWorktreeMeta()).length === 0 && + current.path === existing.path && + isFolderRepo(current) && + getRepoExecutionHostId(current) === getRepoExecutionHostId(existing) + ) { + const updated = store.updateRepo(current, { + kind: 'git', + ...conversion, + projectHostSetupMethod: 'cloned' + }) + if (updated) { + await prepareLocalWorktreeRootForRepo(store, updated) + invalidateAuthorizedRootsCache() + notifyReposChanged(mainWindow) + // Folder-to-Git conversion counts as new Git provisioning. + emitRepoAdded('clone_url', false, true) + return updated + } + } + // Recreating the folder loses its worktree root and invalidates cached authorization. + if (current) { + await prepareLocalWorktreeRootForRepo(store, current) + invalidateAuthorizedRootsCache() + notifyReposChanged(mainWindow) + emitRepoAdded('clone_url', true, true) + return current + } + } + + // Register Git's checkout path so the clone stays its own selected checkout. + const repoPath = (await inspectGitRepoForRegistrationAsync(clonePath)).rootPath + const detected = await detectRepoIconAndUpstream({ + repoPath, + kind: 'git', + executionHostId: LOCAL_EXECUTION_HOST_ID + }) + const repo: Repo = { + id: randomUUID(), + path: repoPath, + displayName: getRepoName(clonePath), + badgeColor: DEFAULT_REPO_BADGE_COLOR, + ...detected, + addedAt: Date.now(), + kind: 'git', + externalWorktreeVisibilityLegacy: false, + projectHostSetupMethod: 'cloned' + } + + store.addRepo(repo) + await prepareLocalWorktreeRootForRepo(store, repo) + invalidateAuthorizedRootsCache() + notifyReposChanged(mainWindow) + emitRepoAdded('clone_url', false, true) + return repo + } finally { + const metadata = cloneMetadataRef.current + if (metadata?.abortRequested) { + settleCloneAbortCleanup(metadata) + } + } + }) + }) } diff --git a/src/main/runtime/construction/runtime-host-services.ts b/src/main/runtime/construction/runtime-host-services.ts index b297b9631d4..b1515e72345 100644 --- a/src/main/runtime/construction/runtime-host-services.ts +++ b/src/main/runtime/construction/runtime-host-services.ts @@ -280,7 +280,7 @@ export abstract class OrcaRuntimeWithPreservedBranchCleanup extends OrcaRuntimeW getStore: () => this.store, listRepos: () => this.listRepos(), addRepo: (path, kind, hostId) => this.addRepo(path, kind, hostId), - cloneRepo: (url, destination, hostId) => this.cloneRepo(url, destination, hostId), + cloneRepo: (args) => this.cloneRepo(args.url, args.destination, args.hostId, args.folderName), invalidateResolvedWorktrees: () => this.invalidateResolvedWorktreeCache(), invalidateWorktreeScan: (repoId) => this.invalidateWorktreeScanCacheForRepo(repoId), notifyReposChanged: () => this.notifyReposChanged(), diff --git a/src/main/runtime/repositories/runtime-project-host-setup-controller.ts b/src/main/runtime/repositories/runtime-project-host-setup-controller.ts index e29c4afc4de..87fd723ece6 100644 --- a/src/main/runtime/repositories/runtime-project-host-setup-controller.ts +++ b/src/main/runtime/repositories/runtime-project-host-setup-controller.ts @@ -51,7 +51,7 @@ type RuntimeProjectHostSetupDependencies = { getStore: () => RuntimeStore | null listRepos: () => Repo[] addRepo: (path: string, kind: 'folder' | 'git', hostId: ExecutionHostId) => Promise - cloneRepo: (url: string, destination: string, hostId: ExecutionHostId) => Promise + cloneRepo: (args: ProjectHostSetupCloneArgs) => Promise invalidateResolvedWorktrees: () => void invalidateWorktreeScan: (repoId: string) => void notifyReposChanged: () => void @@ -151,7 +151,7 @@ export class RuntimeProjectHostSetupController { async setupClone(args: ProjectHostSetupCloneArgs): Promise { assertCloneHostIsSupported(args.hostId) const knownRepoIds = new Set(this.deps.listRepos().map((repo) => repo.id)) - const repo = await this.deps.cloneRepo(args.url, args.destination, args.hostId) + const repo = await this.deps.cloneRepo(args) return this.completeSetup( { ...args, path: repo.path, kind: 'git', setupMethod: 'cloned' }, repo, diff --git a/src/main/runtime/repositories/runtime-repository-clone-controller.ts b/src/main/runtime/repositories/runtime-repository-clone-controller.ts index 9d6706fe5e5..7d74b2272e3 100644 --- a/src/main/runtime/repositories/runtime-repository-clone-controller.ts +++ b/src/main/runtime/repositories/runtime-repository-clone-controller.ts @@ -16,6 +16,11 @@ import { import { gitSpawnAfterWindowsEnvironmentReady } from '../../git/command-runner/git-spawn' import { promptGuardGitEnv } from '../../git/command-runner/git-process-env' import { reuseSavedCloneTarget } from '../../git/saved-clone-target' +import { checkCloneTarget } from '../../git/clone-target-check' +import type { + CloneTargetArgs, + CloneTargetCheck +} from '../../../shared/git/clone/clone-target-check' import { runWithGitReadCacheInvalidation } from '../../git/source-control/git-read-cache-invalidation' import { invalidateAuthorizedRootsCache } from '../../ipc/worktrees/removal/registered-worktree-roots-cache' import { detectRepoIconAndUpstream } from '../../repository-icons/repo-icon-autodetect' @@ -34,15 +39,46 @@ type RuntimeRepositoryCloneDependencies = { notifyReposChanged: () => void } +// Git runs here; an SSH row with the same path refers to another machine. +function findRegistrationCandidateAt( + store: RuntimeStore, + pathKeys: string[], + executionHostId?: ExecutionHostId | null +): Repo | undefined { + return store + .getRepos() + .find( + (repo) => + pathKeys.includes(getClonePathComparisonKey(repo.path)) && + runtimeRepoIsRegistrationCandidate(repo, executionHostId) + ) +} + export class RuntimeRepositoryCloneController { private readonly inFlightByPath = new Map>() constructor(private readonly deps: RuntimeRepositoryCloneDependencies) {} + async checkTarget( + args: CloneTargetArgs, + executionHostId?: ExecutionHostId | null + ): Promise { + const store = this.deps.getStore() + if (!store) { + throw new Error('runtime_unavailable') + } + return checkCloneTarget( + { ...args, url: args.url.trim(), destination: args.destination.trim() }, + (pathKeys) => findRegistrationCandidateAt(store, pathKeys, executionHostId), + LOCAL_EXECUTION_HOST_ID + ) + } + async clone( url: string, destination: string, - executionHostId?: ExecutionHostId | null + executionHostId?: ExecutionHostId | null, + folderName?: string ): Promise { if (!this.deps.getStore()) { throw new Error('runtime_unavailable') @@ -52,7 +88,11 @@ export class RuntimeRepositoryCloneController { if (!trimmedDestination) { throw new Error('Clone destination is required') } - const clonePath = deriveValidatedClonePath({ url: trimmedUrl, destination: trimmedDestination }) + const clonePath = deriveValidatedClonePath({ + url: trimmedUrl, + destination: trimmedDestination, + folderName + }) await refuseWslHostWindowsDrivePath(trimmedDestination) const clonePathKey = getClonePathComparisonKey(clonePath) const previous = this.inFlightByPath.get(clonePathKey) ?? Promise.resolve() @@ -89,14 +129,8 @@ export class RuntimeRepositoryCloneController { throw new Error('runtime_unavailable') } let savedPathKeys = await getSavedProjectPathKeys(clonePath) - // Git runs here; an SSH row with the same path refers to another machine. const findSaved = (): Repo | undefined => - store.getRepos().find((repo) => { - return ( - savedPathKeys.includes(getClonePathComparisonKey(repo.path)) && - runtimeRepoIsRegistrationCandidate(repo, executionHostId) - ) - }) + findRegistrationCandidateAt(store, savedPathKeys, executionHostId) const reused = await reuseSavedCloneTarget(findSaved, trimmedUrl, LOCAL_EXECUTION_HOST_ID) if (reused) { return reused diff --git a/src/main/runtime/repositories/runtime-repository-command-surface.ts b/src/main/runtime/repositories/runtime-repository-command-surface.ts index c533644ee71..928c9142fc4 100644 --- a/src/main/runtime/repositories/runtime-repository-command-surface.ts +++ b/src/main/runtime/repositories/runtime-repository-command-surface.ts @@ -64,6 +64,7 @@ export type RuntimeRepositoryCommandSurface = { addRepo: RuntimeRepositoryRegistrationController['add'] createRepo: RuntimeRepositoryRegistrationController['create'] cloneRepo: RuntimeRepositoryCloneController['clone'] + checkCloneTarget: RuntimeRepositoryCloneController['checkTarget'] showRepo: RuntimeRepositorySettingsController['show'] setRepoBaseRef: RuntimeRepositorySettingsController['setBaseRef'] updateRepo: RuntimeRepositorySettingsController['update'] @@ -142,6 +143,7 @@ export function installRuntimeRepositoryCommandSurface( addRepo: registrations.add.bind(registrations), createRepo: registrations.create.bind(registrations), cloneRepo: clones.clone.bind(clones), + checkCloneTarget: clones.checkTarget.bind(clones), showRepo: settings.show.bind(settings), setRepoBaseRef: settings.setBaseRef.bind(settings), updateRepo: settings.update.bind(settings), diff --git a/src/main/runtime/rpc/methods/projects/repo.ts b/src/main/runtime/rpc/methods/projects/repo.ts index a745f852106..e3987d8a38d 100644 --- a/src/main/runtime/rpc/methods/projects/repo.ts +++ b/src/main/runtime/rpc/methods/projects/repo.ts @@ -173,11 +173,22 @@ export const REPO_METHODS = [ params: RepoClone, handler: async (params, context) => ({ repo: projectRepoVisibilityForClient( - await context.runtime.cloneRepo(params.url, params.destination), + await context.runtime.cloneRepo( + params.url, + params.destination, + undefined, + params.folderName + ), context ) }) }), + defineMethod({ + name: 'repo.checkCloneTarget', + permission: 'workspace', + params: RepoClone, + handler: async (params, { runtime }) => ({ target: await runtime.checkCloneTarget(params) }) + }), defineMethod({ name: 'repo.show', permission: 'workspace', diff --git a/src/preload/api/repos-bridge.ts b/src/preload/api/repos-bridge.ts index a5c1ec58e36..af5c8549aa7 100644 --- a/src/preload/api/repos-bridge.ts +++ b/src/preload/api/repos-bridge.ts @@ -35,6 +35,8 @@ export const reposApi = { clone: (args) => ipcRenderer.invoke('repos:clone', args), + checkCloneTarget: (args) => ipcRenderer.invoke('repos:checkCloneTarget', args), + cloneAbort: () => ipcRenderer.invoke('repos:cloneAbort'), onCloneProgress: (callback: (data: { phase: string; percent: number }) => void): (() => void) => { diff --git a/src/preload/api/repository-api.ts b/src/preload/api/repository-api.ts index 0b134c50cc9..e74b1a80af9 100644 --- a/src/preload/api/repository-api.ts +++ b/src/preload/api/repository-api.ts @@ -1,4 +1,5 @@ import type { RepoCreateFailure } from '../../shared/repo-create-failure-copy' +import type { CloneTargetArgs, CloneTargetCheck } from '../../shared/git/clone/clone-target-check' import type { ExecutionHostId } from '../../shared/execution-host' import type { GhAccountBinding } from '../../shared/github/account-binding' import type { @@ -80,7 +81,8 @@ export type RepositoryApi = { pickFolder: () => Promise pickFolders: () => Promise pickDirectory: () => Promise - clone: (args: { url: string; destination: string }) => Promise + clone: (args: CloneTargetArgs) => Promise + checkCloneTarget: (args: CloneTargetArgs) => Promise cloneAbort: () => Promise create: (args: { parentPath: string diff --git a/src/renderer/src/components/new-workspace/SetProjectLocationDialog.tsx b/src/renderer/src/components/new-workspace/SetProjectLocationDialog.tsx index 8e79a471bd3..1fc7424bee0 100644 --- a/src/renderer/src/components/new-workspace/SetProjectLocationDialog.tsx +++ b/src/renderer/src/components/new-workspace/SetProjectLocationDialog.tsx @@ -1,4 +1,6 @@ import { useEffect, useRef, useState, type RefObject } from 'react' +import { useCloneFolderOverride } from '../sidebar/project-admission/clone-folder/use-clone-folder-override' +import { getProjectCloneCheckTarget } from '../../store/projects/project-host-routing' import { Download, FolderOpen } from 'lucide-react' import { Dialog, @@ -108,6 +110,8 @@ function SetProjectLocationDialogBody({ const [setupKind, setSetupKind] = useState(projectKind) const [cloneUrl, setCloneUrl] = useState(defaultCloneUrl) const [cloneDestination, setCloneDestination] = useState('') + const { cloneFolder, cloneArgs, setCloneFolderName, checkCloneFolderTaken } = + useCloneFolderOverride(cloneUrl, cloneDestination) const [isSubmitting, setIsSubmitting] = useState(false) // Clones may finish long after dismissal; do not retarget a composer the user has since moved to another host. const abandoned = useRef(false) @@ -176,11 +180,13 @@ function SetProjectLocationDialogBody({ } setIsSubmitting(true) try { + if (await checkCloneFolderTaken(getProjectCloneCheckTarget(option.hostId))) { + return + } const result = await setupProjectClone({ projectId: option.projectId, hostId: option.hostId, - url: cloneUrl.trim(), - destination: cloneDestination.trim(), + ...cloneArgs, displayName: projectName }) if (result && !abandoned.current) { @@ -277,6 +283,8 @@ function SetProjectLocationDialogBody({ onBack={() => setView('choose')} onCloneUrlChange={setCloneUrl} onCloneDestinationChange={setCloneDestination} + cloneFolder={cloneFolder} + onCloneFolderNameChange={setCloneFolderName} onBrowse={() => openHostBrowser('clone')} onSubmit={() => void handleCloneSubmit()} /> diff --git a/src/renderer/src/components/new-workspace/SetProjectLocationForms.tsx b/src/renderer/src/components/new-workspace/SetProjectLocationForms.tsx index 248c8f9abbb..cb14f715638 100644 --- a/src/renderer/src/components/new-workspace/SetProjectLocationForms.tsx +++ b/src/renderer/src/components/new-workspace/SetProjectLocationForms.tsx @@ -1,4 +1,6 @@ import { ArrowLeft, FolderOpen, LoaderCircle } from 'lucide-react' +import { CloneFolderNameField } from '../sidebar/project-admission/clone-folder/CloneFolderNameField' +import type { CloneFolderOverride } from '../sidebar/project-admission/clone-folder/use-clone-folder-override' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { @@ -103,6 +105,8 @@ export function CloneForm({ onBack, onCloneUrlChange, onCloneDestinationChange, + cloneFolder, + onCloneFolderNameChange, onBrowse, onSubmit }: { @@ -112,6 +116,8 @@ export function CloneForm({ onBack: () => void onCloneUrlChange: (value: string) => void onCloneDestinationChange: (value: string) => void + cloneFolder: CloneFolderOverride | null + onCloneFolderNameChange: (name: string) => void onBrowse: () => void onSubmit: () => void }): React.JSX.Element { @@ -157,11 +163,23 @@ export function CloneForm({
+ {cloneFolder ? ( + + ) : null}
+ {cloneFolder ? ( + + ) : null}
+ {cloneFolder && onFolderNameChange && ( + + )} + {cloneError &&

{cloneError}

} - ) : null} - {extension.source === 'unpacked' ? ( diff --git a/src/renderer/src/components/settings/form-controls/SettingsFormControls.tsx b/src/renderer/src/components/settings/form-controls/SettingsFormControls.tsx index 5d91fa2a81a..80348d4cd9f 100644 --- a/src/renderer/src/components/settings/form-controls/SettingsFormControls.tsx +++ b/src/renderer/src/components/settings/form-controls/SettingsFormControls.tsx @@ -10,32 +10,6 @@ import { normalizeColor } from '@/lib/terminal-theme' import { cn } from '@/lib/utils' import { useTranslate } from '@/i18n/use-translate' -type SettingsSwitchProps = { - checked: boolean - onChange: () => void - ariaLabel?: string - ariaLabelledBy?: string - disabled?: boolean -} - -export function SettingsSwitch({ - checked, - onChange, - ariaLabel, - ariaLabelledBy, - disabled -}: SettingsSwitchProps): React.JSX.Element { - return ( - - ) -} - type SettingsRowProps = { label: React.ReactNode description?: React.ReactNode @@ -101,11 +75,11 @@ export function SettingsSwitchRow({ description={description} className={className} control={ - } /> diff --git a/src/renderer/src/components/settings/notifications/NotificationsPane.tsx b/src/renderer/src/components/settings/notifications/NotificationsPane.tsx index b3ad3f926e6..44c9d24e8dd 100644 --- a/src/renderer/src/components/settings/notifications/NotificationsPane.tsx +++ b/src/renderer/src/components/settings/notifications/NotificationsPane.tsx @@ -11,13 +11,30 @@ import { import { NotificationSettingToggle } from './NotificationSettingToggle' import { NotificationHostToggles } from './NotificationHostToggles' import { NotificationSoundSection } from './NotificationSoundSection' -import { - createNotificationVolumeDraftState, - resolveNotificationVolumeDraftState, - sendNotificationSettingsTestNotification -} from './notification-settings-copy' +import { sendNotificationSettingsTestNotification } from '@/components/notifications/notification-test-delivery' import { useTranslate } from '@/i18n/use-translate' +type NotificationVolumeDraftState = { + sourceVolume: number + draft: number +} + +function createNotificationVolumeDraftState(sourceVolume: number): NotificationVolumeDraftState { + return { + sourceVolume, + draft: sourceVolume + } +} + +function resolveNotificationVolumeDraftState( + state: NotificationVolumeDraftState, + sourceVolume: number +): NotificationVolumeDraftState { + return state.sourceVolume === sourceVolume + ? state + : createNotificationVolumeDraftState(sourceVolume) +} + type NotificationsPaneProps = { settings: GlobalSettings updateSettings: (updates: Partial) => void | Promise 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 cba6f693dd0..e3ec3097cca 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 @@ -9,7 +9,7 @@ import { VoicePane } from '../voice/VoicePane' import { SettingsSection } from '../section-layout/SettingsSection' import { SettingsHostScopeUnavailableNotice } from '../host-scope/SettingsHostScopePicker' import { LOCAL_SETTINGS_HOST_SCOPE } from '../host-scope/settings-host-scope' -import { wslHostDistroForTarget } from '@/lib/wsl-host-account-owner' +import { wslHostDistroForTarget } from '@/runtime/provider-accounts/ownership/wsl-host-account-owner' import { translate } from '@/i18n/i18n' import type { Translate } from '@/i18n/use-translate' import type { SettingsRenderContext } from './settings-render-context' diff --git a/src/renderer/src/components/settings/repository/hooks/RepositoryHookPolicySettings.tsx b/src/renderer/src/components/settings/repository/hooks/RepositoryHookPolicySettings.tsx index be8c1bb7854..4a7b83f505e 100644 --- a/src/renderer/src/components/settings/repository/hooks/RepositoryHookPolicySettings.tsx +++ b/src/renderer/src/components/settings/repository/hooks/RepositoryHookPolicySettings.tsx @@ -9,7 +9,7 @@ import { Trans } from 'react-i18next' import { i18n, translate } from '@/i18n/i18n' import { useTranslate, type Translate } from '@/i18n/use-translate' import { matchesSettingsSearch } from '../../search/settings-search' -import { SettingsSwitch } from '../../form-controls/SettingsFormControls' +import { Switch } from '@/components/ui/switch' import { RepositoryHooksYamlStatus } from './RepositoryHooksYamlStatus' type PolicyOption = { policy: Policy; label: string; description: string } @@ -142,14 +142,14 @@ export function RepositorySetupPolicySetting({ )} - + onCheckedChange={() => onStartupPolicyChange( setupAgentStartupPolicy === 'wait-for-setup' ? 'start-immediately' : 'wait-for-setup' ) } - ariaLabel={t( + aria-label={t( 'auto.components.settings.RepositoryHooksSection.waitForSetupBeforeAgent', 'Wait for setup to complete before starting agent' )} diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-action-agent-draft.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-action-agent-draft.ts index 81e47fdfb35..a8b71989f35 100644 --- a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-action-agent-draft.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-action-agent-draft.ts @@ -7,10 +7,7 @@ import type { import type { RepoSourceControlAiOverrides } from '../../../../../../shared/source-control/ai/settings/source-control-ai-settings-types' import type { GlobalSettings } from '../../../../../../shared/global-settings-types' import { normalizeRepoAiDraft, setActionOverride } from './repository-source-control-ai-draft' - -type RepoActionRecipe = NonNullable< - NonNullable[SourceControlActionId] -> +import type { RepoActionRecipe } from './repository-source-control-ai-recipe-completion' export function withRepoAiActionAgent( base: RepoSourceControlAiOverrides, diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts index db7d5e42d0f..971de67c0df 100644 --- a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-completion.ts @@ -15,9 +15,7 @@ export type RepoActionRecipe = NonNullable< function completeRepoActionRecipe( recipe: SourceControlActionRecipe, actionId: SourceControlActionId -): NonNullable< - NonNullable[SourceControlActionId] -> { +): RepoActionRecipe { if (recipe.argumentChoices && recipe.argumentChoices.syntaxAgent !== recipe.agentId) { recipe = demoteActionArgumentChoices(recipe) } diff --git a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-text.ts b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-text.ts index 1d1bf4077af..2ec6d0be6ce 100644 --- a/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-text.ts +++ b/src/renderer/src/components/settings/repository/source-control/repository-source-control-ai-recipe-text.ts @@ -4,9 +4,7 @@ import type { } from '../../../../../../shared/source-control/ai/settings/source-control-ai-settings-types' import type { SourceControlActionId } from '../../../../../../shared/source-control/ai/recipes/source-control-ai-actions' import type { SourceControlAiModelEdits } from '../../../../../../shared/source-control/ai/settings/source-control-ai-action-model-edits' -type RepoActionRecipe = NonNullable< - NonNullable[SourceControlActionId] -> +import type { RepoActionRecipe } from './repository-source-control-ai-recipe-completion' /** The action's text as stored: an unset or cleared value stays so until the user types; inputs * show it as empty. */ diff --git a/src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-mutation-actions.ts b/src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-mutation-actions.ts index 3c1c6d17912..8df8d37b596 100644 --- a/src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-mutation-actions.ts +++ b/src/renderer/src/components/settings/runtime-environments/catalog/use-runtime-environment-mutation-actions.ts @@ -6,7 +6,7 @@ import type { GlobalSettings } from '../../../../../../shared/global-settings-ty import type { PublicKnownRuntimeEnvironment } from '../../../../../../shared/remote-runtime/host-catalog/runtime-environments' import type { RuntimeStatus } from '../../../../../../shared/runtime-session-contracts' import { isRuntimeEnvironmentRemovalBlocked } from './runtime-environment-host-details' -import { defaultServerNameForAccessLink } from './runtime-server-default-name' +import { defaultServerNameForAccessLink } from '../../../../../../shared/remote-runtime/host-catalog/runtime-host-default-name' import { forgetCachedRuntimeHostDetails } from './use-runtime-environment-catalog' import type { RuntimeHostAccessFailure } from '../../../runtime-access/RuntimeHostAccessFailureAlert' import { warnWslHostServerStop } from '../../../sidebar/wsl-host-removal' diff --git a/src/renderer/src/components/settings/session-history/SessionHistoryComputerRow.tsx b/src/renderer/src/components/settings/session-history/SessionHistoryComputerRow.tsx index 69c2e659b98..96d3543e1f6 100644 --- a/src/renderer/src/components/settings/session-history/SessionHistoryComputerRow.tsx +++ b/src/renderer/src/components/settings/session-history/SessionHistoryComputerRow.tsx @@ -2,7 +2,7 @@ import { Laptop, Server } from 'lucide-react' import { Button } from '@/components/ui/button' import { cn } from '@/lib/utils' import { useTranslate } from '@/i18n/use-translate' -import { SettingsSwitch } from '../form-controls/SettingsFormControls' +import { Switch } from '@/components/ui/switch' const NO_DETAILS: readonly string[] = [] @@ -73,11 +73,11 @@ export function SessionHistoryComputerRow({ ))}
- diff --git a/src/renderer/src/components/settings/skills/linear/LinearAgentSkillPane.tsx b/src/renderer/src/components/settings/skills/linear/LinearAgentSkillPane.tsx index 87f8ea69d99..86f57bd479b 100644 --- a/src/renderer/src/components/settings/skills/linear/LinearAgentSkillPane.tsx +++ b/src/renderer/src/components/settings/skills/linear/LinearAgentSkillPane.tsx @@ -17,7 +17,7 @@ import { getLinearUsageExamples } from '@/lib/linear-usage-examples' import type { SkillUsageExample } from '@/lib/skill-usage-example' import { useLinearProviderConnected } from '@/hooks/useLinearProviderConnected' import { normalizeVisibleTaskProviders } from '../../../../../../shared/work-items/task-providers' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { AgentSkillSetupPanel } from '../../../skills/setup/AgentSkillSetupPanel' import { LinearAgentSkillGuide } from './LinearAgentSkillGuide' import { LinearAgentSkillNotes } from './LinearAgentSkillNotes' diff --git a/src/renderer/src/components/settings/tasks/use-task-source-provider-readiness.ts b/src/renderer/src/components/settings/tasks/use-task-source-provider-readiness.ts index eb2476cb544..805ca4ebbea 100644 --- a/src/renderer/src/components/settings/tasks/use-task-source-provider-readiness.ts +++ b/src/renderer/src/components/settings/tasks/use-task-source-provider-readiness.ts @@ -9,7 +9,7 @@ import { useLinearProviderConnected } from '@/hooks/useLinearProviderConnected' import { LINEAR_AGENT_SKILL_NAMES } from '@/lib/agent-feature-skill-names' import { getLocalPreflightContext } from '@/lib/local-preflight-context' import { localPreflightContextKey } from '@/lib/local-preflight-context-key' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { useAppStore } from '@/store/index' import type { TaskProviderReadiness } from './task-source-setup-state' diff --git a/src/renderer/src/components/settings/terminal/appearance/TerminalSettingsPreview.tsx b/src/renderer/src/components/settings/terminal/appearance/TerminalSettingsPreview.tsx index f459dd8ad93..2551f6d0cf9 100644 --- a/src/renderer/src/components/settings/terminal/appearance/TerminalSettingsPreview.tsx +++ b/src/renderer/src/components/settings/terminal/appearance/TerminalSettingsPreview.tsx @@ -17,7 +17,7 @@ import { resolveTerminalFontWeights } from '../../../../../../shared/terminal/ap import { resolveTerminalLigaturesEnabled } from '../../../../../../shared/terminal/appearance/terminal-ligatures' import { normalizeTerminalLineHeight } from '../../../../../../shared/terminal/appearance/terminal-line-height-settings' import { PREVIEW_BUFFER } from './terminal-preview-content' -import { SettingsSwitch } from '../../form-controls/SettingsFormControls' +import { Switch } from '@/components/ui/switch' import type { GlobalSettings } from '../../../../../../shared/global-settings-types' import { useTranslate } from '@/i18n/use-translate' @@ -267,10 +267,10 @@ export function TerminalSettingsPreview({ {t('auto.components.settings.TerminalSettingsPreview.50419052fe', 'Pane divider')} - setPreviewPaneDividerVisible((visible) => !visible)} - ariaLabel={t( + onCheckedChange={() => setPreviewPaneDividerVisible((visible) => !visible)} + aria-label={t( 'auto.components.settings.TerminalSettingsPreview.f8931d407d', 'Show pane divider in preview' )} diff --git a/src/renderer/src/components/settings/virtual-machines/recipes/EphemeralVmsExperimentalSetting.tsx b/src/renderer/src/components/settings/virtual-machines/recipes/EphemeralVmsExperimentalSetting.tsx index 3f02e87d580..af0982fdc6e 100644 --- a/src/renderer/src/components/settings/virtual-machines/recipes/EphemeralVmsExperimentalSetting.tsx +++ b/src/renderer/src/components/settings/virtual-machines/recipes/EphemeralVmsExperimentalSetting.tsx @@ -4,7 +4,7 @@ import { Label } from '../../../ui/label' import { EphemeralVmsPane } from './EphemeralVmsPane' import { getExperimentalSearchEntry } from '../../advanced/experimental-search' import { SearchableSetting } from '../../section-layout/SearchableSetting' -import { SettingsSwitch } from '../../form-controls/SettingsFormControls' +import { Switch } from '@/components/ui/switch' type EphemeralVmsExperimentalSettingProps = { settings: GlobalSettings @@ -39,13 +39,13 @@ export function EphemeralVmsExperimentalSetting({ )}

- updateSettings({ experimentalEphemeralVms: !enabled })} + onCheckedChange={() => updateSettings({ experimentalEphemeralVms: !enabled })} /> {enabled ? : null} diff --git a/src/renderer/src/components/settings/wsl/hosts/WslHostsExperimentalSetting.tsx b/src/renderer/src/components/settings/wsl/hosts/WslHostsExperimentalSetting.tsx index 2ce7f4c201e..e989a2840cf 100644 --- a/src/renderer/src/components/settings/wsl/hosts/WslHostsExperimentalSetting.tsx +++ b/src/renderer/src/components/settings/wsl/hosts/WslHostsExperimentalSetting.tsx @@ -2,7 +2,7 @@ import type { GlobalSettings } from '../../../../../../shared/global-settings-ty import { useTranslate } from '@/i18n/use-translate' import { Label } from '../../../ui/label' import { SearchableSetting } from '../../section-layout/SearchableSetting' -import { SettingsSwitch } from '../../form-controls/SettingsFormControls' +import { Switch } from '@/components/ui/switch' import { getWslHostsExperimentalSearchEntry } from './wsl-hosts-experimental-search' import { useWslHostServerFilesMissing } from '../../../sidebar/use-wsl-hosts-enabled' import { getWslHostServerFilesMissingLabel } from '../../../sidebar/wsl-host-row-status' @@ -33,13 +33,13 @@ export function WslHostsExperimentalSetting({

{getWslHostServerFilesMissingLabel(t)}

) : null} - updateSettings({ experimentalWslHosts: !enabled })} + onCheckedChange={() => updateSettings({ experimentalWslHosts: !enabled })} /> ) diff --git a/src/renderer/src/components/setup-guide/use-setup-guide-progress.ts b/src/renderer/src/components/setup-guide/use-setup-guide-progress.ts index b19db5ba7dd..83f7f921614 100644 --- a/src/renderer/src/components/setup-guide/use-setup-guide-progress.ts +++ b/src/renderer/src/components/setup-guide/use-setup-guide-progress.ts @@ -5,7 +5,7 @@ import { getRepoExecutionHostId } from '../../../../shared/execution-host' import { checkRuntimeHooks } from '@/runtime/repo-hooks/runtime-hooks-client' import { getLocalPreflightContext, localPreflightContextKey } from '@/lib/local-preflight-context' import { hasEffectiveSetupCommand } from '@/lib/setup-script-status' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { COMPUTER_USE_SKILL_NAME, ORCA_CLI_SKILL_NAME, diff --git a/src/renderer/src/components/sidebar/workspace-board/surface/WorkspaceKanbanSettingsMenu.tsx b/src/renderer/src/components/sidebar/workspace-board/surface/WorkspaceKanbanSettingsMenu.tsx index 77deaad7f55..288f6b4b70b 100644 --- a/src/renderer/src/components/sidebar/workspace-board/surface/WorkspaceKanbanSettingsMenu.tsx +++ b/src/renderer/src/components/sidebar/workspace-board/surface/WorkspaceKanbanSettingsMenu.tsx @@ -10,7 +10,7 @@ import { } from '@/components/ui/dropdown-menu' import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' import { cn } from '@/lib/utils' -import { SettingsSwitch } from '../../../settings/form-controls/SettingsFormControls' +import { Switch } from '@/components/ui/switch' import type { WorkspaceStatusDefinition } from '../../../../../../shared/worktree/types' import { getWorkspaceStatusVisualMeta } from '../../workspace-status/presentation/workspace-status' import WorkspaceStatusAppearancePopover from '../../workspace-status/presentation/WorkspaceStatusAppearancePopover' @@ -94,12 +94,12 @@ export default function WorkspaceKanbanSettingsMenu({ )} - + onCheckedChange={() => onSyncTaskStatusFromWorkspaceBoardChange(!syncTaskStatusFromWorkspaceBoard) } - ariaLabel={t( + aria-label={t( 'auto.components.sidebar.WorkspaceKanbanSettingsMenu.87d24a0c2f', 'Sync board and issue status' )} diff --git a/src/renderer/src/components/sidebar/workspace-card/review/workspace-reference-details.ts b/src/renderer/src/components/sidebar/workspace-card/review/workspace-reference-details.ts index a192b3b21e7..370a4d7cf0b 100644 --- a/src/renderer/src/components/sidebar/workspace-card/review/workspace-reference-details.ts +++ b/src/renderer/src/components/sidebar/workspace-card/review/workspace-reference-details.ts @@ -17,7 +17,7 @@ import { getWorkspaceAttachments } from '../../../../../../shared/workspace-atta import { getWorkspaceAttachmentSourceContext } from './workspace-attachment-source-result' import type { WorktreeCardPrDisplay } from './worktree-card-pr-display' import type { HostedReviewProvider } from '../../../../../../shared/source-control/hosted-review/hosted-review-contracts' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { getRuntimeEnvironmentRevision } from '@/runtime/rpc/environment-revisions/runtime-environment-revision' export type WorkspaceReferenceDetails = { diff --git a/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.tsx b/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.tsx index 8c41ec05206..143267cf225 100644 --- a/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.tsx +++ b/src/renderer/src/components/ssh-host-form/SshHostAdvancedFields.tsx @@ -4,7 +4,7 @@ import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/component import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { cn } from '@/lib/utils' -import { SettingsSwitch } from '../settings/form-controls/SettingsFormControls' +import { Switch } from '@/components/ui/switch' import type { EditingTarget } from './ssh-target-draft' import { useTranslate } from '@/i18n/use-translate' @@ -97,16 +97,16 @@ export function SshHostAdvancedFields({ )}

- + onCheckedChange={() => onFormChange((f) => ({ ...f, systemSshConnectionReuse: !f.systemSshConnectionReuse })) } - ariaLabel={t( + aria-label={t( 'auto.components.settings.SshTargetForm.8c922dffba', 'Reuse SSH connection for faster setup' )} @@ -128,16 +128,16 @@ export function SshHostAdvancedFields({ )}

- + onCheckedChange={() => onFormChange((f) => ({ ...f, systemSshProxyConnectionReuse: !f.systemSshProxyConnectionReuse })) } - ariaLabel={t( + aria-label={t( 'auto.components.settings.SshTargetForm.proxyConnectionReuse', 'Reuse one proxy connection' )} @@ -160,13 +160,13 @@ export function SshHostAdvancedFields({ )}

- + onCheckedChange={() => onFormChange((f) => ({ ...f, reconnectOnLaunch: !f.reconnectOnLaunch })) } - ariaLabel={t( + aria-label={t( 'auto.components.settings.SshTargetForm.reconnectOnLaunch', 'Reconnect on launch' )} @@ -188,16 +188,16 @@ export function SshHostAdvancedFields({ )}

- + onCheckedChange={() => onFormChange((f) => ({ ...f, allowRemoteCliControl: !f.allowRemoteCliControl })) } - ariaLabel={t( + aria-label={t( 'auto.components.settings.SshTargetForm.allowRemoteCliControl', 'Allow this host to control Orca browser tabs in its workspaces' )} diff --git a/src/renderer/src/components/settings/advanced/network/NodeDistMirrorDialog.tsx b/src/renderer/src/components/ssh-host-recovery/NodeDistMirrorDialog.tsx similarity index 97% rename from src/renderer/src/components/settings/advanced/network/NodeDistMirrorDialog.tsx rename to src/renderer/src/components/ssh-host-recovery/NodeDistMirrorDialog.tsx index 6252d0adca5..c160cd8f8a8 100644 --- a/src/renderer/src/components/settings/advanced/network/NodeDistMirrorDialog.tsx +++ b/src/renderer/src/components/ssh-host-recovery/NodeDistMirrorDialog.tsx @@ -9,7 +9,7 @@ import { saveNodeDistMirrorAndRetry } from '@/ssh/node-dist-mirror-recovery' import { toastSshConnectFailure } from '@/ssh/ssh-connect-failure-toast' -import { Button } from '../../../ui/button' +import { Button } from '../ui/button' import { Dialog, DialogContent, @@ -17,9 +17,9 @@ import { DialogFooter, DialogHeader, DialogTitle -} from '../../../ui/dialog' -import { Input } from '../../../ui/input' -import { Label } from '../../../ui/label' +} from '../ui/dialog' +import { Input } from '../ui/input' +import { Label } from '../ui/label' const INPUT_ID = 'node-dist-mirror-dialog-url' const HINT_ID = 'node-dist-mirror-dialog-hint' diff --git a/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx b/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx index 08582d13463..3b07f604a85 100644 --- a/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx +++ b/src/renderer/src/components/status-bar/ClaudeSwitcherForOwner.tsx @@ -4,7 +4,7 @@ import { useProviderAccountList } from './use-provider-account-list' import { providerChatAccountControl } from './ProviderAccountChatButton' import React, { useCallback, useRef, useState } from 'react' import { useAppStore } from '../../store' -import type { AccountOwner } from '@/lib/wsl-host-account-owner' +import type { AccountOwner } from '@/runtime/provider-accounts/ownership/wsl-host-account-owner' import { runtimeTargetEnvironmentId } from '@/runtime/rpc/targets/runtime-client-target' import { selectClaudeProviderAccount, @@ -32,7 +32,7 @@ import { normalizeClaudeStatusRuntimeTarget, resolveClaudeStatusAccountState } from './status-bar-claude-accounts' -import { handleRunningClaudeSessionsAfterSwitch } from '@/lib/claude-account-switch-sessions' +import { handleRunningClaudeSessionsAfterSwitch } from '@/lib/provider-accounts/claude-account-switch-sessions' import { AccountRuntimeToggle } from './StatusBarAccountControls' import { InlineUsageBars, @@ -42,7 +42,7 @@ import { import { AccountSwitchSection, AccountSwitchSignedOutHeading } from './AccountSwitchList' import { AccountSwitchRowItem } from './AccountSwitchRowItem' import { ProviderDetailsMenu } from './ProviderDetailsMenu' -import { signInToClaudeAccount } from '@/lib/claude-account-sign-in' +import { signInToClaudeAccount } from '@/lib/provider-accounts/claude-account-sign-in' // Retain account switch/reset drill-in even though the footer now opens UsageRosterPanel. export function ClaudeSwitcherForOwner({ diff --git a/src/renderer/src/components/status-bar/ClaudeSwitcherMenu.owner-lifetime.test.tsx b/src/renderer/src/components/status-bar/ClaudeSwitcherMenu.owner-lifetime.test.tsx index 52dd509d205..ac375059338 100644 --- a/src/renderer/src/components/status-bar/ClaudeSwitcherMenu.owner-lifetime.test.tsx +++ b/src/renderer/src/components/status-bar/ClaudeSwitcherMenu.owner-lifetime.test.tsx @@ -14,7 +14,7 @@ const mocks = vi.hoisted(() => ({ rpc: vi.fn() })) vi.mock('@/runtime/rpc/calls/runtime-rpc-call', () => ({ callRuntimeRpc: mocks.rpc })) -vi.mock('@/lib/claude-account-switch-sessions', () => ({ +vi.mock('@/lib/provider-accounts/claude-account-switch-sessions', () => ({ handleRunningClaudeSessionsAfterSwitch: vi.fn() })) vi.mock('@/lib/windows-terminal-capabilities', () => ({ diff --git a/src/renderer/src/components/status-bar/status-bar-codex-accounts.ts b/src/renderer/src/components/status-bar/status-bar-codex-accounts.ts index 2e3f09c834e..8eb742a28ef 100644 --- a/src/renderer/src/components/status-bar/status-bar-codex-accounts.ts +++ b/src/renderer/src/components/status-bar/status-bar-codex-accounts.ts @@ -4,7 +4,7 @@ import { translate } from '@/i18n/i18n' import { getCodexAccountDisplayDetail, getCodexAccountDisplayLabel -} from '@/lib/codex-account-display-label' +} from '@/lib/provider-accounts/codex-account-display-label' import { getCodexStatusRuntimeKey, getCodexStatusRuntimeLabel, diff --git a/src/renderer/src/components/task-page/jira/Content.board-owner.test.tsx b/src/renderer/src/components/task-page/jira/Content.board-owner.test.tsx index a868088dd0e..124f199d0bf 100644 --- a/src/renderer/src/components/task-page/jira/Content.board-owner.test.tsx +++ b/src/renderer/src/components/task-page/jira/Content.board-owner.test.tsx @@ -7,7 +7,7 @@ import type { TaskPageComposerActionsModel } from '../use-task-page-composer-act import type { TaskPageJiraBoardView } from './task-page-jira-board-view' import type { TaskSourceContext } from '../../../../../shared/work-items/task-source-context' import { defaultJiraListColumnIds, visibleJiraListColumns } from '../../jira-list-columns' -import { bumpProviderRuntimeSessionGeneration } from '@/lib/provider-runtime-context' +import { bumpProviderRuntimeSessionGeneration } from '@/runtime/hosts/observations/provider-runtime-context' import { TaskPageJiraContent } from './Content' import type * as I18n from '@/i18n/i18n' diff --git a/src/renderer/src/components/task-page/jira/Content.tsx b/src/renderer/src/components/task-page/jira/Content.tsx index 1b87db8c58c..4a7ca204a0c 100644 --- a/src/renderer/src/components/task-page/jira/Content.tsx +++ b/src/renderer/src/components/task-page/jira/Content.tsx @@ -21,7 +21,7 @@ import { ipcOrRuntimeTargetForHostId, runtimeTargetEnvironmentId } from '@/runtime/rpc/targets/runtime-client-target' -import { getEnvironmentRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getEnvironmentRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' export function TaskPageJiraContent({ model }: { diff --git a/src/renderer/src/components/task-page/jira/task-page-jira-board-view.tsx b/src/renderer/src/components/task-page/jira/task-page-jira-board-view.tsx index ca22925a86d..fbe6b20685e 100644 --- a/src/renderer/src/components/task-page/jira/task-page-jira-board-view.tsx +++ b/src/renderer/src/components/task-page/jira/task-page-jira-board-view.tsx @@ -1,6 +1,6 @@ import { useEffect, useRef, useState } from 'react' import { useAppStore } from '@/store/index' -import { getEnvironmentRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getEnvironmentRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { runtimeTargetEnvironmentId } from '@/runtime/rpc/targets/runtime-client-target' import { useTranslate } from '@/i18n/use-translate' import { classifyTransitionRequirements } from '../../../../../shared/jira/jira-transition-fields' diff --git a/src/renderer/src/components/task-page/jira/task-page-jira-status-order.ts b/src/renderer/src/components/task-page/jira/task-page-jira-status-order.ts index 0530df4d4d2..bd5ce28efd1 100644 --- a/src/renderer/src/components/task-page/jira/task-page-jira-status-order.ts +++ b/src/renderer/src/components/task-page/jira/task-page-jira-status-order.ts @@ -1,7 +1,7 @@ import type { JiraIssue, JiraProjectStatusOrder } from '../../../../../shared/jira/jira-types' import type { RuntimeClientTarget } from '../../../../../shared/runtime-client-target' import { runtimeTargetEnvironmentId } from '@/runtime/rpc/targets/runtime-client-target' -import { getEnvironmentRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getEnvironmentRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { jiraGetProjectStatusOrder } from '@/runtime/jira/runtime-jira-client' import type { RowLessSource as RuntimeJiraSettings } from '@/lib/default-creation-host' import { createMetadataRequestStore, loadMetadata } from '@/hooks/metadata-request-cache' diff --git a/src/renderer/src/components/task-page/jira/use-task-page-jira-list-state.ts b/src/renderer/src/components/task-page/jira/use-task-page-jira-list-state.ts index e6ecbd83743..c41c110238e 100644 --- a/src/renderer/src/components/task-page/jira/use-task-page-jira-list-state.ts +++ b/src/renderer/src/components/task-page/jira/use-task-page-jira-list-state.ts @@ -33,7 +33,7 @@ import { ipcOrRuntimeTargetForHostId, runtimeTargetEnvironmentId } from '@/runtime/rpc/targets/runtime-client-target' -import { getEnvironmentRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getEnvironmentRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { getTaskSourceCacheScope } from '../../../../../shared/work-items/task-source-context' import { useTaskPageJiraProjectScope } from './use-task-page-jira-project-scope' const EMPTY_JIRA_PRIORITIES: JiraPrioritiesBySite = new Map() diff --git a/src/renderer/src/components/task-page/linear/task-page-linear-creation-lifetime.test.tsx b/src/renderer/src/components/task-page/linear/task-page-linear-creation-lifetime.test.tsx index cd72139504a..f2459b45568 100644 --- a/src/renderer/src/components/task-page/linear/task-page-linear-creation-lifetime.test.tsx +++ b/src/renderer/src/components/task-page/linear/task-page-linear-creation-lifetime.test.tsx @@ -20,7 +20,7 @@ import { writeLinearIssue } from '@/lib/work-items/linear/linear-issue-write' import { getProviderRuntimeContextKey, bumpProviderRuntimeSessionGeneration -} from '@/lib/provider-runtime-context' +} from '@/runtime/hosts/observations/provider-runtime-context' import type { LinearIssue } from '../../../../../shared/linear/issue-types' import { expect, it, vi } from 'vitest' import { act, renderHook, waitFor } from '@testing-library/react' diff --git a/src/renderer/src/components/task-page/use-task-page-store-bindings.ts b/src/renderer/src/components/task-page/use-task-page-store-bindings.ts index 57587b7a63f..cd858930e85 100644 --- a/src/renderer/src/components/task-page/use-task-page-store-bindings.ts +++ b/src/renderer/src/components/task-page/use-task-page-store-bindings.ts @@ -7,7 +7,7 @@ import { getLocalPreflightContext } from '@/lib/local-preflight-context' import { getProviderRuntimeContextKey, isEnvironmentRuntimeContextKey -} from '@/lib/provider-runtime-context' +} from '@/runtime/hosts/observations/provider-runtime-context' import { useRef } from 'react' import { getScreenSubmitShortcutLabel } from '@/lib/screen-submit-shortcut' import { selectFolderWorkspaces } from '@/store/workspaces/catalog/workspace-record-fields' diff --git a/src/renderer/src/components/terminal-pane/account-restart/detached/codex-detached-pane-restart.ts b/src/renderer/src/components/terminal-pane/account-restart/detached/codex-detached-pane-restart.ts index ae97b51c1a2..a404a310cee 100644 --- a/src/renderer/src/components/terminal-pane/account-restart/detached/codex-detached-pane-restart.ts +++ b/src/renderer/src/components/terminal-pane/account-restart/detached/codex-detached-pane-restart.ts @@ -13,8 +13,8 @@ import { hasRegisteredRuntimeTerminalTab } from '@/runtime/hosted-terminals/moun import { buildCodexAccountRestartStartup, buildPaneAccountRestartStartup -} from '@/lib/agent-account-restart-startup' -import { isClaudePaneMidTurn } from '@/lib/claude-session-account-move' +} from '@/lib/agent-session/accounts/agent-account-restart-startup' +import { isClaudePaneMidTurn } from '@/lib/agent-session/accounts/claude/claude-session-account-move' import type { AccountRestartAgent } from '@/store/terminals/terminal-contracts' import { isForeignMachineCodexPtyId } from '@/lib/codex-pane-selection-lane' import { getLocalProjectExecutionRuntimeContext } from '@/lib/local-preflight-context' diff --git a/src/renderer/src/components/terminal-pane/account-restart/pane-account-restart-readiness.ts b/src/renderer/src/components/terminal-pane/account-restart/pane-account-restart-readiness.ts index 195eaa6238b..dd6366f3875 100644 --- a/src/renderer/src/components/terminal-pane/account-restart/pane-account-restart-readiness.ts +++ b/src/renderer/src/components/terminal-pane/account-restart/pane-account-restart-readiness.ts @@ -1,5 +1,5 @@ import type { AppState } from '@/store/types' -import { isClaudePaneMidTurn } from '@/lib/claude-session-account-move' +import { isClaudePaneMidTurn } from '@/lib/agent-session/accounts/claude/claude-session-account-move' import { makePaneKey } from '../../../../../shared/workspace-layout/stable-pane-id' /** PTYs whose queued account restart can run now: Codex at once, this tab's Claude once its turn ends. */ diff --git a/src/renderer/src/components/terminal-pane/account-restart/use-pane-account-restart-startup.ts b/src/renderer/src/components/terminal-pane/account-restart/use-pane-account-restart-startup.ts index d94d40f173f..4a3cbe4bafb 100644 --- a/src/renderer/src/components/terminal-pane/account-restart/use-pane-account-restart-startup.ts +++ b/src/renderer/src/components/terminal-pane/account-restart/use-pane-account-restart-startup.ts @@ -1,6 +1,6 @@ import { useCallback } from 'react' import { useAppStore } from '../../../store' -import { buildPaneAccountRestartStartup } from '@/lib/agent-account-restart-startup' +import { buildPaneAccountRestartStartup } from '@/lib/agent-session/accounts/agent-account-restart-startup' import type { TerminalPaneCloseController } from '../layout/close/use-terminal-pane-close-actions' import type { AccountRestartAgent } from '@/store/terminals/terminal-contracts' diff --git a/src/renderer/src/hooks/ipc-events/claude-account-switch-ipc-bridge.ts b/src/renderer/src/hooks/ipc-events/claude-account-switch-ipc-bridge.ts index 1b2c738a8c4..6df4b1762d6 100644 --- a/src/renderer/src/hooks/ipc-events/claude-account-switch-ipc-bridge.ts +++ b/src/renderer/src/hooks/ipc-events/claude-account-switch-ipc-bridge.ts @@ -1,4 +1,4 @@ -import { handleRunningClaudeSessionsAfterSwitch } from '@/lib/claude-account-switch-sessions' +import { handleRunningClaudeSessionsAfterSwitch } from '@/lib/provider-accounts/claude-account-switch-sessions' export function registerClaudeAccountSwitchIpcBridge(unsubs: (() => void)[]): void { const subscribe = window.api.claudeAccounts?.onSelectedElsewhere diff --git a/src/renderer/src/hooks/use-integration-provider-status-refresh.ts b/src/renderer/src/hooks/use-integration-provider-status-refresh.ts index 745bd3a2007..1bb8e8a5655 100644 --- a/src/renderer/src/hooks/use-integration-provider-status-refresh.ts +++ b/src/renderer/src/hooks/use-integration-provider-status-refresh.ts @@ -1,7 +1,7 @@ import { useEffect } from 'react' import { getLocalPreflightContext } from '@/lib/local-preflight-context' import { localPreflightContextKey } from '@/lib/local-preflight-context-key' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { useAppStore } from '@/store/index' export function useIntegrationProviderStatusRefresh(): void { diff --git a/src/renderer/src/hooks/useLinearProviderConnected.ts b/src/renderer/src/hooks/useLinearProviderConnected.ts index a3402ac5ce6..bed73a901ec 100644 --- a/src/renderer/src/hooks/useLinearProviderConnected.ts +++ b/src/renderer/src/hooks/useLinearProviderConnected.ts @@ -1,4 +1,4 @@ -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { useAppStore } from '@/store' // Share visibility policy between Settings nav/page and reject another runtime’s cached status. diff --git a/src/renderer/src/lib/agent-account-restart-startup.test.ts b/src/renderer/src/lib/agent-session/accounts/agent-account-restart-startup.test.ts similarity index 94% rename from src/renderer/src/lib/agent-account-restart-startup.test.ts rename to src/renderer/src/lib/agent-session/accounts/agent-account-restart-startup.test.ts index 02ebc090a81..5338f2f8dc3 100644 --- a/src/renderer/src/lib/agent-account-restart-startup.test.ts +++ b/src/renderer/src/lib/agent-session/accounts/agent-account-restart-startup.test.ts @@ -1,9 +1,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' -import type { AgentStatusEntry } from '../../../shared/agent-status/contracts/agent-status-types' -import { useAppStore } from '@/store' -import { makePaneKey } from '../../../shared/workspace-layout/stable-pane-id' -import { getDefaultSettings } from '../../../shared/constants' -import type { ProjectExecutionRuntimeResolution } from '../../../shared/execution-host/project-execution-runtime' +import type { AgentStatusEntry } from '../../../../../shared/agent-status/contracts/agent-status-types' +import { useAppStore } from '@/store/index' +import { makePaneKey } from '../../../../../shared/workspace-layout/stable-pane-id' +import { getDefaultSettings } from '../../../../../shared/constants' +import type { ProjectExecutionRuntimeResolution } from '../../../../../shared/execution-host/project-execution-runtime' import type * as localPreflightContext from '@/lib/local-preflight-context' import { resolveAgentResumeLaunchTarget } from '@/lib/agent-resume-launch-target' import { makeTerminalTab } from '@/store/terminals/terminal-row.test-fixture' diff --git a/src/renderer/src/lib/agent-account-restart-startup.ts b/src/renderer/src/lib/agent-session/accounts/agent-account-restart-startup.ts similarity index 90% rename from src/renderer/src/lib/agent-account-restart-startup.ts rename to src/renderer/src/lib/agent-session/accounts/agent-account-restart-startup.ts index c86e3c23dd7..6de22dd2f3c 100644 --- a/src/renderer/src/lib/agent-account-restart-startup.ts +++ b/src/renderer/src/lib/agent-session/accounts/agent-account-restart-startup.ts @@ -1,7 +1,7 @@ import { getConnectionIdFromState } from '@/lib/connection-owner-resolution' -import { useAppStore } from '@/store' +import { useAppStore } from '@/store/index' import type { AppState } from '@/store/types' -import { buildAgentResumeStartupPlan } from '../../../shared/agent-launch/startup/tui-agent-resume-startup' +import { buildAgentResumeStartupPlan } from '../../../../../shared/agent-launch/startup/tui-agent-resume-startup' import { resolveRuntimeHostReportedPlatform, resolveRuntimeHostTerminalWindowsShell, @@ -13,21 +13,21 @@ import { getExecutionHostIdForWorktree } from '@/lib/workspace/ownership/worktre import { runtimeEnvironmentIdUnlessTwin } from '@/lib/resolve-owner' import { getLocalProjectExecutionRuntimeContext } from '@/lib/local-preflight-context' import { repoMatchesHostIdentity } from '@/store/repos/repo-host-identity' -import { isSessionHeldByAnotherPane } from '@/lib/claude-session-pane-holders' -import { makePaneKey } from '../../../shared/workspace-layout/stable-pane-id' +import { isSessionHeldByAnotherPane } from '@/lib/agent-session/identity/claude-session-pane-holders' +import { makePaneKey } from '../../../../../shared/workspace-layout/stable-pane-id' import { normalizeAgentProviderSession, type AgentProviderSessionMetadata, type SleepingAgentLaunchConfig -} from '../../../shared/agent-session/lifecycle/agent-session-resume' +} from '../../../../../shared/agent-session/lifecycle/agent-session-resume' import { resolveTuiAgentLaunchRawArgs, resolveTuiAgentLaunchEnv -} from '../../../shared/agent-launch/choices/tui-agent-launch-defaults' +} from '../../../../../shared/agent-launch/choices/tui-agent-launch-defaults' import type { AccountRestartAgent } from '@/store/terminals/terminal-contracts' -import type { ProjectExecutionRuntimeResolution } from '../../../shared/execution-host/project-execution-runtime' -import { resolveAgentLaunchCommand } from '../../../shared/agent-launch/startup/tui-agent-launch-command' -import { resolveStartupShell } from '../../../shared/agent-launch/startup/tui-agent-startup-shell' +import type { ProjectExecutionRuntimeResolution } from '../../../../../shared/execution-host/project-execution-runtime' +import { resolveAgentLaunchCommand } from '../../../../../shared/agent-launch/startup/tui-agent-launch-command' +import { resolveStartupShell } from '../../../../../shared/agent-launch/startup/tui-agent-startup-shell' export type AgentAccountRestartStartup = { command: string diff --git a/src/renderer/src/lib/claude-pane-account-folder.ts b/src/renderer/src/lib/agent-session/accounts/claude/claude-pane-account-folder.ts similarity index 96% rename from src/renderer/src/lib/claude-pane-account-folder.ts rename to src/renderer/src/lib/agent-session/accounts/claude/claude-pane-account-folder.ts index 26655800ee5..33f19255b1d 100644 --- a/src/renderer/src/lib/claude-pane-account-folder.ts +++ b/src/renderer/src/lib/agent-session/accounts/claude/claude-pane-account-folder.ts @@ -1,8 +1,8 @@ import type { ClaudeManagedAccount, ClaudeRateLimitAccountsState -} from '../../../shared/managed-account-types' -import { getRendererAppPlatform } from './renderer-app-platform' +} from '../../../../../../shared/managed-account-types' +import { getRendererAppPlatform } from '../../../renderer-app-platform' function normalizeFolder(path: string): string { const slashed = path.replace(/\\/g, '/').replace(/\/+$/, '') diff --git a/src/renderer/src/lib/claude-session-account-move.test.ts b/src/renderer/src/lib/agent-session/accounts/claude/claude-session-account-move.test.ts similarity index 92% rename from src/renderer/src/lib/claude-session-account-move.test.ts rename to src/renderer/src/lib/agent-session/accounts/claude/claude-session-account-move.test.ts index 52a7d6ef562..1a911355eae 100644 --- a/src/renderer/src/lib/claude-session-account-move.test.ts +++ b/src/renderer/src/lib/agent-session/accounts/claude/claude-session-account-move.test.ts @@ -1,13 +1,13 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' -import { useAppStore } from '@/store' -import type { AgentStatusEntry } from '../../../shared/agent-status/contracts/agent-status-types' -import { makePaneKey } from '../../../shared/workspace-layout/stable-pane-id' -import type { TerminalTab } from '../../../shared/workspace-layout/terminal-tab-types' -import type { ClaudeManagedAccount } from '../../../shared/managed-account-types' -import type { ProjectExecutionRuntimeResolution } from '../../../shared/execution-host/project-execution-runtime' -import { getDefaultSettings } from '../../../shared/constants' +import { useAppStore } from '@/store/index' +import type { AgentStatusEntry } from '../../../../../../shared/agent-status/contracts/agent-status-types' +import { makePaneKey } from '../../../../../../shared/workspace-layout/stable-pane-id' +import type { TerminalTab } from '../../../../../../shared/workspace-layout/terminal-tab-types' +import type { ClaudeManagedAccount } from '../../../../../../shared/managed-account-types' +import type { ProjectExecutionRuntimeResolution } from '../../../../../../shared/execution-host/project-execution-runtime' +import { getDefaultSettings } from '../../../../../../shared/constants' import type * as localPreflightContext from '@/lib/local-preflight-context' -import type * as paneSelectionLane from './codex-pane-selection-lane' +import type * as paneSelectionLane from '../../../codex-pane-selection-lane' import { confirmRuntimeTerminalForegroundProcess, inspectRuntimeTerminalProcess @@ -25,12 +25,12 @@ vi.mock('@/runtime/hosted-terminals/inspection/runtime-terminal-process-inspecti inspectRuntimeTerminalProcess: vi.fn(), confirmRuntimeTerminalForegroundProcess: vi.fn() })) -vi.mock('./renderer-app-platform', () => ({ getRendererAppPlatform: () => 'win32' })) +vi.mock('../../../renderer-app-platform', () => ({ getRendererAppPlatform: () => 'win32' })) vi.mock('@/lib/local-preflight-context', async (importOriginal) => ({ ...(await importOriginal()), getLocalProjectExecutionRuntimeContext: () => projectRuntime })) -vi.mock('./codex-pane-selection-lane', async (importOriginal) => ({ +vi.mock('../../../codex-pane-selection-lane', async (importOriginal) => ({ ...(await importOriginal()), resolveCodexPaneSelectionLane: ({ ptyId }: { ptyId: string }) => ({ laneKey: laneByPtyId[ptyId] ?? 'host', diff --git a/src/renderer/src/lib/claude-session-account-move.ts b/src/renderer/src/lib/agent-session/accounts/claude/claude-session-account-move.ts similarity index 92% rename from src/renderer/src/lib/claude-session-account-move.ts rename to src/renderer/src/lib/agent-session/accounts/claude/claude-session-account-move.ts index 7139d714fb8..da1a04cecd7 100644 --- a/src/renderer/src/lib/claude-session-account-move.ts +++ b/src/renderer/src/lib/agent-session/accounts/claude/claude-session-account-move.ts @@ -1,7 +1,7 @@ /** Find sessions affected by account selection; resume preserves the session while launch reads the newly selected account folder. */ /** Find sessions affected by account selection; resume preserves the session while launch reads the newly selected account folder. */ -import { useAppStore } from '@/store' +import { useAppStore } from '@/store/index' import type { AppState } from '@/store/types' import { confirmRuntimeTerminalForegroundProcess, @@ -11,27 +11,30 @@ import { import { isAgentForegroundWrapperProcess, recognizeAgentProcess -} from '../../../shared/agent-status/process/agent-process-recognition' -import { normalizeAgentProviderSession } from '../../../shared/agent-session/lifecycle/agent-session-resume' -import { isTerminalLeafId, makePaneKey } from '../../../shared/workspace-layout/stable-pane-id' +} from '../../../../../../shared/agent-status/process/agent-process-recognition' +import { normalizeAgentProviderSession } from '../../../../../../shared/agent-session/lifecycle/agent-session-resume' +import { + isTerminalLeafId, + makePaneKey +} from '../../../../../../shared/workspace-layout/stable-pane-id' import { type TerminalProcessInspection as RuntimeTerminalProcessInspection, isClientOnlyUnverifiableInspection -} from '../../../shared/terminal/pty/lifecycle/terminal-process-inspection' +} from '../../../../../../shared/terminal/pty/lifecycle/terminal-process-inspection' import { getCodexSelectionLaneKey, normalizeCodexAccountSelectionTarget, type CodexAccountSelectionTarget -} from '../../../shared/codex-selection-lane' -import type { RuntimeClientTarget } from '../../../shared/runtime-client-target' +} from '../../../../../../shared/codex-selection-lane' +import type { RuntimeClientTarget } from '../../../../../../shared/runtime-client-target' import type { ClaudeManagedAccount, ClaudeRateLimitAccountsState -} from '../../../shared/managed-account-types' -import type { TerminalTab } from '../../../shared/workspace-layout/terminal-tab-types' +} from '../../../../../../shared/managed-account-types' +import type { TerminalTab } from '../../../../../../shared/workspace-layout/terminal-tab-types' import { resolvePaneAgentSessionId } from '@/lib/agent-session/identity/pane-agent-session-id' -import { resolveAgentAccountRestartResumeStartup } from './agent-account-restart-startup' -import { isSessionHeldByAnotherPane } from './claude-session-pane-holders' +import { resolveAgentAccountRestartResumeStartup } from '../agent-account-restart-startup' +import { isSessionHeldByAnotherPane } from '../../identity/claude-session-pane-holders' import { isClaudePaneOnAccount, resolveClaudePaneFolder, @@ -42,7 +45,7 @@ import { isForeignMachineCodexPtyId, isLocalCodexSelectionLaneKey, resolveCodexPaneSelectionLane -} from './codex-pane-selection-lane' +} from '../../../codex-pane-selection-lane' export type ClaudeSessionMoveCandidate = { ptyId: string diff --git a/src/renderer/src/lib/agent-session/accounts/codex/codex-session-restart.ts b/src/renderer/src/lib/agent-session/accounts/codex/codex-session-restart.ts index 9c3332961d6..a06fdfbb48b 100644 --- a/src/renderer/src/lib/agent-session/accounts/codex/codex-session-restart.ts +++ b/src/renderer/src/lib/agent-session/accounts/codex/codex-session-restart.ts @@ -9,7 +9,7 @@ import { translate } from '@/i18n/i18n' import { getCodexAccountDisplayLabel, normalizeCodexAccountEmail -} from '../../../codex-account-display-label' +} from '../../../provider-accounts/codex-account-display-label' import { isShellProcess } from '../../../../../../shared/agent-status/process/shell-process-detection' import { isCodexForegroundProcess, diff --git a/src/renderer/src/lib/claude-session-pane-holders.ts b/src/renderer/src/lib/agent-session/identity/claude-session-pane-holders.ts similarity index 89% rename from src/renderer/src/lib/claude-session-pane-holders.ts rename to src/renderer/src/lib/agent-session/identity/claude-session-pane-holders.ts index 5f645abe710..3151f9bca5e 100644 --- a/src/renderer/src/lib/claude-session-pane-holders.ts +++ b/src/renderer/src/lib/agent-session/identity/claude-session-pane-holders.ts @@ -1,6 +1,9 @@ import { resolvePaneAgentSessionId } from '@/lib/agent-session/identity/pane-agent-session-id' import type { AppState } from '@/store/types' -import { isTerminalLeafId, makePaneKey } from '../../../shared/workspace-layout/stable-pane-id' +import { + isTerminalLeafId, + makePaneKey +} from '../../../../../shared/workspace-layout/stable-pane-id' /** Claude reopens one branch of a shared transcript on resume, which may be the other pane's. */ export function isSessionHeldByAnotherPane( diff --git a/src/renderer/src/lib/codex-pane-selection-lane.ts b/src/renderer/src/lib/codex-pane-selection-lane.ts index bc8442f0f17..83ef2aac009 100644 --- a/src/renderer/src/lib/codex-pane-selection-lane.ts +++ b/src/renderer/src/lib/codex-pane-selection-lane.ts @@ -19,7 +19,7 @@ import { parseWorkspaceKey } from '../../../shared/workspace-scope' import { parseWslUncPath } from '../../../shared/wsl-paths' import { getLocalProjectExecutionRuntimeContext } from './local-preflight-context' import { getRendererAppPlatform } from './renderer-app-platform' -import { wslHostDistroForTarget } from './wsl-host-account-owner' +import { wslHostDistroForTarget } from '../runtime/provider-accounts/ownership/wsl-host-account-owner' import { getCachedWindowsTerminalCapabilities, hasCachedWindowsTerminalCapabilities diff --git a/src/renderer/src/lib/local-preflight-context.ts b/src/renderer/src/lib/local-preflight-context.ts index 43768675d02..e97a59d0c04 100644 --- a/src/renderer/src/lib/local-preflight-context.ts +++ b/src/renderer/src/lib/local-preflight-context.ts @@ -16,7 +16,7 @@ import { getIndexedRepoMap, getIndexedReposById } from '@/store/repos/repository import { defaultScopeHost } from './default-creation-host' import { getLocalProjectRuntimeWorkspace } from './local-project-runtime-workspace' import { getActiveWorkspaceOwnerTarget } from './resolved-worktree-execution-host' -import { getEnvironmentRuntimeContextKey } from './provider-runtime-context' +import { getEnvironmentRuntimeContextKey } from '../runtime/hosts/observations/provider-runtime-context' import { getRendererAppPlatform } from './renderer-app-platform' import { getProjectRuntimePreflightContext, diff --git a/src/renderer/src/lib/claude-account-label.ts b/src/renderer/src/lib/provider-accounts/claude-account-label.ts similarity index 85% rename from src/renderer/src/lib/claude-account-label.ts rename to src/renderer/src/lib/provider-accounts/claude-account-label.ts index 70088b9278a..6f509391fd6 100644 --- a/src/renderer/src/lib/claude-account-label.ts +++ b/src/renderer/src/lib/provider-accounts/claude-account-label.ts @@ -1,5 +1,5 @@ import { translate } from '@/i18n/i18n' -import type { ClaudeRateLimitAccountsState } from '../../../shared/managed-account-types' +import type { ClaudeRateLimitAccountsState } from '../../../../shared/managed-account-types' /** Display label for a selected Claude account row; null is System default. */ export function getClaudeAccountLabel( diff --git a/src/renderer/src/lib/claude-account-sign-in.ts b/src/renderer/src/lib/provider-accounts/claude-account-sign-in.ts similarity index 90% rename from src/renderer/src/lib/claude-account-sign-in.ts rename to src/renderer/src/lib/provider-accounts/claude-account-sign-in.ts index 85148154eb7..d7f3a490e70 100644 --- a/src/renderer/src/lib/claude-account-sign-in.ts +++ b/src/renderer/src/lib/provider-accounts/claude-account-sign-in.ts @@ -1,10 +1,10 @@ import { toast } from 'sonner' import { translate } from '@/i18n/i18n' -import { useAppStore } from '@/store' +import { useAppStore } from '@/store/index' import { getClaudeAccountErrorDescription, isClaudeAccountCancellation -} from './provider-accounts/provider-account-action-errors' +} from './provider-account-action-errors' /** Sign in again via hidden claude auth login in the account folder; toast failures and keep cancellation silent. */ export async function signInToClaudeAccount(accountId: string): Promise { diff --git a/src/renderer/src/lib/claude-account-switch-sessions.test.ts b/src/renderer/src/lib/provider-accounts/claude-account-switch-sessions.test.ts similarity index 87% rename from src/renderer/src/lib/claude-account-switch-sessions.test.ts rename to src/renderer/src/lib/provider-accounts/claude-account-switch-sessions.test.ts index ea77dd9004e..dd0eb354623 100644 --- a/src/renderer/src/lib/claude-account-switch-sessions.test.ts +++ b/src/renderer/src/lib/provider-accounts/claude-account-switch-sessions.test.ts @@ -1,9 +1,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' -import type { ClaudeAccountSwitchRunningSessions } from '../../../shared/claude-account-switch-settings' -import type { ClaudeManagedAccountSummary } from '../../../shared/managed-account-types' -import { findClaudeSwitchSessions } from './claude-session-account-move' -import type * as AccountMove from './claude-session-account-move' -import type { ClaudeSwitchSessions } from './claude-session-account-move' +import type { ClaudeAccountSwitchRunningSessions } from '../../../../shared/claude-account-switch-settings' +import type { ClaudeManagedAccountSummary } from '../../../../shared/managed-account-types' +import { findClaudeSwitchSessions } from '../agent-session/accounts/claude/claude-session-account-move' +import type * as AccountMove from '../agent-session/accounts/claude/claude-session-account-move' +import type { ClaudeSwitchSessions } from '../agent-session/accounts/claude/claude-session-account-move' import { answerClaudeAccountSwitchPrompt, getClaudeAccountSwitchPrompt, @@ -31,9 +31,9 @@ const store = vi.hoisted((): StoreStub => ({ const toastSuccess = vi.hoisted(() => vi.fn()) const toastWarning = vi.hoisted(() => vi.fn()) -vi.mock('@/store', () => ({ useAppStore: { getState: () => store } })) +vi.mock('@/store/index', () => ({ useAppStore: { getState: () => store } })) vi.mock('sonner', () => ({ toast: { success: toastSuccess, warning: toastWarning } })) -vi.mock('./claude-session-account-move', async (importOriginal) => ({ +vi.mock('../agent-session/accounts/claude/claude-session-account-move', async (importOriginal) => ({ ...(await importOriginal()), findClaudeSwitchSessions: vi.fn(), findQueuedClaudeRestartsOnAccount: vi.fn(() => []) diff --git a/src/renderer/src/lib/claude-account-switch-sessions.ts b/src/renderer/src/lib/provider-accounts/claude-account-switch-sessions.ts similarity index 95% rename from src/renderer/src/lib/claude-account-switch-sessions.ts rename to src/renderer/src/lib/provider-accounts/claude-account-switch-sessions.ts index f8cd084ad63..e45a3cf34f6 100644 --- a/src/renderer/src/lib/claude-account-switch-sessions.ts +++ b/src/renderer/src/lib/provider-accounts/claude-account-switch-sessions.ts @@ -1,19 +1,19 @@ import { toast } from 'sonner' import { translate } from '@/i18n/i18n' -import { useAppStore } from '@/store' -import type { CodexAccountSelectionTarget } from '../../../shared/codex-selection-lane' -import type { ClaudeAccountSwitchRunningSessions } from '../../../shared/claude-account-switch-settings' -import type { RuntimeClientTarget } from '../../../shared/runtime-client-target' +import { useAppStore } from '@/store/index' +import type { CodexAccountSelectionTarget } from '../../../../shared/codex-selection-lane' +import type { ClaudeAccountSwitchRunningSessions } from '../../../../shared/claude-account-switch-settings' +import type { RuntimeClientTarget } from '../../../../shared/runtime-client-target' import { INLINE_ACTION_CLASS_NAMES } from '@/components/ui/sonner' import { getClaudeAccountLabel } from './claude-account-label' -import type { ClaudeHostFolders } from './claude-pane-account-folder' +import type { ClaudeHostFolders } from '../agent-session/accounts/claude/claude-pane-account-folder' import { claudeLaunchAccountId, findClaudeSwitchSessions, findQueuedClaudeRestartsOnAccount, type ClaudeSessionMoveCandidate, type ClaudeSwitchSessions -} from './claude-session-account-move' +} from '../agent-session/accounts/claude/claude-session-account-move' export type ClaudeAccountSwitchPromptSessions = { onAccount: ClaudeSessionMoveCandidate[] diff --git a/src/renderer/src/lib/codex-account-display-label.ts b/src/renderer/src/lib/provider-accounts/codex-account-display-label.ts similarity index 100% rename from src/renderer/src/lib/codex-account-display-label.ts rename to src/renderer/src/lib/provider-accounts/codex-account-display-label.ts diff --git a/src/renderer/src/lib/provider-runtime-context.ts b/src/renderer/src/runtime/hosts/observations/provider-runtime-context.ts similarity index 93% rename from src/renderer/src/lib/provider-runtime-context.ts rename to src/renderer/src/runtime/hosts/observations/provider-runtime-context.ts index c9a6c2e3563..ac75e604382 100644 --- a/src/renderer/src/lib/provider-runtime-context.ts +++ b/src/renderer/src/runtime/hosts/observations/provider-runtime-context.ts @@ -1,4 +1,4 @@ -import type { GlobalSettings } from '../../../shared/global-settings-types' +import type { GlobalSettings } from '../../../../../shared/global-settings-types' export function getProviderRuntimeContextKey( settings: Pick | null | undefined diff --git a/src/renderer/src/runtime/provider-accounts/ownership/use-active-account-owner.ts b/src/renderer/src/runtime/provider-accounts/ownership/use-active-account-owner.ts index 5707d91b73b..cf7dfce37b9 100644 --- a/src/renderer/src/runtime/provider-accounts/ownership/use-active-account-owner.ts +++ b/src/renderer/src/runtime/provider-accounts/ownership/use-active-account-owner.ts @@ -8,7 +8,7 @@ import { wslHostDistroForTarget, accountOwnerFor, type AccountOwner -} from '../../../lib/wsl-host-account-owner' +} from './wsl-host-account-owner' import type { RuntimeClientTarget } from '../../../../../shared/runtime-client-target' import { useActiveWorkspaceOwnerTarget } from '../../rpc/targets/use-worktree-runtime-target' diff --git a/src/renderer/src/lib/wsl-host-account-owner.ts b/src/renderer/src/runtime/provider-accounts/ownership/wsl-host-account-owner.ts similarity index 87% rename from src/renderer/src/lib/wsl-host-account-owner.ts rename to src/renderer/src/runtime/provider-accounts/ownership/wsl-host-account-owner.ts index f326a05455b..6c7cf7e31d8 100644 --- a/src/renderer/src/lib/wsl-host-account-owner.ts +++ b/src/renderer/src/runtime/provider-accounts/ownership/wsl-host-account-owner.ts @@ -1,5 +1,5 @@ -import type { PublicKnownRuntimeEnvironment } from '../../../shared/remote-runtime/host-catalog/runtime-environments' -import type { RuntimeClientTarget } from '../../../shared/runtime-client-target' +import type { PublicKnownRuntimeEnvironment } from '../../../../../shared/remote-runtime/host-catalog/runtime-environments' +import type { RuntimeClientTarget } from '../../../../../shared/runtime-client-target' type AccountOwnerEnvironments = readonly Pick< PublicKnownRuntimeEnvironment, diff --git a/src/renderer/src/runtime/rpc/targets/use-worktree-runtime-target.ts b/src/renderer/src/runtime/rpc/targets/use-worktree-runtime-target.ts index f4b67b75571..6e1871ab23f 100644 --- a/src/renderer/src/runtime/rpc/targets/use-worktree-runtime-target.ts +++ b/src/renderer/src/runtime/rpc/targets/use-worktree-runtime-target.ts @@ -8,7 +8,7 @@ import { runtimeTargetForOwnerEnvironment, runtimeTargetEnvironmentId } from './runtime-client-target' -import '../../../lib/wsl-host-account-owner' +import '../../provider-accounts/ownership/wsl-host-account-owner' import type { RuntimeClientTarget } from '../../../../../shared/runtime-client-target' /** Target the workspace owner rather than the focused host or port scans hit the wrong machine. Direct-SSH owners return null. */ diff --git a/src/renderer/src/store/slices/jira/jira-connection-actions.ts b/src/renderer/src/store/slices/jira/jira-connection-actions.ts index 87ce5b33aea..9510c9c2b1b 100644 --- a/src/renderer/src/store/slices/jira/jira-connection-actions.ts +++ b/src/renderer/src/store/slices/jira/jira-connection-actions.ts @@ -7,7 +7,7 @@ import { jiraTestConnection } from '@/runtime/jira/runtime-jira-client' import { jiraReadStatus } from '@/runtime/jira/runtime-jira-summary-client' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { translate } from '@/i18n/i18n' import type { JiraSlice, JiraSliceGet, JiraSliceSet } from './jira-slice-contract' import { diff --git a/src/renderer/src/store/slices/jira/jira-read-coordination.ts b/src/renderer/src/store/slices/jira/jira-read-coordination.ts index 6978aaede8b..13934c02dbd 100644 --- a/src/renderer/src/store/slices/jira/jira-read-coordination.ts +++ b/src/renderer/src/store/slices/jira/jira-read-coordination.ts @@ -12,7 +12,7 @@ import { getTaskSourceRuntimeSettings, type TaskSourceContext } from '../../../../../shared/work-items/task-source-context' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { parseJiraStatusError } from '../../../../../shared/jira/jira-status-error' import { defaultScopeSource } from '@/lib/default-creation-host' import type { RowLessSource } from '@/lib/default-creation-host' diff --git a/src/renderer/src/store/slices/linear/linear-slice-scope.ts b/src/renderer/src/store/slices/linear/linear-slice-scope.ts index 68492681596..8113daf7250 100644 --- a/src/renderer/src/store/slices/linear/linear-slice-scope.ts +++ b/src/renderer/src/store/slices/linear/linear-slice-scope.ts @@ -9,7 +9,7 @@ import { import { getProviderRuntimeContextKey, getEnvironmentRuntimeContextKey -} from '@/lib/provider-runtime-context' +} from '@/runtime/hosts/observations/provider-runtime-context' import { getLinearCacheGeneration, getLinearMutationGeneration } from './linear-slice-request-state' import { defaultScopeSource, rowLessSourceTarget } from '@/lib/default-creation-host' import type { RowLessSource } from '@/lib/default-creation-host' diff --git a/src/renderer/src/store/slices/runtime-status/create-runtime-status-slice.ts b/src/renderer/src/store/slices/runtime-status/create-runtime-status-slice.ts index 5d9240da358..a81a65badb3 100644 --- a/src/renderer/src/store/slices/runtime-status/create-runtime-status-slice.ts +++ b/src/renderer/src/store/slices/runtime-status/create-runtime-status-slice.ts @@ -11,7 +11,7 @@ import { clearRuntimeCompatibilityCache } from '@/runtime/rpc/compatibility/runtime-compatibility-cache' import { replaceRuntimeEnvironmentRevisions } from '@/runtime/rpc/environment-revisions/runtime-environment-revision' -import { bumpProviderRuntimeSessionGeneration } from '@/lib/provider-runtime-context' +import { bumpProviderRuntimeSessionGeneration } from '@/runtime/hosts/observations/provider-runtime-context' import { evictInstalledAgentSkillDiscoveryForRuntimeEnvironments } from '@/hooks/installed-agent-skill-discovery' import { dismissRuntimeDisconnectedToast, diff --git a/src/renderer/src/store/slices/runtime-status/runtime-status.test.ts b/src/renderer/src/store/slices/runtime-status/runtime-status.test.ts index 78527c1b78d..90d8026b66c 100644 --- a/src/renderer/src/store/slices/runtime-status/runtime-status.test.ts +++ b/src/renderer/src/store/slices/runtime-status/runtime-status.test.ts @@ -9,7 +9,7 @@ import { clearRuntimeCompatibilityCache as clearRuntimeCompatibilityCacheForTest import { clearRuntimeEnvironmentConnectionGenerations as clearRuntimeEnvironmentConnectionGenerationsForTests } from './runtime-status-connection-generation' import { createRuntimeStatusSlice } from './create-runtime-status-slice' import type { RuntimeStatusSlice } from './runtime-status-types' -import '@/lib/provider-runtime-context' +import '@/runtime/hosts/observations/provider-runtime-context' import '../../../../../shared/workspace-creator-visibility' import { stubRuntimeEnvironmentBridge } from '../../../runtime/rpc/testing/runtime-environment-bridge-fixture' diff --git a/src/renderer/src/store/slices/settings/create-settings-slice.ts b/src/renderer/src/store/slices/settings/create-settings-slice.ts index 3acb8aa9f3c..92e74dcdaa3 100644 --- a/src/renderer/src/store/slices/settings/create-settings-slice.ts +++ b/src/renderer/src/store/slices/settings/create-settings-slice.ts @@ -19,7 +19,7 @@ import { createSettingsSearchState, type SettingsSearchState } from './settings- import { isRuntimeCatalogListingStale } from '../runtime-status/runtime-status-hydration' import { normalizeDisabledTuiAgents } from '../../../../../shared/agent-launch/catalog/tui-agent-selection' import { sanitizeAgentLaunchProfileUpdates } from '../../../../../shared/agent-launch/choices/tui-agent-launch-defaults' -import { bumpProviderRuntimeSessionGeneration } from '@/lib/provider-runtime-context' +import { bumpProviderRuntimeSessionGeneration } from '@/runtime/hosts/observations/provider-runtime-context' import { normalizeUiLanguage } from '../../../../../shared/ui-language' import { normalizeDesktopTerminalScrollbackRows } from '../../../../../shared/terminal/replay/terminal-scrollback-policy' import { translate } from '@/i18n/i18n' diff --git a/src/renderer/src/store/slices/worktrees/session/worktree-terminal-reference.ts b/src/renderer/src/store/slices/worktrees/session/worktree-terminal-reference.ts index 9c9b76ce089..9aa99311aa8 100644 --- a/src/renderer/src/store/slices/worktrees/session/worktree-terminal-reference.ts +++ b/src/renderer/src/store/slices/worktrees/session/worktree-terminal-reference.ts @@ -3,7 +3,7 @@ import { getRepoExecutionHostId, parseExecutionHostId } from '../../../../../../shared/execution-host' -import { getProviderRuntimeContextKey } from '@/lib/provider-runtime-context' +import { getProviderRuntimeContextKey } from '@/runtime/hosts/observations/provider-runtime-context' import { getRuntimeEnvironmentRevision } from '@/runtime/rpc/environment-revisions/runtime-environment-revision' import type { WorktreeSlice } from '../worktree-slice-contract' import type { WorktreeSliceGet } from '../listing/worktree-slice-types' diff --git a/src/renderer/src/store/slices/worktrees/session/worktrees-terminal-reference-source-race.test.ts b/src/renderer/src/store/slices/worktrees/session/worktrees-terminal-reference-source-race.test.ts index 7535980d18b..72c725e5364 100644 --- a/src/renderer/src/store/slices/worktrees/session/worktrees-terminal-reference-source-race.test.ts +++ b/src/renderer/src/store/slices/worktrees/session/worktrees-terminal-reference-source-race.test.ts @@ -2,7 +2,7 @@ vi.mock('@/store/index', () => ({ useAppStore: { getState: () => ({}) } })) import { beforeEach, expect, it, vi } from 'vitest' import type { HostedReviewInfo } from '../../../../../../shared/source-control/hosted-review/hosted-review-contracts' import type { Repo } from '../../../../../../shared/repo-types' -import { bumpProviderRuntimeSessionGeneration } from '@/lib/provider-runtime-context' +import { bumpProviderRuntimeSessionGeneration } from '@/runtime/hosts/observations/provider-runtime-context' import { makeWorktree } from '../worktree.test-fixture' import { createTestStore, diff --git a/src/renderer/src/components/settings/runtime-environments/catalog/runtime-server-default-name.ts b/src/shared/remote-runtime/host-catalog/runtime-host-default-name.ts similarity index 79% rename from src/renderer/src/components/settings/runtime-environments/catalog/runtime-server-default-name.ts rename to src/shared/remote-runtime/host-catalog/runtime-host-default-name.ts index 7df3c2c7f21..234624de20d 100644 --- a/src/renderer/src/components/settings/runtime-environments/catalog/runtime-server-default-name.ts +++ b/src/shared/remote-runtime/host-catalog/runtime-host-default-name.ts @@ -1,5 +1,5 @@ -import { parseHostAccessLink } from '../../../../../../shared/remote-runtime/pairing/remote-pairing-address' -import { findCollidingEnvironmentName } from '../../../../../../shared/remote-runtime/host-catalog/runtime-environment-name-collision' +import { parseHostAccessLink } from '../pairing/remote-pairing-address' +import { findCollidingEnvironmentName } from './runtime-environment-name-collision' /** Choose host, host:port, then numbered suffixes so servers sharing a machine/address still get unique blank-name defaults. */ export function defaultServerNameForAccessLink( From d714d577bf1f6ad551f58eb93afce9e9f729b2c6 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Sun, 8 Nov 2026 19:50:37 -0800 Subject: [PATCH 42/80] test: remove 45 low-value suites and redundant cases (#40072) * test: remove low-value synthetic and duplicated cases * test: remove low-value IPC and renderer cases * test: remove marginal mobile web build helper cases * test: remove marginal browser coverage * test: remove low-value mobile coverage * test: remove fixture inventories and diagnostic-only cases * test: remove low-value shared test suites and matrices * test: remove internal-work and mock-only coverage * test: remove prose passthrough and scan-count assertions * test: remove low-value renderer suites and copy counters --------- Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .../build-mobile-web-app-bundle.test.mjs | 36 +- .../mobile-web-bundle-manifest.test.mjs | 50 --- .../mobile-web-page-routes.test.mjs | 99 ------ .../mobile-web-source-line-endings.test.mjs | 19 -- .../validate-plugin-files.test.mjs | 27 +- .../script-module-dependencies.test.mjs | 66 ---- .../vitest-node-runtime-files.mjs | 1 - .../source-routing/pr-package-test-paths.mjs | 1 - .../pr-unit-support-routing.test.mjs | 98 ------ .../src/app-update/app-update-checker.test.ts | 18 - .../browser-screencast-request.web.test.ts | 116 ------- mobile/src/browser/browser-url.test.ts | 12 - .../mobile-browser-view-mode-state.test.ts | 55 --- .../src/components/DragReorderList.test.tsx | 64 ---- .../components/HostRouteNoticeBanner.test.tsx | 65 ---- mobile/src/components/MobileMarkdown.test.ts | 16 - mobile/src/components/MobileRepoIcon.test.tsx | 45 --- .../MobileRichMarkdownToolbar.test.tsx | 68 ---- .../components/ProtocolBlockScreen.test.ts | 67 +--- src/cli/format-recovery.test.ts | 60 +--- .../acp/history/acp-history-scaling.test.ts | 78 ----- .../agent-launch-mode-settings.test.ts | 2 +- .../trust/agent-workspace-trust.test.ts | 88 ----- ...session-search-agent-compatibility.test.ts | 4 +- .../session-search-deleted-sources.test.ts | 13 - .../session-search-file-write.test.ts | 15 - .../session-search-index-consumer.test.ts | 21 -- .../session-search-index-writer.test.ts | 32 +- .../session-search-lifecycle-matrix.test.ts | 318 ------------------ .../session-search-message-rows.test.ts | 21 -- ...ession-search-native-chat-indexing.test.ts | 106 ------ .../session-search-posting-window.test.ts | 19 -- .../session-search-service-registry.test.ts | 66 +--- .../session-search-service.test.ts | 16 - .../session-search-synthetic-corpus.test.ts | 44 --- .../session-search-tool-purge.test.ts | 21 +- .../ai-vault/ai-vault-scan-progress.test.ts | 72 ---- ...ssion-scanner-service-cancellation.test.ts | 141 -------- .../session-scanner-service-entry.test.ts | 36 -- .../session-scanner-service-search.test.ts | 14 - .../session-scanner-codex-title-index.test.ts | 95 +----- .../antigravity-chat-user-skills.test.ts | 89 ----- .../runtime-resume-pairing.test.ts | 14 +- .../ipc/agent-pane-retirement-ipc.test.ts | 13 +- .../browser-client-page-metadata-ipc.test.ts | 16 +- src/main/ipc/codex-config-sync.test.ts | 13 - .../filesystem/filesystem-mutations.test.ts | 11 - src/main/ipc/mobile.test.ts | 71 +--- src/main/ipc/profiles/orca-profiles.test.ts | 157 --------- .../pty-claude-agent-view-readiness.test.ts | 85 +++-- .../pty/host-env/spawn-cwd-wsl-move.test.ts | 17 +- .../opencode-root-model-launch.test.ts | 10 - .../client-protected-milestones.test.ts | 23 -- .../visible-review-reporting.test.ts | 40 --- ...orkspaceComposerCard.set-location.test.tsx | 138 -------- .../NewWorkspaceComposerCard.test-fixture.tsx | 127 ------- .../activity-clear-completed-hmr.test.ts | 147 -------- .../AgentLaunchPickerMenu.search.test.tsx | 103 ------ .../automation-captured-owner.test.ts | 66 ---- .../automation-host-invalidation.test.ts | 54 --- .../automations/automation-list-view.test.ts | 190 ----------- .../automation-usage-model.test.ts | 112 ------ .../external-automation-list-entries.test.ts | 73 ---- .../ClientHostedBrowserPagePane.test.tsx | 90 ----- .../browser-annotation-output.test.ts | 12 - .../browser-grab-toast-owner-cleanup.test.tsx | 21 -- .../browser-reopen-on-server.test.ts | 23 -- .../doc-preview-webview-attach.test.ts | 51 --- ...ontextual-tour-overlay-measurement.test.ts | 25 -- .../panel/CheckRunDetailsPanel.copy.test.tsx | 306 ----------------- .../agent-session-failure-words-text.test.ts | 41 --- ...-chat-subagent-handoff-copy-budget.test.ts | 95 +----- .../tools/lines/NativeChatToolLine.test.tsx | 43 --- .../scroll/NativeChatJumpToLatest.test.tsx | 28 -- ...veChatMessageList.scroll-behavior.test.tsx | 6 +- .../ai-vault-saved-session-lists.test.ts | 56 --- .../branches/branch-picker-copy.test.tsx | 93 ----- .../use-session-search-status.test.tsx | 63 ---- .../sign-in/HostAccountSignInPrompt.test.tsx | 48 --- .../SettingsHostScopePicker.test.tsx | 44 --- .../catalog/runtime-default-host-row.test.tsx | 56 --- .../remote-viewport-held-chip-portal.test.tsx | 137 -------- .../WorkspaceEmojiPickerButton.test.tsx | 49 --- .../claude-session-continuation.test.ts | 34 +- ...claude-settled-permission-requests.test.ts | 47 --- ...hook-listener-codex-subagent-names.test.ts | 96 ------ .../startup/agent-prompt-injection.test.ts | 34 -- .../opencode2-resume-launch-mode.spec.ts | 53 --- ...ent-launch-command-argument-values.test.ts | 166 --------- .../agent-session-failure-words.test.ts | 79 ----- ...ured-agent-session-rejection-words.test.ts | 100 ------ .../agent-session-option-catalog-omp.test.ts | 15 - 92 files changed, 73 insertions(+), 5612 deletions(-) delete mode 100644 config/scripts/build/mobile-web-app/mobile-web-bundle-manifest.test.mjs delete mode 100644 config/scripts/build/mobile-web-app/mobile-web-page-routes.test.mjs delete mode 100644 config/scripts/build/mobile-web-app/mobile-web-source-line-endings.test.mjs delete mode 100644 config/scripts/test-fixtures/script-copy-closure/script-module-dependencies.test.mjs delete mode 100644 config/scripts/workflow/source-routing/pr-unit-support-routing.test.mjs delete mode 100644 mobile/src/browser/browser-screencast-request.web.test.ts delete mode 100644 mobile/src/browser/browser-url.test.ts delete mode 100644 mobile/src/browser/mobile-browser-view-mode-state.test.ts delete mode 100644 mobile/src/components/HostRouteNoticeBanner.test.tsx delete mode 100644 mobile/src/components/MobileRepoIcon.test.tsx delete mode 100644 mobile/src/components/MobileRichMarkdownToolbar.test.tsx delete mode 100644 src/main/acp/history/acp-history-scaling.test.ts delete mode 100644 src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts delete mode 100644 src/main/ai-vault-search/session-search-native-chat-indexing.test.ts delete mode 100644 src/main/ai-vault-search/session-search-synthetic-corpus.test.ts delete mode 100644 src/main/ai-vault/ai-vault-scan-progress.test.ts delete mode 100644 src/main/ai-vault/scan-service/session-scanner-service-cancellation.test.ts delete mode 100644 src/main/antigravity/antigravity-chat-user-skills.test.ts delete mode 100644 src/main/ipc/profiles/orca-profiles.test.ts delete mode 100644 src/renderer/src/app-shell/visible-review-reporting.test.ts delete mode 100644 src/renderer/src/components/NewWorkspaceComposerCard.set-location.test.tsx delete mode 100644 src/renderer/src/components/NewWorkspaceComposerCard.test-fixture.tsx delete mode 100644 src/renderer/src/components/activity/activity-clear-completed-hmr.test.ts delete mode 100644 src/renderer/src/components/agent/AgentLaunchPickerMenu.search.test.tsx delete mode 100644 src/renderer/src/components/automations/automation-host-invalidation.test.ts delete mode 100644 src/renderer/src/components/automations/automation-list-view.test.ts delete mode 100644 src/renderer/src/components/automations/automation-usage-model.test.ts delete mode 100644 src/renderer/src/components/automations/external-automation-list-entries.test.ts delete mode 100644 src/renderer/src/components/browser-pane/browser-reopen-on-server.test.ts delete mode 100644 src/renderer/src/components/browser-pane/workspace-doc/doc-preview-webview-attach.test.ts delete mode 100644 src/renderer/src/components/contextual-tours/contextual-tour-overlay-measurement.test.ts delete mode 100644 src/renderer/src/components/editor/check-details/panel/CheckRunDetailsPanel.copy.test.tsx delete mode 100644 src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.test.ts delete mode 100644 src/renderer/src/components/native-chat/tools/lines/NativeChatToolLine.test.tsx delete mode 100644 src/renderer/src/components/native-chat/viewport/scroll/NativeChatJumpToLatest.test.tsx delete mode 100644 src/renderer/src/components/right-sidebar/source-control/branches/branch-picker-copy.test.tsx delete mode 100644 src/renderer/src/components/session-search/use-session-search-status.test.tsx delete mode 100644 src/renderer/src/components/settings/accounts/sign-in/HostAccountSignInPrompt.test.tsx delete mode 100644 src/renderer/src/components/settings/host-scope/SettingsHostScopePicker.test.tsx delete mode 100644 src/renderer/src/components/settings/runtime-environments/catalog/runtime-default-host-row.test.tsx delete mode 100644 src/renderer/src/components/terminal-pane/mobile-driver/remote-viewport-held-chip-portal.test.tsx delete mode 100644 src/renderer/src/components/workspace-emoji/WorkspaceEmojiPickerButton.test.tsx delete mode 100644 src/shared/agent-hook-listener/normalization/agent-hook-listener-codex-subagent-names.test.ts delete mode 100644 src/shared/agent-launch/startup/tui-agent-launch-command-argument-values.test.ts delete mode 100644 src/shared/agent-session/failures/agent-session-failure-words.test.ts delete mode 100644 src/shared/agent-session/failures/structured-agent-session-rejection-words.test.ts diff --git a/config/scripts/build/mobile-web-app/build-mobile-web-app-bundle.test.mjs b/config/scripts/build/mobile-web-app/build-mobile-web-app-bundle.test.mjs index 7d97c713144..a1d27b2dbec 100644 --- a/config/scripts/build/mobile-web-app/build-mobile-web-app-bundle.test.mjs +++ b/config/scripts/build/mobile-web-app/build-mobile-web-app-bundle.test.mjs @@ -5,11 +5,7 @@ import { withScratch, copyWrittenBundle } from '../../mobile-web-app/mobile-web-app-bundle-test-fixture.mjs' -import { - bundleMobileWebApp, - renameOutputsByContent, - routeChunkNames -} from './build-mobile-web-app-bundle.mjs' +import { bundleMobileWebApp } from './build-mobile-web-app-bundle.mjs' import { MOBILE_WEB_APP_ROUTE_ROOT } from '../../mobile-web-app/mobile-web-app-route-manifest.mjs' import { verifyMobileWebAppBundle } from './verify-mobile-web-app-bundle.mjs' import { @@ -141,33 +137,3 @@ describe('the verifier', () => { 240_000 ) }) - -describe('naming an output by its bytes', () => { - it('refuses two outputs that name each other', () => { - const emitted = (text) => new TextEncoder().encode(text) - const metafile = { - outputs: { - 'dist/a.js': { imports: [{ path: 'dist/b.js', kind: 'import-statement' }] }, - 'dist/b.js': { imports: [{ path: 'dist/a.js', kind: 'import-statement' }] } - } - } - // Cycles have no final dependency names/content hash; fail if esbuild ever stops emitting a DAG. - expect(() => - renameOutputsByContent(metafile, [ - { path: 'dist/a.js', contents: emitted('import "/assets/b.js"') }, - { path: 'dist/b.js', contents: emitted('import "/assets/a.js"') } - ]) - ).toThrow(/output cycle/) - }) - - it('refuses a route it cannot find an output for', async () => { - await withScratch(async (scratch) => { - const module = join(scratch, 'index.tsx') - await writeFile(module, 'export default function Route() { return null }\n') - // Only metafile maps routes to chunks; missing output must fail before phones request undefined/404. - expect(() => - routeChunkNames({ outputs: {} }, [{ key: './index.tsx', module }], new Map()) - ).toThrow(/\.\/index\.tsx reached no output/) - }) - }) -}) diff --git a/config/scripts/build/mobile-web-app/mobile-web-bundle-manifest.test.mjs b/config/scripts/build/mobile-web-app/mobile-web-bundle-manifest.test.mjs deleted file mode 100644 index e00c3366b52..00000000000 --- a/config/scripts/build/mobile-web-app/mobile-web-bundle-manifest.test.mjs +++ /dev/null @@ -1,50 +0,0 @@ -import { existsSync } from 'node:fs' -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { describe, expect, it } from 'vitest' -import { - contentTypeForExtension, - hashedAsset, - sha256Hex, - writeMobileWebBundleTree -} from './mobile-web-bundle-manifest.mjs' - -describe('hashedAsset', () => { - it('refuses an extension with no registered content type', () => { - // Unknown content type must fail rather than leave phone guessing how to serve addressed bytes. - expect(() => hashedAsset(Buffer.from('body'), 'wasm')).toThrow(/no content type registered/) - }) -}) - -describe('writeMobileWebBundleTree', () => { - it('marks a dark build outside the manifest, so its buildId is the same tree’s', async () => { - const outDir = await mkdtemp(join(tmpdir(), 'mobile-web-bundle-manifest-')) - try { - const bytes = Buffer.from('\n', 'utf8') - const tree = (env) => - writeMobileWebBundleTree({ - outDir, - written: [ - { - path: 'index.html', - bytes, - sha256: sha256Hex(bytes), - byteLength: bytes.byteLength, - contentType: contentTypeForExtension('html') - } - ], - desktopVersion: '1.0.0', - protocolWindow: { runtimeProtocolVersion: 3, minCompatibleRuntimeProtocolVersion: 3 }, - env - }) - const dark = await tree({ ORCA_SIDEBAR_INPUTS: 'main' }) - expect(existsSync(join(outDir, 'ORCA_SIDEBAR_INPUTS_MAIN'))).toBe(true) - const off = await tree({}) - expect(existsSync(join(outDir, 'ORCA_SIDEBAR_INPUTS_MAIN'))).toBe(false) - expect(dark.manifest.buildId).toBe(off.manifest.buildId) - } finally { - await rm(outDir, { recursive: true, force: true }) - } - }) -}) diff --git a/config/scripts/build/mobile-web-app/mobile-web-page-routes.test.mjs b/config/scripts/build/mobile-web-app/mobile-web-page-routes.test.mjs deleted file mode 100644 index eb7522f2543..00000000000 --- a/config/scripts/build/mobile-web-app/mobile-web-page-routes.test.mjs +++ /dev/null @@ -1,99 +0,0 @@ -import { describe, expect, it } from 'vitest' -import { - MOBILE_WEB_BUNDLE_MAX_ROUTE_GRANTS, - MobileWebBundleRouteSchema -} from '../../../../src/shared/mobile-web-bundle/manifest-contract.ts' -import { resolveMobileWebPageRoutes } from './build-mobile-web-app-bundle.mjs' -import { MOBILE_WEB_PAGE_ROUTES } from './mobile-web-page-routes.mjs' -import { routePathnameFromKey } from '../../mobile-web-app/mobile-web-app-route-manifest.mjs' - -// Test declared screens separately from bundle mechanics so route inventory can grow without coupling builder test size. - -describe('the page routes the manifest declares', () => { - it('turns a route key into the URL pattern expo-router gives it', () => { - expect(routePathnameFromKey('./h/[hostId]/index.tsx')).toBe('/h/[hostId]') - expect(routePathnameFromKey('./h/[hostId]/tasks.tsx')).toBe('/h/[hostId]/tasks') - expect(routePathnameFromKey('./h/[hostId]/session/[worktreeId].tsx')).toBe( - '/h/[hostId]/session/[worktreeId]' - ) - }) - - it('answers null for a layout, which is not a screen anyone navigates to', () => { - expect(routePathnameFromKey('./h/_layout.tsx')).toBeNull() - expect(routePathnameFromKey('./h/[hostId]/_layout.tsx')).toBeNull() - }) - - // Synthetic optional-grant input checks member-by-member mapping; newly declared field must survive builder map before reaching phone. - it('carries an optional grant list through, and writes no key for a route without one', () => { - expect( - resolveMobileWebPageRoutes( - ['./h/[hostId]/index.tsx', './h/[hostId]/tasks.tsx'], - [ - { - pathname: '/h/[hostId]', - grants: ['navigate'], - optionalGrants: ['externalNavigation'] - }, - { pathname: '/h/[hostId]/tasks', grants: ['navigate'], optionalGrants: [] } - ] - ) - ).toEqual([ - { pathname: '/h/[hostId]', grants: ['navigate'], optionalGrants: ['externalNavigation'] }, - { pathname: '/h/[hostId]/tasks', grants: ['navigate'] } - ]) - }) - - it('carries the host-area declaration through, and writes no key where there is none', () => { - expect( - resolveMobileWebPageRoutes( - ['./h/[hostId]/index.tsx', './h/[hostId]/tasks.tsx'], - [ - { pathname: '/h/[hostId]', grants: ['navigate'], canOwnHostArea: true }, - { pathname: '/h/[hostId]/tasks', grants: ['navigate'] } - ] - ) - ).toEqual([ - { pathname: '/h/[hostId]', grants: ['navigate'], canOwnHostArea: true }, - { pathname: '/h/[hostId]/tasks', grants: ['navigate'] } - ]) - }) - - /** - * The wide iPad's host-area session is granted every served route's list, so its in-page hops - * stay in the page. Bounded by what one route can be granted, so that grant never outgrows one route's. - */ - - // The phone refuses the whole manifest over one bad route, which takes every page down. - it('declares only routes the manifest schema accepts', () => { - for (const route of MOBILE_WEB_PAGE_ROUTES) { - expect( - MobileWebBundleRouteSchema.safeParse(route).error?.issues ?? [], - route.pathname - ).toEqual([]) - } - }) - - it('holds the optional lane to the manifest grammar and its own ceiling', () => { - // Released shells read each list against the ceiling on its own, so the writer bounds each the same way. - const withOptional = (optionalGrants, grants = []) => - MobileWebBundleRouteSchema.safeParse({ pathname: '/h/[hostId]', grants, optionalGrants }) - .success - expect(withOptional(['externalNavigation'])).toBe(true) - expect(withOptional(['native.externalNavigation'])).toBe(false) - const names = (count, prefix) => - Array.from({ length: count }, (_value, index) => `${prefix}${String(index)}`) - const cap = MOBILE_WEB_BUNDLE_MAX_ROUTE_GRANTS - expect(withOptional(names(cap, 'opt'), names(cap, 'req'))).toBe(true) - expect(withOptional(names(cap + 1, 'opt'))).toBe(false) - }) - - it('fails the build on a declaration the bundle cannot render', () => { - // Declared-but-unbundled route opens page then paints Unmatched; catch mismatch at declaration. - expect(() => - resolveMobileWebPageRoutes( - ['./h/[hostId]/index.tsx'], - [{ pathname: '/h/[hostId]/gone', grants: [] }] - ) - ).toThrow('has no module in the bundle') - }) -}) diff --git a/config/scripts/build/mobile-web-app/mobile-web-source-line-endings.test.mjs b/config/scripts/build/mobile-web-app/mobile-web-source-line-endings.test.mjs deleted file mode 100644 index e5df4594bb8..00000000000 --- a/config/scripts/build/mobile-web-app/mobile-web-source-line-endings.test.mjs +++ /dev/null @@ -1,19 +0,0 @@ -import { mkdtemp, rm, writeFile } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { describe, expect, it } from 'vitest' -import { assertNoCarriageReturnsInSource } from './mobile-web-source-line-endings.mjs' - -describe('assertNoCarriageReturnsInSource', () => { - it('rejects a CRLF source file, because CRLF changes every asset hash and the buildId', async () => { - const scratch = await mkdtemp(join(tmpdir(), 'orca-mobile-web-eol-')) - try { - await writeFile(join(scratch, 'route.tsx'), 'const a = 1\r\nconst b = 2\r\n', 'utf8') - await expect(assertNoCarriageReturnsInSource(scratch)).rejects.toThrow( - /CRLF in mobile web source/ - ) - } finally { - await rm(scratch, { recursive: true, force: true }) - } - }) -}) diff --git a/config/scripts/plugin-authoring/content-validation/validate-plugin-files.test.mjs b/config/scripts/plugin-authoring/content-validation/validate-plugin-files.test.mjs index 047a8d72c74..f9ddb0587af 100644 --- a/config/scripts/plugin-authoring/content-validation/validate-plugin-files.test.mjs +++ b/config/scripts/plugin-authoring/content-validation/validate-plugin-files.test.mjs @@ -1,4 +1,4 @@ -import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import path from 'node:path' import process from 'node:process' @@ -36,29 +36,4 @@ describe('validate-plugin-files', () => { expect(result.code).toBe(1) expect(result.stderr).toContain('panel/index.html') }, 60_000) - - it('passes a scaffolded plugin, explaining why there is no release entry outside Git', async () => { - const dir = path.join(await tempRoot(), 'dusk') - await createPluginFromTemplate({ dir, template: 'theme', publisher: 'acme' }) - const result = await validate(dir) - expect(result.code, result.stderr).toBe(0) - expect(result.stdout).toContain('not in a Git repository') - }, 60_000) - - it('passes a language pack that translates protected copy, warning that Orca ignores it', async () => { - const dir = path.join(await tempRoot(), 'portuguese') - await createPluginFromTemplate({ dir, template: 'language-pack', publisher: 'acme' }) - await writeFile( - path.join(dir, 'locales', 'pt-BR.json'), - JSON.stringify({ - common: { cancel: 'Cancelar' }, - components: { 'native-chat': { approval: { allow: 'Permitir', deny: 'Negar' } } } - }) - ) - const result = await validate(dir) - expect(result.code, result.stderr).toBe(0) - expect(result.stderr).toContain( - 'warning: contributes.languagePacks "pt-BR" (locales/pt-BR.json): 2 protected strings are ignored' - ) - }, 60_000) }) diff --git a/config/scripts/test-fixtures/script-copy-closure/script-module-dependencies.test.mjs b/config/scripts/test-fixtures/script-copy-closure/script-module-dependencies.test.mjs deleted file mode 100644 index 7e0a827a2ff..00000000000 --- a/config/scripts/test-fixtures/script-copy-closure/script-module-dependencies.test.mjs +++ /dev/null @@ -1,66 +0,0 @@ -import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterAll, describe, expect, it } from 'vitest' -import { copyScriptWithLocalModules } from './script-module-dependencies.mjs' - -const fixtureDir = mkdtempSync(join(tmpdir(), 'script-module-dependencies-')) - -afterAll(() => { - rmSync(fixtureDir, { recursive: true, force: true }) -}) - -function sourceTree(files) { - const sourceDir = mkdtempSync(join(fixtureDir, 'source-')) - for (const [name, contents] of Object.entries(files)) { - writeFileSync(join(sourceDir, name), contents) - } - return sourceDir -} - -function copiedNames(files, entryName) { - const sourceDir = sourceTree(files) - const destinationDir = join(mkdtempSync(join(fixtureDir, 'dest-')), 'scripts') - copyScriptWithLocalModules(join(sourceDir, entryName), destinationDir) - const names = readdirSync(destinationDir).sort() - for (const name of names) { - expect(readFileSync(join(destinationDir, name), 'utf8'), name).toBe(files[name]) - } - return names -} - -describe('copyScriptWithLocalModules', () => { - it('preserves parent paths for modules shared with the runtime', () => { - const sourceDir = sourceTree({ 'runtime.ts': 'export const value = 42\n' }) - mkdirSync(join(sourceDir, 'scripts')) - writeFileSync(join(sourceDir, 'scripts', 'entry.mjs'), "export { value } from '../runtime'\n") - const destinationRoot = mkdtempSync(join(fixtureDir, 'dest-')) - copyScriptWithLocalModules( - join(sourceDir, 'scripts', 'entry.mjs'), - join(destinationRoot, 'scripts') - ) - expect(readFileSync(join(destinationRoot, 'runtime.ts'), 'utf8')).toBe( - 'export const value = 42\n' - ) - }) - - // Cover require-only CJS gate dependencies so copied fixture fails for target defect, not missing module. - - it('follows a require reached only through an imported module', () => { - const names = copiedNames( - { - 'entry.mjs': "import './middle.cjs'\n", - 'middle.cjs': "require('./leaf.cjs')\n", - 'leaf.cjs': 'module.exports = {}\n' - }, - 'entry.mjs' - ) - expect(names).toContain('leaf.cjs') - }) - - it('terminates on a cycle rather than recursing forever', () => { - expect( - copiedNames({ 'a.mjs': "import './b.mjs'\n", 'b.mjs': "import './a.mjs'\n" }, 'a.mjs') - ).toEqual(['a.mjs', 'b.mjs']) - }) -}) diff --git a/config/scripts/test-runtime/sqlite-admission/vitest-node-runtime-files.mjs b/config/scripts/test-runtime/sqlite-admission/vitest-node-runtime-files.mjs index 3fc7dd70837..11fa95a615f 100644 --- a/config/scripts/test-runtime/sqlite-admission/vitest-node-runtime-files.mjs +++ b/config/scripts/test-runtime/sqlite-admission/vitest-node-runtime-files.mjs @@ -58,7 +58,6 @@ const NODE_RUNTIME_INCLUDE = [ 'src/main/acp/session/acp-structured-sign-in.test.ts', 'src/main/acp/session/acp-structured-session-adapter.test.ts', 'src/main/acp/session/acp-structured-session-adapter-resume.test.ts', - 'src/main/acp/history/acp-history-scaling.test.ts', 'src/main/pi/rpc-turn-races.test.ts', 'src/main/acp/session/children/acp-structured-child-session-end.test.ts', 'src/main/acp/session/children/acp-structured-child-stop.test.ts', diff --git a/config/scripts/workflow/source-routing/pr-package-test-paths.mjs b/config/scripts/workflow/source-routing/pr-package-test-paths.mjs index 3166e02549d..22cec99aa84 100644 --- a/config/scripts/workflow/source-routing/pr-package-test-paths.mjs +++ b/config/scripts/workflow/source-routing/pr-package-test-paths.mjs @@ -157,7 +157,6 @@ export const WINDOWS_BOUNDARY_TESTS = [ 'config/scripts/native-build/windows/windows-pe-machine.test.mjs', 'config/scripts/native-build/windows/build-windows-cli-launcher.test.mjs', 'config/scripts/desktop-distribution/runtime-verification/verify-packaged-windows-agent-hook.test.mjs', - 'config/scripts/test-fixtures/script-copy-closure/script-module-dependencies.test.mjs', 'src/main/windows/windows-registry-addon.test.ts', 'src/main/daemon/executable-host/windows/testing/daemon-host-process-package.test.ts', 'src/shared/windows-security-addon.win32.test.ts', diff --git a/config/scripts/workflow/source-routing/pr-unit-support-routing.test.mjs b/config/scripts/workflow/source-routing/pr-unit-support-routing.test.mjs deleted file mode 100644 index 7e3a023c4f4..00000000000 --- a/config/scripts/workflow/source-routing/pr-unit-support-routing.test.mjs +++ /dev/null @@ -1,98 +0,0 @@ -import { beforeAll, describe, expect, it } from 'vitest' -import { - buildUnitDependencyGraph, - collectUnitDependencyGraph, - unitConsumers -} from '../../ci/unit-selection/dependency-graph/ci-unit-dependency-graph.mjs' -import { - CROSS_VERSION_WIRE_DIR, - discoverUnitFiles -} from '../../ci/unit-selection/dependency-graph/ci-unit-files.mjs' - -const harness = - 'src/renderer/src/runtime/web-session-tabs-sync/testing/web-session-tabs-sync.test-fixture.ts' -const structuredSessionHarness = - 'src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test-harness.tsx' - -const unitHarnesses = [ - harness, - structuredSessionHarness, - 'src/renderer/src/store/slices/editor/editor-slice-test-harness.ts', - 'src/renderer/src/store/slices/browser/browser-slice-test-harness.ts', - 'src/renderer/src/runtime/hosted-terminals/creation/testing/web-runtime-terminal-create.test-fixture.ts', - 'src/renderer/src/runtime/sync-runtime-graph/testing/sync-runtime-graph-test-harness.ts' -] -const supportBridges = new Set([ - // Launch request suites' store rig; it takes the terminal harness's workspace layout. - 'src/renderer/src/runtime/hosted-terminals/creation/agent-launch/web-runtime-agent-launch-options.test-fixture.ts', - 'src/renderer/src/runtime/browser/creation/testing/web-runtime-browser-creation-placement-test-rig.ts', - 'src/renderer/src/components/editor/model-lifetime/retention/editor-model-lifetime-fixture.ts', - // Autosave suites' store rig; it takes the editor harness's tab-layout maps. - 'src/renderer/src/components/editor/autosave/editor-autosave-controller-test-fixture.ts', - // Closed-saving-view save suites' rig; it builds on the autosave rig above. - 'src/renderer/src/components/editor/save/queue/editor-save-closed-view.test-fixture.ts', - // Browser suites' store with main drawing their tabs; it takes the browser harness's store. - 'src/renderer/src/store/slices/browser/browser-slice-drawn-from-main.test-fixture.ts' -]) - -function productConsumers(file, graph, unitFiles) { - return [...unitConsumers([file], graph.reverse)].filter( - (consumer) => - consumer !== file && - !unitFiles.has(consumer) && - !(consumer.startsWith(CROSS_VERSION_WIRE_DIR) && consumer.endsWith('.unit.test.ts')) && - !supportBridges.has(consumer) - ) -} - -describe('unit support import ownership', () => { - let graph - let unitFiles - beforeAll(() => { - graph = collectUnitDependencyGraph() - unitFiles = new Set(discoverUnitFiles()) - }, 60_000) - - it.each(unitHarnesses)('rejects any product consumer of %s', (unitHarness) => { - expect(graph.files.has(unitHarness)).toBe(true) - expect( - [...unitConsumers([unitHarness], graph.reverse)].some((file) => unitFiles.has(file)) - ).toBe(true) - expect(productConsumers(unitHarness, graph, unitFiles)).toEqual([]) - }) - - it.each([ - "import '@/runtime/web-session-tabs-sync/testing/web-session-tabs-sync.test-fixture'", - "export * from '@/runtime/web-session-tabs-sync/testing/web-session-tabs-sync.test-fixture'", - "import('@/runtime/web-session-tabs-sync/testing/web-session-tabs-sync.test-fixture')", - "import type { Fixture } from '@/runtime/web-session-tabs-sync/testing/web-session-tabs-sync.test-fixture'" - ])('detects a product importer using %s', (source) => { - const importer = 'src/renderer/src/runtime/product.ts' - const test = 'src/renderer/src/runtime/ownership.test.ts' - const synthetic = buildUnitDependencyGraph( - new Map([ - [harness, 'export const value = 1'], - [importer, source], - [test, "import './web-session-tabs-sync/testing/web-session-tabs-sync.test-fixture'"] - ]) - ) - expect(productConsumers(harness, synthetic, new Set([test]))).toEqual([importer]) - }) - - it('rejects product consumers reached through an audited support bridge', () => { - const bridge = - 'src/renderer/src/components/editor/model-lifetime/retention/editor-model-lifetime-fixture.ts' - const importer = 'src/renderer/src/components/editor/product.ts' - const synthetic = buildUnitDependencyGraph( - new Map([ - [harness, 'export const value = 1'], - [ - bridge, - "import '@/runtime/web-session-tabs-sync/testing/web-session-tabs-sync.test-fixture'" - ], - [importer, "import './model-lifetime/retention/editor-model-lifetime-fixture'"] - ]) - ) - expect(productConsumers(harness, synthetic, new Set())).toEqual([importer]) - }) -}) diff --git a/mobile/src/app-update/app-update-checker.test.ts b/mobile/src/app-update/app-update-checker.test.ts index 1652654f393..6cfa6ab6e16 100644 --- a/mobile/src/app-update/app-update-checker.test.ts +++ b/mobile/src/app-update/app-update-checker.test.ts @@ -84,15 +84,6 @@ describe('app update checker', () => { expect(h.checker.getSnapshot().checking).toBe(false) }) - it('joins a manual check to one already in flight', async () => { - const h = harness({ stored: { lastCheckedAt: T0 }, replies: [{ kind: 'current' }] }) - h.checker.start() - await vi.advanceTimersByTimeAsync(0) - const [first, second] = [h.checker.checkNow(), h.checker.checkNow()] - await expect(Promise.all([first, second])).resolves.toEqual(['up-to-date', 'up-to-date']) - expect(checks(h)).toBe(1) - }) - it('keeps the 1 h retry for a manual check that fails while the store is still loading', async () => { let finishLoad: () => void = () => {} const h = harness({ @@ -112,15 +103,6 @@ describe('app update checker', () => { expect(checks(h)).toBe(2) }) - it('applies one load and runs one check across a StrictMode-style double start', async () => { - const h = harness({}) - h.checker.start()() - h.checker.start() - await vi.advanceTimersByTimeAsync(0) - expect(h.deps.loadPreferences).toHaveBeenCalledTimes(1) - expect(checks(h)).toBe(1) - }) - function deferredReply() { let answer: (result: AppUpdateCheckResult) => void = () => {} const reply = new Promise((resolve) => { diff --git a/mobile/src/browser/browser-screencast-request.web.test.ts b/mobile/src/browser/browser-screencast-request.web.test.ts deleted file mode 100644 index cbf56338cea..00000000000 --- a/mobile/src/browser/browser-screencast-request.web.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import { BRIDGE_MAX_MESSAGE_BYTES } from '../mobile-web-shell/bridge/bridge-caps' -import { buildMobileBrowserScreencastRequest } from './browser-screencast-request' -import { - base64Characters, - binaryEventEnvelopeBytes, - budgetedMobileViewDeviceScaleFactor, - buildMobileBrowserScreencastRequest as buildOnWeb, - WORST_CASE_JPEG_BYTES_PER_PIXEL -} from './browser-screencast-request.web' - -/** A phone's measured browser area, in CSS pixels. */ -const PHONE = { width: 390, height: 712 } - -/** What the shell would have to post for a frame of this area at the worst case JPEG has. */ -function frameMessageBytes(areaPixels: number): number { - const imageBytes = Math.ceil(areaPixels * WORST_CASE_JPEG_BYTES_PER_PIXEL) - return base64Characters(imageBytes) + binaryEventEnvelopeBytes() -} - -function mobileFrameArea( - layout: { width: number; height: number }, - request: { deviceScaleFactor?: number } | null -): number { - const scale = request?.deviceScaleFactor - if (scale === undefined) { - throw new Error('the request carried no device scale factor') - } - return Math.round(layout.width) * Math.round(layout.height) * scale * scale -} - -describe('the mobile-view area budget', () => { - // Native density at same phone size exceeds bridge cap; without web budget shell drops frame. - it('is needed: the native request overflows the frame cap on a phone', () => { - const native = buildMobileBrowserScreencastRequest(PHONE, 2, 'mobile') - - expect(native?.deviceScaleFactor).toBe(2) - expect(frameMessageBytes(mobileFrameArea(PHONE, native))).toBeGreaterThan( - BRIDGE_MAX_MESSAGE_BYTES - ) - }) - - it('keeps the worst case frame inside one bridge message', () => { - const budgeted = buildOnWeb(PHONE, 2, 'mobile') - - expect(frameMessageBytes(mobileFrameArea(PHONE, budgeted))).toBeLessThanOrEqual( - BRIDGE_MAX_MESSAGE_BYTES - ) - }) - - it('uses the largest hundredth of density whose frame fits the cap', () => { - const scale = budgetedMobileViewDeviceScaleFactor(PHONE) - expect( - frameMessageBytes(mobileFrameArea(PHONE, { deviceScaleFactor: scale + 0.01 })) - ).toBeGreaterThan(BRIDGE_MAX_MESSAGE_BYTES) - }) - - it('leaves web mode byte-identical to the native request', () => { - expect(buildOnWeb(PHONE, 2, 'web')).toEqual( - buildMobileBrowserScreencastRequest(PHONE, 2, 'web') - ) - expect(buildOnWeb(PHONE, 2)).toEqual(buildMobileBrowserScreencastRequest(PHONE, 2)) - }) - - it('never asks for more density than native, on a viewport the budget does not bind', () => { - expect(budgetedMobileViewDeviceScaleFactor({ width: 200, height: 200 })).toBe(2) - }) - - it('stops at one device pixel per CSS pixel on a viewport no scale would fit', () => { - // Never request fewer device pixels than CSS pixels; shell drops frames still too large at density 1. - expect(budgetedMobileViewDeviceScaleFactor({ width: 2000, height: 1400 })).toBe(1) - }) - - it('answers the native factor when there is no layout to budget against', () => { - expect(budgetedMobileViewDeviceScaleFactor(null)).toBe(2) - expect(buildOnWeb(null, 2, 'mobile')).toBeNull() - expect(buildOnWeb({ width: 0, height: 712 }, 2, 'mobile')).toBeNull() - }) -}) - -it('counts base64 padding for an image that does not fill its last group', () => { - expect(base64Characters(3001)).toBe(4004) -}) - -describe('web-mode request work', () => { - for (const mode of [undefined, 'web'] as const) { - it(`skips discarded mobile density work in ${mode ?? 'default'} view`, () => { - const layouts = [ - { width: 360, height: 640 }, - { width: 390, height: 712 }, - { width: 402, height: 593 }, - { width: 319.49, height: 239.51 }, - { width: 1200, height: 1200 }, - { width: 2000, height: 1400 } - ] - const expected = layouts.map((layout) => buildMobileBrowserScreencastRequest(layout, 2, mode)) - const stringify = vi.spyOn(JSON, 'stringify') - const sqrt = vi.spyOn(Math, 'sqrt') - let actual: ReturnType[] = [] - let envelopeCalls = 0 - let sqrtCalls = 0 - try { - actual = layouts.map((layout) => buildOnWeb(layout, 2, mode)) - envelopeCalls = stringify.mock.calls.length - sqrtCalls = sqrt.mock.calls.length - } finally { - stringify.mockRestore() - sqrt.mockRestore() - } - - expect(actual).toEqual(expected) - expect(envelopeCalls).toBe(0) - expect(sqrtCalls).toBe(0) - }) - } -}) diff --git a/mobile/src/browser/browser-url.test.ts b/mobile/src/browser/browser-url.test.ts deleted file mode 100644 index a0d2a7f11f4..00000000000 --- a/mobile/src/browser/browser-url.test.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { describe, expect, it } from 'vitest' - -import { normalizeBrowserUrl } from './browser-url' - -describe('normalizeBrowserUrl', () => { - it('keeps localhost-style addresses as http URLs', () => { - expect(normalizeBrowserUrl('localhost:3000')).toBe('http://localhost:3000/') - expect(normalizeBrowserUrl('127.0.0.1:6769/web-index.html')).toBe( - 'http://127.0.0.1:6769/web-index.html' - ) - }) -}) diff --git a/mobile/src/browser/mobile-browser-view-mode-state.test.ts b/mobile/src/browser/mobile-browser-view-mode-state.test.ts deleted file mode 100644 index 495955a3cc2..00000000000 --- a/mobile/src/browser/mobile-browser-view-mode-state.test.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { beforeEach, describe, expect, it } from 'vitest' -import { - clearMobileBrowserViewModeState, - getInitialMobileBrowserViewMode, - saveMobileBrowserViewMode -} from './mobile-browser-view-mode-state' - -describe('mobile browser view mode state', () => { - beforeEach(() => { - clearMobileBrowserViewModeState() - }) - - it('defaults each browser page to web view', () => { - expect(getInitialMobileBrowserViewMode('worktree-1', 'page-1')).toBe('web') - expect(getInitialMobileBrowserViewMode('worktree-1', null)).toBe('web') - }) - - it('defaults local HTML reports to mobile view', () => { - expect( - getInitialMobileBrowserViewMode( - 'folder:workspace-1', - 'page-1', - 'file:///Users/me/report%20one.HTML?generated=1' - ) - ).toBe('mobile') - expect( - getInitialMobileBrowserViewMode('worktree-1', 'page-2', 'file:///C:/Users/me/report.htm') - ).toBe('mobile') - }) - - it('keeps remote HTML URLs and non-HTML files in web view', () => { - expect( - getInitialMobileBrowserViewMode('worktree-1', 'page-1', 'https://example.com/report.html') - ).toBe('web') - expect(getInitialMobileBrowserViewMode('worktree-1', 'page-2', 'file:///repo/notes.txt')).toBe( - 'web' - ) - }) - - it('restores the last mode for the same browser page after remount', () => { - saveMobileBrowserViewMode('worktree-1', 'page-1', 'mobile') - - expect(getInitialMobileBrowserViewMode('worktree-1', 'page-1')).toBe('mobile') - expect(getInitialMobileBrowserViewMode('worktree-1', 'page-2')).toBe('web') - expect(getInitialMobileBrowserViewMode('worktree-2', 'page-1')).toBe('web') - }) - - it('preserves an explicit web choice for a local HTML report', () => { - saveMobileBrowserViewMode('worktree-1', 'page-1', 'web') - - expect( - getInitialMobileBrowserViewMode('worktree-1', 'page-1', 'file:///repo/report.html') - ).toBe('web') - }) -}) diff --git a/mobile/src/components/DragReorderList.test.tsx b/mobile/src/components/DragReorderList.test.tsx index bd3cb5aeb07..defe697b10a 100644 --- a/mobile/src/components/DragReorderList.test.tsx +++ b/mobile/src/components/DragReorderList.test.tsx @@ -25,17 +25,6 @@ describe('drag completion and cancellation', () => { await act(async () => fixture.root.unmount()) }) - it('commits the moved order once on a successful drop', async () => { - const fixture = await mounted() - await act(async () => { - fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) - fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) - fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, true) - }) - expect(fixture.onReorder.mock.calls).toEqual([[['b', 'a']]]) - await act(async () => fixture.root.unmount()) - }) - it('does not write after a failed activation or an unchanged drop', async () => { const fixture = await mounted() await act(async () => { @@ -286,56 +275,3 @@ it('keeps measured rows still on hold, restores cancellation, and drops by moved expect(move).toHaveBeenCalledExactlyOnceWith({ movedId: 'a', afterId: 'c' }) await act(async () => root!.unmount()) }) - -it.each([100, 1000])( - 'bounds accessibility work for %i rows while excluded slots stay fixed', - async (count) => { - const ids = Array.from({ length: count }, (_, at) => - at === 1 ? 'fixed' : at === 2 ? 'busy' : `row-${at}` - ) - let keyReads = 0 - let membership = 0 - const onMove = vi.fn() - const onReorder = vi.fn() - function Harness({ items = ids }: { items?: string[] }) { - return createElement(DragReorderList, { - items, - itemKey: (id) => { - keyReads++ - return id - }, - canMove: (id) => { - membership++ - return id !== 'fixed' && id !== 'busy' - }, - rowHeight: 56, - renderRow: (id, handle) => createElement('Text', null, id, handle), - onMove, - onReorder, - scrollRef: useAnimatedRef(), - scrollOffsetY: useSharedValue(0), - scrollContentHeight: useSharedValue(count * 56) - }) - } - let root: ReturnType - await act(async () => { - root = create(createElement(Harness)) - }) - const first = root!.root.findAll( - (node) => node.type === Animated.View && node.props.onAccessibilityAction - )[0]! - const accessibilityAction = first.props.onAccessibilityAction - keyReads = membership = 0 - await act(async () => accessibilityAction({ nativeEvent: { actionName: 'moveUp' } })) - expect(onMove).not.toHaveBeenCalled() - await act(async () => accessibilityAction({ nativeEvent: { actionName: 'moveDown' } })) - expect(keyReads).toBeLessThanOrEqual(3 * count) - expect(membership).toBeLessThanOrEqual(3 * count) - expect(onReorder.mock.calls[0]![0].slice(0, 4)).toEqual(['row-3', 'fixed', 'busy', 'row-0']) - expect(onMove).toHaveBeenCalledExactlyOnceWith({ movedId: 'row-0', beforeId: 'row-4' }) - await act(async () => root!.update(createElement(Harness, { items: ids.slice(1) }))) - await act(async () => accessibilityAction({ nativeEvent: { actionName: 'moveDown' } })) - expect(onMove).toHaveBeenCalledOnce() - await act(async () => root!.unmount()) - } -) diff --git a/mobile/src/components/HostRouteNoticeBanner.test.tsx b/mobile/src/components/HostRouteNoticeBanner.test.tsx deleted file mode 100644 index 7726ba372a9..00000000000 --- a/mobile/src/components/HostRouteNoticeBanner.test.tsx +++ /dev/null @@ -1,65 +0,0 @@ -import { createElement, type ReactNode } from 'react' -import { act, create, type ReactTestRenderer } from 'react-test-renderer' -import { afterEach, describe, expect, it, vi } from 'vitest' - -vi.mock('react-native', async () => { - const React = await import('react') - return { - Pressable: ({ children, ...props }: { children?: ReactNode }) => - React.createElement('Pressable', props, children), - Text: ({ children, ...props }: { children?: ReactNode }) => - React.createElement('Text', props, children), - View: ({ children, ...props }: { children?: ReactNode }) => - React.createElement('View', props, children), - StyleSheet: { create: (styles: unknown) => styles } - } -}) -vi.mock('lucide-react-native', () => ({ X: 'X' })) - -import { HostRouteNoticeBanner } from './HostRouteNoticeBanner' - -// Announce inserted banner because list reader may be below it; failure interrupts, bounced-route context stays polite so urgency retains meaning. -describe('the host route notice banner announces itself', () => { - let renderer: ReactTestRenderer | null = null - - afterEach(() => { - act(() => renderer?.unmount()) - renderer = null - }) - - function banner(tone?: 'notice' | 'failure') { - act(() => { - renderer = create( - createElement(HostRouteNoticeBanner, { - message: 'Nothing happened', - tone, - onDismiss: () => {} - }) - ) - }) - if (renderer === null) { - throw new Error('the banner did not render') - } - // Match host name to avoid ElementType cast for arbitrary RN strings; first match is banner root carrying tested props. - return renderer.root.findAll((node) => String(node.type) === 'View')[0] - } - - it('interrupts for a failure, which is an action that did not happen', () => { - const root = banner('failure') - expect(root.props.accessibilityRole).toBe('alert') - expect(root.props.accessibilityLiveRegion).toBe('assertive') - }) - - it('waits its turn for a notice, which is context rather than a refusal', () => { - const root = banner('notice') - expect(root.props.accessibilityLiveRegion).toBe('polite') - // No alert role: nothing here failed, and an alert is what the tone above is for. - expect(root.props.accessibilityRole).toBeUndefined() - }) - - it('treats the default tone as the notice one, which is what the route caller passes', () => { - const root = banner(undefined) - expect(root.props.accessibilityLiveRegion).toBe('polite') - expect(root.props.accessibilityRole).toBeUndefined() - }) -}) diff --git a/mobile/src/components/MobileMarkdown.test.ts b/mobile/src/components/MobileMarkdown.test.ts index 455c05f153d..1eade1f67f5 100644 --- a/mobile/src/components/MobileMarkdown.test.ts +++ b/mobile/src/components/MobileMarkdown.test.ts @@ -1,22 +1,6 @@ import { describe, expect, it } from 'vitest' -import { isMobileMermaidLanguage } from './mobile-mermaid-language' import { normalizeMobileMarkdownPreviewHtml } from './mobile-markdown-preview-html' -describe('isMobileMermaidLanguage', () => { - it('matches mermaid case-insensitively after trim', () => { - expect(isMobileMermaidLanguage('mermaid')).toBe(true) - expect(isMobileMermaidLanguage('Mermaid')).toBe(true) - expect(isMobileMermaidLanguage(' MERMAID ')).toBe(true) - }) - - it('rejects non-mermaid languages and missing language', () => { - expect(isMobileMermaidLanguage(undefined)).toBe(false) - expect(isMobileMermaidLanguage('')).toBe(false) - expect(isMobileMermaidLanguage('ts')).toBe(false) - expect(isMobileMermaidLanguage('mermaidx')).toBe(false) - }) -}) - describe('normalizeMobileMarkdownPreviewHtml', () => { it('normalizes common README HTML into readable Markdown preview text', () => { const normalized = normalizeMobileMarkdownPreviewHtml(` diff --git a/mobile/src/components/MobileRepoIcon.test.tsx b/mobile/src/components/MobileRepoIcon.test.tsx deleted file mode 100644 index 09355287677..00000000000 --- a/mobile/src/components/MobileRepoIcon.test.tsx +++ /dev/null @@ -1,45 +0,0 @@ -import { Folder } from 'lucide-react-native' -import { createElement } from 'react' -import { Image } from 'react-native' -import { act, create, type ReactTestRenderer } from 'react-test-renderer' -import { afterEach, describe, expect, it, vi } from 'vitest' -import { MobileRepoIcon } from './MobileRepoIcon' - -vi.mock('react-native', () => ({ - Image: 'Image', - StyleSheet: { create: (styles: T) => styles }, - Text: 'Text', - View: 'View' -})) -// Each icon renders as an element named after it. -vi.mock( - 'lucide-react-native', - () => new Proxy({}, { has: () => true, get: (_, name) => String(name) }) -) -vi.mock('../theme/use-mobile-color-scheme', () => ({ useMobileColorScheme: () => 'dark' })) - -let renderer: ReactTestRenderer | null = null -afterEach(() => { - act(() => renderer?.unmount()) - renderer = null -}) - -function imageIcon(src: string) { - return createElement(MobileRepoIcon, { repoIcon: { type: 'image', src, label: 'owner/repo' } }) -} - -describe('MobileRepoIcon', () => { - it('shows the folder after an image fails, and tries a changed image again', () => { - act(() => { - renderer = create(imageIcon('https://github.com/owner.png?size=64')) - }) - act(() => renderer!.root.findByType(Image).props.onError()) - expect(renderer!.root.findAllByType(Image)).toHaveLength(0) - expect(renderer!.root.findAllByType(Folder)).toHaveLength(1) - - act(() => renderer!.update(imageIcon('data:image/png;base64,AAAA'))) - expect(renderer!.root.findByType(Image).props.source).toEqual({ - uri: 'data:image/png;base64,AAAA' - }) - }) -}) diff --git a/mobile/src/components/MobileRichMarkdownToolbar.test.tsx b/mobile/src/components/MobileRichMarkdownToolbar.test.tsx deleted file mode 100644 index 6cdbcbd498a..00000000000 --- a/mobile/src/components/MobileRichMarkdownToolbar.test.tsx +++ /dev/null @@ -1,68 +0,0 @@ -import { createElement } from 'react' -import { act, create, type ReactTestRenderer } from 'react-test-renderer' -import { afterEach, describe, expect, it, vi } from 'vitest' - -vi.mock('react-native', () => ({ - Pressable: 'Pressable', - ScrollView: 'ScrollView', - StyleSheet: { create: (styles: unknown) => styles, hairlineWidth: 1 }, - View: 'View' -})) - -vi.mock('lucide-react-native', () => ({ - Bold: 'Bold', - Code2: 'Code2', - FileCode2: 'FileCode2', - Heading1: 'Heading1', - Heading2: 'Heading2', - Heading3: 'Heading3', - ImageIcon: 'ImageIcon', - Italic: 'Italic', - Link: 'Link', - List: 'List', - ListOrdered: 'ListOrdered', - ListTodo: 'ListTodo', - Pilcrow: 'Pilcrow', - Quote: 'Quote', - Strikethrough: 'Strikethrough' -})) - -import { MobileRichMarkdownToolbar } from './MobileRichMarkdownToolbar' -import type { MobileRichMarkdownCommand } from './mobile-rich-markdown-editor-contract' - -let renderer: ReactTestRenderer | null = null - -afterEach(() => { - act(() => renderer?.unmount()) - renderer = null -}) - -function render(editable: boolean, onCommand: (command: MobileRichMarkdownCommand) => void) { - act(() => { - renderer = create(createElement(MobileRichMarkdownToolbar, { editable, onCommand })) - }) - return renderer!.root.findAll((node) => String(node.type) === 'Pressable') -} - -describe('the rich Markdown toolbar', () => { - it('routes named controls to their editor commands', () => { - const onCommand = vi.fn() - const buttons = render(true, onCommand) - for (const [label, command] of [ - ['Bold', 'bold'], - ['Link', 'link'], - ['Inline code', 'inlineCode'] - ] as const) { - onCommand.mockClear() - const button = buttons.find((candidate) => candidate.props.accessibilityLabel === label) - expect(button).toBeDefined() - act(() => button!.props.onPress()) - expect(onCommand.mock.calls).toEqual([[command]]) - } - }) - - it('disables every button against a document that cannot be edited', () => { - const buttons = render(false, vi.fn()) - expect(buttons.filter((button) => button.props.disabled !== true)).toEqual([]) - }) -}) diff --git a/mobile/src/components/ProtocolBlockScreen.test.ts b/mobile/src/components/ProtocolBlockScreen.test.ts index f8560e28cbe..3b79cc9543e 100644 --- a/mobile/src/components/ProtocolBlockScreen.test.ts +++ b/mobile/src/components/ProtocolBlockScreen.test.ts @@ -72,34 +72,6 @@ describe('ProtocolBlockScreen', () => { renderer = null }) - // Why: the protocol wall shipped before the bundle one; its copy is what users already see. - it('keeps the existing protocol wall rendering unchanged', () => { - const mobile = render({ - kind: 'blocked', - reason: 'mobile-too-old', - desktopVersion: 5, - requiredMobileVersion: 99 - }) - expect(mobile).toContain('Update Orca Mobile') - expect(mobile).toContain( - 'This desktop needs a newer Orca Mobile app. Update Orca Mobile from the App Store, then try this host again.' - ) - expect(mobile).toContain('Open App Store') - act(() => renderer?.unmount()) - - const desktop = render({ - kind: 'blocked', - reason: 'desktop-too-old', - desktopVersion: 0, - requiredDesktopVersion: 2 - }) - expect(desktop).toContain('Update Orca on your computer') - expect(desktop).toContain( - 'This paired desktop app is too old for your current Orca Mobile app. Update Orca on your computer, then try this host again.' - ) - expect(desktop).toContain('Open GitHub Releases') - }) - it('tells a host without a bundle, or without this screen, the desktop is too old', () => { for (const reason of ['bundle-unavailable', 'page-route-unlisted'] as const) { const output = render({ kind: 'blocked', reason }) @@ -112,12 +84,6 @@ describe('ProtocolBlockScreen', () => { } }) - it('sends a screen that needs a grant this app lacks to the mobile update', () => { - const output = render({ kind: 'blocked', reason: 'page-route-grant-missing' }) - expect(output).toContain('Update Orca Mobile') - expect(output).toContain("This desktop's mobile workspace needs a newer Orca Mobile app.") - }) - it('sends an unknown manifest schema to the mobile update', () => { const output = render({ kind: 'blocked', @@ -169,44 +135,13 @@ describe('ProtocolBlockScreen', () => { expect(primaryActionUrl()).toBe(RELEASES_URL) }) - it('sends a desktop whose page is older than this shell to the desktop update', () => { - const output = render({ - kind: 'blocked', - reason: 'bundle-incompatible', - side: 'desktop', - pageVersion: 0, - requiredPageVersion: 1 - }) - expect(output).toContain('Update Orca on your computer') - expect(output).toContain('This paired desktop app is too old for your current Orca Mobile app') - expect(primaryActionUrl()).toBe(RELEASES_URL) - }) - - it('routes an Android bundle wall to GitHub Releases, not a store that has no listing', () => { - nativeTestState.platform.OS = 'android' - const output = render({ - kind: 'blocked', - reason: 'bundle-shell-too-old', - schemaVersion: 2 - }) - expect(output).toContain('Update Orca Mobile from GitHub Releases') - expect(primaryActionUrl()).toBe(RELEASES_URL) - }) - - it('keeps the update walls on two buttons and the full recovery note', () => { - const output = render({ kind: 'blocked', reason: 'bundle-unavailable' }) - expect(output).toContain('Already updated? Go back to Hosts and refresh the connection.') - // The presence precondition for the absence asserted on the refresh wall above. - expect(pressableCount()).toBe(2) - }) - describe('with the newest release known', () => { const release = { version: '0.0.52', url: 'https://github.com/stablyai/orca/releases/tag/mobile-v0.0.52' } - it.each(['ios', 'android'] as const)('opens that exact release on %s', (os) => { + it.each(['android'] as const)('opens that exact release on %s', (os) => { nativeTestState.platform.OS = os const output = render( { kind: 'blocked', reason: 'bundle-shell-too-old', schemaVersion: 2 }, diff --git a/src/cli/format-recovery.test.ts b/src/cli/format-recovery.test.ts index 7a1bc9b5582..d5fdda58ca5 100644 --- a/src/cli/format-recovery.test.ts +++ b/src/cli/format-recovery.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it, vi } from 'vitest' -import { formatCliError, reportCliError } from './format' -import { RuntimeClientError, RuntimeRpcFailureError } from './runtime-client' +import { reportCliError } from './format' +import { RuntimeRpcFailureError } from './runtime-client' function selectorAmbiguous(): RuntimeRpcFailureError { return new RuntimeRpcFailureError({ @@ -32,29 +32,6 @@ describe('ambiguous selector recovery', () => { }) log.mockRestore() }) - - it('gives only the repair step when two folder workspaces share an id', () => { - const log = vi.spyOn(console, 'log').mockImplementation(() => {}) - const repair = 'Remove one of these folder workspaces in the Orca app and add its folder again.' - - reportCliError( - new RuntimeRpcFailureError({ - id: 'req_selector', - ok: false, - error: { - code: 'selector_ambiguous', - message: 'selector_ambiguous', - data: { selector: 'folder:hub-a', duplicateFolderId: 'hub-a', nextSteps: [repair] } - }, - _meta: { runtimeId: 'runtime_local' } - }), - true, - { commandPath: ['worktree', 'rm'], worktreeSelector: 'folder:hub-a' } - ) - - expect(JSON.parse(String(log.mock.calls[0]?.[0])).error.data.nextSteps).toEqual([repair]) - log.mockRestore() - }) }) describe('worktree selector recovery', () => { @@ -90,36 +67,3 @@ describe('worktree selector recovery', () => { log.mockRestore() }) }) - -describe('CLI error recovery', () => { - it('prefers structured recovery over generic computer hints in text output', () => { - const error = new RuntimeClientError('invalid_argument', 'Unknown flag --forcce', { - nextSteps: ['Did you mean: --force'] - }) - - const output = formatCliError(error, { commandPath: ['computer', 'click'] }) - - expect(output).toContain('Next step: Did you mean: --force') - expect(output).not.toContain('Fix the command flags or RPC params') - }) - - it('preserves the local request-id protection for an RPC runtime-unavailable error', () => { - const error = new RuntimeRpcFailureError({ - id: 'req_rpc_runtime_mutation', - ok: false, - error: { - code: 'runtime_unavailable', - message: 'Re-issue the same RPC command with --retry-request mutation_2.', - data: { - orchestrationRequestId: 'mutation_2', - nextSteps: ['Read the RPC mutation status before retrying.'] - } - }, - _meta: { runtimeId: 'runtime_local' } - }) - - expect(formatCliError(error)).toBe( - 'Re-issue the same RPC command with --retry-request mutation_2.' - ) - }) -}) diff --git a/src/main/acp/history/acp-history-scaling.test.ts b/src/main/acp/history/acp-history-scaling.test.ts deleted file mode 100644 index 12848a2e6ec..00000000000 --- a/src/main/acp/history/acp-history-scaling.test.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { afterEach, expect, it } from 'vitest' - -import '../../../shared/agent-session/journal/agent-session-journal-item-key' -import '../../../shared/agent-session/lifecycle/agent-session-turn-record' -import '../../../shared/agent-session/failures/agent-session-failure' -import '../../../shared/agent-session/failures/agent-session-failure-words' -import { - AGENT, - SESSION, - NAMESPACE, - closeProviderTimelineRigs -} from '../../native-chat/agent-session-timeline/provider-timeline-assembler-test-support' -import '../../native-chat/agent-session-journal/projection/journal-dispatch-doubt-reasons' -import '../../native-chat/agent-session-timeline/provider-timeline-history' -import '../../native-chat/agent-session-wire/termination/testing/structured-agent-session-observed-exit.test-fixture' -import '../../native-chat/agent-session-wire/termination/structured-agent-session-unfinished-work' -import '../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' -import '../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger-test-support' -import './acp-history-catch-up' -import './acp-history-catch-up.test-support' -import { acpHistoryTurns, type AcpLoadHistoryFrame } from './acp-history-replay' -import { translateHistoryTurn, type AcpHistoryScope } from './acp-history-translation' -import './acp-history-turn-matching' -import '../timeline/acp-turn-messages' -import { OPENCODE_ACP_DIALECT } from '../acp-dialects/opencode-dialect' -import '../acp-dialects/grok-dialect' -import '../../native-chat/agent-session-wire/acquisition/structured-agent-session-history-catch-up' -import '../../native-chat/agent-session-journal/writing/journal-lifecycle-mutation' - -afterEach(closeProviderTimelineRigs) -const scope: AcpHistoryScope = { - agent: AGENT, - sessionId: SESSION, - providerSessionId: NAMESPACE, - identity: 'user-message-id', - dialect: OPENCODE_ACP_DIALECT -} -const frame = ( - update: Record, - meta?: Record -): AcpLoadHistoryFrame => ({ - method: 'session/update', - params: { sessionId: NAMESPACE, update, ...(meta ? { _meta: meta } : {}) } -}) -const user = (id: string, text: string) => - frame({ sessionUpdate: 'user_message_chunk', messageId: id, content: { type: 'text', text } }) - -it('imports a whole historical turn with more than 128 still-open message segments', () => { - const frames = [user('long-turn', 'Review the repository')] - for (let index = 0; index < 160; index += 1) { - frames.push( - frame({ - sessionUpdate: 'agent_message_chunk', - messageId: `answer-${index}`, - content: { type: 'text', text: 'Retained segment' } - }) - ) - } - frames.push( - frame({ - sessionUpdate: 'agent_message_chunk', - messageId: 'answer-0', - content: { type: 'text', text: ' and its late tail' } - }) - ) - const turn = acpHistoryTurns(frames, scope.identity)[0] - if (!turn || turn.turn === null) { - throw new Error('Missing identified historical turn') - } - const items = translateHistoryTurn(scope, { ...turn, turn: turn.turn }) - const answers = items.flatMap((item) => - item.body.kind === 'message' && item.body.role === 'assistant' ? [item.body] : [] - ) - expect(answers).toHaveLength(160) - expect(answers[0]).toMatchObject({ - blocks: [{ type: 'text', text: 'Retained segment and its late tail' }] - }) -}) diff --git a/src/main/agent-launch/agent-launch-mode-settings.test.ts b/src/main/agent-launch/agent-launch-mode-settings.test.ts index 17b550d5ce8..4090023ed0e 100644 --- a/src/main/agent-launch/agent-launch-mode-settings.test.ts +++ b/src/main/agent-launch/agent-launch-mode-settings.test.ts @@ -9,7 +9,7 @@ import { decideAgentLaunchMode, readAgentLaunchModeSettings } from './agent-laun vi.mock('electron', () => ({ app: { getPath: () => '/orca-state', isPackaged: true } })) describe('host launch preference ownership', () => { - it.each([false, true])('reads saved choices privately with Agents disabled=%s', (disabled) => { + it.each([true])('reads saved choices privately with Agents disabled=%s', (disabled) => { const settings: ReturnType = { ...getDefaultSettings('/tmp'), agentDefaultSurface: 'chat', diff --git a/src/main/agent-launch/trust/agent-workspace-trust.test.ts b/src/main/agent-launch/trust/agent-workspace-trust.test.ts index b60ce51582d..8af5154ff77 100644 --- a/src/main/agent-launch/trust/agent-workspace-trust.test.ts +++ b/src/main/agent-launch/trust/agent-workspace-trust.test.ts @@ -56,12 +56,6 @@ vi.mock('../../claude/claude-folder-trust-file', async (importOriginal) => { import { applyAgentWorkspaceTrust, type AgentTrustLaunchContext } from './agent-workspace-trust' import { applyAgentWorkspaceTrustToSpawn } from './agent-workspace-trust-spawn' import { clearWslHomeCache, rememberWslHome } from '../../wsl/discovery/wsl-home-cache' -import { - AGENT_TRUST_WRITE_DEADLINE_MS, - SHORT_AGENT_TRUST_WRITE_DEADLINE_MS -} from './agent-trust-write-deadline' - -const WORKSPACE = '/workspace/app' const local: AgentTrustLaunchContext = { env: {}, claudeAuth: null, @@ -69,70 +63,10 @@ const local: AgentTrustLaunchContext = { connectionId: null } -function pending(): { promise: Promise; release: () => void } { - let release!: () => void - const promise = new Promise((resolve) => { - release = resolve - }) - return { promise, release } -} - beforeEach(() => { vi.clearAllMocks() }) -afterEach(() => { - vi.useRealTimers() -}) - -describe('applyAgentWorkspaceTrust on this machine', () => { - it('gives only Codex the long deadline its shared config lane needs', async () => { - vi.useFakeTimers() - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - const codexWrite = pending() - mocks.codex.mockReturnValueOnce(codexWrite.promise) - let codexSettled = false - const codex = applyAgentWorkspaceTrust('codex', WORKSPACE, local).then(() => { - codexSettled = true - }) - await vi.advanceTimersByTimeAsync(SHORT_AGENT_TRUST_WRITE_DEADLINE_MS + 1) - expect(codexSettled).toBe(false) - await vi.advanceTimersByTimeAsync(AGENT_TRUST_WRITE_DEADLINE_MS) - await codex - expect(codexSettled).toBe(true) - codexWrite.release() - warn.mockRestore() - }) -}) - -describe('applyAgentWorkspaceTrust in a WSL guest', () => { - const guestHome = '\\\\wsl.localhost\\Ubuntu\\home\\u' - - afterEach(() => { - clearWslHomeCache() - }) - - it('writes Antigravity trust under the distro home, keyed by the guest path', async () => { - rememberWslHome('Ubuntu', guestHome) - await applyAgentWorkspaceTrust('antigravity', `${guestHome}\\repo`, local) - await applyAgentWorkspaceTrust('antigravity', 'C:\\src\\app', { - ...local, - wslDistro: 'ubuntu' - }) - await applyAgentWorkspaceTrust('cursor', `${guestHome}\\repo`, local) - expect(mocks.antigravity.mock.calls).toEqual([ - [`${guestHome}\\repo`, guestHome, '/home/u/repo'], - ['C:\\src\\app', guestHome, '/mnt/c/src/app'] - ]) - expect(mocks.cursor).not.toHaveBeenCalled() - }) - - it('leaves Antigravity to ask when the distro home is not known yet', async () => { - await applyAgentWorkspaceTrust('antigravity', `${guestHome}\\repo`, local) - expect(mocks.antigravity).not.toHaveBeenCalled() - }) -}) - describe('applyAgentWorkspaceTrust for Claude', () => { let root: string @@ -266,26 +200,4 @@ describe('applyAgentWorkspaceTrust for Claude', () => { projects: { [root]: { hasTrustDialogAccepted: true } } }) }) - - it('gives a local Claude write a short budget, after which Claude asks', async () => { - vi.useFakeTimers() - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - const grant = pending() - mocks.claudeGrant.mockReturnValueOnce(grant.promise.then(() => 'granted' as const)) - let settled = false - const claude = applyAgentWorkspaceTrust('claude', root, { - ...local, - env: { CLAUDE_CONFIG_DIR: root } - }).then(() => { - settled = true - }) - await vi.advanceTimersByTimeAsync(SHORT_AGENT_TRUST_WRITE_DEADLINE_MS - 1) - expect(settled).toBe(false) - await vi.advanceTimersByTimeAsync(2) - await claude - expect(settled).toBe(true) - expect(String(warn.mock.calls[0]?.[0])).toContain('did not settle') - grant.release() - warn.mockRestore() - }) }) diff --git a/src/main/ai-vault-search/session-search-agent-compatibility.test.ts b/src/main/ai-vault-search/session-search-agent-compatibility.test.ts index c1454e40054..be0f6440b17 100644 --- a/src/main/ai-vault-search/session-search-agent-compatibility.test.ts +++ b/src/main/ai-vault-search/session-search-agent-compatibility.test.ts @@ -15,7 +15,7 @@ import { afterEach(() => setSessionSearchService(null)) -test.each(['runtime', 'relay'] as const)( +test.each(['relay'] as const)( 'keeps an empty decoder catalog authoritative over both history flags on %s', async (transport) => { const service = fakeSearchService() @@ -40,7 +40,7 @@ test.each(['runtime', 'relay'] as const)( } ) -test.each(['runtime', 'relay'] as const)( +test.each(['relay'] as const)( 'keeps an unsupported-only filter narrow through normal service checks on %s', async (transport) => { const request = { diff --git a/src/main/ai-vault-search/session-search-deleted-sources.test.ts b/src/main/ai-vault-search/session-search-deleted-sources.test.ts index 77e37e041a1..7296d1caee2 100644 --- a/src/main/ai-vault-search/session-search-deleted-sources.test.ts +++ b/src/main/ai-vault-search/session-search-deleted-sources.test.ts @@ -52,19 +52,6 @@ function retire( }) } -it('retires a deleted file the moment its own directory lists without it', async () => { - const kept = join(harness.claudeProjectDir, 'kept.jsonl') - await mkdir(harness.claudeProjectDir, { recursive: true }) - await writeFile(kept, '{}') - const deleted = join(harness.claudeProjectDir, 'deleted.jsonl') - - const result = await retire([kept, deleted]) - expect(result.retired).toEqual([deleted]) - expect(removed).toEqual([deleted]) - expect(result.unverifiable).toEqual([]) - expect(result.degradedRoots).toEqual([]) -}) - it('judges each merged-root directory on its own', async () => { const current = join(harness.roots.openclawStateDir ?? '', 'agents') const legacy = join(harness.roots.openclawLegacyStateDir ?? '', 'agents') diff --git a/src/main/ai-vault-search/session-search-file-write.test.ts b/src/main/ai-vault-search/session-search-file-write.test.ts index 3892dfdeeb9..5c193720221 100644 --- a/src/main/ai-vault-search/session-search-file-write.test.ts +++ b/src/main/ai-vault-search/session-search-file-write.test.ts @@ -71,21 +71,6 @@ function counts(db: SyncDatabase): Record { } } -it('indexes conversation rows and excludes tool output', () => { - replayTranscriptRead({ - messages: [ - { role: 'user', text: 'alpha question', timestamp: null }, - { role: 'assistant', text: 'beta answer', timestamp: null }, - { role: 'tool', text: 'gamma tool output', timestamp: null } - ] - }) - - expect(counts(index.db).full).toBe(2) - expect(matches(index.db, 'messages_fts', 'gamma')).toBe(0) - expect(matches(index.db, 'messages_fts', '{user_text assistant_text}: gamma')).toBe(0) - expect(matches(index.db, 'messages_fts', '{user_text assistant_text}: beta')).toBe(1) -}) - it('rolls a whole file back when a write throws part way through its transaction', () => { replayTranscriptRead({ messages: userMessages('firstgeneration', 3), diff --git a/src/main/ai-vault-search/session-search-index-consumer.test.ts b/src/main/ai-vault-search/session-search-index-consumer.test.ts index 417e3422c9b..167a7c3e340 100644 --- a/src/main/ai-vault-search/session-search-index-consumer.test.ts +++ b/src/main/ai-vault-search/session-search-index-consumer.test.ts @@ -51,24 +51,3 @@ it('keeps the session list running when the index write fails', async () => { expect(errors.length).toBeGreaterThan(0) expect(owed()?.state).not.toBe('current') }) - -it('leaves a recoverable lagging prefix current when the list cursor has advanced', () => { - replayTranscriptRead({ - messages: userMessages('original', 1), - outcome: { byteOffset: 100, prefixHash: 'known-prefix' } - }) - replayTranscriptRead({ - mode: 'append', - previousByteOffset: 200, - messages: userMessages('lagging', 1), - outcome: { byteOffset: 300 } - }) - expect(owed()).toEqual({ state: 'current', fail_count: 0 }) - expect( - store.resumableCursor({ - agent: 'claude', - codexHome: null, - file: { path: SYNTHETIC_TRANSCRIPT, mtimeMs: 1740000000000, sizeBytes: 4096, modifiedAt: '' } - }) - ).toEqual({ byteOffset: 100, prefixHash: 'known-prefix' }) -}) diff --git a/src/main/ai-vault-search/session-search-index-writer.test.ts b/src/main/ai-vault-search/session-search-index-writer.test.ts index 8fce10ff555..bac55ac7f3c 100644 --- a/src/main/ai-vault-search/session-search-index-writer.test.ts +++ b/src/main/ai-vault-search/session-search-index-writer.test.ts @@ -1,5 +1,4 @@ -import { afterEach, beforeEach, expect, it, vi } from 'vitest' -import { SessionSearchIndexConsumer } from './session-search-index-consumer' +import { afterEach, beforeEach, expect, it } from 'vitest' import { openSessionSearchIndexFile, syntheticCandidate, @@ -23,7 +22,6 @@ beforeEach(async () => { }) afterEach(async () => { - vi.restoreAllMocks() store.close() await index.close() }) @@ -104,34 +102,6 @@ it('refuses to commit a write whose file was removed mid-read', () => { expect(count('files')).toBe(0) }) -it('declines a behind cursor in beginRead before it ever reaches the store', () => { - const attempted: number[] = [] - vi.spyOn(store, 'indexedFile').mockReturnValue({ byteOffset: 100, mtimeMs: 1, sizeBytes: 1 }) - vi.spyOn(store, 'beginWrite').mockImplementation((_candidate, _mode, previousByteOffset) => { - attempted.push(previousByteOffset) - return { add: () => undefined, commit: () => true, discard: () => undefined } - }) - vi.spyOn(store, 'setFileState').mockImplementation(() => undefined) - const consumer = new SessionSearchIndexConsumer(store) - - expect( - consumer.beginRead({ - candidate: syntheticCandidate(), - mode: 'append', - previousByteOffset: 900 - }) - ).toBeNull() - expect(attempted).toEqual([]) - expect( - consumer.beginRead({ - candidate: syntheticCandidate(), - mode: 'append', - previousByteOffset: 100 - }) - ).not.toBeNull() - expect(attempted).toEqual([100]) -}) - it('treats half a recorded identity as no identity at all', () => { const partial = { ...syntheticCandidate({ dev: 7 }), diff --git a/src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts b/src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts deleted file mode 100644 index 866d7c0fe65..00000000000 --- a/src/main/ai-vault-search/session-search-lifecycle-matrix.test.ts +++ /dev/null @@ -1,318 +0,0 @@ -import './session-search-worker.test-fixture' -import { chmod, mkdir, rename, rm } from 'node:fs/promises' -import { delimiter, dirname, join } from 'node:path' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' -import { resetSessionParseCacheForTests } from '../ai-vault/parse-cache/session-parse-cache-store' -import { resetTranscriptConsumersForTests } from '../ai-vault/transcripts/session-transcript-consumers' -import type SyncDatabase from '../sqlite/sync-database' -import { SessionSearchIndexer } from './session-search-indexer' -import type { SessionSearchIndexerOptions } from './session-search-indexer-options' -import { removeSessionSearchDatabase } from './session-search-schema' -import { - FakeSessionSearchClock, - openSessionSearchIndexerHarness, - writeClaudeTranscript, - writeMessageGraphTranscript, - type SessionSearchIndexerHarness -} from './session-search-indexer-test-fixture' - -const CAN_DENY_READ = process.platform !== 'win32' && process.getuid?.() !== 0 -const INTERVAL_MS = 20_000 -const SESSIONS = ['aaaaaaaa', 'bbbbbbbb', 'cccccccc'] - -type RootLayout = { - name: string - detachedRoot: (harness: SessionSearchIndexerHarness) => string - detachedFile: (harness: SessionSearchIndexerHarness, session: string) => string - writeDetached: (path: string, session: string) => Promise - healthyFile: (harness: SessionSearchIndexerHarness, session: string) => string - writeHealthy: (path: string, session: string) => Promise -} - -const OPENCLAW_SESSION_DIR = join('agents', 'main', 'sessions') - -const ROOT_LAYOUTS: RootLayout[] = [ - { - name: 'roots discovery reports one per directory', - detachedRoot: (harness) => harness.roots.claudeProjectsDir ?? '', - detachedFile: (harness, session) => join(harness.claudeProjectDir, `${session}.jsonl`), - writeDetached: (path, session) => - writeClaudeTranscript(path, [`detached ${session}`], fullSessionId(session)), - healthyFile: (harness, session) => join(harness.roots.piSessionsDir ?? '', `${session}.jsonl`), - writeHealthy: (path, session) => writeMessageGraphTranscript(path, [`healthy ${session}`]) - }, - { - name: 'roots a merged discovery joins into one label', - detachedRoot: (harness) => join(harness.roots.openclawStateDir ?? '', 'agents'), - detachedFile: (harness, session) => - join(harness.roots.openclawStateDir ?? '', OPENCLAW_SESSION_DIR, `${session}.jsonl`), - writeDetached: (path, session) => writeMessageGraphTranscript(path, [`detached ${session}`]), - healthyFile: (harness, session) => - join(harness.roots.openclawLegacyStateDir ?? '', OPENCLAW_SESSION_DIR, `${session}.jsonl`), - writeHealthy: (path, session) => writeMessageGraphTranscript(path, [`healthy ${session}`]) - } -] - -type UnreachableMode = { - name: string - needsDeniedRead: boolean - /** Without indexed rows, only a root-level listing failure exposes this mode as degraded. */ - visibleWithNoRows: boolean - detach: (root: string, transcriptDir: string, parked: string) => Promise - attach: (root: string, transcriptDir: string, parked: string) => Promise -} - -const UNREACHABLE_MODES: UnreachableMode[] = [ - { - name: 'the root itself is not there', - needsDeniedRead: false, - visibleWithNoRows: false, - detach: (root, _transcriptDir, parked) => rename(root, parked), - attach: (root, _transcriptDir, parked) => rename(parked, root) - }, - { - name: 'the root refuses to list', - needsDeniedRead: true, - visibleWithNoRows: true, - detach: (root) => chmod(root, 0o000), - attach: (root) => chmod(root, 0o755) - }, - { - name: 'the transcripts sit behind a directory that refuses to list', - needsDeniedRead: true, - visibleWithNoRows: false, - detach: (_root, transcriptDir) => chmod(transcriptDir, 0o000), - attach: (_root, transcriptDir) => chmod(transcriptDir, 0o755) - } -] - -type Operation = { - name: string - clearsIndex?: boolean - /** Healthy-root session IDs to delete before running the operation. */ - deletes?: readonly string[] - options?: Partial - run: (context: MatrixContext) => Promise -} - -const OPERATIONS: Operation[] = [ - { name: 'one cycle', run: (context) => context.cycle() }, - { - name: 'two cycles', - run: async (context) => { - await context.cycle() - await context.cycle() - } - }, - { - name: 'close and restart', - run: (context) => context.reopen() - }, - { - name: 'two full reconciles', - run: async (context) => { - await context.indexer().reconcile({ full: true }) - await context.indexer().reconcile({ full: true }) - } - }, - { - name: 'one healthy transcript deleted', - deletes: SESSIONS.slice(0, 1), - run: (context) => context.cycle() - }, - { - name: 'every healthy transcript deleted', - deletes: SESSIONS, - run: async (context) => { - // The first empty-root observation grants one pass of grace; the second can prove deletion. - await context.indexer().reconcile({ full: true }) - await context.indexer().reconcile({ full: true }) - } - }, - { - name: 'the periodic sweep comes round', - options: { fullSweepEveryCycles: 2 }, - run: async (context) => { - await context.cycle() - await context.cycle() - await context.cycle() - } - }, - { - name: 'every pass out of time at its first file', - options: { passDeadlineMs: 0 }, - run: async (context) => { - await context.cycle() - await context.cycle() - } - }, - { - // Fresh transcripts remain inside the narrower window, so reconstruction must preserve them. - name: 'reconstructed for a narrower history window', - run: (context) => context.reopen({ historyDays: 30 }) - }, - { - name: 'the index thrown away and rebuilt', - clearsIndex: true, - run: (context) => context.reopen({ removeDatabase: true }) - } -] - -type MatrixContext = { - indexer: () => SessionSearchIndexer - reopen: (args?: { historyDays?: number | null; removeDatabase?: boolean }) => Promise - cycle: () => Promise - detachedRoot: string - detachedPaths: string[] -} - -function fullSessionId(prefix: string): string { - return `${prefix}-bbbb-4ccc-8ddd-eeeeeeeeeeee` -} - -let harness: SessionSearchIndexerHarness -let clock: FakeSessionSearchClock -let indexer: SessionSearchIndexer | null - -beforeEach(async () => { - resetSessionParseCacheForTests() - resetTranscriptConsumersForTests() - clock = new FakeSessionSearchClock() - harness = await openSessionSearchIndexerHarness('ss-lifecycle') - indexer = null -}) - -afterEach(async () => { - await indexer?.close() - resetTranscriptConsumersForTests() - resetSessionParseCacheForTests() - await harness.cleanup() -}) - -function open(overrides: Partial = {}): SessionSearchIndexer { - indexer = new SessionSearchIndexer({ - databasePath: harness.databasePath, - roots: harness.roots, - historyDays: null, - clock, - reconcileIntervalMs: INTERVAL_MS, - ...overrides - }) - return indexer -} - -/** Drive deadline-limited passes until session count stops increasing or maxCycles is reached. */ -async function driveUntilIndexed(maxCycles: number): Promise { - let held = indexedSessions().length - for (let cycle = 0; cycle < maxCycles; cycle++) { - clock.advance(INTERVAL_MS) - await indexer?.settled() - const now = indexedSessions().length - if (now === held) { - return - } - held = now - } -} - -function indexedSessions(): string[] { - return harness - .read( - (db: SyncDatabase) => - db.prepare('SELECT session_id AS id FROM sessions').all() as { id: string }[] - ) - .map((row) => row.id) - .sort() -} - -for (const roots of ROOT_LAYOUTS) { - for (const unreachable of UNREACHABLE_MODES) { - describe.skipIf(unreachable.needsDeniedRead && !CAN_DENY_READ)( - `${roots.name}, ${unreachable.name}`, - () => { - for (const operation of OPERATIONS) { - it(operation.name, async () => { - const detachedRoot = roots.detachedRoot(harness) - const detachedPaths = SESSIONS.map((session) => roots.detachedFile(harness, session)) - const healthyPaths = SESSIONS.map((session) => roots.healthyFile(harness, session)) - for (const [index, session] of SESSIONS.entries()) { - await roots.writeDetached(detachedPaths[index] ?? '', session) - await roots.writeHealthy(healthyPaths[index] ?? '', session) - } - const transcriptDir = dirname(detachedPaths[0] ?? '') - const parked = join(harness.root, 'parked-root') - - await open(operation.options).start() - await driveUntilIndexed(SESSIONS.length * 2) - const detachedIds = detachedPaths.map((_path, index) => - roots === ROOT_LAYOUTS[0] - ? fullSessionId(SESSIONS[index] ?? '') - : (SESSIONS[index] ?? '') - ) - const healthyIds = SESSIONS.map((session) => session) - expect(indexedSessions()).toEqual([...detachedIds, ...healthyIds].sort()) - clock.advance(INTERVAL_MS) - await indexer?.settled() - - await unreachable.detach(detachedRoot, transcriptDir, parked) - try { - const kept = SESSIONS.filter((session) => !operation.deletes?.includes(session)) - for (const session of operation.deletes ?? []) { - await rm(healthyPaths[SESSIONS.indexOf(session)] ?? '') - } - await operation.run({ - indexer: () => indexer as SessionSearchIndexer, - reopen: async (args = {}) => { - await indexer?.close() - resetTranscriptConsumersForTests() - resetSessionParseCacheForTests() - if (args.removeDatabase === true) { - removeSessionSearchDatabase(harness.databasePath) - } - const overrides = { ...operation.options } - if ('historyDays' in args) { - overrides.historyDays = args.historyDays - } - await open(overrides).start() - await driveUntilIndexed(SESSIONS.length * 2) - }, - cycle: async () => { - clock.advance(INTERVAL_MS) - await indexer?.settled() - }, - detachedRoot, - detachedPaths - }) - - const survivingDetached = operation.clearsIndex ? [] : detachedIds - expect(indexedSessions()).toEqual([...survivingDetached, ...kept].sort()) - - const status = indexer?.status() - const degraded = status?.degradedRoots.map((root) => root.root) ?? [] - // After clearing the index, missing roots and unreadable subdirectories leave no evidence of degradation. - if (operation.clearsIndex && !unreachable.visibleWithNoRows) { - expect(degraded).not.toContain(detachedRoot) - } else { - expect(degraded).toContain(detachedRoot) - expect(degraded.every((root) => !root.includes(delimiter))).toBe(true) - expect(status?.phase).not.toBe('current') - } - } finally { - await unreachable.attach(detachedRoot, transcriptDir, parked) - } - - await mkdir(dirname(healthyPaths[0] ?? ''), { recursive: true }) - await indexer?.reconcile({ full: true }) - await driveUntilIndexed(SESSIONS.length * 2) - expect(indexedSessions()).toEqual( - [ - ...detachedIds, - ...SESSIONS.filter((session) => !operation.deletes?.includes(session)) - ].sort() - ) - }) - } - } - ) - } -} diff --git a/src/main/ai-vault-search/session-search-message-rows.test.ts b/src/main/ai-vault-search/session-search-message-rows.test.ts index a33de16385b..dced66242dc 100644 --- a/src/main/ai-vault-search/session-search-message-rows.test.ts +++ b/src/main/ai-vault-search/session-search-message-rows.test.ts @@ -3,27 +3,6 @@ import { expect, it } from 'vitest' import { insertSearchMessage, searchMessageRows } from './session-search-message-rows' import { openSessionSearchIndexFile } from './session-search-index-test-fixture' -it('cuts at whitespace rather than through the word on the boundary', async () => { - const index = await openSessionSearchIndexFile('ss-rows-whitespace') - try { - // Place the row limit inside the search term to detect a split that makes it unsearchable. - const text = `${' '.repeat(7997)}pericardium` - const chunks = [...searchMessageRows([{ role: 'user', text, timestamp: null }])] - expect(chunks.map((row) => row.text).join('')).toBe(text) - for (const row of chunks) { - insertSearchMessage(index.db, 1, row) - } - - expect( - index.db - .prepare('SELECT count(*) AS n FROM messages_fts WHERE messages_fts MATCH ?') - .get('pericardium') - ).toEqual({ n: 1 }) - } finally { - await index.close() - } -}) - it.each(['/repo/pericardium.ts', 'PROJ-12345', 'C++', 'cafe\u0301ine'])( 'preserves the exact FTS token %s at a chunk boundary', async (token) => { diff --git a/src/main/ai-vault-search/session-search-native-chat-indexing.test.ts b/src/main/ai-vault-search/session-search-native-chat-indexing.test.ts deleted file mode 100644 index c7eda7f3e66..00000000000 --- a/src/main/ai-vault-search/session-search-native-chat-indexing.test.ts +++ /dev/null @@ -1,106 +0,0 @@ -import './session-search-worker.test-fixture' -import { appendFile, mkdir, writeFile } from 'node:fs/promises' -import { join } from 'node:path' -import { afterEach, beforeEach, expect, it } from 'vitest' -import { resetSessionParseCacheForTests } from '../ai-vault/parse-cache/session-parse-cache-store' -import { resetTranscriptConsumersForTests } from '../ai-vault/transcripts/session-transcript-consumers' -import type SyncDatabase from '../sqlite/sync-database' -import { SessionSearchIndexer } from './session-search-indexer' -import { - FakeSessionSearchClock, - openSessionSearchIndexerHarness, - type SessionSearchIndexerHarness -} from './session-search-indexer-test-fixture' - -const INTERVAL_MS = 20_000 -const SESSION_ID = 'cccccccc-dddd-4eee-8fff-000000000000' -const CWD = '/repo/orca' - -let harness: SessionSearchIndexerHarness -let clock: FakeSessionSearchClock -let indexer: SessionSearchIndexer - -beforeEach(async () => { - resetSessionParseCacheForTests() - resetTranscriptConsumersForTests() - clock = new FakeSessionSearchClock() - harness = await openSessionSearchIndexerHarness('ss-native-chat') -}) - -afterEach(async () => { - await indexer.close() - resetTranscriptConsumersForTests() - resetSessionParseCacheForTests() - await harness.cleanup() -}) - -function nativeChatTurn(uuid: string, role: 'user' | 'assistant', text: string): string { - const timestamp = new Date(1_740_000_000_000 + Number(uuid.slice(-2)) * 60_000).toISOString() - return JSON.stringify({ - type: role, - uuid, - sessionId: SESSION_ID, - timestamp, - cwd: CWD, - gitBranch: 'main', - message: { - role, - ...(role === 'assistant' ? { model: 'claude-fable-5' } : {}), - content: [{ type: 'text', text }] - } - }) -} - -function messageTexts(term: string): { role: string; session: string }[] { - return harness.read( - (db: SyncDatabase) => - db - .prepare( - `SELECT m.role AS role, s.session_id AS session FROM messages_fts - JOIN messages m ON m.id = messages_fts.rowid - JOIN sessions s ON s.id = m.session_row_id - WHERE messages_fts MATCH ? ORDER BY m.id` - ) - .all(term) as { role: string; session: string }[] - ) -} - -it('indexes a native-chat conversation and its later turns with no panel and no service', async () => { - const path = join(harness.claudeProjectDir, `${SESSION_ID}.jsonl`) - await mkdir(harness.claudeProjectDir, { recursive: true }) - await writeFile( - path, - `${[ - nativeChatTurn('turn-01', 'user', 'why does the relay drop the lease at 105 seconds'), - nativeChatTurn('turn-02', 'assistant', 'that is the client silence watchdog, not a cliff') - ].join('\n')}\n` - ) - - indexer = new SessionSearchIndexer({ - databasePath: harness.databasePath, - roots: harness.roots, - historyDays: null, - clock, - reconcileIntervalMs: INTERVAL_MS - }) - await indexer.start() - - expect(messageTexts('watchdog')).toEqual([{ role: 'assistant', session: SESSION_ID }]) - expect(harness.read((db: SyncDatabase) => db.prepare('SELECT cwd FROM sessions').get())).toEqual({ - cwd: CWD - }) - - await appendFile( - path, - `${[ - nativeChatTurn('turn-03', 'user', 'and the fleetwide 4408 bursts'), - nativeChatTurn('turn-04', 'assistant', 'those are desktop lease rotations, cohort waves') - ].join('\n')}\n` - ) - clock.advance(INTERVAL_MS) - await indexer.settled() - - expect(messageTexts('cohort')).toEqual([{ role: 'assistant', session: SESSION_ID }]) - expect(messageTexts('4408')).toEqual([{ role: 'user', session: SESSION_ID }]) - expect(indexer.status().phase).toBe('current') -}) diff --git a/src/main/ai-vault-search/session-search-posting-window.test.ts b/src/main/ai-vault-search/session-search-posting-window.test.ts index eb8105429b1..6585f1514a1 100644 --- a/src/main/ai-vault-search/session-search-posting-window.test.ts +++ b/src/main/ai-vault-search/session-search-posting-window.test.ts @@ -149,22 +149,3 @@ it('keeps original hits and truncation when the shared vocabulary seek allowance await index.close() } }) - -it('seeks past a whole vocabulary term while retaining its extensions', async () => { - const index = await openSessionSearchHarness('ss-posting-vocabulary-successor') - try { - addSyntheticSession(index.db, { id: 1, text: 'coalesce coalesces coalescing', rows: 2 }) - const seek = index.db.prepare( - 'SELECT term FROM temp.session_search_vocab WHERE term >= ? ORDER BY term LIMIT 1' - ) - expect(seek.get('coal')).toEqual({ term: 'coalesce' }) - expect(seek.get('coalesce\u0000')).toEqual({ term: 'coalesces' }) - expect(seek.get('coalesces\u0000')).toEqual({ term: 'coalescing' }) - expect(seek.get('coalescing\u0000')).toBeUndefined() - const result = index.engine.search({ query: 'coalescs' }) - expect(result.planner.repairedTerms).toEqual(['coalesces']) - expect(result.hits.map((hit) => hit.sessionId)).toEqual(['1']) - } finally { - await index.close() - } -}) diff --git a/src/main/ai-vault-search/session-search-service-registry.test.ts b/src/main/ai-vault-search/session-search-service-registry.test.ts index a96044d83f8..f5abb60a6b3 100644 --- a/src/main/ai-vault-search/session-search-service-registry.test.ts +++ b/src/main/ai-vault-search/session-search-service-registry.test.ts @@ -1,32 +1,18 @@ -import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterEach, describe, expect, it } from 'vitest' import { fakeSearchService } from '../../shared/ai-vault/search/ai-vault-search-test-fixture' import { unavailableSessionSearchStatus } from '../../shared/ai-vault/search/ai-vault-search-client' import { setSessionSearchService, - searchSessionService, sessionSearchServiceStatus } from './session-search-service-registry' afterEach(() => { setSessionSearchService(null) - vi.useRealTimers() }) describe('session search service registry', () => { - it('answers without constructing an indexer and validates even when unavailable', async () => { - expect(await searchSessionService({ query: 'needle' }, 'ipc')).toEqual({ - kind: 'unavailable', - reason: 'no-service' - }) - expect(await sessionSearchServiceStatus({}, 'ipc')).toMatchObject({ - enabled: false, - phase: 'idle', - generation: 0 - }) - await expect(searchSessionService({ query: 42 }, 'ipc')).rejects.toThrow() - }) - it.each(['ipc', 'runtime', 'relay'] as const)( + it.each(['ipc', 'relay'] as const)( 'withholds degraded-root paths from %s status per the exposure policy', async (transport) => { const service = fakeSearchService() @@ -44,52 +30,4 @@ describe('session search service registry', () => { ]) } ) - it('searches indexed data by default, drops legacy options and suppresses unsolicited diagnostics', async () => { - const service = fakeSearchService() - setSessionSearchService(service) - const result = await searchSessionService( - { query: 'needle', tier: 'conversation', refresh: true }, - 'ipc' - ) - expect(service.reconcile).not.toHaveBeenCalled() - expect(service.search).toHaveBeenCalledWith( - { - query: 'needle', - limit: 20 - }, - undefined - ) - expect(result).not.toHaveProperty('debug') - expect(await searchSessionService({ query: 'needle', debug: true }, 'ipc')).toHaveProperty( - 'debug' - ) - }) - - it('searches after the default five-second bound and observes a late rejection', async () => { - vi.useFakeTimers() - const service = fakeSearchService() - let reject!: (error: Error) => void - service.reconcile.mockImplementation( - () => - new Promise((_resolve, rejectPromise) => { - reject = rejectPromise - }) - ) - setSessionSearchService(service) - const result = searchSessionService( - { query: 'needle', freshness: 'wait-until-current' }, - 'relay' - ) - await vi.advanceTimersByTimeAsync(4_999) - expect(service.search).not.toHaveBeenCalled() - await vi.advanceTimersByTimeAsync(1) - expect(await result).toMatchObject({ - kind: 'results', - truncated: { freshness: true }, - hits: [expect.objectContaining({ source: { presence: 'present' } })] - }) - reject(new Error('late failure')) - await Promise.resolve() - expect(vi.getTimerCount()).toBe(0) - }) }) diff --git a/src/main/ai-vault-search/session-search-service.test.ts b/src/main/ai-vault-search/session-search-service.test.ts index e4895fb99e1..f7bc3801439 100644 --- a/src/main/ai-vault-search/session-search-service.test.ts +++ b/src/main/ai-vault-search/session-search-service.test.ts @@ -90,20 +90,4 @@ describe('real index to public service adapter', () => { expect(AiVaultSearchStatusSchema.parse(status)).toEqual(status) expect(status.generation).toBeGreaterThan(0) }) - it('keeps tool rows out of both scopes and reports query truncation', async () => { - const { db, service } = await fixture() - addSyntheticSession(db, { id: 1, role: 'tool', text: 'needle' }) - expect(await service.search({ query: 'needle', scope: 'conversation' })).toMatchObject({ - kind: 'results', - hits: [] - }) - expect(await service.search({ query: 'needle' })).toMatchObject({ - kind: 'results', - hits: [] - }) - expect(await service.search({ query: 'needle '.repeat(100) })).toMatchObject({ - kind: 'results', - truncated: { query: true } - }) - }) }) diff --git a/src/main/ai-vault-search/session-search-synthetic-corpus.test.ts b/src/main/ai-vault-search/session-search-synthetic-corpus.test.ts deleted file mode 100644 index a1ff461b6e5..00000000000 --- a/src/main/ai-vault-search/session-search-synthetic-corpus.test.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { rm } from 'node:fs/promises' -import { join } from 'node:path' -import { expect, it } from 'vitest' -import { resetSessionParseCacheForTests } from '../ai-vault/parse-cache/session-parse-cache-store' -import { resetTranscriptConsumersForTests } from '../ai-vault/transcripts/session-transcript-consumers' -import { registerSessionSearchIndexConsumer } from './session-search-index-consumer' -import { SessionSearchStore } from './session-search-store' -import { writeSyntheticTranscriptCorpus } from './session-search-synthetic-corpus' -import { parseTranscript } from './session-search-transcript-fixtures' - -it.each([Infinity, -Infinity, Number.NaN, -1, 1.5])( - 'rejects invalid corpus loop bounds: %s', - async (value) => { - for (const field of ['sessions', 'turnsPerSession', 'toolResultWords']) { - await expect(writeSyntheticTranscriptCorpus({ [field]: value })).rejects.toThrow(RangeError) - } - } -) - -it.each([0, 200, 2000])( - 'counts the indexed messages with %s tool words', - async (toolResultWords) => { - const corpus = await writeSyntheticTranscriptCorpus({ - sessions: 1, - turnsPerSession: 1, - toolResultWords - }) - const store = new SessionSearchStore(join(corpus.root, 'index.sqlite')) - const unregister = registerSessionSearchIndexConsumer(store) - try { - await parseTranscript(corpus.files[0]!) - expect(corpus.messageCount).toBe(toolResultWords === 0 ? 3 : 4) - expect(store.connection.prepare('SELECT count(*) AS n FROM messages').get()).toEqual({ - n: 2 - }) - } finally { - unregister() - resetTranscriptConsumersForTests() - resetSessionParseCacheForTests() - store.close() - await rm(corpus.root, { recursive: true, force: true }) - } - } -) diff --git a/src/main/ai-vault-search/session-search-tool-purge.test.ts b/src/main/ai-vault-search/session-search-tool-purge.test.ts index 2ef3ea2bb62..2bee7b4b6a9 100644 --- a/src/main/ai-vault-search/session-search-tool-purge.test.ts +++ b/src/main/ai-vault-search/session-search-tool-purge.test.ts @@ -2,7 +2,7 @@ import { expect, it } from 'vitest' import { addSyntheticSession, openSessionSearchHarness } from './session-search-engine-test-fixture' import { readIndexContentRevision, readIndexGeneration } from './session-search-index-generation' import { encodeSessionSearchCursor, sessionSearchPageKey } from './session-search-page-cursor' -import { SessionSearchToolPurge, readSessionSearchToolPurgePage } from './session-search-tool-purge' +import { SessionSearchToolPurge } from './session-search-tool-purge' it('purges legacy tool rows across batches while preserving conversations and raw generation fences', async () => { const index = await openSessionSearchHarness('ss-tool-purge') @@ -37,25 +37,6 @@ it('purges legacy tool rows across batches while preserving conversations and ra } }) -it('bounds empty conversation pages and seeks across sparse rowid gaps', async () => { - const index = await openSessionSearchHarness('ss-tool-purge-sparse') - try { - addSyntheticSession(index.db, { id: 1, text: 'kept', rows: 300 }) - addSyntheticSession(index.db, { id: 2, text: 'legacytool', role: 'tool' }) - index.db.exec('UPDATE messages SET id = 1000000 WHERE id = 301') - const first = readSessionSearchToolPurgePage(index.db, 0) - expect(first).toEqual({ cursor: 256, ids: [] }) - expect(readSessionSearchToolPurgePage(index.db, 256)).toEqual({ cursor: 512, ids: [] }) - expect(readSessionSearchToolPurgePage(index.db, 512)).toEqual({ - cursor: 1000255, - ids: [{ id: 1000000 }] - }) - expect(readSessionSearchToolPurgePage(index.db, 1000255)).toBeNull() - } finally { - await index.close() - } -}) - it.each(['relevance', 'newest'] as const)( 'continues frozen %s ordering and fork folding throughout tool cleanup', async (sort) => { diff --git a/src/main/ai-vault/ai-vault-scan-progress.test.ts b/src/main/ai-vault/ai-vault-scan-progress.test.ts deleted file mode 100644 index fbc20f97b4a..00000000000 --- a/src/main/ai-vault/ai-vault-scan-progress.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, describe, expect, it, vi } from 'vitest' -import type { AiVaultScanWork } from '../../shared/ai-vault/scanning/ai-vault-scan-progress' -import { - AI_VAULT_SCAN_PROGRESS_INTERVAL_MS, - trackAiVaultScanProgress -} from './ai-vault-scan-progress' -import { scanAiVaultSessions } from './session-scanner' -import { resetSessionParseCacheForTests } from './parse-cache/session-parse-cache-store' -import { isolatedScanRoots, writeJsonlFile } from './session-scanner-test-fixtures' - -let tempRoots: string[] = [] - -afterEach(async () => { - vi.useRealTimers() - resetSessionParseCacheForTests() - await Promise.all(tempRoots.map((root) => rm(root, { recursive: true, force: true }))) - tempRoots = [] -}) - -async function tempRoot(): Promise { - const root = await mkdtemp(join(tmpdir(), 'orca-scan-progress-')) - tempRoots.push(root) - return root -} - -describe('scan progress reporting', () => { - it('reports discovery and reading for its own scan, never for an unrelated one', async () => { - const root = await tempRoot() - const roots = isolatedScanRoots(root) - for (const name of ['a', 'b', 'c']) { - await writeJsonlFile(join(roots.claudeProjectsDir, 'project', `${name}.jsonl`), [ - { - type: 'user', - sessionId: name, - timestamp: '2026-05-01T10:00:00.000Z', - cwd: '/tmp/claude', - message: { role: 'user', content: `ask ${name}` } - } - ]) - } - const reports: AiVaultScanWork[] = [] - const tracked = new AbortController() - let now = 0 - trackAiVaultScanProgress( - tracked.signal, - (work) => reports.push(work), - () => (now += AI_VAULT_SCAN_PROGRESS_INTERVAL_MS) - ) - - await scanAiVaultSessions({ - ...roots, - platform: 'darwin', - limit: 20, - signal: new AbortController().signal - }) - expect(reports).toEqual([]) - - resetSessionParseCacheForTests() - const result = await scanAiVaultSessions({ - ...roots, - platform: 'darwin', - limit: 20, - signal: tracked.signal - }) - expect(result.sessions).toHaveLength(3) - expect(reports.some((work) => work.stage === 'finding')).toBe(true) - expect(reports.at(-1)).toMatchObject({ stage: 'reading', read: { done: 3, total: 3 } }) - }) -}) diff --git a/src/main/ai-vault/scan-service/session-scanner-service-cancellation.test.ts b/src/main/ai-vault/scan-service/session-scanner-service-cancellation.test.ts deleted file mode 100644 index 90822535432..00000000000 --- a/src/main/ai-vault/scan-service/session-scanner-service-cancellation.test.ts +++ /dev/null @@ -1,141 +0,0 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import type { - AiVaultServiceChildMessage, - AiVaultServiceParentMessage -} from './session-scanner-service-protocol' -import { AI_VAULT_SERVICE_PROTOCOL_VERSION } from './session-scanner-service-protocol' - -const scanAiVaultSessions = vi.hoisted(() => vi.fn()) -const flushSessionParseCachePersist = vi.hoisted(() => vi.fn(async () => undefined)) -const closeSearch = vi.hoisted(() => vi.fn()) - -vi.mock('../session-scanner', () => ({ scanAiVaultSessions })) -vi.mock('../parse-cache/session-parse-cache-store', async (importOriginal) => ({ - ...(await importOriginal()), - invalidateSessionParseCacheEntry: vi.fn() -})) -vi.mock('../parse-cache/session-parse-cache-persistence', () => ({ - flushSessionParseCachePersist, - initSessionParseCachePersistence: vi.fn() -})) -vi.mock('./session-scanner-service-search', () => ({ - SessionScannerServiceSearch: class { - handles(): boolean { - return false - } - close(): void { - closeSearch() - } - } -})) - -const result = { sessions: [], issues: [], scannedAt: '2026-09-15' } -const sent: AiVaultServiceChildMessage[] = [] -const disconnect = vi.fn() -let restoreProcess = (): void => undefined - -function emit(message: AiVaultServiceParentMessage): void { - process.emit('message', message, undefined) -} - -function scan(id: number): void { - emit({ type: 'request', id, operation: 'scan', options: {} }) -} - -function settle(): Promise { - return new Promise((resolve) => setImmediate(resolve)) -} - -function cancelAndObserveSet(id: number): Set { - const add = vi.spyOn(Set.prototype, 'add') - try { - emit({ type: 'cancel', id }) - const index = add.mock.calls.findIndex(([value]) => value === id) - const retained = add.mock.contexts[index] - if (!(retained instanceof Set)) { - throw new Error('Expected an admitted cancellation.') - } - return retained - } finally { - add.mockRestore() - } -} - -beforeEach(async () => { - vi.resetModules() - vi.clearAllMocks() - sent.length = 0 - scanAiVaultSessions.mockResolvedValue(result) - const sendDescriptor = Object.getOwnPropertyDescriptor(process, 'send') - const disconnectDescriptor = Object.getOwnPropertyDescriptor(process, 'disconnect') - const messageListeners = new Set(process.listeners('message')) - const disconnectListeners = new Set(process.listeners('disconnect')) - Object.defineProperty(process, 'send', { - configurable: true, - value: (message: AiVaultServiceChildMessage) => { - sent.push(message) - return true - } - }) - Object.defineProperty(process, 'disconnect', { configurable: true, value: disconnect }) - restoreProcess = () => { - for (const listener of process.listeners('message')) { - if (!messageListeners.has(listener)) { - process.removeListener('message', listener) - } - } - for (const listener of process.listeners('disconnect')) { - if (!disconnectListeners.has(listener)) { - process.removeListener('disconnect', listener) - } - } - if (sendDescriptor) { - Object.defineProperty(process, 'send', sendDescriptor) - } else { - Reflect.deleteProperty(process, 'send') - } - if (disconnectDescriptor) { - Object.defineProperty(process, 'disconnect', disconnectDescriptor) - } else { - Reflect.deleteProperty(process, 'disconnect') - } - } - await import('./session-scanner-service-entry') - emit({ - type: 'init', - protocol: AI_VAULT_SERVICE_PROTOCOL_VERSION, - sessionParseCache: null, - sessionSearch: null - }) -}) - -afterEach(async () => { - emit({ type: 'shutdown' }) - await settle() - restoreProcess() -}) - -describe('AI Vault service cancellation ownership', () => { - it('cancels running and queued requests and releases both IDs when they settle', async () => { - const first = Promise.withResolvers() - const signals: AbortSignal[] = [] - scanAiVaultSessions.mockImplementation(({ signal }: { signal: AbortSignal }) => { - signals.push(signal) - return signals.length === 1 ? first.promise : Promise.resolve(result) - }) - scan(1) - scan(2) - await settle() - const cancelled = cancelAndObserveSet(1) - emit({ type: 'cancel', id: 2 }) - expect(signals).toHaveLength(1) - expect(signals[0]?.aborted).toBe(true) - expect(cancelled.size).toBe(2) - first.resolve(result) - await settle() - expect(signals).toHaveLength(2) - expect(signals[1]?.aborted).toBe(true) - expect(cancelled.size).toBe(0) - expect(sent.filter((message) => message.type === 'result')).toHaveLength(2) - }) -}) diff --git a/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts b/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts index fec29a49870..0ab2312a470 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-entry.test.ts @@ -25,15 +25,6 @@ function emit(message: unknown): void { process.emit('message', message as never, undefined as never) } -async function runScan(id: number): Promise { - emit({ type: 'request', id, operation: 'scan', options: {} }) - await vi.waitFor(() => expect(sent.some((message) => message.id === id)).toBe(true)) -} - -function invalidationsFor(path: string): number { - return invalidateSessionParseCacheEntry.mock.calls.filter((call) => call[0] === path).length -} - describe('AI Vault service entry cache invalidation', () => { beforeAll(async () => { process.send = ((message: { type: string; id?: number }) => { @@ -50,33 +41,6 @@ describe('AI Vault service entry cache invalidation', () => { scanAiVaultSessions.mockResolvedValue({ sessions: [], issues: [], scannedAt: '2026-08-10' }) }) - it('keeps re-applying while another request is still executing', async () => { - let releaseSlowScan: (() => void) | undefined - scanAiVaultSessions.mockImplementationOnce( - () => - new Promise((resolve) => { - releaseSlowScan = () => - resolve({ sessions: [], issues: [], scannedAt: '2026-08-10' } as never) - }) - ) - // Subagents can overlap scans on the interactive lane; titles share the scan lane. - emit({ type: 'request', id: 10, operation: 'scan', options: {} }) - await vi.waitFor(() => expect(releaseSlowScan).toBeDefined()) - - emit({ type: 'invalidate', generation: 2, paths: ['/transcripts/b.jsonl'] }) - emit({ type: 'request', id: 11, operation: 'subagents', request: {} }) - await vi.waitFor(() => expect(sent.some((message) => message.id === 11)).toBe(true)) - - expect(invalidationsFor('/transcripts/b.jsonl')).toBe(2) - - releaseSlowScan?.() - await vi.waitFor(() => expect(sent.some((message) => message.id === 10)).toBe(true)) - expect(invalidationsFor('/transcripts/b.jsonl')).toBe(3) - - await runScan(12) - expect(invalidationsFor('/transcripts/b.jsonl')).toBe(3) - }) - it('sends rate-limited progress under the running scan id, and none once it settles', async () => { let now = 0 const clock = vi.spyOn(performance, 'now').mockImplementation(() => now) diff --git a/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts b/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts index 098c43e1369..a9b83f01c98 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-search.test.ts @@ -110,20 +110,6 @@ afterAll(async () => { await harness.cleanup() }) -it('answers a search and a reconcile over the protocol', async () => { - expect(await call({ type: 'request', operation: 'searchReconcile' })).toEqual({ - operation: 'searchReconcile', - value: null, - type: 'result', - id: expect.any(Number) - }) - const response = await searchSessions('distinctive') - expect(response.kind).toBe('results') - if (response.kind === 'results') { - expect(response.hits.map((hit) => hit.sessionId)).toEqual([SESSION_ID]) - } -}) - it('discovers a new root through the parent exchange on manual reconciliation', async () => { const lateHome = join(harness.root, 'late-home') const id = 'bbbbbbbb-cccc-4ddd-8eee-ffffffffffff' diff --git a/src/main/ai-vault/scanners/codex/session-scanner-codex-title-index.test.ts b/src/main/ai-vault/scanners/codex/session-scanner-codex-title-index.test.ts index 1af7d282874..58a8a847ef4 100644 --- a/src/main/ai-vault/scanners/codex/session-scanner-codex-title-index.test.ts +++ b/src/main/ai-vault/scanners/codex/session-scanner-codex-title-index.test.ts @@ -1,18 +1,11 @@ -import { readAiVaultSessionTitlesFromFiles } from '../../session-titles/session-title-file-reader' import { tmpdir } from 'node:os' import { join } from 'node:path' -import * as transcriptFs from '../../../native-chat/wsl-transcript-fs-access' import { readCodexIndexUpdate } from './session-scanner-codex-index-read' import { parseAgentSessionFileCached } from '../../parse-cache/session-scanner-parse-cache' import { resetSessionParseCacheForTests } from '../../parse-cache/session-parse-cache-store' import { appendFile, mkdir, mkdtemp, rename, rm, stat, writeFile } from 'node:fs/promises' import { afterEach, describe, expect, it, vi } from 'vitest' -import { - _getCodexSessionIndexTitleCacheSizeForTest, - _readCachedCodexSessionIndexTitlesForTest, - _storeCodexSessionIndexTitleCacheEntryForTest, - resetCodexSessionIndexTitleCacheForTests -} from './session-scanner-codex-title-index' +import { resetCodexSessionIndexTitleCacheForTests } from './session-scanner-codex-title-index' let tempRoots: string[] = [] @@ -24,28 +17,6 @@ afterEach(async () => { tempRoots = [] }) -describe('codex session index title cache', () => { - it('does not replace a newer same-home entry when an older cache hit resolves', async () => { - let resolveOldTitles: (titles: Map) => void = () => {} - const oldTitles = new Promise>((resolve) => { - resolveOldTitles = resolve - }) - _storeCodexSessionIndexTitleCacheEntryForTest('same-home', 'old', oldTitles) - const pendingOldHit = _readCachedCodexSessionIndexTitlesForTest('same-home', 'old') - - const newTitles = new Map([['new-session', 'New title']]) - _storeCodexSessionIndexTitleCacheEntryForTest('same-home', 'new', Promise.resolve(newTitles)) - const resolvedOldTitles = new Map([['old-session', 'Old title']]) - resolveOldTitles(resolvedOldTitles) - - await expect(pendingOldHit).resolves.toBe(resolvedOldTitles) - await expect(_readCachedCodexSessionIndexTitlesForTest('same-home', 'new')).resolves.toBe( - newTitles - ) - expect(_getCodexSessionIndexTitleCacheSizeForTest()).toBe(1) - }) -}) - async function indexHome() { const root = await mkdtemp(join(tmpdir(), 'orca-codex-index-')) tempRoots.push(root) @@ -54,30 +25,6 @@ async function indexHome() { const entry = (id: string, thread_name: string) => `${JSON.stringify({ id, thread_name })}\n` describe('incremental Codex session names', () => { - it('applies only appended complete records and retains a partial tail for the next read', async () => { - const { path } = await indexHome() - const initial = entry('session', 'First') - await writeFile(path, initial) - const beforeAppend = await stat(path) - const appended = entry('session', 'After stat') - await appendFile(path, appended) - const first = await readCodexIndexUpdate(path, beforeAppend) - expect(first.titles.get('session')).toBe('After stat') - const stream = vi.spyOn(transcriptFs, 'openTranscriptReadStream') - await appendFile(path, `${entry('session', 'Renamed')}{"id":"session","thread_name":"Fin`) - const next = await readCodexIndexUpdate(path, await stat(path), first) - expect(next.titles.get('session')).toBe('Renamed') - expect( - stream.mock.calls.some( - ([, options]) => options.start === Buffer.byteLength(initial + appended) - ) - ).toBe(true) - await appendFile(path, 'al"}\n') - expect((await readCodexIndexUpdate(path, await stat(path), next)).titles.get('session')).toBe( - 'Final' - ) - }) - it('rereads an EOF title observed after stat when growth invalidates its record', async () => { const { path } = await indexHome() await writeFile(path, entry('head', 'Head') + entry('tail', 'Original')) @@ -144,46 +91,6 @@ describe('incremental Codex session names', () => { } ) - it('shares negative home lookups across cold and cached fallbacks, but refreshes them on a later call', async () => { - const { root, path } = await indexHome() - await mkdir(join(root, 'sessions')) - await writeFile(path, entry('unrelated', 'Other thread')) - const requests = await Promise.all( - Array.from({ length: 5 }, async (_, index) => { - const sessionId = `fallback-${index}` - const transcriptPath = join(root, 'sessions', `${sessionId}.jsonl`) - await writeFile( - transcriptPath, - `${JSON.stringify({ timestamp: '2026-11-05T10:00:00.000Z', type: 'session_meta', payload: { id: sessionId, title: 'Metadata name', cwd: '/repo/app' } })}\n` - ) - return { agent: 'codex' as const, sessionId, transcriptPath } - }) - ) - const indexStats = vi.spyOn(transcriptFs, 'wslGatedStat') - for (const phase of ['cold', 'cached', 'renamed']) { - if (phase === 'renamed') { - await appendFile( - path, - requests.map((request) => entry(request.sessionId, 'Indexed rename')).join('') - ) - } - indexStats.mockClear() - const result = await readAiVaultSessionTitlesFromFiles(requests) - expect(result.titles.map((title) => title.title)).toEqual( - phase === 'renamed' - ? [ - 'Indexed rename', - 'Indexed rename', - 'Indexed rename', - 'Indexed rename', - 'Indexed rename' - ] - : ['Metadata name', 'Metadata name', 'Metadata name', 'Metadata name', 'Metadata name'] - ) - expect(indexStats.mock.calls.filter(([file]) => file === path)).toHaveLength(1) - } - }) - it('keeps the indexed rename over a recorded session_meta title when the rollout grows', async () => { const { root, path } = await indexHome() const rollout = join(root, 'sessions', 'rollout.jsonl') diff --git a/src/main/antigravity/antigravity-chat-user-skills.test.ts b/src/main/antigravity/antigravity-chat-user-skills.test.ts deleted file mode 100644 index 2ad430400ed..00000000000 --- a/src/main/antigravity/antigravity-chat-user-skills.test.ts +++ /dev/null @@ -1,89 +0,0 @@ -import type * as FsPromises from 'node:fs/promises' -import { mkdir, mkdtemp, readFile, readlink, rm, rmdir, symlink, writeFile } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, beforeEach, expect, it, vi } from 'vitest' -import { linkAntigravityUserSkills } from './antigravity-chat-user-skills' - -const fsCalls = vi.hoisted(() => ({ count: 0 })) -vi.mock('node:fs/promises', async (importOriginal) => { - const real = await importOriginal() - const counted = - (fn: (...args: A) => Promise) => - (...args: A) => { - fsCalls.count++ - return fn(...args) - } - return { ...real, lstat: counted(real.lstat), rmdir: vi.fn(real.rmdir) } -}) - -let root: string -let chatHome: string -let userHome: string - -beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'agy-user-skills-')) - chatHome = join(root, 'antigravity-chat') - userHome = join(root, 'home') - await mkdir(join(userHome, '.gemini', 'config', 'skills', 'orca-cli'), { recursive: true }) - await writeFile(join(userHome, '.gemini', 'config', 'skills', 'orca-cli', 'SKILL.md'), 'skill') -}) - -afterEach(async () => { - vi.mocked(rmdir).mockRestore() - await rm(root, { recursive: true, force: true }) -}) - -it('replaces the empty placeholder with a link to the user skills', async () => { - await mkdir(join(chatHome, 'config', 'skills'), { recursive: true }) - - await linkAntigravityUserSkills(chatHome, userHome) - - const linked = join(chatHome, 'config', 'skills', 'orca-cli', 'SKILL.md') - expect(await readFile(linked, 'utf8')).toBe('skill') - // No ~/.gemini/antigravity-cli/skills, so nothing is linked there. - await expect(readlink(join(chatHome, 'antigravity-cli', 'skills'))).rejects.toThrow() -}) - -it('removes a link whose target is gone', async () => { - const link = join(chatHome, 'antigravity-cli', 'skills') - const target = join(userHome, '.gemini', 'antigravity-cli', 'skills') - await mkdir(join(chatHome, 'antigravity-cli'), { recursive: true }) - await mkdir(target, { recursive: true }) - // Windows creates directory links without extra rights only as junctions, to a target that exists. - await symlink(target, link, process.platform === 'win32' ? 'junction' : 'dir') - await rm(target, { recursive: true }) - - await linkAntigravityUserSkills(chatHome, userHome) - - await expect(readlink(link)).rejects.toThrow() -}) - -it('lets one launch finish linking before another starts on the same chat home', async () => { - const realRmdir = vi.mocked(rmdir).getMockImplementation()! - await mkdir(join(chatHome, 'config', 'skills'), { recursive: true }) - let release!: () => void - const held = new Promise((resolve) => (release = resolve)) - let first!: () => void - const firstRemoving = new Promise((resolve) => (first = resolve)) - vi.mocked(rmdir).mockImplementationOnce(async (path) => { - first() - await held - return realRmdir(path) - }) - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - - const one = linkAntigravityUserSkills(chatHome, userHome) - await firstRemoving - const before = fsCalls.count - const two = linkAntigravityUserSkills(chatHome, userHome) - // A second launch that started at once would already have looked at the folder. - expect(fsCalls.count).toBe(before) - release() - await Promise.all([one, two]) - - expect(warn).not.toHaveBeenCalled() - const linked = join(chatHome, 'config', 'skills', 'orca-cli', 'SKILL.md') - expect(await readFile(linked, 'utf8')).toBe('skill') - warn.mockRestore() -}) diff --git a/src/main/ephemeral-vm/runtime-resume-pairing.test.ts b/src/main/ephemeral-vm/runtime-resume-pairing.test.ts index 8fe94cfd5d8..1cb7937226d 100644 --- a/src/main/ephemeral-vm/runtime-resume-pairing.test.ts +++ b/src/main/ephemeral-vm/runtime-resume-pairing.test.ts @@ -7,8 +7,7 @@ import type { PairedRuntimeConnection } from '../../shared/remote-runtime/pairin import { addEnvironmentFromPairingCode, listEnvironments, - resolveEnvironmentPairingOffer, - updateEnvironmentFromPairingCode + resolveEnvironmentPairingOffer } from '../../shared/remote-runtime/host-store/runtime-environment-store' import { RemoteRuntimeClientError } from '../../shared/remote-runtime/remote-runtime-client-error' import { DeviceRegistry } from '../runtime/rpc/server/pairing/device-registry' @@ -114,17 +113,6 @@ describe('resuming a recipe VM server', () => { expect(listEnvironments(desktopDataPath)[0]!.pairingRevision).toBe(before.pairingRevision) }) - it('grows one device row per resume when every resume re-pairs (previous behaviour)', () => { - const environmentId = provision() - for (let cycle = 1; cycle <= 20; cycle++) { - updateEnvironmentFromPairingCode(desktopDataPath, environmentId, { - pairingCode: bootVm(serverDataPath, `wss://vm-${cycle}.example/runtime`) - }) - connect(serverDataPath, desktopDataPath, environmentId) - } - expect(new DeviceRegistry(serverDataPath).listDevices()).toHaveLength(21) - }) - it('leaves the store untouched but still drops the transport when nothing moved', async () => { const environmentId = provision() const before = listEnvironments(desktopDataPath)[0]! diff --git a/src/main/ipc/agent-pane-retirement-ipc.test.ts b/src/main/ipc/agent-pane-retirement-ipc.test.ts index a2db1dd1bec..5fd961719d9 100644 --- a/src/main/ipc/agent-pane-retirement-ipc.test.ts +++ b/src/main/ipc/agent-pane-retirement-ipc.test.ts @@ -30,12 +30,9 @@ beforeEach(() => { }) describe('pane retirement IPC request identity', () => { - it.each([null, '', 'bad-id', 42, {}, `${id} `])( - 'rejects malformed request identity %j', - (requestId) => { - mocks.handlers.get('agentStatus:retirePaneAuthority')?.({}, paneKey, requestId) - expect(mocks.retire).not.toHaveBeenCalled() - expect(mocks.clear).not.toHaveBeenCalled() - } - ) + it.each([null, 'bad-id', `${id} `])('rejects malformed request identity %j', (requestId) => { + mocks.handlers.get('agentStatus:retirePaneAuthority')?.({}, paneKey, requestId) + expect(mocks.retire).not.toHaveBeenCalled() + expect(mocks.clear).not.toHaveBeenCalled() + }) }) diff --git a/src/main/ipc/browser/browser-client-page-metadata-ipc.test.ts b/src/main/ipc/browser/browser-client-page-metadata-ipc.test.ts index 734d9d7cb0b..550f1e43618 100644 --- a/src/main/ipc/browser/browser-client-page-metadata-ipc.test.ts +++ b/src/main/ipc/browser/browser-client-page-metadata-ipc.test.ts @@ -73,21 +73,7 @@ describe('browser:publishClientPageMetadata', () => { it.each([ ['no environment', { environmentId: '', params: PARAMS }], ['a non-string environment', { environmentId: 7, params: PARAMS }], - ['no params at all', { environmentId: 'environment-a' }], - [ - 'a revision below the protocol floor', - { - environmentId: 'environment-a', - params: { ...PARAMS, revision: 0 } - } - ], - [ - 'a page id the protocol rejects', - { - environmentId: 'environment-a', - params: { ...PARAMS, browserPageId: '' } - } - ] + ['no params at all', { environmentId: 'environment-a' }] ])('refuses %s without reaching the lease', async (_case, args) => { await expect(publishMetadata(args)).resolves.toEqual({ status: 'refused' }) expect(mocks.publish).not.toHaveBeenCalled() diff --git a/src/main/ipc/codex-config-sync.test.ts b/src/main/ipc/codex-config-sync.test.ts index 33f1933dc47..7136807dfd5 100644 --- a/src/main/ipc/codex-config-sync.test.ts +++ b/src/main/ipc/codex-config-sync.test.ts @@ -65,19 +65,6 @@ describe('codexConfigSync:status handler', () => { }) }) - it('reports synced when the selection runs directly on the real home', () => { - // System default uses no mirror; stale shared homes must not produce warnings. - const staleSharedHome = join(root, 'codex-runtime-home', 'home') - mkdirSync(staleSharedHome, { recursive: true }) - writeFileSync(join(staleSharedHome, 'config.toml'), 'model = "stale"\n', 'utf-8') - - expect(invokeHandler(null)).toEqual({ - state: 'synced', - reason: null, - systemConfigPath: join(root, '.codex', 'config.toml') - }) - }) - it('reports a stall when the selected home baseline cannot be read', () => { const perAccountHome = join(root, 'codex-accounts', 'acct-1', 'home') mkdirSync(perAccountHome, { recursive: true }) diff --git a/src/main/ipc/filesystem/filesystem-mutations.test.ts b/src/main/ipc/filesystem/filesystem-mutations.test.ts index cea6a25543c..2eb47d4b578 100644 --- a/src/main/ipc/filesystem/filesystem-mutations.test.ts +++ b/src/main/ipc/filesystem/filesystem-mutations.test.ts @@ -85,17 +85,6 @@ describe('registerFilesystemMutationHandlers', () => { registerFilesystemMutationHandlers(store as never) }) - it('rejects file creation when path already exists (wx flag)', async () => { - // wx rejects collisions atomically without an existence-check race. - writeFileMock.mockRejectedValue(Object.assign(new Error('EEXIST'), { code: 'EEXIST' })) - - await expect( - handlers.get('fs:createFile')!(null, { - filePath: path.resolve('/workspace/repo/existing.ts') - }) - ).rejects.toThrow("A file or folder named 'existing.ts' already exists in this location") - }) - it('rejects file creation outside allowed roots', async () => { mockRealpath({ [path.resolve('/workspace/repo/link.ts')]: path.resolve('/private/secret.ts') diff --git a/src/main/ipc/mobile.test.ts b/src/main/ipc/mobile.test.ts index 95dfe0f9c20..87b303a5f91 100644 --- a/src/main/ipc/mobile.test.ts +++ b/src/main/ipc/mobile.test.ts @@ -1,34 +1,12 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' -import type * as NodeOs from 'node:os' -const { defaultRouteInterfaceNamesMock, handleMock, networkInterfacesMock } = vi.hoisted(() => ({ - defaultRouteInterfaceNamesMock: vi.fn(), - handleMock: vi.fn(), - networkInterfacesMock: vi.fn() -})) +const { handleMock } = vi.hoisted(() => ({ handleMock: vi.fn() })) vi.mock('electron', () => ({ app: { isPackaged: false }, ipcMain: { handle: handleMock }, shell: { openExternal: vi.fn() } })) - -vi.mock('qrcode', () => ({ - default: { - create: vi.fn().mockReturnValue({ modules: { size: 21 } }), - toDataURL: vi.fn().mockResolvedValue('data:image/png;base64,qr') - } -})) - -// Mock interface enumeration only; integration fixtures still need real os.tmpdir. -vi.mock('os', async (importOriginal) => ({ - ...(await importOriginal()), - networkInterfaces: networkInterfacesMock -})) - -vi.mock('../runtime/rpc/server/pairing/network/windows-default-route-interfaces', () => ({ - getWindowsDefaultRouteInterfaceNames: defaultRouteInterfaceNamesMock -})) import { registerMobileHandlers } from './mobile' describe('registerMobileHandlers', () => { @@ -37,58 +15,11 @@ describe('registerMobileHandlers', () => { beforeEach(() => { handlers.clear() handleMock.mockReset() - networkInterfacesMock.mockReset() - networkInterfacesMock.mockReturnValue({}) - defaultRouteInterfaceNamesMock.mockReset().mockResolvedValue(new Set()) handleMock.mockImplementation((channel: string, handler: (...args: unknown[]) => unknown) => { handlers.set(channel, handler) }) }) - // All accepted loopback spellings must honor an explicit this-computer reach. - - // Custom loopback frontends can tunnel to this host's LAN address; do not infer local-only intent from their shape. - - it('mints desktop pairing offers with the QR and link TTLs and returns their expiry', async () => { - const createMobilePairingOffer = vi.fn().mockResolvedValue({ - available: true, - pairingUrl: 'orca://pair#mobile', - endpoint: 'ws://192.168.1.24:6768', - deviceId: 'mobile-1', - connectionMode: 'automatic', - offerExpiresAt: 1_000 - }) - const createPairingOffer = vi.fn().mockReturnValue({ - available: true, - pairingUrl: 'orca://pair#runtime', - webClientUrl: 'http://192.168.1.24:6768/web-index.html?pairing=runtime', - endpoint: 'ws://192.168.1.24:6768', - deviceId: 'runtime-1', - offerExpiresAt: 2_000 - }) - registerMobileHandlers( - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these handlers only call the three stubbed server members. - { - createMobilePairingOffer, - createPairingOffer, - ensureNetworkExposure: vi.fn().mockResolvedValue(undefined) - } as never - ) - - await expect( - handlers.get('mobile:getPairingQR')?.(null, { address: '192.168.1.24' }) - ).resolves.toMatchObject({ available: true, expiresAt: 1_000 }) - expect(createMobilePairingOffer).toHaveBeenCalledWith( - expect.objectContaining({ offerTtlMs: 10 * 60_000 }) - ) - await expect( - handlers.get('mobile:getRuntimePairingUrl')?.(null, { address: '192.168.1.24' }) - ).resolves.toMatchObject({ available: true, expiresAt: 2_000 }) - expect(createPairingOffer).toHaveBeenCalledWith( - expect.objectContaining({ offerTtlMs: 60 * 60_000 }) - ) - }) - it('rejects firewall mutation from a non-window renderer', async () => { const runPowerShell = vi.fn() const rpcServer = { getWebSocketEndpoint: () => 'ws://0.0.0.0:6768' } diff --git a/src/main/ipc/profiles/orca-profiles.test.ts b/src/main/ipc/profiles/orca-profiles.test.ts deleted file mode 100644 index bc1f3665038..00000000000 --- a/src/main/ipc/profiles/orca-profiles.test.ts +++ /dev/null @@ -1,157 +0,0 @@ -import type * as NewProfileStateModule from '../../orca-profiles/new-profile-state' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import type * as ProfileStoragePaths from '../../orca-profiles/profile-storage-paths' -import type * as ProfileCloudService from '../../orca-profiles/profile-cloud-service' - -const { - handlers, - appExitMock, - appQuitMock, - appRelaunchMock, - relaunchAppMock, - destroySystemTrayMock, - createLocalOrcaProfileMock, - createCloudLinkedOrcaProfileMock, - getOrcaProfileListStateMock, - initializeNewOrcaProfileStateMock, - setActiveOrcaProfileMock, - transferOrcaProfileProjectMock, - hasOrcaProfileStateDatabaseMock -} = vi.hoisted(() => ({ - handlers: new Map unknown>(), - appExitMock: vi.fn(), - appQuitMock: vi.fn(), - appRelaunchMock: vi.fn(), - relaunchAppMock: vi.fn(), - destroySystemTrayMock: vi.fn(), - createLocalOrcaProfileMock: vi.fn(), - createCloudLinkedOrcaProfileMock: vi.fn(), - getOrcaProfileListStateMock: vi.fn(), - initializeNewOrcaProfileStateMock: vi.fn(), - setActiveOrcaProfileMock: vi.fn(), - transferOrcaProfileProjectMock: vi.fn(), - hasOrcaProfileStateDatabaseMock: vi.fn() -})) - -vi.mock('electron', () => ({ - app: { - exit: appExitMock, - quit: appQuitMock, - relaunch: appRelaunchMock - }, - ipcMain: { - handle: vi.fn((channel: string, handler: (_event: unknown, args?: unknown) => unknown) => { - handlers.set(channel, handler) - }) - } -})) - -vi.mock('../../tray/system-tray', () => ({ - destroySystemTray: destroySystemTrayMock -})) - -vi.mock('../../startup/quit/app-relaunch', () => ({ - relaunchApp: relaunchAppMock -})) - -vi.mock('../../orca-profiles/profile-index-store', () => ({ - createLocalOrcaProfile: createLocalOrcaProfileMock, - getOrcaProfileListState: getOrcaProfileListStateMock, - initializeNewOrcaProfileState: initializeNewOrcaProfileStateMock, - setActiveOrcaProfile: setActiveOrcaProfileMock -})) - -vi.mock('../../orca-profiles/new-profile-state', async (importOriginal) => ({ - ...(await importOriginal()), - initializeNewOrcaProfileState: initializeNewOrcaProfileStateMock -})) - -vi.mock('../../orca-profiles/profile-cloud-service', async (importOriginal) => ({ - ...(await importOriginal()), - createCloudLinkedOrcaProfile: createCloudLinkedOrcaProfileMock -})) - -function makeStoreMock(flushPendingOrThrowAsync = vi.fn()) { - const freezeWrites = vi.fn() - const resumeMaintenance = vi.fn(async () => {}) - return { - flushPendingOrThrowAsync, - freezeWrites, - resumeMaintenance, - beginProfileMaintenance: vi.fn(async (options: unknown) => { - await flushPendingOrThrowAsync(options) - freezeWrites() - return { resume: resumeMaintenance } - }), - getSettings: () => ({}) - } -} - -vi.mock('../../orca-profiles/profile-project-transfer', () => ({ - transferOrcaProfileProject: transferOrcaProfileProjectMock -})) - -vi.mock('../../orca-profiles/profile-storage-paths', async (importOriginal) => ({ - ...(await importOriginal()), - hasOrcaProfileStateDatabase: hasOrcaProfileStateDatabaseMock -})) - -import { registerOrcaProfileHandlers } from './orca-profiles' -import { installFakeAppEnvironment } from '../../../../config/scripts/test-runtime/environment/vitest-host-ports-setup' - -const ipcEvent = { sender: { isDestroyed: () => false, send: vi.fn() } } - -describe('registerOrcaProfileHandlers', () => { - beforeEach(() => { - // Reinstall AppEnvironment per test after global beforeEach resets its fake. - installFakeAppEnvironment({ getPath: () => '/tmp/orca-user-data' }) - vi.useFakeTimers() - handlers.clear() - ipcEvent.sender.send.mockClear() - appExitMock.mockReset() - appQuitMock.mockReset() - appRelaunchMock.mockReset() - relaunchAppMock.mockReset() - relaunchAppMock.mockImplementation(() => appRelaunchMock()) - destroySystemTrayMock.mockReset() - createLocalOrcaProfileMock.mockReset() - createCloudLinkedOrcaProfileMock.mockReset() - getOrcaProfileListStateMock.mockReset() - initializeNewOrcaProfileStateMock.mockReset() - setActiveOrcaProfileMock.mockReset() - transferOrcaProfileProjectMock.mockReset() - hasOrcaProfileStateDatabaseMock.mockReset().mockReturnValue(false) - }) - - afterEach(() => { - vi.useRealTimers() - }) - - it('initializes the selected workspace for a newly created local profile', () => { - const created = { profile: { id: 'local-work', name: 'Work' } } - createLocalOrcaProfileMock.mockReturnValue(created) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: Profile creation uses only the mocked store methods. - registerOrcaProfileHandlers(makeStoreMock() as never) - - expect(handlers.get('orcaProfiles:createLocal')?.(ipcEvent, { name: 'Work' })).toBe(created) - expect(initializeNewOrcaProfileStateMock).toHaveBeenCalledWith('local-work', undefined) - }) - - it('initializes Home for a newly created cloud-linked profile', async () => { - const created = { - status: 'created', - auth: { activeProfileId: 'local-default' }, - activeProfileId: 'local-default', - profiles: [], - profile: { id: 'cloud-1' } - } - createCloudLinkedOrcaProfileMock.mockResolvedValue(created) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: Profile creation uses only the mocked store methods. - registerOrcaProfileHandlers(makeStoreMock() as never) - - await expect( - handlers.get('orcaProfiles:createCloudLinked')?.(ipcEvent, { orgId: 'org-1', name: 'Work' }) - ).resolves.toBe(created) - expect(initializeNewOrcaProfileStateMock).toHaveBeenCalledWith('cloud-1', undefined) - }) -}) diff --git a/src/main/ipc/pty-claude-agent-view-readiness.test.ts b/src/main/ipc/pty-claude-agent-view-readiness.test.ts index 0247392ffbe..98f2bf3f711 100644 --- a/src/main/ipc/pty-claude-agent-view-readiness.test.ts +++ b/src/main/ipc/pty-claude-agent-view-readiness.test.ts @@ -56,50 +56,47 @@ describe.each(['local', 'daemon'] as const)('%s PTY navigation readiness', (prov const { handlers, mainWindow, spawnAndGetEnv } = setupPtyIpcSuite() const { daemonSpawnAndGetEnv } = createDaemonActiveProviderFixtures({ handlers, mainWindow }) - it.each([false, true])( - 'spawns a plain shell while reconciliation is pending (navigation: %s)', - async (enabled) => { - let release = () => {} - const gate = new Promise((resolve) => { - release = resolve - }) - const config = join(tmpdir(), 'orca-spawn-pending-agent-view-config') - vi.stubEnv('CLAUDE_CONFIG_DIR', config) - const reconcileConfig = vi.fn(() => gate) - const service = new agentView.ClaudeAgentViewService( - join(tmpdir(), 'orca-spawn-pending-agent-view'), - { - reconcileConfig, - ensurePlugin: async () => join(tmpdir(), 'orca-spawn-agent-view-plugin'), - loginEnv: async () => ({ CLAUDE_CONFIG_DIR: config }), - listProfiles: async () => [] - } - ) - const settings = createGlobalSettingsFixture({ claudeSubagentNavigationEnabled: enabled }) - let reconciled = false - const reconcile = service.reconcile(settings).then(() => { - reconciled = true - }) - await vi.waitFor(() => expect(reconcileConfig).toHaveBeenCalledOnce()) - const serviceSpy = vi.spyOn(agentView, 'getClaudeAgentViewService').mockReturnValue(service) - let env: Record | undefined - const spawn = ( - provider === 'daemon' - ? daemonSpawnAndGetEnv({}, undefined, () => settings) - : spawnAndGetEnv({}, undefined, undefined, () => settings) - ).then((result) => { - env = result - }) - try { - await vi.waitFor(() => expect(env).toBeDefined()) - expect(reconciled).toBe(false) - expect(env).not.toHaveProperty('ORCA_CLAUDE_AGENT_VIEW_PLUGIN_DIR') - } finally { - release() - await Promise.all([reconcile, spawn]) - serviceSpy.mockRestore() - vi.unstubAllEnvs() + it('spawns a plain shell while reconciliation is pending', async () => { + let release = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + const config = join(tmpdir(), 'orca-spawn-pending-agent-view-config') + vi.stubEnv('CLAUDE_CONFIG_DIR', config) + const reconcileConfig = vi.fn(() => gate) + const service = new agentView.ClaudeAgentViewService( + join(tmpdir(), 'orca-spawn-pending-agent-view'), + { + reconcileConfig, + ensurePlugin: async () => join(tmpdir(), 'orca-spawn-agent-view-plugin'), + loginEnv: async () => ({ CLAUDE_CONFIG_DIR: config }), + listProfiles: async () => [] } + ) + const settings = createGlobalSettingsFixture({ claudeSubagentNavigationEnabled: true }) + let reconciled = false + const reconcile = service.reconcile(settings).then(() => { + reconciled = true + }) + await vi.waitFor(() => expect(reconcileConfig).toHaveBeenCalledOnce()) + const serviceSpy = vi.spyOn(agentView, 'getClaudeAgentViewService').mockReturnValue(service) + let env: Record | undefined + const spawn = ( + provider === 'daemon' + ? daemonSpawnAndGetEnv({}, undefined, () => settings) + : spawnAndGetEnv({}, undefined, undefined, () => settings) + ).then((result) => { + env = result + }) + try { + await vi.waitFor(() => expect(env).toBeDefined()) + expect(reconciled).toBe(false) + expect(env).not.toHaveProperty('ORCA_CLAUDE_AGENT_VIEW_PLUGIN_DIR') + } finally { + release() + await Promise.all([reconcile, spawn]) + serviceSpy.mockRestore() + vi.unstubAllEnvs() } - ) + }) }) diff --git a/src/main/ipc/pty/host-env/spawn-cwd-wsl-move.test.ts b/src/main/ipc/pty/host-env/spawn-cwd-wsl-move.test.ts index fb6e84fec1f..1fb8d8d0e46 100644 --- a/src/main/ipc/pty/host-env/spawn-cwd-wsl-move.test.ts +++ b/src/main/ipc/pty/host-env/spawn-cwd-wsl-move.test.ts @@ -76,15 +76,14 @@ describe('a spawn by its folder, between quiesce and commit', () => { await expect(acceptCwd(cwd)).rejects.toBeInstanceOf(WslMoveProjectFencedError) }) - it.each([ - ['a floating terminal', 'global-floating-terminal'], - ['another project', 'repo-2::/x'], - ['a folder workspace', 'folder:folder-9'] - ])('refuses %s that starts in a moving folder', async (_label, worktreeId) => { - await expect(acceptCwd('/home/dev/repo-1/src', worktreeId)).rejects.toBeInstanceOf( - WslMoveProjectFencedError - ) - }) + it.each([['a folder workspace', 'folder:folder-9']])( + 'refuses %s that starts in a moving folder', + async (_label, worktreeId) => { + await expect(acceptCwd('/home/dev/repo-1/src', worktreeId)).rejects.toBeInstanceOf( + WslMoveProjectFencedError + ) + } + ) it('stops at the commit, once the host owns the folder', async () => { seedWslMoveCutover(userDataPath, cutover('move-1', 'destination-committed')) diff --git a/src/main/opencode/opencode-root-model-launch.test.ts b/src/main/opencode/opencode-root-model-launch.test.ts index 4158475d6db..1913e5d1a21 100644 --- a/src/main/opencode/opencode-root-model-launch.test.ts +++ b/src/main/opencode/opencode-root-model-launch.test.ts @@ -29,16 +29,6 @@ const launch = (version: string, command: string, envToDelete: string[] = []) => beforeEach(() => run.mockReset()) -it('starts OpenCode 2 on its own server with the picked model in its launch config', async () => { - const result = await launch('2.0.21', "opencode '--model' 'opencode/big-pickle'") - expect(result.command).toBe('opencode --standalone') - expect(JSON.parse(result.env?.OPENCODE_CONFIG_CONTENT ?? '')).toEqual({ - autoupdate: false, - model: 'opencode/big-pickle', - agents: { build: { model: 'opencode/big-pickle' } } - }) -}) - it('keeps the overlay when the caller asked to drop inherited OpenCode config', async () => { const result = await launch('2.0.21', "opencode '--model' 'opencode/big-pickle'", [ 'OPENCODE_CONFIG_CONTENT' diff --git a/src/main/telemetry/client-protected-milestones.test.ts b/src/main/telemetry/client-protected-milestones.test.ts index 3b0dd9e715a..954c93ca98b 100644 --- a/src/main/telemetry/client-protected-milestones.test.ts +++ b/src/main/telemetry/client-protected-milestones.test.ts @@ -227,29 +227,6 @@ describe('protected telemetry milestones', () => { expect(captured('meaningful_activity_day')[0].properties.trigger).toBe('native_chat') }) - it('counts a provider-accepted phone prompt as an interaction and an active day', () => { - track('native_chat_prompt_submitted', { - agent_kind: 'claude-code', - runtime: 'unknown', - client: 'mobile' - }) - - expect(captured('first_agent_interaction')[0].properties).toMatchObject({ - source: 'mobile_chat', - agent_kind: 'claude-code' - }) - expect(captured('meaningful_activity_day')[0].properties).toMatchObject({ - trigger: 'mobile_chat', - install_cohort: 'fresh' - }) - }) - - it('reports profiles without a frozen cohort as existing', () => { - state.settings.telemetry = telemetry({}) - track('agent_started', sidebarLaunch) - expect(captured('meaningful_activity_day')[0].properties.install_cohort).toBe('existing') - }) - it('marks terminal activity once per day across restart and UTC rollover', () => { observeTerminalUserInput() restart() diff --git a/src/renderer/src/app-shell/visible-review-reporting.test.ts b/src/renderer/src/app-shell/visible-review-reporting.test.ts deleted file mode 100644 index 9166bb4661c..00000000000 --- a/src/renderer/src/app-shell/visible-review-reporting.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { describe, expect, it } from 'vitest' -import { createTestStore, makePRRefreshWorktree } from '../store/github/github-slice-test-harness' - -import { visibleReviewWorktreeIdsForState } from './use-visible-review-refresh-reporting' - -function state() { - const store = createTestStore() - store.setState({ - repos: [ - { id: 'repo-1', path: '/repo', displayName: 'repo', badgeColor: '', addedAt: 1, kind: 'git' } - ], - worktreesByRepo: { - 'repo-1': [makePRRefreshWorktree({ id: 'selected' }), makePRRefreshWorktree({ id: 'card' })] - }, - activeWorktreeId: 'selected', - activeView: 'terminal', - rightSidebarOpen: true, - rightSidebarTab: 'checks', - visibleReviewCardWorktreeIds: [] - }) - return store -} - -describe('review surface visibility union', () => { - it('keeps the selected panel when the left sidebar has no visible cards', () => { - expect(visibleReviewWorktreeIdsForState(state().getState())).toEqual(['selected']) - }) - it('unions cards and the panel without duplicate requests', () => { - const store = state() - store.setState({ visibleReviewCardWorktreeIds: ['card', 'selected'] }) - expect(visibleReviewWorktreeIdsForState(store.getState())).toEqual(['card', 'selected']) - }) - it('drops the selected panel when closed or suppressed by the active view', () => { - const store = state() - store.setState({ visibleReviewCardWorktreeIds: ['card'], activeView: 'settings' }) - expect(visibleReviewWorktreeIdsForState(store.getState())).toEqual(['card']) - store.setState({ activeView: 'terminal', rightSidebarOpen: false }) - expect(visibleReviewWorktreeIdsForState(store.getState())).toEqual(['card']) - }) -}) diff --git a/src/renderer/src/components/NewWorkspaceComposerCard.set-location.test.tsx b/src/renderer/src/components/NewWorkspaceComposerCard.set-location.test.tsx deleted file mode 100644 index 56244dbde40..00000000000 --- a/src/renderer/src/components/NewWorkspaceComposerCard.set-location.test.tsx +++ /dev/null @@ -1,138 +0,0 @@ -// @vitest-environment happy-dom - -import React, { act } from 'react' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { renderCard } from './NewWorkspaceComposerCard.test-fixture' - -const storeMocks = vi.hoisted(() => ({ - closeModal: vi.fn(), - openModal: vi.fn(), - openSettingsPage: vi.fn(), - openSettingsTarget: vi.fn() -})) - -vi.mock('@/store', () => ({ - useAppStore: Object.assign( - (selector: (state: unknown) => unknown) => - selector({ - closeModal: storeMocks.closeModal, - openModal: storeMocks.openModal, - openSettingsPage: storeMocks.openSettingsPage, - openSettingsTarget: storeMocks.openSettingsTarget, - setRuntimeEnvironmentStatus: vi.fn(), - setupProjectExistingFolder: vi.fn(), - setupProjectClone: vi.fn(), - activeModal: 'new-workspace-composer', - settings: { defaultTuiAgent: null, disabledTuiAgents: [] }, - updateSettings: vi.fn(), - projects: [], - repos: [] - }), - { getState: () => ({}) } - ) -})) - -vi.mock('@/components/contextual-tours/use-contextual-tour', () => ({ - useContextualTour: vi.fn() -})) - -vi.mock('@/components/ui/tooltip', () => ({ - Tooltip: ({ children }: { children: React.ReactNode }) => <>{children}, - TooltipContent: ({ children }: { children: React.ReactNode }) => <>{children}, - TooltipTrigger: ({ children }: { children: React.ReactNode }) => <>{children} -})) - -vi.mock('@/components/ui/tooltip', () => ({ - Tooltip: ({ children }: { children: React.ReactNode }) => <>{children}, - TooltipContent: ({ children }: { children: React.ReactNode }) => <>{children}, - TooltipTrigger: ({ children }: { children: React.ReactNode }) => <>{children} -})) - -vi.mock('@/components/agent/AgentCombobox', () => ({ - default: () => -})) - -vi.mock('@/components/sidebar/host-admission/ssh/AddRemoteHostDialog', () => ({ - AddRemoteHostDialog: () => null -})) - -vi.mock('@/components/sparse/SparseCheckoutPresetSelect', () => ({ - default: () => null -})) - -vi.mock('@/components/new-workspace/SmartWorkspaceNameField', () => ({ - default: () => -})) - -vi.mock('@/components/new-workspace/ProjectCombobox', () => ({ - default: () =>
-})) - -vi.mock('@/components/new-workspace/SetProjectLocationDialog', () => ({ - SetProjectLocationDialog: ({ - option, - projectName, - onClose, - onReady - }: { - option: { label: string } | null - projectName: string - onClose: () => void - onReady: (setupId: string) => void - }) => - option ? ( -
- - -
- ) : null -})) - -describe('NewWorkspaceComposerCard set location', () => { - let container: HTMLDivElement | null = null - - beforeEach(() => { - storeMocks.closeModal.mockReset() - storeMocks.openModal.mockReset() - storeMocks.openSettingsPage.mockReset() - }) - - afterEach(() => { - container?.remove() - container = null - }) - - // Flush lazy resolution; the click mounts Suspense before the chunk arrives. - it('opens set-location over the composer without leaving the create dialog', async () => { - const nestedOpenChanges: boolean[] = [] - container = await renderCard({ - onNestedDialogOpenChange: (open) => nestedOpenChanges.push(open) - }) - - act(() => { - container?.querySelector('div[data-run-target-combobox-root="true"]')?.click() - }) - const setLocation = [...document.body.querySelectorAll('button')].find( - (button) => button.textContent?.includes('Set project location') - ) - expect(setLocation).toBeTruthy() - act(() => setLocation?.click()) - await act(async () => {}) - - const dialog = document.body.querySelector('[data-testid="set-project-location-dialog"]') - expect(dialog?.getAttribute('data-host')).toBe('Devbox') - expect(dialog?.getAttribute('data-project')).toBe('Platform') - expect(nestedOpenChanges).toEqual([true]) - expect(storeMocks.closeModal).not.toHaveBeenCalled() - expect(storeMocks.openModal).not.toHaveBeenCalled() - expect(storeMocks.openSettingsPage).not.toHaveBeenCalled() - }) -}) diff --git a/src/renderer/src/components/NewWorkspaceComposerCard.test-fixture.tsx b/src/renderer/src/components/NewWorkspaceComposerCard.test-fixture.tsx deleted file mode 100644 index 3f61a31494e..00000000000 --- a/src/renderer/src/components/NewWorkspaceComposerCard.test-fixture.tsx +++ /dev/null @@ -1,127 +0,0 @@ -import React, { act } from 'react' -import { createRoot, type Root } from 'react-dom/client' -import NewWorkspaceComposerCard from './NewWorkspaceComposerCard' -import type { NewWorkspaceProjectOption } from '@/lib/new-workspace-project-options' -import type { ProjectHostSetupOption } from '@/lib/project-host-setup-options' - -const cardRoots = new WeakMap() -export function unmountCard(container: HTMLDivElement): void { - act(() => cardRoots.get(container)?.unmount()) - cardRoots.delete(container) - container.remove() -} - -export const projectOptions: NewWorkspaceProjectOption[] = [ - { - kind: 'project-group', - id: 'project-group:platform', - projectGroupId: 'platform', - displayName: 'Platform', - badgeColor: 'var(--muted-foreground)', - detail: '/workspace/platform', - parentPath: '/workspace/platform', - connectionId: null - } -] - -export const hostOptions: ProjectHostSetupOption[] = [ - { - kind: 'ready', - id: 'setup-local', - projectId: 'project-group:platform', - hostId: 'local', - repoId: 'repo-a', - label: 'Local Mac', - detail: 'Orca', - path: '/Users/alice/orca' - }, - { - kind: 'needs-setup', - id: 'needs-setup:ssh:devbox', - projectId: 'project-group:platform', - hostId: 'ssh:devbox', - label: 'Devbox', - detail: 'Project location not set', - isAvailable: true, - attention: false, - canSetLocation: true - } -] - -export async function renderCard( - overrides: Partial> = {} -): Promise { - const container = document.createElement('div') - document.body.appendChild(container) - const root = createRoot(container) - cardRoots.set(container, root) - act(() => { - root.render( - {}} - eligibleRepos={[]} - repoId="repo-a" - projectOptions={projectOptions} - selectedProjectId="project-group:platform" - selectedRepoIsGit - onRepoChange={() => {}} - onProjectChange={() => {}} - primaryActionLabel="Create workspace" - name="" - onNameValueChange={() => {}} - onSmartGitHubItemSelect={() => {}} - onSmartGitLabItemSelect={() => {}} - onSmartBranchSelect={() => {}} - onSmartLinearIssueSelect={() => {}} - smartNameSelection={null} - onClearSmartNameSelection={() => {}} - canReuseSelectedBranch={false} - reuseSelectedBranch={false} - onReuseSelectedBranchChange={() => {}} - forkPushWarning={null} - detectedAgentIds={null} - onOpenAgentSettings={() => {}} - advancedOpen={false} - onToggleAdvanced={() => {}} - parentWorktreeId={null} - onParentWorktreeIdChange={() => {}} - createDisabled={false} - projectError={null} - creating={false} - onCreate={() => {}} - note="" - onNoteChange={() => {}} - setupConfig={null} - requiresExplicitSetupChoice={false} - setupDecision={null} - onSetupDecisionChange={() => {}} - setupAgentStartupPolicy="start-immediately" - onSetupAgentStartupPolicyChange={() => {}} - shouldWaitForSetupCheck={false} - resolvedSetupDecision={null} - createError={null} - selectedRepoConnectionId={null} - selectedRepoSshStatus={null} - selectedRepoRequiresConnection={false} - selectedRepoConnectInProgress={false} - onConnectSelectedRepo={async () => {}} - canUseSparseCheckout={false} - sparsePresets={[]} - sparseSelectedPresetId={null} - onSparseSelectPreset={() => {}} - branchNameOverride={undefined} - onBranchNameOverrideChange={() => {}} - branchesEnabled={false} - setupControlsEnabled={false} - sparseControlsEnabled={false} - projectHostSetupOptions={hostOptions} - selectedProjectHostSetupId="setup-local" - {...overrides} - /> - ) - }) - // Settle mount-time warm before clicking; Vitest serializes overlapping imports unlike browsers. - await act(async () => {}) - return container -} diff --git a/src/renderer/src/components/activity/activity-clear-completed-hmr.test.ts b/src/renderer/src/components/activity/activity-clear-completed-hmr.test.ts deleted file mode 100644 index 395ef76fb99..00000000000 --- a/src/renderer/src/components/activity/activity-clear-completed-hmr.test.ts +++ /dev/null @@ -1,147 +0,0 @@ -// @vitest-environment happy-dom -import { afterEach, beforeEach, expect, it, vi } from 'vitest' -import type { RetainedAgentEntry } from '@/store/slices/agent-status/agent-status-contract' -import type { ActivityEvent, AgentPaneThread } from './activity-thread-types' -import { makeTab, makeWorktree } from './ActivityPrototypePage-test-fixtures' - -const mockStore = vi.hoisted(() => { - const activityClearedAtByPaneKey: Record = {} - const agentStatusByPaneKey: Record = {} - const retainedAgentsByPaneKey: Record = {} - const retentionSuppressedPaneKeys: Record = {} - const state = { - activityClearedAtByPaneKey, - agentStatusByPaneKey, - retainedAgentsByPaneKey, - retentionSuppressedPaneKeys, - applyActivityClearedAt: vi.fn((patch: Record) => { - const next = { ...state.activityClearedAtByPaneKey } - for (const [key, value] of Object.entries(patch)) { - if (value === null) { - delete next[key] - } else { - next[key] = value - } - } - state.activityClearedAtByPaneKey = next - }), - dismissRetainedAgents: vi.fn((paneKeys: readonly string[]) => { - const next = { ...state.retainedAgentsByPaneKey } - for (const key of paneKeys) { - if (state.agentStatusByPaneKey[key]) { - state.retentionSuppressedPaneKeys[key] = true - } - delete next[key] - } - state.retainedAgentsByPaneKey = next - }), - clearRetentionSuppressedPaneKeys: vi.fn((paneKeys: string[]) => { - for (const key of paneKeys) { - delete state.retentionSuppressedPaneKeys[key] - } - }), - retainAgents: vi.fn((entries: RetainedAgentEntry[]) => { - const next = { ...state.retainedAgentsByPaneKey } - for (const retained of entries) { - next[retained.entry.paneKey] = retained - } - state.retainedAgentsByPaneKey = next - }) - } - return state -}) - -const toastSpy = vi.hoisted(() => vi.fn()) - -vi.mock('@/store', () => ({ - useAppStore: { getState: () => mockStore } -})) -vi.mock('sonner', () => ({ toast: toastSpy })) - -import { - CLEAR_COMPLETED_EVICTION_FALLBACK_MS, - disposePendingClearCompletedEvictionListener, - clearCompletedActivity, - flushPendingClearCompletedEvictions -} from './activity-clear-completed' - -function makeThread(paneKey: string, overrides: Partial = {}): AgentPaneThread { - return { - paneKey, - tab: makeTab(), - worktree: makeWorktree(), - repo: null, - currentAgentState: null, - currentAgentEntry: null, - latestEvent: null, - latestTimestamp: 5_000, - agentType: 'claude', - unread: false, - paneTitle: `Agent ${paneKey}`, - responsePreview: '', - events: [], - ...overrides - } -} - -function doneEvent(interrupted: boolean): ActivityEvent { - return { - id: 'evt', - state: 'done', - timestamp: 5_000, - observedAt: 5_000, - worktree: makeWorktree(), - repo: null, - entry: { ...makeRetained('t-done:1').entry, interrupted }, - tab: makeTab(), - agentType: 'claude', - agentAlive: false, - unread: false - } -} - -const doneThread = makeThread('t-done:1', { latestEvent: doneEvent(false) }) - -function makeRetained(paneKey: string): RetainedAgentEntry { - return { - entry: { - state: 'done', - prompt: 'retained run', - updatedAt: 5_000, - stateStartedAt: 5_000, - paneKey, - stateHistory: [], - agentType: 'claude' - }, - worktreeId: 'wt-1', - tab: makeTab(), - agentType: 'claude', - startedAt: 5_000 - } -} - -const drop = vi.fn() -const domWindow = window -beforeEach(() => { - vi.useFakeTimers() - Object.assign(domWindow, { api: { agentStatus: { dropPersistedBatch: drop } } }) - mockStore.retainedAgentsByPaneKey = { 't-done:1': makeRetained('t-done:1') } - mockStore.activityClearedAtByPaneKey = {} - mockStore.retentionSuppressedPaneKeys = {} -}) -afterEach(() => { - flushPendingClearCompletedEvictions() - vi.useRealTimers() - vi.clearAllMocks() -}) - -it('preserves Undo after HMR without dropping the restored persisted row', () => { - clearCompletedActivity([doneThread]) - disposePendingClearCompletedEvictionListener() - const options = toastSpy.mock.calls.at(-1)?.[1] - options.action.onClick() - domWindow.dispatchEvent(new Event('pagehide')) - vi.advanceTimersByTime(CLEAR_COMPLETED_EVICTION_FALLBACK_MS) - expect(drop).not.toHaveBeenCalled() - expect(mockStore.retainedAgentsByPaneKey['t-done:1']).toBeDefined() -}) diff --git a/src/renderer/src/components/agent/AgentLaunchPickerMenu.search.test.tsx b/src/renderer/src/components/agent/AgentLaunchPickerMenu.search.test.tsx deleted file mode 100644 index c4cd46c9ff6..00000000000 --- a/src/renderer/src/components/agent/AgentLaunchPickerMenu.search.test.tsx +++ /dev/null @@ -1,103 +0,0 @@ -// @vitest-environment happy-dom -import { cleanup, fireEvent, render, screen } from '@testing-library/react' -import { afterEach, expect, it, vi } from 'vitest' -import type { ComponentProps } from 'react' -import type * as LaunchSetup from './AgentLaunchSetupButton' -import { AgentLaunchSetupButton } from './AgentLaunchSetupButton' -import { AgentLaunchPickerMenu } from './AgentLaunchPickerMenu' -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuTrigger, - DropdownMenuSub, - DropdownMenuSubTrigger -} from '@/components/ui/dropdown-menu' -import type { AgentPickerEntry } from '../../../../shared/agent-launch/picker/agent-launch-picker' - -vi.mock('@/hooks/useShortcutLabel', () => ({ useOptionalShortcutLabel: () => null })) -vi.mock('./AgentLaunchSetupButton', async (importOriginal) => ({ - ...(await importOriginal()), - AgentLaunchSetupButton: vi.fn((props: ComponentProps) => ( - - - {props.renderAnchor?.()} - - - )) -})) -afterEach(() => { - cleanup() - vi.clearAllMocks() -}) - -it('skips unchanged search rows on unrelated inventory updates and launches current choices', () => { - const codex: AgentPickerEntry = { - agent: 'codex', - label: 'Codex', - rows: [{ agent: 'codex', kind: 'cli' }], - surfaces: ['terminal'], - defaultSurface: 'terminal', - terminal: {}, - structured: {}, - catalog: { - origin: 'probe', - fetchedAt: 1, - models: [ - { - id: 'host-model', - label: 'Host model', - isDefault: true, - efforts: [{ value: 'low', label: 'Low' }] - } - ] - } - } - const claude: AgentPickerEntry = { - ...codex, - agent: 'claude', - label: 'Claude', - rows: [{ agent: 'claude', kind: 'cli' }], - catalog: { origin: 'probe', fetchedAt: 1, models: [] } - } - const launch = vi.fn() - const menu = (entries: AgentPickerEntry[]) => ( - - Open - - - - - ) - const view = render(menu([codex, claude])) - fireEvent.change(screen.getByRole('textbox'), { target: { value: 'host low' } }) - expect(screen.getByRole('menuitem').textContent).toContain('Codex · Host model · Low') - expect(vi.mocked(AgentLaunchSetupButton).mock.lastCall?.[0].seed).toEqual({ - agent: 'codex', - surface: 'terminal', - choices: { model: 'host-model', options: { effort: 'low' } }, - acceptedEdits: { choices: { model: 'host-model', options: { effort: 'low' } } }, - delegated: [], - literalKeys: [] - }) - vi.mocked(AgentLaunchSetupButton).mockClear() - view.rerender( - menu([codex, { ...claude, catalog: { origin: 'probe', fetchedAt: 2, models: [] } }]) - ) - expect(AgentLaunchSetupButton).not.toHaveBeenCalled() - - fireEvent.change(screen.getByRole('textbox'), { target: { value: 'host' } }) - expect(AgentLaunchSetupButton).toHaveBeenCalled() - fireEvent.click(screen.getByRole('menuitem')) - expect(launch).toHaveBeenCalledWith( - expect.objectContaining({ agent: 'codex', kind: 'cli' }), - { - agent: 'codex', - surface: 'terminal', - choices: { model: 'host-model' }, - acceptedEdits: { choices: { model: 'host-model', options: {} } }, - delegated: [], - literalKeys: [] - }, - codex - ) -}) diff --git a/src/renderer/src/components/automations/automation-captured-owner.test.ts b/src/renderer/src/components/automations/automation-captured-owner.test.ts index 4d7c268fa66..bfed0d073d2 100644 --- a/src/renderer/src/components/automations/automation-captured-owner.test.ts +++ b/src/renderer/src/components/automations/automation-captured-owner.test.ts @@ -1,63 +1,9 @@ import { describe, expect, it } from 'vitest' -import type { Automation } from '../../../../shared/automations/automation-definition' -import type { AutomationOwnerRef } from '../../../../shared/automations/ownership/automation-owner-ref' -import type { AutomationHostRow } from './automation-host-cache-types' import { automationActionAvailability, - captureAutomationOwners, isAutomationActionEnabled } from './automation-captured-owner' -const DESKTOP_SELF: AutomationOwnerRef = { - authority: { kind: 'desktop' }, - selector: { kind: 'self' } -} - -const RUNTIME_SELF: AutomationOwnerRef = { - authority: { kind: 'runtime', environmentId: 'gpu', pairingRevision: 1 }, - selector: { kind: 'self' } -} - -function automation(overrides: Partial = {}): Automation { - return { - id: 'a-1', - name: 'Nightly', - prompt: 'sweep', - precheck: null, - agentId: 'claude', - runContext: null, - projectId: 'repo-1', - executionTargetType: 'local', - executionTargetId: 'local', - schedulerOwner: 'local_host_service', - workspaceMode: 'new_per_run', - workspaceId: null, - baseBranch: null, - reuseSession: false, - timezone: 'UTC', - rrule: 'FREQ=DAILY', - dtstart: 1, - enabled: true, - nextRunAt: 2, - missedRunPolicy: 'run_once_within_grace', - missedRunGraceMinutes: 720, - createdAt: 1, - updatedAt: 1, - ...overrides - } -} - -function row(overrides: Partial): AutomationHostRow { - return { - automation: automation(), - owner: DESKTOP_SELF, - selector: { kind: 'self' }, - usageSummary: null, - usageKnown: false, - ...overrides - } -} - describe('captured owner availability', () => { it('keeps delete and pause fenceable on an orphan while refusing to run it', () => { const orphan = { owner: null, selector: { kind: 'orphan' as const, issue: 'gone' } } @@ -71,16 +17,4 @@ describe('captured owner availability', () => { expect(isAutomationActionEnabled(orphan, 'run')).toBe(false) expect(isAutomationActionEnabled(orphan, 'delete')).toBe(true) }) - - it("keeps both authorities' owners when they hold the same automation ID", () => { - const captured = captureAutomationOwners([ - { rowKey: 'row|desktop|a-1', row: row({ automation: automation({ id: 'a-1' }) }) }, - { - rowKey: 'row|runtime|a-1', - row: row({ automation: automation({ id: 'a-1' }), owner: RUNTIME_SELF }) - } - ]) - expect(captured.get('row|desktop|a-1')?.owner).toEqual(DESKTOP_SELF) - expect(captured.get('row|runtime|a-1')?.owner).toEqual(RUNTIME_SELF) - }) }) diff --git a/src/renderer/src/components/automations/automation-host-invalidation.test.ts b/src/renderer/src/components/automations/automation-host-invalidation.test.ts deleted file mode 100644 index 85559723498..00000000000 --- a/src/renderer/src/components/automations/automation-host-invalidation.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -/** Scoped events invalidate one entry, unscoped events their authority, and neither crosses authority boundaries. */ - -import { describe, expect, it, vi } from 'vitest' -import type { Automation } from '../../../../shared/automations/automation-definition' - -import type { - StableAutomationAuthorityRef, - StableAutomationCatalogRef -} from '../../../../shared/automations/ownership/automation-owner-ref' -import { createAutomationHostCache, type AutomationHostCache } from './automation-host-cache' -import type { AutomationHostRow } from './automation-host-cache-types' -import { createAutomationHostInvalidation } from './automation-host-invalidation' - -const DESKTOP: StableAutomationAuthorityRef = { kind: 'desktop' } -const RUNTIME: StableAutomationAuthorityRef = { kind: 'runtime', environmentId: 'env-1' } -const DESKTOP_SELF: StableAutomationCatalogRef = { authority: DESKTOP, selector: { kind: 'self' } } -const DESKTOP_SSH: StableAutomationCatalogRef = { - authority: DESKTOP, - selector: { kind: 'ssh', targetId: 'target-1' } -} -const RUNTIME_SELF: StableAutomationCatalogRef = { authority: RUNTIME, selector: { kind: 'self' } } - -function row(id: string): AutomationHostRow { - return { - automation: { id } as Automation, - owner: null, - selector: { kind: 'self' }, - usageSummary: null, - usageKnown: false - } -} - -function seeded(): AutomationHostCache { - const cache = createAutomationHostCache({ - catalogGeneration: () => 0, - connectionGeneration: () => 0 - }) - for (const ref of [DESKTOP_SELF, DESKTOP_SSH, RUNTIME_SELF]) { - cache.commit(cache.beginRequest(ref), { rows: [row('a')] }) - } - return cache -} - -describe('automation host invalidation', () => { - it('drops queued work once disposed', async () => { - const cache = seeded() - const onInvalidated = vi.fn() - const invalidation = createAutomationHostInvalidation({ cache, onInvalidated }) - invalidation.handle({ authority: DESKTOP }) - invalidation.dispose() - await Promise.resolve() - expect(onInvalidated).not.toHaveBeenCalled() - }) -}) diff --git a/src/renderer/src/components/automations/automation-list-view.test.ts b/src/renderer/src/components/automations/automation-list-view.test.ts deleted file mode 100644 index 1a408982695..00000000000 --- a/src/renderer/src/components/automations/automation-list-view.test.ts +++ /dev/null @@ -1,190 +0,0 @@ -import { describe, expect, it } from 'vitest' -import type { Automation } from '../../../../shared/automations/automation-definition' -import type { AutomationRunStatus } from '../../../../shared/automations/history/automation-run' -import type { - ExternalAutomationJob, - ExternalAutomationManager -} from '../../../../shared/automations/external/external-automation' -import { - EMPTY_AUTOMATION_LIST_FILTER, - filterAutomationListRows, - filterExternalAutomationListEntries, - type AutomationListFilter -} from './automation-list-view' -import { hostStableKey } from '../../../../shared/automations/ownership/automation-owner-key' -import type { AutomationListRow } from './automation-list-row-identity' -import type { ExternalAutomationListEntry } from './external-automation-list-entries' - -function makeAutomation(overrides: Partial = {}): Automation { - return { - id: 'automation-1', - name: 'Zebra job', - prompt: 'run', - precheck: null, - agentId: 'codex', - projectId: 'repo-1', - executionTargetType: 'local', - executionTargetId: 'local', - schedulerOwner: 'local_host_service', - workspaceMode: 'existing', - workspaceId: 'worktree-1', - baseBranch: null, - reuseSession: false, - timezone: 'UTC', - rrule: 'FREQ=DAILY', - dtstart: 1, - enabled: true, - nextRunAt: 2, - missedRunPolicy: 'run_once_within_grace', - missedRunGraceMinutes: 720, - createdAt: 1, - updatedAt: 1, - ...overrides - } -} - -function makeExternalEntry( - overrides: Partial = {} -): ExternalAutomationListEntry { - const manager: ExternalAutomationManager = { - id: 'manager-1', - provider: 'openclaw', - label: 'Local OpenClaw', - targetLabel: 'Local', - target: { type: 'local' }, - status: 'available', - error: null, - canManage: true, - jobs: [] - } - const job: ExternalAutomationJob = { - id: 'job-1', - managerId: manager.id, - provider: 'openclaw', - name: 'Alpha digest', - schedule: '0 9 * * 1-5', - rawSchedule: null, - enabled: true, - state: 'enabled', - prompt: null, - promptPreview: '', - nextRunAt: null, - lastRunAt: '2026-08-12T01:00:00Z', - lastStatus: 'failed', - lastError: null, - workdir: null, - runCount: 1, - runs: [], - ...overrides - } - return { - key: `${manager.id}:${job.id}`, - scope: { - owner: { authority: { kind: 'desktop' }, selector: { kind: 'self' } }, - provider: manager.provider - }, - manager, - job - } -} - -/** A catalog row with an optional projected last-run status, keyed like a real host row. */ -function makeCatalogRow( - id: string, - overrides: Partial = {}, - lastRunStatus?: AutomationRunStatus -): AutomationListRow { - return { - key: `row|host|${id}`, - automation: makeAutomation({ id, ...overrides }), - hostLabel: 'This computer', - usageSummary: lastRunStatus - ? { - knownRuns: 1, - unavailableRuns: 0, - inputTokens: 0, - outputTokens: 0, - cacheTokens: 0, - reasoningOutputTokens: 0, - totalTokens: 0, - estimatedCostUsd: null, - lastRunStatus, - lastRunAt: 111 - } - : null - } -} - -describe('automation-list-view', () => { - it('filters catalog rows by status, agent, and the projected last-run status', () => { - const rows = [ - makeCatalogRow('paused-codex', { enabled: false, agentId: 'codex' }), - makeCatalogRow('failed-claude', { agentId: 'claude' }, 'dispatch_failed'), - makeCatalogRow('succeeded-codex', { agentId: 'codex' }, 'completed'), - makeCatalogRow('never-codex', { agentId: 'codex' }) - ] - const ids = (filter: Partial) => - filterAutomationListRows(rows, { - ...EMPTY_AUTOMATION_LIST_FILTER, - ...filter - }).map((row) => row.automation.id) - - expect(ids({ status: 'paused' })).toEqual(['paused-codex']) - expect(ids({ agentIds: ['claude'] })).toEqual(['failed-claude']) - expect(ids({ lastRun: 'failed' })).toEqual(['failed-claude']) - expect(ids({ lastRun: 'succeeded' })).toEqual(['succeeded-codex']) - expect(ids({ lastRun: 'never' })).toEqual(['paused-codex', 'never-codex']) - // Inactive filter keeps the input identity so nothing re-renders for it. - expect(filterAutomationListRows(rows, EMPTY_AUTOMATION_LIST_FILTER)).toBe(rows) - }) - - it('narrows rows to any of the selected hosts and drops pre-catalog rows', () => { - const hostRow = (id: string, targetId: string | null): AutomationListRow => ({ - key: `row|${targetId ?? 'unscoped'}|${id}`, - automation: makeAutomation({ id }), - catalogRef: - targetId === null - ? null - : { - authority: { kind: 'desktop' }, - selector: { kind: 'ssh', targetId } - }, - hostLabel: targetId ?? '', - usageSummary: null - }) - const rows = [hostRow('on-a', 'ssh-a'), hostRow('on-b', 'ssh-b'), hostRow('legacy', null)] - const keyOf = (row: AutomationListRow): string => - row.catalogRef ? hostStableKey(row.catalogRef) : '' - const ids = (hostStableKeys: readonly string[]) => - filterAutomationListRows(rows, { - ...EMPTY_AUTOMATION_LIST_FILTER, - hostStableKeys - }).map((row) => row.automation.id) - - // Multi-select is any-of; a pre-catalog row names no host and is excluded. - expect(ids([keyOf(rows[0]), keyOf(rows[1])])).toEqual(['on-a', 'on-b']) - expect(ids([keyOf(rows[1])])).toEqual(['on-b']) - }) - - it('excludes external jobs from any agent filter and honors their status', () => { - const entries = [makeExternalEntry({ enabled: false, state: 'disabled' })] - expect( - filterExternalAutomationListEntries(entries, { - ...EMPTY_AUTOMATION_LIST_FILTER, - agentIds: ['codex'] - }) - ).toEqual([]) - expect( - filterExternalAutomationListEntries(entries, { - ...EMPTY_AUTOMATION_LIST_FILTER, - status: 'paused' - }) - ).toHaveLength(1) - expect( - filterExternalAutomationListEntries(entries, { - ...EMPTY_AUTOMATION_LIST_FILTER, - status: 'enabled' - }) - ).toEqual([]) - }) -}) diff --git a/src/renderer/src/components/automations/automation-usage-model.test.ts b/src/renderer/src/components/automations/automation-usage-model.test.ts deleted file mode 100644 index a4ccbc78c20..00000000000 --- a/src/renderer/src/components/automations/automation-usage-model.test.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { describe, expect, it } from 'vitest' -import type { AutomationRun } from '../../../../shared/automations/history/automation-run' -import { summarizeAutomationRunUsage } from '../../../../shared/automations/history/automation-usage-summary' - -function makeRun(overrides: Partial): AutomationRun { - return { - id: 'run-1', - automationId: 'automation-1', - title: 'Run 1', - scheduledFor: 1, - status: 'completed', - trigger: 'manual', - workspaceId: 'wt1', - sessionKind: 'terminal', - chatSessionId: null, - terminalSessionId: 'tab-1', - terminalPaneKey: 'tab-1:11111111-1111-4111-8111-111111111111', - terminalPtyId: 'pty-1', - outputSnapshot: null, - precheckResult: null, - usage: null, - error: null, - startedAt: 1, - dispatchedAt: 1, - createdAt: 1, - ...overrides - } -} - -describe('automation usage model', () => { - it('rolls known run usage up while preserving unavailable coverage', () => { - const summary = summarizeAutomationRunUsage([ - makeRun({ - usage: { - status: 'known', - provider: 'codex', - model: 'gpt-5.4', - inputTokens: 1000, - outputTokens: 300, - cacheReadTokens: 400, - cacheWriteTokens: null, - reasoningOutputTokens: 50, - totalTokens: 1350, - estimatedCostUsd: 0.0042, - estimatedCostSource: 'api_equivalent', - providerSessionId: 'session-1', - attribution: 'provider_session_time_window', - collectedAt: 1, - unavailableReason: null, - unavailableMessage: null - } - }), - makeRun({ - id: 'run-3', - createdAt: 3, - dispatchedAt: 3, - usage: { - status: 'known', - provider: 'claude', - model: 'sonnet', - inputTokens: 200, - outputTokens: 700, - cacheReadTokens: null, - cacheWriteTokens: 50, - reasoningOutputTokens: null, - totalTokens: 900, - estimatedCostUsd: 0.0042, - estimatedCostSource: 'api_equivalent', - providerSessionId: 'session-3', - attribution: 'provider_session_time_window', - collectedAt: 3, - unavailableReason: null, - unavailableMessage: null - } - }), - makeRun({ - id: 'run-2', - usage: { - status: 'unavailable', - provider: 'claude', - model: null, - inputTokens: null, - outputTokens: null, - cacheReadTokens: null, - cacheWriteTokens: null, - reasoningOutputTokens: null, - totalTokens: null, - estimatedCostUsd: null, - estimatedCostSource: null, - providerSessionId: null, - attribution: null, - collectedAt: 2, - unavailableReason: 'no_matching_session', - unavailableMessage: 'No matching session' - } - }) - ]) - - expect(summary).toEqual({ - knownRuns: 2, - unavailableRuns: 1, - inputTokens: 1200, - outputTokens: 1000, - cacheTokens: 450, - reasoningOutputTokens: 50, - totalTokens: 2250, - estimatedCostUsd: 0.0084, - lastRunStatus: 'completed', - lastRunAt: 3 - }) - }) -}) diff --git a/src/renderer/src/components/automations/external-automation-list-entries.test.ts b/src/renderer/src/components/automations/external-automation-list-entries.test.ts deleted file mode 100644 index 6f1e6a6dab3..00000000000 --- a/src/renderer/src/components/automations/external-automation-list-entries.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { describe, expect, it } from 'vitest' -import type { ExternalAutomationManager } from '../../../../shared/automations/external/external-automation' -import { buildExternalAutomationListEntries } from './external-automation-list-entries' -import type { ScopedExternalAutomationManager } from './external-automation-scope-client' - -function manager(overrides: Partial = {}): ExternalAutomationManager { - return { - id: 'openclaw:ssh:openclaw', - provider: 'openclaw', - label: 'OpenClaw on openclaw', - targetLabel: 'openclaw', - target: { type: 'ssh', connectionId: 'openclaw' }, - status: 'unavailable', - error: 'SSH target is not connected.', - canManage: false, - jobs: [], - ...overrides - } -} - -function job(id = 'job-1'): ExternalAutomationManager['jobs'][number] { - return { - id, - managerId: 'openclaw:ssh:openclaw', - provider: 'openclaw', - name: 'Nightly', - schedule: '0 9 * * *', - rawSchedule: '0 9 * * *', - enabled: true, - state: 'active', - prompt: 'do work', - promptPreview: 'do work', - nextRunAt: null, - lastRunAt: null, - lastStatus: null, - lastError: null, - workdir: null, - runCount: 1, - runs: [] - } -} - -function scoped( - environmentId: string | null, - overrides: Partial = {} -): ScopedExternalAutomationManager { - return { - scope: { - owner: { - authority: - environmentId === null - ? { kind: 'desktop' } - : { kind: 'runtime', environmentId, pairingRevision: 1 }, - selector: { kind: 'self' } - }, - provider: 'openclaw' - }, - manager: manager(overrides) - } -} - -describe('buildExternalAutomationListEntries', () => { - it('separates identical manager and job IDs reported by two authorities', () => { - // Manager/job IDs collide across authorities; qualify row identity to preserve independent selection, dialogs and reconciliation. - const entries = buildExternalAutomationListEntries([ - scoped(null, { id: 'openclaw:local', target: { type: 'local' }, jobs: [job()] }), - scoped('env-7', { id: 'openclaw:local', target: { type: 'local' }, jobs: [job()] }) - ]) - - expect(entries).toHaveLength(2) - expect(entries[0]?.key).not.toBe(entries[1]?.key) - }) -}) diff --git a/src/renderer/src/components/browser-pane/ClientHostedBrowserPagePane.test.tsx b/src/renderer/src/components/browser-pane/ClientHostedBrowserPagePane.test.tsx index 6fa362ff9bf..bab415abcf3 100644 --- a/src/renderer/src/components/browser-pane/ClientHostedBrowserPagePane.test.tsx +++ b/src/renderer/src/components/browser-pane/ClientHostedBrowserPagePane.test.tsx @@ -197,96 +197,6 @@ describe('ClientHostedBrowserPagePane', () => { expect(screen.getByText('Client-hosted browser unavailable')).not.toBeNull() expect(document.querySelector('webview')).toBeNull() }) - - it('requests the one-time intro tour on the first active client-hosted page', async () => { - vi.useFakeTimers() - const { useAppStore } = await import('@/store') - const prior = { - persistedUIReady: useAppStore.getState().persistedUIReady, - contextualToursSeenIds: useAppStore.getState().contextualToursSeenIds, - activeContextualTourId: useAppStore.getState().activeContextualTourId - } - useAppStore.setState({ - persistedUIReady: true, - contextualToursSeenIds: [], - activeContextualTourId: null - }) - const { webview } = createWebview() - mocks.attach.mockReturnValue(retainedAttachment(webview)) - try { - render( - - ) - // Why: happy-dom rects are zero-sized and the tour gate requires a measurable target. - const target = document.querySelector( - '[data-contextual-tour-target="client-hosted-browser-controls"]' - ) - expect(target).not.toBeNull() - target!.getBoundingClientRect = () => new DOMRect(0, 0, 400, 32) - await act(async () => { - await vi.advanceTimersByTimeAsync(0) - }) - expect(useAppStore.getState().activeContextualTourId).toBe('client-hosted-browser') - } finally { - useAppStore.setState(prior) - vi.useRealTimers() - } - }) - - it('never re-requests the intro tour once it has been seen', async () => { - vi.useFakeTimers() - const { useAppStore } = await import('@/store') - const prior = { - persistedUIReady: useAppStore.getState().persistedUIReady, - contextualToursSeenIds: useAppStore.getState().contextualToursSeenIds, - activeContextualTourId: useAppStore.getState().activeContextualTourId - } - useAppStore.setState({ - persistedUIReady: true, - contextualToursSeenIds: ['client-hosted-browser'], - activeContextualTourId: null - }) - const { webview } = createWebview() - mocks.attach.mockReturnValue(retainedAttachment(webview)) - try { - render( - - ) - // Why: same measurable target as the positive case — only seenIds differs. - const target = document.querySelector( - '[data-contextual-tour-target="client-hosted-browser-controls"]' - ) - expect(target).not.toBeNull() - target!.getBoundingClientRect = () => new DOMRect(0, 0, 400, 32) - await act(async () => { - await vi.advanceTimersByTimeAsync(0) - }) - expect(useAppStore.getState().activeContextualTourId).toBeNull() - } finally { - useAppStore.setState(prior) - vi.useRealTimers() - } - }) }) describe('ClientHostedBrowserPagePane address bar parity', () => { diff --git a/src/renderer/src/components/browser-pane/annotate/browser-annotation-output.test.ts b/src/renderer/src/components/browser-pane/annotate/browser-annotation-output.test.ts index c2b5fdb82f7..b0079dabb8e 100644 --- a/src/renderer/src/components/browser-pane/annotate/browser-annotation-output.test.ts +++ b/src/renderer/src/components/browser-pane/annotate/browser-annotation-output.test.ts @@ -103,18 +103,6 @@ describe('formatBrowserAnnotationsAsMarkdown', () => { expect(markdown).not.toContain('\n## injected') }) - it('heads document preview annotations with the Windows file path', () => { - const annotation = makeAnnotation() - const sanitizedUrl = 'C:\\repo\\docs\\report.html' - const markdown = formatBrowserAnnotationsAsMarkdown([ - makeAnnotation({ - payload: { ...annotation.payload, page: { ...annotation.payload.page, sanitizedUrl } } - }) - ]) - - expect(markdown).toContain(`## Design Feedback: ${sanitizedUrl}`) - }) - it('does not split surrogate pairs at the inline annotation cap', () => { const annotation = makeAnnotation() const selectedText = `${'x'.repeat(BROWSER_ANNOTATION_INLINE_TEXT_MAX_LENGTH - 1)}😀tail` diff --git a/src/renderer/src/components/browser-pane/annotate/browser-grab-toast-owner-cleanup.test.tsx b/src/renderer/src/components/browser-pane/annotate/browser-grab-toast-owner-cleanup.test.tsx index dc279e30bd6..75877305444 100644 --- a/src/renderer/src/components/browser-pane/annotate/browser-grab-toast-owner-cleanup.test.tsx +++ b/src/renderer/src/components/browser-pane/annotate/browser-grab-toast-owner-cleanup.test.tsx @@ -287,27 +287,6 @@ describe('browser grab toast owner', () => { expect(next.view.result.current.grabToast).toBeNull() expect(vi.getTimerCount()).toBe(0) }) - it.each(['clipboard', 'interaction'] as const)( - 'honors reentrant cleanup during %s delivery', - async (point) => { - const payload = makePayload() - extract.mockResolvedValue({ ok: true, payload }) - const { view, bounds } = mount() - if (point === 'clipboard') { - writes.mockImplementationOnce(() => view.unmount()) - } else { - state.recordFeatureInteraction.mockImplementationOnce(() => view.unmount()) - } - await act(async () => { - view.result.current.handleGrabActionShortcut('c') - await Promise.resolve() - }) - expect(writes.mock.calls).toEqual([[formatGrabPayloadAsText(payload)]]) - expect(state.recordFeatureInteraction.mock.calls).toEqual([['browser-grab']]) - expect(bounds).not.toHaveBeenCalled() - expect(vi.getTimerCount()).toBe(0) - } - ) it('preserves latest target reads and screenshot completion after cleanup', async () => { const extraction = deferred() const image = deferred<{ ok: true; screenshot: typeof screenshot }>() diff --git a/src/renderer/src/components/browser-pane/browser-reopen-on-server.test.ts b/src/renderer/src/components/browser-pane/browser-reopen-on-server.test.ts deleted file mode 100644 index ea8e840138c..00000000000 --- a/src/renderer/src/components/browser-pane/browser-reopen-on-server.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import { ORCA_BROWSER_BLANK_URL } from '../../../../shared/constants' - -const mocks = vi.hoisted(() => ({ - createWebRuntimeSessionBrowserTab: vi.fn(async (_args: Record) => true) -})) - -vi.mock('@/runtime/browser/creation/web-runtime-browser-creation', () => ({ - createWebRuntimeSessionBrowserTab: mocks.createWebRuntimeSessionBrowserTab -})) - -import { resolveBrowserReopenOnServerUrl } from './browser-reopen-on-server' - -describe('resolveBrowserReopenOnServerUrl', () => { - it('reopens blank rather than replaying something the new engine cannot restore', () => { - expect(resolveBrowserReopenOnServerUrl(ORCA_BROWSER_BLANK_URL)).toBeUndefined() - expect(resolveBrowserReopenOnServerUrl('about:blank')).toBeUndefined() - expect(resolveBrowserReopenOnServerUrl('file:///etc/hosts')).toBeUndefined() - expect(resolveBrowserReopenOnServerUrl('')).toBeUndefined() - expect(resolveBrowserReopenOnServerUrl(null)).toBeUndefined() - expect(resolveBrowserReopenOnServerUrl(undefined)).toBeUndefined() - }) -}) diff --git a/src/renderer/src/components/browser-pane/workspace-doc/doc-preview-webview-attach.test.ts b/src/renderer/src/components/browser-pane/workspace-doc/doc-preview-webview-attach.test.ts deleted file mode 100644 index 243b075427a..00000000000 --- a/src/renderer/src/components/browser-pane/workspace-doc/doc-preview-webview-attach.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -// @vitest-environment happy-dom -import { expect, it, vi } from 'vitest' -import { publishNativeChatVisualGuestTheme } from '@/components/native-chat/media/visuals/NativeChatVisualGuestThemePublisher' - -vi.mock('@/components/native-chat/media/visuals/NativeChatVisualGuestThemePublisher', () => ({ - publishNativeChatVisualGuestTheme: vi.fn() -})) -import { acquireWebviewsDragPassthrough } from '../host-guest/webview-drag-passthrough' -import { webviewRegistry } from '../host-guest/webview-registry' -import { attachDocPreviewWebview } from './doc-preview-webview-attach' - -function attach(previewId: string, container: HTMLDivElement) { - return attachDocPreviewWebview({ - previewId, - container, - url: 'orca-preview://grant/index.html', - ariaLabel: 'HTML preview', - onLoadStarted: vi.fn(), - onLoadStopped: vi.fn(), - onLoadFailed: vi.fn(), - onNavigated: vi.fn(), - onTitleUpdated: vi.fn(), - onViewportMessage: vi.fn() - }) -} - -it('restores pointer input when a drag ends after attaching a document preview', () => { - const container = document.createElement('div') - document.body.appendChild(container) - const append = vi.spyOn(container, 'appendChild') - append.mockImplementation((node) => { - expect(publishNativeChatVisualGuestTheme).toHaveBeenCalledTimes(1) - expect((node as HTMLElement).style.pointerEvents).toBe('none') - return Node.prototype.appendChild.call(container, node) - }) - const release = acquireWebviewsDragPassthrough() - const attached = attach('preview-drag', container) - - try { - expect(webviewRegistry.get('preview-drag')).toBe(attached.webview) - expect(attached.webview.style.pointerEvents).toBe('none') - release() - expect(attached.webview.style.pointerEvents).toBe('') - } finally { - release() - attached.detach() - container.remove() - append.mockRestore() - } - expect(webviewRegistry.has('preview-drag')).toBe(false) -}) diff --git a/src/renderer/src/components/contextual-tours/contextual-tour-overlay-measurement.test.ts b/src/renderer/src/components/contextual-tours/contextual-tour-overlay-measurement.test.ts deleted file mode 100644 index 1d68afea4c9..00000000000 --- a/src/renderer/src/components/contextual-tours/contextual-tour-overlay-measurement.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { describe, expect, it } from 'vitest' -import { getContextualTour } from '../../../../shared/contextual-tours' -import { getContextualTourDisplayProgress } from './contextual-tour-overlay-measurement' - -const tour = getContextualTour('workspace-agent-sessions') - -function progress(visibleStepIndexes: number[], stepIndex: number) { - return getContextualTourDisplayProgress({ - tour, - visibleStepIndexes, - stepIndex, - activeStep: tour.steps[stepIndex] - }) -} - -describe('workspace agent sessions tour progress', () => { - it('keeps the split lesson counted after a chat split focuses its landing', () => { - expect(progress([0, 1, 3], 1)).toEqual({ current: 2, total: 3 }) - expect(progress([0, 3], 3)).toEqual({ current: 3, total: 3 }) - }) - - it('counts a still-visible split lesson once', () => { - expect(progress([0, 2, 3], 3)).toEqual({ current: 3, total: 3 }) - }) -}) diff --git a/src/renderer/src/components/editor/check-details/panel/CheckRunDetailsPanel.copy.test.tsx b/src/renderer/src/components/editor/check-details/panel/CheckRunDetailsPanel.copy.test.tsx deleted file mode 100644 index ca923ea65b8..00000000000 --- a/src/renderer/src/components/editor/check-details/panel/CheckRunDetailsPanel.copy.test.tsx +++ /dev/null @@ -1,306 +0,0 @@ -// @vitest-environment happy-dom - -import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { TooltipProvider } from '@/components/ui/tooltip' -import type { PRCheckDetail, PRCheckRunDetails } from '../../../../../../shared/github/check-types' -import { CheckRunDetailsPanel } from './CheckRunDetailsPanel' - -import { useAppStore } from '@/store/index' -import { createEditorTabsStore } from '@/store/slices/editor/editor-slice-test-harness' -import { scrollTopCache } from '@/lib/scroll-cache' -import { readingCheck, readingDetails } from '../reading/check-details-reading-fixture' -import { StrictMode } from 'react' - -const writeClipboardText = vi.fn<(text: string) => Promise>() -const prCheckDetails = vi.fn() -const runtimeCall = vi.fn() - -vi.mock('@/i18n/i18n', () => ({ - getIntlLocale: () => 'en', - i18n: { language: 'en' }, - translate: (_key: string, fallback: string, values?: Record) => - fallback.replace(/{{(\w+)}}/g, (_match, key) => String(values?.[key] ?? '')) -})) - -vi.mock('@/components/sidebar/CommentMarkdown', () => ({ - default: ({ content }: { content: string }) => content -})) - -vi.mock('@/components/right-sidebar/source-control/panel/source-control-fix-split-button', () => ({ - SourceControlFixSplitButton: () => null -})) - -vi.mock('../actions/check-run-details-fix-with-ai', () => ({ - useCheckRunDetailsFixWithAI: () => ({ - canFixWithAI: false, - disabledReason: null, - isFixing: false, - fixPrompt: '', - repoId: null, - connectionId: null, - launchPlatform: null, - savedAgentId: null, - savedCommandInputTemplate: null, - savedAgentArgs: [], - saveLaunchActionDefault: vi.fn(), - openSourceControlAiSettings: vi.fn(), - fixWithAI: vi.fn() - }) -})) - -afterEach(() => { - cleanup() - vi.restoreAllMocks() -}) - -beforeEach(() => { - writeClipboardText.mockReset().mockResolvedValue(undefined) - prCheckDetails.mockReset() - runtimeCall.mockReset() - Object.assign(window, { - api: { - ui: { writeClipboardText }, - gh: { prCheckDetails }, - runtime: { call: runtimeCall }, - runtimeEnvironments: { call: runtimeCall, subscribe: runtimeCall } - } - }) -}) - -const check: PRCheckDetail = { - name: 'package', - status: 'completed', - conclusion: 'failure', - url: null, - checkRunId: 42 -} - -const details: PRCheckRunDetails = { - name: 'package', - status: 'completed', - conclusion: 'failure', - url: null, - detailsUrl: null, - startedAt: null, - completedAt: null, - title: 'Build failed', - summary: 'One test failed.', - text: 'Run the suite locally.', - annotations: [ - { - path: 'src/main.ts', - startLine: 10, - endLine: 10, - annotationLevel: 'failure', - title: 'Type error', - message: 'Expected string', - rawDetails: 'Actual: number' - } - ], - jobs: [ - { - id: 1, - name: 'unit', - status: 'completed', - conclusion: 'failure', - startedAt: null, - completedAt: null, - url: null, - logTail: 'AssertionError: expected true', - steps: [ - { - name: 'test', - status: 'completed', - conclusion: 'failure', - startedAt: null, - completedAt: null - } - ] - } - ] -} - -function panel(payload = details, loading = false, error: string | null = null) { - return ( - - {}} - /> - - ) -} - -describe('failure presentation and combined refresh', () => { - afterEach(() => { - vi.useRealTimers() - vi.restoreAllMocks() - }) - - it('resets only the disclosure when a successful setup-only job becomes failed', () => { - const setup = { - name: 'setup only', - status: 'completed', - conclusion: 'success', - startedAt: null, - completedAt: null - } - const first = { - ...details, - conclusion: 'success', - jobs: [{ ...details.jobs[0], conclusion: 'success', steps: [setup] }] - } - const { rerender, container } = render(panel(first)) - const pre = container.querySelector('pre.max-h-72') - expect(screen.getByRole('button', { name: '1 succeeded' }).getAttribute('aria-expanded')).toBe( - 'true' - ) - rerender( - panel({ - ...first, - conclusion: 'failure', - jobs: [{ ...first.jobs[0], conclusion: 'startup_failure' }] - }) - ) - expect(screen.getByRole('button', { name: '1 succeeded' }).getAttribute('aria-expanded')).toBe( - 'false' - ) - expect(container.querySelector('pre.min-h-48')).toBe(pre) - expect(screen.queryByText('setup only')).toBeNull() - }) - - it('keeps stable job viewers when equal-named jobs reorder under new payload objects', () => { - const one = details.jobs[0] - const two = { ...one, id: 2, logTail: 'Error: second synthetic failure' } - const { container, rerender } = render(panel({ ...details, jobs: [one, two] })) - const first = container.querySelector('pre') - if (!first) { - throw new Error('Missing job viewer') - } - first.scrollTop = 180 - rerender(panel({ ...details, jobs: [{ ...two }, { ...one }] })) - const viewers = [...container.querySelectorAll('pre')] - expect(viewers[1]).toBe(first) - expect(first.scrollTop).toBe(180) - }) -}) - -function StoredDetailsPanel({ scope = 'real-pane' }: { scope?: string }) { - const file = useAppStore((state) => state.openFiles[0]) - const snapshot = file?.checkRunDetails - if (!snapshot) { - return null - } - return ( - - { - void useAppStore.getState().reloadOpenCheckRunDetailsTab(file.id) - }} - /> - - ) -} - -describe('mounted store-driven details refresh', () => { - const original = useAppStore.getState() - afterEach(() => { - cleanup() - useAppStore.setState(original, true) - scrollTopCache.clear() - vi.restoreAllMocks() - }) - it.each(['equal', 'null', 'throw'])( - 'preserves the actual viewer and both offsets through deferred %s requests and Strict Mode', - async (result) => { - const fixture = createEditorTabsStore().getState() - const payload = readingDetails(undefined, 'available') - let resolve: (details: PRCheckRunDetails | null) => void = () => {} - let reject: (reason: Error) => void = () => {} - const fetch = vi.fn( - () => - new Promise((done, fail) => { - resolve = done - reject = fail - }) - ) - useAppStore.setState({ - openFiles: [], - repos: fixture.repos, - worktreesByRepo: fixture.worktreesByRepo, - fetchPRCheckDetails: fetch - }) - useAppStore.getState().openCheckRunDetails('wt-1', 'context', readingCheck, { - details: payload, - loading: false, - error: null - }) - vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(400) - vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockReturnValue(200) - vi.spyOn(HTMLElement.prototype, 'scrollHeight', 'get').mockReturnValue(2000) - vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue( - new DOMRect(0, 0, 400, 200) - ) - const { container, unmount } = render( - - - - ) - const pre = container.querySelector('pre') - const outer = container.querySelector('[aria-busy].overflow-y-auto') - if (!(pre instanceof HTMLPreElement) || !(outer instanceof HTMLDivElement)) { - throw new Error('Missing reading nodes') - } - fireEvent.wheel(pre) - pre.scrollTop = 180 - fireEvent.scroll(pre) - outer.scrollTop = 320 - fireEvent.scroll(outer) - fireEvent.click(screen.getByRole('button', { name: 'Refresh' })) - expect(fetch).toHaveBeenCalledTimes(1) - expect(container.querySelector('pre')).toBe(pre) - expect(pre.scrollTop).toBe(180) - expect(outer.scrollTop).toBe(320) - await act(async () => { - if (result === 'throw') { - reject(new Error('SECRET')) - } else { - resolve( - result === 'null' ? null : { ...payload, jobs: payload.jobs.map((job) => ({ ...job })) } - ) - } - }) - expect(container.querySelector('pre')).toBe(pre) - expect(pre.scrollTop).toBe(180) - expect(outer.scrollTop).toBe(320) - if (result !== 'equal') { - expect(screen.getByRole('alert').textContent).not.toContain('SECRET') - } - unmount() - const remount = render( - - - - ) - expect(remount.container.querySelector('pre')?.scrollTop).toBe(180) - const remountedOuter = remount.container.querySelector('[aria-busy].overflow-y-auto') - expect(remountedOuter?.scrollTop).toBe(320) - expect(fetch).toHaveBeenCalledTimes(1) - } - ) -}) diff --git a/src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.test.ts b/src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.test.ts deleted file mode 100644 index fb2ded7366f..00000000000 --- a/src/renderer/src/components/native-chat/messages/notices/agent-session-failure-words-text.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { afterEach, expect, it } from 'vitest' -import { i18n } from '@/i18n/i18n' -import { agentSessionFailureSentence } from '../../../../../../shared/agent-session/failures/agent-session-failure-words' -import { sayAgentSessionFailureTranslated } from './agent-session-failure-words-text' - -afterEach(async () => { - await i18n.changeLanguage('en') -}) - -it.each([ - [ - 'en', - 'OMP has no model selected. Check your model selection and OMP sign-in, then send again.', - "OMP couldn't use @worker_be. Update OMP to 18.1.9 or later to keep it after a restart, or pick another model and send again." - ], - [ - 'zh', - 'OMP 没有选择模型。请检查模型选择和 OMP 登录状态,然后重新发送。', - expect.stringContaining('请将 OMP 更新到 18.1.9 或更高版本') - ] -] as const)( - 'words a missing OMP model and the update hint on desktop (%s)', - async (locale, none, update) => { - await i18n.changeLanguage(locale) - const say = (fact: Parameters[0]) => - agentSessionFailureSentence( - fact, - 'rejection', - { agentName: 'OMP' }, - sayAgentSessionFailureTranslated - ) - expect(say({ kind: 'modelNotSelected' })).toBe(none) - expect( - say({ - kind: 'modelUnavailable', - detail: { text: '@worker_be', audience: 'log' }, - agentUpdate: '18.1.9' - }) - ).toEqual(update) - } -) diff --git a/src/renderer/src/components/native-chat/subagents/native-chat-subagent-handoff-copy-budget.test.ts b/src/renderer/src/components/native-chat/subagents/native-chat-subagent-handoff-copy-budget.test.ts index 4c0e340490d..d2b4d8daad6 100644 --- a/src/renderer/src/components/native-chat/subagents/native-chat-subagent-handoff-copy-budget.test.ts +++ b/src/renderer/src/components/native-chat/subagents/native-chat-subagent-handoff-copy-budget.test.ts @@ -1,8 +1,7 @@ -import { describe, expect, it, vi } from 'vitest' +import { describe, expect, it } from 'vitest' import type { NativeChatMessage } from '../../../../../shared/native-chat/native-chat-types' import { nativeChatSubagentSections } from './native-chat-subagent-sections' import { nativeChatSubagentLiveSections } from './native-chat-subagent-live-frontier' -import { buildNativeChatTranscriptSlots } from '../conversation/slots/native-chat-transcript-slots' function message( id: string, @@ -72,99 +71,7 @@ function fixture(calls: readonly NativeChatMessage[]) { return { conversation, sections, roster, parent, child } } -function slots(input: ReturnType) { - return buildNativeChatTranscriptSlots({ - messages: input.conversation, - turnKeys: input.conversation.map(() => undefined), - liveTurnKey: undefined, - receipts: new Map(), - turnStatuses: { active: null, completedByTurn: {} }, - turnDiffs: new Map(), - expandedTurnKeys: new Set(), - isWorking: true, - lifecycleWorking: false, - subagentSections: input.sections - }) -} - -function countHandoffCopies( - input: ReturnType, - calls: readonly NativeChatMessage[] -) { - const tracked = new Set(calls) - const set = Map.prototype.set - const values = Array.prototype[Symbol.iterator] - let copies = 0 - const spy = vi.spyOn(Map.prototype, 'set').mockImplementation(function ( - this: Map, - key, - value - ) { - if (key === 'parent' && Array.isArray(value) && tracked.has(value[0])) { - Object.defineProperty(value, Symbol.iterator, { - configurable: true, - value: () => { - const iterator = values.call(value) - const next = iterator.next.bind(iterator) - iterator.next = (...args) => { - const item = next(...args) - if (!item.done) { - copies += 1 - } - return item - } - return iterator - } - }) - } - return set.call(this, key, value) - }) - try { - const live = nativeChatSubagentLiveSections(input.conversation, input.sections, true) - spy.mockRestore() - return { live, copies } - } finally { - spy.mockRestore() - } -} - describe('nested subagent handoff accumulation', () => { - it.each([12, 128, 1000])('copies %s handoffs only for their existing final merge', (count) => { - const calls = Array.from({ length: count }, (_, index) => handoff(index)) - const input = fixture(calls) - const before = structuredClone(input) - const result = countHandoffCopies(input, calls) - expect([...result.live]).toEqual(['parent', 'child']) - expect(input).toEqual(before) - const messages = slots(input).flatMap((slot) => - slot.kind === 'message' ? (slot.workRun ?? [slot.message]) : [] - ) - const expected = [input.roster, input.parent, input.child, ...calls] - expect(messages).toEqual(expected) - for (const [index, row] of messages.entries()) { - expect(row).toBe(expected[index]) - } - expect(result.copies).toBe(count) - }) - - it('retains duplicate records, colliding message IDs and unknown input fields without mutating inputs', () => { - const first = handoff(0) - const second = handoff(1) - second.id = first.id - const calls = [first, first, second] - const input = fixture(calls) - const before = structuredClone(input) - Object.freeze(input.conversation) - for (const row of input.conversation) { - Object.freeze(row.blocks) - Object.freeze(row) - } - const result = countHandoffCopies(input, calls) - expect([...result.live]).toEqual(['parent', 'child']) - expect(input).toEqual(before) - expect(result.copies).toBe(3) - }) - it.each([{ agents: [] }, { agents: [''] }, { agents: ['unknown'] }])( 'keeps unknown or empty target behavior for %j', ({ agents }) => { diff --git a/src/renderer/src/components/native-chat/tools/lines/NativeChatToolLine.test.tsx b/src/renderer/src/components/native-chat/tools/lines/NativeChatToolLine.test.tsx deleted file mode 100644 index 7cfc39704cc..00000000000 --- a/src/renderer/src/components/native-chat/tools/lines/NativeChatToolLine.test.tsx +++ /dev/null @@ -1,43 +0,0 @@ -// @vitest-environment happy-dom -import '@testing-library/jest-dom/vitest' -import { cleanup, fireEvent, render, screen } from '@testing-library/react' -import { afterEach, describe, expect, it, vi } from 'vitest' -import { NativeChatToolLine } from './NativeChatToolLine' - -vi.mock('@/lib/syntax-highlighting/oniguruma', async () => ({ - loadOniguruma: (await import('@/lib/syntax-highlighting/oniguruma-test-harness')) - .loadNodeOniguruma -})) - -afterEach(cleanup) - -describe('tool sentence rows', () => { - it('keeps metadata and errors while toggling output behind the sentence', () => { - render( - - ) - const button = screen.getByRole('button') - expect(button).toHaveAccessibleName(/Bash.*Ran.*false.*exit 1.*1s/) - expect(screen.getByText('1s').parentElement).toHaveClass('ml-auto', 'tabular-nums', 'font-sans') - expect(screen.queryByText('command failed')).toBeNull() - fireEvent.click(button) - expect(screen.getByText('command failed')).toHaveClass( - 'text-destructive', - 'bg-chat-code-surface', - 'border-chat-code-border', - 'rounded-lg', - 'text-chat-code' - ) - fireEvent.click(button) - expect(screen.queryByText('command failed')).toBeNull() - }) -}) diff --git a/src/renderer/src/components/native-chat/viewport/scroll/NativeChatJumpToLatest.test.tsx b/src/renderer/src/components/native-chat/viewport/scroll/NativeChatJumpToLatest.test.tsx deleted file mode 100644 index 7c867c7b9d9..00000000000 --- a/src/renderer/src/components/native-chat/viewport/scroll/NativeChatJumpToLatest.test.tsx +++ /dev/null @@ -1,28 +0,0 @@ -// @vitest-environment happy-dom - -import '@testing-library/jest-dom/vitest' - -import { cleanup, render, screen } from '@testing-library/react' -import userEvent from '@testing-library/user-event' -import { afterEach, expect, it, vi } from 'vitest' -import { NativeChatJumpToLatest } from './NativeChatJumpToLatest' - -afterEach(cleanup) - -// The reader is usually mid-draft when they reach for this: a press must not take the caret. -it('leaves focus in the composer when pressed with the pointer', async () => { - const onJump = vi.fn() - render( - <> - -