pi-odd-worktree 3.1.1 → 3.3.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.
@@ -38,6 +38,8 @@
38
38
  // evidence module is presentation only — it is read here to decide a heading and
39
39
  // nothing else in this package may read it.
40
40
  const { isDestructiveCommand, isDestructiveSegment, isSqlClientSegment } = require('./bash-destructive.js');
41
+ // The family an inline interpreter script proves from its own text (`python3 -c`, `node -e`).
42
+ const { inlineScriptActivity } = require('./bash-inline-script.js');
41
43
 
42
44
  /**
43
45
  * The activity families, in the reference's precedence order. A family is a card
@@ -104,14 +106,26 @@ const CHECKING_COMMANDS = [
104
106
  // Waiting on or reading CI results verifies work; it is not exploration.
105
107
  /^gh\s+pr\s+checks\b/,
106
108
  /^gh\s+run\s+(view|watch|list)\b/,
109
+ // A syntax check parses the file and reports; it runs none of it.
110
+ /^node\s+(--check|-c)\b/,
111
+ /^(bash|sh|dash|zsh)\s+-n\b/,
112
+ // The validators of a tool's own config report and change nothing.
113
+ /^(herdr\s+config\s+check|niri\s+validate)\b/,
114
+ /^(qmllint|maestro\s+test)\b/,
107
115
  ];
108
116
 
109
117
  // These runners verify only when they are not asked to rewrite files: `ruff check
110
118
  // --fix` and `deno lint --fix` repair code, so they are not checks. The flag is the
111
119
  // one the shell passes, so a quoted `--fix` counts too, while `--fix-dry-run` writes
112
- // nothing and stays a check.
113
- const FIXABLE_CHECKING_COMMANDS = [/^ruff\s+check\b/, /^deno\s+lint\b/];
114
- const FIX_FLAGS = new Set(['--fix', '--fix-only', '--unsafe-fixes']);
120
+ // nothing and stays a check. `biome` writes with `--write` as well as `--fix`; the
121
+ // linters beside it only report.
122
+ const FIXABLE_CHECKING_COMMANDS = [
123
+ /^ruff\s+check\b/,
124
+ /^deno\s+lint\b/,
125
+ /^((npx|pnpx|bunx)\s+)?biome\s+(check|ci|lint)\b/,
126
+ /^((npx|pnpx|bunx)\s+)?(shellcheck|actionlint|yamllint|hadolint|tflint|mypy|pyright|flake8|pylint)\b/,
127
+ ];
128
+ const FIX_FLAGS = new Set(['--fix', '--fix-only', '--unsafe-fixes', '--write']);
115
129
 
116
130
  // A formatter verifies only when it is told to report. The report flag is the tool's
117
131
  // own: `-l` lists files for gofmt, gofumpt and shfmt but is the line length for black,
@@ -221,6 +235,11 @@ const PREPARING_COMMANDS = [
221
235
  /^git\s+config\s+[^-\s]+\s+\S/,
222
236
  /^(pip|pip3)\s+config\s+set\b/,
223
237
  /^go\s+env\s+-w\b/,
238
+ // `tee file` writes its input into the file; `tar` extracting or creating builds files up.
239
+ /^tee\s+(?:-\S+\s+)*[^-\s]/,
240
+ // Installing, linking or updating the harness's own packages and plugins builds the machine up.
241
+ /^(pi\s+(install|update)|herdr\s+plugin\s+(install|link|unlink|update)|flatpak\s+install|snap\s+install)\b/,
242
+ /^tar\s+-?[a-zA-Z]*[xc]/,
224
243
  ];
225
244
  // The package managers install, scaffold and store configuration through the same
226
245
  // subcommand position, so one reader serves every ecosystem.
@@ -246,7 +265,7 @@ const VERSIONING_COMMANDS = [
246
265
  /^git\s+reset\b(?!.*\s--hard\b)/,
247
266
  // A branch or a tag the call creates, deletes or renames. The listing forms are
248
267
  // inspection and are matched before this family, so what is left moves a ref.
249
- /^git\s+branch\s+(-[dDmMcuU]\w*|--(delete|move|copy|set-upstream-to|unset-upstream|edit-description|create-reflog)\b)/,
268
+ /^git\s+branch\s+(-[dDmMcCfuU]\w*|--(delete|move|copy|set-upstream-to|unset-upstream|edit-description|create-reflog)\b)/,
250
269
  /^git\s+branch\s+[^-\s]/,
251
270
  /^git\s+tag\s+(-[adfsuv]\w*|--(annotate|delete|sign|force|local-user|message|file|cleanup|verify)\b)/,
252
271
  /^git\s+tag\s+[^-\s]/,
@@ -254,6 +273,8 @@ const VERSIONING_COMMANDS = [
254
273
  // The local stash holds work this machine keeps; dropping or clearing it is destructive
255
274
  // and is read before this family.
256
275
  /^git\s+stash\b/,
276
+ // Plumbing that writes objects, the index or a ref without touching the worktree.
277
+ /^git\s+(hash-object\s+-w|update-index|update-ref|write-tree|commit-tree|read-tree|mktree)\b/,
257
278
  // A lock is a file in the worktree's own git directory, like the index.
258
279
  /^git\s+worktree\s+(lock|unlock)\b/,
259
280
  // Checking a PR out brings its branch here and publishes nothing.
@@ -315,6 +336,8 @@ const EXPLORING_COMMANDS = [
315
336
  /^git\s+(status|log|diff|show|blame|rev-parse|rev-list|merge-base|ls-files|ls-remote|ls-tree|cat-file|describe|shortlog|reflog|grep|check-ignore|for-each-ref|name-rev|count-objects)\b/,
316
337
  new RegExp(String.raw`^git\s+(branch|tag)${GIT_LIST_FLAG}`),
317
338
  /^git\s+remote(\s+(-v|--verbose|show|get-url)\b.*)?$/,
339
+ // `git branch` listing with patterns: a list flag and no flag that creates, moves or deletes.
340
+ /^git\s+branch\b(?=.*\s(--list|-l|-v|-vv|-a|-r|--show-current|--contains|--merged|--no-merged)(\s|$))(?!.*\s(-[dDmMcCfuU]\w*|--delete|--move|--copy|--set-upstream-to|--unset-upstream)(\s|=|$))/,
318
341
  /^git\s+(worktree|stash)\s+list\b/,
319
342
  /^git\s+config\s+(--get|--get-all|--get-regexp|-l|--list)\b/,
320
343
  // `git config <key>` without a value reports what is stored.
@@ -355,7 +378,38 @@ const EXPLORING_COMMANDS = [
355
378
  // The harness tools the transcripts call: only a read-only word names the card, and the
356
379
  // noun before it is transparent, so `herdr plugin list` is the list it is while
357
380
  // `herdr plugin install` still names nothing.
358
- /^(herdr|pi|gentle-ai|gentle-shell|odd-worktree)\s+([\w-]+\s+)?(version|list|ls|status|doctor|show|info|--version|--help|--status|-v|-h)(?=\s|$)/,
381
+ /^(herdr|pi|gentle-ai|gentle-shell|odd-worktree)\s+([\w-]+\s+){0,2}(version|list|ls|status|doctor|show|info|--version|--help|--status|-v|-h)(?=\s|$)/,
382
+ /^herdr\s+pane\s+(read|list|get|process-info)\b/,
383
+ /^herdr\s+api\s+(snapshot|get|list)\b/,
384
+ /^voxtype\s+config\s+show\b/,
385
+ /^gh\s+stack\s+status\b/,
386
+ /^gh\s+(extension|ext|alias|gpg-key|ssh-key|codespace)\s+list\b/,
387
+ /^(flatpak|snap)\s+(list|info|search)\b/,
388
+ /^herdr\s+plugin\s+config-dir\b/,
389
+ // Version probes: the universal `--version`/`-V`, and each runtime's own `-v`/`version`.
390
+ /^[\w./+-]+\s+(--version|-V)\s*$/,
391
+ /^(node|python3?(\.\d+)?|ruby|php|java|npm|pnpm|yarn|bun|deno|rustc|cargo|go|gcc|clang|make|git|docker|uv|pip3?|ffmpeg|jq|nvim|tmux)\s+(-v|version)\s*$/,
392
+ // What the shell hands its children, and the state the kernel and the daemons report.
393
+ /^env\s*$/,
394
+ /^ss\b(?!.*\s(-\w*K\w*|--kill)\b)/,
395
+ /^(lsblk|findmnt|lscpu|lspci|lsusb|lsmod|fc-list|fc-match|ffprobe)\b/,
396
+ /^mount(\s+-l)?\s*$/,
397
+ /^ip\s+(-\w+\s+)*(a|addr|address|l|link|r|route|n|neigh|neighbor|rule)(\s+(show|list|ls)(\s+(dev\s+)?[\w.-]+)?)?\s*$/,
398
+ /^dconf\s+(dump|read|list)\b/,
399
+ /^gsettings\s+(get|list-\w+|range|describe)\b/,
400
+ /^niri\s+msg\s+(--json\s+)?(outputs|windows|workspaces|focused-window|focused-output|layers|version|keyboard-layouts|overview-state)\b/,
401
+ /^pactl\s+(-\S+\s+)*(info|list|get-[\w-]+)\b/,
402
+ /^wpctl\s+(status|inspect|get-volume)\b/,
403
+ /^(hostnamectl|timedatectl)(\s+status)?\s*$/,
404
+ /^loginctl\s+(list-\w+|show-\w+|status|show)\b/,
405
+ /^adb\s+(-s\s+\S+\s+)?(devices|get-state|shell\s+getprop(\s+\S+)?)\s*$/,
406
+ // Reading a repository out as an archive or an object id writes nothing unless told to.
407
+ /^git\s+archive\b(?!.*\s(-o|--output)\b)/,
408
+ /^git\s+hash-object\b(?!.*\s-w\b)/,
409
+ // A packing dry run resolves the file list without writing the tarball.
410
+ /^(npm|pnpm|yarn|bun)\s+pack\b.*\s--dry-run\b/,
411
+ // Listing an archive reads it.
412
+ /^tar\s+-?[a-zA-Z]*t/,
359
413
  // Containers, clusters and package managers print state without changing it.
360
414
  /^docker\s+(ps|images|logs|inspect|version|info|stats|top|port|diff|history|(image|container|volume|network)\s+(ls|inspect|logs)|compose\s+(ps|logs|config|images|top))\b/,
361
415
  /^kubectl\s+(get|describe|logs|version|top|explain|api-resources|config\s+(view|get-contexts|current-context))\b/,
@@ -369,7 +423,10 @@ const EXPLORING_COMMANDS = [
369
423
  // words, no-op builtins, and bare variable assignments (their command substitutions
370
424
  // are classified as commands of their own).
371
425
  const NEUTRAL_COMMANDS = [
372
- /^(cd|pushd|popd|echo|printf|sleep|true|false|export|set|unset|local|read|wait|return|exit|umask)\b/,
426
+ /^(cd|pushd|popd|echo|printf|sleep|true|false|export|set|unset|local|read|wait|return|exit|umask|declare|typeset|readonly|shift)\b/,
427
+ // The lines of a loop or a branch that carry no command of their own.
428
+ /^(do|then|else|break|continue)(\s|$)/,
429
+ /^done(\s+<\s*\S+)?$/,
373
430
  // `:` is the shell's own no-op, and it is not a word, so it needs its own boundary.
374
431
  /^:(\s|$)/,
375
432
  // A line that starts with `#` is a comment the shell never runs, so a script's prose
@@ -410,19 +467,23 @@ function inferBashActivity(command) {
410
467
  const deletesData = isDestructiveCommand(command);
411
468
  const sqlPayload = deletesData ? sqlPayloadCoverage(segments) : [];
412
469
  const runsCodeGraph = activities.includes('codegraph');
413
- // A segment nothing can name keeps the whole card generic, so the heading never hides
414
- // the work the call also did silently — unless the call runs CodeGraph, whose card
415
- // names the command itself whichever company it keeps.
416
- if (!runsCodeGraph && activities.some((activity, index) => activity === undefined && sqlPayload[index] !== true)) return undefined;
417
470
  // A deletion outranks every other identity on the same call: it is the one
418
- // consequence a reader cannot undo, so a check beside it never takes the card.
471
+ // consequence a reader cannot undo, so nothing beside it takes the card — not a check,
472
+ // not a segment nothing can name. The heading claims only what the call provably did,
473
+ // and a call that provably deletes did.
419
474
  if (activities.includes('destructive') || deletesData) return 'destructive';
475
+ // Below a deletion, a segment nothing can name keeps the whole card generic, so the
476
+ // heading never hides the work the call also did silently — unless the call runs
477
+ // CodeGraph, whose card names the command itself whichever company it keeps.
478
+ if (!runsCodeGraph && activities.some((activity, index) => activity === undefined && sqlPayload[index] !== true)) return undefined;
420
479
  // A call that also runs CodeGraph is CodeGraph's card: the command is its detail, so
421
480
  // a check or a build beside it never takes the row.
422
481
  if (runsCodeGraph) return 'codegraph';
423
- if (activities.includes('checking')) return 'checking';
482
+ // What the call changed outranks what it verified: a remote write, then a history
483
+ // change, then a check, then a rewrite and a build-up, with inspection the base.
424
484
  if (activities.includes('publishing')) return 'publishing';
425
485
  if (activities.includes('versioning')) return 'versioning';
486
+ if (activities.includes('checking')) return 'checking';
426
487
  if (activities.includes('formatting')) return 'formatting';
427
488
  if (activities.includes('preparing')) return 'preparing';
428
489
  return 'exploring';
@@ -476,26 +537,164 @@ function segmentActivity(segment) {
476
537
  // other family reads it, so a deletion is never filed as the check or the inspection
477
538
  // its verb resembles.
478
539
  if (isDestructiveSegment(segment)) return 'destructive';
479
- if (isCheckingSegment(segment)) return 'checking';
480
- if (isFormattingSegment(segment)) return 'formatting';
481
- if (isReadOnlyInspection(segment)) return 'exploring';
482
- // A segment whose payload this classifier cannot read, or that redirects its output
483
- // into a file, is a write it cannot name.
484
- if (HIDDEN_SUBSTITUTION.test(segment) || writesFile(segment)) return undefined;
485
- if (isPreparingSegment(segment)) return 'preparing';
486
- if (matchesAny(VERSIONING_COMMANDS, segment)) return 'versioning';
487
- if (matchesAny(PUBLISHING_COMMANDS, segment)) return 'publishing';
488
- if (codeGraphCommandWords(segment) !== undefined) return 'codegraph';
540
+ // Every family below reads the text the shell acts on: a heredoc body is the data the
541
+ // program is handed, so its words are never flags, redirects or substitutions.
542
+ const split = splitHeredocs(segment);
543
+ const text = split.text;
544
+ const hidden = hasHiddenSubstitution(segment);
545
+ if (isCheckingSegment(text)) return 'checking';
546
+ if (isFormattingSegment(text)) return 'formatting';
547
+ if (isReadOnlyInspection(text, hidden)) return 'exploring';
548
+ // A segment whose payload this classifier cannot read is a write it cannot name.
549
+ if (hidden) return undefined;
550
+ // A redirect into a file creates it. What feeds it must be a read or plain text, so
551
+ // the heading claims only that: a mutation beside a redirect stays unnamed.
552
+ const redirected = writesFile(text);
553
+ const base = redirected ? withoutFileRedirects(text) : text;
554
+ if (base === undefined || writesFile(base)) return undefined;
555
+ const curl = curlReading(shellWords(base));
556
+ if (curl !== undefined) {
557
+ if (curl === 'unnamed') return undefined;
558
+ return redirected ? 'preparing' : curl;
559
+ }
560
+ // An inline script is named by what its own text proves; a shell handed a command line
561
+ // (`bash -c`, `ssh host '…'`) is named by that command line.
562
+ const words = shellWords(base);
563
+ const firstBody = split.bodies[0] === undefined ? undefined : split.bodies[0].text.replace(/^\n/, '');
564
+ const inline = inlineScriptActivity(words.filter((word) => !word.startsWith('<')), firstBody);
565
+ if (inline !== undefined) return redirected && inline === 'exploring' ? 'preparing' : inline;
566
+ const payload = shellPayload(words);
567
+ if (payload !== undefined) {
568
+ // A shell inside a shell is read two levels deep and no further, so a pile of wrappers
569
+ // costs a bounded amount of work and stays unnamed.
570
+ if (payloadDepth >= 2) return undefined;
571
+ payloadDepth += 1;
572
+ let inner;
573
+ try {
574
+ inner = inferBashActivity(payload);
575
+ } finally {
576
+ payloadDepth -= 1;
577
+ }
578
+ // A redirect keeps the answer in a file: a read becomes the file it creates, a deletion
579
+ // is still the deletion, and anything else stays unnamed like `git push > out`.
580
+ if (!redirected) return inner;
581
+ return inner === 'exploring' ? 'preparing' : inner === 'destructive' ? 'destructive' : undefined;
582
+ }
583
+ if (redirected) return TEXT_PRODUCER.test(base) || isReadOnlyInspection(base, false) ? 'preparing' : undefined;
584
+ // An in-place edit rewrites the file it names, as `cat >> file` does. The flag is one the
585
+ // shell hands the program, so a `-i` inside quoted text is never one.
586
+ if (isInPlaceEdit(words)) return 'preparing';
587
+ if (isPreparingSegment(text)) return 'preparing';
588
+ if (matchesAny(VERSIONING_COMMANDS, text)) return 'versioning';
589
+ if (matchesAny(PUBLISHING_COMMANDS, text)) return 'publishing';
489
590
  return undefined;
490
591
  }
491
592
 
593
+ // How many `bash -c` / `ssh host` command lines deep the classifier is reading right now.
594
+ let payloadDepth = 0;
595
+
596
+ // `ssh` options that take the word after them, and the ones that stand alone; any other
597
+ // option (a forwarded port, a jump of its own) leaves the command unread.
598
+ const SSH_VALUE_OPTIONS = new Set(['-o', '-p', '-i', '-l', '-F', '-J', '-S', '-b', '-c', '-E']);
599
+ const SSH_PLAIN_OPTIONS = new Set(['-T', '-t', '-tt', '-n', '-q', '-v', '-4', '-6', '-A', '-a', '-x', '-C']);
600
+
601
+ /** The command line a shell or an `ssh` call is handed, or `undefined` when it runs a file or none. */
602
+ function shellPayload(words) {
603
+ const program = words[0];
604
+ if (program === 'bash' || program === 'sh' || program === 'zsh' || program === 'dash') {
605
+ return words[1] === '-c' && words[2] !== undefined ? words[2] : undefined;
606
+ }
607
+ if (program !== 'ssh') return undefined;
608
+ let index = 1;
609
+ for (; index < words.length; index += 1) {
610
+ const word = words[index] === undefined ? '' : words[index];
611
+ if (SSH_VALUE_OPTIONS.has(word)) index += 1;
612
+ else if (!SSH_PLAIN_OPTIONS.has(word)) break;
613
+ }
614
+ const rest = words.slice(index + 1);
615
+ if (words[index] === undefined || words[index].startsWith('-') || rest.length === 0) return undefined;
616
+ return rest.join(' ');
617
+ }
618
+
619
+ /** Whether the words are `sed` or `perl` asked to edit a file in place. */
620
+ function isInPlaceEdit(words) {
621
+ if (words[0] === 'sed') return words.slice(1).some((word) => /^-[a-zA-Z]*i[a-zA-Z.]*$/.test(word) || /^--in-place(=|$)/.test(word));
622
+ if (words[0] === 'perl') return words.slice(1).some((word) => /^-[a-zA-Z]*i[a-zA-Z.]*$/.test(word));
623
+ return false;
624
+ }
625
+
626
+ // What a redirect can honestly be said to create: the text a program prints.
627
+ const TEXT_PRODUCER = /^(cat|echo|printf|tee)\b/;
628
+
629
+ // `curl` reads a URL, and `-o`/`-O`/a redirect keep the answer in a file; anything it
630
+ // sends (a body, a form, an upload, a method other than GET or HEAD) is a write to a
631
+ // server this classifier cannot read, so it stays unnamed.
632
+ const CURL_SENDING_FLAGS = new Set(['--data', '--data-raw', '--data-binary', '--data-urlencode', '--data-ascii', '--form', '--form-string', '--upload-file', '--json', '--config']);
633
+ // Long options that keep the answer in a file named by their value (`-` is stdout), and the
634
+ // ones that only imply a file.
635
+ const CURL_FILE_VALUE_FLAGS = new Set(['--output', '--dump-header', '--cookie-jar', '--trace', '--trace-ascii', '--etag-save', '--libcurl', '--stderr', '--hsts', '--alt-svc']);
636
+ const CURL_FILE_FLAGS = new Set(['--remote-name', '--remote-name-all']);
637
+ // Options that take a value and keep nothing in a file by themselves.
638
+ const CURL_VALUE_ONLY_FLAGS = new Set(['--output-dir']);
639
+ // Short options that take a value, glued on (`-oout.txt`) or as the next word.
640
+ const CURL_SHORT_VALUE_FLAGS = new Set(['A', 'b', 'c', 'C', 'd', 'D', 'e', 'E', 'F', 'H', 'K', 'm', 'o', 'P', 'Q', 'r', 'T', 'u', 'U', 'w', 'x', 'X', 'y', 'Y', 'z']);
641
+
642
+ /** `undefined` when the words are not a curl call, else `exploring`, `preparing` or `unnamed`. */
643
+ function curlReading(words) {
644
+ if (words[0] !== 'curl') return undefined;
645
+ let toFile = false;
646
+ const isReadMethod = (value) => /^(GET|HEAD)$/i.test(value === undefined ? '' : value);
647
+ for (let index = 1; index < words.length; index += 1) {
648
+ const word = words[index] === undefined ? '' : words[index];
649
+ if (word.startsWith('--')) {
650
+ const equals = word.indexOf('=');
651
+ const name = equals < 0 ? word : word.slice(0, equals);
652
+ const attached = equals < 0 ? undefined : word.slice(equals + 1);
653
+ if (name === '--request') {
654
+ const value = attached === undefined ? words[index + 1] : attached;
655
+ if (attached === undefined) index += 1;
656
+ if (!isReadMethod(value)) return 'unnamed';
657
+ } else if (CURL_SENDING_FLAGS.has(name)) {
658
+ return 'unnamed';
659
+ } else if (CURL_FILE_VALUE_FLAGS.has(name)) {
660
+ const value = attached === undefined ? words[index + 1] : attached;
661
+ if (attached === undefined) index += 1;
662
+ if (value !== '-') toFile = true;
663
+ } else if (CURL_FILE_FLAGS.has(name)) {
664
+ toFile = true;
665
+ } else if (CURL_VALUE_ONLY_FLAGS.has(name) && attached === undefined) {
666
+ index += 1;
667
+ }
668
+ } else if (word.length > 1 && word.startsWith('-')) {
669
+ const chars = word.slice(1);
670
+ for (let at = 0; at < chars.length; at += 1) {
671
+ const flag = chars[at];
672
+ if (flag === 'O') toFile = true;
673
+ if (!CURL_SHORT_VALUE_FLAGS.has(flag)) continue;
674
+ // The first value-taking flag ends the cluster: the rest of the word is its value.
675
+ let value = chars.slice(at + 1);
676
+ if (value === '') {
677
+ value = words[index + 1];
678
+ index += 1;
679
+ }
680
+ // A body, a form, an upload or a config this reader cannot see.
681
+ if ('dFTK'.includes(flag)) return 'unnamed';
682
+ if (flag === 'X' && !isReadMethod(value)) return 'unnamed';
683
+ if ('oDc'.includes(flag) && value !== '-') toFile = true;
684
+ break;
685
+ }
686
+ }
687
+ }
688
+ return toFile ? 'preparing' : 'exploring';
689
+ }
690
+
492
691
  /**
493
692
  * Whether a segment asks a known program for its own usage page: a request, a flag the
494
693
  * segment's own text merely mentions or an option consumed, or an unreadable one this
495
694
  * classifier refuses to guess about.
496
695
  */
