@nightloom/isy 1.0.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 (223) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -0
  3. package/dist/agents/claude.d.ts +6 -0
  4. package/dist/agents/claude.js +66 -0
  5. package/dist/agents/claude.js.map +1 -0
  6. package/dist/agents/codex-records.d.ts +34 -0
  7. package/dist/agents/codex-records.js +492 -0
  8. package/dist/agents/codex-records.js.map +1 -0
  9. package/dist/agents/codex.d.ts +5 -0
  10. package/dist/agents/codex.js +205 -0
  11. package/dist/agents/codex.js.map +1 -0
  12. package/dist/agents/index.d.ts +25 -0
  13. package/dist/agents/index.js +50 -0
  14. package/dist/agents/index.js.map +1 -0
  15. package/dist/agents/kimi-records.d.ts +24 -0
  16. package/dist/agents/kimi-records.js +252 -0
  17. package/dist/agents/kimi-records.js.map +1 -0
  18. package/dist/agents/kimi-wire.d.ts +8 -0
  19. package/dist/agents/kimi-wire.js +158 -0
  20. package/dist/agents/kimi-wire.js.map +1 -0
  21. package/dist/agents/kimi.d.ts +6 -0
  22. package/dist/agents/kimi.js +332 -0
  23. package/dist/agents/kimi.js.map +1 -0
  24. package/dist/agents/types.d.ts +83 -0
  25. package/dist/agents/types.js +2 -0
  26. package/dist/agents/types.js.map +1 -0
  27. package/dist/alert.d.ts +24 -0
  28. package/dist/alert.js +68 -0
  29. package/dist/alert.js.map +1 -0
  30. package/dist/api.d.ts +103 -0
  31. package/dist/api.js +171 -0
  32. package/dist/api.js.map +1 -0
  33. package/dist/commands/analyze.d.ts +61 -0
  34. package/dist/commands/analyze.js +198 -0
  35. package/dist/commands/analyze.js.map +1 -0
  36. package/dist/commands/check.d.ts +55 -0
  37. package/dist/commands/check.js +180 -0
  38. package/dist/commands/check.js.map +1 -0
  39. package/dist/commands/commit.d.ts +26 -0
  40. package/dist/commands/commit.js +130 -0
  41. package/dist/commands/commit.js.map +1 -0
  42. package/dist/commands/health.d.ts +47 -0
  43. package/dist/commands/health.js +366 -0
  44. package/dist/commands/health.js.map +1 -0
  45. package/dist/commands/init.d.ts +3 -0
  46. package/dist/commands/init.js +173 -0
  47. package/dist/commands/init.js.map +1 -0
  48. package/dist/commands/logout.d.ts +1 -0
  49. package/dist/commands/logout.js +30 -0
  50. package/dist/commands/logout.js.map +1 -0
  51. package/dist/commands/notes.d.ts +11 -0
  52. package/dist/commands/notes.js +49 -0
  53. package/dist/commands/notes.js.map +1 -0
  54. package/dist/commands/scan.d.ts +47 -0
  55. package/dist/commands/scan.js +199 -0
  56. package/dist/commands/scan.js.map +1 -0
  57. package/dist/commands/settings.d.ts +15 -0
  58. package/dist/commands/settings.js +94 -0
  59. package/dist/commands/settings.js.map +1 -0
  60. package/dist/commands/status.d.ts +61 -0
  61. package/dist/commands/status.js +151 -0
  62. package/dist/commands/status.js.map +1 -0
  63. package/dist/commands/upload.d.ts +42 -0
  64. package/dist/commands/upload.js +429 -0
  65. package/dist/commands/upload.js.map +1 -0
  66. package/dist/config.d.ts +4 -0
  67. package/dist/config.js +32 -0
  68. package/dist/config.js.map +1 -0
  69. package/dist/detectors.d.ts +43 -0
  70. package/dist/detectors.js +1026 -0
  71. package/dist/detectors.js.map +1 -0
  72. package/dist/git.d.ts +22 -0
  73. package/dist/git.js +68 -0
  74. package/dist/git.js.map +1 -0
  75. package/dist/githook.d.ts +71 -0
  76. package/dist/githook.js +238 -0
  77. package/dist/githook.js.map +1 -0
  78. package/dist/hook.d.ts +39 -0
  79. package/dist/hook.js +173 -0
  80. package/dist/hook.js.map +1 -0
  81. package/dist/index.d.ts +2 -0
  82. package/dist/index.js +205 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/log.d.ts +19 -0
  85. package/dist/log.js +97 -0
  86. package/dist/log.js.map +1 -0
  87. package/dist/mask-paths.d.ts +75 -0
  88. package/dist/mask-paths.js +364 -0
  89. package/dist/mask-paths.js.map +1 -0
  90. package/dist/pairing.d.ts +60 -0
  91. package/dist/pairing.js +137 -0
  92. package/dist/pairing.js.map +1 -0
  93. package/dist/parser.d.ts +48 -0
  94. package/dist/parser.js +516 -0
  95. package/dist/parser.js.map +1 -0
  96. package/dist/paths.d.ts +41 -0
  97. package/dist/paths.js +181 -0
  98. package/dist/paths.js.map +1 -0
  99. package/dist/plan.d.ts +25 -0
  100. package/dist/plan.js +39 -0
  101. package/dist/plan.js.map +1 -0
  102. package/dist/queue.d.ts +26 -0
  103. package/dist/queue.js +120 -0
  104. package/dist/queue.js.map +1 -0
  105. package/dist/redact.d.ts +29 -0
  106. package/dist/redact.js +254 -0
  107. package/dist/redact.js.map +1 -0
  108. package/dist/spawn.d.ts +13 -0
  109. package/dist/spawn.js +32 -0
  110. package/dist/spawn.js.map +1 -0
  111. package/dist/stage0.d.ts +20 -0
  112. package/dist/stage0.js +90 -0
  113. package/dist/stage0.js.map +1 -0
  114. package/dist/state.d.ts +84 -0
  115. package/dist/state.js +150 -0
  116. package/dist/state.js.map +1 -0
  117. package/dist/test/analyze.test.d.ts +1 -0
  118. package/dist/test/analyze.test.js +408 -0
  119. package/dist/test/analyze.test.js.map +1 -0
  120. package/dist/test/check.test.d.ts +1 -0
  121. package/dist/test/check.test.js +193 -0
  122. package/dist/test/check.test.js.map +1 -0
  123. package/dist/test/cli.test.d.ts +1 -0
  124. package/dist/test/cli.test.js +132 -0
  125. package/dist/test/cli.test.js.map +1 -0
  126. package/dist/test/codex.test.d.ts +1 -0
  127. package/dist/test/codex.test.js +585 -0
  128. package/dist/test/codex.test.js.map +1 -0
  129. package/dist/test/commit.test.d.ts +1 -0
  130. package/dist/test/commit.test.js +430 -0
  131. package/dist/test/commit.test.js.map +1 -0
  132. package/dist/test/git.test.d.ts +1 -0
  133. package/dist/test/git.test.js +75 -0
  134. package/dist/test/git.test.js.map +1 -0
  135. package/dist/test/health.test.d.ts +1 -0
  136. package/dist/test/health.test.js +156 -0
  137. package/dist/test/health.test.js.map +1 -0
  138. package/dist/test/hook.test.d.ts +1 -0
  139. package/dist/test/hook.test.js +144 -0
  140. package/dist/test/hook.test.js.map +1 -0
  141. package/dist/test/kimi.test.d.ts +1 -0
  142. package/dist/test/kimi.test.js +349 -0
  143. package/dist/test/kimi.test.js.map +1 -0
  144. package/dist/test/mask-paths.test.d.ts +1 -0
  145. package/dist/test/mask-paths.test.js +230 -0
  146. package/dist/test/mask-paths.test.js.map +1 -0
  147. package/dist/test/notes.test.d.ts +1 -0
  148. package/dist/test/notes.test.js +41 -0
  149. package/dist/test/notes.test.js.map +1 -0
  150. package/dist/test/pairing.test.d.ts +1 -0
  151. package/dist/test/pairing.test.js +138 -0
  152. package/dist/test/pairing.test.js.map +1 -0
  153. package/dist/test/parser.test.d.ts +1 -0
  154. package/dist/test/parser.test.js +312 -0
  155. package/dist/test/parser.test.js.map +1 -0
  156. package/dist/test/plan.test.d.ts +1 -0
  157. package/dist/test/plan.test.js +34 -0
  158. package/dist/test/plan.test.js.map +1 -0
  159. package/dist/test/redact.test.d.ts +1 -0
  160. package/dist/test/redact.test.js +303 -0
  161. package/dist/test/redact.test.js.map +1 -0
  162. package/dist/test/settings.test.d.ts +1 -0
  163. package/dist/test/settings.test.js +32 -0
  164. package/dist/test/settings.test.js.map +1 -0
  165. package/dist/test/stage0.test.d.ts +1 -0
  166. package/dist/test/stage0.test.js +607 -0
  167. package/dist/test/stage0.test.js.map +1 -0
  168. package/dist/test/sweep.test.d.ts +1 -0
  169. package/dist/test/sweep.test.js +298 -0
  170. package/dist/test/sweep.test.js.map +1 -0
  171. package/dist/test/update.test.d.ts +1 -0
  172. package/dist/test/update.test.js +26 -0
  173. package/dist/test/update.test.js.map +1 -0
  174. package/dist/test/upload.test.d.ts +1 -0
  175. package/dist/test/upload.test.js +331 -0
  176. package/dist/test/upload.test.js.map +1 -0
  177. package/dist/types.d.ts +142 -0
  178. package/dist/types.js +2 -0
  179. package/dist/types.js.map +1 -0
  180. package/dist/update.d.ts +17 -0
  181. package/dist/update.js +46 -0
  182. package/dist/update.js.map +1 -0
  183. package/package.json +60 -0
  184. package/src/agents/claude.ts +89 -0
  185. package/src/agents/codex-records.ts +533 -0
  186. package/src/agents/codex.ts +238 -0
  187. package/src/agents/index.ts +58 -0
  188. package/src/agents/kimi-records.ts +274 -0
  189. package/src/agents/kimi-wire.ts +179 -0
  190. package/src/agents/kimi.ts +390 -0
  191. package/src/agents/types.ts +92 -0
  192. package/src/alert.ts +68 -0
  193. package/src/api.ts +269 -0
  194. package/src/commands/analyze.ts +308 -0
  195. package/src/commands/check.ts +239 -0
  196. package/src/commands/commit.ts +179 -0
  197. package/src/commands/health.ts +444 -0
  198. package/src/commands/init.ts +186 -0
  199. package/src/commands/logout.ts +33 -0
  200. package/src/commands/notes.ts +57 -0
  201. package/src/commands/scan.ts +278 -0
  202. package/src/commands/settings.ts +107 -0
  203. package/src/commands/status.ts +236 -0
  204. package/src/commands/upload.ts +524 -0
  205. package/src/config.ts +33 -0
  206. package/src/detectors.ts +1138 -0
  207. package/src/git.ts +86 -0
  208. package/src/githook.ts +269 -0
  209. package/src/hook.ts +237 -0
  210. package/src/index.ts +229 -0
  211. package/src/log.ts +113 -0
  212. package/src/mask-paths.ts +434 -0
  213. package/src/pairing.ts +190 -0
  214. package/src/parser.ts +581 -0
  215. package/src/paths.ts +191 -0
  216. package/src/plan.ts +56 -0
  217. package/src/queue.ts +145 -0
  218. package/src/redact.ts +335 -0
  219. package/src/spawn.ts +33 -0
  220. package/src/stage0.ts +121 -0
  221. package/src/state.ts +208 -0
  222. package/src/types.ts +165 -0
  223. package/src/update.ts +46 -0
