From 0f18e8a40cb12ec3d61325b0802ba2e68330279f Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Thu, 5 Nov 2026 15:42:22 -0800 Subject: [PATCH] fix(browser): pin the workspace host for browser downloads into a server workspace (#38721) The server-side download transfer store wrote through the runtime file mutations without a host expectation, so every write was refused as coming from an outdated client. Each transfer now captures the workspace's host as it prepares the download directory, and passes that expectation on every later mutation, so a host that moves mid-transfer refuses the rest. Fixes #38122 --- ...st-lease-download-transfer-cleanup.test.ts | 1 + ...ser-client-download-transfer-store.test.ts | 77 +++++++++++++++++++ .../browser-client-download-transfer-store.ts | 73 ++++++++++++++---- .../browser-client-download-transfers.test.ts | 4 +- .../browser-client-download-transfers.ts | 47 ++++++----- .../files/file-mutation-expectation.ts | 25 +++++- 6 files changed, 191 insertions(+), 36 deletions(-) create mode 100644 src/main/runtime/browser/downloads/browser-client-download-transfer-store.test.ts diff --git a/src/main/runtime/browser-host/leases/browser-host-lease-download-transfer-cleanup.test.ts b/src/main/runtime/browser-host/leases/browser-host-lease-download-transfer-cleanup.test.ts index e6a0188b17f..32acdadd3cf 100644 --- a/src/main/runtime/browser-host/leases/browser-host-lease-download-transfer-cleanup.test.ts +++ b/src/main/runtime/browser-host/leases/browser-host-lease-download-transfer-cleanup.test.ts @@ -7,6 +7,7 @@ function createRuntime() { const removed: string[] = [] const runtime = { getRuntimeId: () => 'runtime-a', + resolveRuntimeFileTarget: vi.fn().mockResolvedValue({ executionHostId: 'local' }), writeFileExplorerFileBase64Chunk: vi.fn().mockResolvedValue(undefined), commitFileExplorerUpload: vi.fn().mockResolvedValue(undefined), deleteFileExplorerPath: vi.fn(async (_worktree: string, relativePath: string) => { diff --git a/src/main/runtime/browser/downloads/browser-client-download-transfer-store.test.ts b/src/main/runtime/browser/downloads/browser-client-download-transfer-store.test.ts new file mode 100644 index 00000000000..884b8ec439e --- /dev/null +++ b/src/main/runtime/browser/downloads/browser-client-download-transfer-store.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, it } from 'vitest' + +import type { ExecutionHostId } from '../../../../shared/execution-host' +import { getBrowserClientDownloadTransferStore } from './browser-client-download-transfer-store' +import { assertRuntimeFileMutationExpectation } from '../../files/file-mutation-expectation' + +// Mutations enforce the same host expectation RuntimeFileMutations does. +function createServerRuntime(executionHostId: ExecutionHostId) { + const files = new Map() + const guard = (generation?: number, sshTargetId?: string, hostId?: string): void => + assertRuntimeFileMutationExpectation(executionHostId, hostId, sshTargetId, generation) + const runtime = { + resolveRuntimeFileTarget: async () => ({ executionHostId }), + createFileExplorerDir: async ( + _worktree: string, + _relativePath: string, + ...expectation: [number?, string?, string?] + ) => guard(...expectation), + writeFileExplorerFileBase64Chunk: async ( + _worktree: string, + relativePath: string, + contentBase64: string, + append: boolean, + ...expectation: [number?, string?, string?] + ) => { + guard(...expectation) + files.set(relativePath, (append ? (files.get(relativePath) ?? '') : '') + contentBase64) + }, + commitFileExplorerUpload: async ( + _worktree: string, + tempRelativePath: string, + finalRelativePath: string, + ...expectation: [number?, string?, string?] + ) => { + guard(...expectation) + files.set(finalRelativePath, files.get(tempRelativePath) ?? '') + files.delete(tempRelativePath) + }, + deleteFileExplorerPath: async ( + _worktree: string, + relativePath: string, + _recursive: boolean, + ...expectation: [number?, string?, string?] + ) => { + guard(...expectation) + files.delete(relativePath) + }, + statRuntimeFile: async (_worktree: string, relativePath: string) => { + if (!files.has(relativePath)) { + throw new Error('ENOENT') + } + return {} + } + } + return { runtime, files } +} + +describe('browser client download transfer store', () => { + it('saves a download into a workspace the server hosts (#38122)', async () => { + const { runtime, files } = createServerRuntime('local') + const chunk = { + transferId: 'transfer-1', + browserPageId: 'page-1', + pageHostGeneration: 1, + workspaceId: 'workspace-1', + filename: 'photo.png', + platform: 'linux' as const + } + const store = getBrowserClientDownloadTransferStore(runtime) + + await store.accept({ ...chunk, contentBase64: 'AAAA', offset: 0, final: false }) + const commit = await store.accept({ ...chunk, contentBase64: 'BBBB', offset: 3, final: true }) + + expect(commit).toEqual({ workspaceRelativePath: '.orca/browser-downloads/photo.png' }) + expect(files.get('.orca/browser-downloads/photo.png')).toBe('AAAABBBB') + }) +}) diff --git a/src/main/runtime/browser/downloads/browser-client-download-transfer-store.ts b/src/main/runtime/browser/downloads/browser-client-download-transfer-store.ts index dccff2edeb9..69960e49818 100644 --- a/src/main/runtime/browser/downloads/browser-client-download-transfer-store.ts +++ b/src/main/runtime/browser/downloads/browser-client-download-transfer-store.ts @@ -1,26 +1,53 @@ +import type { ExecutionHostId } from '../../../../shared/execution-host' import { BrowserClientDownloadTransferStore } from './browser-client-download-transfers' +import { + captureRuntimeFileMutationExpectation, + type RuntimeFileMutationExpectation +} from '../../files/file-mutation-expectation' + +type ExpectationArgs = [ + expectedSshConnectionGeneration: number | undefined, + expectedSshTargetId: string | undefined, + expectedExecutionHostId: string +] type RuntimeFileChannelHost = { + resolveRuntimeFileTarget(selector: string): Promise<{ executionHostId: ExecutionHostId }> writeFileExplorerFileBase64Chunk( worktree: string, relativePath: string, contentBase64: string, - append: boolean + append: boolean, + ...expectation: ExpectationArgs ): Promise commitFileExplorerUpload( worktree: string, tempRelativePath: string, - finalRelativePath: string + finalRelativePath: string, + ...expectation: ExpectationArgs ): Promise deleteFileExplorerPath( worktree: string, relativePath: string, - recursive?: boolean + recursive: boolean, + ...expectation: ExpectationArgs + ): Promise + createFileExplorerDir( + worktree: string, + relativePath: string, + ...expectation: ExpectationArgs ): Promise - createFileExplorerDir(worktree: string, relativePath: string): Promise statRuntimeFile(worktree: string, relativePath: string): Promise } +function expectationArgs(expectation: RuntimeFileMutationExpectation): ExpectationArgs { + return [ + expectation.expectedSshConnectionGeneration, + expectation.expectedSshTargetId, + expectation.expectedExecutionHostId + ] +} + // Lease cleanup knows only the runtime id; file-channel hosts create the store. type DownloadTransferRuntime = RuntimeFileChannelHost | { getRuntimeId(): string } @@ -41,22 +68,42 @@ export function getBrowserClientDownloadTransferStore( let store = stores.get(runtime) if (!store) { store = new BrowserClientDownloadTransferStore({ - writeChunk: async ({ workspaceId, relativePath, contentBase64, append }) => { + // The server is the writer: pin the workspace's current host for the whole transfer. + prepareDirectory: async (workspaceId, relativePath) => { + const expectation = captureRuntimeFileMutationExpectation( + (await runtime.resolveRuntimeFileTarget(workspaceId)).executionHostId + ) + await runtime.createFileExplorerDir( + workspaceId, + relativePath, + ...expectationArgs(expectation) + ) + return expectation + }, + writeChunk: async ({ workspaceId, expectation, relativePath, contentBase64, append }) => { await runtime.writeFileExplorerFileBase64Chunk( workspaceId, relativePath, contentBase64, - append + append, + ...expectationArgs(expectation) ) }, - commit: async ({ workspaceId, tempRelativePath, finalRelativePath }) => { - await runtime.commitFileExplorerUpload(workspaceId, tempRelativePath, finalRelativePath) + commit: async ({ workspaceId, expectation, tempRelativePath, finalRelativePath }) => { + await runtime.commitFileExplorerUpload( + workspaceId, + tempRelativePath, + finalRelativePath, + ...expectationArgs(expectation) + ) }, - remove: async ({ workspaceId, relativePath }) => { - await runtime.deleteFileExplorerPath(workspaceId, relativePath, false) - }, - ensureDirectory: async ({ workspaceId, relativePath }) => { - await runtime.createFileExplorerDir(workspaceId, relativePath) + remove: async ({ workspaceId, expectation, relativePath }) => { + await runtime.deleteFileExplorerPath( + workspaceId, + relativePath, + false, + ...expectationArgs(expectation) + ) }, exists: async ({ workspaceId, relativePath }) => { try { diff --git a/src/main/runtime/browser/downloads/browser-client-download-transfers.test.ts b/src/main/runtime/browser/downloads/browser-client-download-transfers.test.ts index 41598b9d995..13a5c236e8b 100644 --- a/src/main/runtime/browser/downloads/browser-client-download-transfers.test.ts +++ b/src/main/runtime/browser/downloads/browser-client-download-transfers.test.ts @@ -16,6 +16,7 @@ function createStore( const committed: { tempRelativePath: string; finalRelativePath: string }[] = [] const existing = new Set() const dependencies: BrowserClientDownloadTransferDependencies = { + prepareDirectory: async () => ({ expectedExecutionHostId: 'local' }), writeChunk: async ({ relativePath, contentBase64, append }) => { written.push({ relativePath, contentBase64, append }) }, @@ -25,7 +26,6 @@ function createStore( remove: async ({ relativePath }) => { removed.push(relativePath) }, - ensureDirectory: async () => {}, exists: async ({ relativePath }) => existing.has(relativePath), ...overrides } @@ -248,10 +248,10 @@ describe('BrowserClientDownloadTransferStore', () => { it('bounds concurrent transfers', async () => { const dependencies = { + prepareDirectory: vi.fn().mockResolvedValue({ expectedExecutionHostId: 'local' }), writeChunk: vi.fn().mockResolvedValue(undefined), commit: vi.fn().mockResolvedValue(undefined), remove: vi.fn().mockResolvedValue(undefined), - ensureDirectory: vi.fn().mockResolvedValue(undefined), exists: vi.fn().mockResolvedValue(false) } const store = new BrowserClientDownloadTransferStore(dependencies, 1) diff --git a/src/main/runtime/browser/downloads/browser-client-download-transfers.ts b/src/main/runtime/browser/downloads/browser-client-download-transfers.ts index 7239176c44b..09d59b8f6ba 100644 --- a/src/main/runtime/browser/downloads/browser-client-download-transfers.ts +++ b/src/main/runtime/browser/downloads/browser-client-download-transfers.ts @@ -8,25 +8,23 @@ import { MAX_BROWSER_DOWNLOAD_COLLISION_ATTEMPTS, normalizeBrowserDownloadFilename } from '../../../../shared/browser-download-filename' +import type { RuntimeFileMutationExpectation } from '../../files/file-mutation-expectation' // Remote downloads need an in-workspace home reachable through containment-checked transfers. export const BROWSER_CLIENT_DOWNLOAD_WORKSPACE_DIRECTORY = '.orca/browser-downloads' +type TransferMutation = { workspaceId: string; expectation: RuntimeFileMutationExpectation } export type BrowserClientDownloadTransferDependencies = { - writeChunk(input: { - workspaceId: string - relativePath: string - contentBase64: string - append: boolean - }): Promise - commit(input: { - workspaceId: string - tempRelativePath: string - finalRelativePath: string - }): Promise - remove(input: { workspaceId: string; relativePath: string }): Promise + /** Pins the workspace's current host and creates the directory there. */ + prepareDirectory(workspaceId: string, directory: string): Promise + writeChunk( + input: TransferMutation & { relativePath: string; contentBase64: string; append: boolean } + ): Promise + commit( + input: TransferMutation & { tempRelativePath: string; finalRelativePath: string } + ): Promise + remove(input: TransferMutation & { relativePath: string }): Promise exists(input: { workspaceId: string; relativePath: string }): Promise - ensureDirectory(input: { workspaceId: string; relativePath: string }): Promise } type TransferSession = { @@ -35,6 +33,7 @@ type TransferSession = { pageHostGeneration: number workspaceId: string tempRelativePath: string + pinnedExpectation: RuntimeFileMutationExpectation | null bytesWritten: number /** Set synchronously before queued cleanup. */ canceled: boolean @@ -166,16 +165,19 @@ export class BrowserClientDownloadTransferStore { await this.release(session) throw new Error('browser_client_download_transfer_too_large') } + let expectation = session.pinnedExpectation try { - if (session.bytesWritten === 0) { - await this.dependencies.ensureDirectory({ - workspaceId: session.workspaceId, - relativePath: BROWSER_CLIENT_DOWNLOAD_WORKSPACE_DIRECTORY - }) + if (!expectation) { + expectation = await this.dependencies.prepareDirectory( + session.workspaceId, + BROWSER_CLIENT_DOWNLOAD_WORKSPACE_DIRECTORY + ) + session.pinnedExpectation = expectation this.assertLive(session) } await this.dependencies.writeChunk({ workspaceId: session.workspaceId, + expectation, relativePath: session.tempRelativePath, contentBase64: input.contentBase64, append: session.bytesWritten > 0 @@ -191,7 +193,7 @@ export class BrowserClientDownloadTransferStore { return null } try { - return await this.commit(session, input.filename, input.platform) + return await this.commit(session, expectation, input.filename, input.platform) } catch (error) { await this.release(session) throw error @@ -235,6 +237,7 @@ export class BrowserClientDownloadTransferStore { pageHostGeneration: input.pageHostGeneration, workspaceId: input.workspaceId, tempRelativePath: `${BROWSER_CLIENT_DOWNLOAD_WORKSPACE_DIRECTORY}/.incoming-${input.transferId}`, + pinnedExpectation: null, bytesWritten: 0, canceled: false, operations: Promise.resolve(), @@ -246,6 +249,7 @@ export class BrowserClientDownloadTransferStore { private async commit( session: TransferSession, + expectation: RuntimeFileMutationExpectation, filename: string, platform: NodeJS.Platform ): Promise { @@ -265,6 +269,7 @@ export class BrowserClientDownloadTransferStore { try { await this.dependencies.commit({ workspaceId: session.workspaceId, + expectation, tempRelativePath: session.tempRelativePath, finalRelativePath }) @@ -284,12 +289,14 @@ export class BrowserClientDownloadTransferStore { private async release(session: TransferSession): Promise { const owned = this.sessions.get(session.key) === session this.settle(session) - if (!owned) { + // Nothing reached the workspace before an expectation was pinned. + if (!owned || !session.pinnedExpectation) { return } try { await this.dependencies.remove({ workspaceId: session.workspaceId, + expectation: session.pinnedExpectation, relativePath: session.tempRelativePath }) } catch { diff --git a/src/main/runtime/files/file-mutation-expectation.ts b/src/main/runtime/files/file-mutation-expectation.ts index b62b918b5a0..ecc95b07958 100644 --- a/src/main/runtime/files/file-mutation-expectation.ts +++ b/src/main/runtime/files/file-mutation-expectation.ts @@ -2,7 +2,10 @@ import { getSshTargetIdForExecutionHost, type ExecutionHostId } from '../../../shared/execution-host' -import { assertSshMutationExpectation } from '../../ssh/connection/ssh-connection-generation' +import { + assertSshMutationExpectation, + getSshConnectionGeneration +} from '../../ssh/connection/ssh-connection-generation' export const RUNTIME_FILE_MUTATION_UPDATE_REQUIRED = 'Remote file changes require a newer Orca client. Update the paired client and try again.' @@ -26,3 +29,23 @@ export function assertRuntimeFileMutationExpectation( expectedSshConnectionGeneration ) } + +export type RuntimeFileMutationExpectation = { + expectedExecutionHostId: ExecutionHostId + expectedSshTargetId?: string + expectedSshConnectionGeneration?: number +} + +/** The expectation a server-originated write pins at its start, so a moved or reconnected host refuses the rest. */ +export function captureRuntimeFileMutationExpectation( + executionHostId: ExecutionHostId +): RuntimeFileMutationExpectation { + const sshTargetId = getSshTargetIdForExecutionHost(executionHostId) + return sshTargetId + ? { + expectedExecutionHostId: executionHostId, + expectedSshTargetId: sshTargetId, + expectedSshConnectionGeneration: getSshConnectionGeneration(sshTargetId) + } + : { expectedExecutionHostId: executionHostId } +}