Files
orca/src/main/worktree-create-execution-host-route.test.ts
T
Neil 5d8532f6d3 fix(worktrees): resolve the execution host at both worktree-create entry points (#18545)
Two entry points create the same workspace and disagreed about how to read its
host. `orca-runtime-create-managed-worktree.ts:63` resolved through
`getRepoSshConnectionId` and then normalized the row; the `worktrees:create` IPC
handler branched on raw `repo.connectionId`
(`register-worktree-create-handlers.ts:66-69`). So a repo naming its owner only
as `executionHostId: 'ssh:<target>'` created remotely through the runtime and ran
`git worktree add` on the client against a remote path through IPC (#11163).
Same repo, two entry points, different answers.

Both now take one route, resolved through the existing layer
(`getRepoExecutionHostId` -> #18296's `resolveGitRouteForHost`). No new resolver.

The row normalization on the `ssh` variant is kept, and it is a **workaround, not
the pattern**. `createRemoteWorktree` and its callees re-read `repo.connectionId!`
at five depths in `ipc/worktree-remote.ts` (1627, 1847, 1848, 1865, 2029), so the
resolved connection has to reach them through the field they already read. It
travels only as far as that object does — anything downstream that re-reads the
row from the store still sees the unnormalized one, and it cannot express the
`runtime:` refusal on its own. Proper fix, deliberately not done here: give that
pipeline an explicit connection parameter and delete `repo.connectionId!` from it
so every reader becomes a compile error, the technique #18307/#18325 used. That
is a change inside a 2800-line module plus its callers, and it wants its own PR.

Three answers that used to collapse into one, now distinct at both entry points:

- `executionHostId: 'ssh:*'` with no `connectionId` -> that SSH host (IPC used to
  create locally);
- `executionHostId: 'local'` with a surviving `connectionId` -> local, since a
  local row cannot nest an SSH namespace. This is what `getRepoSshConnectionId`
  and therefore the runtime sibling already answered; IPC used to go remote;
- `runtime:<env>` -> refused. Its worktree is created by that environment's own
  server and the SSH target on its repo row is that server's nested one,
  addressable only as (environmentId, targetId). The renderer already routes
  runtime-environment creates over `worktree.create` RPC rather than this IPC
  channel, so reaching either entry point with one is a routing mistake. Matches
  `workspace-cleanup-git-route` and `runtime-git-command-target`.

Folder-workspace creation is untouched on both sides: it is a registration, not a
filesystem create, so the route is resolved after that branch on the IPC side, and
on the runtime side only the agent trust write consumes it — where a `runtime:`
host now yields `null` instead of the nested target, so the write stops going to a
same-named target in this client's table.

No wire or persistence change: the normalized row is a local value passed to the
create pipeline, never stored, and `CreateWorktreeResult` is untouched.
2026-09-03 16:27:13 -07:00

107 lines
3.6 KiB
TypeScript

import { afterEach, describe, expect, it } from 'vitest'
import { registerSshGitProvider, unregisterSshGitProvider } from './providers/ssh-git-dispatch'
import { ExecutionHostNotDispatchableError } from './providers/execution-host-provider-dispatch'
import type { Repo } from '../shared/repo-types'
import {
requireWorktreeCreateRoute,
resolveWorktreeCreateRoute
} from './worktree-create-execution-host-route'
const HOST_A = 'target-a'
const HOST_B = 'target-b'
function repoRow(fields: Partial<Repo>): Repo {
return {
id: 'repo-1',
path: '/remote/repo',
displayName: 'repo',
badgeColor: '#000',
addedAt: 0,
...fields
} as Repo
}
afterEach(() => {
unregisterSshGitProvider(HOST_A)
unregisterSshGitProvider(HOST_B)
})
describe('resolveWorktreeCreateRoute', () => {
it('routes a row that names its host only as executionHostId to that SSH target', () => {
registerSshGitProvider(HOST_A, { name: 'git-a' } as never)
expect(resolveWorktreeCreateRoute(repoRow({ executionHostId: 'ssh:target-a' }))).toMatchObject({
kind: 'ssh',
hostId: 'ssh:target-a',
connectionId: HOST_A,
repo: { connectionId: HOST_A }
})
})
it('normalizes the row for a legacy connectionId-only repo without changing its answer', () => {
expect(resolveWorktreeCreateRoute(repoRow({ connectionId: HOST_A }))).toMatchObject({
kind: 'ssh',
connectionId: HOST_A,
repo: { connectionId: HOST_A }
})
})
it('keeps two simultaneously registered SSH hosts on their own connections', () => {
registerSshGitProvider(HOST_A, { name: 'git-a' } as never)
registerSshGitProvider(HOST_B, { name: 'git-b' } as never)
expect(resolveWorktreeCreateRoute(repoRow({ executionHostId: 'ssh:target-a' }))).toMatchObject({
connectionId: HOST_A,
repo: { connectionId: HOST_A }
})
expect(resolveWorktreeCreateRoute(repoRow({ executionHostId: 'ssh:target-b' }))).toMatchObject({
connectionId: HOST_B,
repo: { connectionId: HOST_B }
})
})
it('lets an explicit local host win over a surviving connectionId', () => {
// A contradictory row. `getRepoExecutionHostId` answers `local`, which is what the runtime
// create sibling has always done; the raw read sent it remote.
expect(
resolveWorktreeCreateRoute(repoRow({ executionHostId: 'local', connectionId: HOST_A }))
).toEqual({ kind: 'local', hostId: 'local' })
})
it('answers runtime for a runtime row with no nested SSH target', () => {
expect(resolveWorktreeCreateRoute(repoRow({ executionHostId: 'runtime:env-1' }))).toEqual({
kind: 'runtime',
hostId: 'runtime:env-1',
environmentId: 'env-1'
})
})
it('answers runtime for a runtime row whose nested target is dialable here', () => {
registerSshGitProvider(HOST_A, { name: 'git-a' } as never)
expect(
resolveWorktreeCreateRoute(
repoRow({ executionHostId: 'runtime:env-1', connectionId: HOST_A })
)
).toMatchObject({ kind: 'runtime', environmentId: 'env-1' })
})
})
describe('requireWorktreeCreateRoute', () => {
it('refuses a runtime host rather than creating through this client', () => {
expect(() => requireWorktreeCreateRoute(repoRow({ executionHostId: 'runtime:env-1' }))).toThrow(
ExecutionHostNotDispatchableError
)
})
it('passes local and SSH hosts through unchanged', () => {
registerSshGitProvider(HOST_A, { name: 'git-a' } as never)
expect(requireWorktreeCreateRoute(repoRow({}))).toEqual({ kind: 'local', hostId: 'local' })
expect(requireWorktreeCreateRoute(repoRow({ executionHostId: 'ssh:target-a' }))).toMatchObject({
kind: 'ssh',
connectionId: HOST_A
})
})
})