plugin-agent-orchestrator 1.0.32 → 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.
- package/README.md +114 -114
- package/dist/externalVersion.js +6 -6
- package/dist/server/collections/skill-executions.js +21 -0
- package/dist/server/plugin.js +132 -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 +98 -24
- 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/client-v2/components/AgentRunsTab.tsx +494 -494
- package/src/client-v2/components/TracingTab.tsx +432 -432
- package/src/client-v2/hooks/useApiRequest.ts +121 -121
- 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 +151 -0
- package/src/server/resources/agent-loop.ts +1 -2
- package/src/server/resources/agent-monitor.ts +482 -482
- 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/ExecutionSpanService.ts +110 -110
- package/src/server/services/FileManager.ts +16 -1
- package/src/server/services/NativeSubAgentObserver.ts +528 -507
- 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 +106 -23
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +51 -6
- package/src/server/utils/skill-settings.ts +1 -0
|
@@ -34,6 +34,7 @@ module.exports = __toCommonJS(NativeSubAgentObserver_exports);
|
|
|
34
34
|
var import_crypto = require("crypto");
|
|
35
35
|
var import_ExecutionSpanService = require("./ExecutionSpanService");
|
|
36
36
|
var import_AgentMemoryContextService = require("./AgentMemoryContextService");
|
|
37
|
+
var import_TokenTracker = require("./TokenTracker");
|
|
37
38
|
var import_ctx_utils = require("../utils/ctx-utils");
|
|
38
39
|
const WRAP_STATE_KEY = Symbol.for("plugin-agent-orchestrator.nativeSubAgentObserver");
|
|
39
40
|
const NATIVE_SOURCE = "native-plugin-ai";
|
|
@@ -65,9 +66,11 @@ class NativeSubAgentObserver {
|
|
|
65
66
|
this.plugin = plugin;
|
|
66
67
|
this.spanService = new import_ExecutionSpanService.ExecutionSpanService(plugin);
|
|
67
68
|
this.memoryService = new import_AgentMemoryContextService.AgentMemoryContextService(plugin);
|
|
69
|
+
this.tokenTracker = new import_TokenTracker.TokenTracker(plugin);
|
|
68
70
|
}
|
|
69
71
|
spanService;
|
|
70
72
|
memoryService;
|
|
73
|
+
tokenTracker;
|
|
71
74
|
install() {
|
|
72
75
|
var _a, _b, _c, _d, _e, _f;
|
|
73
76
|
const aiPlugin = (_b = (_a = this.plugin.app.pm) == null ? void 0 : _a.get) == null ? void 0 : _b.call(_a, "ai");
|
|
@@ -178,6 +181,12 @@ ${memory.context}`
|
|
|
178
181
|
completedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
179
182
|
}
|
|
180
183
|
});
|
|
184
|
+
await this.tokenTracker.estimateAndTrack(
|
|
185
|
+
state.rootSpanId,
|
|
186
|
+
task.question,
|
|
187
|
+
result,
|
|
188
|
+
state.rootMetadata.agentLoopRunId ? Number(state.rootMetadata.agentLoopRunId) : void 0
|
|
189
|
+
);
|
|
181
190
|
return result;
|
|
182
191
|
} catch (error) {
|
|
183
192
|
await this.flushPending(state);
|
|
@@ -188,6 +197,12 @@ ${memory.context}`
|
|
|
188
197
|
failedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
189
198
|
}
|
|
190
199
|
});
|
|
200
|
+
await this.tokenTracker.estimateAndTrack(
|
|
201
|
+
state.rootSpanId,
|
|
202
|
+
task.question,
|
|
203
|
+
errorMessage(error),
|
|
204
|
+
state.rootMetadata.agentLoopRunId ? Number(state.rootMetadata.agentLoopRunId) : void 0
|
|
205
|
+
);
|
|
191
206
|
throw error;
|
|
192
207
|
}
|
|
193
208
|
}
|
|
@@ -319,6 +334,11 @@ ${memory.context}`
|
|
|
319
334
|
messageId: toolCall.messageId
|
|
320
335
|
}
|
|
321
336
|
});
|
|
337
|
+
await this.tokenTracker.estimateAndTrack(
|
|
338
|
+
existing.spanId,
|
|
339
|
+
normalizeString(toolCall.name),
|
|
340
|
+
typeof output === "string" ? output : JSON.stringify(output)
|
|
341
|
+
);
|
|
322
342
|
}
|
|
323
343
|
resolveParentSessionId(task) {
|
|
324
344
|
var _a, _b, _c;
|
|
@@ -0,0 +1,95 @@
|
|
|
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 PlanningPromptService_exports = {};
|
|
28
|
+
__export(PlanningPromptService_exports, {
|
|
29
|
+
PlanningPromptService: () => PlanningPromptService
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(PlanningPromptService_exports);
|
|
32
|
+
const PLAN_KEYWORDS = [
|
|
33
|
+
"k\u1EBF ho\u1EA1ch",
|
|
34
|
+
"l\u1EADp k\u1EBF ho\u1EA1ch",
|
|
35
|
+
"l\xEAn k\u1EBF ho\u1EA1ch",
|
|
36
|
+
"k\u1EBF ho\u1EA1ch h\xF3a",
|
|
37
|
+
"l\u1EADp plan",
|
|
38
|
+
"t\u1EA1o k\u1EBF ho\u1EA1ch",
|
|
39
|
+
"x\xE2y d\u1EF1ng k\u1EBF ho\u1EA1ch",
|
|
40
|
+
"planning",
|
|
41
|
+
"make a plan",
|
|
42
|
+
"create a plan"
|
|
43
|
+
];
|
|
44
|
+
const PLAN_WORD_PATTERN = /\bplan\b/i;
|
|
45
|
+
const PLANNING_PREFIX = `[PLAN MODE] The user's request may need a structured plan. First, analyze the request:
|
|
46
|
+
|
|
47
|
+
STEP 0 \u2014 Evaluate: Does this request truly require 3+ distinct steps (building, creating, deploying, researching, etc.)?
|
|
48
|
+
- NO \u2192 The user mentioned "plan" but this is a simple question or single action. Respond directly WITHOUT calling any agent_loop tools.
|
|
49
|
+
- YES \u2192 Proceed with the plan workflow below.
|
|
50
|
+
|
|
51
|
+
PLAN WORKFLOW (only if truly multi-step):
|
|
52
|
+
1. Call \`agent_loop_start\` with a concrete step-by-step plan. Use small, executable steps with clear planKey values and proper dependsOn chains. Structure the user's goal clearly in the "goal" field.
|
|
53
|
+
2. After the plan is approved, execute each step by calling \`agent_loop_update_step\` with status="running" before the action, then status="succeeded" or "failed" after.
|
|
54
|
+
3. If execution diverges, call \`agent_loop_replan\` to update remaining steps.
|
|
55
|
+
4. When all steps complete, call \`agent_loop_finish\`.
|
|
56
|
+
|
|
57
|
+
User request:
|
|
58
|
+
`;
|
|
59
|
+
class PlanningPromptService {
|
|
60
|
+
static getMessageText(content) {
|
|
61
|
+
if (typeof content === "string") return content;
|
|
62
|
+
if (content && typeof content === "object" && "content" in content) {
|
|
63
|
+
const nested = content.content;
|
|
64
|
+
return typeof nested === "string" ? nested : "";
|
|
65
|
+
}
|
|
66
|
+
return "";
|
|
67
|
+
}
|
|
68
|
+
shouldInjectPlanningPrompt(text) {
|
|
69
|
+
if (!text) return false;
|
|
70
|
+
const normalized = text.toLowerCase();
|
|
71
|
+
if (PLAN_WORD_PATTERN.test(normalized)) return true;
|
|
72
|
+
return PLAN_KEYWORDS.some((keyword) => normalized.includes(keyword));
|
|
73
|
+
}
|
|
74
|
+
getPlanningPrefix() {
|
|
75
|
+
return PLANNING_PREFIX;
|
|
76
|
+
}
|
|
77
|
+
getPlanningInstructions() {
|
|
78
|
+
return PLANNING_PREFIX.replace(/User request:\s*$/, "").trim();
|
|
79
|
+
}
|
|
80
|
+
applyPlanningContext(messages) {
|
|
81
|
+
const shouldInject = messages.some(
|
|
82
|
+
(message) => message.role === "user" && this.shouldInjectPlanningPrompt(PlanningPromptService.getMessageText(message.content))
|
|
83
|
+
);
|
|
84
|
+
if (!shouldInject) return false;
|
|
85
|
+
messages.unshift({
|
|
86
|
+
role: "system",
|
|
87
|
+
content: { type: "text", content: this.getPlanningInstructions() }
|
|
88
|
+
});
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
93
|
+
0 && (module.exports = {
|
|
94
|
+
PlanningPromptService
|
|
95
|
+
});
|
|
@@ -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);
|