import { materializeWorktreePushTargetRemote } from '../ipc/worktree-remote' import { getLocalProjectWorktreeGitOptions } from '../project-runtime-git-options' import type { GitPushTarget } from '../../shared/worktree/types' import type { Repo } from '../../shared/repo-types' import { getRepoExecutionHostId, LOCAL_EXECUTION_HOST_ID, parseExecutionHostId } from '../../shared/execution-host' import { hostWorktreeTarget } from '../../shared/worktree/host-qualified-identity' import type { WorktreePushTargetStore } from '../ipc/worktree-push-target-cleanup' import type { ProjectRuntimeResolutionStore } from '../local-project-runtime-resolution' type TerminalPushTargetStore = WorktreePushTargetStore & ProjectRuntimeResolutionStore // Why (#17828): a fork-PR remote deferred at worktree-create time must exist before an // autonomous agent's raw git commands run in a freshly opened terminal -- "sync through // Orca first" isn't an option mid-task. Fires on every terminal spawn into the worktree; // materialize() is already a no-op once the remote exists, so repeat spawns cost one probe. // Never awaited by callers: terminal spawn must not block on remote-add/fetch network I/O. export function triggerTerminalSpawnPushTargetMaterialization( worktreePath: string, pushTarget: GitPushTarget | undefined, repo: Repo | null | undefined, store: TerminalPushTargetStore | null | undefined, repoId?: string, worktreeId?: string ): void { // A server's own `runtime:` stamp runs here too; only an `ssh:` row's git runs elsewhere. const hostId = repo ? getRepoExecutionHostId(repo) : LOCAL_EXECUTION_HOST_ID if ( !pushTarget?.remoteUrl || pushTarget.remoteCreated || repo?.connectionId || parseExecutionHostId(hostId)?.kind === 'ssh' ) { return } materializeWorktreePushTargetRemote( worktreePath, pushTarget, store, repoId, localGitOptionsForTerminalSpawn(store, repo), worktreeId ? hostWorktreeTarget({ id: worktreeId, hostId }) : undefined ).catch((error: unknown) => { console.warn( `[terminal-spawn] failed to materialize push target remote for ${worktreePath}:`, error ) }) } function localGitOptionsForTerminalSpawn( store: ProjectRuntimeResolutionStore | null | undefined, repo: Repo | null | undefined ): { wslDistro?: string } { if (!store || !repo) { return {} } try { // Why: a WSL-hosted repo's remote add/fetch must run under the same distro as // the terminal, or it can target the wrong git binary entirely (repair-required // project runtimes throw here -- fall back to host git rather than crash spawn). return getLocalProjectWorktreeGitOptions(store, repo) } catch (error) { console.warn(`[terminal-spawn] failed to resolve local git options for ${repo.path}:`, error) return {} } }