Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(cindy-make): 保留快照索引时间以识别快速改写 (#6136)

FicoWang8e9153ab9872
diff --git a/apps/desktop/src/main/cindy-make/__tests__/keepChanges.git-integration.test.ts b/apps/desktop/src/main/cindy-make/__tests__/keepChanges.git-integration.test.ts
index fcba2b3f8b..f8fb8f8847 100644
--- a/apps/desktop/src/main/cindy-make/__tests__/keepChanges.git-integration.test.ts
+++ b/apps/desktop/src/main/cindy-make/__tests__/keepChanges.git-integration.test.ts
@@ -422,8 +422,11 @@ it.each(['lines', 'hunks'] as const)('rejects reordered added %s while accepting
await expect(applyUpstreamMerge(h.userData, state, h.git)).rejects.toMatchObject({
code: 'checksFailed', missing: { count: 1, commits: [reordered] },
});
+ expect(await h.git(['rev-parse', 'HEAD'], h.source)).toBe(reordered);
+ expect(await readFile(path.join(h.source, 'feature.txt'), 'utf8')).toBe(text(original));
await h.write(worktree, 'feature.txt', original);
await expect(applyUpstreamMerge(h.userData, state, h.git)).resolves.toMatchObject({ status: 'merged' });
+ expect(await readFile(path.join(h.source, 'feature.txt'), 'utf8')).toBe(text(original));
} finally {
await h.clean();
}
diff --git a/apps/desktop/src/main/cindy-make/__tests__/sourceContent.git-integration.test.ts b/apps/desktop/src/main/cindy-make/__tests__/sourceContent.git-integration.test.ts
new file mode 100644
index 0000000000..b15e595b6c
--- /dev/null
+++ b/apps/desktop/src/main/cindy-make/__tests__/sourceContent.git-integration.test.ts
@@ -0,0 +1,83 @@
+import { copyFile, mkdtemp, readFile, rm, stat, utimes, writeFile } from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import { afterEach, expect, it, vi } from 'vitest';
+import { snapshotContent, type ContentGit } from '../sourceContent';
+import { runSourceGit } from '../sourceGit';
+
+vi.mock('node:fs/promises', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('node:fs/promises')>();
+ return { ...actual, copyFile: vi.fn(actual.copyFile) };
+});
+
+afterEach(() => {
+ vi.mocked(copyFile).mockReset();
+ vi.restoreAllMocks();
+});
+
+// The second fixture also catches Stats.mtime rounding up into the next second.
+// The third models clock rollback: copied index < cached file < original index.
+// The remaining fixtures model Windows CopyFileW preserving the index mtime,
+// with a clock at the end of a second, crossing forward, or rolling backward.
+const futureTick = Math.floor(Date.now() / 1000) + 3600;
+it.each<[number, number, { before: number; after: number }?]>([
+ [1_700_000_000, 1_700_000_000],
+ [1_700_000_000.9999, 1_700_000_000.9999],
+ [futureTick, futureTick + 3600],
+ [1_700_000_000.9999, 1_700_003_600, { before: 1_700_000_000_999, after: 1_700_000_000_999 }],
+ [1_700_000_000.9999, 1_700_003_600, { before: 1_700_000_000_999, after: 1_700_000_001_000 }],
+ [1_700_000_000.9999, 1_700_003_600, { before: 1_700_000_001_000, after: 1_700_000_000_999 }],
+])('snapshots a racily clean edit without changing the real index (%s, %s, %j)', async (tick, indexTick, clock) => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'cindy-content-racy-'));
+ const env = {
+ PATH: process.env.PATH,
+ SystemRoot: process.env.SystemRoot,
+ GIT_CONFIG_GLOBAL: path.join(root, 'empty.gitconfig'),
+ GIT_CONFIG_NOSYSTEM: '1',
+ };
+ const git: ContentGit = (args, cwd, indexFile) => runSourceGit(
+ { ...env, ...(indexFile ? { GIT_INDEX_FILE: indexFile } : {}) },
+ args, cwd, AbortSignal.timeout(30_000),
+ );
+ try {
+ await writeFile(env.GIT_CONFIG_GLOBAL, '');
+ await git(['init', '--initial-branch=main'], root);
+ // Model a same-tick, equal-size rewrite without depending on runner speed
+ // or whether this Git build compares subsecond timestamps.
+ await git(['config', 'core.trustctime', 'false'], root);
+ await git(['config', 'core.checkStat', 'minimal'], root);
+ const file = path.join(root, 'feature.txt');
+ await writeFile(file, 'release()\nacquire()\n');
+ await utimes(file, tick, tick);
+ await git(['add', 'feature.txt'], root);
+ const index = (await git(['rev-parse', '--path-format=absolute', '--git-path', 'index'], root)).trim();
+ await utimes(index, indexTick, indexTick);
+ const before = await readFile(index);
+ const beforeStat = await stat(index);
+ const staged = await git(['rev-parse', ':feature.txt'], root);
+ await writeFile(file, 'acquire()\nrelease()\n');
+ await utimes(file, tick, tick);
+ const restored = await git(['hash-object', 'feature.txt'], root);
+ expect(restored).not.toBe(staged);
+
+ if (clock) {
+ const actual = await vi.importActual<typeof import('node:fs/promises')>('node:fs/promises');
+ let now = clock.before;
+ vi.spyOn(Date, 'now').mockImplementation(() => now);
+ vi.mocked(copyFile).mockImplementationOnce(async (...args) => {
+ await actual.copyFile(...args);
+ await utimes(args[1], indexTick, indexTick);
+ expect((await stat(args[1])).mtimeMs).toBe(beforeStat.mtimeMs);
+ now = clock.after;
+ });
+ }
+ const tree = await snapshotContent(git, root);
+ expect(await git(['rev-parse', `${tree}:feature.txt`], root)).toBe(restored);
+ expect(await readFile(file, 'utf8')).toBe('acquire()\nrelease()\n');
+ expect(await readFile(index)).toEqual(before);
+ expect((await stat(index)).mtimeMs).toBe(beforeStat.mtimeMs);
+ expect(await git(['rev-parse', ':feature.txt'], root)).toBe(staged);
+ } finally {
+ await rm(root, { recursive: true, force: true, maxRetries: 3, retryDelay: 50 });
+ }
+}, 60_000);
diff --git a/apps/desktop/src/main/cindy-make/sourceContent.ts b/apps/desktop/src/main/cindy-make/sourceContent.ts
index 271ae15cc9..42e9205ad3 100644
--- a/apps/desktop/src/main/cindy-make/sourceContent.ts
+++ b/apps/desktop/src/main/cindy-make/sourceContent.ts
@@ -1,4 +1,4 @@
-import { copyFile, mkdtemp, rm, stat } from 'node:fs/promises';
+import { copyFile, mkdtemp, rm, stat, utimes } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
@@ -43,7 +43,19 @@ export async function snapshotContent(git: ContentGit, cwd: string, ref?: string
const original = (
await git(['rev-parse', '--path-format=absolute', '--git-path', 'index'], cwd)
).trim();
+ // A later index mtime can hide equal-size edits from Git's racy-clean check.
+ // Read the original before copying so a concurrent replacement cannot lend
+ // old bytes a newer timestamp. Windows copyFile preserves mtime, so also cap
+ // it with clock samples before/after copying. Taking both handles crossing
+ // a second boundary or clock rollback during the copy. Round down to seconds
+ // since Stats.mtime can round up across a second boundary, hiding the edit.
+ const copyStartedAt = Math.floor(Date.now() / 1000);
+ const originalStat = await stat(original, { bigint: true });
await copyFile(original, index);
+ const copiedStat = await stat(index, { bigint: true });
+ const mtimeNs = originalStat.mtimeNs < copiedStat.mtimeNs ? originalStat.mtimeNs : copiedStat.mtimeNs;
+ const mtime = Math.min(Number(mtimeNs / 1_000_000_000n), copyStartedAt, Math.floor(Date.now() / 1000));
+ await utimes(index, originalStat.atime, mtime);
await git(['add', '--all', '--', '.'], cwd, index);
const tree = (await git(['write-tree'], cwd, index)).trim();
if (!HASH.test(tree)) throw new Error('Invalid content snapshot');