Every worker bot now works on a computer as its own Linux account (cb-<bot id>, no sudo), owning its folder; a workspace's folder belongs to a group its bots share. Only the coordinator works as gem, with sudo. gem, and so Chrome, can't open worker folders: downloads, uploads and files saved by name are copied by the worker's own account, and workers open no file: URLs. The computer's own services (agent, DevTools, Playwright MCP, VNC, ttyd) are kept from worker accounts by an nftables rule, so containers get NET_ADMIN; without the rule the agent refuses worker commands. The X display needs gem's cookie. File reads for a worker are checked on the open descriptor, and writes are made as the worker. Workers ask the coordinator to install system packages with request_install: a coordinator task in the super admin's conversation weighs it and answers, and the answer wakes the worker. Stop ends a worker's leftover processes on its computers. Computer image 1.2.0; the layout bump recreates every computer once.
54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
import { AgentConfig } from "./config";
|
|
import readJsonBody from "./read-json-body";
|
|
import requestRunUser from "./request-run-user";
|
|
import runCommand from "./run-command";
|
|
import sendJson from "./send-json";
|
|
import userEnv from "./user-env";
|
|
import workerEnv from "./worker-env";
|
|
|
|
/**
|
|
* `POST /api/exec` `{ command, timeout_seconds?, cwd?, run_as? }`: runs
|
|
* one bash command as the computer's user (`runCommand`), or with
|
|
* `run_as: { bot_id, workspace_id? }` as that worker bot's own account
|
|
* (`requestRunUser`), from its home unless `cwd` is given. The command is
|
|
* killed if the caller goes away. Answers `{ output, exit_code,
|
|
* timed_out, truncated }`.
|
|
*/
|
|
export default async function handleExec({ req, res }: { req: IncomingMessage; res: ServerResponse }) {
|
|
const body = await readJsonBody({ req, max_bytes: 1024 * 1024 });
|
|
|
|
if (typeof body?.command != "string" || !body.command.trim()) {
|
|
return sendJson({ res, status: 400, body: { error: "Give the command." } });
|
|
}
|
|
|
|
const run_user = await requestRunUser({ res, run_as: body.run_as });
|
|
|
|
if (run_user === undefined) return;
|
|
|
|
const asked = Number(body.timeout_seconds);
|
|
const timeout_seconds = Number.isFinite(asked) && asked > 0
|
|
? Math.min(Math.ceil(asked), AgentConfig.CommandMaxSeconds)
|
|
: AgentConfig.CommandDefaultSeconds;
|
|
|
|
const controller = new AbortController();
|
|
|
|
res.on("close", () => {
|
|
if (!res.writableFinished) controller.abort();
|
|
});
|
|
|
|
const home = run_user ? run_user.home : AgentConfig.Home;
|
|
|
|
const result = await runCommand({
|
|
command: body.command,
|
|
cwd: typeof body.cwd == "string" && body.cwd.startsWith("/") ? body.cwd : home,
|
|
env: run_user ? workerEnv({ run_user }) : userEnv(),
|
|
...(run_user ? { as: { uid: run_user.uid, gid: run_user.gid } } : { uid: AgentConfig.Uid, gid: AgentConfig.Gid }),
|
|
timeout_seconds,
|
|
max_output_bytes: AgentConfig.CommandMaxOutputBytes,
|
|
signal: controller.signal,
|
|
});
|
|
|
|
if (!res.destroyed) sendJson({ res, body: result });
|
|
}
|