@tenonai/yew 0.0.0-stage → 1.4.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 +296 -0
- package/LICENSE +21 -0
- package/README.md +55 -2
- package/THIRD-PARTY-NOTICES.md +122 -0
- package/bin/yew.js +9 -0
- package/dist/cli/app.js +198 -0
- package/dist/cli/export.js +57 -0
- package/dist/cli/io.js +65 -0
- package/dist/cli/key.js +113 -0
- package/dist/cli/main.js +44 -0
- package/dist/cli/mcp.js +76 -0
- package/dist/cli/remote.js +116 -0
- package/dist/cli/run.js +72 -0
- package/dist/cli/schedule.js +123 -0
- package/dist/cli/send.js +126 -0
- package/dist/cli/serve.js +301 -0
- package/dist/cli/service.js +241 -0
- package/dist/cli/system.js +44 -0
- package/dist/remote/access.js +107 -0
- package/dist/remote/connector.js +278 -0
- package/dist/remote/identity.js +71 -0
- package/dist/remote/noise.js +287 -0
- package/dist/remote/protocol.js +51 -0
- package/dist/remote/push.js +37 -0
- package/dist/server/agent/compaction.js +242 -0
- package/dist/server/agent/context.js +119 -0
- package/dist/server/agent/held.js +48 -0
- package/dist/server/agent/helper.js +122 -0
- package/dist/server/agent/mailroom.js +510 -0
- package/dist/server/agent/prompt.js +268 -0
- package/dist/server/agent/runner.js +1159 -0
- package/dist/server/artifacts.js +216 -0
- package/dist/server/budget.js +88 -0
- package/dist/server/data-export.js +105 -0
- package/dist/server/data-usage.js +143 -0
- package/dist/server/db/database.js +49 -0
- package/dist/server/db/migrations.js +347 -0
- package/dist/server/errors.js +16 -0
- package/dist/server/events.js +17 -0
- package/dist/server/export.js +378 -0
- package/dist/server/folder-picker.js +32 -0
- package/dist/server/from-jig.js +98 -0
- package/dist/server/http/agents.js +78 -0
- package/dist/server/http/api.js +468 -0
- package/dist/server/http/app.js +111 -0
- package/dist/server/http/artifacts.js +53 -0
- package/dist/server/http/data.js +54 -0
- package/dist/server/http/devices.js +47 -0
- package/dist/server/http/events.js +134 -0
- package/dist/server/http/idempotency.js +53 -0
- package/dist/server/http/input.js +106 -0
- package/dist/server/http/mail.js +52 -0
- package/dist/server/http/mcp.js +134 -0
- package/dist/server/http/notes.js +91 -0
- package/dist/server/http/problem.js +55 -0
- package/dist/server/http/providers.js +131 -0
- package/dist/server/http/schedules.js +58 -0
- package/dist/server/http/search.js +42 -0
- package/dist/server/http/security.js +168 -0
- package/dist/server/http/skills.js +53 -0
- package/dist/server/http/system.js +26 -0
- package/dist/server/ids.js +30 -0
- package/dist/server/keys.js +51 -0
- package/dist/server/mail/envelope.js +152 -0
- package/dist/server/mail/post.js +603 -0
- package/dist/server/mail/store.js +279 -0
- package/dist/server/manage.js +601 -0
- package/dist/server/mcp-builtin.js +179 -0
- package/dist/server/mcp-config.js +305 -0
- package/dist/server/mcp-oauth.js +448 -0
- package/dist/server/mcp.js +668 -0
- package/dist/server/model/gateway.js +410 -0
- package/dist/server/paths.js +34 -0
- package/dist/server/permissions/engine.js +249 -0
- package/dist/server/permissions/paths.js +93 -0
- package/dist/server/permissions/shell.js +635 -0
- package/dist/server/push.js +345 -0
- package/dist/server/reveal.js +36 -0
- package/dist/server/runtime.js +17 -0
- package/dist/server/schedules.js +356 -0
- package/dist/server/settings.js +80 -0
- package/dist/server/skills.js +262 -0
- package/dist/server/store.js +1077 -0
- package/dist/server/tools/files.js +318 -0
- package/dist/server/tools/html.js +221 -0
- package/dist/server/tools/jobs.js +38 -0
- package/dist/server/tools/mail.js +680 -0
- package/dist/server/tools/match.js +81 -0
- package/dist/server/tools/misc.js +253 -0
- package/dist/server/tools/process.js +128 -0
- package/dist/server/tools/read-state.js +40 -0
- package/dist/server/tools/registry.js +43 -0
- package/dist/server/tools/search.js +175 -0
- package/dist/server/tools/shell.js +124 -0
- package/dist/server/tools/types.js +43 -0
- package/dist/server/tools/web-search.js +198 -0
- package/dist/server/tools/web.js +240 -0
- package/dist/shared/cron.js +105 -0
- package/dist/shared/mcp-presets.js +170 -0
- package/dist/shared/messages.js +45 -0
- package/dist/shared/policy.js +42 -0
- package/dist/shared/types.js +34 -0
- package/dist/shared/version.js +8 -0
- package/mcp-builtin/package-lock.json +3398 -0
- package/mcp-builtin/package.json +14 -0
- package/mcp-builtin/presets/gitee/package-lock.json +105 -0
- package/mcp-builtin/presets/gitee/package.json +10 -0
- package/mcp-builtin/presets/notion/package-lock.json +2243 -0
- package/mcp-builtin/presets/notion/package.json +10 -0
- package/mcp-builtin/presets/sentry/package-lock.json +28 -0
- package/mcp-builtin/presets/sentry/package.json +10 -0
- package/package.json +75 -5
- package/web/dist/THIRD-PARTY-NOTICES.txt +1944 -0
- package/web/dist/assets/index-B9oWPSod.css +1 -0
- package/web/dist/assets/index-C5qBMzFX.js +68 -0
- package/web/dist/icons/app-icon.png +0 -0
- package/web/dist/icons/apple-touch-icon.png +0 -0
- package/web/dist/icons/favicon-16.png +0 -0
- package/web/dist/icons/favicon-32.png +0 -0
- package/web/dist/icons/favicon-64.png +0 -0
- package/web/dist/icons/tenon-mark.svg +1 -0
- package/web/dist/icons/yew-mark.svg +1 -0
- package/web/dist/index.html +18 -0
- package/web/dist/mascot/gift.webp +0 -0
- package/web/dist/mascot/greeting.webp +0 -0
- package/web/dist/mascot/happy.webp +0 -0
- package/web/dist/mascot/lost.webp +0 -0
- package/web/dist/mascot/shush.webp +0 -0
- package/web/dist/mascot/sleepy.webp +0 -0
- package/web/dist/mascot/sorry.webp +0 -0
- package/web/dist/mascot/thinking.webp +0 -0
- package/web/dist/mascot/waking.webp +0 -0
- package/web/dist/mascot/writing.webp +0 -0
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
const OPERATOR_CHARS = new Set(["&", "|", ";", "\n"]);
|
|
3
|
+
/**
|
|
4
|
+
* `$(cat <<'EOF' … EOF)`: a way to write a long literal argument (a commit message, say). With a
|
|
5
|
+
* quoted delimiter nothing in the text is expanded and `cat` only hands it back, so it is the text.
|
|
6
|
+
*/
|
|
7
|
+
const LITERAL_TEXT = /^\$\(\s*cat\s*<<-?\s*(['"])([A-Za-z_][A-Za-z0-9_]*)\1[ \t]*\n([\s\S]*?)\n[ \t]*\2[ \t]*\n?\s*\)/;
|
|
8
|
+
export function parseShell(command) {
|
|
9
|
+
const segments = [];
|
|
10
|
+
let segment = { argv: [], redirects: [], dynamic: false };
|
|
11
|
+
let word = "";
|
|
12
|
+
let inWord = false;
|
|
13
|
+
let pendingRedirect = null;
|
|
14
|
+
let reason;
|
|
15
|
+
/** Here-documents opened on the current line; their text starts on the next one. */
|
|
16
|
+
const opened = [];
|
|
17
|
+
const fail = (why) => {
|
|
18
|
+
reason ??= why;
|
|
19
|
+
};
|
|
20
|
+
const endWord = () => {
|
|
21
|
+
if (!inWord)
|
|
22
|
+
return;
|
|
23
|
+
if (pendingRedirect) {
|
|
24
|
+
segment.redirects.push({ op: pendingRedirect, target: word });
|
|
25
|
+
pendingRedirect = null;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
segment.argv.push(word);
|
|
29
|
+
}
|
|
30
|
+
word = "";
|
|
31
|
+
inWord = false;
|
|
32
|
+
};
|
|
33
|
+
const endSegment = () => {
|
|
34
|
+
endWord();
|
|
35
|
+
if (pendingRedirect)
|
|
36
|
+
fail("a redirect without a target");
|
|
37
|
+
if (segment.argv.length > 0 || segment.redirects.length > 0)
|
|
38
|
+
segments.push(segment);
|
|
39
|
+
segment = { argv: [], redirects: [], dynamic: false };
|
|
40
|
+
};
|
|
41
|
+
let i = 0;
|
|
42
|
+
while (i < command.length) {
|
|
43
|
+
const c = command[i];
|
|
44
|
+
const next = command[i + 1];
|
|
45
|
+
if (c === "\\") {
|
|
46
|
+
if (next === "\n") {
|
|
47
|
+
i += 2;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (next !== undefined)
|
|
51
|
+
word += next;
|
|
52
|
+
inWord = true;
|
|
53
|
+
i += 2;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (c === "'") {
|
|
57
|
+
const end = command.indexOf("'", i + 1);
|
|
58
|
+
if (end < 0) {
|
|
59
|
+
fail("an unclosed single quote");
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
word += command.slice(i + 1, end);
|
|
63
|
+
inWord = true;
|
|
64
|
+
i = end + 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (c === '"') {
|
|
68
|
+
let j = i + 1;
|
|
69
|
+
let closed = false;
|
|
70
|
+
while (j < command.length) {
|
|
71
|
+
const d = command[j];
|
|
72
|
+
if (d === "\\" && j + 1 < command.length) {
|
|
73
|
+
const escaped = command[j + 1];
|
|
74
|
+
word += '"\\$`'.includes(escaped) ? escaped : `\\${escaped}`;
|
|
75
|
+
j += 2;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (d === '"') {
|
|
79
|
+
closed = true;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
if (d === "$") {
|
|
83
|
+
const literal = LITERAL_TEXT.exec(command.slice(j));
|
|
84
|
+
if (literal) {
|
|
85
|
+
word += literal[3];
|
|
86
|
+
j += literal[0].length;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (d === "$" || d === "`")
|
|
91
|
+
segment.dynamic = true;
|
|
92
|
+
word += d;
|
|
93
|
+
j++;
|
|
94
|
+
}
|
|
95
|
+
if (!closed) {
|
|
96
|
+
fail("an unclosed double quote");
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
inWord = true;
|
|
100
|
+
i = j + 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (c === "$") {
|
|
104
|
+
const literal = LITERAL_TEXT.exec(command.slice(i));
|
|
105
|
+
if (literal) {
|
|
106
|
+
word += literal[3];
|
|
107
|
+
inWord = true;
|
|
108
|
+
i += literal[0].length;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (c === "$" || c === "`") {
|
|
113
|
+
segment.dynamic = true;
|
|
114
|
+
word += c;
|
|
115
|
+
inWord = true;
|
|
116
|
+
i++;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (c === "#" && !inWord) {
|
|
120
|
+
const end = command.indexOf("\n", i);
|
|
121
|
+
i = end < 0 ? command.length : end;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (c === " " || c === "\t") {
|
|
125
|
+
endWord();
|
|
126
|
+
i++;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (c === "(" || c === ")" || c === "{" || c === "}") {
|
|
130
|
+
if (c === "{" || c === "}") {
|
|
131
|
+
// Braces inside a word (`a{b,c}` or `${x}`) are fine; on their own they start a group.
|
|
132
|
+
if (inWord) {
|
|
133
|
+
word += c;
|
|
134
|
+
i++;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
fail("a subshell or command group");
|
|
139
|
+
word += c;
|
|
140
|
+
inWord = true;
|
|
141
|
+
i++;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (c === "<" || c === ">") {
|
|
145
|
+
if (c === "<" && next === "<" && command[i + 2] === "<") {
|
|
146
|
+
// A here-string: the next word is the command's input.
|
|
147
|
+
endWord();
|
|
148
|
+
pendingRedirect = "<<<";
|
|
149
|
+
i += 3;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (c === "<" && next === "<") {
|
|
153
|
+
// A here-document: the lines after this one, up to the delimiter, are the command's input.
|
|
154
|
+
endWord();
|
|
155
|
+
i += 2;
|
|
156
|
+
const stripTabs = command[i] === "-";
|
|
157
|
+
if (stripTabs)
|
|
158
|
+
i++;
|
|
159
|
+
while (command[i] === " " || command[i] === "\t")
|
|
160
|
+
i++;
|
|
161
|
+
const start = i;
|
|
162
|
+
while (i < command.length && !/[\s;&|<>()]/.test(command[i]))
|
|
163
|
+
i++;
|
|
164
|
+
const raw = command.slice(start, i);
|
|
165
|
+
const delimiter = raw.replace(/['"\\]/g, "");
|
|
166
|
+
if (!/^[A-Za-z_][A-Za-z0-9_.-]*$/.test(delimiter)) {
|
|
167
|
+
fail("a here-document");
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
opened.push({ delimiter, stripTabs, literal: raw !== delimiter });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (next === "(") {
|
|
174
|
+
segment.dynamic = true;
|
|
175
|
+
fail("process substitution");
|
|
176
|
+
i += 2;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
// A word made only of digits right before the operator is a file descriptor: 2>, 1>>.
|
|
180
|
+
let op = "";
|
|
181
|
+
if (inWord && /^\d+$/.test(word)) {
|
|
182
|
+
op = word;
|
|
183
|
+
word = "";
|
|
184
|
+
inWord = false;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
endWord();
|
|
188
|
+
}
|
|
189
|
+
op += c;
|
|
190
|
+
i++;
|
|
191
|
+
if (command[i] === c) {
|
|
192
|
+
op += c;
|
|
193
|
+
i++;
|
|
194
|
+
}
|
|
195
|
+
if (command[i] === "&") {
|
|
196
|
+
op += "&";
|
|
197
|
+
i++;
|
|
198
|
+
}
|
|
199
|
+
pendingRedirect = op;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (c === "&" && next === ">") {
|
|
203
|
+
endWord();
|
|
204
|
+
let op = "&>";
|
|
205
|
+
i += 2;
|
|
206
|
+
if (command[i] === ">") {
|
|
207
|
+
op += ">";
|
|
208
|
+
i++;
|
|
209
|
+
}
|
|
210
|
+
pendingRedirect = op;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (c === "\n" && opened.length > 0) {
|
|
214
|
+
// The text of each here-document is input, not commands: step over it.
|
|
215
|
+
let at = i + 1;
|
|
216
|
+
for (const doc of opened) {
|
|
217
|
+
let closed = false;
|
|
218
|
+
let text = "";
|
|
219
|
+
while (at <= command.length) {
|
|
220
|
+
const end = command.indexOf("\n", at);
|
|
221
|
+
const line = command.slice(at, end < 0 ? command.length : end);
|
|
222
|
+
at = end < 0 ? command.length + 1 : end + 1;
|
|
223
|
+
if ((doc.stripTabs ? line.replace(/^\t+/, "") : line) === doc.delimiter) {
|
|
224
|
+
closed = true;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
text += `${line}\n`;
|
|
228
|
+
}
|
|
229
|
+
if (!closed)
|
|
230
|
+
fail("an unclosed here-document");
|
|
231
|
+
else if (!doc.literal && /[$`]/.test(text))
|
|
232
|
+
fail("a here-document that runs commands");
|
|
233
|
+
}
|
|
234
|
+
opened.length = 0;
|
|
235
|
+
endSegment();
|
|
236
|
+
i = Math.min(at, command.length);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (OPERATOR_CHARS.has(c)) {
|
|
240
|
+
endSegment();
|
|
241
|
+
if ((c === "&" || c === "|") && next === c)
|
|
242
|
+
i += 2;
|
|
243
|
+
else if (c === "|" && next === "&")
|
|
244
|
+
i += 2;
|
|
245
|
+
else
|
|
246
|
+
i++;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
word += c;
|
|
250
|
+
inWord = true;
|
|
251
|
+
i++;
|
|
252
|
+
}
|
|
253
|
+
endSegment();
|
|
254
|
+
if (opened.length > 0)
|
|
255
|
+
fail("an unclosed here-document");
|
|
256
|
+
return reason ? { segments, understood: false, reason } : { segments, understood: true };
|
|
257
|
+
}
|
|
258
|
+
const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
259
|
+
const SHELLS = new Set(["bash", "sh", "zsh", "dash", "ksh", "fish"]);
|
|
260
|
+
/**
|
|
261
|
+
* Peels wrappers off a command so the real program is checked: `sudo`, `env VAR=x`, `exec`,
|
|
262
|
+
* `nohup`, `time`, `nice`, `timeout`, `xargs`, and `bash -c '...'` or `eval '...'`, whose string is
|
|
263
|
+
* parsed again. Stops after four levels (prior-art 4.3, after Kimi Code's dangerous-command check).
|
|
264
|
+
*/
|
|
265
|
+
export function unwrap(argv, depth = 0) {
|
|
266
|
+
if (depth > 4)
|
|
267
|
+
return { commands: [argv], privileged: false, understood: false };
|
|
268
|
+
let args = [...argv];
|
|
269
|
+
while (args.length > 0 && ASSIGNMENT.test(args[0]))
|
|
270
|
+
args = args.slice(1);
|
|
271
|
+
const name = basename(args[0] ?? "");
|
|
272
|
+
const rest = args.slice(1);
|
|
273
|
+
const skipOptions = (list, withValue = new Set()) => {
|
|
274
|
+
let k = 0;
|
|
275
|
+
while (k < list.length && list[k].startsWith("-")) {
|
|
276
|
+
if (withValue.has(list[k]))
|
|
277
|
+
k++;
|
|
278
|
+
k++;
|
|
279
|
+
}
|
|
280
|
+
return list.slice(k);
|
|
281
|
+
};
|
|
282
|
+
if (name === "sudo" || name === "doas") {
|
|
283
|
+
const inner = unwrap(skipOptions(rest, new Set(["-u", "-g", "-C", "-D", "-h", "-p", "-r", "-t"])), depth + 1);
|
|
284
|
+
return { ...inner, privileged: true };
|
|
285
|
+
}
|
|
286
|
+
if (name === "env") {
|
|
287
|
+
let inner = skipOptions(rest, new Set(["-u", "-C", "-S"]));
|
|
288
|
+
while (inner.length > 0 && ASSIGNMENT.test(inner[0]))
|
|
289
|
+
inner = inner.slice(1);
|
|
290
|
+
return inner.length
|
|
291
|
+
? unwrap(inner, depth + 1)
|
|
292
|
+
: { commands: [args], privileged: false, understood: true };
|
|
293
|
+
}
|
|
294
|
+
if (name === "exec" ||
|
|
295
|
+
name === "command" ||
|
|
296
|
+
name === "nohup" ||
|
|
297
|
+
name === "time" ||
|
|
298
|
+
name === "builtin") {
|
|
299
|
+
return rest.length
|
|
300
|
+
? unwrap(skipOptions(rest), depth + 1)
|
|
301
|
+
: { commands: [args], privileged: false, understood: true };
|
|
302
|
+
}
|
|
303
|
+
if (name === "nice")
|
|
304
|
+
return unwrap(skipOptions(rest, new Set(["-n"])), depth + 1);
|
|
305
|
+
if (name === "timeout") {
|
|
306
|
+
const inner = skipOptions(rest, new Set(["-s", "-k", "--signal", "--kill-after"]));
|
|
307
|
+
return unwrap(inner.slice(1), depth + 1);
|
|
308
|
+
}
|
|
309
|
+
if (name === "xargs") {
|
|
310
|
+
const inner = skipOptions(rest, new Set(["-I", "-n", "-P", "-L", "-s", "-d", "-E"]));
|
|
311
|
+
return inner.length
|
|
312
|
+
? unwrap(inner, depth + 1)
|
|
313
|
+
: { commands: [["echo"]], privileged: false, understood: true };
|
|
314
|
+
}
|
|
315
|
+
if (SHELLS.has(name) && !rest.includes("-c") && rest.every((arg) => arg.startsWith("-"))) {
|
|
316
|
+
// A shell with no script named reads its commands from its input (a pipe, a here-document):
|
|
317
|
+
// commands Yew has not read.
|
|
318
|
+
return { commands: [args], privileged: false, understood: false };
|
|
319
|
+
}
|
|
320
|
+
if ((SHELLS.has(name) && rest.includes("-c")) || name === "eval") {
|
|
321
|
+
const script = name === "eval" ? rest.join(" ") : rest[rest.indexOf("-c") + 1];
|
|
322
|
+
if (script === undefined)
|
|
323
|
+
return { commands: [args], privileged: false, understood: false };
|
|
324
|
+
const parsed = parseShell(script);
|
|
325
|
+
const commands = [];
|
|
326
|
+
let privileged = false;
|
|
327
|
+
let understood = parsed.understood;
|
|
328
|
+
for (const segment of parsed.segments) {
|
|
329
|
+
if (segment.dynamic)
|
|
330
|
+
understood = false;
|
|
331
|
+
const inner = unwrap(segment.argv, depth + 1);
|
|
332
|
+
commands.push(...inner.commands);
|
|
333
|
+
privileged ||= inner.privileged;
|
|
334
|
+
understood &&= inner.understood;
|
|
335
|
+
}
|
|
336
|
+
return { commands, privileged, understood };
|
|
337
|
+
}
|
|
338
|
+
return { commands: [args], privileged: false, understood: true };
|
|
339
|
+
}
|
|
340
|
+
function gitSubcommand(args) {
|
|
341
|
+
let k = 0;
|
|
342
|
+
while (k < args.length && args[k].startsWith("-")) {
|
|
343
|
+
if (["-C", "-c", "--git-dir", "--work-tree", "--namespace"].includes(args[k]))
|
|
344
|
+
k++;
|
|
345
|
+
k++;
|
|
346
|
+
}
|
|
347
|
+
return { sub: args[k], rest: args.slice(k + 1) };
|
|
348
|
+
}
|
|
349
|
+
/** Why an operation must be confirmed every time (spec 21.2), or null. Checked on unwrapped commands. */
|
|
350
|
+
export function mustConfirmReason(argv) {
|
|
351
|
+
const name = basename(argv[0] ?? "");
|
|
352
|
+
const args = argv.slice(1);
|
|
353
|
+
const has = (...flags) => args.some((arg) => flags.includes(arg));
|
|
354
|
+
const hasPrefix = (...prefixes) => args.some((arg) => prefixes.some((prefix) => arg.startsWith(prefix)));
|
|
355
|
+
if (["rm", "rmdir", "unlink", "shred", "srm", "trash"].includes(name))
|
|
356
|
+
return "delete-files";
|
|
357
|
+
// Yew's own command line sends letters and manages keys and the service; sessions use their tools.
|
|
358
|
+
if (name === "yew" || name === "yew") {
|
|
359
|
+
const informational = has("--help", "-h") || args.every((arg) => ["--version", "-v", "--json"].includes(arg));
|
|
360
|
+
if (!informational)
|
|
361
|
+
return "yew-cli";
|
|
362
|
+
}
|
|
363
|
+
if (name === "find" && has("-delete"))
|
|
364
|
+
return "delete-files";
|
|
365
|
+
if (name === "rsync" && hasPrefix("--delete", "--remove-source-files"))
|
|
366
|
+
return "delete-files";
|
|
367
|
+
if (name === "git") {
|
|
368
|
+
const { sub, rest } = gitSubcommand(args);
|
|
369
|
+
const restHas = (...flags) => rest.some((arg) => flags.includes(arg));
|
|
370
|
+
if (sub === "push")
|
|
371
|
+
return "push";
|
|
372
|
+
if (sub === "reset" && restHas("--hard", "--merge", "--keep"))
|
|
373
|
+
return "discard-changes";
|
|
374
|
+
if (sub === "clean")
|
|
375
|
+
return "delete-untracked";
|
|
376
|
+
if (sub === "branch" && restHas("-d", "-D", "--delete"))
|
|
377
|
+
return "delete-branch";
|
|
378
|
+
if (sub === "tag" && restHas("-d", "--delete"))
|
|
379
|
+
return "delete-tag";
|
|
380
|
+
if (sub === "stash" && (rest[0] === "drop" || rest[0] === "clear"))
|
|
381
|
+
return "delete-stash";
|
|
382
|
+
if (sub === "checkout" && (restHas("--", ".") || restHas("-f", "--force")))
|
|
383
|
+
return "discard-changes";
|
|
384
|
+
if (sub === "restore" && !restHas("--staged", "-S"))
|
|
385
|
+
return "discard-changes";
|
|
386
|
+
if (sub === "filter-branch" || sub === "filter-repo")
|
|
387
|
+
return "rewrite-history";
|
|
388
|
+
if (sub === "update-ref" && restHas("-d"))
|
|
389
|
+
return "delete-ref";
|
|
390
|
+
if (sub === "worktree" && rest[0] === "remove")
|
|
391
|
+
return "delete-worktree";
|
|
392
|
+
}
|
|
393
|
+
if (["npm", "pnpm", "yarn", "bun"].includes(name) && args[0] === "publish")
|
|
394
|
+
return "publish-package";
|
|
395
|
+
if (name === "npm" && (args[0] === "unpublish" || args[0] === "deprecate"))
|
|
396
|
+
return "change-package";
|
|
397
|
+
if (["cargo", "poetry", "flit", "hatch"].includes(name) && args[0] === "publish")
|
|
398
|
+
return "publish-package";
|
|
399
|
+
if (name === "gem" && args[0] === "push")
|
|
400
|
+
return "publish-package";
|
|
401
|
+
if (name === "twine" && args[0] === "upload")
|
|
402
|
+
return "publish-package";
|
|
403
|
+
if (name === "docker" && (args[0] === "push" || (args[0] === "image" && args[1] === "push"))) {
|
|
404
|
+
return "publish-image";
|
|
405
|
+
}
|
|
406
|
+
if (name === "gh") {
|
|
407
|
+
const action = `${args[0] ?? ""} ${args[1] ?? ""}`;
|
|
408
|
+
if (/^(release (create|delete|upload|edit)|repo (delete|create|edit|rename|archive)|pr (merge|close|create)|issue (create|close|delete|comment)|secret (set|delete)|workflow run|api) ?/.test(`${action} `)) {
|
|
409
|
+
return "github";
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (["ssh", "scp", "sftp", "mosh", "telnet", "ftp", "sshpass"].includes(name)) {
|
|
413
|
+
return "remote-machine";
|
|
414
|
+
}
|
|
415
|
+
if (name === "rsync" && args.some((arg) => /^[^/]*:/.test(arg) && !arg.startsWith("-"))) {
|
|
416
|
+
return "remote-copy";
|
|
417
|
+
}
|
|
418
|
+
if (name === "curl") {
|
|
419
|
+
const sends = args.some((arg, index) => ["-d", "-F", "-T", "--form", "--upload-file", "--json"].includes(arg) ||
|
|
420
|
+
arg.startsWith("--data") ||
|
|
421
|
+
arg.startsWith("--form") ||
|
|
422
|
+
(/^-[a-zA-Z]*[dFT]/.test(arg) && !arg.startsWith("--")) ||
|
|
423
|
+
((arg === "-X" || arg === "--request") &&
|
|
424
|
+
/^(POST|PUT|PATCH|DELETE)$/i.test(args[index + 1] ?? "")));
|
|
425
|
+
if (sends)
|
|
426
|
+
return "send-data";
|
|
427
|
+
}
|
|
428
|
+
if (name === "wget" &&
|
|
429
|
+
hasPrefix("--post-data", "--post-file", "--method", "--body-data", "--body-file")) {
|
|
430
|
+
return "send-data";
|
|
431
|
+
}
|
|
432
|
+
if (["dd", "mkfs", "fdisk", "parted", "diskutil"].includes(name) || name.startsWith("mkfs.")) {
|
|
433
|
+
return "write-disk";
|
|
434
|
+
}
|
|
435
|
+
if (name === "kubectl" &&
|
|
436
|
+
["apply", "delete", "create", "replace", "patch", "scale", "drain", "rollout"].includes(args[0] ?? "")) {
|
|
437
|
+
return "cluster";
|
|
438
|
+
}
|
|
439
|
+
if (name === "terraform" && ["apply", "destroy", "import"].includes(args[0] ?? "")) {
|
|
440
|
+
return "infrastructure";
|
|
441
|
+
}
|
|
442
|
+
if (["launchctl", "systemctl", "crontab", "shutdown", "reboot", "halt"].includes(name)) {
|
|
443
|
+
return "system-services";
|
|
444
|
+
}
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
const READ_ONLY = new Set([
|
|
448
|
+
"ls",
|
|
449
|
+
"pwd",
|
|
450
|
+
"cat",
|
|
451
|
+
"head",
|
|
452
|
+
"tail",
|
|
453
|
+
"wc",
|
|
454
|
+
"file",
|
|
455
|
+
"stat",
|
|
456
|
+
"du",
|
|
457
|
+
"which",
|
|
458
|
+
"whoami",
|
|
459
|
+
"uname",
|
|
460
|
+
"date",
|
|
461
|
+
"echo",
|
|
462
|
+
"printf",
|
|
463
|
+
"realpath",
|
|
464
|
+
"dirname",
|
|
465
|
+
"basename",
|
|
466
|
+
"diff",
|
|
467
|
+
"cmp",
|
|
468
|
+
"jq",
|
|
469
|
+
"rg",
|
|
470
|
+
"grep",
|
|
471
|
+
"egrep",
|
|
472
|
+
"fgrep",
|
|
473
|
+
"cut",
|
|
474
|
+
"tr",
|
|
475
|
+
"sort",
|
|
476
|
+
"tree",
|
|
477
|
+
"find",
|
|
478
|
+
"true",
|
|
479
|
+
"false",
|
|
480
|
+
]);
|
|
481
|
+
const GIT_READ_ONLY = new Set([
|
|
482
|
+
"status",
|
|
483
|
+
"diff",
|
|
484
|
+
"log",
|
|
485
|
+
"show",
|
|
486
|
+
"rev-parse",
|
|
487
|
+
"ls-files",
|
|
488
|
+
"blame",
|
|
489
|
+
"describe",
|
|
490
|
+
"shortlog",
|
|
491
|
+
"grep",
|
|
492
|
+
"branch",
|
|
493
|
+
"tag",
|
|
494
|
+
"remote",
|
|
495
|
+
]);
|
|
496
|
+
/**
|
|
497
|
+
* True when a command only reads. The lists follow Gemini CLI's read-only check (prior-art 4.3) and
|
|
498
|
+
* exclude the options that execute other programs or write files. Paths are checked by the caller.
|
|
499
|
+
*/
|
|
500
|
+
export function isReadOnly(argv) {
|
|
501
|
+
const name = argv[0] ?? "";
|
|
502
|
+
const args = argv.slice(1);
|
|
503
|
+
if (name.includes("/"))
|
|
504
|
+
return false;
|
|
505
|
+
if (args.length === 1 && args[0] === "--version")
|
|
506
|
+
return true;
|
|
507
|
+
if (!READ_ONLY.has(name) && name !== "git")
|
|
508
|
+
return false;
|
|
509
|
+
if (name === "find") {
|
|
510
|
+
const writes = [
|
|
511
|
+
"-exec",
|
|
512
|
+
"-execdir",
|
|
513
|
+
"-ok",
|
|
514
|
+
"-okdir",
|
|
515
|
+
"-delete",
|
|
516
|
+
"-fprint",
|
|
517
|
+
"-fprint0",
|
|
518
|
+
"-fprintf",
|
|
519
|
+
"-fls",
|
|
520
|
+
];
|
|
521
|
+
return !args.some((arg) => writes.includes(arg));
|
|
522
|
+
}
|
|
523
|
+
if (name === "rg")
|
|
524
|
+
return !args.some((arg) => arg.startsWith("--pre"));
|
|
525
|
+
if (name === "sort")
|
|
526
|
+
return !args.some((arg) => arg === "-o" || arg.startsWith("--output"));
|
|
527
|
+
if (name === "tree")
|
|
528
|
+
return !args.some((arg) => arg === "-o");
|
|
529
|
+
if (name === "git") {
|
|
530
|
+
if (args.some((arg) => arg === "-c" ||
|
|
531
|
+
arg.startsWith("--config") ||
|
|
532
|
+
arg === "-C" ||
|
|
533
|
+
arg.startsWith("--exec-path") ||
|
|
534
|
+
arg.startsWith("--output") ||
|
|
535
|
+
arg.startsWith("--ext-diff"))) {
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
const { sub, rest } = gitSubcommand(args);
|
|
539
|
+
if (!sub || !GIT_READ_ONLY.has(sub))
|
|
540
|
+
return false;
|
|
541
|
+
if (sub === "branch")
|
|
542
|
+
return rest.every((arg) => ["-a", "-r", "-v", "-vv", "--list", "--show-current", "--all", "--remotes"].includes(arg));
|
|
543
|
+
if (sub === "tag")
|
|
544
|
+
return rest.every((arg) => ["-l", "--list"].includes(arg));
|
|
545
|
+
if (sub === "remote")
|
|
546
|
+
return rest.every((arg) => arg === "-v");
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
return true;
|
|
550
|
+
}
|
|
551
|
+
/** Arguments that look like paths, which the caller keeps inside the working folder. */
|
|
552
|
+
export function pathArguments(argv) {
|
|
553
|
+
return argv
|
|
554
|
+
.slice(1)
|
|
555
|
+
.filter((arg) => !arg.startsWith("-") &&
|
|
556
|
+
(arg.includes("/") || arg.startsWith("~") || arg === ".." || arg.startsWith(".")));
|
|
557
|
+
}
|
|
558
|
+
const MULTI_WORD = new Set([
|
|
559
|
+
"git",
|
|
560
|
+
"npm",
|
|
561
|
+
"pnpm",
|
|
562
|
+
"yarn",
|
|
563
|
+
"bun",
|
|
564
|
+
"cargo",
|
|
565
|
+
"go",
|
|
566
|
+
"docker",
|
|
567
|
+
"kubectl",
|
|
568
|
+
"gh",
|
|
569
|
+
"brew",
|
|
570
|
+
"pip",
|
|
571
|
+
"pip3",
|
|
572
|
+
"uv",
|
|
573
|
+
"poetry",
|
|
574
|
+
"make",
|
|
575
|
+
"just",
|
|
576
|
+
"dotnet",
|
|
577
|
+
"mvn",
|
|
578
|
+
"gradle",
|
|
579
|
+
"swift",
|
|
580
|
+
"deno",
|
|
581
|
+
]);
|
|
582
|
+
const THIRD_WORD = {
|
|
583
|
+
npm: ["run", "exec", "x"],
|
|
584
|
+
pnpm: ["run", "exec", "dlx"],
|
|
585
|
+
yarn: ["run", "dlx"],
|
|
586
|
+
bun: ["run", "x"],
|
|
587
|
+
uv: ["run", "pip"],
|
|
588
|
+
poetry: ["run"],
|
|
589
|
+
go: ["run", "test"],
|
|
590
|
+
deno: ["run", "task"],
|
|
591
|
+
};
|
|
592
|
+
const INTERPRETERS = new Set([
|
|
593
|
+
"node",
|
|
594
|
+
"python",
|
|
595
|
+
"python3",
|
|
596
|
+
"ruby",
|
|
597
|
+
"perl",
|
|
598
|
+
"php",
|
|
599
|
+
"bash",
|
|
600
|
+
"sh",
|
|
601
|
+
"zsh",
|
|
602
|
+
"osascript",
|
|
603
|
+
"tsx",
|
|
604
|
+
"ts-node",
|
|
605
|
+
]);
|
|
606
|
+
/**
|
|
607
|
+
* The prefix "allow and remember" stores, after OpenCode's arity table (prior-art 4.3): `git status`,
|
|
608
|
+
* `npm run build`. Interpreters store the whole command, since their first word allows anything.
|
|
609
|
+
*/
|
|
610
|
+
export function rememberPrefix(argv) {
|
|
611
|
+
const name = argv[0] ?? "";
|
|
612
|
+
if (INTERPRETERS.has(name))
|
|
613
|
+
return argv.join(" ");
|
|
614
|
+
if (!MULTI_WORD.has(name))
|
|
615
|
+
return name;
|
|
616
|
+
const second = argv.slice(1).find((arg) => !arg.startsWith("-"));
|
|
617
|
+
if (!second)
|
|
618
|
+
return name;
|
|
619
|
+
const index = argv.indexOf(second);
|
|
620
|
+
const third = argv.slice(index + 1).find((arg) => !arg.startsWith("-"));
|
|
621
|
+
if (third && THIRD_WORD[name]?.includes(second))
|
|
622
|
+
return `${name} ${second} ${third}`;
|
|
623
|
+
return `${name} ${second}`;
|
|
624
|
+
}
|
|
625
|
+
/** Word-wise prefix match: the rule `npm run build` matches `npm run build --watch`, not `npm run builder`. */
|
|
626
|
+
export function matchesPrefix(argv, pattern) {
|
|
627
|
+
const words = pattern
|
|
628
|
+
.trim()
|
|
629
|
+
.replace(/\s+\*$/, "")
|
|
630
|
+
.split(/\s+/)
|
|
631
|
+
.filter(Boolean);
|
|
632
|
+
if (words.length === 0)
|
|
633
|
+
return true;
|
|
634
|
+
return words.every((word, index) => argv[index] === word);
|
|
635
|
+
}
|