Runtime
Long-running Processes
Keep an AgentOS VM or guest process alive for repeated and interactive work.
Use spawn() when a guest server, REPL, worker, or stdin-driven program must
remain alive:
import { JavaScriptRuntime } from "@rivet-dev/agentos/javascript";
const serverSource = `
import http from "node:http";
const app = http.createServer((req, res) => {
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ ok: true, path: req.url }));
});
app.listen(3000, "127.0.0.1", () => console.log("ready"));
await new Promise(() => {});
`;
const runtime = await JavaScriptRuntime.create({
permissions: { network: "allow" },
});
let ready!: () => void;
const serverReady = new Promise<void>((resolve) => {
ready = resolve;
});
try {
const server = await runtime.spawn(serverSource, {
onStdout: (chunk) => {
process.stdout.write(chunk);
if (chunk.includes("ready")) ready();
},
});
await serverReady;
const response = await runtime.vm.httpRequest({ port: 3000, path: "/health" });
console.log(response.status, new TextDecoder().decode(response.body));
server.kill("SIGTERM");
await server.wait();
} finally {
await runtime.dispose();
}
Keep the runtime to reuse VM state; keep its returned CodeProcess to write
stdin, send a signal, or await that one process. The general process model is
documented in AgentOS Processes & Shell.