@holmes-lab/holmes-kit 0.40.0 → 0.41.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 +312 -0
- package/README.md +79 -5
- package/dist/.build-id +1 -1
- package/dist/holmes/cli/approve-context.js +4 -3
- package/dist/holmes/cli/approve.js +22 -12
- package/dist/holmes/cli/autonomy.d.ts +21 -0
- package/dist/holmes/cli/autonomy.js +35 -5
- package/dist/holmes/cli/doctor.js +58 -6
- package/dist/holmes/cli/index.js +394 -267
- package/dist/holmes/cli/init.js +17 -6
- package/dist/holmes/cli/interactive-prompt.d.ts +39 -2
- package/dist/holmes/cli/interactive-prompt.js +98 -16
- package/dist/holmes/cli/ledger-witness.d.ts +61 -0
- package/dist/holmes/cli/ledger-witness.js +212 -0
- package/dist/holmes/cli/playbook-skills.d.ts +3 -56
- package/dist/holmes/cli/playbook-skills.js +26 -349
- package/dist/holmes/config/config.d.ts +21 -0
- package/dist/holmes/config/config.js +22 -3
- package/dist/holmes/governance/approval-queue.d.ts +65 -1
- package/dist/holmes/governance/approval-queue.js +279 -9
- package/dist/holmes/governance/autonomous-shell.d.ts +13 -1
- package/dist/holmes/governance/autonomous-shell.js +157 -4
- package/dist/holmes/governance/autonomy.d.ts +216 -0
- package/dist/holmes/governance/autonomy.js +1162 -10
- package/dist/holmes/governance/install-manifest.d.ts +7 -4
- package/dist/holmes/governance/install-manifest.js +144 -14
- package/dist/holmes/governance/ledger-witness.d.ts +63 -0
- package/dist/holmes/governance/ledger-witness.js +296 -0
- package/dist/holmes/governance/provenance-chain.d.ts +8 -1
- package/dist/holmes/governance/provenance-chain.js +23 -4
- package/dist/holmes/governance/push-gate-runner.d.ts +2 -0
- package/dist/holmes/governance/push-gate-runner.js +40 -3
- package/dist/holmes/governance/push-gate.d.ts +13 -2
- package/dist/holmes/governance/push-gate.js +106 -14
- package/dist/holmes/governance/recorded-pairs.d.ts +4 -0
- package/dist/holmes/governance/recorded-pairs.js +25 -1
- package/dist/holmes/governance/session-context.d.ts +7 -0
- package/dist/holmes/governance/session-context.js +34 -6
- package/dist/holmes/guardrail/advisory-line.d.ts +102 -0
- package/dist/holmes/guardrail/advisory-line.js +718 -0
- package/dist/holmes/guardrail/anchor-scope-text.d.ts +9 -0
- package/dist/holmes/guardrail/anchor-scope-text.js +7 -0
- package/dist/holmes/guardrail/command-traits.d.ts +102 -0
- package/dist/holmes/guardrail/command-traits.js +379 -0
- package/dist/holmes/guardrail/fixtures/manager-builtins.json +23 -0
- package/dist/holmes/guardrail/fixtures/npm-commands.json +7 -0
- package/dist/holmes/guardrail/impact-gate.d.ts +6 -0
- package/dist/holmes/guardrail/impact-gate.js +32 -5
- package/dist/holmes/guardrail/outward-acts.d.ts +47 -0
- package/dist/holmes/guardrail/outward-acts.js +1102 -0
- package/dist/holmes/guardrail/refusal-codes.js +8 -1
- package/dist/holmes/guardrail/refusal-templates.d.ts +15 -0
- package/dist/holmes/guardrail/refusal-templates.js +71 -0
- package/dist/holmes/guardrail/risk-classifier.d.ts +22 -6
- package/dist/holmes/guardrail/risk-classifier.js +12 -118
- package/dist/holmes/guardrail/shell-lexer.d.ts +69 -0
- package/dist/holmes/guardrail/shell-lexer.js +567 -0
- package/dist/holmes/guardrail/write-target.js +7 -1
- package/dist/holmes/hooks/pre-tool-use.d.ts +71 -0
- package/dist/holmes/hooks/pre-tool-use.js +1466 -14
- package/dist/holmes/hooks/session-start.js +27 -1
- package/dist/holmes/hooks/stop.d.ts +7 -0
- package/dist/holmes/hooks/stop.js +39 -2
- package/dist/holmes/mcp/elicit-approval.d.ts +0 -21
- package/dist/holmes/mcp/elicit-approval.js +5 -2
- package/dist/holmes/mcp/handlers/approval-ask.js +3 -2
- package/dist/holmes/mcp/handlers/entity-integration.d.ts +1 -1
- package/dist/holmes/mcp/handlers/entity-integration.js +9 -6
- package/dist/holmes/mcp/handlers/entity-renumber.js +2 -1
- package/dist/holmes/mcp/handlers/entity-store.js +7 -2
- package/dist/holmes/mcp/handlers/maintenance-analysis.d.ts +13 -0
- package/dist/holmes/mcp/handlers/maintenance-analysis.js +59 -2
- package/dist/holmes/mcp/handlers/operator-inspection.d.ts +2 -0
- package/dist/holmes/mcp/handlers/operator-inspection.js +25 -4
- package/dist/holmes/mcp/handlers/spec-approval.d.ts +3 -1
- package/dist/holmes/mcp/handlers/spec-approval.js +28 -10
- package/dist/holmes/mcp/handlers/spec-lifecycle.js +10 -7
- package/dist/holmes/mcp/handlers/test-execution.d.ts +470 -3
- package/dist/holmes/mcp/handlers/test-execution.js +201 -21
- package/dist/holmes/mcp/handlers.d.ts +473 -8
- package/dist/holmes/mcp/handlers.js +6 -1
- package/dist/holmes/mcp/maintenance-analyze.d.ts +5 -0
- package/dist/holmes/mcp/maintenance-analyze.js +4 -2
- package/dist/holmes/mcp/maintenance-evidence.d.ts +9 -0
- package/dist/holmes/mcp/maintenance-evidence.js +4 -1
- package/dist/holmes/mcp/tool-schemas.js +16 -10
- package/dist/holmes/project/baseline.d.ts +15 -2
- package/dist/holmes/project/baseline.js +11 -6
- package/dist/holmes/project/bounded-io.d.ts +1 -0
- package/dist/holmes/project/bounded-io.js +4 -2
- package/dist/holmes/project/change-source.d.ts +4 -0
- package/dist/holmes/project/change-source.js +13 -5
- package/dist/holmes/project/playbook-reference.d.ts +50 -0
- package/dist/holmes/project/playbook-reference.js +259 -0
- package/dist/holmes/project/playbook-skill-states.d.ts +94 -0
- package/dist/holmes/project/playbook-skill-states.js +396 -0
- package/dist/holmes/project/root.d.ts +9 -1
- package/dist/holmes/project/root.js +26 -3
- package/dist/holmes/project/running-package.d.ts +15 -0
- package/dist/holmes/project/running-package.js +87 -0
- package/dist/holmes/review/evidence-mode.d.ts +21 -0
- package/dist/holmes/review/evidence-mode.js +33 -0
- package/dist/holmes/review/head-worktree.d.ts +55 -0
- package/dist/holmes/review/head-worktree.js +329 -0
- package/dist/holmes/review/mrp.js +13 -3
- package/dist/holmes/review/run-provenance.d.ts +45 -0
- package/dist/holmes/review/run-provenance.js +314 -0
- package/dist/holmes/review/test-evidence.d.ts +174 -0
- package/dist/holmes/review/test-evidence.js +330 -2
- package/dist/holmes/review/test-reports.d.ts +94 -0
- package/dist/holmes/review/test-reports.js +382 -0
- package/dist/holmes/review/test-runner.d.ts +178 -65
- package/dist/holmes/review/test-runner.js +551 -319
- package/dist/holmes/review/test-units.d.ts +159 -0
- package/dist/holmes/review/test-units.js +814 -0
- package/dist/holmes/spec/entity-integration-transaction.d.ts +19 -2
- package/dist/holmes/spec/entity-integration-transaction.js +68 -7
- package/package.json +5 -2
- package/playbooks/publish/PLAYBOOK.md +2 -1
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shownValue = exports.holdsSubstitution = void 0;
|
|
4
|
+
exports.lexShell = lexShell;
|
|
5
|
+
exports.heredocExpansions = heredocExpansions;
|
|
6
|
+
exports.heredocExpansionsRead = heredocExpansionsRead;
|
|
7
|
+
exports.splitRawClauses = splitRawClauses;
|
|
8
|
+
/** A character that ends a word outside quotes. */
|
|
9
|
+
const BREAK = new Set([' ', '\t', '\n', ';', '&', '|', '<', '>', '(', ')']);
|
|
10
|
+
/** The value a substitution leaves in a word: not a character a command can name. */
|
|
11
|
+
const SUBST_MARK = '\u0000';
|
|
12
|
+
class Lexer {
|
|
13
|
+
s;
|
|
14
|
+
nesting;
|
|
15
|
+
i = 0;
|
|
16
|
+
ok = true;
|
|
17
|
+
/** A-SPEC-794.87 — set when subshell groups nested past MAX_GROUP_DEPTH stopped the lexer. */
|
|
18
|
+
tooDeep = false;
|
|
19
|
+
groupSeq = 0;
|
|
20
|
+
pending = [];
|
|
21
|
+
cur;
|
|
22
|
+
out = [];
|
|
23
|
+
/** How deep substitutions nest before the lexer stops (not ok): a bound, not a shell limit. */
|
|
24
|
+
static MAX_NESTING = 200;
|
|
25
|
+
/** A-SPEC-794.87 — how deep subshell groups nest before the lexer stops: each level copies the group stack. */
|
|
26
|
+
// A-SPEC-794.92 — 64: real commands nest at most 2 deep (87,927 measured), and 999 deep ran to 1.2 GB
|
|
27
|
+
static MAX_GROUP_DEPTH = 64;
|
|
28
|
+
constructor(s, nesting = 0) {
|
|
29
|
+
this.s = s;
|
|
30
|
+
this.nesting = nesting;
|
|
31
|
+
}
|
|
32
|
+
/** Clauses up to the end, or (close) up to an unmatched `)` left at `this.i`. */
|
|
33
|
+
sequence(close) {
|
|
34
|
+
const s = this.s;
|
|
35
|
+
const groups = [];
|
|
36
|
+
// A-SPEC-794.92 — one snapshot of the stack, shared by every clause until a group opens or closes (copying it per
|
|
37
|
+
// clause was depth × clauses)
|
|
38
|
+
let snap = [];
|
|
39
|
+
let pipeNext = false;
|
|
40
|
+
this.out = [];
|
|
41
|
+
const fresh = () => { this.cur = { words: [], redirects: [], heredocs: [], substitutions: [], groups: snap, sep: '', pipedFrom: null, source: '', start: this.i }; };
|
|
42
|
+
const end = (sep, pipe = false) => {
|
|
43
|
+
const c = this.cur;
|
|
44
|
+
const has = c.words.length + c.redirects.length + c.substitutions.length + c.heredocs.length > 0;
|
|
45
|
+
if (has) {
|
|
46
|
+
c.sep = sep;
|
|
47
|
+
c.source = s.slice(c.start, this.i).trim();
|
|
48
|
+
if (pipeNext)
|
|
49
|
+
c.pipedFrom = this.out.length - 1;
|
|
50
|
+
const { start: _start, ...clause } = c;
|
|
51
|
+
this.out.push(clause);
|
|
52
|
+
}
|
|
53
|
+
pipeNext = pipe && has;
|
|
54
|
+
fresh();
|
|
55
|
+
};
|
|
56
|
+
fresh();
|
|
57
|
+
while (this.i < s.length) {
|
|
58
|
+
const c = s[this.i];
|
|
59
|
+
if (c === ' ' || c === '\t' || c === '\r') {
|
|
60
|
+
this.i++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (c === '\\' && s[this.i + 1] === '\n') {
|
|
64
|
+
this.i += 2;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (this.cur.words.length === 0 && this.cur.redirects.length === 0 && this.cur.substitutions.length === 0)
|
|
68
|
+
this.cur.start = this.i;
|
|
69
|
+
if (c === '#') {
|
|
70
|
+
while (this.i < s.length && s[this.i] !== '\n')
|
|
71
|
+
this.i++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (c === '\n') {
|
|
75
|
+
end('\n');
|
|
76
|
+
this.i++;
|
|
77
|
+
this.readHeredocBodies();
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (c === ';') {
|
|
81
|
+
end(';');
|
|
82
|
+
this.i += s[this.i + 1] === ';' || s[this.i + 1] === '&' ? 2 : 1;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (c === '&') {
|
|
86
|
+
if (s[this.i + 1] === '&') {
|
|
87
|
+
end('&&');
|
|
88
|
+
this.i += 2;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (s[this.i + 1] === '>') {
|
|
92
|
+
const op = s[this.i + 2] === '>' ? '&>>' : '&>';
|
|
93
|
+
this.i += op.length;
|
|
94
|
+
this.redirect(op);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
end('&');
|
|
98
|
+
this.i++;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (c === '|') {
|
|
102
|
+
if (s[this.i + 1] === '|') {
|
|
103
|
+
end('||');
|
|
104
|
+
this.i += 2;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (s[this.i + 1] === '&') {
|
|
108
|
+
end('|&', true);
|
|
109
|
+
this.i += 2;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
end('|', true);
|
|
113
|
+
this.i++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (c === '(') {
|
|
117
|
+
if (s[this.i + 1] === '(') {
|
|
118
|
+
this.word();
|
|
119
|
+
continue;
|
|
120
|
+
} // arithmetic (( … ))
|
|
121
|
+
if (this.cur.words.length > 0 && s[this.i + 1] === ')') {
|
|
122
|
+
this.i += 2;
|
|
123
|
+
continue;
|
|
124
|
+
} // name() — a function
|
|
125
|
+
end(';');
|
|
126
|
+
// A-SPEC-794.87 — past the bound the lexer stops: copying the stack per level was quadratic (45,000 groups: 26 s)
|
|
127
|
+
if (groups.length >= Lexer.MAX_GROUP_DEPTH) {
|
|
128
|
+
this.ok = false;
|
|
129
|
+
this.tooDeep = true;
|
|
130
|
+
this.i = s.length;
|
|
131
|
+
return this.out;
|
|
132
|
+
}
|
|
133
|
+
groups.push(++this.groupSeq);
|
|
134
|
+
snap = [...groups];
|
|
135
|
+
this.cur.groups = snap;
|
|
136
|
+
this.i++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (c === ')') {
|
|
140
|
+
if (groups.length > 0) {
|
|
141
|
+
end(')');
|
|
142
|
+
groups.pop();
|
|
143
|
+
snap = [...groups];
|
|
144
|
+
this.cur.groups = snap;
|
|
145
|
+
this.i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (close) {
|
|
149
|
+
end('');
|
|
150
|
+
return this.out;
|
|
151
|
+
}
|
|
152
|
+
this.i++; // a case arm's `)`
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if ((c === '<' || c === '>') && s[this.i + 1] === '(') {
|
|
156
|
+
this.processSubstitution(c);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const fd = /^\d+(?=[<>])/.exec(s.slice(this.i, this.i + 12));
|
|
160
|
+
if (c === '<' || c === '>' || fd !== null) {
|
|
161
|
+
if (fd !== null)
|
|
162
|
+
this.i += fd[0].length;
|
|
163
|
+
this.redirect(this.operator());
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
this.cur.words.push(this.word());
|
|
167
|
+
}
|
|
168
|
+
end('');
|
|
169
|
+
if (close || groups.length > 0)
|
|
170
|
+
this.ok = false;
|
|
171
|
+
return this.out;
|
|
172
|
+
}
|
|
173
|
+
operator() {
|
|
174
|
+
const s = this.s;
|
|
175
|
+
for (const op of ['<<<', '<<-', '<<', '<&', '<>', '>>', '>|', '>&', '<', '>'])
|
|
176
|
+
if (s.startsWith(op, this.i)) {
|
|
177
|
+
this.i += op.length;
|
|
178
|
+
return op;
|
|
179
|
+
}
|
|
180
|
+
return s[this.i++];
|
|
181
|
+
}
|
|
182
|
+
redirect(op) {
|
|
183
|
+
while (this.s[this.i] === ' ' || this.s[this.i] === '\t')
|
|
184
|
+
this.i++;
|
|
185
|
+
const atEnd = this.i >= this.s.length || BREAK.has(this.s[this.i]) && this.s[this.i] !== '(';
|
|
186
|
+
const target = atEnd ? null : this.word();
|
|
187
|
+
this.cur.redirects.push({ op, target });
|
|
188
|
+
if ((op === '<<' || op === '<<-') && target !== null) {
|
|
189
|
+
const hd = { body: '', quoted: /['"\\]/.test(target.raw), hereString: false };
|
|
190
|
+
this.cur.heredocs.push(hd);
|
|
191
|
+
this.pending.push({ delim: target.value, strip: op === '<<-', hd });
|
|
192
|
+
}
|
|
193
|
+
else if (op === '<<<' && target !== null) {
|
|
194
|
+
this.cur.heredocs.push({ body: target.value.split(SUBST_MARK).join(''), quoted: /^['"]/.test(target.raw), hereString: true });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
readHeredocBodies() {
|
|
198
|
+
const s = this.s;
|
|
199
|
+
for (const p of this.pending) {
|
|
200
|
+
const lines = [];
|
|
201
|
+
while (this.i < s.length) {
|
|
202
|
+
let nl = s.indexOf('\n', this.i);
|
|
203
|
+
if (nl < 0)
|
|
204
|
+
nl = s.length;
|
|
205
|
+
const line = s.slice(this.i, nl);
|
|
206
|
+
this.i = Math.min(nl + 1, s.length);
|
|
207
|
+
if ((p.strip ? line.replace(/^\t+/, '') : line) === p.delim)
|
|
208
|
+
break;
|
|
209
|
+
lines.push(line);
|
|
210
|
+
}
|
|
211
|
+
p.hd.body = lines.join('\n');
|
|
212
|
+
}
|
|
213
|
+
this.pending = [];
|
|
214
|
+
}
|
|
215
|
+
/** The body of `(…)` starting at `from` (after the open parenthesis), recursively lexed to find its end. */
|
|
216
|
+
parenBody(from) {
|
|
217
|
+
if (this.nesting >= Lexer.MAX_NESTING) {
|
|
218
|
+
this.ok = false;
|
|
219
|
+
this.i = this.s.length;
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
const inner = new Lexer(this.s, this.nesting + 1);
|
|
223
|
+
inner.i = from;
|
|
224
|
+
inner.sequence(true);
|
|
225
|
+
if (inner.tooDeep)
|
|
226
|
+
this.tooDeep = true; // A-SPEC-794.87
|
|
227
|
+
if (!inner.ok || this.s[inner.i] !== ')') {
|
|
228
|
+
this.ok = false;
|
|
229
|
+
this.i = this.s.length;
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
this.i = inner.i + 1;
|
|
233
|
+
return this.s.slice(from, inner.i);
|
|
234
|
+
}
|
|
235
|
+
processSubstitution(c) {
|
|
236
|
+
const start = this.i;
|
|
237
|
+
const body = this.parenBody(this.i + 2);
|
|
238
|
+
if (body !== null)
|
|
239
|
+
this.cur.substitutions.push({ kind: c === '<' ? '<(' : '>(', body });
|
|
240
|
+
this.cur.words.push({ value: SUBST_MARK, raw: this.s.slice(start, this.i), literal: false });
|
|
241
|
+
}
|
|
242
|
+
/** The substitutions an unquoted here-document's body runs (quotes are characters there). */
|
|
243
|
+
expansions() {
|
|
244
|
+
const s = this.s;
|
|
245
|
+
const out = [];
|
|
246
|
+
while (this.i < s.length) {
|
|
247
|
+
const c = s[this.i];
|
|
248
|
+
if (c === '\\') {
|
|
249
|
+
this.i += 2;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (c === '`' || (c === '$' && s[this.i + 1] === '(')) {
|
|
253
|
+
const before = this.cur;
|
|
254
|
+
this.cur = { words: [], redirects: [], heredocs: [], substitutions: [], groups: [], sep: '', pipedFrom: null, source: '', start: 0 };
|
|
255
|
+
this.substitution();
|
|
256
|
+
out.push(...this.cur.substitutions.map((x) => x.body));
|
|
257
|
+
this.cur = before;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
this.i++;
|
|
261
|
+
}
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
backtick() {
|
|
265
|
+
const s = this.s;
|
|
266
|
+
let body = '';
|
|
267
|
+
for (let j = this.i + 1; j < s.length; j++) {
|
|
268
|
+
if (s[j] === '\\' && j + 1 < s.length) {
|
|
269
|
+
body += '$`\\'.includes(s[j + 1]) ? s[j + 1] : s[j] + s[j + 1];
|
|
270
|
+
j++;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (s[j] === '`') {
|
|
274
|
+
this.i = j + 1;
|
|
275
|
+
return body;
|
|
276
|
+
}
|
|
277
|
+
body += s[j];
|
|
278
|
+
}
|
|
279
|
+
this.ok = false;
|
|
280
|
+
this.i = s.length;
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
/** `$(…)`, `$((…))` or a backtick at `this.i`: the substitution recorded, the word marked. */
|
|
284
|
+
substitution() {
|
|
285
|
+
const s = this.s;
|
|
286
|
+
if (s[this.i] === '`') {
|
|
287
|
+
const body = this.backtick();
|
|
288
|
+
if (body !== null)
|
|
289
|
+
this.cur.substitutions.push({ kind: '`', body });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (s[this.i + 2] === '(') { // arithmetic (or a subshell's substitution): its value is not a literal
|
|
293
|
+
this.arithmetic(this.i + 1);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const body = this.parenBody(this.i + 2);
|
|
297
|
+
if (body !== null)
|
|
298
|
+
this.cur.substitutions.push({ kind: '$(', body });
|
|
299
|
+
}
|
|
300
|
+
word() {
|
|
301
|
+
const s = this.s;
|
|
302
|
+
const start = this.i;
|
|
303
|
+
let value = '';
|
|
304
|
+
let literal = true;
|
|
305
|
+
if (s.startsWith('((', this.i)) {
|
|
306
|
+
this.substitutionArithmetic();
|
|
307
|
+
return { value: SUBST_MARK, raw: s.slice(start, this.i), literal: false };
|
|
308
|
+
}
|
|
309
|
+
while (this.i < s.length) {
|
|
310
|
+
const c = s[this.i];
|
|
311
|
+
if (BREAK.has(c))
|
|
312
|
+
break;
|
|
313
|
+
if (c === '\\') {
|
|
314
|
+
if (s[this.i + 1] !== '\n')
|
|
315
|
+
value += s[this.i + 1] ?? '';
|
|
316
|
+
this.i += 2;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (c === "'") {
|
|
320
|
+
const j = s.indexOf("'", this.i + 1);
|
|
321
|
+
if (j < 0) {
|
|
322
|
+
this.ok = false;
|
|
323
|
+
value += s.slice(this.i + 1);
|
|
324
|
+
this.i = s.length;
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
value += s.slice(this.i + 1, j);
|
|
328
|
+
this.i = j + 1;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (c === '$' && s[this.i + 1] === "'") {
|
|
332
|
+
value += this.ansiC();
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (c === '"') {
|
|
336
|
+
const v = this.doubleQuoted();
|
|
337
|
+
value += v.value;
|
|
338
|
+
literal = literal && v.literal;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (c === '`' || (c === '$' && s[this.i + 1] === '(')) {
|
|
342
|
+
this.substitution();
|
|
343
|
+
value += SUBST_MARK;
|
|
344
|
+
literal = false;
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (c === '$' || c === '*' || c === '?' || c === '[' || (c === '~' && this.i === start))
|
|
348
|
+
literal = false;
|
|
349
|
+
value += c;
|
|
350
|
+
this.i++;
|
|
351
|
+
}
|
|
352
|
+
return { value, raw: s.slice(start, this.i), literal };
|
|
353
|
+
}
|
|
354
|
+
substitutionArithmetic() { this.arithmetic(this.i); }
|
|
355
|
+
/**
|
|
356
|
+
* @implements A-SPEC-794.92 (round 2, review A H3) — `((…))` whose first `(` is at `at`, reached from `$((`, `((` or
|
|
357
|
+
* `for ((`. Arithmetic is not data: bash and zsh run each `$(…)` and backtick inside it, so they are recorded as
|
|
358
|
+
* substitutions (a nested arithmetic is read the same way). A body that does not close with `))` (`$((cmd) )`) is a
|
|
359
|
+
* subshell inside a substitution or a group, and the whole is recorded as that substitution. An unclosed or unreadable
|
|
360
|
+
* body leaves the lexer not ok (the caller judges the command unjudged).
|
|
361
|
+
*/
|
|
362
|
+
arithmetic(at) {
|
|
363
|
+
const s = this.s;
|
|
364
|
+
let depth = 0;
|
|
365
|
+
for (let j = at; j < s.length; j++) {
|
|
366
|
+
if (s[j] === '(')
|
|
367
|
+
depth++;
|
|
368
|
+
else if (s[j] === ')' && --depth === 0) {
|
|
369
|
+
this.i = j + 1;
|
|
370
|
+
if (s[j - 1] !== ')' || j - 1 <= at + 1) {
|
|
371
|
+
this.cur.substitutions.push({ kind: '$(', body: s.slice(at + 1, j) });
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const body = s.slice(at + 2, j - 1);
|
|
375
|
+
if (body.includes('`') || body.includes('$(')) {
|
|
376
|
+
const inner = new Lexer(body);
|
|
377
|
+
const bodies = inner.expansions();
|
|
378
|
+
for (const b of bodies)
|
|
379
|
+
this.cur.substitutions.push({ kind: '$(', body: b });
|
|
380
|
+
if (!inner.ok)
|
|
381
|
+
this.ok = false;
|
|
382
|
+
if (inner.tooDeep)
|
|
383
|
+
this.tooDeep = true;
|
|
384
|
+
}
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
this.ok = false;
|
|
389
|
+
this.i = s.length;
|
|
390
|
+
}
|
|
391
|
+
ansiC() {
|
|
392
|
+
const s = this.s;
|
|
393
|
+
let v = '';
|
|
394
|
+
for (let j = this.i + 2; j < s.length; j++) {
|
|
395
|
+
if (s[j] === '\\' && j + 1 < s.length) {
|
|
396
|
+
const e = s[j + 1];
|
|
397
|
+
v += e === 'n' ? '\n' : e === 't' ? '\t' : e;
|
|
398
|
+
j++;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
if (s[j] === "'") {
|
|
402
|
+
this.i = j + 1;
|
|
403
|
+
return v;
|
|
404
|
+
}
|
|
405
|
+
v += s[j];
|
|
406
|
+
}
|
|
407
|
+
this.ok = false;
|
|
408
|
+
this.i = s.length;
|
|
409
|
+
return v;
|
|
410
|
+
}
|
|
411
|
+
doubleQuoted() {
|
|
412
|
+
const s = this.s;
|
|
413
|
+
let value = '';
|
|
414
|
+
let literal = true;
|
|
415
|
+
this.i++;
|
|
416
|
+
while (this.i < s.length) {
|
|
417
|
+
const c = s[this.i];
|
|
418
|
+
if (c === '"') {
|
|
419
|
+
this.i++;
|
|
420
|
+
return { value, literal };
|
|
421
|
+
}
|
|
422
|
+
if (c === '\\') {
|
|
423
|
+
const n = s[this.i + 1] ?? '';
|
|
424
|
+
if (n === '\n') {
|
|
425
|
+
this.i += 2;
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
value += '$`"\\'.includes(n) ? n : c + n;
|
|
429
|
+
this.i += 2;
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (c === '`' || (c === '$' && s[this.i + 1] === '(')) {
|
|
433
|
+
this.substitution();
|
|
434
|
+
value += SUBST_MARK;
|
|
435
|
+
literal = false;
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
if (c === '$')
|
|
439
|
+
literal = false;
|
|
440
|
+
value += c;
|
|
441
|
+
this.i++;
|
|
442
|
+
}
|
|
443
|
+
this.ok = false;
|
|
444
|
+
return { value, literal };
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function lexShell(text) {
|
|
448
|
+
const lx = new Lexer(text);
|
|
449
|
+
const clauses = lx.sequence(false);
|
|
450
|
+
return { clauses, ok: lx.ok, ...(lx.tooDeep ? { tooDeep: true } : {}) }; // A-SPEC-794.87
|
|
451
|
+
}
|
|
452
|
+
/** The command substitutions an unquoted here-document's body runs. */
|
|
453
|
+
function heredocExpansions(body) { return new Lexer(body).expansions(); }
|
|
454
|
+
/**
|
|
455
|
+
* @implements A-SPEC-794.92 — the substitutions an unquoted here-document runs, and whether every one was read: a
|
|
456
|
+
* substitution the lexer could not read ends the scan, and the rest of the body is unread (the caller closes it).
|
|
457
|
+
*/
|
|
458
|
+
function heredocExpansionsRead(body) {
|
|
459
|
+
const lx = new Lexer(body);
|
|
460
|
+
const bodies = lx.expansions();
|
|
461
|
+
return { bodies, ok: lx.ok, tooDeep: lx.tooDeep };
|
|
462
|
+
}
|
|
463
|
+
/** True when a word's value holds a substitution. */
|
|
464
|
+
const holdsSubstitution = (v) => v.includes(SUBST_MARK);
|
|
465
|
+
exports.holdsSubstitution = holdsSubstitution;
|
|
466
|
+
/** The value a word holds where a substitution was (for display: removed). */
|
|
467
|
+
const shownValue = (v) => v.split(SUBST_MARK).join('…');
|
|
468
|
+
exports.shownValue = shownValue;
|
|
469
|
+
/**
|
|
470
|
+
* The raw text pieces between top-level separators; null for an unclosed quote, substitution or backtick. `atPipe`
|
|
471
|
+
* false keeps a pipeline whole (A-SPEC-747.3). Here-document lines are pieces (the line view).
|
|
472
|
+
*/
|
|
473
|
+
function splitRawClauses(cmd, atPipe = true) {
|
|
474
|
+
const out = [];
|
|
475
|
+
let cur = '';
|
|
476
|
+
let quote = null;
|
|
477
|
+
let depth = 0;
|
|
478
|
+
let tick = false;
|
|
479
|
+
for (let i = 0; i < cmd.length; i++) {
|
|
480
|
+
const ch = cmd[i];
|
|
481
|
+
if (quote === "'") {
|
|
482
|
+
cur += ch;
|
|
483
|
+
if (ch === "'")
|
|
484
|
+
quote = null;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (ch === '\\' && i + 1 < cmd.length) {
|
|
488
|
+
cur += ch + cmd[i + 1];
|
|
489
|
+
i++;
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
if (ch === '`') {
|
|
493
|
+
tick = !tick;
|
|
494
|
+
cur += ch;
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if ((ch === '$' || ch === '<' || ch === '>') && cmd[i + 1] === '(') {
|
|
498
|
+
depth++;
|
|
499
|
+
cur += ch + '(';
|
|
500
|
+
i++;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
if (depth > 0 && ch === ')') {
|
|
504
|
+
depth--;
|
|
505
|
+
cur += ch;
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (quote === '"') {
|
|
509
|
+
cur += ch;
|
|
510
|
+
if (ch === '"')
|
|
511
|
+
quote = null;
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (ch === '"' || ch === "'") {
|
|
515
|
+
quote = ch;
|
|
516
|
+
cur += ch;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (depth === 0 && !tick) {
|
|
520
|
+
if (ch === '\n' || ch === ';') {
|
|
521
|
+
out.push(cur);
|
|
522
|
+
cur = '';
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
if (ch === '|' && cmd[i + 1] === '|') {
|
|
526
|
+
i++;
|
|
527
|
+
out.push(cur);
|
|
528
|
+
cur = '';
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
if (ch === '|' && cmd[i + 1] === '&') {
|
|
532
|
+
if (atPipe) {
|
|
533
|
+
i++;
|
|
534
|
+
out.push(cur);
|
|
535
|
+
cur = '';
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
cur += '|&';
|
|
539
|
+
i++;
|
|
540
|
+
}
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
if (ch === '|' && atPipe) {
|
|
544
|
+
out.push(cur);
|
|
545
|
+
cur = '';
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
if (ch === '&' && cmd[i + 1] === '&') {
|
|
549
|
+
i++;
|
|
550
|
+
out.push(cur);
|
|
551
|
+
cur = '';
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
// A-SPEC-794.70 — a lone & ends a clause; the & of `&>`, `>&`, `<&` and `2>&1` is a redirect
|
|
555
|
+
if (ch === '&' && cmd[i + 1] !== '>' && !/[<>]$/.test(cur)) {
|
|
556
|
+
out.push(cur);
|
|
557
|
+
cur = '';
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
cur += ch;
|
|
562
|
+
}
|
|
563
|
+
if (quote !== null || depth !== 0 || tick)
|
|
564
|
+
return null;
|
|
565
|
+
out.push(cur);
|
|
566
|
+
return out;
|
|
567
|
+
}
|
|
@@ -73,7 +73,7 @@ exports.commandBases = commandBases;
|
|
|
73
73
|
exports.shellSegments = shellSegments;
|
|
74
74
|
exports.resolvesInside = resolvesInside;
|
|
75
75
|
exports.foreignMcpPaths = foreignMcpPaths;
|
|
76
|
-
// @implements A-SPEC-728, A-SPEC-733, A-SPEC-163, A-SPEC-627, A-SPEC-768.2, A-SPEC-768.3, A-SPEC-163.8, A-SPEC-476.3, A-SPEC-476.7, A-SPEC-792.3, A-SPEC-794.38, A-SPEC-794.40
|
|
76
|
+
// @implements A-SPEC-794.73, A-SPEC-728, A-SPEC-733, A-SPEC-163, A-SPEC-627, A-SPEC-768.2, A-SPEC-768.3, A-SPEC-163.8, A-SPEC-476.3, A-SPEC-476.7, A-SPEC-792.3, A-SPEC-794.38, A-SPEC-794.40
|
|
77
77
|
const fs = __importStar(require("node:fs"));
|
|
78
78
|
const path = __importStar(require("node:path"));
|
|
79
79
|
const os = __importStar(require("node:os"));
|
|
@@ -2990,6 +2990,12 @@ function shellSegments(command, startBase, depth = 0, opts = {}) {
|
|
|
2990
2990
|
comment = false;
|
|
2991
2991
|
}
|
|
2992
2992
|
else if (quote !== null) {
|
|
2993
|
+
// @implements A-SPEC-794.73 — inside double quotes a backslash escapes the next character (`\"` stays inside
|
|
2994
|
+
// the quote, as in the shell); inside single quotes it escapes nothing
|
|
2995
|
+
if (quote === '"' && ch === '\\' && k + 1 < text.length) {
|
|
2996
|
+
add(ch + text[++k]);
|
|
2997
|
+
continue;
|
|
2998
|
+
}
|
|
2993
2999
|
add(ch);
|
|
2994
3000
|
if (ch === quote)
|
|
2995
3001
|
quote = null;
|