@nightloom/isy 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +7 -5
  2. package/dist/agents/codex-records.js +10 -1
  3. package/dist/agents/codex-records.js.map +1 -1
  4. package/dist/commands/commit.js +21 -1
  5. package/dist/commands/commit.js.map +1 -1
  6. package/dist/commands/upload.d.ts +1 -0
  7. package/dist/commands/upload.js +13 -5
  8. package/dist/commands/upload.js.map +1 -1
  9. package/dist/index.js +8 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/parser.js +1 -0
  12. package/dist/parser.js.map +1 -1
  13. package/dist/redact.js +12 -5
  14. package/dist/redact.js.map +1 -1
  15. package/dist/subagents.js +7 -5
  16. package/dist/subagents.js.map +1 -1
  17. package/package.json +3 -2
  18. package/src/agents/codex-records.ts +10 -1
  19. package/src/commands/commit.ts +22 -1
  20. package/src/commands/upload.ts +13 -6
  21. package/src/index.ts +8 -2
  22. package/src/parser.ts +1 -0
  23. package/src/redact.ts +13 -5
  24. package/src/subagents.ts +7 -5
  25. package/dist/test/analyze.test.d.ts +0 -1
  26. package/dist/test/analyze.test.js +0 -412
  27. package/dist/test/analyze.test.js.map +0 -1
  28. package/dist/test/check.test.d.ts +0 -1
  29. package/dist/test/check.test.js +0 -215
  30. package/dist/test/check.test.js.map +0 -1
  31. package/dist/test/cli.test.d.ts +0 -1
  32. package/dist/test/cli.test.js +0 -142
  33. package/dist/test/cli.test.js.map +0 -1
  34. package/dist/test/codex.test.d.ts +0 -1
  35. package/dist/test/codex.test.js +0 -666
  36. package/dist/test/codex.test.js.map +0 -1
  37. package/dist/test/commit.test.d.ts +0 -1
  38. package/dist/test/commit.test.js +0 -1898
  39. package/dist/test/commit.test.js.map +0 -1
  40. package/dist/test/e2e-harness.test.d.ts +0 -1
  41. package/dist/test/e2e-harness.test.js +0 -53
  42. package/dist/test/e2e-harness.test.js.map +0 -1
  43. package/dist/test/git.test.d.ts +0 -1
  44. package/dist/test/git.test.js +0 -145
  45. package/dist/test/git.test.js.map +0 -1
  46. package/dist/test/health.test.d.ts +0 -1
  47. package/dist/test/health.test.js +0 -156
  48. package/dist/test/health.test.js.map +0 -1
  49. package/dist/test/hook.test.d.ts +0 -1
  50. package/dist/test/hook.test.js +0 -343
  51. package/dist/test/hook.test.js.map +0 -1
  52. package/dist/test/init.test.d.ts +0 -1
  53. package/dist/test/init.test.js +0 -162
  54. package/dist/test/init.test.js.map +0 -1
  55. package/dist/test/kimi-cli.test.d.ts +0 -1
  56. package/dist/test/kimi-cli.test.js +0 -231
  57. package/dist/test/kimi-cli.test.js.map +0 -1
  58. package/dist/test/kimi-code.test.d.ts +0 -1
  59. package/dist/test/kimi-code.test.js +0 -302
  60. package/dist/test/kimi-code.test.js.map +0 -1
  61. package/dist/test/kimi-config.test.d.ts +0 -1
  62. package/dist/test/kimi-config.test.js +0 -246
  63. package/dist/test/kimi-config.test.js.map +0 -1
  64. package/dist/test/kimi-hook.test.d.ts +0 -1
  65. package/dist/test/kimi-hook.test.js +0 -163
  66. package/dist/test/kimi-hook.test.js.map +0 -1
  67. package/dist/test/kimi-listing.test.d.ts +0 -1
  68. package/dist/test/kimi-listing.test.js +0 -233
  69. package/dist/test/kimi-listing.test.js.map +0 -1
  70. package/dist/test/kimi.test.d.ts +0 -1
  71. package/dist/test/kimi.test.js +0 -418
  72. package/dist/test/kimi.test.js.map +0 -1
  73. package/dist/test/mask-paths.test.d.ts +0 -1
  74. package/dist/test/mask-paths.test.js +0 -275
  75. package/dist/test/mask-paths.test.js.map +0 -1
  76. package/dist/test/notes.test.d.ts +0 -1
  77. package/dist/test/notes.test.js +0 -41
  78. package/dist/test/notes.test.js.map +0 -1
  79. package/dist/test/pairing.test.d.ts +0 -1
  80. package/dist/test/pairing.test.js +0 -138
  81. package/dist/test/pairing.test.js.map +0 -1
  82. package/dist/test/parser.test.d.ts +0 -1
  83. package/dist/test/parser.test.js +0 -1041
  84. package/dist/test/parser.test.js.map +0 -1
  85. package/dist/test/plan.test.d.ts +0 -1
  86. package/dist/test/plan.test.js +0 -35
  87. package/dist/test/plan.test.js.map +0 -1
  88. package/dist/test/redact.test.d.ts +0 -1
  89. package/dist/test/redact.test.js +0 -3053
  90. package/dist/test/redact.test.js.map +0 -1
  91. package/dist/test/settings.test.d.ts +0 -1
  92. package/dist/test/settings.test.js +0 -32
  93. package/dist/test/settings.test.js.map +0 -1
  94. package/dist/test/stage0.test.d.ts +0 -1
  95. package/dist/test/stage0.test.js +0 -1304
  96. package/dist/test/stage0.test.js.map +0 -1
  97. package/dist/test/subagents.test.d.ts +0 -1
  98. package/dist/test/subagents.test.js +0 -290
  99. package/dist/test/subagents.test.js.map +0 -1
  100. package/dist/test/sweep.test.d.ts +0 -1
  101. package/dist/test/sweep.test.js +0 -434
  102. package/dist/test/sweep.test.js.map +0 -1
  103. package/dist/test/update.test.d.ts +0 -1
  104. package/dist/test/update.test.js +0 -26
  105. package/dist/test/update.test.js.map +0 -1
  106. package/dist/test/upload.test.d.ts +0 -1
  107. package/dist/test/upload.test.js +0 -393
  108. package/dist/test/upload.test.js.map +0 -1
