@causyne/agent 1.20.120 → 1.20.121
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/package.json +1 -1
- package/src/agent/errorClassifier.js +113 -0
- package/src/agent/errorClassifier.ts +161 -0
- package/src/agent/promptIntent.js +200 -0
- package/src/agent/promptIntent.ts +203 -0
- package/src/agent/toolArgValidator.js +97 -0
- package/src/agent/toolArgValidator.ts +112 -0
- package/src/agentLoop.js +22 -339
- package/src/agentLoop.ts +40 -347
- package/src/autoUpdate.js +111 -8
- package/src/autoUpdate.ts +144 -8
- package/src/cliMode.js +16 -2
- package/src/commands.js +10 -9
- package/src/config.js +42 -63
- package/src/configVault.js +204 -0
- package/src/configVault.ts +213 -0
- package/src/core/contextCompactor.js +70 -11
- package/src/core/contextCompactor.ts +83 -11
- package/src/dynamicCatalogue.js +42 -73
- package/src/dynamicCatalogue.ts +74 -60
- package/src/entrypoint.js +77 -4
- package/src/headlessCli.js +304 -0
- package/src/headlessCli.ts +380 -0
- package/src/mcp/mcpClient.js +38 -11
- package/src/mcp/mcpClient.ts +44 -11
- package/src/mcp/mcpRegistry.js +2 -5
- package/src/mcp/mcpRegistry.ts +2 -5
- package/src/mcp/mcpSecurity.js +189 -0
- package/src/mcp/mcpSecurity.ts +190 -0
- package/src/osKeychain.js +209 -0
- package/src/osKeychain.ts +245 -0
- package/src/providers/anthropic.js +28 -11
- package/src/providers/byokHttp.js +354 -0
- package/src/providers/byokHttp.ts +454 -0
- package/src/providers/byokStream.js +657 -0
- package/src/providers/byokStream.ts +759 -0
- package/src/providers/dashscope-byok.js +34 -25
- package/src/providers/deepseek.js +31 -22
- package/src/providers/gemini.js +40 -13
- package/src/providers/moonshot.js +31 -22
- package/src/providers/muse.js +10 -10
- package/src/providers/ollama.js +25 -10
- package/src/providers/openai-compatible.js +46 -14
- package/src/providers/openrouter.js +63 -29
- package/src/providers/openrouter.ts +73 -29
- package/src/providers/qwen-byok.js +33 -16
- package/src/providers/xai.js +31 -22
- package/src/sandbox.js +77 -56
- package/src/sandboxPolicy.js +1208 -0
- package/src/sandboxPolicy.ts +1300 -0
- package/src/sessionJournal.js +47 -0
- package/src/sessionJournal.ts +49 -0
- package/src/tools/osSandbox.js +473 -0
- package/src/tools/osSandbox.ts +554 -0
- package/src/tools/persistentShell.js +8 -3
- package/src/tools/subprocessEnv.js +197 -0
- package/src/tools/subprocessEnv.ts +212 -0
- package/src/tools/sysRun.js +39 -6
- package/src/tools/sysRun.ts +53 -7
package/src/sessionJournal.js
CHANGED
|
@@ -6,6 +6,7 @@ export const SESSION_DIR = path.join(causyneHome(), 'sessions');
|
|
|
6
6
|
const LEGACY_HISTORY = path.join(causyneHome(), 'memory', 'chat_history.json');
|
|
7
7
|
const POINTER_FILE = path.join(SESSION_DIR, 'active.json');
|
|
8
8
|
export const RESUME_WINDOW_MS = 12 * 60 * 60 * 1000;
|
|
9
|
+
export const ORPHAN_EMPTY_SESSION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
9
10
|
export const MAX_SESSIONS = 50;
|
|
10
11
|
const MAX_TRANSCRIPT_BYTES = 64 * 1024 * 1024;
|
|
11
12
|
const MAX_EVENT_BYTES = 8 * 1024 * 1024;
|
|
@@ -261,6 +262,47 @@ export function listSessions() {
|
|
|
261
262
|
return [];
|
|
262
263
|
}
|
|
263
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Removes abandoned 0-message session files older than `maxAgeMs` (default 24h).
|
|
267
|
+
* Never removes:
|
|
268
|
+
* - any session with 1 or more messages (real conversation history)
|
|
269
|
+
* - the currently active session or `preserveId`
|
|
270
|
+
* - recent empty sessions younger than `maxAgeMs`
|
|
271
|
+
*/
|
|
272
|
+
export function pruneEmptyOrphanSessions(options = {}) {
|
|
273
|
+
try {
|
|
274
|
+
ensureDir();
|
|
275
|
+
const maxAgeMs = typeof options.maxAgeMs === 'number' && Number.isFinite(options.maxAgeMs) && options.maxAgeMs >= 0
|
|
276
|
+
? options.maxAgeMs
|
|
277
|
+
: ORPHAN_EMPTY_SESSION_TTL_MS;
|
|
278
|
+
const now = typeof options.now === 'number' && Number.isFinite(options.now) ? options.now : Date.now();
|
|
279
|
+
const activeId = getActiveSessionId();
|
|
280
|
+
const preserveId = options.preserveId ? safeSessionId(options.preserveId) : null;
|
|
281
|
+
let removed = 0;
|
|
282
|
+
for (const row of listSessions()) {
|
|
283
|
+
if (row.messageCount > 0)
|
|
284
|
+
continue;
|
|
285
|
+
if (row.id === activeId || (preserveId && row.id === preserveId))
|
|
286
|
+
continue;
|
|
287
|
+
if (now - row.updatedAt < maxAgeMs)
|
|
288
|
+
continue;
|
|
289
|
+
let removedSession = false;
|
|
290
|
+
for (const file of [sessionTranscriptFile(row.id), legacySessionFile(row.id), `${legacySessionFile(row.id)}.migrated`]) {
|
|
291
|
+
try {
|
|
292
|
+
fs.unlinkSync(file);
|
|
293
|
+
removedSession = true;
|
|
294
|
+
}
|
|
295
|
+
catch { /* absent files need no action */ }
|
|
296
|
+
}
|
|
297
|
+
if (removedSession)
|
|
298
|
+
removed += 1;
|
|
299
|
+
}
|
|
300
|
+
return removed;
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return 0;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
264
306
|
export function pruneSessions(max = MAX_SESSIONS) {
|
|
265
307
|
let removed = 0;
|
|
266
308
|
for (const row of listSessions().slice(max)) {
|
|
@@ -334,6 +376,11 @@ export function resolveSession(workspace, options = {}) {
|
|
|
334
376
|
const workspaceText = String(workspace ?? '');
|
|
335
377
|
ensureDir();
|
|
336
378
|
migrateLegacyHistory(workspaceText);
|
|
379
|
+
const preserveCandidate = options.preferredId || getActiveSessionId();
|
|
380
|
+
try {
|
|
381
|
+
pruneEmptyOrphanSessions({ preserveId: preserveCandidate });
|
|
382
|
+
}
|
|
383
|
+
catch { /* housekeeping must never block session resolution */ }
|
|
337
384
|
if (options.forceNew) {
|
|
338
385
|
return createSession(workspaceText);
|
|
339
386
|
}
|
package/src/sessionJournal.ts
CHANGED
|
@@ -56,6 +56,7 @@ export const SESSION_DIR = path.join(causyneHome(), 'sessions');
|
|
|
56
56
|
const LEGACY_HISTORY = path.join(causyneHome(), 'memory', 'chat_history.json');
|
|
57
57
|
const POINTER_FILE = path.join(SESSION_DIR, 'active.json');
|
|
58
58
|
export const RESUME_WINDOW_MS = 12 * 60 * 60 * 1000;
|
|
59
|
+
export const ORPHAN_EMPTY_SESSION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
59
60
|
export const MAX_SESSIONS = 50;
|
|
60
61
|
const MAX_TRANSCRIPT_BYTES = 64 * 1024 * 1024;
|
|
61
62
|
const MAX_EVENT_BYTES = 8 * 1024 * 1024;
|
|
@@ -294,6 +295,50 @@ export function listSessions(): Array<Omit<SessionRecord, 'messages'> & { messag
|
|
|
294
295
|
}
|
|
295
296
|
}
|
|
296
297
|
|
|
298
|
+
export interface PruneEmptyOrphanOptions {
|
|
299
|
+
maxAgeMs?: number | undefined;
|
|
300
|
+
preserveId?: string | null | undefined;
|
|
301
|
+
now?: number | undefined;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Removes abandoned 0-message session files older than `maxAgeMs` (default 24h).
|
|
306
|
+
* Never removes:
|
|
307
|
+
* - any session with 1 or more messages (real conversation history)
|
|
308
|
+
* - the currently active session or `preserveId`
|
|
309
|
+
* - recent empty sessions younger than `maxAgeMs`
|
|
310
|
+
*/
|
|
311
|
+
export function pruneEmptyOrphanSessions(options: PruneEmptyOrphanOptions = {}): number {
|
|
312
|
+
try {
|
|
313
|
+
ensureDir();
|
|
314
|
+
const maxAgeMs = typeof options.maxAgeMs === 'number' && Number.isFinite(options.maxAgeMs) && options.maxAgeMs >= 0
|
|
315
|
+
? options.maxAgeMs
|
|
316
|
+
: ORPHAN_EMPTY_SESSION_TTL_MS;
|
|
317
|
+
const now = typeof options.now === 'number' && Number.isFinite(options.now) ? options.now : Date.now();
|
|
318
|
+
const activeId = getActiveSessionId();
|
|
319
|
+
const preserveId = options.preserveId ? safeSessionId(options.preserveId) : null;
|
|
320
|
+
|
|
321
|
+
let removed = 0;
|
|
322
|
+
for (const row of listSessions()) {
|
|
323
|
+
if (row.messageCount > 0) continue;
|
|
324
|
+
if (row.id === activeId || (preserveId && row.id === preserveId)) continue;
|
|
325
|
+
if (now - row.updatedAt < maxAgeMs) continue;
|
|
326
|
+
|
|
327
|
+
let removedSession = false;
|
|
328
|
+
for (const file of [sessionTranscriptFile(row.id), legacySessionFile(row.id), `${legacySessionFile(row.id)}.migrated`]) {
|
|
329
|
+
try {
|
|
330
|
+
fs.unlinkSync(file);
|
|
331
|
+
removedSession = true;
|
|
332
|
+
} catch { /* absent files need no action */ }
|
|
333
|
+
}
|
|
334
|
+
if (removedSession) removed += 1;
|
|
335
|
+
}
|
|
336
|
+
return removed;
|
|
337
|
+
} catch {
|
|
338
|
+
return 0;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
297
342
|
export function pruneSessions(max = MAX_SESSIONS): number {
|
|
298
343
|
let removed = 0;
|
|
299
344
|
for (const row of listSessions().slice(max)) {
|
|
@@ -371,6 +416,10 @@ export function resolveSession(workspace: unknown, options: ResolveSessionOption
|
|
|
371
416
|
const workspaceText = String(workspace ?? '');
|
|
372
417
|
ensureDir();
|
|
373
418
|
migrateLegacyHistory(workspaceText);
|
|
419
|
+
const preserveCandidate = options.preferredId || getActiveSessionId();
|
|
420
|
+
try {
|
|
421
|
+
pruneEmptyOrphanSessions({ preserveId: preserveCandidate });
|
|
422
|
+
} catch { /* housekeeping must never block session resolution */ }
|
|
374
423
|
if (options.forceNew) {
|
|
375
424
|
return createSession(workspaceText);
|
|
376
425
|
}
|
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — OS-Level Process Sandboxing Wrapper (Sector 2B)
|
|
3
|
+
*
|
|
4
|
+
* Provides kernel-enforced filesystem & network isolation around shell tool
|
|
5
|
+
* subprocesses when enabled via `CAUSYNE_OS_SANDBOX` or `config.osSandbox`:
|
|
6
|
+
* - macOS (`darwin`): Apple Seatbelt (`/usr/bin/sandbox-exec -p <SBPL>`)
|
|
7
|
+
* - Linux (`linux`): Bubblewrap (`bwrap`) mount/network namespaces
|
|
8
|
+
* - Windows / nested containers: honest capability reporting (`backend: 'none'`)
|
|
9
|
+
* with fail-closed enforcement when `strict` mode is explicitly requested.
|
|
10
|
+
*
|
|
11
|
+
* Complements application-level `Sandbox` classification and `sanitizeSubprocessEnv`
|
|
12
|
+
* without ever stripping or degrading peer ACP capabilities.
|
|
13
|
+
*/
|
|
14
|
+
import { spawnSync } from 'node:child_process';
|
|
15
|
+
import fs from 'node:fs';
|
|
16
|
+
import os from 'node:os';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
const SEATBELT_BIN = '/usr/bin/sandbox-exec';
|
|
19
|
+
const CREDENTIAL_SUBPATHS = [
|
|
20
|
+
'.ssh',
|
|
21
|
+
'.aws',
|
|
22
|
+
'.gnupg',
|
|
23
|
+
'.kube',
|
|
24
|
+
'.causyne',
|
|
25
|
+
'.netrc',
|
|
26
|
+
'.git-credentials',
|
|
27
|
+
'.pypirc',
|
|
28
|
+
'Library/Keychains'
|
|
29
|
+
];
|
|
30
|
+
const SHELL_PERSISTENCE_FILES = [
|
|
31
|
+
'.bashrc',
|
|
32
|
+
'.bash_profile',
|
|
33
|
+
'.bash_login',
|
|
34
|
+
'.profile',
|
|
35
|
+
'.zshrc',
|
|
36
|
+
'.zprofile',
|
|
37
|
+
'.zshenv',
|
|
38
|
+
'.zlogin',
|
|
39
|
+
'.config/fish/config.fish'
|
|
40
|
+
];
|
|
41
|
+
const DEV_CACHE_SUBPATHS = [
|
|
42
|
+
'.npm',
|
|
43
|
+
'.cache',
|
|
44
|
+
'.cargo',
|
|
45
|
+
'.rustup',
|
|
46
|
+
'.gradle',
|
|
47
|
+
'.m2',
|
|
48
|
+
'go/pkg',
|
|
49
|
+
'Library/Caches'
|
|
50
|
+
];
|
|
51
|
+
let cachedProbe = null;
|
|
52
|
+
/**
|
|
53
|
+
* Escapes a filesystem path for safe embedding in an Apple Seatbelt SBPL string.
|
|
54
|
+
* Rejects control characters and NUL bytes to prevent S-expression injection.
|
|
55
|
+
*/
|
|
56
|
+
export function escapeSeatbeltPath(rawPath) {
|
|
57
|
+
if (typeof rawPath !== 'string' || rawPath.length === 0)
|
|
58
|
+
return null;
|
|
59
|
+
for (let i = 0; i < rawPath.length; i++) {
|
|
60
|
+
const code = rawPath.charCodeAt(i);
|
|
61
|
+
if (code < 0x20 || code === 0x7f)
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
return rawPath.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
|
65
|
+
}
|
|
66
|
+
function resolveCanonicalPaths(rawPath) {
|
|
67
|
+
const resolved = path.resolve(rawPath);
|
|
68
|
+
const out = new Set([resolved]);
|
|
69
|
+
try {
|
|
70
|
+
const real = fs.realpathSync.native(resolved);
|
|
71
|
+
out.add(real);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// Walk up to nearest existing ancestor so symlinked prefixes (e.g. /var -> /private/var) resolve
|
|
75
|
+
let current = resolved;
|
|
76
|
+
const tail = [];
|
|
77
|
+
for (let depth = 0; depth < 64; depth++) {
|
|
78
|
+
const parent = path.dirname(current);
|
|
79
|
+
if (parent === current)
|
|
80
|
+
break;
|
|
81
|
+
tail.unshift(path.basename(current));
|
|
82
|
+
current = parent;
|
|
83
|
+
try {
|
|
84
|
+
const realAncestor = fs.realpathSync.native(current);
|
|
85
|
+
out.add(path.resolve(realAncestor, ...tail));
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return [...out];
|
|
94
|
+
}
|
|
95
|
+
function isSameOrChildPath(candidate, parent) {
|
|
96
|
+
const rel = path.relative(parent, candidate);
|
|
97
|
+
return rel === '' || (rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel));
|
|
98
|
+
}
|
|
99
|
+
function workspaceOverlapsPath(workspacePaths, targetPath) {
|
|
100
|
+
const targetVariants = resolveCanonicalPaths(targetPath);
|
|
101
|
+
for (const ws of workspacePaths) {
|
|
102
|
+
const wsVariants = resolveCanonicalPaths(ws);
|
|
103
|
+
for (const wv of wsVariants) {
|
|
104
|
+
for (const tv of targetVariants) {
|
|
105
|
+
if (isSameOrChildPath(wv, tv))
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Parses `CAUSYNE_OS_SANDBOX` / `config.osSandbox` into an explicit OS sandbox mode.
|
|
114
|
+
* Defaults to `'off'` unless explicitly enabled so standard workflows never break unexpectedly.
|
|
115
|
+
*/
|
|
116
|
+
export function resolveOsSandboxMode(env = process.env, configMode) {
|
|
117
|
+
const raw = (typeof env.CAUSYNE_OS_SANDBOX === 'string' && env.CAUSYNE_OS_SANDBOX.trim())
|
|
118
|
+
? env.CAUSYNE_OS_SANDBOX.trim()
|
|
119
|
+
: (typeof configMode === 'string' ? configMode.trim() : '');
|
|
120
|
+
const lower = raw.toLowerCase();
|
|
121
|
+
const networkEnv = typeof env.CAUSYNE_OS_SANDBOX_NETWORK === 'string'
|
|
122
|
+
? env.CAUSYNE_OS_SANDBOX_NETWORK.trim().toLowerCase()
|
|
123
|
+
: '';
|
|
124
|
+
const allowNetwork = !(networkEnv === '0' || networkEnv === 'false' || networkEnv === 'off' || networkEnv === 'deny');
|
|
125
|
+
if (!lower || lower === 'off' || lower === '0' || lower === 'false' || lower === 'disabled') {
|
|
126
|
+
return { mode: 'off', strict: false, allowNetwork };
|
|
127
|
+
}
|
|
128
|
+
if (lower === 'read-only' || lower === 'readonly' || lower === 'ro') {
|
|
129
|
+
return { mode: 'read-only', strict: false, allowNetwork };
|
|
130
|
+
}
|
|
131
|
+
if (lower === 'read-only-strict') {
|
|
132
|
+
return { mode: 'read-only', strict: true, allowNetwork };
|
|
133
|
+
}
|
|
134
|
+
if (lower === 'strict') {
|
|
135
|
+
return { mode: 'workspace-write', strict: true, allowNetwork };
|
|
136
|
+
}
|
|
137
|
+
if (lower === 'workspace-write' || lower === 'on' || lower === '1' || lower === 'true' || lower === 'auto') {
|
|
138
|
+
return { mode: 'workspace-write', strict: false, allowNetwork };
|
|
139
|
+
}
|
|
140
|
+
return { mode: 'off', strict: false, allowNetwork };
|
|
141
|
+
}
|
|
142
|
+
function findExecutableInPath(binaryName, env = process.env) {
|
|
143
|
+
const pathVal = env.PATH || '';
|
|
144
|
+
for (const dir of pathVal.split(path.delimiter)) {
|
|
145
|
+
if (!dir)
|
|
146
|
+
continue;
|
|
147
|
+
const candidate = path.join(dir, binaryName);
|
|
148
|
+
try {
|
|
149
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
150
|
+
if (fs.statSync(candidate).isFile())
|
|
151
|
+
return candidate;
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Probes whether an OS-level kernel sandbox backend is installed and functional
|
|
161
|
+
* (not blocked by a nested container or restricted user namespace).
|
|
162
|
+
*/
|
|
163
|
+
export function probeOsSandbox(options) {
|
|
164
|
+
const platform = options?.platform ?? process.platform;
|
|
165
|
+
if (!options?.bypassCache && cachedProbe && cachedProbe.platform === platform) {
|
|
166
|
+
return cachedProbe.result;
|
|
167
|
+
}
|
|
168
|
+
let result;
|
|
169
|
+
if (platform === 'darwin') {
|
|
170
|
+
if (!fs.existsSync(SEATBELT_BIN)) {
|
|
171
|
+
result = {
|
|
172
|
+
backend: 'none',
|
|
173
|
+
platform,
|
|
174
|
+
available: false,
|
|
175
|
+
binaryPath: null,
|
|
176
|
+
reason: 'macOS sandbox-exec binary not found.'
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const check = spawnSync(SEATBELT_BIN, ['-p', '(version 1)(allow default)', '/usr/bin/true'], {
|
|
181
|
+
stdio: 'ignore',
|
|
182
|
+
timeout: 3000
|
|
183
|
+
});
|
|
184
|
+
if (check.status === 0) {
|
|
185
|
+
result = {
|
|
186
|
+
backend: 'seatbelt',
|
|
187
|
+
platform,
|
|
188
|
+
available: true,
|
|
189
|
+
binaryPath: SEATBELT_BIN,
|
|
190
|
+
reason: 'macOS Seatbelt (sandbox-exec) active and verified.'
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
result = {
|
|
195
|
+
backend: 'none',
|
|
196
|
+
platform,
|
|
197
|
+
available: false,
|
|
198
|
+
binaryPath: SEATBELT_BIN,
|
|
199
|
+
reason: 'macOS Seatbelt cannot apply in this environment (likely nested sandbox).'
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (platform === 'linux') {
|
|
205
|
+
const bwrapPath = findExecutableInPath('bwrap');
|
|
206
|
+
if (!bwrapPath) {
|
|
207
|
+
result = {
|
|
208
|
+
backend: 'none',
|
|
209
|
+
platform,
|
|
210
|
+
available: false,
|
|
211
|
+
binaryPath: null,
|
|
212
|
+
reason: 'Linux bubblewrap (bwrap) is not installed.'
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
const check = spawnSync(bwrapPath, ['--ro-bind', '/', '/', '--dev', '/dev', '--proc', '/proc', '/bin/true'], {
|
|
217
|
+
stdio: 'ignore',
|
|
218
|
+
timeout: 3000
|
|
219
|
+
});
|
|
220
|
+
if (check.status === 0) {
|
|
221
|
+
result = {
|
|
222
|
+
backend: 'bubblewrap',
|
|
223
|
+
platform,
|
|
224
|
+
available: true,
|
|
225
|
+
binaryPath: bwrapPath,
|
|
226
|
+
reason: 'Linux bubblewrap (bwrap) namespaces active and verified.'
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
result = {
|
|
231
|
+
backend: 'none',
|
|
232
|
+
platform,
|
|
233
|
+
available: false,
|
|
234
|
+
binaryPath: bwrapPath,
|
|
235
|
+
reason: 'Linux bubblewrap (bwrap) unavailable (user namespaces disabled in container).'
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
result = {
|
|
242
|
+
backend: 'none',
|
|
243
|
+
platform,
|
|
244
|
+
available: false,
|
|
245
|
+
binaryPath: null,
|
|
246
|
+
reason: `Kernel subprocess sandboxing is not implemented for platform "${platform}"; application sandbox and env isolation remain active.`
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
if (!options?.bypassCache) {
|
|
250
|
+
cachedProbe = { platform, result };
|
|
251
|
+
}
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Generates an Apple Seatbelt SBPL profile for macOS `sandbox-exec`.
|
|
256
|
+
* Uses Seatbelt's last-match-wins semantics:
|
|
257
|
+
* 1. Allow default reads/exec of system libraries & toolchains.
|
|
258
|
+
* 2. Optionally deny remote outbound IP connections (`allowNetwork === false`).
|
|
259
|
+
* 3. Deny all `file-write*`, then allow only safe device nodes, workspace roots,
|
|
260
|
+
* temp dirs, and standard dev caches (in `workspace-write` mode).
|
|
261
|
+
* 4. Explicitly deny `file-write*` on shell startup profiles and credential vaults.
|
|
262
|
+
* 5. Explicitly deny `file-read*` on credential vaults (unless the user explicitly
|
|
263
|
+
* selected that directory as their workspace).
|
|
264
|
+
*/
|
|
265
|
+
export function buildSeatbeltProfile(options) {
|
|
266
|
+
const mode = options.mode ?? 'workspace-write';
|
|
267
|
+
const homeDir = options.homeDir ?? os.homedir();
|
|
268
|
+
const tmpDir = options.tmpDir ?? os.tmpdir();
|
|
269
|
+
const workspaces = options.workspaceRoots && options.workspaceRoots.length > 0
|
|
270
|
+
? [...options.workspaceRoots, options.cwd]
|
|
271
|
+
: [options.cwd];
|
|
272
|
+
const lines = [
|
|
273
|
+
'(version 1)',
|
|
274
|
+
'(allow default)'
|
|
275
|
+
];
|
|
276
|
+
if (options.allowNetwork === false) {
|
|
277
|
+
lines.push('(deny network-outbound (remote ip))');
|
|
278
|
+
}
|
|
279
|
+
lines.push('(deny file-write*)');
|
|
280
|
+
// Always allow writing to standard harmless device sinks
|
|
281
|
+
const safeDeviceLiterals = [
|
|
282
|
+
'/dev/null',
|
|
283
|
+
'/dev/zero',
|
|
284
|
+
'/dev/tty',
|
|
285
|
+
'/dev/ptmx',
|
|
286
|
+
'/dev/dtracehelper',
|
|
287
|
+
'/dev/stdout',
|
|
288
|
+
'/dev/stderr'
|
|
289
|
+
];
|
|
290
|
+
lines.push(`(allow file-write* ${safeDeviceLiterals.map(p => `(literal "${p}")`).join(' ')})`);
|
|
291
|
+
if (mode === 'workspace-write') {
|
|
292
|
+
const writableSubpaths = new Set();
|
|
293
|
+
const addWritable = (rawPath) => {
|
|
294
|
+
for (const variant of resolveCanonicalPaths(rawPath)) {
|
|
295
|
+
const escaped = escapeSeatbeltPath(variant);
|
|
296
|
+
if (escaped)
|
|
297
|
+
writableSubpaths.add(escaped);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
for (const ws of workspaces) {
|
|
301
|
+
addWritable(ws);
|
|
302
|
+
}
|
|
303
|
+
if (options.includeTempDirs !== false) {
|
|
304
|
+
for (const t of ['/tmp', '/private/tmp', '/private/var/tmp', '/private/var/folders', tmpDir]) {
|
|
305
|
+
if (t)
|
|
306
|
+
addWritable(t);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (options.allowDevCaches !== false && homeDir) {
|
|
310
|
+
for (const rel of DEV_CACHE_SUBPATHS) {
|
|
311
|
+
addWritable(path.join(homeDir, rel));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (writableSubpaths.size > 0) {
|
|
315
|
+
const clauses = [...writableSubpaths].map(p => `(subpath "${p}")`).join(' ');
|
|
316
|
+
lines.push(`(allow file-write* ${clauses})`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// Last-match-wins: shell startup persistence targets remain strictly unwritable
|
|
320
|
+
if (homeDir) {
|
|
321
|
+
const denyWriteLiterals = new Set();
|
|
322
|
+
for (const rel of SHELL_PERSISTENCE_FILES) {
|
|
323
|
+
const full = path.join(homeDir, rel);
|
|
324
|
+
for (const variant of resolveCanonicalPaths(full)) {
|
|
325
|
+
const escaped = escapeSeatbeltPath(variant);
|
|
326
|
+
if (escaped)
|
|
327
|
+
denyWriteLiterals.add(escaped);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (denyWriteLiterals.size > 0) {
|
|
331
|
+
lines.push(`(deny file-write* ${[...denyWriteLiterals].map(p => `(literal "${p}")`).join(' ')})`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// Last-match-wins: credential vaults denied for both read and write unless explicitly chosen as workspace
|
|
335
|
+
const denyCredSubpaths = new Set();
|
|
336
|
+
if (homeDir) {
|
|
337
|
+
for (const rel of CREDENTIAL_SUBPATHS) {
|
|
338
|
+
const full = path.join(homeDir, rel);
|
|
339
|
+
if (workspaceOverlapsPath(workspaces, full))
|
|
340
|
+
continue;
|
|
341
|
+
for (const variant of resolveCanonicalPaths(full)) {
|
|
342
|
+
const escaped = escapeSeatbeltPath(variant);
|
|
343
|
+
if (escaped)
|
|
344
|
+
denyCredSubpaths.add(escaped);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (Array.isArray(options.extraDenyReadPaths)) {
|
|
349
|
+
for (const extra of options.extraDenyReadPaths) {
|
|
350
|
+
for (const variant of resolveCanonicalPaths(extra)) {
|
|
351
|
+
const escaped = escapeSeatbeltPath(variant);
|
|
352
|
+
if (escaped)
|
|
353
|
+
denyCredSubpaths.add(escaped);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (denyCredSubpaths.size > 0) {
|
|
358
|
+
const clauses = [...denyCredSubpaths].map(p => `(subpath "${p}")`).join(' ');
|
|
359
|
+
lines.push(`(deny file-read* ${clauses})`);
|
|
360
|
+
lines.push(`(deny file-write* ${clauses})`);
|
|
361
|
+
}
|
|
362
|
+
return `${lines.join('\n')}\n`;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Generates shell-free argv for Linux `bwrap` (bubblewrap) isolation.
|
|
366
|
+
*/
|
|
367
|
+
export function buildBubblewrapArgs(shell, shellArgs, options) {
|
|
368
|
+
const mode = options.mode ?? 'workspace-write';
|
|
369
|
+
const homeDir = options.homeDir ?? os.homedir();
|
|
370
|
+
const workspaces = options.workspaceRoots && options.workspaceRoots.length > 0
|
|
371
|
+
? [...options.workspaceRoots, options.cwd]
|
|
372
|
+
: [options.cwd];
|
|
373
|
+
const args = [
|
|
374
|
+
'--die-with-parent',
|
|
375
|
+
'--ro-bind', '/', '/',
|
|
376
|
+
'--dev', '/dev',
|
|
377
|
+
'--proc', '/proc'
|
|
378
|
+
];
|
|
379
|
+
if (options.includeTempDirs !== false) {
|
|
380
|
+
args.push('--tmpfs', '/tmp');
|
|
381
|
+
}
|
|
382
|
+
if (options.allowNetwork === false) {
|
|
383
|
+
args.push('--unshare-net');
|
|
384
|
+
}
|
|
385
|
+
if (mode === 'workspace-write') {
|
|
386
|
+
const bound = new Set();
|
|
387
|
+
const bindIfExists = (rawPath) => {
|
|
388
|
+
const resolved = path.resolve(rawPath);
|
|
389
|
+
if (bound.has(resolved))
|
|
390
|
+
return;
|
|
391
|
+
if (!fs.existsSync(resolved))
|
|
392
|
+
return;
|
|
393
|
+
bound.add(resolved);
|
|
394
|
+
args.push('--bind', resolved, resolved);
|
|
395
|
+
};
|
|
396
|
+
for (const ws of workspaces) {
|
|
397
|
+
bindIfExists(ws);
|
|
398
|
+
}
|
|
399
|
+
if (options.allowDevCaches !== false && homeDir) {
|
|
400
|
+
for (const rel of DEV_CACHE_SUBPATHS) {
|
|
401
|
+
bindIfExists(path.join(homeDir, rel));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
// Mask existing credential directories with empty tmpfs mounts unless inside selected workspace
|
|
406
|
+
if (homeDir) {
|
|
407
|
+
for (const rel of ['.ssh', '.aws', '.gnupg', '.kube', '.causyne']) {
|
|
408
|
+
const full = path.join(homeDir, rel);
|
|
409
|
+
if (workspaceOverlapsPath(workspaces, full))
|
|
410
|
+
continue;
|
|
411
|
+
try {
|
|
412
|
+
if (fs.existsSync(full) && fs.statSync(full).isDirectory()) {
|
|
413
|
+
args.push('--tmpfs', full);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
args.push('--chdir', path.resolve(options.cwd), '--', shell, ...shellArgs);
|
|
422
|
+
return args;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Wraps a `(shell, shellArgs)` invocation in an OS kernel sandbox when enabled.
|
|
426
|
+
*/
|
|
427
|
+
export function wrapCommandWithOsSandbox(shell, shellArgs, options) {
|
|
428
|
+
const mode = options.mode ?? 'off';
|
|
429
|
+
if (mode === 'off') {
|
|
430
|
+
return {
|
|
431
|
+
command: shell,
|
|
432
|
+
args: [...shellArgs],
|
|
433
|
+
sandboxed: false,
|
|
434
|
+
backend: 'none',
|
|
435
|
+
mode: 'off'
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const platform = options.platform ?? process.platform;
|
|
439
|
+
const backend = options.backendOverride
|
|
440
|
+
?? (probeOsSandbox({ platform }).available ? probeOsSandbox({ platform }).backend : 'none');
|
|
441
|
+
if (backend === 'seatbelt') {
|
|
442
|
+
const profile = buildSeatbeltProfile({ ...options, mode });
|
|
443
|
+
return {
|
|
444
|
+
command: options.binaryPathOverride || SEATBELT_BIN,
|
|
445
|
+
args: ['-p', profile, '--', shell, ...shellArgs],
|
|
446
|
+
sandboxed: true,
|
|
447
|
+
backend: 'seatbelt',
|
|
448
|
+
mode,
|
|
449
|
+
profile
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
if (backend === 'bubblewrap') {
|
|
453
|
+
const bin = options.binaryPathOverride || probeOsSandbox({ platform }).binaryPath || 'bwrap';
|
|
454
|
+
return {
|
|
455
|
+
command: bin,
|
|
456
|
+
args: buildBubblewrapArgs(shell, shellArgs, { ...options, mode }),
|
|
457
|
+
sandboxed: true,
|
|
458
|
+
backend: 'bubblewrap',
|
|
459
|
+
mode
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
if (options.strict) {
|
|
463
|
+
const probe = probeOsSandbox({ platform });
|
|
464
|
+
throw new Error(`Strict OS sandbox requested (${mode}) but no kernel sandbox backend is available: ${probe.reason}`);
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
command: shell,
|
|
468
|
+
args: [...shellArgs],
|
|
469
|
+
sandboxed: false,
|
|
470
|
+
backend: 'none',
|
|
471
|
+
mode
|
|
472
|
+
};
|
|
473
|
+
}
|