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.
46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
import type { ServerResponse } from "node:http";
|
|
import { AgentState } from "./agent-state";
|
|
import ensureRunUser from "./ensure-run-user";
|
|
import runUserFor, { type RunUser } from "./run-user-for";
|
|
import sendJson from "./send-json";
|
|
|
|
/**
|
|
* The worker account a request asks to act as (`run_as: { bot_id,
|
|
* workspace_id? }`), set up and ready, or null for gem's own (the
|
|
* coordinator, the app's own commands). For a bad id, and while worker
|
|
* accounts can't be kept off the services (`AgentState.workers_guarded`:
|
|
* acting as one then isn't safe), it answers the request with the error
|
|
* itself and returns undefined.
|
|
*/
|
|
export default async function requestRunUser({ res, run_as }: { res: ServerResponse; run_as: unknown }): Promise<RunUser | null | undefined> {
|
|
if (run_as == null) return null;
|
|
|
|
const refuse = ({ status, message }: { status: number; message: string }) => {
|
|
sendJson({ res, status, body: { error: message } });
|
|
return undefined;
|
|
};
|
|
|
|
if (typeof run_as != "object") return refuse({ status: 400, message: "run_as must be { bot_id, workspace_id? }." });
|
|
|
|
let run_user: RunUser;
|
|
|
|
try {
|
|
const { bot_id, workspace_id } = run_as as { bot_id?: unknown; workspace_id?: unknown };
|
|
|
|
run_user = runUserFor({ bot_id, workspace_id });
|
|
} catch (error: any) {
|
|
return refuse({ status: 400, message: String(error?.message || error) });
|
|
}
|
|
|
|
if (!AgentState.workers_guarded) {
|
|
return refuse({
|
|
message: `This computer can't keep its bots apart, so it runs nothing for a worker bot: its firewall couldn't be set up (${AgentState.guard_error || "unknown error"}). Rebuild the computer from its settings; its container needs NET_ADMIN.`,
|
|
status: 503,
|
|
});
|
|
}
|
|
|
|
await ensureRunUser({ run_user });
|
|
|
|
return run_user;
|
|
}
|