@nightloom/isy 1.0.5 → 1.0.6-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 +17 -2
  2. package/dist/agents/claude.js +3 -2
  3. package/dist/agents/claude.js.map +1 -1
  4. package/dist/agents/codex.js +14 -5
  5. package/dist/agents/codex.js.map +1 -1
  6. package/dist/agents/index.d.ts +14 -10
  7. package/dist/agents/index.js +65 -15
  8. package/dist/agents/index.js.map +1 -1
  9. package/dist/agents/kimi.js +26 -8
  10. package/dist/agents/kimi.js.map +1 -1
  11. package/dist/agents/types.d.ts +12 -3
  12. package/dist/commands/commit.d.ts +6 -0
  13. package/dist/commands/commit.js +326 -16
  14. package/dist/commands/commit.js.map +1 -1
  15. package/dist/commands/health.js +1 -1
  16. package/dist/commands/health.js.map +1 -1
  17. package/dist/commands/init.js +30 -5
  18. package/dist/commands/init.js.map +1 -1
  19. package/dist/commands/status.d.ts +2 -0
  20. package/dist/commands/status.js +7 -4
  21. package/dist/commands/status.js.map +1 -1
  22. package/dist/commands/upload.d.ts +8 -1
  23. package/dist/commands/upload.js +46 -39
  24. package/dist/commands/upload.js.map +1 -1
  25. package/dist/detectors.d.ts +32 -0
  26. package/dist/detectors.js +279 -72
  27. package/dist/detectors.js.map +1 -1
  28. package/dist/git.d.ts +52 -0
  29. package/dist/git.js +166 -9
  30. package/dist/git.js.map +1 -1
  31. package/dist/githook.d.ts +18 -7
  32. package/dist/githook.js +147 -17
  33. package/dist/githook.js.map +1 -1
  34. package/dist/hook.d.ts +22 -2
  35. package/dist/hook.js +28 -3
  36. package/dist/hook.js.map +1 -1
  37. package/dist/index.js +14 -5
  38. package/dist/index.js.map +1 -1
  39. package/dist/mask-paths.d.ts +4 -3
  40. package/dist/mask-paths.js +4 -3
  41. package/dist/mask-paths.js.map +1 -1
  42. package/dist/parser.d.ts +58 -9
  43. package/dist/parser.js +337 -58
  44. package/dist/parser.js.map +1 -1
  45. package/dist/paths.d.ts +19 -1
  46. package/dist/paths.js +28 -3
  47. package/dist/paths.js.map +1 -1
  48. package/dist/redact.d.ts +2 -0
  49. package/dist/redact.js +956 -97
  50. package/dist/redact.js.map +1 -1
  51. package/dist/stage0.js +27 -8
  52. package/dist/stage0.js.map +1 -1
  53. package/dist/state.d.ts +15 -0
  54. package/dist/state.js +8 -0
  55. package/dist/state.js.map +1 -1
  56. package/dist/test/check.test.js +1 -1
  57. package/dist/test/check.test.js.map +1 -1
  58. package/dist/test/codex.test.js +27 -4
  59. package/dist/test/codex.test.js.map +1 -1
  60. package/dist/test/commit.test.js +1466 -6
  61. package/dist/test/commit.test.js.map +1 -1
  62. package/dist/test/e2e-harness.test.d.ts +1 -0
  63. package/dist/test/e2e-harness.test.js +53 -0
  64. package/dist/test/e2e-harness.test.js.map +1 -0
  65. package/dist/test/git.test.js +72 -2
  66. package/dist/test/git.test.js.map +1 -1
  67. package/dist/test/hook.test.js +138 -3
  68. package/dist/test/hook.test.js.map +1 -1
  69. package/dist/test/init.test.js +60 -1
  70. package/dist/test/init.test.js.map +1 -1
  71. package/dist/test/kimi-config.test.js +27 -5
  72. package/dist/test/kimi-config.test.js.map +1 -1
  73. package/dist/test/kimi-hook.test.js +1 -1
  74. package/dist/test/kimi-hook.test.js.map +1 -1
  75. package/dist/test/kimi-listing.test.js +42 -2
  76. package/dist/test/kimi-listing.test.js.map +1 -1
  77. package/dist/test/kimi.test.js +26 -2
  78. package/dist/test/kimi.test.js.map +1 -1
  79. package/dist/test/parser.test.js +527 -1
  80. package/dist/test/parser.test.js.map +1 -1
  81. package/dist/test/redact.test.js +2442 -14
  82. package/dist/test/redact.test.js.map +1 -1
  83. package/dist/test/stage0.test.js +617 -1
  84. package/dist/test/stage0.test.js.map +1 -1
  85. package/dist/test/sweep.test.js +99 -3
  86. package/dist/test/sweep.test.js.map +1 -1
  87. package/package.json +1 -1
  88. package/src/agents/claude.ts +3 -2
  89. package/src/agents/codex.ts +14 -5
  90. package/src/agents/index.ts +71 -16
  91. package/src/agents/kimi.ts +28 -8
  92. package/src/agents/types.ts +12 -3
  93. package/src/commands/commit.ts +354 -18
  94. package/src/commands/health.ts +1 -1
  95. package/src/commands/init.ts +37 -7
  96. package/src/commands/status.ts +9 -4
  97. package/src/commands/upload.ts +54 -54
  98. package/src/detectors.ts +284 -75
  99. package/src/git.ts +175 -9
  100. package/src/githook.ts +151 -19
  101. package/src/hook.ts +30 -3
  102. package/src/index.ts +15 -5
  103. package/src/mask-paths.ts +5 -4
  104. package/src/parser.ts +355 -51
  105. package/src/paths.ts +30 -3
  106. package/src/redact.ts +959 -100
  107. package/src/stage0.ts +28 -8
  108. package/src/state.ts +18 -0
