Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(git-snapshot): bound automatic worktree captures before sending
zcinta0514ddab4afb3af4
diff --git a/apps/desktop/src/main/__tests__/gitSnapshotCoordinator.test.ts b/apps/desktop/src/main/__tests__/gitSnapshotCoordinator.test.tsindex 454b75d337..9c7c913cbd 100644--- a/apps/desktop/src/main/__tests__/gitSnapshotCoordinator.test.ts+++ b/apps/desktop/src/main/__tests__/gitSnapshotCoordinator.test.ts@@ -6,6 +6,7 @@ import {type GitSnapshotCoordinatorDeps,} from '../git-snapshot/gitSnapshotCoordinator';import { enqueueGitRepoWrite } from '../git-snapshot/gitRepoWriteQueue';+import { SnapshotCaptureBudgetExceededError } from '../git-snapshot/snapshotFileFilter';import type {CreateShadowSavepointInput,ShadowSavepointResult,@@ -259,6 +260,9 @@ describe('GitSnapshotCoordinator', () => {expect(deps.createShadowSavepoint).toHaveBeenCalledTimes(2);expect(deps.createShadowSavepoint).toHaveBeenNthCalledWith(1, '/repo', {sessionId: 's1',+ captureBudget: expect.objectContaining({+ maxFiles: expect.any(Number), maxTotalBytes: expect.any(Number), timeoutMs: expect.any(Number),+ }),label: '本轮开始时的工作区基线',meta: { kind: 'turn-start', anchor: 'msg-1' },});@@ -274,6 +278,7 @@ describe('GitSnapshotCoordinator', () => {baselineCommit: 'hash1',});expect(afterEditInput.skipIfTreeEquals).toBe('hash1');+ expect(afterEditInput.captureBudget).toEqual(vi.mocked(deps.createShadowSavepoint).mock.calls[0][1].captureBudget);expect(typeof afterEditInput.label).toBe('function');expect(deps.createShadowMarker).not.toHaveBeenCalled();});@@ -426,6 +431,25 @@ describe('GitSnapshotCoordinator', () => {});});+ // #6901 unit: a budget failure followed by Stop never reaches onTurnEnd.+ // Existing baseline-failure coverage only exercised completed turns.+ it('fences an over-budget baseline before dispatch even when the turn is later aborted', async () => {+ const deps = makeDeps({+ getSessionContext: vi.fn().mockResolvedValue({ workingDir: '/repo', agentKind: 'pi' }),+ createShadowSavepoint: vi.fn().mockRejectedValueOnce(new SnapshotCaptureBudgetExceededError('file-count'))+ .mockResolvedValue(savepointResult('recovered')),+ });+ const coordinator = new GitSnapshotCoordinator(deps);+ await coordinator.onTurnStart('s1');+ expect(deps.createShadowMarker).toHaveBeenCalledOnce();+ expect(vi.mocked(deps.createShadowMarker).mock.calls[0][1].meta.kind).toBe('rewind-blocked');+ coordinator.onTurnAbort('s1');+ expect(coordinator.hasPendingTurnStart('s1')).toBe(false);+ await coordinator.onTurnStart('s1');+ await coordinator.onTurnEnd('s1');+ expect(deps.createShadowMarker).toHaveBeenCalledOnce();+ });+it('appends a rewind gap marker when the turn introduces newly filtered paths', async () => {// 本轮新出现的被过滤文件(如 Agent 写入 .env):其改动没进任何快照,// 覆盖本轮的回退必须降级,链上补 gap 标记。diff --git a/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.ts b/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.tsindex 15aac32b06..f2b1e2f3cf 100644--- a/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.ts+++ b/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.ts@@ -182,6 +182,7 @@ beforeEach(async () => {});afterEach(async () => {+ vi.restoreAllMocks();await fs.rm(repoPath, { recursive: true, force: true, maxRetries: 20, retryDelay: 100 });if (originalGitLocaleEnv.LC_ALL === undefined) delete process.env.LC_ALL;else process.env.LC_ALL = originalGitLocaleEnv.LC_ALL;@@ -243,6 +244,64 @@ describe('createShadowSavepoint', () => {expect(await readSavepointTip(repoPath, SESSION)).toBeNull();expect(await captureUserVisibleState()).toEqual(before);}, REAL_GIT_TEST_TIMEOUT_MS);+ // #6901: advance the monotonic clock after allocating the temporary index.+ // The real Git executor must reject the expired deadline and the kernel+ // must remove that index, rather than granting staging a fresh timeout.+ it('cleans up its temporary index when the shared budget expires before staging', async () => {+ await commitSeed();+ await writeRepoFile('a.txt', 'aa');+ const before = await captureUserVisibleState();+ const mkdtemp = fs.mkdtemp.bind(fs);+ const now = performance.now.bind(performance);+ const clock = vi.spyOn(performance, 'now');+ let indexDir: string | undefined;+ vi.spyOn(fs, 'mkdtemp').mockImplementation(async (...args) => {+ const dir = await mkdtemp(...args);+ if (String(args[0]).includes('xdt-snapshot-index-')) {+ indexDir = String(dir);+ clock.mockReturnValue(now() + 60_000);+ }+ return dir;+ });+ await expect(createShadowSavepoint(repoPath, {+ sessionId: SESSION, label: 'baseline', meta: { kind: 'turn-start' },+ captureBudget: { maxFiles: 10, maxTotalBytes: 100, timeoutMs: 5_000 },+ })).rejects.toMatchObject({ timedOut: true });+ vi.restoreAllMocks();+ expect(indexDir).toBeDefined();+ await expect(fs.access(indexDir!)).rejects.toMatchObject({ code: 'ENOENT' });+ expect(await readSavepointTip(repoPath, SESSION)).toBeNull();+ expect(await captureUserVisibleState()).toEqual(before);+ });++ // #6901: real Git integration; existing tests cover individual exclusions,+ // not an aggregate capture budget. Two small files must fail as a whole,+ // leaving the user's index and the shadow ref untouched, then recover.+ it.each([+ { maxFiles: 1, maxTotalBytes: 100, timeoutMs: 5_000, reason: 'file-count' },+ { maxFiles: 10, maxTotalBytes: 3, timeoutMs: 5_000, reason: 'total-bytes' },+ { maxFiles: 10, maxTotalBytes: 100, timeoutMs: 0, reason: 'deadline' },+ ])('rejects an over-budget capture without publishing a partial baseline: %j', async ({ reason, ...captureBudget }) => {+ await gitExec(['commit', '--allow-empty', '-m', 'empty project'], repoPath);+ await writeRepoFile('a.txt', 'aa');+ await writeRepoFile('b.txt', 'bb');+ await gitExec(['add', 'a.txt'], repoPath);+ const before = await captureUserVisibleState();+ const input = { sessionId: SESSION, label: 'baseline', meta: { kind: 'turn-start' as const } };++ await expect(createShadowSavepoint(repoPath, { ...input, captureBudget })).rejects.toMatchObject({+ name: 'SnapshotCaptureBudgetExceededError', reason,+ });+ expect(await readSavepointTip(repoPath, SESSION)).toBeNull();+ expect(await captureUserVisibleState()).toEqual(before);+ expect(await fs.readFile(path.join(repoPath, 'b.txt'), 'utf8')).toBe('bb');++ const result = await createShadowSavepoint(repoPath, input);+ expect(await gitStdout(['show', `${result.commit}:a.txt`])).toBe('aa');+ expect(await gitStdout(['show', `${result.commit}:b.txt`])).toBe('bb');+ expect(await captureUserVisibleState()).toEqual(before);+ });+it('never changes HEAD, branch, status or the cached diff', async () => {await commitSeed();await writeRepoFile('staged.txt', 'staged change\n');diff --git a/apps/desktop/src/main/__tests__/snapshotFileFilter.git-integration.test.ts b/apps/desktop/src/main/__tests__/snapshotFileFilter.git-integration.test.tsindex d4b8bcc384..05fb52f633 100644--- a/apps/desktop/src/main/__tests__/snapshotFileFilter.git-integration.test.ts+++ b/apps/desktop/src/main/__tests__/snapshotFileFilter.git-integration.test.ts@@ -69,6 +69,7 @@ describe('buildSnapshotFilePlan', () => {});afterEach(async () => {+ vi.restoreAllMocks();vi.unstubAllEnvs();await fs.rm(testRoot, { recursive: true, force: true });});@@ -83,6 +84,29 @@ describe('buildSnapshotFilePlan', () => {]);});+ // #6901: the file-count gate must run before per-path IO, and the directory+ // cache must not hide a nested .git created between two captures. Real fs+ // fixtures belong in the integration tier; existing tests only scan once.+ it('rejects an oversized status plan before inspecting its paths', async () => {+ const lstat = vi.spyOn(fs, 'lstat');+ await expect(buildSnapshotFilePlanFromEntries(repoPath, [+ { code: '??', path: 'a.txt' }, { code: '??', path: 'b.txt' },+ ], { maxFiles: 1 })).rejects.toMatchObject({ reason: 'file-count' });+ expect(lstat).not.toHaveBeenCalled();+ });++ it('rechecks nested repository markers on the next scan', async () => {+ await writeRepoFile(repoPath, 'nested/a.txt', 'aa');+ await writeRepoFile(repoPath, 'nested/b.txt', 'bb');+ const entries = ['nested/a.txt', 'nested/b.txt'].map((path) => ({ code: '??', path }));+ const first = await buildSnapshotFilePlanFromEntries(repoPath, entries);+ expect(first.includedFiles).toHaveLength(2);+ await fs.mkdir(path.join(repoPath, 'nested/.git'));+ const second = await buildSnapshotFilePlanFromEntries(repoPath, entries);+ expect(second.includedFiles).toEqual([]);+ expect(second.skippedFiles.map((file) => file.reason)).toEqual(['nested-git-repo', 'nested-git-repo']);+ });+it('does not execute repository-local fsmonitor when reading git status', async () => {const marker = path.join(testRoot, 'fsmonitor-ran');const script = path.join(testRoot, 'fsmonitor.js');diff --git a/apps/desktop/src/main/git-snapshot/gitSnapshotCoordinator.ts b/apps/desktop/src/main/git-snapshot/gitSnapshotCoordinator.tsindex 78884bac2b..b7606b50e3 100644--- a/apps/desktop/src/main/git-snapshot/gitSnapshotCoordinator.ts+++ b/apps/desktop/src/main/git-snapshot/gitSnapshotCoordinator.ts@@ -17,6 +17,15 @@ import type {SkippedFileFingerprint,} from './gitSnapshotService';+// Automatic captures sit on the send path. Bound the changed/untracked work+// before staging; a failed capture uses the existing rewind-gap semantics.+// This is not a repository-size limit and does not apply to manual snapshots.+const AUTO_CAPTURE_BUDGET = {+ maxFiles: 10_000,+ maxTotalBytes: 256 * 1024 * 1024,+ timeoutMs: 5_000,+};+interface CoordinatorLogger {info: (msg: string, meta?: Record<string, unknown>) => void;warn: (msg: string, meta?: Record<string, unknown>) => void;@@ -91,6 +100,8 @@ interface TurnStartState {interface TurnStartRecord extends Partial<TurnStartState> {operation: OperationBudget;+ /** Baseline failure is fenced before dispatch, including turns later aborted. */+ rewindGapRecorded?: boolean;autoSnapshotEnabled: boolean;autoInitProjectGit: boolean;promise: Promise<void>;@@ -187,13 +198,25 @@ export class GitSnapshotCoordinator {await enqueueGitRepoWrite(resolved.repoRoot, async () => {const metadata = await operationWait(metadataPromise);record.metadata = metadata;- const turnStart = await operationWait(this.createTurnStartSavepoint(- sessionId,- resolved.repoRoot,- metadata,- ));- record.turnStartCommit = turnStart.commit;- record.turnStartSkippedFingerprints = turnStart.skippedFingerprints;+ try {+ const turnStart = await operationWait(this.createTurnStartSavepoint(+ sessionId,+ resolved.repoRoot,+ metadata,+ ));+ record.turnStartCommit = turnStart.commit;+ record.turnStartSkippedFingerprints = turnStart.skippedFingerprints;+ } finally {+ // Budget failures must fence rewind before the agent can edit files.+ // Waiting until onTurnEnd misses turns that are stopped or fail.+ if (!record.turnStartCommit && resolved.savepointRewind) {+ record.rewindGapRecorded = await this.createRewindBlockedMarker(+ sessionId, resolved.repoRoot,+ 'File rewind gap: turn-start baseline unavailable',+ '[git-snapshot] rewind gap marker created', metadata,+ );+ }+ }});record.metadata ??= await operationWait(metadataPromise);} catch (err) {@@ -317,7 +340,7 @@ export class GitSnapshotCoordinator {// Without a baseline this turn's delta is unrecoverable; append a gap// marker so the file-rewind planner truncates ranges that cross it.// Only codex/pi consume the savepoint chain for file rewind.- if (savepointRewind) {+ if (savepointRewind && !turnStart?.rewindGapRecorded) {await this.createRewindBlockedMarker(sessionId,repoRoot,@@ -358,6 +381,7 @@ export class GitSnapshotCoordinator {try {result = await operationWait(this.deps.createShadowSavepoint(repoRoot, {sessionId,+ captureBudget: AUTO_CAPTURE_BUDGET,label: (diff) =>createAfterEditLabel({ diff, userPrompt: metadata.userPrompt },@@ -459,11 +483,11 @@ export class GitSnapshotCoordinator {repoRoot: string,metadata: TurnStartMetadata,): Promise<{ commit: string | undefined; skippedFingerprints: SkippedFileFingerprint[] }> {- // Always created, dirty or clean, so every turn has a uniform restore- // baseline; an unchanged tree costs almost nothing thanks to object reuse.+ // Attempt a baseline even for a clean worktree; a fresh index still hashes dirty files.operationCheckpoint();const result = await operationWait(this.deps.createShadowSavepoint(repoRoot, {sessionId,+ captureBudget: AUTO_CAPTURE_BUDGET,label: '本轮开始时的工作区基线',meta: {kind: 'turn-start',@@ -525,7 +549,7 @@ export class GitSnapshotCoordinator {label: string,logMessage: string,metadata: TurnStartMetadata,- ): Promise<void> {+ ): Promise<boolean> {operationCheckpoint();const commit = await operationWait(this.deps.createShadowMarker(repoRoot, {sessionId,@@ -538,7 +562,7 @@ export class GitSnapshotCoordinator {if (!commit) {// Marker tree could not be built (git fully unavailable); the failure// is logged by the kernel and the outer swallow keeps the turn alive.- return;+ return false;}this.deps.logger.info(logMessage, {sessionId,@@ -546,6 +570,7 @@ export class GitSnapshotCoordinator {commit: commit.slice(0, 8),...(metadata.anchor ? { anchor: metadata.anchor } : {}),});+ return true;}private pushTurnStartRecord(sessionId: string, record: TurnStartRecord): void {diff --git a/apps/desktop/src/main/git-snapshot/gitSnapshotService.ts b/apps/desktop/src/main/git-snapshot/gitSnapshotService.tsindex 0ad7728a57..fc78f766ac 100644--- a/apps/desktop/src/main/git-snapshot/gitSnapshotService.ts+++ b/apps/desktop/src/main/git-snapshot/gitSnapshotService.ts@@ -18,6 +18,7 @@ import { isolatedGitExec, snapshotGitIsolationArgs } from './isolatedGitExec';import { gitPathOutput } from '../worktree/gitPathOutput';import {buildSnapshotFilePlan,+ checkSnapshotCaptureDeadline,resolveSnapshotGitPath,type SnapshotIncludedFile,type SnapshotFileFilterOptions,@@ -414,6 +415,8 @@ export interface CreateShadowSavepointInput {skipIfTreeEquals?: string;/** Optional safety filter limit overrides, mainly for tests. */fileFilter?: Partial<SnapshotFileFilterOptions>;+ /** Turn-boundary capture only; manual/pre-rollback snapshots stay unbounded. */+ captureBudget?: { maxFiles: number; maxTotalBytes: number; timeoutMs: number };}/**@@ -457,12 +460,23 @@ export async function createShadowSavepoint(repoPath: string,input: CreateShadowSavepointInput,): Promise<ShadowSavepointResult> {- const blockedState = await detectBlockedGitState(repoPath);+ const deadlineAtMs = input.captureBudget+ ? performance.now() + input.captureBudget.timeoutMs+ : undefined;+ checkSnapshotCaptureDeadline(deadlineAtMs);+ const blockedState = await detectBlockedGitState(repoPath, deadlineAtMs);if (blockedState) {throw new SnapshotBlockedByGitStateError(blockedState);}- const plan = await buildSnapshotFilePlan(repoPath, input.fileFilter);+ const plan = await buildSnapshotFilePlan(repoPath, {+ ...input.fileFilter,+ ...(input.captureBudget ? {+ maxFiles: input.captureBudget.maxFiles,+ maxTotalBytes: input.captureBudget.maxTotalBytes,+ deadlineAtMs,+ } : {}),+ });if (plan.statusTruncated) {// status 溢出时脏文件视图未知:空计划会拍出一个静默漏掉全部脏文件的// 基线/pre-rollback,读侧还会据此把未受保护的路径当成安全。失败关闭@@ -486,7 +500,7 @@ export async function createShadowSavepoint(// the repository (a parent segment became a symlink pointing outside) must// be treated as deleted — never handed to `git add`, which would either// refuse it or, worse, snapshot out-of-repo content.- const skippedFingerprints = await collectSkippedFingerprints(repoPath, skippedFiles);+ const skippedFingerprints = await collectSkippedFingerprints(repoPath, skippedFiles, deadlineAtMs);if (input.baselineSkippedFingerprints && skippedFingerprintsChanged(input.baselineSkippedFingerprints, skippedFingerprints)) {throw new Error('File rewind gap: turn changes were partially filtered');}@@ -497,6 +511,7 @@ export async function createShadowSavepoint(let scanIndex = 0;for (const file of plan.includedFiles) {await operationYield(scanIndex++);+ checkSnapshotCaptureDeadline(deadlineAtMs);const abs = resolveSnapshotGitPath(repoPath, file.path);if (!abs) continue;if (!(await pathExists(abs))) {@@ -520,16 +535,16 @@ export async function createShadowSavepoint(isolatedGitExec(['add', '-A', '--pathspec-from-file', pathspecFile, '--pathspec-file-nul'],repoPath,- { extraEnv },+ { extraEnv, deadlineAtMs },),);}for (const chunk of chunkRawPaths(missingPaths)) {- await gitExec(['update-index', '--force-remove', '--', ...chunk], repoPath, { extraEnv });+ await gitExec(['update-index', '--force-remove', '--', ...chunk], repoPath, { extraEnv, deadlineAtMs });}- await removeRenameOldPaths(repoPath, renameOldPaths, extraEnv);+ await removeRenameOldPaths(repoPath, renameOldPaths, extraEnv, deadlineAtMs);- const tree = (await gitExec(['write-tree'], repoPath, { extraEnv })).stdout.trim();+ const tree = (await gitExec(['write-tree'], repoPath, { extraEnv, deadlineAtMs })).stdout.trim();let unchanged = false;if (input.skipIfTreeEquals) {@@ -585,18 +600,20 @@ export async function createShadowSavepoint(skippedFiles,skippedFingerprints,};- });+ }, deadlineAtMs);}/** lstat fingerprints (no content) for skipped paths that exist as files. */async function collectSkippedFingerprints(repoPath: string,skippedFiles: readonly SnapshotSkippedFile[],+ deadlineAtMs?: number,): Promise<SkippedFileFingerprint[]> {const out: SkippedFileFingerprint[] = [];let scanIndex = 0;for (const file of skippedFiles) {await operationYield(scanIndex++);+ checkSnapshotCaptureDeadline(deadlineAtMs);const abs = resolveSnapshotGitPath(repoPath, file.path);if (!abs) continue;const stats = await fs.lstat(abs).catch(() => null);@@ -968,9 +985,9 @@ function renameOldPathsToRemove(files: readonly SnapshotCommitFile[]): string[]);}-async function detectBlockedGitState(repoPath: string): Promise<SnapshotBlockedGitState | null> {+async function detectBlockedGitState(repoPath: string, deadlineAtMs?: number): Promise<SnapshotBlockedGitState | null> {for (const { reason, marker } of BLOCKED_GIT_STATE_MARKERS) {- const markerPath = await resolveGitInternalPath(repoPath, marker);+ const markerPath = await resolveGitInternalPath(repoPath, marker, deadlineAtMs);if (markerPath && (await pathExists(markerPath))) {return { reason, marker, markerPath };}@@ -978,8 +995,8 @@ async function detectBlockedGitState(repoPath: string): Promise<SnapshotBlockedGreturn null;}-async function resolveGitInternalPath(repoPath: string, marker: string): Promise<string | null> {- const { stdout } = await gitExec(['rev-parse', '--git-path', marker], repoPath);+async function resolveGitInternalPath(repoPath: string, marker: string, deadlineAtMs?: number): Promise<string | null> {+ const { stdout } = await gitExec(['rev-parse', '--git-path', marker], repoPath, { deadlineAtMs });const gitPath = gitPathOutput(stdout);if (!gitPath) return null;return path.isAbsolute(gitPath) ? gitPath : path.resolve(repoPath, gitPath);@@ -995,11 +1012,13 @@ async function pathExists(filePath: string): Promise<boolean> {async function withTemporaryIndex<T>(repoPath: string,fn: (extraEnv: Record<string, string>) => Promise<T>,+ deadlineAtMs?: number,): Promise<T> {+ checkSnapshotCaptureDeadline(deadlineAtMs);const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'xdt-snapshot-index-'));const extraEnv = { GIT_INDEX_FILE: path.join(dir, 'index') };try {- await seedTemporaryIndex(repoPath, extraEnv);+ await seedTemporaryIndex(repoPath, extraEnv, deadlineAtMs);return await fn(extraEnv);} finally {await fs.rm(dir, { recursive: true, force: true });@@ -1009,9 +1028,10 @@ async function withTemporaryIndex<T>(async function seedTemporaryIndex(repoPath: string,extraEnv: Record<string, string>,+ deadlineAtMs?: number,): Promise<void> {try {- await gitExec(['read-tree', 'HEAD'], repoPath, { extraEnv });+ await gitExec(['read-tree', 'HEAD'], repoPath, { extraEnv, deadlineAtMs });} catch (err) {if (err instanceof GitExecError && isUnbornHeadError(err)) return;throw err;@@ -1038,9 +1058,10 @@ async function removeRenameOldPaths(repoPath: string,oldPaths: readonly string[],extraEnv: Record<string, string>,+ deadlineAtMs?: number,): Promise<void> {for (const chunk of chunkRawPaths(oldPaths)) {- await gitExec(['update-index', '--force-remove', '--', ...chunk], repoPath, { extraEnv });+ await gitExec(['update-index', '--force-remove', '--', ...chunk], repoPath, { extraEnv, deadlineAtMs });}}diff --git a/apps/desktop/src/main/git-snapshot/snapshotFileFilter.ts b/apps/desktop/src/main/git-snapshot/snapshotFileFilter.tsindex 9834e098dd..65c23cbc55 100644--- a/apps/desktop/src/main/git-snapshot/snapshotFileFilter.ts+++ b/apps/desktop/src/main/git-snapshot/snapshotFileFilter.ts@@ -45,6 +45,29 @@ export interface SnapshotIncludedFile {export interface SnapshotFileFilterOptions {maxFileBytes: number;maxContentScanBytes: number;+ /** Whole-capture limits. Exceeding one aborts the plan, never truncates it. */+ maxFiles?: number;+ maxTotalBytes?: number;+ /** Monotonic deadline shared with Git status and staging. */+ deadlineAtMs?: number;+}++export class SnapshotCaptureBudgetExceededError extends Error {+ constructor(readonly reason: 'file-count' | 'total-bytes' | 'deadline') {+ super(`Automatic snapshot capture budget exceeded: ${reason}`);+ this.name = 'SnapshotCaptureBudgetExceededError';+ }+}++export function checkSnapshotCaptureDeadline(deadlineAtMs?: number): void {+ if (deadlineAtMs !== undefined && performance.now() >= deadlineAtMs) {+ throw new SnapshotCaptureBudgetExceededError('deadline');+ }+}++interface FilePlanScan {+ gitMarkers: Map<string, boolean>;+ totalBytes: number;}/** A parsed `git status --porcelain=v1 -z` entry. */@@ -70,6 +93,7 @@ export interface SnapshotFilePlan {function withDefaults(opts?: Partial<SnapshotFileFilterOptions>): SnapshotFileFilterOptions {return {+ ...opts,maxFileBytes: opts?.maxFileBytes ?? DEFAULT_MAX_FILE_BYTES,maxContentScanBytes: opts?.maxContentScanBytes ?? DEFAULT_MAX_CONTENT_SCAN_BYTES,};@@ -160,11 +184,13 @@ export function parseStatusPorcelainZ(output: string): SnapshotStatusEntry[] {async function readStatusEntries(repoPath: string,+ deadlineAtMs?: number,): Promise<{ entries: SnapshotStatusEntry[]; truncated: boolean }> {try {const { stdout } = await isolatedGitExec(['status', '--porcelain=v1', '-z', '--untracked-files=all'],repoPath,+ { deadlineAtMs },);return { entries: parseStatusPorcelainZ(stdout), truncated: false };} catch (err) {@@ -178,18 +204,20 @@ function isGitStatusOutputTooLargeError(err: unknown): boolean {return err.cause?.code === 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' || /maxBuffer/i.test(err.message);}-async function hasGitMarker(dir: string, markers: Map<string, boolean>): Promise<boolean> {- const cached = markers.get(dir);+async function hasGitMarker(dir: string, cache: Map<string, boolean>): Promise<boolean> {+ const cached = cache.get(dir);if (cached !== undefined) return cached;const exists = await fs.lstat(path.join(dir, '.git')).then(() => true,() => false,);- markers.set(dir, exists);+ cache.set(dir, exists);return exists;}-async function isInsideNestedGitRepo(repoPath: string, gitPath: string, markers: Map<string, boolean>): Promise<boolean> {+async function isInsideNestedGitRepo(+ repoPath: string, gitPath: string, scan: FilePlanScan, opts: SnapshotFileFilterOptions,+): Promise<boolean> {const abs = resolveSnapshotGitPath(repoPath, gitPath);if (!abs) return false;@@ -203,9 +231,10 @@ async function isInsideNestedGitRepo(repoPath: string, gitPath: string, markers:const repoRoot = path.resolve(repoPath);let cur = stat?.isDirectory() ? abs : path.dirname(abs);while (path.resolve(cur) !== repoRoot) {+ checkSnapshotCaptureDeadline(opts.deadlineAtMs);const rel = path.relative(repoRoot, cur);if (rel.startsWith('..') || path.isAbsolute(rel)) return false;- if (await hasGitMarker(cur, markers)) return true;+ if (await hasGitMarker(cur, scan.gitMarkers)) return true;const parent = path.dirname(cur);if (parent === cur) return false;@@ -218,6 +247,7 @@ async function scanWorktreeFileSize(repoPath: string,gitPath: string,opts: SnapshotFileFilterOptions,+ scan: FilePlanScan,): Promise<SnapshotSkippedFile | null> {const abs = resolveSnapshotGitPath(repoPath, gitPath);if (!abs) {@@ -237,6 +267,10 @@ async function scanWorktreeFileSize(if (stat.size > opts.maxFileBytes) {return { path: gitPath, reason: 'large-file', sizeBytes: stat.size };}+ scan.totalBytes += stat.size;+ if (scan.totalBytes > (opts.maxTotalBytes ?? Infinity)) {+ throw new SnapshotCaptureBudgetExceededError('total-bytes');+ }return null;}@@ -244,7 +278,7 @@ async function classifyEntry(repoPath: string,entry: SnapshotStatusEntry,opts: SnapshotFileFilterOptions,- markers: Map<string, boolean>,+ scan: FilePlanScan,): Promise<SnapshotSkippedFile | null> {if (hasConflictStatus(entry.code)) return { path: entry.path, reason: 'conflict' };@@ -266,12 +300,12 @@ async function classifyEntry(}for (const gitPath of rawPathsFor(entry)) {- if (await isInsideNestedGitRepo(repoPath, gitPath, markers)) {+ if (await isInsideNestedGitRepo(repoPath, gitPath, scan, opts)) {return { path: entry.path, reason: 'nested-git-repo' };}}- return scanWorktreeFileSize(repoPath, entry.path, opts);+ return scanWorktreeFileSize(repoPath, entry.path, opts, scan);}/** Builds a safe/unsafe file plan from already parsed status entries. */@@ -281,18 +315,23 @@ export async function buildSnapshotFilePlanFromEntries(opts?: Partial<SnapshotFileFilterOptions>,): Promise<SnapshotFilePlan> {const options = withDefaults(opts);+ checkSnapshotCaptureDeadline(options.deadlineAtMs);+ if (entries.length > (options.maxFiles ?? Infinity)) {+ throw new SnapshotCaptureBudgetExceededError('file-count');+ }+ // Only this scan owns the cache: a newly created nested repository must be+ // detected on the next snapshot, including after a failed capture.+ const scan: FilePlanScan = { gitMarkers: new Map(), totalBytes: 0 };const includedFiles: SnapshotIncludedFile[] = [];const skippedFiles: SnapshotSkippedFile[] = [];- // Siblings share ancestor probes, but later scans must see new/removed .git markers.- // File stats and content safety checks remain live and are never cached here.- const markers = new Map<string, boolean>();let scanIndex = 0;for (const entry of entries) {await operationYield(scanIndex++);+ checkSnapshotCaptureDeadline(options.deadlineAtMs);const pathsForPathspec = pathspecPathsFor(entry);const oldPath = entry.oldPath && entry.code[0] === 'R' ? { oldPath: entry.oldPath } : {};- const skip = await classifyEntry(repoPath, entry, options, markers);+ const skip = await classifyEntry(repoPath, entry, options, scan);if (skip) {skippedFiles.push({ ...skip, ...oldPath, pathsForPathspec });continue;@@ -301,6 +340,7 @@ export async function buildSnapshotFilePlanFromEntries(includedFiles.push({ path: entry.path, ...oldPath, pathsForPathspec });}+ checkSnapshotCaptureDeadline(options.deadlineAtMs);return { includedFiles, skippedFiles };}@@ -309,7 +349,8 @@ export async function buildSnapshotFilePlan(repoPath: string,opts?: Partial<SnapshotFileFilterOptions>,): Promise<SnapshotFilePlan> {- const { entries, truncated } = await readStatusEntries(repoPath);+ checkSnapshotCaptureDeadline(opts?.deadlineAtMs);+ const { entries, truncated } = await readStatusEntries(repoPath, opts?.deadlineAtMs);const plan = await buildSnapshotFilePlanFromEntries(repoPath, entries, opts);return truncated ? { ...plan, statusTruncated: true } : plan;}