From de29a9da12cd525b99a6971a2844bc873535ae71 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:12:36 -0800 Subject: [PATCH] fix(hooks): stop optional setup reads at the deadline and cover automation recovery, VM and web paths --- .../refs/resolution/repo-default-base-ref.ts | 6 +++- .../creation/worktree-base-ref-probe.ts | 1 + .../create-source-setup-hooks.ts | 33 ++++++++++--------- .../authoring/automation-setup-review.ts | 2 +- ...worktree-creation-hook-preparation.test.ts | 27 +++++++++++++++ .../worktree-creation-hook-preparation.ts | 15 +++++++-- .../src/web/preload-api/web-review-api.ts | 5 +-- 7 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.test.ts diff --git a/src/main/git/refs/resolution/repo-default-base-ref.ts b/src/main/git/refs/resolution/repo-default-base-ref.ts index b1592b3a736..d05aa7f84a6 100644 --- a/src/main/git/refs/resolution/repo-default-base-ref.ts +++ b/src/main/git/refs/resolution/repo-default-base-ref.ts @@ -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 } : {}) } } diff --git a/src/main/git/worktrees/creation/worktree-base-ref-probe.ts b/src/main/git/worktrees/creation/worktree-base-ref-probe.ts index 748eb448d29..7f54c788994 100644 --- a/src/main/git/worktrees/creation/worktree-base-ref-probe.ts +++ b/src/main/git/worktrees/creation/worktree-base-ref-probe.ts @@ -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. */ diff --git a/src/main/workspace-hooks/create-source-setup-hooks.ts b/src/main/workspace-hooks/create-source-setup-hooks.ts index a20f2e6b93c..36c510affa2 100644 --- a/src/main/workspace-hooks/create-source-setup-hooks.ts +++ b/src/main/workspace-hooks/create-source-setup-hooks.ts @@ -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 (work: () => Promise): Promise => { 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[0], + gitOptions: LocalGitExecOptions, step: (work: () => Promise) => Promise ): Promise { - 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 diff --git a/src/renderer/src/components/automations/authoring/automation-setup-review.ts b/src/renderer/src/components/automations/authoring/automation-setup-review.ts index a0e40aef3c0..48359df0a4c 100644 --- a/src/renderer/src/components/automations/authoring/automation-setup-review.ts +++ b/src/renderer/src/components/automations/authoring/automation-setup-review.ts @@ -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 diff --git a/src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.test.ts b/src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.test.ts new file mode 100644 index 00000000000..0dc5946b673 --- /dev/null +++ b/src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.test.ts @@ -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' }) + }) +}) diff --git a/src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.ts b/src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.ts index 4d432d898d6..0c454bf1ad8 100644 --- a/src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.ts +++ b/src/renderer/src/lib/workspace/creation/worktree-creation-hook-preparation.ts @@ -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 } diff --git a/src/renderer/src/web/preload-api/web-review-api.ts b/src/renderer/src/web/preload-api/web-review-api.ts index 129fa7c0d06..1bdcfc177fa 100644 --- a/src/renderer/src/web/preload-api/web-review-api.ts +++ b/src/renderer/src/web/preload-api/web-review-api.ts @@ -13,10 +13,11 @@ export function createRuntimeNamespaceApi(prefix: string): never { export function createHooksApi(): NonNullable['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 }),