@nightloom/isy 1.0.6-next.0 → 1.0.7-next.0

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,1304 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { parseLines } from "../parser.js";
4
- import { LOG_ONLY_CATCHES, SUPPRESSORS, addedDependencies, detectAbandonedApproach, detectDestructiveCommand, detectErrorSuppressed, detectStaleContext, detectTestModifiedToPass, detectUnverifiedAssumption, detectUnverifiedFix, installedPackages, isTestFile, weakensTest, } from "../detectors.js";
5
- import { MIN_TOOL_USES, eligibility, runStage0 } from "../stage0.js";
6
- let clock = 0;
7
- function nextTimestamp() {
8
- clock += 1;
9
- return new Date(Date.UTC(2026, 7, 16, 9, clock)).toISOString();
10
- }
11
- function assistant(uuid, parentUuid, content) {
12
- return {
13
- type: "assistant",
14
- uuid,
15
- parentUuid,
16
- sessionId: "s1",
17
- timestamp: nextTimestamp(),
18
- message: { role: "assistant", content },
19
- };
20
- }
21
- function result(uuid, parentUuid, toolUseId, extra) {
22
- return {
23
- type: "user",
24
- uuid,
25
- parentUuid,
26
- sessionId: "s1",
27
- timestamp: nextTimestamp(),
28
- message: { role: "user", content: [{ type: "tool_result", tool_use_id: toolUseId, content: "ok" }] },
29
- ...extra,
30
- };
31
- }
32
- function build(records) {
33
- clock = 0;
34
- return parseLines(records.map((record) => JSON.stringify(record)));
35
- }
36
- /** A path-less Glob: a tool call that reads no file and trips no detector. */
37
- function lookup(i) {
38
- return { type: "tool_use", id: `pad-t${i}`, name: "Glob", input: { pattern: "*" } };
39
- }
40
- /** Twenty records ending in `pad19`, the first of them tool calls enough to pass the length gate. */
41
- function padded(records) {
42
- const filler = [];
43
- for (let i = 0; i < 20; i += 1) {
44
- const content = i < MIN_TOOL_USES ? [lookup(i)] : [{ type: "text", text: "x" }];
45
- filler.push(assistant(`pad${i}`, i === 0 ? null : `pad${i - 1}`, content));
46
- }
47
- return build([...filler, ...records]);
48
- }
49
- test("rejects a session with too few tool calls", () => {
50
- const session = build([
51
- assistant("a", null, [
52
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "x", new_string: "y" } },
53
- ]),
54
- ]);
55
- assert.equal(eligibility(session), "too-short");
56
- assert.deepEqual(runStage0(session).candidates, []);
57
- });
58
- test("counts length in tool calls, so a few records holding several calls pass", () => {
59
- // Codex packs several calls into one harness script: few records, real work.
60
- const session = build([
61
- assistant("a", null, [
62
- lookup(0),
63
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "x", new_string: "y" } },
64
- { type: "tool_use", id: "t2", name: "Bash", input: { command: "npm test" } },
65
- ]),
66
- ]);
67
- assert.equal(eligibility(session), undefined);
68
- });
69
- test("a short session that never edited is told apart from a short one that did", () => {
70
- const session = build([assistant("a", null, [lookup(0)])]);
71
- assert.equal(eligibility(session), "no-file-edits");
72
- });
73
- test("rejects a long session that never edited a file", () => {
74
- const session = padded([]);
75
- assert.equal(eligibility(session), "no-file-edits");
76
- assert.equal(runStage0(session).eligible, false);
77
- });
78
- test("accepts a long session that edited a file", () => {
79
- const session = padded([
80
- assistant("e", "pad19", [
81
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "x", new_string: "y" } },
82
- ]),
83
- ]);
84
- assert.equal(eligibility(session), undefined);
85
- assert.equal(runStage0(session).eligible, true);
86
- });
87
- test("a session with too many edits to hold up to its calls finds nothing, and is not rejected for it", () => {
88
- const session = (edits) => parseLines([JSON.stringify(assistant("u0", null, [
89
- ...Array.from({ length: edits }, (_, index) => ({ type: "tool_use", id: `e${index}`, name: "Edit", input: { file_path: "a.ts", old_string: "a", new_string: `${index}` } })),
90
- { type: "tool_use", id: "t0", name: "Bash", input: { command: "git reset --hard" } },
91
- { type: "tool_use", id: "t1", name: "Bash", input: { command: "sed -i x a.ts" } },
92
- ]))]);
93
- assert.ok(runStage0(session(1000)).candidates.length > 0);
94
- const result = runStage0(session(20_000));
95
- assert.equal(result.eligible, true);
96
- assert.equal(result.reason, undefined);
97
- assert.deepEqual(result.candidates, []);
98
- });
99
- test("a long session with few edits keeps its findings, as nothing it holds multiplies", () => {
100
- const session = parseLines([JSON.stringify(assistant("u0", null, [
101
- ...Array.from({ length: 30_000 }, (_, index) => ({ type: "tool_use", id: `r${index}`, name: "Read", input: { file_path: `src/${index}.ts` } })),
102
- { type: "tool_use", id: "t0", name: "Bash", input: { command: "git reset --hard" } },
103
- { type: "tool_use", id: "t1", name: "Bash", input: { command: "sed -i x a.ts" } },
104
- ]))]);
105
- assert.ok(runStage0(session).candidates.length > 0);
106
- });
107
- test("recognises test files by path and by name", () => {
108
- for (const path of [
109
- "/r/tests/thing.py",
110
- "/r/__tests__/thing.js",
111
- "/r/src/payments.test.ts",
112
- "/r/src/payments_spec.rs",
113
- "/r/Thing.spec.java",
114
- ]) {
115
- assert.equal(isTestFile(path), true, path);
116
- }
117
- assert.equal(isTestFile("/r/TestRounding.java"), true);
118
- assert.equal(isTestFile("/r/test_rounding.py"), true);
119
- for (const path of ["/r/src/payments.ts", "/r/latest/notes.md", "/r/contest.go", "/r/latest.ts", "/r/spectrum.ts"]) {
120
- assert.equal(isTestFile(path), false, path);
121
- }
122
- });
123
- test("spots each way a test can be weakened", () => {
124
- assert.match(weakensTest("expect(a).toBe(1);\nexpect(b).toBe(2);", "expect(a).toBe(1);") ?? "", /assertions removed/);
125
- assert.match(weakensTest("it('works', () => {})", "it.only('works', () => {})") ?? "", /skipped/);
126
- assert.match(weakensTest("def test_a():\n pass\ndef test_b():\n pass", "def test_a():\n pass") ?? "", /deleted/);
127
- assert.equal(weakensTest("expect(a).toBe(1);", "expect(a).toBe(2);"), undefined);
128
- });
129
- test("flags a weakened test and scores it higher after a failing command", () => {
130
- const session = padded([
131
- assistant("b", "pad19", [{ type: "tool_use", id: "t0", name: "Bash", input: { command: "npm test" } }]),
132
- result("br", "b", "t0", { toolUseResult: { stdout: "1 test FAILED", stderr: "" } }),
133
- assistant("c", "br", [
134
- {
135
- type: "tool_use",
136
- id: "t1",
137
- name: "Edit",
138
- input: {
139
- file_path: "/r/tests/rounding.test.ts",
140
- old_string: "expect(cents).toBe(3);\nexpect(rest).toBe(0);",
141
- new_string: "expect(cents).toBe(3);",
142
- },
143
- },
144
- ]),
145
- ]);
146
- const candidates = detectTestModifiedToPass(session);
147
- assert.equal(candidates.length, 1);
148
- assert.equal(candidates[0]?.filePath, "/r/tests/rounding.test.ts");
149
- assert.equal(candidates[0]?.weight, 0.9);
150
- assert.match(candidates[0]?.detail ?? "", /failing command/);
151
- });
152
- test("scores a weakened test lower when nothing failed beforehand", () => {
153
- const session = padded([
154
- assistant("c", "pad19", [
155
- {
156
- type: "tool_use",
157
- id: "t1",
158
- name: "Edit",
159
- input: {
160
- file_path: "/r/tests/rounding.test.ts",
161
- old_string: "expect(a).toBe(1);\nexpect(b).toBe(2);",
162
- new_string: "expect(a).toBe(1);",
163
- },
164
- },
165
- ]),
166
- ]);
167
- const candidates = detectTestModifiedToPass(session);
168
- assert.equal(candidates.length, 1);
169
- assert.equal(candidates[0]?.weight, 0.6);
170
- });
171
- test("flags an assertion deleted outright, with an empty replacement", () => {
172
- // Claude Code deletes lines with `new_string: ""`; read as no replacement at
173
- // all, the most direct weakening of a test was the one never flagged.
174
- const session = padded([
175
- assistant("b", "pad19", [{ type: "tool_use", id: "t0", name: "Bash", input: { command: "npm test" } }]),
176
- result("br", "b", "t0", { toolUseResult: { stdout: "1 test FAILED", stderr: "" } }),
177
- assistant("c", "br", [
178
- {
179
- type: "tool_use",
180
- id: "t1",
181
- name: "Edit",
182
- input: { file_path: "/r/test/money.test.js", old_string: "assert.equal(split(5, 2), [3, 2]);\n", new_string: "" },
183
- },
184
- ]),
185
- ]);
186
- const candidates = detectTestModifiedToPass(session);
187
- assert.equal(candidates.length, 1);
188
- assert.equal(candidates[0]?.weight, 0.9);
189
- });
190
- test("ignores a strengthened test and a non-test file", () => {
191
- const session = padded([
192
- assistant("c", "pad19", [
193
- {
194
- type: "tool_use",
195
- id: "t1",
196
- name: "Edit",
197
- input: {
198
- file_path: "/r/tests/rounding.test.ts",
199
- old_string: "expect(a).toBe(1);",
200
- new_string: "expect(a).toBe(1);\nexpect(b).toBe(2);",
201
- },
202
- },
203
- {
204
- type: "tool_use",
205
- id: "t2",
206
- name: "Edit",
207
- input: { file_path: "/r/src/app.ts", old_string: "expect(a);\nexpect(b);", new_string: "expect(a);" },
208
- },
209
- ]),
210
- ]);
211
- assert.deepEqual(detectTestModifiedToPass(session), []);
212
- });
213
- test("notices a file rebuilt back to content it already had", () => {
214
- const session = padded([
215
- assistant("r", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
216
- result("rr", "r", "t0", {
217
- toolUseResult: { type: "text", file: { filePath: "/r/a.ts", content: "const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 } },
218
- }),
219
- assistant("e1", "rr", [
220
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 1;", new_string: "const a = 2;" } },
221
- ]),
222
- assistant("e2", "e1", [
223
- { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 2;", new_string: "const a = 1;" } },
224
- ]),
225
- ]);
226
- const candidates = detectAbandonedApproach(session);
227
- assert.ok(candidates.length >= 1);
228
- assert.equal(candidates[0]?.category, "abandoned_approach");
229
- assert.equal(candidates[0]?.filePath, "/r/a.ts");
230
- assert.ok(candidates.some((candidate) => candidate.weight >= 0.7));
231
- });
232
- test("does not flag a file that only moves forward", () => {
233
- const session = padded([
234
- assistant("r", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
235
- result("rr", "r", "t0", {
236
- toolUseResult: { file: { filePath: "/r/a.ts", content: "const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 } },
237
- }),
238
- assistant("e1", "rr", [
239
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 1;", new_string: "const a = 2;" } },
240
- ]),
241
- assistant("e2", "e1", [
242
- { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 2;", new_string: "const a = 3;" } },
243
- ]),
244
- ]);
245
- assert.deepEqual(detectAbandonedApproach(session), []);
246
- });
247
- test("does not treat re-reading a file after editing it as a revert", () => {
248
- const session = padded([
249
- assistant("r1", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
250
- result("rr1", "r1", "t0", {
251
- toolUseResult: { file: { filePath: "/r/a.ts", content: "const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 } },
252
- }),
253
- assistant("e1", "rr1", [
254
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 1;", new_string: "const a = 2;" } },
255
- ]),
256
- assistant("r2", "e1", [{ type: "tool_use", id: "t2", name: "Read", input: { file_path: "/r/a.ts" } }]),
257
- result("rr2", "r2", "t2", {
258
- toolUseResult: { file: { filePath: "/r/a.ts", content: "const a = 2;\n", numLines: 1, startLine: 1, totalLines: 1 } },
259
- }),
260
- ]);
261
- assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("returned to a state")), []);
262
- });
263
- test("treats a repeated read with no edits between as unremarkable", () => {
264
- const session = padded([
265
- assistant("r1", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
266
- result("rr1", "r1", "t0", {
267
- toolUseResult: { file: { filePath: "/r/a.ts", content: "same\n", numLines: 1, startLine: 1, totalLines: 1 } },
268
- }),
269
- assistant("r2", "rr1", [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: "/r/a.ts" } }]),
270
- result("rr2", "r2", "t1", {
271
- toolUseResult: { file: { filePath: "/r/a.ts", content: "same\n", numLines: 1, startLine: 1, totalLines: 1 } },
272
- }),
273
- assistant("e", "rr2", [
274
- { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/b.ts", old_string: "x", new_string: "y" } },
275
- ]),
276
- ]);
277
- assert.deepEqual(detectAbandonedApproach(session), []);
278
- });
279
- test("a file of padding or blank lines is compared in linear time", () => {
280
- for (const content of [`${" ".repeat(200_000)}x`, `${"\n".repeat(200_000)}x`]) {
281
- const session = padded([
282
- assistant("w", "pad19", [{ type: "tool_use", id: "t1", name: "Write", input: { file_path: "/r/a.txt", content } }]),
283
- ]);
284
- const started = performance.now();
285
- detectAbandonedApproach(session);
286
- const took = performance.now() - started;
287
- assert.ok(took < 1000, `took ${Math.round(took)}ms on ${JSON.stringify(content.slice(0, 2))}…`);
288
- }
289
- });
290
- test("flags edits thrown away by a git command", () => {
291
- for (const command of ["git checkout -- src/app.ts", "git restore src/app.ts", "git reset --hard", "git stash"]) {
292
- const session = padded([
293
- assistant("e", "pad19", [
294
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
295
- ]),
296
- assistant("g", "e", [{ type: "tool_use", id: "t2", name: "Bash", input: { command } }]),
297
- ]);
298
- const candidates = detectAbandonedApproach(session).filter((c) => c.detail.includes("git command"));
299
- assert.equal(candidates.length, 1, command);
300
- assert.equal(candidates[0]?.filePath, "/r/src/app.ts");
301
- }
302
- });
303
- test("does not treat restoring a stash as discarding work", () => {
304
- const session = padded([
305
- assistant("e", "pad19", [
306
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
307
- ]),
308
- assistant("g", "e", [{ type: "tool_use", id: "t2", name: "Bash", input: { command: "git stash pop" } }]),
309
- ]);
310
- assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("git command")), []);
311
- });
312
- test("ignores a git revert that happened before the file was touched", () => {
313
- const session = padded([
314
- assistant("g", "pad19", [{ type: "tool_use", id: "t1", name: "Bash", input: { command: "git reset --hard" } }]),
315
- assistant("e", "g", [
316
- { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
317
- ]),
318
- ]);
319
- assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("git command")), []);
320
- });
321
- test("flags edits on a branch the session walked away from", () => {
322
- const session = padded([
323
- assistant("dead", "pad19", [
324
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/tried.ts", old_string: "x", new_string: "y" } },
325
- ]),
326
- assistant("live", "pad19", [{ type: "text", text: "different approach" }]),
327
- assistant("tip", "live", [{ type: "text", text: "done" }]),
328
- ]);
329
- const candidates = detectAbandonedApproach(session).filter((c) => c.detail.includes("branch"));
330
- assert.equal(candidates.length, 1);
331
- assert.equal(candidates[0]?.filePath, "/r/src/tried.ts");
332
- assert.equal(candidates[0]?.weight, 0.4);
333
- });
334
- test("does not call a truncated history an abandoned branch", () => {
335
- const session = padded([
336
- assistant("orphan", "missing-from-this-file", [
337
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/kept.ts", old_string: "x", new_string: "y" } },
338
- ]),
339
- assistant("tip", "pad19", [{ type: "text", text: "done" }]),
340
- ]);
341
- assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("branch")), []);
342
- });
343
- test("stage 0 returns both categories sorted by position in the session", () => {
344
- const session = padded([
345
- assistant("e1", "pad19", [
346
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
347
- ]),
348
- assistant("g", "e1", [{ type: "tool_use", id: "t2", name: "Bash", input: { command: "git checkout -- src/app.ts" } }]),
349
- assistant("e2", "g", [
350
- {
351
- type: "tool_use",
352
- id: "t3",
353
- name: "Edit",
354
- input: {
355
- file_path: "/r/tests/app.test.ts",
356
- old_string: "expect(a).toBe(1);\nexpect(b).toBe(2);",
357
- new_string: "expect(a).toBe(1);",
358
- },
359
- },
360
- ]),
361
- ]);
362
- const result = runStage0(session);
363
- assert.equal(result.eligible, true);
364
- const categories = new Set(result.candidates.map((candidate) => candidate.category));
365
- assert.ok(categories.has("abandoned_approach"));
366
- assert.ok(categories.has("test_modified_to_pass"));
367
- const indices = result.candidates.map((candidate) => candidate.recordIndex);
368
- assert.deepEqual(indices, [...indices].sort((a, b) => a - b));
369
- });
370
- test("does not read an unchanged rewrite as a return to an earlier state", () => {
371
- const session = padded([
372
- assistant("w1", "pad19", [
373
- { type: "tool_use", id: "t0", name: "Write", input: { file_path: "/r/hook.ts", content: "export const a = 1;\n" } },
374
- ]),
375
- assistant("r", "w1", [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: "/r/hook.ts" } }]),
376
- result("rr", "r", "t1", {
377
- toolUseResult: {
378
- file: { filePath: "/r/hook.ts", content: "export const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 },
379
- },
380
- }),
381
- assistant("w2", "rr", [
382
- { type: "tool_use", id: "t2", name: "Write", input: { file_path: "/r/hook.ts", content: "export const a = 1;\n" } },
383
- ]),
384
- ]);
385
- assert.deepEqual(detectAbandonedApproach(session), []);
386
- });
387
- test("collapses a git revert that discards many files into one candidate", () => {
388
- const session = padded([
389
- assistant("e", "pad19", [
390
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "a", new_string: "b" } },
391
- { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/b.ts", old_string: "a", new_string: "b" } },
392
- { type: "tool_use", id: "t3", name: "Edit", input: { file_path: "/r/c.ts", old_string: "a", new_string: "b" } },
393
- ]),
394
- assistant("g", "e", [
395
- { type: "tool_use", id: "t4", name: "Bash", input: { command: "git reset --hard origin/main" } },
396
- ]),
397
- ]);
398
- const candidates = detectAbandonedApproach(session);
399
- assert.equal(candidates.length, 1);
400
- assert.equal(candidates[0]?.filePath, undefined);
401
- assert.match(candidates[0]?.detail ?? "", /edits to 3 files were discarded .*a\.ts, b\.ts, c\.ts/);
402
- });
403
- test("keeps the file on a git revert that discards a single file", () => {
404
- const session = padded([
405
- assistant("e", "pad19", [
406
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "a", new_string: "b" } },
407
- ]),
408
- assistant("g", "e", [
409
- { type: "tool_use", id: "t2", name: "Bash", input: { command: "git checkout -- a.ts" } },
410
- ]),
411
- ]);
412
- const candidates = detectAbandonedApproach(session);
413
- assert.equal(candidates.length, 1);
414
- assert.equal(candidates[0]?.filePath, "/r/a.ts");
415
- });
416
- test("counts a shell read as having looked at the file", () => {
417
- const blind = padded([
418
- assistant("e", "pad19", [
419
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/pricing.ts", old_string: "a", new_string: "b" } },
420
- ]),
421
- ]);
422
- assert.equal(detectUnverifiedAssumption(blind).length, 1);
423
- const inspected = padded([
424
- assistant("s", "pad19", [
425
- { type: "tool_use", id: "t0", name: "Bash", input: { command: "sed -n '1,40p' src/pricing.ts" } },
426
- ]),
427
- assistant("e", "s", [
428
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/pricing.ts", old_string: "a", new_string: "b" } },
429
- ]),
430
- ]);
431
- assert.deepEqual(detectUnverifiedAssumption(inspected), []);
432
- });
433
- /** One Bash call, with the cwd a real transcript carries on every record. */
434
- function ran(uuid, parentUuid, command, cwd = "/repo") {
435
- return {
436
- type: "assistant",
437
- uuid,
438
- parentUuid,
439
- sessionId: "s1",
440
- cwd,
441
- timestamp: nextTimestamp(),
442
- message: {
443
- role: "assistant",
444
- content: [{ type: "tool_use", id: `bash-${uuid}`, name: "Bash", input: { command } }],
445
- },
446
- };
447
- }
448
- function edited(uuid, parentUuid, filePath, newString = "b") {
449
- return assistant(uuid, parentUuid, [
450
- {
451
- type: "tool_use",
452
- id: `edit-${uuid}`,
453
- name: "Edit",
454
- input: { file_path: filePath, old_string: "a", new_string: newString },
455
- },
456
- ]);
457
- }
458
- test("a file read through a glob counts as read", () => {
459
- // `cat src/*.js` read src/server.js; a candidate here was a false "never read".
460
- const session = padded([
461
- ran("s", "pad19", "cat src/*.js test/*.js README.md"),
462
- edited("e", "s", "/repo/src/server.js"),
463
- ]);
464
- assert.deepEqual(detectUnverifiedAssumption(session), []);
465
- const elsewhere = padded([ran("s", "pad19", "cat lib/*.js"), edited("e", "s", "/repo/src/server.js")]);
466
- assert.equal(detectUnverifiedAssumption(elsewhere).length, 1);
467
- });
468
- test("an edit through sed -i is an edit to the detectors", () => {
469
- // Never read, edited in place: the same assumption an Edit would make.
470
- const blind = padded([ran("s", "pad19", "sed -i 's/2.34/2.35/' README.md")]);
471
- const found = detectUnverifiedAssumption(blind);
472
- assert.equal(found.length, 1);
473
- assert.equal(found[0]?.filePath, "/repo/README.md");
474
- // A test rewritten in place right after a failure: what changed is the model's to read.
475
- const afterFailure = padded([
476
- ran("t", "pad19", "npm test"),
477
- result("tr", "t", "bash-t", { toolUseResult: { stdout: "1 test FAILED", stderr: "" } }),
478
- ran("s", "tr", "sed -i 's/\\[0.02, 0.03\\]/[0.03, 0.02]/' test/money.test.js"),
479
- ]);
480
- const weakened = detectTestModifiedToPass(afterFailure);
481
- assert.equal(weakened.length, 1);
482
- assert.equal(weakened[0]?.filePath, "/repo/test/money.test.js");
483
- assert.equal(weakened[0]?.weight, 0.5);
484
- // The same edit with nothing failing before it is no signal.
485
- const calm = padded([ran("s", "pad19", "sed -i 's/a/b/' test/money.test.js")]);
486
- assert.deepEqual(detectTestModifiedToPass(calm), []);
487
- });
488
- test("code edited after the last test run is one note, not one per file", () => {
489
- const session = padded([
490
- edited("e1", "pad19", "/repo/src/a.ts"),
491
- ran("v", "e1", "npm test"),
492
- edited("e2", "v", "/repo/src/b.ts"),
493
- edited("e3", "e2", "/repo/src/c.ts"),
494
- ]);
495
- const found = detectUnverifiedFix(session);
496
- assert.equal(found.length, 1, "one session, one note");
497
- assert.match(found[0].detail, /after the last test or build run/);
498
- assert.match(found[0].detail, /b\.ts, c\.ts/);
499
- // Anchored at the last unchecked edit, which is where the session stopped looking.
500
- assert.equal(found[0].toolUseId, "edit-e3");
501
- assert.equal(found[0].weight, 0.6);
502
- });
503
- test("a session that verified after its last edit is not flagged", () => {
504
- const session = padded([edited("e1", "pad19", "/repo/src/a.ts"), ran("v", "e1", "cargo test")]);
505
- assert.deepEqual(detectUnverifiedFix(session), []);
506
- });
507
- test("a finding about a file outside both the repository and the home is dropped", () => {
508
- // One edit after the last test run: an unverified_fix that names its file.
509
- const files = (cwd, filePath) => {
510
- const run = cwd === undefined
511
- ? assistant("v", "pad19", [{ type: "tool_use", id: "bash-v", name: "Bash", input: { command: "npm test" } }])
512
- : ran("v", "pad19", "npm test", cwd);
513
- return runStage0(padded([run, edited("e1", "v", filePath)])).candidates.map((found) => found.filePath);
514
- };
515
- // On the machine, before masking.
516
- assert.ok(!files("/home/alice/repo", "/tmp/reset.py").includes("/tmp/reset.py"));
517
- assert.ok(files("/home/alice/repo", "/home/alice/repo/src/a.py").includes("/home/alice/repo/src/a.py"));
518
- // A monorepo session started in a subdirectory still edits its siblings.
519
- assert.ok(files("/home/alice/repo/client", "/home/alice/repo/server/a.py").includes("/home/alice/repo/server/a.py"));
520
- // On the server the transcript arrives masked, its cwd along with it.
521
- assert.ok(!files(".", "/tmp/reset.py").includes("/tmp/reset.py"));
522
- assert.ok(files(".", "src/a.py").includes("src/a.py"));
523
- assert.ok(files(".", "~/repo/server/a.py").includes("~/repo/server/a.py"));
524
- // Nothing to judge against: kept.
525
- assert.ok(files(undefined, "/tmp/reset.py").includes("/tmp/reset.py"));
526
- });
527
- test("a session that never ran anything says so, and ignores tests and prose", () => {
528
- const session = padded([
529
- edited("e1", "pad19", "/repo/src/a.ts"),
530
- edited("e2", "e1", "/repo/tests/test_a.py"),
531
- edited("e3", "e2", "/repo/README.md"),
532
- ran("g", "e3", "git status"),
533
- ]);
534
- const found = detectUnverifiedFix(session);
535
- assert.equal(found.length, 1);
536
- assert.match(found[0].detail, /no test, build or lint command ran/);
537
- assert.match(found[0].detail, /a\.ts/);
538
- assert.ok(!found[0].detail.includes("README"), "prose is not code");
539
- assert.ok(!found[0].detail.includes("test_a"), "a test file has its own category");
540
- });
541
- test("a suppression added to production code is a signal, and one already there is not", () => {
542
- const added = padded([
543
- assistant("e", "pad19", [
544
- {
545
- type: "tool_use",
546
- id: "t1",
547
- name: "Edit",
548
- input: {
549
- file_path: "/repo/src/pay.ts",
550
- old_string: "const id = charge.ref;",
551
- new_string: "// @ts-ignore\nconst id = charge.ref;",
552
- },
553
- },
554
- ]),
555
- ]);
556
- const found = detectErrorSuppressed(added);
557
- assert.equal(found.length, 1);
558
- assert.match(found[0].detail, /TypeScript error was suppressed/);
559
- assert.equal(found[0].weight, 0.45, "no failing command before it");
560
- const untouched = padded([
561
- assistant("e", "pad19", [
562
- {
563
- type: "tool_use",
564
- id: "t1",
565
- name: "Edit",
566
- input: {
567
- file_path: "/repo/src/pay.ts",
568
- old_string: "// @ts-ignore\nconst id = charge.ref;",
569
- new_string: "// @ts-ignore\nconst id = charge.reference;",
570
- },
571
- },
572
- ]),
573
- ]);
574
- assert.deepEqual(detectErrorSuppressed(untouched), []);
575
- });
576
- test("a check whose failure is thrown away is a signal, a cleanup line is not", () => {
577
- const discarded = padded([
578
- edited("e1", "pad19", "/repo/src/a.ts"),
579
- ran("b", "e1", "npm test || true"),
580
- ]);
581
- assert.equal(detectErrorSuppressed(discarded).length, 1);
582
- const cleanup = padded([edited("e1", "pad19", "/repo/src/a.ts"), ran("b", "e1", "pkill node || true")]);
583
- assert.deepEqual(detectErrorSuppressed(cleanup), []);
584
- });
585
- /** `content` written over a file, right after a command that failed or, with `failing` off, passed. */
586
- /** A command's result, with the exit status the transcript records as `is_error`. */
587
- function ranAndGot(command, stdout, isError) {
588
- return [
589
- ran("b", "pad19", command),
590
- result("br", "b", "bash-b", {
591
- message: { role: "user", content: [{ type: "tool_result", tool_use_id: "bash-b", content: stdout, is_error: isError }] },
592
- toolUseResult: { stdout, stderr: "" },
593
- }),
594
- ];
595
- }
596
- function writtenAfterCheck(content, failing = true, filePath = "/repo/src/config.js") {
597
- return padded([
598
- ...ranAndGot("npm run check", failing ? "check FAILED" : "all good", failing),
599
- assistant("w", "br", [{ type: "tool_use", id: "t1", name: "Write", input: { file_path: filePath, content } }]),
600
- ]);
601
- }
602
- test("a catch left only logging right after a failing command is a signal", () => {
603
- const swallowed = [
604
- // What gets a release gate green over a broken config.
605
- `export function loadConfig(path) {
606
- try {
607
- return JSON.parse(readFileSync(path, "utf8"));
608
- } catch (err) {
609
- // TEMP: the file is truncated upstream.
610
- console.warn(\`WARNING: cannot load \${path}: \${err.message}\`);
611
- return null;
612
- }
613
- }`,
614
- "try { x(); } catch { logger.error('no'); }",
615
- "try { x(); } catch (e) {\n console.error(\n `long message ${e.message}`,\n e,\n );\n return [];\n}",
616
- "try { x(); } catch (IOException e) {\r\n log.warn(\"x\", e);\r\n return;\r\n}",
617
- ];
618
- for (const content of swallowed) {
619
- const found = detectErrorSuppressed(writtenAfterCheck(content));
620
- assert.equal(found.length, 1, content);
621
- assert.equal(found[0].detail, "an exception is caught and only logged, shortly after a failing command");
622
- assert.equal(found[0].weight, 0.7);
623
- }
624
- const python = "try:\n x()\nexcept ValueError as e:\n # note\n logger.warning('x %s', e)\n return None\n";
625
- assert.equal(detectErrorSuppressed(writtenAfterCheck(python, true, "/repo/src/config.py")).length, 1);
626
- });
627
- test("a catch that only logs is everyday code with no failure before it", () => {
628
- const content = "try { x(); } catch (e) { console.warn(e); return null; }";
629
- assert.deepEqual(detectErrorSuppressed(writtenAfterCheck(content, false)), []);
630
- });
631
- test("a catch that does something with the failure is no signal, even right after one", () => {
632
- const handled = [
633
- "try { x(); } catch (e) { console.warn(e); throw e; }",
634
- "try { x(); } catch (e) { console.error(e); process.exit(1); }",
635
- "try { x(); } catch (e) { console.warn(e); doMore(); return null; }",
636
- "try { x(); } catch (e) { console.warn(e)\n doMore()\n}",
637
- "try { x(); } catch (e) { logger.error(e); return { ok: false, error: e }; }",
638
- "try { x(); } catch { return undefined; }",
639
- "promise.catch(report);",
640
- ];
641
- for (const content of handled)
642
- assert.deepEqual(detectErrorSuppressed(writtenAfterCheck(content)), [], content);
643
- for (const content of [
644
- "except ValueError as e:\n logger.warning(e)\n raise\n",
645
- "except ValueError as e:\n logger.warning(e)\n return compute()\n",
646
- "except ValueError as e:\n logger.warning(e)\n retry()\n return None\n",
647
- "except ValueError:\n return None\n",
648
- ]) {
649
- assert.deepEqual(detectErrorSuppressed(writtenAfterCheck(content, true, "/repo/src/config.py")), [], content);
650
- }
651
- // Already there before the edit: not what this edit did about the failure.
652
- const kept = "try { x(); } catch (e) { console.warn(e); return null; }";
653
- const session = padded([
654
- ...ranAndGot("npm run check", "check FAILED", true),
655
- assistant("e", "br", [
656
- { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/repo/src/a.js", old_string: `${kept}\nold();`, new_string: `${kept}\nnew();` } },
657
- ]),
658
- ]);
659
- assert.deepEqual(detectErrorSuppressed(session), []);
660
- // A test file has its own category.
661
- assert.deepEqual(detectErrorSuppressed(writtenAfterCheck(kept, true, "/repo/src/config.test.js")), []);
662
- });
663
- test("a catch that only logs counts after a check failed, not after a word or a search that found nothing", () => {
664
- const content = "try { x(); } catch (e) { console.warn(e); return null; }";
665
- const written = (command, stdout, isError) => padded([
666
- ...ranAndGot(command, stdout, isError),
667
- assistant("w", "br", [{ type: "tool_use", id: "t1", name: "Write", input: { file_path: "/repo/src/a.js", content } }]),
668
- ]);
669
- for (const [command, stdout, isError] of [
670
- ["cargo test", "test result: ok. 12 passed; 0 failed; 0 ignored", false],
671
- ["pytest -q", "12 passed, 0 failed in 0.3s", false],
672
- ["sed -n 1,40p src/upload.ts", 'if (!ok) console.error("upload failed");', false],
673
- ["git log --oneline -5", "abc123 fix failed retry in the queue", false],
674
- ["grep -rq TODO src", "", true],
675
- ["grep -rq 'npm test' .github", "", true],
676
- ["ls missing", "ls: cannot access 'missing'", true],
677
- ["cat <<'EOF' > notes.txt\nnpm test\nEOF\nfalse", "", true],
678
- ]) {
679
- assert.deepEqual(detectErrorSuppressed(written(command, stdout, isError)), [], command);
680
- }
681
- for (const command of [
682
- "cargo test",
683
- "cat package.json && npm test",
684
- "cat <<'EOF' > fixture.json\n{}\nEOF\nnpm run check 2>&1 | tail -5",
685
- ]) {
686
- assert.equal(detectErrorSuppressed(written(command, "", true)).length, 1, command);
687
- }
688
- });
689
- test("a silent catch given a log line is no signal, even right after a failed check", () => {
690
- for (const [file_path, old_string, new_string] of [
691
- ["/repo/src/a.js", "catch (err) {}", "catch (err) { console.error(err); }"],
692
- ["/repo/src/a.py", "except OSError:\n pass\n", "except OSError as e:\n logger.error(e)\n return None\n"],
693
- ]) {
694
- const session = padded([
695
- ...ranAndGot("npm run check", "check FAILED", true),
696
- assistant("e", "br", [{ type: "tool_use", id: "t1", name: "Edit", input: { file_path, old_string, new_string } }]),
697
- ]);
698
- assert.deepEqual(detectErrorSuppressed(session), [], file_path);
699
- }
700
- });
701
- test("a catch that only logs in a document or in a string is no signal, even right after a failed check", () => {
702
- const fenced = "```js\ntry { x(); } catch (e) { console.warn(e); return null; }\n```\n";
703
- assert.deepEqual(detectErrorSuppressed(writtenAfterCheck(fenced, true, "/repo/docs/guide.md")), []);
704
- for (const content of [
705
- `const help = "use try { x() } catch (e) { console.log(e) } here";`,
706
- `const help = 'use try { x() } catch (e) { console.log(e) } here';`,
707
- "const help = `use try { x() } catch (e) { console.log(e) } here`;",
708
- ]) {
709
- assert.deepEqual(detectErrorSuppressed(writtenAfterCheck(content)), [], content);
710
- }
711
- });
712
- test("the rules for a catch that only logs get through 200 KB of text built to stall them", () => {
713
- const size = 200_000;
714
- const fill = (unit) => unit.repeat(Math.ceil(size / unit.length)).slice(0, size);
715
- const shapes = [
716
- `catch${" ".repeat(size)}`,
717
- `catch${"\n".repeat(size)}`,
718
- fill("catch("),
719
- `catch(x)${" ".repeat(size)}`,
720
- `catch{${" ".repeat(size)}`,
721
- `catch{${"//".repeat(size / 2)}`,
722
- `catch{// ${fill("// ")}\n`,
723
- `catch{${fill("// x\n")}`,
724
- fill("catch{//"),
725
- fill("catch { // "),
726
- fill("catch {\n// "),
727
- fill("// } catch {\n// other();\n// }\n"),
728
- fill("catch{console.log("),
729
- `catch{console.log(${"\n".repeat(size)}`,
730
- `catch{console.log(${")".repeat(size)}`,
731
- `catch{console.log(x)${" ".repeat(size)}`,
732
- `catch{console.log(x);return${" ".repeat(size)}`,
733
- `catch{console.log(x);return null${" ".repeat(size)}`,
734
- fill("except:\n"),
735
- `except${" ".repeat(size)}`,
736
- `except:\n${fill(" # x\n")}`,
737
- fill("except:\nprint("),
738
- `except:\nprint(${")".repeat(size)}`,
739
- `except:\nprint(x)\nreturn${" ".repeat(size)}`,
740
- `${" ".repeat(size)}except`,
741
- ];
742
- for (const text of shapes) {
743
- for (const { pattern } of LOG_ONLY_CATCHES) {
744
- const started = performance.now();
745
- text.match(pattern);
746
- const took = performance.now() - started;
747
- assert.ok(took < 1000, `took ${Math.round(took)}ms on ${JSON.stringify(text.slice(0, 24))}…`);
748
- }
749
- }
750
- });
751
- test("the rules for a dropped exception get through 200 KB of text built to stall them", () => {
752
- const size = 200_000;
753
- const fill = (unit) => unit.repeat(Math.ceil(size / unit.length)).slice(0, size);
754
- const shapes = [
755
- `catch${" ".repeat(size)}`,
756
- `catch${"\n".repeat(size)}`,
757
- fill("catch("),
758
- `catch(x)${" ".repeat(size)}`,
759
- `catch{${" ".repeat(size)}`,
760
- fill(`catch${" ".repeat(400)}`),
761
- fill("catch(x"),
762
- fill(`catch(${"x".repeat(1200)}`),
763
- fill("except:# "),
764
- `except:${"\n".repeat(size)}`,
765
- `except:${" ".repeat(size)}`,
766
- `except:\n${"\n".repeat(size)}`,
767
- fill("except x"),
768
- fill("except:#"),
769
- fill(`except:${"\n".repeat(400)}`),
770
- `rescue${"\n".repeat(size)}`,
771
- `rescue${" ".repeat(size)}`,
772
- `rescue =>${" ".repeat(size)}`,
773
- `rescue => a${"\n".repeat(size)}`,
774
- fill("rescue => a"),
775
- fill(`rescue${"\n".repeat(400)}`),
776
- ];
777
- for (const text of shapes) {
778
- for (const { pattern } of SUPPRESSORS) {
779
- const started = performance.now();
780
- text.match(pattern);
781
- const took = performance.now() - started;
782
- assert.ok(took < 1000, `took ${Math.round(took)}ms on ${JSON.stringify(text.slice(0, 24))}…`);
783
- }
784
- }
785
- });
786
- test("the rules for a dropped exception read a long line as well as a short one", () => {
787
- const tuple = Array.from({ length: 25 }, (_, index) => `SomeLongError${index}`).join(", ");
788
- for (const [filePath, content] of [
789
- ["/repo/src/a.py", `try:\n a()\nexcept (${tuple}):\n pass\n`],
790
- ["/repo/src/a.py", `try:\n a()\nexcept OSError: # ${"x".repeat(500)}\n pass\n`],
791
- ["/repo/src/a.ts", `try { a(); } catch (e: ${"T".repeat(300)}) {}`],
792
- ["/repo/src/a.ts", `try { a(); } catch (e: ${"T".repeat(5000)}) {}`],
793
- ]) {
794
- const found = detectErrorSuppressed(writtenAfterCheck(content, false, filePath));
795
- assert.equal(found[0]?.detail, "an exception is caught and dropped", content.slice(0, 40));
796
- }
797
- });
798
- test("a recursive delete is reported, unless the path grows back on its own", () => {
799
- const real = padded([
800
- edited("e1", "pad19", "/repo/src/a.ts"),
801
- ran("b", "e1", "rm -rf src/generated"),
802
- ]);
803
- const found = detectDestructiveCommand(real);
804
- assert.equal(found.length, 1);
805
- assert.match(found[0].detail, /deleted recursively: src\/generated/);
806
- const rebuilt = padded([
807
- edited("e1", "pad19", "/repo/src/a.ts"),
808
- ran("b", "e1", "rm -rf node_modules dist && npm ci"),
809
- ]);
810
- assert.deepEqual(detectDestructiveCommand(rebuilt), []);
811
- });
812
- test("the pattern has to be a command, not text a command carries", () => {
813
- // The corpus is full of both: a grep for the phrase, and a path outside the
814
- // repository that the pull request has no stake in.
815
- const quoted = padded([
816
- edited("e1", "pad19", "/repo/src/a.ts"),
817
- ran("b", "e1", "grep -rn 'rm -rf' scripts/ && echo 'DROP TABLE users'"),
818
- ]);
819
- assert.deepEqual(detectDestructiveCommand(quoted), []);
820
- const elsewhere = padded([
821
- edited("e1", "pad19", "/repo/src/a.ts"),
822
- ran("b", "e1", "rm -rf ~/android"),
823
- ]);
824
- assert.deepEqual(detectDestructiveCommand(elsewhere), []);
825
- });
826
- test("a destructive database command is reported wherever it ran", () => {
827
- const dropped = padded([
828
- edited("e1", "pad19", "/repo/src/a.ts"),
829
- ran("b", "e1", 'psql -c "DROP DATABASE IF EXISTS shop_dev"'),
830
- ran("c", "b", 'sqlite3 app.db "DELETE FROM sessions"'),
831
- ]);
832
- const found = detectDestructiveCommand(dropped);
833
- assert.equal(found.length, 2);
834
- assert.match(found[0].detail, /database object was dropped/);
835
- assert.match(found[1].detail, /no WHERE clause/);
836
- });
837
- test("one git reset is one note, under the category that explains it", () => {
838
- const session = padded([
839
- edited("e1", "pad19", "/repo/src/a.ts"),
840
- ran("b", "e1", "git reset --hard HEAD"),
841
- ]);
842
- const categories = runStage0(session).candidates.map((candidate) => candidate.category);
843
- assert.deepEqual(categories.filter((c) => c === "destructive_command"), []);
844
- assert.ok(categories.includes("abandoned_approach"));
845
- });
846
- /** One Read call, the way the agent looks a file up before touching it. */
847
- function read(uuid, parentUuid, filePath) {
848
- return assistant(uuid, parentUuid, [
849
- { type: "tool_use", id: `read-${uuid}`, name: "Read", input: { file_path: filePath } },
850
- ]);
851
- }
852
- test("an edit written after a long break, on a file read before it, is stale context", () => {
853
- const records = [read("r", "pad19", "/repo/src/api.ts")];
854
- // 45 minutes of nothing. One tick is spent by the record that follows.
855
- clock += 44;
856
- records.push(edited("e", "r", "/repo/src/api.ts"));
857
- const candidates = detectStaleContext(padded(records));
858
- assert.equal(candidates.length, 1);
859
- assert.equal(candidates[0]?.category, "stale_context");
860
- assert.equal(candidates[0]?.filePath, "/repo/src/api.ts");
861
- assert.match(candidates[0].detail, /a 45m break and edited api\.ts/);
862
- });
863
- test("one note per break, however many files the agent went on to edit", () => {
864
- const records = [read("r1", "pad19", "/repo/src/a.ts"), read("r2", "r1", "/repo/src/b.ts")];
865
- clock += 44;
866
- records.push(edited("e1", "r2", "/repo/src/a.ts"));
867
- records.push(edited("e2", "e1", "/repo/src/b.ts"));
868
- assert.equal(detectStaleContext(padded(records)).length, 1);
869
- });
870
- test("many breaks in one session are one note that says how many", () => {
871
- const records = [read("r1", "pad19", "/repo/src/a.ts"), read("r2", "r1", "/repo/src/b.ts")];
872
- clock += 44;
873
- records.push(edited("e1", "r2", "/repo/src/a.ts"));
874
- // A second break, and the agent picks up b.ts on what it read before both.
875
- clock += 90;
876
- records.push(edited("e2", "e1", "/repo/src/b.ts"));
877
- const candidates = detectStaleContext(padded(records));
878
- assert.equal(candidates.length, 1);
879
- // The longest break is the one printed, and both files are named.
880
- assert.match(candidates[0].detail, /a 1h31 break and edited a\.ts, b\.ts without reading them again/);
881
- assert.match(candidates[0].detail, /2 breaks in this session/);
882
- assert.equal(candidates[0]?.filePath, "/repo/src/b.ts");
883
- });
884
- test("a break is only stale context when the agent came back without looking again", () => {
885
- const rechecked = [read("r", "pad19", "/repo/src/api.ts")];
886
- clock += 44;
887
- rechecked.push(read("r2", "r", "/repo/src/api.ts"));
888
- rechecked.push(edited("e", "r2", "/repo/src/api.ts"));
889
- assert.deepEqual(detectStaleContext(padded(rechecked)), []);
890
- // Never read at all is `unverified_assumption`, and one edit must not be two notes.
891
- const blind = [ran("b", "pad19", "npm test")];
892
- clock += 44;
893
- blind.push(edited("e", "b", "/repo/src/api.ts"));
894
- assert.deepEqual(detectStaleContext(padded(blind)), []);
895
- // A few minutes is the developer reading the diff before approving the call.
896
- const brief = [read("r", "pad19", "/repo/src/api.ts")];
897
- clock += 5;
898
- brief.push(edited("e", "r", "/repo/src/api.ts"));
899
- assert.deepEqual(detectStaleContext(padded(brief)), []);
900
- });
901
- test("SQL inside a search pattern or a substitution is text, not a migration", () => {
902
- // Both shapes come straight off the corpus: an alternation in a grep pattern,
903
- // and a grep whose output is an argument to something else.
904
- const alternation = padded([
905
- edited("e1", "pad19", "/repo/src/a.ts"),
906
- ran("b", "e1", 'grep -rn "DELETE FROM|DROP TABLE" src'),
907
- ]);
908
- assert.deepEqual(detectDestructiveCommand(alternation), []);
909
- const substitution = padded([
910
- edited("e1", "pad19", "/repo/src/a.ts"),
911
- ran("b", "e1", 'node check.js $(grep -rl "DROP INDEX" .)'),
912
- ]);
913
- assert.deepEqual(detectDestructiveCommand(substitution), []);
914
- // The real thing still reports.
915
- const real = padded([
916
- edited("e1", "pad19", "/repo/src/a.ts"),
917
- ran("b", "e1", 'sqlite3 data/app.db "DELETE FROM sessions;"'),
918
- ]);
919
- assert.equal(detectDestructiveCommand(real).length, 1);
920
- });
921
- function writtenThenEdited(content, edits) {
922
- return padded([
923
- assistant("w", "pad19", [{ type: "tool_use", id: "w0", name: "Write", input: { file_path: "/r/a.txt", content } }]),
924
- ...edits.map((edit, i) => assistant(`e${i}`, i === 0 ? "w" : `e${i - 1}`, [
925
- { type: "tool_use", id: `t${i}`, name: "Edit", input: { file_path: "/r/a.txt", ...edit } },
926
- ])),
927
- ]);
928
- }
929
- test("a file doubled over and over stops being followed before it takes the heap", () => {
930
- const doubling = { old_string: "\n", new_string: "\n\n", replace_all: true };
931
- const session = writtenThenEdited("\n", Array.from({ length: 40 }, () => doubling));
932
- assert.deepEqual(detectAbandonedApproach(session), []);
933
- });
934
- test("a large file edited over and over stops being followed before it takes minutes", () => {
935
- const edits = Array.from({ length: 1000 }, (_, i) => ({ old_string: i % 2 ? "b" : "a", new_string: i % 2 ? "a" : "b" }));
936
- const session = writtenThenEdited(`a${"\n".repeat(4_000_000)}`, edits);
937
- const started = performance.now();
938
- detectAbandonedApproach(session);
939
- const took = performance.now() - started;
940
- assert.ok(took < 5000, `took ${took} ms`);
941
- });
942
- test("a long edit that almost repeats is looked for in time proportional to the file", () => {
943
- const half = "a".repeat(50_000);
944
- const session = writtenThenEdited("a".repeat(400_000), [{ old_string: `${half}b${half}`, new_string: "c" }]);
945
- const started = performance.now();
946
- assert.deepEqual(detectAbandonedApproach(session), []);
947
- const took = performance.now() - started;
948
- assert.ok(took < 1000, `took ${took} ms`);
949
- });
950
- test("an edit's new text is literal, as the tool that made it", () => {
951
- const candidates = detectAbandonedApproach(writtenThenEdited("a b", [
952
- { old_string: "a", new_string: "$&x" },
953
- { old_string: "$&", new_string: "a" },
954
- { old_string: "x", new_string: "" },
955
- ]));
956
- assert.deepEqual(candidates.map((candidate) => candidate.category), ["abandoned_approach"]);
957
- });
958
- test("replace_all takes occurrences from the left without overlap, and a plain edit only the first", () => {
959
- const back = (edit, after) => detectAbandonedApproach(writtenThenEdited("aaaaa", [edit, { old_string: after, new_string: "aaaaa" }])).length;
960
- assert.equal(back({ old_string: "aa", new_string: "b", replace_all: true }, "bba"), 1);
961
- assert.equal(back({ old_string: "aa", new_string: "b" }, "baaa"), 1);
962
- assert.equal(back({ old_string: "aa", new_string: "b" }, "bba"), 0);
963
- });
964
- function readThenEdited(glob, filePath) {
965
- return padded([read("r", "pad19", glob), edited("e", "r", filePath)]);
966
- }
967
- test("what a glob read covers of an edited file", () => {
968
- const covers = [
969
- ["src/*.js", "/repo/src/a.js", true],
970
- ["src/*.js", "/repo/src/lib/a.js", false],
971
- ["*.js", "/repo/lib/a.js", true],
972
- ["src/**/*.js", "/repo/src/a.js", true],
973
- ["src/**/*.js", "/repo/src/lib/deep/a.js", true],
974
- ["src/**/*.js", "/repo/other/a.js", false],
975
- ["**/a.js", "/repo/a.js", true],
976
- ["/repo/*.js", "/repo/a.js", true],
977
- ["/repo/*.js", "/other/repo/a.js", false],
978
- ["/repo/**", "/repo/x/y.js", true],
979
- ["a?.js", "/repo/ab.js", true],
980
- ["a?.js", "/repo/a/.js", false],
981
- ["src/a*b*c.js", "/repo/src/axbyc.js", true],
982
- ["src/a*b*c.js", "/repo/src/axbyd.js", false],
983
- ["s*c/*.js", "/repo/src/a.js", true],
984
- ["s*/**/z.js", "/repo/src/a/b/z.js", true],
985
- ["re.o/*.js", "/repo/re.o/a.js", true],
986
- ["re.o/*.js", "/repo/rexo/a.js", false],
987
- ["a**/b.js", "/repo/ax/y/b.js", true],
988
- ["a*a*b.js", "/repo/aaab.js", true],
989
- ["src/*/a.js", "/repo/src/x/y/a.js", false],
990
- ];
991
- for (const [glob, filePath, covered] of covers) {
992
- const found = detectUnverifiedAssumption(readThenEdited(glob, filePath));
993
- assert.equal(found.length === 0, covered, `${glob} over ${filePath}`);
994
- }
995
- });
996
- test("a read of many stars is held up to an edit in time proportional to it", () => {
997
- const started = performance.now();
998
- const found = detectUnverifiedAssumption(readThenEdited(`${"*a".repeat(6)}*b`, `/${"a".repeat(60)}`));
999
- const took = performance.now() - started;
1000
- assert.equal(found.length, 1);
1001
- assert.ok(took < 1000, `took ${took} ms`);
1002
- });
1003
- test("a read of more stars than a pattern can hold is a read like any other", () => {
1004
- assert.equal(detectUnverifiedAssumption(readThenEdited(`${"*".repeat(128000)}a.js`, "/repo/a.js")).length, 0);
1005
- assert.equal(detectUnverifiedAssumption(readThenEdited(`${"*?".repeat(64000)}b`, "/repo/a.js")).length, 1);
1006
- });
1007
- test("many long globs against many long paths give up rather than take hours", () => {
1008
- const records = [];
1009
- const stretch = "a".repeat(2000);
1010
- for (let i = 0; i < 40; i += 1)
1011
- records.push(read(`r${i}`, i === 0 ? "pad19" : `r${i - 1}`, `/**${stretch}${i}b`));
1012
- for (let i = 0; i < 40; i += 1)
1013
- records.push(edited(`e${i}`, i === 0 ? "r39" : `e${i - 1}`, `/${stretch}${stretch}${i}`));
1014
- const session = padded(records);
1015
- const started = performance.now();
1016
- detectUnverifiedAssumption(session);
1017
- detectStaleContext(session);
1018
- const took = performance.now() - started;
1019
- assert.ok(took < 5000, `took ${took} ms`);
1020
- });
1021
- test("a failed check is read once, not again for each edit after it", () => {
1022
- const command = `npm test ${"a ".repeat(150_000)}`;
1023
- const edits = Array.from({ length: 300 }, (_, i) => ({ type: "tool_use", id: `e${i}`, name: "Edit", input: { file_path: "/repo/src/a.ts", old_string: "a", new_string: "b" } }));
1024
- const session = padded([
1025
- assistant("c", "pad19", [{ type: "tool_use", id: "bash", name: "Bash", input: { command } }]),
1026
- result("cr", "c", "bash", { message: { role: "user", content: [{ type: "tool_result", tool_use_id: "bash", content: "failed", is_error: true }] } }),
1027
- assistant("e", "cr", edits),
1028
- ]);
1029
- const started = performance.now();
1030
- detectErrorSuppressed(session);
1031
- const took = performance.now() - started;
1032
- assert.ok(took < 3000, `took ${took} ms`);
1033
- });
1034
- /** A session around the record of one edit: `before` holds what stands so many records back, and the rest is filler. */
1035
- function aroundAnEdit(editRecord, before, after = []) {
1036
- const contents = [];
1037
- for (let back = Math.max(0, ...before.keys()); back >= 1; back -= 1)
1038
- contents.push(before.get(back) ?? [{ type: "text", text: "x" }]);
1039
- contents.push(editRecord);
1040
- if (after.length > 0)
1041
- contents.push(after);
1042
- return padded(contents.map((content, i) => assistant(`r${i}`, i === 0 ? "pad19" : `r${i - 1}`, content)));
1043
- }
1044
- const bashCall = (id) => ({ type: "tool_use", id, name: "Bash", input: { command: "npm test" } });
1045
- /** The call and its result in one record, so that the result takes no place among those counted back. */
1046
- const failedCall = (id) => [bashCall(id), { type: "tool_result", tool_use_id: id, content: "1 test FAILED" }];
1047
- test("a failing call counts for ten records before an edit, on each of the three ways to ask", () => {
1048
- const weakening = { type: "tool_use", id: "edit", name: "Edit", input: { file_path: "/r/t/a.test.ts", old_string: "expect(a).toBe(1);\nexpect(b).toBe(2);", new_string: "expect(a).toBe(1);" } };
1049
- const sed = { type: "tool_use", id: "edit", name: "Bash", input: { command: "sed -i x t/a.test.ts" } };
1050
- const suppression = { type: "tool_use", id: "edit", name: "Edit", input: { file_path: "/r/src/a.ts", old_string: "const x = f();", new_string: "// @ts-ignore\nconst x = f();" } };
1051
- const asks = [
1052
- ["a weakened test", weakening, (session) => detectTestModifiedToPass(session)[0].weight === 0.9],
1053
- ["a sed on a test", sed, (session) => detectTestModifiedToPass(session).length === 1],
1054
- ["a suppression", suppression, (session) => detectErrorSuppressed(session)[0].weight === 0.7],
1055
- ];
1056
- const failedRead = [{ type: "tool_use", id: "f", name: "Read", input: { file_path: "/r/a" } }, { type: "tool_result", tool_use_id: "f", content: "FAILED", is_error: true }];
1057
- for (const [label, edit, seesFailure] of asks) {
1058
- for (const [back, counted] of [[1, true], [10, true], [11, false]]) {
1059
- assert.equal(seesFailure(aroundAnEdit([edit], new Map([[back, failedCall("f")]]))), counted, `${label}, ${back} back`);
1060
- }
1061
- assert.equal(seesFailure(aroundAnEdit([...failedCall("f"), edit], new Map())), false, `${label}, in its own record`);
1062
- assert.equal(seesFailure(aroundAnEdit([edit], new Map(), failedCall("f"))), false, `${label}, after it`);
1063
- assert.equal(seesFailure(aroundAnEdit([edit], new Map([[1, [bashCall("f")]]]))), false, `${label}, a call with no result`);
1064
- assert.equal(seesFailure(aroundAnEdit([edit], new Map([[1, failedRead]]))), false, `${label}, a failure of no shell call`);
1065
- }
1066
- });
1067
- test("a finding starts from the earliest failing call in reach of its edit", () => {
1068
- const sed = { type: "tool_use", id: "edit", name: "Bash", input: { command: "sed -i x t/a.test.ts" } };
1069
- const session = aroundAnEdit([sed], new Map([[12, failedCall("out")], [9, failedCall("first")], [3, failedCall("second")]]));
1070
- const startedAt = (id) => session.toolUses.find((use) => use.id === id)?.timestamp;
1071
- const [found] = detectTestModifiedToPass(session);
1072
- assert.equal(found?.startedAt, startedAt("first"));
1073
- assert.notEqual(startedAt("first"), startedAt("second"));
1074
- assert.notEqual(startedAt("first"), startedAt("out"));
1075
- });
1076
- test("what edits ask of a session's calls comes to one look at each, however many edits ask", () => {
1077
- // Counted and not timed: a look at each call for each edit is seconds at this size, which a
1078
- // loaded machine can match, and a million looks here against the few thousand of one round.
1079
- const calls = 2000;
1080
- const edits = 500;
1081
- const bash = Array.from({ length: calls }, (_, i) => bashCall(`b${i}`));
1082
- const failures = bash.map((_, i) => ({ type: "tool_result", tool_use_id: `b${i}`, content: "no", is_error: true }));
1083
- const seds = Array.from({ length: edits }, (_, i) => ({ type: "tool_use", id: `s${i}`, name: "Bash", input: { command: "sed -i x t/a.test.ts" } }));
1084
- const suppressions = Array.from({ length: edits }, (_, i) => ({ type: "tool_use", id: `e${i}`, name: "Edit", input: { file_path: "/r/src/a.ts", old_string: "const x = f();", new_string: "// @ts-ignore\nconst x = f();" } }));
1085
- const asked = [...seds, ...suppressions];
1086
- const gap = Array.from({ length: 12 }, () => [{ type: "text", text: "x" }]);
1087
- const forms = [
1088
- ["no results", [bash, asked]],
1089
- ["all failed right before", [bash, failures, asked]],
1090
- ["all failed out of reach", [bash, failures, ...gap, asked]],
1091
- ["all failed after", [asked, bash, failures]],
1092
- ];
1093
- for (const [label, contents] of forms) {
1094
- const session = padded(contents.map((content, i) => assistant(`f${i}`, i === 0 ? "pad19" : `f${i - 1}`, content)));
1095
- let looks = 0;
1096
- session.toolUses = session.toolUses.map((use) => new Proxy(use, {
1097
- get(target, key, receiver) {
1098
- looks += 1;
1099
- return Reflect.get(target, key, receiver);
1100
- },
1101
- }));
1102
- detectTestModifiedToPass(session);
1103
- detectErrorSuppressed(session);
1104
- const perCall = looks / session.toolUses.length;
1105
- assert.ok(perCall < 20, `${label}: ${Math.round(perCall)} looks at each of ${session.toolUses.length} calls`);
1106
- }
1107
- });
1108
- test("the output of a call no edit reaches is never read", () => {
1109
- // Counted and not timed: a record's many blocks can each carry its one output of megabytes, so
1110
- // a read of it per call is half a minute from an upload of a few kilobytes.
1111
- const sed = { type: "tool_use", id: "edit", name: "Bash", input: { command: "sed -i x t/a.test.ts" } };
1112
- const calls = [bashCall("b0"), bashCall("b1")];
1113
- const results = calls.map((_, i) => ({ type: "tool_result", tool_use_id: `b${i}`, content: "1 test FAILED" }));
1114
- const gap = Array.from({ length: 12 }, () => [{ type: "text", text: "x" }]);
1115
- const forms = [
1116
- ["before its reach", [calls, results, ...gap, [sed]]],
1117
- ["after the edit", [[sed], calls, results]],
1118
- ];
1119
- for (const [label, contents] of forms) {
1120
- const session = padded(contents.map((content, i) => assistant(`g${i}`, i === 0 ? "pad19" : `g${i - 1}`, content)));
1121
- let reads = 0;
1122
- for (const use of session.toolUses) {
1123
- if (use.result === undefined)
1124
- continue;
1125
- use.result = new Proxy(use.result, {
1126
- get(target, key, receiver) {
1127
- if (key === "stdout" || key === "stderr" || key === "text")
1128
- reads += 1;
1129
- return Reflect.get(target, key, receiver);
1130
- },
1131
- });
1132
- }
1133
- assert.deepEqual(detectTestModifiedToPass(session), [], label);
1134
- assert.deepEqual(detectErrorSuppressed(session), [], label);
1135
- assert.equal(reads, 0, label);
1136
- }
1137
- });
1138
- function assertQuick(label, run, limit = 2000) {
1139
- const started = performance.now();
1140
- run();
1141
- const took = performance.now() - started;
1142
- assert.ok(took < limit, `${label} took ${Math.round(took)} ms`);
1143
- }
1144
- test("a manifest of blank lines or of an open table is read in time proportional to it", () => {
1145
- for (const [file, text] of [
1146
- ["requirements.txt", "\n".repeat(40_000)],
1147
- ["requirements.txt", " \n".repeat(40_000)],
1148
- ["pyproject.toml", "\n".repeat(40_000)],
1149
- ["pyproject.toml", ` ${"\n ".repeat(40_000)}`],
1150
- ["pyproject.toml", `[${" ".repeat(3_000)}`],
1151
- ]) {
1152
- assertQuick(`${file} of ${JSON.stringify(text.slice(0, 2))}`, () => addedDependencies(`/r/${file}`, "", text));
1153
- }
1154
- });
1155
- test("a dependency is found behind blank lines and indentation, and a table however it is spaced", () => {
1156
- assert.deepEqual(addedDependencies("/r/requirements.txt", "", "\n\n \n\trequests==2.31.0\r\n\r\n\u00a0flask>=2\n\ufeffclick~=8\n"), ["click", "flask", "requests"]);
1157
- assert.deepEqual(addedDependencies("/r/Gemfile", "", "\n \n gem 'rails'\n"), ["rails"]);
1158
- assert.deepEqual(addedDependencies("/r/go.mod", "", "\n \n require github.com/a/b v1.0.0\n"), ["github.com/a/b"]);
1159
- assert.deepEqual(addedDependencies("/r/Cargo.toml", "", '\n\n[ dependencies ] # pinned\n\n serde = "1.0"\n\n[[ bin ]]\nclap = "4"\n'), ["serde"]);
1160
- assert.deepEqual(addedDependencies("/r/Cargo.toml", "", '\n \n "serde" = "1.0"\n'), ["serde"]);
1161
- assert.deepEqual(addedDependencies("/r/pyproject.toml", "", '\n\n[ project ]\n\n dependencies = ["click"]\n'), ["click"]);
1162
- });
1163
- test("a command that says one thing over and over is read in time proportional to it", () => {
1164
- const destructive = (command) => detectDestructiveCommand(padded([ran("b", "pad19", command)]));
1165
- assertQuick("a run of dots", () => installedPackages(`npm install ${".".repeat(100_000)}a`));
1166
- assertQuick("a run of spaces after a delete", () => destructive(`DELETE FROM a${" ".repeat(100_000)}x`));
1167
- assertQuick("many pushes", () => destructive("git push ".repeat(20_000)));
1168
- assertQuick("a run of spaces after a push", () => destructive(`git push${" ".repeat(40_000)}${"x".repeat(40_000)}`));
1169
- });
1170
- test("a package, a delete and a force push are found however they are spaced", () => {
1171
- assert.deepEqual(installedPackages("npm install left-pad, and more")?.packages, ["left-pad"]);
1172
- assert.deepEqual(installedPackages("npm install left-pad...")?.packages, ["left-pad"]);
1173
- assert.equal(installedPackages("npm install ...a"), undefined);
1174
- for (const [command, fires, detail] of [
1175
- ["DELETE FROM a", true, "no WHERE clause"],
1176
- ["delete from a ;", true, "no WHERE clause"],
1177
- ['psql -c "DELETE FROM a " ', true, "no WHERE clause"],
1178
- ["psql -c 'DELETE FROM a'", true, "no WHERE clause"],
1179
- ["DELETE FROM a WHERE b", false, "no WHERE clause"],
1180
- ["git push origin main --force", true, "force-pushed"],
1181
- ["git push \t origin --force", true, "force-pushed"],
1182
- ["git push origin -f", true, "force-pushed"],
1183
- ["git push -f", true, "force-pushed"],
1184
- ["git push a git push b --force", true, "force-pushed"],
1185
- ["git push --force-with-lease git push --force", true, "force-pushed"],
1186
- ["git push a git push -f", true, "force-pushed"],
1187
- ["git push git push git push -f", true, "force-pushed"],
1188
- ["git push origin --force-with-lease", false, "force-pushed"],
1189
- ["git push origin main", false, "force-pushed"],
1190
- ]) {
1191
- const found = detectDestructiveCommand(padded([ran("b", "pad19", command)]));
1192
- assert.equal(found.some((candidate) => candidate.detail.includes(detail)), fires, command);
1193
- }
1194
- });
1195
- test("a discard is held up to a session's files once, however many paths it names", () => {
1196
- const files = Array.from({ length: 200 }, (_, index) => edited(`e${index}`, "pad19", `/r/f${index}.ts`));
1197
- const discards = Array.from({ length: 8 }, (_, index) => ran(`g${index}`, "e199", `git checkout ${"z ".repeat(20_000)}`));
1198
- const session = padded([...files, ...discards]);
1199
- assertQuick("many paths", () => detectAbandonedApproach(session));
1200
- });
1201
- test("a discard goes by the last part of a path, in or out of quotes", () => {
1202
- for (const [command, fires] of [
1203
- ["git checkout -- src/app.ts", true],
1204
- ["git checkout -- ./src/app.ts", true],
1205
- ["git checkout -- app.ts", true],
1206
- ["git checkout -- other/app.ts", true],
1207
- ['git restore "src/app.ts"', true],
1208
- ["git restore 'app.ts' b.ts", true],
1209
- ["git restore b.ts app.ts", true],
1210
- ["git checkout -- .", true],
1211
- ["git restore '*'", true],
1212
- ["git checkout -- src/app.tsx", false],
1213
- ["git restore pp.ts", false],
1214
- ["git restore src/", false],
1215
- ]) {
1216
- const session = padded([edited("e", "pad19", "/r/src/app.ts"), ran("g", "e", command)]);
1217
- const found = detectAbandonedApproach(session).filter((candidate) => candidate.detail.includes("git command"));
1218
- assert.equal(found.length, fires ? 1 : 0, command);
1219
- }
1220
- });
1221
- test("the rule for a dropped exception looks from one except as far as the next", () => {
1222
- const rule = SUPPRESSORS.find(({ pattern }) => pattern.source.startsWith("except"));
1223
- assertQuick("many excepts", () => "except ".repeat(1_000_000).match(rule.pattern));
1224
- for (const [text, count] of [
1225
- ["except except:\n pass\n", 1],
1226
- ["except (exceptions.A, exceptions.B):\n pass\n", 1],
1227
- ["except A: # except this\n pass\n", 1],
1228
- ["except A: x\nexcept B:\n pass\nexcept C:\n pass\n", 2],
1229
- ]) {
1230
- assert.equal(text.match(rule.pattern)?.length, count, text);
1231
- }
1232
- });
1233
- test("what a command printed is looked through once, not again for each edit after it", () => {
1234
- const printed = "x".repeat(16_000_000);
1235
- const session = padded([
1236
- ran("c", "pad19", "true"),
1237
- result("cr", "c", "bash-c", { toolUseResult: { stdout: printed, stderr: printed } }),
1238
- ran("s", "cr", `sed -i x ${"tests/a.ts ".repeat(1000)}`),
1239
- ]);
1240
- assertQuick("many edits", () => assert.deepEqual(detectTestModifiedToPass(session), []));
1241
- });
1242
- test("a long edit is held up to the ones before it in time proportional to them", () => {
1243
- const stretch = "q".repeat(1400);
1244
- const steps = Array.from({ length: 8000 }, () => ({ old_string: `${stretch}1`, new_string: `${stretch}2` }));
1245
- const session = padded([assistant("m", "pad19", [{ type: "tool_use", id: "t1", name: "MultiEdit", input: { file_path: "/r/a.ts", edits: steps } }])]);
1246
- assertQuick("many steps", () => assert.deepEqual(detectAbandonedApproach(session), []));
1247
- });
1248
- test("an edit undoes one before it only if each one's two sides are the other's", () => {
1249
- const editing = (uuid, parent, old_string, new_string) => assistant(uuid, parent, [{ type: "tool_use", id: `t-${uuid}`, name: "Edit", input: { file_path: "/r/a.ts", old_string, new_string } }]);
1250
- const undone = (first, second) => detectAbandonedApproach(padded([editing("e1", "pad19", ...first), editing("e2", "e1", ...second)])).length;
1251
- assert.equal(undone(["ab", "c"], ["c", "ab"]), 1);
1252
- assert.equal(undone(["ab", "c"], ["bc", "a"]), 0);
1253
- assert.equal(undone(["ab", "c"], ["ab", "c"]), 0);
1254
- });
1255
- test("a discard takes the last part of each file's path once, however many discards there are", () => {
1256
- const stretch = "p".repeat(5000);
1257
- const files = Array.from({ length: 500 }, (_, index) => edited(`e${index}`, "pad19", `/${stretch}${index}`));
1258
- const discards = Array.from({ length: 2000 }, (_, index) => ran(`g${index}`, "e499", "git checkout z"));
1259
- const session = padded([...files, ...discards]);
1260
- assertQuick("many long paths", () => detectAbandonedApproach(session));
1261
- });
1262
- test("a discard names the first edit of each file it took back, not one made after it", () => {
1263
- const session = padded([ran("g", "pad19", "git checkout a.ts"), edited("e1", "g", "/r/a.ts")]);
1264
- assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("discarded")), []);
1265
- const taken = padded([edited("e1", "pad19", "/r/a.ts"), ran("g", "e1", "git checkout a.ts"), edited("e2", "g", "/r/a.ts")]);
1266
- assert.deepEqual(detectAbandonedApproach(taken).filter((c) => c.detail.includes("discarded")).map((c) => c.filePath), ["/r/a.ts"]);
1267
- });
1268
- test("a side chain is walked once, however many edits stand on it", () => {
1269
- const records = [];
1270
- const parentOf = (index) => (index === 0 ? "pad19" : `c${index - 1}`);
1271
- for (let index = 0; index < 60_000; index += 1)
1272
- records.push(assistant(`c${index}`, parentOf(index), [{ type: "text", text: "x" }]));
1273
- for (let index = 0; index < 2000; index += 1)
1274
- records.push(edited(`e${index}`, index === 0 ? "c59999" : `e${index - 1}`, "/r/a.ts"));
1275
- records.push(assistant("live", "pad19", [{ type: "text", text: "different approach" }]));
1276
- records.push(assistant("tip", "live", [{ type: "text", text: "done" }]));
1277
- const session = padded(records);
1278
- let found = 0;
1279
- assertQuick("a long chain", () => {
1280
- found = detectAbandonedApproach(session).filter((c) => c.detail.includes("branch")).length;
1281
- });
1282
- assert.equal(found, 1);
1283
- });
1284
- test("many long reads against many edited files give up rather than take minutes, but for the path itself", () => {
1285
- const stretch = "r".repeat(1000);
1286
- const reads = Array.from({ length: 20_000 }, (_, index) => read(`r${index}`, index === 0 ? "pad19" : `r${index - 1}`, `${stretch}${index}`));
1287
- const files = Array.from({ length: 1000 }, (_, index) => edited(`e${index}`, index === 0 ? "last" : `e${index - 1}`, `/r/f${index}.ts`));
1288
- const session = padded([...reads, read("last", "r19999", "/r/f999.ts"), ...files]);
1289
- let found = [];
1290
- // What the budget still lets through is seconds on a busy machine, and all of it minutes.
1291
- assertQuick("long reads", () => {
1292
- found = detectUnverifiedAssumption(session);
1293
- }, 8000);
1294
- assert.equal(found.length, 999);
1295
- });
1296
- test("many reads that share a long start with many edited files give up rather than take minutes", () => {
1297
- const stretch = "/".concat("r".repeat(2000));
1298
- const reads = Array.from({ length: 5000 }, (_, index) => read(`r${index}`, index === 0 ? "pad19" : `r${index - 1}`, `${stretch}${index}`));
1299
- const files = Array.from({ length: 1000 }, (_, index) => edited(`e${index}`, index === 0 ? "r4999" : `e${index - 1}`, `${stretch}/f${index}.ts`));
1300
- const session = padded([...reads, ...files]);
1301
- // What the budget still lets through is seconds on a busy machine, and all of it minutes.
1302
- assertQuick("shared starts", () => detectUnverifiedAssumption(session), 20_000);
1303
- });
1304
- //# sourceMappingURL=stage0.test.js.map