@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/src/parser.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { posix } from "node:path";
2
2
  import { fileLines } from "./lines.js";
3
+ import { REDACTION_MARKER } from "./mask-paths.js";
3
4
  import type {
4
5
  ContentBlock,
5
6
  FileEdit,
@@ -85,6 +86,8 @@ interface ParseState {
85
86
  delegated: Set<string>;
86
87
  /** Calls whose result counted the subagent's edits, whatever the count. */
87
88
  counted: Set<string>;
89
+ shell: Budget;
90
+ structures: Budget;
88
91
  }
89
92
 
90
93
  function isObject(value: unknown): value is Record<string, unknown> {
@@ -117,6 +120,8 @@ function createState(): ParseState {
117
120
  delegatedEdits: false,
118
121
  delegated: new Set(),
119
122
  counted: new Set(),
123
+ shell: { left: SHELL_CHARS },
124
+ structures: { left: STRUCTURES },
120
125
  };
121
126
  }
122
127
 
@@ -195,7 +200,8 @@ function pushFileEdits(state: ParseState, use: ToolUse): void {
195
200
  }
196
201
 
197
202
  const existing = state.fileEdits.get(filePath);
198
- if (existing) existing.push(...entries);
203
+ // One at a time, as spreading a long `edits` overflows the call's arguments.
204
+ if (existing) for (const entry of entries) existing.push(entry);
199
205
  else state.fileEdits.set(filePath, entries);
200
206
  }
201
207
 
@@ -205,10 +211,11 @@ function pushLookup(state: ParseState, use: ToolUse): void {
205
211
  }
206
212
 
207
213
  /**
208
- * A heredoc opener: `<<EOF`, `<<-EOF`, `<<'PY'`. The delimiter is what ends the
209
- * body, and quoting it changes nothing about where the body stops.
214
+ * A heredoc opener: `<<EOF`, `<<-EOF`, `<<'PY'`, `<<\EOF`. The delimiter is what
215
+ * ends the body, and quoting it changes nothing about where the body stops. `<<<`
216
+ * is a here-string, whose word is the whole of its input, and it has no body.
210
217
  */
