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.
@@ -326,9 +326,9 @@ test('make options keep a check while an unknown recipe names nothing', () => {
326
326
  }
327
327
  });
328
328
 
329
- // A watchdog bounds how long a check may run without changing what it does, so a
330
- // runner behind `timeout` still names the call. Anything else behind one is not a
331
- // check and is not inspection either.
329
+ // A watchdog bounds how long a command may run without changing what it does, so the
330
+ // command behind `timeout` names the call exactly as it would alone (every other family
331
+ // is pinned in 'timeout names the command it bounds'). What no table names stays unnamed.
332
332
  test('a verification runner behind timeout still names the checking activity', () => {
333
333
  for (const command of [
334
334
  'timeout 900 pnpm typecheck 2>&1 | tail -20',
@@ -343,7 +343,7 @@ test('a verification runner behind timeout still names the checking activity', (
343
343
  ]) {
344
344
  assert.equal(inferBashActivity(command), 'checking', command);
345
345
  }
346
- for (const command of ['timeout 5 node scripts/release.mjs', 'timeout 5 git status', 'timeout 300 unknown-script --watch', 'timeout 5']) {
346
+ for (const command of ['timeout 5 node scripts/release.mjs', 'timeout 300 unknown-script --watch', 'timeout 5']) {
347
347
  assert.equal(inferBashActivity(command), undefined, command);
348
348
  }
349
349
  });
@@ -726,14 +726,16 @@ test('a database client fed a deletion names the destructive card', () => {
726
726
  ]) {
727
727
  assert.equal(inferBashActivity(command), undefined, command);
728
728
  }
729
- // A segment outside the payload is still unnamed, so the whole call stays generic:
730
- // the deletion is proven, but the card never hides the work it cannot name.
729
+ // A segment outside the payload that nothing can name does not take the deletion away:
730
+ // the deletion is proven, and the heading claims only what the call provably did.
731
731
  for (const command of [
732
732
  `psql -c 'DROP TABLE t' && ./scripts/unknown.sh`,
733
733
  `echo 'DROP TABLE t' | psql && ./scripts/unknown.sh`,
734
734
  "psql <<'SQL'\nDROP TABLE t;\nSQL\n./scripts/unknown.sh",
735
+ 'rm -rf dist && ./scripts/unknown.sh',
736
+ 'python3 tools/run.py && rm -f out.log',
735
737
  ]) {
736
- assert.equal(inferBashActivity(command), undefined, command);
738
+ assert.equal(inferBashActivity(command), 'destructive', command);
737
739
  }
738
740
  });
739
741
 
@@ -753,7 +755,9 @@ test('a deleting program asked for its usage page is the query it is', () => {
753
755
  ]) {
754
756
  assert.equal(inferBashActivity(command), 'exploring', command);
755
757
  }
756
- assert.equal(inferBashActivity('psql --help'), undefined);
758
+ // An unknown program asked for its usage in the one plain shape (bare words and a final
759
+ // `--help`) is the same query; any other shape of an unknown program stays unnamed.
760
+ assert.equal(inferBashActivity('psql --help'), 'exploring');
757
761
  // A help flag directly after a flag-shaped word stays that word's value, the
758
762
  // reading this classifier already makes everywhere (T2): the flag is then not a
759
763
  // query, and the deletion it would have printed usage for removed nothing either,
@@ -769,16 +773,17 @@ test('a deleting program asked for its usage page is the query it is', () => {
769
773
  // whose name is an inherited member of a lookup table answers "no evidence" instead
770
774
  // of throwing on the row it was asked to draw.
771
775
  test('prototype-hazardous programs never throw and never name a deletion', () => {
772
- for (const command of ['constructor -rf dist', '__proto__ -rf dist', '__proto__ rm -rf dist', 'constructor --help']) {
776
+ for (const command of ['constructor -rf dist', '__proto__ -rf dist', '__proto__ rm -rf dist']) {
773
777
  assert.equal(inferBashActivity(command), undefined, command);
774
778
  }
779
+ assert.equal(inferBashActivity('constructor --help'), 'exploring');
775
780
  });
776
781
 
777
782
  // ---- the precedence ladder ----
778
783
 
779
- // A check outranks a remote write, a history change, a rewrite and a build-up; a
780
- // remote write outranks a history change, a rewrite outranks a build-up, and
781
- // CodeGraph sits just above plain inspection. Every rung is stated with the two
784
+ // What the call changed outranks what it verified: a remote write outranks a history
785
+ // change, a history change outranks a check, a check outranks a rewrite, a rewrite outranks
786
+ // a build-up, and CodeGraph sits just above plain inspection. Every rung is stated with the two
782
787
  // families it separates, so a moved rung fails here.
783
788
  test('the strongest named segment decides the card', () => {
784
789
  assert.equal(inferBashActivity('pnpm test | tail -20'), 'checking');
@@ -788,9 +793,12 @@ test('the strongest named segment decides the card', () => {
788
793
  assert.equal(inferBashActivity('pnpm install && git config user.name x'), 'preparing');
789
794
  assert.equal(inferBashActivity('prettier --write . && pnpm install'), 'formatting');
790
795
  assert.equal(inferBashActivity('git status && pnpm install'), 'preparing');
791
- for (const command of ['pnpm install && pnpm test', 'git push && pnpm test', 'git commit -m x && make test']) {
796
+ for (const command of ['pnpm install && pnpm test', 'prettier --write . && pnpm test', 'git status && make test']) {
792
797
  assert.equal(inferBashActivity(command), 'checking', command);
793
798
  }
799
+ assert.equal(inferBashActivity('git push && pnpm test'), 'publishing');
800
+ assert.equal(inferBashActivity('git commit -m x && make test'), 'versioning');
801
+ assert.equal(inferBashActivity('pnpm test && git commit -m x'), 'versioning');
794
802
  // The top rung: a deletion outranks every identity beside it, because it is the
795
803
  // one consequence of the call a reader cannot undo. A check, an inspection or a
796
804
  // remote write in the same command never takes the card from it.
@@ -813,11 +821,9 @@ test('the strongest named segment decides the card', () => {
813
821
  test('an unnamed segment anywhere keeps the whole call generic', () => {
814
822
  for (const command of [
815
823
  'pnpm test && node scripts/release.mjs',
816
- 'pnpm test && echo ok > out.txt',
817
- 'pnpm test && echo done | tee log.txt',
818
824
  'pnpm test && ./scripts/deploy.sh',
819
825
  "pnpm test && bash -c 'unknown-tool --flag'",
820
- 'curl https://example.com',
826
+ 'curl -X POST https://example.com',
821
827
  'ls && unknown-tool --flag',
822
828
  // A program whose name is an inherited member of a lookup table is still just an
823
829
  // unknown program: the row it is drawn on must never throw.
@@ -867,8 +873,10 @@ test('quoted text never turns a command into a write', () => {
867
873
  assert.equal(inferBashActivity('gh issue comment 7 --body "before > after"'), 'publishing');
868
874
  assert.equal(inferBashActivity('git log --format="%h > %s" -3'), 'exploring');
869
875
  assert.equal(inferBashActivity("printf '%s\\n' \"a > b\" | cat"), 'exploring');
870
- for (const command of ['git commit -m x > out.txt', 'cat src/a > out', "echo 'a > b' > out.txt"]) {
871
- assert.equal(inferBashActivity(command), undefined, command);
876
+ assert.equal(inferBashActivity('git commit -m x > out.txt'), undefined);
877
+ // The unquoted redirect is a file the call creates; the quoted `>` stays text.
878
+ for (const command of ['cat src/a > out', "echo 'a > b' > out.txt"]) {
879
+ assert.equal(inferBashActivity(command), 'preparing', command);
872
880
  }
873
881
  });
874
882
 
@@ -902,10 +910,12 @@ test('help detection never masks a mutation or a payload it cannot read', () =>
902
910
  assert.equal(inferBashActivity("git commit -m 'docs: mention --help'"), 'versioning');
903
911
  assert.equal(inferBashActivity("printf 'git push --help'"), undefined);
904
912
  assert.equal(inferBashActivity('echo "--help"'), undefined);
905
- // An unknown program's help flag is a guess, and a payload flag can swallow the
906
- // help flag, so neither is a query.
913
+ // An unknown program's help flag is a query only in the plain shape (bare words, a final
914
+ // `--help`, no path, no runner); a script path, a shell and a payload flag can swallow
915
+ // the help flag, so none of them is a query.
907
916
  for (const command of [
908
- 'unknown-tool --help',
917
+ 'bash deploy.sh --help',
918
+ 'sudo --help',
909
919
  './scripts/deploy.sh --help',
910
920
  "bash -c 'git status' --help",
911
921
  "node -e 'process.exit(0)' --help",
@@ -1000,7 +1010,7 @@ test('the segmenter refuses to split what it cannot prove', () => {
1000
1010
  ['git commit -m "unterminated && pnpm test', 'versioning'],
1001
1011
  // A quoted separator is one argument, and the call keeps the identity its own
1002
1012
  // command names.
1003
- ['git commit -m "a; b" && pnpm test', 'checking'],
1013
+ ['git commit -m "a; b" && pnpm test', 'versioning'],
1004
1014
  // A quoted `$(...)` still contributes its own commands, so a check hidden in a
1005
1015
  // commit message is a check this card names.
1006
1016
  ['git commit -m "$(printf x)"', 'versioning'],
@@ -1111,8 +1121,8 @@ test('a program named by a path classifies as its basename', () => {
1111
1121
 
1112
1122
  // A wrapper changes how the command runs, not what it does, so the command behind it
1113
1123
  // names the card. `sudo` is deliberately absent: it runs the command as another user,
1114
- // and no heading may file a privileged action as the read it resembles. `timeout` keeps
1115
- // the reading this suite already pinned — only a check is unwrapped.
1124
+ // and no heading may file a privileged action as the read it resembles. `timeout` only
1125
+ // bounds the time, so it is unwrapped like the rest.
1116
1126
  test('a wrapper names the command it runs, and sudo never names anything', () => {
1117
1127
  for (const [command, activity] of [
1118
1128
  ['env FOO=1 git status', 'exploring'],
@@ -1134,8 +1144,8 @@ test('a wrapper names the command it runs, and sudo never names anything', () =>
1134
1144
  ['sudo git status', undefined],
1135
1145
  ['sudo -u root pnpm test', undefined],
1136
1146
  ['env -S "git status"', undefined],
1137
- ['env', undefined],
1138
- ['timeout 5 git status', undefined],
1147
+ ['env', 'exploring'],
1148
+ ['timeout 5 git status', 'exploring'],
1139
1149
  ]) {
1140
1150
  assert.equal(inferBashActivity(command), activity, command);
1141
1151
  }
@@ -1364,10 +1374,9 @@ test('a heredoc body belongs to the command that reads it', () => {
1364
1374
  assert.equal(inferBashActivity("git commit -F - <<'EOF'\nMerge branch 'main' of example\nEOF"), 'versioning');
1365
1375
  assert.equal(inferBashActivity("gh pr create --title x --body \"$(cat <<'EOF'\nsome body\nEOF\n)\""), 'publishing');
1366
1376
  assert.equal(inferBashActivity("git commit -m \"$(cat <<'EOF'\nMerge branch 'main'\n\nbody\nEOF\n)\""), 'versioning');
1367
- // A heredoc that writes a file is still a write, and a payload this classifier cannot
1368
- // read is still unnamed.
1369
- assert.equal(inferBashActivity("cat > notes.txt <<'EOF'\nhello\nEOF"), undefined);
1370
- assert.equal(inferBashActivity("python3 - <<'PY'\nprint(1)\nPY"), undefined);
1377
+ // A heredoc that writes a file creates it, and a script that only prints is read.
1378
+ assert.equal(inferBashActivity("cat > notes.txt <<'EOF'\nhello\nEOF"), 'preparing');
1379
+ assert.equal(inferBashActivity("python3 - <<'PY'\nprint(1)\nPY"), 'exploring');
1371
1380
  });
1372
1381
 
1373
1382
  // The payloads nobody can read stay generic on purpose: an inline script, an interpreter
@@ -1375,15 +1384,10 @@ test('a heredoc body belongs to the command that reads it', () => {
1375
1384
  // file, a privileged invocation, a shell fed by a pipe, and `eval`.
1376
1385
  test('the payloads this classifier cannot read stay generic on purpose', () => {
1377
1386
  for (const command of [
1378
- "python3 -c 'print(1)'",
1379
- "node -e 'console.log(1)'",
1380
1387
  "bash -c 'echo hi'",
1381
1388
  'node scripts/build.mjs',
1382
1389
  'python3 tools/run.py --fast',
1383
1390
  'bash scripts/deploy.sh',
1384
- "sed -i 's/a/b/' file.txt",
1385
- "perl -pi -e 's/a/b/' file.txt",
1386
- 'cat > notes.txt',
1387
1391
  'echo hi | sh',
1388
1392
  'eval "$(cat setup.sh)"',
1389
1393
  'xargs rm -rf',
@@ -1418,3 +1422,364 @@ test('a removed checkout wears the destructive family, a created one prepares',
1418
1422
  assert.equal(inferBashActivity('git worktree add ../new -b feat/x'), 'preparing');
1419
1423
  assert.equal(inferBashActivity('git worktree list'), 'exploring');
1420
1424
  });
1425
+
1426
+ // ---- coverage: the shapes real transcripts left generic ----
1427
+ //
1428
+ // Mined from the user's own sessions (19k bash calls, ~29% generic): the largest honest
1429
+ // share was not an unknown program but a SHAPE the reader misread — a heredoc body that
1430
+ // carries backticks or an arrow, a redirect that only creates a file, a bare `do`/`break`
1431
+ // line, a `timeout` around an inspection. Each case below names what the call does and
1432
+ // nothing it did not.
1433
+
1434
+ // A heredoc whose delimiter is quoted hands its body to the program as plain data: a
1435
+ // backtick or an `->` in a commit message is that message, not a hidden command and not
1436
+ // a redirect. Only an expanding body (unquoted delimiter) can still run something.
1437
+ test('a quoted heredoc body is data, whatever characters it carries', () => {
1438
+ assert.equal(inferBashActivity("git commit -q -F - <<'MSG'\nfix(cards): repaint `Card tags` a -> b\n\nbody > more\nMSG"), 'versioning');
1439
+ assert.equal(
1440
+ inferBashActivity("cd /repo && set -e\ngit add a b\ngit commit -q -F - <<'MSG'\nfix: x `y`\nMSG\ngit log --oneline -5; git status --short | wc -l"),
1441
+ 'versioning',
1442
+ );
1443
+ assert.equal(inferBashActivity("cat <<'EOF'\nhello `x` --help -> y\nEOF"), 'exploring');
1444
+ assert.equal(inferBashActivity('git commit -F - <<"MSG"\nfix `x`\nMSG'), 'versioning');
1445
+ assert.equal(inferBashActivity('git commit -F - <<\\MSG\nfix `x`\nMSG'), 'versioning');
1446
+ // An unquoted delimiter expands its body, so a substitution there may run anything.
1447
+ for (const command of ['git commit -F - <<MSG\nfix `date`\nMSG', 'git commit -F - <<MSG\nfix $(date)\nMSG']) {
1448
+ assert.equal(inferBashActivity(command), undefined, command);
1449
+ }
1450
+ // What follows the delimiter on the header line is still the command's own text.
1451
+ assert.equal(inferBashActivity("git commit -F - <<'MSG' > out.txt\nfix\nMSG"), undefined);
1452
+ });
1453
+
1454
+ // A redirect that only CREATES a file from a read or from text is the build-up the
1455
+ // `preparing` family already names for `mkdir`, `touch` and `cp`; it is never a mutation
1456
+ // of anything else, so a mutating command beside a redirect stays generic.
1457
+ test('a redirect that writes a file from a read or from text prepares', () => {
1458
+ for (const command of [
1459
+ "cat > \"$B\" <<'EOF'\n## What\nEOF",
1460
+ "cat >> notes.md <<'EOF'\nmore\nEOF",
1461
+ 'echo ok > out.txt',
1462
+ "printf '%s\\n' a b > list.txt",
1463
+ 'git show HEAD:a.css > build/a.css',
1464
+ 'gh pr view 12 --json body -q .body > body.md',
1465
+ 'git log --oneline 2>&1 > log.txt',
1466
+ 'echo done | tee log.txt',
1467
+ 'ls -la | tee -a listing.txt',
1468
+ ]) {
1469
+ assert.equal(inferBashActivity(command), 'preparing', command);
1470
+ }
1471
+ assert.equal(inferBashActivity('pnpm test && echo ok > out.txt'), 'checking');
1472
+ assert.equal(inferBashActivity('git push -q && echo ok > out.txt'), 'publishing');
1473
+ assert.equal(
1474
+ inferBashActivity("B=$(mktemp /tmp/b.XXXXXX)\ncat > \"$B\" <<'EOF'\nx\nEOF\ngh pr create --title t --body-file \"$B\" 2>&1 | tail -1"),
1475
+ 'publishing',
1476
+ );
1477
+ // The deletion beside it still outranks everything.
1478
+ assert.equal(
1479
+ inferBashActivity("B=$(mktemp /tmp/b.XXXXXX)\ncat > \"$B\" <<'EOF'\nx\nEOF\ngh pr create --title t --body-file \"$B\"\nrm -f \"$B\""),
1480
+ 'destructive',
1481
+ );
1482
+ for (const command of [
1483
+ 'git push > out.txt',
1484
+ 'npm install > install.log',
1485
+ 'git commit -m x > out.txt',
1486
+ 'unknown-tool > out.txt',
1487
+ 'python3 script.py > out.txt',
1488
+ 'echo x > >(tee f)',
1489
+ 'cat <(touch marker)',
1490
+ 'sed -i s/a/b/ f > out',
1491
+ 'echo x >',
1492
+ ]) {
1493
+ assert.equal(inferBashActivity(command), undefined, command);
1494
+ }
1495
+ });
1496
+
1497
+ // The lines of a script the shell reads as keywords carry no work of their own.
1498
+ test('loop keywords and declarations are neutral beside the commands they frame', () => {
1499
+ assert.equal(inferBashActivity('for f in a b; do\n git add "$f"\ndone'), 'versioning');
1500
+ assert.equal(inferBashActivity('for x in a b; do git status; break; done'), 'exploring');
1501
+ assert.equal(inferBashActivity('while read -r l; do\n git status\n continue\ndone < list.txt'), 'exploring');
1502
+ assert.equal(inferBashActivity('declare -A m=( [1]=2 )\ngit status'), 'exploring');
1503
+ assert.equal(inferBashActivity('if true; then\n git status\nelse\n git log\nfi'), 'exploring');
1504
+ for (const command of ['do', 'done < list.txt', 'break', 'declare -A m=( [1]=2 )']) {
1505
+ assert.equal(inferBashActivity(command), undefined, command);
1506
+ }
1507
+ });
1508
+
1509
+ // A watchdog changes how long a command may run, never what it does, so the command
1510
+ // behind it names the call exactly as it would alone.
1511
+ test('timeout names the command it bounds', () => {
1512
+ for (const [command, activity] of [
1513
+ ['timeout 5 git status', 'exploring'],
1514
+ ['timeout -k 5 60 git fetch', 'versioning'],
1515
+ ['timeout --foreground 30 git push', 'publishing'],
1516
+ ['timeout 30 rm -rf node_modules', 'destructive'],
1517
+ ['timeout 25 grep -rIl -i foo /tmp', 'exploring'],
1518
+ ['timeout 5', undefined],
1519
+ ['timeout 5 unknown-tool --watch', undefined],
1520
+ ['timeout 5 node scripts/release.mjs', undefined],
1521
+ ]) {
1522
+ assert.equal(inferBashActivity(command), activity, command);
1523
+ }
1524
+ });
1525
+
1526
+ test('version probes, environment listings and read-only host probes are exploring', () => {
1527
+ for (const command of [
1528
+ 'node -v',
1529
+ 'node --version',
1530
+ 'python3 --version',
1531
+ 'git --version',
1532
+ 'cargo -V',
1533
+ 'go version',
1534
+ 'env',
1535
+ 'mount',
1536
+ 'ss -ltnp',
1537
+ 'lsblk -f',
1538
+ 'findmnt /',
1539
+ 'ip addr show',
1540
+ 'ip -br link',
1541
+ 'dconf dump /',
1542
+ 'gsettings get org.gnome.desktop.interface gtk-theme',
1543
+ 'niri msg outputs',
1544
+ 'niri msg focused-window',
1545
+ 'pactl info',
1546
+ 'wpctl status',
1547
+ 'fc-list',
1548
+ 'adb devices',
1549
+ 'adb -s emulator-5556 shell getprop sys.boot_completed',
1550
+ 'herdr pane read wC5:p1 --source visible --lines 60',
1551
+ 'herdr plugin config-dir worktree-hooks',
1552
+ 'gentle-ai review mode status',
1553
+ 'git archive HEAD',
1554
+ 'git hash-object lib/a.js',
1555
+ 'npm pack --dry-run',
1556
+ 'npm pack --dry-run 2>&1 | grep -E "filename|total files" | head -4',
1557
+ "git branch --show-current && pwd",
1558
+ ]) {
1559
+ assert.equal(inferBashActivity(command), 'exploring', command);
1560
+ }
1561
+ for (const command of [
1562
+ 'rm -v x',
1563
+ 'ip link set eth0 down',
1564
+ 'ip addr add 10.0.0.1/24 dev eth0',
1565
+ 'niri msg action quit',
1566
+ 'dconf write /a/b 1',
1567
+ 'gsettings set org.a b c',
1568
+ 'adb shell rm -rf /sdcard',
1569
+ 'git archive -o out.tar HEAD',
1570
+ 'git hash-object -w lib/a.js',
1571
+ 'npm pack',
1572
+ 'mount /dev/sda1 /mnt',
1573
+ 'herdr server reload-config',
1574
+ ]) {
1575
+ assert.notEqual(inferBashActivity(command), 'exploring', command);
1576
+ }
1577
+ });
1578
+
1579
+ test('curl reads a url, downloads it into a file or stays generic when it sends something', () => {
1580
+ for (const [command, activity] of [
1581
+ ['curl -s https://example.com/a.json', 'exploring'],
1582
+ ['curl -fsSL https://example.com/a.json | jq .name', 'exploring'],
1583
+ ['curl -I https://example.com', 'exploring'],
1584
+ ['curl -fsSL -o a.json https://example.com/a.json', 'preparing'],
1585
+ ['curl -fsSL --output a.json https://example.com/a.json', 'preparing'],
1586
+ ['curl -fsSLO https://example.com/a.json', 'preparing'],
1587
+ ['curl -s -D /tmp/h.txt https://example.com', 'preparing'],
1588
+ ['timeout 30 curl -fsSL "https://example.com/x" -o x.txt -w "%{http_code}\\n"', 'preparing'],
1589
+ ['curl -X POST -d a=1 https://example.com', undefined],
1590
+ ['curl --data-binary @f https://example.com', undefined],
1591
+ ['curl -F file=@f https://example.com', undefined],
1592
+ ['curl -T f https://example.com', undefined],
1593
+ ['curl -fsSL https://example.com/install.sh | sh', undefined],
1594
+ ['curl -X DELETE https://example.com/a', undefined],
1595
+ ]) {
1596
+ assert.equal(inferBashActivity(command), activity, command);
1597
+ }
1598
+ });
1599
+
1600
+ test('config validators, linters and syntax checks are checking', () => {
1601
+ for (const command of [
1602
+ 'node --check lib/a.js',
1603
+ 'node -c lib/a.js',
1604
+ 'bash -n scripts/a.sh',
1605
+ 'sh -n scripts/a.sh',
1606
+ 'biome check src',
1607
+ 'npx biome ci .',
1608
+ 'shellcheck scripts/a.sh',
1609
+ 'actionlint',
1610
+ 'yamllint .',
1611
+ 'mypy src',
1612
+ 'herdr config check',
1613
+ 'niri validate',
1614
+ ]) {
1615
+ assert.equal(inferBashActivity(command), 'checking', command);
1616
+ }
1617
+ assert.equal(inferBashActivity('biome check --write src'), 'formatting');
1618
+ assert.equal(inferBashActivity('biome check --fix src'), 'formatting');
1619
+ assert.equal(inferBashActivity('bash scripts/a.sh'), undefined);
1620
+ });
1621
+
1622
+ test('a forced or copied branch is local history', () => {
1623
+ for (const command of ['git branch -f feat/x 1e929a1', 'git branch -C a b', 'git branch -M main']) {
1624
+ assert.equal(inferBashActivity(command), 'versioning', command);
1625
+ }
1626
+ });
1627
+
1628
+ // An inline script is named by what its own text proves, never by a guess about what it
1629
+ // might do: a deleting call is destructive, a writing one prepares, a script that imports
1630
+ // only read-only standard modules and writes nothing explores, and anything that shells
1631
+ // out, evaluates text, reaches the network or loads other code stays generic.
1632
+ test('an inline script is named by what its own text proves', () => {
1633
+ for (const [command, activity] of [
1634
+ ["python3 -c 'print(1)'", 'exploring'],
1635
+ ['python3 -I -c "import json;d=json.load(open(\'$f\'));print(d.get(\'a\'))"', 'exploring'],
1636
+ ["python3 - <<'PY'\nimport json,sys\nd=json.load(open('a.json'))\nprint(d)\nPY", 'exploring'],
1637
+ ["python3 <<'PY'\nprint(1)\nPY", 'exploring'],
1638
+ ["node -e 'console.log(1)'", 'exploring'],
1639
+ ['node -p "require(\'./package.json\').version"', 'exploring'],
1640
+ ["node --input-type=module -e \"import fs from 'node:fs'; console.log(fs.readFileSync('a','utf8'))\"", 'exploring'],
1641
+ ["node - <<'JS'\nconst fs=require('node:fs');process.stdout.write(fs.readFileSync('a','utf8'))\nJS", 'exploring'],
1642
+ ["python3 - <<'PY'\nimport pathlib\npathlib.Path('a').write_text('x')\nPY", 'preparing'],
1643
+ ["python3 -c \"open('a','w').write('x')\"", 'preparing'],
1644
+ ["node -e \"require('fs').writeFileSync('a','x')\"", 'preparing'],
1645
+ ["python3 -c \"import os; os.remove('a')\"", 'destructive'],
1646
+ ["python3 - <<'PY'\nimport shutil\nshutil.rmtree('d')\nPY", 'destructive'],
1647
+ ["node -e \"require('fs').rmSync('d',{recursive:true})\"", 'destructive'],
1648
+ ['python3 -c "print(1)" > out.txt', 'preparing'],
1649
+ // Not readable: another program, text run as code, the network, code loaded by name.
1650
+ ["python3 -c \"import subprocess; subprocess.run(['ls'])\"", undefined],
1651
+ ['python3 -c "import os; os.system(\'ls\')"', undefined],
1652
+ ['python3 -c "eval(input())"', undefined],
1653
+ ['python3 -c "import requests"', undefined],
1654
+ ['python3 -c "import mymodule"', undefined],
1655
+ ["node -e \"require('child_process').execSync('ls')\"", undefined],
1656
+ ["node -e \"import('file://$PWD/x.js')\"", undefined],
1657
+ ["node -e \"require('./x.js')\"", undefined],
1658
+ ["node -e \"fetch('https://example.com')\"", undefined],
1659
+ // A file or a module to run is not an inline script.
1660
+ ['python3 tools/run.py', undefined],
1661
+ ['python3 -m http.server', undefined],
1662
+ ['node scripts/build.mjs', undefined],
1663
+ ['node -r ./hook.js -e "1"', undefined],
1664
+ ]) {
1665
+ assert.equal(inferBashActivity(command), activity, command);
1666
+ }
1667
+ });
1668
+
1669
+ test('a shell or ssh handed a command line is named by that command line', () => {
1670
+ for (const [command, activity] of [
1671
+ ["bash -c 'git status'", 'exploring'],
1672
+ ["sh -c 'git push'", 'publishing'],
1673
+ ["bash -c 'rm -rf dist'", 'destructive'],
1674
+ ["ssh sv-home 'cat ~/.config/herdr/config.toml'", 'exploring'],
1675
+ ["ssh -o BatchMode=yes -o ConnectTimeout=8 sv-home 'hostname; ls -la ~/x'", 'exploring'],
1676
+ ["ssh sv-home 'rm -rf /tmp/x'", 'destructive'],
1677
+ ["bash -c 'echo hi'", undefined],
1678
+ ["bash -c 'unknown-tool'", undefined],
1679
+ ['bash scripts/deploy.sh', undefined],
1680
+ ['ssh sv-home', undefined],
1681
+ ["ssh -L 8080:localhost:80 sv-home 'ls'", undefined],
1682
+ ]) {
1683
+ assert.equal(inferBashActivity(command), activity, command);
1684
+ }
1685
+ });
1686
+
1687
+ // Single quotes expand nothing, so a backtick inside them hides no command.
1688
+ test('a backtick inside single quotes is text', () => {
1689
+ assert.equal(inferBashActivity("git commit -m 'fix `Card tags` row'"), 'versioning');
1690
+ assert.equal(inferBashActivity('git commit -m "fix `date`"'), undefined);
1691
+ });
1692
+
1693
+ // An unknown program's usage page, and an in-place edit, are named by what they do.
1694
+ test('plain usage requests and in-place edits are named', () => {
1695
+ for (const command of ['unknown-tool --help', 'dms ipc --help', 'voxtype transcribe --help']) {
1696
+ assert.equal(inferBashActivity(command), 'exploring', command);
1697
+ }
1698
+ for (const command of ['./scripts/deploy.sh --help', 'bash deploy.sh --help', 'tool --help extra', 'tool -x --help']) {
1699
+ assert.notEqual(inferBashActivity(command), 'exploring', command);
1700
+ }
1701
+ for (const command of ["sed -i 's/a/b/' file.txt", "sed -i '49,60d' AGENTS.md", "perl -pi -e 's/a/b/' file.txt", "sed -n -i.bak 1p f"]) {
1702
+ assert.equal(inferBashActivity(command), 'preparing', command);
1703
+ }
1704
+ assert.equal(inferBashActivity("sed -n '1,5p' file.txt"), 'exploring');
1705
+ });
1706
+
1707
+ // ---- regressions an independent verification found ----
1708
+
1709
+ // A script that only PRINTS a deleting word does not delete; a script that mutates through a
1710
+ // spelling a pattern list could miss is never filed as a read.
1711
+ test('inline script text: strings are not calls, and a mutation is never a read', () => {
1712
+ assert.equal(inferBashActivity(`python3 -c 'print("os.remove")'`), 'exploring');
1713
+ assert.equal(inferBashActivity(`node -e 'console.log("rmSync")'`), 'exploring');
1714
+ assert.equal(inferBashActivity(`codegraph status && python3 -c 'print("os.remove")'`), 'codegraph');
1715
+ for (const command of [
1716
+ `python3 -c 'from os import unlink; unlink("a")'`,
1717
+ `python3 -c 'import os as o; o.remove("a")'`,
1718
+ `python3 -c 'from pathlib import Path; open(Path("a"), "w").close()'`,
1719
+ `python3 -c 'open("a", mode="w").close()'`,
1720
+ `node -e 'const {rm}=require("node:fs/promises"); rm("a")'`,
1721
+ `node -e 'const stdout=new (require("fs").WriteStream)("out"); stdout.write("x")'`,
1722
+ `node -e 'require("fs").openSync("a","w")'`,
1723
+ ]) {
1724
+ assert.notEqual(inferBashActivity(command), 'exploring', command);
1725
+ }
1726
+ assert.equal(inferBashActivity(`python3 -c 'from pathlib import Path; open(Path("a")).read()'`), 'exploring');
1727
+ assert.equal(inferBashActivity(`node -e 'const {readFileSync}=require("fs"); console.log(readFileSync("a","utf8"))'`), 'exploring');
1728
+ });
1729
+
1730
+ test('curl: what it sends, what it keeps in a file, and stdout', () => {
1731
+ for (const args of ['-dfoo=bar', '-T./payload', '-sXMERGE', '--config request.txt', '-X POST', '--request=PUT']) {
1732
+ assert.equal(inferBashActivity(`curl ${args} https://example.test`), undefined, args);
1733
+ }
1734
+ assert.equal(inferBashActivity('curl -oout.txt https://example.test'), 'preparing');
1735
+ assert.equal(inferBashActivity('curl -o - https://example.test'), 'exploring');
1736
+ assert.equal(inferBashActivity('curl -sSLf --max-time 5 https://example.test'), 'exploring');
1737
+ assert.equal(inferBashActivity('curl -X GET https://example.test'), 'exploring');
1738
+ });
1739
+
1740
+ test('wrappers: positional arguments, redirects and a bounded depth', () => {
1741
+ assert.equal(inferBashActivity(`bash -c 'rm -f "$1"' _ out`), 'destructive');
1742
+ assert.equal(inferBashActivity(`bash -c 'git push' > out`), undefined);
1743
+ assert.equal(inferBashActivity(`bash -c 'git status' > out`), 'preparing');
1744
+ const started = Date.now();
1745
+ assert.doesNotThrow(() => inferBashActivity('ssh host '.repeat(4000) + 'true'));
1746
+ assert.ok(Date.now() - started < 2000, 'a pile of ssh wrappers is read in bounded time');
1747
+ });
1748
+
1749
+ test('tee and sed -i need a real file and a real flag', () => {
1750
+ assert.equal(inferBashActivity('tee --append'), undefined);
1751
+ assert.equal(inferBashActivity('tee -a log.txt'), 'preparing');
1752
+ assert.notEqual(inferBashActivity('sed -n "1p" "report -i backup.txt"'), 'preparing');
1753
+ });
1754
+
1755
+ // What follows a heredoc's header on its own line is still the shell's text: a pipe or an
1756
+ // `&&` there starts a command of its own, and the body stays with the command that opened it.
1757
+ test('a command after a heredoc header on the same line is read', () => {
1758
+ assert.deepEqual(shellCommandSegments("cat <<'EOF' | head x\nbody\nEOF"), ["cat <<'EOF' \nbody\nEOF", ' head x']);
1759
+ assert.equal(inferBashActivity("cat <<'EOF' | rm -f a\nbody\nEOF"), 'destructive');
1760
+ assert.equal(inferBashActivity("cat <<'EOF' | head -3\nbody\nEOF"), 'exploring');
1761
+ // An escaped substitution in an expanding body is text.
1762
+ assert.equal(inferBashActivity('git commit -F - <<MSG\nfix \\$(date)\nMSG'), 'versioning');
1763
+ assert.equal(inferBashActivity('git commit -F - <<MSG\nfix $(date)\nMSG'), undefined);
1764
+ });
1765
+
1766
+ // ---- second verification: a word is a call only when it is called ----
1767
+ test('inline scripts, curl, comments and stacked heredocs name only what they perform', () => {
1768
+ for (const [command, expected] of [
1769
+ [`python3 -c 'unlink = 4; print(unlink)'`, 'exploring'],
1770
+ [`node -e 'const rmSync=3; console.log(rmSync)'`, 'exploring'],
1771
+ [`node -e 'console.log(/rmSync/.test("xyz"))'`, 'exploring'],
1772
+ [`python3 -c 'print("open(x, mode=w)")'`, 'exploring'],
1773
+ [`python3 -c 'from pathlib import Path; Path("out").open(mode="w").close()'`, 'preparing'],
1774
+ [`python3 -c 'from pathlib import Path; Path("out").open("w").close()'`, 'preparing'],
1775
+ [`node -e 'console.log(1)' -r ./helper.cjs`, undefined],
1776
+ ['curl --etag-save etag.txt file:///tmp/input.txt', 'preparing'],
1777
+ ['curl --output-dir here file:///tmp/input.txt', 'exploring'],
1778
+ ["cat <<'ONE' <<'TWO'\nfirst\nONE\nrm -f a\nTWO", 'exploring'],
1779
+ ['echo hello # && rm -rf x', undefined],
1780
+ ['git status # && rm -rf x', 'exploring'],
1781
+ ['git commit -m "a # b"', 'versioning'],
1782
+ ]) {
1783
+ assert.equal(inferBashActivity(command), expected, command);
1784
+ }
1785
+ });