@tangle-network/agent-runtime 9.0.11 → 9.0.13
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/CHANGELOG.md +12 -0
- package/dist/{activation-B_10QJ3D.js → activation-CB3FgbhP.js} +2 -2
- package/dist/{activation-B_10QJ3D.js.map → activation-CB3FgbhP.js.map} +1 -1
- package/dist/{activation-emYXDX7T.d.ts → activation-druKjqeg.d.ts} +2 -2
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +2 -2
- package/dist/{coordination-manager-BUct8qFe.js → coordination-manager-DGLireA0.js} +4 -4
- package/dist/{coordination-manager-BUct8qFe.js.map → coordination-manager-DGLireA0.js.map} +1 -1
- package/dist/{delegate-DHtsLCyr.js → delegate-DkmUWRTv.js} +2 -2
- package/dist/{delegate-DHtsLCyr.js.map → delegate-DkmUWRTv.js.map} +1 -1
- package/dist/durable.d.ts +1 -1
- package/dist/durable.js +8 -8
- package/dist/{graph-D6KJ6wxN.js → graph-RSKyd-_g.js} +423 -126
- package/dist/graph-RSKyd-_g.js.map +1 -0
- package/dist/{improve-CUqmH45C.js → improve-D77Hq7bo.js} +5 -5
- package/dist/{improve-CUqmH45C.js.map → improve-D77Hq7bo.js.map} +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +9 -9
- package/dist/intelligence.d.ts +2 -2
- package/dist/intelligence.js +4 -4
- package/dist/kernel.d.ts +2 -2
- package/dist/kernel.js +13 -13
- package/dist/{loop-runner-CCeBurqG.js → loop-runner-B-k9u4Vs.js} +2 -2
- package/dist/{loop-runner-CCeBurqG.js.map → loop-runner-B-k9u4Vs.js.map} +1 -1
- package/dist/{loop-runner-Q9nlHcEH.d.ts → loop-runner-DN8kPiwC.d.ts} +2 -2
- package/dist/loop-runner-bin.d.ts +1 -1
- package/dist/loop-runner-bin.js +1 -1
- package/dist/mcp/bin.js +3 -3
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +5 -5
- package/dist/profiles.d.ts +1 -1
- package/dist/{provision-supervisor-CNTBbuDV.js → provision-supervisor-h3xHBR4a.js} +4 -4
- package/dist/{provision-supervisor-CNTBbuDV.js.map → provision-supervisor-h3xHBR4a.js.map} +1 -1
- package/dist/{run-cancellation-Cy9ie5cQ.js → run-cancellation-BbM8qZQS.js} +2 -2
- package/dist/{run-cancellation-Cy9ie5cQ.js.map → run-cancellation-BbM8qZQS.js.map} +1 -1
- package/dist/{run-state-C0nr4O7P.js → run-state-eDCDYXXQ.js} +5 -5
- package/dist/{run-state-C0nr4O7P.js.map → run-state-eDCDYXXQ.js.map} +1 -1
- package/dist/{runtime-C5yB7Vkk.js → runtime-lQ7NmAaw.js} +184 -15
- package/dist/runtime-lQ7NmAaw.js.map +1 -0
- package/dist/{search-executor-BgEY5MKQ.js → search-executor-CIRcqZor.js} +3 -3
- package/dist/{search-executor-BgEY5MKQ.js.map → search-executor-CIRcqZor.js.map} +1 -1
- package/dist/{server-X67SS4r8.js → server-CCqBzpYt.js} +3 -3
- package/dist/{server-X67SS4r8.js.map → server-CCqBzpYt.js.map} +1 -1
- package/dist/{settle-record-tgpwk6QF.js → settle-record-BtI6hieb.js} +2 -2
- package/dist/{settle-record-tgpwk6QF.js.map → settle-record-BtI6hieb.js.map} +1 -1
- package/dist/{structural-rollout-BSzN8a0T.js → structural-rollout-BpuG0cEv.js} +3 -3
- package/dist/{structural-rollout-BSzN8a0T.js.map → structural-rollout-BpuG0cEv.js.map} +1 -1
- package/dist/{supervise-RJG2ruTF.js → supervise-CPm-6Kqg.js} +5 -5
- package/dist/{supervise-RJG2ruTF.js.map → supervise-CPm-6Kqg.js.map} +1 -1
- package/dist/{supervision-discovery-Hekh50FG.js → supervision-discovery-DqT5WUUN.js} +2 -2
- package/dist/{supervision-discovery-Hekh50FG.js.map → supervision-discovery-DqT5WUUN.js.map} +1 -1
- package/dist/{supervisor-DbtgkJyr.js → supervisor-CtS12u8Y.js} +2 -2
- package/dist/{supervisor-DbtgkJyr.js.map → supervisor-CtS12u8Y.js.map} +1 -1
- package/dist/{supervisor-accounting-us4w7tG6.js → supervisor-accounting-CMdpc2LW.js} +4 -16
- package/dist/supervisor-accounting-CMdpc2LW.js.map +1 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +11 -11
- package/dist/{top-web-xSGEh02D.js → top-web-JnHgbTEg.js} +5 -5
- package/dist/{top-web-xSGEh02D.js.map → top-web-JnHgbTEg.js.map} +1 -1
- package/dist/tui/bin.js +3 -3
- package/dist/tui/index.d.ts +1 -1
- package/dist/tui/index.js +2 -2
- package/dist/{types-DMqr8Tmx.d.ts → types-DQkRr7Iy.d.ts} +145 -3
- package/package.json +2 -2
- package/dist/graph-D6KJ6wxN.js.map +0 -1
- package/dist/runtime-C5yB7Vkk.js.map +0 -1
- package/dist/supervisor-accounting-us4w7tG6.js.map +0 -1
|
@@ -2,24 +2,381 @@ import { i as ConfigError, m as ValidationError } from "./errors-DG8yufqX.js";
|
|
|
2
2
|
import { t as composeRuntimeHooks } from "./runtime-hooks-CsW2pfFk.js";
|
|
3
3
|
import { v as harnessRunsAgent } from "./model-policy-CZwhXp8O.js";
|
|
4
4
|
import { A as portableContinuationPolicySchema, C as boxCheckOutputReader, D as stopCheckBoxForRecovery, E as retainIsolatedCheckOutputs, O as stopCheckProcess, T as prepareIsolatedCheckOutputs, k as validateIsolatedCheckOutputs, w as localCheckOutputReader } from "./graph-registry-BbZN5deo.js";
|
|
5
|
-
import { Qr as runtimeFlowBrain, Xr as runtimeCheckFactory } from "./supervisor-accounting-
|
|
6
|
-
import { C as journaledGateDecider, D as graphRootProfile, F as kernelPromptRegistry, O as validateGraphTopology, S as flowRootProfile, T as validateFlowGraph, b as GraphRouteError, i as profileGuidanceComposer, r as superviseWithTestBrain, t as supervise, w as routeFlowOf, x as createFlowRun, y as createGraphOverlay } from "./supervise-
|
|
7
|
-
import { f as createFileRunContext, m as withRunContext, p as createInMemoryRunContext } from "./coordination-manager-
|
|
5
|
+
import { Qr as runtimeFlowBrain, Xr as runtimeCheckFactory } from "./supervisor-accounting-CMdpc2LW.js";
|
|
6
|
+
import { C as journaledGateDecider, D as graphRootProfile, F as kernelPromptRegistry, O as validateGraphTopology, S as flowRootProfile, T as validateFlowGraph, b as GraphRouteError, i as profileGuidanceComposer, r as superviseWithTestBrain, t as supervise, w as routeFlowOf, x as createFlowRun, y as createGraphOverlay } from "./supervise-CPm-6Kqg.js";
|
|
7
|
+
import { f as createFileRunContext, m as withRunContext, p as createInMemoryRunContext } from "./coordination-manager-DGLireA0.js";
|
|
8
8
|
import { n as detachedSnapshot } from "./snapshot-BKI_w3JD.js";
|
|
9
9
|
import { a as captureMaterializedWorkspace, c as scanMaterializedWorkspaceManifest, n as WorkspaceScanLimitError, p as canonicalCandidateBytes$1 } from "./output-artifacts-AqkZ8lO2.js";
|
|
10
10
|
import { N as contentAddress, v as retainedExecutorContext } from "./failure-cause-WifbY42M.js";
|
|
11
11
|
import { a as readCommittedJsonLines, c as appendCommittedJsonLine, m as runAbortable, s as withJsonlFileLock } from "./jsonl-file-BhuxeejY.js";
|
|
12
|
-
import { Nt as defaultRunCommand, Pt as runSettledCommand } from "./supervisor-
|
|
12
|
+
import { Nt as defaultRunCommand, Pt as runSettledCommand } from "./supervisor-CtS12u8Y.js";
|
|
13
13
|
import { o as armDeadlineTimer } from "./deadline-CySmJ_0S.js";
|
|
14
14
|
import { agentCandidateArtifactRefSchema, agentCandidateWorkspaceManifestMaterialSchema, canonicalCandidateDigest, sha256DigestSchema } from "@tangle-network/agent-interface";
|
|
15
15
|
import { z } from "zod";
|
|
16
16
|
import { randomUUID } from "node:crypto";
|
|
17
|
+
import { setTimeout } from "node:timers/promises";
|
|
17
18
|
import { constants, mkdirSync, realpathSync } from "node:fs";
|
|
18
19
|
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
19
20
|
import { execFile, spawn } from "node:child_process";
|
|
20
|
-
import { chmod, cp, lstat, mkdir, mkdtemp, open, opendir, readlink, realpath, rm, stat, symlink, writeFile } from "node:fs/promises";
|
|
21
|
+
import { chmod, cp, lstat, mkdir, mkdtemp, open, opendir, readFile, readlink, realpath, rm, rmdir, stat, symlink, writeFile } from "node:fs/promises";
|
|
21
22
|
import { promisify } from "node:util";
|
|
22
23
|
import { tmpdir } from "node:os";
|
|
24
|
+
// src/runtime/isolated-check-cpu.ts
|
|
25
|
+
const cgroupRoot = "/sys/fs/cgroup";
|
|
26
|
+
async function optionalFile(path) {
|
|
27
|
+
try {
|
|
28
|
+
return await readFile(path, "utf8");
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function optionalCounter(match) {
|
|
34
|
+
const value = match ? Number(match[1]) : NaN;
|
|
35
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
36
|
+
}
|
|
37
|
+
async function resources(path) {
|
|
38
|
+
const pressure = await optionalFile(join(path, "cpu.pressure"));
|
|
39
|
+
const ancestors = [];
|
|
40
|
+
let current = dirname(path);
|
|
41
|
+
let complete = false;
|
|
42
|
+
for (let depth = 0; depth < 32; depth++) {
|
|
43
|
+
const [identity, quota, text] = await Promise.all([
|
|
44
|
+
stat(current).catch(() => null),
|
|
45
|
+
optionalFile(join(current, "cpu.max")),
|
|
46
|
+
optionalFile(join(current, "cpu.stat"))
|
|
47
|
+
]);
|
|
48
|
+
ancestors.push({
|
|
49
|
+
cgroupPath: current,
|
|
50
|
+
inode: identity?.ino ?? null,
|
|
51
|
+
cpuMax: quota && /^(max|[0-9]+) [1-9][0-9]*$/.test(quota.trim()) ? quota.trim() : null,
|
|
52
|
+
throttledUsec: optionalCounter(/^throttled_usec ([0-9]+)$/m.exec(text ?? ""))
|
|
53
|
+
});
|
|
54
|
+
if (current === cgroupRoot) {
|
|
55
|
+
complete = true;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
current = dirname(current);
|
|
59
|
+
if (!current.startsWith(`${cgroupRoot}/`) && current !== cgroupRoot) break;
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
runnableWaitSomeUsec: optionalCounter(/^some [^\n]*total=([0-9]+)\s*$/m.exec(pressure ?? "")),
|
|
63
|
+
ancestors,
|
|
64
|
+
ancestorsComplete: complete
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const sessions = /* @__PURE__ */ new WeakMap();
|
|
68
|
+
async function counters(path) {
|
|
69
|
+
const text = await readFile(join(path, "cpu.stat"), "utf8");
|
|
70
|
+
const counter = (key) => {
|
|
71
|
+
const match = new RegExp(`^${key} ([0-9]+)$`, "m").exec(text);
|
|
72
|
+
const value = match ? Number(match[1]) : NaN;
|
|
73
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new Error(`Invalid CPU counter ${key}`);
|
|
74
|
+
return value;
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
usageUsec: counter("usage_usec"),
|
|
78
|
+
userUsec: counter("user_usec"),
|
|
79
|
+
systemUsec: counter("system_usec"),
|
|
80
|
+
resources: await resources(path)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
async function emptyGroup(state) {
|
|
84
|
+
const path = state.identity.cgroupPath;
|
|
85
|
+
if ((await stat(path)).ino !== state.identity.inode) throw new Error("CPU cgroup identity changed");
|
|
86
|
+
if (!/^populated 0$/m.test(await readFile(join(path, "cgroup.events"), "utf8"))) throw new Error("CPU cgroup still contains processes");
|
|
87
|
+
}
|
|
88
|
+
/** Requires delegation from the execution placement; never creates a host-wide scope or fallback. */
|
|
89
|
+
async function createIsolatedCheckCpuSession() {
|
|
90
|
+
let path;
|
|
91
|
+
let identity;
|
|
92
|
+
try {
|
|
93
|
+
if (process.platform !== "linux") throw new Error("CPU accounting requires delegated Linux cgroup v2");
|
|
94
|
+
const own = /^0::(\/[^\n]*)$/m.exec(await readFile("/proc/self/cgroup", "utf8"))?.[1];
|
|
95
|
+
if (!own || own.split("/").includes("..")) throw new Error("Current cgroup v2 identity unavailable");
|
|
96
|
+
const id = randomUUID();
|
|
97
|
+
const target = join(cgroupRoot, own.slice(1), `runtime-check-${id}`);
|
|
98
|
+
await mkdir(target);
|
|
99
|
+
path = target;
|
|
100
|
+
identity = {
|
|
101
|
+
source: "cgroup-v2",
|
|
102
|
+
boundary: "after-membership-through-tree-exit",
|
|
103
|
+
sessionId: id,
|
|
104
|
+
cgroupPath: path,
|
|
105
|
+
inode: (await stat(path)).ino
|
|
106
|
+
};
|
|
107
|
+
await counters(path);
|
|
108
|
+
await writeFile(join(path, "cgroup.kill"), "1");
|
|
109
|
+
const state = {
|
|
110
|
+
identity,
|
|
111
|
+
busy: false,
|
|
112
|
+
closed: false
|
|
113
|
+
};
|
|
114
|
+
const session = Object.freeze({
|
|
115
|
+
id,
|
|
116
|
+
cgroupPath: path,
|
|
117
|
+
async close() {
|
|
118
|
+
if (state.closed) return;
|
|
119
|
+
if (state.busy) throw new Error("CPU session has an active check");
|
|
120
|
+
return state.closing ??= (async () => {
|
|
121
|
+
try {
|
|
122
|
+
await emptyGroup(state);
|
|
123
|
+
await rmdir(target);
|
|
124
|
+
state.closed = true;
|
|
125
|
+
} finally {
|
|
126
|
+
state.closing = void 0;
|
|
127
|
+
}
|
|
128
|
+
})();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
sessions.set(session, state);
|
|
132
|
+
return {
|
|
133
|
+
succeeded: true,
|
|
134
|
+
value: session
|
|
135
|
+
};
|
|
136
|
+
} catch (error) {
|
|
137
|
+
let recoveryCgroupPath = path;
|
|
138
|
+
if (path && identity) try {
|
|
139
|
+
if ((await stat(path)).ino === identity.inode) {
|
|
140
|
+
await rmdir(path);
|
|
141
|
+
recoveryCgroupPath = void 0;
|
|
142
|
+
}
|
|
143
|
+
} catch {}
|
|
144
|
+
return {
|
|
145
|
+
succeeded: false,
|
|
146
|
+
reason: "unavailable",
|
|
147
|
+
diagnostic: String(error),
|
|
148
|
+
...recoveryCgroupPath ? { recoveryCgroupPath } : {}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Package-internal lease: the caller cannot manufacture a resource identity. */
|
|
153
|
+
async function acquireIsolatedCheckCpu(session) {
|
|
154
|
+
const state = sessions.get(session);
|
|
155
|
+
if (!state || state.closed || state.closing || state.busy) throw new Error("CPU session is unavailable or busy");
|
|
156
|
+
state.busy = true;
|
|
157
|
+
let before;
|
|
158
|
+
try {
|
|
159
|
+
await emptyGroup(state);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
state.busy = false;
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
async start(pids) {
|
|
166
|
+
if (before) throw new Error("CPU workload already started");
|
|
167
|
+
if ((await stat(state.identity.cgroupPath)).ino !== state.identity.inode) throw new Error("CPU cgroup identity changed");
|
|
168
|
+
for (const pid of new Set(pids)) {
|
|
169
|
+
if (!Number.isSafeInteger(pid) || pid < 1) throw new Error("Invalid protected process identity");
|
|
170
|
+
await writeFile(join(state.identity.cgroupPath, "cgroup.procs"), String(pid));
|
|
171
|
+
}
|
|
172
|
+
before = await counters(state.identity.cgroupPath);
|
|
173
|
+
},
|
|
174
|
+
async kill() {
|
|
175
|
+
if ((await stat(state.identity.cgroupPath)).ino !== state.identity.inode) throw new Error("CPU cgroup identity changed");
|
|
176
|
+
await writeFile(join(state.identity.cgroupPath, "cgroup.kill"), "1");
|
|
177
|
+
},
|
|
178
|
+
async finish(remainingMs) {
|
|
179
|
+
try {
|
|
180
|
+
if (!before) throw new Error("Workload was not released under CPU accounting");
|
|
181
|
+
const deadline = performance.now() + Math.max(0, remainingMs);
|
|
182
|
+
while (true) {
|
|
183
|
+
if ((await stat(state.identity.cgroupPath)).ino !== state.identity.inode) throw new Error("CPU cgroup identity changed");
|
|
184
|
+
if (/^populated 0$/m.test(await readFile(join(state.identity.cgroupPath, "cgroup.events"), "utf8"))) break;
|
|
185
|
+
if (performance.now() >= deadline) throw new Error("CPU cgroup still contains processes at the check deadline");
|
|
186
|
+
await setTimeout(Math.min(10, Math.max(1, deadline - performance.now())));
|
|
187
|
+
}
|
|
188
|
+
const after = await counters(state.identity.cgroupPath);
|
|
189
|
+
if ([
|
|
190
|
+
"usageUsec",
|
|
191
|
+
"userUsec",
|
|
192
|
+
"systemUsec"
|
|
193
|
+
].some((key) => after[key] < before[key])) throw new Error("CPU counters moved backwards");
|
|
194
|
+
return {
|
|
195
|
+
...state.identity,
|
|
196
|
+
available: true,
|
|
197
|
+
before,
|
|
198
|
+
after
|
|
199
|
+
};
|
|
200
|
+
} catch (error) {
|
|
201
|
+
return {
|
|
202
|
+
...state.identity,
|
|
203
|
+
available: false,
|
|
204
|
+
diagnostic: String(error)
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
release() {
|
|
209
|
+
state.busy = false;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
// src/runtime/isolated-checker-process.ts
|
|
214
|
+
function executeIsolatedCheckProcess(args, timeoutMs, limit, signal, cpu) {
|
|
215
|
+
return new Promise((done) => {
|
|
216
|
+
const began = performance.now();
|
|
217
|
+
const gatedExec = "import os,sys; os.write(3,b\"R\\n\"); permit=os.read(4,1); os.close(4); (os.execve(sys.argv[1],sys.argv[1:],{}) if permit==b\"1\" else os._exit(125))";
|
|
218
|
+
const argv = [
|
|
219
|
+
"/usr/bin/bwrap",
|
|
220
|
+
"--json-status-fd",
|
|
221
|
+
"3",
|
|
222
|
+
...cpu ? ["--unshare-user", "--disable-userns"] : [],
|
|
223
|
+
...args
|
|
224
|
+
];
|
|
225
|
+
const child = spawn(cpu ? "/usr/bin/python3" : argv[0], cpu ? [
|
|
226
|
+
"-I",
|
|
227
|
+
"-c",
|
|
228
|
+
gatedExec,
|
|
229
|
+
...argv
|
|
230
|
+
] : argv.slice(1), {
|
|
231
|
+
env: {},
|
|
232
|
+
stdio: cpu ? [
|
|
233
|
+
"ignore",
|
|
234
|
+
"pipe",
|
|
235
|
+
"pipe",
|
|
236
|
+
"pipe",
|
|
237
|
+
"pipe"
|
|
238
|
+
] : [
|
|
239
|
+
"ignore",
|
|
240
|
+
"pipe",
|
|
241
|
+
"pipe",
|
|
242
|
+
"pipe"
|
|
243
|
+
],
|
|
244
|
+
detached: true
|
|
245
|
+
});
|
|
246
|
+
const stdout = [];
|
|
247
|
+
const stderr = [];
|
|
248
|
+
let bytes = 0;
|
|
249
|
+
let status = "";
|
|
250
|
+
let failure;
|
|
251
|
+
let cpuError;
|
|
252
|
+
let cpuStarting;
|
|
253
|
+
let cpuStopping;
|
|
254
|
+
const release = cpu ? child.stdio[4] : void 0;
|
|
255
|
+
const killProcessGroup = () => {
|
|
256
|
+
if (child.pid && child.exitCode === null && child.signalCode === null) try {
|
|
257
|
+
process.kill(-child.pid, "SIGKILL");
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if (error.code !== "ESRCH") child.kill("SIGKILL");
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
const stop = (reason) => {
|
|
263
|
+
failure ??= reason;
|
|
264
|
+
if (!cpu) return killProcessGroup();
|
|
265
|
+
cpuStopping ??= (async () => {
|
|
266
|
+
await cpuStarting;
|
|
267
|
+
try {
|
|
268
|
+
await cpu.kill();
|
|
269
|
+
} catch (error) {
|
|
270
|
+
cpuError ??= String(error);
|
|
271
|
+
} finally {
|
|
272
|
+
killProcessGroup();
|
|
273
|
+
}
|
|
274
|
+
})();
|
|
275
|
+
};
|
|
276
|
+
const clearTimer = armDeadlineTimer(timeoutMs, () => stop("timeout"), true);
|
|
277
|
+
const cancel = () => stop("cancelled");
|
|
278
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
279
|
+
if (signal?.aborted) cancel();
|
|
280
|
+
const capture = (target) => (chunk) => {
|
|
281
|
+
const remaining = Math.max(0, limit - bytes);
|
|
282
|
+
bytes += chunk.length;
|
|
283
|
+
if (remaining) target.push(chunk.subarray(0, remaining));
|
|
284
|
+
if (bytes > limit) stop("output-limit");
|
|
285
|
+
};
|
|
286
|
+
child.stdio[3]?.on("data", (chunk) => {
|
|
287
|
+
if (status.length + chunk.length > 16384) {
|
|
288
|
+
stop("output-limit");
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
status += chunk.toString();
|
|
292
|
+
if (cpu && !cpuStarting && !failure && child.pid && status.startsWith("R\n")) cpuStarting = (async () => {
|
|
293
|
+
try {
|
|
294
|
+
await cpu.start([child.pid]);
|
|
295
|
+
if (!failure && child.exitCode === null && child.signalCode === null) release?.end("1");
|
|
296
|
+
} catch (error) {
|
|
297
|
+
cpuError = String(error);
|
|
298
|
+
stop("refused");
|
|
299
|
+
}
|
|
300
|
+
})();
|
|
301
|
+
});
|
|
302
|
+
release?.on("error", (error) => {
|
|
303
|
+
cpuError = String(error);
|
|
304
|
+
stop("refused");
|
|
305
|
+
});
|
|
306
|
+
child.once("exit", () => release?.destroy());
|
|
307
|
+
child.stdout.on("data", capture(stdout));
|
|
308
|
+
child.stderr.on("data", capture(stderr));
|
|
309
|
+
let spawnError;
|
|
310
|
+
child.on("error", (error) => {
|
|
311
|
+
spawnError = error;
|
|
312
|
+
});
|
|
313
|
+
child.on("close", async (code) => {
|
|
314
|
+
const cpuCommandWallMs = cpu ? performance.now() - began : void 0;
|
|
315
|
+
await cpuStarting;
|
|
316
|
+
await cpuStopping;
|
|
317
|
+
const cpuEvidence = await cpu?.finish(timeoutMs - (performance.now() - began));
|
|
318
|
+
clearTimer();
|
|
319
|
+
signal?.removeEventListener("abort", cancel);
|
|
320
|
+
const outBytes = Buffer.concat(stdout);
|
|
321
|
+
const errBytes = Buffer.concat(stderr);
|
|
322
|
+
const out = outBytes.toString();
|
|
323
|
+
const err = errBytes.toString();
|
|
324
|
+
const command = {
|
|
325
|
+
wallMs: cpuCommandWallMs ?? performance.now() - began,
|
|
326
|
+
stdoutBase64: outBytes.toString("base64"),
|
|
327
|
+
stderrBase64: errBytes.toString("base64"),
|
|
328
|
+
outputComplete: bytes <= limit,
|
|
329
|
+
...cpuEvidence ? { cpu: cpuEvidence } : {}
|
|
330
|
+
};
|
|
331
|
+
const evidence = {
|
|
332
|
+
stdout: out,
|
|
333
|
+
stderr: err,
|
|
334
|
+
exitCode: code,
|
|
335
|
+
command
|
|
336
|
+
};
|
|
337
|
+
const executed = status.split("\n").some((line) => {
|
|
338
|
+
try {
|
|
339
|
+
const record = JSON.parse(line);
|
|
340
|
+
return record !== null && typeof record === "object" && "exit-code" in record && typeof record["exit-code"] === "number" && record["exit-code"] === code;
|
|
341
|
+
} catch {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
if (failure) done({
|
|
346
|
+
succeeded: false,
|
|
347
|
+
reason: failure,
|
|
348
|
+
diagnostic: cpuError || err || failure,
|
|
349
|
+
...evidence
|
|
350
|
+
});
|
|
351
|
+
else if (spawnError) done({
|
|
352
|
+
succeeded: false,
|
|
353
|
+
reason: "refused",
|
|
354
|
+
diagnostic: String(spawnError),
|
|
355
|
+
...evidence
|
|
356
|
+
});
|
|
357
|
+
else if (!executed) done({
|
|
358
|
+
succeeded: false,
|
|
359
|
+
reason: "refused",
|
|
360
|
+
diagnostic: err || "Bubblewrap did not confirm command execution",
|
|
361
|
+
...evidence
|
|
362
|
+
});
|
|
363
|
+
else if (code !== 0) done({
|
|
364
|
+
succeeded: false,
|
|
365
|
+
reason: "failed",
|
|
366
|
+
diagnostic: err || out || `Check exited ${code}`,
|
|
367
|
+
...evidence
|
|
368
|
+
});
|
|
369
|
+
else done({
|
|
370
|
+
succeeded: true,
|
|
371
|
+
value: {
|
|
372
|
+
stdout: out,
|
|
373
|
+
stderr: err
|
|
374
|
+
},
|
|
375
|
+
command
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
}
|
|
23
380
|
// src/runtime/isolated-checker.ts
|
|
24
381
|
const toolchains = [
|
|
25
382
|
"/usr",
|
|
@@ -51,6 +408,27 @@ const contains = (parent, child) => {
|
|
|
51
408
|
* caller's responsibility; the resource-limited placement requirement still applies.
|
|
52
409
|
*/
|
|
53
410
|
async function runIsolatedCheck(options) {
|
|
411
|
+
if (!options.cpuSession) return runProtectedCheck(options);
|
|
412
|
+
if (options.box) return {
|
|
413
|
+
succeeded: false,
|
|
414
|
+
reason: "refused",
|
|
415
|
+
diagnostic: "CPU sessions require local delegated execution"
|
|
416
|
+
};
|
|
417
|
+
let cpu;
|
|
418
|
+
try {
|
|
419
|
+
cpu = await acquireIsolatedCheckCpu(options.cpuSession);
|
|
420
|
+
return await runProtectedCheck(options, cpu);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
return {
|
|
423
|
+
succeeded: false,
|
|
424
|
+
reason: "refused",
|
|
425
|
+
diagnostic: String(error)
|
|
426
|
+
};
|
|
427
|
+
} finally {
|
|
428
|
+
cpu?.release();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
async function runProtectedCheck(options, cpu) {
|
|
54
432
|
if (options.box) return runInBox(options, options.box);
|
|
55
433
|
let scratch;
|
|
56
434
|
let outputs;
|
|
@@ -82,9 +460,9 @@ async function runIsolatedCheck(options) {
|
|
|
82
460
|
if (contains(tree, scratch)) throw new Error("Temporary storage must be outside the input tree");
|
|
83
461
|
const copy = join(scratch, "tree");
|
|
84
462
|
await copyBoundedTree(tree, copy, maxInputBytes, maxInputEntries);
|
|
85
|
-
const input = retention ? await scanMaterializedWorkspaceManifest(copy) : void 0;
|
|
463
|
+
const input = retention || cpu ? await scanMaterializedWorkspaceManifest(copy) : void 0;
|
|
86
464
|
if (retention && input) assertOutputPathsUnoccupied(retention.paths, input);
|
|
87
|
-
result = await
|
|
465
|
+
result = await executeIsolatedCheckProcess([
|
|
88
466
|
"--unshare-all",
|
|
89
467
|
"--die-with-parent",
|
|
90
468
|
"--new-session",
|
|
@@ -119,7 +497,8 @@ async function runIsolatedCheck(options) {
|
|
|
119
497
|
tree,
|
|
120
498
|
"--",
|
|
121
499
|
...options.command
|
|
122
|
-
], timeoutMs, maxOutputBytes, options.signal);
|
|
500
|
+
], timeoutMs, maxOutputBytes, options.signal, cpu);
|
|
501
|
+
if (cpu && input && result.command) result.command.inputDigest = canonicalCandidateDigest(input);
|
|
123
502
|
if (retention && input && (result.succeeded || result.reason !== "refused")) {
|
|
124
503
|
outputs = await retainIsolatedCheckOutputs(retention, canonicalCandidateDigest(input), localCheckOutputReader(copy));
|
|
125
504
|
if (outputs.status === "incomplete") outputs = {
|
|
@@ -537,121 +916,6 @@ async function executeInBox(box, command, timeoutMs, limit, env, signal) {
|
|
|
537
916
|
}
|
|
538
917
|
});
|
|
539
918
|
}
|
|
540
|
-
function execute(args, timeoutMs, limit, signal) {
|
|
541
|
-
return new Promise((done) => {
|
|
542
|
-
const began = performance.now();
|
|
543
|
-
const child = spawn("/usr/bin/bwrap", [
|
|
544
|
-
"--json-status-fd",
|
|
545
|
-
"3",
|
|
546
|
-
...args
|
|
547
|
-
], {
|
|
548
|
-
env: {},
|
|
549
|
-
stdio: [
|
|
550
|
-
"ignore",
|
|
551
|
-
"pipe",
|
|
552
|
-
"pipe",
|
|
553
|
-
"pipe"
|
|
554
|
-
],
|
|
555
|
-
detached: true
|
|
556
|
-
});
|
|
557
|
-
const stdout = [];
|
|
558
|
-
const stderr = [];
|
|
559
|
-
let bytes = 0;
|
|
560
|
-
let status = "";
|
|
561
|
-
let failure;
|
|
562
|
-
const stop = (reason) => {
|
|
563
|
-
failure ??= reason;
|
|
564
|
-
if (child.pid) try {
|
|
565
|
-
process.kill(-child.pid, "SIGKILL");
|
|
566
|
-
} catch (error) {
|
|
567
|
-
if (error.code !== "ESRCH") child.kill("SIGKILL");
|
|
568
|
-
}
|
|
569
|
-
};
|
|
570
|
-
const clearTimer = armDeadlineTimer(timeoutMs, () => stop("timeout"), true);
|
|
571
|
-
const cancel = () => stop("cancelled");
|
|
572
|
-
signal?.addEventListener("abort", cancel, { once: true });
|
|
573
|
-
if (signal?.aborted) cancel();
|
|
574
|
-
const capture = (target) => (chunk) => {
|
|
575
|
-
const remaining = Math.max(0, limit - bytes);
|
|
576
|
-
bytes += chunk.length;
|
|
577
|
-
if (remaining) target.push(chunk.subarray(0, remaining));
|
|
578
|
-
if (bytes > limit) stop("output-limit");
|
|
579
|
-
};
|
|
580
|
-
child.stdio[3]?.on("data", (chunk) => {
|
|
581
|
-
if (status.length + chunk.length > 16384) {
|
|
582
|
-
stop("output-limit");
|
|
583
|
-
return;
|
|
584
|
-
}
|
|
585
|
-
status += chunk.toString();
|
|
586
|
-
});
|
|
587
|
-
child.stdout.on("data", capture(stdout));
|
|
588
|
-
child.stderr.on("data", capture(stderr));
|
|
589
|
-
let spawnError;
|
|
590
|
-
child.on("error", (error) => {
|
|
591
|
-
spawnError = error;
|
|
592
|
-
});
|
|
593
|
-
child.on("close", (code) => {
|
|
594
|
-
clearTimer();
|
|
595
|
-
signal?.removeEventListener("abort", cancel);
|
|
596
|
-
const outBytes = Buffer.concat(stdout);
|
|
597
|
-
const errBytes = Buffer.concat(stderr);
|
|
598
|
-
const out = outBytes.toString();
|
|
599
|
-
const err = errBytes.toString();
|
|
600
|
-
const command = {
|
|
601
|
-
wallMs: performance.now() - began,
|
|
602
|
-
stdoutBase64: outBytes.toString("base64"),
|
|
603
|
-
stderrBase64: errBytes.toString("base64"),
|
|
604
|
-
outputComplete: bytes <= limit
|
|
605
|
-
};
|
|
606
|
-
const evidence = {
|
|
607
|
-
stdout: out,
|
|
608
|
-
stderr: err,
|
|
609
|
-
exitCode: code,
|
|
610
|
-
command
|
|
611
|
-
};
|
|
612
|
-
const executed = status.split("\n").some((line) => {
|
|
613
|
-
try {
|
|
614
|
-
const record = JSON.parse(line);
|
|
615
|
-
return record !== null && typeof record === "object" && "exit-code" in record && typeof record["exit-code"] === "number" && record["exit-code"] === code;
|
|
616
|
-
} catch {
|
|
617
|
-
return false;
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
if (failure) done({
|
|
621
|
-
succeeded: false,
|
|
622
|
-
reason: failure,
|
|
623
|
-
diagnostic: err || failure,
|
|
624
|
-
...evidence
|
|
625
|
-
});
|
|
626
|
-
else if (spawnError) done({
|
|
627
|
-
succeeded: false,
|
|
628
|
-
reason: "refused",
|
|
629
|
-
diagnostic: String(spawnError),
|
|
630
|
-
...evidence
|
|
631
|
-
});
|
|
632
|
-
else if (!executed) done({
|
|
633
|
-
succeeded: false,
|
|
634
|
-
reason: "refused",
|
|
635
|
-
diagnostic: err || "Bubblewrap did not confirm command execution",
|
|
636
|
-
...evidence
|
|
637
|
-
});
|
|
638
|
-
else if (code !== 0) done({
|
|
639
|
-
succeeded: false,
|
|
640
|
-
reason: "failed",
|
|
641
|
-
diagnostic: err || out || `Check exited ${code}`,
|
|
642
|
-
...evidence
|
|
643
|
-
});
|
|
644
|
-
else done({
|
|
645
|
-
succeeded: true,
|
|
646
|
-
value: {
|
|
647
|
-
stdout: out,
|
|
648
|
-
stderr: err
|
|
649
|
-
},
|
|
650
|
-
command
|
|
651
|
-
});
|
|
652
|
-
});
|
|
653
|
-
});
|
|
654
|
-
}
|
|
655
919
|
const executeFile = promisify(execFile);
|
|
656
920
|
/** GNU tools traverse relative to open directories, including trees deeper than PATH_MAX.
|
|
657
921
|
* Recursive chmod ignores encountered symlinks; rm never follows their targets. */
|
|
@@ -770,11 +1034,44 @@ const isolatedCheckBoxEvidenceSchema = z.object({
|
|
|
770
1034
|
message: "Check input digest does not match its manifest"
|
|
771
1035
|
});
|
|
772
1036
|
});
|
|
1037
|
+
const cpuCounterSchema = z.object({
|
|
1038
|
+
usageUsec: z.number().int().nonnegative(),
|
|
1039
|
+
userUsec: z.number().int().nonnegative(),
|
|
1040
|
+
systemUsec: z.number().int().nonnegative(),
|
|
1041
|
+
resources: z.object({
|
|
1042
|
+
runnableWaitSomeUsec: z.number().int().nonnegative().nullable(),
|
|
1043
|
+
ancestorsComplete: z.boolean(),
|
|
1044
|
+
ancestors: z.array(z.object({
|
|
1045
|
+
cgroupPath: z.string().min(1),
|
|
1046
|
+
inode: z.number().int().positive().nullable(),
|
|
1047
|
+
cpuMax: z.string().nullable(),
|
|
1048
|
+
throttledUsec: z.number().int().nonnegative().nullable()
|
|
1049
|
+
}).strict()).max(32)
|
|
1050
|
+
}).strict().optional()
|
|
1051
|
+
}).strict();
|
|
1052
|
+
const cpuIdentityShape = {
|
|
1053
|
+
source: z.literal("cgroup-v2"),
|
|
1054
|
+
boundary: z.literal("after-membership-through-tree-exit"),
|
|
1055
|
+
sessionId: z.string().uuid(),
|
|
1056
|
+
cgroupPath: z.string().min(1),
|
|
1057
|
+
inode: z.number().int().positive()
|
|
1058
|
+
};
|
|
773
1059
|
const isolatedCheckCommandEvidenceSchema = z.object({
|
|
774
1060
|
wallMs: z.number().finite().nonnegative(),
|
|
1061
|
+
inputDigest: sha256DigestSchema.optional(),
|
|
775
1062
|
stdoutBase64: z.base64(),
|
|
776
1063
|
stderrBase64: z.base64(),
|
|
777
|
-
outputComplete: z.boolean()
|
|
1064
|
+
outputComplete: z.boolean(),
|
|
1065
|
+
cpu: z.discriminatedUnion("available", [z.object({
|
|
1066
|
+
...cpuIdentityShape,
|
|
1067
|
+
available: z.literal(true),
|
|
1068
|
+
before: cpuCounterSchema,
|
|
1069
|
+
after: cpuCounterSchema
|
|
1070
|
+
}).strict(), z.object({
|
|
1071
|
+
...cpuIdentityShape,
|
|
1072
|
+
available: z.literal(false),
|
|
1073
|
+
diagnostic: z.string()
|
|
1074
|
+
}).strict()]).optional()
|
|
778
1075
|
}).strict();
|
|
779
1076
|
/** Execution evidence, not a verdict about task correctness or permission to execute again. */
|
|
780
1077
|
const isolatedCheckResultSchema = z.discriminatedUnion("succeeded", [z.object({
|
|
@@ -1716,6 +2013,6 @@ function superviseAgentGraph(graph, opts, brain) {
|
|
|
1716
2013
|
};
|
|
1717
2014
|
return start();
|
|
1718
2015
|
}
|
|
1719
|
-
export { isolatedCheckBoxEvidenceSchema as a, runIsolatedCheck as c, superviseAgentGraph as i, runGraph as n, isolatedCheckResultSchema as o, runGraphWithTestBrain as r, graphExecutionOptions as s, GraphEdgeCapError as t };
|
|
2016
|
+
export { isolatedCheckBoxEvidenceSchema as a, runIsolatedCheck as c, superviseAgentGraph as i, createIsolatedCheckCpuSession as l, runGraph as n, isolatedCheckResultSchema as o, runGraphWithTestBrain as r, graphExecutionOptions as s, GraphEdgeCapError as t };
|
|
1720
2017
|
|
|
1721
|
-
//# sourceMappingURL=graph-
|
|
2018
|
+
//# sourceMappingURL=graph-RSKyd-_g.js.map
|