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): carry capture deadline through label diffs
zcinta05148d95a6da3cbf
diff --git a/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.ts b/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.tsindex f2b1e2f3cf..5a1c3024b3 100644--- a/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.ts+++ b/apps/desktop/src/main/__tests__/gitSnapshotShadow.git-integration.test.ts@@ -34,6 +34,7 @@ import {} from '../git-snapshot/savepointRefs';import { parseSnapshotCommit } from '../git-snapshot/snapshotTrailers';import { gitExec } from '../worktree/gitExec';+import * as gitExecModule from '../worktree/gitExec';const REAL_GIT_TEST_TIMEOUT_MS = process.platform === 'win32' ? 60_000 : 20_000;const SESSION = 'shadow-sess-1';@@ -244,6 +245,53 @@ describe('createShadowSavepoint', () => {expect(await readSavepointTip(repoPath, SESSION)).toBeNull();expect(await captureUserVisibleState()).toEqual(before);}, REAL_GIT_TEST_TIMEOUT_MS);+ // #6908 P2: 81 paths require a second label-diff chunk. Expiry after the+ // first real diff must stop that chunk and must not publish a savepoint or+ // call the model-backed labeler with a partial diff (git-integration tier).+ it('shares the capture deadline with label diffs and waits for both readers before cleanup', async () => {+ await commitSeed();+ await Promise.all(Array.from({ length: 81 }, (_, i) => writeRepoFile(+ 'changed-' + i + '.txt', 'changed\n',+ )));+ const before = await captureUserVisibleState();+ const actualGitExec = gitExecModule.gitExec;+ const actualNow = performance.now.bind(performance);+ const clock = vi.spyOn(performance, 'now');+ let diffCalls = 0;+ let activeDiffReaders = 0;+ const deadlines: Array<number | undefined> = [];+ const execSpy = vi.spyOn(gitExecModule, 'gitExec').mockImplementation(async (args, cwd, opts) => {+ if (!args.includes('diff')) return actualGitExec(args, cwd, opts);+ diffCalls += 1;+ deadlines.push(opts?.deadlineAtMs);+ activeDiffReaders += 1;+ try {+ return await actualGitExec(args, cwd, opts);+ } finally {+ activeDiffReaders -= 1;+ clock.mockReturnValue(actualNow() + 60_000);+ }+ });+ const label = vi.fn(() => 'must not label an expired capture');+ try {+ await expect(createShadowSavepoint(repoPath, {+ sessionId: SESSION, label, meta: { kind: 'after-edit' },+ captureBudget: { maxFiles: 100, maxTotalBytes: 1024, timeoutMs: 5_000 },+ })).rejects.toMatchObject({ name: 'SnapshotCaptureBudgetExceededError', reason: 'deadline' });+ expect(activeDiffReaders).toBe(0);+ expect(diffCalls).toBeGreaterThan(0);+ expect(diffCalls).toBeLessThanOrEqual(2);+ expect(deadlines.every((deadline) => typeof deadline === 'number')).toBe(true);+ expect(new Set(deadlines).size).toBe(1);+ expect(label).not.toHaveBeenCalled();+ } finally {+ execSpy.mockRestore();+ clock.mockRestore();+ }+ expect(await readSavepointTip(repoPath, SESSION)).toBeNull();+ expect(await captureUserVisibleState()).toEqual(before);+ });+// #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.diff --git a/apps/desktop/src/main/git-snapshot/gitSnapshotService.ts b/apps/desktop/src/main/git-snapshot/gitSnapshotService.tsindex fc78f766ac..39c50420af 100644--- a/apps/desktop/src/main/git-snapshot/gitSnapshotService.ts+++ b/apps/desktop/src/main/git-snapshot/gitSnapshotService.ts@@ -572,7 +572,7 @@ export async function createShadowSavepoint(// Diff against the turn baseline, not HEAD: the worktree stays// dirty across turns now, so a HEAD-based diff would describe the// cumulative session delta instead of this turn's changes.- await collectStagedDiff(repoPath, commitPathspecs, extraEnv, input.skipIfTreeEquals),+ await collectStagedDiff(repoPath, commitPathspecs, extraEnv, input.skipIfTreeEquals, deadlineAtMs),)): input.label;const [branch, baseHead, parent] = await Promise.all([@@ -900,12 +900,21 @@ async function collectStagedDiff(pathspecs: readonly string[],extraEnv: Record<string, string>,diffBase?: string,+ deadlineAtMs?: number,): Promise<StagedDiff> {const baseArgs = diffBase ? ['diff', '--cached', diffBase] : ['diff', '--cached'];- const [diffStat, rawDiffText] = await Promise.all([- safeGitStdoutForPathspecs([...baseArgs, '--stat'], repoPath, pathspecs, extraEnv),- safeGitStdoutForPathspecs([...baseArgs], repoPath, pathspecs, extraEnv),+ // Both readers share the temporary index. A deadline failure in one must+ // not let withTemporaryIndex remove it while the other still runs Git.+ const reads = await Promise.allSettled([+ safeGitStdoutForPathspecs([...baseArgs, '--stat'], repoPath, pathspecs, extraEnv, deadlineAtMs),+ safeGitStdoutForPathspecs([...baseArgs], repoPath, pathspecs, extraEnv, deadlineAtMs),]);+ checkSnapshotCaptureDeadline(deadlineAtMs);+ const [stat, patch] = reads;+ if (stat.status === 'rejected') throw stat.reason;+ if (patch.status === 'rejected') throw patch.reason;+ const diffStat = stat.value;+ const rawDiffText = patch.value;const diffText =rawDiffText.length > STAGED_DIFF_TEXT_MAX_BYTES? `${rawDiffText.slice(0, STAGED_DIFF_TEXT_MAX_BYTES)}\n...[diff truncated]`@@ -917,11 +926,16 @@ async function safeGitStdout(args: readonly string[],repoPath: string,extraEnv: Record<string, string>,+ deadlineAtMs?: number,): Promise<string> {+ checkSnapshotCaptureDeadline(deadlineAtMs);try {- const { stdout } = await isolatedGitExec(args, repoPath, { extraEnv });+ const { stdout } = await isolatedGitExec(args, repoPath, { extraEnv, deadlineAtMs });return stdout;} catch (err) {+ // Best-effort label reads may degrade on ordinary Git errors, but must+ // not swallow expiry and start another pathspec chunk without a budget.+ checkSnapshotCaptureDeadline(deadlineAtMs);log.debug('[createSnapshot] git read failed, degrade to empty', {args: args.join(' '),error: err instanceof Error ? err.message : String(err),@@ -935,10 +949,11 @@ async function safeGitStdoutForPathspecs(repoPath: string,pathspecs: readonly string[],extraEnv: Record<string, string>,+ deadlineAtMs?: number,): Promise<string> {const parts: string[] = [];for (const chunk of chunkPathspecArgs(pathspecs)) {- const stdout = await safeGitStdout([...baseArgs, '--', ...chunk], repoPath, extraEnv);+ const stdout = await safeGitStdout([...baseArgs, '--', ...chunk], repoPath, extraEnv, deadlineAtMs);if (stdout) parts.push(stdout);}return parts.join('\n');