211
- export const HEREDOC = /<<-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/;
218
+ export const HEREDOC = /(?<!<)<<-?\s*\\?(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/;
212
219
 
213
220
  /**
214
221
  * The command with every heredoc body cut out.
@@ -245,9 +252,8 @@ export function withoutHeredocs(command: string): string {
245
252
  * inside a search pattern is the commonest way a `|` shows up in a command that
246
253
  * runs nothing at all.
247
254
  *
248
- * ponytail: quotes nest as a single state, and a backslash escape only counts
249
- * inside double quotes, which is what a shell does anyway. Real parsing is a
250
- * dependency, and this is here to stop three regexes from lying.
255
+ * ponytail: quotes nest as a single state. Real parsing is a dependency, and this
256
+ * is here to stop three regexes from lying.
251
257
  */
252
258
  export function commandSegments(command: string): string[] {
253
259
  const segments: string[] = [];
@@ -266,9 +272,16 @@ export function commandSegments(command: string): string[] {
266
272
  for (let i = 0; i < flat.length; i += 1) {
267
273
  const char = flat[i]!;
268
274
 
275
+ // What a backslash escapes is no quote and no operator, except in single quotes;
276
+ // at the end of a line it only joins that line to the next.
277
+ if (char === "\\" && quote !== "'" && flat[i + 1] !== "\n") {
278
+ current += char + (flat[i + 1] ?? "");
279
+ i += 1;
280
+ continue;
281
+ }
269
282
  if (quote !== undefined) {
270
283
  current += char;
271
- if (char === quote && (quote === "'" || flat[i - 1] !== "\\")) quote = undefined;
284
+ if (char === quote) quote = undefined;
272
285
  continue;
273
286
  }
274
287
  if (char === '"' || char === "'" || char === "`") {
@@ -406,15 +419,72 @@ export function mayWrite(name: string, input: Record<string, unknown>): boolean
406
419
  return keys.length === 0 || told;
407
420
  }
408
421
 
422
+ /** What an MCP tool's name opens with when all it does is read. */
423
+ const MCP_READ_VERBS = new Set([
424
+ "read", "get", "list", "search", "find", "fetch", "query", "show", "view", "describe", "resolve", "validate", "wait",
425
+ ]);
426
+
427
+ /**
428
+ * Whether an MCP tool's name says it leaves everything as it found it.
429
+ *
430
+ * ponytail: judged by names, as `mayWrite` is. A tool that opens with none of these
431
+ * reads as one nobody can answer for, a browser's `navigate` included, and its
432
+ * session keeps a commit on another branch. Add the word if one such proves to move
433
+ * sessions to the wrong pull request.
434
+ */
435
+ export function mcpOnlyReads(name: string): boolean {
436
+ return MCP_READ_VERBS.has(nameWords(name.slice(name.lastIndexOf("__") + 2))[0]!);
437
+ }
438
+
409
439
  /** A redirect into a file: `> a.ts`, `>> log`, `2> out`. Not `2>&1`, not `/dev/null`. */
410
440
  const REDIRECT_WRITE = /(?:^|[^<>&\d])\d*>>?(?!&)\s*(?!\/dev\/|\/tmp\/)[^\s&|;<>]/;
411
441
  const TEE_WRITE = /\btee\s+(?:-a\s+)?(?!\/dev\/|\/tmp\/)[^\s&|;-]/;
412
442
  const FILE_MUTATORS =
413
443
  /^\s*(?:sudo\s+)?(?:patch|truncate|mv|cp|git\s+(?:apply|mv|rm|restore|checkout\s+--))\b/;
414
- const IN_PLACE_EDITORS = /^\s*(?:sudo\s+)?(?:g?sed|perl)\b/;
415
- /** An interpreter fed a script on stdin: whatever the script writes, the command line cannot say. */
416
- const INTERPRETER_HEREDOC =
417
- /(?:^|[\s;&|(])(?:python[0-9.]*|node|deno|bun|tsx|ruby|perl|php|bash|sh|zsh)\b[^\n]*<</;
444
+ /** What can stand in front of a command and leave it the same command: `do sed -i …`. */
445
+ const LEADING_WORDS = /^\s*(?:(?:sudo|if|then|elif|else|while|until|do)\s+)*/;
446
+ const IN_PLACE_EDITORS = /^(g?sed|g?awk|perl|ruby)(?=\s|$)/;
447
+ /**
448
+ * Theirs is among the flags in front, in a cluster that may hold a digit: `perl -0pi`.
449
+ * Only behind flags that take nothing glued to them, as `-Ilib`, `-rdigest` and
450
+ * `-Mstrict` hold an `i` that is no flag.
451
+ */
452
+ const PERL_IN_PLACE = /^\S+(?:\s+-\S+)*?\s+-[0-9acdlnpsStTuUvwWXy]*i/;
453
+ /** To awk `-i` loads a library, and the one that rewrites is gawk's `inplace`, unless another flag takes the word. */
454
+ const AWK_IN_PLACE = /(?:^|\s)(?=-)(?<!\s-[efFv]\s+)(?:-i\s*|--include[=\s]\s*)inplace(?:\s|$)/;
455
+
456
+ /**
457
+ * What quotes hold, emptied out: an operator in there is an argument, as in
458
+ * `grep "->" a`. An escaped quote opens nothing, and stepping over it first keeps a
459
+ * run of them from each scanning the rest of the text for a close that never comes.
460
+ */
461
+ function withoutQuoted(segment: string): string {
462
+ return segment.replace(/\\.|'[^']*'|"(?:[^"\\]|\\.)*"/g, (match) => (match.startsWith("\\") ? match : "''"));
463
+ }
464
+
465
+ /**
466
+ * Whether a segment, with what its quotes hold emptied out, runs an editor that
467
+ * rewrites its files: `sed 's/ -i //' a.ts` only prints.
468
+ */
469
+ function editsInPlace(bare: string): boolean {
470
+ const called = bare.replace(LEADING_WORDS, "");
471
+ const editor = IN_PLACE_EDITORS.exec(called)?.[1];
472
+ if (editor === undefined) return false;
473
+ if (editor === "perl" || editor === "ruby") return PERL_IN_PLACE.test(called);
474
+ return (editor.endsWith("awk") ? AWK_IN_PLACE : IN_PLACE).test(called);
475
+ }
476
+
477
+ const INTERPRETER = /(?:^|[\s;&|(])(?:python[0-9.]*|node|deno|bun|tsx|ruby|perl|php|bash|sh|zsh)\b/;
478
+
479
+ /**
480
+ * An interpreter fed a script on stdin: whatever the script writes, the command line
481
+ * cannot say. Looked for ahead of a line's last `<<`, because scanning on from every
482
+ * interpreter word to find one took 20 s over 200 KB of them.
483
+ */
484
+ function feedsInterpreter(ran: string): boolean {
485
+ if (!ran.includes("<<")) return false;
486
+ return ran.split("\n").some((line) => INTERPRETER.test(line.slice(0, Math.max(line.lastIndexOf("<<"), 0))));
487
+ }
418
488
 
419
489
  /**
420
490
  * Whether a shell command could have changed a file in the repository.
@@ -433,17 +503,97 @@ const INTERPRETER_HEREDOC =
433
503
  */
434
504
  export function shellWrites(command: string): boolean {
435
505
  const ran = withoutHeredocs(command);
436
- if (INTERPRETER_HEREDOC.test(ran)) return true;
506
+ if (feedsInterpreter(ran)) return true;
437
507
 
438
508
  for (const segment of commandSegments(ran)) {
439
- // What sits inside quotes is an argument, not an operator: `grep "->" a`.
440
- const bare = segment.replace(/'[^']*'|"(?:[^"\\]|\\.)*"/g, "''");
509
+ const bare = withoutQuoted(segment);
441
510
  if (REDIRECT_WRITE.test(bare) || TEE_WRITE.test(bare) || FILE_MUTATORS.test(bare)) return true;
442
- if (IN_PLACE_EDITORS.test(bare) && (IN_PLACE.test(bare) || /\bperl\s+-\w*i/.test(bare))) return true;
511
+ if (editsInPlace(bare)) return true;
443
512
  }
444
513
  return false;
445
514
  }
446
515
 
516
+ /**
517
+ * A `git` call as a shell runs one: what can stand in front of it, the global
518
+ * flags, then the subcommand. `if FOO=1 git -C app -c a=b commit -m x`. Read with
519
+ * what quotes hold emptied out, so a value with a space in it is one word.
520
+ */
521
+ const GIT_CALL =
522
+ /^\s*(?:[({!]\s*|(?:sudo|if|then|elif|else|while|until|do|time|command|env)\s+|\w+=\S*\s+)*git\s+(?:-[Cc]\s+\S+\s+|--?[^\s=]+(?:=\S*)?\s+){0,8}([a-z][\w-]*)/;
523
+
524
+ /**
525
+ * The git subcommands a shell command runs, in the order it runs them. By command
526
+ * and not by word: `pytest tests/git/`, `npm --prefix tools/commit run dev` and a
527
+ * message saying "git commit" run none.
528
+ */
529
+ export function gitCommands(command: string): string[] {
530
+ if (!command.includes("git")) return [];
531
+ const found: string[] = [];
532
+ for (const segment of commandSegments(withoutHeredocs(command))) {
533
+ const subcommand = GIT_CALL.exec(withoutQuoted(segment))?.[1];
534
+ if (subcommand !== undefined) found.push(subcommand);
535
+ }
536
+ return found;
537
+ }
538
+
539
+ /** Besides `SHELL_READERS`, what leaves the tree as it found it: a comment and a loop's own words too. */
540
+ const SHELL_LOOKS =
541
+ /^(?:#|(?:cd|ls|pwd|echo|printf|date|sleep|true|false|which|sort|uniq|cut|tr|ps|mkdir|export|test|\[|gh|for|done|fi)(?:\s|$))/;
542
+
543
+ /** Git commands that put nothing into the tree for a commit to hold. */
544
+ const GIT_LOOKS = new Set([
545
+ "status", "log", "diff", "show", "blame", "reflog", "rev-parse", "rev-list", "ls-files", "describe",
546
+ "config", "remote", "fetch", "push", "branch", "tag", "worktree", "switch", "checkout", "add", "commit",
547
+ ]);
548
+
549
+ /** What only the shell can turn into a file's name, so the command line never spells it. */
550
+ const SHELL_EXPANDS = /[*?$`{]/;
551
+
552
+ /** The one `git checkout` that stands on a branch and takes no file from anywhere. */
553
+ const CHECKOUT_BRANCH = /\bcheckout(?:\s+(?:-[bB]\s+\S+|-[qft]|--(?:quiet|force|track|detach)))*(?:\s+\S+)?\s*$/;
554
+
555
+ /** Readers that write when a flag or a second name says so, and the `gh` that moves the tree or fills it. */
556
+ const WRITES_BY_ARGUMENT =
557
+ /^(?:gh\s+(?:pr\s+checkout|co|release\s+download|run\s+download|repo\s+clone|gist\s+clone)\s|sort\s(?:.*\s)?(?:-[a-zA-Z]*o|--output)|yq\s(?:.*\s)?(?:-[a-zA-Z]*i|--inplace)|(?:uniq|xxd)(?:\s+-\S+)*\s+[^\s-]\S*\s+[^\s-])/;
558
+
559
+ /** `sed`'s write, as a command or as the flag of an `s`: `w out`, `1,5w out`, `s/a/b/gw out`. */
560
+ const SED_WRITE = /(?:^|[^A-Za-z])[gpiImMe]*[wW]\s+\S/;
561
+
562
+ /**
563
+ * Whether an `awk` or `sed` writes from inside its program, which `withoutQuoted`
564
+ * empties: `awk '{print > "x"}'`, `sed -n 'w x'`. Behind a `print`, as `$1 > 5` is a
565
+ * comparison; a `>` in a printed string reads as a write, and the session keeps the commit.
566
+ */
567
+ function programWrites(called: string, segment: string): boolean {
568
+ const [reader] = /^(?:awk|sed)\b/.exec(called) ?? [];
569
+ if (reader === undefined) return false;
570
+ const program = (segment.match(/\\.|'[^']*'|"(?:[^"\\]|\\.)*"/g) ?? []).join("");
571
+ if (reader === "sed") return SED_WRITE.test(program);
572
+ const print = program.search(/\bprintf?\b/);
573
+ return /\bsystem\s*\(/.test(program) || (print !== -1 && /[>|]/.test(program.slice(print)));
574
+ }
575
+
576
+ /**
577
+ * Whether a shell command leaves nothing behind for a commit to hold, apart from
578
+ * what it writes by name (`shellWrites`). Anything not known here reads as no: a
579
+ * formatter, a build, a script write what nobody can say from the command line, and
580
+ * neither can anyone say which files a write through a glob or a variable took.
581
+ */
582
+ export function shellOnlyLooks(command: string): boolean {
583
+ // `commandSegments` lifts a substitution out of its segment, and it may be the target.
584
+ const substitutionsKept = withoutHeredocs(command).replace(/\$\([^()]*\)/g, "$$_$&");
585
+ return commandSegments(substitutionsKept).every((segment) => {
586
+ if (shellWrites(segment) && SHELL_EXPANDS.test(segment.replace(/'[^']*'/g, ""))) return false;
587
+ const bare = withoutQuoted(segment);
588
+ const subcommand = GIT_CALL.exec(bare)?.[1];
589
+ if (subcommand === "checkout") return CHECKOUT_BRANCH.test(bare);
590
+ if (subcommand !== undefined) return GIT_LOOKS.has(subcommand) && !/\s--output\b/.test(bare);
591
+ const called = bare.replace(LEADING_WORDS, "");
592
+ if (WRITES_BY_ARGUMENT.test(called) || programWrites(called, segment)) return false;
593
+ return called.trim() === "" || SHELL_READERS.test(called) || SHELL_LOOKS.test(called);
594
+ });
595
+ }
596
+
447
597
  /**
448
598
  * `sed -i` rewrites the file; whatever else it is, it is not a look at it. GNU
449
599
  * takes any backup suffix (`-i.bak`, `-ie`), the flag rides in a cluster
@@ -454,7 +604,53 @@ const IN_PLACE = /(?:^|\s)(?:-[a-zA-Z]*[iI]\S*|--in-place\S*)(?:\s|$)/;
454
604
  /** Tools whose first non-flag argument is a program, not a path. */
455
605
  const SCRIPTED = /^\s*(?:sudo\s+)?(?:sed|awk)\b/;
456
606
 
457
- export function shellReadPaths(command: string): string[] {
607
+ /** `main:src/a.ts` names `src/a.ts`; the colon in a path's own marker names no revision. */
608
+ function afterRevision(token: string): string {
609
+ let colon = token.lastIndexOf(":");
610
+ while (colon > 0 && token.endsWith(REDACTION_MARKER, colon)) colon = token.lastIndexOf(":", colon - 1);
611
+ return token.slice(colon + 1);
612
+ }
613
+
614
+ /** The longest path a filesystem takes: a directory past it is none, however a record or a chain of `cd` spells it. */
615
+ const PATH_MAX = 4096;
616
+
617
+ /**
618
+ * What reading one session's shell may spend, in characters: its commands, and the
619
+ * paths they spell out. Every word of a command is joined to its directory, so two
620
+ * bytes of command cost a directory's length, and records repeat that as often as a
621
+ * transcript has room: no cap per command bounds it. Past this a command already seen
622
+ * to write still counts, and the rest count as writes nobody can name. The largest
623
+ * real session, with its subagents, spends 13 MB.
624
+ */
625
+ const SHELL_CHARS = 32 * 1024 * 1024;
626
+ /** What keeping a path costs besides its characters, so that short ones spend `SHELL_CHARS` too. */
627
+ const PATH_COST = 64;
628
+ /** The longest command read, against 39 KB in real sessions, and short of the 8 MB word that overflows the stack of the patterns reading it. */
629
+ const COMMAND_CHARS = 1024 * 1024;
630
+
631
+ /** What is left of a limit that holds over a whole session. */
632
+ export interface Budget {
633
+ left: number;
634
+ }
635
+
636
+ /** Whether this much still fits the budget, which pays for it if so. */
637
+ export function spend(budget: Budget, amount: number): boolean {
638
+ if (budget.left < amount) return false;
639
+ budget.left -= amount;
640
+ return true;
641
+ }
642
+
643
+ /**
644
+ * `to` as seen from a directory. Nothing past `PATH_MAX`, where normalising is what
645
+ * costs, as it copies the path so far at each `..`, or past the budget.
646
+ */
647
+ function pathFrom(dir: string | undefined, to: string, budget: Budget): string | undefined {
648
+ if (to.startsWith("/")) return to.length <= PATH_MAX && spend(budget, to.length) ? posix.normalize(to) : undefined;
649
+ if (dir === undefined || dir.length + to.length >= PATH_MAX || !spend(budget, dir.length + to.length)) return undefined;
650
+ return posix.join(dir, to);
651
+ }
652
+
653
+ export function shellReadPaths(command: string, budget: Budget = { left: SHELL_CHARS }): string[] {
458
654
  const paths: string[] = [];
459
655
 
460
656
  for (const segment of commandSegments(withoutHeredocs(command))) {
@@ -478,11 +674,12 @@ export function shellReadPaths(command: string): string[] {
478
674
 
479
675
  // A glob stays one: `cat src/*.js` read every file it expanded to, and
480
676
  // `detectors.ts:samePath` matches it against the file edited.
481
- const path = token.includes(":") ? token.slice(token.lastIndexOf(":") + 1) : token;
677
+ const path = afterRevision(token);
482
678
  // "/" and "." name no file, and "/" matches every path there is.
483
679
  if (/^[./]+$/.test(path)) continue;
484
680
  if (!/\.[A-Za-z0-9]+$/.test(path) && !path.includes("/")) continue;
485
681
 
682
+ if (!spend(budget, path.length + PATH_COST)) return paths;
486
683
  paths.push(path.replace(/^\.\//, ""));
487
684
  }
488
685
  }
@@ -490,8 +687,8 @@ export function shellReadPaths(command: string): string[] {
490
687
  return paths;
491
688
  }
492
689
 
493
- /** A shell word, quotes and all: `'s/a b/c/'` is one word, not two. */
494
- const SHELL_WORD = /(?:'[^']*'|"(?:[^"\\]|\\.)*"|[^\s'"])+/g;
690
+ /** A shell word, quotes and all: `'s/a b/c/'` is one word, not two. What is escaped goes first, as in `withoutQuoted`. */
691
+ const SHELL_WORD = /(?:\\.|'[^']*'|"(?:[^"\\]|\\.)*"|[^\s'"])+/g;
495
692
 
496
693
  /**
497
694
  * Flags whose next word is the script, not a file. Only the bare ones for sed:
@@ -499,9 +696,12 @@ const SHELL_WORD = /(?:'[^']*'|"(?:[^"\\]|\\.)*"|[^\s'"])+/g;
499
696
  */
500
697
  const SED_SCRIPT_FLAG = /^(?:-e|--expression|-f|--file)$/;
501
698
  const PERL_SCRIPT_FLAG = /^-[a-zA-Z]*e$/;
699
+ /** awk's flags that take a word that is neither the script nor a file. */
700
+ const AWK_VALUE_FLAG = /^(?:-[ivF]|--(?:include|assign|field-separator))$/;
502
701
 
503
702
  /**
504
- * The files `sed -i` and `perl -i` rewrite: every argument after the script.
703
+ * The files `sed -i`, `perl -i`, `ruby -i` and `awk -i inplace` rewrite: every
704
+ * argument after the script.
505
705
  * What the edit changed stays unseen — a sed script is a program, not the text
506
706
  * it replaces — but where it landed is on the command line, and without it a
507
707
  * session editing through sed read to the detectors as one that edited nothing.
@@ -516,49 +716,60 @@ const PERL_SCRIPT_FLAG = /^-[a-zA-Z]*e$/;
516
716
  * still names no file to the detectors (`shellWrites` knows only that one was
517
717
  * written); a glob names files this cannot list.
518
718
  */
519
- export function shellEditPaths(command: string, cwd?: string): string[] {
719
+ export function shellEditPaths(command: string, cwd?: string, budget?: Budget): string[] {
720
+ return shellEditWords(command, cwd, budget).filter((word) => !/[*?]/.test(word));
721
+ }
722
+
723
+ /** As `shellEditPaths`, with a glob left in: to say what a command edits, `.env*` is as good as a file. */
724
+ export function shellEditWords(command: string, cwd?: string, budget: Budget = { left: SHELL_CHARS }): string[] {
520
725
  const paths: string[] = [];
521
- let dir = cwd;
726
+ let dir = cwd !== undefined && cwd.length <= PATH_MAX ? cwd : undefined;
522
727
 
523
728
  for (const segment of commandSegments(withoutHeredocs(command))) {
524
729
  if (/^\s*cd(?:\s|$)/.test(segment)) {
525
730
  const to = /^\s*cd\s+(\S+)\s*$/.exec(segment)?.[1]?.replace(/["']/g, "");
526
- if (to?.startsWith("/")) dir = posix.normalize(to);
527
- else dir = dir === undefined || to === undefined || /^[~$-]/.test(to) ? undefined : posix.join(dir, to);
731
+ dir = to === undefined || /^[~$-]/.test(to) ? undefined : pathFrom(dir, to, budget);
528
732
  continue;
529
733
  }
530
- if (!IN_PLACE_EDITORS.test(segment)) continue;
531
- const perl = /^\s*(?:sudo\s+)?perl\b/.test(segment);
734
+ const called = segment.replace(LEADING_WORDS, "");
532
735
  // The same test `shellWrites` applies, on the same unquoted text, so the two
533
- // agree on what was written: `sed 's/ -i //' a.ts` only prints.
534
- const bare = segment.replace(/'[^']*'|"(?:[^"\\]|\\.)*"/g, "''");
535
- if (!(perl ? /\bperl\s+-\w*i/.test(bare) : IN_PLACE.test(bare))) continue;
736
+ // agree on what was written.
737
+ if (!IN_PLACE_EDITORS.test(called) || !editsInPlace(withoutQuoted(called))) continue;
738
+ const scriptFlag = /^(?:perl|ruby)\b/.test(called) ? PERL_SCRIPT_FLAG : SED_SCRIPT_FLAG;
739
+ const awk = /^g?awk\b/.test(called);
536
740
 
537
741
  let scriptSeen = false;
538
- let scriptNext = false;
539
- const words = segment.replace(/^\s*sudo\s+/, "").match(SHELL_WORD) ?? [];
742
+ let skipNext = false;
743
+ const words = called.match(SHELL_WORD) ?? [];
540
744
  for (const word of words.slice(1)) {
541
745
  if (/^\d*[<>|]|^&/.test(word)) break;
542
746
  const token = word.replace(/["']/g, "");
543
- if (scriptNext) {
544
- scriptNext = false;
747
+ if (skipNext) {
748
+ skipNext = false;
545
749
  continue;
546
750
  }
547
- if ((perl ? PERL_SCRIPT_FLAG : SED_SCRIPT_FLAG).test(token)) {
751
+ if (scriptFlag.test(token)) {
548
752
  scriptSeen = true;
549
- scriptNext = true;
753
+ skipNext = true;
754
+ continue;
755
+ }
756
+ if (awk && AWK_VALUE_FLAG.test(token)) {
757
+ skipNext = true;
550
758
  continue;
551
759
  }
552
760
  if (/^--(?:expression|file)=/.test(token)) scriptSeen = true;
553
- // An empty word is BSD sed's backup suffix: `sed -i '' 's/a/b/' f`.
554
- if (token.length === 0 || token.startsWith("-") || token.startsWith("$")) continue;
761
+ // An empty word is BSD sed's backup suffix: `sed -i '' 's/a/b/' f`. A bare
762
+ // `$VAR` can be flags as well as the script; quoted, it is one word, and in
763
+ // single quotes `$` is sed's own, the last line: `sed -i '$a K=v' f`.
764
+ if (token.length === 0 || token.startsWith("-") || word.startsWith("$")) continue;
555
765
  if (!scriptSeen) {
556
766
  scriptSeen = true;
557
767
  continue;
558
768
  }
559
- if (/[*?]/.test(token) || /^[./]+$/.test(token)) continue;
560
- if (dir === undefined || token.startsWith("~")) paths.push(token.replace(/^\.\//, ""));
561
- else paths.push(token.startsWith("/") ? posix.normalize(token) : posix.join(dir, token));
769
+ if (token.startsWith("$") || /^[./]+$/.test(token)) continue;
770
+ const path = dir === undefined || token.startsWith("~") ? undefined : pathFrom(dir, token, budget);
771
+ if (!spend(budget, path === undefined ? token.length + PATH_COST : PATH_COST)) return paths;
772
+ paths.push(path ?? token.replace(/^\.\//, ""));
562
773
  }
563
774
  }
564
775
 
@@ -566,7 +777,7 @@ export function shellEditPaths(command: string, cwd?: string): string[] {
566
777
  }
567
778
 
568
779
  function pushShellEdits(state: ParseState, use: ToolUse, command: string, cwd: unknown): void {
569
- for (const filePath of shellEditPaths(command, asString(cwd))) {
780
+ for (const filePath of shellEditPaths(command, asString(cwd), state.shell)) {
570
781
  const edit: FileEdit = {
571
782
  toolUseId: use.id,
572
783
  tool: use.name,
@@ -610,9 +821,17 @@ function pushToolUse(
610
821
  }
611
822
  if (LOOKUP_TOOLS.has(name)) pushLookup(state, use);
612
823
  if (name === "Bash" && typeof use.input.command === "string") {
613
- for (const path of shellReadPaths(use.input.command)) state.filesRead.add(path);
614
- if (shellWrites(use.input.command)) state.shellWrote = true;
615
- pushShellEdits(state, use, use.input.command, record.cwd);
824
+ const { command } = use.input;
825
+ if (command.length <= COMMAND_CHARS && spend(state.shell, command.length)) {
826
+ for (const path of shellReadPaths(command, state.shell)) state.filesRead.add(path);
827
+ if (shellWrites(command)) state.shellWrote = true;
828
+ pushShellEdits(state, use, command, record.cwd);
829
+ } else {
830
+ // Not read, which does not say nothing was written. The call keeps no command,
831
+ // so that nothing after this reads it either.
832
+ state.opaqueWrite = true;
833
+ use.input = {};
834
+ }
616
835
  }
617
836
 
618
837
  if (mayWrite(name, use.input)) state.opaqueWrite = true;
@@ -699,6 +918,23 @@ function delegatedUnseen(state: ParseState): boolean {
699
918
  );
700
919
  }
701
920
 
921
+ /**
922
+ * Whether the agent ran a commit: what it put on a branch is its work however the
923
+ * files got there, and a formatter, a generator or a script writes nothing
924
+ * `shellWrites` can read. A call that failed committed nothing.
925
+ */
926
+ function committedFromShell(state: ParseState): boolean {
927
+ return state.toolUses.some((use) => {
928
+ const { command } = use.input;
929
+ return (
930
+ use.name === "Bash" &&
931
+ typeof command === "string" &&
932
+ use.result?.isError !== true &&
933
+ gitCommands(command).includes("commit")
934
+ );
935
+ });
936
+ }
937
+
702
938
  /**
703
939
  * Records of what Claude Code sent to the API, not of what happened: a blob
704
940
  * repeats messages already in the session — tool calls and results included —
@@ -706,6 +942,51 @@ function delegatedUnseen(state: ParseState): boolean {
706
942
  */
707
943
  const REQUEST_RECORD_TYPES = new Set(["api-request", "api-request-shape", "api-request-blob"]);
708
944
 
945
+ /**
946
+ * The longest line parsed, against 4.5 MB in 1215 real sessions. Past some 200 MB of
947
+ * `1,1,1,` in one array, parsing it ends the process, which nothing can catch.
948
+ */
949
+ const LINE_CHARS = 64 * 1024 * 1024;
950
+
951
+ /**
952
+ * What one session's objects, arrays, keys and items may come to, counting an object
953
+ * or array as three keys and an item as a third of one. Parsed, the three bytes of
954
+ * `{},` are 56 and with what is kept of the block 200, a key takes 72 in an object of
955
+ * hundreds or of a shape of its own, ten times its line, and the four bytes of `0.5,`
956
+ * in an array are 24. So a small upload that inflates to nothing else outgrows any
957
+ * heap, and it has to be counted before it is parsed.
958
+ *
959
+ * ponytail: one flat count, 14.6 million in the most of 97 real sessions (252 MB), and
960
+ * a colon after a quote or a comma inside a string counts as a key or an item. What
961
+ * comes past it, or past `LINE_CHARS`, reads as a write nobody can name, and is not
962
+ * read. Raise it if sessions of 500 MB as dense as that one turn up, and the heap with
963
+ * it.
964
+ */
965
+ export const STRUCTURES = 16_000_000;
966
+ const KEYS_PER_STRUCTURE = 3;
967
+ const ITEMS_PER_KEY = 3;
968
+
969
+ function structuresIn(line: string): number {
970
+ let count = 0;
971
+ for (const opening of ["{", "["]) {
972
+ for (let at = line.indexOf(opening); at !== -1; at = line.indexOf(opening, at + 1)) count += KEYS_PER_STRUCTURE;
973
+ }
974
+ for (let at = line.indexOf(":"); at !== -1; at = line.indexOf(":", at + 1)) {
975
+ // Backwards over what JSON lets stand before a colon, which ends at the colon before.
976
+ let before = at - 1;
977
+ while (line[before] === " " || line[before] === "\t" || line[before] === "\r") before -= 1;
978
+ if (line[before] === '"') count += 1;
979
+ }
980
+ let items = 0;
981
+ for (let at = line.indexOf(","); at !== -1; at = line.indexOf(",", at + 1)) items += 1;
982
+ return count + Math.floor(items / ITEMS_PER_KEY);
983
+ }
984
+
985
+ /** Whether a line of a transcript may be parsed at all, which spends its part of `STRUCTURES` if so. */
986
+ export function mayParse(line: string, structures: Budget): boolean {
987
+ return line.length <= LINE_CHARS && spend(structures, structuresIn(line));
988
+ }
989
+
709
990
  function pushLine(state: ParseState, line: string): void {
710
991
  state.skipped.lines += 1;
711
992
 
@@ -715,6 +996,12 @@ function pushLine(state: ParseState, line: string): void {
715
996
  return;
716
997
  }
717
998
 
999
+ if (!mayParse(trimmed, state.structures)) {
1000
+ state.skipped.malformedJson += 1;
1001
+ state.opaqueWrite = true;
1002
+ return;
1003
+ }
1004
+
718
1005
  let parsed: unknown;
719
1006
  try {
720
1007
  parsed = JSON.parse(trimmed);
@@ -839,13 +1126,15 @@ function finish(state: ParseState): ParsedSession {
839
1126
  // Not the same question as editToolUses > 0: a file written from the
840
1127
  // shell counts, a call the parser cannot see into is "cannot tell", which
841
1128
  // must not read as "edited nothing", and so is a subagent that says it
842
- // edited, or whose work never came back to say.
1129
+ // edited, or whose work never came back to say. So does a commit the agent
1130
+ // ran, whatever changed the files in it.
843
1131
  hasFileEdits:
844
1132
  state.editToolUses > 0 ||
845
1133
  state.shellWrote ||
846
1134
  state.opaqueWrite ||
847
1135
  state.delegatedEdits ||
848
- delegatedUnseen(state),
1136
+ delegatedUnseen(state) ||
1137
+ committedFromShell(state),
849
1138
  sidechainRecords,
850
1139
  },
851
1140
  skipped: state.skipped,
@@ -875,22 +1164,37 @@ export function onMainPath(session: ParsedSession, uuid: string | undefined): bo
875
1164
  return typeof uuid === "string" && session.mainPath.has(uuid);
876
1165
  }
877
1166
 
878
- export function onAbandonedBranch(session: ParsedSession, uuid: string | undefined): boolean {
1167
+ /** `walked` holds what is known of records already walked, as all on a way back share what is at its end. */
1168
+ export function onAbandonedBranch(
1169
+ session: ParsedSession,
1170
+ uuid: string | undefined,
1171
+ walked = new Map<string, boolean>(),
1172
+ ): boolean {
879
1173
  if (typeof uuid !== "string" || session.mainPath.size === 0) return false;
880
1174
  if (session.mainPath.has(uuid)) return false;
881
1175
 
882
1176
  const visited = new Set<string>();
1177
+ let abandoned = false;
883
1178
  let current = session.byUuid.get(uuid);
884
1179
 
885
1180
  while (current && typeof current.uuid === "string" && !visited.has(current.uuid)) {
1181
+ const known = walked.get(current.uuid);
1182
+ if (known !== undefined) {
1183
+ abandoned = known;
1184
+ break;
1185
+ }
886
1186
  visited.add(current.uuid);
887
1187
  const parent = parentOf(current);
888
- if (parent === undefined) return false;
889
- if (session.mainPath.has(parent)) return true;
1188
+ if (parent === undefined) break;
1189
+ if (session.mainPath.has(parent)) {
1190
+ abandoned = true;
1191
+ break;
1192
+ }
890
1193
  current = session.byUuid.get(parent);
891
1194
  }
892
1195
 
893
- return false;
1196
+ for (const seen of visited) walked.set(seen, abandoned);
1197
+ return abandoned;
894
1198
  }
895
1199
 
896
1200
  export function hasSkips(skipped: SkipStats): boolean {
package/src/paths.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { readFile, readdir, stat } from "node:fs/promises";
2
2
  import { homedir } from "node:os";
3
- import { basename, join } from "node:path";
3
+ import { basename, isAbsolute, join, relative, sep } from "node:path";
4
4
  import { fileLines } from "./lines.js";
5
5
  import { sessionFingerprint } from "./subagents.js";
6
6
  import type { SessionFile } from "./types.js";
@@ -116,14 +116,38 @@ export async function latestSession(cwd: string): Promise<SessionFile | undefine
116
116
  return sessions[0];
117
117
  }
118
118
 
119
+ /**
120
+ * How far back a sweep looks. A sweep has no baseline to start from — every
121
+ * session it has never seen reads as changed — so without a window the first
122
+ * one on a machine would upload years of transcripts from every directory the
123
+ * CLI was ever used in. Nothing older than this can reach an analysis anyway:
124
+ * sessions are matched to a pull request by commit SHA, and a transcript
125
+ * untouched for a fortnight belongs to commits that were pushed long ago.
126
+ *
127
+ * ponytail: a fixed window, not a high-water mark. A machine that was offline
128
+ * longer than this loses the tail; move the boundary to a `lastSweepAt` in the
129
+ * config if that ever happens to anyone.
130
+ */
131
+ export const SWEEP_WINDOW_MS = 14 * 24 * 60 * 60 * 1000;
132
+
133
+ export function isWithin(top: string, directory: string): boolean {
134
+ const path = relative(top, directory);
135
+ return path !== ".." && !path.startsWith(`..${sep}`) && !isAbsolute(path);
136
+ }
137
+
119
138
  /**
120
139
  * Every Claude session on this machine, newest first, without opening a single
121
140
  * transcript: a scan that runs on a timer must cost `readdir` and `stat`, not a
122
141
  * read per session. Which directory each one ran in is `firstCwd`'s job, asked
123
142
  * only about the sessions that turned out to have changed.
143
+ *
144
+ * `under` skips the project folders that cannot hold a session from that
145
+ * directory or below it, which keeps a commit from listing the whole machine. A
146
+ * slug is lossy, so what it lets through may still be from somewhere else.
124
147
  */
125
- export async function allClaudeSessions(): Promise<SessionFile[]> {
148
+ export async function allClaudeSessions(under?: string): Promise<SessionFile[]> {
126
149
  const root = claudeProjectsDir();
150
+ const slug = under === undefined ? undefined : projectSlug(under);
127
151
 
128
152
  let projects: string[];
129
153
  try {
@@ -133,7 +157,10 @@ export async function allClaudeSessions(): Promise<SessionFile[]> {
133
157
  }
134
158
 
135
159
  const sessions: SessionFile[] = [];
136
- for (const project of projects) sessions.push(...(await sessionsInDir(join(root, project))));
160
+ for (const project of projects) {
161
+ if (slug !== undefined && project !== slug && !project.startsWith(`${slug}-`)) continue;
162
+ sessions.push(...(await sessionsInDir(join(root, project))));
163
+ }
137
164
 
138
165
  return newestFirst(sessions);
139
166
  }