pi-keeper 0.0.0-stage → 0.1.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 +63 -0
- package/LICENSE +201 -0
- package/NOTICE +36 -0
- package/README.md +71 -2
- package/extensions/ask-heartbeat.ts +55 -0
- package/extensions/ask-mailbox.ts +497 -0
- package/extensions/ask-server.ts +452 -0
- package/extensions/attention.ts +24 -0
- package/extensions/cc-extensions-compat.ts +103 -0
- package/extensions/child-auto.ts +32 -0
- package/extensions/child-cwd.ts +76 -0
- package/extensions/child-default.ts +32 -0
- package/extensions/child-lane.ts +134 -0
- package/extensions/child-plan.ts +12 -0
- package/extensions/config.ts +347 -0
- package/extensions/dialog-lock.ts +70 -0
- package/extensions/dialog.ts +420 -0
- package/extensions/index.ts +3453 -0
- package/extensions/modes.ts +1330 -0
- package/extensions/options.ts +109 -0
- package/extensions/perf.ts +54 -0
- package/extensions/plan-approval.ts +345 -0
- package/extensions/preview.ts +232 -0
- package/extensions/project-rules.ts +565 -0
- package/extensions/prompt-blocks.ts +163 -0
- package/extensions/remote-relay.ts +163 -0
- package/extensions/rtk-rewrite.ts +60 -0
- package/extensions/rulebook.ts +302 -0
- package/extensions/rules-line.ts +71 -0
- package/extensions/rules-view.ts +168 -0
- package/extensions/rules.ts +412 -0
- package/extensions/secure-file.ts +157 -0
- package/extensions/session-grant-store.ts +139 -0
- package/extensions/session-grants.ts +154 -0
- package/extensions/settings-panel.ts +194 -0
- package/extensions/shell-highlight-bridge.ts +50 -0
- package/extensions/subagents-bridge.ts +129 -0
- package/extensions/telemetry.ts +213 -0
- package/extensions/thinking.ts +62 -0
- package/extensions/title.ts +80 -0
- package/extensions/ui-relay.ts +149 -0
- package/extensions/utils.ts +3981 -0
- package/package.json +64 -5
- package/prompts/auto-mode.md +16 -0
- package/prompts/child-auto.md +26 -0
- package/prompts/child-default.md +21 -0
- package/prompts/child-plan.md +16 -0
- package/prompts/contract.md +29 -0
- package/prompts/default-mode.md +8 -0
- package/prompts/plan-mode.md +45 -0
- package/prompts/rule-warning.md +8 -0
|
@@ -0,0 +1,1330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mode definitions and command lists for pi-keeper.
|
|
3
|
+
*
|
|
4
|
+
* Single edit point for modes, permission rules, and every command list
|
|
5
|
+
* (read-only allowlist, dangerous asks, privilege escalation, protected
|
|
6
|
+
* paths). Logic lives in utils.ts (command analysis) and rules.ts
|
|
7
|
+
* (rule evaluation); this file is data.
|
|
8
|
+
*
|
|
9
|
+
* Mode prompts are loaded from prompts/*.md so they can be edited without
|
|
10
|
+
* touching extension code.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { accessSync, constants, readFileSync, statSync } from "node:fs";
|
|
14
|
+
import { delimiter, dirname, join } from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import type { ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
import type { ModeRules } from "./rules.ts";
|
|
18
|
+
|
|
19
|
+
export type ModeName = "default" | "auto" | "plan";
|
|
20
|
+
|
|
21
|
+
export interface ModeDefinition {
|
|
22
|
+
name: ModeName;
|
|
23
|
+
/** Tools made active in this mode. Enforcement is in `rules`, not here. */
|
|
24
|
+
tools: string[];
|
|
25
|
+
/** Footer badge text, icon, and theme color (rendered bold, see formatModeStatus). */
|
|
26
|
+
badge: string;
|
|
27
|
+
icon: string;
|
|
28
|
+
color: ThemeColor;
|
|
29
|
+
/** One line per mode for the /mode selector (the README's "Use it for" column). */
|
|
30
|
+
description: string;
|
|
31
|
+
/** The mode's own text, the first part of its mode message (prompts/<file>). */
|
|
32
|
+
prompt: string;
|
|
33
|
+
/** Permission rules (see rules.ts for semantics). */
|
|
34
|
+
permissions: ModeRules;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Extension code lives in extensions/, prompts in ../prompts/
|
|
38
|
+
const PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
39
|
+
|
|
40
|
+
function loadPrompt(name: string): string {
|
|
41
|
+
try {
|
|
42
|
+
return readFileSync(join(PACKAGE_ROOT, "prompts", name), "utf8").trim();
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error(`pi-keeper: failed to load prompts/${name}:`, error);
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Mode a pi-subagents child is forced into (auto only when enabled and its asks can relay). */
|
|
50
|
+
export type ChildMode = "plan" | "default" | "auto";
|
|
51
|
+
|
|
52
|
+
/** System prompt of a subagent child, instead of its mode's prompt (prompts/child-<mode>.md). */
|
|
53
|
+
export const CHILD_PROMPTS: Record<ChildMode, string> = {
|
|
54
|
+
plan: loadPrompt("child-plan.md"),
|
|
55
|
+
default: loadPrompt("child-default.md"),
|
|
56
|
+
auto: loadPrompt("child-auto.md"),
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The mode-independent half of everything the models are told (prompts/contract.md):
|
|
61
|
+
* the permission layer, "the newest message wins", the gates that hold in every
|
|
62
|
+
* mode, rejection and privilege handling, the scratch convention. This is the only
|
|
63
|
+
* part of our text that goes into the system prompt, so it must be identical on
|
|
64
|
+
* every run; prompt-blocks.ts appends the rtk nudge and the todo path to it.
|
|
65
|
+
*/
|
|
66
|
+
export const CONTRACT = loadPrompt("contract.md");
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The text injected when a rulebook rule fires (prompts/rule-warning.md): what
|
|
70
|
+
* happened, what to do next, then the rule's name, description and body
|
|
71
|
+
* (rulebook.ts fills in the `{{name}}`, `{{description}}` and `{{body}}` slots).
|
|
72
|
+
* It is never part of the prompt or of the mode message — only a hidden message,
|
|
73
|
+
* only on a fire — so it costs nothing on a run where no rule matches.
|
|
74
|
+
*/
|
|
75
|
+
export const RULE_WARNING = loadPrompt("rule-warning.md");
|
|
76
|
+
|
|
77
|
+
/** Tool name in a rule that matches every tool (only with a matcher that narrows it). */
|
|
78
|
+
export const ANY_TOOL = "*";
|
|
79
|
+
|
|
80
|
+
/** Tool that ends plan mode (registered in index.ts). */
|
|
81
|
+
export const EXIT_PLAN_TOOL = "exit_plan_mode";
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Tools that never change anything. `questionnaire` is the current pi name;
|
|
85
|
+
* `ask_user_question` is kept for older builds (unknown names are ignored).
|
|
86
|
+
*/
|
|
87
|
+
export const READ_TOOLS = ["read", "grep", "find", "ls", "questionnaire", "ask_user_question"];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* pi-subagents' launcher. Allowed in every mode, but only while its children
|
|
91
|
+
* are guaranteed to load this extension (subagents-bridge.ts); see the
|
|
92
|
+
* bridgeInactive rule in GLOBAL_RULES.
|
|
93
|
+
*/
|
|
94
|
+
export const SUBAGENT_TOOL = "subagent";
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The call pi-subagents makes before each workflow child starts (a `workflow`,
|
|
98
|
+
* `parallel`, `tasks` or `chain` child; pi-subagents 0.85 and newer). It is
|
|
99
|
+
* not declared to the model, and the model only sees the `subagent` call that
|
|
100
|
+
* started the workflow, so the same rules must cover this name too.
|
|
101
|
+
*
|
|
102
|
+
* Its input carries what the parent runs after the child stops: the child's
|
|
103
|
+
* `gate` and `acceptance.verify` shell commands (`commands`) and the file the
|
|
104
|
+
* child's `output` writes (`output`). The gate (index.ts, `decide`) judges
|
|
105
|
+
* each of them with the mode's own bash and write rules.
|
|
106
|
+
*/
|
|
107
|
+
export const SUBAGENT_LAUNCH_TOOL = "subagent_launch";
|
|
108
|
+
|
|
109
|
+
/** Every pi-subagents launch tool: one name for a single child, one per workflow child. */
|
|
110
|
+
export const LAUNCH_TOOLS = [SUBAGENT_TOOL, SUBAGENT_LAUNCH_TOOL];
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Tools pi-subagents gives its children: escalating to the parent
|
|
114
|
+
* (`contact_supervisor`), returning schema-checked output (`structured_output`)
|
|
115
|
+
* and waiting for background runs (`bg_wait`). Allowed only in a child session.
|
|
116
|
+
*/
|
|
117
|
+
export const CHILD_PROTOCOL_TOOLS = ["contact_supervisor", "structured_output", "bg_wait"];
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Built-in search tools. Off by default: search goes through `rtk grep` /
|
|
121
|
+
* `rtk find` / `rtk ls` in bash (compact output, one less tool schema per
|
|
122
|
+
* request). `pi --enable-builtin-tools` activates them in every mode; they
|
|
123
|
+
* are always allowed by the rules, so enabling them via pi's `defaultTools`
|
|
124
|
+
* works too.
|
|
125
|
+
*/
|
|
126
|
+
export const BUILTIN_SEARCH_TOOLS = ["grep", "find", "ls"];
|
|
127
|
+
export const BUILTIN_TOOLS_FLAG = "enable-builtin-tools";
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The search nudge of this setup: rtk is the default way to grep/find/ls, and its
|
|
131
|
+
* output is trimmed, so exact edit text comes from `read`. Constant text, never
|
|
132
|
+
* keyed on the active tool set, because it sits in the system prompt; it is keyed
|
|
133
|
+
* on whether rtk is installed, decided once (`RTK_INSTALLED`).
|
|
134
|
+
* `rtk init -g --agent pi` installs ~/.pi/agent/extensions/rtk.ts, but that only
|
|
135
|
+
* rewrites bash commands and tells the model nothing, so this is the only signal.
|
|
136
|
+
*/
|
|
137
|
+
export const RTK_SEARCH_HINT =
|
|
138
|
+
"Search and list with rtk in bash: `rtk grep <pattern> [path]`, `rtk find <path> -name <glob>`, `rtk ls <dir>`. They don't prompt inside the working directory, but a curl GET asks until your user allows its host (printed to stdout, `curl -s https://<host>/...`).\nrtk grep already recurses (leave out -r/-R: those also read hidden files such as .env) and trims indentation, so take the exact text for an edit from the `read` tool.";
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The same nudge where rtk is not installed: the built-in search tools when they
|
|
142
|
+
* are active (`--enable-builtin-tools`), plain `grep`/`find`/`ls` in bash
|
|
143
|
+
* otherwise. Telling a model to run a binary that is not on PATH only costs it a
|
|
144
|
+
* failed call, so the choice is made once, here (see `searchHint`).
|
|
145
|
+
*/
|
|
146
|
+
export const BUILTIN_SEARCH_HINT =
|
|
147
|
+
"Search and list with the built-in `grep`, `find` and `ls` tools when they are active, else with plain `grep`, `find` and `ls` in bash (a bash search may prompt, and a curl GET asks until your user allows its host). Take the exact text for an edit from the `read` tool.";
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Is an `rtk` executable on PATH? `RTK_DISABLED=1` — rtk's own kill switch, the one
|
|
151
|
+
* `rtk-rewrite.ts` honours — counts as absent. A plain PATH scan, so it costs no
|
|
152
|
+
* process and works on the first prompt.
|
|
153
|
+
*/
|
|
154
|
+
export function rtkOnPath(env: NodeJS.ProcessEnv = process.env, platform: string = process.platform): boolean {
|
|
155
|
+
if (env.RTK_DISABLED === "1") return false;
|
|
156
|
+
const dirs = (env.PATH ?? env.Path ?? "").split(delimiter).filter((dir) => dir !== "");
|
|
157
|
+
const names = platform === "win32" ? ["rtk.exe", "rtk.cmd", "rtk.bat"] : ["rtk"];
|
|
158
|
+
return dirs.some((dir) => names.some((name) => isExecutableFile(join(dir, name))));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isExecutableFile(path: string): boolean {
|
|
162
|
+
try {
|
|
163
|
+
accessSync(path, constants.X_OK);
|
|
164
|
+
return statSync(path).isFile();
|
|
165
|
+
} catch {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Whether this installation gets the rtk nudge, decided once at load: the hint sits
|
|
172
|
+
* in the system prompt, which must be byte-identical for the whole session (a
|
|
173
|
+
* changing section flushes the prompt cache).
|
|
174
|
+
*/
|
|
175
|
+
export const RTK_INSTALLED = rtkOnPath();
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The search nudge for this installation: rtk's when it is installed, else the
|
|
179
|
+
* built-in one. `rtk` is the take, not the requirement — the extension works
|
|
180
|
+
* either way.
|
|
181
|
+
*/
|
|
182
|
+
export function searchHint(rtk: boolean = RTK_INSTALLED): string {
|
|
183
|
+
return rtk ? RTK_SEARCH_HINT : BUILTIN_SEARCH_HINT;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Tools whose `path` argument names what they read. */
|
|
187
|
+
const PATH_READ_TOOLS = ["read", ...BUILTIN_SEARCH_TOOLS];
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Asks shared by every mode: secrets (always ask) and the first reads
|
|
191
|
+
* outside the working directory (until "allow reads outside the cwd").
|
|
192
|
+
*/
|
|
193
|
+
const READ_SAFETY_RULES: ModeRules["rules"] = [
|
|
194
|
+
{ tools: PATH_READ_TOOLS, match: "secretFile", action: "ask" },
|
|
195
|
+
{ tools: ["bash"], match: "secretBash", action: "ask" },
|
|
196
|
+
// first, so a search over another tree offers that directory (the gate it would
|
|
197
|
+
// unlock) instead of a secret ask nobody can grant; both are asks, so the order
|
|
198
|
+
// only decides which dialog the user sees
|
|
199
|
+
{ tools: [...PATH_READ_TOOLS, "bash"], match: "outsideCwdRead", action: "ask" },
|
|
200
|
+
// ask beats the read-only and remembered allows: a rule for `grep` never covers `grep -r` over a .env
|
|
201
|
+
{ tools: ["bash"], match: "secretSearch", action: "ask" },
|
|
202
|
+
// uploading a secret file (`curl -d @.env`) asks in auto too
|
|
203
|
+
{ tools: ["bash"], match: "secretUpload", action: "ask" },
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
/** Read tools the modes activate (built-in search tools are added by the flag). */
|
|
207
|
+
const ACTIVE_READ_TOOLS = READ_TOOLS.filter((name) => !BUILTIN_SEARCH_TOOLS.includes(name));
|
|
208
|
+
const WRITE_TOOLS = ["edit", "write"];
|
|
209
|
+
const SHELL_TOOLS = ["bash", "powershell"];
|
|
210
|
+
|
|
211
|
+
export const MODES: Record<ModeName, ModeDefinition> = {
|
|
212
|
+
default: {
|
|
213
|
+
name: "default",
|
|
214
|
+
// Write tools stay declared so the model can attempt them and the user
|
|
215
|
+
// gets a permission dialog. `exit_plan_mode` is declared in every mode so the
|
|
216
|
+
// tool list is identical in all of them (a per-mode list changes the request
|
|
217
|
+
// prefix, which flushes the cache and, behind some gateways, fails signed-thinking
|
|
218
|
+
// sessions); the rule below denies it here.
|
|
219
|
+
tools: [...ACTIVE_READ_TOOLS, ...WRITE_TOOLS, "bash", "mcp", EXIT_PLAN_TOOL],
|
|
220
|
+
badge: "default",
|
|
221
|
+
icon: "●",
|
|
222
|
+
color: "mdLink",
|
|
223
|
+
description: "everyday work: reads run, edits and other commands ask",
|
|
224
|
+
prompt: loadPrompt("default-mode.md"),
|
|
225
|
+
permissions: {
|
|
226
|
+
fallback: "ask",
|
|
227
|
+
rules: [
|
|
228
|
+
{ tools: [...READ_TOOLS, ...LAUNCH_TOOLS], action: "allow" },
|
|
229
|
+
{ tools: CHILD_PROTOCOL_TOOLS, match: "childSession", action: "allow" },
|
|
230
|
+
{ tools: ["bash"], match: "readOnlyBash", action: "allow" },
|
|
231
|
+
{ tools: ["bash"], match: "rememberedBash", action: "allow" },
|
|
232
|
+
// a whole command approved for the session; plan mode ignores it, so planning stays read-only
|
|
233
|
+
{ tools: ["bash"], match: "rememberedVerbatim", action: "allow" },
|
|
234
|
+
// persisted per-project allow rules; plan mode ignores them
|
|
235
|
+
{ tools: ["bash"], match: "projectAllowBash", action: "allow" },
|
|
236
|
+
// tools declaring readOnlyHint (MCP): no dialog
|
|
237
|
+
{ tools: [ANY_TOOL], match: "readOnlyTool", action: "allow" },
|
|
238
|
+
// ask beats allow: a remembered `git` never covers `git push`
|
|
239
|
+
{ tools: SHELL_TOOLS, match: "dangerousBash", action: "ask" },
|
|
240
|
+
// "Yes, allow all edits this session": the asks below still win
|
|
241
|
+
{ tools: WRITE_TOOLS, match: "scratchFile", action: "allow" },
|
|
242
|
+
// the session plan file: the model ticks off steps while executing the plan
|
|
243
|
+
{ tools: WRITE_TOOLS, match: "planFile", action: "allow" },
|
|
244
|
+
// a subagent child's own report (pi-subagents output dir)
|
|
245
|
+
{ tools: WRITE_TOOLS, match: "subagentArtifact", action: "allow" },
|
|
246
|
+
{ tools: WRITE_TOOLS, match: "sessionEdits", action: "allow" },
|
|
247
|
+
{ tools: WRITE_TOOLS, match: "protectedPath", action: "ask" },
|
|
248
|
+
{ tools: WRITE_TOOLS, match: "outsideCwd", action: "ask" },
|
|
249
|
+
{ tools: WRITE_TOOLS, match: "secretFile", action: "ask" },
|
|
250
|
+
...READ_SAFETY_RULES,
|
|
251
|
+
{ tools: [EXIT_PLAN_TOOL], action: "deny" },
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
auto: {
|
|
256
|
+
name: "auto",
|
|
257
|
+
tools: [...ACTIVE_READ_TOOLS, ...WRITE_TOOLS, "bash", "mcp", EXIT_PLAN_TOOL],
|
|
258
|
+
badge: "auto",
|
|
259
|
+
icon: "⏵⏵",
|
|
260
|
+
color: "warning",
|
|
261
|
+
description: "a trusted task: nearly everything runs; dangerous, secret and outside actions ask",
|
|
262
|
+
prompt: loadPrompt("auto-mode.md"),
|
|
263
|
+
permissions: {
|
|
264
|
+
fallback: "allow",
|
|
265
|
+
rules: [
|
|
266
|
+
{ tools: SHELL_TOOLS, match: "dangerousBash", action: "ask" },
|
|
267
|
+
// shell writes to a literal path outside the cwd (unresolvable targets stay allowed)
|
|
268
|
+
{ tools: SHELL_TOOLS, match: "outsideCwdBashWrite", action: "ask" },
|
|
269
|
+
// rm, mv, truncate, find -delete, git checkout/clean/... aimed outside the cwd; unlike
|
|
270
|
+
// the write rule above, a target that can't be resolved (`rm $X`) asks too
|
|
271
|
+
{ tools: SHELL_TOOLS, match: "outsideCwdDestructive", action: "ask" },
|
|
272
|
+
{ tools: WRITE_TOOLS, match: "scratchFile", action: "allow" },
|
|
273
|
+
{ tools: WRITE_TOOLS, match: "planFile", action: "allow" },
|
|
274
|
+
{ tools: WRITE_TOOLS, match: "subagentArtifact", action: "allow" },
|
|
275
|
+
{ tools: WRITE_TOOLS, match: "protectedPath", action: "ask" },
|
|
276
|
+
{ tools: WRITE_TOOLS, match: "outsideCwd", action: "ask" },
|
|
277
|
+
{ tools: WRITE_TOOLS, match: "secretFile", action: "ask" },
|
|
278
|
+
...READ_SAFETY_RULES,
|
|
279
|
+
{ tools: [EXIT_PLAN_TOOL], action: "deny" },
|
|
280
|
+
],
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
plan: {
|
|
284
|
+
name: "plan",
|
|
285
|
+
// edit/write must be active so the plan file can be written; every
|
|
286
|
+
// other target is denied by the rules below.
|
|
287
|
+
tools: [...ACTIVE_READ_TOOLS, ...WRITE_TOOLS, "bash", "mcp", EXIT_PLAN_TOOL],
|
|
288
|
+
badge: "plan",
|
|
289
|
+
icon: "⏸",
|
|
290
|
+
color: "success",
|
|
291
|
+
description: "researching and writing a plan: read-only except the plan file",
|
|
292
|
+
prompt: loadPrompt("plan-mode.md"),
|
|
293
|
+
permissions: {
|
|
294
|
+
// Allowlist: any tool not named here (mcp, powershell, tools from
|
|
295
|
+
// other packages, codemode tools) is denied.
|
|
296
|
+
fallback: "deny",
|
|
297
|
+
rules: [
|
|
298
|
+
{ tools: [...READ_TOOLS, ...LAUNCH_TOOLS, EXIT_PLAN_TOOL], action: "allow" },
|
|
299
|
+
{ tools: CHILD_PROTOCOL_TOOLS, match: "childSession", action: "allow" },
|
|
300
|
+
// read-only MCP tools fit research; every other unknown tool stays denied
|
|
301
|
+
{ tools: [ANY_TOOL], match: "readOnlyTool", action: "allow" },
|
|
302
|
+
{ tools: ["bash"], match: "readOnlyBash", action: "allow" },
|
|
303
|
+
// "Yes, and always allow" prefixes are shared with default mode
|
|
304
|
+
{ tools: ["bash"], match: "rememberedBash", action: "allow" },
|
|
305
|
+
// A dangerous command is never context-gathering: denied, so the
|
|
306
|
+
// model puts the step in the plan. Beats every allow above.
|
|
307
|
+
{ tools: ["bash"], match: "dangerousBash", action: "deny" },
|
|
308
|
+
// e.g. running tests or a build to gather context: the user decides.
|
|
309
|
+
// Bash only: other shells stay denied.
|
|
310
|
+
{ tools: ["bash"], match: "unapprovedBash", action: "ask" },
|
|
311
|
+
{ tools: WRITE_TOOLS, match: "planFile", action: "allow" },
|
|
312
|
+
// a subagent child's own report (pi-subagents output dir)
|
|
313
|
+
{ tools: WRITE_TOOLS, match: "subagentArtifact", action: "allow" },
|
|
314
|
+
// the session scratch dir: notes, repros, cloned references
|
|
315
|
+
{ tools: WRITE_TOOLS, match: "scratchFile", action: "allow" },
|
|
316
|
+
...READ_SAFETY_RULES,
|
|
317
|
+
],
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/** The theme calls the footer chip needs (a subset of pi's Theme). */
|
|
323
|
+
export interface StatusTheme {
|
|
324
|
+
fg(color: ThemeColor, text: string): string;
|
|
325
|
+
bold(text: string): string;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Footer chip, styled here; pi-cc-extensions' footer passes the colors through as sent.
|
|
330
|
+
* Bold `<icon> <badge>` in the mode color, plus a dim ` · ` and yellow `edits` while
|
|
331
|
+
* session edits are allowed.
|
|
332
|
+
*/
|
|
333
|
+
export function formatModeStatus(theme: StatusTheme, mode: ModeName, edits: boolean): string {
|
|
334
|
+
const { color, icon, badge } = MODES[mode];
|
|
335
|
+
const chip = theme.bold(theme.fg(color, `${icon} ${badge}`));
|
|
336
|
+
return edits ? chip + theme.fg("dim", " · ") + theme.bold(theme.fg("warning", "edits")) : chip;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Rules prepended to every mode. Deny wins over everything. */
|
|
340
|
+
export const GLOBAL_RULES: ModeRules["rules"] = [
|
|
341
|
+
{ tools: SHELL_TOOLS, match: "privilegedBash", action: "deny" },
|
|
342
|
+
// no subagent launch unless its children will load this extension (not even in auto);
|
|
343
|
+
// both launch names: the workflow children behind `subagent_launch` are children too
|
|
344
|
+
{ tools: LAUNCH_TOOLS, match: "bridgeInactive", action: "deny" },
|
|
345
|
+
// user rules from .pi/settings.local.json: deny and ask apply in every mode
|
|
346
|
+
{ tools: SHELL_TOOLS, match: "projectDeny", action: "deny" },
|
|
347
|
+
{ tools: SHELL_TOOLS, match: "projectAsk", action: "ask" },
|
|
348
|
+
];
|
|
349
|
+
|
|
350
|
+
/** Every tool any mode activates; tools outside this set are preserved as-is. */
|
|
351
|
+
export const MANAGED_TOOLS = new Set<string>(Object.values(MODES).flatMap((m) => m.tools));
|
|
352
|
+
|
|
353
|
+
/** Ctrl+Shift+M / Alt+M cycle in this order. */
|
|
354
|
+
export const MODE_CYCLE: ModeName[] = ["default", "auto", "plan"];
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* The closing instruction of each mode, the last line of its mode message: what to
|
|
358
|
+
* do now that the mode changed. The mode prompt above it carries the rules; this
|
|
359
|
+
* only names the next step, so it never repeats the mode's own text.
|
|
360
|
+
*/
|
|
361
|
+
export const MODE_ANNOUNCEMENTS: Record<ModeName, string> = {
|
|
362
|
+
default: "Continue the task.",
|
|
363
|
+
auto: "Continue the task.",
|
|
364
|
+
plan: "Call `exit_plan_mode` when the plan file is ready.",
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* The short announcement steered mid-run under `modeInstructions: "prompt"`, where the mode
|
|
369
|
+
* text sits in the system prompt and is only recomposed on the next prompt: it says what
|
|
370
|
+
* changed now and why earlier restrictions are void, without repeating the mode's own text.
|
|
371
|
+
*/
|
|
372
|
+
export const MODE_CHANGE_NOTICES: Record<ModeName, string> = {
|
|
373
|
+
default:
|
|
374
|
+
"default mode: file edits and non-read-only bash now ask the user (a permission dialog is shown). Plan-mode and earlier permission instructions no longer apply. Continue the task.",
|
|
375
|
+
auto: "auto mode: file edits and bash now run without asking; only dangerous commands, protected paths, changes outside the working directory and secrets still ask. Plan-mode and earlier permission restrictions no longer apply. Continue the task.",
|
|
376
|
+
plan: "plan mode: read-only research. Write the plan to the plan file, then call exit_plan_mode. Commands that are not read-only ask the user: use them only to gather context. Dangerous commands are denied: put them in the plan.",
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
/** Deny reason for privilege escalation (all modes). */
|
|
380
|
+
export const PRIVILEGE_REASON =
|
|
381
|
+
"Commands needing elevated privileges (sudo, su, doas, pkexec, run0) are never run by you. Give the user the exact command to run themselves (they can type `! <command>` in pi), explain why it is needed, then stop and wait for the user to confirm it is done before continuing.";
|
|
382
|
+
|
|
383
|
+
// ---------------------------------------------------------------------------
|
|
384
|
+
// Command analysis lists (used by utils.ts)
|
|
385
|
+
// ---------------------------------------------------------------------------
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Bash commands considered read-only in default and plan modes. Roots with
|
|
389
|
+
* subcommand or flag restrictions are validated further in utils.ts.
|
|
390
|
+
*/
|
|
391
|
+
export const READ_ONLY_BASH_COMMANDS: string[] = [
|
|
392
|
+
// File inspection (less/more excluded: interactive `!` shell escapes; they are INTERPRETERS)
|
|
393
|
+
"cat",
|
|
394
|
+
"head",
|
|
395
|
+
"tail",
|
|
396
|
+
"wc",
|
|
397
|
+
"file",
|
|
398
|
+
"stat",
|
|
399
|
+
"diff",
|
|
400
|
+
"cmp",
|
|
401
|
+
"nl",
|
|
402
|
+
// Text processing (stdout only)
|
|
403
|
+
"sort",
|
|
404
|
+
"uniq",
|
|
405
|
+
"cut",
|
|
406
|
+
"tr",
|
|
407
|
+
"column",
|
|
408
|
+
"jq",
|
|
409
|
+
"sed", // only with a script the analyzer in utils.ts (sedBlocker) fully understands
|
|
410
|
+
// Search
|
|
411
|
+
"find",
|
|
412
|
+
"fd",
|
|
413
|
+
"grep",
|
|
414
|
+
"rg",
|
|
415
|
+
// Paths / directories
|
|
416
|
+
"ls",
|
|
417
|
+
"pwd",
|
|
418
|
+
"tree",
|
|
419
|
+
"du",
|
|
420
|
+
"df",
|
|
421
|
+
"basename",
|
|
422
|
+
"dirname",
|
|
423
|
+
"realpath",
|
|
424
|
+
"readlink",
|
|
425
|
+
"cd",
|
|
426
|
+
// Subcommand-checked
|
|
427
|
+
"git",
|
|
428
|
+
"rtk",
|
|
429
|
+
"node",
|
|
430
|
+
"npm",
|
|
431
|
+
"pnpm",
|
|
432
|
+
"yarn",
|
|
433
|
+
// Subcommand-checked service CLIs (query subcommands only; see utils.ts)
|
|
434
|
+
"gh",
|
|
435
|
+
"kubectl",
|
|
436
|
+
"docker",
|
|
437
|
+
"podman",
|
|
438
|
+
// Environment listing (`env` with no command, `printenv [NAME…]`)
|
|
439
|
+
"env",
|
|
440
|
+
"printenv",
|
|
441
|
+
// System info
|
|
442
|
+
"uname",
|
|
443
|
+
"whoami",
|
|
444
|
+
"id",
|
|
445
|
+
"hostname",
|
|
446
|
+
"date",
|
|
447
|
+
"uptime",
|
|
448
|
+
"which",
|
|
449
|
+
"type",
|
|
450
|
+
// Shell builtins with no side effects
|
|
451
|
+
"echo",
|
|
452
|
+
"printf",
|
|
453
|
+
"true",
|
|
454
|
+
"false",
|
|
455
|
+
"test",
|
|
456
|
+
"[",
|
|
457
|
+
];
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Flag matchers. `exact`/`prefixes` match whole args; `shortLetters` match
|
|
461
|
+
* inside single-dash clusters (`-Hx` contains `x`).
|
|
462
|
+
*/
|
|
463
|
+
export interface FlagSpec {
|
|
464
|
+
exact?: string[];
|
|
465
|
+
prefixes?: string[];
|
|
466
|
+
shortLetters?: string;
|
|
467
|
+
/** Long options (`--name`): matched as `--name`, `--name=…` and any getopt abbreviation (`--use-c`). */
|
|
468
|
+
longs?: string[];
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Flags that make an otherwise read-only command run a program, hang, or
|
|
473
|
+
* write somewhere that can't be named (effect "exec": never covered by a
|
|
474
|
+
* rule). The flags that write a nameable file (`sort -o`, `tree -o`) are in
|
|
475
|
+
* OUTPUT_FLAGS instead.
|
|
476
|
+
*/
|
|
477
|
+
export const WRITE_FLAGS: Record<string, FlagSpec> = {
|
|
478
|
+
find: { exact: ["-delete", "-exec", "-execdir", "-ok", "-okdir", "-fls"], prefixes: ["-fprint"] },
|
|
479
|
+
fd: { exact: ["--exec", "--exec-batch"], prefixes: ["--exec=", "--exec-batch="], shortLetters: "xX" },
|
|
480
|
+
// --hostname-bin runs its value to build hyperlinks
|
|
481
|
+
rg: { exact: ["--pre", "--hostname-bin"], prefixes: ["--pre=", "--hostname-bin="] },
|
|
482
|
+
// tree -R writes 00Tree.html files
|
|
483
|
+
tree: { exact: ["--fromfile"], shortLetters: "R" },
|
|
484
|
+
// --compress-program runs its value
|
|
485
|
+
sort: { longs: ["--compress-program"] },
|
|
486
|
+
file: {
|
|
487
|
+
exact: ["--compile", "--magic-file", "--files-from"],
|
|
488
|
+
prefixes: ["--magic-file=", "--files-from="],
|
|
489
|
+
shortLetters: "Cmf",
|
|
490
|
+
},
|
|
491
|
+
tail: { exact: ["--follow"], prefixes: ["--follow="], shortLetters: "fF" },
|
|
492
|
+
date: { exact: ["-s", "--set"], prefixes: ["--set=", "-s"] },
|
|
493
|
+
printf: { exact: ["-v"] },
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Output-file flags of read-only commands: the value is a write target
|
|
498
|
+
* (effect "write", coverable when the target is inside the project).
|
|
499
|
+
* `valueShort` lists the other short options that take a value (their text
|
|
500
|
+
* is not scanned for flags); `attached` allows `-ofile`.
|
|
501
|
+
*/
|
|
502
|
+
export const OUTPUT_FLAGS: Record<string, { short: string; longs: string[]; valueShort: string; attached: boolean }> = {
|
|
503
|
+
sort: { short: "o", longs: ["--output"], valueShort: "kStT", attached: true },
|
|
504
|
+
tree: { short: "o", longs: [], valueShort: "PIHTL", attached: false },
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Roots whose args can be turned into flags or extra operands by an
|
|
509
|
+
* unquoted glob, brace, or `$` expansion (a file named `-delete` makes
|
|
510
|
+
* `find *` destructive; `uniq *` writes its second file). → not read-only,
|
|
511
|
+
* and exec-effect: the targets are unknowable.
|
|
512
|
+
*/
|
|
513
|
+
export const GLOB_SENSITIVE_COMMANDS = new Set(["find", "fd", "rg", "git", "sort", "tree", "file", "uniq", "sed", "gh", "kubectl", "docker", "podman"]);
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Commands that only write files named by their operands. Not read-only;
|
|
517
|
+
* a remembered rule covers one only when every target passes the project
|
|
518
|
+
* check (utils.ts `targetProblem`). Flags are listed so a value
|
|
519
|
+
* (`install -m 644`, `truncate -s 0`) is never mistaken for a target: an
|
|
520
|
+
* unlisted flag makes the targets unknown (not covered). `dir` = `-t` /
|
|
521
|
+
* `--target-directory` names a destination directory. Every operand counts
|
|
522
|
+
* as a target, sources of cp/mv/ln/install included (reading an outside or
|
|
523
|
+
* secret file into the project should ask, and mv removes its sources).
|
|
524
|
+
* Removals (rm, rmdir, unlink, shred) are NEVER_REMEMBER, not here.
|
|
525
|
+
* The extractor is `fileWriterTargets` in utils.ts (it reads this table).
|
|
526
|
+
*/
|
|
527
|
+
export interface WriterSpec {
|
|
528
|
+
/** Short flags without a value. */
|
|
529
|
+
flags: string;
|
|
530
|
+
/** Short flags taking a value (next word, or attached). */
|
|
531
|
+
valueFlags: string;
|
|
532
|
+
/** Long flags without a value. */
|
|
533
|
+
longs: string[];
|
|
534
|
+
/** Long flags with a value (`--x=v` or `--x v`). */
|
|
535
|
+
valueLongs: string[];
|
|
536
|
+
/** Long flags with an optional attached value (`--backup[=CTL]`). */
|
|
537
|
+
optionalLongs: string[];
|
|
538
|
+
/** `-t` / `--target-directory` take a destination directory. */
|
|
539
|
+
dir?: boolean;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export const FILE_WRITERS: Record<string, WriterSpec> = {
|
|
543
|
+
cp: {
|
|
544
|
+
flags: "abcdfHiLlnPpRrsTuUvxZ",
|
|
545
|
+
valueFlags: "S",
|
|
546
|
+
longs: ["archive", "attributes-only", "copy-contents", "debug", "dereference", "force", "interactive", "link", "no-clobber", "no-dereference", "no-target-directory", "one-file-system", "parents", "recursive", "remove-destination", "strip-trailing-slashes", "symbolic-link", "verbose"],
|
|
547
|
+
valueLongs: ["suffix", "no-preserve", "sparse"],
|
|
548
|
+
optionalLongs: ["backup", "preserve", "reflink", "update", "context"],
|
|
549
|
+
dir: true,
|
|
550
|
+
},
|
|
551
|
+
mv: {
|
|
552
|
+
flags: "bfinTuvZ",
|
|
553
|
+
valueFlags: "S",
|
|
554
|
+
longs: ["debug", "force", "interactive", "no-clobber", "no-target-directory", "strip-trailing-slashes", "verbose"],
|
|
555
|
+
valueLongs: ["suffix"],
|
|
556
|
+
optionalLongs: ["backup", "update", "context"],
|
|
557
|
+
dir: true,
|
|
558
|
+
},
|
|
559
|
+
ln: {
|
|
560
|
+
flags: "bdFfiLnPrsTv",
|
|
561
|
+
valueFlags: "S",
|
|
562
|
+
longs: ["directory", "force", "interactive", "logical", "no-dereference", "no-target-directory", "physical", "relative", "symbolic", "verbose"],
|
|
563
|
+
valueLongs: ["suffix"],
|
|
564
|
+
optionalLongs: ["backup"],
|
|
565
|
+
dir: true,
|
|
566
|
+
},
|
|
567
|
+
// -s / --strip-program run a program: left out, so they make the targets unknown
|
|
568
|
+
install: {
|
|
569
|
+
flags: "bCcDdpTv",
|
|
570
|
+
valueFlags: "gmoS",
|
|
571
|
+
longs: ["compare", "debug", "directory", "no-target-directory", "preserve-timestamps", "verbose"],
|
|
572
|
+
valueLongs: ["group", "mode", "owner", "suffix"],
|
|
573
|
+
optionalLongs: ["backup", "context"],
|
|
574
|
+
dir: true,
|
|
575
|
+
},
|
|
576
|
+
mkdir: { flags: "pvZ", valueFlags: "m", longs: ["parents", "verbose"], valueLongs: ["mode"], optionalLongs: ["context"] },
|
|
577
|
+
touch: {
|
|
578
|
+
flags: "acfhm",
|
|
579
|
+
valueFlags: "drtA",
|
|
580
|
+
longs: ["no-create", "no-dereference"],
|
|
581
|
+
valueLongs: ["date", "reference", "time"],
|
|
582
|
+
optionalLongs: [],
|
|
583
|
+
},
|
|
584
|
+
tee: { flags: "aip", valueFlags: "", longs: ["append", "ignore-interrupts"], valueLongs: [], optionalLongs: ["output-error"] },
|
|
585
|
+
truncate: { flags: "co", valueFlags: "rs", longs: ["no-create", "io-blocks"], valueLongs: ["reference", "size"], optionalLongs: [] },
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Commands that write only where specific options say (not "every operand",
|
|
590
|
+
* as FILE_WRITERS). Same coverage rule: a remembered rule covers one only
|
|
591
|
+
* when every target stays in the project, and auto asks for a literal target
|
|
592
|
+
* outside it. A flag that isn't listed (it might take a value, or write
|
|
593
|
+
* somewhere) makes the targets unknown. The extractor is `outputWriterTargets`
|
|
594
|
+
* in utils.ts; the per-command meaning of the flags is documented there.
|
|
595
|
+
* Tables are deliberately small: the common flags of each tool, no more.
|
|
596
|
+
*/
|
|
597
|
+
export interface OptionSpec {
|
|
598
|
+
/** Short flags without a value. */
|
|
599
|
+
flags: string;
|
|
600
|
+
/** Short flags taking a value that is not a write target (next word, or attached). */
|
|
601
|
+
valueFlags: string;
|
|
602
|
+
/** Short flags taking a value that is a path the command writes or extracts into. */
|
|
603
|
+
outFlags: string;
|
|
604
|
+
/** Short flags followed by a list of words up to the next flag (`zip -x a b`). */
|
|
605
|
+
listFlags: string;
|
|
606
|
+
/** Long flags without a value (`--no-<name>` is accepted too). */
|
|
607
|
+
longs: string[];
|
|
608
|
+
/** Long flags with a value that is not a target. */
|
|
609
|
+
valueLongs: string[];
|
|
610
|
+
/** Long flags with a value that is a path written. */
|
|
611
|
+
outLongs: string[];
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
export const OUTPUT_WRITERS: Record<string, OptionSpec> = {
|
|
615
|
+
// -o/--output, -c/--cookie-jar, -D/--dump-header, --trace*, --stderr, --etag-save and --output-dir
|
|
616
|
+
// write; -O/--remote-name writes into the cwd. -K/--config and --next are left out (unknown).
|
|
617
|
+
curl: {
|
|
618
|
+
flags: "0123456#GIJLNORSZafghijklnpqsvV",
|
|
619
|
+
valueFlags: "AbCdeEFHmPQrTuUwxXyYzt",
|
|
620
|
+
outFlags: "ocD",
|
|
621
|
+
listFlags: "",
|
|
622
|
+
longs: ["silent", "show-error", "location", "fail", "fail-with-body", "insecure", "include", "head", "verbose", "compressed", "globoff", "get", "no-progress-meter", "progress-bar", "remote-name", "remote-name-all", "remote-header-name", "create-dirs", "http1.1", "http2", "http3", "ipv4", "ipv6", "no-buffer", "netrc", "raw", "tcp-nodelay", "junk-session-cookies", "location-trusted", "remote-time", "parallel", "retry-connrefused", "retry-all-errors", "negotiate", "anyauth", "basic", "digest", "ntlm", "tlsv1.2", "tlsv1.3", "path-as-is", "disable", "list-only", "fail-early"],
|
|
623
|
+
valueLongs: ["header", "request", "user", "user-agent", "referer", "cookie", "max-time", "connect-timeout", "retry", "retry-delay", "retry-max-time", "proxy", "proxy-user", "cert", "cacert", "capath", "key", "resolve", "write-out", "range", "url", "upload-file", "limit-rate", "proto", "proto-redir", "oauth2-bearer", "aws-sigv4", "time-cond", "continue-at", "request-target", "max-redirs", "expect100-timeout", "interface", "noproxy", "keepalive-time", "data", "data-raw", "data-binary", "data-ascii", "data-urlencode", "json", "form", "form-string", "max-filesize", "speed-limit", "speed-time", "connect-to", "unix-socket", "ciphers", "pinnedpubkey", "quote"],
|
|
624
|
+
outLongs: ["output", "output-dir", "cookie-jar", "dump-header", "trace", "trace-ascii", "stderr", "etag-save"],
|
|
625
|
+
},
|
|
626
|
+
// -O/--output-document and -P/--directory-prefix; with neither it saves into the cwd.
|
|
627
|
+
// -o/-a (log files) and --save-cookies write too. -e/--execute is left out (wgetrc commands).
|
|
628
|
+
wget: {
|
|
629
|
+
flags: "cdhkmnpqrvxEFHKLNSV46",
|
|
630
|
+
valueFlags: "tTwQlUiBARDIX",
|
|
631
|
+
outFlags: "OPoa",
|
|
632
|
+
listFlags: "",
|
|
633
|
+
longs: ["quiet", "verbose", "no-verbose", "continue", "recursive", "no-clobber", "no-parent", "no-directories", "no-host-directories", "timestamping", "mirror", "convert-links", "page-requisites", "no-check-certificate", "content-disposition", "adjust-extension", "server-response", "spider", "show-progress", "inet4-only", "inet6-only", "no-cache", "no-cookies", "force-directories", "span-hosts", "relative", "backup-converted"],
|
|
634
|
+
valueLongs: ["tries", "timeout", "wait", "waitretry", "read-timeout", "dns-timeout", "connect-timeout", "limit-rate", "user-agent", "referer", "header", "user", "password", "http-user", "http-password", "post-data", "post-file", "body-data", "body-file", "level", "accept", "reject", "domains", "include-directories", "exclude-directories", "bind-address", "quota", "input-file", "base", "load-cookies", "method"],
|
|
635
|
+
outLongs: ["output-document", "directory-prefix", "output-file", "append-output", "save-cookies"],
|
|
636
|
+
},
|
|
637
|
+
// creating (-c -r -u -A): -f/--file is written; extracting (-x): -C/--directory, else the cwd;
|
|
638
|
+
// listing/diffing (-t -d): nothing. No mode, or two, makes the targets unknown. Program flags
|
|
639
|
+
// (--to-command, -I, ...) are PROGRAM_FLAGS; --remove-files and --listed-incremental are unlisted.
|
|
640
|
+
tar: {
|
|
641
|
+
flags: "cruxtAdzjJavpPkhmoOSUwWBl",
|
|
642
|
+
valueFlags: "XTbNVH",
|
|
643
|
+
outFlags: "fC",
|
|
644
|
+
listFlags: "",
|
|
645
|
+
longs: ["create", "append", "update", "catenate", "concatenate", "extract", "get", "list", "diff", "compare", "gzip", "gunzip", "ungzip", "bzip2", "xz", "zstd", "lzma", "lzop", "auto-compress", "verbose", "preserve-permissions", "same-permissions", "no-same-permissions", "same-owner", "no-same-owner", "numeric-owner", "keep-old-files", "keep-newer-files", "skip-old-files", "overwrite", "unlink-first", "one-file-system", "dereference", "hard-dereference", "absolute-names", "anchored", "wildcards", "totals", "sparse", "ignore-failed-read", "ignore-zeros", "verify", "recursion", "touch", "exclude-vcs", "exclude-caches", "exclude-backups", "no-overwrite-dir", "preserve", "acls", "xattrs", "selinux", "null"],
|
|
646
|
+
valueLongs: ["exclude", "exclude-from", "files-from", "transform", "xform", "strip-components", "owner", "group", "mode", "mtime", "format", "newer", "newer-mtime", "blocking-factor", "label"],
|
|
647
|
+
outLongs: ["file", "directory"],
|
|
648
|
+
},
|
|
649
|
+
// the archive operand is written (-O/--output-file names it instead; -b/--temp-path is a temp dir).
|
|
650
|
+
// -m (moves: deletes the sources) and -d (deletes entries) are left out. -T/-TT are PROGRAM_FLAGS.
|
|
651
|
+
zip: {
|
|
652
|
+
flags: "rqjDXyufgvV0123456789",
|
|
653
|
+
valueFlags: "tnPs",
|
|
654
|
+
outFlags: "Ob",
|
|
655
|
+
listFlags: "ix",
|
|
656
|
+
longs: ["recurse-paths", "quiet", "junk-paths", "no-dir-entries", "no-extra", "symlinks", "update", "freshen", "filesync", "verbose"],
|
|
657
|
+
valueLongs: ["password", "split-size"],
|
|
658
|
+
outLongs: ["output-file", "temp-path"],
|
|
659
|
+
},
|
|
660
|
+
// extracts into -d DIR, else the cwd; -l -t -p -v only list, test or print.
|
|
661
|
+
unzip: { flags: "oqnjaCLMUXVKfuvltpT", valueFlags: "P", outFlags: "d", listFlags: "x", longs: [], valueLongs: [], outLongs: [] },
|
|
662
|
+
// the last operand (the destination) is written; a remote `host:path` counts as outside.
|
|
663
|
+
// -e/--rsh/--rsync-path are PROGRAM_FLAGS; --remove-source-files is unlisted (deletes the sources).
|
|
664
|
+
rsync: {
|
|
665
|
+
flags: "avzrlptgoDHnPucxhqiSWRbLKkmyAXOJUE8",
|
|
666
|
+
valueFlags: "fB",
|
|
667
|
+
outFlags: "T",
|
|
668
|
+
listFlags: "",
|
|
669
|
+
longs: ["archive", "verbose", "compress", "recursive", "links", "perms", "times", "group", "owner", "devices", "specials", "dry-run", "progress", "update", "checksum", "partial", "inplace", "append", "append-verify", "delete", "delete-before", "delete-during", "delete-after", "delete-excluded", "human-readable", "stats", "itemize-changes", "whole-file", "relative", "backup", "copy-links", "copy-dirlinks", "keep-dirlinks", "hard-links", "xattrs", "acls", "sparse", "one-file-system", "prune-empty-dirs", "quiet", "numeric-ids", "list-only", "existing", "ignore-existing", "size-only", "ignore-times", "omit-dir-times", "mkpath", "protect-args", "cvs-exclude", "delay-updates", "fuzzy"],
|
|
670
|
+
valueLongs: ["exclude", "include", "exclude-from", "include-from", "filter", "files-from", "port", "bwlimit", "timeout", "contimeout", "chmod", "chown", "usermap", "groupmap", "max-size", "min-size", "suffix", "password-file", "link-dest", "copy-dest", "compare-dest", "info", "debug", "modify-window", "block-size", "iconv", "skip-compress", "sockopts", "address", "max-delete", "compress-level", "out-format"],
|
|
671
|
+
outLongs: ["backup-dir", "temp-dir", "partial-dir", "log-file", "write-batch", "only-write-batch"],
|
|
672
|
+
},
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Flags that make a command that is NOT read-only run a program (checked
|
|
677
|
+
* before the read-only list, so a remembered prefix such as `tar` never
|
|
678
|
+
* covers them: they always ask). Small on purpose: single-token commands
|
|
679
|
+
* that "Yes, and always allow" would otherwise remember. Same matching as
|
|
680
|
+
* WRITE_FLAGS.
|
|
681
|
+
*/
|
|
682
|
+
export const PROGRAM_FLAGS: Record<string, FlagSpec> = {
|
|
683
|
+
// --to-command / --checkpoint-action=exec / compress, info and rsh programs; old-style `tar xIf prog`
|
|
684
|
+
tar: {
|
|
685
|
+
longs: ["--to-command", "--checkpoint-action", "--use-compress-program", "--info-script", "--new-volume-script", "--rsh-command"],
|
|
686
|
+
shortLetters: "IF",
|
|
687
|
+
},
|
|
688
|
+
// -e / --rsh and --rsync-path run a program
|
|
689
|
+
rsync: { longs: ["--rsh", "--rsync-path"], shortLetters: "e" },
|
|
690
|
+
// -T tests with unzip, -TT <cmd> runs <cmd>
|
|
691
|
+
zip: { exact: ["-T", "-TT"], prefixes: ["-TT"], longs: ["--unzip-command", "--test"] },
|
|
692
|
+
// -S <program> and -o ProxyCommand=… run programs
|
|
693
|
+
scp: { shortLetters: "So" },
|
|
694
|
+
sftp: { shortLetters: "So" },
|
|
695
|
+
// pager / browser programs
|
|
696
|
+
man: { longs: ["--pager", "--html", "--browser"], shortLetters: "PHB" },
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
/** Git subcommands that only read (no further arg checks). */
|
|
700
|
+
export const READ_ONLY_GIT_SUBCOMMANDS: string[] = [
|
|
701
|
+
"status",
|
|
702
|
+
"log",
|
|
703
|
+
"diff",
|
|
704
|
+
"show",
|
|
705
|
+
"blame",
|
|
706
|
+
"rev-parse",
|
|
707
|
+
"ls-files",
|
|
708
|
+
"ls-tree",
|
|
709
|
+
"cat-file",
|
|
710
|
+
"shortlog",
|
|
711
|
+
"describe",
|
|
712
|
+
"grep",
|
|
713
|
+
"merge-base",
|
|
714
|
+
"rev-list",
|
|
715
|
+
"for-each-ref",
|
|
716
|
+
"name-rev",
|
|
717
|
+
];
|
|
718
|
+
// Also read-only with arg checks in utils.ts: branch (list forms), tag
|
|
719
|
+
// (list forms), remote (-v/show/get-url), stash list/show, reflog [show],
|
|
720
|
+
// worktree list, config --get/--list.
|
|
721
|
+
|
|
722
|
+
/** npm-family subcommands that only read. `run` executes scripts. */
|
|
723
|
+
export const READ_ONLY_PACKAGE_SUBCOMMANDS: Record<string, string[]> = {
|
|
724
|
+
npm: ["list", "ls", "outdated", "info", "view"],
|
|
725
|
+
pnpm: ["list", "ls", "outdated", "info", "why"],
|
|
726
|
+
yarn: ["list", "info", "outdated", "why"],
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
/** gh command groups and the subcommands of them that only read (`gh api` is checked on its own). */
|
|
730
|
+
export const READ_ONLY_GH_GROUPS = ["pr", "issue", "run", "repo", "release"];
|
|
731
|
+
export const READ_ONLY_GH_SUBCOMMANDS = ["view", "list", "diff", "status", "checks"];
|
|
732
|
+
|
|
733
|
+
/** kubectl subcommands that only query the cluster. */
|
|
734
|
+
export const READ_ONLY_KUBECTL_SUBCOMMANDS = ["get", "describe", "logs", "top", "explain", "version", "api-resources"];
|
|
735
|
+
|
|
736
|
+
/** docker / podman subcommands that only query the engine. */
|
|
737
|
+
export const READ_ONLY_CONTAINER_SUBCOMMANDS = ["ps", "images", "inspect", "logs", "version", "info"];
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* rtk subcommands that only read/filter. `rtk env` is excluded (prints
|
|
741
|
+
* environment secrets). `rtk git|find|rg` follow the native checks.
|
|
742
|
+
*/
|
|
743
|
+
export const READ_ONLY_RTK_SUBCOMMANDS: string[] = [
|
|
744
|
+
"ls",
|
|
745
|
+
"tree",
|
|
746
|
+
"read",
|
|
747
|
+
"smart",
|
|
748
|
+
"grep",
|
|
749
|
+
"wc",
|
|
750
|
+
"diff",
|
|
751
|
+
"log",
|
|
752
|
+
"json",
|
|
753
|
+
"deps",
|
|
754
|
+
"help",
|
|
755
|
+
"gain",
|
|
756
|
+
"cc-economics",
|
|
757
|
+
"discover",
|
|
758
|
+
"session",
|
|
759
|
+
];
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* Leading env assignments that keep a command read-only (a short allowlist of
|
|
763
|
+
* display and locale variables). Any other assignment makes it not read-only.
|
|
764
|
+
*/
|
|
765
|
+
export const SAFE_ENV_VARS = new Set(["LANG", "LANGUAGE", "NO_COLOR", "TERM", "FORCE_COLOR", "CI", "NODE_ENV", "RUST_BACKTRACE", "TZ", "COLUMNS", "PAGER", "GIT_PAGER"]);
|
|
766
|
+
export const SAFE_ENV_PREFIXES = ["LC_"];
|
|
767
|
+
|
|
768
|
+
/** Wrappers stripped before matching (they run their argument). */
|
|
769
|
+
export const WRAPPERS = new Set(["timeout", "time", "nice", "nohup", "stdbuf", "command", "builtin", "noglob", "xargs"]);
|
|
770
|
+
|
|
771
|
+
/** Shells whose `-c <script>` argument is analyzed recursively. */
|
|
772
|
+
export const SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh", "fish"]);
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* Commands that run arbitrary code: "Yes, and always allow" is never offered for
|
|
776
|
+
* them (a remembered `python` prefix would allow everything).
|
|
777
|
+
*/
|
|
778
|
+
export const INTERPRETERS = new Set([
|
|
779
|
+
...SHELLS,
|
|
780
|
+
// awk: system(), pipes, `print > file`, getline from a command (busybox runs any applet)
|
|
781
|
+
"awk",
|
|
782
|
+
"gawk",
|
|
783
|
+
"mawk",
|
|
784
|
+
"nawk",
|
|
785
|
+
"busybox",
|
|
786
|
+
// editors and pagers with shell escapes (`:!cmd`, `!cmd`)
|
|
787
|
+
"vim",
|
|
788
|
+
"vi",
|
|
789
|
+
"view",
|
|
790
|
+
"vimdiff",
|
|
791
|
+
"nvim",
|
|
792
|
+
"emacs",
|
|
793
|
+
"nano",
|
|
794
|
+
"pico",
|
|
795
|
+
"ed",
|
|
796
|
+
"ex",
|
|
797
|
+
"less",
|
|
798
|
+
"more",
|
|
799
|
+
// other scripting languages / debuggers
|
|
800
|
+
"lua",
|
|
801
|
+
"luajit",
|
|
802
|
+
"tclsh",
|
|
803
|
+
"expect",
|
|
804
|
+
"Rscript",
|
|
805
|
+
"julia",
|
|
806
|
+
"gdb",
|
|
807
|
+
"python",
|
|
808
|
+
"python3",
|
|
809
|
+
"node",
|
|
810
|
+
"deno",
|
|
811
|
+
"bun",
|
|
812
|
+
"perl",
|
|
813
|
+
"ruby",
|
|
814
|
+
"php",
|
|
815
|
+
"eval",
|
|
816
|
+
"exec",
|
|
817
|
+
"source",
|
|
818
|
+
".",
|
|
819
|
+
"env",
|
|
820
|
+
"npx",
|
|
821
|
+
"bunx",
|
|
822
|
+
"pnpx",
|
|
823
|
+
"ssh",
|
|
824
|
+
"watch",
|
|
825
|
+
"parallel",
|
|
826
|
+
]);
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Roots whose subcommands name what they do, so removal subcommands are
|
|
830
|
+
* looked for after them (`git rm`, `docker rmi`). Roots listed in
|
|
831
|
+
* COMMAND_ARITY count too. The remembered prefix depth comes from
|
|
832
|
+
* COMMAND_ARITY.
|
|
833
|
+
*/
|
|
834
|
+
export const MULTI_COMMAND_TOOLS = new Set([
|
|
835
|
+
"git",
|
|
836
|
+
"npm",
|
|
837
|
+
"pnpm",
|
|
838
|
+
"yarn",
|
|
839
|
+
"cargo",
|
|
840
|
+
"go",
|
|
841
|
+
"uv",
|
|
842
|
+
"pip",
|
|
843
|
+
"pip3",
|
|
844
|
+
"poetry",
|
|
845
|
+
"make",
|
|
846
|
+
"just",
|
|
847
|
+
"docker",
|
|
848
|
+
"podman",
|
|
849
|
+
"kubectl",
|
|
850
|
+
"oc",
|
|
851
|
+
"helm",
|
|
852
|
+
"gh",
|
|
853
|
+
"terraform",
|
|
854
|
+
"tofu",
|
|
855
|
+
"systemctl",
|
|
856
|
+
"brew",
|
|
857
|
+
"dnf",
|
|
858
|
+
"apt",
|
|
859
|
+
"mvn",
|
|
860
|
+
"gradle",
|
|
861
|
+
"dotnet",
|
|
862
|
+
]);
|
|
863
|
+
|
|
864
|
+
/** Subcommands that run a named script: the script name joins the prefix. */
|
|
865
|
+
export const RUNNER_SUBCOMMANDS = new Set(["run", "exec", "x", "dlx", "run-script"]);
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* Removals always ask: never offered for "Yes, and always allow" and never covered
|
|
869
|
+
* by a remembered prefix, as a root (`rm`) or a subcommand (`git rm`,
|
|
870
|
+
* `docker rm`).
|
|
871
|
+
*/
|
|
872
|
+
export const NEVER_REMEMBER = new Set(["rm", "rmdir", "unlink", "shred"]);
|
|
873
|
+
|
|
874
|
+
/**
|
|
875
|
+
* Subcommands that remove things (`git rm`, `docker container rm`,
|
|
876
|
+
* `docker rmi`, `pip uninstall`). Matched in the first subcommand
|
|
877
|
+
* positions, after global flags, so `git -C . rm` is still a removal.
|
|
878
|
+
*/
|
|
879
|
+
export const REMOVAL_SUBCOMMANDS = new Set(["rm", "rmi", "rmdir", "remove", "uninstall", "prune", "purge"]);
|
|
880
|
+
|
|
881
|
+
/** rtk subcommands that run the command given in their args (`rtk test cargo test`). */
|
|
882
|
+
export const RTK_RUNNERS = new Set(["proxy", "err", "test", "summary"]);
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* rtk subcommands that are 1:1 proxies of the real command of the same name
|
|
886
|
+
* (`rtk git add` runs `git add`; `rtk rewrite` maps `git status` →
|
|
887
|
+
* `rtk git status`), only filtering the output. They canonicalize to the
|
|
888
|
+
* real command. Left out on purpose: rtk-native commands and name
|
|
889
|
+
* collisions (`read`, `test`, `diff`, `log`, `ls`, `find`, `grep`, `err`,
|
|
890
|
+
* `summary`, `json`, `env`, `run`), renames (`lint` = eslint, `next` =
|
|
891
|
+
* next build, `format`), `pnpm` (has rtk-only subcommands such as
|
|
892
|
+
* `typecheck`) and `wget` (own syntax).
|
|
893
|
+
*/
|
|
894
|
+
export const RTK_PROXY_COMMANDS = new Set([
|
|
895
|
+
"git",
|
|
896
|
+
"gh",
|
|
897
|
+
"glab",
|
|
898
|
+
"gt",
|
|
899
|
+
"aws",
|
|
900
|
+
"psql",
|
|
901
|
+
"npm",
|
|
902
|
+
"npx",
|
|
903
|
+
"docker",
|
|
904
|
+
"kubectl",
|
|
905
|
+
"dotnet",
|
|
906
|
+
"cargo",
|
|
907
|
+
"go",
|
|
908
|
+
"golangci-lint",
|
|
909
|
+
"pip",
|
|
910
|
+
"pytest",
|
|
911
|
+
"ruff",
|
|
912
|
+
"mypy",
|
|
913
|
+
"tsc",
|
|
914
|
+
"rake",
|
|
915
|
+
"rubocop",
|
|
916
|
+
"rspec",
|
|
917
|
+
"jest",
|
|
918
|
+
"vitest",
|
|
919
|
+
"playwright",
|
|
920
|
+
"prisma",
|
|
921
|
+
"curl",
|
|
922
|
+
]);
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* rtk global flags that only change how output is displayed (checked with
|
|
926
|
+
* `rtk --help`): `-v`, `-vv`, `-vvv`, `--verbose`, `--ultra-compact`. Any
|
|
927
|
+
* other flag before the subcommand (`--skip-env` sets a child env var) means
|
|
928
|
+
* no canonicalization.
|
|
929
|
+
*/
|
|
930
|
+
export const RTK_DISPLAY_FLAGS = /^(-v{1,3}|--verbose|--ultra-compact)$/;
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* How many leading tokens identify a command for "Yes, and always allow" (`git`: 2 →
|
|
934
|
+
* `git commit`; `gh`: 3 → `gh pr create`). Longest key wins; a key only
|
|
935
|
+
* appears when its arity differs from its parent's. Commands not listed use
|
|
936
|
+
* their first token only. Adapted from OpenCode's permission/arity.ts (MIT):
|
|
937
|
+
* https://github.com/anomalyco/opencode/blob/219b473e660994ac69a0c5d753ac65da951f3bf8/packages/opencode/src/permission/arity.ts
|
|
938
|
+
* (see NOTICE).
|
|
939
|
+
* Interpreters and removals are refused separately, so they are not listed.
|
|
940
|
+
*/
|
|
941
|
+
export const COMMAND_ARITY: Record<string, number> = {
|
|
942
|
+
aws: 3,
|
|
943
|
+
az: 3,
|
|
944
|
+
bazel: 2,
|
|
945
|
+
brew: 2,
|
|
946
|
+
cargo: 2,
|
|
947
|
+
"cargo add": 3,
|
|
948
|
+
"cargo run": 3,
|
|
949
|
+
cdk: 2,
|
|
950
|
+
cmake: 2,
|
|
951
|
+
composer: 2,
|
|
952
|
+
consul: 2,
|
|
953
|
+
"consul kv": 3,
|
|
954
|
+
crictl: 2,
|
|
955
|
+
dnf: 2,
|
|
956
|
+
doctl: 3,
|
|
957
|
+
docker: 2,
|
|
958
|
+
"docker builder": 3,
|
|
959
|
+
"docker compose": 3,
|
|
960
|
+
"docker container": 3,
|
|
961
|
+
"docker image": 3,
|
|
962
|
+
"docker network": 3,
|
|
963
|
+
"docker volume": 3,
|
|
964
|
+
dotnet: 2,
|
|
965
|
+
eksctl: 2,
|
|
966
|
+
"eksctl create": 3,
|
|
967
|
+
firebase: 2,
|
|
968
|
+
flyctl: 2,
|
|
969
|
+
gcloud: 3,
|
|
970
|
+
gh: 3,
|
|
971
|
+
git: 2,
|
|
972
|
+
"git config": 3,
|
|
973
|
+
"git remote": 3,
|
|
974
|
+
"git stash": 3,
|
|
975
|
+
glab: 3,
|
|
976
|
+
go: 2,
|
|
977
|
+
gradle: 2,
|
|
978
|
+
apt: 2,
|
|
979
|
+
helm: 2,
|
|
980
|
+
heroku: 2,
|
|
981
|
+
hugo: 2,
|
|
982
|
+
ip: 2,
|
|
983
|
+
"ip addr": 3,
|
|
984
|
+
"ip link": 3,
|
|
985
|
+
"ip netns": 3,
|
|
986
|
+
"ip route": 3,
|
|
987
|
+
just: 2,
|
|
988
|
+
kind: 2,
|
|
989
|
+
"kind create": 3,
|
|
990
|
+
kubectl: 2,
|
|
991
|
+
"kubectl kustomize": 3,
|
|
992
|
+
"kubectl rollout": 3,
|
|
993
|
+
kustomize: 2,
|
|
994
|
+
make: 2,
|
|
995
|
+
mc: 2,
|
|
996
|
+
"mc admin": 3,
|
|
997
|
+
minikube: 2,
|
|
998
|
+
mvn: 2,
|
|
999
|
+
ng: 2,
|
|
1000
|
+
npm: 2,
|
|
1001
|
+
"npm exec": 3,
|
|
1002
|
+
"npm init": 3,
|
|
1003
|
+
"npm run": 3,
|
|
1004
|
+
"npm view": 3,
|
|
1005
|
+
nx: 2,
|
|
1006
|
+
oc: 2,
|
|
1007
|
+
openssl: 2,
|
|
1008
|
+
"openssl req": 3,
|
|
1009
|
+
"openssl x509": 3,
|
|
1010
|
+
pip: 2,
|
|
1011
|
+
pip3: 2,
|
|
1012
|
+
pipenv: 2,
|
|
1013
|
+
pnpm: 2,
|
|
1014
|
+
"pnpm dlx": 3,
|
|
1015
|
+
"pnpm exec": 3,
|
|
1016
|
+
"pnpm run": 3,
|
|
1017
|
+
podman: 2,
|
|
1018
|
+
"podman container": 3,
|
|
1019
|
+
"podman image": 3,
|
|
1020
|
+
poetry: 2,
|
|
1021
|
+
"poetry run": 3,
|
|
1022
|
+
pulumi: 2,
|
|
1023
|
+
"pulumi stack": 3,
|
|
1024
|
+
rake: 2,
|
|
1025
|
+
rustup: 2,
|
|
1026
|
+
serverless: 2,
|
|
1027
|
+
skaffold: 2,
|
|
1028
|
+
sls: 2,
|
|
1029
|
+
sst: 2,
|
|
1030
|
+
swift: 2,
|
|
1031
|
+
systemctl: 2,
|
|
1032
|
+
terraform: 2,
|
|
1033
|
+
"terraform workspace": 3,
|
|
1034
|
+
tmux: 2,
|
|
1035
|
+
tofu: 2,
|
|
1036
|
+
turbo: 2,
|
|
1037
|
+
ufw: 2,
|
|
1038
|
+
uv: 2,
|
|
1039
|
+
"uv run": 3,
|
|
1040
|
+
vault: 2,
|
|
1041
|
+
"vault auth": 3,
|
|
1042
|
+
"vault kv": 3,
|
|
1043
|
+
vercel: 2,
|
|
1044
|
+
volta: 2,
|
|
1045
|
+
yarn: 2,
|
|
1046
|
+
"yarn dlx": 3,
|
|
1047
|
+
"yarn run": 3,
|
|
1048
|
+
};
|
|
1049
|
+
|
|
1050
|
+
/** Privilege escalation — denied in every mode. */
|
|
1051
|
+
export const PRIVILEGE_COMMANDS = new Set(["sudo", "sudoedit", "su", "doas", "pkexec", "run0"]);
|
|
1052
|
+
|
|
1053
|
+
/** Commands that execute another command given in their args. */
|
|
1054
|
+
export const COMMAND_RUNNERS = new Set(["xargs", "find", "fd", "parallel", "watch", "setsid", "nohup", "exec", "eval", "script", "flock", "chroot", "unbuffer", "env", "nice", "timeout", "stdbuf", "time", "command"]);
|
|
1055
|
+
|
|
1056
|
+
/** Flags that disarm a safety guard; dangerous on any command. */
|
|
1057
|
+
export const SAFETY_OFF_FLAGS = new Set(["--no-verify", "--insecure", "--dangerously-skip-permissions", "--yolo", "--no-sandbox"]);
|
|
1058
|
+
|
|
1059
|
+
/**
|
|
1060
|
+
* Dangerous commands (ask in default and auto). Matched per command unit
|
|
1061
|
+
* (after stripping wrappers/env) and inside `sh -c` / `eval` scripts.
|
|
1062
|
+
* `args` excludes the root. Accident guard, not a security boundary.
|
|
1063
|
+
*/
|
|
1064
|
+
export const DANGEROUS_COMMANDS: Record<string, (args: string[]) => boolean> = {
|
|
1065
|
+
git: (args) => isDangerousGit(args),
|
|
1066
|
+
npm: (args) => hasAny(args, ["publish", "unpublish", "deprecate"]) || isGlobalInstall(args),
|
|
1067
|
+
pnpm: (args) => hasAny(args, ["publish"]) || isGlobalInstall(args),
|
|
1068
|
+
yarn: (args) => hasAny(args, ["publish"]) || (args[0] === "global" && args[1] === "add"),
|
|
1069
|
+
cargo: (args) => hasAny(fromSubcommand(args, CARGO_GLOBAL_VALUE_FLAGS, true).slice(0, 1), ["publish", "yank"]),
|
|
1070
|
+
gem: (args) => hasAny(fromSubcommand(args, GEM_GLOBAL_VALUE_FLAGS).slice(0, 1), ["push", "yank"]),
|
|
1071
|
+
poetry: (args) => hasAny(fromSubcommand(args, POETRY_GLOBAL_VALUE_FLAGS).slice(0, 1), ["publish"]),
|
|
1072
|
+
twine: (args) => hasAny(fromSubcommand(args, TWINE_GLOBAL_VALUE_FLAGS).slice(0, 1), ["upload"]),
|
|
1073
|
+
docker: (args) => hasAny(fromSubcommand(args, DOCKER_GLOBAL_VALUE_FLAGS).slice(0, 2), ["push"]),
|
|
1074
|
+
podman: (args) => hasAny(fromSubcommand(args, PODMAN_GLOBAL_VALUE_FLAGS).slice(0, 2), ["push"]),
|
|
1075
|
+
gh: (args) => isDangerousGh(args),
|
|
1076
|
+
terraform: (args) => hasAny(args, ["apply", "destroy"]),
|
|
1077
|
+
tofu: (args) => hasAny(args, ["apply", "destroy"]),
|
|
1078
|
+
terragrunt: (args) => hasAny(args, ["apply", "destroy", "run-all"]),
|
|
1079
|
+
pulumi: (args) => hasAny(args, ["up", "destroy"]),
|
|
1080
|
+
cdk: (args) => hasAny(args, ["deploy", "destroy"]),
|
|
1081
|
+
kubectl: (args) => hasAny(args, KUBE_WRITE_VERBS),
|
|
1082
|
+
oc: (args) => hasAny(args, KUBE_WRITE_VERBS),
|
|
1083
|
+
helm: (args) => hasAny(args, ["install", "upgrade", "uninstall", "delete", "rollback"]),
|
|
1084
|
+
aws: (args) => args.some((a) => /^(delete|remove|terminate|destroy)/.test(a) || a === "rm" || a === "rb") || args.includes("--delete"),
|
|
1085
|
+
gcloud: (args) => args.some((a) => /^(delete|remove)/.test(a)),
|
|
1086
|
+
az: (args) => args.some((a) => /^(delete|remove)/.test(a)),
|
|
1087
|
+
ngrok: () => true,
|
|
1088
|
+
cloudflared: (args) => args.includes("tunnel"),
|
|
1089
|
+
ssh: (args) => args.some((a) => /^-[a-zA-Z]*R/.test(a)),
|
|
1090
|
+
nc: (args) => args.some((a) => /^-[a-zA-Z]*[le]/.test(a)),
|
|
1091
|
+
ncat: (args) => args.some((a) => /^-[a-zA-Z]*[le]/.test(a)),
|
|
1092
|
+
netcat: (args) => args.some((a) => /^-[a-zA-Z]*[le]/.test(a)),
|
|
1093
|
+
dd: () => true,
|
|
1094
|
+
shutdown: () => true,
|
|
1095
|
+
reboot: () => true,
|
|
1096
|
+
halt: () => true,
|
|
1097
|
+
poweroff: () => true,
|
|
1098
|
+
systemctl: (args) => hasAny(args, ["start", "stop", "restart", "enable", "disable", "mask", "kill", "isolate"]),
|
|
1099
|
+
chmod: (args) => args.some((a) => a === "--recursive" || /^-[a-zA-Z]*R/.test(a) || /^0?777$/.test(a)),
|
|
1100
|
+
chown: (args) => args.some((a) => a === "--recursive" || /^-[a-zA-Z]*R/.test(a)),
|
|
1101
|
+
crontab: (args) => args.some((a) => /^-[a-zA-Z]*[re]/.test(a)) || args.some((a) => !a.startsWith("-")),
|
|
1102
|
+
};
|
|
1103
|
+
|
|
1104
|
+
/** Roots matched by prefix (mkfs, mkfs.ext4, ...). */
|
|
1105
|
+
export const DANGEROUS_ROOT_PREFIXES = ["mkfs"];
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* Raw-string patterns for forms the unit analysis can't see (download and
|
|
1109
|
+
* run, substitution as an rm target).
|
|
1110
|
+
*/
|
|
1111
|
+
export const DANGEROUS_RAW_PATTERNS: RegExp[] = [
|
|
1112
|
+
// curl/wget piped into a shell or interpreter
|
|
1113
|
+
/\b(curl|wget)\b[^;&\n]*\|\s*(sudo\s+)?(ba|z|da|k)?sh\b/,
|
|
1114
|
+
/\b(curl|wget)\b[^;&\n]*\|\s*(sudo\s+)?(python3?|perl|ruby|node)\b/,
|
|
1115
|
+
// bash <(curl ...) / sh -c "$(curl ...)"
|
|
1116
|
+
/<\(\s*(curl|wget)\b/,
|
|
1117
|
+
/\$\(\s*(curl|wget)\b/,
|
|
1118
|
+
// anything piped into a shell as its script (`… | bash`, `… | sh -s`)
|
|
1119
|
+
/\|&?\s*(\S*\/)?(ba|z|da|k|fi)?sh(\s+-[a-zA-Z]+)*\s*($|[;&|)\n])/,
|
|
1120
|
+
/\|&?\s*(\S*\/)?(ba|z|da|k|fi)?sh\s+(-[a-zA-Z]+\s+)*-[a-zA-Z]*s\b/,
|
|
1121
|
+
// shell or source reading a process substitution (`bash <(…)`, `. <(…)`)
|
|
1122
|
+
/(^|[\s;&|(])(\S*\/)?((ba|z|da|k|fi)?sh|source|\.)\s+(-[a-zA-Z]+\s+)*<\(/,
|
|
1123
|
+
// rm whose target comes from command substitution
|
|
1124
|
+
/\brm\b[^;&|\n]*(\$\(|`)/,
|
|
1125
|
+
];
|
|
1126
|
+
|
|
1127
|
+
/** Global options that take a separate value, per tool, placed before the subcommand. */
|
|
1128
|
+
const DOCKER_GLOBAL_VALUE_FLAGS = new Set(["-H", "--host", "-c", "--context", "--config", "-l", "--log-level", "--tlscacert", "--tlscert", "--tlskey"]);
|
|
1129
|
+
const PODMAN_GLOBAL_VALUE_FLAGS = new Set(["-c", "--connection", "--url", "--identity", "--log-level", "--root", "--runroot", "--runtime", "--storage-driver", "--cgroup-manager", "--conmon", "--tmpdir", "--namespace"]);
|
|
1130
|
+
const CARGO_GLOBAL_VALUE_FLAGS = new Set(["-C", "--config", "-Z", "--color"]);
|
|
1131
|
+
const GEM_GLOBAL_VALUE_FLAGS = new Set(["-C", "--config-file"]);
|
|
1132
|
+
const POETRY_GLOBAL_VALUE_FLAGS = new Set(["--directory"]);
|
|
1133
|
+
const TWINE_GLOBAL_VALUE_FLAGS = new Set(["--config-file"]);
|
|
1134
|
+
/** gh options that take a value; `-R` is accepted before the group and between group and action. */
|
|
1135
|
+
const GH_VALUE_FLAGS = new Set(["-R", "--repo", "-H", "--hostname"]);
|
|
1136
|
+
|
|
1137
|
+
/**
|
|
1138
|
+
* The words from the subcommand on: leading options are skipped, and so is the
|
|
1139
|
+
* separate value of each option in `valueFlags` (`-H tcp://x`). `--flag=value`
|
|
1140
|
+
* is one word. With `skipToolchain`, a leading `+toolchain` word is skipped too.
|
|
1141
|
+
*/
|
|
1142
|
+
function fromSubcommand(args: string[], valueFlags: Set<string>, skipToolchain = false): string[] {
|
|
1143
|
+
let i = 0;
|
|
1144
|
+
while (i < args.length && (args[i].startsWith("-") || (skipToolchain && args[i].startsWith("+")))) {
|
|
1145
|
+
i += valueFlags.has(args[i]) ? 2 : 1;
|
|
1146
|
+
}
|
|
1147
|
+
return args.slice(i);
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/** The non-option words of a gh command, so the group and action can follow options. */
|
|
1151
|
+
function ghWords(args: string[]): string[] {
|
|
1152
|
+
const words: string[] = [];
|
|
1153
|
+
for (let i = 0; i < args.length; i++) {
|
|
1154
|
+
if (!args[i].startsWith("-")) words.push(args[i]);
|
|
1155
|
+
else if (GH_VALUE_FLAGS.has(args[i])) i++;
|
|
1156
|
+
}
|
|
1157
|
+
return words;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
const KUBE_WRITE_VERBS = ["apply", "create", "delete", "replace", "patch", "edit", "scale", "drain", "cordon", "rollout", "taint"];
|
|
1161
|
+
|
|
1162
|
+
function hasAny(args: string[], words: string[]): boolean {
|
|
1163
|
+
return args.some((a) => words.includes(a));
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
function isGlobalInstall(args: string[]): boolean {
|
|
1167
|
+
return hasAny(args, ["i", "install", "add"]) && hasAny(args, ["-g", "--global"]);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
function isDangerousGit(args: string[]): boolean {
|
|
1171
|
+
let i = 0;
|
|
1172
|
+
while (i < args.length && args[i].startsWith("-")) {
|
|
1173
|
+
if (args[i] === "-C" || args[i] === "-c") i++;
|
|
1174
|
+
i++;
|
|
1175
|
+
}
|
|
1176
|
+
const sub = args[i];
|
|
1177
|
+
const rest = args.slice(i + 1);
|
|
1178
|
+
switch (sub) {
|
|
1179
|
+
case "push":
|
|
1180
|
+
case "filter-branch":
|
|
1181
|
+
case "filter-repo":
|
|
1182
|
+
return true;
|
|
1183
|
+
case "reset":
|
|
1184
|
+
return rest.includes("--hard");
|
|
1185
|
+
case "checkout":
|
|
1186
|
+
return rest.includes(".") || rest.includes(":/");
|
|
1187
|
+
case "restore":
|
|
1188
|
+
return rest.includes(".") || rest.includes(":/");
|
|
1189
|
+
case "clean":
|
|
1190
|
+
return rest.some((a) => a === "--force" || /^-[a-zA-Z]*f/.test(a));
|
|
1191
|
+
case "stash":
|
|
1192
|
+
return rest[0] === "drop" || rest[0] === "clear";
|
|
1193
|
+
case "commit":
|
|
1194
|
+
return rest.includes("--amend");
|
|
1195
|
+
case "branch":
|
|
1196
|
+
return rest.some((a) => a === "-D" || (/^-[a-zA-Z]*d/.test(a) && rest.some((b) => b === "-f" || b === "--force")));
|
|
1197
|
+
case "remote":
|
|
1198
|
+
return rest[0] === "add" || rest[0] === "set-url";
|
|
1199
|
+
case "config":
|
|
1200
|
+
return (hasAny(rest, ["--global", "--system"]) && !hasAny(rest, ["--get", "--get-all", "--get-regexp", "--list", "-l"])) || setsCodeRunningConfig(rest);
|
|
1201
|
+
default:
|
|
1202
|
+
return false;
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* Git config keys whose value makes git run a program (hooks, fsmonitor, ssh,
|
|
1208
|
+
* pager, editor, askpass, aliases, textconv/external diff and diff/merge
|
|
1209
|
+
* drivers, filters, credential helpers, includes, upload/receive hooks).
|
|
1210
|
+
* Matched on the lower-cased key; `*` stands for a subsection.
|
|
1211
|
+
*/
|
|
1212
|
+
const CODE_RUNNING_CONFIG_KEYS: RegExp[] = [
|
|
1213
|
+
/^core\.(hookspath|fsmonitor|sshcommand|pager|editor|askpass)$/,
|
|
1214
|
+
/^alias\./,
|
|
1215
|
+
/^diff\.external$/,
|
|
1216
|
+
/^diff\..+\.(textconv|command)$/,
|
|
1217
|
+
/^merge\..+\.driver$/,
|
|
1218
|
+
/^filter\./,
|
|
1219
|
+
/^credential\.(.+\.)?helper$/,
|
|
1220
|
+
/^include\.path$/,
|
|
1221
|
+
/^includeif\./,
|
|
1222
|
+
/^uploadpack\./,
|
|
1223
|
+
/^receive\./,
|
|
1224
|
+
];
|
|
1225
|
+
|
|
1226
|
+
/** Config flags whose next word is a value, not the key. */
|
|
1227
|
+
const CONFIG_VALUE_FLAGS = new Set(["-f", "--file", "--blob", "-t", "--type", "--default", "--comment"]);
|
|
1228
|
+
|
|
1229
|
+
/**
|
|
1230
|
+
* `git config <key> <value>` (also --add, --replace-all, `set`) for a
|
|
1231
|
+
* CODE_RUNNING_CONFIG_KEYS key, at local or worktree scope (--global and
|
|
1232
|
+
* --system are dangerous on their own). A key with a `$` can't be checked and
|
|
1233
|
+
* counts. Reads, --unset and removals don't.
|
|
1234
|
+
*/
|
|
1235
|
+
function setsCodeRunningConfig(rest: string[]): boolean {
|
|
1236
|
+
if (hasAny(rest, ["--get", "--get-all", "--get-regexp", "--list", "-l", "--unset", "--unset-all", "--remove-section", "--rename-section"])) return false;
|
|
1237
|
+
const words: string[] = [];
|
|
1238
|
+
for (let i = 0; i < rest.length; i++) {
|
|
1239
|
+
const a = rest[i];
|
|
1240
|
+
if (a === "--") {
|
|
1241
|
+
words.push(...rest.slice(i + 1));
|
|
1242
|
+
break;
|
|
1243
|
+
}
|
|
1244
|
+
if (a.startsWith("-") && a !== "-") {
|
|
1245
|
+
if (CONFIG_VALUE_FLAGS.has(a)) i++;
|
|
1246
|
+
continue;
|
|
1247
|
+
}
|
|
1248
|
+
words.push(a);
|
|
1249
|
+
}
|
|
1250
|
+
if (words[0] === "set") words.shift();
|
|
1251
|
+
else if (words[0] === "get" || words[0] === "list" || words[0] === "unset") return false;
|
|
1252
|
+
if (words.length < 2) return false;
|
|
1253
|
+
const key = words[0].toLowerCase();
|
|
1254
|
+
return key.includes("$") || CODE_RUNNING_CONFIG_KEYS.some((pattern) => pattern.test(key));
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
function isDangerousGh(args: string[]): boolean {
|
|
1258
|
+
const [group, action] = ghWords(args);
|
|
1259
|
+
if (group === "release") return action === "create" || action === "delete" || action === "upload";
|
|
1260
|
+
if (group === "pr") return action === "merge" || (action === "review" && hasAny(args, ["--approve", "-a"]));
|
|
1261
|
+
if (group === "repo") return hasAny([action ?? ""], ["delete", "edit", "archive", "rename"]);
|
|
1262
|
+
if (group === "secret" || group === "variable") return action === "set" || action === "delete";
|
|
1263
|
+
if (group === "api") {
|
|
1264
|
+
const methodIdx = args.findIndex((a) => a === "-X" || a === "--method");
|
|
1265
|
+
if (methodIdx !== -1) return (args[methodIdx + 1] ?? "").toUpperCase() !== "GET";
|
|
1266
|
+
const inline = args.find((a) => a.startsWith("--method=") || /^-X[A-Za-z]/.test(a));
|
|
1267
|
+
if (inline !== undefined) return !/GET$/i.test(inline);
|
|
1268
|
+
// -f/-F/--input default the method to POST
|
|
1269
|
+
return args.some((a) => /^(-f|-F|--field|--raw-field|--input)(=|$)/.test(a));
|
|
1270
|
+
}
|
|
1271
|
+
return false;
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
/**
|
|
1275
|
+
* Protected paths (ask in auto). Directories match any path segment;
|
|
1276
|
+
* files match the basename. `~/.pi/agent` (pi's own settings, extensions,
|
|
1277
|
+
* sessions) is protected separately in utils.ts, except the plan file.
|
|
1278
|
+
*/
|
|
1279
|
+
export const PROTECTED_DIRS = new Set([".git", ".husky", ".vscode", ".idea", ".devcontainer", ".pi"]);
|
|
1280
|
+
export const PROTECTED_FILES = new Set([
|
|
1281
|
+
".gitconfig",
|
|
1282
|
+
".gitmodules",
|
|
1283
|
+
".bashrc",
|
|
1284
|
+
".bash_profile",
|
|
1285
|
+
".bash_login",
|
|
1286
|
+
".bash_aliases",
|
|
1287
|
+
".bash_logout",
|
|
1288
|
+
".zshrc",
|
|
1289
|
+
".zprofile",
|
|
1290
|
+
".zshenv",
|
|
1291
|
+
".zlogin",
|
|
1292
|
+
".zlogout",
|
|
1293
|
+
".profile",
|
|
1294
|
+
".envrc",
|
|
1295
|
+
".npmrc",
|
|
1296
|
+
".yarnrc",
|
|
1297
|
+
".yarnrc.yml",
|
|
1298
|
+
".mcp.json",
|
|
1299
|
+
".pre-commit-config.yaml",
|
|
1300
|
+
"lefthook.yml",
|
|
1301
|
+
"lefthook.yaml",
|
|
1302
|
+
".lefthook.yml",
|
|
1303
|
+
".lefthook.yaml",
|
|
1304
|
+
]);
|
|
1305
|
+
|
|
1306
|
+
/**
|
|
1307
|
+
* Secret locations under the home directory (ask in every mode, never
|
|
1308
|
+
* covered by "allow reads outside the cwd"). Directories cover everything
|
|
1309
|
+
* inside them; public keys (`*.pub`) are exempt.
|
|
1310
|
+
*/
|
|
1311
|
+
export const SECRET_HOME_PATHS = [
|
|
1312
|
+
".ssh",
|
|
1313
|
+
".gnupg",
|
|
1314
|
+
".aws",
|
|
1315
|
+
".azure",
|
|
1316
|
+
".config/gcloud",
|
|
1317
|
+
".config/gh/hosts.yml",
|
|
1318
|
+
".docker/config.json",
|
|
1319
|
+
".kube/config",
|
|
1320
|
+
".netrc",
|
|
1321
|
+
".pgpass",
|
|
1322
|
+
".git-credentials",
|
|
1323
|
+
".pi/agent/auth.json",
|
|
1324
|
+
];
|
|
1325
|
+
|
|
1326
|
+
/** Secret file names anywhere: private keys and certificates bundles. */
|
|
1327
|
+
export const SECRET_FILE_PATTERNS: RegExp[] = [/^id_(rsa|dsa|ecdsa|ed25519)(_sk)?$/, /\.(pem|key|p12|pfx)$/];
|
|
1328
|
+
|
|
1329
|
+
/** Bash commands that write; with a protected path in the command → ask. */
|
|
1330
|
+
export const WRITE_INDICATORS: RegExp = /(^|[\s;&|])(tee|cp|mv|rm|ln|chmod|chown|truncate|install|rsync|dd)\b|\bsed\s+(-[a-zA-Z]*i|--in-place)|>(?!\s*\/dev\/null)(?!&)/;
|