mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 19:34:29 +00:00
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
This commit is contained in:
+1
@@ -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) => {
|
||||
|
||||
@@ -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<string, string>()
|
||||
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')
|
||||
})
|
||||
})
|
||||
@@ -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<unknown>
|
||||
commitFileExplorerUpload(
|
||||
worktree: string,
|
||||
tempRelativePath: string,
|
||||
finalRelativePath: string
|
||||
finalRelativePath: string,
|
||||
...expectation: ExpectationArgs
|
||||
): Promise<unknown>
|
||||
deleteFileExplorerPath(
|
||||
worktree: string,
|
||||
relativePath: string,
|
||||
recursive?: boolean
|
||||
recursive: boolean,
|
||||
...expectation: ExpectationArgs
|
||||
): Promise<unknown>
|
||||
createFileExplorerDir(
|
||||
worktree: string,
|
||||
relativePath: string,
|
||||
...expectation: ExpectationArgs
|
||||
): Promise<unknown>
|
||||
createFileExplorerDir(worktree: string, relativePath: string): Promise<unknown>
|
||||
statRuntimeFile(worktree: string, relativePath: string): Promise<unknown>
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -16,6 +16,7 @@ function createStore(
|
||||
const committed: { tempRelativePath: string; finalRelativePath: string }[] = []
|
||||
const existing = new Set<string>()
|
||||
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)
|
||||
|
||||
@@ -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<void>
|
||||
commit(input: {
|
||||
workspaceId: string
|
||||
tempRelativePath: string
|
||||
finalRelativePath: string
|
||||
}): Promise<void>
|
||||
remove(input: { workspaceId: string; relativePath: string }): Promise<void>
|
||||
/** Pins the workspace's current host and creates the directory there. */
|
||||
prepareDirectory(workspaceId: string, directory: string): Promise<RuntimeFileMutationExpectation>
|
||||
writeChunk(
|
||||
input: TransferMutation & { relativePath: string; contentBase64: string; append: boolean }
|
||||
): Promise<void>
|
||||
commit(
|
||||
input: TransferMutation & { tempRelativePath: string; finalRelativePath: string }
|
||||
): Promise<void>
|
||||
remove(input: TransferMutation & { relativePath: string }): Promise<void>
|
||||
exists(input: { workspaceId: string; relativePath: string }): Promise<boolean>
|
||||
ensureDirectory(input: { workspaceId: string; relativePath: string }): Promise<void>
|
||||
}
|
||||
|
||||
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<BrowserClientDownloadCommit> {
|
||||
@@ -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<void> {
|
||||
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 {
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user