@wraith-ai/core 0.1.0 → 0.1.1
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/README.md +1 -1
- package/dist/{attempt-BeB7EltG.d.mts → attempt-HgararU3.d.mts} +2 -1
- package/dist/attempt-HgararU3.d.mts.map +1 -0
- package/dist/{capabilities-BPb5HSH6.d.mts → capabilities-CNsPrkB7.d.mts} +2 -1
- package/dist/capabilities-CNsPrkB7.d.mts.map +1 -0
- package/dist/index.d.mts +6 -5
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +11 -9
- package/dist/index.mjs.map +1 -0
- package/dist/{methods-CZp6XGn8.mjs → methods-7Ryz6zbd.mjs} +4 -2
- package/dist/methods-7Ryz6zbd.mjs.map +1 -0
- package/dist/{model-B6tJ9kX_.mjs → model-C16NUwXW.mjs} +2 -0
- package/dist/model-C16NUwXW.mjs.map +1 -0
- package/dist/{model-D6LQSveS.d.mts → model-_wqePNTL.d.mts} +4 -3
- package/dist/model-_wqePNTL.d.mts.map +1 -0
- package/dist/{models-dev-Y0ZikMBQ.d.mts → models-dev-DNgymZy3.d.mts} +3 -2
- package/dist/models-dev-DNgymZy3.d.mts.map +1 -0
- package/dist/{openai-codex-BhtZvNpm.mjs → openai-codex-Bz1EFPEi.mjs} +3 -1
- package/dist/openai-codex-Bz1EFPEi.mjs.map +1 -0
- package/dist/{openai-codex-Bk-iG7MZ.mjs → openai-codex-uvjhVoVy.mjs} +6 -4
- package/dist/openai-codex-uvjhVoVy.mjs.map +1 -0
- package/dist/{openai-compatible-hkXv-P74.mjs → openai-compatible-DBppnQ_N.mjs} +4 -2
- package/dist/openai-compatible-DBppnQ_N.mjs.map +1 -0
- package/dist/{openrouter-BflQBug2.mjs → openrouter-D8nTOAQo.mjs} +5 -3
- package/dist/openrouter-D8nTOAQo.mjs.map +1 -0
- package/dist/providers/openai-codex.d.mts +4 -3
- package/dist/providers/openai-codex.d.mts.map +1 -0
- package/dist/providers/openai-codex.mjs +1 -1
- package/dist/providers/openai-compatible.d.mts +3 -2
- package/dist/providers/openai-compatible.d.mts.map +1 -0
- package/dist/providers/openai-compatible.mjs +1 -1
- package/dist/providers/openrouter.d.mts +3 -2
- package/dist/providers/openrouter.d.mts.map +1 -0
- package/dist/providers/openrouter.mjs +1 -1
- package/dist/{shared-8d7RsyPH.mjs → shared-BIJaPhMm.mjs} +3 -1
- package/dist/shared-BIJaPhMm.mjs.map +1 -0
- package/dist/snapshot.d.mts +3 -2
- package/dist/snapshot.d.mts.map +1 -0
- package/dist/snapshot.mjs +4999 -4977
- package/dist/snapshot.mjs.map +1 -0
- package/dist/{turn-effort-DquDDwdE.mjs → turn-effort-C61L4JIt.mjs} +2 -0
- package/dist/turn-effort-C61L4JIt.mjs.map +1 -0
- package/package.json +3 -2
- package/src/agent/agent.ts +90 -0
- package/src/agent/attachments.ts +195 -0
- package/src/agent/classifier.ts +525 -0
- package/src/agent/compact.ts +384 -0
- package/src/agent/conversation.ts +300 -0
- package/src/agent/cost.ts +164 -0
- package/src/agent/inbox.ts +80 -0
- package/src/agent/instructions.ts +62 -0
- package/src/agent/loop.ts +675 -0
- package/src/agent/messages.ts +252 -0
- package/src/agent/on-demand.ts +58 -0
- package/src/agent/prompts.ts +35 -0
- package/src/agent/request-images.ts +94 -0
- package/src/agent/stop.ts +37 -0
- package/src/agent/title.ts +122 -0
- package/src/agent/tool-groups.ts +186 -0
- package/src/agent/tools.ts +106 -0
- package/src/app.ts +67 -0
- package/src/config/credential-file-store.ts +66 -0
- package/src/config/environment.ts +105 -0
- package/src/config/instructions.ts +157 -0
- package/src/config/load.ts +667 -0
- package/src/config/locked-file.ts +85 -0
- package/src/config/paths.ts +368 -0
- package/src/config/schema.ts +363 -0
- package/src/config/subagents.ts +156 -0
- package/src/config/trust.ts +53 -0
- package/src/config/write.ts +241 -0
- package/src/errors.ts +85 -0
- package/src/events/types.ts +258 -0
- package/src/glob.ts +161 -0
- package/src/index.ts +681 -0
- package/src/logger.ts +47 -0
- package/src/paths.ts +143 -0
- package/src/permissions/chain.ts +42 -0
- package/src/permissions/command.ts +233 -0
- package/src/permissions/layered.ts +77 -0
- package/src/permissions/match.ts +80 -0
- package/src/permissions/modes.ts +48 -0
- package/src/permissions/policy.ts +160 -0
- package/src/permissions/types.ts +159 -0
- package/src/providers/cache.ts +107 -0
- package/src/providers/capabilities.ts +77 -0
- package/src/providers/catalog-view.ts +91 -0
- package/src/providers/catalog.ts +65 -0
- package/src/providers/errors.ts +108 -0
- package/src/providers/factories/index.ts +15 -0
- package/src/providers/factories/openai-codex.ts +382 -0
- package/src/providers/factories/openai-compatible.ts +81 -0
- package/src/providers/factories/openrouter.ts +165 -0
- package/src/providers/factories/shared.ts +22 -0
- package/src/providers/model.ts +197 -0
- package/src/providers/models-dev.ts +355 -0
- package/src/providers/oauth/methods.ts +10 -0
- package/src/providers/oauth/openai-codex.ts +228 -0
- package/src/providers/oauth.ts +177 -0
- package/src/providers/openai-codex.ts +7 -0
- package/src/providers/openai-compatible.ts +2 -0
- package/src/providers/openrouter.ts +2 -0
- package/src/providers/registry.ts +464 -0
- package/src/providers/turn-effort.ts +156 -0
- package/src/providers/types.ts +83 -0
- package/src/session/backend.ts +44 -0
- package/src/session/file-store.ts +278 -0
- package/src/session/format.ts +1037 -0
- package/src/session/memory-store.ts +73 -0
- package/src/session/session.ts +153 -0
- package/src/session/store.ts +219 -0
- package/src/sign-in/attempt.ts +166 -0
- package/src/sign-in/loopback.ts +74 -0
- package/src/snapshot/data.json +8936 -0
- package/src/snapshot/index.ts +37 -0
- package/src/tools/bin.ts +364 -0
- package/src/tools/builtin.ts +48 -0
- package/src/tools/change.ts +88 -0
- package/src/tools/define.ts +267 -0
- package/src/tools/describe.ts +96 -0
- package/src/tools/fs/edit-file.ts +129 -0
- package/src/tools/fs/glob.ts +75 -0
- package/src/tools/fs/grep.ts +212 -0
- package/src/tools/fs/line-endings.ts +17 -0
- package/src/tools/fs/list-dir.ts +85 -0
- package/src/tools/fs/read-file.ts +230 -0
- package/src/tools/fs/ripgrep.ts +211 -0
- package/src/tools/fs/write-file.ts +97 -0
- package/src/tools/job.ts +252 -0
- package/src/tools/mcp/auth-store.ts +127 -0
- package/src/tools/mcp/command.ts +151 -0
- package/src/tools/mcp/config.ts +168 -0
- package/src/tools/mcp/servers.ts +463 -0
- package/src/tools/mcp/sign-in.ts +226 -0
- package/src/tools/mcp/stdio.ts +204 -0
- package/src/tools/mcp/tools.ts +173 -0
- package/src/tools/on-demand.ts +184 -0
- package/src/tools/registry.ts +935 -0
- package/src/tools/shell/bash.ts +217 -0
- package/src/tools/shell/find-bash.ts +162 -0
- package/src/tools/source.ts +35 -0
- package/src/tools/spill.ts +127 -0
- package/src/tools/subagents.ts +597 -0
- package/src/tools/tasks.ts +603 -0
- package/src/tools/to-model.ts +33 -0
- package/src/tools/web/address.ts +95 -0
- package/src/tools/web/exa.ts +163 -0
- package/src/tools/web/fetch.ts +373 -0
- package/src/tools/web/html.ts +173 -0
- package/src/tools/web/search.ts +111 -0
- package/src/tools/web/source.ts +51 -0
- package/src/units.ts +18 -0
- package/src/version.ts +23 -0
package/src/paths.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
2
|
+
import { AgentError } from "./errors";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Every path a tool touches passes through here. It keeps the agent inside the
|
|
6
|
+
* workspace and gives the permission policy one canonical spelling to match
|
|
7
|
+
* rules against. The permission policy needs it too, so it stays out of
|
|
8
|
+
* `tools/`.
|
|
9
|
+
*
|
|
10
|
+
* `node:path` only, with no filesystem access. Symlinks are not resolved;
|
|
11
|
+
* escaping the workspace through a symlink is possible and accepted for now.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export function toPosix(path: string): string {
|
|
15
|
+
return path.split(sep).join("/").replace(/\\/g, "/");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* An optional path argument, as the workspace root when there is none.
|
|
20
|
+
*
|
|
21
|
+
* An empty or blank string counts as none too; without that, an empty path
|
|
22
|
+
* reaches the permission policy, and `describe` has nothing to say.
|
|
23
|
+
*/
|
|
24
|
+
export function pathOrRoot(path: string | undefined): string {
|
|
25
|
+
return path === undefined || path.trim() === "" ? "." : path;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A drive path as a POSIX shell on Windows prints it — `/c/Users/x` from Git
|
|
30
|
+
* Bash, `/mnt/c/…`, `/cygdrive/c/…` — as the path it names. `pwd` hands the
|
|
31
|
+
* model that spelling, and its next `read_file` arrives with it.
|
|
32
|
+
*/
|
|
33
|
+
export function fromShellPath(path: string, windows: boolean = sep === "\\"): string {
|
|
34
|
+
if (!windows) return path;
|
|
35
|
+
const match = /^\/(?:(?:mnt|cygdrive)\/)?([A-Za-z])(?=\/|$)/.exec(path);
|
|
36
|
+
if (match?.[1] === undefined) return path;
|
|
37
|
+
return `${match[1].toUpperCase()}:${path.slice(match[0].length) || "/"}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function absoluteFrom(cwd: string, path: string): string {
|
|
41
|
+
const native = fromShellPath(path);
|
|
42
|
+
return isAbsolute(native) ? resolve(native) : resolve(cwd, native);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Workspace-relative posix spelling, used for rules, claims and display. */
|
|
46
|
+
export function workspaceRelative(cwd: string, path: string): string {
|
|
47
|
+
const result = relative(resolve(cwd), absoluteFrom(cwd, path));
|
|
48
|
+
return result === "" ? "." : toPosix(result);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function isInsideWorkspace(cwd: string, path: string): boolean {
|
|
52
|
+
const result = workspaceRelative(cwd, path);
|
|
53
|
+
return result !== ".." && !result.startsWith("../") && !isAbsolute(result);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A directory outside the workspace that a host opened to the file tools. */
|
|
57
|
+
export interface ExtraRoot {
|
|
58
|
+
/** Absolute. */
|
|
59
|
+
path: string;
|
|
60
|
+
/** `write` includes reading. */
|
|
61
|
+
access: "read" | "write";
|
|
62
|
+
/**
|
|
63
|
+
* The same directory as the shell spells it, when no rule can derive that:
|
|
64
|
+
* Git Bash shows the Windows temp directory as `/tmp`. A path under the alias
|
|
65
|
+
* means this root.
|
|
66
|
+
*/
|
|
67
|
+
alias?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Where a tool may act. A `ToolContext` is one, and so are `PolicyOptions`. */
|
|
71
|
+
export interface PathScope {
|
|
72
|
+
cwd: string;
|
|
73
|
+
roots?: readonly ExtraRoot[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The extra root holding this path, and the path as the disk knows it.
|
|
78
|
+
*
|
|
79
|
+
* Absolute paths only: a relative one always means the workspace, so `../x`
|
|
80
|
+
* cannot wander into a root by accident of where the two directories sit.
|
|
81
|
+
*/
|
|
82
|
+
function placeInRoot(
|
|
83
|
+
scope: PathScope,
|
|
84
|
+
path: string,
|
|
85
|
+
): { root: ExtraRoot; absolute: string } | undefined {
|
|
86
|
+
const spelled = toPosix(path);
|
|
87
|
+
|
|
88
|
+
for (const root of scope.roots ?? []) {
|
|
89
|
+
const { alias } = root;
|
|
90
|
+
const native =
|
|
91
|
+
alias !== undefined && (spelled === alias || spelled.startsWith(`${alias}/`))
|
|
92
|
+
? join(root.path, spelled.slice(alias.length))
|
|
93
|
+
: fromShellPath(path);
|
|
94
|
+
// Checked after the alias is undone, so `<alias>/../x` is still an escape.
|
|
95
|
+
if (isAbsolute(native) && isInsideWorkspace(root.path, native)) {
|
|
96
|
+
return { root, absolute: resolve(native) };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function extraRootOf(scope: PathScope, path: string): ExtraRoot | undefined {
|
|
103
|
+
return placeInRoot(scope, path)?.root;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Absolute path, or an AgentError when it leaves the workspace and every root
|
|
108
|
+
* the host opened. A bare `cwd` string is accepted as the scope.
|
|
109
|
+
*/
|
|
110
|
+
export function resolveInWorkspace(
|
|
111
|
+
scope: string | PathScope,
|
|
112
|
+
path: string,
|
|
113
|
+
access: "read" | "write" = "read",
|
|
114
|
+
): string {
|
|
115
|
+
const where: PathScope = typeof scope === "string" ? { cwd: scope } : scope;
|
|
116
|
+
|
|
117
|
+
if (isInsideWorkspace(where.cwd, path)) return absoluteFrom(where.cwd, path);
|
|
118
|
+
|
|
119
|
+
const placed = placeInRoot(where, path);
|
|
120
|
+
if (placed === undefined) {
|
|
121
|
+
throw new AgentError(
|
|
122
|
+
"tool/path-escape",
|
|
123
|
+
`Path "${path}" is outside the workspace and cannot be used.`,
|
|
124
|
+
{ details: { path, cwd: toPosix(where.cwd) } },
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
if (access === "write" && placed.root.access === "read") {
|
|
128
|
+
throw new AgentError("tool/path-escape", `Path "${path}" is read-only and cannot be written.`, {
|
|
129
|
+
details: { path, root: toPosix(placed.root.path) },
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return placed.absolute;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* What a tool reports back: workspace-relative inside it, absolute outside.
|
|
137
|
+
*
|
|
138
|
+
* Forward slashes either way — the same string has to work in `read_file` and
|
|
139
|
+
* in bash, where a backslash is an escape.
|
|
140
|
+
*/
|
|
141
|
+
export function displayPath(cwd: string, path: string): string {
|
|
142
|
+
return isInsideWorkspace(cwd, path) ? workspaceRelative(cwd, path) : toPosix(absoluteFrom(cwd, path));
|
|
143
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ApprovalHandler } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Asks handlers in order until one answers. A decision, approved or denied,
|
|
5
|
+
* ends the chain; only `undefined` ("I could not decide") moves on. If none
|
|
6
|
+
* answers, the chain returns `undefined`, which the registry reads as a missing
|
|
7
|
+
* handler: refused, recorded as `unattended`, never as the user's.
|
|
8
|
+
*
|
|
9
|
+
* A refusal is an answer: waking a person for each one would make `--auto` add
|
|
10
|
+
* prompts instead of removing them, and the escalation counter would have
|
|
11
|
+
* nothing to count. A handler that cannot reach its model returns `undefined`,
|
|
12
|
+
* since it has decided nothing; pretending otherwise would leave an agent
|
|
13
|
+
* refusing everything while a provider is down. The host chooses the order:
|
|
14
|
+
* which handler is cheap, which is a person, and whether there is one at all.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Given only handlers that are present, the result is a handler. The
|
|
18
|
+
* `undefined` slots are for a host with a person only if there is a terminal;
|
|
19
|
+
* only this form can return no handler at all.
|
|
20
|
+
*/
|
|
21
|
+
export function chainApproval(
|
|
22
|
+
first: ApprovalHandler,
|
|
23
|
+
...rest: readonly ApprovalHandler[]
|
|
24
|
+
): ApprovalHandler;
|
|
25
|
+
export function chainApproval(
|
|
26
|
+
...handlers: readonly (ApprovalHandler | undefined)[]
|
|
27
|
+
): ApprovalHandler | undefined;
|
|
28
|
+
export function chainApproval(
|
|
29
|
+
...handlers: readonly (ApprovalHandler | undefined)[]
|
|
30
|
+
): ApprovalHandler | undefined {
|
|
31
|
+
const present = handlers.filter((handler): handler is ApprovalHandler => handler !== undefined);
|
|
32
|
+
if (present.length === 0) return undefined;
|
|
33
|
+
if (present.length === 1) return present[0];
|
|
34
|
+
|
|
35
|
+
return async (request, context) => {
|
|
36
|
+
for (const handler of present) {
|
|
37
|
+
const answer = await handler(request, context);
|
|
38
|
+
if (answer !== undefined) return answer;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits a shell command into segments and lists what the text cannot settle.
|
|
3
|
+
*
|
|
4
|
+
* Each segment is judged on its own, so `git *` does not cover
|
|
5
|
+
* `git status; rm -rf src`. Covered: chaining, pipes, substitution (the inner
|
|
6
|
+
* command runs first), variables, redirection and wrappers. Whatever the text
|
|
7
|
+
* cannot settle is not allowed on a rule; it goes to the human.
|
|
8
|
+
*
|
|
9
|
+
* Quote-aware enough to split correctly, not a shell parser: it fails closed.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Commands whose whole purpose is to run another command. */
|
|
13
|
+
const WRAPPERS = new Set([
|
|
14
|
+
"bash",
|
|
15
|
+
"busybox",
|
|
16
|
+
"command",
|
|
17
|
+
"dash",
|
|
18
|
+
"doas",
|
|
19
|
+
"env",
|
|
20
|
+
"eval",
|
|
21
|
+
"exec",
|
|
22
|
+
"flock",
|
|
23
|
+
"ionice",
|
|
24
|
+
"nice",
|
|
25
|
+
"nohup",
|
|
26
|
+
"setsid",
|
|
27
|
+
"sh",
|
|
28
|
+
"ssh",
|
|
29
|
+
"sudo",
|
|
30
|
+
"time",
|
|
31
|
+
"timeout",
|
|
32
|
+
"watch",
|
|
33
|
+
"xargs",
|
|
34
|
+
"zsh",
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
/** `find` is only a wrapper when asked to be one. */
|
|
38
|
+
const FIND_ACTIONS = ["-exec", "-execdir", "-ok", "-okdir", "-delete"];
|
|
39
|
+
|
|
40
|
+
export interface CommandAnalysis {
|
|
41
|
+
/** Each independently executed command on the line. */
|
|
42
|
+
segments: string[];
|
|
43
|
+
/**
|
|
44
|
+
* Why the line cannot be judged from its text alone. Any concern caps a
|
|
45
|
+
* rule-derived `allow` at `ask` — it never turns an `allow` into a `deny`,
|
|
46
|
+
* and it never overrides a mode the user chose deliberately.
|
|
47
|
+
*/
|
|
48
|
+
concerns: string[];
|
|
49
|
+
/**
|
|
50
|
+
* A lone `&` leaves a command running after the line returns. A line that
|
|
51
|
+
* ends in `wait` leaves nothing, so it does not count.
|
|
52
|
+
*/
|
|
53
|
+
detaches: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function analyzeCommand(command: string): CommandAnalysis {
|
|
57
|
+
const segments: string[] = [];
|
|
58
|
+
const concerns = new Set<string>();
|
|
59
|
+
let backgrounded = false;
|
|
60
|
+
|
|
61
|
+
let current = "";
|
|
62
|
+
let quote: '"' | "'" | null = null;
|
|
63
|
+
let escaped = false;
|
|
64
|
+
|
|
65
|
+
const push = (): void => {
|
|
66
|
+
const trimmed = current.trim();
|
|
67
|
+
if (trimmed !== "") segments.push(trimmed);
|
|
68
|
+
current = "";
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
for (let index = 0; index < command.length; index += 1) {
|
|
72
|
+
const char = command[index] as string;
|
|
73
|
+
const next = command[index + 1];
|
|
74
|
+
|
|
75
|
+
if (escaped) {
|
|
76
|
+
current += char;
|
|
77
|
+
escaped = false;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (char === "\\" && quote !== "'") {
|
|
81
|
+
current += char;
|
|
82
|
+
escaped = true;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (quote !== null) {
|
|
87
|
+
// Single quotes suppress expansion; double quotes do not, so `$(…)`
|
|
88
|
+
// inside them is still a live command.
|
|
89
|
+
if (quote === '"') noteExpansions(char, next, concerns);
|
|
90
|
+
current += char;
|
|
91
|
+
if (char === quote) quote = null;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (char === '"' || char === "'") {
|
|
96
|
+
quote = char;
|
|
97
|
+
current += char;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
noteExpansions(char, next, concerns);
|
|
102
|
+
|
|
103
|
+
// The `&` of `2>&1` or `<&3` belongs to the redirection; read as a
|
|
104
|
+
// separator it cut `bun test 2>&1` in two, and no rule matched the "1".
|
|
105
|
+
if ((char === ">" || char === "<") && next === "&") {
|
|
106
|
+
// `>&2` points a descriptor at another one; `>&file` writes the file.
|
|
107
|
+
const target = command[index + 2];
|
|
108
|
+
const duplicates = target !== undefined && /[0-9-]/.test(target);
|
|
109
|
+
if (char === ">" && !duplicates && !discards(command, index + 2)) {
|
|
110
|
+
concerns.add("output redirection");
|
|
111
|
+
}
|
|
112
|
+
current += char + next;
|
|
113
|
+
index += 1;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (char === ">") {
|
|
118
|
+
// `>>` appends and `>|` overrides noclobber; the bar is not a pipe.
|
|
119
|
+
const operator = next === ">" || next === "|" ? char + next : char;
|
|
120
|
+
if (!discards(command, index + operator.length)) concerns.add("output redirection");
|
|
121
|
+
current += operator;
|
|
122
|
+
index += operator.length - 1;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// `&>file` and `&>>file` send both streams to a file; the `>` decides which.
|
|
127
|
+
if (char === "&" && next === ">") {
|
|
128
|
+
current += char;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (char === "\n") {
|
|
133
|
+
push();
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (char === ";") {
|
|
138
|
+
push();
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (char === "&" || char === "|") {
|
|
143
|
+
push();
|
|
144
|
+
// Swallow the second character of &&, ||, |& so it does not start a segment.
|
|
145
|
+
if (next === "&" || next === "|") index += 1;
|
|
146
|
+
else if (char === "&") backgrounded = true;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
current += char;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
push();
|
|
154
|
+
|
|
155
|
+
if (quote !== null) concerns.add("unbalanced quoting");
|
|
156
|
+
|
|
157
|
+
for (const segment of segments) {
|
|
158
|
+
const words = tokenize(segment);
|
|
159
|
+
const head = words[0];
|
|
160
|
+
if (head === undefined) continue;
|
|
161
|
+
|
|
162
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(head)) {
|
|
163
|
+
concerns.add("environment assignment");
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (WRAPPERS.has(basename(head))) {
|
|
167
|
+
concerns.add(`${basename(head)} runs another command`);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (basename(head) === "find" && words.some((word) => FIND_ACTIONS.includes(word))) {
|
|
171
|
+
concerns.add("find runs another command");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const last = segments[segments.length - 1];
|
|
176
|
+
const waits = last !== undefined && tokenize(last)[0] === "wait";
|
|
177
|
+
return { segments, concerns: [...concerns], detaches: backgrounded && !waits };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** `/dev/null` is no file; only the bare word counts, so `/dev/nullx` is still a write. */
|
|
181
|
+
function discards(command: string, from: number): boolean {
|
|
182
|
+
return /^[ \t]*\/dev\/null(?=[\s;&|<>()]|$)/.test(command.slice(from));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function noteExpansions(char: string, next: string | undefined, concerns: Set<string>): void {
|
|
186
|
+
if (char === "`") {
|
|
187
|
+
concerns.add("command substitution");
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (char === "<" && next === "(") {
|
|
191
|
+
concerns.add("process substitution");
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (char !== "$" || next === undefined) return;
|
|
195
|
+
if (next === "(") {
|
|
196
|
+
concerns.add("command substitution");
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (/[A-Za-z_{]/.test(next)) concerns.add("variable expansion");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Whitespace split that keeps quoted runs together and drops the quotes. */
|
|
203
|
+
function tokenize(segment: string): string[] {
|
|
204
|
+
const words: string[] = [];
|
|
205
|
+
let current = "";
|
|
206
|
+
let quote: '"' | "'" | null = null;
|
|
207
|
+
|
|
208
|
+
for (const char of segment) {
|
|
209
|
+
if (quote !== null) {
|
|
210
|
+
if (char === quote) quote = null;
|
|
211
|
+
else current += char;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (char === '"' || char === "'") {
|
|
215
|
+
quote = char;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (/\s/.test(char)) {
|
|
219
|
+
if (current !== "") words.push(current);
|
|
220
|
+
current = "";
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
current += char;
|
|
224
|
+
}
|
|
225
|
+
if (current !== "") words.push(current);
|
|
226
|
+
return words;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** `/usr/bin/sudo` is still sudo. */
|
|
230
|
+
function basename(word: string): string {
|
|
231
|
+
const parts = word.split(/[/\\]/);
|
|
232
|
+
return parts[parts.length - 1] ?? word;
|
|
233
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { createPermissionPolicy, type PolicyOptions } from "./policy";
|
|
2
|
+
import type {
|
|
3
|
+
PermissionPolicy,
|
|
4
|
+
PermissionPolicyInput,
|
|
5
|
+
PermissionRule,
|
|
6
|
+
WorkspaceWrites,
|
|
7
|
+
} from "./types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Permissions a running session has changed, over what its config file says.
|
|
11
|
+
*
|
|
12
|
+
* `mode` is not here: it is read late, separately. The classifier is not here
|
|
13
|
+
* either, since composing one needs the network, and this layer must stay
|
|
14
|
+
* synchronous.
|
|
15
|
+
*/
|
|
16
|
+
export interface SessionPermissions {
|
|
17
|
+
workspaceWrites?: WorkspaceWrites;
|
|
18
|
+
/**
|
|
19
|
+
* Added to the base's rules. Never a replacement for them.
|
|
20
|
+
*/
|
|
21
|
+
rules?: readonly PermissionRule[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A policy that answers from the config file until the session overrides it.
|
|
26
|
+
* Each composed policy is immutable; only which one answers can change. A
|
|
27
|
+
* session changes its permissions in the middle of a turn, where the agent
|
|
28
|
+
* cannot be rebuilt: a rebuild closes the tool registry the turn is using.
|
|
29
|
+
*
|
|
30
|
+
* - Rules are appended, never substituted: a session may not drop a `deny`
|
|
31
|
+
* from the base. `overrides.rules ?? base.rules` deletes them silently.
|
|
32
|
+
* - `workspaceWrites` is overridden, not merged: it is one value.
|
|
33
|
+
* - Empty overrides count as absent: the base policy answers, not a copy of it.
|
|
34
|
+
* - Composed policies are memoised on the identity of what `overrides` returns.
|
|
35
|
+
*
|
|
36
|
+
* @param overrides Asked at the moment a call needs deciding, never captured.
|
|
37
|
+
* Must return a new object on any change: a mutated one keeps its identity,
|
|
38
|
+
* so the old policy keeps answering, silently.
|
|
39
|
+
*/
|
|
40
|
+
export function createLayeredPolicy(
|
|
41
|
+
base: PermissionPolicyInput,
|
|
42
|
+
overrides: () => SessionPermissions | undefined,
|
|
43
|
+
options: PolicyOptions,
|
|
44
|
+
): PermissionPolicy {
|
|
45
|
+
const fromBase = createPermissionPolicy(base, options);
|
|
46
|
+
|
|
47
|
+
let lastOverrides: SessionPermissions | undefined;
|
|
48
|
+
let lastPolicy = fromBase;
|
|
49
|
+
|
|
50
|
+
const current = (): PermissionPolicy => {
|
|
51
|
+
const session = overrides();
|
|
52
|
+
if (session === undefined || isEmpty(session)) return fromBase;
|
|
53
|
+
|
|
54
|
+
if (session !== lastOverrides) {
|
|
55
|
+
lastOverrides = session;
|
|
56
|
+
lastPolicy = createPermissionPolicy(
|
|
57
|
+
{
|
|
58
|
+
...base,
|
|
59
|
+
...(session.workspaceWrites === undefined
|
|
60
|
+
? {}
|
|
61
|
+
: { workspaceWrites: session.workspaceWrites }),
|
|
62
|
+
rules: [...base.rules, ...(session.rules ?? [])],
|
|
63
|
+
},
|
|
64
|
+
options,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return lastPolicy;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return { decide: (query) => current().decide(query) };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Nothing changed yet, which is what every session looks like at the start. */
|
|
75
|
+
function isEmpty(overrides: SessionPermissions): boolean {
|
|
76
|
+
return overrides.workspaceWrites === undefined && (overrides.rules ?? []).length === 0;
|
|
77
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { matchGlob, type GlobMode } from "../glob";
|
|
2
|
+
import type { PermissionRule } from "./types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 1. A leading `!` negates a pattern: `!.git/**` matches paths outside `.git`.
|
|
6
|
+
* 2. A permissive rule must cover **every** claimed path; a restrictive one
|
|
7
|
+
* needs only **one**. Otherwise a call touching `src/a.ts` and `.git/config`
|
|
8
|
+
* would be allowed outright by a rule about `src/**`.
|
|
9
|
+
* 3. Command patterns are matched flat, so `rm *` covers `rm -rf /`. Under path
|
|
10
|
+
* semantics `*` stops at a slash, which is wrong for a command.
|
|
11
|
+
* 4. A `domain` is compared with the host an address names, and only a call
|
|
12
|
+
* that claims an address can match one.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export interface RuleTarget {
|
|
16
|
+
toolName: string;
|
|
17
|
+
/** Workspace-relative, posix separators. */
|
|
18
|
+
paths: readonly string[];
|
|
19
|
+
command?: string;
|
|
20
|
+
url?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function matchPattern(pattern: string, value: string, mode: GlobMode = "path"): boolean {
|
|
24
|
+
if (pattern.startsWith("!")) return !matchGlob(pattern.slice(1), value, mode);
|
|
25
|
+
return matchGlob(pattern, value, mode);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The host an address names, as a rule compares it: lower case, no trailing dot. */
|
|
29
|
+
export function hostOf(url: string): string | undefined {
|
|
30
|
+
try {
|
|
31
|
+
const host = new URL(url).hostname.toLowerCase().replace(/\.$/, "");
|
|
32
|
+
return host === "" ? undefined : host;
|
|
33
|
+
} catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function hostMatches(pattern: string, host: string): boolean {
|
|
39
|
+
const wanted = pattern.toLowerCase().replace(/\.$/, "");
|
|
40
|
+
if (wanted === "*") return true;
|
|
41
|
+
// Subdomains at any depth, and not the name itself.
|
|
42
|
+
if (wanted.startsWith("*.")) return host.endsWith(wanted.slice(1));
|
|
43
|
+
return host === wanted;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function ruleMatches(rule: PermissionRule, target: RuleTarget): boolean {
|
|
47
|
+
if (rule.tool !== undefined && !matchPattern(rule.tool, target.toolName)) return false;
|
|
48
|
+
|
|
49
|
+
if (rule.domain !== undefined) {
|
|
50
|
+
// The host, never the text: `https://docs.rs@evil.com/` reads as docs.rs.
|
|
51
|
+
const host = target.url === undefined ? undefined : hostOf(target.url);
|
|
52
|
+
if (host === undefined || !hostMatches(rule.domain, host)) return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (rule.path !== undefined) {
|
|
56
|
+
if (target.paths.length === 0) return false;
|
|
57
|
+
// Claimed paths are workspace-relative, so `./secrets/**` would match nothing.
|
|
58
|
+
const pattern = rule.path.replace(/^(!?)(?:\.\/)+/, "$1");
|
|
59
|
+
const matches = target.paths.filter((path) => matchPattern(pattern, path));
|
|
60
|
+
const needsAll = rule.decision === "allow";
|
|
61
|
+
if (needsAll ? matches.length !== target.paths.length : matches.length === 0) return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (rule.command !== undefined) {
|
|
65
|
+
if (target.command === undefined) return false;
|
|
66
|
+
if (!matchPattern(rule.command, target.command, "flat")) return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** How the matched rule is explained to the user and to the model. */
|
|
73
|
+
export function describeRule(rule: PermissionRule): string {
|
|
74
|
+
const parts: string[] = [];
|
|
75
|
+
if (rule.tool !== undefined) parts.push(`tool ${rule.tool}`);
|
|
76
|
+
if (rule.path !== undefined) parts.push(`path ${rule.path}`);
|
|
77
|
+
if (rule.command !== undefined) parts.push(`command ${rule.command}`);
|
|
78
|
+
if (rule.domain !== undefined) parts.push(`domain ${rule.domain}`);
|
|
79
|
+
return parts.length === 0 ? "the catch-all rule" : `rule ${parts.join(" + ")}`;
|
|
80
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Config } from "../config/schema";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Who answers a permission question: a person, the classifier first, or nobody
|
|
5
|
+
* because everything is approved. Not the policy's `PermissionMode`, which has
|
|
6
|
+
* a `deny` value: this is which handler sits in the chain, a host's choice.
|
|
7
|
+
*/
|
|
8
|
+
export type ApprovalMode = "ask" | "auto" | "yolo";
|
|
9
|
+
|
|
10
|
+
export interface ApprovalModes {
|
|
11
|
+
/** Where this process starts. */
|
|
12
|
+
initial: ApprovalMode;
|
|
13
|
+
/**
|
|
14
|
+
* Every mode this process can reach. Decided at launch and never widened:
|
|
15
|
+
* `yolo` is in the set only when a flag or the config file put it there, so
|
|
16
|
+
* no keystroke can reach it.
|
|
17
|
+
*/
|
|
18
|
+
reachable: readonly ApprovalMode[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ApprovalModeOptions {
|
|
22
|
+
yolo: boolean;
|
|
23
|
+
auto: boolean;
|
|
24
|
+
/** Whether the host can switch at all. A one-shot run cannot; a chat can. */
|
|
25
|
+
switchable: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The launch-time half of the permission mode: where a host starts, and what
|
|
30
|
+
* it may switch to. `classifier.enabled` folds into the mode and is not read
|
|
31
|
+
* again, so the mode is the single truth about who answers. Otherwise a status
|
|
32
|
+
* bar could say `ask` while a model decides, and switching to `ask` would turn
|
|
33
|
+
* nothing off.
|
|
34
|
+
*/
|
|
35
|
+
export function approvalModes(config: Config, options: ApprovalModeOptions): ApprovalModes {
|
|
36
|
+
// A config file that already says yolo leaves nothing to switch to: `ask` and
|
|
37
|
+
// `auto` would both run the configured policy, which passes everything.
|
|
38
|
+
if (config.permissions.mode === "yolo") return { initial: "yolo", reachable: ["yolo"] };
|
|
39
|
+
|
|
40
|
+
const initial: ApprovalMode = options.yolo
|
|
41
|
+
? "yolo"
|
|
42
|
+
: options.auto || config.permissions.classifier.enabled
|
|
43
|
+
? "auto"
|
|
44
|
+
: "ask";
|
|
45
|
+
|
|
46
|
+
if (!options.switchable) return { initial, reachable: [initial] };
|
|
47
|
+
return { initial, reachable: options.yolo ? ["ask", "auto", "yolo"] : ["ask", "auto"] };
|
|
48
|
+
}
|