@rivet-dev/agentos-core0.4.953.2 kB
import { execFileSync } from "node:child_process";
import * as fsSync from "node:fs";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as posixPath from "node:path/posix";
import { fileURLToPath } from "node:url";
import { resolvePublishedSidecarBinary } from "./sidecar/binary.js";
import { findCargoBinary, resolveCargoBinary } from "./sidecar/cargo.js";
import { serializePermissionsForSidecar } from "./sidecar/permissions.js";
import { NativeSidecarKernelProxy, SidecarProcess, serializeMountConfigForSidecar, } from "./sidecar/rpc-client.js";
export const AF_INET = 2;
export const AF_UNIX = 1;
export const SOCK_STREAM = 1;
export const SOCK_DGRAM = 2;
export const SIGTERM = 15;
const NODE_RUNTIME_BOOTSTRAP_COMMANDS = [
"node",
"npm",
"npx",
"python",
"python3",
];
const KERNEL_POSIX_BOOTSTRAP_DIRS = [
"/dev",
"/proc",
"/tmp",
"/bin",
"/lib",
"/sbin",
"/boot",
"/etc",
"/root",
"/run",
"/srv",
"/sys",
"/opt",
"/mnt",
"/media",
"/home",
"/home/agentos",
"/workspace",
"/usr",
"/usr/bin",
"/usr/games",
"/usr/include",
"/usr/lib",
"/usr/libexec",
"/usr/man",
"/usr/local",
"/usr/local/bin",
"/usr/sbin",
"/usr/share",
"/usr/share/man",
"/var",
"/var/cache",
"/var/empty",
"/var/lib",
"/var/lock",
"/var/log",
"/var/run",
"/var/spool",
"/var/tmp",
];
const REPO_ROOT = fileURLToPath(new URL("../../..", import.meta.url));
const SIDECAR_BINARY = path.join(REPO_ROOT, "target/debug/agentos-native-sidecar");
const SIDECAR_BUILD_INPUTS = [
path.join(REPO_ROOT, "Cargo.toml"),
path.join(REPO_ROOT, "Cargo.lock"),
path.join(REPO_ROOT, "crates/bridge"),
path.join(REPO_ROOT, "crates/build-support"),
path.join(REPO_ROOT, "crates/execution"),
path.join(REPO_ROOT, "crates/kernel"),
path.join(REPO_ROOT, "crates/native-sidecar"),
path.join(REPO_ROOT, "crates/native-sidecar-core"),
path.join(REPO_ROOT, "crates/sidecar-protocol"),
path.join(REPO_ROOT, "crates/v8-runtime"),
path.join(REPO_ROOT, "crates/vfs"),
path.join(REPO_ROOT, "crates/vm-config"),
path.join(REPO_ROOT, "packages/build-tools/bridge-src"),
path.join(REPO_ROOT, "packages/build-tools/package.json"),
path.join(REPO_ROOT, "packages/build-tools/scripts/build-v8-bridge.mjs"),
path.join(REPO_ROOT, "packages/core/fixtures/base-filesystem.json"),
path.join(REPO_ROOT, "packages/runtime-core/fixtures/base-filesystem.json"),
path.join(REPO_ROOT, "pnpm-lock.yaml"),
];
let ensuredSidecarBinary = null;
export class KernelError extends Error {
code;
constructor(code, message) {
super(message.startsWith(`${code}:`) ? message : `${code}: ${message}`);
this.name = "KernelError";
this.code = code;
}
}
function normalizePath(inputPath) {
if (!inputPath)
return "/";
let normalized = inputPath.startsWith("/") ? inputPath : `/${inputPath}`;
normalized = normalized.replace(/\/+/g, "/");
if (normalized.length > 1 && normalized.endsWith("/")) {
normalized = normalized.slice(0, -1);
}
const parts = normalized.split("/");
const resolved = [];
for (const part of parts) {
if (part === "" || part === ".")
continue;
if (part === "..") {
resolved.pop();
continue;
}
resolved.push(part);
}
return resolved.length === 0 ? "/" : `/${resolved.join("/")}`;
}
function dirnameVirtual(inputPath) {
const normalized = normalizePath(inputPath);
if (normalized === "/")
return "/";
const parts = normalized.split("/").filter(Boolean);
return parts.length <= 1 ? "/" : `/${parts.slice(0, -1).join("/")}`;
}
export class NodeFileSystem {
rootPath;
constructor(options) {
this.rootPath = fsSync.realpathSync(options.root);
}
normalizeTarget(targetPath) {
const normalized = normalizePath(targetPath).replace(/^\/+/, "");
const resolved = path.resolve(this.rootPath, normalized);
if (resolved !== this.rootPath &&
!resolved.startsWith(`${this.rootPath}${path.sep}`)) {
throw errnoError("EACCES", `path escapes root '${targetPath}'`);
}
return resolved;
}
toStat(stat) {
const posixStat = stat;
return {
mode: stat.mode,
size: stat.size,
blocks: posixStat.blocks ?? (stat.size === 0 ? 0 : Math.ceil(stat.size / 512)),
dev: posixStat.dev ?? 1,
rdev: posixStat.rdev ?? 0,
isDirectory: stat.isDirectory(),
isSymbolicLink: stat.isSymbolicLink(),
atimeMs: Math.trunc(stat.atimeMs),
mtimeMs: Math.trunc(stat.mtimeMs),
ctimeMs: Math.trunc(stat.ctimeMs),
birthtimeMs: Math.trunc(stat.birthtimeMs),
ino: stat.ino,
nlink: stat.nlink,
uid: stat.uid,
gid: stat.gid,
};
}
async readFile(targetPath) {
return new Uint8Array(await fs.readFile(this.normalizeTarget(targetPath)));
}
async readTextFile(targetPath) {
return fs.readFile(this.normalizeTarget(targetPath), "utf8");
}
async readDir(targetPath) {
return fs.readdir(this.normalizeTarget(targetPath));
}
async readDirWithTypes(targetPath) {
const entries = await fs.readdir(this.normalizeTarget(targetPath), {
withFileTypes: true,
});
return entries.map((entry) => ({
name: entry.name,
isDirectory: entry.isDirectory(),
isSymbolicLink: entry.isSymbolicLink(),
}));
}
async writeFile(targetPath, content) {
const resolved = this.normalizeTarget(targetPath);
await fs.mkdir(path.dirname(resolved), { recursive: true });
await fs.writeFile(resolved, content);
}
async createDir(targetPath) {
await fs.mkdir(this.normalizeTarget(targetPath));
}
async mkdir(targetPath, options) {
await fs.mkdir(this.normalizeTarget(targetPath), {
recursive: options?.recursive ?? true,
});
}
async exists(targetPath) {
try {
await fs.access(this.normalizeTarget(targetPath));
return true;
}
catch {
return false;
}
}
async stat(targetPath) {
return this.toStat(await fs.stat(this.normalizeTarget(targetPath)));
}
async removeFile(targetPath) {
await fs.unlink(this.normalizeTarget(targetPath));
}
async removeDir(targetPath) {
await fs.rmdir(this.normalizeTarget(targetPath));
}
async rename(oldPath, newPath) {
const nextPath = this.normalizeTarget(newPath);
await fs.mkdir(path.dirname(nextPath), { recursive: true });
await fs.rename(this.normalizeTarget(oldPath), nextPath);
}
async realpath(targetPath) {
const real = await fs.realpath(this.normalizeTarget(targetPath));
const relative = path.relative(this.rootPath, real);
return relative ? `/${relative.split(path.sep).join("/")}` : "/";
}
async symlink(target, linkPath) {
const resolvedLink = this.normalizeTarget(linkPath);
await fs.mkdir(path.dirname(resolvedLink), { recursive: true });
await fs.symlink(target, resolvedLink);
}
async readlink(targetPath) {
return fs.readlink(this.normalizeTarget(targetPath));
}
async lstat(targetPath) {
return this.toStat(await fs.lstat(this.normalizeTarget(targetPath)));
}
async link(oldPath, newPath) {
await fs.link(this.normalizeTarget(oldPath), this.normalizeTarget(newPath));
}
async chmod(targetPath, mode) {
await fs.chmod(this.normalizeTarget(targetPath), mode);
}
async chown(targetPath, uid, gid) {
await fs.chown(this.normalizeTarget(targetPath), uid, gid);
}
async utimes(targetPath, atime, mtime) {
await fs.utimes(this.normalizeTarget(targetPath), atime / 1000, mtime / 1000);
}
async truncate(targetPath, length) {
await fs.truncate(this.normalizeTarget(targetPath), length);
}
async pread(targetPath, offset, length) {
const handle = await fs.open(this.normalizeTarget(targetPath), "r");
try {
const buffer = Buffer.alloc(length);
const { bytesRead } = await handle.read(buffer, 0, length, offset);
return new Uint8Array(buffer.buffer, buffer.byteOffset, bytesRead);
}
finally {
await handle.close();
}
}
async pwrite(targetPath, offset, data) {
const handle = await fs.open(this.normalizeTarget(targetPath), "r+");
try {
await handle.write(data, 0, data.length, offset);
}
finally {
await handle.close();
}
}
}
function permissionAllows(mode) {
return mode !== "deny";
}
function globMatches(pattern, value) {
const escaped = pattern.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
const expression = escaped.replace(/\*/g, ".*");
return new RegExp(`^${expression}$`).test(value);
}
function envPolicyAllows(policy, name) {
if (policy === undefined) {
return true;
}
if (typeof policy === "string") {
return permissionAllows(policy);
}
let mode = policy.default ?? "deny";
for (const rule of policy.rules) {
const operationsMatch = !rule.operations ||
rule.operations.length === 0 ||
rule.operations.includes("read");
const patternsMatch = !rule.patterns ||
rule.patterns.length === 0 ||
rule.patterns.some((pattern) => globMatches(pattern, name));
if (operationsMatch && patternsMatch) {
mode = rule.mode;
}
}
return permissionAllows(mode);
}
export const allowAllFs = "allow";
export const allowAllNetwork = "allow";
export const allowAllChildProcess = "allow";
export const allowAllProcess = "allow";
export const allowAllEnv = "allow";
export const allowAll = {
fs: allowAllFs,
network: allowAllNetwork,
childProcess: allowAllChildProcess,
process: allowAllProcess,
env: allowAllEnv,
};
export function filterEnv(env, permissions) {
const input = env ?? {};
if (!permissions?.env)
return { ...input };
const output = {};
for (const [name, value] of Object.entries(input)) {
if (envPolicyAllows(permissions.env, name)) {
output[name] = value;
}
}
return output;
}
export function createProcessScopedFileSystem(filesystem) {
return filesystem;
}
export async function exists(filesystem, targetPath) {
return filesystem.exists(targetPath);
}
export async function stat(filesystem, targetPath) {
return filesystem.stat(targetPath);
}
export async function rename(filesystem, oldPath, newPath) {
return filesystem.rename(oldPath, newPath);
}
export async function readDirWithTypes(filesystem, targetPath) {
return filesystem.readDirWithTypes(targetPath);
}
export async function mkdir(filesystem, targetPath, options) {
return filesystem.mkdir(targetPath, options);
}
export function createNodeHostCommandExecutor() {
return {
spawn() {
throw new Error("createNodeHostCommandExecutor is not supported on the native runtime path");
},
};
}
export function createKernelCommandExecutor(kernel) {
return {
spawn(command, args, options) {
return kernel.spawn(command, args, options);
},
};
}
export function createKernelVfsAdapter(kernelVfs) {
return kernelVfs;
}
export function createHostFallbackVfs(base) {
return base;
}
export function isPrivateIp(host) {
return (host === "localhost" ||
host === "127.0.0.1" ||
host.startsWith("10.") ||
host.startsWith("192.168.") ||
/^172\.(1[6-9]|2\d|3[0-1])\./.test(host));
}
export function createNodeHostNetworkAdapter() {
return createDefaultNetworkAdapter();
}
export function createDefaultNetworkAdapter() {
return {
async fetch(url, options) {
const response = await globalThis.fetch(url, {
method: options?.method ?? "GET",
headers: options?.headers,
body: options?.body,
});
const headers = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
return {
ok: response.ok,
status: response.status,
statusText: response.statusText,
headers,
body: await response.text(),
url: response.url,
redirected: response.redirected,
};
},
async dnsLookup(hostname) {
return { address: hostname, family: hostname.includes(":") ? 6 : 4 };
},
async httpRequest(url, options) {
const response = await globalThis.fetch(url, {
method: options?.method ?? "GET",
headers: options?.headers,
body: options?.body,
});
const headers = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
return {
status: response.status,
statusText: response.statusText,
headers,
body: await response.text(),
url: response.url,
};
},
};
}
export function createNodeDriver(options = {}) {
return {
filesystem: options.filesystem,
network: options.networkAdapter,
commandExecutor: options.commandExecutor,
permissions: options.permissions,
mounts: options.mounts ?? [],
runtime: {
process: options.processConfig ?? {},
os: options.osConfig ?? {},
},
};
}
export class NodeExecutionDriver {
options;
network;
constructor(options) {
this.options = options;
this.network = options.system.network;
}
async exec() {
throw new Error("NodeExecutionDriver is not available after the native runtime migration");
}
async run() {
throw new Error("NodeExecutionDriver is not available after the native runtime migration");
}
dispose() {
void this.options;
}
async terminate() { }
}
export class NodeRuntime extends NodeExecutionDriver {
}
export function createNodeRuntimeDriverFactory() {
return {
createRuntimeDriver(options) {
return new NodeRuntime(options);
},
};
}
export const WASMVM_COMMANDS = Object.freeze([
"sh",
"bash",
"grep",
"egrep",
"fgrep",
"rg",
"sed",
"awk",
"jq",
"yq",
"find",
"fd",
"cat",
"chmod",
"column",
"cp",
"dd",
"diff",
"du",
"expr",
"file",
"head",
"ln",
"logname",
"ls",
"mkdir",
"mktemp",
"mv",
"pathchk",
"rev",
"rm",
"sleep",
"sort",
"split",
"stat",
"strings",
"tac",
"tail",
"test",
"[",
"touch",
"tree",
"tsort",
"whoami",
"gzip",
"gunzip",
"zcat",
"tar",
"zip",
"unzip",
"sqlite3",
"curl",
"git",
"git-remote-http",
"git-remote-https",
"env",
"envsubst",
"nice",
"nohup",
"stdbuf",
"timeout",
"xargs",
"base32",
"base64",
"basenc",
"basename",
"comm",
"cut",
"dircolors",
"dirname",
"echo",
"expand",
"factor",
"false",
"fmt",
"fold",
"join",
"nl",
"numfmt",
"od",
"paste",
"printenv",
"printf",
"ptx",
"seq",
"shuf",
"tr",
"true",
"unexpand",
"uniq",
"wc",
"yes",
"b2sum",
"cksum",
"md5sum",
"sha1sum",
"sha224sum",
"sha256sum",
"sha384sum",
"sha512sum",
"sum",
"link",
"pwd",
"readlink",
"realpath",
"rmdir",
"shred",
"tee",
"truncate",
"unlink",
"arch",
"date",
"nproc",
"uname",
"dir",
"vdir",
"hostname",
"hostid",
"more",
"sync",
"tty",
"chcon",
"runcon",
"chgrp",
"chown",
"chroot",
"df",
"groups",
"id",
"install",
"kill",
"mkfifo",
"mknod",
"pinky",
"who",
"users",
"uptime",
"stty",
]);
export const DEFAULT_FIRST_PARTY_TIERS = Object.freeze({
sh: "full",
bash: "full",
env: "full",
timeout: "full",
xargs: "full",
nice: "full",
nohup: "full",
stdbuf: "full",
git: "full",
"git-remote-http": "full",
"git-remote-https": "full",
grep: "read-only",
egrep: "read-only",
fgrep: "read-only",
rg: "read-only",
cat: "read-only",
head: "read-only",
tail: "read-only",
wc: "read-only",
sort: "read-only",
uniq: "read-only",
diff: "read-only",
find: "read-only",
fd: "read-only",
tree: "read-only",
file: "read-only",
du: "read-only",
ls: "read-only",
dir: "read-only",
vdir: "read-only",
strings: "read-only",
stat: "read-only",
rev: "read-only",
column: "read-only",
cut: "read-only",
tr: "read-only",
paste: "read-only",
join: "read-only",
fold: "read-only",
expand: "read-only",
nl: "read-only",
od: "read-only",
comm: "read-only",
basename: "read-only",
dirname: "read-only",
realpath: "read-only",
readlink: "read-only",
pwd: "read-only",
echo: "read-only",
envsubst: "read-only",
printf: "read-only",
true: "read-only",
false: "read-only",
yes: "read-only",
seq: "read-only",
test: "read-only",
"[": "read-only",
expr: "read-only",
factor: "read-only",
date: "read-only",
uname: "read-only",
nproc: "read-only",
whoami: "read-only",
id: "read-only",
groups: "read-only",
base64: "read-only",
md5sum: "read-only",
sha256sum: "read-only",
tac: "read-only",
tsort: "read-only",
curl: "full",
sqlite3: "read-write",
});
class NativeRuntimeDescriptor {
kind;
name;
commands;
commandDirs;
constructor(kind, name, commands, commandDirs) {
this.kind = kind;
this.name = name;
this.commands = commands;
this.commandDirs = commandDirs;
}
}
function normalizeCommandLookup(command) {
return path.posix.basename(command);
}
function isWasmBinaryFile(filePath) {
try {
const header = fsSync.readFileSync(filePath, { encoding: null });
return (header.length >= 4 &&
header[0] === 0x00 &&
header[1] === 0x61 &&
header[2] === 0x73 &&
header[3] === 0x6d);
}
catch {
return false;
}
}
function discoverWasmCommandEntries(commandDirs) {
const discovered = [];
const seen = new Set();
for (const commandDir of commandDirs) {
let entries;
try {
entries = fsSync
.readdirSync(commandDir)
.sort((left, right) => left.localeCompare(right));
}
catch {
continue;
}
for (const entry of entries) {
if (entry.startsWith("."))
continue;
if (seen.has(entry))
continue;
const fullPath = path.join(commandDir, entry);
if (isWasmBinaryFile(fullPath)) {
seen.add(entry);
discovered.push({
name: entry,
hostPath: fullPath,
});
continue;
}
try {
const realPath = fsSync.realpathSync(fullPath);
if (isWasmBinaryFile(realPath)) {
seen.add(entry);
discovered.push({
name: entry,
hostPath: fullPath,
});
}
}
catch { }
}
}
return discovered;
}
class WasmVmRuntimeDescriptor {
kind = "wasmvm";
name = "wasmvm";
commands = [];
commandDirs;
_commandPaths = new Map();
_moduleCache = new Map();
constructor(options) {
this.commandDirs =
options.commandDirs && options.commandDirs.length > 0
? [...options.commandDirs]
: undefined;
if (options.commandDirs && options.commandDirs.length > 0) {
this.refreshDiscovery();
return;
}
this.commands.push(...WASMVM_COMMANDS);
if (options.wasmBinaryPath) {
console.warn("createWasmVmRuntime({ wasmBinaryPath }) is deprecated; use commandDirs instead.");
}
}
init(_kernel) {
if (this.commandDirs && this.commandDirs.length > 0) {
this.refreshDiscovery();
}
}
tryResolve(command) {
if (!this.commandDirs || this.commandDirs.length === 0) {
return false;
}
const normalized = normalizeCommandLookup(command);
if (this._commandPaths.has(normalized)) {
return true;
}
this.refreshDiscovery();
return this._commandPaths.has(normalized);
}
recordModuleExecution(command) {
const normalized = normalizeCommandLookup(command);
if (this._commandPaths.has(normalized) ||
((!this.commandDirs || this.commandDirs.length === 0) &&
this.commands.includes(normalized))) {
this._moduleCache.set(normalized, true);
}
}
refreshDiscovery() {
if (!this.commandDirs || this.commandDirs.length === 0) {
return;
}
const discovered = discoverWasmCommandEntries(this.commandDirs);
this.commands.length = 0;
this._commandPaths.clear();
for (const entry of discovered) {
this.commands.push(entry.name);
this._commandPaths.set(entry.name, entry.hostPath);
}
}
}
export function createWasmVmRuntime(options = {}) {
return new WasmVmRuntimeDescriptor(options);
}
export function createNodeRuntime() {
return new NativeRuntimeDescriptor("node", "node", ["node", "npm", "npx"]);
}
function latestMtimeMs(targetPath) {
try {
const stats = fsSync.statSync(targetPath);
if (!stats.isDirectory()) {
return stats.mtimeMs;
}
let latest = stats.mtimeMs;
for (const entry of fsSync.readdirSync(targetPath)) {
latest = Math.max(latest, latestMtimeMs(path.join(targetPath, entry)));
}
return latest;
}
catch {
return 0;
}
}
function sidecarBinaryNeedsBuild() {
if (!fsSync.existsSync(SIDECAR_BINARY)) {
return true;
}
const binaryMtime = latestMtimeMs(SIDECAR_BINARY);
return SIDECAR_BUILD_INPUTS.some((inputPath) => latestMtimeMs(inputPath) > binaryMtime);
}
function ensureNativeSidecarBinary() {
// A published install has no in-repo Cargo workspace to build from: resolve
// the prebuilt platform binary (or the AGENTOS_SIDECAR_BIN override).
if (process.env.AGENTOS_SIDECAR_BIN ||
!fsSync.existsSync(path.join(REPO_ROOT, "Cargo.toml"))) {
return resolvePublishedSidecarBinary();
}
if (ensuredSidecarBinary &&
fsSync.existsSync(ensuredSidecarBinary) &&
!sidecarBinaryNeedsBuild()) {
return ensuredSidecarBinary;
}
if (sidecarBinaryNeedsBuild()) {
const cargoBinary = findCargoBinary();
if (cargoBinary) {
execFileSync(cargoBinary, ["build", "-q", "-p", "agentos-native-sidecar"], {
cwd: REPO_ROOT,
stdio: "pipe",
});
}
else if (!fsSync.existsSync(SIDECAR_BINARY)) {
execFileSync(resolveCargoBinary(), ["build", "-q", "-p", "agentos-native-sidecar"], {
cwd: REPO_ROOT,
stdio: "pipe",
});
}
}
ensuredSidecarBinary = SIDECAR_BINARY;
return ensuredSidecarBinary;
}
function rootEntryExecutable(kind, mode) {
return kind === "file" && (mode & 0o111) !== 0;
}
function createBootstrapEntries() {
return [
{
path: "/",
kind: "directory",
mode: 0o755,
uid: 0,
gid: 0,
executable: false,
},
...KERNEL_POSIX_BOOTSTRAP_DIRS.map((entryPath) => ({
path: entryPath,
kind: "directory",
mode: entryPath === "/tmp" || entryPath === "/var/tmp"
? 0o1777
: entryPath === "/home/agentos"
? 0o2755
: 0o755,
uid: entryPath === "/home/agentos" || entryPath === "/workspace" ? 1000 : 0,
gid: entryPath === "/home/agentos" || entryPath === "/workspace" ? 1000 : 0,
executable: false,
})),
{
path: "/usr/bin/env",
kind: "file",
mode: 0o644,
uid: 0,
gid: 0,
content: "",
encoding: "utf8",
executable: false,
},
];
}
function mergeRootFilesystemEntries(baseEntries, overrideEntries) {
const merged = new Map();
for (const entry of baseEntries) {
merged.set(entry.path, entry);
}
for (const entry of overrideEntries) {
merged.set(entry.path, entry);
}
return [...merged.values()];
}
function rootFilesystemEntriesForConfig(entries) {
return entries.map((entry) => ({
...entry,
executable: entry.executable ?? false,
}));
}
async function snapshotFilesystemEntries(filesystem, targetPath = "/", output = [], options) {
const passthroughDirectories = options?.passthroughDirectories;
const passthroughDirectory = passthroughDirectories?.has(targetPath) ?? false;
const statInfo = targetPath === "/" || passthroughDirectory
? await filesystem.stat(targetPath)
: await filesystem.lstat(targetPath);
if (statInfo.isSymbolicLink) {
output.push({
path: targetPath,
kind: "symlink",
mode: statInfo.mode,
uid: statInfo.uid,
gid: statInfo.gid,
target: await filesystem.readlink(targetPath),
executable: false,
});
return output;
}
if (statInfo.isDirectory) {
output.push({
path: targetPath,
kind: "directory",
mode: statInfo.mode,
uid: statInfo.uid,
gid: statInfo.gid,
executable: false,
});
if (passthroughDirectory) {
return output;
}
const children = (await filesystem.readDirWithTypes(targetPath))
.map((entry) => entry.name)
.filter((name) => name !== "." && name !== "..")
.sort((left, right) => left.localeCompare(right));
for (const child of children) {
const childPath = targetPath === "/"
? posixPath.join("/", child)
: posixPath.join(targetPath, child);
await snapshotFilesystemEntries(filesystem, childPath, output, options);
}
return output;
}
output.push({
path: targetPath,
kind: "file",
mode: statInfo.mode,
uid: statInfo.uid,
gid: statInfo.gid,
content: Buffer.from(await filesystem.readFile(targetPath)).toString("base64"),
encoding: "base64",
executable: rootEntryExecutable("file", statInfo.mode),
});
return output;
}
async function materializeSnapshotEntriesIntoVm(client, session, vm, entries) {
for (const entry of entries) {
if (entry.path === "/") {
continue;
}
if (entry.kind === "directory") {
await client.mkdir(session, vm, entry.path, { recursive: true });
}
else if (entry.kind === "file") {
await client.writeFile(session, vm, entry.path, decodeRootFilesystemEntryContent(entry));
}
else {
await client.symlink(session, vm, entry.target ?? "", entry.path);
continue;
}
if (typeof entry.mode === "number") {
await client.chmod(session, vm, entry.path, entry.mode);
}
if (typeof entry.uid === "number" && typeof entry.gid === "number") {
await client.chown(session, vm, entry.path, entry.uid, entry.gid);
}
}
}
function decodeRootFilesystemEntryContent(entry) {
const content = entry.content ?? "";
if (entry.encoding === "base64") {
return new Uint8Array(Buffer.from(content, "base64"));
}
return new TextEncoder().encode(content);
}
const NODE_FILESYSTEM_ROOT_PASSTHROUGH_DIRS = ["node_modules"];
function planNodeFilesystemPassthroughMounts(filesystem, existingMounts) {
if (!(filesystem instanceof NodeFileSystem)) {
return {
mounts: [],
passthroughDirectories: new Set(),
};
}
const passthroughDirectories = new Set();
const existingGuestPaths = new Set(existingMounts.map((mount) => mount.path));
const mounts = [];
for (const directoryName of NODE_FILESYSTEM_ROOT_PASSTHROUGH_DIRS) {
const guestPath = normalizePath(`/${directoryName}`);
const hostPath = path.join(filesystem.rootPath, directoryName);
let statInfo;
try {
statInfo = fsSync.statSync(hostPath);
}
catch {
continue;
}
if (!statInfo.isDirectory()) {
continue;
}
passthroughDirectories.add(guestPath);
if (existingGuestPaths.has(guestPath)) {
continue;
}
mounts.push({
path: guestPath,
fs: new NodeFileSystem({ root: hostPath }),
readOnly: true,
});
}
return {
mounts,
passthroughDirectories,
};
}
class DeferredFileSystem {
getFilesystem;
constructor(getFilesystem) {
this.getFilesystem = getFilesystem;
}
filesystem() {
const filesystem = this.getFilesystem();
if (!filesystem) {
throw new Error("kernel filesystem is not ready; mount a runtime first");
}
return filesystem;
}
readFile(path) {
return this.filesystem().readFile(path);
}
readTextFile(path) {
return this.filesystem().readTextFile(path);
}
readDir(path) {
return this.filesystem().readDir(path);
}
readDirWithTypes(path) {
return this.filesystem().readDirWithTypes(path);
}
writeFile(path, content) {
return this.filesystem().writeFile(path, content);
}
createDir(path) {
return this.filesystem().createDir(path);
}
mkdir(path, options) {
return this.filesystem().mkdir(path, options);
}
exists(path) {
return this.filesystem().exists(path);
}
stat(path) {
return this.filesystem().stat(path);
}
removeFile(path) {
return this.filesystem().removeFile(path);
}
removeDir(path) {
return this.filesystem().removeDir(path);
}
rename(oldPath, newPath) {
return this.filesystem().rename(oldPath, newPath);
}
realpath(path) {
return this.filesystem().realpath(path);
}
symlink(target, linkPath) {
return this.filesystem().symlink(target, linkPath);
}
readlink(path) {
return this.filesystem().readlink(path);
}
lstat(path) {
return this.filesystem().lstat(path);
}
link(oldPath, newPath) {
return this.filesystem().link(oldPath, newPath);
}
chmod(path, mode) {
return this.filesystem().chmod(path, mode);
}
chown(path, uid, gid) {
return this.filesystem().chown(path, uid, gid);
}
utimes(path, atime, mtime) {
return this.filesystem().utimes(path, atime, mtime);
}
truncate(path, length) {
return this.filesystem().truncate(path, length);
}
pread(path, offset, length) {
return this.filesystem().pread(path, offset, length);
}
pwrite(path, offset, data) {
return this.filesystem().pwrite(path, offset, data);
}
}
const VIRTUAL_FILESYSTEM_METHOD_NAMES = [
"readFile",
"readTextFile",
"readDir",
"readDirWithTypes",
"writeFile",
"createDir",
"mkdir",
"exists",
"stat",
"removeFile",
"removeDir",
"rename",
"realpath",
"symlink",
"readlink",
"lstat",
"link",
"chmod",
"chown",
"utimes",
"truncate",
"pread",
"pwrite",
];
const DEFAULT_LIVE_FILESYSTEM_SYNC_MAX_BYTES = 64 * 1024 * 1024;
class LiveFilesystemSyncLimitError extends Error {
code = "ERR_AGENTOS_LIVE_FILESYSTEM_SYNC_LIMIT";
}
function topLevelSyncRoot(targetPath) {
const normalized = normalizePath(targetPath);
const [first] = normalized.split("/").filter(Boolean);
return first ? `/${first}` : "/";
}
function collectLiveFilesystemSyncRoots(entries) {
const roots = new Set();
for (const entry of entries) {
if (entry.path === "/") {
continue;
}
roots.add(topLevelSyncRoot(entry.path));
}
return [...roots].sort((left, right) => left.localeCompare(right));
}
async function callBoundFilesystemMethod(methods, method, ...args) {
const delegate = methods[method];
if (!delegate) {
throw new Error(`filesystem method ${method} is unavailable`);
}
return (await delegate(...args));
}
async function ensureBoundParentDirectory(methods, targetPath) {
const parent = dirnameVirtual(targetPath);
if (parent === targetPath) {
return;
}
await callBoundFilesystemMethod(methods, "mkdir", parent, {
recursive: true,
});
}
async function syncLiveFilesystemToBoundMethods(live, methods, paths, maxBytes) {
const snapshot = [];
const usage = { bytes: 0 };
for (const targetPath of [...new Set(paths.map(normalizePath))].sort((left, right) => left.localeCompare(right))) {
if (!(await live.exists(targetPath).catch(() => false))) {
continue;
}
await snapshotLiveFilesystemPath(live, targetPath, snapshot, usage, maxBytes);
}
if (usage.bytes >= maxBytes * 0.8) {
process.emitWarning(`live filesystem sync retained ${usage.bytes} of ${maxBytes} bytes; ` +
"raise createKernel({ maxFilesystemBytes }) before increasing VM filesystem limits", { code: "AGENTOS_LIVE_FILESYSTEM_SYNC_NEAR_LIMIT" });
}
for (const entry of snapshot) {
await applyLiveFilesystemSnapshotEntry(methods, entry);
}
}
async function snapshotLiveFilesystemPath(live, targetPath, snapshot, usage, maxBytes, knownEntry) {
const stat = knownEntry ??
(targetPath === "/"
? await live.stat(targetPath)
: await live.lstat(targetPath));
if (stat.isSymbolicLink) {
snapshot.push({
kind: "symlink",
path: targetPath,
target: await live.readlink(targetPath),
});
return;
}
if (stat.isDirectory) {
snapshot.push({ kind: "directory", path: targetPath });
const children = (await live.readDirWithTypes(targetPath))
.filter((entry) => entry.name !== "." && entry.name !== "..")
.sort((left, right) => left.name.localeCompare(right.name));
for (const child of children) {
await snapshotLiveFilesystemPath(live, targetPath === "/"
? posixPath.join("/", child.name)
: posixPath.join(targetPath, child.name), snapshot, usage, maxBytes, child);
}
return;
}
const fileSize = "size" in stat && typeof stat.size === "number"
? stat.size
: (await live.lstat(targetPath)).size;
if (fileSize > maxBytes - usage.bytes) {
throw new LiveFilesystemSyncLimitError(`live filesystem sync for ${targetPath} exceeds ${maxBytes} bytes ` +
"(createKernel maxFilesystemBytes); raise createKernel({ maxFilesystemBytes })");
}
const content = await live.readFile(targetPath);
if (content.byteLength > maxBytes - usage.bytes) {
throw new LiveFilesystemSyncLimitError(`live filesystem sync for ${targetPath} exceeds ${maxBytes} bytes ` +
"(createKernel maxFilesystemBytes); raise createKernel({ maxFilesystemBytes })");
}
usage.bytes += content.byteLength;
snapshot.push({ kind: "file", path: targetPath, content });
}
async function applyLiveFilesystemSnapshotEntry(methods, entry) {
if (entry.kind === "directory") {
await callBoundFilesystemMethod(methods, "mkdir", entry.path, {
recursive: true,
});
return;
}
await ensureBoundParentDirectory(methods, entry.path);
if (entry.kind === "symlink") {
await callBoundFilesystemMethod(methods, "removeFile", entry.path).catch(() => { });
await callBoundFilesystemMethod(methods, "symlink", entry.target, entry.path);
return;
}
await callBoundFilesystemMethod(methods, "writeFile", entry.path, entry.content);
}
function bindLiveFilesystem(target, getFilesystem, maxBytes) {
const fallback = {};
for (const method of VIRTUAL_FILESYSTEM_METHOD_NAMES) {
const candidate = target[method];
if (typeof candidate === "function") {
fallback[method] = candidate.bind(target);
}
}
for (const method of VIRTUAL_FILESYSTEM_METHOD_NAMES) {
target[method] = (...args) => {
const filesystem = getFilesystem();
const delegate = filesystem
? filesystem[method].bind(filesystem)
: fallback[method];
if (!delegate) {
throw new Error(`kernel filesystem is not ready; mount a runtime before calling ${method}()`);
}
return delegate(...args);
};
}
return {
async syncFromLive(paths) {
const filesystem = getFilesystem();
if (!filesystem) {
return;
}
await syncLiveFilesystemToBoundMethods(filesystem, fallback, paths, maxBytes);
},
restore() {
for (const [method, delegate] of Object.entries(fallback)) {
target[method] = delegate;
}
},
};
}
function serializeLocalCompatMountForSidecar(mount) {
return (mount.sidecarMount ??
(mount.fs instanceof NodeFileSystem
? serializeMountConfigForSidecar({
path: mount.path,
readOnly: mount.readOnly,
plugin: {
id: "host_dir",
config: {
hostPath: mount.fs.rootPath,
readOnly: mount.readOnly,
},
},
})
: serializeMountConfigForSidecar({
path: mount.path,
driver: mount.fs,
readOnly: mount.readOnly,
})));
}
function makeLocalCompatMount(options) {
const localMount = {
path: normalizePath(options.path),
fs: options.fs,
readOnly: options.readOnly ?? false,
};
localMount.sidecarMount = serializeLocalCompatMountForSidecar(localMount);
return localMount;
}
class NativeKernel {
options;
env;
cwd;
commands = new Map();
processes = new Map();
socketTable;
processTable;
timerTable = {};
vfs;
client = null;
session = null;
vm = null;
proxy = null;
rootFilesystem = null;
readyPromise = null;
liveFilesystemBinding;
liveFilesystemSyncRoots = [];
pendingLocalMounts = [];
mountedCommandDirs = [];
mountedRuntimeDrivers = [];
loopbackExemptPorts;
constructor(options) {
this.options = options;
this.env = { ...(options.env ?? {}) };
this.cwd = options.cwd ?? "/workspace";
this.socketTable = {
hasHostNetworkAdapter: () => Boolean(options.hostNetworkAdapter),
findListener: (request) => this.proxy?.findListener(request) ?? null,
findBoundUdp: (request) => this.proxy?.findBoundUdp(request) ?? null,
};
this.processTable = {
getSignalState: (pid) => this.proxy?.getSignalState(pid) ?? {
handlers: new Map(),
},
};
this.loopbackExemptPorts = [...(options.loopbackExemptPorts ?? [])];
for (const mount of options.mounts ?? []) {
this.pendingLocalMounts.push(makeLocalCompatMount({
path: mount.path,
fs: mount.fs,
readOnly: mount.readOnly,
}));
}
this.vfs = new DeferredFileSystem(() => this.rootFilesystem);
this.liveFilesystemBinding = bindLiveFilesystem(this.options.filesystem, () => this.rootFilesystem, options.maxFilesystemBytes ?? DEFAULT_LIVE_FILESYSTEM_SYNC_MAX_BYTES);
}
get zombieTimerCount() {
return this.proxy?.zombieTimerCount ?? 0;
}
async configureRuntimeCommandStubs(commands, commandDirs = this.mountedCommandDirs) {
if (!this.client || !this.session || !this.vm) {
throw new Error("kernel is not ready");
}
const sidecarMounts = commandDirs.map((commandDir, index) => serializeMountConfigForSidecar({
path: `/__agentos/commands/${index}`,
readOnly: true,
plugin: {
id: "host_dir",
config: {
hostPath: commandDir,
readOnly: true,
},
},
}));
const localMounts = this.pendingLocalMounts.map((mount) => serializeLocalCompatMountForSidecar(mount));
const configuredVm = await this.client.configureVm(this.session, this.vm, {
mounts: [...localMounts, ...sidecarMounts],
loopbackExemptPorts: this.loopbackExemptPorts,
bootstrapCommands: [...new Set(commands)].sort((left, right) => left.localeCompare(right)),
});
return new Map(configuredVm.projectedCommands.map((command) => [
command.name,
command.guestPath,
]));
}
async mount(driver) {
await this.ensureReady();
if (!this.proxy || !this.client || !this.session || !this.vm) {
throw new Error("kernel is not ready");
}
await driver.init?.(this);
if (driver.kind === "node") {
for (const command of driver.commands) {
this.commands.set(command, "node");
}
this.mountedRuntimeDrivers.push(driver);
await this.configureRuntimeCommandStubs(driver.commands);
return;
}
const commandDirs = driver.commandDirs ?? [];
if (commandDirs.length === 0) {
for (const command of driver.commands) {
this.commands.set(command, "wasmvm");
}
this.mountedRuntimeDrivers.push(driver);
await this.configureRuntimeCommandStubs(driver.commands);
return;
}
const allCommandDirs = [...this.mountedCommandDirs, ...commandDirs];
const projectedCommands = await this.configureRuntimeCommandStubs(driver.commands, allCommandDirs);
this.proxy.registerCommandGuestPaths(projectedCommands);
this.mountedCommandDirs.push(...commandDirs);
this.mountedRuntimeDrivers.push(driver);
for (const command of projectedCommands.keys()) {
this.commands.set(command, "wasmvm");
}
}
async dispose() {
await this.readyPromise?.catch(() => { });
let syncError;
if (this.options.syncFilesystemOnDispose !== false &&
this.rootFilesystem &&
!(this.options.filesystem instanceof NodeFileSystem)) {
try {
await this.liveFilesystemBinding.syncFromLive(this.liveFilesystemSyncRoots);
}
catch (error) {
syncError = error;
}
}
try {
await this.proxy?.dispose().catch(() => { });
}
finally {
this.proxy = null;
this.rootFilesystem = null;
this.client = null;
this.session = null;
this.vm = null;
this.liveFilesystemBinding.restore();
}
if (syncError) {
throw syncError;
}
}
async exec(command, options) {
await this.ensureReady();
if (!this.proxy) {
throw new Error("kernel is not ready");
}
return this.proxy.exec(command, options);
}
spawn(command, args, options) {
if (!this.proxy) {
throw new Error("kernel is not ready; await kernel.mount(...) first");
}
const normalized = normalizeCommandLookup(command);
const knownCommand = this.commands.has(command) || this.commands.has(normalized);
if (!knownCommand && !this.tryResolveMountedCommand(command)) {
throw new Error(`ENOENT: command not found: ${command}`);
}
const proc = this.proxy.spawn(command, args, options);
const syncProcessSnapshot = () => {
const snapshot = this.proxy?.processes.get(proc.pid);
if (!snapshot) {
return;
}
this.processes.set(proc.pid, {
...snapshot,
args: [...snapshot.args],
});
};
syncProcessSnapshot();
return {
pid: proc.pid,
writeStdin(data) {
return proc.writeStdin(data);
},
closeStdin() {
return proc.closeStdin();
},
kill(signal) {
proc.kill(signal);
syncProcessSnapshot();
},
async wait() {
const exitCode = await proc.wait();
syncProcessSnapshot();
return exitCode;
},
get exitCode() {
return proc.exitCode;
},
};
}
openShell(options) {
if (!this.proxy) {
throw new Error("kernel is not ready; await kernel.mount(...) first");
}
return this.proxy.openShell(options);
}
async connectTerminal(options) {
await this.ensureReady();
if (!this.proxy) {
throw new Error("kernel is not ready");
}
return this.proxy.connectTerminal(options);
}
mountFs(mountPath, filesystem, options) {
const localMount = makeLocalCompatMount({
path: mountPath,
fs: filesystem,
readOnly: options?.readOnly,
});
this.pendingLocalMounts.unshift(localMount);
this.pendingLocalMounts.sort((left, right) => right.path.length - left.path.length);
if (!this.proxy) {
// Pre-boot mounts apply during kernel initialization.
return;
}
return this.proxy.mountFs(mountPath, filesystem, {
readOnly: localMount.readOnly,
sidecarMount: localMount.sidecarMount,
});
}
unmountFs(mountPath) {
const normalized = normalizePath(mountPath);
const pendingIndex = this.pendingLocalMounts.findIndex((mount) => mount.path === normalized);
if (pendingIndex >= 0) {
this.pendingLocalMounts.splice(pendingIndex, 1);
}
return this.proxy?.unmountFs(mountPath);
}
async readFile(targetPath) {
await this.ensureReady();
return this.proxy.readFile(targetPath);
}
async writeFile(targetPath, content) {
await this.ensureReady();
return this.proxy.writeFile(targetPath, content);
}
async mkdir(targetPath) {
await this.ensureReady();
return this.proxy.mkdir(targetPath);
}
async readdir(targetPath) {
await this.ensureReady();
return this.proxy.readdir(targetPath);
}
async readdirRecursive(targetPath, options) {
await this.ensureReady();
return this.proxy.readdirRecursive(targetPath, options);
}
async stat(targetPath) {
await this.ensureReady();
return this.proxy.stat(targetPath);
}
async exists(targetPath) {
await this.ensureReady();
return this.proxy.exists(targetPath);
}
async removeFile(targetPath) {
await this.ensureReady();
return this.proxy.removeFile(targetPath);
}
async removeDir(targetPath) {
await this.ensureReady();
return this.proxy.removeDir(targetPath);
}
async removePath(targetPath, options) {
await this.ensureReady();
return this.proxy.removePath(targetPath, options);
}
async rename(oldPath, newPath) {
await this.ensureReady();
return this.proxy.rename(oldPath, newPath);
}
async movePath(oldPath, newPath) {
await this.ensureReady();
return this.proxy.movePath(oldPath, newPath);
}
tryResolveMountedCommand(command) {
const normalized = normalizeCommandLookup(command);
for (const driver of this.mountedRuntimeDrivers) {
if (!driver.tryResolve?.(command)) {
continue;
}
this.commands.set(normalized, driver.kind);
return true;
}
return false;
}
recordModuleExecution(command) {
for (const driver of this.mountedRuntimeDrivers) {
driver.recordModuleExecution?.(command);
}
}
async ensureReady() {
if (!this.readyPromise) {
this.readyPromise = this.initialize();
}
return this.readyPromise;
}
async initialize() {
const createVmEnv = { ...this.env };
const requestedPermissions = this.options.permissions;
const bootstrapPermissions = requestedPermissions ? allowAll : undefined;
if (this.loopbackExemptPorts.length > 0) {
createVmEnv.AGENT_OS_LOOPBACK_EXEMPT_PORTS = JSON.stringify(this.loopbackExemptPorts);
}
const rootPassthroughPlan = planNodeFilesystemPassthroughMounts(this.options.filesystem, this.pendingLocalMounts);
const snapshotEntries = await snapshotFilesystemEntries(this.options.filesystem, "/", [], {
passthroughDirectories: rootPassthroughPlan.passthroughDirectories,
});
this.liveFilesystemSyncRoots =
collectLiveFilesystemSyncRoots(snapshotEntries);
const rootFilesystem = {
mode: "ephemeral",
disableDefaultBaseLayer: true,
lowers: [
{
kind: "snapshot",
entries: rootFilesystemEntriesForConfig(mergeRootFilesystemEntries(createBootstrapEntries(), snapshotEntries)),
},
],
bootstrapEntries: [],
};
const client = SidecarProcess.spawn({
cwd: REPO_ROOT,
command: ensureNativeSidecarBinary(),
args: [],
});
const session = await client.authenticateAndOpenSession();
const createVmConfig = {
env: createVmEnv,
rootFilesystem,
permissions: bootstrapPermissions
? serializePermissionsForSidecar(bootstrapPermissions)
: undefined,
loopbackExemptPorts: this.loopbackExemptPorts,
bootstrapCommands: [...NODE_RUNTIME_BOOTSTRAP_COMMANDS],
};
const vm = await client.createVm(session, {
runtime: "java_script",
config: createVmConfig,
});
await client.waitForEvent({
type: "vm_lifecycle",
ownership: {
scope: "vm",
connection_id: session.connectionId,
session_id: session.sessionId,
vm_id: vm.vmId,
},
state: "ready",
}, 10_000);
if (requestedPermissions && snapshotEntries.length > 1) {
await materializeSnapshotEntriesIntoVm(client, session, vm, snapshotEntries);
}
if (rootPassthroughPlan.mounts.length > 0) {
this.pendingLocalMounts.push(...rootPassthroughPlan.mounts.map((mount) => makeLocalCompatMount({
path: mount.path,
fs: mount.fs,
readOnly: mount.readOnly,
})));
}
if (this.pendingLocalMounts.length > 0 ||
this.loopbackExemptPorts.length > 0 ||
requestedPermissions) {
const sidecarMounts = this.pendingLocalMounts.map((mount) => serializeLocalCompatMountForSidecar(mount));
await client.configureVm(session, vm, {
mounts: sidecarMounts,
permissions: requestedPermissions,
loopbackExemptPorts: this.loopbackExemptPorts,
});
}
const configuredSidecarMounts = this.pendingLocalMounts.map((mount) => serializeLocalCompatMountForSidecar(mount));
const proxy = new NativeSidecarKernelProxy({
client,
session,
vm,
env: this.env,
cwd: this.cwd,
defaultExecCwd: this.options.cwd === undefined ? "/workspace" : this.cwd,
localMounts: this.pendingLocalMounts,
sidecarMounts: configuredSidecarMounts,
permissions: requestedPermissions,
loopbackExemptPorts: this.loopbackExemptPorts,
commandGuestPaths: new Map(),
onWasmCommandResolved: (command) => {
this.recordModuleExecution(command);
},
});
this.client = client;
this.session = session;
this.vm = vm;
this.proxy = proxy;
this.rootFilesystem = proxy.createRootView();
}
}
export function createKernel(options) {
return new NativeKernel(options);
}
function errnoError(code, message) {
return new KernelError(code, `${code}: ${message}`);
}
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.