plugin-agent-orchestrator 1.0.33 → 1.0.35

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.
Files changed (39) hide show
  1. package/dist/externalVersion.js +6 -6
  2. package/dist/server/collections/skill-executions.js +21 -0
  3. package/dist/server/plugin.js +132 -3
  4. package/dist/server/resources/agent-loop.js +1 -2
  5. package/dist/server/services/AgentLoopController.js +39 -1
  6. package/dist/server/services/AgentLoopService.js +3 -0
  7. package/dist/server/services/AgentRuntimeLifecycle.js +70 -0
  8. package/dist/server/services/FileManager.js +12 -0
  9. package/dist/server/services/NativeSubAgentObserver.js +20 -0
  10. package/dist/server/services/PlanningPromptService.js +95 -0
  11. package/dist/server/services/SandboxRunner.js +251 -56
  12. package/dist/server/services/TokenTracker.js +20 -0
  13. package/dist/server/services/WorkerEnvManager.js +76 -10
  14. package/dist/server/services/pythonGuardTemplate.js +105 -0
  15. package/dist/server/services/sandboxCapabilities.js +286 -0
  16. package/dist/server/skill-hub/plugin.js +98 -24
  17. package/dist/server/skill-hub/tasks/SkillExecutionTask.js +53 -11
  18. package/dist/server/utils/skill-settings.js +1 -1
  19. package/package.json +47 -47
  20. package/src/server/__tests__/agent-runtime-lifecycle.test.ts +46 -0
  21. package/src/server/__tests__/planning-prompt-service.test.ts +23 -0
  22. package/src/server/__tests__/skill-sandbox-worker-mode.test.ts +15 -0
  23. package/src/server/collections/skill-executions.ts +21 -0
  24. package/src/server/plugin.ts +151 -0
  25. package/src/server/resources/agent-loop.ts +1 -2
  26. package/src/server/services/AgentLoopController.ts +50 -1
  27. package/src/server/services/AgentLoopService.ts +4 -0
  28. package/src/server/services/AgentRuntimeLifecycle.ts +64 -0
  29. package/src/server/services/FileManager.ts +16 -1
  30. package/src/server/services/NativeSubAgentObserver.ts +21 -0
  31. package/src/server/services/PlanningPromptService.ts +72 -0
  32. package/src/server/services/SandboxRunner.ts +341 -74
  33. package/src/server/services/TokenTracker.ts +26 -0
  34. package/src/server/services/WorkerEnvManager.ts +95 -9
  35. package/src/server/services/pythonGuardTemplate.ts +87 -0
  36. package/src/server/services/sandboxCapabilities.ts +348 -0
  37. package/src/server/skill-hub/plugin.ts +1099 -1016
  38. package/src/server/skill-hub/tasks/SkillExecutionTask.ts +515 -470
  39. package/src/server/utils/skill-settings.ts +1 -0
@@ -1,6 +1,16 @@
1
1
  import Application from '@nocobase/server';
2
2
  import { Database } from '@nocobase/database';
3
+ import { mkdirSync, readFileSync, writeFileSync } from 'fs';
4
+ import { join } from 'path';
3
5
  import { parseJsonText, stringifyJsonText } from '../skill-hub/utils/json-fields';
6
+ import { PY_GUARD_SITECUSTOMIZE_SOURCE } from './pythonGuardTemplate';
7
+ import {
8
+ getPythonGuardDir,
9
+ getSandboxUsername,
10
+ refreshSandboxCapabilities,
11
+ runProbeCommand,
12
+ SandboxLogger,
13
+ } from './sandboxCapabilities';
4
14
 