package/dist/detectors.js CHANGED
@@ -60,22 +60,48 @@ export function weakensTest(oldString, newString) {
60
60
  }
61
61
  return undefined;
62
62
  }
63
- function failedBefore(session, recordIndex) {
64
- return session.toolUses.find((use) => {
65
- if (use.name !== "Bash")
66
- return false;
67
- if (use.recordIndex >= recordIndex)
68
- return false;
69
- if (use.recordIndex < recordIndex - FAILURE_LOOKBACK_RECORDS)
70
- return false;
71
- const result = use.result;
72
- if (!result)
73
- return false;
74
- return (result.isError ||
63
+ function saysItFailed(use) {
64
+ const result = use.result;
65
+ return (result !== undefined &&
66
+ (result.isError ||
75
67
  FAILURE_OUTPUT.test(result.stdout ?? "") ||
76
68
  FAILURE_OUTPUT.test(result.stderr ?? "") ||
77
- FAILURE_OUTPUT.test(result.text));
78
- });
69
+ FAILURE_OUTPUT.test(result.text)));
70
+ }
71
+ /**
72
+ * A session's shell calls by record, and the first of them to fail once an edit has
73
+ * asked. Only what an edit reaches is read, and each record once: every block of a record
74
+ * can carry that record's one output, which can run to megabytes, and thousands of edits
75
+ * can ask about 100000 calls. The parse is over by the time a detector runs, so what a
76
+ * call came to no longer changes.
77
+ */
78
+ const shellCalls = new WeakMap();
79
+ function failedBefore(session, recordIndex) {
80
+ let records = shellCalls.get(session);
81
+ if (records === undefined) {
82
+ records = new Map();
83
+ for (const use of session.toolUses) {
84
+ if (use.name !== "Bash")
85
+ continue;
86
+ const record = records.get(use.recordIndex);
87
+ if (record === undefined)
88
+ records.set(use.recordIndex, { calls: [use] });
89
+ else
90
+ record.calls.push(use);
91
+ }
92
+ shellCalls.set(session, records);
93
+ }
94
+ // Earliest first, as a finding starts from it.
95
+ for (let back = FAILURE_LOOKBACK_RECORDS; back >= 1; back -= 1) {
96
+ const record = records.get(recordIndex - back);
97
+ if (record === undefined)
98
+ continue;
99
+ if (record.failing === undefined)
100
+ record.failing = record.calls.find(saysItFailed) ?? null;
101
+ if (record.failing !== null)
102
+ return record.failing;
103
+ }
104
+ return undefined;
79
105
  }
80
106
  export function detectTestModifiedToPass(session) {
81
107
  const candidates = [];
@@ -131,14 +157,55 @@ export function detectTestModifiedToPass(session) {
131
157
  // Code writes one replacement, which is what made the fan-out visible.
132
158
  return dedupe(candidates);
133
159
  }
134
- function applyEdit(content, oldString, newString, all) {
135
- if (oldString.length === 0 || !content.includes(oldString))
160
+ // What following a session's files may cost, in characters walked: each edit
161
+ // walks its file and digests the result, so a file doubled by replace_all, or
162
+ // edited thousands of times at full size, took the heap or many minutes. The
163
+ // most in a real session, of 518 MB of transcript, was 10.6 million.
164
+ // ponytail: past this no file is followed and later abandoned approaches go unseen; a budget per file if one file should not blind the rest.
165
+ const TRACKED_CHARS = 32 * 1024 * 1024;
166
+ // `text.split(pattern)`, or only around the first occurrence, in time
167
+ // proportional to both: the engine's own search walks a long pattern that almost
168
+ // repeats again from every position, half a minute for 100 KB over a 1 MB file.
169
+ function piecesAround(text, pattern, all) {
170
+ const fallback = new Int32Array(pattern.length);
171
+ for (let at = 1, matched = 0; at < pattern.length; at += 1) {
172
+ while (matched > 0 && pattern.charCodeAt(at) !== pattern.charCodeAt(matched))
173
+ matched = fallback[matched - 1];
174
+ if (pattern.charCodeAt(at) === pattern.charCodeAt(matched))
175
+ matched += 1;
176
+ fallback[at] = matched;
177
+ }
178
+ const pieces = [];
179
+ let end = 0;
180
+ for (let at = 0, matched = 0; at < text.length; at += 1) {
181
+ while (matched > 0 && text.charCodeAt(at) !== pattern.charCodeAt(matched))
182
+ matched = fallback[matched - 1];
183
+ if (text.charCodeAt(at) === pattern.charCodeAt(matched))
184
+ matched += 1;
185
+ if (matched === pattern.length) {
186
+ pieces.push(text.slice(end, at + 1 - matched));
187
+ end = at + 1;
188
+ matched = 0;
189
+ if (!all)
190
+ break;
191
+ }
192
+ }
193
+ pieces.push(text.slice(end));
194
+ return pieces;
195
+ }
196
+ // Literal, as the edit tools are, and undefined when the text is not there or
197
+ // the result would not fit in `room`, which is known before it is built.
198
+ function applyEdit(content, oldString, newString, all, room) {
199
+ if (oldString.length === 0 || oldString.length > content.length || room < 0)
136
200
  return undefined;
137
- return all ? content.replaceAll(oldString, newString) : content.replace(oldString, newString);
201
+ const pieces = piecesAround(content, oldString, all);
202
+ const length = content.length + (pieces.length - 1) * (newString.length - oldString.length);
203
+ return pieces.length === 1 || length > room ? undefined : pieces.join(newString);
138
204
  }
139
205
  function trackContent(session) {
140
206
  const candidates = [];
141
207
  const states = new Map();
208
+ let room = TRACKED_CHARS;
142
209
  const state = (filePath) => {
143
210
  let existing = states.get(filePath);
144
211
  if (!existing) {
@@ -150,6 +217,11 @@ function trackContent(session) {
150
217
  const record = (file, filePath, use, isEdit) => {
151
218
  if (file.content === undefined)
152
219
  return;
220
+ room -= file.content.length;
221
+ if (room < 0) {
222
+ file.content = undefined;
223
+ return;
224
+ }
153
225
  const key = digest(file.content);
154
226
  const firstSeen = file.seen.get(key);
155
227
  const unchanged = key === file.lastKey;
@@ -202,24 +274,27 @@ function trackContent(session) {
202
274
  file.content = undefined;
203
275
  break;
204
276
  }
205
- file.content = applyEdit(file.content, from, to, use.input.replace_all === true);
277
+ room -= file.content.length + from.length;
278
+ file.content = applyEdit(file.content, from, to, use.input.replace_all === true, room);
206
279
  }
207
280
  record(file, filePath, use, true);
208
281
  }
209
282
  }
210
283
  return candidates;
211
284
  }
285
+ /** An edit's two sides as one key. A digest, since past 16 KB a string hashes by its length alone, and a set of those is a walk. */
286
+ function sidesOf(from, to) {
287
+ return createHash("sha1").update(`${from.length}:${from}${to}`).digest("hex");
288
+ }
212
289
  function detectRevertPairs(session) {
213
290
  const candidates = [];
214
291
  for (const [filePath, edits] of session.fileEdits) {
215
- for (let later = 1; later < edits.length; later += 1) {
216
- const current = edits[later];
292
+ const earlier = new Set();
293
+ for (const current of edits) {
217
294
  if (current.oldString === undefined || current.newString === undefined)
218
295
  continue;
219
- const reverted = edits.slice(0, later).some((earlier) => earlier.oldString !== undefined &&
220
- earlier.newString !== undefined &&
221
- earlier.oldString === current.newString &&
222
- earlier.newString === current.oldString);
296
+ const reverted = earlier.has(sidesOf(current.newString, current.oldString));
297
+ earlier.add(sidesOf(current.oldString, current.newString));
223
298
  if (!reverted)
224
299
  continue;
225
300
  candidates.push({
@@ -240,7 +315,7 @@ function detectRevertPairs(session) {
240
315
  }
241
316
  function revertTargets(command) {
242
317
  if (GIT_RESET.test(command) || GIT_STASH.test(command)) {
243
- return { everything: true, paths: [] };
318
+ return { everything: true, names: new Set() };
244
319
  }
245
320
  const match = GIT_CHECKOUT.exec(command) ?? GIT_RESTORE.exec(command);
246
321
  const raw = match?.groups?.paths;
@@ -249,18 +324,18 @@ function revertTargets(command) {
249
324
  const paths = raw
250
325
  .trim()
251
326
  .split(/\s+/)
252
- .filter((part) => part.length > 0 && !part.startsWith("-"));
253
- return paths.length > 0 ? { everything: false, paths } : undefined;
254
- }
255
- function matchesPath(filePath, target) {
256
- const cleaned = target.replace(/^\.\//, "").replace(/["']/g, "");
257
- if (cleaned === "." || cleaned === "*")
258
- return true;
259
- return filePath.endsWith(`/${cleaned}`) || basename(filePath) === basename(cleaned);
327
+ .filter((part) => part.length > 0 && !part.startsWith("-"))
328
+ .map((part) => part.replace(/^\.\//, "").replace(/["']/g, ""));
329
+ if (paths.length === 0)
330
+ return undefined;
331
+ // A file goes by its last part, so a command takes one look per file whatever it lists.
332
+ return { everything: paths.includes(".") || paths.includes("*"), names: new Set(paths.map((path) => basename(path))) };
260
333
  }
261
334
  const REVERT_FILES_LISTED = 5;
262
335
  function detectGitReverts(session) {
263
336
  const candidates = [];
337
+ // Once each, as a long path walked again for every discard is what takes the time.
338
+ const files = [...session.fileEdits].map(([filePath, edits]) => ({ filePath, edits, name: basename(filePath) }));
264
339
  for (const use of session.toolUses) {
265
340
  if (use.name !== "Bash")
266
341
  continue;
@@ -272,15 +347,14 @@ function detectGitReverts(session) {
272
347
  continue;
273
348
  const discarded = [];
274
349
  let startedAt;
275
- for (const [filePath, edits] of session.fileEdits) {
276
- const earlier = edits.filter((edit) => edit.recordIndex < use.recordIndex);
277
- if (earlier.length === 0)
350
+ for (const { filePath, edits, name } of files) {
351
+ const earliest = edits.find((edit) => edit.recordIndex < use.recordIndex);
352
+ if (!earliest)
278
353
  continue;
279
- if (!targets.everything && !targets.paths.some((path) => matchesPath(filePath, path))) {
354
+ if (!targets.everything && !targets.names.has(name))
280
355
  continue;
281
- }
282
356
  discarded.push(filePath);
283
- const first = earlier[0]?.timestamp;
357
+ const first = earliest.timestamp;
284
358
  if (first && (!startedAt || first < startedAt))
285
359
  startedAt = first;
286
360
  }
@@ -310,9 +384,10 @@ function detectAbandonedBranches(session) {
310
384
  const candidates = [];
311
385
  if (session.mainPath.size === 0)
312
386
  return candidates;
387
+ const walked = new Map();
313
388
  for (const [filePath, edits] of session.fileEdits) {
314
389
  for (const edit of edits) {
315
- if (!onAbandonedBranch(session, edit.uuid))
390
+ if (!onAbandonedBranch(session, edit.uuid, walked))
316
391
  continue;
317
392
  candidates.push({
318
393
  category: "abandoned_approach",
@@ -428,11 +503,12 @@ function jsonDependencies(text) {
428
503
  }
429
504
  function otherDependencies(text) {
430
505
  const names = new Set();
506
+ // A line's indentation is `\s` short of what ends a line: from each line, `\s` would walk all the blank ones after it.
431
507
  const patterns = [
432
- /^\s*([A-Za-z0-9._-]+)\s*(?:==|>=|<=|~=|!=)/gm,
433
- /^\s*gem\s+["']([^"']+)["']/gm,
508
+ /^[^\S\r\n\u2028\u2029]*([A-Za-z0-9._-]+)\s*(?:==|>=|<=|~=|!=)/gm,
509
+ /^[^\S\r\n\u2028\u2029]*gem\s+["']([^"']+)["']/gm,
434
510
  /<artifactId>([^<]+)<\/artifactId>/g,
435
- /^\s*(?:require\s+)?([a-z0-9.-]+\.[a-z]{2,}\/[^\s]+)\s+v[0-9]/gm,
511
+ /^[^\S\r\n\u2028\u2029]*(?:require\s+)?([a-z0-9.-]+\.[a-z]{2,}\/[^\s]+)\s+v[0-9]/gm,
436
512
  /(?:implementation|api|testImplementation)\s+["']([^"':]+:[^"':]+)/g,
437
513
  ];
438
514
  for (const pattern of patterns) {
@@ -451,10 +527,11 @@ const TOML_DEPENDENCY_SUBTABLE = /(?:^|\.)(?:dev-|build-)?dependencies\.([A-Za-z
451
527
  const TOML_REQUIREMENT_TABLES = new Set(["project.optional-dependencies", "dependency-groups"]);
452
528
  /** Keys a TOML manifest gives a version, none of them a package. */
453
529
  const TOML_VERSION_KEYS = new Set(["python", "version", "edition", "rust-version", "requires-python"]);
454
- const TOML_HEADER = /^\s*\[\[?\s*([^\]]+?)\s*\]\]?\s*(?:#.*)?$/;
455
- const TOML_ASSIGNMENT = /^\s*["']?([A-Za-z0-9._-]+)["']?\s*=\s*(.*)$/gm;
530
+ /** The name comes with its spaces, which its reader takes out: looked for here, they are a cubic walk of an open table. */
531
+ const TOML_HEADER = /^\s*\[\[?([^\]]+)\]\]?\s*(?:#.*)?$/;
532
+ const TOML_ASSIGNMENT = /^[^\S\r\n\u2028\u2029]*["']?([A-Za-z0-9._-]+)["']?\s*=\s*(.*)$/gm;
456
533
  /** `key = [ … ]`, across lines, a `]` inside a quoted requirement (`"requests[socks]"`) included. */
457
- const TOML_ARRAY = /^\s*["']?([A-Za-z0-9_-]+)["']?\s*=\s*\[((?:"[^"]*"|'[^']*'|[^\]"'])*)/gm;
534
+ const TOML_ARRAY = /^[^\S\r\n\u2028\u2029]*["']?([A-Za-z0-9_-]+)["']?\s*=\s*\[((?:"[^"]*"|'[^']*'|[^\]"'])*)/gm;
458
535
  /** Package names out of the body of a TOML array of PEP 508 requirement strings. */
459
536
  function requirementNames(array) {
460
537
  const names = [];
@@ -558,7 +635,7 @@ function packagesInSegment(segment, afterIndex) {
558
635
  continue;
559
636
  }
560
637
  const cleaned = token.replace(/["']/g, "");
561
- const trimmed = cleaned.replace(/[.,;:]+$/, "");
638
+ const trimmed = cleaned.replace(/(?<![.,;:])[.,;:]+$/, "");
562
639
  // A registry name, not whatever else stood in that position: a line
563
640
  // continuation, a shell expansion whose value only the shell knows, a
564
641
  // stray brace from prose the command happened to carry.
@@ -643,34 +720,101 @@ export function detectExternalDependency(session) {
643
720
  * way round, a directory matches everything under it, and a glob from
644
721
  * `cat src/*.js` matches every file it could have expanded to.
645
722
  */
646
- function samePath(known, filePath) {
723
+ function samePath(known, filePath, budget) {
647
724
  if (known === filePath)
648
725
  return true;
726
+ budget.left -= known.length / CHARS_PER_STEP;
727
+ if (budget.left <= 0)
728
+ return false;
649
729
  if (/[*?]/.test(known))
650
- return globMatches(known, filePath);
730
+ return globMatches(known, filePath, budget);
651
731
  if (filePath.startsWith(known.endsWith("/") ? known : `${known}/`))
652
732
  return true;
653
733
  if (!filePath.startsWith("/") && known.endsWith(`/${filePath}`))
654
734
  return true;
655
735
  return !known.startsWith("/") && filePath.endsWith(`/${known}`);
656
736
  }
657
- const GLOB_PARTS = { "?": "[^/]", "*": "[^/]*", "**": ".*", "**/": "(?:.*/)?" };
658
- /** `*` and `?` within one path segment, `**` across them; a relative glob matches by suffix, like a relative path. */
659
- function globMatches(glob, filePath) {
660
- const body = glob
661
- .replace(/[.+^${}()|[\]\\]/g, "\\$&")
662
- .replace(/\*\*\/?|\*|\?/g, (wildcard) => GLOB_PARTS[wildcard]);
663
- return new RegExp(glob.startsWith("/") ? `^${body}$` : `(?:^|/)${body}$`).test(filePath);
737
+ // What holding reads up to a session's edited files may cost, in steps, where a
738
+ // long glob over a long path takes millions each. Any other read walks as far as
739
+ // the two paths agree, at a fraction of a step a character. The most in a real
740
+ // session was 350 thousand steps of glob and 7.4 million characters.
741
+ // ponytail: past this only the path itself counts, so what else was read counts as unread; matching in linear time if a real session comes near.
742
+ const GLOB_STEPS = 20_000_000;
743
+ const CHARS_PER_STEP = 10;
744
+ const STAR = 42;
745
+ const QUESTION = 63;
746
+ const SLASH = 47;
747
+ /**
748
+ * `*` and `?` within one path segment, `**` across them; a relative glob matches
749
+ * by suffix, like a relative path. It goes back only to the last star of each
750
+ * kind, since what an earlier one could take a later one can too: compiled to a
751
+ * pattern, a dozen stars over 100 characters never came back. For that a `**\/`
752
+ * stands for no directories only as a whole segment; `a**\/b` needs its slash.
753
+ */
754
+ function globMatches(glob, filePath, budget) {
755
+ let g = 0;
756
+ let at = 0;
757
+ let star = -1;
758
+ let starAt = 0;
759
+ let across = glob.startsWith("/") ? -1 : 0;
760
+ let acrossAt = 0;
761
+ let afterSlash = across === 0;
762
+ while (budget.left > 0) {
763
+ budget.left -= 1;
764
+ if (g < glob.length) {
765
+ const char = glob.charCodeAt(g);
766
+ if (char === STAR && glob.charCodeAt(g + 1) === STAR) {
767
+ afterSlash = glob.charCodeAt(g + 2) === SLASH && (g === 0 || glob.charCodeAt(g - 1) === SLASH);
768
+ g += afterSlash ? 3 : 2;
769
+ across = g;
770
+ acrossAt = at;
771
+ star = -1;
772
+ continue;
773
+ }
774
+ if (char === STAR) {
775
+ g += 1;
776
+ star = g;
777
+ starAt = at;
778
+ continue;
779
+ }
780
+ if (at < filePath.length && (char === QUESTION ? filePath.charCodeAt(at) !== SLASH : filePath.charCodeAt(at) === char)) {
781
+ g += 1;
782
+ at += 1;
783
+ continue;
784
+ }
785
+ }
786
+ else if (at === filePath.length) {
787
+ return true;
788
+ }
789
+ if (star !== -1 && starAt < filePath.length && filePath.charCodeAt(starAt) !== SLASH) {
790
+ starAt += 1;
791
+ g = star;
792
+ at = starAt;
793
+ continue;
794
+ }
795
+ if (across === -1)
796
+ return false;
797
+ const next = afterSlash ? filePath.indexOf("/", acrossAt) + 1 : acrossAt + 1;
798
+ if (next === 0 || next > filePath.length)
799
+ return false;
800
+ budget.left -= next - acrossAt;
801
+ acrossAt = next;
802
+ g = across;
803
+ at = next;
804
+ star = -1;
805
+ }
806
+ return false;
664
807
  }
665
- function wasLookedUp(session, filePath) {
808
+ function wasLookedUp(session, filePath, budget) {
666
809
  for (const known of session.filesRead) {
667
- if (samePath(known, filePath))
810
+ if (samePath(known, filePath, budget))
668
811
  return true;
669
812
  }
670
813
  return false;
671
814
  }
672
815
  export function detectUnverifiedAssumption(session) {
673
816
  const candidates = [];
817
+ const globSteps = { left: GLOB_STEPS };
674
818
  for (const [filePath, edits] of session.fileEdits) {
675
819
  const first = edits[0];
676
820
  if (!first)
@@ -678,7 +822,7 @@ export function detectUnverifiedAssumption(session) {
678
822
  // `sed -i` edits in place like Edit does, on the same picture of the file.
679
823
  if (first.tool !== "Edit" && first.tool !== "MultiEdit" && first.tool !== "Bash")
680
824
  continue;
681
- if (wasLookedUp(session, filePath))
825
+ if (wasLookedUp(session, filePath, globSteps))
682
826
  continue;
683
827
  candidates.push({
684
828
  category: "unverified_assumption",
@@ -761,9 +905,16 @@ export function detectUnverifiedFix(session) {
761
905
  /**
762
906
  * Silencing rather than fixing. `test_modified_to_pass` covers the test-file
763
907
  * half of this; here it is production code, where a suppression comment has no
764
- * failing test to explain it and simply removes the warning from view.
908
+ * failing test to explain it and simply removes the warning from view. Whole
909
+ * files go through these, so every run is bounded or has one way to match.
910
+ *
911
+ * ponytail: an `except` tuple or its comment past 1000 characters goes unread,
912
+ * and so does a blank run past 200. Raise them if one shows up: 1 MB built to
913
+ * stall these takes most of a second as it is. A catch's parameter needs no bound:
914
+ * no scan goes past the next `(`, so they cannot pile up, and a parameter holding
915
+ * one was no match anyway.
765
916
  */
766
- const SUPPRESSORS = [
917
+ export const SUPPRESSORS = [
767
918
  { pattern: /@ts-(?:ignore|nocheck|expect-error)\b/g, detail: "a TypeScript error was suppressed" },
768
919
  { pattern: /eslint-disable(?:-next-line|-line)?\b/g, detail: "an ESLint rule was disabled" },
769
920
  { pattern: /#\s*type:\s*ignore\b/g, detail: "a type error was suppressed" },
@@ -771,14 +922,36 @@ const SUPPRESSORS = [
771
922
  { pattern: /#\s*pylint:\s*disable\b/g, detail: "a pylint rule was disabled" },
772
923
  { pattern: /@SuppressWarnings\b/g, detail: "a compiler warning was suppressed" },
773
924
  { pattern: /#\[allow\(/g, detail: "a Rust lint was allowed" },
774
- { pattern: /except[^\n:]*:\s*(?:#[^\n]*)?\n\s*pass\b/g, detail: "an exception is caught and dropped" },
775
- { pattern: /catch\s*(?:\([^)]*\))?\s*\{\s*\}/g, detail: "an exception is caught and dropped" },
776
- { pattern: /rescue\s*(?:=>\s*\w+\s*)?\n\s*end\b/g, detail: "an exception is caught and dropped" },
925
+ { pattern: /except(?:(?!except)[^\n:]){0,1000}:\s{0,200}(?:#[^\n]{0,1000})?\n\s{0,200}pass\b/g, detail: "an exception is caught and dropped" },
926
+ { pattern: /catch\s{0,200}(?:\([^()]*\)\s{0,200})?\{\s{0,200}\}/g, detail: "an exception is caught and dropped" },
927
+ { pattern: /rescue\s{0,200}(?:=>\s{0,200}\w+\s{0,200})?\n\s{0,200}end\b/g, detail: "an exception is caught and dropped" },
928
+ ];
929
+ /**
930
+ * A catch that only logs, and returns nothing much if it returns at all: the
931
+ * failure is still there, and the check that reported it goes green. Everyday
932
+ * code otherwise, so these count only right after a check failed. Python's needs
933
+ * the return: read line by line, a log call alone could be the start of a real
934
+ * handler. Whole files go through these, so every run is bounded or has one way
935
+ * to match.
936
+ *
937
+ * ponytail: a quote earlier on the line is what says `catch` sits in a string. A
938
+ * real catch after a string on its own line goes unread, and a sample spanning
939
+ * lines of a template literal reads as code. Tokenize the file if either shows up.
940
+ */
941
+ export const LOG_ONLY_CATCHES = [
942
+ {
943
+ pattern: /\bcatch(?<!["'`][^\n]{0,200}catch)\s*(?:\([^)]{0,200}\)\s*)?\{(?:[ \t\r\n]{0,200}\/\/[^\n]{0,400}\n){0,20}\s*(?:this\.)?(?:console|logger|log)\.(?:warn|warning|error|info|debug|log)\((?:[^;\n]{0,400}|[ \t]*\n[^;]{0,400}\n[ \t]*)\)(?:\s*;)?\s*(?:return(?:\s+(?:null|undefined|false|\[\]|\{\}))?(?:\s*;)?\s*)?\}/g,
944
+ detail: "an exception is caught and only logged",
945
+ },
946
+ {
947
+ pattern: /^[ \t]*except\b[^\n:]{0,200}:[ \t]*(?:#[^\n]*)?\n(?:[ \t]*#[^\n]*\n)*[ \t]*(?:print|(?:self\.)?(?:logger|logging|log)\.(?:warning|warn|error|info|debug|exception)|warnings\.warn)\([^\n]{0,400}\)[ \t]*\n[ \t]*return(?:[ \t]+(?:None|False|\[\]|\{\}))?[ \t]*(?:#[^\n]*)?$/gm,
948
+ detail: "an exception is caught and only logged",
949
+ },
777
950
  ];
778
951
  /** `|| true` on a check, and the flags that walk past a failing hook. */
779
952
  const SUPPRESSING_FLAG = /--no-verify\b|--ignore-errors\b|--exit-zero\b/;
780
- function suppressionAdded(oldString, newString) {
781
- for (const { pattern, detail } of SUPPRESSORS) {
953
+ function suppressionAdded(oldString, newString, rules = SUPPRESSORS) {
954
+ for (const { pattern, detail } of rules) {
782
955
  // Counted, not tested: an edit that keeps one suppression and adds a second
783
956
  // is still an addition, and the file may have arrived with one already.
784
957
  if (countMatches(newString, pattern) > countMatches(oldString, pattern))
@@ -786,8 +959,34 @@ function suppressionAdded(oldString, newString) {
786
959
  }
787
960
  return undefined;
788
961
  }
962
+ /**
963
+ * A check that failed by its exit status, shortly before. Narrower than
964
+ * `failedBefore`, for a rule with nothing else to go on: that one's word reads
965
+ * `0 failed` and a `git log` as failures, and a grep that found nothing exits 1
966
+ * too, with the name of a check as its pattern as often as not. A failure piped
967
+ * through `tail` exits 0 and goes unread here.
968
+ */
969
+ function failedChecks(session) {
970
+ const records = new Set();
971
+ for (const use of session.toolUses) {
972
+ if (use.result?.isError === true &&
973
+ verifies(use) &&
974
+ commandSegments(withoutHeredocs(use.input.command)).some((segment) => VERIFY_COMMAND.test(segment) && !READER.test(segment))) {
975
+ records.add(use.recordIndex);
976
+ }
977
+ }
978
+ return records;
979
+ }
980
+ function checkFailedBefore(failed, recordIndex) {
981
+ for (let back = 1; back <= FAILURE_LOOKBACK_RECORDS; back += 1) {
982
+ if (failed.has(recordIndex - back))
983
+ return true;
984
+ }
985
+ return false;
986
+ }
789
987
  export function detectErrorSuppressed(session) {
790
988
  const candidates = [];
989
+ const failed = failedChecks(session);
791
990
  for (const [filePath, edits] of session.fileEdits) {
792
991
  // A weakened test is the same act with its own category and its own weight.
793
992
  if (isTestFile(filePath))
@@ -797,7 +996,13 @@ export function detectErrorSuppressed(session) {
797
996
  const after = edit.newString ?? edit.content ?? "";
798
997
  if (after.length === 0)
799
998
  continue;
800
- const reason = suppressionAdded(before, after);
999
+ // Taken out of this same edit, a suppression makes a log line the better
1000
+ // of the two: a silent catch given one hides less than it did. What a
1001
+ // whole-file write replaced is not in the transcript, so that reads as new.
1002
+ const logOnly = CODE_EXTENSION.test(filePath) && checkFailedBefore(failed, edit.recordIndex) && !suppressionAdded(after, before)
1003
+ ? suppressionAdded(before, after, LOG_ONLY_CATCHES)
1004
+ : undefined;
1005
+ const reason = suppressionAdded(before, after) ?? logOnly;
801
1006
  if (!reason)
802
1007
  continue;
803
1008
  const failure = failedBefore(session, edit.recordIndex);
@@ -850,8 +1055,9 @@ export function detectErrorSuppressed(session) {
850
1055
  const DESTRUCTIVE = [
851
1056
  { pattern: /\bDROP\s+(TABLE|DATABASE|SCHEMA)\b/i, weight: 0.8, detail: "a database object was dropped" },
852
1057
  { pattern: /\bTRUNCATE\s+(TABLE\s+)?\w/i, weight: 0.8, detail: "a table was truncated" },
853
- { pattern: /\bDELETE\s+FROM\s+\w+\s*["']?\s*(?:;|$)/i, weight: 0.8, detail: "rows were deleted with no WHERE clause" },
854
- { pattern: /\bgit\s+push\s+[^\n;|&]*(?:--force\b(?!-with-lease)|(?:^|\s)-f\b)/, weight: 0.7, detail: "a branch was force-pushed" },
1058
+ { pattern: /\bDELETE\s+FROM\s+\w+\s*(?:["']\s*)?(?:;|$)/i, weight: 0.8, detail: "rows were deleted with no WHERE clause" },
1059
+ // Only as far as the next push, which looks on from there, short of one with its `-f` right behind it: that flag wants a space of its own.
1060
+ { pattern: /\bgit\s+push\s(?:(?!\bgit\s+push\s(?!-f\b))[^\n;|&])*(?:--force\b(?!-with-lease)|(?:^|\s)-f\b)/, weight: 0.7, detail: "a branch was force-pushed" },
855
1061
  { pattern: /\bgit\s+clean\s+-[a-z]*[fd]/, weight: 0.6, detail: "untracked files were deleted by git clean" },
856
1062
  { pattern: /\bgit\s+branch\s+-D\b/, weight: 0.5, detail: "a branch was deleted unmerged" },
857
1063
  { pattern: /\bgit\s+reset\s+--hard\b/, weight: 0.6, detail: "the working tree was reset, discarding uncommitted work" },
@@ -981,10 +1187,10 @@ function pathsSeen(use) {
981
1187
  return paths;
982
1188
  }
983
1189
  /** The last call that looked at this file, or nothing if none did. */
984
- function lastLook(seen, filePath) {
1190
+ function lastLook(seen, filePath, budget) {
985
1191
  let latest;
986
1192
  for (const [known, index] of seen) {
987
- if (!samePath(known, filePath))
1193
+ if (!samePath(known, filePath, budget))
988
1194
  continue;
989
1195
  if (latest === undefined || index > latest)
990
1196
  latest = index;
@@ -1011,6 +1217,7 @@ function lastLook(seen, filePath) {
1011
1217
  export function detectStaleContext(session) {
1012
1218
  const breaks = [];
1013
1219
  const seen = new Map();
1220
+ const globSteps = { left: GLOB_STEPS };
1014
1221
  let previous;
1015
1222
  let pauseIndex;
1016
1223
  let pauseMs = 0;
@@ -1029,7 +1236,7 @@ export function detectStaleContext(session) {
1029
1236
  previous = use;
1030
1237
  if (!reported && pauseIndex !== undefined && EDIT_TOOLS.has(use.name)) {
1031
1238
  const filePath = typeof use.input.file_path === "string" ? use.input.file_path : undefined;
1032
- const looked = filePath === undefined ? undefined : lastLook(seen, filePath);
1239
+ const looked = filePath === undefined ? undefined : lastLook(seen, filePath, globSteps);
1033
1240
  // A file nobody ever looked at is `unverified_assumption`, and saying it
1034
1241
  // twice under two headings is one edit and two notes to reconcile.
1035
1242
  if (filePath && looked !== undefined && looked < pauseIndex) {