@@ -0,0 +1,607 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+ import { parseLines } from "../parser.js";
4
+ import { detectAbandonedApproach, detectDestructiveCommand, detectErrorSuppressed, detectStaleContext, detectTestModifiedToPass, detectUnverifiedAssumption, detectUnverifiedFix, isTestFile, weakensTest, } from "../detectors.js";
5
+ import { MIN_ASSISTANT_RECORDS, 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
+ function padded(records) {
37
+ const filler = [];
38
+ for (let i = 0; i < MIN_ASSISTANT_RECORDS; i += 1) {
39
+ filler.push(assistant(`pad${i}`, i === 0 ? null : `pad${i - 1}`, [{ type: "text", text: "x" }]));
40
+ }
41
+ return build([...filler, ...records]);
42
+ }
43
+ test("rejects a session with too few assistant records", () => {
44
+ const session = build([
45
+ assistant("a", null, [
46
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "x", new_string: "y" } },
47
+ ]),
48
+ ]);
49
+ assert.equal(eligibility(session), "too-short");
50
+ assert.deepEqual(runStage0(session).candidates, []);
51
+ });
52
+ test("rejects a long session that never edited a file", () => {
53
+ const session = padded([]);
54
+ assert.equal(eligibility(session), "no-file-edits");
55
+ assert.equal(runStage0(session).eligible, false);
56
+ });
57
+ test("accepts a long session that edited a file", () => {
58
+ const session = padded([
59
+ assistant("e", "pad19", [
60
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "x", new_string: "y" } },
61
+ ]),
62
+ ]);
63
+ assert.equal(eligibility(session), undefined);
64
+ assert.equal(runStage0(session).eligible, true);
65
+ });
66
+ test("recognises test files by path and by name", () => {
67
+ for (const path of [
68
+ "/r/tests/thing.py",
69
+ "/r/__tests__/thing.js",
70
+ "/r/src/payments.test.ts",
71
+ "/r/src/payments_spec.rs",
72
+ "/r/Thing.spec.java",
73
+ ]) {
74
+ assert.equal(isTestFile(path), true, path);
75
+ }
76
+ assert.equal(isTestFile("/r/TestRounding.java"), true);
77
+ assert.equal(isTestFile("/r/test_rounding.py"), true);
78
+ for (const path of ["/r/src/payments.ts", "/r/latest/notes.md", "/r/contest.go", "/r/latest.ts", "/r/spectrum.ts"]) {
79
+ assert.equal(isTestFile(path), false, path);
80
+ }
81
+ });
82
+ test("spots each way a test can be weakened", () => {
83
+ assert.match(weakensTest("expect(a).toBe(1);\nexpect(b).toBe(2);", "expect(a).toBe(1);") ?? "", /assertions removed/);
84
+ assert.match(weakensTest("it('works', () => {})", "it.only('works', () => {})") ?? "", /skipped/);
85
+ assert.match(weakensTest("def test_a():\n pass\ndef test_b():\n pass", "def test_a():\n pass") ?? "", /deleted/);
86
+ assert.equal(weakensTest("expect(a).toBe(1);", "expect(a).toBe(2);"), undefined);
87
+ });
88
+ test("flags a weakened test and scores it higher after a failing command", () => {
89
+ const session = padded([
90
+ assistant("b", "pad19", [{ type: "tool_use", id: "t0", name: "Bash", input: { command: "npm test" } }]),
91
+ result("br", "b", "t0", { toolUseResult: { stdout: "1 test FAILED", stderr: "" } }),
92
+ assistant("c", "br", [
93
+ {
94
+ type: "tool_use",
95
+ id: "t1",
96
+ name: "Edit",
97
+ input: {
98
+ file_path: "/r/tests/rounding.test.ts",
99
+ old_string: "expect(cents).toBe(3);\nexpect(rest).toBe(0);",
100
+ new_string: "expect(cents).toBe(3);",
101
+ },
102
+ },
103
+ ]),
104
+ ]);
105
+ const candidates = detectTestModifiedToPass(session);
106
+ assert.equal(candidates.length, 1);
107
+ assert.equal(candidates[0]?.filePath, "/r/tests/rounding.test.ts");
108
+ assert.equal(candidates[0]?.weight, 0.9);
109
+ assert.match(candidates[0]?.detail ?? "", /failing command/);
110
+ });
111
+ test("scores a weakened test lower when nothing failed beforehand", () => {
112
+ const session = padded([
113
+ assistant("c", "pad19", [
114
+ {
115
+ type: "tool_use",
116
+ id: "t1",
117
+ name: "Edit",
118
+ input: {
119
+ file_path: "/r/tests/rounding.test.ts",
120
+ old_string: "expect(a).toBe(1);\nexpect(b).toBe(2);",
121
+ new_string: "expect(a).toBe(1);",
122
+ },
123
+ },
124
+ ]),
125
+ ]);
126
+ const candidates = detectTestModifiedToPass(session);
127
+ assert.equal(candidates.length, 1);
128
+ assert.equal(candidates[0]?.weight, 0.6);
129
+ });
130
+ test("ignores a strengthened test and a non-test file", () => {
131
+ const session = padded([
132
+ assistant("c", "pad19", [
133
+ {
134
+ type: "tool_use",
135
+ id: "t1",
136
+ name: "Edit",
137
+ input: {
138
+ file_path: "/r/tests/rounding.test.ts",
139
+ old_string: "expect(a).toBe(1);",
140
+ new_string: "expect(a).toBe(1);\nexpect(b).toBe(2);",
141
+ },
142
+ },
143
+ {
144
+ type: "tool_use",
145
+ id: "t2",
146
+ name: "Edit",
147
+ input: { file_path: "/r/src/app.ts", old_string: "expect(a);\nexpect(b);", new_string: "expect(a);" },
148
+ },
149
+ ]),
150
+ ]);
151
+ assert.deepEqual(detectTestModifiedToPass(session), []);
152
+ });
153
+ test("notices a file rebuilt back to content it already had", () => {
154
+ const session = padded([
155
+ assistant("r", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
156
+ result("rr", "r", "t0", {
157
+ toolUseResult: { type: "text", file: { filePath: "/r/a.ts", content: "const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 } },
158
+ }),
159
+ assistant("e1", "rr", [
160
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 1;", new_string: "const a = 2;" } },
161
+ ]),
162
+ assistant("e2", "e1", [
163
+ { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 2;", new_string: "const a = 1;" } },
164
+ ]),
165
+ ]);
166
+ const candidates = detectAbandonedApproach(session);
167
+ assert.ok(candidates.length >= 1);
168
+ assert.equal(candidates[0]?.category, "abandoned_approach");
169
+ assert.equal(candidates[0]?.filePath, "/r/a.ts");
170
+ assert.ok(candidates.some((candidate) => candidate.weight >= 0.7));
171
+ });
172
+ test("does not flag a file that only moves forward", () => {
173
+ const session = padded([
174
+ assistant("r", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
175
+ result("rr", "r", "t0", {
176
+ toolUseResult: { file: { filePath: "/r/a.ts", content: "const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 } },
177
+ }),
178
+ assistant("e1", "rr", [
179
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 1;", new_string: "const a = 2;" } },
180
+ ]),
181
+ assistant("e2", "e1", [
182
+ { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 2;", new_string: "const a = 3;" } },
183
+ ]),
184
+ ]);
185
+ assert.deepEqual(detectAbandonedApproach(session), []);
186
+ });
187
+ test("does not treat re-reading a file after editing it as a revert", () => {
188
+ const session = padded([
189
+ assistant("r1", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
190
+ result("rr1", "r1", "t0", {
191
+ toolUseResult: { file: { filePath: "/r/a.ts", content: "const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 } },
192
+ }),
193
+ assistant("e1", "rr1", [
194
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "const a = 1;", new_string: "const a = 2;" } },
195
+ ]),
196
+ assistant("r2", "e1", [{ type: "tool_use", id: "t2", name: "Read", input: { file_path: "/r/a.ts" } }]),
197
+ result("rr2", "r2", "t2", {
198
+ toolUseResult: { file: { filePath: "/r/a.ts", content: "const a = 2;\n", numLines: 1, startLine: 1, totalLines: 1 } },
199
+ }),
200
+ ]);
201
+ assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("returned to a state")), []);
202
+ });
203
+ test("treats a repeated read with no edits between as unremarkable", () => {
204
+ const session = padded([
205
+ assistant("r1", "pad19", [{ type: "tool_use", id: "t0", name: "Read", input: { file_path: "/r/a.ts" } }]),
206
+ result("rr1", "r1", "t0", {
207
+ toolUseResult: { file: { filePath: "/r/a.ts", content: "same\n", numLines: 1, startLine: 1, totalLines: 1 } },
208
+ }),
209
+ assistant("r2", "rr1", [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: "/r/a.ts" } }]),
210
+ result("rr2", "r2", "t1", {
211
+ toolUseResult: { file: { filePath: "/r/a.ts", content: "same\n", numLines: 1, startLine: 1, totalLines: 1 } },
212
+ }),
213
+ assistant("e", "rr2", [
214
+ { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/b.ts", old_string: "x", new_string: "y" } },
215
+ ]),
216
+ ]);
217
+ assert.deepEqual(detectAbandonedApproach(session), []);
218
+ });
219
+ test("flags edits thrown away by a git command", () => {
220
+ for (const command of ["git checkout -- src/app.ts", "git restore src/app.ts", "git reset --hard", "git stash"]) {
221
+ const session = padded([
222
+ assistant("e", "pad19", [
223
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
224
+ ]),
225
+ assistant("g", "e", [{ type: "tool_use", id: "t2", name: "Bash", input: { command } }]),
226
+ ]);
227
+ const candidates = detectAbandonedApproach(session).filter((c) => c.detail.includes("git command"));
228
+ assert.equal(candidates.length, 1, command);
229
+ assert.equal(candidates[0]?.filePath, "/r/src/app.ts");
230
+ }
231
+ });
232
+ test("does not treat restoring a stash as discarding work", () => {
233
+ const session = padded([
234
+ assistant("e", "pad19", [
235
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
236
+ ]),
237
+ assistant("g", "e", [{ type: "tool_use", id: "t2", name: "Bash", input: { command: "git stash pop" } }]),
238
+ ]);
239
+ assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("git command")), []);
240
+ });
241
+ test("ignores a git revert that happened before the file was touched", () => {
242
+ const session = padded([
243
+ assistant("g", "pad19", [{ type: "tool_use", id: "t1", name: "Bash", input: { command: "git reset --hard" } }]),
244
+ assistant("e", "g", [
245
+ { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
246
+ ]),
247
+ ]);
248
+ assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("git command")), []);
249
+ });
250
+ test("flags edits on a branch the session walked away from", () => {
251
+ const session = padded([
252
+ assistant("dead", "pad19", [
253
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/tried.ts", old_string: "x", new_string: "y" } },
254
+ ]),
255
+ assistant("live", "pad19", [{ type: "text", text: "different approach" }]),
256
+ assistant("tip", "live", [{ type: "text", text: "done" }]),
257
+ ]);
258
+ const candidates = detectAbandonedApproach(session).filter((c) => c.detail.includes("branch"));
259
+ assert.equal(candidates.length, 1);
260
+ assert.equal(candidates[0]?.filePath, "/r/src/tried.ts");
261
+ assert.equal(candidates[0]?.weight, 0.4);
262
+ });
263
+ test("does not call a truncated history an abandoned branch", () => {
264
+ const session = padded([
265
+ assistant("orphan", "missing-from-this-file", [
266
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/kept.ts", old_string: "x", new_string: "y" } },
267
+ ]),
268
+ assistant("tip", "pad19", [{ type: "text", text: "done" }]),
269
+ ]);
270
+ assert.deepEqual(detectAbandonedApproach(session).filter((c) => c.detail.includes("branch")), []);
271
+ });
272
+ test("stage 0 returns both categories sorted by position in the session", () => {
273
+ const session = padded([
274
+ assistant("e1", "pad19", [
275
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/app.ts", old_string: "x", new_string: "y" } },
276
+ ]),
277
+ assistant("g", "e1", [{ type: "tool_use", id: "t2", name: "Bash", input: { command: "git checkout -- src/app.ts" } }]),
278
+ assistant("e2", "g", [
279
+ {
280
+ type: "tool_use",
281
+ id: "t3",
282
+ name: "Edit",
283
+ input: {
284
+ file_path: "/r/tests/app.test.ts",
285
+ old_string: "expect(a).toBe(1);\nexpect(b).toBe(2);",
286
+ new_string: "expect(a).toBe(1);",
287
+ },
288
+ },
289
+ ]),
290
+ ]);
291
+ const result = runStage0(session);
292
+ assert.equal(result.eligible, true);
293
+ const categories = new Set(result.candidates.map((candidate) => candidate.category));
294
+ assert.ok(categories.has("abandoned_approach"));
295
+ assert.ok(categories.has("test_modified_to_pass"));
296
+ const indices = result.candidates.map((candidate) => candidate.recordIndex);
297
+ assert.deepEqual(indices, [...indices].sort((a, b) => a - b));
298
+ });
299
+ test("does not read an unchanged rewrite as a return to an earlier state", () => {
300
+ const session = padded([
301
+ assistant("w1", "pad19", [
302
+ { type: "tool_use", id: "t0", name: "Write", input: { file_path: "/r/hook.ts", content: "export const a = 1;\n" } },
303
+ ]),
304
+ assistant("r", "w1", [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: "/r/hook.ts" } }]),
305
+ result("rr", "r", "t1", {
306
+ toolUseResult: {
307
+ file: { filePath: "/r/hook.ts", content: "export const a = 1;\n", numLines: 1, startLine: 1, totalLines: 1 },
308
+ },
309
+ }),
310
+ assistant("w2", "rr", [
311
+ { type: "tool_use", id: "t2", name: "Write", input: { file_path: "/r/hook.ts", content: "export const a = 1;\n" } },
312
+ ]),
313
+ ]);
314
+ assert.deepEqual(detectAbandonedApproach(session), []);
315
+ });
316
+ test("collapses a git revert that discards many files into one candidate", () => {
317
+ const session = padded([
318
+ assistant("e", "pad19", [
319
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "a", new_string: "b" } },
320
+ { type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/r/b.ts", old_string: "a", new_string: "b" } },
321
+ { type: "tool_use", id: "t3", name: "Edit", input: { file_path: "/r/c.ts", old_string: "a", new_string: "b" } },
322
+ ]),
323
+ assistant("g", "e", [
324
+ { type: "tool_use", id: "t4", name: "Bash", input: { command: "git reset --hard origin/main" } },
325
+ ]),
326
+ ]);
327
+ const candidates = detectAbandonedApproach(session);
328
+ assert.equal(candidates.length, 1);
329
+ assert.equal(candidates[0]?.filePath, undefined);
330
+ assert.match(candidates[0]?.detail ?? "", /edits to 3 files were discarded .*a\.ts, b\.ts, c\.ts/);
331
+ });
332
+ test("keeps the file on a git revert that discards a single file", () => {
333
+ const session = padded([
334
+ assistant("e", "pad19", [
335
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/a.ts", old_string: "a", new_string: "b" } },
336
+ ]),
337
+ assistant("g", "e", [
338
+ { type: "tool_use", id: "t2", name: "Bash", input: { command: "git checkout -- a.ts" } },
339
+ ]),
340
+ ]);
341
+ const candidates = detectAbandonedApproach(session);
342
+ assert.equal(candidates.length, 1);
343
+ assert.equal(candidates[0]?.filePath, "/r/a.ts");
344
+ });
345
+ test("counts a shell read as having looked at the file", () => {
346
+ const blind = padded([
347
+ assistant("e", "pad19", [
348
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/pricing.ts", old_string: "a", new_string: "b" } },
349
+ ]),
350
+ ]);
351
+ assert.equal(detectUnverifiedAssumption(blind).length, 1);
352
+ const inspected = padded([
353
+ assistant("s", "pad19", [
354
+ { type: "tool_use", id: "t0", name: "Bash", input: { command: "sed -n '1,40p' src/pricing.ts" } },
355
+ ]),
356
+ assistant("e", "s", [
357
+ { type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/r/src/pricing.ts", old_string: "a", new_string: "b" } },
358
+ ]),
359
+ ]);
360
+ assert.deepEqual(detectUnverifiedAssumption(inspected), []);
361
+ });
362
+ /** One Bash call, with the cwd a real transcript carries on every record. */
363
+ function ran(uuid, parentUuid, command, cwd = "/repo") {
364
+ return {
365
+ type: "assistant",
366
+ uuid,
367
+ parentUuid,
368
+ sessionId: "s1",
369
+ cwd,
370
+ timestamp: nextTimestamp(),
371
+ message: {
372
+ role: "assistant",
373
+ content: [{ type: "tool_use", id: `bash-${uuid}`, name: "Bash", input: { command } }],
374
+ },
375
+ };
376
+ }
377
+ function edited(uuid, parentUuid, filePath, newString = "b") {
378
+ return assistant(uuid, parentUuid, [
379
+ {
380
+ type: "tool_use",
381
+ id: `edit-${uuid}`,
382
+ name: "Edit",
383
+ input: { file_path: filePath, old_string: "a", new_string: newString },
384
+ },
385
+ ]);
386
+ }
387
+ test("code edited after the last test run is one note, not one per file", () => {
388
+ const session = padded([
389
+ edited("e1", "pad19", "/repo/src/a.ts"),
390
+ ran("v", "e1", "npm test"),
391
+ edited("e2", "v", "/repo/src/b.ts"),
392
+ edited("e3", "e2", "/repo/src/c.ts"),
393
+ ]);
394
+ const found = detectUnverifiedFix(session);
395
+ assert.equal(found.length, 1, "one session, one note");
396
+ assert.match(found[0].detail, /after the last test or build run/);
397
+ assert.match(found[0].detail, /b\.ts, c\.ts/);
398
+ // Anchored at the last unchecked edit, which is where the session stopped looking.
399
+ assert.equal(found[0].toolUseId, "edit-e3");
400
+ assert.equal(found[0].weight, 0.6);
401
+ });
402
+ test("a session that verified after its last edit is not flagged", () => {
403
+ const session = padded([edited("e1", "pad19", "/repo/src/a.ts"), ran("v", "e1", "cargo test")]);
404
+ assert.deepEqual(detectUnverifiedFix(session), []);
405
+ });
406
+ test("a finding about a file outside both the repository and the home is dropped", () => {
407
+ // One edit after the last test run: an unverified_fix that names its file.
408
+ const files = (cwd, filePath) => {
409
+ const run = cwd === undefined
410
+ ? assistant("v", "pad19", [{ type: "tool_use", id: "bash-v", name: "Bash", input: { command: "npm test" } }])
411
+ : ran("v", "pad19", "npm test", cwd);
412
+ return runStage0(padded([run, edited("e1", "v", filePath)])).candidates.map((found) => found.filePath);
413
+ };
414
+ // On the machine, before masking.
415
+ assert.ok(!files("/home/alice/repo", "/tmp/reset.py").includes("/tmp/reset.py"));
416
+ assert.ok(files("/home/alice/repo", "/home/alice/repo/src/a.py").includes("/home/alice/repo/src/a.py"));
417
+ // A monorepo session started in a subdirectory still edits its siblings.
418
+ assert.ok(files("/home/alice/repo/client", "/home/alice/repo/server/a.py").includes("/home/alice/repo/server/a.py"));
419
+ // On the server the transcript arrives masked, its cwd along with it.
420
+ assert.ok(!files(".", "/tmp/reset.py").includes("/tmp/reset.py"));
421
+ assert.ok(files(".", "src/a.py").includes("src/a.py"));
422
+ assert.ok(files(".", "~/repo/server/a.py").includes("~/repo/server/a.py"));
423
+ // Nothing to judge against: kept.
424
+ assert.ok(files(undefined, "/tmp/reset.py").includes("/tmp/reset.py"));
425
+ });
426
+ test("a session that never ran anything says so, and ignores tests and prose", () => {
427
+ const session = padded([
428
+ edited("e1", "pad19", "/repo/src/a.ts"),
429
+ edited("e2", "e1", "/repo/tests/test_a.py"),
430
+ edited("e3", "e2", "/repo/README.md"),
431
+ ran("g", "e3", "git status"),
432
+ ]);
433
+ const found = detectUnverifiedFix(session);
434
+ assert.equal(found.length, 1);
435
+ assert.match(found[0].detail, /no test, build or lint command ran/);
436
+ assert.match(found[0].detail, /a\.ts/);
437
+ assert.ok(!found[0].detail.includes("README"), "prose is not code");
438
+ assert.ok(!found[0].detail.includes("test_a"), "a test file has its own category");
439
+ });
440
+ test("a suppression added to production code is a signal, and one already there is not", () => {
441
+ const added = padded([
442
+ assistant("e", "pad19", [
443
+ {
444
+ type: "tool_use",
445
+ id: "t1",
446
+ name: "Edit",
447
+ input: {
448
+ file_path: "/repo/src/pay.ts",
449
+ old_string: "const id = charge.ref;",
450
+ new_string: "// @ts-ignore\nconst id = charge.ref;",
451
+ },
452
+ },
453
+ ]),
454
+ ]);
455
+ const found = detectErrorSuppressed(added);
456
+ assert.equal(found.length, 1);
457
+ assert.match(found[0].detail, /TypeScript error was suppressed/);
458
+ assert.equal(found[0].weight, 0.45, "no failing command before it");
459
+ const untouched = padded([
460
+ assistant("e", "pad19", [
461
+ {
462
+ type: "tool_use",
463
+ id: "t1",
464
+ name: "Edit",
465
+ input: {
466
+ file_path: "/repo/src/pay.ts",
467
+ old_string: "// @ts-ignore\nconst id = charge.ref;",
468
+ new_string: "// @ts-ignore\nconst id = charge.reference;",
469
+ },
470
+ },
471
+ ]),
472
+ ]);
473
+ assert.deepEqual(detectErrorSuppressed(untouched), []);
474
+ });
475
+ test("a check whose failure is thrown away is a signal, a cleanup line is not", () => {
476
+ const discarded = padded([
477
+ edited("e1", "pad19", "/repo/src/a.ts"),
478
+ ran("b", "e1", "npm test || true"),
479
+ ]);
480
+ assert.equal(detectErrorSuppressed(discarded).length, 1);
481
+ const cleanup = padded([edited("e1", "pad19", "/repo/src/a.ts"), ran("b", "e1", "pkill node || true")]);
482
+ assert.deepEqual(detectErrorSuppressed(cleanup), []);
483
+ });
484
+ test("a recursive delete is reported, unless the path grows back on its own", () => {
485
+ const real = padded([
486
+ edited("e1", "pad19", "/repo/src/a.ts"),
487
+ ran("b", "e1", "rm -rf src/generated"),
488
+ ]);
489
+ const found = detectDestructiveCommand(real);
490
+ assert.equal(found.length, 1);
491
+ assert.match(found[0].detail, /deleted recursively: src\/generated/);
492
+ const rebuilt = padded([
493
+ edited("e1", "pad19", "/repo/src/a.ts"),
494
+ ran("b", "e1", "rm -rf node_modules dist && npm ci"),
495
+ ]);
496
+ assert.deepEqual(detectDestructiveCommand(rebuilt), []);
497
+ });
498
+ test("the pattern has to be a command, not text a command carries", () => {
499
+ // The corpus is full of both: a grep for the phrase, and a path outside the
500
+ // repository that the pull request has no stake in.
501
+ const quoted = padded([
502
+ edited("e1", "pad19", "/repo/src/a.ts"),
503
+ ran("b", "e1", "grep -rn 'rm -rf' scripts/ && echo 'DROP TABLE users'"),
504
+ ]);
505
+ assert.deepEqual(detectDestructiveCommand(quoted), []);
506
+ const elsewhere = padded([
507
+ edited("e1", "pad19", "/repo/src/a.ts"),
508
+ ran("b", "e1", "rm -rf ~/android"),
509
+ ]);
510
+ assert.deepEqual(detectDestructiveCommand(elsewhere), []);
511
+ });
512
+ test("a destructive database command is reported wherever it ran", () => {
513
+ const dropped = padded([
514
+ edited("e1", "pad19", "/repo/src/a.ts"),
515
+ ran("b", "e1", 'psql -c "DROP DATABASE IF EXISTS shop_dev"'),
516
+ ran("c", "b", 'sqlite3 app.db "DELETE FROM sessions"'),
517
+ ]);
518
+ const found = detectDestructiveCommand(dropped);
519
+ assert.equal(found.length, 2);
520
+ assert.match(found[0].detail, /database object was dropped/);
521
+ assert.match(found[1].detail, /no WHERE clause/);
522
+ });
523
+ test("one git reset is one note, under the category that explains it", () => {
524
+ const session = padded([
525
+ edited("e1", "pad19", "/repo/src/a.ts"),
526
+ ran("b", "e1", "git reset --hard HEAD"),
527
+ ]);
528
+ const categories = runStage0(session).candidates.map((candidate) => candidate.category);
529
+ assert.deepEqual(categories.filter((c) => c === "destructive_command"), []);
530
+ assert.ok(categories.includes("abandoned_approach"));
531
+ });
532
+ /** One Read call, the way the agent looks a file up before touching it. */
533
+ function read(uuid, parentUuid, filePath) {
534
+ return assistant(uuid, parentUuid, [
535
+ { type: "tool_use", id: `read-${uuid}`, name: "Read", input: { file_path: filePath } },
536
+ ]);
537
+ }
538
+ test("an edit written after a long break, on a file read before it, is stale context", () => {
539
+ const records = [read("r", "pad19", "/repo/src/api.ts")];
540
+ // 45 minutes of nothing. One tick is spent by the record that follows.
541
+ clock += 44;
542
+ records.push(edited("e", "r", "/repo/src/api.ts"));
543
+ const candidates = detectStaleContext(padded(records));
544
+ assert.equal(candidates.length, 1);
545
+ assert.equal(candidates[0]?.category, "stale_context");
546
+ assert.equal(candidates[0]?.filePath, "/repo/src/api.ts");
547
+ assert.match(candidates[0].detail, /a 45m break and edited api\.ts/);
548
+ });
549
+ test("one note per break, however many files the agent went on to edit", () => {
550
+ const records = [read("r1", "pad19", "/repo/src/a.ts"), read("r2", "r1", "/repo/src/b.ts")];
551
+ clock += 44;
552
+ records.push(edited("e1", "r2", "/repo/src/a.ts"));
553
+ records.push(edited("e2", "e1", "/repo/src/b.ts"));
554
+ assert.equal(detectStaleContext(padded(records)).length, 1);
555
+ });
556
+ test("many breaks in one session are one note that says how many", () => {
557
+ const records = [read("r1", "pad19", "/repo/src/a.ts"), read("r2", "r1", "/repo/src/b.ts")];
558
+ clock += 44;
559
+ records.push(edited("e1", "r2", "/repo/src/a.ts"));
560
+ // A second break, and the agent picks up b.ts on what it read before both.
561
+ clock += 90;
562
+ records.push(edited("e2", "e1", "/repo/src/b.ts"));
563
+ const candidates = detectStaleContext(padded(records));
564
+ assert.equal(candidates.length, 1);
565
+ // The longest break is the one printed, and both files are named.
566
+ assert.match(candidates[0].detail, /a 1h31 break and edited a\.ts, b\.ts without reading them again/);
567
+ assert.match(candidates[0].detail, /2 breaks in this session/);
568
+ assert.equal(candidates[0]?.filePath, "/repo/src/b.ts");
569
+ });
570
+ test("a break is only stale context when the agent came back without looking again", () => {
571
+ const rechecked = [read("r", "pad19", "/repo/src/api.ts")];
572
+ clock += 44;
573
+ rechecked.push(read("r2", "r", "/repo/src/api.ts"));
574
+ rechecked.push(edited("e", "r2", "/repo/src/api.ts"));
575
+ assert.deepEqual(detectStaleContext(padded(rechecked)), []);
576
+ // Never read at all is `unverified_assumption`, and one edit must not be two notes.
577
+ const blind = [ran("b", "pad19", "npm test")];
578
+ clock += 44;
579
+ blind.push(edited("e", "b", "/repo/src/api.ts"));
580
+ assert.deepEqual(detectStaleContext(padded(blind)), []);
581
+ // A few minutes is the developer reading the diff before approving the call.
582
+ const brief = [read("r", "pad19", "/repo/src/api.ts")];
583
+ clock += 5;
584
+ brief.push(edited("e", "r", "/repo/src/api.ts"));
585
+ assert.deepEqual(detectStaleContext(padded(brief)), []);
586
+ });
587
+ test("SQL inside a search pattern or a substitution is text, not a migration", () => {
588
+ // Both shapes come straight off the corpus: an alternation in a grep pattern,
589
+ // and a grep whose output is an argument to something else.
590
+ const alternation = padded([
591
+ edited("e1", "pad19", "/repo/src/a.ts"),
592
+ ran("b", "e1", 'grep -rn "DELETE FROM|DROP TABLE" src'),
593
+ ]);
594
+ assert.deepEqual(detectDestructiveCommand(alternation), []);
595
+ const substitution = padded([
596
+ edited("e1", "pad19", "/repo/src/a.ts"),
597
+ ran("b", "e1", 'node check.js $(grep -rl "DROP INDEX" .)'),
598
+ ]);
599
+ assert.deepEqual(detectDestructiveCommand(substitution), []);
600
+ // The real thing still reports.
601
+ const real = padded([
602
+ edited("e1", "pad19", "/repo/src/a.ts"),
603
+ ran("b", "e1", 'sqlite3 data/app.db "DELETE FROM sessions;"'),
604
+ ]);
605
+ assert.equal(detectDestructiveCommand(real).length, 1);
606
+ });
607
+ //# sourceMappingURL=stage0.test.js.map