@dream_engine/computer 0.0.0-stage → 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/LICENSE +21 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/computer-agent.d.ts +93 -0
- package/dist/computer-agent.d.ts.map +1 -0
- package/dist/computer-agent.js +126 -0
- package/dist/computer-agent.js.map +1 -0
- package/dist/desktop-assessment.d.ts +95 -0
- package/dist/desktop-assessment.d.ts.map +1 -0
- package/dist/desktop-assessment.js +127 -0
- package/dist/desktop-assessment.js.map +1 -0
- package/dist/filesystem.d.ts +17 -0
- package/dist/filesystem.d.ts.map +1 -0
- package/dist/filesystem.js +245 -0
- package/dist/filesystem.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/permission.d.ts +76 -0
- package/dist/permission.d.ts.map +1 -0
- package/dist/permission.js +142 -0
- package/dist/permission.js.map +1 -0
- package/dist/security-review.d.ts +134 -0
- package/dist/security-review.d.ts.map +1 -0
- package/dist/security-review.js +253 -0
- package/dist/security-review.js.map +1 -0
- package/dist/terminal.d.ts +22 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +141 -0
- package/dist/terminal.js.map +1 -0
- package/package.json +34 -3
- package/README.md +0 -3
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ToolRegistry } from '@dream_engine/agent';
|
|
2
|
+
import type { GateLike } from './permission.js';
|
|
3
|
+
/** Host dependencies for the filesystem tools. */
|
|
4
|
+
export interface FilesystemHost {
|
|
5
|
+
gate: GateLike;
|
|
6
|
+
cwd: string;
|
|
7
|
+
maxReadBytes?: number;
|
|
8
|
+
maxList?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates the filesystem tools for Agent 2. Every operation is gated by the
|
|
12
|
+
* {@link GateLike} permission gate before execution. Paths are resolved against the
|
|
13
|
+
* host cwd; writes/deletes are confined to the configured roots unless the gate
|
|
14
|
+
* grants unrestricted execution.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createFilesystemTools(host: FilesystemHost): ToolRegistry;
|
|
17
|
+
//# sourceMappingURL=filesystem.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesystem.d.ts","sourceRoot":"","sources":["../src/filesystem.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAgC,MAAM,qBAAqB,CAAC;AACjF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEhD,kDAAkD;AAClD,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,QAAQ,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAWD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,cAAc,GAAG,YAAY,CA4OxE"}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import { dirname, join, relative, resolve } from 'node:path';
|
|
3
|
+
import { ValidationError, RuntimeError, serializeError } from '@dream_engine/core';
|
|
4
|
+
import { isPathInside, sanitizeFilename } from '@dream_engine/security';
|
|
5
|
+
import { ToolRegistry, defineTool } from '@dream_engine/agent';
|
|
6
|
+
const DEFAULT_MAX_READ_BYTES = 256 * 1024;
|
|
7
|
+
const DEFAULT_MAX_LIST = 200;
|
|
8
|
+
/** Resolves a path against the host cwd and normalises it. */
|
|
9
|
+
function resolvePath(host, p) {
|
|
10
|
+
const abs = resolve(host.cwd, p);
|
|
11
|
+
return abs;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Creates the filesystem tools for Agent 2. Every operation is gated by the
|
|
15
|
+
* {@link GateLike} permission gate before execution. Paths are resolved against the
|
|
16
|
+
* host cwd; writes/deletes are confined to the configured roots unless the gate
|
|
17
|
+
* grants unrestricted execution.
|
|
18
|
+
*/
|
|
19
|
+
export function createFilesystemTools(host) {
|
|
20
|
+
const maxRead = host.maxReadBytes ?? DEFAULT_MAX_READ_BYTES;
|
|
21
|
+
const maxList = host.maxList ?? DEFAULT_MAX_LIST;
|
|
22
|
+
const registry = new ToolRegistry();
|
|
23
|
+
registry.register(defineTool({
|
|
24
|
+
name: 'computer_read_file',
|
|
25
|
+
description: 'Read a file from the local filesystem. Returns the file contents (truncated to maxReadBytes).',
|
|
26
|
+
inputSchema: {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: { path: { type: 'string', description: 'File path (absolute or relative to cwd).' } },
|
|
29
|
+
required: ['path'],
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
},
|
|
32
|
+
annotations: { readOnlyHint: true },
|
|
33
|
+
handler: async (args, _ctx) => {
|
|
34
|
+
const abs = resolvePath(host, args.path);
|
|
35
|
+
const check = host.gate.checkRead(abs);
|
|
36
|
+
if (!check.allowed)
|
|
37
|
+
throw new RuntimeError(check.reason ?? 'Read denied');
|
|
38
|
+
try {
|
|
39
|
+
const content = await fs.readFile(abs, 'utf8');
|
|
40
|
+
return { data: { path: abs, content: content.slice(0, maxRead), truncated: content.length > maxRead } };
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
throw new RuntimeError(`Cannot read file: ${error.message}`, { details: { path: abs } });
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
}));
|
|
47
|
+
registry.register(defineTool({
|
|
48
|
+
name: 'computer_write_file',
|
|
49
|
+
description: 'Write content to a file on the local filesystem. Creates or overwrites the file.',
|
|
50
|
+
inputSchema: {
|
|
51
|
+
type: 'object',
|
|
52
|
+
properties: {
|
|
53
|
+
path: { type: 'string', description: 'File path (absolute or relative to cwd).' },
|
|
54
|
+
content: { type: 'string', description: 'Content to write.' },
|
|
55
|
+
},
|
|
56
|
+
required: ['path', 'content'],
|
|
57
|
+
additionalProperties: false,
|
|
58
|
+
},
|
|
59
|
+
annotations: { destructiveHint: true },
|
|
60
|
+
handler: async (args, _ctx) => {
|
|
61
|
+
const abs = resolvePath(host, args.path);
|
|
62
|
+
const check = host.gate.checkWrite(abs);
|
|
63
|
+
if (!check.allowed)
|
|
64
|
+
throw new RuntimeError(check.reason ?? 'Write denied');
|
|
65
|
+
try {
|
|
66
|
+
await fs.mkdir(dirname(abs), { recursive: true });
|
|
67
|
+
await fs.writeFile(abs, args.content, 'utf8');
|
|
68
|
+
return { data: { path: abs, bytesWritten: args.content.length } };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw new RuntimeError(`Cannot write file: ${error.message}`, { details: { path: abs } });
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
}));
|
|
75
|
+
registry.register(defineTool({
|
|
76
|
+
name: 'computer_list_dir',
|
|
77
|
+
description: 'List the contents of a directory. Returns entries with type, size and modified time.',
|
|
78
|
+
inputSchema: {
|
|
79
|
+
type: 'object',
|
|
80
|
+
properties: { path: { type: 'string', description: 'Directory path (default: cwd).' } },
|
|
81
|
+
required: [],
|
|
82
|
+
additionalProperties: false,
|
|
83
|
+
},
|
|
84
|
+
annotations: { readOnlyHint: true },
|
|
85
|
+
handler: async (args, _ctx) => {
|
|
86
|
+
const abs = resolvePath(host, args.path ?? '.');
|
|
87
|
+
const check = host.gate.checkRead(abs);
|
|
88
|
+
if (!check.allowed)
|
|
89
|
+
throw new RuntimeError(check.reason ?? 'Read denied');
|
|
90
|
+
try {
|
|
91
|
+
const entries = await fs.readdir(abs, { withFileTypes: true });
|
|
92
|
+
const items = await Promise.all(entries.slice(0, maxList).map(async (e) => {
|
|
93
|
+
const full = join(abs, e.name);
|
|
94
|
+
let size = 0;
|
|
95
|
+
let mtime = '';
|
|
96
|
+
try {
|
|
97
|
+
const stat = await fs.stat(full);
|
|
98
|
+
size = stat.size;
|
|
99
|
+
mtime = stat.mtime.toISOString();
|
|
100
|
+
}
|
|
101
|
+
catch { /* ignore */ }
|
|
102
|
+
return { name: e.name, type: e.isDirectory() ? 'dir' : 'file', size, mtime };
|
|
103
|
+
}));
|
|
104
|
+
return { data: { path: abs, entries: items, truncated: entries.length > maxList } };
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
throw new RuntimeError(`Cannot list directory: ${error.message}`, { details: { path: abs } });
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
}));
|
|
111
|
+
registry.register(defineTool({
|
|
112
|
+
name: 'computer_move_file',
|
|
113
|
+
description: 'Move or rename a file or directory on the local filesystem.',
|
|
114
|
+
inputSchema: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: {
|
|
117
|
+
from: { type: 'string', description: 'Source path.' },
|
|
118
|
+
to: { type: 'string', description: 'Destination path.' },
|
|
119
|
+
},
|
|
120
|
+
required: ['from', 'to'],
|
|
121
|
+
additionalProperties: false,
|
|
122
|
+
},
|
|
123
|
+
annotations: { destructiveHint: true },
|
|
124
|
+
handler: async (args, _ctx) => {
|
|
125
|
+
const fromAbs = resolvePath(host, args.from);
|
|
126
|
+
const toAbs = resolvePath(host, args.to);
|
|
127
|
+
const check = host.gate.checkWrite(toAbs);
|
|
128
|
+
if (!check.allowed)
|
|
129
|
+
throw new RuntimeError(check.reason ?? 'Write denied');
|
|
130
|
+
try {
|
|
131
|
+
await fs.rename(fromAbs, toAbs);
|
|
132
|
+
return { data: { from: fromAbs, to: toAbs } };
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
throw new RuntimeError(`Cannot move: ${error.message}`);
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
}));
|
|
139
|
+
registry.register(defineTool({
|
|
140
|
+
name: 'computer_delete_file',
|
|
141
|
+
description: 'Delete a file or directory (recursively) on the local filesystem.',
|
|
142
|
+
inputSchema: {
|
|
143
|
+
type: 'object',
|
|
144
|
+
properties: { path: { type: 'string', description: 'Path to delete.' } },
|
|
145
|
+
required: ['path'],
|
|
146
|
+
additionalProperties: false,
|
|
147
|
+
},
|
|
148
|
+
annotations: { destructiveHint: true },
|
|
149
|
+
handler: async (args, _ctx) => {
|
|
150
|
+
const abs = resolvePath(host, args.path);
|
|
151
|
+
const check = host.gate.checkWrite(abs);
|
|
152
|
+
if (!check.allowed)
|
|
153
|
+
throw new RuntimeError(check.reason ?? 'Write denied');
|
|
154
|
+
try {
|
|
155
|
+
await fs.rm(abs, { recursive: true, force: true });
|
|
156
|
+
return { data: { path: abs, deleted: true } };
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
throw new RuntimeError(`Cannot delete: ${error.message}`);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
}));
|
|
163
|
+
registry.register(defineTool({
|
|
164
|
+
name: 'computer_exists',
|
|
165
|
+
description: 'Check whether a path exists on the local filesystem.',
|
|
166
|
+
inputSchema: {
|
|
167
|
+
type: 'object',
|
|
168
|
+
properties: { path: { type: 'string', description: 'Path to check.' } },
|
|
169
|
+
required: ['path'],
|
|
170
|
+
additionalProperties: false,
|
|
171
|
+
},
|
|
172
|
+
annotations: { readOnlyHint: true },
|
|
173
|
+
handler: async (args, _ctx) => {
|
|
174
|
+
const abs = resolvePath(host, args.path);
|
|
175
|
+
const check = host.gate.checkRead(abs);
|
|
176
|
+
if (!check.allowed)
|
|
177
|
+
throw new RuntimeError(check.reason ?? 'Read denied');
|
|
178
|
+
try {
|
|
179
|
+
await fs.access(abs);
|
|
180
|
+
return { data: { path: abs, exists: true } };
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
return { data: { path: abs, exists: false } };
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
}));
|
|
187
|
+
registry.register(defineTool({
|
|
188
|
+
name: 'computer_make_dir',
|
|
189
|
+
description: 'Create a directory (recursively) on the local filesystem.',
|
|
190
|
+
inputSchema: {
|
|
191
|
+
type: 'object',
|
|
192
|
+
properties: { path: { type: 'string', description: 'Directory path to create.' } },
|
|
193
|
+
required: ['path'],
|
|
194
|
+
additionalProperties: false,
|
|
195
|
+
},
|
|
196
|
+
annotations: { destructiveHint: true },
|
|
197
|
+
handler: async (args, _ctx) => {
|
|
198
|
+
const abs = resolvePath(host, args.path);
|
|
199
|
+
const check = host.gate.checkWrite(abs);
|
|
200
|
+
if (!check.allowed)
|
|
201
|
+
throw new RuntimeError(check.reason ?? 'Write denied');
|
|
202
|
+
try {
|
|
203
|
+
await fs.mkdir(abs, { recursive: true });
|
|
204
|
+
return { data: { path: abs, created: true } };
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
throw new RuntimeError(`Cannot create directory: ${error.message}`);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
}));
|
|
211
|
+
registry.register(defineTool({
|
|
212
|
+
name: 'computer_file_info',
|
|
213
|
+
description: 'Get metadata (size, type, modified time) for a file or directory.',
|
|
214
|
+
inputSchema: {
|
|
215
|
+
type: 'object',
|
|
216
|
+
properties: { path: { type: 'string', description: 'Path to inspect.' } },
|
|
217
|
+
required: ['path'],
|
|
218
|
+
additionalProperties: false,
|
|
219
|
+
},
|
|
220
|
+
annotations: { readOnlyHint: true },
|
|
221
|
+
handler: async (args, _ctx) => {
|
|
222
|
+
const abs = resolvePath(host, args.path);
|
|
223
|
+
const check = host.gate.checkRead(abs);
|
|
224
|
+
if (!check.allowed)
|
|
225
|
+
throw new RuntimeError(check.reason ?? 'Read denied');
|
|
226
|
+
try {
|
|
227
|
+
const stat = await fs.stat(abs);
|
|
228
|
+
return {
|
|
229
|
+
data: {
|
|
230
|
+
path: abs,
|
|
231
|
+
type: stat.isDirectory() ? 'dir' : 'file',
|
|
232
|
+
size: stat.size,
|
|
233
|
+
mtime: stat.mtime.toISOString(),
|
|
234
|
+
isDirectory: stat.isDirectory(),
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
throw new RuntimeError(`Cannot get file info: ${error.message}`);
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
}));
|
|
243
|
+
return registry;
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=filesystem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesystem.js","sourceRoot":"","sources":["../src/filesystem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAoB,MAAM,qBAAqB,CAAC;AAWjF,MAAM,sBAAsB,GAAG,GAAG,GAAG,IAAI,CAAC;AAC1C,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B,8DAA8D;AAC9D,SAAS,WAAW,CAAC,IAAoB,EAAE,CAAS;IAClD,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IACjC,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAoB;IACxD,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,IAAI,sBAAsB,CAAC;IAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,gBAAgB,CAAC;IAEjD,MAAM,QAAQ,GAAG,IAAI,YAAY,EAAE,CAAC;IAEpC,QAAQ,CAAC,QAAQ,CACf,UAAU,CAAC;QACT,IAAI,EAAE,oBAAoB;QAC1B,WAAW,EAAE,+FAA+F;QAC5G,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE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EAAE,CAAC,CAAC;YAC9E,CAAC;QACH,CAAC;KACF,CAAC,CACH,CAAC;IAEF,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent 2's **11-level permission gate**. Levels are cumulative: a higher level
|
|
3
|
+
* grants every capability of the levels below it, plus more. The agent's effective
|
|
4
|
+
* level is set once at construction (or via {@link PermissionGate.setLevel}) and every
|
|
5
|
+
* filesystem / terminal / network action is checked against it before execution.
|
|
6
|
+
*
|
|
7
|
+
* The scale goes from fully locked down to fully unrestricted, so an operator can pick
|
|
8
|
+
* the least privilege that still lets a task succeed.
|
|
9
|
+
*/
|
|
10
|
+
export declare const PERMISSION_LEVELS: readonly ["deny", "observe", "read", "workspace", "tmp", "execute-safe", "execute", "execute-any", "network", "admin", "root"];
|
|
11
|
+
export type PermissionLevel = (typeof PERMISSION_LEVELS)[number];
|
|
12
|
+
export interface PermissionCapabilities {
|
|
13
|
+
/** Read files (within roots unless level >= execute-any). */
|
|
14
|
+
read: boolean;
|
|
15
|
+
/** Write / create / delete files (within roots, or anywhere at higher levels). */
|
|
16
|
+
write: boolean;
|
|
17
|
+
/** Run terminal commands. */
|
|
18
|
+
execute: boolean;
|
|
19
|
+
/** Run arbitrary (non-allowlisted) commands. */
|
|
20
|
+
executeAny: boolean;
|
|
21
|
+
/** Access the network. */
|
|
22
|
+
network: boolean;
|
|
23
|
+
/** Escalate privileges (sudo). */
|
|
24
|
+
admin: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** The level index (0-based) used for ordering / comparison. */
|
|
27
|
+
export declare function levelIndex(level: PermissionLevel): number;
|
|
28
|
+
export declare function isAtLeast(level: PermissionLevel, required: PermissionLevel): boolean;
|
|
29
|
+
export declare function capabilitiesFor(level: PermissionLevel): PermissionCapabilities;
|
|
30
|
+
export interface PermissionCheck {
|
|
31
|
+
allowed: boolean;
|
|
32
|
+
reason?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Minimal structural interface for any permission gate (PermissionGate or AuditedGate).
|
|
36
|
+
* Used by filesystem/terminal hosts so they don't depend on the concrete gate class.
|
|
37
|
+
*/
|
|
38
|
+
export interface GateLike {
|
|
39
|
+
readonly level: PermissionLevel;
|
|
40
|
+
readonly capabilities: PermissionCapabilities;
|
|
41
|
+
checkRead(path: string): PermissionCheck;
|
|
42
|
+
checkWrite(path: string): PermissionCheck;
|
|
43
|
+
checkCommand(command: string): PermissionCheck;
|
|
44
|
+
checkNetwork(): PermissionCheck;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The permission gate. Holds the effective level and checks actions against it.
|
|
48
|
+
* Filesystem and terminal operations consult this gate before they do anything.
|
|
49
|
+
*/
|
|
50
|
+
export declare class PermissionGate {
|
|
51
|
+
private _level;
|
|
52
|
+
private readonly _roots;
|
|
53
|
+
private readonly commandAllowlist;
|
|
54
|
+
constructor(level?: PermissionLevel, options?: {
|
|
55
|
+
roots?: string[];
|
|
56
|
+
commandAllowlist?: string[];
|
|
57
|
+
});
|
|
58
|
+
get level(): PermissionLevel;
|
|
59
|
+
get levelIndex(): number;
|
|
60
|
+
get capabilities(): PermissionCapabilities;
|
|
61
|
+
get roots(): string[];
|
|
62
|
+
setLevel(level: PermissionLevel): void;
|
|
63
|
+
/** Whether a path falls inside one of the configured roots. */
|
|
64
|
+
isPathWithinRoots(path: string): boolean;
|
|
65
|
+
/** Checks a filesystem read. */
|
|
66
|
+
checkRead(path: string): PermissionCheck;
|
|
67
|
+
/** Checks a filesystem write (create/modify/delete). */
|
|
68
|
+
checkWrite(path: string): PermissionCheck;
|
|
69
|
+
/** Checks a terminal command. */
|
|
70
|
+
checkCommand(command: string): PermissionCheck;
|
|
71
|
+
/** Checks network access. */
|
|
72
|
+
checkNetwork(): PermissionCheck;
|
|
73
|
+
}
|
|
74
|
+
/** Commands considered read-only / safe to run at the execute-safe level. */
|
|
75
|
+
export declare const SAFE_COMMANDS: string[];
|
|
76
|
+
//# sourceMappingURL=permission.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permission.d.ts","sourceRoot":"","sources":["../src/permission.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,gIAYpB,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjE,MAAM,WAAW,sBAAsB;IACrC,6DAA6D;IAC7D,IAAI,EAAE,OAAO,CAAC;IACd,kFAAkF;IAClF,KAAK,EAAE,OAAO,CAAC;IACf,6BAA6B;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,UAAU,EAAE,OAAO,CAAC;IACpB,0BAA0B;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,kCAAkC;IAClC,KAAK,EAAE,OAAO,CAAC;CAChB;AAgBD,gEAAgE;AAChE,wBAAgB,UAAU,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAIzD;AAED,wBAAgB,SAAS,CAAC,KAA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Ae;IAQzC,iCAAiC;IACjC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe;IAS9C,6BAA6B;IAC7B,YAAY,IAAI,eAAe;CAIhC;AAED,6EAA6E;AAC7E,eAAO,MAAM,aAAa,UAIzB,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { ValidationError } from '@dream_engine/core';
|
|
2
|
+
import { resolve as resolvePathImpl, sep as pathSep, basename as pathBasename } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Agent 2's **11-level permission gate**. Levels are cumulative: a higher level
|
|
5
|
+
* grants every capability of the levels below it, plus more. The agent's effective
|
|
6
|
+
* level is set once at construction (or via {@link PermissionGate.setLevel}) and every
|
|
7
|
+
* filesystem / terminal / network action is checked against it before execution.
|
|
8
|
+
*
|
|
9
|
+
* The scale goes from fully locked down to fully unrestricted, so an operator can pick
|
|
10
|
+
* the least privilege that still lets a task succeed.
|
|
11
|
+
*/
|
|
12
|
+
export const PERMISSION_LEVELS = [
|
|
13
|
+
'deny', // 1 — nothing allowed
|
|
14
|
+
'observe', // 2 — read-only, no writes, no execution
|
|
15
|
+
'read', // 3 — read files within roots
|
|
16
|
+
'workspace', // 4 — read + write within roots
|
|
17
|
+
'tmp', // 5 — write to temp directories
|
|
18
|
+
'execute-safe', // 6 — run read-only / safe commands
|
|
19
|
+
'execute', // 7 — run arbitrary commands within roots
|
|
20
|
+
'execute-any', // 8 — run arbitrary commands anywhere
|
|
21
|
+
'network', // 9 — allow outbound network access
|
|
22
|
+
'admin', // 10 — full system access (sudo-like)
|
|
23
|
+
'root', // 11 — unrestricted (root)
|
|
24
|
+
];
|
|
25
|
+
const CAPABILITIES = {
|
|
26
|
+
deny: { read: false, write: false, execute: false, executeAny: false, network: false, admin: false },
|
|
27
|
+
observe: { read: true, write: false, execute: false, executeAny: false, network: false, admin: false },
|
|
28
|
+
read: { read: true, write: false, execute: false, executeAny: false, network: false, admin: false },
|
|
29
|
+
workspace: { read: true, write: true, execute: false, executeAny: false, network: false, admin: false },
|
|
30
|
+
tmp: { read: true, write: true, execute: false, executeAny: false, network: false, admin: false },
|
|
31
|
+
'execute-safe': { read: true, write: true, execute: true, executeAny: false, network: false, admin: false },
|
|
32
|
+
execute: { read: true, write: true, execute: true, executeAny: false, network: false, admin: false },
|
|
33
|
+
'execute-any': { read: true, write: true, execute: true, executeAny: true, network: false, admin: false },
|
|
34
|
+
network: { read: true, write: true, execute: true, executeAny: true, network: true, admin: false },
|
|
35
|
+
admin: { read: true, write: true, execute: true, executeAny: true, network: true, admin: true },
|
|
36
|
+
root: { read: true, write: true, execute: true, executeAny: true, network: true, admin: true },
|
|
37
|
+
};
|
|
38
|
+
/** The level index (0-based) used for ordering / comparison. */
|
|
39
|
+
export function levelIndex(level) {
|
|
40
|
+
const i = PERMISSION_LEVELS.indexOf(level);
|
|
41
|
+
if (i < 0)
|
|
42
|
+
throw new ValidationError(`Unknown permission level: ${level}`);
|
|
43
|
+
return i;
|
|
44
|
+
}
|
|
45
|
+
export function isAtLeast(level, required) {
|
|
46
|
+
return levelIndex(level) >= levelIndex(required);
|
|
47
|
+
}
|
|
48
|
+
export function capabilitiesFor(level) {
|
|
49
|
+
return { ...CAPABILITIES[level] };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The permission gate. Holds the effective level and checks actions against it.
|
|
53
|
+
* Filesystem and terminal operations consult this gate before they do anything.
|
|
54
|
+
*/
|
|
55
|
+
export class PermissionGate {
|
|
56
|
+
_level;
|
|
57
|
+
_roots;
|
|
58
|
+
commandAllowlist;
|
|
59
|
+
constructor(level = 'observe', options = {}) {
|
|
60
|
+
this._level = level;
|
|
61
|
+
this._roots = options.roots ?? [];
|
|
62
|
+
this.commandAllowlist = options.commandAllowlist ?? SAFE_COMMANDS;
|
|
63
|
+
}
|
|
64
|
+
get level() {
|
|
65
|
+
return this._level;
|
|
66
|
+
}
|
|
67
|
+
get levelIndex() {
|
|
68
|
+
return levelIndex(this._level);
|
|
69
|
+
}
|
|
70
|
+
get capabilities() {
|
|
71
|
+
return capabilitiesFor(this._level);
|
|
72
|
+
}
|
|
73
|
+
get roots() {
|
|
74
|
+
return [...this._roots];
|
|
75
|
+
}
|
|
76
|
+
setLevel(level) {
|
|
77
|
+
if (!PERMISSION_LEVELS.includes(level))
|
|
78
|
+
throw new ValidationError(`Unknown permission level: ${level}`);
|
|
79
|
+
this._level = level;
|
|
80
|
+
}
|
|
81
|
+
/** Whether a path falls inside one of the configured roots. */
|
|
82
|
+
isPathWithinRoots(path) {
|
|
83
|
+
if (!this._roots.length)
|
|
84
|
+
return false;
|
|
85
|
+
const resolved = resolvePath(path);
|
|
86
|
+
return this._roots.some((root) => {
|
|
87
|
+
const r = resolvePath(root);
|
|
88
|
+
return resolved === r || resolved.startsWith(r.endsWith(sep()) ? r : r + sep());
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/** Checks a filesystem read. */
|
|
92
|
+
checkRead(path) {
|
|
93
|
+
if (!this.capabilities.read)
|
|
94
|
+
return { allowed: false, reason: `Read is not permitted at level "${this._level}"` };
|
|
95
|
+
return { allowed: true };
|
|
96
|
+
}
|
|
97
|
+
/** Checks a filesystem write (create/modify/delete). */
|
|
98
|
+
checkWrite(path) {
|
|
99
|
+
if (!this.capabilities.write)
|
|
100
|
+
return { allowed: false, reason: `Write is not permitted at level "${this._level}"` };
|
|
101
|
+
// Writes must stay within roots unless the level grants unrestricted execution.
|
|
102
|
+
if (this.capabilities.executeAny)
|
|
103
|
+
return { allowed: true };
|
|
104
|
+
if (this.isPathWithinRoots(path))
|
|
105
|
+
return { allowed: true };
|
|
106
|
+
return { allowed: false, reason: `Write to "${path}" is outside the configured roots at level "${this._level}"` };
|
|
107
|
+
}
|
|
108
|
+
/** Checks a terminal command. */
|
|
109
|
+
checkCommand(command) {
|
|
110
|
+
if (!this.capabilities.execute)
|
|
111
|
+
return { allowed: false, reason: `Execution is not permitted at level "${this._level}"` };
|
|
112
|
+
if (this.capabilities.executeAny)
|
|
113
|
+
return { allowed: true };
|
|
114
|
+
// Within roots but not execute-any: only allowlisted (safe) commands.
|
|
115
|
+
const base = basenameOf(command.trim().split(/\s+/)[0] ?? '');
|
|
116
|
+
if (this.commandAllowlist.includes(base))
|
|
117
|
+
return { allowed: true };
|
|
118
|
+
return { allowed: false, reason: `Command "${base}" is not in the allowlist at level "${this._level}"` };
|
|
119
|
+
}
|
|
120
|
+
/** Checks network access. */
|
|
121
|
+
checkNetwork() {
|
|
122
|
+
if (!this.capabilities.network)
|
|
123
|
+
return { allowed: false, reason: `Network access is not permitted at level "${this._level}"` };
|
|
124
|
+
return { allowed: true };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Commands considered read-only / safe to run at the execute-safe level. */
|
|
128
|
+
export const SAFE_COMMANDS = [
|
|
129
|
+
'ls', 'cat', 'pwd', 'echo', 'head', 'tail', 'wc', 'grep', 'find', 'which', 'whoami',
|
|
130
|
+
'date', 'uname', 'stat', 'file', 'diff', 'sort', 'uniq', 'cut', 'tr', 'awk', 'sed',
|
|
131
|
+
'tree', 'du', 'df', 'free', 'env', 'printenv', 'id', 'hostname', 'uptime',
|
|
132
|
+
];
|
|
133
|
+
function resolvePath(p) {
|
|
134
|
+
return resolvePathImpl(p);
|
|
135
|
+
}
|
|
136
|
+
function sep() {
|
|
137
|
+
return pathSep;
|
|
138
|
+
}
|
|
139
|
+
function basenameOf(p) {
|
|
140
|
+
return pathBasename(p);
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=permission.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permission.js","sourceRoot":"","sources":["../src/permission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,GAAG,IAAI,OAAO,EAAE,QAAQ,IAAI,YAAY,EAAE,MAAM,WAAW,CAAC;AAEjG;;;;;;;;GAQG;AAC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