fix(hooks): stop optional setup reads at the deadline and cover automation recovery, VM and web paths

This commit is contained in:
Brennan Benson
2026-11-12 21:12:36 -08:00
parent c220cfb605
commit de29a9da12
7 changed files with 68 additions and 21 deletions
@@ -5,12 +5,15 @@ import { resolveDefaultBaseRefViaExec } from '../../../../shared/git/refs/git-de
export type LocalGitExecOptions = {
wslDistro?: string
admissionTier?: GitAdmissionTier
/** Checked at admission; a bounded optional read refuses git that would start past its deadline. */
canStart?: () => boolean
}
export type LocalDefaultBaseRefGitOptions = {
cwd: string
wslDistro?: string
admissionTier?: GitAdmissionTier
canStart?: () => boolean
}
export const DEFAULT_BASE_REF_PROBE_TIMEOUT_MS = 15_000
@@ -22,7 +25,8 @@ export function gitExecOptions(
return {
cwd,
...(options.wslDistro ? { wslDistro: options.wslDistro } : {}),
...(options.admissionTier ? { admissionTier: options.admissionTier } : {})
...(options.admissionTier ? { admissionTier: options.admissionTier } : {}),
...(options.canStart ? { canStart: options.canStart } : {})
}
}
@@ -8,6 +8,7 @@ import { resolveWorktreeAddBaseRef } from '../../../../shared/worktree/base-ref'
type GitExecOptions = {
wslDistro?: string
admissionTier?: GitAdmissionTier
canStart?: () => boolean
}
/** Return the probe's commit OID so callers need not resolve it again. */
@@ -57,7 +57,8 @@ export async function readCreateSourceSetupHooks(args: {
return undefined
}
const deadline = Date.now() + CREATE_SOURCE_READ_DEADLINE_MS
// No git process starts after the deadline; one already running finishes under its own timeout.
// Git admission refuses past the deadline, even for a step the race already abandoned.
const gated = { ...args.gitOptions, canStart: () => Date.now() < deadline }
const step = async <T>(work: () => Promise<T>): Promise<T> => {
const remaining = deadline - Date.now()
if (remaining <= 0) {
@@ -76,8 +77,8 @@ export async function readCreateSourceSetupHooks(args: {
}
}
try {
const ref = await step(() => resolveCreateSourceRef(args, step))
return ref ? await step(() => readOrcaYamlAtRef(args.repo, ref, args.gitOptions)) : undefined
const ref = await step(() => resolveCreateSourceRef(args, gated, step))
return ref ? await step(() => readOrcaYamlAtRef(args.repo, ref, gated)) : undefined
} catch {
return undefined
}
@@ -85,9 +86,10 @@ export async function readCreateSourceSetupHooks(args: {
async function resolveCreateSourceRef(
args: Parameters<typeof readCreateSourceSetupHooks>[0],
gitOptions: LocalGitExecOptions,
step: <T>(work: () => Promise<T>) => Promise<T>
): Promise<string | null> {
const { repo, source, runtime, gitOptions } = args
const { repo, source, runtime } = args
// Explicit reuse checks out exactly the local branch when it exists, otherwise creates it from the base.
if (source.kind === 'branch') {
const localRef = `refs/heads/${source.ref}`
@@ -124,9 +126,9 @@ async function resolveCreateSourceRef(
])
)
if (hasRemoteRef || !hasLocalRef) {
// Never rejects or aborts: past the deadline create joins this same fetch.
// Ungated and never aborted: past the deadline create joins this same fetch.
await step(() =>
runtime.getOrStartRemoteTrackingBaseRefresh(repo.path, remoteBase, gitOptions)
runtime.getOrStartRemoteTrackingBaseRefresh(repo.path, remoteBase, args.gitOptions)
)
}
}
@@ -144,17 +146,18 @@ async function readOrcaYamlAtRef(
repo: Repo,
ref: string,
gitOptions: LocalGitExecOptions
): Promise<{ hooks: OrcaHooks | null }> {
const { stdout: listing } = await gitExecFileAsync(
['ls-tree', '--name-only', ref, '--', 'orca.yaml'],
{
cwd: repo.path,
...gitOptions
}
)
if (!listing.trim()) {
): Promise<{ hooks: OrcaHooks | null } | undefined> {
const { stdout: entry } = await gitExecFileAsync(['ls-tree', ref, '--', 'orca.yaml'], {
cwd: repo.path,
...gitOptions
})
if (!entry.trim()) {
return { hooks: null }
}
// A symlink's blob is its target path; the checkout follows it, so read the checkout instead.
if (!entry.startsWith('100')) {
return undefined
}
const { stdout } = await gitExecFileAsync(['show', `${ref}:orca.yaml`], {
cwd: repo.path,
...gitOptions
@@ -119,7 +119,7 @@ export async function reconfirmSkippedSetup(
hostId,
'automation'
)
: await ensureHooksApproved(useAppStore.getState(), repoId, 'setup', hostId)
: await approveAutomationSetup(repoId, hostId, automation.baseBranch)
return approval.decision === 'run' && approval.approvedContentHash !== automation.setupTrustHash
? approval.approvedContentHash
: undefined
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest'
import { setupHooksCreateSource } from './worktree-creation-hook-preparation'
describe('setupHooksCreateSource', () => {
it('reads the named branch only for explicit reuse', () => {
expect(
setupHooksCreateSource({
branchIntent: 'open-existing-v2',
branchNameOverride: 'feature',
baseBranch: 'origin/main'
})
).toEqual({ kind: 'branch', ref: 'feature', baseBranch: 'origin/main' })
})
it('reads the base when a new branch only borrows the name', () => {
expect(
setupHooksCreateSource({
branchIntent: 'create-new',
branchNameOverride: 'feature',
baseBranch: 'origin/main'
})
).toEqual({ kind: 'base', baseBranch: 'origin/main' })
expect(
setupHooksCreateSource({ branchNameOverride: 'pr-branch', baseBranch: 'refs/pull/7/head' })
).toEqual({ kind: 'base', baseBranch: 'refs/pull/7/head' })
})
})
@@ -91,8 +91,19 @@ export async function prepareWorktreeCreationHooks(
return prepared
}
/** Only explicit reuse checks out the named branch; every other create starts from the base. */
function setupHooksCreateSource(request: WorktreeCreationRequest): SetupHooksCreateSource {
/**
* Only explicit reuse checks out the named branch; every other create starts from the base. A VM
* create picks its base after this check, so it keeps reading the project checkout.
*/
export function setupHooksCreateSource(
request: Pick<
WorktreeCreationRequest,
'branchIntent' | 'branchNameOverride' | 'baseBranch' | 'ephemeralVmRecipe'
>
): SetupHooksCreateSource | undefined {
if (request.ephemeralVmRecipe) {
return undefined
}
return request.branchIntent === 'open-existing-v2' && request.branchNameOverride
? { kind: 'branch', ref: request.branchNameOverride, baseBranch: request.baseBranch }
: { kind: 'base', baseBranch: request.baseBranch }
@@ -13,10 +13,11 @@ export function createRuntimeNamespaceApi(prefix: string): never {
export function createHooksApi(): NonNullable<Partial<PreloadApi>['hooks']> {
return {
check: async ({ repoId, background }) =>
check: async ({ repoId, background, createSource }) =>
callRuntimeResult('repo.hooksCheck', {
repo: repoId,
...repoHooksCheckWakeParams(background)
...repoHooksCheckWakeParams(background),
...(createSource ? { createSource } : {})
}),
inspectSetupScriptImports: async ({ repoId }) =>
callRuntimeResult('repo.setupScriptImports', { repo: repoId }),