@rivet-dev/agentos-core 0.0.0-integrate-dylib-into-main.815fcda → 0.0.0-main.ab2ce7c
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 +5 -8
- package/dist/agent-os.d.ts +168 -46
- package/dist/agent-os.js +809 -543
- package/dist/agentos-package.d.ts +29 -0
- package/dist/agentos-package.js +88 -0
- package/dist/base-filesystem.d.ts +2 -15
- package/dist/base-filesystem.js +23 -171
- package/dist/cron/types.d.ts +1 -1
- package/dist/default-software.d.ts +8 -0
- package/dist/default-software.js +10 -0
- package/dist/generated/AcpLimitsConfig.d.ts +4 -0
- package/dist/generated/AcpLimitsConfig.js +2 -0
- package/dist/generated/CreateVmConfig.d.ts +26 -0
- package/dist/generated/CreateVmConfig.js +1 -0
- package/dist/generated/FsPermissionRule.d.ts +6 -0
- package/dist/generated/FsPermissionRule.js +1 -0
- package/dist/generated/FsPermissionRuleSet.d.ts +6 -0
- package/dist/generated/FsPermissionRuleSet.js +1 -0
- package/dist/generated/FsPermissionScope.d.ts +3 -0
- package/dist/generated/FsPermissionScope.js +1 -0
- package/dist/generated/HttpLimitsConfig.d.ts +3 -0
- package/dist/generated/HttpLimitsConfig.js +2 -0
- package/dist/generated/JsModuleResolution.d.ts +1 -0
- package/dist/generated/JsModuleResolution.js +2 -0
- package/dist/generated/JsRuntimeConfig.d.ts +31 -0
- package/dist/generated/JsRuntimeConfig.js +1 -0
- package/dist/generated/JsRuntimeLimitsConfig.d.ts +11 -0
- package/dist/generated/JsRuntimeLimitsConfig.js +2 -0
- package/dist/generated/JsRuntimePlatform.d.ts +1 -0
- package/dist/generated/JsRuntimePlatform.js +2 -0
- package/dist/generated/MountPluginDescriptor.d.ts +4 -0
- package/dist/generated/MountPluginDescriptor.js +2 -0
- package/dist/generated/NativeRootFilesystemConfig.d.ts +5 -0
- package/dist/generated/NativeRootFilesystemConfig.js +1 -0
- package/dist/generated/PatternPermissionRule.d.ts +6 -0
- package/dist/generated/PatternPermissionRule.js +1 -0
- package/dist/generated/PatternPermissionRuleSet.d.ts +6 -0
- package/dist/generated/PatternPermissionRuleSet.js +1 -0
- package/dist/generated/PatternPermissionScope.d.ts +3 -0
- package/dist/generated/PatternPermissionScope.js +1 -0
- package/dist/generated/PermissionMode.d.ts +1 -0
- package/dist/generated/PermissionMode.js +2 -0
- package/dist/generated/PermissionsPolicy.d.ts +10 -0
- package/dist/generated/PermissionsPolicy.js +1 -0
- package/dist/generated/PluginLimitsConfig.d.ts +4 -0
- package/dist/generated/PluginLimitsConfig.js +2 -0
- package/dist/generated/PythonLimitsConfig.d.ts +6 -0
- package/dist/generated/PythonLimitsConfig.js +2 -0
- package/dist/generated/ResourceLimitsConfig.d.ts +24 -0
- package/dist/generated/ResourceLimitsConfig.js +2 -0
- package/dist/generated/RootFilesystemConfig.d.ts +9 -0
- package/dist/generated/RootFilesystemConfig.js +1 -0
- package/dist/generated/RootFilesystemEntry.d.ts +13 -0
- package/dist/generated/RootFilesystemEntry.js +1 -0
- package/dist/generated/RootFilesystemEntryEncoding.d.ts +1 -0
- package/dist/generated/RootFilesystemEntryEncoding.js +2 -0
- package/dist/generated/RootFilesystemEntryKind.d.ts +1 -0
- package/dist/generated/RootFilesystemEntryKind.js +2 -0
- package/dist/generated/RootFilesystemLowerDescriptor.d.ts +7 -0
- package/dist/generated/RootFilesystemLowerDescriptor.js +1 -0
- package/dist/generated/RootFilesystemMode.d.ts +1 -0
- package/dist/generated/RootFilesystemMode.js +2 -0
- package/dist/generated/ToolLimitsConfig.d.ts +10 -0
- package/dist/generated/ToolLimitsConfig.js +2 -0
- package/dist/generated/VmDnsConfig.d.ts +6 -0
- package/dist/generated/VmDnsConfig.js +2 -0
- package/dist/generated/VmLimitsConfig.d.ts +18 -0
- package/dist/generated/VmLimitsConfig.js +1 -0
- package/dist/generated/VmListenPolicyConfig.d.ts +5 -0
- package/dist/generated/VmListenPolicyConfig.js +2 -0
- package/dist/generated/WasmLimitsConfig.d.ts +7 -0
- package/dist/generated/WasmLimitsConfig.js +2 -0
- package/dist/host-dir-mount.d.ts +1 -1
- package/dist/host-tools-zod.js +27 -7
- package/dist/host-tools.js +9 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/dist/layers.d.ts +2 -2
- package/dist/layers.js +19 -10
- package/dist/options-schema.d.ts +453 -0
- package/dist/options-schema.js +268 -0
- package/dist/packages.d.ts +17 -116
- package/dist/packages.js +35 -322
- package/dist/runtime-compat.d.ts +22 -9
- package/dist/runtime-compat.js +130 -144
- package/dist/runtime.d.ts +32 -5
- package/dist/sandbox.d.ts +112 -0
- package/dist/sandbox.js +264 -0
- package/dist/sidecar/agentos-protocol.d.ts +99 -1
- package/dist/sidecar/agentos-protocol.js +155 -18
- package/dist/sidecar/native-process-client.d.ts +6 -2
- package/dist/sidecar/native-process-client.js +8 -1
- package/dist/sidecar/permissions.d.ts +1 -1
- package/dist/sidecar/permissions.js +4 -4
- package/dist/sidecar/rpc-client.d.ts +62 -4
- package/dist/sidecar/rpc-client.js +339 -85
- package/dist/types.d.ts +10 -3
- package/package.json +40 -33
- package/dist/agents.d.ts +0 -63
- package/dist/agents.js +0 -42
- package/fixtures/AGENTOS_SYSTEM_PROMPT.md +0 -21
package/dist/agent-os.js
CHANGED
|
@@ -1,25 +1,17 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { existsSync,
|
|
4
|
-
import {
|
|
5
|
-
import { dirname, join, posix as posixPath, resolve as resolveHostPath, } from "node:path";
|
|
3
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
4
|
+
import { join, posix as posixPath, resolve as resolveHostPath, } from "node:path";
|
|
6
5
|
import { fileURLToPath } from "node:url";
|
|
7
6
|
import { validateToolkits } from "./host-tools.js";
|
|
8
7
|
import { zodToJsonSchema } from "./host-tools-zod.js";
|
|
8
|
+
import { parseAgentOsOptions } from "./options-schema.js";
|
|
9
9
|
import { resolvePublishedSidecarBinary } from "./sidecar/binary.js";
|
|
10
10
|
import { findCargoBinary, resolveCargoBinary } from "./sidecar/cargo.js";
|
|
11
11
|
export { isAcpTimeoutErrorData } from "./json-rpc.js";
|
|
12
12
|
const ACP_PROTOCOL_VERSION = 1;
|
|
13
13
|
const ACP_EXTENSION_NAMESPACE = "dev.rivet.agent-os.acp";
|
|
14
14
|
const SHELL_DISPOSE_TIMEOUT_MS = 5_000;
|
|
15
|
-
/**
|
|
16
|
-
* Reserved `env` key on `AcpResumeSessionRequest` carrying the resolved adapter
|
|
17
|
-
* bin entrypoint. The resume wire request omits a dedicated `adapterEntrypoint`
|
|
18
|
-
* field; the sidecar reads the entrypoint from this key and strips it before
|
|
19
|
-
* launching the adapter. Must stay in sync with the sidecar constant of the same
|
|
20
|
-
* name in `crates/agentos-sidecar/src/acp_extension.rs`.
|
|
21
|
-
*/
|
|
22
|
-
const RESUME_ADAPTER_ENTRYPOINT_ENV = "AGENT_OS_RESUME_ADAPTER_ENTRYPOINT";
|
|
23
15
|
function defaultAcpClientCapabilities() {
|
|
24
16
|
return {
|
|
25
17
|
fs: {
|
|
@@ -40,19 +32,24 @@ async function waitForTrackedExitPromises(promises, timeoutMs) {
|
|
|
40
32
|
}),
|
|
41
33
|
]);
|
|
42
34
|
}
|
|
43
|
-
import {
|
|
44
|
-
import { getBaseEnvironment, getBaseFilesystemEntries, } from "./base-filesystem.js";
|
|
35
|
+
import { getBaseEnvironment } from "./base-filesystem.js";
|
|
45
36
|
import { CronManager } from "./cron/cron-manager.js";
|
|
46
37
|
import { TimerScheduleDriver } from "./cron/timer-driver.js";
|
|
47
38
|
import { snapshotVirtualFilesystem, sortFilesystemEntries, } from "./filesystem-snapshot.js";
|
|
48
|
-
import { createHostDirBackend } from "./host-dir-mount.js";
|
|
49
39
|
import { serializeMountConfigForSidecar, } from "./js-bridge.js";
|
|
50
40
|
import { createSnapshotExport, } from "./layers.js";
|
|
51
|
-
import {
|
|
41
|
+
import { OPT_AGENTOS_ROOT, } from "./agentos-package.js";
|
|
42
|
+
import { resolveDefaultSoftware } from "./default-software.js";
|
|
52
43
|
import { allowAll } from "./runtime-compat.js";
|
|
53
44
|
import { AcpRuntimeKind, decodeAcpCallback, decodeAcpEvent, decodeAcpResponse, encodeAcpCallbackResponse, encodeAcpRequest, } from "./sidecar/agentos-protocol.js";
|
|
54
45
|
import { serializePermissionsForSidecar } from "./sidecar/permissions.js";
|
|
55
46
|
import { createAgentOsSidecarClient, NATIVE_SIDECAR_FRAME_TIMEOUT_MS, NativeSidecarKernelProxy, SidecarProcess, serializeRootFilesystemForSidecar, } from "./sidecar/rpc-client.js";
|
|
47
|
+
function defaultAgentStderrHandler(event) {
|
|
48
|
+
process.stderr.write(event.chunk);
|
|
49
|
+
}
|
|
50
|
+
function defaultAgentExitHandler(event) {
|
|
51
|
+
process.stderr.write(`[agentos] agent adapter exited unexpectedly: session=${event.sessionId} agent=${event.agentType} exitCode=${event.exitCode ?? "unknown"} restart=${event.restart} (${event.restartCount}/${event.maxRestarts})\n`);
|
|
52
|
+
}
|
|
56
53
|
const LEGACY_PERMISSION_METHOD = "request/permission";
|
|
57
54
|
const ACP_PERMISSION_METHOD = "session/request_permission";
|
|
58
55
|
class AcpDispatchError extends Error {
|
|
@@ -105,6 +102,28 @@ function toRecord(value) {
|
|
|
105
102
|
? value
|
|
106
103
|
: {};
|
|
107
104
|
}
|
|
105
|
+
function normalizePackageRef(value) {
|
|
106
|
+
// The single package reference is `packagePath`: the packed `.aospkg` file
|
|
107
|
+
// (registry-built packages export `{ packagePath }`), or a package dir for
|
|
108
|
+
// local transition fixtures. A raw string is shorthand for the same path.
|
|
109
|
+
if (typeof value === "string") {
|
|
110
|
+
return { path: value };
|
|
111
|
+
}
|
|
112
|
+
const record = toRecord(value);
|
|
113
|
+
if (typeof record.packagePath === "string") {
|
|
114
|
+
return { path: record.packagePath };
|
|
115
|
+
}
|
|
116
|
+
// Recognizably-legacy shapes fail loudly: silently dropping a software
|
|
117
|
+
// entry boots a VM with missing packages and no diagnostic.
|
|
118
|
+
for (const legacy of ["packageTar", "packageDir", "dir"]) {
|
|
119
|
+
if (typeof record[legacy] === "string") {
|
|
120
|
+
throw new Error(`agentOS package ref uses removed field "${legacy}" (value: ${JSON.stringify(record[legacy])}); ` +
|
|
121
|
+
"packages are referenced by a single `packagePath` — update the package " +
|
|
122
|
+
"(rebuild @agentos-software/* dependencies) or pass { packagePath }");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
108
127
|
function parseAcpJson(value, context) {
|
|
109
128
|
if (value === null) {
|
|
110
129
|
return undefined;
|
|
@@ -160,11 +179,11 @@ class BoundedSet {
|
|
|
160
179
|
}
|
|
161
180
|
this.limit = limit;
|
|
162
181
|
}
|
|
163
|
-
add(value) {
|
|
182
|
+
add(value, associated) {
|
|
164
183
|
if (this.#entries.has(value)) {
|
|
165
184
|
this.#entries.delete(value);
|
|
166
185
|
}
|
|
167
|
-
this.#entries.set(value,
|
|
186
|
+
this.#entries.set(value, associated);
|
|
168
187
|
if (this.#entries.size <= this.limit) {
|
|
169
188
|
return;
|
|
170
189
|
}
|
|
@@ -176,6 +195,9 @@ class BoundedSet {
|
|
|
176
195
|
has(value) {
|
|
177
196
|
return this.#entries.has(value);
|
|
178
197
|
}
|
|
198
|
+
get(value) {
|
|
199
|
+
return this.#entries.get(value);
|
|
200
|
+
}
|
|
179
201
|
delete(value) {
|
|
180
202
|
return this.#entries.delete(value);
|
|
181
203
|
}
|
|
@@ -223,6 +245,7 @@ function sessionEntryFromInit(sessionId, agentType, initData) {
|
|
|
223
245
|
agentInfo: initData.agentInfo ?? null,
|
|
224
246
|
eventHandlers: new Set(),
|
|
225
247
|
permissionHandlers: new Set(),
|
|
248
|
+
warnedNoPermissionHandler: false,
|
|
226
249
|
configOverrides: new Map(),
|
|
227
250
|
pendingPermissionReplies: new Map(),
|
|
228
251
|
};
|
|
@@ -366,7 +389,8 @@ const KERNEL_POSIX_BOOTSTRAP_DIRS = [
|
|
|
366
389
|
"/mnt",
|
|
367
390
|
"/media",
|
|
368
391
|
"/home",
|
|
369
|
-
"/home/
|
|
392
|
+
"/home/agentos",
|
|
393
|
+
"/workspace",
|
|
370
394
|
"/usr",
|
|
371
395
|
"/usr/bin",
|
|
372
396
|
"/usr/games",
|
|
@@ -390,8 +414,31 @@ const KERNEL_POSIX_BOOTSTRAP_DIRS = [
|
|
|
390
414
|
"/var/tmp",
|
|
391
415
|
"/etc/agentos",
|
|
392
416
|
];
|
|
393
|
-
|
|
394
|
-
|
|
417
|
+
// Standard POSIX metadata for the bootstrap dirs whose mode/owner is NOT the
|
|
418
|
+
// default (`755`, root:root). Replicated as a constant so building the no-base
|
|
419
|
+
// bootstrap layer needs no `base-filesystem.json` read — when a base IS present
|
|
420
|
+
// the sidecar's embedded base layer is authoritative and these are never emitted.
|
|
421
|
+
const KERNEL_POSIX_BOOTSTRAP_DIR_METADATA = {
|
|
422
|
+
"/tmp": { mode: "1777", uid: 0, gid: 0 },
|
|
423
|
+
"/root": { mode: "700", uid: 0, gid: 0 },
|
|
424
|
+
"/sys": { mode: "555", uid: 0, gid: 0 },
|
|
425
|
+
"/home/agentos": { mode: "2755", uid: 1000, gid: 1000 },
|
|
426
|
+
"/workspace": { mode: "755", uid: 1000, gid: 1000 },
|
|
427
|
+
"/var/empty": { mode: "555", uid: 0, gid: 0 },
|
|
428
|
+
"/var/lock": { mode: "777", uid: 0, gid: 0 },
|
|
429
|
+
"/var/run": { mode: "777", uid: 0, gid: 0 },
|
|
430
|
+
"/var/tmp": { mode: "1777", uid: 0, gid: 0 },
|
|
431
|
+
};
|
|
432
|
+
// Runtime commands that get a `/bin/<cmd>` stub at bootstrap so the guest shell
|
|
433
|
+
// resolves them on PATH (e.g. `sh -c "python ..."`, pipelines). The sidecar
|
|
434
|
+
// intercepts these by name and routes them to the embedded V8 / Pyodide runtime.
|
|
435
|
+
const RUNTIME_BOOTSTRAP_COMMANDS = [
|
|
436
|
+
"node",
|
|
437
|
+
"npm",
|
|
438
|
+
"npx",
|
|
439
|
+
"python",
|
|
440
|
+
"python3",
|
|
441
|
+
];
|
|
395
442
|
const REPO_ROOT = fileURLToPath(new URL("../../..", import.meta.url));
|
|
396
443
|
const SIDECAR_BINARY = join(REPO_ROOT, "target/debug/agentos-sidecar");
|
|
397
444
|
const SIDECAR_BUILD_INPUTS = [
|
|
@@ -403,114 +450,6 @@ const SIDECAR_BUILD_INPUTS = [
|
|
|
403
450
|
join(REPO_ROOT, "crates/sidecar"),
|
|
404
451
|
];
|
|
405
452
|
let ensuredSidecarBinary = null;
|
|
406
|
-
function isWasmBinaryFile(path) {
|
|
407
|
-
try {
|
|
408
|
-
const header = readFileSync(path);
|
|
409
|
-
return (header.length >= 4 &&
|
|
410
|
-
header[0] === 0x00 &&
|
|
411
|
-
header[1] === 0x61 &&
|
|
412
|
-
header[2] === 0x73 &&
|
|
413
|
-
header[3] === 0x6d);
|
|
414
|
-
}
|
|
415
|
-
catch {
|
|
416
|
-
return false;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
function collectBootstrapWasmCommands(commandDirs) {
|
|
420
|
-
const commands = [];
|
|
421
|
-
const seen = new Set();
|
|
422
|
-
for (const dir of commandDirs) {
|
|
423
|
-
let entries;
|
|
424
|
-
try {
|
|
425
|
-
entries = readdirSync(dir).sort((a, b) => a.localeCompare(b));
|
|
426
|
-
}
|
|
427
|
-
catch {
|
|
428
|
-
continue;
|
|
429
|
-
}
|
|
430
|
-
for (const entry of entries) {
|
|
431
|
-
if (entry.startsWith(".")) {
|
|
432
|
-
continue;
|
|
433
|
-
}
|
|
434
|
-
const fullPath = join(dir, entry);
|
|
435
|
-
try {
|
|
436
|
-
if (statSync(fullPath).isDirectory()) {
|
|
437
|
-
continue;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
catch {
|
|
441
|
-
continue;
|
|
442
|
-
}
|
|
443
|
-
if (!isWasmBinaryFile(fullPath) || seen.has(entry)) {
|
|
444
|
-
continue;
|
|
445
|
-
}
|
|
446
|
-
seen.add(entry);
|
|
447
|
-
commands.push(entry);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
return commands;
|
|
451
|
-
}
|
|
452
|
-
function resolveDeclaredCommandSource(commandDir, commandName, aliases) {
|
|
453
|
-
let current = commandName;
|
|
454
|
-
const visited = new Set();
|
|
455
|
-
while (!visited.has(current)) {
|
|
456
|
-
visited.add(current);
|
|
457
|
-
const candidatePath = join(commandDir, current);
|
|
458
|
-
if (isWasmBinaryFile(candidatePath)) {
|
|
459
|
-
return candidatePath;
|
|
460
|
-
}
|
|
461
|
-
const next = aliases[current];
|
|
462
|
-
if (!next) {
|
|
463
|
-
return null;
|
|
464
|
-
}
|
|
465
|
-
current = next;
|
|
466
|
-
}
|
|
467
|
-
return null;
|
|
468
|
-
}
|
|
469
|
-
function prepareCommandDirs(commandPackages) {
|
|
470
|
-
const commandDirs = [];
|
|
471
|
-
const tempDirs = [];
|
|
472
|
-
try {
|
|
473
|
-
for (const commandPackage of commandPackages) {
|
|
474
|
-
commandDirs.push(commandPackage.commandDir);
|
|
475
|
-
const aliasEntries = Object.entries(commandPackage.aliases)
|
|
476
|
-
.sort(([leftAlias], [rightAlias]) => leftAlias.localeCompare(rightAlias))
|
|
477
|
-
.flatMap(([aliasName]) => {
|
|
478
|
-
const aliasPath = join(commandPackage.commandDir, aliasName);
|
|
479
|
-
if (isWasmBinaryFile(aliasPath)) {
|
|
480
|
-
return [];
|
|
481
|
-
}
|
|
482
|
-
const sourcePath = resolveDeclaredCommandSource(commandPackage.commandDir, aliasName, commandPackage.aliases);
|
|
483
|
-
if (!sourcePath) {
|
|
484
|
-
return [];
|
|
485
|
-
}
|
|
486
|
-
return [[aliasName, sourcePath]];
|
|
487
|
-
});
|
|
488
|
-
if (aliasEntries.length === 0) {
|
|
489
|
-
continue;
|
|
490
|
-
}
|
|
491
|
-
const aliasDir = mkdtempSync(join(tmpdir(), "agentos-command-aliases-"));
|
|
492
|
-
for (const [aliasName, sourcePath] of aliasEntries) {
|
|
493
|
-
writeFileSync(join(aliasDir, aliasName), readFileSync(sourcePath));
|
|
494
|
-
}
|
|
495
|
-
tempDirs.push(aliasDir);
|
|
496
|
-
commandDirs.push(aliasDir);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
catch (error) {
|
|
500
|
-
for (const tempDir of tempDirs) {
|
|
501
|
-
rmSync(tempDir, { recursive: true, force: true });
|
|
502
|
-
}
|
|
503
|
-
throw error;
|
|
504
|
-
}
|
|
505
|
-
return {
|
|
506
|
-
commandDirs,
|
|
507
|
-
dispose() {
|
|
508
|
-
for (const tempDir of tempDirs) {
|
|
509
|
-
rmSync(tempDir, { recursive: true, force: true });
|
|
510
|
-
}
|
|
511
|
-
},
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
453
|
function collectConfiguredLowerPaths(config) {
|
|
515
454
|
const paths = new Set();
|
|
516
455
|
for (const lower of config?.lowers ?? []) {
|
|
@@ -533,16 +472,15 @@ function findBootstrapSeedEntry(config, path) {
|
|
|
533
472
|
return entry;
|
|
534
473
|
}
|
|
535
474
|
}
|
|
536
|
-
|
|
475
|
+
// No base-filesystem JSON read: standard non-default dir metadata comes from
|
|
476
|
+
// the constant table. When a base layer IS present these dirs are never
|
|
477
|
+
// emitted (see createKernelBootstrapLower), so this only seeds the no-base case.
|
|
478
|
+
const meta = KERNEL_POSIX_BOOTSTRAP_DIR_METADATA[path];
|
|
479
|
+
return meta ? { path, type: "directory", ...meta } : undefined;
|
|
537
480
|
}
|
|
538
|
-
function createKernelBootstrapLower(config,
|
|
481
|
+
function createKernelBootstrapLower(config, extraEntries = []) {
|
|
539
482
|
const includesBundledBaseLayer = !(config?.disableDefaultBaseLayer ?? false);
|
|
540
483
|
const existingPaths = collectConfiguredLowerPaths(config);
|
|
541
|
-
if (includesBundledBaseLayer) {
|
|
542
|
-
for (const entry of getBaseFilesystemEntries()) {
|
|
543
|
-
existingPaths.add(entry.path);
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
484
|
const entries = [
|
|
547
485
|
{
|
|
548
486
|
path: "/",
|
|
@@ -552,18 +490,24 @@ function createKernelBootstrapLower(config, commandNames, extraEntries = []) {
|
|
|
552
490
|
gid: 0,
|
|
553
491
|
},
|
|
554
492
|
];
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
493
|
+
// Only run the FS bootstrap (creating the POSIX dir tree) when there is NO
|
|
494
|
+
// base layer. When the bundled base IS present, the sidecar's embedded base
|
|
495
|
+
// layer already provides every POSIX dir with the correct mode/owner, so we
|
|
496
|
+
// emit nothing here and never read its filesystem table.
|
|
497
|
+
if (!includesBundledBaseLayer) {
|
|
498
|
+
for (const dir of KERNEL_POSIX_BOOTSTRAP_DIRS) {
|
|
499
|
+
if (existingPaths.has(dir)) {
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
const seed = findBootstrapSeedEntry(config, dir);
|
|
503
|
+
entries.push({
|
|
504
|
+
path: dir,
|
|
505
|
+
type: "directory",
|
|
506
|
+
mode: seed?.type === "directory" ? seed.mode : "755",
|
|
507
|
+
uid: seed?.uid ?? 0,
|
|
508
|
+
gid: seed?.gid ?? 0,
|
|
509
|
+
});
|
|
558
510
|
}
|
|
559
|
-
const seed = findBootstrapSeedEntry(config, dir);
|
|
560
|
-
entries.push({
|
|
561
|
-
path: dir,
|
|
562
|
-
type: "directory",
|
|
563
|
-
mode: seed?.type === "directory" ? seed.mode : "755",
|
|
564
|
-
uid: seed?.uid ?? 0,
|
|
565
|
-
gid: seed?.gid ?? 0,
|
|
566
|
-
});
|
|
567
511
|
}
|
|
568
512
|
if (!includesBundledBaseLayer && !existingPaths.has("/usr/bin/env")) {
|
|
569
513
|
entries.push({
|
|
@@ -576,22 +520,6 @@ function createKernelBootstrapLower(config, commandNames, extraEntries = []) {
|
|
|
576
520
|
encoding: "base64",
|
|
577
521
|
});
|
|
578
522
|
}
|
|
579
|
-
const uniqueCommands = [...new Set(commandNames)].sort((a, b) => a.localeCompare(b));
|
|
580
|
-
for (const command of uniqueCommands) {
|
|
581
|
-
const stubPath = `/bin/${command}`;
|
|
582
|
-
if (existingPaths.has(stubPath)) {
|
|
583
|
-
continue;
|
|
584
|
-
}
|
|
585
|
-
entries.push({
|
|
586
|
-
path: stubPath,
|
|
587
|
-
type: "file",
|
|
588
|
-
mode: "755",
|
|
589
|
-
uid: 0,
|
|
590
|
-
gid: 0,
|
|
591
|
-
content: KERNEL_COMMAND_STUB,
|
|
592
|
-
encoding: "utf8",
|
|
593
|
-
});
|
|
594
|
-
}
|
|
595
523
|
for (const entry of sortFilesystemEntries(extraEntries)) {
|
|
596
524
|
if (existingPaths.has(entry.path)) {
|
|
597
525
|
continue;
|
|
@@ -706,28 +634,6 @@ function latestMtimeMs(path) {
|
|
|
706
634
|
}
|
|
707
635
|
return latest;
|
|
708
636
|
}
|
|
709
|
-
function collectGuestCommandPaths(commandDirs) {
|
|
710
|
-
const guestPaths = new Map();
|
|
711
|
-
for (const [index, commandDir] of commandDirs.entries()) {
|
|
712
|
-
let entries;
|
|
713
|
-
try {
|
|
714
|
-
entries = readdirSync(commandDir).sort((left, right) => left.localeCompare(right));
|
|
715
|
-
}
|
|
716
|
-
catch {
|
|
717
|
-
continue;
|
|
718
|
-
}
|
|
719
|
-
for (const entry of entries) {
|
|
720
|
-
if (entry.startsWith(".")) {
|
|
721
|
-
continue;
|
|
722
|
-
}
|
|
723
|
-
if (!isWasmBinaryFile(join(commandDir, entry)) || guestPaths.has(entry)) {
|
|
724
|
-
continue;
|
|
725
|
-
}
|
|
726
|
-
guestPaths.set(entry, `/__secure_exec/commands/${index}/${entry}`);
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
return guestPaths;
|
|
730
|
-
}
|
|
731
637
|
async function resolveCompatLocalMounts(mounts) {
|
|
732
638
|
if (!mounts) {
|
|
733
639
|
return [];
|
|
@@ -796,52 +702,24 @@ function collectSidecarMountPlan(options) {
|
|
|
796
702
|
}
|
|
797
703
|
for (const mount of options.mounts ?? []) {
|
|
798
704
|
if (!isNativeMountConfig(mount)) {
|
|
705
|
+
sidecarMounts.push({
|
|
706
|
+
guestPath: mount.path,
|
|
707
|
+
readOnly: isOverlayMountConfig(mount)
|
|
708
|
+
? (mount.filesystem.mode ?? "ephemeral") === "read-only"
|
|
709
|
+
: (mount.readOnly ?? false),
|
|
710
|
+
plugin: {
|
|
711
|
+
id: "js_bridge",
|
|
712
|
+
config: {},
|
|
713
|
+
},
|
|
714
|
+
});
|
|
799
715
|
continue;
|
|
800
716
|
}
|
|
801
717
|
pushMount(mount);
|
|
802
718
|
}
|
|
803
|
-
for (const root of options.softwareRoots) {
|
|
804
|
-
pushMount({
|
|
805
|
-
path: root.vmPath,
|
|
806
|
-
plugin: createHostDirBackend({
|
|
807
|
-
hostPath: root.hostPath,
|
|
808
|
-
readOnly: true,
|
|
809
|
-
}),
|
|
810
|
-
readOnly: true,
|
|
811
|
-
});
|
|
812
|
-
}
|
|
813
|
-
for (const [index, commandDir] of options.commandDirs.entries()) {
|
|
814
|
-
pushMount({
|
|
815
|
-
path: `/__secure_exec/commands/${index}`,
|
|
816
|
-
plugin: createHostDirBackend({
|
|
817
|
-
hostPath: commandDir,
|
|
818
|
-
readOnly: true,
|
|
819
|
-
}),
|
|
820
|
-
readOnly: true,
|
|
821
|
-
});
|
|
822
|
-
}
|
|
823
|
-
if (options.shimDir) {
|
|
824
|
-
pushMount({
|
|
825
|
-
path: "/usr/local/bin",
|
|
826
|
-
plugin: createHostDirBackend({
|
|
827
|
-
hostPath: options.shimDir,
|
|
828
|
-
readOnly: true,
|
|
829
|
-
}),
|
|
830
|
-
readOnly: true,
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
719
|
hostMounts.sort((left, right) => right.vmPath.length - left.vmPath.length);
|
|
834
720
|
hostPathMappings.sort((left, right) => right.vmPath.length - left.vmPath.length);
|
|
835
721
|
return { sidecarMounts, hostMounts, hostPathMappings };
|
|
836
722
|
}
|
|
837
|
-
function materializeToolShimDir(toolKits) {
|
|
838
|
-
const shimDir = mkdtempSync(join(tmpdir(), "agentos-host-tools-shims-"));
|
|
839
|
-
writeFileSync(join(shimDir, "agentos"), KERNEL_COMMAND_STUB, { mode: 0o755 });
|
|
840
|
-
for (const toolKit of toolKits) {
|
|
841
|
-
writeFileSync(join(shimDir, `agentos-${toolKit.name}`), KERNEL_COMMAND_STUB, { mode: 0o755 });
|
|
842
|
-
}
|
|
843
|
-
return shimDir;
|
|
844
|
-
}
|
|
845
723
|
function collectToolkitBootstrapCommands(toolKits) {
|
|
846
724
|
if (toolKits.length === 0) {
|
|
847
725
|
return [];
|
|
@@ -1038,7 +916,7 @@ async function handleHostCallback(request, context) {
|
|
|
1038
916
|
return {
|
|
1039
917
|
type: "host_callback_result",
|
|
1040
918
|
invocation_id: payload.invocation_id,
|
|
1041
|
-
error: `EACCES: blocked by
|
|
919
|
+
error: `EACCES: blocked by binding.invoke policy for ${payload.callback_key}`,
|
|
1042
920
|
};
|
|
1043
921
|
}
|
|
1044
922
|
const parsed = tool.inputSchema.safeParse(payload.input);
|
|
@@ -1073,6 +951,134 @@ function buildToolMap(toolKits) {
|
|
|
1073
951
|
}
|
|
1074
952
|
return toolMap;
|
|
1075
953
|
}
|
|
954
|
+
function bridgeErrorMessage(error) {
|
|
955
|
+
return error instanceof Error ? error.message : String(error);
|
|
956
|
+
}
|
|
957
|
+
function toBridgeArgs(value) {
|
|
958
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
959
|
+
throw new Error("js_bridge args must be an object");
|
|
960
|
+
}
|
|
961
|
+
return value;
|
|
962
|
+
}
|
|
963
|
+
function bridgePath(mountId, value) {
|
|
964
|
+
if (!mountId.startsWith("/")) {
|
|
965
|
+
throw new Error(`Unsupported js_bridge mount id: ${mountId}`);
|
|
966
|
+
}
|
|
967
|
+
if (typeof value !== "string") {
|
|
968
|
+
throw new Error("js_bridge path argument must be a string");
|
|
969
|
+
}
|
|
970
|
+
return posixPath.normalize(posixPath.join(mountId, value));
|
|
971
|
+
}
|
|
972
|
+
function requireBridgeNumber(value, field) {
|
|
973
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
974
|
+
throw new Error(`js_bridge args.${field} must be a number`);
|
|
975
|
+
}
|
|
976
|
+
return value;
|
|
977
|
+
}
|
|
978
|
+
function decodeBridgeBytes(value, field) {
|
|
979
|
+
if (typeof value === "string") {
|
|
980
|
+
return new Uint8Array(Buffer.from(value, "base64"));
|
|
981
|
+
}
|
|
982
|
+
if (Array.isArray(value) &&
|
|
983
|
+
value.every((entry) => Number.isInteger(entry) && entry >= 0 && entry <= 255)) {
|
|
984
|
+
return new Uint8Array(value);
|
|
985
|
+
}
|
|
986
|
+
throw new Error(`js_bridge args.${field} must be base64 bytes`);
|
|
987
|
+
}
|
|
988
|
+
async function handleJsBridgeCall(request, context) {
|
|
989
|
+
try {
|
|
990
|
+
const args = toBridgeArgs(request.args);
|
|
991
|
+
const fs = context.filesystem;
|
|
992
|
+
const path = () => bridgePath(request.mount_id, args.path);
|
|
993
|
+
let result;
|
|
994
|
+
switch (request.operation) {
|
|
995
|
+
case "readFile":
|
|
996
|
+
result = Buffer.from(await fs.readFile(path())).toString("base64");
|
|
997
|
+
break;
|
|
998
|
+
case "readDir":
|
|
999
|
+
result = await fs.readDir(path());
|
|
1000
|
+
break;
|
|
1001
|
+
case "readDirWithTypes":
|
|
1002
|
+
result = await fs.readDirWithTypes(path());
|
|
1003
|
+
break;
|
|
1004
|
+
case "writeFile":
|
|
1005
|
+
await fs.writeFile(path(), decodeBridgeBytes(args.content, "content"));
|
|
1006
|
+
break;
|
|
1007
|
+
case "createDir":
|
|
1008
|
+
await fs.createDir(path());
|
|
1009
|
+
break;
|
|
1010
|
+
case "mkdir":
|
|
1011
|
+
await fs.mkdir(path(), { recursive: args.recursive !== false });
|
|
1012
|
+
break;
|
|
1013
|
+
case "exists":
|
|
1014
|
+
result = await fs.exists(path());
|
|
1015
|
+
break;
|
|
1016
|
+
case "stat":
|
|
1017
|
+
result = await fs.stat(path());
|
|
1018
|
+
break;
|
|
1019
|
+
case "removeFile":
|
|
1020
|
+
await fs.removeFile(path());
|
|
1021
|
+
break;
|
|
1022
|
+
case "removeDir":
|
|
1023
|
+
await fs.removeDir(path());
|
|
1024
|
+
break;
|
|
1025
|
+
case "rename":
|
|
1026
|
+
await fs.rename(bridgePath(request.mount_id, args.oldPath), bridgePath(request.mount_id, args.newPath));
|
|
1027
|
+
break;
|
|
1028
|
+
case "realpath":
|
|
1029
|
+
result = await fs.realpath(path());
|
|
1030
|
+
break;
|
|
1031
|
+
case "symlink": {
|
|
1032
|
+
if (typeof args.target !== "string") {
|
|
1033
|
+
throw new Error("js_bridge args.target must be a string");
|
|
1034
|
+
}
|
|
1035
|
+
await fs.symlink(args.target, bridgePath(request.mount_id, args.linkPath));
|
|
1036
|
+
break;
|
|
1037
|
+
}
|
|
1038
|
+
case "readlink":
|
|
1039
|
+
result = await fs.readlink(path());
|
|
1040
|
+
break;
|
|
1041
|
+
case "lstat":
|
|
1042
|
+
result = await fs.lstat(path());
|
|
1043
|
+
break;
|
|
1044
|
+
case "link":
|
|
1045
|
+
await fs.link(bridgePath(request.mount_id, args.oldPath), bridgePath(request.mount_id, args.newPath));
|
|
1046
|
+
break;
|
|
1047
|
+
case "chmod":
|
|
1048
|
+
await fs.chmod(path(), requireBridgeNumber(args.mode, "mode"));
|
|
1049
|
+
break;
|
|
1050
|
+
case "chown":
|
|
1051
|
+
await fs.chown(path(), requireBridgeNumber(args.uid, "uid"), requireBridgeNumber(args.gid, "gid"));
|
|
1052
|
+
break;
|
|
1053
|
+
case "utimes":
|
|
1054
|
+
await fs.utimes(path(), requireBridgeNumber(args.atimeMs, "atimeMs"), requireBridgeNumber(args.mtimeMs, "mtimeMs"));
|
|
1055
|
+
break;
|
|
1056
|
+
case "truncate":
|
|
1057
|
+
await fs.truncate(path(), requireBridgeNumber(args.length, "length"));
|
|
1058
|
+
break;
|
|
1059
|
+
case "pread":
|
|
1060
|
+
result = Buffer.from(await fs.pread(path(), requireBridgeNumber(args.offset, "offset"), requireBridgeNumber(args.length, "length"))).toString("base64");
|
|
1061
|
+
break;
|
|
1062
|
+
case "pwrite":
|
|
1063
|
+
await fs.pwrite(path(), requireBridgeNumber(args.offset, "offset"), decodeBridgeBytes(args.content, "content"));
|
|
1064
|
+
break;
|
|
1065
|
+
default:
|
|
1066
|
+
throw new Error(`Unsupported js_bridge operation: ${request.operation}`);
|
|
1067
|
+
}
|
|
1068
|
+
return {
|
|
1069
|
+
type: "js_bridge_result",
|
|
1070
|
+
call_id: request.call_id,
|
|
1071
|
+
...(result === undefined ? {} : { result }),
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
1074
|
+
catch (error) {
|
|
1075
|
+
return {
|
|
1076
|
+
type: "js_bridge_result",
|
|
1077
|
+
call_id: request.call_id,
|
|
1078
|
+
error: bridgeErrorMessage(error),
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1076
1082
|
function parseHostCommandCallbackInput(input) {
|
|
1077
1083
|
const value = asRecord(input);
|
|
1078
1084
|
if (value.type !== "command" ||
|
|
@@ -1153,7 +1159,7 @@ async function invokeHostTool({ toolKit, toolName, args, cwd, context, }) {
|
|
|
1153
1159
|
const callbackKey = `${toolKit.name}:${toolName}`;
|
|
1154
1160
|
const permissionMode = toolPermissionMode(context.permissions, callbackKey);
|
|
1155
1161
|
if (permissionMode !== "allow") {
|
|
1156
|
-
throw new Error(`EACCES: blocked by
|
|
1162
|
+
throw new Error(`EACCES: blocked by binding.invoke policy for ${callbackKey}`);
|
|
1157
1163
|
}
|
|
1158
1164
|
const input = await parseHostToolInput(tool, args, cwd, context.readFile);
|
|
1159
1165
|
return executeHostTool(tool, callbackKey, input);
|
|
@@ -1309,7 +1315,7 @@ function describeToolPayload(toolKit, toolName) {
|
|
|
1309
1315
|
};
|
|
1310
1316
|
}
|
|
1311
1317
|
function toolPermissionMode(permissions, callbackKey) {
|
|
1312
|
-
const scope = permissions.
|
|
1318
|
+
const scope = permissions.binding;
|
|
1313
1319
|
if (!scope) {
|
|
1314
1320
|
return "deny";
|
|
1315
1321
|
}
|
|
@@ -1383,13 +1389,14 @@ export class AgentOs {
|
|
|
1383
1389
|
_pendingSessionRequestResolvers = new Map();
|
|
1384
1390
|
_processes = new Map();
|
|
1385
1391
|
_shells = new Map();
|
|
1392
|
+
// Value is the recorded exit code (undefined until/unless the exit
|
|
1393
|
+
// resolves) so waitShell can still report it after the entry is dropped.
|
|
1386
1394
|
_closedShellIds = new BoundedSet(CLOSED_SHELL_ID_RETENTION_LIMIT);
|
|
1387
1395
|
_pendingShellExitPromises = new Set();
|
|
1388
1396
|
_shellCounter = 0;
|
|
1389
1397
|
_acpTerminals = new Map();
|
|
1390
1398
|
_acpTerminalCounter = 0;
|
|
1391
1399
|
_softwareRoots;
|
|
1392
|
-
_softwareAgentConfigs;
|
|
1393
1400
|
_cronManager;
|
|
1394
1401
|
_toolKits = [];
|
|
1395
1402
|
_toolReference = "";
|
|
@@ -1397,22 +1404,29 @@ export class AgentOs {
|
|
|
1397
1404
|
_hostMounts;
|
|
1398
1405
|
_env;
|
|
1399
1406
|
_rootFilesystem;
|
|
1407
|
+
_additionalInstructions;
|
|
1400
1408
|
_sidecarLease = null;
|
|
1401
1409
|
_sidecarClient;
|
|
1402
1410
|
_sidecarSession;
|
|
1403
1411
|
_sidecarVm;
|
|
1404
1412
|
_disposeSidecarEventListener;
|
|
1405
|
-
|
|
1413
|
+
_agentStderrHandler;
|
|
1414
|
+
_agentExitHandler;
|
|
1415
|
+
_limitWarningHandler;
|
|
1416
|
+
constructor(kernel, sidecar, softwareRoots, hostMounts, env, rootFilesystem, sidecarClient, sidecarSession, sidecarVm, additionalInstructions, agentStderrHandler, agentExitHandler, limitWarningHandler) {
|
|
1406
1417
|
this.#kernel = kernel;
|
|
1407
1418
|
this.sidecar = sidecar;
|
|
1408
1419
|
this._softwareRoots = softwareRoots;
|
|
1409
|
-
this._softwareAgentConfigs = softwareAgentConfigs;
|
|
1410
1420
|
this._hostMounts = hostMounts;
|
|
1411
1421
|
this._env = env;
|
|
1412
1422
|
this._rootFilesystem = rootFilesystem;
|
|
1413
1423
|
this._sidecarClient = sidecarClient;
|
|
1414
1424
|
this._sidecarSession = sidecarSession;
|
|
1415
1425
|
this._sidecarVm = sidecarVm;
|
|
1426
|
+
this._additionalInstructions = additionalInstructions;
|
|
1427
|
+
this._agentStderrHandler = agentStderrHandler;
|
|
1428
|
+
this._agentExitHandler = agentExitHandler;
|
|
1429
|
+
this._limitWarningHandler = limitWarningHandler;
|
|
1416
1430
|
this._disposeSidecarEventListener = this._sidecarClient.onEvent((event) => {
|
|
1417
1431
|
this._handleSidecarEvent(event);
|
|
1418
1432
|
});
|
|
@@ -1430,72 +1444,90 @@ export class AgentOs {
|
|
|
1430
1444
|
return getSharedAgentOsSidecarInternal(options);
|
|
1431
1445
|
}
|
|
1432
1446
|
static async create(options) {
|
|
1433
|
-
|
|
1447
|
+
options = parseAgentOsOptions(options);
|
|
1448
|
+
// Default software is FULLY DYNAMIC: this package's own NON-agent
|
|
1449
|
+
// @agentos-software/* dependencies (e.g. common), each default-exporting
|
|
1450
|
+
// its registry-built descriptor. Agent packages are NOT projected here —
|
|
1451
|
+
// createSession(id) links the matching agent dependency into the running
|
|
1452
|
+
// VM on first use, so agent closures (and pi's V8 snapshot bundle) only
|
|
1453
|
+
// enter VMs that run them. Unbuilt packages throw with build
|
|
1454
|
+
// instructions; opt out via defaultSoftware: false.
|
|
1455
|
+
const defaultSoftware = options?.defaultSoftware === false ? [] : resolveDefaultSoftware();
|
|
1456
|
+
const software = options?.defaultSoftware === false
|
|
1457
|
+
? (options.software ?? [])
|
|
1458
|
+
: [...defaultSoftware, ...(options?.software ?? [])];
|
|
1459
|
+
// Packages are projected by the SIDECAR: the client forwards only the
|
|
1460
|
+
// package `path` over `configureVm` and the sidecar reads metadata from
|
|
1461
|
+
// the packed vbare manifest (chunk1 of the `.aospkg`).
|
|
1462
|
+
const flatSoftware = software.flat();
|
|
1463
|
+
// Honor the AgentOsOptions.defaultSoftware contract ("entries already present
|
|
1464
|
+
// in `software` are not duplicated"): the default bundle and an explicitly
|
|
1465
|
+
// passed one resolve to the same package paths, so dedup by path. Without
|
|
1466
|
+
// this the sidecar rejects the second projection with a duplicate-command
|
|
1467
|
+
// error (e.g. coreutils' `[`).
|
|
1468
|
+
const seenPackagePaths = new Set();
|
|
1469
|
+
const sidecarPackages = flatSoftware.flatMap((entry) => {
|
|
1470
|
+
const ref = normalizePackageRef(entry);
|
|
1471
|
+
if (!ref || seenPackagePaths.has(ref.path)) {
|
|
1472
|
+
return [];
|
|
1473
|
+
}
|
|
1474
|
+
seenPackagePaths.add(ref.path);
|
|
1475
|
+
return [{ path: ref.path }];
|
|
1476
|
+
});
|
|
1477
|
+
// All package software is projected into `/opt/agentos` by the sidecar. The
|
|
1478
|
+
// client stages nothing host-side and parses NO package manifests: the
|
|
1479
|
+
// sidecar owns agent resolution, agent enumeration, and agent snapshot
|
|
1480
|
+
// bundle loading from the projected package dirs.
|
|
1434
1481
|
const localMounts = await resolveCompatLocalMounts(options?.mounts);
|
|
1435
1482
|
const toolKits = options?.toolKits;
|
|
1436
1483
|
if (toolKits && toolKits.length > 0) {
|
|
1437
1484
|
validateToolkits(toolKits);
|
|
1438
1485
|
}
|
|
1486
|
+
// Resolve the sidecar handle up front so every VM created here leases the
|
|
1487
|
+
// one shared native sidecar process owned by that handle.
|
|
1488
|
+
const sidecar = resolveAgentOsSidecar(options?.sidecar);
|
|
1439
1489
|
const createVmAdmin = async () => {
|
|
1440
|
-
|
|
1490
|
+
// The `/opt/agentos` projection is built by the sidecar from the
|
|
1491
|
+
// forwarded `packages` (it owns the staging dir + read-only mount, and
|
|
1492
|
+
// runtime `linkSoftware` appends to that live dir). The client no longer
|
|
1493
|
+
// stages packages host-side.
|
|
1441
1494
|
const toolBootstrapCommands = collectToolkitBootstrapCommands(toolKits ?? []);
|
|
1442
|
-
const
|
|
1443
|
-
...
|
|
1444
|
-
...NODE_RUNTIME_BOOTSTRAP_COMMANDS,
|
|
1495
|
+
const bootstrapCommands = [
|
|
1496
|
+
...RUNTIME_BOOTSTRAP_COMMANDS,
|
|
1445
1497
|
...toolBootstrapCommands,
|
|
1446
|
-
]
|
|
1498
|
+
];
|
|
1499
|
+
const bootstrapLower = createKernelBootstrapLower(options?.rootFilesystem);
|
|
1447
1500
|
let toolReference = "";
|
|
1448
1501
|
let rootBridge = null;
|
|
1449
1502
|
let kernel = null;
|
|
1450
1503
|
let client = null;
|
|
1451
|
-
let
|
|
1504
|
+
let createdNativeVm = null;
|
|
1505
|
+
let nativeSession = null;
|
|
1452
1506
|
let cleanedUp = false;
|
|
1453
1507
|
const cleanup = async () => {
|
|
1454
1508
|
if (cleanedUp) {
|
|
1455
1509
|
return;
|
|
1456
1510
|
}
|
|
1457
1511
|
cleanedUp = true;
|
|
1458
|
-
if (toolShimDir) {
|
|
1459
|
-
rmSync(toolShimDir, { recursive: true, force: true });
|
|
1460
|
-
toolShimDir = null;
|
|
1461
|
-
}
|
|
1462
|
-
preparedCommandDirs.dispose();
|
|
1463
1512
|
};
|
|
1464
1513
|
try {
|
|
1465
1514
|
const env = getBaseEnvironment();
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
const commandGuestPaths =
|
|
1470
|
-
const requestedMounts = options?.moduleAccessCwd
|
|
1471
|
-
? [
|
|
1472
|
-
...(options.mounts ?? []),
|
|
1473
|
-
{
|
|
1474
|
-
path: "/root/node_modules",
|
|
1475
|
-
plugin: createHostDirBackend({
|
|
1476
|
-
hostPath: join(options.moduleAccessCwd, "node_modules"),
|
|
1477
|
-
readOnly: true,
|
|
1478
|
-
}),
|
|
1479
|
-
readOnly: true,
|
|
1480
|
-
},
|
|
1481
|
-
]
|
|
1482
|
-
: options?.mounts;
|
|
1515
|
+
// Guest command paths. The sidecar owns the `/opt/agentos` projection and
|
|
1516
|
+
// reports the exact projected package commands after `configureVm`.
|
|
1517
|
+
// Tool-shim commands are added below.
|
|
1518
|
+
const commandGuestPaths = new Map();
|
|
1483
1519
|
const { sidecarMounts, hostMounts, hostPathMappings } = collectSidecarMountPlan({
|
|
1484
|
-
mounts:
|
|
1485
|
-
softwareRoots: processed.softwareRoots,
|
|
1486
|
-
commandDirs: preparedCommandDirs.commandDirs,
|
|
1487
|
-
shimDir: toolShimDir,
|
|
1488
|
-
});
|
|
1489
|
-
client = SidecarProcess.spawn({
|
|
1490
|
-
cwd: REPO_ROOT,
|
|
1491
|
-
command: ensureNativeSidecarBinary(),
|
|
1492
|
-
args: [],
|
|
1493
|
-
frameTimeoutMs: NATIVE_SIDECAR_FRAME_TIMEOUT_MS,
|
|
1520
|
+
mounts: options?.mounts,
|
|
1494
1521
|
});
|
|
1495
|
-
|
|
1522
|
+
// Reuse the sidecar handle's single shared native process; this VM
|
|
1523
|
+
// becomes another tenant of it rather than spawning its own process.
|
|
1524
|
+
const shared = await ensureSharedSidecarNativeProcess(sidecar);
|
|
1525
|
+
client = shared.client;
|
|
1526
|
+
const session = shared.session;
|
|
1527
|
+
nativeSession = session;
|
|
1496
1528
|
const hostPermissions = options?.permissions ?? {
|
|
1497
1529
|
...allowAll,
|
|
1498
|
-
|
|
1530
|
+
binding: "allow",
|
|
1499
1531
|
};
|
|
1500
1532
|
const sidecarPermissions = serializePermissionsForSidecar(hostPermissions);
|
|
1501
1533
|
const createVmConfig = {
|
|
@@ -1504,18 +1536,25 @@ export class AgentOs {
|
|
|
1504
1536
|
permissions: sidecarPermissions,
|
|
1505
1537
|
limits: options?.limits,
|
|
1506
1538
|
loopbackExemptPorts: options?.loopbackExemptPorts ?? [],
|
|
1539
|
+
bootstrapCommands,
|
|
1507
1540
|
// 0.3: the Node builtin allow-list moved from configureVm to
|
|
1508
1541
|
// VM creation. `undefined` => engine default allow-list;
|
|
1509
1542
|
// `[]` => deny all; `[..]` => exactly those. Platform and
|
|
1510
1543
|
// module resolution keep their engine defaults (full Node
|
|
1511
1544
|
// emulation), matching the prior behavior where Agent OS only
|
|
1512
1545
|
// constrained the builtin allow-list.
|
|
1513
|
-
...(options?.allowedNodeBuiltins !== undefined
|
|
1546
|
+
...(options?.allowedNodeBuiltins !== undefined ||
|
|
1547
|
+
options?.highResolutionTime !== undefined
|
|
1514
1548
|
? {
|
|
1515
1549
|
jsRuntime: {
|
|
1516
1550
|
platform: "node",
|
|
1517
1551
|
moduleResolution: "node",
|
|
1518
|
-
|
|
1552
|
+
...(options?.allowedNodeBuiltins !== undefined
|
|
1553
|
+
? { allowedBuiltins: options.allowedNodeBuiltins }
|
|
1554
|
+
: {}),
|
|
1555
|
+
...(options?.highResolutionTime !== undefined
|
|
1556
|
+
? { highResolutionTime: options.highResolutionTime }
|
|
1557
|
+
: {}),
|
|
1519
1558
|
},
|
|
1520
1559
|
}
|
|
1521
1560
|
: {}),
|
|
@@ -1524,14 +1563,25 @@ export class AgentOs {
|
|
|
1524
1563
|
runtime: "java_script",
|
|
1525
1564
|
config: createVmConfig,
|
|
1526
1565
|
});
|
|
1566
|
+
createdNativeVm = nativeVm;
|
|
1567
|
+
// Scope the readiness wait to THIS VM's ownership; on a shared process
|
|
1568
|
+
// other VMs are emitting their own lifecycle events concurrently.
|
|
1527
1569
|
await client.waitForEvent((event) => event.payload.type === "vm_lifecycle" &&
|
|
1528
|
-
event.payload.state === "ready"
|
|
1529
|
-
|
|
1570
|
+
event.payload.state === "ready" &&
|
|
1571
|
+
event.ownership.scope === "vm" &&
|
|
1572
|
+
event.ownership.vm_id === nativeVm.vmId, 10_000);
|
|
1573
|
+
const configuredVm = await client.configureVm(session, nativeVm, {
|
|
1530
1574
|
mounts: sidecarMounts,
|
|
1531
1575
|
permissions: sidecarPermissions,
|
|
1532
|
-
commandPermissions:
|
|
1576
|
+
commandPermissions: {},
|
|
1533
1577
|
loopbackExemptPorts: options?.loopbackExemptPorts,
|
|
1578
|
+
packages: sidecarPackages,
|
|
1579
|
+
packagesMountAt: OPT_AGENTOS_ROOT,
|
|
1580
|
+
toolShimCommands: toolBootstrapCommands,
|
|
1534
1581
|
});
|
|
1582
|
+
for (const command of configuredVm.projectedCommands) {
|
|
1583
|
+
commandGuestPaths.set(command.name, command.guestPath);
|
|
1584
|
+
}
|
|
1535
1585
|
if (toolKits && toolKits.length > 0) {
|
|
1536
1586
|
toolReference = await registerToolkitsOnSidecar(client, session, nativeVm, toolKits);
|
|
1537
1587
|
commandGuestPaths.set("agentos", "/bin/agentos");
|
|
@@ -1544,10 +1594,17 @@ export class AgentOs {
|
|
|
1544
1594
|
session,
|
|
1545
1595
|
vm: nativeVm,
|
|
1546
1596
|
env,
|
|
1547
|
-
cwd: "/
|
|
1597
|
+
cwd: "/workspace",
|
|
1548
1598
|
localMounts,
|
|
1599
|
+
sidecarMounts,
|
|
1600
|
+
permissions: sidecarPermissions,
|
|
1601
|
+
commandPermissions: {},
|
|
1602
|
+
loopbackExemptPorts: options?.loopbackExemptPorts,
|
|
1549
1603
|
commandGuestPaths,
|
|
1550
1604
|
onDispose: cleanup,
|
|
1605
|
+
// The native process is owned by the AgentOsSidecar handle and
|
|
1606
|
+
// shared across VMs; disposing this VM must not kill the process.
|
|
1607
|
+
ownsClient: false,
|
|
1551
1608
|
});
|
|
1552
1609
|
await bootstrapLiveBootstrapDirectories(client, session, nativeVm, options?.rootFilesystem);
|
|
1553
1610
|
kernel = rootBridge;
|
|
@@ -1557,6 +1614,10 @@ export class AgentOs {
|
|
|
1557
1614
|
hostMounts,
|
|
1558
1615
|
kernel,
|
|
1559
1616
|
rootView: rootBridge.createRootView(),
|
|
1617
|
+
sidecarMounts,
|
|
1618
|
+
sidecarPermissions,
|
|
1619
|
+
commandPermissions: {},
|
|
1620
|
+
loopbackExemptPorts: options?.loopbackExemptPorts,
|
|
1560
1621
|
sidecarClient: client,
|
|
1561
1622
|
sidecarSession: session,
|
|
1562
1623
|
sidecarVm: nativeVm,
|
|
@@ -1581,6 +1642,8 @@ export class AgentOs {
|
|
|
1581
1642
|
};
|
|
1582
1643
|
}
|
|
1583
1644
|
catch (error) {
|
|
1645
|
+
// The native process is shared and owned by the sidecar handle, so
|
|
1646
|
+
// never dispose the client here — only tear down this VM's resources.
|
|
1584
1647
|
if (kernel) {
|
|
1585
1648
|
await kernel.dispose().catch(() => { });
|
|
1586
1649
|
}
|
|
@@ -1588,20 +1651,23 @@ export class AgentOs {
|
|
|
1588
1651
|
await rootBridge.dispose().catch(() => { });
|
|
1589
1652
|
}
|
|
1590
1653
|
else {
|
|
1591
|
-
|
|
1654
|
+
if (createdNativeVm && nativeSession && client) {
|
|
1655
|
+
await client
|
|
1656
|
+
.disposeVm(nativeSession, createdNativeVm)
|
|
1657
|
+
.catch(() => { });
|
|
1658
|
+
}
|
|
1592
1659
|
await cleanup();
|
|
1593
1660
|
}
|
|
1594
1661
|
throw error;
|
|
1595
1662
|
}
|
|
1596
1663
|
};
|
|
1597
|
-
const sidecar = resolveAgentOsSidecar(options?.sidecar);
|
|
1598
1664
|
let sidecarLease = null;
|
|
1599
1665
|
try {
|
|
1600
1666
|
sidecarLease = await leaseAgentOsSidecarVm(sidecar, {
|
|
1601
1667
|
createVm: async () => createVmAdmin(),
|
|
1602
1668
|
});
|
|
1603
1669
|
const vmAdmin = sidecarLease.admin;
|
|
1604
|
-
const vm = new AgentOs(vmAdmin.kernel, sidecar,
|
|
1670
|
+
const vm = new AgentOs(vmAdmin.kernel, sidecar, [], vmAdmin.hostMounts, vmAdmin.env, vmAdmin.rootView, vmAdmin.sidecarClient, vmAdmin.sidecarSession, vmAdmin.sidecarVm, options?.additionalInstructions, options?.onAgentStderr ?? defaultAgentStderrHandler, options?.onAgentExit ?? defaultAgentExitHandler, options?.onLimitWarning);
|
|
1605
1671
|
vm._sidecarLease = sidecarLease;
|
|
1606
1672
|
vm._toolKits = vmAdmin.toolKits;
|
|
1607
1673
|
vm._toolReference = vmAdmin.toolReference;
|
|
@@ -1632,6 +1698,11 @@ export class AgentOs {
|
|
|
1632
1698
|
exitHandlers,
|
|
1633
1699
|
};
|
|
1634
1700
|
this._processes.set(proc.pid, entry);
|
|
1701
|
+
// NOTE: do NOT delete from `_processes` on exit — the public API contract
|
|
1702
|
+
// (getProcess/listProcesses/stopProcess, see process-management.test.ts)
|
|
1703
|
+
// requires exited processes to stay queryable (running:false, exitCode set).
|
|
1704
|
+
// `_processes` is a process table for this VM's lifetime; it is freed wholesale
|
|
1705
|
+
// in dispose(). (H5: the leak was that dispose() never cleared it.)
|
|
1635
1706
|
void proc.wait().then((code) => {
|
|
1636
1707
|
for (const h of exitHandlers)
|
|
1637
1708
|
h(code);
|
|
@@ -1665,14 +1736,14 @@ export class AgentOs {
|
|
|
1665
1736
|
const entry = this._processes.get(pid);
|
|
1666
1737
|
if (!entry)
|
|
1667
1738
|
throw new Error(`Process not found: ${pid}`);
|
|
1668
|
-
entry.proc.writeStdin(data);
|
|
1739
|
+
return entry.proc.writeStdin(data);
|
|
1669
1740
|
}
|
|
1670
1741
|
/** Close a process's stdin stream. */
|
|
1671
1742
|
closeProcessStdin(pid) {
|
|
1672
1743
|
const entry = this._processes.get(pid);
|
|
1673
1744
|
if (!entry)
|
|
1674
1745
|
throw new Error(`Process not found: ${pid}`);
|
|
1675
|
-
entry.proc.closeStdin();
|
|
1746
|
+
return entry.proc.closeStdin();
|
|
1676
1747
|
}
|
|
1677
1748
|
/** Subscribe to stdout data from a process. Returns an unsubscribe function. */
|
|
1678
1749
|
onProcessStdout(pid, handler) {
|
|
@@ -1733,35 +1804,6 @@ export class AgentOs {
|
|
|
1733
1804
|
_vfs() {
|
|
1734
1805
|
return this.#kernel.vfs;
|
|
1735
1806
|
}
|
|
1736
|
-
async _copyPath(from, to) {
|
|
1737
|
-
const stat = await this._vfs().lstat(from);
|
|
1738
|
-
if (stat.isSymbolicLink) {
|
|
1739
|
-
const target = await this._vfs().readlink(from);
|
|
1740
|
-
await this._vfs().symlink(target, to);
|
|
1741
|
-
return;
|
|
1742
|
-
}
|
|
1743
|
-
if (stat.isDirectory) {
|
|
1744
|
-
await this._mkdirp(posixPath.dirname(to));
|
|
1745
|
-
if (!(await this.#kernel.exists(to))) {
|
|
1746
|
-
await this.#kernel.mkdir(to);
|
|
1747
|
-
}
|
|
1748
|
-
await this._vfs().chmod(to, stat.mode);
|
|
1749
|
-
await this._vfs().chown(to, stat.uid, stat.gid);
|
|
1750
|
-
const entries = await this.#kernel.readdir(from);
|
|
1751
|
-
for (const entry of entries) {
|
|
1752
|
-
if (entry === "." || entry === "..")
|
|
1753
|
-
continue;
|
|
1754
|
-
const fromPath = from === "/" ? `/${entry}` : `${from}/${entry}`;
|
|
1755
|
-
const toPath = to === "/" ? `/${entry}` : `${to}/${entry}`;
|
|
1756
|
-
await this._copyPath(fromPath, toPath);
|
|
1757
|
-
}
|
|
1758
|
-
return;
|
|
1759
|
-
}
|
|
1760
|
-
const content = await this.#kernel.readFile(from);
|
|
1761
|
-
await this.writeFile(to, content);
|
|
1762
|
-
await this._vfs().chmod(to, stat.mode);
|
|
1763
|
-
await this._vfs().chown(to, stat.uid, stat.gid);
|
|
1764
|
-
}
|
|
1765
1807
|
async readFile(path) {
|
|
1766
1808
|
this._assertSafeAbsolutePath(path);
|
|
1767
1809
|
return this.#kernel.readFile(path);
|
|
@@ -1814,14 +1856,13 @@ export class AgentOs {
|
|
|
1814
1856
|
/** Recursively create directories (mkdir -p). */
|
|
1815
1857
|
async _mkdirp(path) {
|
|
1816
1858
|
this._assertWritableAbsolutePath(path);
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
}
|
|
1859
|
+
// `kernel.mkdir` is already recursive (it defaults to recursive=true on both
|
|
1860
|
+
// the native sidecar and compat kernels) and creating an existing directory is
|
|
1861
|
+
// a no-op, so a single call is sufficient. Do NOT probe each ancestor with
|
|
1862
|
+
// `exists()` first: on the native sidecar every read-side op
|
|
1863
|
+
// (exists/stat/readFile) triggers a full shadow-tree walk, so a per-component
|
|
1864
|
+
// exists() loop makes `mkdir -p` cost O(components * tree).
|
|
1865
|
+
await this.#kernel.mkdir(path);
|
|
1825
1866
|
}
|
|
1826
1867
|
async mkdir(path, options) {
|
|
1827
1868
|
if (options?.recursive) {
|
|
@@ -1836,49 +1877,31 @@ export class AgentOs {
|
|
|
1836
1877
|
}
|
|
1837
1878
|
async readdirRecursive(path, options) {
|
|
1838
1879
|
this._assertSafeAbsolutePath(path);
|
|
1839
|
-
const maxDepth = options?.maxDepth;
|
|
1840
1880
|
const exclude = options?.exclude ? new Set(options.exclude) : undefined;
|
|
1881
|
+
const entries = await this.#kernel.readdirRecursive(path, {
|
|
1882
|
+
maxDepth: options?.maxDepth,
|
|
1883
|
+
});
|
|
1884
|
+
const excludedPrefixes = [];
|
|
1841
1885
|
const results = [];
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
if (
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
const entries = await this.#kernel.readdir(dirPath);
|
|
1850
|
-
for (const name of entries) {
|
|
1851
|
-
if (name === "." || name === "..")
|
|
1852
|
-
continue;
|
|
1853
|
-
if (exclude?.has(name))
|
|
1854
|
-
continue;
|
|
1855
|
-
const fullPath = dirPath === "/" ? `/${name}` : `${dirPath}/${name}`;
|
|
1856
|
-
const s = await this.#kernel.stat(fullPath);
|
|
1857
|
-
if (s.isSymbolicLink) {
|
|
1858
|
-
results.push({
|
|
1859
|
-
path: fullPath,
|
|
1860
|
-
type: "symlink",
|
|
1861
|
-
size: s.size,
|
|
1862
|
-
});
|
|
1863
|
-
}
|
|
1864
|
-
else if (s.isDirectory) {
|
|
1865
|
-
results.push({
|
|
1866
|
-
path: fullPath,
|
|
1867
|
-
type: "directory",
|
|
1868
|
-
size: s.size,
|
|
1869
|
-
});
|
|
1870
|
-
if (maxDepth === undefined || depth < maxDepth) {
|
|
1871
|
-
queue.push([fullPath, depth + 1]);
|
|
1872
|
-
}
|
|
1873
|
-
}
|
|
1874
|
-
else {
|
|
1875
|
-
results.push({
|
|
1876
|
-
path: fullPath,
|
|
1877
|
-
type: "file",
|
|
1878
|
-
size: s.size,
|
|
1879
|
-
});
|
|
1886
|
+
for (const entry of entries) {
|
|
1887
|
+
if (excludedPrefixes.some((prefix) => entry.path === prefix || entry.path.startsWith(`${prefix}/`))) {
|
|
1888
|
+
continue;
|
|
1889
|
+
}
|
|
1890
|
+
if (exclude?.has(entry.name)) {
|
|
1891
|
+
if (entry.isDirectory && !entry.isSymbolicLink) {
|
|
1892
|
+
excludedPrefixes.push(entry.path);
|
|
1880
1893
|
}
|
|
1894
|
+
continue;
|
|
1881
1895
|
}
|
|
1896
|
+
results.push({
|
|
1897
|
+
path: entry.path,
|
|
1898
|
+
type: entry.isSymbolicLink
|
|
1899
|
+
? "symlink"
|
|
1900
|
+
: entry.isDirectory
|
|
1901
|
+
? "directory"
|
|
1902
|
+
: "file",
|
|
1903
|
+
size: entry.size,
|
|
1904
|
+
});
|
|
1882
1905
|
}
|
|
1883
1906
|
return results;
|
|
1884
1907
|
}
|
|
@@ -1906,30 +1929,15 @@ export class AgentOs {
|
|
|
1906
1929
|
this.#kernel.unmountFs(path);
|
|
1907
1930
|
}
|
|
1908
1931
|
async move(from, to) {
|
|
1909
|
-
this.
|
|
1910
|
-
this.
|
|
1911
|
-
|
|
1912
|
-
if (!sourceStat.isDirectory || sourceStat.isSymbolicLink) {
|
|
1913
|
-
return this.#kernel.rename(from, to);
|
|
1914
|
-
}
|
|
1915
|
-
await this._copyPath(from, to);
|
|
1916
|
-
await this.delete(from, { recursive: true });
|
|
1932
|
+
this._assertWritableAbsolutePath(from);
|
|
1933
|
+
this._assertWritableAbsolutePath(to);
|
|
1934
|
+
await this.#kernel.movePath(from, to);
|
|
1917
1935
|
}
|
|
1918
1936
|
async delete(path, options) {
|
|
1919
|
-
this.
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
const entries = await this.#kernel.readdir(path);
|
|
1924
|
-
for (const entry of entries) {
|
|
1925
|
-
if (entry === "." || entry === "..")
|
|
1926
|
-
continue;
|
|
1927
|
-
await this.delete(`${path}/${entry}`, { recursive: true });
|
|
1928
|
-
}
|
|
1929
|
-
}
|
|
1930
|
-
return this.#kernel.removeDir(path);
|
|
1931
|
-
}
|
|
1932
|
-
return this.#kernel.removeFile(path);
|
|
1937
|
+
this._assertWritableAbsolutePath(path);
|
|
1938
|
+
await this.#kernel.removePath(path, {
|
|
1939
|
+
recursive: options?.recursive ?? false,
|
|
1940
|
+
});
|
|
1933
1941
|
}
|
|
1934
1942
|
async fetch(port, request) {
|
|
1935
1943
|
const url = new URL(request.url);
|
|
@@ -1964,15 +1972,24 @@ export class AgentOs {
|
|
|
1964
1972
|
const entry = {
|
|
1965
1973
|
handle,
|
|
1966
1974
|
dataHandlers,
|
|
1967
|
-
exitPromise: Promise.resolve(),
|
|
1975
|
+
exitPromise: Promise.resolve(0),
|
|
1968
1976
|
};
|
|
1969
|
-
const exitPromise = handle.wait()
|
|
1970
|
-
|
|
1977
|
+
const exitPromise = handle.wait();
|
|
1978
|
+
const finalize = (exitCode) => {
|
|
1971
1979
|
this._pendingShellExitPromises.delete(entry.exitPromise);
|
|
1972
1980
|
if (this._shells.get(shellId) === entry) {
|
|
1973
1981
|
this._shells.delete(shellId);
|
|
1974
|
-
this._closedShellIds.add(shellId);
|
|
1975
1982
|
}
|
|
1983
|
+
// Record the exit code even when closeShell already dropped the
|
|
1984
|
+
// entry, so a waitShell issued after exit still resolves with it.
|
|
1985
|
+
this._closedShellIds.add(shellId, exitCode);
|
|
1986
|
+
};
|
|
1987
|
+
entry.exitPromise = exitPromise.then((exitCode) => {
|
|
1988
|
+
finalize(exitCode);
|
|
1989
|
+
return exitCode;
|
|
1990
|
+
}, (error) => {
|
|
1991
|
+
finalize();
|
|
1992
|
+
throw error;
|
|
1976
1993
|
});
|
|
1977
1994
|
this._pendingShellExitPromises.add(entry.exitPromise);
|
|
1978
1995
|
this._shells.set(shellId, entry);
|
|
@@ -1986,7 +2003,7 @@ export class AgentOs {
|
|
|
1986
2003
|
const entry = this._shells.get(shellId);
|
|
1987
2004
|
if (!entry)
|
|
1988
2005
|
throw new Error(`Shell not found: ${shellId}`);
|
|
1989
|
-
entry.handle.write(data);
|
|
2006
|
+
return entry.handle.write(data);
|
|
1990
2007
|
}
|
|
1991
2008
|
/** Subscribe to data output from a shell. Returns an unsubscribe function. */
|
|
1992
2009
|
onShellData(shellId, handler) {
|
|
@@ -2005,6 +2022,21 @@ export class AgentOs {
|
|
|
2005
2022
|
throw new Error(`Shell not found: ${shellId}`);
|
|
2006
2023
|
entry.handle.resize(cols, rows);
|
|
2007
2024
|
}
|
|
2025
|
+
/**
|
|
2026
|
+
* Wait for a shell to exit and return its process exit code. Resolves
|
|
2027
|
+
* immediately for a shell that has already exited (within the closed-shell
|
|
2028
|
+
* retention window).
|
|
2029
|
+
*/
|
|
2030
|
+
waitShell(shellId) {
|
|
2031
|
+
const entry = this._shells.get(shellId);
|
|
2032
|
+
if (!entry) {
|
|
2033
|
+
const exitCode = this._closedShellIds.get(shellId);
|
|
2034
|
+
if (exitCode !== undefined)
|
|
2035
|
+
return Promise.resolve(exitCode);
|
|
2036
|
+
throw new Error(`Shell not found: ${shellId}`);
|
|
2037
|
+
}
|
|
2038
|
+
return entry.exitPromise;
|
|
2039
|
+
}
|
|
2008
2040
|
/** Kill a shell process and remove it from tracking. */
|
|
2009
2041
|
closeShell(shellId) {
|
|
2010
2042
|
const entry = this._shells.get(shellId);
|
|
@@ -2119,53 +2151,56 @@ export class AgentOs {
|
|
|
2119
2151
|
}
|
|
2120
2152
|
return session;
|
|
2121
2153
|
}
|
|
2122
|
-
/**
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
.
|
|
2154
|
+
/**
|
|
2155
|
+
* Dynamically link a software package into the RUNNING VM. The package's
|
|
2156
|
+
* `bin/` commands appear under `/opt/agentos/bin` (on `$PATH`) and its `share/man`
|
|
2157
|
+
* pages under MANPATH immediately — the `/opt/agentos` mount is host-backed, so
|
|
2158
|
+
* writing into its staging dir is reflected live with no reboot. An `agent`
|
|
2159
|
+
* block registers the package for `createSession(name)`. Persists for the VM's
|
|
2160
|
+
* lifetime (and across a snapshot iff the volume persists).
|
|
2161
|
+
*/
|
|
2162
|
+
async linkSoftware(descriptor) {
|
|
2163
|
+
const ref = normalizePackageRef(descriptor);
|
|
2164
|
+
if (!ref) {
|
|
2165
|
+
throw new Error("Invalid agentOS package reference");
|
|
2166
|
+
}
|
|
2167
|
+
// Forward to the sidecar, which owns the `/opt/agentos` projection and
|
|
2168
|
+
// appends the package to its live host-backed staging dir; the commands
|
|
2169
|
+
// appear under `/opt/agentos/bin` immediately. The sidecar rejects a
|
|
2170
|
+
// duplicate command, surfaced here as a thrown error.
|
|
2171
|
+
const commands = await this._sidecarClient.linkPackage(this._sidecarSession, this._sidecarVm, { path: ref.path });
|
|
2172
|
+
if (this.#kernel instanceof NativeSidecarKernelProxy) {
|
|
2173
|
+
this.#kernel.registerCommandGuestPaths(new Map(commands.projectedCommands.map((command) => [
|
|
2174
|
+
command.name,
|
|
2175
|
+
command.guestPath,
|
|
2176
|
+
])));
|
|
2177
|
+
}
|
|
2178
|
+
// The client parses no manifests: an `agent` block in the linked package is
|
|
2179
|
+
// picked up by the sidecar (it owns the projected `/opt/agentos` and answers
|
|
2180
|
+
// createSession/listAgents from it). Nothing to record client-side.
|
|
2181
|
+
}
|
|
2182
|
+
async providedCommands() {
|
|
2183
|
+
return this._sidecarClient.providedCommands(this._sidecarSession, this._sidecarVm);
|
|
2184
|
+
}
|
|
2185
|
+
/**
|
|
2186
|
+
* Returns all registered agents with their installation status. Thin forwarder:
|
|
2187
|
+
* sends `AcpListAgentsRequest` and maps the response. The sidecar enumerates the
|
|
2188
|
+
* projected `/opt/agentos` packages (the client parses no manifests). Every such
|
|
2189
|
+
* agent is a package materialized into the VM, so `installed` is always `true`.
|
|
2190
|
+
*/
|
|
2191
|
+
async listAgents() {
|
|
2192
|
+
const response = await this._sendAcpRequest({
|
|
2193
|
+
tag: "AcpListAgentsRequest",
|
|
2194
|
+
val: { reserved: false },
|
|
2195
|
+
});
|
|
2196
|
+
if (response.tag !== "AcpListAgentsResponse") {
|
|
2197
|
+
throw new Error(`unexpected list_agents response: ${response.tag}`);
|
|
2198
|
+
}
|
|
2199
|
+
return response.val.agents.map((agent) => ({
|
|
2200
|
+
id: agent.id,
|
|
2201
|
+
installed: agent.installed,
|
|
2202
|
+
adapterEntrypoint: agent.adapterEntrypoint,
|
|
2203
|
+
}));
|
|
2169
2204
|
}
|
|
2170
2205
|
_syncSessionState(session, state) {
|
|
2171
2206
|
session.processId = state.processId;
|
|
@@ -2244,6 +2279,106 @@ export class AgentOs {
|
|
|
2244
2279
|
session.eventHandlers.delete(subscriber);
|
|
2245
2280
|
};
|
|
2246
2281
|
}
|
|
2282
|
+
/**
|
|
2283
|
+
* Warn once per session (host-visible) that a tool-permission request was
|
|
2284
|
+
* auto-denied because no `onPermissionRequest` handler is registered. Shared
|
|
2285
|
+
* by both the bare-callback and JSON-RPC permission paths so the message and
|
|
2286
|
+
* the once-per-session guard cannot drift between them.
|
|
2287
|
+
*/
|
|
2288
|
+
_warnNoPermissionHandlerOnce(session, params) {
|
|
2289
|
+
if (session.warnedNoPermissionHandler) {
|
|
2290
|
+
return;
|
|
2291
|
+
}
|
|
2292
|
+
session.warnedNoPermissionHandler = true;
|
|
2293
|
+
this._emitSessionWarning(session, `agentos: a tool-permission request (${this._permissionToolLabel(params)}) was ` +
|
|
2294
|
+
`auto-denied because no onPermissionRequest handler is registered for session ` +
|
|
2295
|
+
`${session.sessionId}. Register one with vm.onPermissionRequest(sessionId, ...) and ` +
|
|
2296
|
+
`reply via vm.respondPermission(...) to let the agent use tools.`);
|
|
2297
|
+
}
|
|
2298
|
+
/** Best-effort human label for the tool named in a permission request. */
|
|
2299
|
+
_permissionToolLabel(params) {
|
|
2300
|
+
if (typeof params.toolName === "string") {
|
|
2301
|
+
return params.toolName;
|
|
2302
|
+
}
|
|
2303
|
+
const toolCall = params.toolCall;
|
|
2304
|
+
if (toolCall &&
|
|
2305
|
+
typeof toolCall === "object" &&
|
|
2306
|
+
typeof toolCall.title === "string") {
|
|
2307
|
+
return toolCall.title;
|
|
2308
|
+
}
|
|
2309
|
+
return "a tool";
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* Emit a host-visible warning for a session through the same agent-process log
|
|
2313
|
+
* channel that surfaces adapter stderr (`onAgentStderr`, default: process
|
|
2314
|
+
* stderr). Used for agent-os-owned diagnostics — e.g. a permission request
|
|
2315
|
+
* that was auto-denied because no host hook is registered — so they never fire
|
|
2316
|
+
* silently inside the sidecar.
|
|
2317
|
+
*/
|
|
2318
|
+
_emitSessionWarning(session, message) {
|
|
2319
|
+
const handler = this._agentStderrHandler;
|
|
2320
|
+
if (!handler) {
|
|
2321
|
+
return;
|
|
2322
|
+
}
|
|
2323
|
+
try {
|
|
2324
|
+
handler({
|
|
2325
|
+
sessionId: session.sessionId,
|
|
2326
|
+
agentType: session.agentType,
|
|
2327
|
+
processId: session.processId,
|
|
2328
|
+
pid: session.pid,
|
|
2329
|
+
chunk: new TextEncoder().encode(`${message}\n`),
|
|
2330
|
+
});
|
|
2331
|
+
}
|
|
2332
|
+
catch {
|
|
2333
|
+
// A warning sink failure must never affect permission handling.
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
_recordAgentStderr(event) {
|
|
2337
|
+
const session = (event.sessionId ? this._sessions.get(event.sessionId) : undefined) ??
|
|
2338
|
+
[...this._sessions.values()].find((candidate) => candidate.processId === event.processId);
|
|
2339
|
+
const sessionId = event.sessionId || session?.sessionId;
|
|
2340
|
+
if (!sessionId) {
|
|
2341
|
+
return;
|
|
2342
|
+
}
|
|
2343
|
+
const handler = this._agentStderrHandler;
|
|
2344
|
+
if (!handler) {
|
|
2345
|
+
return;
|
|
2346
|
+
}
|
|
2347
|
+
try {
|
|
2348
|
+
handler({
|
|
2349
|
+
sessionId,
|
|
2350
|
+
agentType: event.agentType || session?.agentType || "",
|
|
2351
|
+
processId: event.processId,
|
|
2352
|
+
pid: session?.pid ?? null,
|
|
2353
|
+
chunk: new Uint8Array(event.chunk),
|
|
2354
|
+
});
|
|
2355
|
+
}
|
|
2356
|
+
catch {
|
|
2357
|
+
// Ignore subscriber callback failures and keep event delivery moving.
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
_recordAgentExit(event) {
|
|
2361
|
+
const session = this._sessions.get(event.sessionId);
|
|
2362
|
+
const handler = this._agentExitHandler;
|
|
2363
|
+
if (!handler) {
|
|
2364
|
+
return;
|
|
2365
|
+
}
|
|
2366
|
+
try {
|
|
2367
|
+
handler({
|
|
2368
|
+
sessionId: event.sessionId,
|
|
2369
|
+
agentType: event.agentType || session?.agentType || "",
|
|
2370
|
+
processId: event.processId,
|
|
2371
|
+
pid: session?.pid ?? null,
|
|
2372
|
+
exitCode: event.exitCode,
|
|
2373
|
+
restart: event.restart,
|
|
2374
|
+
restartCount: event.restartCount,
|
|
2375
|
+
maxRestarts: event.maxRestarts,
|
|
2376
|
+
});
|
|
2377
|
+
}
|
|
2378
|
+
catch {
|
|
2379
|
+
// Ignore subscriber callback failures and keep event delivery moving.
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
2247
2382
|
_applySyntheticConfigOverrides(session) {
|
|
2248
2383
|
if (session.configOverrides.size === 0) {
|
|
2249
2384
|
return;
|
|
@@ -2266,6 +2401,10 @@ export class AgentOs {
|
|
|
2266
2401
|
if (event.payload.type !== "structured") {
|
|
2267
2402
|
return;
|
|
2268
2403
|
}
|
|
2404
|
+
if (event.payload.name === "limit_warning") {
|
|
2405
|
+
this._handleLimitWarning(event.payload.detail);
|
|
2406
|
+
return;
|
|
2407
|
+
}
|
|
2269
2408
|
if (event.payload.name !== "acp.session_event") {
|
|
2270
2409
|
return;
|
|
2271
2410
|
}
|
|
@@ -2285,6 +2424,27 @@ export class AgentOs {
|
|
|
2285
2424
|
// Ignore malformed event payloads from the sidecar.
|
|
2286
2425
|
}
|
|
2287
2426
|
}
|
|
2427
|
+
_handleLimitWarning(detail) {
|
|
2428
|
+
if (!this._limitWarningHandler) {
|
|
2429
|
+
return;
|
|
2430
|
+
}
|
|
2431
|
+
const toNumber = (value) => {
|
|
2432
|
+
const parsed = Number(value);
|
|
2433
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
2434
|
+
};
|
|
2435
|
+
try {
|
|
2436
|
+
this._limitWarningHandler({
|
|
2437
|
+
limit: detail.limit ?? "",
|
|
2438
|
+
category: detail.category ?? "",
|
|
2439
|
+
observed: toNumber(detail.observed),
|
|
2440
|
+
capacity: toNumber(detail.capacity),
|
|
2441
|
+
fillPercent: toNumber(detail.fillPercent),
|
|
2442
|
+
});
|
|
2443
|
+
}
|
|
2444
|
+
catch {
|
|
2445
|
+
// A throwing handler must never break the sidecar event loop.
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2288
2448
|
_handleAcpExtEvent(envelope) {
|
|
2289
2449
|
if (envelope.namespace !== ACP_EXTENSION_NAMESPACE) {
|
|
2290
2450
|
return;
|
|
@@ -2300,6 +2460,14 @@ export class AgentOs {
|
|
|
2300
2460
|
this._recordSessionNotification(session, toJsonRpcNotification(JSON.parse(event.val.notification)));
|
|
2301
2461
|
return;
|
|
2302
2462
|
}
|
|
2463
|
+
case "AcpAgentStderrEvent": {
|
|
2464
|
+
this._recordAgentStderr(event.val);
|
|
2465
|
+
return;
|
|
2466
|
+
}
|
|
2467
|
+
case "AcpAgentExitedEvent": {
|
|
2468
|
+
this._recordAgentExit(event.val);
|
|
2469
|
+
return;
|
|
2470
|
+
}
|
|
2303
2471
|
}
|
|
2304
2472
|
}
|
|
2305
2473
|
catch {
|
|
@@ -2515,37 +2683,30 @@ export class AgentOs {
|
|
|
2515
2683
|
this._syncSessionState(session, state);
|
|
2516
2684
|
}
|
|
2517
2685
|
async createSession(agentType, options) {
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
// System-prompt assembly
|
|
2523
|
-
// the sidecar
|
|
2524
|
-
// skipOsInstructions plus the
|
|
2525
|
-
const
|
|
2526
|
-
|
|
2527
|
-
const sessionCwd = options?.cwd ?? "/home/user";
|
|
2528
|
-
const adapterEntrypoint = this._resolveAdapterBin(config.acpAdapter);
|
|
2529
|
-
if ((agentType === "pi" || agentType === "pi-cli") &&
|
|
2530
|
-
!launchEnv.PI_ACP_PI_COMMAND) {
|
|
2531
|
-
launchEnv = {
|
|
2532
|
-
...launchEnv,
|
|
2533
|
-
PI_ACP_PI_COMMAND: this._resolvePackageBin(config.agentPackage, "pi"),
|
|
2534
|
-
};
|
|
2535
|
-
}
|
|
2686
|
+
// The client is npm-agnostic: it sends only the agent NAME. The sidecar
|
|
2687
|
+
// resolves the name -> package -> entrypoint/env/launchArgs from the
|
|
2688
|
+
// projected `/opt/agentos/<name>/current/agentos-package.json` and spawns
|
|
2689
|
+
// (including the agent's static launch args and manifest env defaults).
|
|
2690
|
+
// System-prompt assembly/injection (launch args / OPENCODE_CONTEXTPATHS) is
|
|
2691
|
+
// owned by the sidecar; the host only forwards additionalInstructions /
|
|
2692
|
+
// skipOsInstructions plus the caller's env.
|
|
2693
|
+
const launchEnv = { ...options?.env };
|
|
2694
|
+
const sessionCwd = options?.cwd ?? "/workspace";
|
|
2536
2695
|
const response = await this._sendAcpRequest({
|
|
2537
2696
|
tag: "AcpCreateSessionRequest",
|
|
2538
2697
|
val: {
|
|
2539
2698
|
agentType: String(agentType),
|
|
2540
2699
|
runtime: AcpRuntimeKind.JavaScript,
|
|
2541
|
-
|
|
2542
|
-
args: launchArgs,
|
|
2700
|
+
args: [],
|
|
2543
2701
|
env: new Map(Object.entries(launchEnv)),
|
|
2544
2702
|
cwd: sessionCwd,
|
|
2545
2703
|
mcpServers: JSON.stringify(options?.mcpServers ?? []),
|
|
2546
2704
|
protocolVersion: ACP_PROTOCOL_VERSION,
|
|
2547
2705
|
clientCapabilities: JSON.stringify(defaultAcpClientCapabilities()),
|
|
2548
|
-
additionalInstructions: combineInstructions(options?.additionalInstructions
|
|
2706
|
+
additionalInstructions: combineInstructions([this._additionalInstructions, options?.additionalInstructions]
|
|
2707
|
+
.map((part) => part?.trim())
|
|
2708
|
+
.filter((part) => Boolean(part))
|
|
2709
|
+
.join("\n\n") || undefined, this._toolReference),
|
|
2549
2710
|
skipOsInstructions: options?.skipOsInstructions ?? false,
|
|
2550
2711
|
},
|
|
2551
2712
|
});
|
|
@@ -2596,27 +2757,11 @@ export class AgentOs {
|
|
|
2596
2757
|
* there is no surviving store and the fallback tier always runs.
|
|
2597
2758
|
*/
|
|
2598
2759
|
async resumeSession(sessionId, agentType, options) {
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
const
|
|
2604
|
-
let launchEnv = { ...config.defaultEnv, ...options?.env };
|
|
2605
|
-
const sessionCwd = options?.cwd ?? "/home/user";
|
|
2606
|
-
if ((agentType === "pi" || agentType === "pi-cli") &&
|
|
2607
|
-
!launchEnv.PI_ACP_PI_COMMAND) {
|
|
2608
|
-
launchEnv = {
|
|
2609
|
-
...launchEnv,
|
|
2610
|
-
PI_ACP_PI_COMMAND: this._resolvePackageBin(config.agentPackage, "pi"),
|
|
2611
|
-
};
|
|
2612
|
-
}
|
|
2613
|
-
// The resume wire request has no dedicated `adapterEntrypoint` field; carry
|
|
2614
|
-
// the resolved entrypoint through env under the sidecar's reserved key. The
|
|
2615
|
-
// sidecar reads it and strips it before launching the adapter.
|
|
2616
|
-
launchEnv = {
|
|
2617
|
-
...launchEnv,
|
|
2618
|
-
[RESUME_ADAPTER_ENTRYPOINT_ENV]: adapterEntrypoint,
|
|
2619
|
-
};
|
|
2760
|
+
// The client is npm-agnostic: it sends only the agent NAME. The sidecar
|
|
2761
|
+
// resolves the name -> package -> entrypoint/env/launchArgs from the
|
|
2762
|
+
// projected manifest, exactly as createSession does.
|
|
2763
|
+
const sessionCwd = options?.cwd ?? "/workspace";
|
|
2764
|
+
const launchEnv = { ...options?.env };
|
|
2620
2765
|
const response = await this._sendAcpRequest({
|
|
2621
2766
|
tag: "AcpResumeSessionRequest",
|
|
2622
2767
|
val: {
|
|
@@ -2632,6 +2777,10 @@ export class AgentOs {
|
|
|
2632
2777
|
}
|
|
2633
2778
|
const { sessionId: liveSessionId, mode } = response.val;
|
|
2634
2779
|
// Register + hydrate the live session so subsequent prompts route to it.
|
|
2780
|
+
const existing = this._sessions.get(liveSessionId);
|
|
2781
|
+
if (existing !== undefined && !existing.closed) {
|
|
2782
|
+
throw new Error(`session id collision: ${liveSessionId}`);
|
|
2783
|
+
}
|
|
2635
2784
|
const session = sessionEntryFromInit(liveSessionId, String(agentType), {});
|
|
2636
2785
|
this._closedSessionIds.delete(liveSessionId);
|
|
2637
2786
|
this._sessions.set(liveSessionId, session);
|
|
@@ -2644,65 +2793,6 @@ export class AgentOs {
|
|
|
2644
2793
|
}
|
|
2645
2794
|
return { sessionId: liveSessionId, mode };
|
|
2646
2795
|
}
|
|
2647
|
-
/**
|
|
2648
|
-
* Resolve the VM bin entry point of an ACP adapter package.
|
|
2649
|
-
* Reads from the host filesystem since kernel.readFile() resolves through
|
|
2650
|
-
* mounts; adapter package.json is read directly off the host dir backing
|
|
2651
|
-
* the relevant mount.
|
|
2652
|
-
*/
|
|
2653
|
-
_resolveAdapterBin(adapterPackage) {
|
|
2654
|
-
return this._resolvePackageBin(adapterPackage);
|
|
2655
|
-
}
|
|
2656
|
-
/**
|
|
2657
|
-
* Parent of the host directory backing the `/root/node_modules` mount, if
|
|
2658
|
-
* the caller supplied one (e.g. via `nodeModulesMount(...)`). Used as the
|
|
2659
|
-
* `resolvePackageDir` start dir for adapter/agent package resolution when no
|
|
2660
|
-
* software root matches. `nodeModulesMount` mounts `<dir>/node_modules`, so
|
|
2661
|
-
* the start dir is `<dir>` (one level above the node_modules tree).
|
|
2662
|
-
*/
|
|
2663
|
-
_nodeModulesHostRoot() {
|
|
2664
|
-
for (const mount of this._hostMounts) {
|
|
2665
|
-
if (mount.vmPath === "/root/node_modules") {
|
|
2666
|
-
return dirname(mount.hostPath);
|
|
2667
|
-
}
|
|
2668
|
-
}
|
|
2669
|
-
return null;
|
|
2670
|
-
}
|
|
2671
|
-
_resolvePackageBin(packageName, binName) {
|
|
2672
|
-
const vmPrefix = `/root/node_modules/${packageName}`;
|
|
2673
|
-
let hostPkgJsonPath = null;
|
|
2674
|
-
for (const root of this._softwareRoots) {
|
|
2675
|
-
if (root.vmPath === vmPrefix) {
|
|
2676
|
-
hostPkgJsonPath = join(root.hostPath, "package.json");
|
|
2677
|
-
break;
|
|
2678
|
-
}
|
|
2679
|
-
}
|
|
2680
|
-
// Fall back to the host dir behind the `/root/node_modules` mount.
|
|
2681
|
-
if (!hostPkgJsonPath) {
|
|
2682
|
-
const nodeModulesRoot = this._nodeModulesHostRoot();
|
|
2683
|
-
if (!nodeModulesRoot) {
|
|
2684
|
-
throw new Error(`Cannot resolve package "${packageName}": no software root provides it and ` +
|
|
2685
|
-
`no /root/node_modules mount was supplied. Pass ` +
|
|
2686
|
-
`mounts: [nodeModulesMount("<host>/node_modules")] to AgentOs.create().`);
|
|
2687
|
-
}
|
|
2688
|
-
hostPkgJsonPath = join(resolvePackageDir(nodeModulesRoot, packageName), "package.json");
|
|
2689
|
-
}
|
|
2690
|
-
const pkg = JSON.parse(readFileSync(hostPkgJsonPath, "utf-8"));
|
|
2691
|
-
let binEntry;
|
|
2692
|
-
if (typeof pkg.bin === "string") {
|
|
2693
|
-
binEntry = pkg.bin;
|
|
2694
|
-
}
|
|
2695
|
-
else if (typeof pkg.bin === "object" && pkg.bin !== null) {
|
|
2696
|
-
binEntry =
|
|
2697
|
-
(binName ? pkg.bin[binName] : undefined) ??
|
|
2698
|
-
pkg.bin[packageName] ??
|
|
2699
|
-
Object.values(pkg.bin)[0];
|
|
2700
|
-
}
|
|
2701
|
-
if (!binEntry) {
|
|
2702
|
-
throw new Error(`No bin entry found in ${packageName}/package.json`);
|
|
2703
|
-
}
|
|
2704
|
-
return `${vmPrefix}/${binEntry}`;
|
|
2705
|
-
}
|
|
2706
2796
|
_installSidecarRequestHandler() {
|
|
2707
2797
|
const context = {
|
|
2708
2798
|
toolKits: this._toolKits,
|
|
@@ -2715,10 +2805,8 @@ export class AgentOs {
|
|
|
2715
2805
|
case "host_callback":
|
|
2716
2806
|
return handleHostCallback(request, context);
|
|
2717
2807
|
case "js_bridge_call":
|
|
2718
|
-
return
|
|
2719
|
-
|
|
2720
|
-
call_id: request.payload.call_id,
|
|
2721
|
-
error: `unsupported sidecar request type: ${request.payload.type}`,
|
|
2808
|
+
return handleJsBridgeCall(request.payload, {
|
|
2809
|
+
filesystem: this.#kernel.vfs,
|
|
2722
2810
|
});
|
|
2723
2811
|
case "ext":
|
|
2724
2812
|
return this._handleAcpExtSidecarRequest(request.payload.envelope);
|
|
@@ -2905,6 +2993,10 @@ export class AgentOs {
|
|
|
2905
2993
|
_acpMethod: request.method,
|
|
2906
2994
|
};
|
|
2907
2995
|
if (session.permissionHandlers.size === 0) {
|
|
2996
|
+
// Default-closed deny; warn once (host-visible) so a forgotten
|
|
2997
|
+
// onPermissionRequest handler is an observable cause rather than a
|
|
2998
|
+
// silent denial. See _warnNoPermissionHandlerOnce.
|
|
2999
|
+
this._warnNoPermissionHandlerOnce(session, permissionParams);
|
|
2908
3000
|
return this._buildAcpPermissionResult("reject", permissionParams);
|
|
2909
3001
|
}
|
|
2910
3002
|
const reply = await new Promise((resolve, reject) => {
|
|
@@ -3187,6 +3279,10 @@ export class AgentOs {
|
|
|
3187
3279
|
return "reject";
|
|
3188
3280
|
}
|
|
3189
3281
|
if (session.permissionHandlers.size === 0) {
|
|
3282
|
+
// Default-closed: deny when no host hook is listening, and warn once
|
|
3283
|
+
// (host-visible) so a forgotten onPermissionRequest handler is not an
|
|
3284
|
+
// invisible cause of an agent that cannot use any tool.
|
|
3285
|
+
this._warnNoPermissionHandlerOnce(session, params);
|
|
3190
3286
|
return "reject";
|
|
3191
3287
|
}
|
|
3192
3288
|
try {
|
|
@@ -3216,14 +3312,6 @@ export class AgentOs {
|
|
|
3216
3312
|
return "reject";
|
|
3217
3313
|
}
|
|
3218
3314
|
}
|
|
3219
|
-
/**
|
|
3220
|
-
* Resolve an agent config by ID. Package-provided configs take
|
|
3221
|
-
* precedence over the hardcoded AGENT_CONFIGS.
|
|
3222
|
-
*/
|
|
3223
|
-
_resolveAgentConfig(agentType) {
|
|
3224
|
-
return (this._softwareAgentConfigs.get(agentType) ??
|
|
3225
|
-
AGENT_CONFIGS[agentType]);
|
|
3226
|
-
}
|
|
3227
3315
|
/**
|
|
3228
3316
|
* Gracefully destroy a session: cancel any pending work, close the client,
|
|
3229
3317
|
* and remove from tracking. Unlike close() which is abrupt, this attempts
|
|
@@ -3407,6 +3495,7 @@ export class AgentOs {
|
|
|
3407
3495
|
terminalExitPromises.push(terminal.waitPromise.then(() => undefined, () => undefined));
|
|
3408
3496
|
}
|
|
3409
3497
|
this._acpTerminals.clear();
|
|
3498
|
+
this._processes.clear();
|
|
3410
3499
|
await waitForTrackedExitPromises([...shellExitPromises, ...terminalExitPromises], SHELL_DISPOSE_TIMEOUT_MS);
|
|
3411
3500
|
this._disposeSidecarEventListener();
|
|
3412
3501
|
const sidecarLease = this._sidecarLease;
|
|
@@ -3436,6 +3525,164 @@ function resolveAgentOsSidecar(config) {
|
|
|
3436
3525
|
}
|
|
3437
3526
|
const sidecarStates = new WeakMap();
|
|
3438
3527
|
const sharedSidecars = new Map();
|
|
3528
|
+
let sidecarProcessExitHookInstalled = false;
|
|
3529
|
+
/**
|
|
3530
|
+
* Install a one-time, synchronous `process.on("exit")` hook that SIGKILLs any
|
|
3531
|
+
* pooled shared sidecar child. Once a one-shot host process is allowed to exit
|
|
3532
|
+
* (its sidecar handles are unref'd at 0 leases), this reaps the sidecar
|
|
3533
|
+
* immediately instead of waiting for its stdin-EOF grace window — no orphan, no
|
|
3534
|
+
* delay. We deliberately do NOT install SIGINT/SIGTERM handlers: a library
|
|
3535
|
+
* should not hijack the host's signal handling. SIGINT still reaches the sidecar
|
|
3536
|
+
* via the process group, and SIGTERM-driven exit still closes its stdin.
|
|
3537
|
+
*/
|
|
3538
|
+
function ensureSidecarProcessExitCleanup() {
|
|
3539
|
+
if (sidecarProcessExitHookInstalled)
|
|
3540
|
+
return;
|
|
3541
|
+
sidecarProcessExitHookInstalled = true;
|
|
3542
|
+
process.on("exit", () => {
|
|
3543
|
+
for (const sidecar of sharedSidecars.values()) {
|
|
3544
|
+
try {
|
|
3545
|
+
sidecarStates.get(sidecar)?.sharedChild?.kill?.("SIGKILL");
|
|
3546
|
+
}
|
|
3547
|
+
catch {
|
|
3548
|
+
// best-effort reap; the process is exiting regardless
|
|
3549
|
+
}
|
|
3550
|
+
}
|
|
3551
|
+
});
|
|
3552
|
+
}
|
|
3553
|
+
function sidecarChildHandle(client) {
|
|
3554
|
+
// SidecarProcess -> StdioSidecarProtocolClient.child (the spawned ChildProcess).
|
|
3555
|
+
const protocolClient = client?.protocolClient;
|
|
3556
|
+
return protocolClient?.child ?? undefined;
|
|
3557
|
+
}
|
|
3558
|
+
/**
|
|
3559
|
+
* Apply the current hold state to the shared sidecar's child + stdio: ref them
|
|
3560
|
+
* while ≥1 hold is live so in-flight VM work keeps the host process alive; unref
|
|
3561
|
+
* them at 0 so a one-shot script exits on its own after `dispose()`. The sidecar
|
|
3562
|
+
* process itself stays running (reusable) and self-exits on stdin EOF when the
|
|
3563
|
+
* host finally goes away. Best-effort: never let ref/unref break VM lifecycle.
|
|
3564
|
+
*/
|
|
3565
|
+
function applySharedSidecarHold(state) {
|
|
3566
|
+
const child = state.sharedChild;
|
|
3567
|
+
if (!child)
|
|
3568
|
+
return;
|
|
3569
|
+
const hold = (state.eventLoopHolds ?? 0) > 0;
|
|
3570
|
+
for (const handle of [child, child.stdin, child.stdout, child.stderr]) {
|
|
3571
|
+
if (!handle)
|
|
3572
|
+
continue;
|
|
3573
|
+
try {
|
|
3574
|
+
if (hold)
|
|
3575
|
+
handle.ref?.();
|
|
3576
|
+
else
|
|
3577
|
+
handle.unref?.();
|
|
3578
|
+
}
|
|
3579
|
+
catch {
|
|
3580
|
+
// ref/unref is an optimization, not correctness-critical
|
|
3581
|
+
}
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
/**
|
|
3585
|
+
* Take a hold for the entire create→use→dispose lifetime of one VM lease. Taken
|
|
3586
|
+
* BEFORE VM creation starts (not when the lease lands in `activeLeases`) so a VM
|
|
3587
|
+
* that is still mid-creation keeps the sidecar ref'd and a concurrent dispose
|
|
3588
|
+
* cannot unref it out from under the in-flight create.
|
|
3589
|
+
*/
|
|
3590
|
+
function acquireSharedSidecarHold(state) {
|
|
3591
|
+
state.eventLoopHolds = (state.eventLoopHolds ?? 0) + 1;
|
|
3592
|
+
if (state.eventLoopHolds === 1)
|
|
3593
|
+
applySharedSidecarHold(state);
|
|
3594
|
+
}
|
|
3595
|
+
/** Release a hold taken by {@link acquireSharedSidecarHold}; unref at 0. */
|
|
3596
|
+
function releaseSharedSidecarHold(state) {
|
|
3597
|
+
const current = state.eventLoopHolds ?? 0;
|
|
3598
|
+
if (current <= 0) {
|
|
3599
|
+
// The `holdReleased` guard makes each lease release exactly once, so this
|
|
3600
|
+
// should be unreachable. Warn rather than silently floor, per the repo's
|
|
3601
|
+
// no-silent-masking rule, so an accounting bug surfaces instead of hiding.
|
|
3602
|
+
state.eventLoopHolds = 0;
|
|
3603
|
+
console.warn("[agentos] shared sidecar event-loop hold released more than acquired");
|
|
3604
|
+
return;
|
|
3605
|
+
}
|
|
3606
|
+
state.eventLoopHolds = current - 1;
|
|
3607
|
+
if (state.eventLoopHolds === 0)
|
|
3608
|
+
applySharedSidecarHold(state);
|
|
3609
|
+
}
|
|
3610
|
+
/**
|
|
3611
|
+
* Spawn-once accessor for a sidecar handle's shared native process. Concurrent
|
|
3612
|
+
* callers await the same promise, so one `AgentOsSidecar` maps to exactly one
|
|
3613
|
+
* `agent-os-sidecar` OS process for its whole lifetime.
|
|
3614
|
+
*/
|
|
3615
|
+
function ensureSharedSidecarNativeProcess(sidecar) {
|
|
3616
|
+
const state = getSidecarState(sidecar);
|
|
3617
|
+
if (!state.nativeProcess) {
|
|
3618
|
+
ensureSidecarProcessExitCleanup();
|
|
3619
|
+
state.nativeProcess = (async () => {
|
|
3620
|
+
const client = SidecarProcess.spawn({
|
|
3621
|
+
cwd: REPO_ROOT,
|
|
3622
|
+
command: ensureNativeSidecarBinary(),
|
|
3623
|
+
args: [],
|
|
3624
|
+
frameTimeoutMs: NATIVE_SIDECAR_FRAME_TIMEOUT_MS,
|
|
3625
|
+
});
|
|
3626
|
+
// Track the child immediately — BEFORE the handshake await — so a
|
|
3627
|
+
// failed `authenticateAndOpenSession()` can still reap it (otherwise
|
|
3628
|
+
// the spawned child is untracked, unreapable, and pins the loop).
|
|
3629
|
+
state.sharedChild = sidecarChildHandle(client);
|
|
3630
|
+
if (!state.sharedChild) {
|
|
3631
|
+
// We reached into @rivet-dev/agentos-runtime-core internals to get the child for
|
|
3632
|
+
// idle-unref. If that shape ever changes this returns undefined and
|
|
3633
|
+
// the optimization silently stops working (one-shot scripts would
|
|
3634
|
+
// hang again). Make it loud rather than a silent regression.
|
|
3635
|
+
console.warn("[agentos] could not resolve the shared sidecar child handle; " +
|
|
3636
|
+
"standalone scripts may not exit cleanly after dispose(). " +
|
|
3637
|
+
"This usually means @rivet-dev/agentos-runtime-core internals changed.");
|
|
3638
|
+
}
|
|
3639
|
+
// Apply the current hold state to the just-spawned child.
|
|
3640
|
+
applySharedSidecarHold(state);
|
|
3641
|
+
try {
|
|
3642
|
+
const session = await client.authenticateAndOpenSession();
|
|
3643
|
+
return { client, session };
|
|
3644
|
+
}
|
|
3645
|
+
catch (error) {
|
|
3646
|
+
// Spawn/handshake failed: reap the child, drop the cached handle,
|
|
3647
|
+
// and CLEAR the rejected promise so the next create() retries
|
|
3648
|
+
// instead of permanently wedging on a rejected `nativeProcess`.
|
|
3649
|
+
try {
|
|
3650
|
+
state.sharedChild?.kill?.("SIGKILL");
|
|
3651
|
+
}
|
|
3652
|
+
catch {
|
|
3653
|
+
// already gone
|
|
3654
|
+
}
|
|
3655
|
+
state.sharedChild = undefined;
|
|
3656
|
+
state.nativeProcess = undefined;
|
|
3657
|
+
throw error;
|
|
3658
|
+
}
|
|
3659
|
+
})();
|
|
3660
|
+
}
|
|
3661
|
+
return state.nativeProcess;
|
|
3662
|
+
}
|
|
3663
|
+
/** Dispose a sidecar handle's shared native process, if one was spawned. */
|
|
3664
|
+
async function disposeSharedSidecarNativeProcess(state) {
|
|
3665
|
+
const pending = state.nativeProcess;
|
|
3666
|
+
if (!pending) {
|
|
3667
|
+
return;
|
|
3668
|
+
}
|
|
3669
|
+
state.nativeProcess = undefined;
|
|
3670
|
+
// The cached child is now dead; drop it (symmetric with the assignment in
|
|
3671
|
+
// ensureSharedSidecarNativeProcess). We deliberately do NOT zero
|
|
3672
|
+
// `eventLoopHolds` here: this runs only from `AgentOsSidecar.dispose()`, which
|
|
3673
|
+
// has already set the handle to `disposing` (so no new lease can acquire) and
|
|
3674
|
+
// drained `activeLeases`; the disposed handle's state is then abandoned. Force-
|
|
3675
|
+
// zeroing a shared counter could clobber a hold on a freshly re-acquired
|
|
3676
|
+
// process generation, so it is left to the balanced acquire/release pairs.
|
|
3677
|
+
state.sharedChild = undefined;
|
|
3678
|
+
try {
|
|
3679
|
+
const { client } = await pending;
|
|
3680
|
+
await client.dispose();
|
|
3681
|
+
}
|
|
3682
|
+
catch {
|
|
3683
|
+
// Process may have already exited; nothing to reclaim.
|
|
3684
|
+
}
|
|
3685
|
+
}
|
|
3439
3686
|
export class AgentOsSidecar {
|
|
3440
3687
|
constructor(sidecarId, placement, sharedPool) {
|
|
3441
3688
|
sidecarStates.set(this, {
|
|
@@ -3470,6 +3717,8 @@ export class AgentOsSidecar {
|
|
|
3470
3717
|
}
|
|
3471
3718
|
state.activeLeases.clear();
|
|
3472
3719
|
state.description.activeVmCount = 0;
|
|
3720
|
+
// Tear down the shared native process after all leased VMs are gone.
|
|
3721
|
+
await disposeSharedSidecarNativeProcess(state);
|
|
3473
3722
|
state.description.state = "disposed";
|
|
3474
3723
|
if (state.sharedPool && sharedSidecars.get(state.sharedPool) === this) {
|
|
3475
3724
|
sharedSidecars.delete(state.sharedPool);
|
|
@@ -3531,6 +3780,18 @@ async function leaseAgentOsSidecarVm(sidecar, options) {
|
|
|
3531
3780
|
return transport;
|
|
3532
3781
|
},
|
|
3533
3782
|
});
|
|
3783
|
+
// Hold the shared sidecar's event-loop ref for this lease's WHOLE lifetime —
|
|
3784
|
+
// taken now, before VM creation, so a concurrent dispose cannot unref the
|
|
3785
|
+
// sidecar while this create is still in flight. Released exactly once on
|
|
3786
|
+
// dispose or on a failed create.
|
|
3787
|
+
acquireSharedSidecarHold(state);
|
|
3788
|
+
let holdReleased = false;
|
|
3789
|
+
const releaseHold = () => {
|
|
3790
|
+
if (holdReleased)
|
|
3791
|
+
return;
|
|
3792
|
+
holdReleased = true;
|
|
3793
|
+
releaseSharedSidecarHold(state);
|
|
3794
|
+
};
|
|
3534
3795
|
let disposed = false;
|
|
3535
3796
|
let leaseRecord;
|
|
3536
3797
|
try {
|
|
@@ -3555,6 +3816,10 @@ async function leaseAgentOsSidecarVm(sidecar, options) {
|
|
|
3555
3816
|
state.activeLeases.delete(leaseRecord);
|
|
3556
3817
|
state.description.activeVmCount = state.activeLeases.size;
|
|
3557
3818
|
await client.dispose();
|
|
3819
|
+
// Release this lease's hold; the shared sidecar is unref'd only
|
|
3820
|
+
// once the last hold (across all in-flight + active leases) drops,
|
|
3821
|
+
// so a one-shot host process can then exit on its own.
|
|
3822
|
+
releaseHold();
|
|
3558
3823
|
},
|
|
3559
3824
|
};
|
|
3560
3825
|
leaseRecord = {
|
|
@@ -3566,6 +3831,7 @@ async function leaseAgentOsSidecarVm(sidecar, options) {
|
|
|
3566
3831
|
}
|
|
3567
3832
|
catch (error) {
|
|
3568
3833
|
await client.dispose().catch(() => { });
|
|
3834
|
+
releaseHold();
|
|
3569
3835
|
throw error;
|
|
3570
3836
|
}
|
|
3571
3837
|
}
|