497
696
  function helpFlag(segment) {
498
- const words = shellWordTokens(segment);
697
+ const words = shellWordTokens(commandText(segment));
499
698
  const at = words.findIndex((word) => !word.quoted && word.text === '--help');
500
699
  if (at < 0) return 'absent';
501
700
  // An option may take the help flag as its own value: `git commit -m --help` commits a
@@ -510,13 +709,32 @@ function helpFlag(segment) {
510
709
  const previous = words[at - 1];
511
710
  if (previous !== undefined && (previous.text.startsWith('-') || previous.text.startsWith('$'))) return 'absent';
512
711
  const program = (words[0] === undefined ? '' : words[0].text).split('/').pop();
513
- if (!HELP_AWARE_COMMANDS.has(program === undefined ? '' : program)) return 'unreadable';
712
+ if (!HELP_AWARE_COMMANDS.has(program === undefined ? '' : program)) return isPlainUsageRequest(words, at) ? 'request' : 'unreadable';
514
713
  // `node -e '…' --help` runs the payload with `--help` as its own argument.
515
714
  if (SCRIPT_RUNNERS.has(program) && words.slice(1).some((word) => PAYLOAD_FLAGS.has(word.text))) return 'unreadable';
516
- if (writesFile(segment) || HIDDEN_SUBSTITUTION.test(segment)) return 'unreadable';
715
+ if (writesFile(segment) || hasHiddenSubstitution(segment)) return 'unreadable';
517
716
  return 'request';
518
717
  }
519
718
 
719
+ // Programs that run what they are handed, so a `--help` after them may reach code instead of
720
+ // asking for usage.
721
+ const HELP_RUNNERS = new Set([
722
+ 'bash', 'sh', 'zsh', 'fish', 'dash', 'ksh', 'python', 'python3', 'ruby', 'perl', 'php', 'lua', 'xargs',
723
+ 'env', 'sudo', 'doas', 'exec', 'nohup', 'watch', 'ssh', 'time', 'timeout', 'eval', 'source', 'sh', 'bunx', 'pnpx', 'npx',
724
+ ]);
725
+
726
+ /**
727
+ * An unknown program asked for its usage, in the one shape no program reads as anything
728
+ * else: bare words (a program and its subcommands, no option, no path) and a final
729
+ * `--help`. A program that runs code, or a path to a script, is never read this way.
730
+ */
731
+ function isPlainUsageRequest(words, at) {
732
+ if (at !== words.length - 1 || at < 1 || at > 4) return false;
733
+ const program = words[0].text;
734
+ if (HELP_RUNNERS.has(program) || /[/.]/.test(program)) return false;
735
+ return words.slice(0, at).every((word) => !word.quoted && /^[A-Za-z][\w:-]*$/.test(word.text));
736
+ }
737
+
520
738
  /**
521
739
  * Splits a segment into the words the program receives, dropping the quoting
522
740
  * characters the shell would drop. A quoted `--help` is text handed to the program, and
@@ -801,8 +1019,20 @@ function shellCommandSegments(command) {
801
1019
  const commands = [];
802
1020
  let current = '';
803
1021
  let quote;
1022
+ // The heredoc opened on the line being read: its body begins at the next newline.
1023
+ let pending;
804
1024
  for (let i = 0; i < command.length; i += 1) {
805
1025
  const char = command[i];
1026
+ if (pending !== undefined && i === pending.newline && quote === undefined) {
1027
+ // The body belongs to the segment that OPENED the heredoc, which may already be in
1028
+ // the list when a pipe or `&&` followed the header on the same line.
1029
+ const body = command.slice(pending.newline, pending.end);
1030
+ if (pending.owner === undefined) current += body;
1031
+ else commands[pending.owner] += body;
1032
+ i = pending.end - 1;
1033
+ pending = undefined;
1034
+ continue;
1035
+ }
806
1036
  if (quote === "'") {
807
1037
  current += char;
808
1038
  if (char === "'") quote = undefined;
@@ -827,13 +1057,27 @@ function shellCommandSegments(command) {
827
1057
  // their own. A body this reader cannot delimit is left in place, and the split below
828
1058
  // reads it exactly as it always did.
829
1059
  const end = heredocEnd(command, i);
830
- if (end === undefined) {
1060
+ const header = HEREDOC_HEADER.exec(command.slice(i));
1061
+ const newline = header === null ? -1 : command.indexOf('\n', i + header[0].length);
1062
+ if (pending !== undefined && header !== null) {
1063
+ // A second heredoc on the same line stacks its body behind the first: this reader does
1064
+ // not follow that, so the rest is ONE segment it cannot name, never commands of its own.
1065
+ return [...commands, current + command.slice(i)];
1066
+ }
1067
+ if (end === undefined || header === null || newline < 0 || newline >= end) {
831
1068
  current += char;
832
1069
  } else {
833
- current += command.slice(i, end);
834
- i = end - 1;
1070
+ // What follows the header on its line (`| head x`, `&& rm y`) is still the shell's
1071
+ // own text and is read as commands; only the body after the newline is data.
1072
+ current += header[0];
1073
+ pending = { newline, end, owner: undefined };
1074
+ i += header[0].length - 1;
835
1075
  }
1076
+ } else if (char === '#' && (current === '' || /[\s;&|(]$/.test(current))) {
1077
+ // A comment runs to the end of its line and is never a command.
1078
+ while (i + 1 < command.length && command[i + 1] !== '\n') i += 1;
836
1079
  } else if (isSeparator(command, i)) {
1080
+ if (pending !== undefined && pending.owner === undefined) pending.owner = commands.length;
837
1081
  commands.push(current);
838
1082
  current = '';
839
1083
  if ((char === '|' || char === '&') && command[i + 1] === char) i += 1;
@@ -982,7 +1226,7 @@ function stripToolGlobalOptions(segment) {
982
1226
  // as another user, and a heading must never file a privileged action as the read it
983
1227
  // resembles. `timeout` is absent too, because this suite pins its reading: a watchdog is
984
1228
  // unwrapped for a check and for nothing else.
985
- const WRAPPERS = new Set(['env', 'nice', 'ionice', 'nohup', 'stdbuf', 'command', 'exec', 'time']);
1229
+ const WRAPPERS = new Set(['env', 'nice', 'ionice', 'nohup', 'stdbuf', 'command', 'exec', 'time', 'timeout']);
986
1230
  // Options that take their value as the next word (or attached with `=`) and change
987
1231
  // nothing about which command runs.
988
1232
  const WRAPPER_VALUE_OPTIONS = {
@@ -992,6 +1236,7 @@ const WRAPPER_VALUE_OPTIONS = {
992
1236
  stdbuf: new Set(['-i', '--input', '-o', '--output', '-e', '--error']),
993
1237
  time: new Set(['-f', '--format']),
994
1238
  exec: new Set(['-a']),
1239
+ timeout: new Set(['-s', '--signal', '-k', '--kill-after']),
995
1240
  };
996
1241
  // Options whose value is attached to the flag itself (`-c2`, `-oL`, `-10`).
997
1242
  const WRAPPER_ATTACHED_VALUES = { ionice: /^-[cn].+/, stdbuf: /^-[ioe].+/, nice: /^-\d+$/ };
@@ -1058,6 +1303,8 @@ function stripWrapper(segment) {
1058
1303
  // `nohup --help`, `command -p`. It is skipped, and the command behind it still runs.
1059
1304
  index += 1;
1060
1305
  }
1306
+ // `timeout` takes its duration before the command it bounds.
1307
+ if (program === 'timeout') index += 1;
1061
1308
  const rest = words[index];
1062
1309
  return rest === undefined ? segment : segment.slice(rest.start);
1063
1310
  }
@@ -1114,9 +1361,9 @@ function codeGraphCommandWords(segment) {
1114
1361
  return at < 0 ? undefined : words.slice(at + 1);
1115
1362
  }
1116
1363
 
1117
- function isReadOnlyInspection(segment) {
1364
+ function isReadOnlyInspection(segment, hidden = hasHiddenSubstitution(segment)) {
1118
1365
  return !writesFile(segment)
1119
- && !HIDDEN_SUBSTITUTION.test(segment)
1366
+ && !hidden
1120
1367
  && (matchesAny(EXPLORING_COMMANDS, segment) || isGhApiRead(segment) || isGoEnvRead(segment));
1121
1368
  }
1122
1369
 
@@ -1125,6 +1372,92 @@ function isReadOnlyInspection(segment) {
1125
1372
  // segment carrying one is ambiguous rather than inspection.
1126
1373
  const HIDDEN_SUBSTITUTION = /`|<\(/;
1127
1374
 
1375
+ // A heredoc hands its body to the program as data, so the body is no part of the text the
1376
+ // shell acts on: a quoted delimiter (`<<'EOF'`, `<<"EOF"`, `<<\EOF`) keeps it inert, and an
1377
+ // unquoted one lets the shell expand `$(...)` and backticks inside it.
1378
+ const HEREDOC_HEADER = /^<<(-?)\s*(?:'([^']*)'|"([^"]*)"|(\\?)([A-Za-z_][\w.-]*))/;
1379
+
1380
+ /**
1381
+ * A segment split into the text the shell acts on and the heredoc bodies it hands to a
1382
+ * program. A heredoc this reader cannot delimit stays where it is, so what it cannot read
1383
+ * is never dropped from the text.
1384
+ */
1385
+ function splitHeredocs(segment) {
1386
+ if (!segment.includes('<<')) return { text: segment, bodies: [] };
1387
+ let text = '';
1388
+ const bodies = [];
1389
+ let quote;
1390
+ for (let i = 0; i < segment.length; i += 1) {
1391
+ const char = segment[i];
1392
+ if (quote === "'") {
1393
+ text += char;
1394
+ if (char === "'") quote = undefined;
1395
+ } else if (char === '\\') {
1396
+ text += char + (segment[i + 1] === undefined ? '' : segment[i + 1]);
1397
+ i += 1;
1398
+ } else if (quote === '"') {
1399
+ text += char;
1400
+ if (char === '"') quote = undefined;
1401
+ } else if (char === "'" || char === '"') {
1402
+ quote = char;
1403
+ text += char;
1404
+ } else if (char === '<' && segment[i + 1] === '<' && segment[i + 2] !== '<') {
1405
+ const end = heredocEnd(segment, i);
1406
+ const header = HEREDOC_HEADER.exec(segment.slice(i));
1407
+ const newline = header === null ? -1 : segment.indexOf('\n', i + header[0].length);
1408
+ if (end === undefined || header === null || newline < 0 || newline >= end) {
1409
+ text += char;
1410
+ } else {
1411
+ text += segment.slice(i, newline);
1412
+ bodies.push({ text: segment.slice(newline, end), expanding: header[2] === undefined && header[3] === undefined && header[4] === '' });
1413
+ i = end - 1;
1414
+ }
1415
+ } else {
1416
+ text += char;
1417
+ }
1418
+ }
1419
+ return { text, bodies };
1420
+ }
1421
+
1422
+ /** The text with every single-quoted span blanked: nothing inside one is ever expanded. */
1423
+ function blankSingleQuotes(text) {
1424
+ let out = '';
1425
+ let quote;
1426
+ for (let i = 0; i < text.length; i += 1) {
1427
+ const char = text[i];
1428
+ if (quote === "'") {
1429
+ if (char === "'") {
1430
+ quote = undefined;
1431
+ out += char;
1432
+ }
1433
+ } else if (char === '\\') {
1434
+ out += char + (text[i + 1] === undefined ? '' : text[i + 1]);
1435
+ i += 1;
1436
+ } else if (quote === '"') {
1437
+ if (char === '"') quote = undefined;
1438
+ out += char;
1439
+ } else {
1440
+ if (char === "'" || char === '"') quote = char;
1441
+ out += char;
1442
+ }
1443
+ }
1444
+ return out;
1445
+ }
1446
+
1447
+ /** The text of a segment the shell acts on, without the data its heredocs hand over. */
1448
+ function commandText(segment) {
1449
+ return splitHeredocs(segment).text;
1450
+ }
1451
+
1452
+ /**
1453
+ * Whether a segment carries a command this reader cannot follow: a backtick or `<(...)` in
1454
+ * the text, or a substitution inside the body of a heredoc whose delimiter is unquoted.
1455
+ */
1456
+ function hasHiddenSubstitution(segment) {
1457
+ const { text, bodies } = splitHeredocs(segment);
1458
+ return HIDDEN_SUBSTITUTION.test(blankSingleQuotes(text).replace(/\\[\s\S]/g, ' ')) || bodies.some((body) => body.expanding && /`|\$\(/.test(body.text.replace(/\\[\s\S]/g, ' ')));
1459
+ }
1460
+
1128
1461
  /** `gh api` without a method, field, or input body reads; with one it writes. */
1129
1462
  function isGhApiRead(segment) {
1130
1463
  const words = segment.split(/\s+/);
@@ -1150,7 +1483,47 @@ const HARMLESS_REDIRECTS = /\s*(\d?>&\d|&?\d?>\s*\/dev\/null)/g;
1150
1483
  // redirect, so only the text the shell would act on counts. Deliberately naive beyond
1151
1484
  // that: an unbalanced quote keeps its `>`, which only ever leaves the call unnamed.
1152
1485
  function writesFile(segment) {
1153
- return unquotedSegment(segment).replace(HARMLESS_REDIRECTS, '').includes('>');
1486
+ return unquotedSegment(commandText(segment)).replace(HARMLESS_REDIRECTS, '').includes('>');
1487
+ }
1488
+
1489
+ /**
1490
+ * The text without its redirections into files, or `undefined` when one is not a plain
1491
+ * `> target`: a process substitution (`>(...)`) runs a command, and a redirect with no
1492
+ * target is a command this reader cannot follow.
1493
+ */
1494
+ function withoutFileRedirects(text) {
1495
+ let out = '';
1496
+ let quote;
1497
+ for (let i = 0; i < text.length; i += 1) {
1498
+ const char = text[i];
1499
+ if (quote) {
1500
+ out += char;
1501
+ if (char === quote) quote = undefined;
1502
+ else if (char === '\\' && quote === '"') {
1503
+ out += text[i + 1] === undefined ? '' : text[i + 1];
1504
+ i += 1;
1505
+ }
1506
+ } else if (char === '\\') {
1507
+ out += char + (text[i + 1] === undefined ? '' : text[i + 1]);
1508
+ i += 1;
1509
+ } else if (char === "'" || char === '"') {
1510
+ quote = char;
1511
+ out += char;
1512
+ } else if (char === '>') {
1513
+ if (text[i + 1] === '(') return undefined;
1514
+ let at = i + 1;
1515
+ if (text[at] === '>' || text[at] === '|' || text[at] === '&') at += 1;
1516
+ while (text[at] === ' ' || text[at] === '\t') at += 1;
1517
+ const target = /^("[^"]*"|'[^']*'|[^\s;|&<>()]+)/.exec(text.slice(at));
1518
+ if (target === null) return undefined;
1519
+ i = at + target[0].length - 1;
1520
+ // The file descriptor or `&` written right before the operator belongs to it.
1521
+ out = out.replace(/(^|\s)\d*&?$/, '$1');
1522
+ } else {
1523
+ out += char;
1524
+ }
1525
+ }
1526
+ return out.replace(/\s+/g, ' ').trim();
1154
1527
  }
1155
1528
 
1156
1529
  function matchesAny(patterns, segment) {