@holmes-lab/holmes-kit 0.30.0 → 0.31.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/CHANGELOG.md +494 -0
- package/README.md +10 -1
- package/bin/holmes-hook-antigravity.js +4 -0
- package/bin/holmes-kit.js +4 -0
- package/bin/holmes-pre-push.js +3 -0
- package/bin/holmes-stop-antigravity.js +4 -0
- package/dist/.build-id +1 -1
- package/dist/holmes/cli/agents.d.ts +41 -0
- package/dist/holmes/cli/agents.js +382 -2
- package/dist/holmes/cli/attest.d.ts +2 -0
- package/dist/holmes/cli/attest.js +66 -15
- package/dist/holmes/cli/ci-gate.js +16 -1
- package/dist/holmes/cli/claude-switches.d.ts +95 -0
- package/dist/holmes/cli/claude-switches.js +967 -0
- package/dist/holmes/cli/codex-hooks.d.ts +77 -0
- package/dist/holmes/cli/codex-hooks.js +802 -0
- package/dist/holmes/cli/codex-oracle.d.ts +194 -0
- package/dist/holmes/cli/codex-oracle.js +1245 -0
- package/dist/holmes/cli/codex-toml.d.ts +35 -0
- package/dist/holmes/cli/codex-toml.js +321 -52
- package/dist/holmes/cli/doctor.d.ts +98 -8
- package/dist/holmes/cli/doctor.js +1328 -94
- package/dist/holmes/cli/index.js +23 -11
- package/dist/holmes/cli/init.d.ts +11 -0
- package/dist/holmes/cli/init.js +736 -44
- package/dist/holmes/cli/mcp-launcher.d.ts +64 -6
- package/dist/holmes/cli/mcp-launcher.js +189 -6
- package/dist/holmes/cli/mcp-version.js +13 -2
- package/dist/holmes/cli/settings-merge.js +8 -2
- package/dist/holmes/cli/upgrade.d.ts +13 -0
- package/dist/holmes/cli/upgrade.js +116 -23
- package/dist/holmes/cli/workspace-scope.d.ts +22 -0
- package/dist/holmes/cli/workspace-scope.js +375 -0
- package/dist/holmes/cpg/cpg-scanner.d.ts +29 -0
- package/dist/holmes/cpg/cpg-scanner.js +403 -3
- package/dist/holmes/cpg/cycle-detect.js +2 -1
- package/dist/holmes/cpg/forbidden-edges.js +9 -2
- package/dist/holmes/cpg/required-calls.js +8 -1
- package/dist/holmes/governance/constitution-debt.js +3 -2
- package/dist/holmes/governance/constitution.js +15 -3
- package/dist/holmes/governance/provenance-chain.js +3 -2
- package/dist/holmes/governance/push-gate-runner.js +4 -3
- package/dist/holmes/guardrail/dependency-delta.js +4 -3
- package/dist/holmes/guardrail/linear-match.d.ts +57 -0
- package/dist/holmes/guardrail/linear-match.js +256 -0
- package/dist/holmes/guardrail/risk-classifier.d.ts +6 -7
- package/dist/holmes/guardrail/risk-classifier.js +931 -44
- package/dist/holmes/guardrail/risk-types.d.ts +6 -0
- package/dist/holmes/guardrail/scope-judgment.d.ts +4 -24
- package/dist/holmes/guardrail/scope-judgment.js +100 -23
- package/dist/holmes/guardrail/shell-words.d.ts +42 -0
- package/dist/holmes/guardrail/shell-words.js +1720 -0
- package/dist/holmes/guardrail/write-target.d.ts +168 -3
- package/dist/holmes/guardrail/write-target.js +2379 -87
- package/dist/holmes/hooks/adapters/antigravity.d.ts +3 -1
- package/dist/holmes/hooks/adapters/antigravity.js +115 -11
- package/dist/holmes/hooks/pre-tool-use.d.ts +32 -6
- package/dist/holmes/hooks/pre-tool-use.js +923 -87
- package/dist/holmes/hooks/rtm-refresh-child.js +3 -1
- package/dist/holmes/hooks/rtm-refresh.js +8 -4
- package/dist/holmes/hooks/session-start.d.ts +10 -0
- package/dist/holmes/hooks/session-start.js +31 -7
- package/dist/holmes/hooks/stop.d.ts +2 -1
- package/dist/holmes/hooks/stop.js +197 -91
- package/dist/holmes/mcp/approval-commit.d.ts +5 -6
- package/dist/holmes/mcp/approval-commit.js +18 -2
- package/dist/holmes/mcp/elicit-approval.js +4 -2
- package/dist/holmes/mcp/handlers/graph-operations.js +3 -2
- package/dist/holmes/mcp/handlers/maintenance-analysis.js +11 -5
- package/dist/holmes/mcp/handlers/maintenance-evidence.js +3 -2
- package/dist/holmes/mcp/handlers/phase-queries.d.ts +22 -7
- package/dist/holmes/mcp/handlers/phase-queries.js +81 -2
- package/dist/holmes/mcp/handlers/requirement-triage.js +5 -3
- package/dist/holmes/mcp/handlers/review-evidence.js +8 -2
- package/dist/holmes/mcp/handlers/slice-orchestration.d.ts +11 -0
- package/dist/holmes/mcp/handlers/slice-orchestration.js +115 -16
- package/dist/holmes/mcp/handlers/spec-approval.d.ts +9 -0
- package/dist/holmes/mcp/handlers/spec-approval.js +110 -17
- package/dist/holmes/mcp/handlers/spec-authoring.js +17 -2
- package/dist/holmes/mcp/handlers/spec-queries.d.ts +39 -3
- package/dist/holmes/mcp/handlers/spec-queries.js +114 -7
- package/dist/holmes/mcp/handlers/test-execution.js +9 -3
- package/dist/holmes/mcp/handlers.d.ts +81 -9
- package/dist/holmes/mcp/handlers.js +70 -23
- package/dist/holmes/mcp/harness-attestation.d.ts +20 -0
- package/dist/holmes/mcp/harness-attestation.js +179 -0
- package/dist/holmes/mcp/requirement-triage-evidence.d.ts +36 -0
- package/dist/holmes/mcp/requirement-triage-evidence.js +160 -4
- package/dist/holmes/mcp/server.js +20 -1
- package/dist/holmes/mcp/tool-schemas.js +26 -9
- package/dist/holmes/mcp/validate-args.js +12 -0
- package/dist/holmes/project/bounded-io.d.ts +102 -0
- package/dist/holmes/project/bounded-io.js +212 -0
- package/dist/holmes/project/change-source.js +3 -1
- package/dist/holmes/project/ci-runs.js +3 -2
- package/dist/holmes/project/ignore.d.ts +31 -2
- package/dist/holmes/project/ignore.js +409 -50
- package/dist/holmes/project/regular-file-guard.d.ts +13 -0
- package/dist/holmes/project/regular-file-guard.js +149 -0
- package/dist/holmes/project/root.js +12 -3
- package/dist/holmes/reverse/anchor.js +4 -3
- package/dist/holmes/reverse/scan.js +6 -2
- package/dist/holmes/review/commit-text.js +3 -2
- package/dist/holmes/review/condition-hunk-replay.js +3 -2
- package/dist/holmes/review/failed-test-names.js +13 -10
- package/dist/holmes/review/findings.js +3 -1
- package/dist/holmes/review/kills-check.js +6 -1
- package/dist/holmes/review/point-in-time-replay.js +5 -4
- package/dist/holmes/review/replay-corpus.js +5 -4
- package/dist/holmes/review/run-replay.js +3 -2
- package/dist/holmes/review/temporal-prior.js +3 -2
- package/dist/holmes/review/test-evidence.js +3 -2
- package/dist/holmes/review/test-outcomes.js +3 -2
- package/dist/holmes/review/test-runner.d.ts +6 -0
- package/dist/holmes/review/test-runner.js +36 -15
- package/dist/holmes/rtm/advisory-outcomes.js +3 -2
- package/dist/holmes/rtm/anchor-ids.js +42 -2
- package/dist/holmes/rtm/file-anchors.js +4 -1
- package/dist/holmes/rtm/ftt-fulfilment.js +8 -3
- package/dist/holmes/rtm/git-changes.js +3 -1
- package/dist/holmes/rtm/graph-store.js +3 -2
- package/dist/holmes/rtm/test-scope.js +9 -4
- package/dist/holmes/spec/compat-impact.d.ts +25 -4
- package/dist/holmes/spec/compat-impact.js +682 -11
- package/dist/holmes/spec/entity-git-snapshot.js +3 -2
- package/dist/holmes/spec/entity-integration.js +3 -2
- package/dist/holmes/spec/entity-renumber-transaction.d.ts +19 -0
- package/dist/holmes/spec/entity-renumber-transaction.js +79 -6
- package/dist/holmes/spec/remote-spec-refs.js +3 -2
- package/dist/holmes/spec/renumber.js +25 -10
- package/dist/holmes/spec/spec-parser.js +9 -3
- package/dist/holmes/spec/yaml-scalar.js +2 -2
- package/dist/holmes/update/refresh.js +3 -1
- package/dist/holmes/update/update-notice.d.ts +6 -0
- package/dist/holmes/update/update-notice.js +18 -1
- package/package.json +11 -4
|
@@ -0,0 +1,1720 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.MAX_COMMAND = void 0;
|
|
37
|
+
exports.shellWords = shellWords;
|
|
38
|
+
exports.commandOf = commandOf;
|
|
39
|
+
exports.findRunsUnfiltered = findRunsUnfiltered;
|
|
40
|
+
exports.harnessWiringActOf = harnessWiringActOf;
|
|
41
|
+
exports.harnessWiringActOfCommand = harnessWiringActOfCommand;
|
|
42
|
+
exports.globToRe = globToRe;
|
|
43
|
+
// @implements A-SPEC-728, A-SPEC-733
|
|
44
|
+
// Shell WORDS, not spellings. The wiring-protection rule was first written as regexes over verb
|
|
45
|
+
// spellings and the adversarial round walked through it twenty ways (`\rm`, `env rm`, `rm .mcp"".json`,
|
|
46
|
+
// `rm .claude/settings.{json,local.json}`, `mv`, `git rm`, `find -delete`, …): a gate over a spelling
|
|
47
|
+
// is not a gate over the act. This module reads a segment the way the shell will — quote removal,
|
|
48
|
+
// escapes, `$'…'`, brace lists, home expansion, redirects — finds the real command word, and
|
|
49
|
+
// classifies what that command does to the files it names. Round 2 added what the first reading
|
|
50
|
+
// missed: process substitution, zsh glob groups, nested shell programs, interpreter programs, package
|
|
51
|
+
// runners, git's own base, option clusters, archives. It stays a HEURISTIC over text (A-SPEC-728 §7
|
|
52
|
+
// names the residuals); the identity helpers do the judging.
|
|
53
|
+
const fs = __importStar(require("node:fs"));
|
|
54
|
+
const os = __importStar(require("node:os"));
|
|
55
|
+
const path = __importStar(require("node:path"));
|
|
56
|
+
const linear_match_1 = require("./linear-match");
|
|
57
|
+
const write_target_1 = require("./write-target");
|
|
58
|
+
const NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*/;
|
|
59
|
+
/**
|
|
60
|
+
* `$NAME` / `${NAME[:-…]}` at `src[i]` → [value | null(unknown), length consumed, what an unknown
|
|
61
|
+
* variable's default spells]. `${X:-word}` with X unknowable may expand to `word` (round 4).
|
|
62
|
+
*/
|
|
63
|
+
function readVar(src, i, env, home, pwd) {
|
|
64
|
+
const known = (name) => name === 'HOME' ? home : name === 'CODEX_HOME' && env.CODEX_HOME ? env.CODEX_HOME : name === 'PWD' ? pwd : null;
|
|
65
|
+
if (src[i + 1] === '{') {
|
|
66
|
+
const close = src.indexOf('}', i + 2);
|
|
67
|
+
if (close < 0)
|
|
68
|
+
return [null, src.length - i];
|
|
69
|
+
const inner = src.slice(i + 2, close);
|
|
70
|
+
const name = NAME_RE.exec(inner)?.[0] ?? '';
|
|
71
|
+
const rest = inner.slice(name.length);
|
|
72
|
+
// `${HOME}`, `${HOME:-x}`, `${HOME:?}`, `${HOME=…}`: the value is HOME's whenever HOME is set.
|
|
73
|
+
const v = /^(?::?[-=?].*)?$/s.test(rest) ? known(name) : null;
|
|
74
|
+
const dflt = /^:?[-=](.*)$/s.exec(rest)?.[1];
|
|
75
|
+
return [v, close + 1 - i, v === null && dflt ? dflt.replace(/^(['"])(.*)\1$/s, '$2') : undefined];
|
|
76
|
+
}
|
|
77
|
+
// Linear: the name is read in place, never from a copy of the rest of the text (round 4).
|
|
78
|
+
NAME_Y.lastIndex = i + 1;
|
|
79
|
+
const name = NAME_Y.exec(src)?.[0];
|
|
80
|
+
if (!name)
|
|
81
|
+
return [null, 2];
|
|
82
|
+
return [known(name), 1 + name.length];
|
|
83
|
+
}
|
|
84
|
+
const NAME_Y = /[A-Za-z_][A-Za-z0-9_]*/y;
|
|
85
|
+
/** `$'…'` (ANSI-C quoting) starting at `src[i]` (the `$`) → [decoded text, index of the closing quote]. */
|
|
86
|
+
function readAnsiC(src, i) {
|
|
87
|
+
let out = '';
|
|
88
|
+
let j = i + 2;
|
|
89
|
+
const simple = { n: '\n', t: '\t', r: '\r', a: '\x07', b: '\b', e: '\x1b', E: '\x1b', f: '\f', v: '\v', '\\': '\\', "'": "'", '"': '"', '?': '?' };
|
|
90
|
+
for (; j < src.length && src[j] !== "'"; j++) {
|
|
91
|
+
if (src[j] !== '\\') {
|
|
92
|
+
out += src[j];
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const n = src[j + 1] ?? '';
|
|
96
|
+
// Sticky reads in place: a `.slice` per escape was quadratic on a long `$'…'` (round 4).
|
|
97
|
+
const at = (re) => { re.lastIndex = j + 1; return re.exec(src); };
|
|
98
|
+
const hex = at(HEX_Y);
|
|
99
|
+
const oct = at(OCT_Y);
|
|
100
|
+
const uni = at(UNI_Y);
|
|
101
|
+
if (hex) {
|
|
102
|
+
out += String.fromCharCode(parseInt(hex[1], 16));
|
|
103
|
+
j += hex[0].length;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (uni) {
|
|
107
|
+
const cp = parseInt(uni[1], 16);
|
|
108
|
+
out += cp <= 0x10ffff ? String.fromCodePoint(cp) : '\ufffd';
|
|
109
|
+
j += uni[0].length;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (oct) {
|
|
113
|
+
out += String.fromCharCode(parseInt(oct[1], 8));
|
|
114
|
+
j += oct[0].length;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (n in simple) {
|
|
118
|
+
out += simple[n];
|
|
119
|
+
j++;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
out += n;
|
|
123
|
+
j++;
|
|
124
|
+
}
|
|
125
|
+
return [out, j];
|
|
126
|
+
}
|
|
127
|
+
const HEX_Y = /x([0-9a-fA-F]{1,2})/y;
|
|
128
|
+
const OCT_Y = /([0-7]{1,3})/y;
|
|
129
|
+
const UNI_Y = /[uU]([0-9a-fA-F]{1,8})/y;
|
|
130
|
+
/** Index of the paren balancing the `(` at `src[i]`, or the end. */
|
|
131
|
+
function balance(src, i) {
|
|
132
|
+
let d = 0;
|
|
133
|
+
for (let j = i; j < src.length; j++) {
|
|
134
|
+
if (src[j] === '(')
|
|
135
|
+
d++;
|
|
136
|
+
else if (src[j] === ')' && --d === 0)
|
|
137
|
+
return j;
|
|
138
|
+
}
|
|
139
|
+
return src.length - 1;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Expand unquoted `{a,b}` lists (bounded), keeping quoted braces literal.
|
|
143
|
+
* @implements A-SPEC-728 (round 8) — the total characters produced are bounded too (1 MB per segment):
|
|
144
|
+
* counting alternatives alone let `{a,b}` × n copy the whole word at every level until a 4 GB heap
|
|
145
|
+
* aborted the hook with no verdict (measured). Past the bound the word stays literal and is marked
|
|
146
|
+
* unknowable (`overflow`), the conservative reading.
|
|
147
|
+
*/
|
|
148
|
+
const BRACE_CHAR_BUDGET = 1024 * 1024;
|
|
149
|
+
function expandBraces(chars, budget = { n: 64 }) {
|
|
150
|
+
if (budget.chars === undefined)
|
|
151
|
+
budget.chars = BRACE_CHAR_BUDGET;
|
|
152
|
+
let depth = 0, open = -1;
|
|
153
|
+
for (let i = 0; i < chars.length; i++) {
|
|
154
|
+
const { c, q } = chars[i];
|
|
155
|
+
if (q)
|
|
156
|
+
continue;
|
|
157
|
+
if (c === '{') {
|
|
158
|
+
if (depth === 0)
|
|
159
|
+
open = i;
|
|
160
|
+
depth++;
|
|
161
|
+
}
|
|
162
|
+
else if (c === '}' && depth > 0) {
|
|
163
|
+
depth--;
|
|
164
|
+
if (depth === 0 && open >= 0) {
|
|
165
|
+
const inner = chars.slice(open + 1, i);
|
|
166
|
+
const alts = [];
|
|
167
|
+
let d = 0, start = 0;
|
|
168
|
+
for (let j = 0; j < inner.length; j++) {
|
|
169
|
+
if (inner[j].q)
|
|
170
|
+
continue;
|
|
171
|
+
if (inner[j].c === '{')
|
|
172
|
+
d++;
|
|
173
|
+
else if (inner[j].c === '}')
|
|
174
|
+
d--;
|
|
175
|
+
else if (inner[j].c === ',' && d === 0) {
|
|
176
|
+
alts.push(inner.slice(start, j));
|
|
177
|
+
start = j + 1;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (alts.length === 0) {
|
|
181
|
+
open = -1;
|
|
182
|
+
continue;
|
|
183
|
+
} // `{x}` is literal
|
|
184
|
+
alts.push(inner.slice(start));
|
|
185
|
+
const out = [];
|
|
186
|
+
for (const a of alts) {
|
|
187
|
+
if (budget.n-- <= 0)
|
|
188
|
+
break;
|
|
189
|
+
const size = open + a.length + (chars.length - i - 1);
|
|
190
|
+
if ((budget.chars -= size) < 0) {
|
|
191
|
+
budget.overflow = true;
|
|
192
|
+
return [chars];
|
|
193
|
+
}
|
|
194
|
+
out.push(...expandBraces([...chars.slice(0, open), ...a, ...chars.slice(i + 1)], budget));
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return [chars];
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The simple commands of one segment, as the shell would split and expand them. Operators split
|
|
204
|
+
* commands (`;`, `&`, `|`, newline, `(`, `)` at a word start); redirect targets are collected per
|
|
205
|
+
* command. `pwd` is the segment's working directory (for `~+` and `$PWD`), null when unknowable.
|
|
206
|
+
*/
|
|
207
|
+
let USERNAME;
|
|
208
|
+
function shellWords(src, env = process.env, home = os.homedir(), pwd = null) {
|
|
209
|
+
// @implements A-SPEC-728 (rule 42) — read once per process: it ran for every segment (13% of a 256 KB command — measured).
|
|
210
|
+
const me = USERNAME ?? (USERNAME = (() => { try {
|
|
211
|
+
return os.userInfo().username;
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
return '';
|
|
215
|
+
} })());
|
|
216
|
+
const commands = [];
|
|
217
|
+
let cur = { words: [], redirects: [] };
|
|
218
|
+
let chars = [];
|
|
219
|
+
let inWord = false, known = true;
|
|
220
|
+
let pending = null;
|
|
221
|
+
const subs = [];
|
|
222
|
+
const budget = { n: 4096 }; // brace expansions per segment; beyond it the segment is too large to judge
|
|
223
|
+
const finishWord = () => {
|
|
224
|
+
if (!inWord)
|
|
225
|
+
return;
|
|
226
|
+
let raw = chars;
|
|
227
|
+
// Tilde at the start of an unquoted word: `~`, `~/…`, `~<me>`, `~+` (the working directory).
|
|
228
|
+
if (raw.length > 0 && raw[0].c === '~' && !raw[0].q) {
|
|
229
|
+
const slash = raw.findIndex((x) => x.c === '/');
|
|
230
|
+
const user = raw.slice(1, slash < 0 ? raw.length : slash).map((x) => x.c).join('');
|
|
231
|
+
const to = user === '' || user === me ? home : user === '+' ? pwd : null;
|
|
232
|
+
if (to !== null)
|
|
233
|
+
raw = [...[...to].map((c) => ({ c, q: true })), ...(slash < 0 ? [] : raw.slice(slash))];
|
|
234
|
+
else
|
|
235
|
+
known = false;
|
|
236
|
+
}
|
|
237
|
+
const expanded = expandBraces(raw, budget);
|
|
238
|
+
if (budget.overflow)
|
|
239
|
+
known = false;
|
|
240
|
+
for (const w of expanded) {
|
|
241
|
+
const full = w.map((x) => x.c).join('');
|
|
242
|
+
const nul = full.indexOf('\0'); // an argument ends at NUL: `.mcp.json\0x` is `.mcp.json`
|
|
243
|
+
const word = { text: nul >= 0 ? full.slice(0, nul) : full, known, glob: w.some((x) => !x.q && /[*?[(]/.test(x.c)) };
|
|
244
|
+
if (pending === 'write')
|
|
245
|
+
cur.redirects.push(word);
|
|
246
|
+
else if (pending === null)
|
|
247
|
+
cur.words.push(word);
|
|
248
|
+
}
|
|
249
|
+
pending = null;
|
|
250
|
+
chars = [];
|
|
251
|
+
inWord = false;
|
|
252
|
+
known = true;
|
|
253
|
+
};
|
|
254
|
+
const endCommand = () => {
|
|
255
|
+
finishWord();
|
|
256
|
+
if (cur.words.length > 0 || cur.redirects.length > 0)
|
|
257
|
+
commands.push(cur);
|
|
258
|
+
cur = { words: [], redirects: [] };
|
|
259
|
+
};
|
|
260
|
+
const push = (c, q) => { chars.push({ c, q }); inWord = true; };
|
|
261
|
+
const pushText = (t) => { for (const c of t)
|
|
262
|
+
push(c, true); };
|
|
263
|
+
for (let i = 0; i < src.length; i++) {
|
|
264
|
+
const c = src[i];
|
|
265
|
+
if (c === '\\') {
|
|
266
|
+
const n = src[i + 1];
|
|
267
|
+
if (n === '\n') {
|
|
268
|
+
i++;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (n !== undefined) {
|
|
272
|
+
push(n, true);
|
|
273
|
+
i++;
|
|
274
|
+
}
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (c === '$' && src[i + 1] === "'") {
|
|
278
|
+
const [t, end] = readAnsiC(src, i);
|
|
279
|
+
inWord = true;
|
|
280
|
+
pushText(t);
|
|
281
|
+
i = end;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (c === "'") {
|
|
285
|
+
const close = src.indexOf("'", i + 1);
|
|
286
|
+
const end = close < 0 ? src.length : close;
|
|
287
|
+
inWord = true;
|
|
288
|
+
pushText(src.slice(i + 1, end));
|
|
289
|
+
i = end;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (c === '"') {
|
|
293
|
+
inWord = true;
|
|
294
|
+
let j = i + 1;
|
|
295
|
+
for (; j < src.length && src[j] !== '"'; j++) {
|
|
296
|
+
if (src[j] === '\\' && /["\\$`\n]/.test(src[j + 1] ?? '')) {
|
|
297
|
+
if (src[j + 1] !== '\n')
|
|
298
|
+
push(src[j + 1], true);
|
|
299
|
+
j++;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (src[j] === '$' && /[{A-Za-z_]/.test(src[j + 1] ?? '')) {
|
|
303
|
+
const [v, len, dflt] = readVar(src, j, env, home, pwd);
|
|
304
|
+
if (v === null) {
|
|
305
|
+
known = false;
|
|
306
|
+
if (dflt)
|
|
307
|
+
pushText(dflt);
|
|
308
|
+
}
|
|
309
|
+
else
|
|
310
|
+
pushText(v);
|
|
311
|
+
j += len - 1;
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (src[j] === '$' && src[j + 1] === '(') {
|
|
315
|
+
const end = balance(src, j + 1);
|
|
316
|
+
subs.push(src.slice(j + 2, end));
|
|
317
|
+
known = false;
|
|
318
|
+
j = end;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (src[j] === '`') {
|
|
322
|
+
const close = src.indexOf('`', j + 1);
|
|
323
|
+
const end = close < 0 ? src.length : close;
|
|
324
|
+
subs.push(src.slice(j + 1, end));
|
|
325
|
+
known = false;
|
|
326
|
+
j = end;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
push(src[j], true);
|
|
330
|
+
}
|
|
331
|
+
i = j;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
if (c === '$' && src[i + 1] === '(') {
|
|
335
|
+
const end = balance(src, i + 1);
|
|
336
|
+
subs.push(src.slice(i + 2, end));
|
|
337
|
+
known = false;
|
|
338
|
+
inWord = true;
|
|
339
|
+
i = end;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (c === '`') {
|
|
343
|
+
const close = src.indexOf('`', i + 1);
|
|
344
|
+
const end = close < 0 ? src.length : close;
|
|
345
|
+
subs.push(src.slice(i + 1, end));
|
|
346
|
+
known = false;
|
|
347
|
+
inWord = true;
|
|
348
|
+
i = end;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
if (c === '$' && /[{A-Za-z_]/.test(src[i + 1] ?? '')) {
|
|
352
|
+
const [v, len, dflt] = readVar(src, i, env, home, pwd);
|
|
353
|
+
if (v === null) {
|
|
354
|
+
known = false;
|
|
355
|
+
inWord = true;
|
|
356
|
+
if (dflt)
|
|
357
|
+
pushText(dflt);
|
|
358
|
+
}
|
|
359
|
+
else
|
|
360
|
+
pushText(v);
|
|
361
|
+
i += len - 1;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (/\s/.test(c)) {
|
|
365
|
+
if (c === '\n')
|
|
366
|
+
endCommand();
|
|
367
|
+
else
|
|
368
|
+
finishWord();
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
// A `#` that starts a word starts a comment: the rest of the line is not run (round 4).
|
|
372
|
+
if (c === '#' && !inWord) {
|
|
373
|
+
const nl = src.indexOf('\n', i);
|
|
374
|
+
i = (nl < 0 ? src.length : nl) - 1;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
// Process substitution `<(…)`/`>(…)`: one unknowable word; it does not end the command.
|
|
378
|
+
if ((c === '<' || c === '>') && src[i + 1] === '(' && !inWord) {
|
|
379
|
+
finishWord();
|
|
380
|
+
const end = balance(src, i + 1);
|
|
381
|
+
subs.push(src.slice(i + 2, end));
|
|
382
|
+
cur.words.push({ text: src.slice(i, end + 1), known: false, glob: false });
|
|
383
|
+
i = end;
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
// Redirect operators: `N>`, `>`, `>>`, `>|`, `&>`, `&>>`, `<>`; `<`, `<<`, `<<<`, `>&N` read or dup.
|
|
387
|
+
if (c === '>' || (c === '&' && src[i + 1] === '>') || (c === '<')) {
|
|
388
|
+
const fdWord = inWord && chars.every((x) => /[0-9]/.test(x.c) && !x.q);
|
|
389
|
+
if (fdWord) {
|
|
390
|
+
chars = [];
|
|
391
|
+
inWord = false;
|
|
392
|
+
}
|
|
393
|
+
else
|
|
394
|
+
finishWord();
|
|
395
|
+
let op = c;
|
|
396
|
+
let j = i + 1;
|
|
397
|
+
while (j < src.length && /[<>&|!]/.test(src[j]) && op.length < 3) {
|
|
398
|
+
op += src[j];
|
|
399
|
+
j++;
|
|
400
|
+
}
|
|
401
|
+
i = j - 1;
|
|
402
|
+
if (/^&?>>?[|!]?$|^<>$/.test(op))
|
|
403
|
+
pending = 'write';
|
|
404
|
+
else if (/^>>?&$/.test(op)) {
|
|
405
|
+
// `>&N`/`>&-` duplicates a descriptor; `>&WORD` (bash) writes stdout+stderr to WORD.
|
|
406
|
+
let k = j;
|
|
407
|
+
while (k < src.length && /[ \t]/.test(src[k]))
|
|
408
|
+
k++;
|
|
409
|
+
pending = /[0-9-]/.test(src[k] ?? '') && !/[^\s;&|]/.test(src[k + 1] ?? ' ') ? 'skip' : 'write';
|
|
410
|
+
}
|
|
411
|
+
else
|
|
412
|
+
pending = 'skip'; // `<`, `<<`, `<<<`: input, delimiter or data
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
// An unquoted `(…)` INSIDE a word is a zsh glob group (`.(mcp|x).json`, `@(…)`), part of the word.
|
|
416
|
+
if (c === '(' && inWord) {
|
|
417
|
+
const end = balance(src, i);
|
|
418
|
+
for (const ch of src.slice(i, end + 1))
|
|
419
|
+
push(ch, false);
|
|
420
|
+
i = end;
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (c === ';' || c === '|' || c === '&' || c === '(' || c === ')') {
|
|
424
|
+
endCommand();
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
push(c, false);
|
|
428
|
+
}
|
|
429
|
+
endCommand();
|
|
430
|
+
return { commands, subs, overflow: budget.n <= 0, words: commands[0]?.words ?? [], redirects: commands[0]?.redirects ?? [] };
|
|
431
|
+
}
|
|
432
|
+
const WRAPPERS_WITH_ARG = {
|
|
433
|
+
sudo: /^-[ugpChrtUD]$|^--(?:user|group|prompt|chdir|host|role|type|other-user)$/,
|
|
434
|
+
doas: /^-[uC]$/,
|
|
435
|
+
env: /^-u$|^--unset$|^-C$|^--chdir$/,
|
|
436
|
+
nice: /^-n$|^--adjustment$/,
|
|
437
|
+
stdbuf: /^-[ioe]$/,
|
|
438
|
+
timeout: /^-[sk]$|^--(?:signal|kill-after)$/,
|
|
439
|
+
};
|
|
440
|
+
const PLAIN_WRAPPERS = new Set(['(', '{', '!', 'then', 'do', 'else', 'elif', 'if', 'while', 'until', 'time', 'exec', 'nohup', 'command', 'builtin', 'busybox', 'sudo', 'env', 'nice', 'stdbuf', 'timeout', 'doas', 'coproc']);
|
|
441
|
+
/** The words from the real command word on, behind wrappers, assignments and grouping keywords. */
|
|
442
|
+
function commandOf(input) {
|
|
443
|
+
let words = input;
|
|
444
|
+
let i = 0;
|
|
445
|
+
let unwraps = 0;
|
|
446
|
+
while (i < words.length) {
|
|
447
|
+
if (unwraps > 64)
|
|
448
|
+
throw new Error('wrapper nesting too deep to judge');
|
|
449
|
+
const t = words[i].text;
|
|
450
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(t)) {
|
|
451
|
+
i++;
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (!PLAIN_WRAPPERS.has(t))
|
|
455
|
+
break;
|
|
456
|
+
i++;
|
|
457
|
+
const withArg = WRAPPERS_WITH_ARG[t];
|
|
458
|
+
while (i < words.length && words[i].text.startsWith('-') && words[i].text !== '-') {
|
|
459
|
+
const o = words[i].text;
|
|
460
|
+
i++;
|
|
461
|
+
if (o === '--')
|
|
462
|
+
break;
|
|
463
|
+
// `env -S 'prog args'` / `--split-string=…`: the value IS the command line.
|
|
464
|
+
if (t === 'env' && (o === '-S' || o === '--split-string' || o.startsWith('--split-string=') || /^-S./.test(o))) {
|
|
465
|
+
const prog = o.startsWith('--split-string=') ? o.slice('--split-string='.length) : /^-S./.test(o) ? o.slice(2) : words[i]?.text ?? '';
|
|
466
|
+
if (!(o.startsWith('--split-string=') || /^-S./.test(o)))
|
|
467
|
+
i++;
|
|
468
|
+
words = [...shellWords(prog).words, ...words.slice(i)];
|
|
469
|
+
i = 0;
|
|
470
|
+
unwraps++;
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
if (withArg?.test(o) && i < words.length)
|
|
474
|
+
i++;
|
|
475
|
+
}
|
|
476
|
+
if (t === 'env')
|
|
477
|
+
while (i < words.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[i].text))
|
|
478
|
+
i++;
|
|
479
|
+
if (t === 'timeout' && i < words.length && /^[0-9.]+[smhd]?$/.test(words[i].text))
|
|
480
|
+
i++;
|
|
481
|
+
if (t === 'nice' && i < words.length && /^-?[0-9]+$/.test(words[i].text))
|
|
482
|
+
i++;
|
|
483
|
+
}
|
|
484
|
+
return words.slice(i);
|
|
485
|
+
}
|
|
486
|
+
const READ_VERBS = new Set(['cat', 'less', 'more', 'head', 'tail', 'grep', 'egrep', 'fgrep', 'rg', 'ag', 'diff',
|
|
487
|
+
'cmp', 'wc', 'stat', 'ls', 'file', 'bat', 'md5', 'md5sum', 'shasum', 'sha1sum', 'sha224sum', 'sha256sum', 'sha384sum',
|
|
488
|
+
'sha512sum', 'b2sum', 'cksum', 'od', 'hexdump', 'xxd', 'strings', 'test', '[', 'realpath', 'readlink', 'basename',
|
|
489
|
+
'dirname', 'echo', 'printf', 'true', 'false', ':', 'cd', 'pushd', 'popd', 'pwd', 'which', 'type', 'cut', 'tr', 'column',
|
|
490
|
+
'nl', 'fold', 'fmt', 'code', 'open', 'git-log']);
|
|
491
|
+
const INPLACE_EDITORS = new Set(['tee', 'sponge', 'truncate', 'touch', 'chmod', 'chown', 'chgrp', 'ex', 'vi', 'vim', 'nvim', 'nano', 'emacs', 'patch']);
|
|
492
|
+
const DELETE_VERBS = new Set(['rm', 'unlink', 'shred', 'trash', 'trash-put', 'srm', 'rimraf', 'del', 'del-cli', 'rm-cli']);
|
|
493
|
+
const SHELLS = new Set(['sh', 'bash', 'zsh', 'dash', 'ksh', 'mksh', 'fish']);
|
|
494
|
+
const INTERPRETER = /^(?:node|nodejs|python[0-9.]*|ruby|perl|deno|bun|php|pwsh|tsx|ts-node|ts-node-esm)$/;
|
|
495
|
+
// Runners whose `run` sub-command runs the program that follows (round 4), and their valued options.
|
|
496
|
+
const RUN_RUNNERS = new Set(['uv', 'poetry', 'pipx', 'pdm', 'hatch', 'conda', 'mamba', 'micromamba', 'rye', 'pixi']);
|
|
497
|
+
const RUNNER_VALUED = /^(?:-n|--name|-p|--prefix|--with|--python|-e|--env|--env-file|--spec|--directory|--project|-C|--cwd)$/;
|
|
498
|
+
// Long paths are judged by name only: resolving 58 000 components took 37 s (round 4).
|
|
499
|
+
const MAX_PATH = 4096;
|
|
500
|
+
// Beyond this a command is not parsed (A-SPEC-728 §10).
|
|
501
|
+
exports.MAX_COMMAND = 256 * 1024;
|
|
502
|
+
// Rule 37: the `open(…, 'w')` half is read linearly (the one regex rescanned from every `open(` — measured quadratic).
|
|
503
|
+
const MUTATION_WORD = /\b(?:unlink|remove|rmtree|rename|rm|rmdir|delete|write|truncate|copy|move|symlink|chmod|outputFile|replace)\w*/i;
|
|
504
|
+
const OPEN_WRITE = (0, linear_match_1.openForWriting)(true);
|
|
505
|
+
const MUTATION = { test: (t) => MUTATION_WORD.test(t) || OPEN_WRITE.test(t) };
|
|
506
|
+
const QUOTED_LITERAL = /(['"`])((?:(?!\1)[^\\\n]|\\.){1,512})\1/g;
|
|
507
|
+
const verbName = (w) => path.basename(w.replace(/\\/g, '/')).replace(/\.exe$/i, '').toLowerCase();
|
|
508
|
+
const flagSet = (args) => args.filter((a) => a.text.startsWith('-') && a.text !== '--' && a.text !== '-').map((a) => a.text);
|
|
509
|
+
/** Values of an option in every spelling: `-o F`, `-oF`, `-sSLo F`, `-qOF`, `--out F`, `--out=F`. */
|
|
510
|
+
function optValues(args, letters, long) {
|
|
511
|
+
const out = [];
|
|
512
|
+
for (let i = 0; i < args.length; i++) {
|
|
513
|
+
const t = args[i].text;
|
|
514
|
+
if (t === '--')
|
|
515
|
+
break;
|
|
516
|
+
const eq = /^(--[\w-]+)=(.*)$/.exec(t);
|
|
517
|
+
if (eq && long.test(eq[1])) {
|
|
518
|
+
out.push({ value: { ...args[i], text: eq[2] }, consumed: [args[i]] });
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
if (long.test(t) && args[i + 1]) {
|
|
522
|
+
out.push({ value: args[i + 1], consumed: [args[i], args[i + 1]] });
|
|
523
|
+
i++;
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
if (!/^-[A-Za-z]/.test(t) || t.startsWith('--'))
|
|
527
|
+
continue;
|
|
528
|
+
for (let k = 1; k < t.length; k++) {
|
|
529
|
+
if (!letters.includes(t[k]))
|
|
530
|
+
continue;
|
|
531
|
+
const rest = t.slice(k + 1);
|
|
532
|
+
if (rest !== '')
|
|
533
|
+
out.push({ value: { ...args[i], text: rest }, consumed: [args[i]] });
|
|
534
|
+
else if (args[i + 1]) {
|
|
535
|
+
out.push({ value: args[i + 1], consumed: [args[i], args[i + 1]] });
|
|
536
|
+
i++;
|
|
537
|
+
}
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return out;
|
|
542
|
+
}
|
|
543
|
+
/** Non-option words, excluding option values already consumed. */
|
|
544
|
+
function positionalsOf(args, consumed = []) {
|
|
545
|
+
const out = [];
|
|
546
|
+
let done = false;
|
|
547
|
+
// Rule 44: a set — a list made this O(arguments × option values) (1.1 s per judgement at 256 KB — measured).
|
|
548
|
+
const skip = new Set(consumed);
|
|
549
|
+
for (const a of args) {
|
|
550
|
+
if (skip.has(a) && !done)
|
|
551
|
+
continue;
|
|
552
|
+
if (!done && a.text === '--') {
|
|
553
|
+
done = true;
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (!done && a.text.startsWith('-') && a.text !== '-')
|
|
557
|
+
continue;
|
|
558
|
+
out.push(a);
|
|
559
|
+
}
|
|
560
|
+
return out;
|
|
561
|
+
}
|
|
562
|
+
/** A write-kind that names a whole directory (a wiring directory or `.ax`), not a file in it. */
|
|
563
|
+
const isDirectoryKind = (k) => /directory$/.test(k) || k === write_target_1.GOVERNANCE_STORE_LABEL;
|
|
564
|
+
/** Whether text names a wiring file or directory — literally, or by a glob token that can match one. */
|
|
565
|
+
// @implements A-SPEC-728 (round 7) — asked about the WHOLE command once per segment, this was quadratic
|
|
566
|
+
// (200 KB of `; bash `: 42.7 s end to end, past the hook timeout — measured). The last answer is kept.
|
|
567
|
+
const textMemo = new Map();
|
|
568
|
+
/**
|
|
569
|
+
* A whole-command fact, computed once per (fact, text).
|
|
570
|
+
* @implements A-SPEC-728 (rule 44) — up to 256 texts per fact, as `mentionsWiring` keeps them: with only the last text kept, each
|
|
571
|
+
* nested program's own text evicted the whole command's answer (ten `xargs touch` / `bash -c 'xargs touch'` pairs before 100 KB
|
|
572
|
+
* of `find ` took 6 s — measured).
|
|
573
|
+
*/
|
|
574
|
+
const perText = (fact, text, compute) => {
|
|
575
|
+
let answers = textMemo.get(fact);
|
|
576
|
+
if (answers === undefined) {
|
|
577
|
+
answers = new Map();
|
|
578
|
+
textMemo.set(fact, answers);
|
|
579
|
+
}
|
|
580
|
+
return remembered(answers, text, compute);
|
|
581
|
+
};
|
|
582
|
+
/**
|
|
583
|
+
* @implements A-SPEC-728 (rule 45) — an answer about a text: a text of up to 512 characters is computed each time (it costs its
|
|
584
|
+
* own length), a longer one is kept among the 256 most recently asked, and one is dropped at a time. Rule 43's map was cleared
|
|
585
|
+
* whole at 256 entries and every quoted literal of a program took one: 257 literals evicted the whole command's answer, and
|
|
586
|
+
* 90 programs after 17,000 glob words took 10.7 s (measured).
|
|
587
|
+
*/
|
|
588
|
+
// @implements A-SPEC-728 (rule 46) — whatever the text's length, least-recently-used within 8 MiB of text and 65,536 entries:
|
|
589
|
+
// rule 45 computed every text of up to 512 characters each time, and a 487-character program of 115 glob words asked about once
|
|
590
|
+
// per segment by 16 loops took 5.5 s (measured — regression).
|
|
591
|
+
const REMEMBERED_BYTES_MAX = 8 * 1024 * 1024;
|
|
592
|
+
const REMEMBERED_ENTRIES_MAX = 65_536;
|
|
593
|
+
const rememberedBytes = new WeakMap();
|
|
594
|
+
function remembered(answers, text, compute) {
|
|
595
|
+
const hit = answers.get(text);
|
|
596
|
+
if (hit !== undefined) {
|
|
597
|
+
answers.delete(text);
|
|
598
|
+
answers.set(text, hit);
|
|
599
|
+
return hit;
|
|
600
|
+
}
|
|
601
|
+
const answer = compute(text);
|
|
602
|
+
let bytes = (rememberedBytes.get(answers) ?? 0) + text.length;
|
|
603
|
+
answers.set(text, answer);
|
|
604
|
+
for (const oldest of answers.keys()) {
|
|
605
|
+
if (bytes <= REMEMBERED_BYTES_MAX && answers.size <= REMEMBERED_ENTRIES_MAX)
|
|
606
|
+
break;
|
|
607
|
+
if (oldest === text)
|
|
608
|
+
break;
|
|
609
|
+
answers.delete(oldest);
|
|
610
|
+
bytes -= oldest.length;
|
|
611
|
+
}
|
|
612
|
+
rememberedBytes.set(answers, bytes);
|
|
613
|
+
return answer;
|
|
614
|
+
}
|
|
615
|
+
/** @implements A-SPEC-728 (rule 46) — identical words are judged once (261 KB of one repeated member or source took 2.3–4.2 s). */
|
|
616
|
+
function distinctWords(words) {
|
|
617
|
+
const seen = new Set();
|
|
618
|
+
return words.filter((w) => { const k = `${w.known ? 1 : 0}${w.glob ? 1 : 0}${w.text}`; if (seen.has(k))
|
|
619
|
+
return false; seen.add(k); return true; });
|
|
620
|
+
}
|
|
621
|
+
const FIND_FILTERS = ['name', 'iname', 'path', 'ipath', 'regex'];
|
|
622
|
+
/**
|
|
623
|
+
* @implements A-SPEC-728 (rule 44) — whether some `find` runs with no `-name`/`-path`/`-regex` filter after it in its clause
|
|
624
|
+
* (`/(^|[|;&\s])find\b(?![^|;&]*-(?:i?name|i?path|regex))/`), in one pass: the regex rescanned the clause from every `find`
|
|
625
|
+
* (100 KB of `find `: 97% of a 6 s verdict — measured). In a clause only its last `find` decides: a filter after it is after
|
|
626
|
+
* every earlier one.
|
|
627
|
+
*/
|
|
628
|
+
function findRunsUnfiltered(t) {
|
|
629
|
+
let findEnd = -1;
|
|
630
|
+
let filtered = false;
|
|
631
|
+
for (let i = 0; i < t.length; i++) {
|
|
632
|
+
const c = t[i];
|
|
633
|
+
if (c === '|' || c === ';' || c === '&') {
|
|
634
|
+
if (findEnd >= 0 && !filtered)
|
|
635
|
+
return true;
|
|
636
|
+
findEnd = -1;
|
|
637
|
+
filtered = false;
|
|
638
|
+
}
|
|
639
|
+
else if (c === 'f' && t.startsWith('find', i) && (i === 0 || /[|;&\s]/.test(t[i - 1])) && (i + 4 === t.length || !/\w/.test(t[i + 4]))) {
|
|
640
|
+
findEnd = i + 4;
|
|
641
|
+
filtered = false;
|
|
642
|
+
}
|
|
643
|
+
else if (c === '-' && findEnd >= 0 && FIND_FILTERS.some((f) => t.startsWith(f, i + 1))) {
|
|
644
|
+
filtered = true;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return findEnd >= 0 && !filtered;
|
|
648
|
+
}
|
|
649
|
+
// @implements A-SPEC-728 (rule 43) — remembered per text (up to 256): a nested shell asked about its own text and evicted the whole
|
|
650
|
+
// command's answer, so each pair of `bash; bash -c bash` recompiled every glob of the command (200 pairs: 18.5 s — measured).
|
|
651
|
+
const wiringAnswers = new Map();
|
|
652
|
+
const mentionsWiring = (text) => {
|
|
653
|
+
// Rule 46: one memo — the single-slot one beside it made each defence untestable alone.
|
|
654
|
+
return remembered(wiringAnswers, text, mentionsWiringUncached);
|
|
655
|
+
};
|
|
656
|
+
const mentionsWiringUncached = (text) => {
|
|
657
|
+
const t = text.toLowerCase();
|
|
658
|
+
if (write_target_1.WIRING_FILE_NAMES.some((n) => t.includes(n)) || /(^|[^\w.-])\.(?:claude|agents|codex|ax)(?![\w-])/.test(t))
|
|
659
|
+
return true;
|
|
660
|
+
for (const tok of t.split(/[\s;|&<>()'"`=]+/)) {
|
|
661
|
+
if (!/[*?[]/.test(tok) || tok.length > 256)
|
|
662
|
+
continue;
|
|
663
|
+
const re = (0, write_target_1.globPatternRe)(tok.split('/').pop() ?? tok, true);
|
|
664
|
+
if ([...write_target_1.WIRING_FILE_NAMES, ...write_target_1.WIRING_DIR_NAMES].some((n) => re.test(n)))
|
|
665
|
+
return true;
|
|
666
|
+
}
|
|
667
|
+
return false;
|
|
668
|
+
};
|
|
669
|
+
const CALL_ROLES = new Map([
|
|
670
|
+
...['unlink', 'unlinksync', 'remove', 'removesync', 'rmtree', 'rm', 'rmsync', 'rm_r', 'rm_rf', 'rm_f', 'rmdir', 'rmdirsync',
|
|
671
|
+
'remove_dir', 'remove_entry', 'remove_entry_secure', 'remove_file', 'delete', 'unlink_all', 'writefile', 'writefilesync',
|
|
672
|
+
'appendfile', 'appendfilesync', 'truncate', 'truncatesync', 'outputfile', 'outputfilesync', 'outputjson', 'outputjsonsync',
|
|
673
|
+
'writejson', 'writejsonsync', 'writetextfile', 'writetextfilesync', 'createwritestream', 'chmod', 'chmodsync', 'lchmod',
|
|
674
|
+
'file_put_contents', 'emptydir', 'emptydirsync', 'ensurefile', 'ensurefilesync', 'write', 'binwrite', 'shred']
|
|
675
|
+
.map((n) => [n, 'first']),
|
|
676
|
+
...['copy', 'copy2', 'copyfile', 'copyfilesync', 'copy_file', 'cp', 'cp_r', 'cpsync', 'copytree', 'copysync', 'symlink',
|
|
677
|
+
'symlinksync', 'link', 'linksync', 'ln', 'ln_s', 'ln_sf', 'install', 'ensuresymlink', 'ensuresymlinksync', 'ensurelink']
|
|
678
|
+
.map((n) => [n, 'second']),
|
|
679
|
+
...['rename', 'renamesync', 'move', 'movesync', 'mv', 'replace'].map((n) => [n, 'move']),
|
|
680
|
+
...['open', 'opensync', 'fopen', 'sysopen'].map((n) => [n, 'open']),
|
|
681
|
+
]);
|
|
682
|
+
// Names that take a PATH only as a function of these modules/classes (`File.write`, `os.replace`),
|
|
683
|
+
// never as a stream, string or list method (`f.write(data)`, `s.replace(a, b)`, `os.write(fd, b)`).
|
|
684
|
+
const MODULE_ONLY = new Map([['write', /^(?:File|IO|Bun|Deno)$/], ['binwrite', /^(?:File|IO)$/],
|
|
685
|
+
['delete', /^(?:File|Files|FileUtils)$/], ['replace', /^os$/]]);
|
|
686
|
+
// Identifiers are bounded and must start a name, so a long identifier cannot make this quadratic.
|
|
687
|
+
const CALL_RE = /(?<![\w$])([A-Za-z_$][\w$]{0,63})(\s*\??\.\s*)?([A-Za-z_$][\w$]{0,63})?(?=\s*(?:\?\.\s*)?\(|[ \t]+['"])/g;
|
|
688
|
+
// @implements A-SPEC-728 (round 5) — the other call spellings: a bracketed name (`fs['unlinkSync'](`),
|
|
689
|
+
// dynamic dispatch (`getattr(os,'remove')(`, `.send(:delete, …)`) and a mutating function taken by
|
|
690
|
+
// reference (`const f = fs.unlinkSync`). The bracketed form is read by role like any call; dispatch
|
|
691
|
+
// and references cannot be, so they count as a mutating call with a computed target.
|
|
692
|
+
const BRACKET_CALL_RE = /\[\s*(['"`])([A-Za-z_$][\w$]{0,63})\1\s*\](?=\s*(?:\?\.\s*)?\()/g;
|
|
693
|
+
const DISPATCH_RE = /\bgetattr\s*\(\s*[^,()]{1,128},\s*['"]([A-Za-z_]\w{0,63})['"]|\.(?:send|public_send|__send__)\s*\(?\s*:['"]?([A-Za-z_]\w{0,63})|\bmethod\s*\(\s*:([A-Za-z_]\w{0,63})/g;
|
|
694
|
+
const REFERENCE_RE = /[=:(,]\s*(?:[A-Za-z_$][\w$]{0,63}\s*\??\.\s*){1,4}([A-Za-z_$][\w$]{0,63})\s*(?=[;,)}\n]|$)/g;
|
|
695
|
+
const CALL_SCAN_MAX = 16 * 1024 * 1024;
|
|
696
|
+
const PATH_METHOD = /(['"])((?:(?!\1)[^\\\n]){1,512})\1\s*\)\s*\.\s*(?:unlink|write_text|write_bytes|rename|replace|rmdir|chmod|touch)\b/gi;
|
|
697
|
+
/**
|
|
698
|
+
* The arguments of a call whose text starts at `at` (after `(` when `paren`), bounded to 4 KiB; `scan.left` is charged
|
|
699
|
+
* the characters read (rule 37).
|
|
700
|
+
*/
|
|
701
|
+
function callArgs(program, at, paren, scan) {
|
|
702
|
+
const out = [];
|
|
703
|
+
let depth = 0, q = null, acc = '';
|
|
704
|
+
const limit = Math.min(program.length, at + 4096);
|
|
705
|
+
let i = at;
|
|
706
|
+
for (; i < limit; i++) {
|
|
707
|
+
const ch = program[i];
|
|
708
|
+
if (q !== null) {
|
|
709
|
+
acc += ch;
|
|
710
|
+
if (ch === '\\') {
|
|
711
|
+
acc += program[i + 1] ?? '';
|
|
712
|
+
i++;
|
|
713
|
+
}
|
|
714
|
+
else if (ch === q)
|
|
715
|
+
q = null;
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
719
|
+
q = ch;
|
|
720
|
+
acc += ch;
|
|
721
|
+
continue;
|
|
722
|
+
}
|
|
723
|
+
if (ch === '(' || ch === '[' || ch === '{') {
|
|
724
|
+
depth++;
|
|
725
|
+
acc += ch;
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
if (ch === ')' || ch === ']' || ch === '}') {
|
|
729
|
+
if (depth === 0)
|
|
730
|
+
break;
|
|
731
|
+
depth--;
|
|
732
|
+
acc += ch;
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
if (ch === ',' && depth === 0) {
|
|
736
|
+
out.push(acc.trim());
|
|
737
|
+
acc = '';
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
if (!paren && depth === 0 && (ch === ';' || ch === '\n'))
|
|
741
|
+
break;
|
|
742
|
+
acc += ch;
|
|
743
|
+
}
|
|
744
|
+
scan.left -= i - at;
|
|
745
|
+
out.push(acc.trim());
|
|
746
|
+
// Keyword arguments land where their name says (`shutil.copy(src=…, dst=…)`, `open(f, mode="w")`).
|
|
747
|
+
const pos = [];
|
|
748
|
+
const kw = { src: 0, source: 0, path: 0, file: 0, filename: 0, old: 0, oldpath: 0, dst: 1, dest: 1, destination: 1, target: 1, new: 1, newpath: 1, mode: 1, flags: 1 };
|
|
749
|
+
for (const a of out) {
|
|
750
|
+
const m = /^([A-Za-z_]\w*)\s*=(?!=)\s*([\s\S]*)$/.exec(a);
|
|
751
|
+
if (m && m[1] in kw)
|
|
752
|
+
pos[kw[m[1]]] = m[2];
|
|
753
|
+
else {
|
|
754
|
+
let k = 0;
|
|
755
|
+
while (pos[k] !== undefined)
|
|
756
|
+
k++;
|
|
757
|
+
pos[k] = a;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return pos.map((a) => a ?? '');
|
|
761
|
+
}
|
|
762
|
+
/** A string literal's text, or null when the argument is computed (a name, a call, interpolation). */
|
|
763
|
+
function literalOf(arg) {
|
|
764
|
+
const m = /^[rRbBuU]{0,2}(['"`])((?:(?!\1)[^\\]|\\.)*)\1$/s.exec(arg.trim());
|
|
765
|
+
if (!m)
|
|
766
|
+
return null;
|
|
767
|
+
if (m[1] === '`' && m[2].includes('${'))
|
|
768
|
+
return null;
|
|
769
|
+
if (m[1] === '"' && /\$[\w{]|#\{/.test(m[2]))
|
|
770
|
+
return null; // Perl/Ruby/PHP interpolation
|
|
771
|
+
return m[2];
|
|
772
|
+
}
|
|
773
|
+
const SHELLY = /\b(?:rm|unlink|mv|cp|tee|shred|truncate|dd|git|find|xargs|sh|bash|zsh|ln|install|rsync|sed|perl|python[0-9.]*|node)\b/;
|
|
774
|
+
/**
|
|
775
|
+
* @implements A-SPEC-728
|
|
776
|
+
* Which governance wiring target one segment writes or deletes, or `null`. `base` is the segment's
|
|
777
|
+
* working directory (null = unknowable: only absolute words are judged); `fullText` is the whole
|
|
778
|
+
* command, which `xargs`/`parallel` and stdin shells consult because their arguments arrive on stdin.
|
|
779
|
+
*/
|
|
780
|
+
function harnessWiringActOf(root, base, segmentText, fullText, env = process.env, home = os.homedir(), depth = 0) {
|
|
781
|
+
if (depth > 8)
|
|
782
|
+
return mentionsWiring(segmentText) ? 'a wiring file named in a command nested too deep to judge' : null;
|
|
783
|
+
const { commands, subs, overflow } = shellWords(segmentText, env, home, base);
|
|
784
|
+
if (overflow)
|
|
785
|
+
return 'a command too large to judge (brace expansion)';
|
|
786
|
+
const absolute = (t) => path.isAbsolute(t) || /^[A-Za-z]:[\\/]/.test(t);
|
|
787
|
+
// Where the base is unknowable (a dynamic cd, a nesting deeper than the segmenter follows), a
|
|
788
|
+
// RELATIVE word cannot be resolved — but a word whose name is a wiring file (or, for a recursive
|
|
789
|
+
// deletion, a wiring directory) is refused by name rather than let through (round 3).
|
|
790
|
+
const byName = (w, dirs) => {
|
|
791
|
+
const n = (0, linear_match_1.trimTrailing)(w.text, '/\\').split(/[/\\]/).pop()?.toLowerCase() ?? '';
|
|
792
|
+
const shown = w.text.length > 120 ? `…${w.text.slice(-80)}` : w.text;
|
|
793
|
+
return write_target_1.WIRING_FILE_NAMES.includes(n) || (dirs && write_target_1.WIRING_DIR_NAMES.includes(n))
|
|
794
|
+
? `${shown} (${!w.known ? 'a path spelled with an unknowable part' : w.text.length > MAX_PATH ? 'a path too long to resolve' : 'where the command runs is unknowable'})` : null;
|
|
795
|
+
};
|
|
796
|
+
// A word made unknowable by `$(…)` or a variable is still refused when its last component is a
|
|
797
|
+
// wiring file name (`rm "$(git rev-parse --show-toplevel)/.agents/hooks.json"`, round 4).
|
|
798
|
+
const unknownByName = (w, dirs) => (w.text === '' ? null : byName(w, dirs));
|
|
799
|
+
// @implements A-SPEC-728 (rule 47) — a path that passed is not judged again within one judgement of a command (a loop judges
|
|
800
|
+
// its body once per word: 16 loops over 80 tar members at six directories took 10.6 s — measured), and one command judges at
|
|
801
|
+
// most PATHS_JUDGED_MAX distinct paths: past that it is refused as too large to judge, so the bound never makes a pass.
|
|
802
|
+
// Rule 48: and at most PATHS_CHARS_MAX characters of path text — 40,000 short words against one 4 KB directory are 40,000
|
|
803
|
+
// distinct paths and 160 MB (2.6–4.0 s, measured). 32 MiB let 9 KB of loops over wide members run 2.2–2.4 s.
|
|
804
|
+
const judged = pathsJudged ?? { passed: new Map(), fresh: 0, chars: 0 };
|
|
805
|
+
const at = (b) => {
|
|
806
|
+
let mine = judged.passed.get(b);
|
|
807
|
+
if (mine === undefined) {
|
|
808
|
+
mine = new Set();
|
|
809
|
+
judged.passed.set(b, mine);
|
|
810
|
+
}
|
|
811
|
+
const passedAt = mine;
|
|
812
|
+
const once = (act, w, judge) => {
|
|
813
|
+
const k = `${act}${w.known ? 1 : 0}${w.glob ? 1 : 0}${w.text}`;
|
|
814
|
+
if (passedAt.has(k))
|
|
815
|
+
return null;
|
|
816
|
+
if (++judged.fresh > PATHS_JUDGED_MAX)
|
|
817
|
+
return `a command too large to judge (more than ${PATHS_JUDGED_MAX.toLocaleString('en-US')} paths) that could name a wiring file`;
|
|
818
|
+
if ((judged.chars += (b?.length ?? 0) + w.text.length) > PATHS_CHARS_MAX)
|
|
819
|
+
return `a command too large to judge (more than ${PATHS_CHARS_MAX / (1024 * 1024)} MiB of paths) that could name a wiring file`;
|
|
820
|
+
const hit = judge();
|
|
821
|
+
if (hit === null)
|
|
822
|
+
passedAt.add(k);
|
|
823
|
+
return hit;
|
|
824
|
+
};
|
|
825
|
+
return {
|
|
826
|
+
judgeable: (w) => w.known && w.text !== '' && (b !== null || absolute(w.text)),
|
|
827
|
+
del: (w, recursive) => once(recursive ? 'R' : 'D', w, () => !w.known ? unknownByName(w, recursive) : w.text === '' ? null
|
|
828
|
+
: w.text.length > MAX_PATH ? byName(w, recursive)
|
|
829
|
+
: b !== null || absolute(w.text) ? (0, write_target_1.wiringDeletionKindOf)(root, b, w.text, env, home, recursive) : byName(w, recursive)),
|
|
830
|
+
wr: (w) => once('W', w, () => !w.known ? (w.glob ? null : unknownByName(w, false)) : w.text === '' || w.glob ? null
|
|
831
|
+
: w.text.length > MAX_PATH ? byName(w, false)
|
|
832
|
+
: b !== null || absolute(w.text) ? (0, write_target_1.wiringWriteKindOf)(root, b ?? root, w.text, env, home) : byName(w, false)),
|
|
833
|
+
};
|
|
834
|
+
};
|
|
835
|
+
const { judgeable, del, wr } = at(base);
|
|
836
|
+
const first = (xs) => xs.find((x) => x !== null) ?? null;
|
|
837
|
+
const joinBase = (b, d) => (absolute(d) ? d : b === null ? null : path.resolve(b, d));
|
|
838
|
+
/** A quoted literal naming a wiring target, in a program that shows mutation evidence. */
|
|
839
|
+
const inlineProgramHit = (program) => {
|
|
840
|
+
// A quoted literal that is itself a shell command (`system("rm .mcp.json")`, `os.system`, exec).
|
|
841
|
+
// @implements A-SPEC-728 (rule 45) — each quoted command is judged once per judgement (by depth, base and text) and at most
|
|
842
|
+
// 4,096 of them: 21,800 literals `'cd b;rm'` were each parsed and placed again (2.6 s — measured).
|
|
843
|
+
const judged = quotedJudged ?? new Set();
|
|
844
|
+
for (const m of program.matchAll(QUOTED_LITERAL)) {
|
|
845
|
+
if (!(m[2].length < 4096 && SHELLY.test(m[2])))
|
|
846
|
+
continue;
|
|
847
|
+
const key = JSON.stringify([depth, base, m[2]]);
|
|
848
|
+
if (judged.has(key))
|
|
849
|
+
continue;
|
|
850
|
+
if (judged.size >= QUOTED_COMMANDS_MAX)
|
|
851
|
+
return `a program too large to judge (more than ${QUOTED_COMMANDS_MAX} quoted shell commands) that could name a wiring file`;
|
|
852
|
+
judged.add(key);
|
|
853
|
+
const h = recurse(m[2], base);
|
|
854
|
+
if (h)
|
|
855
|
+
return h;
|
|
856
|
+
}
|
|
857
|
+
// A wiring literal in the ROLE of a mutating call's target — not merely present beside one.
|
|
858
|
+
const lit = (text) => ({ text, known: true, glob: false });
|
|
859
|
+
let computed = false; // a mutating call whose target is not a literal
|
|
860
|
+
const sites = [];
|
|
861
|
+
// @implements A-SPEC-728 (rule 37) — the argument reading is bounded: each call reads up to 4 KiB, so 87,000 unclosed
|
|
862
|
+
// `rm(` calls read 350 MB (2.7 s — measured). Past 16 MiB the program is refused as too large to judge; a call's
|
|
863
|
+
// arguments end at its `)`, so ordinary code reads about its own length.
|
|
864
|
+
// Rule 38: the 16 MiB are the whole command's (`callScan`), not each program's — 64 loop substitutions of an 8 KB
|
|
865
|
+
// program and 17 programs of 14 KB each stayed under a per-program budget and took 2–8 s (measured).
|
|
866
|
+
const scan = callScan ?? { left: CALL_SCAN_MAX };
|
|
867
|
+
for (const m of program.matchAll(CALL_RE)) {
|
|
868
|
+
const name = (m[3] ?? m[1]).toLowerCase();
|
|
869
|
+
const receiver = MODULE_ONLY.get(name);
|
|
870
|
+
if (receiver !== undefined && !(m[3] && receiver.test(m[1])))
|
|
871
|
+
continue;
|
|
872
|
+
sites.push({ name, after: (m.index ?? 0) + m[0].length });
|
|
873
|
+
}
|
|
874
|
+
for (const m of program.matchAll(BRACKET_CALL_RE)) {
|
|
875
|
+
const name = m[2].toLowerCase();
|
|
876
|
+
if (!MODULE_ONLY.has(name))
|
|
877
|
+
sites.push({ name, after: (m.index ?? 0) + m[0].length });
|
|
878
|
+
}
|
|
879
|
+
for (const m of program.matchAll(DISPATCH_RE)) {
|
|
880
|
+
const name = (m[1] ?? m[2] ?? m[3]).toLowerCase();
|
|
881
|
+
if (CALL_ROLES.has(name))
|
|
882
|
+
computed = true;
|
|
883
|
+
}
|
|
884
|
+
for (const m of program.matchAll(REFERENCE_RE)) {
|
|
885
|
+
const name = m[1].toLowerCase();
|
|
886
|
+
if (CALL_ROLES.has(name) && !MODULE_ONLY.has(name))
|
|
887
|
+
computed = true;
|
|
888
|
+
}
|
|
889
|
+
for (const { name, after } of sites) {
|
|
890
|
+
const role = CALL_ROLES.get(name);
|
|
891
|
+
if (role === undefined)
|
|
892
|
+
continue;
|
|
893
|
+
let k = after;
|
|
894
|
+
while (k < program.length && k < after + 64 && /\s/.test(program[k]))
|
|
895
|
+
k++;
|
|
896
|
+
if (program.startsWith('?.', k)) {
|
|
897
|
+
k += 2;
|
|
898
|
+
while (k < program.length && /\s/.test(program[k]))
|
|
899
|
+
k++;
|
|
900
|
+
}
|
|
901
|
+
if (k >= program.length)
|
|
902
|
+
continue;
|
|
903
|
+
const paren = program[k] === '(';
|
|
904
|
+
if (scan.left < 0)
|
|
905
|
+
return 'a program with too many calls to judge that could name a wiring file';
|
|
906
|
+
const args = callArgs(program, paren ? k + 1 : k, paren, scan);
|
|
907
|
+
const judgeArg = (a, how) => {
|
|
908
|
+
if (a === undefined || a === '')
|
|
909
|
+
return null;
|
|
910
|
+
const t = literalOf(a);
|
|
911
|
+
if (t === null) {
|
|
912
|
+
computed = true;
|
|
913
|
+
return null;
|
|
914
|
+
}
|
|
915
|
+
if (/[$`]/.test(t))
|
|
916
|
+
return null;
|
|
917
|
+
return how === 'del' ? del(lit(t), true) : how === 'wr' ? wr(lit(t)) : del(lit(t), true) ?? wr(lit(t));
|
|
918
|
+
};
|
|
919
|
+
let h = null;
|
|
920
|
+
if (role === 'first')
|
|
921
|
+
h = judgeArg(args[0], 'any');
|
|
922
|
+
else if (role === 'second')
|
|
923
|
+
h = judgeArg(args[1], 'wr');
|
|
924
|
+
else if (role === 'move')
|
|
925
|
+
h = judgeArg(args[0], 'del') ?? judgeArg(args[1], 'wr');
|
|
926
|
+
else {
|
|
927
|
+
// `'w'`, `'a'`, `'x'`, `'r+'`, `'wb'`, `'as'`; `os.O_WRONLY|…` flags. Reading modes write nothing.
|
|
928
|
+
const mode = args[1] ?? '';
|
|
929
|
+
const ml = literalOf(mode);
|
|
930
|
+
if (ml !== null ? /[wax+]/.test(ml) : /O_(?:WRONLY|RDWR|CREAT|TRUNC|APPEND)/.test(mode))
|
|
931
|
+
h = judgeArg(args[0], 'any');
|
|
932
|
+
}
|
|
933
|
+
if (h)
|
|
934
|
+
return h;
|
|
935
|
+
}
|
|
936
|
+
for (const m of program.matchAll(PATH_METHOD)) {
|
|
937
|
+
if (/[$`]/.test(m[2]))
|
|
938
|
+
continue;
|
|
939
|
+
const h = del(lit(m[2]), true) ?? wr(lit(m[2]));
|
|
940
|
+
if (h)
|
|
941
|
+
return h;
|
|
942
|
+
}
|
|
943
|
+
// A computed target in a program that quotes a wiring path (`p=".mcp.json"; os.remove(p)`).
|
|
944
|
+
if (computed) {
|
|
945
|
+
for (const m of program.matchAll(QUOTED_LITERAL))
|
|
946
|
+
if (mentionsWiring(m[2]))
|
|
947
|
+
return `${m[2]} (the target of a mutating call is computed from it)`;
|
|
948
|
+
}
|
|
949
|
+
return null;
|
|
950
|
+
};
|
|
951
|
+
const recurse = (program, b) => harnessWiringActOfCommand(root, program, env, home, b ?? root, depth + 1, b === null);
|
|
952
|
+
const judge = (words, nest = 0) => {
|
|
953
|
+
if (words.length === 0)
|
|
954
|
+
return null;
|
|
955
|
+
if (nest > 64)
|
|
956
|
+
throw new Error('runner nesting too deep to judge');
|
|
957
|
+
const verb = verbName(words[0].text);
|
|
958
|
+
const args = words.slice(1);
|
|
959
|
+
const pos = positionalsOf(args);
|
|
960
|
+
const flags = flagSet(args);
|
|
961
|
+
const recursiveFlag = flags.some((f) => /^-[a-zA-Z]*[rR]/.test(f) || f === '--recursive');
|
|
962
|
+
// Package runners run another program: judge that program.
|
|
963
|
+
const afterFlags = (ws) => {
|
|
964
|
+
let k = 0;
|
|
965
|
+
while (k < ws.length && ws[k].text.startsWith('-')) {
|
|
966
|
+
if (ws[k].text === '--') {
|
|
967
|
+
k++;
|
|
968
|
+
break;
|
|
969
|
+
}
|
|
970
|
+
if (/^(?:-p|--package|-c|--call)$/.test(ws[k].text))
|
|
971
|
+
k++;
|
|
972
|
+
k++;
|
|
973
|
+
}
|
|
974
|
+
return ws.slice(k);
|
|
975
|
+
};
|
|
976
|
+
// `-c`/`--call` carries a shell command line (`npx -c 'rm x'`, `npm exec -c …`).
|
|
977
|
+
const callProgram = (ws) => ws.find((w, i) => i > 0 && /^(?:-c|--call)$/.test(ws[i - 1].text));
|
|
978
|
+
// `eval ARGS` runs its arguments joined as a command line (round 4).
|
|
979
|
+
if (verb === 'eval') {
|
|
980
|
+
if (args.some((a) => !a.known))
|
|
981
|
+
return mentionsWiring(fullText) ? 'a wiring file named in an eval whose text cannot be read' : null;
|
|
982
|
+
return recurse(args.map((a) => a.text).join(' '), base);
|
|
983
|
+
}
|
|
984
|
+
if (RUN_RUNNERS.has(verb) && pos[0]?.text === 'run') {
|
|
985
|
+
const rest = args.slice(args.indexOf(pos[0]) + 1);
|
|
986
|
+
let k = 0;
|
|
987
|
+
while (k < rest.length && rest[k].text.startsWith('-')) {
|
|
988
|
+
if (rest[k].text === '--') {
|
|
989
|
+
k++;
|
|
990
|
+
break;
|
|
991
|
+
}
|
|
992
|
+
if (RUNNER_VALUED.test(rest[k].text))
|
|
993
|
+
k++;
|
|
994
|
+
k++;
|
|
995
|
+
}
|
|
996
|
+
return judge(commandOf(rest.slice(k)), nest + 1);
|
|
997
|
+
}
|
|
998
|
+
if (verb === 'npx' || verb === 'bunx' || verb === 'pnpx') {
|
|
999
|
+
const call = callProgram(args);
|
|
1000
|
+
if (call)
|
|
1001
|
+
return call.known ? recurse(call.text, base) : (mentionsWiring(fullText) ? 'a wiring file named in a runner program that cannot be read' : null);
|
|
1002
|
+
return judge(commandOf(afterFlags(args)), nest + 1);
|
|
1003
|
+
}
|
|
1004
|
+
if ((verb === 'npm' || verb === 'pnpm' || verb === 'yarn') && pos.length > 0) {
|
|
1005
|
+
// Global options may sit before the sub-command (`npm --prefix . exec`, `pnpm -C . exec`).
|
|
1006
|
+
const execAt = args.findIndex((a) => ['exec', 'x', 'dlx'].includes(a.text));
|
|
1007
|
+
const sub = execAt >= 0 ? args[execAt].text : pos[0].text;
|
|
1008
|
+
const rest = args.slice(execAt >= 0 ? execAt + 1 : args.indexOf(pos[0]) + 1);
|
|
1009
|
+
if (execAt >= 0) {
|
|
1010
|
+
const call = callProgram(rest);
|
|
1011
|
+
if (call)
|
|
1012
|
+
return call.known ? recurse(call.text, base) : (mentionsWiring(fullText) ? 'a wiring file named in a runner program that cannot be read' : null);
|
|
1013
|
+
return judge(commandOf(afterFlags(rest)), nest + 1);
|
|
1014
|
+
}
|
|
1015
|
+
if (verb === 'yarn' && !['add', 'remove', 'install', 'upgrade', 'init', 'run', 'why', 'info', 'config', 'cache', 'global',
|
|
1016
|
+
'workspace', 'workspaces', 'set', 'plugin', 'npm', 'pack', 'publish', 'list', 'outdated', 'link', 'unlink', 'test', 'build'].includes(sub)) {
|
|
1017
|
+
return judge(commandOf(args.slice(args.indexOf(pos[0]))), nest + 1);
|
|
1018
|
+
}
|
|
1019
|
+
return null; // their own sub-commands (`npm rm left-pad`, `npm run build`)
|
|
1020
|
+
}
|
|
1021
|
+
if (READ_VERBS.has(verb))
|
|
1022
|
+
return null;
|
|
1023
|
+
if (SHELLS.has(verb)) {
|
|
1024
|
+
const cIdx = args.findIndex((a) => /^-[a-zA-Z]*c[a-zA-Z]*$/.test(a.text));
|
|
1025
|
+
if (cIdx >= 0) {
|
|
1026
|
+
const prog = args.slice(cIdx + 1).find((a) => !a.text.startsWith('-'));
|
|
1027
|
+
if (prog && !prog.known)
|
|
1028
|
+
return mentionsWiring(fullText) ? 'a wiring file named in a shell program that cannot be read' : null;
|
|
1029
|
+
return prog ? recurse(prog.text, base) : null;
|
|
1030
|
+
}
|
|
1031
|
+
const script = pos[0] ?? args.find((a) => a.text === '-');
|
|
1032
|
+
if (script === undefined || script.text === '-' || /^\/dev\/(?:stdin|fd\/0)$/.test(script.text) || !script.known) {
|
|
1033
|
+
return mentionsWiring(fullText) ? 'a wiring file named in a command fed to a shell on stdin' : null;
|
|
1034
|
+
}
|
|
1035
|
+
return wr(script);
|
|
1036
|
+
}
|
|
1037
|
+
if (verb === '.' || verb === 'source') {
|
|
1038
|
+
const script = pos[0];
|
|
1039
|
+
if (script === undefined || /^\/dev\/(?:stdin|fd\/0)$/.test(script.text) || !script.known) {
|
|
1040
|
+
return mentionsWiring(fullText) ? 'a wiring file named in a command sourced from stdin' : null;
|
|
1041
|
+
}
|
|
1042
|
+
return null;
|
|
1043
|
+
}
|
|
1044
|
+
if (INTERPRETER.test(verb)) {
|
|
1045
|
+
const letters = verb.startsWith('python') ? 'c' : verb === 'perl' ? 'eE' : verb === 'ruby' ? 'e' : 'epr';
|
|
1046
|
+
// A program flag in a cluster (`-pe`, `-ne`) takes the NEXT word; `-cprint(1)` carries it glued.
|
|
1047
|
+
let prog;
|
|
1048
|
+
for (let i = 0; i < args.length && !prog; i++) {
|
|
1049
|
+
const t = args[i].text;
|
|
1050
|
+
if (/^--(?:eval|print)$/.test(t) && args[i + 1]) {
|
|
1051
|
+
prog = { value: args[i + 1], consumed: [args[i], args[i + 1]] };
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
const eq = /^--(?:eval|print)=(.*)$/s.exec(t);
|
|
1055
|
+
if (eq) {
|
|
1056
|
+
prog = { value: { ...args[i], text: eq[1] }, consumed: [args[i]] };
|
|
1057
|
+
break;
|
|
1058
|
+
}
|
|
1059
|
+
if (!/^-[a-zA-Z]/.test(t) || t.startsWith('--'))
|
|
1060
|
+
continue;
|
|
1061
|
+
for (let k = 1; k < t.length; k++) {
|
|
1062
|
+
if (!letters.includes(t[k]))
|
|
1063
|
+
continue;
|
|
1064
|
+
const rest = t.slice(k + 1);
|
|
1065
|
+
if (rest !== '' && !/^[a-zA-Z]+$/.test(rest))
|
|
1066
|
+
prog = { value: { ...args[i], text: rest }, consumed: [args[i]] };
|
|
1067
|
+
else if (args[i + 1])
|
|
1068
|
+
prog = { value: args[i + 1], consumed: [args[i], args[i + 1]] };
|
|
1069
|
+
break;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
if (!prog && verb === 'deno' && pos[0]?.text === 'eval' && pos[1])
|
|
1073
|
+
prog = { value: pos[1], consumed: [pos[0], pos[1]] };
|
|
1074
|
+
const inPlacePerl = verb === 'perl' && flags.some((f) => /^-[a-zA-Z]*i/.test(f));
|
|
1075
|
+
if (inPlacePerl)
|
|
1076
|
+
return first(positionalsOf(args, prog?.consumed).map(wr));
|
|
1077
|
+
if (prog)
|
|
1078
|
+
return prog.value.known ? inlineProgramHit(prog.value.text) : (mentionsWiring(fullText) ? 'a wiring file named in a program that cannot be read' : null);
|
|
1079
|
+
if (pos.length === 0 || pos[0].text === '-')
|
|
1080
|
+
return mentionsWiring(fullText) && perText('mutation', fullText, (t) => MUTATION.test(t)) ? 'a wiring file named in a program read from stdin' : null;
|
|
1081
|
+
// A script file: an argument that IS a wiring file may be written by it.
|
|
1082
|
+
return first(pos.slice(1).map((a) => { const k = wr(a); return k !== null && !isDirectoryKind(k) ? k : null; }));
|
|
1083
|
+
}
|
|
1084
|
+
if (DELETE_VERBS.has(verb))
|
|
1085
|
+
return first(distinctWords(pos).map((w) => del(w, verb.startsWith('trash') || verb === 'rimraf' || verb.startsWith('del') || recursiveFlag)));
|
|
1086
|
+
if (['mv', 'cp', 'install', 'rsync', 'ln', 'ditto'].includes(verb)) { // ditto: macOS copy (round 9)
|
|
1087
|
+
const t = optValues(args, 't', /^--target-directory$/)[0];
|
|
1088
|
+
const p2 = positionalsOf(args, t?.consumed);
|
|
1089
|
+
const tDir = t?.value;
|
|
1090
|
+
const srcs = distinctWords(tDir ? p2 : p2.slice(0, -1));
|
|
1091
|
+
const dest = tDir ?? p2[p2.length - 1];
|
|
1092
|
+
if (verb === 'mv') {
|
|
1093
|
+
const h = first(srcs.map((w) => del(w, true)));
|
|
1094
|
+
if (h)
|
|
1095
|
+
return h;
|
|
1096
|
+
}
|
|
1097
|
+
// @implements A-SPEC-728 (round 9) — `--remove-source-files` deletes what it copied, as `mv` does.
|
|
1098
|
+
if (verb === 'rsync' && flags.includes('--remove-source-files')) {
|
|
1099
|
+
const h = first(srcs.map((w) => del(w, true)));
|
|
1100
|
+
if (h)
|
|
1101
|
+
return h;
|
|
1102
|
+
}
|
|
1103
|
+
if (!dest)
|
|
1104
|
+
return null;
|
|
1105
|
+
if (verb === 'rsync' && flags.some((f) => f.startsWith('--delete'))) {
|
|
1106
|
+
const h = del(dest, true);
|
|
1107
|
+
if (h)
|
|
1108
|
+
return h;
|
|
1109
|
+
}
|
|
1110
|
+
// Copying a directory's CONTENTS (`src/`, `src/.`) into a directory that holds a wiring target.
|
|
1111
|
+
if (verb === 'ditto' || ((verb === 'rsync' || (verb === 'cp' && recursiveFlag)) && srcs.some((s) => /[/\\]\.?$/.test(s.text)))) {
|
|
1112
|
+
const h = del(dest, true);
|
|
1113
|
+
if (h)
|
|
1114
|
+
return h;
|
|
1115
|
+
}
|
|
1116
|
+
// An EXISTING directory destination receives the sources; only the landing paths are written
|
|
1117
|
+
// (`cp CLAUDE.md ~/.claude/`, round 4). A missing one is created from the source.
|
|
1118
|
+
const destDir = (() => {
|
|
1119
|
+
const k = wr(dest);
|
|
1120
|
+
if (k === null || !/directory$/.test(k))
|
|
1121
|
+
return k;
|
|
1122
|
+
const e = (0, write_target_1.expandHomeSpelling)(dest.text, env, home);
|
|
1123
|
+
if (e === null || (base === null && !absolute(e)))
|
|
1124
|
+
return k;
|
|
1125
|
+
try {
|
|
1126
|
+
return fs.statSync(path.resolve(base ?? root, e)).isDirectory() ? null : k;
|
|
1127
|
+
}
|
|
1128
|
+
catch {
|
|
1129
|
+
return k;
|
|
1130
|
+
}
|
|
1131
|
+
})();
|
|
1132
|
+
return first([destDir, ...srcs.map((s) => wr({ ...dest, text: `${(0, linear_match_1.trimTrailing)(dest.text, '/\\')}/${path.basename((0, linear_match_1.trimTrailing)(s.text, '/\\'))}` }))]);
|
|
1133
|
+
}
|
|
1134
|
+
if (verb === 'touch') {
|
|
1135
|
+
// `-r REF`/`-d DATE`/`-t STAMP` take values that are not written (round 4).
|
|
1136
|
+
const vals = optValues(args, 'rdt', /^--(?:reference|date)$/);
|
|
1137
|
+
return first(positionalsOf(args, vals.flatMap((o) => o.consumed)).map(wr));
|
|
1138
|
+
}
|
|
1139
|
+
if (verb === 'patch') {
|
|
1140
|
+
const h = first(pos.map(wr));
|
|
1141
|
+
if (h)
|
|
1142
|
+
return h;
|
|
1143
|
+
// Without a named file the patch lands wherever its headers say (round 4).
|
|
1144
|
+
return mentionsWiring(fullText) ? 'a wiring file named in a patch that patch applies' : null;
|
|
1145
|
+
}
|
|
1146
|
+
if (INPLACE_EDITORS.has(verb))
|
|
1147
|
+
return first(pos.map(wr));
|
|
1148
|
+
if (verb === 'sed') {
|
|
1149
|
+
// A-SPEC-728 rule 33: BSD `-I` and getopt_long abbreviations of `--in-place` (`--in`) edit in place too (measured on macOS).
|
|
1150
|
+
if (flags.some((f) => /^-[a-zA-Z]*[iI]/.test(f) || /^--i(?:n(?:-(?:p(?:l(?:a(?:c(?:e)?)?)?)?)?)?)?(?:=|$)/.test(f)))
|
|
1151
|
+
return first(pos.map(wr));
|
|
1152
|
+
// `w FILE` and `s///w FILE` write FILE even without -i.
|
|
1153
|
+
const scripts = [...optValues(args, 'e', /^--expression$/).map((o) => o.value), ...(optValues(args, 'e', /^--expression$/).length ? [] : pos.slice(0, 1))];
|
|
1154
|
+
for (const sc of scripts) {
|
|
1155
|
+
for (const m of sc.text.matchAll(/(?:^|[;\n{}]|\s)\d*[,\d$]*\s*w\s+([^\s;}]+)/g)) {
|
|
1156
|
+
const h = wr({ ...sc, text: m[1] });
|
|
1157
|
+
if (h)
|
|
1158
|
+
return h;
|
|
1159
|
+
}
|
|
1160
|
+
for (const m of sc.text.matchAll(/s(.)(?:(?!\1).)*\1(?:(?!\1).)*\1[gpiImMe0-9]*w\s*([^\s;}]+)/g)) {
|
|
1161
|
+
const h = wr({ ...sc, text: m[2] });
|
|
1162
|
+
if (h)
|
|
1163
|
+
return h;
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
return null;
|
|
1167
|
+
}
|
|
1168
|
+
if (verb === 'jq' || verb === 'yq')
|
|
1169
|
+
return flags.some((f) => /^-[a-zA-Z]*i/.test(f) || f.startsWith('--inplace')) ? first(pos.map(wr)) : null;
|
|
1170
|
+
if (verb === 'awk' || verb === 'gawk' || verb === 'mawk' || verb === 'nawk') {
|
|
1171
|
+
if (args.some((a) => a.text === 'inplace' || a.text.startsWith('--inplace') || /^-i\s*inplace$/.test(a.text)))
|
|
1172
|
+
return first(pos.map(wr));
|
|
1173
|
+
const program = pos[0];
|
|
1174
|
+
return program && program.known ? inlineProgramHit(program.text) : null;
|
|
1175
|
+
}
|
|
1176
|
+
if (verb === 'zip') {
|
|
1177
|
+
// `-m`/`--move` deletes the sources once archived; the archive itself is written.
|
|
1178
|
+
const archive = pos[0];
|
|
1179
|
+
const h = archive ? wr(archive) : null;
|
|
1180
|
+
if (h)
|
|
1181
|
+
return h;
|
|
1182
|
+
if (flags.some((f) => (/^-[a-zA-Z]*m/.test(f) && !f.startsWith('--')) || f === '--move'))
|
|
1183
|
+
return first(pos.slice(1).map((w) => del(w, recursiveFlag)));
|
|
1184
|
+
return null;
|
|
1185
|
+
}
|
|
1186
|
+
// Rule 33: `--o`, `--outp` — any prefix of `--output` (measured: `sort --outp=F` wrote F).
|
|
1187
|
+
if (verb === 'sort')
|
|
1188
|
+
return first(optValues(args, 'o', /^--o(?:u(?:t(?:p(?:u(?:t)?)?)?)?)?$/).map((o) => wr(o.value)));
|
|
1189
|
+
if (verb === 'uniq')
|
|
1190
|
+
return pos[1] ? wr(pos[1]) : null;
|
|
1191
|
+
if (verb === 'base64')
|
|
1192
|
+
return first(optValues(args, 'o', /^--output$/).map((o) => wr(o.value)));
|
|
1193
|
+
if (verb === 'openssl')
|
|
1194
|
+
return first(args.filter((a, i) => i > 0 && args[i - 1].text === '-out').map(wr));
|
|
1195
|
+
if (verb === 'dd')
|
|
1196
|
+
return first(args.filter((a) => a.text.startsWith('of=')).map((a) => wr({ ...a, text: a.text.slice(3) })));
|
|
1197
|
+
if (verb === 'curl') {
|
|
1198
|
+
const outs = optValues(args, 'o', /^--output$/);
|
|
1199
|
+
const h = first(outs.map((o) => wr(o.value)));
|
|
1200
|
+
if (h)
|
|
1201
|
+
return h;
|
|
1202
|
+
if (flags.some((f) => /^-[a-zA-Z]*O/.test(f) || f.startsWith('--remote-name'))) {
|
|
1203
|
+
return first(positionalsOf(args, outs.flatMap((o) => o.consumed)).map((u) => wr({ ...u, text: path.basename((0, linear_match_1.stripUrlTail)(u.text)) })));
|
|
1204
|
+
}
|
|
1205
|
+
return null;
|
|
1206
|
+
}
|
|
1207
|
+
if (verb === 'wget') {
|
|
1208
|
+
const outs = optValues(args, 'O', /^--output-document$/);
|
|
1209
|
+
if (outs.length > 0)
|
|
1210
|
+
return first(outs.map((o) => wr(o.value)));
|
|
1211
|
+
const pfx = optValues(args, 'P', /^--directory-prefix$/)[0];
|
|
1212
|
+
const urls = positionalsOf(args, pfx?.consumed);
|
|
1213
|
+
const dir = pfx?.value.text ?? '.';
|
|
1214
|
+
return first(urls.map((u) => wr({ ...u, text: path.join(dir, path.basename((0, linear_match_1.stripUrlTail)(u.text))) })));
|
|
1215
|
+
}
|
|
1216
|
+
if (verb === 'tar' || verb === 'bsdtar') {
|
|
1217
|
+
// @implements A-SPEC-728 (rule 46) — tar is judged under several READINGS of its options, and refused when any of them
|
|
1218
|
+
// names a protected target; a reading only ever adds. (1) As before: every `-C`/`-f` letter of a dash bundle takes a value.
|
|
1219
|
+
// (2, 3) In a bundle the first letter that takes a value takes the rest of the word (`-xfConfig.tar` is `-f Config.tar`),
|
|
1220
|
+
// and an old-style bundle (`tar xfC e.tar ..`) takes its values from the words after it in the letters' order — with
|
|
1221
|
+
// bsdtar's value letters, and with GNU tar's (`L`, `H` are flags for one and take values for the other — bsdtar measured).
|
|
1222
|
+
// A first attempt REPLACED reading 1 by one such grammar and dropped option "values" as members: `tar -cLf .mcp.json src`
|
|
1223
|
+
// and `tar -xf e.tar m.txt --exclude .mcp.json` passed (measured before it was committed). The tar option grammar is not
|
|
1224
|
+
// transcribed: spellings that move where members land (`--strip-components`, `--transform`, `s`) judge members by name too.
|
|
1225
|
+
const oldStyle = args[0] !== undefined && /^[a-zA-Z]+$/.test(args[0].text) && !args[0].text.includes('.') ? args[0].text : '';
|
|
1226
|
+
const allC = optValues(args, 'C', /^--directory$/);
|
|
1227
|
+
const allF = optValues(args, 'f', /^--file$/);
|
|
1228
|
+
const readingWith = (valued) => {
|
|
1229
|
+
const valuedFirst = (t) => { for (const ch of t.slice(1))
|
|
1230
|
+
if (valued.includes(ch))
|
|
1231
|
+
return ch; return null; };
|
|
1232
|
+
const bundled = (o, letter) => { const t = o.consumed[0].text; return t.startsWith('--') || valuedFirst(t) === letter; };
|
|
1233
|
+
const old = [];
|
|
1234
|
+
{
|
|
1235
|
+
let k = 1;
|
|
1236
|
+
for (const ch of oldStyle)
|
|
1237
|
+
if (valued.includes(ch) && args[k] !== undefined)
|
|
1238
|
+
old.push({ letter: ch, value: args[k++] });
|
|
1239
|
+
}
|
|
1240
|
+
const oldOf = (letter) => old.filter((v) => v.letter === letter).map((v) => ({ value: v.value, consumed: [v.value] }));
|
|
1241
|
+
return { c: [...oldOf('C'), ...allC.filter((o) => bundled(o, 'C'))], f: [...oldOf('f'), ...allF.filter((o) => bundled(o, 'f'))],
|
|
1242
|
+
extra: old.filter((v) => v.letter !== 'C' && v.letter !== 'f').map((v) => v.value) };
|
|
1243
|
+
};
|
|
1244
|
+
const readings = [{ c: allC, f: allF, extra: [] }, readingWith('bCfIsTX'), readingWith('bCfTXKNVgHIL')];
|
|
1245
|
+
// Rule 49: an `s` is the option only BEFORE the bundle's first value-taking letter, under either tar's letters — after
|
|
1246
|
+
// one it is that letter's value (`-Cdist`, `-Izstd`, `-fsite.tar` read as "an s in a bundle" made a plain extraction a
|
|
1247
|
+
// strip one, and a member named like a wiring file was refused — wave 48, measured).
|
|
1248
|
+
// In a DASH bundle a letter that takes a value for bsdtar or for GNU tar ends the options (`-Nyesterday`, `-gsnap.snar`:
|
|
1249
|
+
// stopping only at a letter valued for both read them as the option). In an OLD-STYLE first argument every letter is
|
|
1250
|
+
// an option — values come from the following words — so any `s` in it is (`tar xfs e.tar ,pk/,, pk/.mcp.json` passed
|
|
1251
|
+
// when the word was stopped at `f`, and bsdtar lands the wiring file — wave 49, measured).
|
|
1252
|
+
const sOption = (letters) => {
|
|
1253
|
+
for (const ch of letters) {
|
|
1254
|
+
if (ch === 's')
|
|
1255
|
+
return true;
|
|
1256
|
+
if (!/[a-zA-Z]/.test(ch) || 'bCfITXKNVgHL'.includes(ch))
|
|
1257
|
+
return false;
|
|
1258
|
+
}
|
|
1259
|
+
return false;
|
|
1260
|
+
};
|
|
1261
|
+
const moving = /^(?:strip-components|strip|transform|xform)(?:=|$)/;
|
|
1262
|
+
const byNameToo = args.some((a, i) => (a.text.startsWith('--') && moving.test(a.text.slice(2))) || (/^-[a-zA-Z]/.test(a.text) && sOption(a.text.slice(1)))
|
|
1263
|
+
// bsdtar's `-W longopt`: the long option as the next word.
|
|
1264
|
+
|| (a.text === '-W' && moving.test(args[i + 1]?.text ?? '')))
|
|
1265
|
+
|| oldStyle.includes('s');
|
|
1266
|
+
// @implements A-SPEC-728 (rule 44) — `-C` values compose as `cd`s do: `tar -C sub -C ../.ax -xf e.tar` extracts into `.ax`
|
|
1267
|
+
// (measured with bsdtar) and each value alone named nothing protected. The composed directory is judged too.
|
|
1268
|
+
// @implements A-SPEC-728 (rule 45) — by position: bsdtar stops reading options at the first member name (a later `-C` is a
|
|
1269
|
+
// member pattern — measured), GNU tar applies each `-C` to the names after it (documented). A member is judged at the
|
|
1270
|
+
// directory in effect under each reading; rule 44 composed every `-C` of the command, so `tar -C . -xf e.tar .mcp.json -C
|
|
1271
|
+
// sub` was judged in `sub` and passed (regression). An empty value changes nothing; an unknown one makes the directory
|
|
1272
|
+
// unknown until an absolute value.
|
|
1273
|
+
const step = (dir, v) => (!v.known ? null : v.text === '' ? dir : absolute(v.text) ? v.text : dir === null ? null : dir === '' ? v.text : `${dir}/${v.text}`);
|
|
1274
|
+
const mode = (re, long) => args.some((a, i) => (re.test(a.text) && (i === 0 || a.text.startsWith('-')) && !a.text.startsWith('--')) || long.test(a.text));
|
|
1275
|
+
const creating = mode(/^-?[a-zA-Z]*[cruA]/, /^--(?:create|append|update|catenate|concatenate)$/);
|
|
1276
|
+
const extracting = mode(/^-?[a-zA-Z]*x/, /^--(?:extract|get)$/);
|
|
1277
|
+
// @implements A-SPEC-728 (round 9) — `--remove-files` deletes the archived members, as `mv` does.
|
|
1278
|
+
const removes = args.some((a) => a.text === '--remove-files');
|
|
1279
|
+
const where = new Map();
|
|
1280
|
+
args.forEach((a, i) => where.set(a, i));
|
|
1281
|
+
const judges = new Map();
|
|
1282
|
+
const inDir = (d) => {
|
|
1283
|
+
let j = judges.get(d);
|
|
1284
|
+
if (j === undefined) {
|
|
1285
|
+
j = at(d === null ? null : d === '' ? base : joinBase(base, d));
|
|
1286
|
+
judges.set(d, j);
|
|
1287
|
+
}
|
|
1288
|
+
return j;
|
|
1289
|
+
};
|
|
1290
|
+
// @implements A-SPEC-728 (rule 49, owner decision 2026-09-28) — under an option that moves where members land, a member
|
|
1291
|
+
// any of whose components is a wiring file's or a wiring directory's name is refused BY NAME, whatever directory it
|
|
1292
|
+
// lands in. Rules 47 and 48 modelled where a stripped member lands (suffixes judged at the directory) and were passed in
|
|
1293
|
+
// four waves: every suffix (time), the last three (a `-C` above the target), the last sixteen (`./` padding), then a
|
|
1294
|
+
// leading separator, `a/..` pairs and pattern members (each lands on the wiring — bsdtar, measured). The decided cost:
|
|
1295
|
+
// `tar -xzf theme.tgz --strip-components=1 -C dist package/settings.json` needs an approval.
|
|
1296
|
+
const namedLikeWiring = (m) => {
|
|
1297
|
+
if (!byNameToo)
|
|
1298
|
+
return null;
|
|
1299
|
+
const hit = m.text.split(/[/\\]/).find((c) => { const n = c.toLowerCase(); return n !== '' && n !== '.' && (write_target_1.WIRING_FILE_NAMES.includes(n) || write_target_1.WIRING_DIR_NAMES.includes(n)); });
|
|
1300
|
+
if (hit === undefined)
|
|
1301
|
+
return null;
|
|
1302
|
+
const shown = m.text.length > 120 ? `…${m.text.slice(-80)}` : m.text;
|
|
1303
|
+
return `${shown} (a tar member named like a wiring file, under an option that moves where members land)`;
|
|
1304
|
+
};
|
|
1305
|
+
const readingKeys = new Set();
|
|
1306
|
+
for (const { c, f, extra } of readings) {
|
|
1307
|
+
const key = JSON.stringify([c.map((o) => [where.get(o.consumed[0]), o.value.text]), f.map((o) => [where.get(o.consumed[0]), o.value.text]), extra.map((w) => where.get(w))]);
|
|
1308
|
+
if (readingKeys.has(key))
|
|
1309
|
+
continue;
|
|
1310
|
+
readingKeys.add(key);
|
|
1311
|
+
const h = first(c.map((o) => wr(o.value)));
|
|
1312
|
+
if (h)
|
|
1313
|
+
return h;
|
|
1314
|
+
// Creating/appending WRITES the -f archive (`tar -cf .mcp.json src`).
|
|
1315
|
+
if (creating) {
|
|
1316
|
+
const hf = first(f.map((o) => wr(o.value)));
|
|
1317
|
+
if (hf)
|
|
1318
|
+
return hf;
|
|
1319
|
+
}
|
|
1320
|
+
// Rule 45: an old-style mode word (`tar xf e.tar`) is the FIRST argument only — the first member after options (`tar -xf
|
|
1321
|
+
// e.tar a -C sub .mcp.json`) was read as one and dropped, so the next member counted as the first.
|
|
1322
|
+
const members = positionalsOf(args, [...[...c, ...f].flatMap((o) => o.consumed), ...extra]).filter((m) => !(where.get(m) === 0 && /^[a-zA-Z]+$/.test(m.text) && !m.text.includes('.')));
|
|
1323
|
+
const changes = c.map((o) => ({ value: o.value, at: where.get(o.consumed[0]) ?? 0 })).sort((x, y) => x.at - y.at);
|
|
1324
|
+
// '' is the segment's own directory; null an unknown one.
|
|
1325
|
+
let running = '';
|
|
1326
|
+
let next = 0;
|
|
1327
|
+
let beforeFirst = '';
|
|
1328
|
+
const inEffect = new Map();
|
|
1329
|
+
// Rule 48: a directory past 4,096 characters is not composed further (only an absolute value or an unknown one
|
|
1330
|
+
// replaces it): 21,000 `-C` values were composed for every member before the length was looked at, and the judging
|
|
1331
|
+
// worker ended out of memory (measured).
|
|
1332
|
+
const move = (v) => { if (running === null || running.length <= 4096 || !v.known || absolute(v.text))
|
|
1333
|
+
running = step(running, v); };
|
|
1334
|
+
for (const [i, m] of members.entries()) {
|
|
1335
|
+
const pos = where.get(m) ?? 0;
|
|
1336
|
+
for (; next < changes.length && changes[next].at < pos; next++)
|
|
1337
|
+
move(changes[next].value);
|
|
1338
|
+
if (i === 0)
|
|
1339
|
+
beforeFirst = running;
|
|
1340
|
+
inEffect.set(m, running);
|
|
1341
|
+
}
|
|
1342
|
+
for (; next < changes.length; next++)
|
|
1343
|
+
move(changes[next].value);
|
|
1344
|
+
if (members.length === 0)
|
|
1345
|
+
beforeFirst = running;
|
|
1346
|
+
const dirs = [...new Set([running, beforeFirst, ...inEffect.values()])];
|
|
1347
|
+
// Rule 44: members are judged at a directory of at most 4,096 characters, built once — each member resolved at a 52 KB
|
|
1348
|
+
// composed directory took 16.9 s for 26,000 of them (measured).
|
|
1349
|
+
if ((removes || extracting) && members.length > 0 && dirs.some((d) => d !== null && d.length > 4096))
|
|
1350
|
+
return 'a tar -C directory too long to judge (more than 4,096 characters)';
|
|
1351
|
+
// Every directory in effect is judged as each `-C` value is.
|
|
1352
|
+
if (c.length > 1) {
|
|
1353
|
+
const hc = first(dirs.filter((d) => d !== null && d !== '' && d.length <= MAX_PATH).map((d) => wr({ text: d, known: true, glob: false })));
|
|
1354
|
+
if (hc)
|
|
1355
|
+
return hc;
|
|
1356
|
+
}
|
|
1357
|
+
// Rule 49: by name first — it costs a split, and no path is judged for a member it refuses.
|
|
1358
|
+
if (removes || extracting) {
|
|
1359
|
+
const hn = first(members.map(namedLikeWiring));
|
|
1360
|
+
if (hn)
|
|
1361
|
+
return hn;
|
|
1362
|
+
}
|
|
1363
|
+
const each = (kind) => {
|
|
1364
|
+
// A path that passed is remembered for the whole command (rule 47), so a directory two readings share costs once.
|
|
1365
|
+
const once = (d, m) => (kind === 'del' ? inDir(d).del(m, true) : inDir(d).wr(m));
|
|
1366
|
+
for (const m of members) {
|
|
1367
|
+
const gnu = inEffect.has(m) ? inEffect.get(m) ?? null : null;
|
|
1368
|
+
const hit = once(beforeFirst, m) ?? (gnu === beforeFirst ? null : once(gnu, m));
|
|
1369
|
+
if (hit)
|
|
1370
|
+
return hit;
|
|
1371
|
+
}
|
|
1372
|
+
return null;
|
|
1373
|
+
};
|
|
1374
|
+
if (removes) {
|
|
1375
|
+
const hd = each('del');
|
|
1376
|
+
if (hd)
|
|
1377
|
+
return hd;
|
|
1378
|
+
}
|
|
1379
|
+
if (!extracting)
|
|
1380
|
+
continue;
|
|
1381
|
+
// No member named: the whole archive lands in the current directory (`cd .ax && tar -xf e.tar`).
|
|
1382
|
+
if (members.length === 0 && c.length === 0 && args[0]) {
|
|
1383
|
+
const hi = wr({ ...args[0], text: '.', known: true });
|
|
1384
|
+
if (hi)
|
|
1385
|
+
return hi;
|
|
1386
|
+
}
|
|
1387
|
+
const hw = each('wr');
|
|
1388
|
+
if (hw)
|
|
1389
|
+
return hw;
|
|
1390
|
+
}
|
|
1391
|
+
return null;
|
|
1392
|
+
}
|
|
1393
|
+
if (verb === 'unzip') {
|
|
1394
|
+
const d = optValues(args, 'd', /^--directory$/);
|
|
1395
|
+
const h = first(d.map((o) => wr(o.value)));
|
|
1396
|
+
if (h)
|
|
1397
|
+
return h;
|
|
1398
|
+
const members = positionalsOf(args, d.flatMap((o) => o.consumed)).slice(1);
|
|
1399
|
+
// Rule 48: the directory is resolved once and the members judged until the first refusal (40,000 members resolved a
|
|
1400
|
+
// 4 KB directory each — measured).
|
|
1401
|
+
const into = at(joinBase(base, d[0]?.value.text ?? '.'));
|
|
1402
|
+
for (const m of members) {
|
|
1403
|
+
const hit = into.wr(m);
|
|
1404
|
+
if (hit !== null)
|
|
1405
|
+
return hit;
|
|
1406
|
+
}
|
|
1407
|
+
return null;
|
|
1408
|
+
}
|
|
1409
|
+
if (verb === '7z' || verb === '7za' || verb === '7zz') {
|
|
1410
|
+
const o = args.find((a) => /^-o./.test(a.text));
|
|
1411
|
+
return o ? wr({ ...o, text: o.text.slice(2) }) : null;
|
|
1412
|
+
}
|
|
1413
|
+
if (verb === 'git') {
|
|
1414
|
+
// git's own base: every `-C DIR` (cumulative) and `--work-tree=DIR` move where pathspecs land.
|
|
1415
|
+
// @implements A-SPEC-728 (rule 44) — the `-C` values are collected and resolved once: rule 43 made the base unknown as soon
|
|
1416
|
+
// as it passed 4,096 characters on the way (17 hops through 253-character directories, 17 `-C ..`, `-C .ax rm -r specs`
|
|
1417
|
+
// passed — regression), and resolving at every hop was quadratic (52,000 `-C a`). Only the final base is bounded.
|
|
1418
|
+
let from = base;
|
|
1419
|
+
let hops = [];
|
|
1420
|
+
let workTree = null;
|
|
1421
|
+
let i = 0;
|
|
1422
|
+
for (; i < args.length; i++) {
|
|
1423
|
+
const t = args[i].text;
|
|
1424
|
+
if (t === '-C' && args[i + 1]) {
|
|
1425
|
+
const v = args[i + 1];
|
|
1426
|
+
// Rule 45: git leaves the directory unchanged for an empty value (git(1)); joined with `/` it made the base absolute
|
|
1427
|
+
// (`git -C "" -C .ax rm -r specs` was judged at `/.ax` and passed — regression of rule 44).
|
|
1428
|
+
if (!v.known) {
|
|
1429
|
+
from = null;
|
|
1430
|
+
hops = [];
|
|
1431
|
+
}
|
|
1432
|
+
else if (v.text === '') { /* unchanged */ }
|
|
1433
|
+
else if (absolute(v.text)) {
|
|
1434
|
+
from = v.text;
|
|
1435
|
+
hops = [];
|
|
1436
|
+
}
|
|
1437
|
+
else
|
|
1438
|
+
hops.push(v.text);
|
|
1439
|
+
i++;
|
|
1440
|
+
continue;
|
|
1441
|
+
}
|
|
1442
|
+
// Rule 46: the work tree does not replace the `-C` directory — pathspecs stay relative to where git runs (`git -C sub
|
|
1443
|
+
// --work-tree=.. rm ../.mcp.json` removed the root file — git 2.54, measured). Both are judged.
|
|
1444
|
+
if (t.startsWith('--work-tree=')) {
|
|
1445
|
+
workTree = t.slice('--work-tree='.length);
|
|
1446
|
+
continue;
|
|
1447
|
+
}
|
|
1448
|
+
if (t === '--work-tree' && args[i + 1]) {
|
|
1449
|
+
workTree = args[i + 1].text;
|
|
1450
|
+
i++;
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
if (t === '-c' || t === '--git-dir' || t === '--namespace') {
|
|
1454
|
+
i++;
|
|
1455
|
+
continue;
|
|
1456
|
+
}
|
|
1457
|
+
if (t.startsWith('-'))
|
|
1458
|
+
continue;
|
|
1459
|
+
break;
|
|
1460
|
+
}
|
|
1461
|
+
const gitBase = from === null || hops.length === 0 ? from : path.resolve(from, hops.join('/'));
|
|
1462
|
+
const sub = args[i]?.text;
|
|
1463
|
+
const rest = args.slice(i + 1);
|
|
1464
|
+
// Rule 46: the roles are read from the words as written; identical words are dropped within a role only (`git mv -k other
|
|
1465
|
+
// proj other` lost `proj` as a source when the list was de-duplicated first — measured).
|
|
1466
|
+
const written = positionalsOf(rest);
|
|
1467
|
+
const specs = distinctWords(written);
|
|
1468
|
+
// Rule 44: a base past 4,096 characters is refused where pathspecs land in it, not read as unknown — unknown judged them by
|
|
1469
|
+
// name only (`git -C .ax/specs/<long> rm -r x`) — and every pathspec resolved at it cost its length.
|
|
1470
|
+
if (gitBase !== null && gitBase.length > 4096 && /^(?:rm|mv|checkout|restore|checkout-index|update-index|clean|stash)$/.test(sub ?? '')) {
|
|
1471
|
+
return 'a git -C directory too long to judge (more than 4,096 characters)';
|
|
1472
|
+
}
|
|
1473
|
+
// The `-C` directory and the work tree resolved from it, each distinct directory once (a third base, the work tree from the
|
|
1474
|
+
// segment's own directory, is where git never runs: three bases took 4.4 s for 51,000 pathspecs — measured).
|
|
1475
|
+
// Rule 47: where the `-C` directory is unknowable the work tree is resolved from the segment's own directory, as before
|
|
1476
|
+
// rule 46 (`git -C "$(git rev-parse --show-toplevel)" --work-tree=.ax rm -r specs` passed — regression).
|
|
1477
|
+
const gitBases = workTree === null ? [gitBase]
|
|
1478
|
+
: [...new Set([gitBase, joinBase(gitBase ?? base, workTree)].map((b) => (b === null ? null : path.resolve(b))))];
|
|
1479
|
+
if (gitBases.some((b) => b !== null && b.length > 4096) && /^(?:rm|mv|checkout|restore|checkout-index|update-index|clean|stash)$/.test(sub ?? '')) {
|
|
1480
|
+
return 'a git -C directory too long to judge (more than 4,096 characters)';
|
|
1481
|
+
}
|
|
1482
|
+
const gs = gitBases.map((b) => at(b));
|
|
1483
|
+
const firstOf = (ask) => { for (const x of gs) {
|
|
1484
|
+
const hit = ask(x);
|
|
1485
|
+
if (hit !== null)
|
|
1486
|
+
return hit;
|
|
1487
|
+
} return null; };
|
|
1488
|
+
const g = { del: (w, r) => firstOf((x) => x.del(w, r)), wr: (w) => firstOf((x) => x.wr(w)) };
|
|
1489
|
+
const rflags = flagSet(rest);
|
|
1490
|
+
// `git rm --cached` touches only the index (round 4).
|
|
1491
|
+
if (sub === 'rm')
|
|
1492
|
+
return rflags.includes('--cached') ? null : first(specs.map((w) => g.del(w, true)));
|
|
1493
|
+
if (sub === 'mv') {
|
|
1494
|
+
const dest = written[written.length - 1];
|
|
1495
|
+
const srcs = distinctWords(written.slice(0, -1));
|
|
1496
|
+
// Judged in order and stopped at the first refusal (the list was built whole before `first` read it).
|
|
1497
|
+
for (const w of srcs) {
|
|
1498
|
+
const hit = g.del(w, true);
|
|
1499
|
+
if (hit !== null)
|
|
1500
|
+
return hit;
|
|
1501
|
+
}
|
|
1502
|
+
if (!dest)
|
|
1503
|
+
return null;
|
|
1504
|
+
const hd = g.wr(dest);
|
|
1505
|
+
if (hd !== null)
|
|
1506
|
+
return hd;
|
|
1507
|
+
for (const s of srcs) {
|
|
1508
|
+
const hit = g.wr({ ...dest, text: `${(0, linear_match_1.trimTrailing)(dest.text, '/\\')}/${path.basename(s.text)}` });
|
|
1509
|
+
if (hit !== null)
|
|
1510
|
+
return hit;
|
|
1511
|
+
}
|
|
1512
|
+
return null;
|
|
1513
|
+
}
|
|
1514
|
+
// A patch lands wherever its headers say: refused when the command names a wiring file (round 4).
|
|
1515
|
+
if ((sub === 'apply' && !rflags.some((f) => /^--(?:check|stat|numstat|summary)$/.test(f))) || sub === 'am') {
|
|
1516
|
+
return mentionsWiring(fullText) ? `a wiring file named in a patch that git ${sub} applies` : null;
|
|
1517
|
+
}
|
|
1518
|
+
if (sub === 'restore' && rflags.some((f) => f === '--staged' || f === '-S') && !rflags.some((f) => f === '--worktree' || f === '-W'))
|
|
1519
|
+
return null;
|
|
1520
|
+
if (sub === 'checkout' || sub === 'restore' || sub === 'checkout-index')
|
|
1521
|
+
return first(specs.map(g.wr));
|
|
1522
|
+
if (sub === 'update-index') {
|
|
1523
|
+
const info = rest.filter((a) => /,/.test(a.text)).map((a) => ({ ...a, text: a.text.split(',').pop() }));
|
|
1524
|
+
return first([...info, ...specs].map(g.wr));
|
|
1525
|
+
}
|
|
1526
|
+
if (sub === 'clean' && rflags.some((f) => /^-[a-zA-Z]*[xX]/.test(f)))
|
|
1527
|
+
return first(gitBases.map((b) => g.del({ text: b ?? root, known: true, glob: false }, true)));
|
|
1528
|
+
if (sub === 'stash' && rflags.some((f) => f === '-a' || f === '--all'))
|
|
1529
|
+
return first(gitBases.map((b) => g.del({ text: b ?? root, known: true, glob: false }, true)));
|
|
1530
|
+
return null;
|
|
1531
|
+
}
|
|
1532
|
+
if (verb === 'find') {
|
|
1533
|
+
const texts = args.map((a) => a.text);
|
|
1534
|
+
const execAt = texts.findIndex((t) => t === '-exec' || t === '-execdir' || t === '-ok' || t === '-okdir');
|
|
1535
|
+
let mutates = texts.includes('-delete');
|
|
1536
|
+
if (execAt >= 0) {
|
|
1537
|
+
const end = texts.findIndex((t, i) => i > execAt && (t === ';' || t === '+' || t === '\\;'));
|
|
1538
|
+
const inner = commandOf(args.slice(execAt + 1, end < 0 ? undefined : end));
|
|
1539
|
+
const iv = inner[0] ? verbName(inner[0].text) : '';
|
|
1540
|
+
mutates = mutates || (iv !== '' && !READ_VERBS.has(iv));
|
|
1541
|
+
// The exec'd command is a command: its own fixed arguments are judged (`-exec rm .mcp.json \;`).
|
|
1542
|
+
const own = judge(inner.filter((w) => w.text !== '{}'), nest + 1);
|
|
1543
|
+
if (own)
|
|
1544
|
+
return own;
|
|
1545
|
+
}
|
|
1546
|
+
if (!mutates)
|
|
1547
|
+
return null;
|
|
1548
|
+
const filters = args.filter((_, i) => i > 0 && /^-(?:i?name|i?path|wholename|iwholename|regex|iregex)$/.test(texts[i - 1])).map((a) => a.text);
|
|
1549
|
+
if (filters.length > 0) {
|
|
1550
|
+
const hit = filters.some((f) => {
|
|
1551
|
+
const re = (0, write_target_1.globPatternRe)(path.basename(f), true);
|
|
1552
|
+
return write_target_1.WIRING_FILE_NAMES.some((n) => re.test(n)) || write_target_1.WIRING_DIR_NAMES.some((d) => re.test(d));
|
|
1553
|
+
});
|
|
1554
|
+
return hit ? `a wiring file matched by find ${filters.join(' ')}` : null;
|
|
1555
|
+
}
|
|
1556
|
+
const startEnd = texts.findIndex((t) => /^[-(!]/.test(t));
|
|
1557
|
+
const starts = args.slice(0, startEnd < 0 ? args.length : startEnd);
|
|
1558
|
+
return first((starts.length > 0 ? starts : [{ text: '.', known: true, glob: false }]).map((w) => del(w, true)));
|
|
1559
|
+
}
|
|
1560
|
+
if (verb === 'xargs' || verb === 'parallel') {
|
|
1561
|
+
const inner = commandOf(positionalsOf(args.filter((a, i) => !(i > 0 && /^-[IiLlnPdEsaj]$/.test(args[i - 1].text)))));
|
|
1562
|
+
const iv = inner[0] ? verbName(inner[0].text) : '';
|
|
1563
|
+
if (iv === '' || READ_VERBS.has(iv))
|
|
1564
|
+
return null;
|
|
1565
|
+
// Round 7: whole-command facts are computed once per text, not once per segment (quadratic).
|
|
1566
|
+
const unfiltered = perText('unfiltered', fullText, (t) => findRunsUnfiltered(t) || /\bls\s+(?:-\w*[aA])/.test(t)
|
|
1567
|
+
// `git ls-files` with no pathspec lists every tracked file, wiring included.
|
|
1568
|
+
|| /\bgit\s+ls-files((?:\s+-[\w-]+)*)\s*(?:$|[|;&])/m.test(t));
|
|
1569
|
+
return mentionsWiring(fullText) || unfiltered ? `a wiring file reachable by a command that ${verb} runs (${iv})` : null;
|
|
1570
|
+
}
|
|
1571
|
+
// Unknown program: an argument that IS a wiring file may be written by it.
|
|
1572
|
+
const argPaths = args.map((a) => { const eq = /^--?[\w-]+=(.+)$/.exec(a.text); return eq ? { ...a, text: eq[1] } : a; });
|
|
1573
|
+
return first(argPaths.filter((a) => !a.text.startsWith('-')).map((a) => {
|
|
1574
|
+
if (!judgeable(a))
|
|
1575
|
+
return null;
|
|
1576
|
+
const k = wr(a);
|
|
1577
|
+
return k !== null && !isDirectoryKind(k) ? k : null;
|
|
1578
|
+
}));
|
|
1579
|
+
};
|
|
1580
|
+
// What RUNS inside `$(…)`, backticks and process substitutions is a command too (round 3).
|
|
1581
|
+
for (const sub of subs) {
|
|
1582
|
+
const h = recurse(sub, base);
|
|
1583
|
+
if (h)
|
|
1584
|
+
return h;
|
|
1585
|
+
}
|
|
1586
|
+
for (const cmd of commands) {
|
|
1587
|
+
const hit = first(cmd.redirects.map(wr)) ?? judge(commandOf(cmd.words));
|
|
1588
|
+
if (hit)
|
|
1589
|
+
return hit;
|
|
1590
|
+
}
|
|
1591
|
+
return null;
|
|
1592
|
+
}
|
|
1593
|
+
/**
|
|
1594
|
+
* @implements A-SPEC-728
|
|
1595
|
+
* The rule over a whole command: every segment (data heredoc bodies included — a `<<` inside quotes
|
|
1596
|
+
* or a comment must not hide the lines after it) at its effective base.
|
|
1597
|
+
*/
|
|
1598
|
+
function harnessWiringActOfCommand(root, command, env = process.env, home = os.homedir(), startBase = root, depth = 0, baseUnknown = false) {
|
|
1599
|
+
// Never throw, never judge from a half-finished read (round 3): a throw or a limit refuses only a
|
|
1600
|
+
// command that names a wiring target, and lets any other through — a hook error must not fail
|
|
1601
|
+
// open for the wiring, nor block ordinary work.
|
|
1602
|
+
// Beyond MAX_COMMAND a command is not parsed: refused when it names a wiring target or holds a
|
|
1603
|
+
// character that can spell one indirectly (quoting, expansion, globbing), else passed (round 4).
|
|
1604
|
+
if (command.length > exports.MAX_COMMAND) {
|
|
1605
|
+
return mentionsWiring(command) || /[$\\`'"{*?[]/.test(command) ? 'a command too large to judge that could name a wiring file' : null;
|
|
1606
|
+
}
|
|
1607
|
+
const run = () => {
|
|
1608
|
+
const loop = forLoopAct(root, command, env, home, startBase, depth, baseUnknown);
|
|
1609
|
+
if (loop)
|
|
1610
|
+
return loop;
|
|
1611
|
+
// @implements A-SPEC-728 (round 14) — a null-base segment is judged once more at each base it MAY run
|
|
1612
|
+
// in under `.ax` (rule 17's candidates, keyed by the top-level child): `npm test; cd .ax; rm
|
|
1613
|
+
// "ledger/provenance.jsonl"` deleted the ledger while the identity rule judged the quoted word by
|
|
1614
|
+
// wiring name only (measured). The shared candidate array is keyed once per run.
|
|
1615
|
+
const keyed = new Map();
|
|
1616
|
+
// Rule 44: each candidate is placed once per judgement, whichever list carries it.
|
|
1617
|
+
// Rule 45: and whichever nested program asks (a quoted command is a judgement of its own).
|
|
1618
|
+
const placed = placedShared ?? new Map();
|
|
1619
|
+
const maybeBases = (maybe, text) => {
|
|
1620
|
+
let k = keyed.get(maybe);
|
|
1621
|
+
if (k === undefined) {
|
|
1622
|
+
k = (0, write_target_1.axCandidateKeys)(root, maybe, Infinity, placed);
|
|
1623
|
+
keyed.set(maybe, k);
|
|
1624
|
+
}
|
|
1625
|
+
const climbs = (0, write_target_1.axClimbBases)(root, k, text);
|
|
1626
|
+
return climbs.length > write_target_1.AX_CLIMB_BASES_MAX ? null : [...k.bases, ...climbs];
|
|
1627
|
+
};
|
|
1628
|
+
for (const seg of (0, write_target_1.shellSegments)(command, startBase, 0, { programs: false })) {
|
|
1629
|
+
// A heredoc body is a command only when a shell reads it (shellSegments already recursed those),
|
|
1630
|
+
// and a program only when an interpreter does; prose fed to cat/git/gh is data.
|
|
1631
|
+
if (seg.data && !seg.interp)
|
|
1632
|
+
continue;
|
|
1633
|
+
const text = seg.interp ? `node -e ${JSON.stringify(seg.text)}` : seg.text;
|
|
1634
|
+
const act = harnessWiringActOf(root, baseUnknown ? null : seg.base, text, command, env, home, depth);
|
|
1635
|
+
if (act)
|
|
1636
|
+
return act;
|
|
1637
|
+
if ((baseUnknown || seg.base === null) && seg.maybe !== undefined) {
|
|
1638
|
+
const bases = maybeBases(seg.maybe, text);
|
|
1639
|
+
if (bases === null)
|
|
1640
|
+
return `a command too large to judge at every directory it may run in under .ax (more than ${write_target_1.AX_CLIMB_BASES_MAX} climbs)`;
|
|
1641
|
+
for (const b of bases) {
|
|
1642
|
+
const at = harnessWiringActOf(root, b, text, command, env, home, depth);
|
|
1643
|
+
if (at)
|
|
1644
|
+
return at;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
return null;
|
|
1649
|
+
};
|
|
1650
|
+
try {
|
|
1651
|
+
if (depth === 0) {
|
|
1652
|
+
loopBudget = { judged: 0, bytes: 0 };
|
|
1653
|
+
callScan = { left: CALL_SCAN_MAX };
|
|
1654
|
+
quotedJudged = new Set();
|
|
1655
|
+
placedShared = new Map();
|
|
1656
|
+
pathsJudged = { passed: new Map(), fresh: 0, chars: 0 };
|
|
1657
|
+
}
|
|
1658
|
+
return depth === 0 ? (0, write_target_1.withIdentCache)(run) : run();
|
|
1659
|
+
}
|
|
1660
|
+
catch {
|
|
1661
|
+
return mentionsWiring(command) ? 'a wiring file named in a command too complex to judge' : null;
|
|
1662
|
+
}
|
|
1663
|
+
finally {
|
|
1664
|
+
if (depth === 0) {
|
|
1665
|
+
callScan = null;
|
|
1666
|
+
quotedJudged = null;
|
|
1667
|
+
placedShared = null;
|
|
1668
|
+
pathsJudged = null;
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* @implements A-SPEC-728 (round 4) — `for V in LIST; do …; done`: the body runs once per LIST word,
|
|
1674
|
+
* so it is judged with each word in place of `$V` (bounded: 64 words, 16 loops).
|
|
1675
|
+
* @implements A-SPEC-728 (rule 37) — and bounded once for the whole command, not per level: the per-loop bounds
|
|
1676
|
+
* multiplied (four nested 64-word loops, 1 KB, took the 20 s deadline; sixteen 8 KB bodies 7 s — measured). Past
|
|
1677
|
+
* 32,768 substituted judgements or 512 KB of substituted text the command is refused as too large to judge: a
|
|
1678
|
+
* word a loop assembles (`$a$b`) is visible only by substituting, so no reading by name can stand in for it.
|
|
1679
|
+
*/
|
|
1680
|
+
const LOOP_JUDGED_MAX = 32_768;
|
|
1681
|
+
const LOOP_BYTES_MAX = 512 * 1024;
|
|
1682
|
+
let loopBudget = { judged: 0, bytes: 0 };
|
|
1683
|
+
// The running command's call-argument budget (null outside `harnessWiringActOfCommand`: a direct call has its own).
|
|
1684
|
+
let callScan = null;
|
|
1685
|
+
/** @implements A-SPEC-728 (rule 45) — the running judgement's quoted commands already judged, and its candidate placements. */
|
|
1686
|
+
const QUOTED_COMMANDS_MAX = 4096;
|
|
1687
|
+
let quotedJudged = null;
|
|
1688
|
+
let placedShared = null;
|
|
1689
|
+
/** @implements A-SPEC-728 (rule 47) — the running judgement's paths that passed, by directory, and how many it has judged. */
|
|
1690
|
+
const PATHS_JUDGED_MAX = 65_536;
|
|
1691
|
+
const PATHS_CHARS_MAX = 16 * 1024 * 1024;
|
|
1692
|
+
let pathsJudged = null;
|
|
1693
|
+
const FOR_RE = /(?:^|[\s;&|(])for[ \t]+([A-Za-z_]\w{0,63})[ \t]+in[ \t]+([^;\n]{0,1024})[;\n]\s*do\b/g;
|
|
1694
|
+
function forLoopAct(root, command, env, home, startBase, depth, baseUnknown) {
|
|
1695
|
+
if (depth > 4)
|
|
1696
|
+
return null;
|
|
1697
|
+
let loops = 0;
|
|
1698
|
+
for (const m of command.matchAll(FOR_RE)) {
|
|
1699
|
+
if (++loops > 16)
|
|
1700
|
+
break;
|
|
1701
|
+
const bodyStart = (m.index ?? 0) + m[0].length;
|
|
1702
|
+
const end = command.indexOf('done', bodyStart);
|
|
1703
|
+
const body = command.slice(bodyStart, end < 0 ? Math.min(command.length, bodyStart + 8192) : Math.min(end, bodyStart + 8192));
|
|
1704
|
+
const v = new RegExp(`"?\\$(?:\\{${m[1]}\\}|${m[1]}(?!\\w))"?`, 'g');
|
|
1705
|
+
if (!v.test(body))
|
|
1706
|
+
continue;
|
|
1707
|
+
const words = shellWords(m[2], env, home, null).words.slice(0, 64);
|
|
1708
|
+
for (const w of words) {
|
|
1709
|
+
const text = w.known ? w.text : '"$__unknown__"';
|
|
1710
|
+
if (++loopBudget.judged > LOOP_JUDGED_MAX || (loopBudget.bytes += body.length) > LOOP_BYTES_MAX)
|
|
1711
|
+
return 'a loop too large to judge that could name a wiring file';
|
|
1712
|
+
const h = harnessWiringActOfCommand(root, body.replace(v, () => text.replace(/[\s;&|]/g, '\\$&')), env, home, startBase, depth + 1, baseUnknown);
|
|
1713
|
+
if (h)
|
|
1714
|
+
return h;
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
return null;
|
|
1718
|
+
}
|
|
1719
|
+
/** Back-compat name for the shared translator (find semantics: dotfiles match). */
|
|
1720
|
+
function globToRe(glob) { return (0, write_target_1.globPatternRe)(glob, true); }
|