@@ -1,1898 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { execFile, spawn } from "node:child_process";
3
- import { createServer } from "node:http";
4
- import { appendFile, chmod, lstat, mkdir, mkdtemp, readFile, realpath, rename, rm, stat, symlink, utimes, writeFile } from "node:fs/promises";
5
- import { tmpdir } from "node:os";
6
- import { basename, dirname, join } from "node:path";
7
- import { after, before, beforeEach, test } from "node:test";
8
- import { promisify } from "node:util";
9
- import { gunzipSync } from "node:zlib";
10
- import { claudeAgent } from "../agents/index.js";
11
- import { writeConfig } from "../config.js";
12
- import { formatCommit, runCommit, wordsOf } from "../commands/commit.js";
13
- import { collectGitMetadata, currentBranch } from "../git.js";
14
- import { readJournal } from "../log.js";
15
- import { formatUploadAlert, runUpload } from "../commands/upload.js";
16
- import { COMMIT_COMMAND, PUSH_COMMAND, autoInstallGitHook, gitHookInstalled, installGitHook, hooksLeftToTheUser, postCommitPath, prePushPath, removeGitHook, } from "../githook.js";
17
- import { projectSlug, sessionStatePath } from "../paths.js";
18
- import { candidateKey, looksUnchanged, readState, unseen, withCommit } from "../state.js";
19
- const run = promisify(execFile);
20
- let home;
21
- let claudeConfig;
22
- let server;
23
- let baseUrl;
24
- const uploads = [];
25
- const saved = {
26
- home: process.env.ISY_HOME,
27
- claude: process.env.CLAUDE_CONFIG_DIR,
28
- kimi: process.env.KIMI_HOME,
29
- codex: process.env.CODEX_HOME,
30
- };
31
- async function newRepo(prefix) {
32
- const dir = await mkdtemp(join(tmpdir(), prefix));
33
- await run("git", ["init", "-q", "-b", "main"], { cwd: dir });
34
- await run("git", ["config", "user.email", "t@e.com"], { cwd: dir });
35
- await run("git", ["config", "user.name", "T"], { cwd: dir });
36
- await writeFile(join(dir, "a.txt"), "one");
37
- await run("git", ["add", "."], { cwd: dir });
38
- await run("git", ["commit", "-q", "-m", "first"], { cwd: dir });
39
- await run("git", ["remote", "add", "origin", "git@github.com:unwinned/ISY.git"], { cwd: dir });
40
- return dir;
41
- }
42
- function record(entry) {
43
- return JSON.stringify(entry);
44
- }
45
- /**
46
- * Long enough to clear the stage 0 eligibility floor, with one manifest edit at
47
- * the end so `external_dependency` has something real to find.
48
- */
49
- function transcript(sessionId, cwd, dependency) {
50
- const lines = [
51
- record({
52
- type: "user",
53
- uuid: `${sessionId}-u`,
54
- parentUuid: null,
55
- sessionId,
56
- cwd,
57
- version: "2.0.0",
58
- timestamp: "2026-08-16T09:00:00Z",
59
- message: { role: "user", content: "go" },
60
- }),
61
- ];
62
- for (let index = 0; index < 21; index += 1) {
63
- lines.push(record({
64
- type: "assistant",
65
- uuid: `${sessionId}-a${index}`,
66
- parentUuid: index === 0 ? `${sessionId}-u` : `${sessionId}-a${index - 1}`,
67
- sessionId,
68
- timestamp: "2026-08-16T09:30:00Z",
69
- message: {
70
- role: "assistant",
71
- content: [
72
- {
73
- type: "tool_use",
74
- id: `${sessionId}-t${index}`,
75
- name: "Edit",
76
- input: {
77
- file_path: join(cwd, `src/f${index}.ts`),
78
- old_string: "one",
79
- new_string: "two",
80
- },
81
- },
82
- ],
83
- },
84
- }));
85
- }
86
- lines.push(record({
87
- type: "assistant",
88
- uuid: `${sessionId}-manifest`,
89
- parentUuid: `${sessionId}-a20`,
90
- sessionId,
91
- timestamp: "2026-08-16T10:00:00Z",
92
- message: {
93
- role: "assistant",
94
- content: [
95
- {
96
- type: "tool_use",
97
- id: `${sessionId}-tm`,
98
- name: "Edit",
99
- input: {
100
- file_path: join(cwd, "package.json"),
101
- old_string: '{\n "dependencies": {}\n}',
102
- new_string: `{\n "dependencies": {\n "${dependency}": "^1.0.0"\n }\n}`,
103
- },
104
- },
105
- ],
106
- },
107
- }));
108
- return lines.join("\n");
109
- }
110
- async function writeSession(cwd, sessionId, dependency) {
111
- const dir = join(claudeConfig, "projects", projectSlug(cwd));
112
- await mkdir(dir, { recursive: true });
113
- const path = join(dir, `${sessionId}.jsonl`);
114
- await writeFile(path, transcript(sessionId, cwd, dependency));
115
- return path;
116
- }
117
- before(async () => {
118
- home = await mkdtemp(join(tmpdir(), "isy-commit-home-"));
119
- claudeConfig = await mkdtemp(join(tmpdir(), "isy-commit-claude-"));
120
- process.env.ISY_HOME = home;
121
- process.env.CLAUDE_CONFIG_DIR = claudeConfig;
122
- // Pinned at directories that do not exist: these assertions are about Claude
123
- // Code, and a machine with Kimi or Codex installed must not change them.
124
- process.env.KIMI_HOME = join(home, "absent-kimi");
125
- process.env.CODEX_HOME = join(home, "absent-codex");
126
- server = createServer((req, res) => {
127
- const chunks = [];
128
- req.on("data", (chunk) => chunks.push(chunk));
129
- req.on("end", () => {
130
- const raw = Buffer.concat(chunks);
131
- if (req.url?.endsWith("/sessions")) {
132
- const body = JSON.parse(gunzipSync(raw).toString("utf8"));
133
- uploads.push(body);
134
- }
135
- res.writeHead(200, { "content-type": "application/json" });
136
- res.end(JSON.stringify({ id: "ses_1", deduplicated: false }));
137
- });
138
- });
139
- await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
140
- const address = server.address();
141
- baseUrl = typeof address === "object" && address ? `http://127.0.0.1:${address.port}` : "";
142
- });
143
- after(async () => {
144
- await new Promise((resolve) => server.close(() => resolve()));
145
- for (const [key, value] of Object.entries({
146
- ISY_HOME: saved.home,
147
- CLAUDE_CONFIG_DIR: saved.claude,
148
- KIMI_HOME: saved.kimi,
149
- CODEX_HOME: saved.codex,
150
- })) {
151
- if (value === undefined)
152
- delete process.env[key];
153
- else
154
- process.env[key] = value;
155
- }
156
- });
157
- beforeEach(async () => {
158
- uploads.length = 0;
159
- await writeFile(sessionStatePath(), "{}\n").catch(() => undefined);
160
- await writeConfig({ token: "t", apiBaseUrl: baseUrl });
161
- });
162
- test("installs a post-commit hook a fresh repository did not have", async () => {
163
- const fresh = await newRepo("isy-hook-fresh-");
164
- assert.equal(await installGitHook(fresh), "installed");
165
- assert.equal(await gitHookInstalled(fresh), true);
166
- const path = (await postCommitPath(fresh));
167
- const body = await readFile(path, "utf8");
168
- assert.match(body, /^#!\/bin\/sh/);
169
- assert.ok(body.includes(COMMIT_COMMAND));
170
- // Git only runs a hook it can execute, so a file it cannot is not installed.
171
- assert.equal(((await stat(path)).mode & 0o111) !== 0, true);
172
- assert.equal(await installGitHook(fresh), "already-present");
173
- });
174
- test("joins a hook the user already wrote instead of replacing it", async () => {
175
- const fresh = await newRepo("isy-hook-existing-");
176
- const path = (await postCommitPath(fresh));
177
- await writeFile(path, "#!/bin/sh\nset -e\necho mine\n");
178
- assert.equal(await installGitHook(fresh), "installed");
179
- const body = await readFile(path, "utf8");
180
- assert.ok(body.includes("echo mine"), "the user's own script survives");
181
- assert.ok(body.includes(COMMIT_COMMAND));
182
- // `set -e` above would abort the hook on a non-zero isy; `|| true` stops that.
183
- assert.ok(body.includes("|| true"));
184
- // Under the shebang and above their script, so a hook ending in `exec` or
185
- // `exit` still runs ours, and ours is never the command git takes the exit
186
- // code from.
187
- assert.match(body, /^#!\/bin\/sh\n# isy:begin/);
188
- assert.ok(body.indexOf(COMMIT_COMMAND) < body.indexOf("echo mine"));
189
- assert.equal(await removeGitHook(fresh), "removed");
190
- const afterRemoval = await readFile(path, "utf8");
191
- assert.ok(afterRemoval.includes("echo mine"));
192
- assert.ok(!afterRemoval.includes("isy"));
193
- });
194
- test("a pre-push that rejects still rejects with our block in the file", async () => {
195
- const fresh = await newRepo("isy-hook-verdict-");
196
- const path = (await prePushPath(fresh));
197
- // Someone else's gate, ending the way most of them do: no explicit `exit`, so
198
- // the hook's verdict is the exit code of its last command — which is exactly
199
- // what a block appended below would take over, `&` always succeeding.
200
- await writeFile(path, "#!/bin/sh\ngrep -q nothing-here /dev/null\n");
201
- assert.equal(await installGitHook(fresh), "installed");
202
- assert.ok((await readFile(path, "utf8")).includes(PUSH_COMMAND));
203
- await assert.rejects(() => run("sh", [path], { cwd: fresh }), "the push the repository's own hook refused is still refused");
204
- });
205
- test("a hook that ends in exec still runs our block", async () => {
206
- const fresh = await newRepo("isy-hook-exec-");
207
- const path = (await postCommitPath(fresh));
208
- const marker = join(fresh, "ours-ran");
209
- await writeFile(path, `#!/bin/sh\nexec echo theirs\n`);
210
- assert.equal(await installGitHook(fresh), "installed");
211
- // Nothing below an `exec` is ever reached, so the proof is that our line runs
212
- // at all: stand a touchable marker in for the upload.
213
- const body = (await readFile(path, "utf8")).replace(COMMIT_COMMAND, `: > "${marker}"`);
214
- await writeFile(path, body);
215
- await run("sh", [path], { cwd: fresh });
216
- await stat(marker);
217
- });
218
- test("replaces a block an older isy wrote rather than stacking a second one", async () => {
219
- const fresh = await newRepo("isy-hook-upgrade-");
220
- const path = (await postCommitPath(fresh));
221
- await writeFile(path, "#!/bin/sh\n# isy:begin — managed by isy, do not edit inside this block\nnpx isy upload --silent\n# isy:end\n");
222
- assert.equal(await installGitHook(fresh), "installed");
223
- const body = await readFile(path, "utf8");
224
- assert.equal(body.match(/isy:begin/g)?.length, 1);
225
- assert.ok(!body.includes("npx isy upload --silent"));
226
- assert.ok(body.includes(COMMIT_COMMAND));
227
- });
228
- const OLD_COMMIT_BLOCK = "# isy:begin — managed by isy, do not edit inside this block\nnpx @nightloom/isy commit --hook || true\n# isy:end\n";
229
- const OLD_PUSH_BLOCK = "# isy:begin — managed by isy, do not edit inside this block\nnpx @nightloom/isy upload --silent --all >/dev/null 2>&1 </dev/null &\n# isy:end\n";
230
- /** This checkout's isy, run the way a hook runs it: nothing on stdin. */
231
- function cli(cwd, args) {
232
- return new Promise((resolve, reject) => {
233
- const child = spawn(process.execPath, ["--import", import.meta.resolve("tsx"), join(import.meta.dirname, "..", "index.ts"), ...args], { cwd, stdio: "ignore" });
234
- child.on("error", reject);
235
- child.on("close", () => resolve());
236
- });
237
- }
238
- test("whatever a hook runs next in a repository brings an older block up to date", async () => {
239
- for (const args of [
240
- ["commit", "--hook"],
241
- ["check", "--hook"],
242
- ["upload", "--hook"],
243
- ["upload", "--silent", "--all"],
244
- ]) {
245
- const label = args.join(" ");
246
- const fresh = await newRepo("isy-hook-stale-");
247
- const commit = (await postCommitPath(fresh));
248
- const push = (await prePushPath(fresh));
249
- await writeFile(commit, `#!/bin/sh\n${OLD_COMMIT_BLOCK}echo mine\n`);
250
- await writeFile(push, `#!/bin/sh\n${OLD_PUSH_BLOCK}echo mine\n`);
251
- await cli(fresh, args);
252
- const begin = OLD_COMMIT_BLOCK.split("\n")[0];
253
- assert.equal(await readFile(commit, "utf8"), `#!/bin/sh\n${begin}\n${COMMIT_COMMAND}\n# isy:end\necho mine\n`, label);
254
- assert.equal(await readFile(push, "utf8"), `#!/bin/sh\n${begin}\n${PUSH_COMMAND}\n# isy:end\necho mine\n`, label);
255
- assert.equal(await gitHookInstalled(fresh), true, label);
256
- }
257
- });
258
- test("bringing a block up to date adds none that is not there, and leaves a shared directory alone", async () => {
259
- const fresh = await newRepo("isy-hook-stale-none-");
260
- await cli(fresh, ["commit", "--hook"]);
261
- await assert.rejects(() => stat(join(fresh, ".git", "hooks", "post-commit")));
262
- const shared = await newRepo("isy-hook-stale-shared-");
263
- await mkdir(join(shared, ".husky"));
264
- await run("git", ["config", "core.hooksPath", ".husky"], { cwd: shared });
265
- // Tracked, so a rewrite nobody asked for leaves the tree dirty and `git rebase` refuses.
266
- const written = `#!/bin/sh\n${OLD_COMMIT_BLOCK}`;
267
- await writeFile(join(shared, ".husky", "post-commit"), written);
268
- await cli(shared, ["commit", "--hook"]);
269
- assert.equal(await readFile(join(shared, ".husky", "post-commit"), "utf8"), written);
270
- // `isy init`, run on purpose, does write there.
271
- await installGitHook(shared);
272
- assert.ok((await readFile(join(shared, ".husky", "post-commit"), "utf8")).includes(COMMIT_COMMAND));
273
- });
274
- test("a hook rewritten while it runs carries on with the rest of the user's script", async () => {
275
- const fresh = await newRepo("isy-hook-running-");
276
- const path = (await postCommitPath(fresh));
277
- const marker = join(fresh, "mine-ran");
278
- // bash goes back to the file for every line, at the offset it had reached.
279
- await writeFile(path, `#!/bin/bash\n${OLD_COMMIT_BLOCK}echo ran >> "${marker}"\n`);
280
- await chmod(path, 0o755);
281
- // The old line as it runs for real: npx starts isy, and isy rewrites the hook running it.
282
- const bin = await mkdtemp(join(tmpdir(), "isy-hook-bin-"));
283
- await writeFile(join(bin, "npx"), `#!/bin/sh\nwhile [ "$1" != "@nightloom/isy" ]; do shift; done\nshift\n` +
284
- `exec "${process.execPath}" --import "${import.meta.resolve("tsx")}" "${join(import.meta.dirname, "..", "index.ts")}" "$@"\n`);
285
- await chmod(join(bin, "npx"), 0o755);
286
- const { stderr } = await run("git", ["commit", "-q", "--allow-empty", "-m", "next"], {
287
- cwd: fresh,
288
- env: { ...process.env, PATH: `${bin}:${process.env.PATH ?? ""}` },
289
- });
290
- assert.equal(stderr, "");
291
- assert.equal(await readFile(marker, "utf8"), "ran\n");
292
- assert.ok((await readFile(path, "utf8")).includes(COMMIT_COMMAND));
293
- });
294
- test("a hook linked in from elsewhere stays linked when its block is rewritten", async () => {
295
- const fresh = await newRepo("isy-hook-linked-");
296
- const path = (await postCommitPath(fresh));
297
- const script = join(fresh, "post-commit.sh");
298
- await writeFile(script, `#!/bin/sh\n${OLD_COMMIT_BLOCK}`);
299
- await symlink(script, path);
300
- assert.equal(await installGitHook(fresh), "installed");
301
- assert.equal((await lstat(path)).isSymbolicLink(), true);
302
- assert.ok((await readFile(script, "utf8")).includes(COMMIT_COMMAND));
303
- });
304
- test("a hook linked to a file in the work tree keeps its older block until asked, so the tree stays clean", async () => {
305
- const fresh = await newRepo("isy-hook-tracked-");
306
- await mkdir(join(fresh, "tools"));
307
- const script = join(fresh, "tools/post-commit");
308
- const wired = `#!/bin/sh\n${OLD_COMMIT_BLOCK}`;
309
- await writeFile(script, wired, { mode: 0o755 });
310
- await run("git", ["add", "."], { cwd: fresh });
311
- await run("git", ["commit", "-q", "-m", "tools"], { cwd: fresh });
312
- await symlink("../../tools/post-commit", (await postCommitPath(fresh)));
313
- const clean = async () => assert.equal((await run("git", ["status", "--porcelain"], { cwd: fresh })).stdout, "");
314
- await cli(fresh, ["commit", "--hook"]);
315
- await clean();
316
- assert.equal(await readFile(script, "utf8"), wired);
317
- assert.equal(await autoInstallGitHook(fresh), "shared-hooks");
318
- await clean();
319
- assert.equal(await installGitHook(fresh), "installed");
320
- assert.ok((await readFile(script, "utf8")).includes(COMMIT_COMMAND));
321
- });
322
- test("a hook linked to a file of a work tree waits, from whichever tree of the repository it is reached", async () => {
323
- const fresh = await newRepo("isy-hook-tracked-linked-");
324
- await mkdir(join(fresh, "tools"));
325
- const script = join(fresh, "tools/post-commit");
326
- const wired = `#!/bin/sh\n${OLD_COMMIT_BLOCK}`;
327
- await writeFile(script, wired, { mode: 0o755 });
328
- await run("git", ["add", "."], { cwd: fresh });
329
- await run("git", ["commit", "-q", "-m", "tools"], { cwd: fresh });
330
- await symlink("../../tools/post-commit", (await postCommitPath(fresh)));
331
- const linked = join(await mkdtemp(join(tmpdir(), "isy-hook-tree-")), "linked");
332
- await run("git", ["worktree", "add", "-q", "-b", "linked", linked], { cwd: fresh });
333
- await cli(linked, ["commit", "--hook"]);
334
- assert.equal((await run("git", ["status", "--porcelain"], { cwd: fresh })).stdout, "");
335
- assert.equal(await readFile(script, "utf8"), wired);
336
- assert.equal(await autoInstallGitHook(linked), "shared-hooks");
337
- });
338
- test("a hook linked to a file in no work tree is nobody else's, and its older block is brought up to date", async () => {
339
- const fresh = await newRepo("isy-hook-dotfile-");
340
- const script = join(await mkdtemp(join(tmpdir(), "isy-hook-dot-")), "post-commit");
341
- await writeFile(script, `#!/bin/sh\n${OLD_COMMIT_BLOCK}`, { mode: 0o755 });
342
- await symlink(script, (await postCommitPath(fresh)));
343
- await cli(fresh, ["commit", "--hook"]);
344
- assert.ok((await readFile(script, "utf8")).includes(COMMIT_COMMAND));
345
- });
346
- test("a link into the git directory is no shared file, and its older block is brought up to date", async () => {
347
- const fresh = await newRepo("isy-hook-private-link-");
348
- const path = (await postCommitPath(fresh));
349
- const script = join(dirname(dirname(path)), "my-post-commit");
350
- await writeFile(script, `#!/bin/sh\n${OLD_COMMIT_BLOCK}`, { mode: 0o755 });
351
- await symlink("../my-post-commit", path);
352
- await cli(fresh, ["commit", "--hook"]);
353
- assert.ok((await readFile(script, "utf8")).includes(COMMIT_COMMAND));
354
- });
355
- test("takes the block out of a hook in another language, byte for byte, and never adds it again", async () => {
356
- const hooks = {
357
- python: Buffer.from("#!/usr/bin/env python3\nimport sys\n\n\n\nsys.exit(0)\n"),
358
- "python given flags": Buffer.from("#!/usr/bin/env -S python3 -u\nimport sys\n"),
359
- node: Buffer.from("#!/usr/bin/env node\nprocess.exit(0);"),
360
- perl: Buffer.from("#!/usr/bin/perl\r\nexit 0;\r\n"),
361
- "not UTF-8": Buffer.concat([Buffer.from("#!/usr/bin/python\n# caf"), Buffer.from([0xe9]), Buffer.from("\n")]),
362
- };
363
- for (const [label, original] of Object.entries(hooks)) {
364
- const fresh = await newRepo("isy-hook-foreign-");
365
- const commit = (await postCommitPath(fresh));
366
- const cut = original.indexOf("\n") + 1;
367
- // Where every older isy put it: under the shebang, above the user's script.
368
- await writeFile(commit, Buffer.concat([original.subarray(0, cut), Buffer.from(OLD_COMMIT_BLOCK), original.subarray(cut)]));
369
- await chmod(commit, 0o700);
370
- assert.equal(await gitHookInstalled(fresh), false, label);
371
- await cli(fresh, ["commit", "--hook"]);
372
- assert.deepEqual(await readFile(commit), original, label);
373
- assert.equal((await stat(commit)).mode & 0o777, 0o700, label);
374
- // Nothing that runs later puts it back.
375
- await installGitHook(fresh);
376
- await autoInstallGitHook(fresh);
377
- await cli(fresh, ["commit", "--hook"]);
378
- assert.deepEqual(await readFile(commit), original, label);
379
- assert.ok((await readFile((await prePushPath(fresh)), "utf8")).includes(PUSH_COMMAND), label);
380
- assert.equal(await gitHookInstalled(fresh), true, label);
381
- assert.deepEqual((await hooksLeftToTheUser(fresh)).map((hook) => hook.name), ["post-commit"], label);
382
- }
383
- });
384
- test("takes a block out of a hook in another language in a shared directory too, and on logout", async () => {
385
- const original = "#!/usr/bin/env python3\nimport sys\n\n\n\nsys.exit(0)";
386
- const damaged = `#!/usr/bin/env python3\n${OLD_COMMIT_BLOCK}import sys\n\n\n\nsys.exit(0)`;
387
- const shared = await newRepo("isy-hook-foreign-shared-");
388
- await mkdir(join(shared, "githooks"));
389
- await run("git", ["config", "core.hooksPath", "githooks"], { cwd: shared });
390
- await writeFile(join(shared, "githooks", "post-commit"), damaged);
391
- await cli(shared, ["check", "--hook"]);
392
- assert.equal(await readFile(join(shared, "githooks", "post-commit"), "utf8"), original);
393
- const fresh = await newRepo("isy-hook-foreign-logout-");
394
- const commit = (await postCommitPath(fresh));
395
- await writeFile(commit, damaged);
396
- assert.equal(await removeGitHook(fresh), "removed");
397
- assert.equal(await readFile(commit, "utf8"), original);
398
- });
399
- test("a push and a commit go through a hook in another language, with the session's hook run before them", async () => {
400
- const fresh = await newRepo("isy-hook-foreign-push-");
401
- const bare = await mkdtemp(join(tmpdir(), "isy-hook-foreign-bare-"));
402
- await run("git", ["init", "-q", "--bare", "-b", "main"], { cwd: bare });
403
- const body = "#!/usr/bin/env node\nprocess.exit(0);\n";
404
- for (const path of [(await postCommitPath(fresh)), (await prePushPath(fresh))]) {
405
- await writeFile(path, body);
406
- await chmod(path, 0o755);
407
- }
408
- assert.equal(await autoInstallGitHook(fresh), "already-present");
409
- const committed = await run("git", ["commit", "-q", "--allow-empty", "-m", "next"], { cwd: fresh });
410
- assert.equal(committed.stderr, "");
411
- await run("git", ["push", "-q", bare, "main"], { cwd: fresh });
412
- assert.equal(await readFile((await prePushPath(fresh)), "utf8"), body);
413
- });
414
- test("a compiled hook is left alone, and a hook git hands to sh is not", async () => {
415
- const fresh = await newRepo("isy-hook-compiled-");
416
- const compiled = Buffer.from([0x7f, 0x45, 0x4c, 0x46, 0x02, 0x00, 0xff, 0xfe, 0x00, 0x0a]);
417
- const commit = (await postCommitPath(fresh));
418
- await writeFile(commit, compiled);
419
- await installGitHook(fresh);
420
- assert.deepEqual(await readFile(commit), compiled);
421
- for (const shell of [
422
- "#!/bin/sh",
423
- "#! /bin/sh -e",
424
- "#!/bin/bash",
425
- "#!/usr/bin/env bash",
426
- "#!/usr/bin/env -S bash -e",
427
- "#!/usr/bin/zsh",
428
- "#!/bin/ksh",
429
- "#!/bin/dash",
430
- "#!/bin/ash",
431
- "#!/nix/store/abc-bash-5.2/bin/bash",
432
- "# no shebang at all",
433
- ]) {
434
- await writeFile(commit, `${shell}\necho mine\n`);
435
- await installGitHook(fresh);
436
- assert.ok((await readFile(commit, "utf8")).includes(COMMIT_COMMAND), shell);
437
- assert.equal(await gitHookInstalled(fresh), true, shell);
438
- }
439
- });
440
- test("writes where core.hooksPath points, not where it does not", async () => {
441
- const fresh = await newRepo("isy-hook-path-");
442
- const custom = join(fresh, ".husky");
443
- await mkdir(custom, { recursive: true });
444
- await run("git", ["config", "core.hooksPath", ".husky"], { cwd: fresh });
445
- assert.equal(await installGitHook(fresh), "installed");
446
- assert.equal(await postCommitPath(fresh), join(custom, "post-commit"));
447
- assert.ok((await readFile(join(custom, "post-commit"), "utf8")).includes(COMMIT_COMMAND));
448
- });
449
- test("deletes a hook file that held nothing but our own block", async () => {
450
- const fresh = await newRepo("isy-hook-solo-");
451
- await installGitHook(fresh);
452
- const path = (await postCommitPath(fresh));
453
- assert.equal(await removeGitHook(fresh), "removed");
454
- assert.equal(await gitHookInstalled(fresh), false);
455
- // Uninstalling leaves the repository as it was, not holding an empty script.
456
- await assert.rejects(() => stat(path));
457
- assert.equal(await removeGitHook(fresh), "absent");
458
- });
459
- test("installs the pre-push hook alongside the commit one", async () => {
460
- const fresh = await newRepo("isy-hook-push-");
461
- assert.equal(await installGitHook(fresh), "installed");
462
- const path = (await prePushPath(fresh));
463
- const body = await readFile(path, "utf8");
464
- assert.match(body, /^#!\/bin\/sh/);
465
- assert.ok(body.includes(PUSH_COMMAND));
466
- assert.equal(((await stat(path)).mode & 0o111) !== 0, true);
467
- // git waits for pre-push and writes the refs being pushed to its stdin: the
468
- // upload has to outlive the hook and must not sit reading those refs.
469
- assert.ok(PUSH_COMMAND.endsWith("&"), "the upload is backgrounded");
470
- assert.ok(PUSH_COMMAND.includes("</dev/null"), "the upload does not read the refs");
471
- });
472
- test("a repository wired before pre-push existed is completed, not left half-wired", async () => {
473
- const fresh = await newRepo("isy-hook-partial-");
474
- await installGitHook(fresh);
475
- // What an older isy left behind: the commit hook and nothing else.
476
- await rm((await prePushPath(fresh)));
477
- assert.equal(await gitHookInstalled(fresh), false);
478
- assert.equal(await installGitHook(fresh), "installed");
479
- assert.equal(await gitHookInstalled(fresh), true);
480
- // The hook that was already correct is not rewritten into a second block.
481
- const body = await readFile((await postCommitPath(fresh)), "utf8");
482
- assert.equal(body.match(/isy:begin/g)?.length, 1);
483
- });
484
- test("uninstalling takes both hooks, and an older half-install with them", async () => {
485
- const fresh = await newRepo("isy-hook-both-");
486
- await installGitHook(fresh);
487
- const commit = (await postCommitPath(fresh));
488
- const push = (await prePushPath(fresh));
489
- assert.equal(await removeGitHook(fresh), "removed");
490
- await assert.rejects(() => stat(commit));
491
- await assert.rejects(() => stat(push));
492
- assert.equal(await removeGitHook(fresh), "absent");
493
- });
494
- test("reports a directory that is not a repository rather than creating one", async () => {
495
- const plain = await mkdtemp(join(tmpdir(), "isy-hook-plain-"));
496
- assert.equal(await installGitHook(plain), "not-a-repository");
497
- assert.equal(await removeGitHook(plain), "not-a-repository");
498
- assert.equal(await gitHookInstalled(plain), false);
499
- });
500
- test("prints possible signals once and stays quiet on the next commit", async () => {
501
- const cwd = await newRepo("isy-commit-signals-");
502
- await writeSession(cwd, "sess-a", "left-pad");
503
- const first = await runCommit({ noUpload: true }, cwd);
504
- assert.equal(first.candidates.length > 0, true);
505
- const rendered = formatCommit(first);
506
- assert.match(rendered, /possible signal/);
507
- assert.match(rendered, /external_dependency/);
508
- // Same transcript, second commit: the finding was already shown, so silence.
509
- const second = await runCommit({ noUpload: true }, cwd);
510
- assert.deepEqual(second.candidates, []);
511
- assert.equal(formatCommit(second), undefined);
512
- });
513
- test("records how many records the transcript held at each commit", async () => {
514
- const cwd = await newRepo("isy-commit-marks-");
515
- const path = await writeSession(cwd, "sess-b", "left-pad");
516
- await runCommit({ noUpload: true }, cwd);
517
- const afterFirst = (await readState())[path];
518
- assert.equal(afterFirst.commits?.length, 1);
519
- const records = afterFirst.commits[0].records;
520
- // A second commit with more of the session behind it gets its own mark.
521
- await writeFile(path, `${await readFile(path, "utf8")}\n${transcript("sess-b", cwd, "ms")}`);
522
- await writeFile(join(cwd, "b.txt"), "two");
523
- await run("git", ["add", "."], { cwd });
524
- await run("git", ["commit", "-q", "-m", "second"], { cwd });
525
- await runCommit({ noUpload: true }, cwd);
526
- const afterSecond = (await readState())[path];
527
- assert.equal(afterSecond.commits?.length, 2);
528
- assert.equal(afterSecond.commits[1].records > records, true);
529
- assert.notEqual(afterSecond.commits[0].sha, afterSecond.commits[1].sha);
530
- });
531
- test("an amend replaces its own mark instead of recording the commit twice", () => {
532
- const once = withCommit({}, { sha: "abc", records: 10 });
533
- const again = withCommit({ commits: once }, { sha: "abc", records: 14 });
534
- assert.deepEqual(again, [{ sha: "abc", records: 14 }]);
535
- });
536
- test("uploads a session that ended without a commit of its own", async () => {
537
- const cwd = await newRepo("isy-commit-all-");
538
- await writeSession(cwd, "sess-quiet", "left-pad");
539
- await writeSession(cwd, "sess-loud", "ms");
540
- const report = await runUpload({ silent: true, all: true }, cwd);
541
- // The scan is not "the newest session": a session that ended days ago with no
542
- // commit of its own is exactly the one the newest-only rule used to lose.
543
- assert.equal(report.sent, 2);
544
- assert.equal(uploads.length, 2);
545
- });
546
- test("does not send a transcript the server already has", async () => {
547
- const cwd = await newRepo("isy-commit-unchanged-");
548
- await writeSession(cwd, "sess-once", "left-pad");
549
- assert.equal((await runUpload({ silent: true, all: true }, cwd)).sent, 1);
550
- uploads.length = 0;
551
- const second = await runUpload({ silent: true, all: true }, cwd);
552
- assert.equal(second.sent, 0);
553
- assert.equal(uploads.length, 0);
554
- assert.match(second.skipped ?? "", /nothing new to upload/);
555
- });
556
- test("sends the commit marks along with the transcript", async () => {
557
- const cwd = await newRepo("isy-commit-payload-");
558
- await writeSession(cwd, "sess-marks", "left-pad");
559
- await runCommit({ noUpload: true }, cwd);
560
- await runUpload({ silent: true, all: true }, cwd);
561
- assert.equal(uploads.length, 1);
562
- const commits = uploads[0].commits;
563
- assert.equal(Array.isArray(commits), true);
564
- assert.equal(commits.length, 1);
565
- assert.equal(typeof commits[0].sha, "string");
566
- assert.equal(commits[0].records > 0, true);
567
- });
568
- test("a commit after the session ended sends it again, with the new mark", async () => {
569
- const cwd = await newRepo("isy-commit-after-");
570
- await writeSession(cwd, "sess-ended", "left-pad");
571
- assert.equal((await runUpload({ silent: true, all: true }, cwd)).sent, 1);
572
- uploads.length = 0;
573
- // The agent is done and the transcript will not grow again, but the commit is
574
- // what the pull request matches on — and it used to stay on this machine.
575
- await runCommit({ noUpload: true }, cwd);
576
- assert.equal((await runUpload({ silent: true, all: true }, cwd)).sent, 1);
577
- assert.equal(uploads[0].commits.length, 1);
578
- uploads.length = 0;
579
- assert.equal((await runUpload({ silent: true, all: true }, cwd)).sent, 0);
580
- });
581
- test("a transcript is only skipped once the server has accepted it", () => {
582
- const file = { sizeBytes: 100, modifiedAt: new Date(1_000) };
583
- assert.equal(looksUnchanged({}, file), false, "never uploaded is never unchanged");
584
- assert.equal(looksUnchanged({ sizeBytes: 100, modifiedMs: 1_000 }, file), false, "size and mtime alone do not prove the server has it");
585
- assert.equal(looksUnchanged({ uploadedHash: "sha256:x", sizeBytes: 100, modifiedMs: 1_000 }, file), true);
586
- assert.equal(looksUnchanged({ uploadedHash: "sha256:x", sizeBytes: 120, modifiedMs: 1_000 }, file), false, "a grown transcript is changed even at the same mtime");
587
- const marked = {
588
- uploadedHash: "sha256:x",
589
- sizeBytes: 100,
590
- modifiedMs: 1_000,
591
- commits: [{ sha: "b", records: 3 }],
592
- };
593
- assert.equal(looksUnchanged(marked, file), false, "a commit since the upload is news");
594
- assert.equal(looksUnchanged({ ...marked, uploadedCommit: "b" }, file), true);
595
- });
596
- test("a candidate keeps its identity as the session grows", () => {
597
- const candidate = {
598
- category: "known_gap",
599
- uuid: "u1",
600
- toolUseId: "t1",
601
- filePath: "/repo/src/a.ts",
602
- recordIndex: 7,
603
- weight: 0.5,
604
- detail: "left for later",
605
- };
606
- // Same finding, reported again after more of the session was appended.
607
- assert.equal(candidateKey(candidate), candidateKey({ ...candidate, detail: "reworded" }));
608
- // A background subagent merged in by time moved its record further down.
609
- assert.equal(candidateKey(candidate), candidateKey({ ...candidate, recordIndex: 12 }));
610
- assert.notEqual(candidateKey(candidate), candidateKey({ ...candidate, toolUseId: "t2" }));
611
- // Pointing at nothing else, the index is all a candidate has.
612
- const loose = { category: "known_gap", recordIndex: 7, weight: 0.5, detail: "x" };
613
- assert.notEqual(candidateKey(loose), candidateKey({ ...loose, recordIndex: 8 }));
614
- assert.deepEqual(unseen({ shown: [candidateKey(candidate)] }, [candidate]), []);
615
- assert.deepEqual(unseen({ shown: [] }, [candidate]), [candidate]);
616
- // What isy 1.0 printed, under the key it wrote then, is not printed again.
617
- const written = ["known_gap", "/repo/src/a.ts", "u1", "t1", 7].join("\x00");
618
- assert.deepEqual(unseen({ shown: [written] }, [candidate]), []);
619
- });
620
- test("the session hook wires a repository's commit hook without isy init", async () => {
621
- const fresh = await newRepo("isy-hook-auto-");
622
- assert.equal(await autoInstallGitHook(fresh), "installed");
623
- assert.equal(await gitHookInstalled(fresh), true);
624
- assert.equal(await autoInstallGitHook(fresh), "already-present");
625
- // Created without templates: no .git/hooks, still a repository to wire.
626
- const bare = await newRepo("isy-hook-auto-nodir-");
627
- await rm(join(bare, ".git", "hooks"), { recursive: true, force: true });
628
- assert.equal(await autoInstallGitHook(bare), "installed");
629
- const plain = await mkdtemp(join(tmpdir(), "isy-hook-auto-plain-"));
630
- assert.equal(await autoInstallGitHook(plain), "not-a-repository");
631
- });
632
- test("leaves a committed hooks directory to isy init", async () => {
633
- const fresh = await newRepo("isy-hook-auto-shared-");
634
- await mkdir(join(fresh, ".husky"), { recursive: true });
635
- await run("git", ["config", "core.hooksPath", ".husky"], { cwd: fresh });
636
- assert.equal(await autoInstallGitHook(fresh), "shared-hooks");
637
- await assert.rejects(() => stat(join(fresh, ".husky", "post-commit")));
638
- // Once the developer put it there on purpose, it counts as present.
639
- await installGitHook(fresh);
640
- assert.equal(await autoInstallGitHook(fresh), "already-present");
641
- });
642
- test("a session hook wires the commit hook with nothing on stdin, the way Codex runs it", async () => {
643
- const cwd = await newRepo("isy-commit-hookflag-");
644
- await writeSession(cwd, "sess-hookflag", "left-pad");
645
- // A hand-run upload is not a session ending: it leaves the repository alone.
646
- await runUpload({ silent: true, all: true }, cwd);
647
- assert.equal(await gitHookInstalled(cwd), false);
648
- await writeSession(cwd, "sess-hookflag-next", "ms");
649
- const report = await runUpload({ silent: true, all: true, hook: true }, cwd);
650
- assert.equal(report.gitHook, "installed");
651
- assert.equal(await gitHookInstalled(cwd), true);
652
- // The line is parked and read wherever the next session starts: "this repository" would be wrong there.
653
- assert.match(formatUploadAlert(report) ?? "", /hooks added to unwinned\/ISY/);
654
- });
655
- test("a parked line about a shared hooks directory names the repository, and outlives an init run elsewhere", async () => {
656
- const cwd = await newRepo("isy-commit-hookflag-shared-");
657
- await mkdir(join(cwd, ".husky"), { recursive: true });
658
- await run("git", ["config", "core.hooksPath", ".husky"], { cwd });
659
- await writeSession(cwd, "sess-hookflag-shared", "left-pad");
660
- const report = await runUpload({ silent: true, all: true, hook: true }, cwd);
661
- assert.equal(report.gitHook, "shared-hooks");
662
- const line = formatUploadAlert(report) ?? "";
663
- assert.match(line, /no git hooks in unwinned\/ISY/);
664
- // `isy init` drops a parked part with this wording, and an init in another repository answers nothing here.
665
- assert.doesNotMatch(line, /run: isy init/);
666
- });
667
- /** A session that last wrote `days` ago, the way a transcript left alone looks. */
668
- async function age(path, days) {
669
- const then = new Date(Date.now() - days * 24 * 60 * 60 * 1000);
670
- await utimes(path, then, then);
671
- }
672
- test("a commit goes to a session started in a subdirectory, though git runs the hook at the top", async () => {
673
- const top = await realpath(await newRepo("isy-commit-subdir-"));
674
- await mkdir(join(top, "src"));
675
- const atTop = await writeSession(top, "sess-at-top", "left-pad");
676
- await age(atTop, 1);
677
- const inSrc = await writeSession(join(top, "src"), "sess-in-src", "ms");
678
- const report = await runCommit({ noUpload: true }, top);
679
- assert.equal(report.sessionId, "sess-in-src");
680
- const state = await readState();
681
- assert.equal(state[inSrc]?.commits?.length, 1);
682
- assert.equal(state[atTop]?.commits, undefined);
683
- // Nothing at the top at all is no reason to skip the commit either.
684
- const alone = await realpath(await newRepo("isy-commit-subdir-alone-"));
685
- await mkdir(join(alone, "lib"));
686
- await writeSession(join(alone, "lib"), "sess-alone", "ms");
687
- assert.equal((await runCommit({ noUpload: true }, alone)).sessionId, "sess-alone");
688
- });
689
- test("the commit and the push send a session started in a subdirectory", async () => {
690
- const top = await realpath(await newRepo("isy-commit-subdir-send-"));
691
- await mkdir(join(top, "src"));
692
- await writeSession(join(top, "src"), "sess-sent", "ms");
693
- await runCommit({ noUpload: true }, top);
694
- const report = await runUpload({ silent: true, all: true }, top);
695
- assert.equal(report.sent, 1);
696
- assert.equal(uploads[0].sessionId, "sess-sent");
697
- assert.equal(uploads[0].commits.length, 1);
698
- });
699
- test("a hook in a linked worktree still finds the session in its subdirectory", async () => {
700
- const main = await realpath(await newRepo("isy-commit-linked-"));
701
- const tree = `${main}-linked`;
702
- await run("git", ["worktree", "add", "-q", "-b", "linked", tree], { cwd: main });
703
- await mkdir(join(tree, "src"));
704
- const inSrc = await writeSession(join(tree, "src"), "sess-linked", "ms");
705
- // What git hands a post-commit hook there, and not in a main worktree: asked
706
- // about any directory, git then answers that it is the top of the tree.
707
- process.env.GIT_DIR = (await run("git", ["rev-parse", "--absolute-git-dir"], { cwd: tree })).stdout.trim();
708
- try {
709
- assert.equal((await runCommit({ noUpload: true }, tree)).sessionId, "sess-linked");
710
- }
711
- finally {
712
- delete process.env.GIT_DIR;
713
- }
714
- assert.equal((await readState())[inSrc]?.commits?.length, 1);
715
- });
716
- test("a commit stays out of the trees beside and inside this one", async () => {
717
- const top = await realpath(await newRepo("isy-commit-trees-"));
718
- const session = await writeSession(top, "sess-here", "left-pad");
719
- await age(session, 1);
720
- // Each of these is newer, and each is its own checkout on its own history.
721
- const worktree = join(top, ".claude", "worktrees", "other");
722
- await mkdir(join(top, ".claude", "worktrees"), { recursive: true });
723
- await run("git", ["worktree", "add", "-q", "-b", "nested", worktree], { cwd: top });
724
- await writeSession(worktree, "sess-nested-worktree", "ms");
725
- const vendored = join(top, "vendor", "lib");
726
- await mkdir(vendored, { recursive: true });
727
- await run("git", ["init", "-q", "-b", "main"], { cwd: vendored });
728
- await writeSession(vendored, "sess-nested-repo", "ms");
729
- // Same prefix on disk, so the same prefix among Claude's project folders.
730
- const beside = await newRepo("isy-commit-trees-beside-");
731
- await rename(beside, `${top}-beside`);
732
- await writeSession(`${top}-beside`, "sess-beside", "ms");
733
- assert.equal((await runCommit({ noUpload: true }, top)).sessionId, "sess-here");
734
- assert.equal((await readState())[session]?.commits?.length, 1);
735
- assert.equal((await runUpload({ silent: true, all: true }, top)).sent, 1);
736
- assert.deepEqual(uploads.map((upload) => upload.sessionId), ["sess-here"]);
737
- });
738
- test("a session elsewhere in the tree counts only as far back as a sweep looks", async () => {
739
- const top = await realpath(await newRepo("isy-commit-subdir-old-"));
740
- await mkdir(join(top, "src"));
741
- const here = await writeSession(top, "sess-old-here", "left-pad");
742
- await age(here, 60);
743
- const inSrc = await writeSession(join(top, "src"), "sess-old-src", "ms");
744
- await age(inSrc, 30);
745
- // The directory's own sessions go however old they are, as they always did.
746
- assert.equal((await runCommit({ noUpload: true }, top)).sessionId, "sess-old-here");
747
- assert.equal((await runUpload({ silent: true, all: true }, top)).sent, 1);
748
- assert.deepEqual(uploads.map((upload) => upload.sessionId), ["sess-old-here"]);
749
- });
750
- /** The paths a transcript went up with, as `sent` lines of JSON. */
751
- function sentRecords(upload) {
752
- const jsonl = gunzipSync(Buffer.from(upload.transcript ?? "", "base64")).toString("utf8");
753
- return jsonl.trim().split("\n").map((line) => JSON.parse(line));
754
- }
755
- function editedPaths(records) {
756
- const paths = [];
757
- for (const entry of records) {
758
- const content = entry.message?.content;
759
- if (!Array.isArray(content))
760
- continue;
761
- for (const block of content) {
762
- if (block.input?.file_path)
763
- paths.push(block.input.file_path);
764
- }
765
- }
766
- return paths;
767
- }
768
- test("a session started in a subdirectory uploads its paths from the top of the repository", async () => {
769
- const top = await realpath(await newRepo("isy-commit-root-"));
770
- const pkg = join(top, "pkg");
771
- await mkdir(pkg);
772
- const path = await writeSession(pkg, "sess-root", "left-pad");
773
- // A sibling of the directory it started in, the way a monorepo session reaches one.
774
- await writeFile(path, `${await readFile(path, "utf8")}\n${record({
775
- type: "assistant",
776
- uuid: "sess-root-sibling",
777
- parentUuid: "sess-root-manifest",
778
- sessionId: "sess-root",
779
- timestamp: "2026-08-16T10:30:00Z",
780
- message: {
781
- role: "assistant",
782
- content: [
783
- { type: "tool_use", id: "t-sibling", name: "Edit", input: { file_path: join(top, "test/x.test.ts"), old_string: "a", new_string: "b" } },
784
- ],
785
- },
786
- })}`);
787
- // The commit reads it the same way, and what it prints is from the top too.
788
- const committed = await runCommit({ noUpload: true }, top);
789
- assert.ok(committed.candidates.some((candidate) => candidate.filePath === "pkg/package.json"));
790
- // From the hook's directory, the session's own hook and the sweep: three routes, one set of bytes.
791
- const hashes = new Set();
792
- for (const send of [
793
- () => runUpload({ silent: true, all: true }, top),
794
- () => runUpload({ silent: true }, pkg),
795
- () => runUpload({ silent: true, sweep: true }, tmpdir()),
796
- ]) {
797
- await writeFile(sessionStatePath(), "{}\n");
798
- uploads.length = 0;
799
- await send();
800
- const sent = uploads.find((upload) => upload.sessionId === "sess-root");
801
- assert.ok(sent);
802
- hashes.add(sent.contentHash);
803
- const records = sentRecords(sent);
804
- assert.equal(records[0].cwd, "pkg");
805
- const edited = editedPaths(records);
806
- assert.ok(edited.includes("pkg/src/f0.ts"), "under the directory it started in");
807
- assert.ok(edited.includes("pkg/package.json"));
808
- assert.ok(edited.includes("test/x.test.ts"), "beside it");
809
- }
810
- assert.equal(hashes.size, 1);
811
- });
812
- test("a session at the top of the repository is masked as it always was", async () => {
813
- const top = await realpath(await newRepo("isy-commit-root-top-"));
814
- await writeSession(top, "sess-top", "left-pad");
815
- await runUpload({ silent: true, all: true }, top);
816
- const records = sentRecords(uploads[0]);
817
- assert.equal(records[0].cwd, ".");
818
- assert.ok(editedPaths(records).includes("src/f0.ts"));
819
- });
820
- /** The session as Claude Code writes it: every record says which branch it ran on. */
821
- async function onBranch(path, branch) {
822
- const lines = (await readFile(path, "utf8")).split("\n");
823
- const stamped = lines.map((line) => record({ ...JSON.parse(line), gitBranch: branch }));
824
- await writeFile(path, stamped.join("\n"));
825
- }
826
- /**
827
- * A commit of one new or changed file, the way someone at a terminal makes one. Its
828
- * own text by default, as the same line under two names reads as one work moved.
829
- */
830
- async function commitFile(cwd, file, content = `text of ${file}`) {
831
- await mkdir(join(cwd, dirname(file)), { recursive: true });
832
- await writeFile(join(cwd, file), content);
833
- await run("git", ["add", "."], { cwd });
834
- await run("git", ["commit", "-q", "-m", file], { cwd });
835
- }
836
- async function appendTurn(path, sessionId, block, timestamp) {
837
- const added = record({
838
- type: "assistant",
839
- uuid: `${sessionId}-${timestamp}`,
840
- parentUuid: `${sessionId}-manifest`,
841
- sessionId,
842
- timestamp,
843
- message: { role: "assistant", content: [block] },
844
- });
845
- await writeFile(path, `${await readFile(path, "utf8")}\n${added}`);
846
- }
847
- const OLD = "2026-08-16T11:00:00Z";
848
- const shell = (id, command) => ({ type: "tool_use", id, name: "Bash", input: { command } });
849
- test("a commit on a branch the session never worked on leaves it alone", async () => {
850
- const cwd = await newRepo("isy-commit-other-");
851
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
852
- const path = await writeSession(cwd, "sess-feat", "left-pad");
853
- await onBranch(path, "feat");
854
- await commitFile(cwd, "own.txt");
855
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
856
- await runUpload({ silent: true, all: true }, cwd);
857
- uploads.length = 0;
858
- // Long after it ended, on a branch made for something else.
859
- await run("git", ["switch", "-q", "-c", "elsewhere", "main"], { cwd });
860
- await commitFile(cwd, "README.md");
861
- const report = await runCommit({ noUpload: true }, cwd);
862
- assert.match(report.skipped ?? "", /another branch/);
863
- assert.deepEqual(report.candidates, []);
864
- const state = (await readState())[path];
865
- assert.equal(state.commits?.length, 1);
866
- assert.equal(state.branch, "feat");
867
- // Nothing goes up that would move it there either.
868
- assert.equal((await runUpload({ silent: true, all: true }, cwd)).sent, 0);
869
- // Back on its own branch, a commit after it ended is its own, as it always was.
870
- await run("git", ["switch", "-q", "feat"], { cwd });
871
- await commitFile(cwd, "later.txt");
872
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
873
- assert.equal((await readState())[path].commits?.length, 2);
874
- });
875
- test("a shell call with no command to read keeps a commit on another branch", async () => {
876
- const cwd = await newRepo("isy-commit-unread-");
877
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
878
- const path = await writeSession(cwd, "sess-unread", "left-pad");
879
- await onBranch(path, "feat");
880
- await commitFile(cwd, "own.txt");
881
- await runCommit({ noUpload: true }, cwd);
882
- await run("git", ["switch", "-q", "-c", "elsewhere", "main"], { cwd });
883
- await appendTurn(path, "sess-unread", { type: "tool_use", id: "t-unread", name: "Bash", input: {} }, new Date().toISOString());
884
- await commitFile(cwd, "README.md");
885
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
886
- });
887
- test("an upload from another checkout leaves a session on the branch its transcript names", async () => {
888
- const cwd = await newRepo("isy-commit-stamp-");
889
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
890
- const path = await writeSession(cwd, "sess-stamp", "left-pad");
891
- await onBranch(path, "feat");
892
- // What a pre-push from a branch made for something else runs.
893
- await run("git", ["switch", "-q", "-c", "elsewhere", "main"], { cwd });
894
- await runUpload({ silent: true, all: true }, cwd);
895
- assert.equal((await readState())[path].branch, "feat");
896
- await commitFile(cwd, "README.md");
897
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
898
- assert.equal((await readState())[path].commits, undefined);
899
- });
900
- test("a detached HEAD is no branch for a session to stand on", async () => {
901
- const cwd = await newRepo("isy-commit-detached-");
902
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
903
- const path = await writeSession(cwd, "sess-detached", "left-pad");
904
- await onBranch(path, "feat");
905
- await commitFile(cwd, "own.txt");
906
- await runCommit({ noUpload: true }, cwd);
907
- // The agent commits on a detached HEAD: a mark, and the session stays where it was.
908
- await run("git", ["checkout", "-q", "--detach"], { cwd });
909
- const now = new Date().toISOString();
910
- await appendTurn(path, "sess-detached", shell("t-try", "git commit -m try"), now);
911
- await commitFile(cwd, "try.txt");
912
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
913
- let state = (await readState())[path];
914
- assert.equal(state.commits?.length, 2);
915
- assert.equal(state.branch, "feat");
916
- // So a commit long after, on a branch it never saw, is still someone else's.
917
- await writeFile(path, (await readFile(path, "utf8")).replaceAll(now, OLD));
918
- await run("git", ["switch", "-q", "-c", "unrelated", "main"], { cwd });
919
- await commitFile(cwd, "README.md");
920
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
921
- // An upload from one stores nothing either, for a session that names no branch.
922
- const bare = await newRepo("isy-commit-detached-bare-");
923
- const barePath = await writeSession(bare, "sess-detached-bare", "left-pad");
924
- await run("git", ["checkout", "-q", "--detach"], { cwd: bare });
925
- await runUpload({ silent: true, all: true }, bare);
926
- state = (await readState())[barePath];
927
- assert.ok(state.uploadedHash);
928
- assert.equal(state.branch, undefined);
929
- });
930
- test("a commit on a new branch holding what the session left uncommitted is its own", async () => {
931
- const cwd = await newRepo("isy-commit-carried-");
932
- const path = await writeSession(cwd, "sess-carried", "left-pad");
933
- await onBranch(path, "main");
934
- // The agent stopped with edits in the tree; the human branched and committed them.
935
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
936
- await commitFile(cwd, "src/f0.ts");
937
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
938
- let state = (await readState())[path];
939
- assert.equal(state.commits?.length, 1);
940
- assert.equal(state.branch, "feat");
941
- // That branch is its own now, whatever goes on it.
942
- await commitFile(cwd, "unrelated.txt");
943
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
944
- state = (await readState())[path];
945
- assert.equal(state.commits?.length, 2);
946
- // The same file on yet another branch is not: that edit is behind its last mark.
947
- await run("git", ["switch", "-q", "-c", "other", "main"], { cwd });
948
- await commitFile(cwd, "src/f0.ts", "someone else's");
949
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
950
- assert.equal((await readState())[path].commits?.length, 2);
951
- });
952
- test("work a session's mark holds stays its own when it moves to another branch", async () => {
953
- const moves = [
954
- ["redone on a new branch", async (cwd) => {
955
- await run("git", ["reset", "-q", "--soft", "HEAD~1"], { cwd });
956
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
957
- await run("git", ["commit", "-q", "-m", "redo"], { cwd });
958
- }],
959
- ["amended on a new branch", async (cwd) => {
960
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
961
- await run("git", ["commit", "-q", "--amend", "-m", "better"], { cwd });
962
- }],
963
- ["picked onto a new branch", async (cwd) => {
964
- const { stdout } = await run("git", ["rev-parse", "HEAD"], { cwd });
965
- await run("git", ["switch", "-q", "-c", "feat", "HEAD~1"], { cwd });
966
- await run("git", ["cherry-pick", stdout.trim()], { cwd });
967
- }],
968
- ["amended on the branch renamed under it", async (cwd) => {
969
- await run("git", ["branch", "-m", "feat"], { cwd });
970
- await run("git", ["commit", "-q", "--amend", "-m", "better"], { cwd });
971
- }],
972
- ["an older mark's file, picked once a later mark changed it", async (cwd) => {
973
- const { stdout } = await run("git", ["rev-parse", "HEAD"], { cwd });
974
- await commitFile(cwd, "src/f0.ts", "changed again");
975
- await runCommit({ noUpload: true }, cwd);
976
- await run("git", ["switch", "-q", "-c", "feat", "HEAD~2"], { cwd });
977
- await run("git", ["cherry-pick", stdout.trim()], { cwd });
978
- }],
979
- ];
980
- for (const [name, move] of moves) {
981
- const cwd = await newRepo("isy-commit-work-moved-");
982
- const path = await writeSession(cwd, "sess-work-moved", "left-pad");
983
- await onBranch(path, "main");
984
- await commitFile(cwd, "src/f0.ts");
985
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined, name);
986
- await move(cwd);
987
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined, name);
988
- const state = (await readState())[path];
989
- const { stdout } = await run("git", ["rev-parse", "HEAD"], { cwd });
990
- assert.equal(state.commits?.at(-1)?.sha, stdout.trim(), name);
991
- assert.equal(state.branch, "feat", name);
992
- await writeFile(sessionStatePath(), "{}\n");
993
- }
994
- });
995
- const LONG = Array.from({ length: 40 }, (_, index) => `line ${index}`).join("\n") + "\n";
996
- const longWith = (top = "line 0", bottom = "line 39") => LONG.replace("line 0\n", `${top}\n`).replace("line 39\n", `${bottom}\n`);
997
- /** A session on main whose mark changed the top of `f.txt`; another branch changed the bottom. */
998
- async function markedLongFile(prefix) {
999
- const cwd = await newRepo(prefix);
1000
- await commitFile(cwd, "f.txt", LONG);
1001
- await run("git", ["branch", "up"], { cwd });
1002
- const path = await writeSession(cwd, "sess-changed", "left-pad");
1003
- await onBranch(path, "main");
1004
- await commitFile(cwd, "f.txt", longWith("agent"));
1005
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1006
- const { stdout } = await run("git", ["rev-parse", "HEAD"], { cwd });
1007
- await run("git", ["switch", "-q", "up"], { cwd });
1008
- await commitFile(cwd, "f.txt", longWith("line 0", "upstream"));
1009
- await run("git", ["switch", "-q", "main"], { cwd });
1010
- return { cwd, path, mark: stdout.trim() };
1011
- }
1012
- test("work a session's mark holds stays its own when it moves to another branch and changes on the way", async () => {
1013
- const amendTweak = async (cwd) => {
1014
- await writeFile(join(cwd, "f.txt"), longWith("agent", "human"));
1015
- await run("git", ["add", "."], { cwd });
1016
- await run("git", ["commit", "-q", "--amend", "--no-edit"], { cwd });
1017
- };
1018
- const moves = [
1019
- ["amended with a tweak on a new branch", async (cwd) => {
1020
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1021
- await amendTweak(cwd);
1022
- }],
1023
- ["redone with a tweak on a new branch", async (cwd) => {
1024
- await run("git", ["reset", "-q", "--soft", "HEAD~1"], { cwd });
1025
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1026
- await commitFile(cwd, "f.txt", longWith("agent", "human"));
1027
- }],
1028
- ["amended with a tweak on the branch renamed under it", async (cwd) => {
1029
- await run("git", ["branch", "-m", "main", "feat"], { cwd });
1030
- await amendTweak(cwd);
1031
- }],
1032
- ["amended with a tweak on a detached HEAD", async (cwd) => {
1033
- await run("git", ["switch", "-q", "--detach"], { cwd });
1034
- await amendTweak(cwd);
1035
- }],
1036
- ["a copy rebased over a branch that changed the file", async (cwd) => {
1037
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1038
- await run("git", ["rebase", "-q", "up"], { cwd });
1039
- }],
1040
- ["picked onto a branch that changed the file", async (cwd, mark) => {
1041
- await run("git", ["switch", "-q", "-c", "feat", "up"], { cwd });
1042
- await run("git", ["cherry-pick", mark], { cwd });
1043
- }],
1044
- ["picked without committing, and tweaked", async (cwd, mark) => {
1045
- await run("git", ["switch", "-q", "-c", "feat", "HEAD~1"], { cwd });
1046
- await run("git", ["cherry-pick", "-n", mark], { cwd });
1047
- await commitFile(cwd, "f.txt", longWith("agent", "human"));
1048
- }],
1049
- ["squashed over a branch that changed the file", async (cwd) => {
1050
- await run("git", ["switch", "-q", "-c", "feat", "up"], { cwd });
1051
- await run("git", ["merge", "-q", "--squash", "main"], { cwd });
1052
- await run("git", ["commit", "-q", "-m", "squashed"], { cwd });
1053
- }],
1054
- ["renamed and amended", async (cwd) => {
1055
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1056
- await run("git", ["mv", "f.txt", "g.txt"], { cwd });
1057
- await run("git", ["commit", "-q", "--amend", "--no-edit"], { cwd });
1058
- }],
1059
- ["reindented with CRLF endings on a new branch", async (cwd) => {
1060
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1061
- await writeFile(join(cwd, "f.txt"), longWith("agent").replaceAll("\n", "\r\n").replaceAll("line", " line").replace("agent", " agent"));
1062
- await run("git", ["add", "."], { cwd });
1063
- await run("git", ["commit", "-q", "--amend", "--no-edit"], { cwd });
1064
- }],
1065
- ];
1066
- for (const [name, move] of moves) {
1067
- const { cwd, path, mark } = await markedLongFile("isy-commit-changed-");
1068
- await move(cwd, mark);
1069
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined, name);
1070
- const { stdout } = await run("git", ["rev-parse", "HEAD"], { cwd });
1071
- assert.equal((await readState())[path].commits?.at(-1)?.sha, stdout.trim(), name);
1072
- await writeFile(sessionStatePath(), "{}\n");
1073
- }
1074
- });
1075
- test("a commit git says replaced a session's mark stays its own, though it shares no line with it", async () => {
1076
- const amended = async (cwd, content) => {
1077
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1078
- await writeFile(join(cwd, "f.txt"), content);
1079
- await run("git", ["add", "."], { cwd });
1080
- await run("git", ["commit", "-q", "--amend", "--no-edit"], { cwd });
1081
- };
1082
- const moves = [
1083
- ["the only line it changed, changed again by an amend", (cwd) => amended(cwd, longWith("human"))],
1084
- ["a mode change amended with a line", async (cwd) => {
1085
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1086
- await run("git", ["update-index", "--chmod=+x", "f.txt"], { cwd });
1087
- await run("git", ["commit", "-q", "--amend", "--no-edit"], { cwd });
1088
- }],
1089
- ["the line a pick stopped on, settled to neither side", async (cwd) => {
1090
- await run("git", ["switch", "-q", "-c", "theirs", "up"], { cwd });
1091
- await commitFile(cwd, "f.txt", longWith("upstream"));
1092
- await run("git", ["switch", "-q", "-c", "feat", "main"], { cwd });
1093
- await assert.rejects(() => run("git", ["rebase", "theirs"], { cwd }));
1094
- await writeFile(join(cwd, "f.txt"), longWith("both"));
1095
- await run("git", ["add", "."], { cwd });
1096
- // As the hook sees each pick of a rebase: HEAD is the new commit, and the rebase goes on.
1097
- await run("git", ["commit", "-q", "-m", "settled"], { cwd });
1098
- }],
1099
- ];
1100
- for (const [name, move] of moves) {
1101
- const { cwd } = await markedLongFile("isy-commit-rewrote-");
1102
- await move(cwd);
1103
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined, name);
1104
- await writeFile(sessionStatePath(), "{}\n");
1105
- }
1106
- // An amend of a commit that was never the session's says nothing of it.
1107
- const { cwd, path } = await markedLongFile("isy-commit-rewrote-");
1108
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1109
- await commitFile(cwd, "h.txt", "human\n");
1110
- await run("git", ["commit", "-q", "--amend", "-m", "again"], { cwd });
1111
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1112
- assert.equal((await readState())[path].commits?.length, 1);
1113
- });
1114
- test("a line stays what it says through a formatter's punctuation", async () => {
1115
- const cwd = await newRepo("isy-commit-formatted-");
1116
- const path = await writeSession(cwd, "sess-formatted", "left-pad");
1117
- await onBranch(path, "main");
1118
- const body = Array.from({ length: 10 }, (_, index) => `call('value ${index}')`).join("\n");
1119
- await commitFile(cwd, "own.ts", `${body}\n`);
1120
- await runCommit({ noUpload: true }, cwd);
1121
- await run("git", ["reset", "-q", "--soft", "HEAD~1"], { cwd });
1122
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1123
- await commitFile(cwd, "own.ts", `${body.replaceAll("'", '"').replaceAll(")", ");")}\n`);
1124
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1125
- });
1126
- test("a merge that settled a file of a session's mark stays its own, unless it kept what stood here", async () => {
1127
- const settles = [
1128
- ["by hand, with both sides on one line", longWith("agent+up")],
1129
- ["taking the mark's side whole", longWith("agent")],
1130
- ["keeping the branch's own side", undefined],
1131
- ];
1132
- for (const [name, settled] of settles) {
1133
- const cwd = await newRepo("isy-commit-settled-");
1134
- await commitFile(cwd, "f.txt", LONG);
1135
- await run("git", ["branch", "up"], { cwd });
1136
- const path = await writeSession(cwd, "sess-settled", "left-pad");
1137
- await onBranch(path, "main");
1138
- await commitFile(cwd, "f.txt", longWith("agent"));
1139
- await runCommit({ noUpload: true }, cwd);
1140
- await run("git", ["switch", "-q", "up"], { cwd });
1141
- await commitFile(cwd, "f.txt", longWith("up"));
1142
- await run("git", ["switch", "-q", "-c", "mix"], { cwd });
1143
- await assert.rejects(run("git", ["merge", "main"], { cwd }));
1144
- await writeFile(join(cwd, "f.txt"), settled ?? longWith("up"));
1145
- await run("git", ["add", "."], { cwd });
1146
- await run("git", ["commit", "-q", "--no-edit"], { cwd });
1147
- const { skipped } = await runCommit({ noUpload: true }, cwd);
1148
- if (settled)
1149
- assert.equal(skipped, undefined, name);
1150
- else
1151
- assert.match(skipped ?? "", /another branch/, name);
1152
- await writeFile(sessionStatePath(), "{}\n");
1153
- }
1154
- });
1155
- test("a file a session wrote and nobody marked stays its own when it is renamed on another branch", async () => {
1156
- const cwd = await newRepo("isy-commit-renamed-");
1157
- const path = await writeSession(cwd, "sess-renamed", "left-pad");
1158
- await onBranch(path, "main");
1159
- const written = "what only this session wrote\n";
1160
- await appendTurn(path, "sess-renamed", { type: "tool_use", id: "t-write", name: "Write", input: { file_path: join(cwd, "new.txt"), content: written } }, OLD);
1161
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1162
- await commitFile(cwd, "moved.txt", written);
1163
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1164
- assert.equal((await readState())[path].branch, "feat");
1165
- });
1166
- test("a binary a session's mark added stays its own when it is renamed on another branch", async () => {
1167
- const cwd = await newRepo("isy-commit-binary-");
1168
- const path = await writeSession(cwd, "sess-binary", "left-pad");
1169
- await onBranch(path, "main");
1170
- await commitFile(cwd, "logo.png", "\0\x01\x02 made of no lines");
1171
- await runCommit({ noUpload: true }, cwd);
1172
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1173
- await run("git", ["mv", "logo.png", "icon.png"], { cwd });
1174
- await run("git", ["commit", "-q", "--amend", "--no-edit"], { cwd });
1175
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1176
- assert.equal((await readState())[path].branch, "feat");
1177
- });
1178
- test("a commit on another branch sharing nothing of a session's marks, or only boilerplate, is not its own", async () => {
1179
- const refused = [
1180
- ["the marked file, to other text", async (cwd) => {
1181
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1182
- await commitFile(cwd, "f.txt", longWith("human"));
1183
- }],
1184
- ["another file", async (cwd) => {
1185
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1186
- await commitFile(cwd, "h.txt", "human\n");
1187
- }],
1188
- ["a line of punctuation in common", async (cwd) => {
1189
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1190
- await commitFile(cwd, "h.txt", "human\n}\n");
1191
- }],
1192
- ["a revert of the mark", async (cwd, mark) => {
1193
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1194
- await run("git", ["revert", "--no-edit", mark], { cwd });
1195
- }],
1196
- ["the marks gone from the repository", async (cwd) => {
1197
- await run("git", ["reset", "-q", "--hard", "HEAD~1"], { cwd });
1198
- await run("git", ["reflog", "expire", "--expire=now", "--all"], { cwd });
1199
- await run("git", ["gc", "-q", "--prune=now"], { cwd });
1200
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1201
- await commitFile(cwd, "h.txt", "human\n");
1202
- }],
1203
- ];
1204
- for (const [name, commit] of refused) {
1205
- const { cwd, path, mark } = await markedLongFile("isy-commit-unrelated-");
1206
- await commit(cwd, mark);
1207
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/, name);
1208
- assert.equal((await readState())[path].commits?.length, 1, name);
1209
- await writeFile(sessionStatePath(), "{}\n");
1210
- }
1211
- // Two lines any two commits in a language have in common, among thirty of each.
1212
- const cwd = await newRepo("isy-commit-boilerplate-");
1213
- const path = await writeSession(cwd, "sess-boilerplate", "left-pad");
1214
- await onBranch(path, "main");
1215
- const body = (who) => Array.from({ length: 28 }, (_, index) => `${who}(${index});`).concat("return null;", "} else {").join("\n");
1216
- await commitFile(cwd, "own.ts", body("agent"));
1217
- await runCommit({ noUpload: true }, cwd);
1218
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1219
- await commitFile(cwd, "theirs.ts", body("human"));
1220
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1221
- assert.equal((await readState())[path].commits?.length, 1);
1222
- });
1223
- test("a mark too large for git to read back leaves the commit with the session", async () => {
1224
- const cwd = await newRepo("isy-commit-huge-mark-");
1225
- const path = await writeSession(cwd, "sess-huge-mark", "left-pad");
1226
- await onBranch(path, "main");
1227
- await commitFile(cwd, "big.txt", "a line of a generated file\n".repeat(200_000));
1228
- await runCommit({ noUpload: true }, cwd);
1229
- await run("git", ["switch", "-q", "-c", "other"], { cwd });
1230
- await commitFile(cwd, "h.txt", "human\n");
1231
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1232
- assert.equal((await readState())[path].branch, "other");
1233
- });
1234
- test("a copy of a session's branch rebased elsewhere stays its own", async () => {
1235
- const cwd = await newRepo("isy-commit-copied-");
1236
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1237
- const path = await writeSession(cwd, "sess-copied", "left-pad");
1238
- await onBranch(path, "feat");
1239
- await commitFile(cwd, "src/f0.ts");
1240
- await runCommit({ noUpload: true }, cwd);
1241
- await commitFile(cwd, "src/f1.ts");
1242
- await runCommit({ noUpload: true }, cwd);
1243
- await run("git", ["switch", "-q", "main"], { cwd });
1244
- await commitFile(cwd, "m.txt");
1245
- await run("git", ["switch", "-q", "-c", "feat2", "feat"], { cwd });
1246
- await run("git", ["rebase", "-q", "main"], { cwd });
1247
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1248
- assert.equal((await readState())[path].branch, "feat2");
1249
- });
1250
- test("what a session edited and no mark took goes with it, though its last mark is behind the edit", async () => {
1251
- const cwd = await newRepo("isy-commit-split-");
1252
- const path = await writeSession(cwd, "sess-split", "left-pad");
1253
- await onBranch(path, "main");
1254
- await commitFile(cwd, "src/f0.ts");
1255
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1256
- await run("git", ["switch", "-q", "-c", "rest"], { cwd });
1257
- await commitFile(cwd, "src/f1.ts");
1258
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1259
- assert.equal((await readState())[path].branch, "rest");
1260
- });
1261
- test("a mark says nothing of another commit to its files, or of a merge that brings them in", async () => {
1262
- const cwd = await newRepo("isy-commit-not-moved-");
1263
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1264
- const path = await writeSession(cwd, "sess-not-moved", "left-pad");
1265
- await onBranch(path, "feat");
1266
- await commitFile(cwd, "src/f0.ts");
1267
- await runCommit({ noUpload: true }, cwd);
1268
- await commitFile(cwd, "own.txt");
1269
- await runCommit({ noUpload: true }, cwd);
1270
- // An earlier mark took this file, and what is committed here is not what it took.
1271
- await run("git", ["switch", "-q", "-c", "other", "main"], { cwd });
1272
- await commitFile(cwd, "src/f0.ts", "someone else's");
1273
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1274
- await run("git", ["merge", "-q", "--no-ff", "-m", "bring it in", "feat", "-X", "ours"], { cwd });
1275
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1276
- assert.equal((await readState())[path].commits?.length, 2);
1277
- assert.equal((await readState())[path].branch, "feat");
1278
- });
1279
- test("a file named HEAD does not cost a commit the list of what it holds", async () => {
1280
- const cwd = await newRepo("isy-commit-named-head-");
1281
- await commitFile(cwd, "HEAD");
1282
- const path = await writeSession(cwd, "sess-named-head", "left-pad");
1283
- await onBranch(path, "main");
1284
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1285
- await commitFile(cwd, "src/f0.ts");
1286
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1287
- assert.equal((await readState())[path].branch, "feat");
1288
- });
1289
- test("a merge is known by the conflict it settled, whichever side that took", async () => {
1290
- for (const settled of ["feat's", "both"]) {
1291
- const cwd = await newRepo("isy-commit-settled-");
1292
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1293
- const path = await writeSession(cwd, "sess-settled", "left-pad");
1294
- await onBranch(path, "feat");
1295
- await commitFile(cwd, "a.txt", "feat's");
1296
- await runCommit({ noUpload: true }, cwd);
1297
- await run("git", ["switch", "-q", "main"], { cwd });
1298
- await commitFile(cwd, "a.txt", "main's");
1299
- await run("git", ["merge", "-q", "feat"], { cwd }).catch(() => undefined);
1300
- await appendTurn(path, "sess-settled", {
1301
- type: "tool_use",
1302
- id: "t-settle",
1303
- name: "Edit",
1304
- input: { file_path: join(cwd, "a.txt"), old_string: "<<<<<<<", new_string: settled },
1305
- }, OLD);
1306
- await commitFile(cwd, "a.txt", settled);
1307
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined, settled);
1308
- assert.equal((await readState())[path].branch, "main", settled);
1309
- await writeFile(sessionStatePath(), "{}\n");
1310
- }
1311
- });
1312
- test("an edit through a symlinked directory is matched against where the file really is", async () => {
1313
- const cwd = await realpath(await newRepo("isy-commit-through-link-"));
1314
- await commitFile(cwd, "real/a.txt", "one");
1315
- await symlink("real", join(cwd, "link"));
1316
- const path = await writeSession(cwd, "sess-through-link", "left-pad");
1317
- await appendTurn(path, "sess-through-link", {
1318
- type: "tool_use",
1319
- id: "t-link",
1320
- name: "Edit",
1321
- input: { file_path: join(cwd, "link/a.txt"), old_string: "one", new_string: "two" },
1322
- }, OLD);
1323
- await onBranch(path, "main");
1324
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1325
- await commitFile(cwd, "real/a.txt", "two");
1326
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1327
- });
1328
- test("an MCP tool whose name does not say it only reads keeps the commit with its session", async () => {
1329
- for (const [tool, input, kept] of [
1330
- ["mcp__ide__executeCode", { code: "open('a','w')" }, true],
1331
- ["mcp__x__format_all", {}, true],
1332
- ["mcp__github__get_file_contents", { path: "a.txt" }, false],
1333
- ["mcp__fs__readFile", { path: "a.txt" }, false],
1334
- ["mcp__docs__search", { query: "x" }, false],
1335
- ]) {
1336
- const cwd = await newRepo("isy-commit-mcp-");
1337
- const path = await writeSession(cwd, "sess-mcp", "left-pad");
1338
- await appendTurn(path, "sess-mcp", { type: "tool_use", id: "t-mcp", name: tool, input }, OLD);
1339
- await onBranch(path, "main");
1340
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1341
- await commitFile(cwd, "README.md");
1342
- const { skipped } = await runCommit({ noUpload: true }, cwd);
1343
- if (kept)
1344
- assert.equal(skipped, undefined, tool);
1345
- else
1346
- assert.match(skipped ?? "", /another branch/, tool);
1347
- await writeFile(sessionStatePath(), "{}\n");
1348
- }
1349
- });
1350
- test("a session in a subdirectory is matched against the commit from the top of the repository", async () => {
1351
- const top = await realpath(await newRepo("isy-commit-carried-sub-"));
1352
- await mkdir(join(top, "pkg"));
1353
- const path = await writeSession(join(top, "pkg"), "sess-carried-sub", "left-pad");
1354
- await onBranch(path, "main");
1355
- await run("git", ["switch", "-q", "-c", "feat"], { cwd: top });
1356
- await commitFile(top, "pkg/src/f0.ts");
1357
- assert.equal((await runCommit({ noUpload: true }, top)).skipped, undefined);
1358
- assert.equal((await readState())[path].commits?.length, 1);
1359
- });
1360
- test("an edit named from where the session stood is matched from the top of the repository", async () => {
1361
- const top = await realpath(await newRepo("isy-commit-relative-"));
1362
- await mkdir(join(top, "pkg"));
1363
- const path = await writeSession(join(top, "pkg"), "sess-relative", "left-pad");
1364
- await appendTurn(path, "sess-relative", {
1365
- type: "tool_use", id: "t-edit", name: "Edit",
1366
- input: { file_path: "lib/x.ts", old_string: "a", new_string: "b" },
1367
- }, OLD);
1368
- await onBranch(path, "main");
1369
- await run("git", ["switch", "-q", "-c", "feat"], { cwd: top });
1370
- await commitFile(top, "pkg/lib/x.ts");
1371
- assert.equal((await runCommit({ noUpload: true }, top)).skipped, undefined);
1372
- assert.equal((await readState())[path].commits?.length, 1);
1373
- });
1374
- test("what a session wrote through the shell counts when the commit holds a file it mentions", async () => {
1375
- const cwd = await newRepo("isy-commit-shell-");
1376
- const path = await writeSession(cwd, "sess-shell", "left-pad");
1377
- await appendTurn(path, "sess-shell", shell("t-heredoc", "cat > docs/notes.md <<'EOF'\nhello\nEOF"), OLD);
1378
- await onBranch(path, "main");
1379
- await run("git", ["switch", "-q", "-c", "unrelated"], { cwd });
1380
- await commitFile(cwd, "other.md");
1381
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1382
- await run("git", ["switch", "-q", "-c", "carried", "main"], { cwd });
1383
- await commitFile(cwd, "docs/notes.md");
1384
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1385
- });
1386
- test("a git too slow to say what a commit added leaves the session its commit, in the time telling apart has, and asks for no fetch", async () => {
1387
- const cwd = await newRepo("isy-commit-slow-lines-");
1388
- await onBranch(await writeSession(cwd, "sess-slow-lines", "left-pad"), "main");
1389
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1390
- await commitFile(cwd, "other.md");
1391
- const bin = await mkdtemp(join(tmpdir(), "isy-commit-bin-"));
1392
- const log = join(bin, "lazy");
1393
- const real = (await run("sh", ["-c", "command -v git"], { cwd })).stdout.trim();
1394
- const script = `#!/bin/sh\ncase " $* " in *" -p "*) echo "\${GIT_NO_LAZY_FETCH:-}" >> "${log}"; exec sleep 30 ;; esac\nexec "${real}" "$@"\n`;
1395
- await writeFile(join(bin, "git"), script, { mode: 0o755 });
1396
- const path = process.env.PATH;
1397
- process.env.PATH = `${bin}:${path}`;
1398
- const started = performance.now();
1399
- try {
1400
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1401
- }
1402
- finally {
1403
- process.env.PATH = path;
1404
- }
1405
- assert.ok(performance.now() - started < 4500, `held ${Math.round(performance.now() - started)} ms`);
1406
- assert.equal((await readFile(log, "utf8")).trim(), "1");
1407
- });
1408
- test("a root commit shows what it holds, whatever log.showroot says", async () => {
1409
- // A file the session edited and no mark took, by its name.
1410
- let cwd = await newRepo("isy-commit-root-");
1411
- await onBranch(await writeSession(cwd, "sess-root", "left-pad"), "main");
1412
- await run("git", ["config", "log.showroot", "false"], { cwd });
1413
- await run("git", ["switch", "-q", "--orphan", "fresh"], { cwd });
1414
- await commitFile(cwd, "src/f0.ts");
1415
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1416
- await writeFile(sessionStatePath(), "{}\n");
1417
- // A mark's lines, by what they say, once a human has changed one of them.
1418
- cwd = await newRepo("isy-commit-root-");
1419
- await onBranch(await writeSession(cwd, "sess-root", "left-pad"), "main");
1420
- await run("git", ["config", "log.showroot", "false"], { cwd });
1421
- await commitFile(cwd, "own.txt", LONG);
1422
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1423
- await run("git", ["switch", "-q", "--orphan", "fresh"], { cwd });
1424
- await commitFile(cwd, "own.txt", longWith("human"));
1425
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1426
- });
1427
- test("a commit moving a submodule's pointer holds what the session edited under it", async () => {
1428
- const cwd = await newRepo("isy-commit-pointer-");
1429
- const lib = await newRepo("isy-commit-pointer-lib-");
1430
- await run("git", ["-c", "protocol.file.allow=always", "submodule", "add", "-q", lib, "sub"], { cwd });
1431
- await run("git", ["commit", "-q", "-m", "sub"], { cwd });
1432
- const path = await writeSession(cwd, "sess-pointer", "left-pad");
1433
- const edit = (id, file) => ({ type: "tool_use", id, name: "Edit", input: { file_path: join(cwd, file), old_string: "a", new_string: "b" } });
1434
- await appendTurn(path, "sess-pointer", edit("t-under", "sub/under.txt"), OLD);
1435
- await onBranch(path, "main");
1436
- const sub = join(cwd, "sub");
1437
- await run("git", ["config", "user.email", "t@e.com"], { cwd: sub });
1438
- await run("git", ["config", "user.name", "T"], { cwd: sub });
1439
- await commitFile(sub, "under.txt");
1440
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1441
- await run("git", ["add", "sub"], { cwd });
1442
- await run("git", ["commit", "-q", "-m", "bump"], { cwd });
1443
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1444
- // A pointer it edited nothing under is no work of its own.
1445
- const other = await newRepo("isy-commit-pointer-other-");
1446
- await run("git", ["-c", "protocol.file.allow=always", "submodule", "add", "-q", lib, "vendored"], { cwd: other });
1447
- await run("git", ["commit", "-q", "-m", "sub"], { cwd: other });
1448
- const away = await writeSession(other, "sess-away", "left-pad");
1449
- await onBranch(away, "main");
1450
- await run("git", ["commit", "-q", "--allow-empty", "-m", "own"], { cwd: other });
1451
- await writeFile(sessionStatePath(), "{}\n");
1452
- await runCommit({ noUpload: true }, other);
1453
- const vendored = join(other, "vendored");
1454
- await run("git", ["config", "user.email", "t@e.com"], { cwd: vendored });
1455
- await run("git", ["config", "user.name", "T"], { cwd: vendored });
1456
- await commitFile(vendored, "z.txt");
1457
- await run("git", ["switch", "-q", "-c", "feat"], { cwd: other });
1458
- await run("git", ["add", "vendored"], { cwd: other });
1459
- await run("git", ["commit", "-q", "-m", "bump"], { cwd: other });
1460
- assert.match((await runCommit({ noUpload: true }, other)).skipped ?? "", /another branch/);
1461
- });
1462
- test("a name a command holds whole counts as mentioned, whatever is in it", async () => {
1463
- for (const [command, file, kept] of [
1464
- [`echo x > "my notes.txt"`, "my notes.txt", true],
1465
- [`cat > "docs/my notes.md" <<'EOF'\nhello\nEOF`, "docs/my notes.md", true],
1466
- [`cat > 'pages/[slug].tsx' <<'EOF'\nhello\nEOF`, "pages/[slug].tsx", true],
1467
- ["echo hi > 'routes/$id.tsx'", "routes/$id.tsx", true],
1468
- ["echo x > 'app/(group)/x(1).md'", "app/(group)/x(1).md", true],
1469
- ["echo x > a=b.txt", "a=b.txt", true],
1470
- ["echo x > '#notes.md'", "#notes.md", true],
1471
- ["echo x > a,b.txt", "a,b.txt", true],
1472
- ["echo x > my\\ notes.txt", "my notes.txt", true],
1473
- [`echo x > "my notes.txt"`, "your notes.txt", false],
1474
- ["cat 'pages/[slug].tsx'", "pages/[slug].tsx", false],
1475
- ]) {
1476
- const cwd = await newRepo("isy-commit-whole-name-");
1477
- const path = await writeSession(cwd, "sess-whole-name", "left-pad");
1478
- await appendTurn(path, "sess-whole-name", shell("t-call", command), OLD);
1479
- await onBranch(path, "main");
1480
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1481
- await commitFile(cwd, file);
1482
- const { skipped } = await runCommit({ noUpload: true }, cwd);
1483
- if (kept)
1484
- assert.equal(skipped, undefined, command);
1485
- else
1486
- assert.match(skipped ?? "", /another branch/, command);
1487
- await writeFile(sessionStatePath(), "{}\n");
1488
- }
1489
- });
1490
- test("a shell call that has not answered yet is the agent making this commit", async () => {
1491
- const cwd = await newRepo("isy-commit-running-");
1492
- const path = await writeSession(cwd, "sess-running", "left-pad");
1493
- await appendTurn(path, "sess-running", shell("t-commit", "git switch -c topic && git commit -am topic"), new Date().toISOString());
1494
- await onBranch(path, "main");
1495
- await run("git", ["switch", "-q", "-c", "topic"], { cwd });
1496
- await commitFile(cwd, "nothing-it-edited.txt");
1497
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1498
- assert.equal((await readState())[path].branch, "topic");
1499
- // The same call left unanswered days ago is a session that died, not one committing.
1500
- const dead = await newRepo("isy-commit-running-dead-");
1501
- const deadPath = await writeSession(dead, "sess-dead", "left-pad");
1502
- await appendTurn(deadPath, "sess-dead", shell("t-commit", "git switch -c topic && git commit -am topic"), OLD);
1503
- await onBranch(deadPath, "main");
1504
- await run("git", ["switch", "-q", "-c", "topic"], { cwd: dead });
1505
- await commitFile(dead, "nothing-it-edited.txt");
1506
- assert.match((await runCommit({ noUpload: true }, dead)).skipped ?? "", /another branch/);
1507
- });
1508
- test("a commit goes to the session making it, though others wrote after it", async () => {
1509
- const cwd = await realpath(await newRepo("isy-commit-maker-"));
1510
- await mkdir(join(cwd, "docs"));
1511
- const making = await writeSession(cwd, "sess-making", "left-pad");
1512
- const below = await writeSession(join(cwd, "docs"), "sess-below", "ms");
1513
- const beside = await writeSession(cwd, "sess-beside", "ms");
1514
- const now = new Date().toISOString();
1515
- await appendTurn(making, "sess-making", shell("t-commit", "git commit -am x"), now);
1516
- await age(making, 1 / 1440);
1517
- // A long shell call that makes no commit says nothing of whose this one is.
1518
- await appendTurn(below, "sess-below", shell("t-test", "npm test"), now);
1519
- await appendTurn(beside, "sess-beside", { type: "text", text: "still reading" }, now);
1520
- await commitFile(cwd, "whatever.txt");
1521
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-making");
1522
- const state = await readState();
1523
- assert.equal(state[making]?.commits?.length, 1);
1524
- assert.equal(state[below]?.commits, undefined);
1525
- assert.equal(state[beside]?.commits, undefined);
1526
- });
1527
- test("a running command that only says git or commit takes nothing from the session whose work it is", async () => {
1528
- for (const busy of [
1529
- "pytest tests/git/ -x",
1530
- "git fetch origin",
1531
- "git log -p | less",
1532
- "tail -f ~/git/app/server.log",
1533
- "cd ~/git/other && npm test",
1534
- "npm --prefix tools/commit run dev",
1535
- ]) {
1536
- const cwd = await realpath(await newRepo("isy-commit-word-"));
1537
- const older = await writeSession(cwd, "sess-busy", "left-pad");
1538
- await appendTurn(older, "sess-busy", shell("t-busy", busy), new Date().toISOString());
1539
- await age(older, 1 / 1440);
1540
- const newest = await writeSession(cwd, "sess-worker", "ms");
1541
- await commitFile(cwd, "src/f0.ts");
1542
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-worker", busy);
1543
- assert.equal((await readState())[newest]?.commits?.length, 1, busy);
1544
- assert.equal((await readState())[older]?.commits, undefined, busy);
1545
- await writeFile(sessionStatePath(), "{}\n");
1546
- }
1547
- });
1548
- test("a commit holding what a session edited goes to it, though another wrote after it", async () => {
1549
- const cwd = await realpath(await newRepo("isy-commit-holder-"));
1550
- await mkdir(join(cwd, "docs"));
1551
- const holder = await writeSession(cwd, "sess-holder", "left-pad");
1552
- await age(holder, 1 / 1440);
1553
- const reader = await writeSession(join(cwd, "docs"), "sess-reader", "ms");
1554
- await commitFile(cwd, "src/f0.ts");
1555
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-holder");
1556
- assert.equal((await readState())[holder]?.commits?.length, 1);
1557
- assert.equal((await readState())[reader]?.commits, undefined);
1558
- // One holding nobody's work goes to the newest, as any other commit does.
1559
- await age(holder, 1 / 1440);
1560
- await commitFile(cwd, "README.md");
1561
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-reader");
1562
- // And a session that has been quiet for longer than a shell call can run is not asked.
1563
- const old = await realpath(await newRepo("isy-commit-holder-old-"));
1564
- await mkdir(join(old, "docs"));
1565
- await age(await writeSession(old, "sess-holder-old", "left-pad"), 1 / 24);
1566
- await writeSession(join(old, "docs"), "sess-reader-old", "ms");
1567
- await commitFile(old, "src/f0.ts");
1568
- assert.equal((await runCommit({ noUpload: true }, old)).sessionId, "sess-reader-old");
1569
- });
1570
- test("a transcript that cannot be read costs the commit no one else's mark", { skip: process.getuid?.() === 0 }, async () => {
1571
- const cwd = await realpath(await newRepo("isy-commit-unreadable-"));
1572
- await mkdir(join(cwd, "pkg"));
1573
- const below = await writeSession(join(cwd, "pkg"), "sess-below", "ms");
1574
- const older = await writeSession(cwd, "sess-older", "left-pad");
1575
- await age(below, 2 / 1440);
1576
- await age(older, 1 / 1440);
1577
- const newest = await writeSession(cwd, "sess-newest", "ms");
1578
- await commitFile(cwd, "whatever.txt");
1579
- // Where a session ran is read off its transcript too, for one further down the tree.
1580
- await chmod(below, 0o000);
1581
- await chmod(older, 0o000);
1582
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-newest");
1583
- assert.equal((await readState())[newest]?.commits?.length, 1);
1584
- await chmod(older, 0o600);
1585
- await chmod(newest, 0o000);
1586
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-older");
1587
- // None left to ask is a commit with no mark, never a hook that dies.
1588
- await chmod(older, 0o000);
1589
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /could be read/);
1590
- for (const path of [below, older, newest])
1591
- await chmod(path, 0o600);
1592
- });
1593
- test("a session redaction throws on leaves a commit with no mark, and the journal says which", async () => {
1594
- const cwd = await realpath(await newRepo("isy-commit-deep-"));
1595
- const session = await writeSession(cwd, "sess-deep", "ms");
1596
- const nested = `${"[".repeat(200_000)}${"]".repeat(200_000)}`;
1597
- await appendFile(session, `\n{"type":"assistant","sessionId":"sess-deep","message":{"role":"assistant","content":[{"type":"tool_use","id":"t","name":"mcp__x__y","input":{"a":${nested}}}]}}\n`);
1598
- await commitFile(cwd, "whatever.txt");
1599
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /could not be read/);
1600
- const said = readJournal().filter((entry) => entry.level === "error").at(-1)?.message ?? "";
1601
- assert.match(said, /^commit: sess-deep could not be read: Maximum call stack/);
1602
- });
1603
- test("telling sessions apart reads no further than its budget, at every pick of a rebase", async () => {
1604
- const cwd = await realpath(await newRepo("isy-commit-budget-"));
1605
- const megabytes = (size) => `${record({ type: "user", message: { role: "user", content: "x".repeat(size * 1024 * 1024) } })}\n`;
1606
- const huge = await writeSession(cwd, "sess-huge", "ms");
1607
- await appendFile(huge, `\n${megabytes(17)}`);
1608
- const second = await writeSession(cwd, "sess-second", "ms");
1609
- await appendFile(second, `\n${megabytes(9)}`);
1610
- const third = await writeSession(cwd, "sess-third", "ms");
1611
- await appendFile(third, `\n${megabytes(9)}`);
1612
- await writeSession(cwd, "sess-newest", "ms");
1613
- await age(huge, 1 / 1440);
1614
- await age(second, 2 / 1440);
1615
- await age(third, 3 / 1440);
1616
- await commitFile(cwd, "README.md");
1617
- const reads = [];
1618
- const { parsedSession, redactedLines } = claudeAgent;
1619
- claudeAgent.parsedSession = async (path, sessionCwd) => {
1620
- reads.push(basename(path, ".jsonl"));
1621
- return parsedSession(path, sessionCwd);
1622
- };
1623
- claudeAgent.redactedLines = async (path, options) => {
1624
- reads.push(basename(path, ".jsonl"));
1625
- return redactedLines(path, options);
1626
- };
1627
- try {
1628
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-newest");
1629
- }
1630
- finally {
1631
- Object.assign(claudeAgent, { parsedSession, redactedLines });
1632
- }
1633
- // One over the budget is passed over for the next one under it, and what is
1634
- // left of the budget, not each session's own size, decides the one after that.
1635
- assert.ok(!reads.includes("sess-huge"), reads.join());
1636
- assert.ok(reads.includes("sess-second"), reads.join());
1637
- assert.ok(!reads.includes("sess-third"), reads.join());
1638
- });
1639
- test("how much of git a commit asks does not grow with the sessions active beside it", async () => {
1640
- const cwd = await realpath(await newRepo("isy-commit-many-"));
1641
- const newest = await writeSession(cwd, "sess-newest", "ms");
1642
- for (let index = 0; index < 20; index += 1) {
1643
- await age(await writeSession(cwd, `sess-${index}`, "ms"), 1 / 1440);
1644
- }
1645
- await commitFile(cwd, "README.md");
1646
- // Every git this process starts leaves a line, before running the real one.
1647
- const bin = await mkdtemp(join(tmpdir(), "isy-commit-bin-"));
1648
- const log = join(bin, "calls");
1649
- const real = (await run("sh", ["-c", "command -v git"], { cwd })).stdout.trim();
1650
- await writeFile(join(bin, "git"), `#!/bin/sh\necho "$1" >> "${log}"\nexec "${real}" "$@"\n`, { mode: 0o755 });
1651
- const path = process.env.PATH;
1652
- process.env.PATH = `${bin}:${path}`;
1653
- try {
1654
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-newest");
1655
- }
1656
- finally {
1657
- process.env.PATH = path;
1658
- }
1659
- assert.equal((await readState())[newest]?.commits?.length, 1);
1660
- const calls = (await readFile(log, "utf8")).trim().split("\n");
1661
- assert.equal(calls.filter((call) => call === "show").length, 1, calls.join());
1662
- assert.ok(calls.length <= 12, calls.join());
1663
- });
1664
- test("a tree full of other repositories' sessions bounds the walk a commit makes for its own", async () => {
1665
- const cwd = await realpath(await newRepo("isy-commit-walk-"));
1666
- const nested = await newRepo("isy-commit-walk-nested-");
1667
- const inside = join(cwd, "work", "proj");
1668
- await mkdir(dirname(inside), { recursive: true });
1669
- await rename(nested, inside);
1670
- for (let index = 0; index < 40; index += 1) {
1671
- await age(await writeSession(inside, `sess-nested-${index}`, "ms"), 2 / 24);
1672
- }
1673
- const own = await writeSession(cwd, "sess-own", "ms");
1674
- await age(own, 3 / 24);
1675
- await commitFile(cwd, "README.md");
1676
- const { cwdOf } = claudeAgent;
1677
- let looked = 0;
1678
- claudeAgent.cwdOf = async (file) => {
1679
- looked += 1;
1680
- return cwdOf(file);
1681
- };
1682
- try {
1683
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-own");
1684
- }
1685
- finally {
1686
- claudeAgent.cwdOf = cwdOf;
1687
- }
1688
- assert.ok(looked <= 32, String(looked));
1689
- });
1690
- test("a CLI that records no branch is judged by the one its upload found", async () => {
1691
- const cwd = await newRepo("isy-commit-nobranch-");
1692
- const path = await writeSession(cwd, "sess-nobranch", "left-pad");
1693
- await runUpload({ silent: true, all: true }, cwd);
1694
- assert.equal((await readState())[path].branch, "main");
1695
- await run("git", ["switch", "-q", "-c", "elsewhere"], { cwd });
1696
- await commitFile(cwd, "README.md");
1697
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1698
- assert.equal((await readState())[path].commits, undefined);
1699
- // Once that branch is gone under another name, its row goes too.
1700
- await run("git", ["branch", "-m", "main", "trunk"], { cwd });
1701
- await commitFile(cwd, "other.md");
1702
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1703
- });
1704
- test("a resume that only read from a branch does not make that branch its own", async () => {
1705
- const cwd = await newRepo("isy-commit-resume-");
1706
- const path = await writeSession(cwd, "sess-resume", "left-pad");
1707
- await onBranch(path, "main");
1708
- await commitFile(cwd, "own.txt");
1709
- await runCommit({ noUpload: true }, cwd);
1710
- await run("git", ["switch", "-q", "-c", "looking"], { cwd });
1711
- await appendTurn(path, "sess-resume", { type: "text", text: "that function is in a.txt" }, OLD);
1712
- await onBranch(path, "looking");
1713
- await commitFile(cwd, "README.md");
1714
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1715
- const state = (await readState())[path];
1716
- assert.equal(state.commits?.length, 1);
1717
- assert.equal(state.branch, "main");
1718
- });
1719
- test("a branch the agent left for itself, or a rename, leaves its row behind and the session on it", async () => {
1720
- const cwd = await newRepo("isy-commit-moved-");
1721
- await run("git", ["switch", "-q", "-c", "feat1"], { cwd });
1722
- const path = await writeSession(cwd, "sess-moved", "left-pad");
1723
- await onBranch(path, "feat1");
1724
- await commitFile(cwd, "own.txt");
1725
- await runCommit({ noUpload: true }, cwd);
1726
- assert.equal((await readState())[path].branch, "feat1");
1727
- // The agent made a branch and went on there; a human commits on it.
1728
- await appendTurn(path, "sess-moved", shell("t-move", "git switch -c feat2"), OLD);
1729
- await onBranch(path, "feat2");
1730
- await run("git", ["switch", "-q", "-c", "feat2"], { cwd });
1731
- await commitFile(cwd, "human.txt");
1732
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1733
- assert.equal((await readState())[path].branch, "feat2");
1734
- // Renamed under it: the name in its row is on nothing any more.
1735
- await run("git", ["branch", "-m", "feat3"], { cwd });
1736
- await appendTurn(path, "sess-moved", { type: "text", text: "still here" }, OLD);
1737
- await onBranch(path, "feat3");
1738
- await commitFile(cwd, "renamed.txt");
1739
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1740
- assert.equal((await readState())[path].branch, "feat3");
1741
- // A move the transcript does not end on says nothing of the branch it did end on.
1742
- await appendTurn(path, "sess-moved", shell("t-away", "git switch -c feat4 && git switch feat3"), OLD);
1743
- await run("git", ["switch", "-q", "-c", "feat4"], { cwd });
1744
- await commitFile(cwd, "someone-else.txt");
1745
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1746
- });
1747
- test("a command nobody can read keeps the commit with its session, and one that only looks gives it up", async () => {
1748
- for (const [command, file, kept] of [
1749
- [`python3 -c "open('docs/n.md','w').write('x')"`, "docs/n.md", true],
1750
- ["npx prettier --write src/a.ts", "src/a.ts", true],
1751
- ["git apply fix.patch", "src/a.ts", true],
1752
- ["curl -o docs/n.md http://x/y", "docs/n.md", true],
1753
- ["cargo fmt", "src/a.rs", true],
1754
- ["npm run format", "src/a.ts", true],
1755
- ["npm install", "package-lock.json", true],
1756
- ["sed -i 's/one/two/' $(git ls-files)", "src/a.ts", true],
1757
- ["for f in src/*.ts; do sed -i 's/a/b/' $f; done", "src/a.ts", true],
1758
- ["git checkout other -- src/a.ts", "src/a.ts", true],
1759
- ["sort -o src/a.txt src/a.txt", "src/a.txt", true],
1760
- ["gh pr checkout 12", "README.md", true],
1761
- [`awk '{print > "docs/n.md"}' README.md`, "docs/n.md", true],
1762
- ["sed -n 'w docs/n.md' README.md", "docs/n.md", true],
1763
- ["git log --output=docs/n.md", "docs/n.md", true],
1764
- ["gh repo clone owner/lib vendor/lib", "vendor/lib/a.ts", true],
1765
- ["xxd -r dump.hex out.bin", "out.bin", true],
1766
- ["git status && git log -3 && cat README.md", "README.md", false],
1767
- ["git add -A && git commit -m notes.md && git push", "docs/notes.md", false],
1768
- ["gh pr create --fill", "other.md", false],
1769
- ]) {
1770
- const cwd = await newRepo("isy-commit-unread-");
1771
- const path = await writeSession(cwd, "sess-unread", "left-pad");
1772
- await appendTurn(path, "sess-unread", shell("t-call", command), OLD);
1773
- await onBranch(path, "main");
1774
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1775
- await commitFile(cwd, file);
1776
- const { skipped } = await runCommit({ noUpload: true }, cwd);
1777
- if (kept)
1778
- assert.equal(skipped, undefined, command);
1779
- else
1780
- assert.match(skipped ?? "", /another branch/, command);
1781
- await writeFile(sessionStatePath(), "{}\n");
1782
- }
1783
- // Waiting on one that only looks says nothing either, and on one nobody can read it may.
1784
- for (const [busy, kept] of [["tail -f server.log", false], ["./release.sh", true]]) {
1785
- const cwd = await newRepo("isy-commit-busy-");
1786
- const path = await writeSession(cwd, "sess-busy", "left-pad");
1787
- await appendTurn(path, "sess-busy", shell("t-busy", busy), new Date().toISOString());
1788
- await onBranch(path, "main");
1789
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1790
- await commitFile(cwd, "README.md");
1791
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped === undefined, kept, busy);
1792
- }
1793
- });
1794
- test("a commit passes over a newer session on another branch for the one standing on this one", async () => {
1795
- const cwd = await realpath(await newRepo("isy-commit-standing-"));
1796
- await mkdir(join(cwd, "docs"));
1797
- await run("git", ["switch", "-q", "-c", "feat"], { cwd });
1798
- const standing = await writeSession(cwd, "sess-standing", "left-pad");
1799
- await onBranch(standing, "feat");
1800
- await age(standing, 1 / 1440);
1801
- const elsewhere = await writeSession(join(cwd, "docs"), "sess-elsewhere", "ms");
1802
- await onBranch(elsewhere, "main");
1803
- await commitFile(cwd, "README.md");
1804
- assert.equal((await runCommit({ noUpload: true }, cwd)).sessionId, "sess-standing");
1805
- assert.equal((await readState())[elsewhere]?.commits, undefined);
1806
- });
1807
- test("a tag named like the branch does not change the branch's name", async () => {
1808
- const cwd = await newRepo("isy-commit-tagged-");
1809
- await run("git", ["switch", "-q", "-c", "release"], { cwd });
1810
- await run("git", ["tag", "release"], { cwd });
1811
- const path = await writeSession(cwd, "sess-tagged", "left-pad");
1812
- await onBranch(path, "release");
1813
- assert.equal(await currentBranch(cwd), "release");
1814
- await commitFile(cwd, "human.txt");
1815
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1816
- assert.equal((await readState())[path].branch, "release");
1817
- const metadata = await collectGitMetadata(cwd);
1818
- assert.ok(metadata.ok);
1819
- assert.equal(metadata.metadata.branch, "release");
1820
- });
1821
- test("a redaction pattern that matches a name does not hide the session from the commit", async () => {
1822
- await writeConfig({ token: "t", apiBaseUrl: baseUrl, extraRedactPatterns: ["acme"] });
1823
- // Standing on a branch the pattern matches, with nothing else to go on.
1824
- const idle = await newRepo("isy-commit-pattern-idle-");
1825
- await run("git", ["switch", "-q", "-c", "fix/acme-login"], { cwd: idle });
1826
- const idlePath = await writeSession(idle, "sess-idle", "left-pad");
1827
- await onBranch(idlePath, "fix/acme-login");
1828
- await commitFile(idle, "human.txt");
1829
- assert.equal((await runCommit({ noUpload: true }, idle)).skipped, undefined);
1830
- assert.equal((await readState())[idlePath].branch, "fix/acme-login");
1831
- // A row an upload stamped holds the name as it went up.
1832
- await writeFile(sessionStatePath(), `${JSON.stringify({ [idlePath]: { branch: "fix/[ISY_REDACTED:custom]-login" } })}\n`);
1833
- await commitFile(idle, "human-again.txt");
1834
- assert.equal((await runCommit({ noUpload: true }, idle)).skipped, undefined);
1835
- // An edit to a file the pattern matches, committed on a new branch by a human.
1836
- const edited = await newRepo("isy-commit-pattern-edit-");
1837
- const editedPath = await writeSession(edited, "sess-edited", "left-pad");
1838
- await appendTurn(editedPath, "sess-edited", {
1839
- type: "tool_use", id: "t-edit", name: "Edit",
1840
- input: { file_path: join(edited, "src/acme/x.ts"), old_string: "a", new_string: "b" },
1841
- }, OLD);
1842
- await onBranch(editedPath, "main");
1843
- await run("git", ["switch", "-q", "-c", "feat"], { cwd: edited });
1844
- await commitFile(edited, "src/acme/x.ts");
1845
- assert.equal((await runCommit({ noUpload: true }, edited)).skipped, undefined);
1846
- // And one made through the shell.
1847
- const shelled = await newRepo("isy-commit-pattern-shell-");
1848
- const shelledPath = await writeSession(shelled, "sess-shelled", "acme-lib");
1849
- await appendTurn(shelledPath, "sess-shelled", shell("t-sed", "sed -i 's/a/b/' acme.txt"), OLD);
1850
- await onBranch(shelledPath, "main");
1851
- await run("git", ["switch", "-q", "-c", "feat"], { cwd: shelled });
1852
- await commitFile(shelled, "acme.txt");
1853
- const report = await runCommit({ noUpload: true }, shelled);
1854
- assert.equal(report.skipped, undefined);
1855
- // Telling sessions apart saw the names, and what it reports went through the pattern.
1856
- assert.ok(report.candidates.length > 0);
1857
- assert.ok(!JSON.stringify(report.candidates).includes("acme"));
1858
- });
1859
- test("a rebase counts as the branch it rebases, not as no branch at all", async () => {
1860
- const cwd = await newRepo("isy-commit-rebase-");
1861
- await run("git", ["switch", "-q", "-c", "topic"], { cwd });
1862
- await commitFile(cwd, "a.txt", "topic's");
1863
- await run("git", ["switch", "-q", "main"], { cwd });
1864
- await commitFile(cwd, "a.txt", "main's");
1865
- await run("git", ["switch", "-q", "topic"], { cwd });
1866
- // Stops on the conflict: detached, the way every pick of a rebase fires the hook.
1867
- await assert.rejects(() => run("git", ["rebase", "main"], { cwd }));
1868
- assert.equal(await currentBranch(cwd), "topic");
1869
- const path = await writeSession(cwd, "sess-rebase", "left-pad");
1870
- await onBranch(path, "other");
1871
- assert.match((await runCommit({ noUpload: true }, cwd)).skipped ?? "", /another branch/);
1872
- await onBranch(path, "topic");
1873
- assert.equal((await runCommit({ noUpload: true }, cwd)).skipped, undefined);
1874
- assert.equal((await readState())[path].branch, "topic");
1875
- });
1876
- test("a name is read out of a command as a whole word, however it is quoted or spelled", () => {
1877
- const words = wordsOf({ command: `cat > "docs/notes.md" <<'EOF'\nhello\nEOF\nprintf x>>src\\a.ts` });
1878
- for (const name of ["notes.md", "a.ts", "hello", "EOF"])
1879
- assert.ok(words.includes(name), name);
1880
- assert.ok(!words.includes("a"));
1881
- assert.ok(!words.includes("otes.md"));
1882
- });
1883
- test("reading the names out of a command stays linear", () => {
1884
- for (const text of [
1885
- "\\".repeat(200_000),
1886
- "/".repeat(200_000),
1887
- "\"".repeat(200_000),
1888
- "a/".repeat(100_000),
1889
- "a".repeat(200_000),
1890
- "\\ ".repeat(100_000),
1891
- ]) {
1892
- const started = performance.now();
1893
- wordsOf({ command: text });
1894
- const took = performance.now() - started;
1895
- assert.ok(took < 1000, `took ${Math.round(took)} ms`);
1896
- }
1897
- });
1898
- //# sourceMappingURL=commit.test.js.map