Public: anyone can read this; the crew can change it.

A crew run's processes carry the cell label crew:<name> #2818

Closed opened this
Closed without merging

scout/forge:scout/2810-a-crew-run-s-processes-carry-the-cell-la → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +67 −9
src/apps/contain.ts +10
@@ -843,6 +843,16 @@ function tick(): void {
843843 }
844844 }
845845
846 /**
847 * Label the process tree under `pid` without holding it to limits: the crew's
848 * agent runs (`crew/sandbox.ts`) are sandboxed by Claude Code, not by a cell,
849 * but /system still groups their processes by `label`. Returns the release.
850 */
851 export function labelTree(pid: number, label: string): () => void {
852 const never: Limits = { memoryMiB: Infinity, cpus: Infinity, diskMiB: Infinity };
853 return watchTree(pid, never, { bytes: Infinity, by: "watch" }, [], label, () => {});
854 }
855
846856 /** The root process of every labelled cell being watched, for grouping the process table by what launched it (`system/view.ts`). */
847857 export function cellRoots(): { label: string; root: number; cap: MemoryCap }[] {
848858 const out: { label: string; root: number; cap: MemoryCap }[] = [];
src/crew/claude.ts +6 −1
@@ -14,7 +14,7 @@
1414
1515 import { childEnv, pump } from "../apps/process.ts";
1616 import { spellings, stripForgeDirs } from "../runs/paths.ts";
17 import type { SandboxPolicy } from "./sandbox.ts";
17 import { labelRun, type SandboxPolicy } from "./sandbox.ts";
1818
1919 export interface ClaudeInput {
2020 readonly binary: string;
@@ -56,6 +56,8 @@ export interface ClaudeInput {
5656 * on the result it already gave. Default `SETTLE_MS`.
5757 */
5858 readonly settleMs?: number | undefined;
59 /** The agent this run is for: its processes carry the label `crew:<name>` in /system. */
60 readonly agent?: string | undefined;
5961 }
6062
6163 /** The idle guard's default: five minutes of silence after a result. */
@@ -187,6 +189,8 @@ export async function runClaude(input: ClaudeInput): Promise<ClaudeOutcome> {
187189 // to one pid never touches a grandchild holding stdout open.
188190 detached: true,
189191 });
192 const unlabel =
193 input.agent === undefined ? undefined : labelRun(proc.pid, input.agent);
190194 // What the agent hears: the prompt first, then whatever a person says
191195 // while it works. Each is a user turn; the session ends when the forge
192196 // closes its stdin after a turn nobody followed.
@@ -405,6 +409,7 @@ export async function runClaude(input: ClaudeInput): Promise<ClaudeOutcome> {
405409 clearTimeout(timer);
406410 clearTimeout(idle);
407411 clearTimeout(settle);
412 unlabel?.();
408413 closeInput();
409414 input.signal.removeEventListener("abort", onAbort);
410415
src/crew/runner.ts +1
@@ -1159,6 +1159,7 @@ async function work(
11591159 resume,
11601160 env,
11611161 sandbox: shellPolicy(),
1162 agent: agent.user.username,
11621163 timeoutMs: Math.max(deadline - Date.now(), 0),
11631164 log: write,
11641165 signal,
src/crew/sandbox.ts +12
@@ -31,6 +31,7 @@
3131 import { readdirSync, rmSync, statSync } from "node:fs";
3232 import { homedir, tmpdir } from "node:os";
3333 import { basename, isAbsolute, join, relative, sep } from "node:path";
34 import { labelTree } from "../apps/contain.ts";
3435 import { crewRoot } from "./workdir.ts";
3536
3637 export interface SandboxPolicy {
@@ -89,6 +90,17 @@ export async function sandboxAvailable(): Promise<
8990 }
9091 }
9192
93 /**
94 * Start watching a run's process tree under the label `crew:<name>`, the way
95 * `apps/contain.ts` labels `check:<run>`, so /system files it under the crew
96 * and not under forge or host. Returns the release. A name that is not a
97 * username is refused rather than put in a label.
98 */
99 export function labelRun(pid: number, agentName: string): () => void {
100 if (!/^[\w.-]{1,64}$/.test(agentName)) return () => {};
101 return labelTree(pid, `crew:${agentName}`);
102 }
103
92104 /** The operator's own secrets, wherever they usually are. */
93105 const HOME_SECRETS = [
94106 ".ssh",
src/system/view.ts +8 −7
@@ -11,7 +11,7 @@ import { TICKS_PER_SECOND } from "./proc.ts";
1111
1212 export type Group = {
1313 label: string;
14 kind: "check" | "app" | "forge" | "host";
14 kind: "check" | "app" | "crew" | "forge" | "host";
1515 procs: number;
1616 cpuCores: number | null;
1717 rssBytes: number;
@@ -53,7 +53,7 @@ const TOP = 8;
5353 export type Roots = readonly { label: string; root: number; cap?: { bytes: number; by: "kernel" | "watch" } }[];
5454
5555 const kindOf = (label: string): Group["kind"] =>
56 label.startsWith("check:") ? "check" : label.startsWith("app:") ? "app" : label === "forge" ? "forge" : "host";
56 label.startsWith("check:") ? "check" : label.startsWith("app:") ? "app" : label.startsWith("crew:") ? "crew" : label === "forge" ? "forge" : "host";
5757
5858 /** Which group each process is in: the nearest cell root above it, the forge, or the host. */
5959 function groupOf(procs: readonly Proc[], roots: Roots, self: number): Map<number, string> {
@@ -112,8 +112,9 @@ export function buildSystem(prev: Raw | undefined, next: Raw, roots: Roots, self
112112 procs: 0,
113113 cpuCores: null,
114114 rssBytes: 0,
115 capBytes: caps.get(label)?.bytes ?? null,
116 capBy: caps.get(label)?.by ?? null,
115 // A crew run is labelled, not capped: its bytes are infinite.
116 capBytes: Number.isFinite(caps.get(label)?.bytes) ? caps.get(label)!.bytes : null,
117 capBy: Number.isFinite(caps.get(label)?.bytes) ? caps.get(label)!.by : null,
117118 oldestSeconds: 0,
118119 top: [],
119120 tests: [],
@@ -186,7 +187,7 @@ export function forAgent(view: SystemView, visible: (label: string) => boolean):
186187 const hide = new Map<string, string>();
187188 const n = { check: 0, app: 0 };
188189 const name = (label: string): string => {
189 if (visible(label) || kindOf(label) === "forge" || kindOf(label) === "host") return label;
190 if (visible(label) || kindOf(label) === "forge" || kindOf(label) === "host" || kindOf(label) === "crew") return label;
190191 const kind = kindOf(label) as "check" | "app";
191192 return hide.get(label) ?? hide.set(label, `${kind}:private-${++n[kind]}`).get(label)!;
192193 };
@@ -194,8 +195,8 @@ export function forAgent(view: SystemView, visible: (label: string) => boolean):
194195 ...view,
195196 groups: view.groups.map((g) =>
196197 // The forge and the host hold whatever no labelled cell claims (a crew run in a private clone,
197 // an unwatched sandbox): their counts stay, their processes and test files never leave.
198 g.kind === "forge" || g.kind === "host"
198 // an unwatched sandbox) and a crew run, which works in any clone: their counts stay, their processes and test files never leave.
199 g.kind === "forge" || g.kind === "host" || g.kind === "crew"
199200 ? { ...g, top: [], tests: [] }
200201 : visible(g.label)
201202 ? g
src/web/system-live.ts +1 −1
@@ -24,7 +24,7 @@ function rank(items, key, kinds) {
2424 export const systemLiveJs = /* js */ `
2525 (function () {
2626 ${procSortJs}
27 var KINDS = ['forge', 'check', 'app', 'host'], key = 'cpu';
27 var KINDS = ['forge', 'check', 'crew', 'app', 'host'], key = 'cpu';
2828 function read(el) {
2929 return { el: el, kind: el.getAttribute('data-kind'), cpu: Number(el.getAttribute('data-cpu')) || 0, mem: Number(el.getAttribute('data-mem')) || 0 };
3030 }
test/crew-label.test.ts added +29
@@ -0,0 +1,29 @@
1 import { expect, test } from "bun:test";
2 import { cellRoots } from "../src/apps/contain.ts";
3 import { HOST, readRaw } from "../src/system/proc.ts";
4 import { buildSystem } from "../src/system/view.ts";
5 import { labelRun } from "../src/crew/sandbox.ts";
6
7 test("a crew run's processes are filed under crew:<name>", async () => {
8 const proc = Bun.spawn(["sh", "-c", "sleep 30 & wait"], { stdout: "ignore" });
9 const release = labelRun(proc.pid, "scout");
10 try {
11 expect(cellRoots().some((r) => r.label === "crew:scout" && r.root === proc.pid)).toBe(true);
12 await Bun.sleep(100);
13 const view = buildSystem(undefined, readRaw(HOST, Date.now()), cellRoots(), process.pid);
14 const group = view.groups.find((g) => g.label === "crew:scout");
15 expect(group?.kind).toBe("crew");
16 expect(group?.procs).toBeGreaterThanOrEqual(2);
17 expect(group?.capBytes).toBeNull();
18 } finally {
19 release();
20 proc.kill("SIGKILL");
21 }
22 expect(cellRoots().some((r) => r.label === "crew:scout")).toBe(false);
23 });
24
25 test("a name that is not a username is not made a label", () => {
26 const release = labelRun(process.pid, "a b:c");
27 expect(cellRoots().some((r) => r.label.includes("a b"))).toBe(false);
28 release();
29 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs