@holmes-lab/holmes-kit 0.34.0 → 0.35.1
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 +89 -0
- package/README.md +6 -0
- package/dist/.build-id +1 -1
- package/dist/holmes/cli/codex-oracle.js +36 -2
- package/dist/holmes/cli/doctor.d.ts +14 -1
- package/dist/holmes/cli/doctor.js +335 -71
- package/dist/holmes/cli/index.d.ts +1 -1
- package/dist/holmes/cli/index.js +38 -5
- package/dist/holmes/cli/runtime-binding-approval.d.ts +7 -0
- package/dist/holmes/cli/runtime-binding-approval.js +102 -0
- package/dist/holmes/cli/runtime-binding.d.ts +29 -0
- package/dist/holmes/cli/runtime-binding.js +570 -0
- package/dist/holmes/governance/approval-queue.d.ts +2 -0
- package/dist/holmes/governance/approval-queue.js +11 -4
- package/dist/holmes/guardrail/impact-gate.js +90 -5
- package/dist/holmes/guardrail/linear-match.js +8 -1
- package/dist/holmes/guardrail/risk-classifier.d.ts +6 -0
- package/dist/holmes/guardrail/risk-classifier.js +68 -23
- package/dist/holmes/guardrail/shell-argument-roles.d.ts +15 -0
- package/dist/holmes/guardrail/shell-argument-roles.js +209 -0
- package/dist/holmes/guardrail/shell-syntax.d.ts +20 -0
- package/dist/holmes/guardrail/shell-syntax.js +489 -0
- package/dist/holmes/guardrail/shell-words.d.ts +2 -18
- package/dist/holmes/guardrail/shell-words.js +67 -442
- package/dist/holmes/guardrail/write-target.d.ts +54 -0
- package/dist/holmes/guardrail/write-target.js +327 -1
- package/dist/holmes/hooks/pre-tool-use.d.ts +12 -0
- package/dist/holmes/hooks/pre-tool-use.js +377 -66
- package/dist/holmes/mcp/handlers/maintenance-analysis.js +4 -2
- package/dist/holmes/mcp/handlers/test-execution.js +6 -2
- package/dist/holmes/mcp/handlers.d.ts +9 -1
- package/dist/holmes/mcp/handlers.js +6 -4
- package/dist/holmes/mcp/maintenance-evidence.d.ts +16 -1
- package/dist/holmes/mcp/maintenance-evidence.js +29 -3
- package/dist/holmes/mcp/operator-recovery.d.ts +9 -0
- package/dist/holmes/mcp/operator-recovery.js +63 -0
- package/dist/holmes/rtm/test-scope.d.ts +9 -0
- package/dist/holmes/rtm/test-scope.js +284 -14
- package/dist/holmes/runtime/binding.d.ts +30 -0
- package/dist/holmes/runtime/binding.js +164 -0
- package/dist/holmes/runtime/dispatch.d.ts +9 -0
- package/dist/holmes/runtime/dispatch.js +169 -0
- package/dist/holmes/runtime/hook-process.d.ts +11 -0
- package/dist/holmes/runtime/hook-process.js +130 -0
- package/docs/install-guide.md +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,489 @@
|
|
|
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.shellWords = shellWords;
|
|
37
|
+
exports.commandOf = commandOf;
|
|
38
|
+
// @implements A-SPEC-753
|
|
39
|
+
// Policy-independent shell lexical primitives; legacy exports remain in shell-words.
|
|
40
|
+
const os = __importStar(require("node:os"));
|
|
41
|
+
const NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*/;
|
|
42
|
+
/**
|
|
43
|
+
* `$NAME` / `${NAME[:-…]}` at `src[i]` → [value | null(unknown), length consumed, what an unknown
|
|
44
|
+
* variable's default spells]. `${X:-word}` with X unknowable may expand to `word` (round 4).
|
|
45
|
+
*/
|
|
46
|
+
function readVar(src, i, env, home, pwd) {
|
|
47
|
+
const known = (name) => name === 'HOME' ? home : name === 'CODEX_HOME' && env.CODEX_HOME ? env.CODEX_HOME : name === 'PWD' ? pwd : null;
|
|
48
|
+
if (src[i + 1] === '{') {
|
|
49
|
+
const close = src.indexOf('}', i + 2);
|
|
50
|
+
if (close < 0)
|
|
51
|
+
return [null, src.length - i];
|
|
52
|
+
const inner = src.slice(i + 2, close);
|
|
53
|
+
const name = NAME_RE.exec(inner)?.[0] ?? '';
|
|
54
|
+
const rest = inner.slice(name.length);
|
|
55
|
+
// `${HOME}`, `${HOME:-x}`, `${HOME:?}`, `${HOME=…}`: the value is HOME's whenever HOME is set.
|
|
56
|
+
const v = /^(?::?[-=?].*)?$/s.test(rest) ? known(name) : null;
|
|
57
|
+
const dflt = /^:?[-=](.*)$/s.exec(rest)?.[1];
|
|
58
|
+
return [v, close + 1 - i, v === null && dflt ? dflt.replace(/^(['"])(.*)\1$/s, '$2') : undefined];
|
|
59
|
+
}
|
|
60
|
+
// Linear: the name is read in place, never from a copy of the rest of the text (round 4).
|
|
61
|
+
NAME_Y.lastIndex = i + 1;
|
|
62
|
+
const name = NAME_Y.exec(src)?.[0];
|
|
63
|
+
if (!name)
|
|
64
|
+
return [null, 2];
|
|
65
|
+
return [known(name), 1 + name.length];
|
|
66
|
+
}
|
|
67
|
+
const NAME_Y = /[A-Za-z_][A-Za-z0-9_]*/y;
|
|
68
|
+
/** `$'…'` (ANSI-C quoting) starting at `src[i]` (the `$`) → [decoded text, index of the closing quote]. */
|
|
69
|
+
function readAnsiC(src, i) {
|
|
70
|
+
let out = '';
|
|
71
|
+
let j = i + 2;
|
|
72
|
+
const simple = { n: '\n', t: '\t', r: '\r', a: '\x07', b: '\b', e: '\x1b', E: '\x1b', f: '\f', v: '\v', '\\': '\\', "'": "'", '"': '"', '?': '?' };
|
|
73
|
+
for (; j < src.length && src[j] !== "'"; j++) {
|
|
74
|
+
if (src[j] !== '\\') {
|
|
75
|
+
out += src[j];
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const n = src[j + 1] ?? '';
|
|
79
|
+
// Sticky reads in place: a `.slice` per escape was quadratic on a long `$'…'` (round 4).
|
|
80
|
+
const at = (re) => { re.lastIndex = j + 1; return re.exec(src); };
|
|
81
|
+
const hex = at(HEX_Y);
|
|
82
|
+
const oct = at(OCT_Y);
|
|
83
|
+
const uni = at(UNI_Y);
|
|
84
|
+
if (hex) {
|
|
85
|
+
out += String.fromCharCode(parseInt(hex[1], 16));
|
|
86
|
+
j += hex[0].length;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (uni) {
|
|
90
|
+
const cp = parseInt(uni[1], 16);
|
|
91
|
+
out += cp <= 0x10ffff ? String.fromCodePoint(cp) : '\ufffd';
|
|
92
|
+
j += uni[0].length;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (oct) {
|
|
96
|
+
out += String.fromCharCode(parseInt(oct[1], 8));
|
|
97
|
+
j += oct[0].length;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (n in simple) {
|
|
101
|
+
out += simple[n];
|
|
102
|
+
j++;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
out += n;
|
|
106
|
+
j++;
|
|
107
|
+
}
|
|
108
|
+
return [out, j];
|
|
109
|
+
}
|
|
110
|
+
const HEX_Y = /x([0-9a-fA-F]{1,2})/y;
|
|
111
|
+
const OCT_Y = /([0-7]{1,3})/y;
|
|
112
|
+
const UNI_Y = /[uU]([0-9a-fA-F]{1,8})/y;
|
|
113
|
+
/** Index of the paren balancing the `(` at `src[i]`, or the end. */
|
|
114
|
+
function balance(src, i) {
|
|
115
|
+
let d = 0;
|
|
116
|
+
for (let j = i; j < src.length; j++) {
|
|
117
|
+
if (src[j] === '(')
|
|
118
|
+
d++;
|
|
119
|
+
else if (src[j] === ')' && --d === 0)
|
|
120
|
+
return j;
|
|
121
|
+
}
|
|
122
|
+
return src.length - 1;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Expand unquoted `{a,b}` lists (bounded), keeping quoted braces literal.
|
|
126
|
+
* @implements A-SPEC-728 (round 8) — the total characters produced are bounded too (1 MB per segment):
|
|
127
|
+
* counting alternatives alone let `{a,b}` × n copy the whole word at every level until a 4 GB heap
|
|
128
|
+
* aborted the hook with no verdict (measured). Past the bound the word stays literal and is marked
|
|
129
|
+
* unknowable (`overflow`), the conservative reading.
|
|
130
|
+
*/
|
|
131
|
+
const BRACE_CHAR_BUDGET = 1024 * 1024;
|
|
132
|
+
function expandBraces(chars, budget = { n: 64 }) {
|
|
133
|
+
if (budget.chars === undefined)
|
|
134
|
+
budget.chars = BRACE_CHAR_BUDGET;
|
|
135
|
+
let depth = 0, open = -1;
|
|
136
|
+
for (let i = 0; i < chars.length; i++) {
|
|
137
|
+
const { c, q } = chars[i];
|
|
138
|
+
if (q)
|
|
139
|
+
continue;
|
|
140
|
+
if (c === '{') {
|
|
141
|
+
if (depth === 0)
|
|
142
|
+
open = i;
|
|
143
|
+
depth++;
|
|
144
|
+
}
|
|
145
|
+
else if (c === '}' && depth > 0) {
|
|
146
|
+
depth--;
|
|
147
|
+
if (depth === 0 && open >= 0) {
|
|
148
|
+
const inner = chars.slice(open + 1, i);
|
|
149
|
+
const alts = [];
|
|
150
|
+
let d = 0, start = 0;
|
|
151
|
+
for (let j = 0; j < inner.length; j++) {
|
|
152
|
+
if (inner[j].q)
|
|
153
|
+
continue;
|
|
154
|
+
if (inner[j].c === '{')
|
|
155
|
+
d++;
|
|
156
|
+
else if (inner[j].c === '}')
|
|
157
|
+
d--;
|
|
158
|
+
else if (inner[j].c === ',' && d === 0) {
|
|
159
|
+
alts.push(inner.slice(start, j));
|
|
160
|
+
start = j + 1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (alts.length === 0) {
|
|
164
|
+
open = -1;
|
|
165
|
+
continue;
|
|
166
|
+
} // `{x}` is literal
|
|
167
|
+
alts.push(inner.slice(start));
|
|
168
|
+
const out = [];
|
|
169
|
+
for (const a of alts) {
|
|
170
|
+
if (budget.n-- <= 0)
|
|
171
|
+
break;
|
|
172
|
+
const size = open + a.length + (chars.length - i - 1);
|
|
173
|
+
if ((budget.chars -= size) < 0) {
|
|
174
|
+
budget.overflow = true;
|
|
175
|
+
return [chars];
|
|
176
|
+
}
|
|
177
|
+
out.push(...expandBraces([...chars.slice(0, open), ...a, ...chars.slice(i + 1)], budget));
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return [chars];
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The simple commands of one segment, as the shell would split and expand them. Operators split
|
|
187
|
+
* commands (`;`, `&`, `|`, newline, `(`, `)` at a word start); redirect targets are collected per
|
|
188
|
+
* command. `pwd` is the segment's working directory (for `~+` and `$PWD`), null when unknowable.
|
|
189
|
+
*/
|
|
190
|
+
let USERNAME;
|
|
191
|
+
function shellWords(src, env = process.env, home = os.homedir(), pwd = null, sourceRanges = false) {
|
|
192
|
+
// @implements A-SPEC-728 (rule 42) — read once per process: it ran for every segment (13% of a 256 KB command — measured).
|
|
193
|
+
const me = USERNAME ?? (USERNAME = (() => { try {
|
|
194
|
+
return os.userInfo().username;
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return '';
|
|
198
|
+
} })());
|
|
199
|
+
const commands = [];
|
|
200
|
+
let cur = { words: [], redirects: [] };
|
|
201
|
+
let chars = [];
|
|
202
|
+
let inWord = false, known = true;
|
|
203
|
+
let offset = 0, wordStart = 0;
|
|
204
|
+
let pending = null;
|
|
205
|
+
const subs = [];
|
|
206
|
+
const budget = { n: 4096 }; // brace expansions per segment; beyond it the segment is too large to judge
|
|
207
|
+
const finishWord = () => {
|
|
208
|
+
if (!inWord)
|
|
209
|
+
return;
|
|
210
|
+
let raw = chars;
|
|
211
|
+
// Tilde at the start of an unquoted word: `~`, `~/…`, `~<me>`, `~+` (the working directory).
|
|
212
|
+
if (raw.length > 0 && raw[0].c === '~' && !raw[0].q) {
|
|
213
|
+
const slash = raw.findIndex((x) => x.c === '/');
|
|
214
|
+
const user = raw.slice(1, slash < 0 ? raw.length : slash).map((x) => x.c).join('');
|
|
215
|
+
const to = user === '' || user === me ? home : user === '+' ? pwd : null;
|
|
216
|
+
if (to !== null)
|
|
217
|
+
raw = [...[...to].map((c) => ({ c, q: true })), ...(slash < 0 ? [] : raw.slice(slash))];
|
|
218
|
+
else
|
|
219
|
+
known = false;
|
|
220
|
+
}
|
|
221
|
+
const expanded = expandBraces(raw, budget);
|
|
222
|
+
if (budget.overflow)
|
|
223
|
+
known = false;
|
|
224
|
+
for (const w of expanded) {
|
|
225
|
+
const full = w.map((x) => x.c).join('');
|
|
226
|
+
const nul = full.indexOf('\0'); // an argument ends at NUL: `.mcp.json\0x` is `.mcp.json`
|
|
227
|
+
const word = { text: nul >= 0 ? full.slice(0, nul) : full, known, glob: w.some((x) => !x.q && /[*?[(]/.test(x.c)) };
|
|
228
|
+
if (sourceRanges) {
|
|
229
|
+
word.sourceStart = wordStart;
|
|
230
|
+
word.sourceEnd = offset;
|
|
231
|
+
}
|
|
232
|
+
if (pending === 'write')
|
|
233
|
+
cur.redirects.push(word);
|
|
234
|
+
else if (pending === null)
|
|
235
|
+
cur.words.push(word);
|
|
236
|
+
}
|
|
237
|
+
pending = null;
|
|
238
|
+
chars = [];
|
|
239
|
+
inWord = false;
|
|
240
|
+
known = true;
|
|
241
|
+
};
|
|
242
|
+
const endCommand = () => {
|
|
243
|
+
finishWord();
|
|
244
|
+
if (cur.words.length > 0 || cur.redirects.length > 0)
|
|
245
|
+
commands.push(cur);
|
|
246
|
+
cur = { words: [], redirects: [] };
|
|
247
|
+
};
|
|
248
|
+
const push = (c, q) => { chars.push({ c, q }); inWord = true; };
|
|
249
|
+
const pushText = (t) => { for (const c of t)
|
|
250
|
+
push(c, true); };
|
|
251
|
+
for (let i = 0; i < src.length; i++) {
|
|
252
|
+
offset = i;
|
|
253
|
+
if (!inWord)
|
|
254
|
+
wordStart = i;
|
|
255
|
+
const c = src[i];
|
|
256
|
+
if (c === '\\') {
|
|
257
|
+
const n = src[i + 1];
|
|
258
|
+
if (n === '\n') {
|
|
259
|
+
i++;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (n !== undefined) {
|
|
263
|
+
push(n, true);
|
|
264
|
+
i++;
|
|
265
|
+
}
|
|
266
|
+
else if (sourceRanges)
|
|
267
|
+
known = false;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (c === '$' && src[i + 1] === "'") {
|
|
271
|
+
const [t, end] = readAnsiC(src, i);
|
|
272
|
+
if (sourceRanges && end >= src.length)
|
|
273
|
+
known = false;
|
|
274
|
+
inWord = true;
|
|
275
|
+
pushText(t);
|
|
276
|
+
i = end;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (c === "'") {
|
|
280
|
+
const close = src.indexOf("'", i + 1);
|
|
281
|
+
const end = close < 0 ? src.length : close;
|
|
282
|
+
if (sourceRanges && close < 0)
|
|
283
|
+
known = false;
|
|
284
|
+
inWord = true;
|
|
285
|
+
pushText(src.slice(i + 1, end));
|
|
286
|
+
i = end;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (c === '"') {
|
|
290
|
+
inWord = true;
|
|
291
|
+
let j = i + 1;
|
|
292
|
+
for (; j < src.length && src[j] !== '"'; j++) {
|
|
293
|
+
if (src[j] === '\\' && /["\\$`\n]/.test(src[j + 1] ?? '')) {
|
|
294
|
+
if (src[j + 1] !== '\n')
|
|
295
|
+
push(src[j + 1], true);
|
|
296
|
+
j++;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (src[j] === '$' && /[{A-Za-z_]/.test(src[j + 1] ?? '')) {
|
|
300
|
+
const [v, len, dflt] = readVar(src, j, env, home, pwd);
|
|
301
|
+
if (v === null) {
|
|
302
|
+
known = false;
|
|
303
|
+
if (dflt)
|
|
304
|
+
pushText(dflt);
|
|
305
|
+
}
|
|
306
|
+
else
|
|
307
|
+
pushText(v);
|
|
308
|
+
j += len - 1;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (sourceRanges && src[j] === '$' && /[0-9@*#?$!\-]/.test(src[j + 1] ?? ''))
|
|
312
|
+
known = false;
|
|
313
|
+
if (src[j] === '$' && src[j + 1] === '(') {
|
|
314
|
+
const end = balance(src, j + 1);
|
|
315
|
+
subs.push(src.slice(j + 2, end));
|
|
316
|
+
known = false;
|
|
317
|
+
j = end;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if (src[j] === '`') {
|
|
321
|
+
const close = src.indexOf('`', j + 1);
|
|
322
|
+
const end = close < 0 ? src.length : close;
|
|
323
|
+
subs.push(src.slice(j + 1, end));
|
|
324
|
+
known = false;
|
|
325
|
+
j = end;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
push(src[j], true);
|
|
329
|
+
}
|
|
330
|
+
if (sourceRanges && j >= src.length)
|
|
331
|
+
known = false;
|
|
332
|
+
i = j;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (c === '$' && src[i + 1] === '(') {
|
|
336
|
+
const end = balance(src, i + 1);
|
|
337
|
+
subs.push(src.slice(i + 2, end));
|
|
338
|
+
known = false;
|
|
339
|
+
inWord = true;
|
|
340
|
+
i = end;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (c === '`') {
|
|
344
|
+
const close = src.indexOf('`', i + 1);
|
|
345
|
+
const end = close < 0 ? src.length : close;
|
|
346
|
+
subs.push(src.slice(i + 1, end));
|
|
347
|
+
known = false;
|
|
348
|
+
inWord = true;
|
|
349
|
+
i = end;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (c === '$' && /[{A-Za-z_]/.test(src[i + 1] ?? '')) {
|
|
353
|
+
const [v, len, dflt] = readVar(src, i, env, home, pwd);
|
|
354
|
+
if (v === null) {
|
|
355
|
+
known = false;
|
|
356
|
+
inWord = true;
|
|
357
|
+
if (dflt)
|
|
358
|
+
pushText(dflt);
|
|
359
|
+
}
|
|
360
|
+
else
|
|
361
|
+
pushText(v);
|
|
362
|
+
i += len - 1;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (sourceRanges && c === '$' && /[0-9@*#?$!\-]/.test(src[i + 1] ?? ''))
|
|
366
|
+
known = false;
|
|
367
|
+
if (/\s/.test(c)) {
|
|
368
|
+
if (c === '\n')
|
|
369
|
+
endCommand();
|
|
370
|
+
else
|
|
371
|
+
finishWord();
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
// A `#` that starts a word starts a comment: the rest of the line is not run (round 4).
|
|
375
|
+
if (c === '#' && !inWord) {
|
|
376
|
+
const nl = src.indexOf('\n', i);
|
|
377
|
+
i = (nl < 0 ? src.length : nl) - 1;
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
// Process substitution `<(…)`/`>(…)`: one unknowable word; it does not end the command.
|
|
381
|
+
if ((c === '<' || c === '>') && src[i + 1] === '(' && !inWord) {
|
|
382
|
+
finishWord();
|
|
383
|
+
const end = balance(src, i + 1);
|
|
384
|
+
subs.push(src.slice(i + 2, end));
|
|
385
|
+
cur.words.push({ text: src.slice(i, end + 1), known: false, glob: false });
|
|
386
|
+
i = end;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
// Redirect operators: `N>`, `>`, `>>`, `>|`, `&>`, `&>>`, `<>`; `<`, `<<`, `<<<`, `>&N` read or dup.
|
|
390
|
+
if (c === '>' || (c === '&' && src[i + 1] === '>') || (c === '<')) {
|
|
391
|
+
const fdWord = inWord && chars.every((x) => /[0-9]/.test(x.c) && !x.q);
|
|
392
|
+
if (fdWord) {
|
|
393
|
+
chars = [];
|
|
394
|
+
inWord = false;
|
|
395
|
+
}
|
|
396
|
+
else
|
|
397
|
+
finishWord();
|
|
398
|
+
let op = c;
|
|
399
|
+
let j = i + 1;
|
|
400
|
+
while (j < src.length && /[<>&|!]/.test(src[j]) && op.length < 3) {
|
|
401
|
+
op += src[j];
|
|
402
|
+
j++;
|
|
403
|
+
}
|
|
404
|
+
i = j - 1;
|
|
405
|
+
if (/^&?>>?[|!]?$|^<>$/.test(op))
|
|
406
|
+
pending = 'write';
|
|
407
|
+
else if (/^>>?&$/.test(op)) {
|
|
408
|
+
// `>&N`/`>&-` duplicates a descriptor; `>&WORD` (bash) writes stdout+stderr to WORD.
|
|
409
|
+
let k = j;
|
|
410
|
+
while (k < src.length && /[ \t]/.test(src[k]))
|
|
411
|
+
k++;
|
|
412
|
+
pending = /[0-9-]/.test(src[k] ?? '') && !/[^\s;&|]/.test(src[k + 1] ?? ' ') ? 'skip' : 'write';
|
|
413
|
+
}
|
|
414
|
+
else
|
|
415
|
+
pending = 'skip'; // `<`, `<<`, `<<<`: input, delimiter or data
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
// An unquoted `(…)` INSIDE a word is a zsh glob group (`.(mcp|x).json`, `@(…)`), part of the word.
|
|
419
|
+
if (c === '(' && inWord) {
|
|
420
|
+
const end = balance(src, i);
|
|
421
|
+
for (const ch of src.slice(i, end + 1))
|
|
422
|
+
push(ch, false);
|
|
423
|
+
i = end;
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (c === ';' || c === '|' || c === '&' || c === '(' || c === ')') {
|
|
427
|
+
endCommand();
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
push(c, false);
|
|
431
|
+
}
|
|
432
|
+
offset = src.length;
|
|
433
|
+
endCommand();
|
|
434
|
+
return { commands, subs, overflow: budget.n <= 0, words: commands[0]?.words ?? [], redirects: commands[0]?.redirects ?? [] };
|
|
435
|
+
}
|
|
436
|
+
const WRAPPERS_WITH_ARG = {
|
|
437
|
+
sudo: /^-[ugpChrtUD]$|^--(?:user|group|prompt|chdir|host|role|type|other-user)$/,
|
|
438
|
+
doas: /^-[uC]$/,
|
|
439
|
+
env: /^-u$|^--unset$|^-C$|^--chdir$/,
|
|
440
|
+
nice: /^-n$|^--adjustment$/,
|
|
441
|
+
stdbuf: /^-[ioe]$/,
|
|
442
|
+
timeout: /^-[sk]$|^--(?:signal|kill-after)$/,
|
|
443
|
+
};
|
|
444
|
+
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']);
|
|
445
|
+
/** The words from the real command word on, behind wrappers, assignments and grouping keywords. */
|
|
446
|
+
function commandOf(input) {
|
|
447
|
+
let words = input;
|
|
448
|
+
let i = 0;
|
|
449
|
+
let unwraps = 0;
|
|
450
|
+
while (i < words.length) {
|
|
451
|
+
if (unwraps > 64)
|
|
452
|
+
throw new Error('wrapper nesting too deep to judge');
|
|
453
|
+
const t = words[i].text;
|
|
454
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(t)) {
|
|
455
|
+
i++;
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
if (!PLAIN_WRAPPERS.has(t))
|
|
459
|
+
break;
|
|
460
|
+
i++;
|
|
461
|
+
const withArg = WRAPPERS_WITH_ARG[t];
|
|
462
|
+
while (i < words.length && words[i].text.startsWith('-') && words[i].text !== '-') {
|
|
463
|
+
const o = words[i].text;
|
|
464
|
+
i++;
|
|
465
|
+
if (o === '--')
|
|
466
|
+
break;
|
|
467
|
+
// `env -S 'prog args'` / `--split-string=…`: the value IS the command line.
|
|
468
|
+
if (t === 'env' && (o === '-S' || o === '--split-string' || o.startsWith('--split-string=') || /^-S./.test(o))) {
|
|
469
|
+
const prog = o.startsWith('--split-string=') ? o.slice('--split-string='.length) : /^-S./.test(o) ? o.slice(2) : words[i]?.text ?? '';
|
|
470
|
+
if (!(o.startsWith('--split-string=') || /^-S./.test(o)))
|
|
471
|
+
i++;
|
|
472
|
+
words = [...shellWords(prog).words, ...words.slice(i)];
|
|
473
|
+
i = 0;
|
|
474
|
+
unwraps++;
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
if (withArg?.test(o) && i < words.length)
|
|
478
|
+
i++;
|
|
479
|
+
}
|
|
480
|
+
if (t === 'env')
|
|
481
|
+
while (i < words.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[i].text))
|
|
482
|
+
i++;
|
|
483
|
+
if (t === 'timeout' && i < words.length && /^[0-9.]+[smhd]?$/.test(words[i].text))
|
|
484
|
+
i++;
|
|
485
|
+
if (t === 'nice' && i < words.length && /^-?[0-9]+$/.test(words[i].text))
|
|
486
|
+
i++;
|
|
487
|
+
}
|
|
488
|
+
return words.slice(i);
|
|
489
|
+
}
|
|
@@ -1,22 +1,6 @@
|
|
|
1
1
|
import { GlobMatcher } from './write-target';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
known: boolean;
|
|
5
|
-
glob: boolean;
|
|
6
|
-
}
|
|
7
|
-
export interface SimpleCommand {
|
|
8
|
-
words: Word[];
|
|
9
|
-
redirects: Word[];
|
|
10
|
-
}
|
|
11
|
-
/** Bodies of `$(…)`, backticks and `<(…)`/`>(…)`: they RUN, so they are judged as commands too. */
|
|
12
|
-
export interface ParsedSegment {
|
|
13
|
-
commands: SimpleCommand[];
|
|
14
|
-
subs: string[];
|
|
15
|
-
overflow: boolean;
|
|
16
|
-
}
|
|
17
|
-
export declare function shellWords(src: string, env?: NodeJS.ProcessEnv, home?: string, pwd?: string | null): SimpleCommand & ParsedSegment;
|
|
18
|
-
/** The words from the real command word on, behind wrappers, assignments and grouping keywords. */
|
|
19
|
-
export declare function commandOf(input: Word[]): Word[];
|
|
2
|
+
export { shellWords, commandOf } from './shell-syntax';
|
|
3
|
+
export type { Word, SimpleCommand, ParsedSegment } from './shell-syntax';
|
|
20
4
|
export declare const MAX_COMMAND: number;
|
|
21
5
|
/**
|
|
22
6
|
* @implements A-SPEC-728 (rule 44) — whether some `find` runs with no `-name`/`-path`/`-regex` filter after it in its clause
|