Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/main/git/worktree-create-preparation-real-git.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { execFileSync } from 'node:child_process'
import { existsSync, watch, type FSWatcher } from 'node:fs'
import { mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import * as gitRunner from './runner'
import {
createWorktreePreparationLockReason,
isWorktreeCreatePreparation,
Expand Down Expand Up @@ -46,6 +48,60 @@ afterEach(async () => {
})

describe('prepared worktree creation with real Git', () => {
it('removes partial checkout files and registration after materialization is aborted', async () => {
const { repoPath, root } = await createRepo()
await Promise.all(
Array.from({ length: 1000 }, (_, index) =>
writeFile(
join(repoPath, `payload-${index.toString().padStart(4, '0')}.txt`),
'payload'.repeat(128)
)
)
)
git(repoPath, ['add', '.'])
git(repoPath, ['commit', '--quiet', '-m', 'materialization fixture'])
const preparationRoot = join(root, WORKTREE_CREATE_PREPARATION_DIRECTORY)
const preparedPath = join(preparationRoot, `${process.pid}-partial`)
await mkdir(preparationRoot, { recursive: true })
const controller = new AbortController()
const original = gitRunner.gitExecFileAsync
let watcher: FSWatcher | undefined
let observedMaterialization = false
const calls: string[][] = []
const spy = vi.spyOn(gitRunner, 'gitExecFileAsync').mockImplementation((args, options) => {
calls.push([...args])
if (args.includes('reset')) {
watcher = watch(preparedPath, (_event, filename) => {
// Only the reset writes here, so an event without a filename is still materialization.
if (filename === null || filename.toString().startsWith('payload-')) {
observedMaterialization = true
watcher?.close()
controller.abort()
}
})
}
return original(args, options)
})
try {
await expect(
prepareWorktreeCreateCheckout(
repoPath,
preparedPath,
'main',
createWorktreePreparationLockReason('partial-test'),
{ signal: controller.signal }
)
).rejects.toThrow()
expect(observedMaterialization).toBe(true)
expect(calls.some((args) => args[args.indexOf('worktree') + 1] === 'lock')).toBe(false)
expect(existsSync(preparedPath)).toBe(false)
expect(await listWorktrees(repoPath, { includeCreatePreparations: true })).toHaveLength(1)
} finally {
watcher?.close()
spy.mockRestore()
}
})

it('cleans up when the create signal is canceled', async () => {
const { repoPath, root } = await createRepo()
const preparationRoot = join(root, WORKTREE_CREATE_PREPARATION_DIRECTORY)
Expand Down
158 changes: 158 additions & 0 deletions src/main/git/worktree-preparation-cancel-latency.bench.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Opt in: ORCA_WORKTREE_PREPARATION_CANCEL_BENCH=1 pnpm exec vitest run --config config/vitest.config.ts src/main/git/worktree-preparation-cancel-latency.bench.test.ts
//
// Measures the create-side cost of an obsolete preparation: the wall time of a fresh checkout
// (the next Create's critical path) while an evicted preparation's checkout is either left running
// (main before #18951) or aborted (after). Same code, same fixture; only the abort differs.
import { execFileSync } from 'node:child_process'
import { existsSync } from 'node:fs'
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { performance } from 'node:perf_hooks'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import {
createWorktreePreparationLockReason,
WORKTREE_CREATE_PREPARATION_DIRECTORY
} from '../../shared/worktree/create-preparation'
import {
discardPreparedWorktree,
prepareWorktreeCreateCheckout
} from './worktree-create-preparation'

const describeBench = process.env.ORCA_WORKTREE_PREPARATION_CANCEL_BENCH ? describe : describe.skip
const FILE_COUNT = Number(process.env.ORCA_WORKTREE_PREPARATION_CANCEL_BENCH_FILES ?? 6000)
const FILE_BYTES = 48 * 1024
const TRIALS = Number(process.env.ORCA_WORKTREE_PREPARATION_CANCEL_BENCH_TRIALS ?? 5)
const OBSOLETE_COUNTS = [1, 3]
const RESULT_PATH = process.env.ORCA_WORKTREE_PREPARATION_CANCEL_BENCH_RESULT

type Variant = 'running' | 'aborted'
type Sample = { variant: Variant; obsolete: number; freshCheckoutMs: number }

let root = ''
let repoPath = ''
let preparationRoot = ''
let sequence = 0

function git(cwd: string, args: string[]): void {
execFileSync('git', args, { cwd, stdio: ['ignore', 'ignore', 'pipe'] })
}

function nextPreparedPath(label: string): string {
sequence += 1
return join(preparationRoot, `${process.pid}-${label}-${sequence}`)
}

function checkout(preparedPath: string, signal?: AbortSignal): Promise<void> {
return prepareWorktreeCreateCheckout(
repoPath,
preparedPath,
'main',
createWorktreePreparationLockReason(`bench-${sequence}`),
signal ? { signal } : {}
)
}

async function runTrial(variant: Variant, obsolete: number): Promise<Sample> {
const controllers = Array.from({ length: obsolete }, () => new AbortController())
const obsoletePaths = controllers.map(() => nextPreparedPath('obsolete'))
const obsoleteWork = obsoletePaths.map((path, index) =>
checkout(path, controllers[index].signal).catch(() => {})
)
if (variant === 'aborted') {
// Eviction aborts in the same turn the incoming preparation is armed, so abort before the
// fresh checkout starts.
controllers.forEach((controller) => controller.abort())
Comment on lines +59 to +65

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Synchronize obsolete checkout materialization before timing.

checkout() is asynchronous and first awaits base-ref resolution. These lines do not prove that any obsolete checkout reached materialization before aborting or starting the fresh checkout. Line 60 also hides failures in the running variant.

Wait until each obsolete worktree has entered materialization before the variant branch. Fail a running trial when an obsolete checkout rejects. Otherwise, the report can compare pre-start cancellation or no contention against normal checkout work.

}
const freshPath = nextPreparedPath('fresh')
const started = performance.now()
await checkout(freshPath)
const freshCheckoutMs = performance.now() - started
await Promise.all(obsoleteWork)
await Promise.all(
[...obsoletePaths, freshPath].map((path) =>
discardPreparedWorktree(repoPath, path).catch(() => {})
)
)
return { variant, obsolete, freshCheckoutMs }
}

function median(values: number[]): number {
const sorted = [...values].sort((left, right) => left - right)
const middle = Math.floor(sorted.length / 2)
return sorted.length % 2 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2
}

describeBench('obsolete preparation cancellation latency', () => {
beforeAll(async () => {
root = await mkdtemp(join(tmpdir(), 'orca-preparation-cancel-bench-'))
repoPath = join(root, 'repo')
preparationRoot = join(root, WORKTREE_CREATE_PREPARATION_DIRECTORY)
await mkdir(preparationRoot, { recursive: true })
execFileSync('git', ['init', '--quiet', repoPath])
git(repoPath, ['symbolic-ref', 'HEAD', 'refs/heads/main'])
git(repoPath, ['config', 'user.email', 'bench@example.com'])
git(repoPath, ['config', 'user.name', 'Bench'])
git(repoPath, ['config', 'core.autocrlf', 'false'])
// Unique content per file so the object store cannot dedupe the materialization work.
for (let batch = 0; batch < FILE_COUNT; batch += 500) {
await Promise.all(
Array.from({ length: Math.min(500, FILE_COUNT - batch) }, (_, offset) => {
const index = batch + offset
return writeFile(
join(repoPath, `payload-${index.toString().padStart(5, '0')}.txt`),
`${index}\n`.repeat(Math.ceil(FILE_BYTES / `${index}\n`.length))
)
})
)
}
git(repoPath, ['add', '.'])
git(repoPath, ['commit', '--quiet', '-m', 'bench fixture'])
}, 600_000)

afterAll(async () => {
await rm(root, { recursive: true, force: true })
})

it('reports fresh checkout wall time with obsolete checkouts running vs aborted', async () => {
// Warm the object store and page cache once so the first variant is not penalised.
const warm = nextPreparedPath('warm')
await checkout(warm)
await discardPreparedWorktree(repoPath, warm)

const samples: Sample[] = []
for (const obsolete of OBSOLETE_COUNTS) {
for (let trial = 0; trial < TRIALS; trial += 1) {
// Alternate order so drift in cache or thermal state does not favour one variant.
const order: Variant[] = trial % 2 ? ['aborted', 'running'] : ['running', 'aborted']
for (const variant of order) {
samples.push(await runTrial(variant, obsolete))
}
}
}
const summary = OBSOLETE_COUNTS.map((obsolete) => {
const pick = (variant: Variant): number[] =>
samples
.filter((sample) => sample.variant === variant && sample.obsolete === obsolete)
.map((sample) => sample.freshCheckoutMs)
const running = median(pick('running'))
const aborted = median(pick('aborted'))
return {
obsolete,
trials: TRIALS,
freshCheckoutMedianMs: { obsoleteRunning: running, obsoleteAborted: aborted },
speedup: running / aborted
}
})
const report = JSON.stringify(
{ fixture: { files: FILE_COUNT, bytesPerFile: FILE_BYTES }, samples, summary },
null,
2
)
console.log(report)
if (RESULT_PATH) {
await writeFile(RESULT_PATH, `${report}\n`)
}
expect(existsSync(preparationRoot)).toBe(true)
}, 900_000)
})
27 changes: 20 additions & 7 deletions src/main/worktree-create-preparation-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export type PreparationEntry = {
createdAt: number
ready: Promise<void>
expiration: NodeJS.Timeout
controller: AbortController
checkoutStarted: boolean
}

export type StartPreparationArgs = {
Expand Down Expand Up @@ -68,6 +70,9 @@ async function discardEntry(entry: PreparationEntry): Promise<void> {
// A failed checkout self-discards, but that self-discard is best-effort too, so it can strand the
// registration for the same reason the discard here can. Enrol either way.
await entry.ready.catch(() => {})
if (!entry.checkoutStarted) {
return
}
await discardPreparationWithRetry({
hostKey: preparationHostKey(entry.repoPathKey, entry.wslDistro),
repoPath: entry.repoPath,
Expand All @@ -86,6 +91,7 @@ function expireEntry(entry: PreparationEntry): void {
return
}
preparations.delete(entry.key)
entry.controller.abort()
discardEntryInBackground(entry)
}

Expand Down Expand Up @@ -118,6 +124,7 @@ function enforcePreparationLimit(
}
preparations.delete(victim.key)
clearTimeout(victim.expiration)
victim.controller.abort()
discardEntryInBackground(victim)
}
}
Expand Down Expand Up @@ -163,6 +170,10 @@ export function startPreparation({
WORKTREE_CREATE_PREPARATION_DIRECTORY
)
const preparedPath = pathOps(workspaceRoot).join(preparationRoot, preparationId)
const controller = new AbortController()
const signal = options.signal
? AbortSignal.any([options.signal, controller.signal])
: controller.signal
const entry = {} as PreparationEntry
const expiration = setTimeout(() => expireEntry(entry), WORKTREE_CREATE_PREPARATION_TTL_MS)
expiration.unref()
Expand All @@ -179,17 +190,19 @@ export function startPreparation({
options,
createdAt: Date.now(),
expiration,
controller,
checkoutStarted: false,
ready: (async () => {
await cleanupStalePreparations(preparationHostKey(repoPathKey, wslDistro), repoPath, options)
signal.throwIfAborted()
await mkdir(toHostFilesystemPath(preparationRoot), { recursive: true })
signal.throwIfAborted()
// Already canonical, so the add re-resolves nothing.
await prepareWorktreeCreateCheckout(
repoPath,
preparedPath,
canonicalBase,
lockReason,
options
)
entry.checkoutStarted = true
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
await prepareWorktreeCreateCheckout(repoPath, preparedPath, canonicalBase, lockReason, {
...options,
signal
})
})()
} satisfies PreparationEntry)
preparations.set(key, entry)
Expand Down
Loading
Loading