Files
orca/src/main/runtime/orca-runtime-git-diff-budget.test.ts
T
Neil d5750648c2 fix(runtime): route runtime Git by resolved execution host, not repo connectionId (#18307)
`RuntimeGitTarget` carried `connectionId?: string` and no host id, so `undefined`
spelled three different answers at once — "runtime: host", "unresolved", and
"genuinely local". Its sole resolver read `store.getRepo(worktree.repoId)?.connectionId`
and never looked at `worktree.hostId`, which outranks every repo row, so one
arbitrarily chosen row decided the execution host for 36 downstream dispatches.

The target now carries `executionHostId: ExecutionHostId` (never null, never
optional), resolved through the shared rule that landed with #17909/#17919 and
dispatched through the host-keyed routes from #18296. Dispatch sites call
`requireRuntimeGitProvider`, where `null` means exactly one thing: the host is
`local` and the command runs here as free functions.

Four answers that used to collapse into one:

- `ssh:x` with a rival row on `ssh:y` — routes to x. Previously the first row won,
  which is the reproduced cross-host leak.
- `local` with a surviving `connectionId` — a row contradicting itself; no SSH
  connection is handed out.
- `runtime:<env>` — throws `ExecutionHostNotDispatchableError`. Its repo row's
  connection names a target in the *server's* namespace; dialling it here reaches a
  same-named target on this client.
- rival rows disagreeing with no worktree host — `worktree_execution_host_unresolved`,
  matching the launch path rather than guessing a row.

An unreachable SSH host still throws `SSH_GIT_PROVIDER_UNAVAILABLE_MESSAGE`; loss of
contact is never evidence of locality (docs/reference/ssh-execution-boundary.md).

`resolveWorktreeLaunchHost` keeps its exact signature and now delegates to
`resolveWorktreeHostRouting`, the same resolution answering "which host is this on"
rather than "what may this client dial" — the git target needs the first question
because `local` and `runtime:` are two different non-SSH answers.

No wire change: `RuntimeGitTarget` is main-process internal, and the SSH and local
model-discovery host keys are byte-identical to before.

`RuntimeFileTarget` has the same defect in ~30 filesystem dispatches and is
deliberately left for a follow-up.
2026-09-02 19:26:07 -07:00

222 lines
7.2 KiB
TypeScript

// Why: the cap lives in orca-runtime-git.ts so both branches of all three diff readers are covered —
// an SSH host forwards its provider's payload verbatim, so an older relay cannot be relied on to clamp it.
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { REMOTE_RPC_MAX_CONTENT_BYTES } from '../../shared/remote-rpc-content-budget'
import type { GitDiffResult } from '../../shared/git-diff-compare-types'
import type { GlobalSettings } from '../../shared/global-settings-types'
import type * as GitStatusModule from '../git/status'
import { RuntimeGitCommands, type ResolvedRuntimeGitWorktree } from './orca-runtime-git'
const mocks = vi.hoisted(() => ({
getSshGitProvider: vi.fn(),
getDiff: vi.fn(),
getBranchDiff: vi.fn(),
getCommitDiff: vi.fn()
}))
vi.mock('../providers/ssh-git-dispatch', () => ({
getSshGitProvider: mocks.getSshGitProvider
}))
vi.mock('../git/status', async () => ({
...(await vi.importActual<typeof GitStatusModule>('../git/status')),
getDiff: mocks.getDiff,
getBranchDiff: mocks.getBranchDiff,
getCommitDiff: mocks.getCommitDiff
}))
const OVERSIZED_BASE64 = 'A'.repeat(REMOTE_RPC_MAX_CONTENT_BYTES + 1)
const BRANCH_COMPARE = { mergeBase: 'base-oid', headOid: 'head-oid' }
const COMMIT_ARGS = {
commitOid: 'commit-oid',
parentOid: 'parent-oid',
filePath: 'assets/logo.png'
}
const TOO_LARGE = { code: 'diff_too_large', data: { maxBytes: REMOTE_RPC_MAX_CONTENT_BYTES } }
function oversizedResult(): GitDiffResult {
return {
kind: 'binary',
originalContent: '',
modifiedContent: OVERSIZED_BASE64,
originalIsBinary: false,
modifiedIsBinary: true
}
}
function commands(
connectionId?: string,
localGitOptions?: { wslDistro: string }
): RuntimeGitCommands {
const worktree = {
id: 'wt-1',
repoId: 'repo-1',
path: '/remote/repo',
git: { path: '/remote/repo', branch: 'main', isBare: false, isMainWorktree: false }
} as unknown as ResolvedRuntimeGitWorktree
return new RuntimeGitCommands({
resolveRuntimeGitTarget: async () => ({
worktree,
executionHostId: connectionId ? (`ssh:${connectionId}` as const) : ('local' as const),
...(localGitOptions ? { localGitOptions } : {})
}),
getRuntimeSettings: () => ({}) as GlobalSettings
})
}
function sshProvider(): {
getDiff: ReturnType<typeof vi.fn>
getBranchDiff: ReturnType<typeof vi.fn>
getCommitDiff: ReturnType<typeof vi.fn>
} {
return {
getDiff: vi.fn().mockResolvedValue(oversizedResult()),
getBranchDiff: vi.fn().mockResolvedValue([oversizedResult()]),
getCommitDiff: vi.fn().mockResolvedValue(oversizedResult())
}
}
describe('runtime git diff transport budget', () => {
beforeEach(() => {
mocks.getSshGitProvider.mockReset()
mocks.getDiff.mockReset().mockResolvedValue(oversizedResult())
mocks.getBranchDiff.mockReset().mockResolvedValue(oversizedResult())
mocks.getCommitDiff.mockReset().mockResolvedValue(oversizedResult())
})
it('caps an SSH-forwarded diff that exceeds the budget', async () => {
const provider = sshProvider()
mocks.getSshGitProvider.mockReturnValue(provider)
await expect(
commands('conn-1').getRuntimeGitDiff(
'id:wt-1',
'assets/logo.png',
false,
undefined,
REMOTE_RPC_MAX_CONTENT_BYTES
)
).rejects.toMatchObject(TOO_LARGE)
expect(provider.getDiff).toHaveBeenCalledWith(
'/remote/repo',
'assets/logo.png',
false,
undefined
)
expect(mocks.getDiff).not.toHaveBeenCalled()
})
it('leaves an SSH-forwarded diff uncapped when no budget is supplied', async () => {
mocks.getSshGitProvider.mockReturnValue(sshProvider())
await expect(
commands('conn-1').getRuntimeGitDiff('id:wt-1', 'assets/logo.png', false)
).resolves.toMatchObject({ modifiedContent: OVERSIZED_BASE64 })
})
it('caps a local-repo diff that exceeds the budget', async () => {
await expect(
commands().getRuntimeGitDiff(
'id:wt-1',
'assets/logo.png',
false,
undefined,
REMOTE_RPC_MAX_CONTENT_BYTES
)
).rejects.toMatchObject(TOO_LARGE)
expect(mocks.getDiff).toHaveBeenCalled()
expect(mocks.getSshGitProvider).not.toHaveBeenCalled()
})
it('leaves a local-repo diff uncapped when no budget is supplied', async () => {
await expect(
commands().getRuntimeGitDiff('id:wt-1', 'assets/logo.png', false)
).resolves.toMatchObject({ modifiedContent: OVERSIZED_BASE64 })
})
it('caps an SSH-forwarded branch diff that exceeds the budget', async () => {
const provider = sshProvider()
mocks.getSshGitProvider.mockReturnValue(provider)
await expect(
commands('conn-1').getRuntimeGitBranchDiff(
'id:wt-1',
BRANCH_COMPARE,
'assets/logo.png',
undefined,
REMOTE_RPC_MAX_CONTENT_BYTES
)
).rejects.toMatchObject(TOO_LARGE)
expect(provider.getBranchDiff).toHaveBeenCalled()
expect(mocks.getBranchDiff).not.toHaveBeenCalled()
})
it('caps a local-repo branch diff that exceeds the budget', async () => {
await expect(
commands().getRuntimeGitBranchDiff(
'id:wt-1',
BRANCH_COMPARE,
'assets/logo.png',
undefined,
REMOTE_RPC_MAX_CONTENT_BYTES
)
).rejects.toMatchObject(TOO_LARGE)
expect(mocks.getBranchDiff).toHaveBeenCalled()
})
it('leaves a local-repo branch diff uncapped when no budget is supplied', async () => {
await expect(
commands().getRuntimeGitBranchDiff('id:wt-1', BRANCH_COMPARE, 'assets/logo.png')
).resolves.toMatchObject({ modifiedContent: OVERSIZED_BASE64 })
})
it('caps an SSH-forwarded commit diff that exceeds the budget', async () => {
const provider = sshProvider()
mocks.getSshGitProvider.mockReturnValue(provider)
await expect(
commands('conn-1').getRuntimeGitCommitDiff(
'id:wt-1',
COMMIT_ARGS,
REMOTE_RPC_MAX_CONTENT_BYTES
)
).rejects.toMatchObject(TOO_LARGE)
expect(provider.getCommitDiff).toHaveBeenCalled()
expect(mocks.getCommitDiff).not.toHaveBeenCalled()
})
it('caps a local-repo commit diff that exceeds the budget', async () => {
await expect(
commands().getRuntimeGitCommitDiff('id:wt-1', COMMIT_ARGS, REMOTE_RPC_MAX_CONTENT_BYTES)
).rejects.toMatchObject(TOO_LARGE)
expect(mocks.getCommitDiff).toHaveBeenCalled()
})
it('leaves a local-repo commit diff uncapped when no budget is supplied', async () => {
await expect(commands().getRuntimeGitCommitDiff('id:wt-1', COMMIT_ARGS)).resolves.toMatchObject(
{
modifiedContent: OVERSIZED_BASE64
}
)
})
it('prioritizes local file diff reads without losing WSL routing', async () => {
const runtime = commands(undefined, { wslDistro: 'Ubuntu' })
await runtime.getRuntimeGitBranchDiff('id:wt-1', BRANCH_COMPARE, 'assets/logo.png')
await runtime.getRuntimeGitCommitDiff('id:wt-1', COMMIT_ARGS)
const options = { admissionTier: 'interactive', wslDistro: 'Ubuntu' }
expect(mocks.getBranchDiff).toHaveBeenLastCalledWith(
'/remote/repo',
expect.objectContaining({ filePath: 'assets/logo.png' }),
options
)
expect(mocks.getCommitDiff).toHaveBeenLastCalledWith(
'/remote/repo',
expect.objectContaining({ filePath: 'assets/logo.png' }),
options
)
})
})