@alexwkleung/reika 0.7.0 → 0.8.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.
- package/README.md +15 -0
- package/README.zh-CN.md +15 -0
- package/dist/agent/loop.js +12 -4
- package/dist/agent/prefillcost.js +16 -0
- package/dist/agent/prompt.js +5 -2
- package/dist/cli.js +2 -2
- package/dist/context/bootstrap.js +1 -1
- package/dist/debug.js +15 -0
- package/dist/{headless.js → headless/headless.js} +182 -48
- package/dist/{headlessargs.js → headless/headlessargs.js} +12 -2
- package/dist/headless/headlessprompt.js +132 -0
- package/dist/mcp/client.js +97 -5
- package/dist/mcp/manager.js +15 -2
- package/dist/provider/client.js +13 -0
- package/dist/session.js +8 -0
- package/dist/{skills.js → skills/skills.js} +93 -13
- package/dist/store/sessions.js +39 -2
- package/dist/store/transcript.js +35 -0
- package/dist/tools/_carriers.js +119 -0
- package/dist/tools/_danger.js +304 -82
- package/dist/tools/_readonly.js +13 -12
- package/dist/ui/App.js +150 -69
- package/dist/ui/Approval.js +80 -33
- package/dist/ui/Confirm.js +1 -27
- package/dist/ui/DiffView.js +23 -12
- package/dist/ui/Question.js +152 -19
- package/dist/ui/ResumeSelect.js +3 -2
- package/dist/ui/Status.js +19 -7
- package/dist/ui/commands.js +1 -1
- package/dist/ui/dialogspecs.js +38 -0
- package/dist/ui/layout.js +118 -0
- package/package.json +1 -1
- /package/dist/{skillmatch.js → skills/skillmatch.js} +0 -0
package/dist/tools/_danger.js
CHANGED
|
@@ -10,6 +10,7 @@ import { existsSync } from 'node:fs';
|
|
|
10
10
|
import { homedir } from 'node:os';
|
|
11
11
|
import { dirname, join, resolve } from 'node:path';
|
|
12
12
|
import { WORD_RE, dequote, maskQuoted, maskShellData, splitSegments, words } from './_readonly.js';
|
|
13
|
+
import { CARRIERS, carrierOptionValue } from './_carriers.js';
|
|
13
14
|
import { scanHeredocs, stripHeredocs } from './_heredoc.js';
|
|
14
15
|
import { maskMarkdownData } from './_markdown.js';
|
|
15
16
|
// Workflow-policy commands: not destructive (a commit is local and reversible, a push is
|
|
@@ -730,22 +731,19 @@ const VERB_PATTERNS = [
|
|
|
730
731
|
// reading, and the value is exactly what that `bash -c` runs. So `verbLabels` reads a segment both
|
|
731
732
|
// ways and reports the union — quoting can add a label, and cannot cost one. Both readings strip the
|
|
732
733
|
// same words, from one list, or the union would disagree about a wrapper only one of them knows.
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
'until',
|
|
747
|
-
'!',
|
|
748
|
-
];
|
|
734
|
+
//
|
|
735
|
+
// The words are `_carriers.ts`'s table plus the shell grammar a segment can open with (`if curl …
|
|
736
|
+
// ; then`). The table is shared with `_readonly.ts`'s `dropCarriers` (#756) because a carrier only
|
|
737
|
+
// one of the two knew is a fetch that runs unprompted on this side and a refused read on that one:
|
|
738
|
+
// `timeout 5 curl x` and `nice curl x` each reported NOTHING here, so under `safe` the fetch ran.
|
|
739
|
+
//
|
|
740
|
+
// The whitespace reading is the blind half of that pair, and it can only see bare words: it has no
|
|
741
|
+
// way to tell which of a carrier's tokens is an option's value, so it strips the carrier's NAME and
|
|
742
|
+
// leaves its operands standing (`timeout 5 curl` reads as `5 curl`). Nothing is lost by that — a
|
|
743
|
+
// leftover operand is a word no `^`-anchored pattern matches, and the union takes the label from
|
|
744
|
+
// the quoted reading, which is the one that reaches the verb.
|
|
745
|
+
const SHELL_PREFIX_KEYWORDS = ['if', 'then', 'else', 'elif', 'do', 'while', 'until', '!'];
|
|
746
|
+
const VERB_PREFIX_WORDS = [...Object.keys(CARRIERS), ...SHELL_PREFIX_KEYWORDS];
|
|
749
747
|
const VERB_PREFIX_WORD_SET = new Set(VERB_PREFIX_WORDS);
|
|
750
748
|
const VERB_PREFIX_WS_RE = new RegExp(String.raw `^(?:(?:[A-Za-z_]\w*=\S*|${VERB_PREFIX_WORDS.join('|')})\s+)+`);
|
|
751
749
|
const SHELL_WORD_RE = /(?:"(?:[^"\\]|\\[\s\S])*"|\$'(?:[^'\\]|\\[\s\S])*'|'[^']*'|\\[\s\S]|[^\s"'\\])+/y;
|
|
@@ -754,61 +752,20 @@ const WORD_GAP_RE = /\s+/y;
|
|
|
754
752
|
const ENV_ASSIGN_WORD_RE = /^[A-Za-z_]\w*=/;
|
|
755
753
|
// A wrapper's own options sit between it and the command it runs (`sudo -u me grep …`, `env -i …`,
|
|
756
754
|
// `time -p …`); left in place, the option became the lead and the command behind it read as an
|
|
757
|
-
// argument (#757 review).
|
|
758
|
-
//
|
|
759
|
-
// `
|
|
755
|
+
// argument (#757 review). Which options those are — and how many operands the carrier itself takes
|
|
756
|
+
// before the command (`timeout`'s DURATION) — is `_carriers.ts`'s table, read here and in
|
|
757
|
+
// `_readonly.ts`'s `dropCarriers` (#756). Its lists are closed on purpose: an option nobody
|
|
760
758
|
// enumerated costs a label, which is the direction this file errs in.
|
|
761
|
-
const
|
|
762
|
-
sudo: new Set(['-u', '-g', '-U', '-C', '-D', '-h', '-p', '-r', '-t', '-T', '-R']),
|
|
763
|
-
env: new Set(['-u', '-C', '-S', '-P']),
|
|
764
|
-
exec: new Set(['-a']),
|
|
765
|
-
time: new Set(['-o', '-f']),
|
|
766
|
-
};
|
|
767
|
-
// The same options spelled out: `sudo --user me curl …` and `env --chdir /x make install` hid the
|
|
768
|
-
// command behind them exactly as the short spellings did (#794).
|
|
769
|
-
const WRAPPER_LONG_VALUE_OPTIONS = {
|
|
770
|
-
sudo: new Set([
|
|
771
|
-
'--user',
|
|
772
|
-
'--group',
|
|
773
|
-
'--other-user',
|
|
774
|
-
'--close-from',
|
|
775
|
-
'--chdir',
|
|
776
|
-
'--host',
|
|
777
|
-
'--prompt',
|
|
778
|
-
'--role',
|
|
779
|
-
'--type',
|
|
780
|
-
'--command-timeout',
|
|
781
|
-
]),
|
|
782
|
-
env: new Set(['--unset', '--chdir', '--split-string', '--path']),
|
|
783
|
-
exec: new Set(['--argv0']),
|
|
784
|
-
time: new Set(['--output', '--format']),
|
|
785
|
-
};
|
|
786
|
-
const WRAPPER_WORDS = new Set(['sudo', 'command', 'nohup', 'exec', 'env', 'time']);
|
|
759
|
+
const CARRIER_WORDS = new Set(Object.keys(CARRIERS));
|
|
787
760
|
// `command -v x` looks `x` up and runs nothing, so it has no verb at all.
|
|
788
761
|
const COMMAND_LOOKUP_RE = /^-[a-zA-Z]*[vV]/;
|
|
789
|
-
// Whether a wrapper option consumes the NEXT word as its value. `word` has already been read as an
|
|
790
|
-
// option. A long spelling carrying `=` (`--user=me`) keeps its value attached and eats nothing.
|
|
791
|
-
function wrapperOptionValue(wrapper, word) {
|
|
792
|
-
if (WRAPPER_VALUE_OPTIONS[wrapper]?.has(word))
|
|
793
|
-
return true;
|
|
794
|
-
if (WRAPPER_LONG_VALUE_OPTIONS[wrapper]?.has(word))
|
|
795
|
-
return true;
|
|
796
|
-
// A BUNDLED short spelling (`sudo -iu me`, `env -iu PATH …`) follows getopt: the letters are read
|
|
797
|
-
// left to right, and the FIRST one that takes a value takes the rest of the bundle as that value
|
|
798
|
-
// (`-ume` is `-u me`, `-of x` is `-o f`) unless it is the bundle's LAST letter, in which case the
|
|
799
|
-
// value is the next word (`-iu me` is `-i -u me`). Anything else is a flag that eats nothing.
|
|
800
|
-
if (!/^-[a-zA-Z]+$/.test(word))
|
|
801
|
-
return false;
|
|
802
|
-
for (let i = 1; i < word.length; i++) {
|
|
803
|
-
if (WRAPPER_VALUE_OPTIONS[wrapper]?.has(`-${word[i]}`))
|
|
804
|
-
return i === word.length - 1;
|
|
805
|
-
}
|
|
806
|
-
return false;
|
|
807
|
-
}
|
|
808
762
|
function stripVerbPrefix(segment) {
|
|
809
763
|
let at = 0;
|
|
810
|
-
let
|
|
764
|
+
let carrier;
|
|
811
765
|
let takesValue = false;
|
|
766
|
+
// Operands the carrier still takes before the command it runs: one for `timeout`, whose DURATION
|
|
767
|
+
// is not an option and used to land in verb position (`timeout 5 curl x` read as a `5`).
|
|
768
|
+
let operands = 0;
|
|
812
769
|
for (;;) {
|
|
813
770
|
SHELL_WORD_RE.lastIndex = at;
|
|
814
771
|
let end = SHELL_WORD_RE.exec(segment) ? SHELL_WORD_RE.lastIndex : at;
|
|
@@ -818,29 +775,38 @@ function stripVerbPrefix(segment) {
|
|
|
818
775
|
end = NON_SPACE_RUN_RE.lastIndex;
|
|
819
776
|
}
|
|
820
777
|
const word = segment.slice(at, end);
|
|
821
|
-
if (
|
|
778
|
+
if (carrier === 'command' && COMMAND_LOOKUP_RE.test(word))
|
|
822
779
|
return '';
|
|
823
|
-
const isOption =
|
|
780
|
+
const isOption = carrier !== undefined && (takesValue || word.startsWith('-'));
|
|
824
781
|
WORD_GAP_RE.lastIndex = end;
|
|
825
782
|
const gap = WORD_GAP_RE.exec(segment);
|
|
826
|
-
if (!gap ||
|
|
783
|
+
if (!gap ||
|
|
784
|
+
!(isOption || operands > 0 || VERB_PREFIX_WORD_SET.has(word) || ENV_ASSIGN_WORD_RE.test(word))) {
|
|
827
785
|
return segment.slice(at);
|
|
828
786
|
}
|
|
829
787
|
if (takesValue) {
|
|
830
788
|
takesValue = false;
|
|
831
789
|
}
|
|
832
790
|
else if (isOption) {
|
|
833
|
-
// `--` ends the
|
|
791
|
+
// `--` ends the carrier's options: the next word is the command, even one spelled `-x`.
|
|
834
792
|
if (word === '--')
|
|
835
|
-
|
|
793
|
+
carrier = undefined;
|
|
836
794
|
else
|
|
837
|
-
takesValue =
|
|
795
|
+
takesValue = carrierOptionValue(carrier, word);
|
|
796
|
+
}
|
|
797
|
+
else if (ENV_ASSIGN_WORD_RE.test(word)) {
|
|
798
|
+
// An assignment is `env`'s own operand (`env FOO=1 -S …`) and leaves the carrier in place.
|
|
799
|
+
}
|
|
800
|
+
else if (operands > 0) {
|
|
801
|
+
operands--;
|
|
838
802
|
}
|
|
839
|
-
else if (
|
|
840
|
-
|
|
803
|
+
else if (CARRIER_WORDS.has(word)) {
|
|
804
|
+
carrier = word;
|
|
805
|
+
operands = CARRIERS[word].operands;
|
|
841
806
|
}
|
|
842
|
-
else
|
|
843
|
-
|
|
807
|
+
else {
|
|
808
|
+
// A shell keyword: it runs the word after it, and is no carrier itself.
|
|
809
|
+
carrier = undefined;
|
|
844
810
|
}
|
|
845
811
|
at = WORD_GAP_RE.lastIndex;
|
|
846
812
|
}
|
|
@@ -1447,6 +1413,232 @@ function nestedBodies(command) {
|
|
|
1447
1413
|
}
|
|
1448
1414
|
return found;
|
|
1449
1415
|
}
|
|
1416
|
+
// An interpreter reading its PROGRAM from stdin rather than from an argument: `python3 -`, `node -`,
|
|
1417
|
+
// `ruby -`, `perl -`. The body arrives by heredoc, so the shell pastes it — nothing quote-parses a
|
|
1418
|
+
// body and the verb readers are blind to it (#699) — but the pasted text is code, not data, and a
|
|
1419
|
+
// script that shells out runs those commands for real. The sandbox does not cover that: signals are
|
|
1420
|
+
// not confined (`pkill`, `killall`), docker's socket is allowed, and a loopback cluster (kind,
|
|
1421
|
+
// minikube) is reachable by `kubectl` (#750).
|
|
1422
|
+
//
|
|
1423
|
+
// It is deliberately NOT read as a nested body the way a `-c` argument is. Handing the whole body to
|
|
1424
|
+
// `detectAtDepth` would re-open #699: a `;` in a string literal in a pasted body splits a segment
|
|
1425
|
+
// there as soon as nothing marks the body, which is what `verbData` suppresses for the outer pass.
|
|
1426
|
+
// So what is read here is narrow, and only in the direction that cannot hide anything:
|
|
1427
|
+
//
|
|
1428
|
+
// - `INTERPRETER_BODY_PATTERNS` over the whole body (they are specific enough to run on text);
|
|
1429
|
+
// - the verb readers over the STRING ARGUMENTS of a shell-out call site, and nothing else.
|
|
1430
|
+
//
|
|
1431
|
+
// Call sites only, not every string literal — `print("x; pkill y")` is data, and reading it is
|
|
1432
|
+
// exactly #699's false positive. The cost of the narrow reading is the other direction: a command
|
|
1433
|
+
// assembled at runtime (`os.system("pk" + "ill node")`) is still not seen.
|
|
1434
|
+
// A flag arm may carry `=value` (`node --input-type=module -`), which is otherwise a word past it.
|
|
1435
|
+
// The `-` is optional: with no script operand at all each of the four reads its program from stdin
|
|
1436
|
+
// too, and `python3 <<'PY'` is the spelling models write most. Bare, the flags must run to the end of
|
|
1437
|
+
// the command (a redirect, a separator, the line's end), or the next word is the script.
|
|
1438
|
+
const INTERPRETER_STDIN_RE = /\b(python[\d.]*|ruby|perl|node)((?:\s+-[\w-]+(?:=[^\s]+)?)*)(?:\s+(-)(?![\w.=-])|(?=[ \t]*(?:[<>|;&)\n]|\d+>|$)))/g;
|
|
1439
|
+
// A last flag that takes the program (`-c`, `-e`) or a module (`-m`) as its value: the program is
|
|
1440
|
+
// not on stdin, and in the masked view the quoted value is blank, so it reads as a bare interpreter.
|
|
1441
|
+
const PROGRAM_FLAG_RE = /\s(?:-[cemEp]|--eval|--print)$/;
|
|
1442
|
+
// `stdinBodies`' pre-test: the two walks it needs (a quote mask, a heredoc scan) cost more than the
|
|
1443
|
+
// rest of the command's detection put together. A name alone is not rare enough — `node` is in every
|
|
1444
|
+
// `node_modules` path — but every body this reader finds is a heredoc, so a command with no `<<` has
|
|
1445
|
+
// none. Not the /g/ pattern above, whose `lastIndex` `matchAll` inherits.
|
|
1446
|
+
const INTERPRETER_NAME_RE = /\b(?:python|ruby|perl|node)/;
|
|
1447
|
+
// Where an interpreter hands a string to a shell. `os.popen` is `os.system` with a pipe, and
|
|
1448
|
+
// `system` alone is Ruby's and Perl's spelling of it; `subprocess.*` and `child_process.*` are the
|
|
1449
|
+
// whole module, since every entry point takes the same argv. A `node -` body usually reaches the
|
|
1450
|
+
// module through `require`, and a named import strips the qualifier entirely, so the spellings that
|
|
1451
|
+
// reach the same call are here too — the sync ones are Node's own names and appear nowhere else,
|
|
1452
|
+
// while bare `exec`/`spawn` also name a database client, and those keep their qualifier.
|
|
1453
|
+
const SHELL_OUT_CALL_RE = /\b(?:os\s*\.\s*(?:system|popen)|subprocess\s*\.\s*[A-Za-z_]\w*|child_process\s*\.\s*\w+|childProcess\s*\.\s*\w+|require\s*\(\s*['"]child_process['"]\s*\)\s*\.\s*\w+|execSync|execFileSync|spawnSync|system)\s*\(\s*/g;
|
|
1454
|
+
// The shell command a call hands over: a plain literal (`os.system("…")`), an `f`/`r`/`b`-prefixed
|
|
1455
|
+
// or triple-quoted one, or an argv — a list (`subprocess.run(["git", "push", "--force"])`), a
|
|
1456
|
+
// program followed by its list (`execFileSync("git", ["push"])`, `spawn`), or a program followed by
|
|
1457
|
+
// its words (Ruby's and Perl's `system("git", "push")`). An argv is joined back into the command it
|
|
1458
|
+
// spells, each element quoted, so `["sh", "-c", "pkill node"]` keeps its `-c` body one word. A
|
|
1459
|
+
// variable, a `.format` template or a concatenation is not text this file can read, and guessing at
|
|
1460
|
+
// one would put words in the command that the shell never sees; an argv stops at its first one.
|
|
1461
|
+
function callArgument(text, from) {
|
|
1462
|
+
let at = skipSpace(text, from);
|
|
1463
|
+
const parts = [];
|
|
1464
|
+
let argv = false;
|
|
1465
|
+
const readList = () => {
|
|
1466
|
+
argv = true;
|
|
1467
|
+
at++;
|
|
1468
|
+
for (;;) {
|
|
1469
|
+
const lit = readLiteral(text, skipSpace(text, at));
|
|
1470
|
+
if (!lit)
|
|
1471
|
+
return false;
|
|
1472
|
+
parts.push(lit.value);
|
|
1473
|
+
at = skipSpace(text, lit.end);
|
|
1474
|
+
if (text[at] !== ',')
|
|
1475
|
+
return text[at] === ']' || text[at] === ')';
|
|
1476
|
+
at++;
|
|
1477
|
+
}
|
|
1478
|
+
};
|
|
1479
|
+
if (text[at] === '[' || text[at] === '(') {
|
|
1480
|
+
readList();
|
|
1481
|
+
}
|
|
1482
|
+
else {
|
|
1483
|
+
const lit = readLiteral(text, at);
|
|
1484
|
+
if (!lit)
|
|
1485
|
+
return undefined;
|
|
1486
|
+
parts.push(lit.value);
|
|
1487
|
+
at = skipSpace(text, lit.end);
|
|
1488
|
+
// Further arguments are the program's words only while they are literals or a closed list.
|
|
1489
|
+
while (text[at] === ',') {
|
|
1490
|
+
at = skipSpace(text, at + 1);
|
|
1491
|
+
if (text[at] === '[') {
|
|
1492
|
+
readList();
|
|
1493
|
+
break;
|
|
1494
|
+
}
|
|
1495
|
+
const next = readLiteral(text, at);
|
|
1496
|
+
if (!next)
|
|
1497
|
+
break;
|
|
1498
|
+
argv = true;
|
|
1499
|
+
parts.push(next.value);
|
|
1500
|
+
at = skipSpace(text, next.end);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
if (!parts.length)
|
|
1504
|
+
return undefined;
|
|
1505
|
+
if (!argv)
|
|
1506
|
+
return parts[0] || undefined;
|
|
1507
|
+
// A shell's `-c` element IS the command, and a call argument is read one level deep, where the
|
|
1508
|
+
// `-c` reader does not run — so it is handed over unwrapped.
|
|
1509
|
+
if (parts.length >= 3 && SHELL_ARGV0_RE.test(parts[0]) && /^-\w*c$/.test(parts[1]))
|
|
1510
|
+
return parts[2];
|
|
1511
|
+
return parts.map(shellQuote).join(' ');
|
|
1512
|
+
}
|
|
1513
|
+
const SHELL_ARGV0_RE = /(?:^|\/)(?:ba|z|da|k)?sh$/;
|
|
1514
|
+
function skipSpace(text, at) {
|
|
1515
|
+
while (at < text.length && /\s/.test(text[at]))
|
|
1516
|
+
at++;
|
|
1517
|
+
return at;
|
|
1518
|
+
}
|
|
1519
|
+
// One string literal at `at`, with the index past its closing quote. A triple quote opens a
|
|
1520
|
+
// Python block string, which a single `"` would read as an empty literal followed by text.
|
|
1521
|
+
function readLiteral(text, at) {
|
|
1522
|
+
const m = /^[A-Za-z]{0,2}("""|'''|['"`])/.exec(text.slice(at));
|
|
1523
|
+
if (!m)
|
|
1524
|
+
return undefined;
|
|
1525
|
+
const quote = m[1];
|
|
1526
|
+
const start = at + m[0].length;
|
|
1527
|
+
let i = start;
|
|
1528
|
+
for (; i < text.length; i++) {
|
|
1529
|
+
if (text[i] === '\\')
|
|
1530
|
+
i++;
|
|
1531
|
+
else if (text.startsWith(quote, i))
|
|
1532
|
+
break;
|
|
1533
|
+
}
|
|
1534
|
+
if (i >= text.length)
|
|
1535
|
+
return undefined;
|
|
1536
|
+
// A raw literal (`r"…"`, `rb"…"`) keeps its backslashes: `\n` there is two characters, not a
|
|
1537
|
+
// line break, and reading it as one would label a second command the shell never runs.
|
|
1538
|
+
const raw = /r/i.test(m[0].slice(0, -quote.length));
|
|
1539
|
+
const body = text.slice(start, i);
|
|
1540
|
+
return { value: raw ? body : unescapeLiteral(body), end: i + quote.length };
|
|
1541
|
+
}
|
|
1542
|
+
// An argv element as the one shell word it was: a bare word stays bare, anything else is quoted.
|
|
1543
|
+
function shellQuote(word) {
|
|
1544
|
+
return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replace(/'/g, `'\\''`)}'`;
|
|
1545
|
+
}
|
|
1546
|
+
// The escapes that change the command's SHAPE, so an argument spelled on one source line reads as
|
|
1547
|
+
// the two commands the shell will run: `os.system("curl x\npkill y")` is a `pkill` line. Everything
|
|
1548
|
+
// else is left alone — `\d` in a regex in the same string is not this file's business.
|
|
1549
|
+
function unescapeLiteral(s) {
|
|
1550
|
+
return s.replace(/\\(?:([nrt])|(['"\\`]))/g, (_m, esc, ch) => esc === 'n' ? '\n' : esc === 'r' ? '\r' : esc === 't' ? '\t' : ch);
|
|
1551
|
+
}
|
|
1552
|
+
// The commands a body hands to a shell, capped like the nested bodies themselves: a body with more
|
|
1553
|
+
// call sites than that is a script, and past a handful the labels say the same thing.
|
|
1554
|
+
function shellOutStrings(body) {
|
|
1555
|
+
const out = [];
|
|
1556
|
+
for (const m of body.matchAll(SHELL_OUT_CALL_RE)) {
|
|
1557
|
+
if (out.length >= MAX_NESTED_BODIES)
|
|
1558
|
+
break;
|
|
1559
|
+
const arg = callArgument(body, m.index + m[0].length);
|
|
1560
|
+
if (arg)
|
|
1561
|
+
out.push(arg);
|
|
1562
|
+
}
|
|
1563
|
+
return out;
|
|
1564
|
+
}
|
|
1565
|
+
// Whether the newline at `nl` is escaped by an odd run of backslashes before it.
|
|
1566
|
+
function continues(command, nl) {
|
|
1567
|
+
if (nl <= 0 || command[nl] !== '\n')
|
|
1568
|
+
return false;
|
|
1569
|
+
let n = 0;
|
|
1570
|
+
while (nl - 1 - n >= 0 && command[nl - 1 - n] === '\\')
|
|
1571
|
+
n++;
|
|
1572
|
+
return n % 2 === 1;
|
|
1573
|
+
}
|
|
1574
|
+
// A `&` that ends a command, not one inside a redirect (`2>&1`, `&>f`, `<&3`).
|
|
1575
|
+
function isCommandAmpersand(view, i) {
|
|
1576
|
+
return view[i] === '&' && view[i - 1] !== '>' && view[i - 1] !== '<' && view[i + 1] !== '>';
|
|
1577
|
+
}
|
|
1578
|
+
// Where the pipeline holding `at` begins on its line: past the last `;`, `&`, `||` or `(` before it.
|
|
1579
|
+
function pipelineStart(view, lineStart, at) {
|
|
1580
|
+
for (let i = at - 1; i >= lineStart; i--) {
|
|
1581
|
+
const c = view[i];
|
|
1582
|
+
if (c === ';' || c === '(' || isCommandAmpersand(view, i))
|
|
1583
|
+
return i + 1;
|
|
1584
|
+
if (c === '|' && view[i - 1] === '|')
|
|
1585
|
+
return i + 1;
|
|
1586
|
+
}
|
|
1587
|
+
return lineStart;
|
|
1588
|
+
}
|
|
1589
|
+
// Where the command starting before `from` ends on its line: the first `;`, `&`, `|` or `)`.
|
|
1590
|
+
function commandEnd(view, from, lineEnd) {
|
|
1591
|
+
for (let i = from; i < lineEnd; i++) {
|
|
1592
|
+
const c = view[i];
|
|
1593
|
+
if (c === ';' || c === '|' || c === ')' || isCommandAmpersand(view, i))
|
|
1594
|
+
return i;
|
|
1595
|
+
}
|
|
1596
|
+
return lineEnd;
|
|
1597
|
+
}
|
|
1598
|
+
// The stdin bodies of the interpreters above. Failures here go the same way as everywhere else in the
|
|
1599
|
+
// nested-body readers: a body that cannot be found costs nothing extra, and one that cannot be
|
|
1600
|
+
// bounded (an unreadable lexer) over-reports.
|
|
1601
|
+
function stdinBodies(command) {
|
|
1602
|
+
if (!command.includes('<<') || !INTERPRETER_NAME_RE.test(command))
|
|
1603
|
+
return [];
|
|
1604
|
+
const found = [];
|
|
1605
|
+
// A `python3 -` inside a quoted argument runs no interpreter: it is another command's text.
|
|
1606
|
+
const view = maskQuoted(command);
|
|
1607
|
+
const scan = scanHeredocs(command);
|
|
1608
|
+
for (const m of view.matchAll(INTERPRETER_STDIN_RE)) {
|
|
1609
|
+
if (found.length >= MAX_NESTED_BODIES)
|
|
1610
|
+
break;
|
|
1611
|
+
if (!m[3] && PROGRAM_FLAG_RE.test(m[2]))
|
|
1612
|
+
continue;
|
|
1613
|
+
const at = m.index;
|
|
1614
|
+
const context = m[3] ? `${m[1]} -` : m[1];
|
|
1615
|
+
const lineStart = command.lastIndexOf('\n', at - 1) + 1;
|
|
1616
|
+
const nl = command.indexOf('\n', at);
|
|
1617
|
+
// A line continued by a backslash puts the body after the NEXT newline (or the pipeline before
|
|
1618
|
+
// the previous one), where the walk below cannot follow it. Like an unreadable lexer, that
|
|
1619
|
+
// reads the whole command as the body: it can only add labels.
|
|
1620
|
+
if (scan.uncertain || continues(command, nl) || continues(command, lineStart - 1)) {
|
|
1621
|
+
if (command.includes('<<'))
|
|
1622
|
+
found.push({ context, body: command });
|
|
1623
|
+
continue;
|
|
1624
|
+
}
|
|
1625
|
+
const lineEnd = nl === -1 ? command.length : nl;
|
|
1626
|
+
// The body lands on the stdin of the command its operator sits on, and the interpreter reads it
|
|
1627
|
+
// when that command is its own or one the pipe hands it on from (`cat <<'PY' | python3 -`). So
|
|
1628
|
+
// every heredoc from the start of the interpreter's pipeline to the end of its own command
|
|
1629
|
+
// counts, and none behind a `;` or `&&`, which is another command's input.
|
|
1630
|
+
const from = pipelineStart(view, lineStart, at);
|
|
1631
|
+
const to = commandEnd(view, at + m[0].length, lineEnd);
|
|
1632
|
+
for (const h of scan.heredocs) {
|
|
1633
|
+
if (h.opAt < from || h.opAt >= to)
|
|
1634
|
+
continue;
|
|
1635
|
+
if (found.length >= MAX_NESTED_BODIES)
|
|
1636
|
+
break;
|
|
1637
|
+
found.push({ context, body: command.slice(h.bodyStart, h.bodyEnd) });
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
return found;
|
|
1641
|
+
}
|
|
1450
1642
|
// Two checks read the working tree, because the command text alone cannot answer them: whether
|
|
1451
1643
|
// `npx vitest` fetches anything, and whether `git checkout App.tsx` names a branch or a file.
|
|
1452
1644
|
// Without a cwd both fall back to the text — npx flags, checkout stays with the regexes above.
|
|
@@ -1609,17 +1801,47 @@ function detectAtDepth(rawCommand, depth, cwd) {
|
|
|
1609
1801
|
for (const { context, body, interp } of nestedBodies(command)) {
|
|
1610
1802
|
const inner = detectAtDepth(body, 1, cwd);
|
|
1611
1803
|
if (interp) {
|
|
1612
|
-
for (const
|
|
1613
|
-
if (
|
|
1804
|
+
for (const label of interpreterBodyLabels(body, cwd)) {
|
|
1805
|
+
if (!inner.includes(label))
|
|
1614
1806
|
inner.push(label);
|
|
1615
1807
|
}
|
|
1616
1808
|
}
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1809
|
+
addNested(hits, context, inner);
|
|
1810
|
+
}
|
|
1811
|
+
// A program pasted onto an interpreter's stdin is the one nested body the outer pass refuses to
|
|
1812
|
+
// read (#699), so it is read here instead — and ONLY through `interpreterBodyLabels`: the body
|
|
1813
|
+
// itself is never handed to `detectAtDepth`, which is what keeps a `;` in one of its string
|
|
1814
|
+
// literals from splitting a segment into a verb.
|
|
1815
|
+
for (const { context, body } of stdinBodies(command)) {
|
|
1816
|
+
addNested(hits, context, interpreterBodyLabels(body, cwd));
|
|
1622
1817
|
}
|
|
1623
1818
|
}
|
|
1624
1819
|
return hits;
|
|
1625
1820
|
}
|
|
1821
|
+
// What an interpreter's own body says on top of what its text says as a command: the stdlib patterns
|
|
1822
|
+
// above, and the commands the body actually hands to a shell. Both are read off the body text alone,
|
|
1823
|
+
// with no dependence on where its quote spans are — which is what makes this the one reading that is
|
|
1824
|
+
// safe on a PASTED body, where the shell has not quote-parsed anything and a `;` in a string literal
|
|
1825
|
+
// is not marked as data (#699).
|
|
1826
|
+
function interpreterBodyLabels(body, cwd) {
|
|
1827
|
+
const labels = [];
|
|
1828
|
+
for (const { re, label } of INTERPRETER_BODY_PATTERNS) {
|
|
1829
|
+
if (re.test(body))
|
|
1830
|
+
labels.push(label);
|
|
1831
|
+
}
|
|
1832
|
+
for (const call of shellOutStrings(body)) {
|
|
1833
|
+
for (const label of detectAtDepth(call, 1, cwd)) {
|
|
1834
|
+
if (!labels.includes(label))
|
|
1835
|
+
labels.push(label);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
return labels;
|
|
1839
|
+
}
|
|
1840
|
+
// The prefixed form of every label a nested body earned.
|
|
1841
|
+
function addNested(hits, context, inner) {
|
|
1842
|
+
for (const label of inner) {
|
|
1843
|
+
const prefixed = `via ${context}: ${label}`;
|
|
1844
|
+
if (!hits.includes(label) && !hits.includes(prefixed))
|
|
1845
|
+
hits.push(prefixed);
|
|
1846
|
+
}
|
|
1847
|
+
}
|
package/dist/tools/_readonly.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { scanQuotes } from './_heredoc.js';
|
|
2
|
+
import { skipCarrier } from './_carriers.js';
|
|
2
3
|
// Shell commands that only READ — asked as TWO questions, because the two callers pull in opposite
|
|
3
4
|
// directions and collapsing them costs whichever one is on the losing side.
|
|
4
5
|
//
|
|
@@ -373,21 +374,21 @@ function readAnsiC(word, start, emit) {
|
|
|
373
374
|
// (#621) — while `_danger.ts`, which matches a command's own patterns through an unlisted carrier
|
|
374
375
|
// (`timeout 5 rm` still gates), saw the gh read and did not flag it. (`xargs` is the other carrier,
|
|
375
376
|
// read in `_sandbox.ts`'s verb reader, where the command it runs is known to be the argument.)
|
|
376
|
-
//
|
|
377
|
-
//
|
|
378
|
-
|
|
377
|
+
//
|
|
378
|
+
// Which carriers are read through, and what their own options and operands are, is
|
|
379
|
+
// `_carriers.ts`'s table (#756): `_danger.ts`'s verb strip walks the same entries, and a carrier
|
|
380
|
+
// only one of the two knew was a fetch left ungated on one side and a refused read on the other.
|
|
381
|
+
// Only the TRANSPARENT entry of the table is skipped here — `nice` and `timeout` bound or
|
|
382
|
+
// deprioritize the command, while `sudo`/`env`/`nohup` change what running it means — and the walk
|
|
383
|
+
// loops, so `timeout 30 nice gh pr view 1` is still the `gh` read.
|
|
379
384
|
export function dropCarriers(wordList) {
|
|
380
385
|
let i = 0;
|
|
381
|
-
|
|
382
|
-
i
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
i++;
|
|
387
|
-
}
|
|
388
|
-
i++; // the DURATION operand
|
|
386
|
+
for (;;) {
|
|
387
|
+
const next = skipCarrier(wordList, i, true);
|
|
388
|
+
if (next < 0)
|
|
389
|
+
return wordList.slice(i);
|
|
390
|
+
i = next;
|
|
389
391
|
}
|
|
390
|
-
return wordList.slice(i);
|
|
391
392
|
}
|
|
392
393
|
// Plan mode's sed: a line-range or pattern-range print (`sed -n '120,180p' f`,
|
|
393
394
|
// `sed -n '/## A/,/## B/p' f`) — the read models reach for most, refused before at a round's cost.
|