@@ -7,7 +7,7 @@ import {
77 loadSnapshot,
88 saveSnapshot,
99 } from "../session/db/index.ts";
10-import { restoreSnapshot, takeSnapshot } from "./snapshot.ts";
10+import { restoreSnapshot, snapshotBeforeEdit } from "./snapshot.ts";
1111
1212 // ─── Helpers ──────────────────────────────────────────────────────────────────
1313
@@ -82,121 +82,86 @@ describe("saveSnapshot / loadSnapshot / deleteSnapshot", () => {
8282 });
8383 });
8484
85-// ─── takeSnapshot ─────────────────────────────────────────────────────────────
85+// ─── snapshotBeforeEdit ───────────────────────────────────────────────────────
8686
87-describe("takeSnapshot", () => {
87+describe("snapshotBeforeEdit", () => {
8888 const state = useTestRepo();
8989
90- test("returns false on a clean working tree", async () => {
91- const result = await takeSnapshot(state.repoDir, state.sessionId, 1);
92- expect(result).toBe(false);
93- });
94-
95- test("returns false for a non-git directory", async () => {
96- const nonGit = join(tmpdir(), `mc-nongit-${Date.now()}`);
97- mkdirSync(nonGit, { recursive: true });
98- try {
99- const result = await takeSnapshot(nonGit, state.sessionId, 2);
100- expect(result).toBe(false);
101- } finally {
102- rmSync(nonGit, { recursive: true, force: true });
103- }
104- });
105-
106- test("snapshots a modified tracked file", async () => {
90+ test("snapshots a modified tracked file before edit", async () => {
10791 const filePath = join(state.repoDir, "README.md");
108- writeFileSync(filePath, "modified content\n");
109-
110- const result = await takeSnapshot(state.repoDir, state.sessionId, 3);
111- expect(result).toBe(true);
92+ const snappedPaths = new Set<string>();
93+
94+ await snapshotBeforeEdit(
95+ state.repoDir,
96+ state.sessionId,
97+ 3,
98+ filePath,
99+ snappedPaths,
100+ );
112101
113102 const rows = loadSnapshot(state.sessionId, 3);
114103 const readme = rows.find((r) => r.path === "README.md");
115104 expect(readme).toBeDefined();
116105 expect(readme?.existed).toBe(true);
117106 expect(new TextDecoder().decode(readme?.content ?? undefined)).toBe(
118- "modified content\n",
107+ "hello\n",
119108 );
109+ expect(snappedPaths.has("README.md")).toBe(true);
120110
121111 deleteSnapshot(state.sessionId, 3);
122112 });
123113
124114 test("snapshots an untracked new file with existed=false", async () => {
125- writeFileSync(join(state.repoDir, "untracked.txt"), "brand new\n");
126-
127- const result = await takeSnapshot(state.repoDir, state.sessionId, 4);
128- expect(result).toBe(true);
115+ const filePath = join(state.repoDir, "untracked.txt");
116+ const snappedPaths = new Set<string>();
117+
118+ await snapshotBeforeEdit(
119+ state.repoDir,
120+ state.sessionId,
121+ 4,
122+ filePath,
123+ snappedPaths,
124+ );
129125
130126 const rows = loadSnapshot(state.sessionId, 4);
131127 const f = rows.find((r) => r.path === "untracked.txt");
132128 expect(f).toBeDefined();
133- // Untracked files are new — on undo they should be deleted, not written back
129+ // File didn't exist before edit
134130 expect(f?.existed).toBe(false);
131+ expect(f?.content).toBeNull();
135132
136133 deleteSnapshot(state.sessionId, 4);
137134 });
138135
139- test("snapshots files inside a new untracked directory", async () => {
140- // Without -u, git collapses new dirs to a single "?? dir/" entry.
141- // With -u, each file inside is listed individually.
142- mkdirSync(join(state.repoDir, "src"), { recursive: true });
143- writeFileSync(join(state.repoDir, "src", "a.ts"), "export const a = 1;\n");
144- writeFileSync(join(state.repoDir, "src", "b.ts"), "export const b = 2;\n");
145-
146- const result = await takeSnapshot(state.repoDir, state.sessionId, 5);
147- expect(result).toBe(true);
148-
149- const rows = loadSnapshot(state.sessionId, 5);
150- const a = rows.find((r) => r.path === "src/a.ts");
151- const b = rows.find((r) => r.path === "src/b.ts");
152- expect(a).toBeDefined();
153- expect(b).toBeDefined();
154- expect(a?.existed).toBe(false);
155- expect(b?.existed).toBe(false);
156-
157- deleteSnapshot(state.sessionId, 5);
158- });
159-
160- test("snapshots a renamed file correctly", async () => {
161- // Commit a file so it's tracked, then rename it
162- writeFileSync(join(state.repoDir, "old-name.ts"), "export const x = 1;\n");
163- spawnSync(["git", "add", "old-name.ts"], state.repoDir);
164- spawnSync(["git", "commit", "-m", "add file"], state.repoDir);
165-
166- // Rename tracked file
167- spawnSync(["git", "mv", "old-name.ts", "new-name.ts"], state.repoDir);
168-
169- const result = await takeSnapshot(state.repoDir, state.sessionId, 6);
170- expect(result).toBe(true);
171-
172- const rows = loadSnapshot(state.sessionId, 6);
173- // Old path: was deleted — should have existed=true with content
174- const oldEntry = rows.find((r) => r.path === "old-name.ts");
175- expect(oldEntry).toBeDefined();
176- expect(oldEntry?.existed).toBe(true);
177- expect(oldEntry?.content).not.toBeNull();
178-
179- // New path: brand new on disk — should have existed=false
180- const newEntry = rows.find((r) => r.path === "new-name.ts");
181- expect(newEntry).toBeDefined();
182- expect(newEntry?.existed).toBe(false);
183-
184- deleteSnapshot(state.sessionId, 6);
185- });
136+ test("does not snapshot the same file twice in one turn", async () => {
137+ const filePath = join(state.repoDir, "README.md");
138+ const snappedPaths = new Set<string>();
139+
140+ await snapshotBeforeEdit(
141+ state.repoDir,
142+ state.sessionId,
143+ 5,
144+ filePath,
145+ snappedPaths,
146+ );
186147
187- test("does not affect git status after snapshotting", async () => {
188- writeFileSync(join(state.repoDir, "README.md"), "changed\n");
148+ // Change the file
149+ writeFileSync(filePath, "changed\n");
189150
190- await takeSnapshot(state.repoDir, state.sessionId, 5);
151+ // Second call should be a no-op
152+ await snapshotBeforeEdit(
153+ state.repoDir,
154+ state.sessionId,
155+ 5,
156+ filePath,
157+ snappedPaths,
158+ );
191159
192- // git status should still show the modification — nothing was stashed
193- const proc = Bun.spawnSync(["git", "status", "--porcelain"], {
194- cwd: state.repoDir,
195- stdout: "pipe",
196- stderr: "pipe",
197- });
198- const statusOut = proc.stdout.toString();
199- expect(statusOut).toContain("README.md");
160+ const rows = loadSnapshot(state.sessionId, 5);
161+ const readme = rows.find((r) => r.path === "README.md");
162+ expect(new TextDecoder().decode(readme?.content ?? undefined)).toBe(
163+ "hello\n",
164+ );
200165
201166 deleteSnapshot(state.sessionId, 5);
202167 });
@@ -216,7 +181,6 @@ describe("restoreSnapshot", () => {
216181 test("restores a modified file to its pre-turn content", async () => {
217182 const filePath = join(state.repoDir, "README.md");
218183 const originalContent = "hello\n";
219- // File was already "hello\n" from makeRepo; now simulate a pre-turn snapshot
220184 saveSnapshot(state.sessionId, 10, [
221185 {
222186 path: "README.md",
@@ -225,17 +189,14 @@ describe("restoreSnapshot", () => {
225189 },
226190 ]);
227191
228- // Agent modifies the file
229192 writeFileSync(filePath, "agent changed this\n");
230193
231- // Undo: restore
232194 const result = await restoreSnapshot(state.repoDir, state.sessionId, 10);
233195 expect(result.restored).toBe(true);
234196
235197 const restoredContent = await Bun.file(filePath).text();
236198 expect(restoredContent).toBe(originalContent);
237199
238- // Snapshot should be cleaned up
239200 expect(loadSnapshot(state.sessionId, 10)).toHaveLength(0);
240201 });
241202
@@ -243,7 +204,6 @@ describe("restoreSnapshot", () => {
243204 const newFile = join(state.repoDir, "agent-created.ts");
244205 writeFileSync(newFile, "export const x = 1;\n");
245206
246- // Snapshot says this file did NOT exist before the turn
247207 saveSnapshot(state.sessionId, 11, [
248208 { path: "agent-created.ts", content: null, existed: false },
249209 ]);
@@ -257,24 +217,20 @@ describe("restoreSnapshot", () => {
257217 expect(loadSnapshot(state.sessionId, 11)).toHaveLength(0);
258218 });
259219
260- test("end-to-end: takeSnapshot then agent creates file, restoreSnapshot deletes it", async () => {
261- // Working tree is clean before the turn — nothing to snapshot
262- const snapped = await takeSnapshot(state.repoDir, state.sessionId, 12);
263- expect(snapped).toBe(false);
264-
265- // Agent creates a new file mid-turn
220+ test("end-to-end: snapshotBeforeEdit then agent creates file, restoreSnapshot deletes it", async () => {
266221 const agentFile = join(state.repoDir, "generated.ts");
267- writeFileSync(agentFile, "export const y = 2;\n");
268-
269- // Now snapshot captures the untracked file with existed=false
270- const snapped2 = await takeSnapshot(state.repoDir, state.sessionId, 13);
271- expect(snapped2).toBe(true);
222+ const snappedPaths = new Set<string>();
223+
224+ await snapshotBeforeEdit(
225+ state.repoDir,
226+ state.sessionId,
227+ 13,
228+ agentFile,
229+ snappedPaths,
230+ );
272231
273- const rows = loadSnapshot(state.sessionId, 13);
274- const entry = rows.find((r) => r.path === "generated.ts");
275- expect(entry?.existed).toBe(false);
232+ writeFileSync(agentFile, "export const y = 2;\n");
276233
277- // Restore: should delete the agent-created file
278234 const result = await restoreSnapshot(state.repoDir, state.sessionId, 13);
279235 expect(result.restored).toBe(true);
280236 expect(await Bun.file(agentFile).exists()).toBe(false);