@holmes-lab/holmes-kit 0.35.25 → 0.36.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 +298 -0
- package/README.md +3 -2
- package/dist/.build-id +1 -1
- package/dist/holmes/cli/approve-status.js +4 -1
- package/dist/holmes/cli/approve-watch.js +3 -1
- package/dist/holmes/cli/approve.d.ts +10 -0
- package/dist/holmes/cli/approve.js +31 -1
- package/dist/holmes/cli/claude-switches.d.ts +7 -1
- package/dist/holmes/cli/claude-switches.js +11 -7
- package/dist/holmes/cli/codex-toml.d.ts +3 -0
- package/dist/holmes/cli/codex-toml.js +2 -1
- package/dist/holmes/cli/doctor.d.ts +25 -1
- package/dist/holmes/cli/doctor.js +153 -7
- package/dist/holmes/cli/index.js +99 -4
- package/dist/holmes/cli/init.js +4 -3
- package/dist/holmes/cli/os-notify.d.ts +3 -1
- package/dist/holmes/cli/os-notify.js +10 -4
- package/dist/holmes/cli/review.d.ts +25 -0
- package/dist/holmes/cli/review.js +111 -0
- package/dist/holmes/cli/roles-readme.d.ts +11 -0
- package/dist/holmes/cli/roles-readme.js +38 -16
- package/dist/holmes/cli/roles.d.ts +12 -0
- package/dist/holmes/cli/roles.js +86 -0
- package/dist/holmes/cli/run.d.ts +65 -0
- package/dist/holmes/cli/run.js +308 -0
- package/dist/holmes/cli/runtime-binding.d.ts +30 -0
- package/dist/holmes/cli/runtime-binding.js +96 -7
- package/dist/holmes/cli/test-platform.d.ts +16 -0
- package/dist/holmes/cli/test-platform.js +31 -1
- package/dist/holmes/config/config.d.ts +49 -0
- package/dist/holmes/config/config.js +146 -3
- package/dist/holmes/config/project-caps.d.ts +21 -0
- package/dist/holmes/config/project-caps.js +114 -0
- package/dist/holmes/governance/approval-queue.d.ts +17 -3
- package/dist/holmes/governance/approval-queue.js +48 -6
- package/dist/holmes/governance/autonomy.d.ts +6 -0
- package/dist/holmes/governance/autonomy.js +21 -0
- package/dist/holmes/governance/push-gate-runner.d.ts +8 -0
- package/dist/holmes/governance/push-gate-runner.js +49 -1
- package/dist/holmes/governance/replica-id.d.ts +9 -0
- package/dist/holmes/governance/replica-id.js +44 -7
- package/dist/holmes/governance/role-policy.d.ts +41 -1
- package/dist/holmes/governance/role-policy.js +137 -20
- package/dist/holmes/governance/run-mode.d.ts +14 -0
- package/dist/holmes/governance/run-mode.js +120 -0
- package/dist/holmes/governance/tool-actions.d.ts +30 -0
- package/dist/holmes/governance/tool-actions.js +121 -0
- package/dist/holmes/guardrail/agent-launch-policy.d.ts +15 -0
- package/dist/holmes/guardrail/agent-launch-policy.js +177 -0
- package/dist/holmes/guardrail/classification-mode.d.ts +17 -0
- package/dist/holmes/guardrail/classification-mode.js +135 -0
- package/dist/holmes/guardrail/dynamic-write.d.ts +43 -0
- package/dist/holmes/guardrail/dynamic-write.js +304 -0
- package/dist/holmes/guardrail/phase.d.ts +2 -1
- package/dist/holmes/guardrail/phase.js +22 -8
- package/dist/holmes/guardrail/powershell-shell.d.ts +21 -0
- package/dist/holmes/guardrail/powershell-shell.js +801 -0
- package/dist/holmes/guardrail/refusal-codes.d.ts +24 -0
- package/dist/holmes/guardrail/refusal-codes.js +140 -0
- package/dist/holmes/guardrail/risk-classifier.d.ts +17 -0
- package/dist/holmes/guardrail/risk-classifier.js +242 -26
- package/dist/holmes/guardrail/risk-types.d.ts +15 -0
- package/dist/holmes/guardrail/run-plan-scope.d.ts +23 -0
- package/dist/holmes/guardrail/run-plan-scope.js +155 -0
- package/dist/holmes/guardrail/run-refusals.d.ts +116 -0
- package/dist/holmes/guardrail/run-refusals.js +348 -0
- package/dist/holmes/guardrail/run-stages.d.ts +67 -0
- package/dist/holmes/guardrail/run-stages.js +188 -0
- package/dist/holmes/guardrail/scope-judgment.d.ts +5 -1
- package/dist/holmes/guardrail/scope-judgment.js +8 -10
- package/dist/holmes/guardrail/scope-refusal-memory.d.ts +3 -0
- package/dist/holmes/guardrail/scope-refusal-memory.js +85 -0
- package/dist/holmes/guardrail/shell-words.js +7 -7
- package/dist/holmes/guardrail/write-target.d.ts +31 -2
- package/dist/holmes/guardrail/write-target.js +118 -16
- package/dist/holmes/hooks/pre-tool-use.d.ts +35 -1
- package/dist/holmes/hooks/pre-tool-use.js +702 -81
- package/dist/holmes/hooks/session-start.js +5 -3
- package/dist/holmes/hooks/stop.d.ts +32 -0
- package/dist/holmes/hooks/stop.js +129 -4
- package/dist/holmes/mcp/handlers/graph-operations.js +5 -5
- package/dist/holmes/mcp/handlers/maintenance-analysis.js +2 -2
- package/dist/holmes/mcp/handlers/operator-inspection.d.ts +2 -1
- package/dist/holmes/mcp/handlers/operator-inspection.js +3 -1
- package/dist/holmes/mcp/handlers/phase-queries.js +16 -1
- package/dist/holmes/mcp/handlers/requirement-triage.js +2 -2
- package/dist/holmes/mcp/handlers/spec-approval.d.ts +3 -3
- package/dist/holmes/mcp/handlers/spec-approval.js +54 -8
- package/dist/holmes/mcp/handlers/spec-lifecycle.d.ts +1 -0
- package/dist/holmes/mcp/handlers/spec-lifecycle.js +3 -3
- package/dist/holmes/mcp/handlers/spec-queries.d.ts +7 -0
- package/dist/holmes/mcp/handlers/spec-queries.js +5 -0
- package/dist/holmes/mcp/handlers/workspace-queries.js +2 -2
- package/dist/holmes/mcp/handlers.d.ts +2 -3
- package/dist/holmes/mcp/handlers.js +22 -2
- package/dist/holmes/mcp/harness-attestation.d.ts +2 -0
- package/dist/holmes/mcp/harness-attestation.js +3 -1
- package/dist/holmes/mcp/operator-recovery.d.ts +2 -0
- package/dist/holmes/mcp/operator-recovery.js +4 -3
- package/dist/holmes/mcp/server.js +9 -0
- package/dist/holmes/mcp/tool-schemas.js +6 -3
- package/dist/holmes/project/root.js +6 -1
- package/dist/holmes/project/run-records.d.ts +17 -0
- package/dist/holmes/project/run-records.js +126 -0
- package/dist/holmes/review/mrp.d.ts +35 -0
- package/dist/holmes/review/mrp.js +119 -0
- package/dist/holmes/review/verdicts.d.ts +117 -0
- package/dist/holmes/review/verdicts.js +345 -0
- package/dist/holmes/semantic/cloud-usage.d.ts +20 -0
- package/dist/holmes/semantic/cloud-usage.js +106 -0
- package/dist/holmes/semantic/embedder.d.ts +15 -6
- package/dist/holmes/semantic/embedder.js +84 -3
- package/dist/holmes/semantic/local-model.d.ts +21 -2
- package/dist/holmes/semantic/local-model.js +14 -3
- package/dist/holmes/semantic/tier.d.ts +25 -0
- package/dist/holmes/semantic/tier.js +86 -1
- package/dist/holmes/semantic/vector-cache.d.ts +1 -0
- package/dist/holmes/semantic/vector-cache.js +4 -1
- package/dist/holmes/spec/approval-blockers.d.ts +11 -1
- package/dist/holmes/spec/approval-blockers.js +70 -2
- package/dist/holmes/spec/renumber.d.ts +1 -1
- package/dist/holmes/spec/renumber.js +5 -2
- package/dist/holmes/spec/transition-policy.d.ts +5 -7
- package/dist/holmes/spec/transition-policy.js +17 -2
- package/package.json +1 -1
|
@@ -0,0 +1,801 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @implements A-SPEC-790, A-SPEC-790.1, A-SPEC-790.3, A-SPEC-790.4, A-SPEC-790.5, A-SPEC-790.6
|
|
3
|
+
/**
|
|
4
|
+
* PowerShell text, read as the shell acts it performs.
|
|
5
|
+
*
|
|
6
|
+
* Measured 2026-10-18 (dist a998bea8, Windows 11): the PreToolUse shell branch ran only for tool_name 'bash', so
|
|
7
|
+
* Claude Code's PowerShell tool passed `Remove-Item -Recurse -Force .ax/specs`, `Set-Content src/...ts` and
|
|
8
|
+
* `git push --force` with no decision. Rather than a second rule set per shell, a PowerShell command is translated into
|
|
9
|
+
* the shell text that does the same thing, and the existing Bash rules judge that text — so every later rule protects
|
|
10
|
+
* both tools at once.
|
|
11
|
+
*
|
|
12
|
+
* PowerShell's own syntax decides: a backslash is a literal path separator (emitted as `/`), a backtick is the escape,
|
|
13
|
+
* single quotes are literal, `$env:NAME` is an environment variable, and cmdlets and their aliases do what PowerShell
|
|
14
|
+
* does. A command the Bash rules would not recognise passes through as its own words, so native tools (git, npm, node)
|
|
15
|
+
* are judged exactly as under Bash. Text that cannot be read is `unjudgeable`; the hook refuses it. Pure.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.powershellAsShell = powershellAsShell;
|
|
19
|
+
class Unjudgeable extends Error {
|
|
20
|
+
}
|
|
21
|
+
const SAFE_WORD = /^[A-Za-z0-9_./:=@%+,~*?[\]^-]+$/;
|
|
22
|
+
const DOTNET_CALL = /^\[(?:System\.)?(IO\.File|IO\.Directory|Environment)\]::(\w+)\s*\(/i;
|
|
23
|
+
const litWord = (text) => ({ k: 'word', parts: [{ lit: text }], quoted: false, src: text, inner: text });
|
|
24
|
+
/** The word's text when it holds only literal parts; null when it expands something. */
|
|
25
|
+
const plain = (w) => (w.parts.every((p) => 'lit' in p) ? w.parts.map((p) => p.lit).join('') : null);
|
|
26
|
+
const sq = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
|
|
27
|
+
/** The end (exclusive) of a `(`-group starting at `open`, skipping quoted text; -1 when it never closes. */
|
|
28
|
+
function closingParen(src, open) {
|
|
29
|
+
let depth = 0;
|
|
30
|
+
for (let i = open; i < src.length; i++) {
|
|
31
|
+
const c = src[i];
|
|
32
|
+
if (c === "'") {
|
|
33
|
+
const e = src.indexOf("'", i + 1);
|
|
34
|
+
if (e < 0)
|
|
35
|
+
return -1;
|
|
36
|
+
i = e;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (c === '"') {
|
|
40
|
+
let j = i + 1;
|
|
41
|
+
while (j < src.length && src[j] !== '"')
|
|
42
|
+
j += src[j] === '`' ? 2 : 1;
|
|
43
|
+
if (j >= src.length)
|
|
44
|
+
return -1;
|
|
45
|
+
i = j;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (c === '`') {
|
|
49
|
+
i++;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (c === '(')
|
|
53
|
+
depth++;
|
|
54
|
+
else if (c === ')' && --depth === 0)
|
|
55
|
+
return i + 1;
|
|
56
|
+
}
|
|
57
|
+
return -1;
|
|
58
|
+
}
|
|
59
|
+
/** A `$` expansion at `i` (src[i] === '$'): the part it makes and where it ends. */
|
|
60
|
+
function dollar(src, i) {
|
|
61
|
+
if (src[i + 1] === '(') {
|
|
62
|
+
const end = closingParen(src, i + 1);
|
|
63
|
+
if (end < 0)
|
|
64
|
+
throw new Unjudgeable('an unterminated $( subexpression');
|
|
65
|
+
const inner = translate(src.slice(i + 2, end - 1));
|
|
66
|
+
return { part: { sub: inner }, end };
|
|
67
|
+
}
|
|
68
|
+
const braced = /^\$\{env:(\w+)\}/i.exec(src.slice(i));
|
|
69
|
+
if (braced)
|
|
70
|
+
return { part: { env: braced[1] }, end: i + braced[0].length };
|
|
71
|
+
const env = /^\$env:(\w+)/i.exec(src.slice(i));
|
|
72
|
+
if (env)
|
|
73
|
+
return { part: { env: env[1] }, end: i + env[0].length };
|
|
74
|
+
const v = /^\$(\{[^}]*\}|[\w?^$]+)/.exec(src.slice(i));
|
|
75
|
+
if (v)
|
|
76
|
+
return { part: { variable: v[1] }, end: i + v[0].length };
|
|
77
|
+
return { part: { lit: '$' }, end: i + 1 };
|
|
78
|
+
}
|
|
79
|
+
function lex(src) {
|
|
80
|
+
const toks = [];
|
|
81
|
+
let parts = [];
|
|
82
|
+
let lit = '';
|
|
83
|
+
let quoted = false;
|
|
84
|
+
let started = false;
|
|
85
|
+
let start = 0;
|
|
86
|
+
let inner = null;
|
|
87
|
+
const atStatementStart = () => { const last = toks[toks.length - 1]; return last === undefined || last.k === 'sep'; };
|
|
88
|
+
const flushLit = () => { if (lit !== '') {
|
|
89
|
+
parts.push({ lit });
|
|
90
|
+
lit = '';
|
|
91
|
+
} };
|
|
92
|
+
const endWord = (at) => {
|
|
93
|
+
flushLit();
|
|
94
|
+
if (started) {
|
|
95
|
+
const s = src.slice(start, at);
|
|
96
|
+
toks.push({ k: 'word', parts, quoted, src: s, inner: inner ?? s });
|
|
97
|
+
}
|
|
98
|
+
parts = [];
|
|
99
|
+
quoted = false;
|
|
100
|
+
started = false;
|
|
101
|
+
inner = null;
|
|
102
|
+
};
|
|
103
|
+
const begin = (at) => { if (!started) {
|
|
104
|
+
started = true;
|
|
105
|
+
start = at;
|
|
106
|
+
} };
|
|
107
|
+
const sep = (text) => { toks.push({ k: 'sep', text }); };
|
|
108
|
+
let i = 0;
|
|
109
|
+
while (i < src.length) {
|
|
110
|
+
const c = src[i];
|
|
111
|
+
if (c === ' ' || c === '\t' || c === '\r') {
|
|
112
|
+
endWord(i);
|
|
113
|
+
i++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (c === '\n') {
|
|
117
|
+
endWord(i);
|
|
118
|
+
sep(';');
|
|
119
|
+
i++;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (c === '`') {
|
|
123
|
+
if (src[i + 1] === '\n' || (src[i + 1] === '\r' && src[i + 2] === '\n')) {
|
|
124
|
+
endWord(i);
|
|
125
|
+
i += src[i + 1] === '\n' ? 2 : 3;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
begin(i);
|
|
129
|
+
if (i + 1 < src.length)
|
|
130
|
+
lit += src[i + 1] === '\\' ? '/' : src[i + 1];
|
|
131
|
+
i += 2;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (!started && c === '#') {
|
|
135
|
+
while (i < src.length && src[i] !== '\n')
|
|
136
|
+
i++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (!started && c === '<' && src[i + 1] === '#') {
|
|
140
|
+
const e = src.indexOf('#>', i + 2);
|
|
141
|
+
if (e < 0)
|
|
142
|
+
throw new Unjudgeable('an unterminated <# comment');
|
|
143
|
+
i = e + 2;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (c === "'") {
|
|
147
|
+
const wasStarted = started;
|
|
148
|
+
begin(i);
|
|
149
|
+
let j = i + 1;
|
|
150
|
+
let text = '';
|
|
151
|
+
for (;;) {
|
|
152
|
+
if (j >= src.length)
|
|
153
|
+
throw new Unjudgeable('an unterminated quote');
|
|
154
|
+
if (src[j] === "'") {
|
|
155
|
+
if (src[j + 1] === "'") {
|
|
156
|
+
text += "'";
|
|
157
|
+
j += 2;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
text += src[j];
|
|
163
|
+
j++;
|
|
164
|
+
}
|
|
165
|
+
lit += text.replace(/\\/g, '/');
|
|
166
|
+
quoted = true;
|
|
167
|
+
if (!wasStarted && (j + 1 >= src.length || /[\s;|&)]/.test(src[j + 1])))
|
|
168
|
+
inner = text;
|
|
169
|
+
i = j + 1;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (c === '"') {
|
|
173
|
+
const wasStarted = started;
|
|
174
|
+
begin(i);
|
|
175
|
+
let j = i + 1;
|
|
176
|
+
for (;;) {
|
|
177
|
+
if (j >= src.length)
|
|
178
|
+
throw new Unjudgeable('an unterminated quote');
|
|
179
|
+
const d = src[j];
|
|
180
|
+
if (d === '"') {
|
|
181
|
+
if (src[j + 1] === '"') {
|
|
182
|
+
lit += '"';
|
|
183
|
+
j += 2;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
if (d === '`') {
|
|
189
|
+
if (j + 1 < src.length)
|
|
190
|
+
lit += src[j + 1] === '\\' ? '/' : src[j + 1];
|
|
191
|
+
j += 2;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (d === '$') {
|
|
195
|
+
const x = dollar(src, j);
|
|
196
|
+
if ('lit' in x.part)
|
|
197
|
+
lit += '$';
|
|
198
|
+
else {
|
|
199
|
+
flushLit();
|
|
200
|
+
parts.push(x.part);
|
|
201
|
+
}
|
|
202
|
+
j = x.end;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
lit += d === '\\' ? '/' : d;
|
|
206
|
+
j++;
|
|
207
|
+
}
|
|
208
|
+
quoted = true;
|
|
209
|
+
if (!wasStarted && (j + 1 >= src.length || /[\s;|&)]/.test(src[j + 1])))
|
|
210
|
+
inner = src.slice(i + 1, j);
|
|
211
|
+
i = j + 1;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (c === '$') {
|
|
215
|
+
begin(i);
|
|
216
|
+
const x = dollar(src, i);
|
|
217
|
+
if ('lit' in x.part)
|
|
218
|
+
lit += '$';
|
|
219
|
+
else {
|
|
220
|
+
flushLit();
|
|
221
|
+
parts.push(x.part);
|
|
222
|
+
}
|
|
223
|
+
i = x.end;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (c === ';') {
|
|
227
|
+
endWord(i);
|
|
228
|
+
sep(';');
|
|
229
|
+
i++;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (c === '|') {
|
|
233
|
+
endWord(i);
|
|
234
|
+
if (src[i + 1] === '|') {
|
|
235
|
+
sep('||');
|
|
236
|
+
i += 2;
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
sep('|');
|
|
240
|
+
i++;
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (c === '&') {
|
|
245
|
+
if (src[i + 1] === '&') {
|
|
246
|
+
endWord(i);
|
|
247
|
+
sep('&&');
|
|
248
|
+
i += 2;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (src[i + 1] === '1' || src[i + 1] === '2') {
|
|
252
|
+
begin(i);
|
|
253
|
+
lit += c;
|
|
254
|
+
i++;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
endWord(i);
|
|
258
|
+
// At a statement start a lone `&` is the call operator: kept as a marker word, so the quoted word after it is read
|
|
259
|
+
// as the command it calls (A-SPEC-790.1), then dropped. Elsewhere it ends the statement (a background job).
|
|
260
|
+
if (atStatementStart())
|
|
261
|
+
toks.push(litWord('&'));
|
|
262
|
+
else
|
|
263
|
+
sep(';');
|
|
264
|
+
i++;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (c === ')') {
|
|
268
|
+
endWord(i);
|
|
269
|
+
toks.push({ k: 'sep', text: ';', close: true });
|
|
270
|
+
i++;
|
|
271
|
+
continue;
|
|
272
|
+
} // A-SPEC-790.3
|
|
273
|
+
if (c === '(' || c === '{' || c === '}') {
|
|
274
|
+
endWord(i);
|
|
275
|
+
sep(';');
|
|
276
|
+
i++;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (c === '*' && src[i + 1] === '>' && !started) {
|
|
280
|
+
i++;
|
|
281
|
+
continue;
|
|
282
|
+
} // `*>` is `>`: the stream prefix is dropped
|
|
283
|
+
if (c === '>') {
|
|
284
|
+
// A one-digit word right before `>` is its stream number: `2>`.
|
|
285
|
+
let fd = '';
|
|
286
|
+
if (started && !quoted && parts.length === 0 && /^\d$/.test(lit)) {
|
|
287
|
+
fd = lit;
|
|
288
|
+
lit = '';
|
|
289
|
+
started = false;
|
|
290
|
+
parts = [];
|
|
291
|
+
}
|
|
292
|
+
else
|
|
293
|
+
endWord(i);
|
|
294
|
+
let op = fd + '>';
|
|
295
|
+
i++;
|
|
296
|
+
if (src[i] === '>') {
|
|
297
|
+
op += '>';
|
|
298
|
+
i++;
|
|
299
|
+
}
|
|
300
|
+
if (src[i] === '&' && /\d/.test(src[i + 1] ?? '')) {
|
|
301
|
+
op += '&' + src[i + 1];
|
|
302
|
+
i += 2;
|
|
303
|
+
}
|
|
304
|
+
toks.push({ k: 'redir', op });
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (!started && c === '[') {
|
|
308
|
+
const m = DOTNET_CALL.exec(src.slice(i));
|
|
309
|
+
if (m) {
|
|
310
|
+
const open = i + m[0].length - 1;
|
|
311
|
+
const end = closingParen(src, open);
|
|
312
|
+
if (end < 0)
|
|
313
|
+
throw new Unjudgeable('an unterminated .NET call');
|
|
314
|
+
const type = m[1].toLowerCase() === 'environment' ? 'environment' : m[1].toLowerCase() === 'io.file' ? 'file' : 'directory';
|
|
315
|
+
toks.push({ k: 'dotnet', type, method: m[2].toLowerCase(), args: dotnetArgs(src.slice(open + 1, end - 1)) });
|
|
316
|
+
i = end;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
begin(i);
|
|
321
|
+
lit += c === '\\' ? '/' : c;
|
|
322
|
+
i++;
|
|
323
|
+
}
|
|
324
|
+
endWord(src.length);
|
|
325
|
+
return toks;
|
|
326
|
+
}
|
|
327
|
+
/** A .NET call's arguments, split at top-level commas; each read as one PowerShell word. */
|
|
328
|
+
function dotnetArgs(text) {
|
|
329
|
+
const out = [];
|
|
330
|
+
let depth = 0;
|
|
331
|
+
let cur = '';
|
|
332
|
+
let q = null;
|
|
333
|
+
for (const c of text) {
|
|
334
|
+
if (q !== null) {
|
|
335
|
+
cur += c;
|
|
336
|
+
if (c === q)
|
|
337
|
+
q = null;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (c === "'" || c === '"') {
|
|
341
|
+
q = c;
|
|
342
|
+
cur += c;
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
if (c === '(')
|
|
346
|
+
depth++;
|
|
347
|
+
if (c === ')')
|
|
348
|
+
depth--;
|
|
349
|
+
if (c === ',' && depth === 0) {
|
|
350
|
+
out.push(cur);
|
|
351
|
+
cur = '';
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
cur += c;
|
|
355
|
+
}
|
|
356
|
+
if (cur.trim() !== '')
|
|
357
|
+
out.push(cur);
|
|
358
|
+
return out.map((a) => {
|
|
359
|
+
const words = lex(a.trim()).filter((t) => t.k === 'word');
|
|
360
|
+
return words[0] ?? litWord('');
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/** One word as shell text: bare when safe, double-quoted when it expands, single-quoted otherwise. */
|
|
364
|
+
function emitWord(w) {
|
|
365
|
+
const p = plain(w);
|
|
366
|
+
// A quoted word is literal: its glob characters stay quoted, so the shell rules do not read a pattern into it.
|
|
367
|
+
if (p !== null)
|
|
368
|
+
return p !== '' && SAFE_WORD.test(p) && !(w.quoted && /[*?[\]]/.test(p)) ? p : sq(p);
|
|
369
|
+
const only = w.parts.length === 1 ? w.parts[0] : undefined;
|
|
370
|
+
if (only !== undefined && 'variable' in only && only.variable.toLowerCase() === 'null')
|
|
371
|
+
return '/dev/null';
|
|
372
|
+
return '"' + w.parts.map((x) => ('lit' in x ? x.lit.replace(/[\\"$`]/g, (ch) => '\\' + ch)
|
|
373
|
+
: 'env' in x ? '$' + x.env : 'variable' in x ? '$' + x.variable : '$(' + x.sub + ')')).join('') + '"';
|
|
374
|
+
}
|
|
375
|
+
/** Named parameters by case-insensitive prefix (`-Rec`, `-Destination:b`), positional words, and unknown ones as written. */
|
|
376
|
+
function parse(args, spec) {
|
|
377
|
+
const named = new Map();
|
|
378
|
+
const switches = new Set();
|
|
379
|
+
const positional = [];
|
|
380
|
+
const unknown = [];
|
|
381
|
+
for (let i = 0; i < args.length; i++) {
|
|
382
|
+
const w = args[i];
|
|
383
|
+
const p = plain(w);
|
|
384
|
+
const m = !w.quoted && p !== null ? /^-([A-Za-z][\w-]*)(?::(.*))?$/s.exec(p) : null;
|
|
385
|
+
if (m === null) {
|
|
386
|
+
positional.push(w);
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const given = m[1].toLowerCase();
|
|
390
|
+
const pick = (names) => names.find((n) => n === given) ?? names.find((n) => n.startsWith(given));
|
|
391
|
+
const value = pick(spec.values);
|
|
392
|
+
const sw = value === undefined ? pick(spec.switches) : undefined;
|
|
393
|
+
if (value !== undefined) {
|
|
394
|
+
const v = m[2] !== undefined ? litWord(m[2]) : args[++i];
|
|
395
|
+
if (v !== undefined)
|
|
396
|
+
named.set(value, [...(named.get(value) ?? []), v]);
|
|
397
|
+
}
|
|
398
|
+
else if (sw !== undefined)
|
|
399
|
+
switches.add(sw);
|
|
400
|
+
else
|
|
401
|
+
unknown.push(emitWord(w));
|
|
402
|
+
}
|
|
403
|
+
return { named, switches, positional, unknown };
|
|
404
|
+
}
|
|
405
|
+
const ENV_PATH = /^env:\/?(\w*)$/i;
|
|
406
|
+
const envName = (w) => {
|
|
407
|
+
const p = w === undefined ? null : plain(w);
|
|
408
|
+
const m = p === null ? null : ENV_PATH.exec(p);
|
|
409
|
+
return m === null ? null : m[1];
|
|
410
|
+
};
|
|
411
|
+
const firstOf = (p, names) => names.flatMap((n) => p.named.get(n) ?? []);
|
|
412
|
+
const COMMON = ['filter', 'include', 'exclude', 'credential', 'stream', 'encoding'];
|
|
413
|
+
const COMMON_SW = ['force', 'whatif', 'confirm', 'passthru', 'verbose', 'debug'];
|
|
414
|
+
const pathsOf = (p) => [...firstOf(p, ['path', 'literalpath']), ...p.positional];
|
|
415
|
+
/**
|
|
416
|
+
* @implements A-SPEC-790.5 — with no path of its own, the cmdlet takes its target from the piped item (by property name):
|
|
417
|
+
* `Get-Item src/x.ts | Set-Content -Value r` became `ls -d src/x.ts | tee` and wrote code unseen (review, 2026-10-19).
|
|
418
|
+
*/
|
|
419
|
+
const pipelineTarget = (paths) => {
|
|
420
|
+
if (paths.length === 0)
|
|
421
|
+
throw new Unjudgeable('a cmdlet whose target comes through the pipeline');
|
|
422
|
+
};
|
|
423
|
+
const reader = (verb, extra) => (args) => {
|
|
424
|
+
const p = parse(args, { values: ['path', 'literalpath', ...COMMON, ...extra.values], switches: [...COMMON_SW, ...extra.switches] });
|
|
425
|
+
const paths = pathsOf(p);
|
|
426
|
+
const env = envName(paths[0]);
|
|
427
|
+
if (env !== null)
|
|
428
|
+
return env === '' ? ['env'] : ['printenv', env];
|
|
429
|
+
return [...verb, ...p.unknown, ...paths.map(emitWord)];
|
|
430
|
+
};
|
|
431
|
+
const removeItem = (args) => {
|
|
432
|
+
const p = parse(args, { values: ['path', 'literalpath', ...COMMON], switches: ['recurse', ...COMMON_SW] });
|
|
433
|
+
const paths = pathsOf(p);
|
|
434
|
+
pipelineTarget(paths);
|
|
435
|
+
const env = envName(paths[0]);
|
|
436
|
+
if (env !== null && env !== '')
|
|
437
|
+
return ['unset', env];
|
|
438
|
+
return ['rm', ...(p.switches.has('recurse') ? ['-r'] : []), ...(p.switches.has('force') ? ['-f'] : []), ...p.unknown, ...paths.map(emitWord)];
|
|
439
|
+
};
|
|
440
|
+
const copyOrMove = (verb) => (args) => {
|
|
441
|
+
const p = parse(args, { values: ['path', 'literalpath', 'destination', 'tosession', 'fromsession', ...COMMON], switches: ['recurse', 'container', ...COMMON_SW] });
|
|
442
|
+
const sources = pathsOf(p);
|
|
443
|
+
const named = p.named.get('destination');
|
|
444
|
+
const dest = named !== undefined ? named[named.length - 1] : sources.length >= 2 ? sources.pop() : litWord('.');
|
|
445
|
+
return [verb, ...(verb === 'cp' && p.switches.has('recurse') ? ['-r'] : []), ...p.unknown, ...sources.map(emitWord), emitWord(dest)];
|
|
446
|
+
};
|
|
447
|
+
const renameItem = (args) => {
|
|
448
|
+
const p = parse(args, { values: ['path', 'literalpath', 'newname', 'credential'], switches: [...COMMON_SW] });
|
|
449
|
+
const paths = pathsOf(p);
|
|
450
|
+
pipelineTarget(paths);
|
|
451
|
+
const target = paths[0];
|
|
452
|
+
const newName = p.named.get('newname')?.[0] ?? paths[1];
|
|
453
|
+
if (target === undefined || newName === undefined)
|
|
454
|
+
return ['mv', ...p.unknown, ...paths.map(emitWord)];
|
|
455
|
+
const t = plain(target);
|
|
456
|
+
const n = plain(newName);
|
|
457
|
+
const dir = t === null ? '' : t.slice(0, t.lastIndexOf('/') + 1);
|
|
458
|
+
const dest = n !== null && !n.includes('/') && dir !== '' ? emitWord(litWord(dir + n)) : emitWord(newName);
|
|
459
|
+
return ['mv', ...p.unknown, emitWord(target), dest];
|
|
460
|
+
};
|
|
461
|
+
const writer = (append) => (args) => {
|
|
462
|
+
const p = parse(args, {
|
|
463
|
+
values: ['path', 'literalpath', 'filepath', 'value', 'inputobject', 'delimiter', 'width', 'variable', ...COMMON],
|
|
464
|
+
switches: ['append', 'nonewline', 'noclobber', 'notypeinformation', ...COMMON_SW],
|
|
465
|
+
});
|
|
466
|
+
const named = firstOf(p, ['path', 'literalpath', 'filepath']);
|
|
467
|
+
const paths = named.length > 0 ? named : p.positional.slice(0, 1);
|
|
468
|
+
if (!p.named.has('variable'))
|
|
469
|
+
pipelineTarget(paths);
|
|
470
|
+
return ['tee', ...(append || p.switches.has('append') ? ['-a'] : []), ...p.unknown, ...paths.map(emitWord)];
|
|
471
|
+
};
|
|
472
|
+
const newItem = (args) => {
|
|
473
|
+
const p = parse(args, { values: ['path', 'name', 'itemtype', 'type', 'value', 'target', 'credential'], switches: [...COMMON_SW] });
|
|
474
|
+
const base = p.named.get('path')?.[0] ?? p.positional[0];
|
|
475
|
+
const name = p.named.get('name')?.[0];
|
|
476
|
+
const bp = base === undefined ? null : plain(base);
|
|
477
|
+
const np = name === undefined ? null : plain(name);
|
|
478
|
+
const where = base !== undefined && name !== undefined && bp !== null && np !== null ? litWord(bp.replace(/\/$/, '') + '/' + np) : (base ?? name);
|
|
479
|
+
const env = envName(where);
|
|
480
|
+
const value = p.named.get('value')?.[0] ?? p.named.get('target')?.[0];
|
|
481
|
+
if (env !== null && env !== '')
|
|
482
|
+
return ['export', `${env}=${value === undefined ? "''" : emitWord(value)}`];
|
|
483
|
+
const type = plain(p.named.get('itemtype')?.[0] ?? p.named.get('type')?.[0] ?? litWord('file'))?.toLowerCase() ?? 'file';
|
|
484
|
+
const at = where === undefined ? [] : [emitWord(where)];
|
|
485
|
+
if (type === 'directory' || type === 'd')
|
|
486
|
+
return ['mkdir', '-p', ...p.unknown, ...at];
|
|
487
|
+
if (['symboliclink', 'hardlink', 'junction'].includes(type))
|
|
488
|
+
return ['ln', '-s', ...p.unknown, ...(value === undefined ? [] : [emitWord(value)]), ...at];
|
|
489
|
+
return ['tee', ...p.unknown, ...at];
|
|
490
|
+
};
|
|
491
|
+
const setItem = (args) => {
|
|
492
|
+
const p = parse(args, { values: ['path', 'literalpath', 'value', ...COMMON], switches: [...COMMON_SW] });
|
|
493
|
+
const paths = pathsOf(p);
|
|
494
|
+
const env = envName(paths[0]);
|
|
495
|
+
const value = p.named.get('value')?.[0] ?? paths[1];
|
|
496
|
+
if (env !== null && env !== '')
|
|
497
|
+
return ['export', `${env}=${value === undefined ? "''" : emitWord(value)}`];
|
|
498
|
+
return ['tee', ...p.unknown, ...paths.slice(0, 1).map(emitWord)];
|
|
499
|
+
};
|
|
500
|
+
const selectString = (args) => {
|
|
501
|
+
const p = parse(args, { values: ['pattern', 'path', 'literalpath', 'include', 'exclude', 'encoding', 'context'],
|
|
502
|
+
switches: ['simplematch', 'casesensitive', 'quiet', 'list', 'notmatch', 'allmatches', 'raw'] });
|
|
503
|
+
const pos = [...p.positional];
|
|
504
|
+
const pattern = p.named.get('pattern')?.[0] ?? pos.shift();
|
|
505
|
+
const paths = [...firstOf(p, ['path', 'literalpath']), ...pos];
|
|
506
|
+
return ['grep', ...p.unknown, ...(pattern === undefined ? [] : [emitWord(pattern)]), ...paths.map(emitWord)];
|
|
507
|
+
};
|
|
508
|
+
/** Re-read a nested command's text — `iex`, `-Command`, `cmd /c` — and quote the result as one shell word. */
|
|
509
|
+
const nested = (words) => sq(translate(words.map((w) => w.inner).join(' ')));
|
|
510
|
+
const invokeExpression = (args) => {
|
|
511
|
+
const p = parse(args, { values: ['command'], switches: [] });
|
|
512
|
+
const words = [...(p.named.get('command') ?? []), ...p.positional];
|
|
513
|
+
if (words.length === 1 && plain(words[0]) === null)
|
|
514
|
+
return ['eval', emitWord(words[0])];
|
|
515
|
+
return ['eval', nested(words)];
|
|
516
|
+
};
|
|
517
|
+
const startProcess = (args) => {
|
|
518
|
+
const p = parse(args, {
|
|
519
|
+
values: ['filepath', 'argumentlist', 'workingdirectory', 'verb', 'credential', 'redirectstandardoutput', 'redirectstandardinput', 'redirectstandarderror', 'windowstyle'],
|
|
520
|
+
switches: ['wait', 'nonewwindow', 'passthru', 'usenewenvironment', 'loaduserprofile'],
|
|
521
|
+
});
|
|
522
|
+
const pos = [...p.positional];
|
|
523
|
+
const file = p.named.get('filepath')?.[0] ?? pos.shift();
|
|
524
|
+
const argWords = [...(p.named.get('argumentlist') ?? []), ...pos];
|
|
525
|
+
const splitArgs = argWords.flatMap((w) => { const t = plain(w); return t === null ? [emitWord(w)] : t.split(',').filter((s) => s !== '').map((s) => emitWord(litWord(s))); });
|
|
526
|
+
const out = [...firstOf(p, ['redirectstandardoutput', 'redirectstandarderror'])].map((w) => '> ' + emitWord(w));
|
|
527
|
+
return [...(file === undefined ? [] : [emitWord(file)]), ...splitArgs, ...out];
|
|
528
|
+
};
|
|
529
|
+
const POWERSHELL_VALUE_SWITCHES = ['executionpolicy', 'ep', 'ex', 'workingdirectory', 'wd', 'outputformat', 'of', 'inputformat', 'if',
|
|
530
|
+
'windowstyle', 'w', 'configurationname', 'settingsfile', 'custompipename', 'psconsolefile', 'version', 'v'];
|
|
531
|
+
const powershellExe = (args) => {
|
|
532
|
+
for (let i = 0; i < args.length; i++) {
|
|
533
|
+
const w = args[i];
|
|
534
|
+
const p = plain(w);
|
|
535
|
+
const flag = !w.quoted && p !== null && /^[-/]/.test(p) ? p.slice(1).toLowerCase() : null;
|
|
536
|
+
if (flag === null) {
|
|
537
|
+
const rest = args.slice(i);
|
|
538
|
+
return p !== null && /\.ps1$/i.test(p) ? ['sh', ...rest.map(emitWord)] : ['sh', '-c', nested(rest)];
|
|
539
|
+
}
|
|
540
|
+
if (flag === 'e' || flag === 'ec' || (flag.length >= 3 && 'encodedcommand'.startsWith(flag)))
|
|
541
|
+
throw new Unjudgeable('an encoded command');
|
|
542
|
+
if (flag === 'c' || flag === 'command' || (flag.length >= 3 && 'command'.startsWith(flag)))
|
|
543
|
+
return ['sh', '-c', nested(args.slice(i + 1))];
|
|
544
|
+
if (flag === 'f' || flag === 'file' || flag === 'fi' || flag === 'fil')
|
|
545
|
+
return ['sh', ...args.slice(i + 1).map(emitWord)];
|
|
546
|
+
if (POWERSHELL_VALUE_SWITCHES.includes(flag))
|
|
547
|
+
i++;
|
|
548
|
+
}
|
|
549
|
+
return [];
|
|
550
|
+
};
|
|
551
|
+
const cmdExe = (args) => {
|
|
552
|
+
const at = args.findIndex((w) => /^\/[ckr]$/i.test(plain(w) ?? ''));
|
|
553
|
+
if (at < 0)
|
|
554
|
+
return ['cmd', ...args.map(emitWord)];
|
|
555
|
+
return ['sh', '-c', nested(args.slice(at + 1))];
|
|
556
|
+
};
|
|
557
|
+
/**
|
|
558
|
+
* @implements A-SPEC-790.1 — a cmdlet that only shapes the objects in its pipeline reads no file: its verb, plus only
|
|
559
|
+
* the argument words that expand something, so an environment read inside a filter stays visible to the secret rules.
|
|
560
|
+
*/
|
|
561
|
+
const shaper = (verb) => (args) => [verb, ...args.filter((w) => plain(w) === null).map(emitWord)];
|
|
562
|
+
const echoes = (args) => ['echo', ...args.map(emitWord)];
|
|
563
|
+
const location = (args) => {
|
|
564
|
+
const p = parse(args, { values: ['path', 'literalpath', 'stackname'], switches: ['passthru'] });
|
|
565
|
+
return ['cd', ...pathsOf(p).slice(0, 1).map(emitWord)];
|
|
566
|
+
};
|
|
567
|
+
/**
|
|
568
|
+
* @implements A-SPEC-790.6 — `-WhatIf` only prints what the cmdlet would do: `Remove-Item -WhatIf -Recurse .ax/specs` was
|
|
569
|
+
* refused as the delete it previews (review, 2026-10-19). The preview reads the words the mutating translation names, so
|
|
570
|
+
* a `$( )` word in them is still judged; `-WhatIf:$false` is the act itself.
|
|
571
|
+
*/
|
|
572
|
+
const WHATIF_ON = /^-(?:whatif|wi)(?::\$true)?$/i;
|
|
573
|
+
const WHATIF_OFF = /^-(?:whatif|wi):\$false$/i;
|
|
574
|
+
const previewable = (mapper) => (args) => {
|
|
575
|
+
const preview = args.some((w) => WHATIF_ON.test(w.src));
|
|
576
|
+
const out = mapper(args.filter((w) => !WHATIF_ON.test(w.src) && !WHATIF_OFF.test(w.src)));
|
|
577
|
+
return preview ? ['ls', '-d', ...out.slice(1).filter((t) => !t.startsWith('-'))] : out;
|
|
578
|
+
};
|
|
579
|
+
const MAPPERS = [
|
|
580
|
+
[['select-object', 'select'], shaper('head')],
|
|
581
|
+
[['where-object', 'where', '?'], shaper('grep')],
|
|
582
|
+
[['sort-object', 'sort'], shaper('sort')],
|
|
583
|
+
[['measure-object', 'measure'], shaper('wc')],
|
|
584
|
+
[['foreach-object', 'foreach', '%', 'group-object', 'group', 'format-list', 'fl', 'format-table', 'ft', 'format-wide', 'fw', 'format-custom', 'fc',
|
|
585
|
+
'out-string', 'out-host', 'oh', 'out-null', 'convertfrom-json', 'convertto-json', 'convertfrom-csv', 'convertto-csv', 'get-unique', 'gu', 'join-string'], shaper('cat')],
|
|
586
|
+
[['write-output', 'write', 'echo', 'write-host', 'write-verbose', 'write-warning', 'write-information', 'join-path', 'split-path', 'get-date',
|
|
587
|
+
'get-location', 'gl', 'pwd', 'get-command', 'gcm', 'get-member', 'gm'], echoes],
|
|
588
|
+
[['set-location', 'sl', 'cd', 'chdir', 'push-location', 'pushd'], location],
|
|
589
|
+
[['pop-location', 'popd'], () => ['popd']],
|
|
590
|
+
[['remove-item', 'ri', 'rm', 'rmdir', 'rd', 'del', 'erase'], previewable(removeItem)],
|
|
591
|
+
[['copy-item', 'copy', 'cp', 'cpi'], previewable(copyOrMove('cp'))],
|
|
592
|
+
[['move-item', 'move', 'mv', 'mi'], previewable(copyOrMove('mv'))],
|
|
593
|
+
[['rename-item', 'ren', 'rni'], previewable(renameItem)],
|
|
594
|
+
[['set-content', 'sc', 'clear-content', 'clc', 'out-file', 'tee-object', 'tee', 'export-csv', 'epcsv', 'export-clixml'], previewable(writer(false))],
|
|
595
|
+
[['add-content', 'ac'], previewable(writer(true))],
|
|
596
|
+
[['new-item', 'ni'], previewable(newItem)],
|
|
597
|
+
[['mkdir', 'md'], previewable((args) => { const p = parse(args, { values: ['path'], switches: [...COMMON_SW] }); return ['mkdir', '-p', ...p.unknown, ...pathsOf(p).map(emitWord)]; })],
|
|
598
|
+
[['get-content', 'gc', 'cat', 'type'], reader(['cat'], { values: ['totalcount', 'tail', 'head', 'first', 'last', 'readcount', 'delimiter'], switches: ['raw', 'wait', 'asbytestream'] })],
|
|
599
|
+
[['get-childitem', 'gci', 'dir', 'ls'], reader(['ls'], { values: ['depth', 'attributes'], switches: ['recurse', 'name', 'directory', 'file', 'hidden', 'readonly', 'system', 'followsymlink'] })],
|
|
600
|
+
[['get-item', 'gi'], reader(['ls', '-d'], { values: [], switches: [] })],
|
|
601
|
+
[['select-string', 'sls'], selectString],
|
|
602
|
+
[['test-path'], reader(['test', '-e'], { values: ['pathtype'], switches: ['isvalid'] })],
|
|
603
|
+
[['resolve-path', 'rvpa'], reader(['realpath'], { values: [], switches: ['relative'] })],
|
|
604
|
+
[['get-filehash'], reader(['sha256sum'], { values: ['algorithm'], switches: [] })],
|
|
605
|
+
[['set-item', 'si'], setItem],
|
|
606
|
+
[['invoke-expression', 'iex'], invokeExpression],
|
|
607
|
+
[['start-process', 'saps', 'start'], startProcess],
|
|
608
|
+
[['powershell', 'pwsh'], powershellExe],
|
|
609
|
+
[['cmd'], cmdExe],
|
|
610
|
+
];
|
|
611
|
+
const MAPPER_OF = new Map(MAPPERS.flatMap(([names, m]) => names.map((n) => [n, m])));
|
|
612
|
+
/** The command word as PowerShell resolves it: a `Module\` qualifier and an `.exe` suffix do not change the command. */
|
|
613
|
+
function commandName(w) {
|
|
614
|
+
const p = plain(w);
|
|
615
|
+
if (p === null)
|
|
616
|
+
return null;
|
|
617
|
+
let name = p.toLowerCase();
|
|
618
|
+
const slash = name.lastIndexOf('/');
|
|
619
|
+
if (slash > 0 && /^[a-z][\w.]*$/.test(name.slice(0, slash)) && MAPPER_OF.has(name.slice(slash + 1)))
|
|
620
|
+
name = name.slice(slash + 1);
|
|
621
|
+
if (!name.includes('/') && name.endsWith('.exe'))
|
|
622
|
+
name = name.slice(0, -4);
|
|
623
|
+
return name;
|
|
624
|
+
}
|
|
625
|
+
function dotnet(t, rest) {
|
|
626
|
+
const a = t.args.map(emitWord);
|
|
627
|
+
const m = t.method;
|
|
628
|
+
if (t.type === 'environment') {
|
|
629
|
+
if (m === 'getenvironmentvariables')
|
|
630
|
+
return ['env'];
|
|
631
|
+
if (m === 'getenvironmentvariable')
|
|
632
|
+
return ['printenv', ...t.args.slice(0, 1).map((w) => plain(w) ?? emitWord(w))];
|
|
633
|
+
if (m === 'setenvironmentvariable')
|
|
634
|
+
return ['export', `${plain(t.args[0] ?? litWord('')) ?? emitWord(t.args[0])}=${a[1] ?? "''"}`];
|
|
635
|
+
}
|
|
636
|
+
else if (t.type === 'file') {
|
|
637
|
+
if (/^(writeall|appendall|create|appendtext|openwrite)/.test(m))
|
|
638
|
+
return ['tee', ...a.slice(0, 1)];
|
|
639
|
+
if (m === 'delete')
|
|
640
|
+
return ['rm', ...a.slice(0, 1)];
|
|
641
|
+
if (m === 'copy')
|
|
642
|
+
return ['cp', ...a.slice(0, 2)];
|
|
643
|
+
if (m === 'move' || m === 'replace')
|
|
644
|
+
return ['mv', ...a.slice(0, 2)];
|
|
645
|
+
if (/^(readall|readlines|exists|openread|opentext)/.test(m))
|
|
646
|
+
return ['cat', ...a.slice(0, 1)];
|
|
647
|
+
}
|
|
648
|
+
else {
|
|
649
|
+
if (m === 'delete')
|
|
650
|
+
return ['rm', '-r', ...a.slice(0, 1)];
|
|
651
|
+
if (m === 'move')
|
|
652
|
+
return ['mv', ...a.slice(0, 2)];
|
|
653
|
+
if (m === 'createdirectory')
|
|
654
|
+
return ['mkdir', '-p', ...a.slice(0, 1)];
|
|
655
|
+
if (/^(exists|get|enumerate)/.test(m))
|
|
656
|
+
return ['ls', ...a.slice(0, 1)];
|
|
657
|
+
}
|
|
658
|
+
// A call this table does not know: its arguments are still shown to the rules, as words of an unknown command.
|
|
659
|
+
return [sq(`[${t.type}]::${t.method}`), ...a, ...rest.map(emitWord)];
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* @implements A-SPEC-790.1 — a statement whose first word is a string, an expansion or a number is an expression: it
|
|
663
|
+
* outputs a value, so it is read as `echo` of its words — unless a word ends in a method that changes a file, which
|
|
664
|
+
* stays as written (an unknown command, refused near `.ax`): `(Get-Item x).Delete()` is not a read.
|
|
665
|
+
*/
|
|
666
|
+
const MUTATING_METHOD = /\.(delete|moveto|copyto|create|createsubdirectory|createtext|appendtext|open|openwrite|replace|encrypt|decrypt|setaccesscontrol|setattributes|attributes)$/i;
|
|
667
|
+
const shape = (w) => w.parts.map((x) => ('lit' in x ? x.lit : 'env' in x ? '$env:' + x.env : 'variable' in x ? '$' + x.variable : '$()')).join('');
|
|
668
|
+
const isExpression = (w) => {
|
|
669
|
+
if (w.quoted)
|
|
670
|
+
return true;
|
|
671
|
+
const p = plain(w);
|
|
672
|
+
return p === null ? !('lit' in w.parts[0]) : /^-?\d[\d.]*$/.test(p);
|
|
673
|
+
};
|
|
674
|
+
/**
|
|
675
|
+
* @implements A-SPEC-790.3 — a member call that changes a file, as the head of its own statement: `.Delete` after
|
|
676
|
+
* `(Get-Item x)`. On main the Bash rules read `.ax` clause by clause (A-SPEC-747.3), so this clause must name the path.
|
|
677
|
+
*/
|
|
678
|
+
const MUTATING_MEMBER = (w) => {
|
|
679
|
+
if (w === undefined || w.k !== 'word' || w.quoted)
|
|
680
|
+
return false;
|
|
681
|
+
const p = plain(w);
|
|
682
|
+
return p !== null && /^\.[A-Za-z]\w*$/.test(p) && MUTATING_METHOD.test(p);
|
|
683
|
+
};
|
|
684
|
+
/**
|
|
685
|
+
* @implements A-SPEC-790.4 — members that only read a value. Emitted as a word they made a read over `.ax` behind a `cd`
|
|
686
|
+
* hard-hitl (measured 2026-10-19); none of them is in `MUTATING_METHOD`.
|
|
687
|
+
*/
|
|
688
|
+
const READ_MEMBER = new Set(['count', 'length', 'name', 'fullname', 'basename', 'extension', 'directoryname', 'exists',
|
|
689
|
+
'lastwritetime', 'lastaccesstime', 'creationtime', 'mode', 'lines', 'words', 'characters', 'sum', 'average', 'maximum',
|
|
690
|
+
'minimum', 'trim', 'trimstart', 'trimend', 'tostring', 'toupper', 'tolower', 'split', 'substring', 'contains',
|
|
691
|
+
'startswith', 'endswith', 'indexof']);
|
|
692
|
+
/** One statement's shell text, and its argument words (what a member call after it acts on, when a `)` closed it). */
|
|
693
|
+
function statement(toks, group = [], afterGroup = false) {
|
|
694
|
+
const words = [];
|
|
695
|
+
const redirs = [];
|
|
696
|
+
for (let i = 0; i < toks.length; i++) {
|
|
697
|
+
const t = toks[i];
|
|
698
|
+
if (t.k === 'redir') {
|
|
699
|
+
const target = toks[i + 1];
|
|
700
|
+
if (target !== undefined && target.k === 'word') {
|
|
701
|
+
redirs.push(`${t.op} ${emitWord(target)}`);
|
|
702
|
+
i++;
|
|
703
|
+
}
|
|
704
|
+
else
|
|
705
|
+
redirs.push(t.op);
|
|
706
|
+
}
|
|
707
|
+
else
|
|
708
|
+
words.push(t);
|
|
709
|
+
}
|
|
710
|
+
// A dot-source word and a leading `$var =` assignment do not change the act; `$env:X = v` is an export.
|
|
711
|
+
const marker = words[0]?.k === 'word' && !words[0].quoted ? plain(words[0]) : null;
|
|
712
|
+
const called = marker === '.' || marker === '&';
|
|
713
|
+
if (called)
|
|
714
|
+
words.shift();
|
|
715
|
+
const first = words[0];
|
|
716
|
+
const second = words[1];
|
|
717
|
+
const isEq = (t) => t !== undefined && t.k === 'word' && plain(t) === '=';
|
|
718
|
+
if (first?.k === 'word' && first.parts.length === 1 && isEq(second)) {
|
|
719
|
+
const only = first.parts[0];
|
|
720
|
+
if ('env' in only) {
|
|
721
|
+
const value = words.slice(2).filter((t) => t.k === 'word');
|
|
722
|
+
return { text: ['export', `${only.env}=${value.length === 0 ? "''" : value.map(emitWord).join(' ')}`, ...redirs].join(' '), args: [] };
|
|
723
|
+
}
|
|
724
|
+
if ('variable' in only)
|
|
725
|
+
words.splice(0, 2);
|
|
726
|
+
}
|
|
727
|
+
const head = words[0];
|
|
728
|
+
const rest = words.slice(1).filter((t) => t.k === 'word');
|
|
729
|
+
// A-SPEC-790.4: a lone read-only member after a `)` group outputs a value and acts on nothing; it is not emitted.
|
|
730
|
+
if (afterGroup && !called && words.length === 1 && redirs.length === 0 && head?.k === 'word' && !head.quoted) {
|
|
731
|
+
const m = /^\.([A-Za-z]\w*)$/.exec(plain(head) ?? '');
|
|
732
|
+
if (m !== null && READ_MEMBER.has(m[1].toLowerCase()))
|
|
733
|
+
return { text: '', args: [] };
|
|
734
|
+
}
|
|
735
|
+
let out;
|
|
736
|
+
if (head === undefined)
|
|
737
|
+
out = [];
|
|
738
|
+
else if (head.k === 'dotnet')
|
|
739
|
+
out = dotnet(head, rest);
|
|
740
|
+
else if (!called && MUTATING_MEMBER(head))
|
|
741
|
+
out = [emitWord(head), ...group, ...rest.map(emitWord)]; // A-SPEC-790.3
|
|
742
|
+
else if (head.k === 'word' && !called && isExpression(head) && !words.some((t) => t.k === 'word' && MUTATING_METHOD.test(shape(t)))) {
|
|
743
|
+
out = ['echo', ...words.map((t) => (t.k === 'word' ? emitWord(t) : dotnet(t, []).join(' ')))];
|
|
744
|
+
}
|
|
745
|
+
else if (head.k === 'word') {
|
|
746
|
+
const name = commandName(head);
|
|
747
|
+
const mapper = name === null ? undefined : MAPPER_OF.get(name);
|
|
748
|
+
if (mapper !== undefined)
|
|
749
|
+
out = mapper(rest);
|
|
750
|
+
else {
|
|
751
|
+
const p = plain(head);
|
|
752
|
+
const shown = p !== null && !p.includes('/') && /\.exe$/i.test(p) ? emitWord(litWord(p.slice(0, -4))) : emitWord(head);
|
|
753
|
+
out = [shown, ...words.slice(1).map((t) => (t.k === 'word' ? emitWord(t) : dotnet(t, []).join(' ')))];
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
else
|
|
757
|
+
out = [];
|
|
758
|
+
return { text: [...out, ...redirs].join(' '), args: out.slice(1) };
|
|
759
|
+
}
|
|
760
|
+
function translate(command) {
|
|
761
|
+
const toks = lex(command);
|
|
762
|
+
const pieces = [];
|
|
763
|
+
let pendingSep = null;
|
|
764
|
+
let current = [];
|
|
765
|
+
let group = []; // A-SPEC-790.3: the arguments of the last statement a `)` closed
|
|
766
|
+
let afterGroup = false; // A-SPEC-790.4: the last emitted statement was closed by `)`
|
|
767
|
+
const flush = (close) => {
|
|
768
|
+
const { text, args } = statement(current, group, afterGroup);
|
|
769
|
+
current = [];
|
|
770
|
+
if (text === '')
|
|
771
|
+
return;
|
|
772
|
+
group = close ? args : [];
|
|
773
|
+
afterGroup = close;
|
|
774
|
+
if (pieces.length > 0)
|
|
775
|
+
pieces.push(pendingSep ?? ';');
|
|
776
|
+
pieces.push(text);
|
|
777
|
+
pendingSep = null;
|
|
778
|
+
};
|
|
779
|
+
for (const t of toks) {
|
|
780
|
+
if (t.k === 'sep') {
|
|
781
|
+
flush(t.close === true);
|
|
782
|
+
if (pieces.length > 0)
|
|
783
|
+
pendingSep = t.text;
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
current.push(t);
|
|
787
|
+
}
|
|
788
|
+
flush(false);
|
|
789
|
+
return pieces.join(' ');
|
|
790
|
+
}
|
|
791
|
+
/** PowerShell text as the shell text whose acts it performs, or why it cannot be read. */
|
|
792
|
+
function powershellAsShell(command) {
|
|
793
|
+
try {
|
|
794
|
+
return { shell: translate(command) };
|
|
795
|
+
}
|
|
796
|
+
catch (e) {
|
|
797
|
+
if (e instanceof Unjudgeable)
|
|
798
|
+
return { unjudgeable: e.message };
|
|
799
|
+
return { unjudgeable: 'the text could not be read' };
|
|
800
|
+
}
|
|
801
|
+
}
|