plugin-agent-orchestrator 1.0.33 → 1.0.36
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/dist/externalVersion.js +6 -6
- package/dist/server/collections/skill-executions.js +21 -0
- package/dist/server/plugin.js +153 -3
- package/dist/server/resources/agent-loop.js +1 -2
- package/dist/server/services/AgentLoopController.js +39 -1
- package/dist/server/services/AgentLoopService.js +3 -0
- package/dist/server/services/AgentRuntimeLifecycle.js +70 -0
- package/dist/server/services/FileManager.js +12 -0
- package/dist/server/services/NativeSubAgentObserver.js +20 -0
- package/dist/server/services/PlanningPromptService.js +95 -0
- package/dist/server/services/SandboxRunner.js +251 -56
- package/dist/server/services/TokenTracker.js +20 -0
- package/dist/server/services/WorkerEnvManager.js +76 -10
- package/dist/server/services/pythonGuardTemplate.js +105 -0
- package/dist/server/services/sandboxCapabilities.js +286 -0
- package/dist/server/skill-hub/plugin.js +123 -28
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +53 -11
- package/dist/server/utils/skill-settings.js +1 -1
- package/package.json +47 -47
- package/src/server/__tests__/agent-runtime-lifecycle.test.ts +46 -0
- package/src/server/__tests__/planning-prompt-service.test.ts +23 -0
- package/src/server/__tests__/skill-sandbox-worker-mode.test.ts +15 -0
- package/src/server/collections/skill-executions.ts +21 -0
- package/src/server/plugin.ts +178 -0
- package/src/server/resources/agent-loop.ts +1 -2
- package/src/server/services/AgentLoopController.ts +50 -1
- package/src/server/services/AgentLoopService.ts +4 -0
- package/src/server/services/AgentRuntimeLifecycle.ts +64 -0
- package/src/server/services/FileManager.ts +16 -1
- package/src/server/services/NativeSubAgentObserver.ts +21 -0
- package/src/server/services/PlanningPromptService.ts +72 -0
- package/src/server/services/SandboxRunner.ts +341 -74
- package/src/server/services/TokenTracker.ts +26 -0
- package/src/server/services/WorkerEnvManager.ts +95 -9
- package/src/server/services/pythonGuardTemplate.ts +87 -0
- package/src/server/services/sandboxCapabilities.ts +348 -0
- package/src/server/skill-hub/plugin.ts +1120 -1016
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +515 -470
- package/src/server/utils/skill-settings.ts +1 -0
|
@@ -26,7 +26,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
26
26
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
27
|
var SandboxRunner_exports = {};
|
|
28
28
|
__export(SandboxRunner_exports, {
|
|
29
|
-
SandboxRunner: () => SandboxRunner
|
|
29
|
+
SandboxRunner: () => SandboxRunner,
|
|
30
|
+
buildBwrapArgs: () => buildBwrapArgs,
|
|
31
|
+
buildNodeArgs: () => buildNodeArgs,
|
|
32
|
+
buildUlimitPrefix: () => buildUlimitPrefix
|
|
30
33
|
});
|
|
31
34
|
module.exports = __toCommonJS(SandboxRunner_exports);
|
|
32
35
|
var import_child_process = require("child_process");
|
|
@@ -34,10 +37,84 @@ var import_fs = require("fs");
|
|
|
34
37
|
var import_path = require("path");
|
|
35
38
|
var import_os = require("os");
|
|
36
39
|
var import_CodeValidator = require("./CodeValidator");
|
|
40
|
+
var import_sandboxCapabilities = require("./sandboxCapabilities");
|
|
37
41
|
const IS_WINDOWS = process.platform === "win32";
|
|
42
|
+
const MAX_STDIO_BYTES = 10 * 1024 * 1024;
|
|
43
|
+
const MAX_STDOUT_CHARS = 5e3;
|
|
44
|
+
const MAX_STDERR_CHARS = 2e3;
|
|
38
45
|
function pythonBinary() {
|
|
39
46
|
return process.env.SKILL_HUB_PYTHON_BIN || (IS_WINDOWS ? "python" : "python3");
|
|
40
47
|
}
|
|
48
|
+
function buildUlimitPrefix(options) {
|
|
49
|
+
const parts = [];
|
|
50
|
+
const add = (flag, value) => {
|
|
51
|
+
if (!Number.isFinite(value) || value <= 0) return;
|
|
52
|
+
parts.push(`ulimit ${flag} ${Math.floor(value)} 2>/dev/null;`);
|
|
53
|
+
};
|
|
54
|
+
if (options.language === "python") {
|
|
55
|
+
add("-v", options.memLimitMb * 1024);
|
|
56
|
+
}
|
|
57
|
+
add("-t", options.timeoutSeconds + 5);
|
|
58
|
+
add("-n", Number(process.env.SKILL_HUB_ULIMIT_NOFILE || 256));
|
|
59
|
+
add("-f", Math.ceil(options.maxOutputSizeMb * 1024 * 1024 / 512));
|
|
60
|
+
if (options.includeNproc) {
|
|
61
|
+
add("-u", Number(process.env.SKILL_HUB_ULIMIT_NPROC || 64));
|
|
62
|
+
}
|
|
63
|
+
return parts.join(" ");
|
|
64
|
+
}
|
|
65
|
+
function buildNodeArgs(options) {
|
|
66
|
+
const args = [`--max-old-space-size=${options.memLimitMb}`];
|
|
67
|
+
const permissionEnabled = process.env.SKILL_HUB_NODE_PERMISSION !== "false";
|
|
68
|
+
if (permissionEnabled && options.capabilities.nodePermissionFlag) {
|
|
69
|
+
args.push(options.capabilities.nodePermissionFlag);
|
|
70
|
+
const readDirs = new Set(
|
|
71
|
+
[options.workDir, (0, import_os.tmpdir)(), options.skillDir || "", ...options.nodePathEntries].filter(Boolean)
|
|
72
|
+
);
|
|
73
|
+
for (const dir of readDirs) {
|
|
74
|
+
args.push(`--allow-fs-read=${dir}`);
|
|
75
|
+
}
|
|
76
|
+
for (const dir of /* @__PURE__ */ new Set([options.workDir, options.outputDir, (0, import_os.tmpdir)()])) {
|
|
77
|
+
args.push(`--allow-fs-write=${dir}`);
|
|
78
|
+
}
|
|
79
|
+
if (options.capabilities.nodePermissionAllowAddons) {
|
|
80
|
+
args.push("--allow-addons");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
args.push("--disallow-code-generation-from-strings");
|
|
84
|
+
args.push(options.scriptPath);
|
|
85
|
+
return args;
|
|
86
|
+
}
|
|
87
|
+
function buildBwrapArgs(options) {
|
|
88
|
+
const args = ["--unshare-all", "--die-with-parent", "--new-session"];
|
|
89
|
+
const bound = /* @__PURE__ */ new Set();
|
|
90
|
+
const roBind = (path) => {
|
|
91
|
+
if (!path || bound.has(path) || !(0, import_fs.existsSync)(path)) return;
|
|
92
|
+
bound.add(path);
|
|
93
|
+
args.push("--ro-bind", path, path);
|
|
94
|
+
};
|
|
95
|
+
for (const path of ["/usr", "/bin", "/sbin", "/lib", "/lib64", "/etc/ssl", "/etc/alternatives"]) {
|
|
96
|
+
roBind(path);
|
|
97
|
+
}
|
|
98
|
+
roBind(options.sandboxWorkspace);
|
|
99
|
+
if (options.skillDir) roBind(options.skillDir);
|
|
100
|
+
for (const dir of options.extraReadOnlyDirs) roBind(dir);
|
|
101
|
+
if (options.network === "inherit") {
|
|
102
|
+
args.push("--share-net");
|
|
103
|
+
for (const path of ["/etc/resolv.conf", "/etc/hosts", "/etc/nsswitch.conf"]) {
|
|
104
|
+
roBind(path);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
args.push("--bind", options.workDir, options.workDir);
|
|
108
|
+
args.push("--tmpfs", (0, import_os.tmpdir)());
|
|
109
|
+
args.push("--proc", "/proc");
|
|
110
|
+
args.push("--dev", "/dev");
|
|
111
|
+
args.push("--chdir", options.workDir);
|
|
112
|
+
args.push("--clearenv");
|
|
113
|
+
for (const [key, value] of Object.entries(options.env)) {
|
|
114
|
+
args.push("--setenv", key, value);
|
|
115
|
+
}
|
|
116
|
+
return args;
|
|
117
|
+
}
|
|
41
118
|
class SandboxRunner {
|
|
42
119
|
constructor(fileManager, logger, storagePath) {
|
|
43
120
|
this.fileManager = fileManager;
|
|
@@ -48,6 +125,7 @@ class SandboxRunner {
|
|
|
48
125
|
validator = new import_CodeValidator.CodeValidator();
|
|
49
126
|
sandboxWorkspace;
|
|
50
127
|
async execute(options) {
|
|
128
|
+
var _a;
|
|
51
129
|
const {
|
|
52
130
|
language,
|
|
53
131
|
code,
|
|
@@ -61,76 +139,186 @@ class SandboxRunner {
|
|
|
61
139
|
} = options;
|
|
62
140
|
this.validator.validate(code, language);
|
|
63
141
|
this.validator.validateImports(code, language, packageWhitelist || []);
|
|
142
|
+
const capabilities = await (0, import_sandboxCapabilities.ensureSandboxCapabilities)(this.logger, this.storagePath);
|
|
64
143
|
const workDir = this.fileManager.createExecDir(execId);
|
|
65
144
|
const outputDir = this.fileManager.getOutputDir(execId);
|
|
66
145
|
const filename = language === "node" ? "script.js" : "script.py";
|
|
67
146
|
const scriptPath = (0, import_path.resolve)(workDir, filename);
|
|
68
147
|
(0, import_fs.writeFileSync)(scriptPath, code, "utf-8");
|
|
148
|
+
let identity = !IS_WINDOWS && ((_a = process.getuid) == null ? void 0 : _a.call(process)) === 0 && capabilities.privilegeDropAvailable ? capabilities.sandboxIdentity : null;
|
|
149
|
+
if (identity) {
|
|
150
|
+
try {
|
|
151
|
+
this.fileManager.chownExecDir(execId, identity.uid, identity.gid);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
this.logger.warn(
|
|
154
|
+
`[skill-hub] chown of exec dir failed for ${execId}; running without privilege drop: ${error.message}`
|
|
155
|
+
);
|
|
156
|
+
identity = null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
69
159
|
onProgress == null ? void 0 : onProgress({ percent: 20, log: "Code validated, executing..." });
|
|
70
160
|
const nodePath = (0, import_path.resolve)(this.sandboxWorkspace, "node_modules");
|
|
71
161
|
const finalNodePath = process.env.NODE_PATH ? `${nodePath}${import_path.delimiter}${process.env.NODE_PATH}` : nodePath;
|
|
162
|
+
const nodePathEntries = finalNodePath.split(import_path.delimiter).filter(Boolean);
|
|
163
|
+
const pythonGuardDir = (0, import_sandboxCapabilities.getPythonGuardDir)(this.storagePath);
|
|
164
|
+
const execEnv = {
|
|
165
|
+
PATH: process.env.PATH || "",
|
|
166
|
+
HOME: (0, import_os.tmpdir)(),
|
|
167
|
+
TMPDIR: (0, import_os.tmpdir)(),
|
|
168
|
+
OUTPUT_DIR: outputDir,
|
|
169
|
+
LANG: "en_US.UTF-8",
|
|
170
|
+
// Node.js
|
|
171
|
+
NODE_PATH: finalNodePath,
|
|
172
|
+
// Python — audit-hook guard dir first, then bundled packages (svg_to_pptx etc.)
|
|
173
|
+
PYTHONPATH: [
|
|
174
|
+
capabilities.pythonGuardActive ? pythonGuardDir : "",
|
|
175
|
+
skillDir ? (0, import_path.resolve)(skillDir, "scripts") : "",
|
|
176
|
+
(0, import_path.resolve)(this.sandboxWorkspace, "python_packages"),
|
|
177
|
+
process.env.PYTHONPATH || ""
|
|
178
|
+
].filter(Boolean).join(import_path.delimiter),
|
|
179
|
+
PYTHONIOENCODING: "utf-8",
|
|
180
|
+
// Denied bytecode-cache writes are best-effort anyway; skip them entirely.
|
|
181
|
+
PYTHONDONTWRITEBYTECODE: "1",
|
|
182
|
+
// Consumed by the sitecustomize.py guard to scope file writes.
|
|
183
|
+
SKILL_HUB_WRITE_ROOTS: [workDir, outputDir, (0, import_os.tmpdir)()].join(import_path.delimiter),
|
|
184
|
+
SKILL_DIR: skillDir || ""
|
|
185
|
+
// DO NOT pass: DB credentials, API keys, APP_KEY, etc.
|
|
186
|
+
};
|
|
72
187
|
const memLimitMb = Math.max(64, Number(process.env.SKILL_HUB_MEM_LIMIT_MB || 512));
|
|
73
|
-
const
|
|
74
|
-
const
|
|
188
|
+
const command = language === "node" ? "node" : pythonBinary();
|
|
189
|
+
const args = language === "node" ? buildNodeArgs({
|
|
190
|
+
memLimitMb,
|
|
191
|
+
scriptPath,
|
|
192
|
+
workDir,
|
|
193
|
+
outputDir,
|
|
194
|
+
skillDir,
|
|
195
|
+
nodePathEntries,
|
|
196
|
+
capabilities
|
|
197
|
+
}) : [scriptPath];
|
|
198
|
+
let spawnCommand = command;
|
|
199
|
+
let spawnArgs = args;
|
|
200
|
+
if (!IS_WINDOWS) {
|
|
201
|
+
const ulimitPrefix = buildUlimitPrefix({
|
|
202
|
+
language,
|
|
203
|
+
memLimitMb,
|
|
204
|
+
timeoutSeconds,
|
|
205
|
+
maxOutputSizeMb,
|
|
206
|
+
includeNproc: identity !== null
|
|
207
|
+
});
|
|
208
|
+
spawnCommand = "/bin/sh";
|
|
209
|
+
spawnArgs = ["-c", `${ulimitPrefix} exec "$0" "$@"`, command, ...args];
|
|
210
|
+
if (capabilities.isolationLevel === "bwrap") {
|
|
211
|
+
const extraReadOnlyDirs = [
|
|
212
|
+
...nodePathEntries.filter((entry) => entry !== nodePath),
|
|
213
|
+
...(process.env.PYTHONPATH || "").split(import_path.delimiter)
|
|
214
|
+
].filter(Boolean);
|
|
215
|
+
spawnArgs = [
|
|
216
|
+
...buildBwrapArgs({
|
|
217
|
+
workDir,
|
|
218
|
+
skillDir,
|
|
219
|
+
sandboxWorkspace: this.sandboxWorkspace,
|
|
220
|
+
extraReadOnlyDirs,
|
|
221
|
+
env: execEnv,
|
|
222
|
+
network: capabilities.network
|
|
223
|
+
}),
|
|
224
|
+
"--",
|
|
225
|
+
spawnCommand,
|
|
226
|
+
...spawnArgs
|
|
227
|
+
];
|
|
228
|
+
spawnCommand = "bwrap";
|
|
229
|
+
}
|
|
230
|
+
}
|
|
75
231
|
const startTime = Date.now();
|
|
76
|
-
let childProc = null;
|
|
77
232
|
let wasCanceled = false;
|
|
233
|
+
let wasTimedOut = false;
|
|
234
|
+
let outputExceeded = false;
|
|
78
235
|
const processResult = await new Promise((_resolve) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
236
|
+
var _a2, _b;
|
|
237
|
+
let stdout = "";
|
|
238
|
+
let stderr = "";
|
|
239
|
+
let totalBytes = 0;
|
|
240
|
+
let settled = false;
|
|
241
|
+
let timeoutTimer = null;
|
|
242
|
+
let forceKillTimer = null;
|
|
243
|
+
const child = (0, import_child_process.spawn)(spawnCommand, spawnArgs, {
|
|
244
|
+
cwd: workDir,
|
|
245
|
+
env: execEnv,
|
|
246
|
+
// Process-group leader on POSIX so kill(-pid) reaches every descendant.
|
|
247
|
+
detached: !IS_WINDOWS,
|
|
248
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
249
|
+
uid: identity == null ? void 0 : identity.uid,
|
|
250
|
+
gid: identity == null ? void 0 : identity.gid
|
|
251
|
+
});
|
|
252
|
+
const finish = (exitCode) => {
|
|
253
|
+
if (settled) return;
|
|
254
|
+
settled = true;
|
|
255
|
+
if (timeoutTimer) clearTimeout(timeoutTimer);
|
|
256
|
+
if (forceKillTimer) clearTimeout(forceKillTimer);
|
|
257
|
+
_resolve({
|
|
258
|
+
exitCode,
|
|
259
|
+
stdout: stdout.slice(0, MAX_STDOUT_CHARS),
|
|
260
|
+
stderr: stderr.slice(0, MAX_STDERR_CHARS)
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
const killTree = (killSignal) => {
|
|
264
|
+
if (!child.pid) return;
|
|
265
|
+
if (IS_WINDOWS) {
|
|
266
|
+
try {
|
|
267
|
+
(0, import_child_process.spawn)("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" });
|
|
268
|
+
} catch {
|
|
269
|
+
child.kill(killSignal);
|
|
104
270
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
process.kill(-child.pid, killSignal);
|
|
275
|
+
} catch {
|
|
276
|
+
try {
|
|
277
|
+
child.kill(killSignal);
|
|
278
|
+
} catch {
|
|
114
279
|
}
|
|
115
|
-
_resolve({
|
|
116
|
-
exitCode,
|
|
117
|
-
stdout: (stdout || "").slice(0, 5e3),
|
|
118
|
-
stderr: (stderr || "").slice(0, 2e3)
|
|
119
|
-
});
|
|
120
280
|
}
|
|
121
|
-
|
|
281
|
+
};
|
|
282
|
+
const terminate = (reason) => {
|
|
283
|
+
this.logger.info(`[skill-hub] Killing process tree for exec ${execId} (${reason})`);
|
|
284
|
+
killTree("SIGTERM");
|
|
285
|
+
if (!forceKillTimer) {
|
|
286
|
+
forceKillTimer = setTimeout(() => killTree("SIGKILL"), 3e3);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
timeoutTimer = setTimeout(() => {
|
|
290
|
+
wasTimedOut = true;
|
|
291
|
+
terminate("timeout");
|
|
292
|
+
}, timeoutSeconds * 1e3);
|
|
293
|
+
const onData = (isStdout) => (chunk) => {
|
|
294
|
+
totalBytes += chunk.length;
|
|
295
|
+
if (totalBytes > MAX_STDIO_BYTES) {
|
|
296
|
+
if (!outputExceeded) {
|
|
297
|
+
outputExceeded = true;
|
|
298
|
+
terminate("output exceeded limit");
|
|
299
|
+
}
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (isStdout) {
|
|
303
|
+
if (stdout.length < MAX_STDOUT_CHARS) stdout += chunk.toString();
|
|
304
|
+
} else if (stderr.length < MAX_STDERR_CHARS) {
|
|
305
|
+
stderr += chunk.toString();
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
(_a2 = child.stdout) == null ? void 0 : _a2.on("data", onData(true));
|
|
309
|
+
(_b = child.stderr) == null ? void 0 : _b.on("data", onData(false));
|
|
310
|
+
child.on("error", (error) => {
|
|
311
|
+
stderr = `Failed to start sandbox process: ${error.message}
|
|
312
|
+
${stderr}`;
|
|
313
|
+
finish(1);
|
|
314
|
+
});
|
|
315
|
+
child.on("close", (exitCode, exitSignal) => {
|
|
316
|
+
finish(exitSignal !== null || exitCode === null ? -1 : exitCode);
|
|
317
|
+
});
|
|
122
318
|
if (signal) {
|
|
123
319
|
signal.addEventListener("abort", () => {
|
|
124
320
|
wasCanceled = true;
|
|
125
|
-
|
|
126
|
-
this.logger.info(`[skill-hub] Killing child process for exec ${execId} (user abort)`);
|
|
127
|
-
childProc.kill("SIGTERM");
|
|
128
|
-
setTimeout(() => {
|
|
129
|
-
if (childProc && !childProc.killed) {
|
|
130
|
-
childProc.kill("SIGKILL");
|
|
131
|
-
}
|
|
132
|
-
}, 3e3);
|
|
133
|
-
}
|
|
321
|
+
terminate("user abort");
|
|
134
322
|
});
|
|
135
323
|
}
|
|
136
324
|
});
|
|
@@ -145,9 +333,13 @@ class SandboxRunner {
|
|
|
145
333
|
);
|
|
146
334
|
}
|
|
147
335
|
onProgress == null ? void 0 : onProgress({ percent: 100, log: "Done" });
|
|
148
|
-
const timedOut =
|
|
336
|
+
const timedOut = wasTimedOut && !wasCanceled;
|
|
149
337
|
if (timedOut) {
|
|
150
338
|
processResult.stderr = `Execution timed out after ${timeoutSeconds}s
|
|
339
|
+
${processResult.stderr}`;
|
|
340
|
+
}
|
|
341
|
+
if (outputExceeded) {
|
|
342
|
+
processResult.stderr = `Execution stdout/stderr exceeded ${Math.floor(MAX_STDIO_BYTES / 1024 / 1024)}MB limit
|
|
151
343
|
${processResult.stderr}`;
|
|
152
344
|
}
|
|
153
345
|
if (wasCanceled) {
|
|
@@ -155,7 +347,7 @@ ${processResult.stderr}`;
|
|
|
155
347
|
${processResult.stderr}`;
|
|
156
348
|
}
|
|
157
349
|
return {
|
|
158
|
-
success: !wasCanceled && !timedOut && processResult.exitCode === 0,
|
|
350
|
+
success: !wasCanceled && !timedOut && !outputExceeded && processResult.exitCode === 0,
|
|
159
351
|
stdout: processResult.stdout,
|
|
160
352
|
stderr: processResult.stderr,
|
|
161
353
|
files,
|
|
@@ -167,5 +359,8 @@ ${processResult.stderr}`;
|
|
|
167
359
|
}
|
|
168
360
|
// Annotate the CommonJS export names for ESM import in node:
|
|
169
361
|
0 && (module.exports = {
|
|
170
|
-
SandboxRunner
|
|
362
|
+
SandboxRunner,
|
|
363
|
+
buildBwrapArgs,
|
|
364
|
+
buildNodeArgs,
|
|
365
|
+
buildUlimitPrefix
|
|
171
366
|
});
|
|
@@ -165,6 +165,26 @@ class TokenTracker {
|
|
|
165
165
|
return { allowed: true };
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Estimate tokens from text length and track on a span.
|
|
170
|
+
* Rough heuristic: ~4 chars per token for English text.
|
|
171
|
+
* Use when actual usage_metadata is not available (e.g. native plugin-ai flow).
|
|
172
|
+
*/
|
|
173
|
+
async estimateAndTrack(spanId, input, output, runId) {
|
|
174
|
+
if (!spanId) return;
|
|
175
|
+
const inputTokens = Math.ceil((input || "").length / 4);
|
|
176
|
+
const outputTokens = Math.ceil((output || "").length / 4);
|
|
177
|
+
await this.trackSpan(
|
|
178
|
+
spanId,
|
|
179
|
+
{
|
|
180
|
+
inputTokens,
|
|
181
|
+
outputTokens,
|
|
182
|
+
totalTokens: inputTokens + outputTokens,
|
|
183
|
+
cost: estimateCost(inputTokens, outputTokens)
|
|
184
|
+
},
|
|
185
|
+
runId
|
|
186
|
+
);
|
|
187
|
+
}
|
|
168
188
|
}
|
|
169
189
|
// Annotate the CommonJS export names for ESM import in node:
|
|
170
190
|
0 && (module.exports = {
|
|
@@ -29,7 +29,11 @@ __export(WorkerEnvManager_exports, {
|
|
|
29
29
|
WorkerEnvManager: () => WorkerEnvManager
|
|
30
30
|
});
|
|
31
31
|
module.exports = __toCommonJS(WorkerEnvManager_exports);
|
|
32
|
+
var import_fs = require("fs");
|
|
33
|
+
var import_path = require("path");
|
|
32
34
|
var import_json_fields = require("../skill-hub/utils/json-fields");
|
|
35
|
+
var import_pythonGuardTemplate = require("./pythonGuardTemplate");
|
|
36
|
+
var import_sandboxCapabilities = require("./sandboxCapabilities");
|
|
33
37
|
const DEFAULT_WHITELIST = {
|
|
34
38
|
python: [
|
|
35
39
|
"python-docx",
|
|
@@ -89,26 +93,88 @@ class WorkerEnvManager {
|
|
|
89
93
|
return "Sandbox environment refresh queued on available workers.";
|
|
90
94
|
}
|
|
91
95
|
async executeInit(payload) {
|
|
96
|
+
var _a;
|
|
97
|
+
const logger = ((_a = this.app) == null ? void 0 : _a.logger) ?? console;
|
|
98
|
+
const publishProgress = async (percent, log) => {
|
|
99
|
+
await this.app.pubSubManager.publish("skill-hub.init-env.progress", { percent, log });
|
|
100
|
+
};
|
|
92
101
|
const customPackages = payload.customPackages || { python: [], node: [], apt: [] };
|
|
93
102
|
const whitelist = {
|
|
94
103
|
python: Array.from(/* @__PURE__ */ new Set([...DEFAULT_WHITELIST.python || [], ...customPackages.python || []])),
|
|
95
104
|
node: Array.from(/* @__PURE__ */ new Set([...DEFAULT_WHITELIST.node || [], ...customPackages.node || []])),
|
|
96
105
|
apt: Array.from(/* @__PURE__ */ new Set([...DEFAULT_WHITELIST.apt || [], ...customPackages.apt || []]))
|
|
97
106
|
};
|
|
98
|
-
await
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
await
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
107
|
+
await publishProgress(10, "Resolved sandbox package whitelist");
|
|
108
|
+
const sandboxUserLog = await this.ensureSandboxUser(logger);
|
|
109
|
+
await publishProgress(25, sandboxUserLog);
|
|
110
|
+
const guardLog = this.writePythonGuard(logger);
|
|
111
|
+
await publishProgress(45, guardLog);
|
|
112
|
+
const capabilities = await (0, import_sandboxCapabilities.refreshSandboxCapabilities)(logger, this.storagePath);
|
|
113
|
+
await publishProgress(60, `Node permission model: ${capabilities.nodePermissionModel}`);
|
|
114
|
+
await publishProgress(
|
|
115
|
+
80,
|
|
116
|
+
capabilities.isolationLevel === "bwrap" ? `bubblewrap namespace sandbox active (${capabilities.bwrapMode})` : `Sandbox isolation level: ${capabilities.isolationLevel}`
|
|
117
|
+
);
|
|
106
118
|
await this.app.pubSubManager.publish("skill-hub.init-env.done", {
|
|
107
119
|
status: "succeeded",
|
|
108
|
-
log:
|
|
109
|
-
whitelist
|
|
120
|
+
log: `Sandbox environment is ready on this worker. Isolation level: ${capabilities.isolationLevel}; privilege drop: ${capabilities.privilegeDropAvailable ? "active" : "unavailable"}; Node permission model: ${capabilities.nodePermissionModel}; Python guard: ${capabilities.pythonGuardActive ? "active" : "inactive"}; network: ${capabilities.network}. Whitelist was refreshed.`,
|
|
121
|
+
whitelist,
|
|
122
|
+
isolationLevel: capabilities.isolationLevel,
|
|
123
|
+
privilegeDropAvailable: capabilities.privilegeDropAvailable,
|
|
124
|
+
nodePermissionModel: capabilities.nodePermissionModel,
|
|
125
|
+
pythonGuardActive: capabilities.pythonGuardActive,
|
|
126
|
+
network: capabilities.network
|
|
110
127
|
});
|
|
111
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Idempotently create the dedicated sandbox user with no supplementary
|
|
131
|
+
* groups (blunts the libuv setuid-without-setgroups gap). Never throws —
|
|
132
|
+
* failure just leaves privilege drop unavailable.
|
|
133
|
+
*/
|
|
134
|
+
async ensureSandboxUser(logger) {
|
|
135
|
+
var _a;
|
|
136
|
+
if (process.platform !== "linux") {
|
|
137
|
+
return "Sandbox user skipped (privilege drop is Linux-only)";
|
|
138
|
+
}
|
|
139
|
+
if (((_a = process.getuid) == null ? void 0 : _a.call(process)) !== 0) {
|
|
140
|
+
return "Sandbox user skipped (worker is not running as root)";
|
|
141
|
+
}
|
|
142
|
+
const username = (0, import_sandboxCapabilities.getSandboxUsername)();
|
|
143
|
+
try {
|
|
144
|
+
const passwd = (0, import_fs.readFileSync)("/etc/passwd", "utf-8");
|
|
145
|
+
if (passwd.split("\n").some((line) => line.startsWith(`${username}:`))) {
|
|
146
|
+
return `Sandbox user "${username}" already exists`;
|
|
147
|
+
}
|
|
148
|
+
} catch (error) {
|
|
149
|
+
logger.warn(`[skill-hub] Could not read /etc/passwd: ${error.message}`);
|
|
150
|
+
}
|
|
151
|
+
const result = await (0, import_sandboxCapabilities.runProbeCommand)(
|
|
152
|
+
"useradd",
|
|
153
|
+
["--system", "--no-create-home", "--shell", "/usr/sbin/nologin", "--no-user-group", username],
|
|
154
|
+
3e4
|
|
155
|
+
);
|
|
156
|
+
if (result.code === 0) {
|
|
157
|
+
return `Created sandbox user "${username}"`;
|
|
158
|
+
}
|
|
159
|
+
const reason = (result.stderr || result.errorCode || `exit ${result.code}`).trim().slice(0, 200);
|
|
160
|
+
logger.warn(`[skill-hub] useradd for "${username}" failed; privilege drop unavailable: ${reason}`);
|
|
161
|
+
return `Sandbox user unavailable (${reason})`;
|
|
162
|
+
}
|
|
163
|
+
/** Write/refresh sitecustomize.py so plugin upgrades propagate guard changes. */
|
|
164
|
+
writePythonGuard(logger) {
|
|
165
|
+
if (process.env.SKILL_HUB_PY_GUARD === "false") {
|
|
166
|
+
return "Python audit-hook guard disabled via SKILL_HUB_PY_GUARD=false";
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const guardDir = (0, import_sandboxCapabilities.getPythonGuardDir)(this.storagePath);
|
|
170
|
+
(0, import_fs.mkdirSync)(guardDir, { recursive: true });
|
|
171
|
+
(0, import_fs.writeFileSync)((0, import_path.join)(guardDir, "sitecustomize.py"), import_pythonGuardTemplate.PY_GUARD_SITECUSTOMIZE_SOURCE, "utf-8");
|
|
172
|
+
return "Python audit-hook guard (sitecustomize.py) refreshed";
|
|
173
|
+
} catch (error) {
|
|
174
|
+
logger.warn(`[skill-hub] Failed to write python guard: ${error.message}`);
|
|
175
|
+
return `Python audit-hook guard unavailable (${error.message})`;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
112
178
|
parsePackageWhitelist(config) {
|
|
113
179
|
var _a;
|
|
114
180
|
return (0, import_json_fields.parseJsonText)(((_a = config == null ? void 0 : config.get) == null ? void 0 : _a.call(config, "packageWhitelist")) ?? (config == null ? void 0 : config.packageWhitelist), DEFAULT_WHITELIST);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var pythonGuardTemplate_exports = {};
|
|
28
|
+
__export(pythonGuardTemplate_exports, {
|
|
29
|
+
PY_GUARD_SITECUSTOMIZE_SOURCE: () => PY_GUARD_SITECUSTOMIZE_SOURCE
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(pythonGuardTemplate_exports);
|
|
32
|
+
const PY_GUARD_SITECUSTOMIZE_SOURCE = `"""NocoBase skill-hub sandbox guard (auto-generated; do not edit)."""
|
|
33
|
+
import os
|
|
34
|
+
import sys
|
|
35
|
+
|
|
36
|
+
_BLOCKED_EVENTS = frozenset((
|
|
37
|
+
"os.system",
|
|
38
|
+
"os.fork",
|
|
39
|
+
"os.forkpty",
|
|
40
|
+
"os.exec",
|
|
41
|
+
"os.spawn",
|
|
42
|
+
"os.posix_spawn",
|
|
43
|
+
"subprocess.Popen",
|
|
44
|
+
"_posixsubprocess.fork_exec",
|
|
45
|
+
"ctypes.dlopen",
|
|
46
|
+
"ctypes.dlsym",
|
|
47
|
+
"ctypes.dlsym/handle",
|
|
48
|
+
))
|
|
49
|
+
|
|
50
|
+
_raw_roots = os.environ.get("SKILL_HUB_WRITE_ROOTS", "")
|
|
51
|
+
_WRITE_ROOTS = tuple(
|
|
52
|
+
os.path.realpath(root) for root in _raw_roots.split(os.pathsep) if root
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
_WRITE_FLAGS = (
|
|
56
|
+
os.O_WRONLY
|
|
57
|
+
| os.O_RDWR
|
|
58
|
+
| os.O_APPEND
|
|
59
|
+
| getattr(os, "O_CREAT", 0)
|
|
60
|
+
| getattr(os, "O_TRUNC", 0)
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _is_write_open(mode, flags):
|
|
65
|
+
if isinstance(mode, str):
|
|
66
|
+
return any(ch in mode for ch in ("w", "a", "x", "+"))
|
|
67
|
+
try:
|
|
68
|
+
return bool(int(flags) & _WRITE_FLAGS)
|
|
69
|
+
except (TypeError, ValueError):
|
|
70
|
+
return False
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _is_allowed_write_path(path):
|
|
74
|
+
try:
|
|
75
|
+
resolved = os.path.realpath(os.fspath(path))
|
|
76
|
+
except (TypeError, ValueError):
|
|
77
|
+
# fd-based or exotic targets cannot be path-scoped here
|
|
78
|
+
return True
|
|
79
|
+
for root in _WRITE_ROOTS:
|
|
80
|
+
if resolved == root or resolved.startswith(root + os.sep):
|
|
81
|
+
return True
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _skill_hub_guard(event, args):
|
|
86
|
+
if event in _BLOCKED_EVENTS:
|
|
87
|
+
raise RuntimeError(
|
|
88
|
+
"skill-hub sandbox: blocked audited operation '%s'" % event
|
|
89
|
+
)
|
|
90
|
+
if _WRITE_ROOTS and event == "open" and len(args) >= 3:
|
|
91
|
+
path, mode, flags = args[0], args[1], args[2]
|
|
92
|
+
if path is not None and _is_write_open(mode, flags) and not _is_allowed_write_path(path):
|
|
93
|
+
# PermissionError (an OSError) so import machinery treats a denied
|
|
94
|
+
# bytecode-cache write as best-effort instead of aborting imports.
|
|
95
|
+
raise PermissionError(
|
|
96
|
+
"skill-hub sandbox: write outside allowed roots denied: %r" % (path,)
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
sys.addaudithook(_skill_hub_guard)
|
|
101
|
+
`;
|
|
102
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
+
0 && (module.exports = {
|
|
104
|
+
PY_GUARD_SITECUSTOMIZE_SOURCE
|
|
105
|
+
});
|