Files
orca/src/main/runtime/runtime-git-generation-context.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

120 lines
4.3 KiB
TypeScript

import type { GlobalSettings } from '../../shared/global-settings-types'
import { gitExecFileAsync } from '../git/runner'
import type { getPullRequestDraftContext } from '../text-generation/pull-request-context'
import {
mergeLegacyCommitMessageAiIntoSourceControlAi,
type ResolvedSourceControlAiGenerationParams
} from '../../shared/source-control-ai'
import type { SourceControlAiOperation } from '../../shared/source-control-ai-types'
import type { CommitMessageAgentRuntimeTarget } from '../text-generation/commit-message-agent-environment'
import type { CommitMessageGenerationTarget } from '../text-generation/commit-message-text-generation'
import type { PullRequestLinkedIssueMeta } from '../source-control/pull-request-linked-issue'
import {
localGitOptionsForTarget,
type RuntimeGitCommandHost,
type RuntimeGitRoute,
type RuntimeGitTarget
} from './runtime-git-command-target'
type PullRequestDraftGitExec = Parameters<typeof getPullRequestDraftContext>[0]
/** Runs the PR draft-context probes on whichever host `route` resolved to. */
export function pullRequestDraftGitExec(
target: RuntimeGitTarget,
route: RuntimeGitRoute
): PullRequestDraftGitExec {
if (route.kind === 'ssh') {
const provider = route.provider
if (!provider) {
throw new Error('ssh_git_provider_unavailable')
}
return (argv, options) => {
const timeoutMs = options?.timeoutMs ?? options?.timeout
return timeoutMs === undefined
? provider.exec(argv, target.worktree.path)
: provider.exec(argv, target.worktree.path, { timeoutMs })
}
}
return (argv, options) =>
gitExecFileAsync(argv, {
cwd: target.worktree.path,
...localGitOptionsForTarget(target),
...(options?.maxBuffer === undefined ? {} : { maxBuffer: options.maxBuffer }),
...(options?.timeoutMs === undefined && options?.timeout === undefined
? {}
: { timeout: options?.timeoutMs ?? options?.timeout }),
admissionTier: 'interactive'
})
}
export type RuntimeCommitMessageSettingsOverride = Partial<
Pick<GlobalSettings, 'commitMessageAi' | 'sourceControlAi' | 'agentCmdOverrides'>
> & {
commitMessageDiscoveryHostKey?: string
sourceControlAiResolvedParams?: ResolvedSourceControlAiGenerationParams
}
export function getRuntimeGitGenerationSettings(
settings: GlobalSettings,
settingsOverride: RuntimeCommitMessageSettingsOverride | undefined,
operation: SourceControlAiOperation
): GlobalSettings {
const mergedSettings = { ...settings, ...settingsOverride }
if (
settingsOverride?.commitMessageAi !== undefined &&
settingsOverride.sourceControlAi === undefined
) {
mergedSettings.sourceControlAi = mergeLegacyCommitMessageAiIntoSourceControlAi(
settings.sourceControlAi,
settingsOverride.commitMessageAi,
{ pullRequestInstructionsFromLegacy: operation === 'pullRequest' }
)
}
return mergedSettings
}
export function localAgentRuntimeTargetForTarget(
target: RuntimeGitTarget
): CommitMessageAgentRuntimeTarget {
const wslDistro = localGitOptionsForTarget(target).wslDistro
return wslDistro ? { runtime: 'wsl', wslDistro } : { runtime: 'host' }
}
export function localTextGenerationTargetForTarget(
target: RuntimeGitTarget,
env?: NodeJS.ProcessEnv
): Extract<CommitMessageGenerationTarget, { kind: 'local' }> {
const wslDistro = localGitOptionsForTarget(target).wslDistro
return {
kind: 'local',
cwd: target.worktree.path,
...(wslDistro ? { wslDistro } : {}),
...(env ? { env } : {})
}
}
export function linkedIssueForTarget(
host: RuntimeGitCommandHost,
target: RuntimeGitTarget
): number | null | undefined {
const live = host.getWorktreeLinkedIssue?.(target.worktree.id)
// Why: `undefined` means the host could not answer, not "unlinked".
return live === undefined ? target.worktree.linkedIssue : live
}
export function linkedIssueMetaForTarget(
host: RuntimeGitCommandHost,
target: RuntimeGitTarget
): PullRequestLinkedIssueMeta | null {
const live = host.getWorktreeLinkedIssueMeta?.(target.worktree.id)
if (live !== undefined) {
return live
}
const liveGitHubIssue = host.getWorktreeLinkedIssue?.(target.worktree.id)
return {
linkedIssue: liveGitHubIssue === undefined ? target.worktree.linkedIssue : liveGitHubIssue,
linkedGitLabIssue: target.worktree.linkedGitLabIssue,
linkedWorkItem: target.worktree.linkedWorkItem
}
}