5
15
  type WorkerEnvConfig = {
6
16
  npmRegistryUrl?: string;
@@ -82,30 +92,106 @@ export class WorkerEnvManager {
82
92
  }
83
93
 
84
94
  async executeInit(payload: WorkerEnvConfig) {
95
+ const logger: SandboxLogger = (this as any).app?.logger ?? console;
96
+ const publishProgress = async (percent: number, log: string) => {
97
+ await (this as any).app.pubSubManager.publish('skill-hub.init-env.progress', { percent, log });
98
+ };
99
+
100
+ // 1. Resolve whitelist (unchanged logic)
85
101
  const customPackages = payload.customPackages || { python: [], node: [], apt: [] };
86
102
  const whitelist = {
87
103
  python: Array.from(new Set([...(DEFAULT_WHITELIST.python || []), ...(customPackages.python || [])])),
88
104
  node: Array.from(new Set([...(DEFAULT_WHITELIST.node || []), ...(customPackages.node || [])])),
89
105
  apt: Array.from(new Set([...(DEFAULT_WHITELIST.apt || []), ...(customPackages.apt || [])])),
90
106
  };
107
+ await publishProgress(10, 'Resolved sandbox package whitelist');
91
108
 
92
- await (this as any).app.pubSubManager.publish('skill-hub.init-env.progress', {
93
- percent: 25,
94
- log: 'Resolved sandbox package whitelist',
95
- });
109
+ // 2. Ensure the dedicated sandbox user exists (Linux + root only)
110
+ const sandboxUserLog = await this.ensureSandboxUser(logger);
111
+ await publishProgress(25, sandboxUserLog);
96
112
 
97
- await (this as any).app.pubSubManager.publish('skill-hub.init-env.progress', {
98
- percent: 75,
99
- log: 'Sandbox runtime uses the local worker environment',
100
- });
113
+ // 3. Write/refresh the Python audit-hook guard
114
+ const guardLog = this.writePythonGuard(logger);
115
+ await publishProgress(45, guardLog);
116
+
117
+ // 4 + 5. Smoke-test Node permission model + bwrap; cache for SandboxRunner
118
+ const capabilities = await refreshSandboxCapabilities(logger, this.storagePath);
119
+ await publishProgress(60, `Node permission model: ${capabilities.nodePermissionModel}`);
120
+ await publishProgress(
121
+ 80,
122
+ capabilities.isolationLevel === 'bwrap'
123
+ ? `bubblewrap namespace sandbox active (${capabilities.bwrapMode})`
124
+ : `Sandbox isolation level: ${capabilities.isolationLevel}`,
125
+ );
101
126
 
127
+ // 6. Done — payload gains the achieved isolation summary
102
128
  await (this as any).app.pubSubManager.publish('skill-hub.init-env.done', {
103
129
  status: 'succeeded',
104
- log: 'Sandbox environment is ready on this worker. Package installation is managed by the worker image/runtime; whitelist was refreshed.',
130
+ log:
131
+ `Sandbox environment is ready on this worker. Isolation level: ${capabilities.isolationLevel}; ` +
132
+ `privilege drop: ${capabilities.privilegeDropAvailable ? 'active' : 'unavailable'}; ` +
133
+ `Node permission model: ${capabilities.nodePermissionModel}; ` +
134
+ `Python guard: ${capabilities.pythonGuardActive ? 'active' : 'inactive'}; ` +
135
+ `network: ${capabilities.network}. Whitelist was refreshed.`,
105
136
  whitelist,
137
+ isolationLevel: capabilities.isolationLevel,
138
+ privilegeDropAvailable: capabilities.privilegeDropAvailable,
139
+ nodePermissionModel: capabilities.nodePermissionModel,
140
+ pythonGuardActive: capabilities.pythonGuardActive,
141
+ network: capabilities.network,
106
142
  });
107
143
  }
108
144
 
145
+ /**
146
+ * Idempotently create the dedicated sandbox user with no supplementary
147
+ * groups (blunts the libuv setuid-without-setgroups gap). Never throws —
148
+ * failure just leaves privilege drop unavailable.
149
+ */
150
+ private async ensureSandboxUser(logger: SandboxLogger): Promise<string> {
151
+ if (process.platform !== 'linux') {
152
+ return 'Sandbox user skipped (privilege drop is Linux-only)';
153
+ }
154
+ if (process.getuid?.() !== 0) {
155
+ return 'Sandbox user skipped (worker is not running as root)';
156
+ }
157
+ const username = getSandboxUsername();
158
+ try {
159
+ const passwd = readFileSync('/etc/passwd', 'utf-8');
160
+ if (passwd.split('\n').some((line) => line.startsWith(`${username}:`))) {
161
+ return `Sandbox user "${username}" already exists`;
162
+ }
163
+ } catch (error) {
164
+ logger.warn(`[skill-hub] Could not read /etc/passwd: ${(error as Error).message}`);
165
+ }
166
+ const result = await runProbeCommand(
167
+ 'useradd',
168
+ ['--system', '--no-create-home', '--shell', '/usr/sbin/nologin', '--no-user-group', username],
169
+ 30000,
170
+ );
171
+ if (result.code === 0) {
172
+ return `Created sandbox user "${username}"`;
173
+ }
174
+ const reason = (result.stderr || result.errorCode || `exit ${result.code}`).trim().slice(0, 200);
175
+ logger.warn(`[skill-hub] useradd for "${username}" failed; privilege drop unavailable: ${reason}`);
176
+ return `Sandbox user unavailable (${reason})`;
177
+ }
178
+
179
+ /** Write/refresh sitecustomize.py so plugin upgrades propagate guard changes. */
180
+ private writePythonGuard(logger: SandboxLogger): string {
181
+ if (process.env.SKILL_HUB_PY_GUARD === 'false') {
182
+ return 'Python audit-hook guard disabled via SKILL_HUB_PY_GUARD=false';
183
+ }
184
+ try {
185
+ const guardDir = getPythonGuardDir(this.storagePath);
186
+ mkdirSync(guardDir, { recursive: true });
187
+ writeFileSync(join(guardDir, 'sitecustomize.py'), PY_GUARD_SITECUSTOMIZE_SOURCE, 'utf-8');
188
+ return 'Python audit-hook guard (sitecustomize.py) refreshed';
189
+ } catch (error) {
190
+ logger.warn(`[skill-hub] Failed to write python guard: ${(error as Error).message}`);
191
+ return `Python audit-hook guard unavailable (${(error as Error).message})`;
192
+ }
193
+ }
194
+
109
195
  parsePackageWhitelist(config: any) {
110
196
  return parseJsonText(config?.get?.('packageWhitelist') ?? config?.packageWhitelist, DEFAULT_WHITELIST);
111
197
  }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Python audit-hook guard, written by WorkerEnvManager.executeInit() to
3
+ * <storagePath>/sandbox-workspace/py-guard/sitecustomize.py and auto-imported
4
+ * by CPython's `site` module because SandboxRunner prepends that directory to
5
+ * PYTHONPATH for python executions.
6
+ *
7
+ * Stored as a TS string constant (same precedent as SkillManager's seed
8
+ * templates) so no build-pipeline asset copying is needed.
9
+ *
10
+ * Explicit caveat: per CPython docs, sys.addaudithook is NOT a hard sandbox
11
+ * boundary — it cannot be removed once installed, but native extensions can
12
+ * bypass it at the C level. It is defense-in-depth against whitelisted-but-
13
+ * misbehaving packages and gaps in the static CodeValidator, not the trust
14
+ * boundary (that is bwrap / the container). Network events (socket.*) are
15
+ * deliberately NOT blocked: whitelisted packages like `requests` need them;
16
+ * network egress control belongs to bwrap --unshare-net, not this hook.
17
+ */
18
+ export const PY_GUARD_SITECUSTOMIZE_SOURCE = `"""NocoBase skill-hub sandbox guard (auto-generated; do not edit)."""
19
+ import os
20
+ import sys
21
+
22
+ _BLOCKED_EVENTS = frozenset((
23
+ "os.system",
24
+ "os.fork",
25
+ "os.forkpty",
26
+ "os.exec",
27
+ "os.spawn",
28
+ "os.posix_spawn",
29
+ "subprocess.Popen",
30
+ "_posixsubprocess.fork_exec",
31
+ "ctypes.dlopen",
32
+ "ctypes.dlsym",
33
+ "ctypes.dlsym/handle",
34
+ ))
35
+
36
+ _raw_roots = os.environ.get("SKILL_HUB_WRITE_ROOTS", "")
37
+ _WRITE_ROOTS = tuple(
38
+ os.path.realpath(root) for root in _raw_roots.split(os.pathsep) if root
39
+ )
40
+
41
+ _WRITE_FLAGS = (
42
+ os.O_WRONLY
43
+ | os.O_RDWR
44
+ | os.O_APPEND
45
+ | getattr(os, "O_CREAT", 0)
46
+ | getattr(os, "O_TRUNC", 0)
47
+ )
48
+
49
+
50
+ def _is_write_open(mode, flags):
51
+ if isinstance(mode, str):
52
+ return any(ch in mode for ch in ("w", "a", "x", "+"))
53
+ try:
54
+ return bool(int(flags) & _WRITE_FLAGS)
55
+ except (TypeError, ValueError):
56
+ return False
57
+
58
+
59
+ def _is_allowed_write_path(path):
60
+ try:
61
+ resolved = os.path.realpath(os.fspath(path))
62
+ except (TypeError, ValueError):
63
+ # fd-based or exotic targets cannot be path-scoped here
64
+ return True
65
+ for root in _WRITE_ROOTS:
66
+ if resolved == root or resolved.startswith(root + os.sep):
67
+ return True
68
+ return False
69
+
70
+
71
+ def _skill_hub_guard(event, args):
72
+ if event in _BLOCKED_EVENTS:
73
+ raise RuntimeError(
74
+ "skill-hub sandbox: blocked audited operation '%s'" % event
75
+ )
76
+ if _WRITE_ROOTS and event == "open" and len(args) >= 3:
77
+ path, mode, flags = args[0], args[1], args[2]
78
+ if path is not None and _is_write_open(mode, flags) and not _is_allowed_write_path(path):
79
+ # PermissionError (an OSError) so import machinery treats a denied
80
+ # bytecode-cache write as best-effort instead of aborting imports.
81
+ raise PermissionError(
82
+ "skill-hub sandbox: write outside allowed roots denied: %r" % (path,)
83
+ )
84
+
85
+
86
+ sys.addaudithook(_skill_hub_guard)
87
+ `;
@@ -0,0 +1,348 @@
1
+ import { spawn } from 'child_process';
2
+ import { chmodSync, existsSync, readFileSync, statSync } from 'fs';
3
+ import { delimiter, join, resolve } from 'path';
4
+
5
+ const IS_WINDOWS = process.platform === 'win32';
6
+
7
+ export interface SandboxLogger {
8
+ info(message: string): void;
9
+ warn(message: string): void;
10
+ }
11
+
12
+ export type NodePermissionModel = 'permission' | 'experimental-permission' | 'unavailable' | 'unknown';
13
+ export type BwrapMode = 'unprivileged' | 'setuid' | 'unavailable';
14
+ export type SandboxIsolationLevel = 'bwrap' | 'hardened-spawn' | 'minimal';
15
+ export type SandboxNetworkMode = 'none' | 'inherit';
16
+
17
+ export interface SandboxIdentity {
18
+ username: string;
19
+ uid: number;
20
+ gid: number;
21
+ }
22
+
23
+ /**
24
+ * Result of the per-worker sandbox capability detection. Computed once at init
25
+ * (or lazily on the first execution) and read on every execution — never
26
+ * re-probed per execution.
27
+ */
28
+ export interface SandboxCapabilities {
29
+ isolationLevel: SandboxIsolationLevel;
30
+ privilegeDropAvailable: boolean;
31
+ sandboxIdentity: SandboxIdentity | null;
32
+ nodePermissionModel: NodePermissionModel;
33
+ /** Confirmed CLI flag name, e.g. '--permission'; null when unavailable. */
34
+ nodePermissionFlag: string | null;
35
+ /** Whether '--allow-addons' is accepted alongside the permission flag. */
36
+ nodePermissionAllowAddons: boolean;
37
+ pythonGuardActive: boolean;
38
+ bwrapMode: BwrapMode;
39
+ network: SandboxNetworkMode;
40
+ }
41
+
42
+ const DEFAULT_CAPABILITIES: SandboxCapabilities = {
43
+ isolationLevel: IS_WINDOWS ? 'minimal' : 'hardened-spawn',
44
+ privilegeDropAvailable: false,
45
+ sandboxIdentity: null,
46
+ nodePermissionModel: 'unknown',
47
+ nodePermissionFlag: null,
48
+ nodePermissionAllowAddons: false,
49
+ pythonGuardActive: false,
50
+ bwrapMode: 'unavailable',
51
+ network: 'none',
52
+ };
53
+
54
+ let current: SandboxCapabilities = { ...DEFAULT_CAPABILITIES };
55
+ let detectPromise: Promise<SandboxCapabilities> | null = null;
56
+
57
+ export function getSandboxCapabilities(): SandboxCapabilities {
58
+ return current;
59
+ }
60
+
61
+ export function setSandboxCapabilities(next: SandboxCapabilities): void {
62
+ current = next;
63
+ }
64
+
65
+ export function getSandboxUsername(): string {
66
+ return process.env.SKILL_HUB_SANDBOX_USER || 'nocobase-sandbox';
67
+ }
68
+
69
+ export function getPythonGuardDir(storagePath: string): string {
70
+ return resolve(storagePath, 'sandbox-workspace', 'py-guard');
71
+ }
72
+
73
+ export interface ProbeResult {
74
+ code: number | null;
75
+ stdout: string;
76
+ stderr: string;
77
+ /** Node error code (e.g. ENOENT) when the process failed to spawn. */
78
+ errorCode?: string;
79
+ }
80
+
81
+ /**
82
+ * Run a short-lived probe/maintenance command without a shell. Never rejects —
83
+ * spawn failures surface as { code: null, errorCode }.
84
+ */
85
+ export function runProbeCommand(
86
+ command: string,
87
+ args: string[],
88
+ timeoutMs = 10000,
89
+ options: { uid?: number; gid?: number } = {},
90
+ ): Promise<ProbeResult> {
91
+ return new Promise<ProbeResult>((_resolve) => {
92
+ let stdout = '';
93
+ let stderr = '';
94
+ let settled = false;
95
+ let timer: NodeJS.Timeout | null = null;
96
+
97
+ const finish = (result: ProbeResult) => {
98
+ if (settled) return;
99
+ settled = true;
100
+ if (timer) clearTimeout(timer);
101
+ _resolve(result);
102
+ };
103
+
104
+ let child: ReturnType<typeof spawn>;
105
+ try {
106
+ child = spawn(command, args, {
107
+ stdio: ['ignore', 'pipe', 'pipe'],
108
+ uid: options.uid,
109
+ gid: options.gid,
110
+ });
111
+ } catch (error) {
112
+ finish({ code: null, stdout: '', stderr: String((error as Error).message || error), errorCode: 'SPAWN' });
113
+ return;
114
+ }
115
+
116
+ timer = setTimeout(() => {
117
+ child.kill('SIGTERM');
118
+ finish({ code: null, stdout, stderr: `${stderr}\n[probe timed out after ${timeoutMs}ms]` });
119
+ }, timeoutMs);
120
+
121
+ child.stdout?.on('data', (chunk: Buffer) => {
122
+ if (stdout.length < 4096) stdout += chunk.toString();
123
+ });
124
+ child.stderr?.on('data', (chunk: Buffer) => {
125
+ if (stderr.length < 4096) stderr += chunk.toString();
126
+ });
127
+ child.on('error', (error: NodeJS.ErrnoException) => {
128
+ finish({ code: null, stdout, stderr: error.message, errorCode: error.code });
129
+ });
130
+ child.on('close', (code) => {
131
+ finish({ code, stdout, stderr });
132
+ });
133
+ });
134
+ }
135
+
136
+ function resolveBinaryPath(name: string): string | null {
137
+ for (const dir of (process.env.PATH || '').split(delimiter)) {
138
+ if (!dir) continue;
139
+ const candidate = join(dir, name);
140
+ if (existsSync(candidate)) return candidate;
141
+ }
142
+ return null;
143
+ }
144
+
145
+ /**
146
+ * Resolve uid/gid of the dedicated sandbox user by reading /etc/passwd
147
+ * directly (no per-execution subprocess).
148
+ */
149
+ export function resolveSandboxIdentity(username: string): SandboxIdentity | null {
150
+ try {
151
+ const passwd = readFileSync('/etc/passwd', 'utf-8');
152
+ for (const line of passwd.split('\n')) {
153
+ const parts = line.split(':');
154
+ if (parts[0] === username && parts.length >= 4) {
155
+ const uid = Number(parts[2]);
156
+ const gid = Number(parts[3]);
157
+ if (Number.isInteger(uid) && uid >= 0 && Number.isInteger(gid) && gid >= 0) {
158
+ return { username, uid, gid };
159
+ }
160
+ }
161
+ }
162
+ } catch {
163
+ // /etc/passwd unreadable — treat as no identity
164
+ }
165
+ return null;
166
+ }
167
+
168
+ async function detectNodePermissionFlag(
169
+ logger: SandboxLogger,
170
+ ): Promise<{ model: NodePermissionModel; flag: string | null; allowAddons: boolean }> {
171
+ const [majorRaw, minorRaw] = process.versions.node.split('.');
172
+ const major = Number(majorRaw);
173
+ const minor = Number(minorRaw);
174
+
175
+ let candidates: string[];
176
+ if (major > 22 || (major === 22 && minor >= 13)) {
177
+ candidates = ['--permission', '--experimental-permission'];
178
+ } else if (major >= 20) {
179
+ candidates = ['--experimental-permission', '--permission'];
180
+ } else {
181
+ logger.info(`[skill-hub] Node ${process.versions.node} has no permission model; skipping that layer`);
182
+ return { model: 'unavailable', flag: null, allowAddons: false };
183
+ }
184
+
185
+ for (const flag of candidates) {
186
+ // Smoke-test with --allow-addons first (needed for whitelisted native
187
+ // packages like sharp); fall back to the bare flag on older 20.x lines.
188
+ const withAddons = await runProbeCommand(
189
+ 'node',
190
+ [flag, '--allow-fs-read=/', '--allow-addons', '-e', 'process.exit(0)'],
191
+ 15000,
192
+ );
193
+ if (withAddons.code === 0) {
194
+ return { model: flag === '--permission' ? 'permission' : 'experimental-permission', flag, allowAddons: true };
195
+ }
196
+ const bare = await runProbeCommand('node', [flag, '--allow-fs-read=/', '-e', 'process.exit(0)'], 15000);
197
+ if (bare.code === 0) {
198
+ return { model: flag === '--permission' ? 'permission' : 'experimental-permission', flag, allowAddons: false };
199
+ }
200
+ }
201
+
202
+ logger.warn('[skill-hub] Node permission model flags rejected by the local node binary; layer disabled');
203
+ return { model: 'unavailable', flag: null, allowAddons: false };
204
+ }
205
+
206
+ const BWRAP_SMOKE_ARGS = [
207
+ '--unshare-user',
208
+ '--unshare-pid',
209
+ '--ro-bind',
210
+ '/',
211
+ '/',
212
+ '--dev',
213
+ '/dev',
214
+ '--proc',
215
+ '/proc',
216
+ '--',
217
+ 'true',
218
+ ];
219
+
220
+ async function detectBwrap(logger: SandboxLogger, identity: SandboxIdentity | null): Promise<BwrapMode> {
221
+ if (process.platform !== 'linux') return 'unavailable';
222
+
223
+ const version = await runProbeCommand('bwrap', ['--version'], 5000);
224
+ if (version.errorCode === 'ENOENT') {
225
+ logger.info('[skill-hub] bwrap not installed; using hardened spawn isolation (Layer 1)');
226
+ return 'unavailable';
227
+ }
228
+
229
+ // Probe under the same uid/gid the real execution will use, so a root-only
230
+ // userns permission does not produce a false positive.
231
+ const probeIds = identity && process.getuid?.() === 0 ? { uid: identity.uid, gid: identity.gid } : {};
232
+ const unprivileged = await runProbeCommand('bwrap', BWRAP_SMOKE_ARGS, 10000, probeIds);
233
+ if (unprivileged.code === 0) return 'unprivileged';
234
+
235
+ if (process.getuid?.() === 0) {
236
+ const bwrapPath = resolveBinaryPath('bwrap');
237
+ if (bwrapPath) {
238
+ try {
239
+ const mode = statSync(bwrapPath).mode;
240
+ if (!(mode & 0o4000)) {
241
+ chmodSync(bwrapPath, (mode & 0o7777) | 0o4000);
242
+ }
243
+ const retried = await runProbeCommand('bwrap', BWRAP_SMOKE_ARGS, 10000, probeIds);
244
+ if (retried.code === 0) return 'setuid';
245
+ logger.warn(
246
+ `[skill-hub] bwrap unusable even with setuid bit — likely unprivileged user namespaces disabled at the host kernel and no-new-privileges set on the container: ${retried.stderr
247
+ .trim()
248
+ .slice(0, 200)}`,
249
+ );
250
+ } catch (error) {
251
+ logger.warn(`[skill-hub] Could not apply setuid fallback to bwrap: ${(error as Error).message}`);
252
+ }
253
+ }
254
+ } else {
255
+ logger.warn(
256
+ `[skill-hub] bwrap installed but namespace creation failed (unprivileged userns disabled?): ${unprivileged.stderr
257
+ .trim()
258
+ .slice(0, 200)}`,
259
+ );
260
+ }
261
+ return 'unavailable';
262
+ }
263
+
264
+ async function detectSandboxCapabilities(logger: SandboxLogger, storagePath: string): Promise<SandboxCapabilities> {
265
+ const network: SandboxNetworkMode = process.env.SKILL_HUB_SANDBOX_NET === 'inherit' ? 'inherit' : 'none';
266
+
267
+ const pythonGuardActive =
268
+ process.env.SKILL_HUB_PY_GUARD !== 'false' && existsSync(join(getPythonGuardDir(storagePath), 'sitecustomize.py'));
269
+
270
+ let nodePermission: { model: NodePermissionModel; flag: string | null; allowAddons: boolean };
271
+ try {
272
+ nodePermission = await detectNodePermissionFlag(logger);
273
+ } catch (error) {
274
+ logger.warn(`[skill-hub] Node permission model detection failed: ${(error as Error).message}`);
275
+ nodePermission = { model: 'unavailable', flag: null, allowAddons: false };
276
+ }
277
+
278
+ let identity: SandboxIdentity | null = null;
279
+ let privilegeDropAvailable = false;
280
+ let bwrapMode: BwrapMode = 'unavailable';
281
+
282
+ if (!IS_WINDOWS) {
283
+ identity = resolveSandboxIdentity(getSandboxUsername());
284
+ privilegeDropAvailable = process.getuid?.() === 0 && identity !== null;
285
+ if (!privilegeDropAvailable) {
286
+ logger.warn(
287
+ `[skill-hub] Privilege drop unavailable (root=${
288
+ process.getuid?.() === 0
289
+ }, sandbox user "${getSandboxUsername()}" resolved=${identity !== null}); executions run as the worker user`,
290
+ );
291
+ }
292
+ try {
293
+ bwrapMode = await detectBwrap(logger, privilegeDropAvailable ? identity : null);
294
+ } catch (error) {
295
+ logger.warn(`[skill-hub] bwrap detection failed: ${(error as Error).message}`);
296
+ bwrapMode = 'unavailable';
297
+ }
298
+ }
299
+
300
+ const isolationLevel: SandboxIsolationLevel =
301
+ bwrapMode !== 'unavailable' ? 'bwrap' : IS_WINDOWS ? 'minimal' : 'hardened-spawn';
302
+
303
+ const capabilities: SandboxCapabilities = {
304
+ isolationLevel,
305
+ privilegeDropAvailable,
306
+ sandboxIdentity: privilegeDropAvailable ? identity : null,
307
+ nodePermissionModel: nodePermission.model,
308
+ nodePermissionFlag: nodePermission.flag,
309
+ nodePermissionAllowAddons: nodePermission.allowAddons,
310
+ pythonGuardActive,
311
+ bwrapMode,
312
+ network,
313
+ };
314
+
315
+ logger.info(
316
+ `[skill-hub] Sandbox isolation level: ${capabilities.isolationLevel}` +
317
+ ` (privilege drop: ${capabilities.privilegeDropAvailable ? 'active' : 'off'},` +
318
+ ` node permission model: ${capabilities.nodePermissionModel},` +
319
+ ` python guard: ${capabilities.pythonGuardActive ? 'active' : 'off'},` +
320
+ ` bwrap: ${capabilities.bwrapMode}, network: ${capabilities.network})`,
321
+ );
322
+
323
+ return capabilities;
324
+ }
325
+
326
+ /**
327
+ * Lazily detect capabilities once per worker process (memoized). Used by
328
+ * SandboxRunner so the first execution after boot has real capability data
329
+ * without requiring an admin-triggered init.
330
+ */
331
+ export function ensureSandboxCapabilities(logger: SandboxLogger, storagePath: string): Promise<SandboxCapabilities> {
332
+ if (!detectPromise) {
333
+ detectPromise = detectSandboxCapabilities(logger, storagePath).then((caps) => {
334
+ current = caps;
335
+ return caps;
336
+ });
337
+ }
338
+ return detectPromise;
339
+ }
340
+
341
+ /** Force a fresh detection run — called from WorkerEnvManager.executeInit(). */
342
+ export function refreshSandboxCapabilities(logger: SandboxLogger, storagePath: string): Promise<SandboxCapabilities> {
343
+ detectPromise = detectSandboxCapabilities(logger, storagePath).then((caps) => {
344
+ current = caps;
345
+ return caps;
346
+ });
347
+ return detectPromise;
348
+ }