Files
orca/src/main/worktree-removal-execution-host-route.test.ts
T
Neil 3c91404820 fix(worktrees): route managed worktree removal by resolved execution host (#18529)
`removeManagedWorktree` resolved its host once — for the metadata prune
(`cleanupHostId ?? getRepoExecutionHostId(repo)`) — and then read raw
`repo.connectionId` for every step that touches the filesystem: the
`git worktree list` deciding whether the path is registered, the provider handed
to the unregistered-removal branch, the registered-remote-vs-local fork, and the
PTY/history teardown. One function, two spellings.

For a row naming its owner only as `executionHostId: 'ssh:<target>'` — the exact
class #18296 names — the list ran on the client against a remote path,
`removeRuntimeUnregisteredWorktree` was entered with `provider: null`, and the
metadata was pruned under `ssh:<target>` while a same-named *local* directory was
the one considered for deletion (#11163). #18358 made this reachable: it migrated
the cleanup scan, so `executionHostId`-only rows now surface as removable
candidates, but removal did not move with it.

Routing is now one answer for the whole removal, taken from the host the prune
already used, through #18296's host-keyed dispatch. The ambiguous
`provider: SshGitProvider | null` carrier is deleted from the callees rather than
supplemented, so every remaining reader is a compile error in the typed modules
that do the destructive work (`runtime-unregistered-worktree-removal`,
`runtime-registered-remote-worktree-removal`, `runtime-worktree-filesystem`).
The orchestrator itself carries `@ts-nocheck` from its mechanical split, so that
guarantee does not reach it — tests cover it instead.

Every change is in the refusing direction; nothing became more aggressive:

- an `ssh:` host with no registered provider throws instead of deleting a
  client-side path (`requireSshGitProvider` already threw for rows that spelled
  the same host as `connectionId`);
- `runtime:<env>` throws rather than dialling a same-named target in this
  client's namespace, matching `workspace-cleanup-git-route` and
  `runtime-git-command-target`;
- the folder-workspace teardown resolves its connection instead of reading the
  raw field, so a `runtime:` row stops dialling the wrong namespace.

No wire or persistence change: `removeWorktreeMetadataAndHistory` already took
the resolved host, and the removal RPC result shape is untouched.
2026-09-03 16:21:52 -07:00

101 lines
3.2 KiB
TypeScript

import { afterEach, describe, expect, it } from 'vitest'
import {
registerSshGitProvider,
SSH_GIT_PROVIDER_UNAVAILABLE_MESSAGE,
unregisterSshGitProvider
} from './providers/ssh-git-dispatch'
import {
registerSshFilesystemProvider,
unregisterSshFilesystemProvider
} from './providers/ssh-filesystem-dispatch'
import { ExecutionHostNotDispatchableError } from './providers/execution-host-provider-dispatch'
import {
getWorktreeRemovalConnectionId,
resolveWorktreeRemovalRoute
} from './worktree-removal-execution-host-route'
const HOST_A = 'target-a'
const HOST_B = 'target-b'
function gitProvider(name: string): never {
return { name } as never
}
function fsProvider(name: string): never {
return { name } as never
}
afterEach(() => {
unregisterSshGitProvider(HOST_A)
unregisterSshGitProvider(HOST_B)
unregisterSshFilesystemProvider(HOST_A)
unregisterSshFilesystemProvider(HOST_B)
})
describe('resolveWorktreeRemovalRoute', () => {
it('routes a local host to this machine with no connection', () => {
const route = resolveWorktreeRemovalRoute('local')
expect(route).toEqual({ kind: 'local', hostId: 'local' })
expect(getWorktreeRemovalConnectionId(route)).toBeUndefined()
})
it('keeps two simultaneously registered SSH hosts on their own providers', () => {
registerSshGitProvider(HOST_A, gitProvider('git-a'))
registerSshGitProvider(HOST_B, gitProvider('git-b'))
registerSshFilesystemProvider(HOST_A, fsProvider('fs-a'))
registerSshFilesystemProvider(HOST_B, fsProvider('fs-b'))
const routeA = resolveWorktreeRemovalRoute('ssh:target-a')
const routeB = resolveWorktreeRemovalRoute('ssh:target-b')
expect(routeA).toMatchObject({
kind: 'ssh',
hostId: 'ssh:target-a',
connectionId: HOST_A,
provider: { name: 'git-a' },
fsProvider: { name: 'fs-a' }
})
expect(routeB).toMatchObject({
kind: 'ssh',
hostId: 'ssh:target-b',
connectionId: HOST_B,
provider: { name: 'git-b' },
fsProvider: { name: 'fs-b' }
})
expect(getWorktreeRemovalConnectionId(routeA)).toBe(HOST_A)
expect(getWorktreeRemovalConnectionId(routeB)).toBe(HOST_B)
})
it('carries a null filesystem provider without falling back to the local one', () => {
registerSshGitProvider(HOST_A, gitProvider('git-a'))
expect(resolveWorktreeRemovalRoute('ssh:target-a')).toMatchObject({
kind: 'ssh',
fsProvider: null
})
})
it('refuses an unreachable SSH host instead of answering local', () => {
expect(() => resolveWorktreeRemovalRoute('ssh:target-a')).toThrow(
SSH_GIT_PROVIDER_UNAVAILABLE_MESSAGE
)
})
it('refuses a runtime host with no nested SSH target', () => {
expect(() => resolveWorktreeRemovalRoute('runtime:env-1')).toThrow(
ExecutionHostNotDispatchableError
)
})
it('refuses a runtime host even when a same-named target is dialable here', () => {
// The nested target lives in the environment's namespace; a same-named local one is a
// different machine, and removing a worktree through it deletes the wrong checkout.
registerSshGitProvider(HOST_A, gitProvider('git-a'))
expect(() => resolveWorktreeRemovalRoute('runtime:target-a')).toThrow(
ExecutionHostNotDispatchableError
)
})
})