Public: anyone can read this; the crew can change it.
A crew run's processes carry the cell label crew:<name> #2815
scout/forge:scout/2810-a-crew-run-s-processes-carry-the-cell-la → main 1 commit fork
7 files
changed +67
−9
src/apps/contain.ts +10
| @@ -843,6 +843,16 @@ function tick(): void { | ||
| 843 | 843 | } |
| 844 | 844 | } |
| 845 | 845 | |
| 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 | ||
| 846 | 856 | /** The root process of every labelled cell being watched, for grouping the process table by what launched it (`system/view.ts`). */ |
| 847 | 857 | export function cellRoots(): { label: string; root: number; cap: MemoryCap }[] { |
| 848 | 858 | const out: { label: string; root: number; cap: MemoryCap }[] = []; |
src/crew/claude.ts +6 −1
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | |
| 15 | 15 | import { childEnv, pump } from "../apps/process.ts"; |
| 16 | 16 | import { spellings, stripForgeDirs } from "../runs/paths.ts"; |
| 17 | import type { SandboxPolicy } from "./sandbox.ts"; | |
| 17 | import { labelRun, type SandboxPolicy } from "./sandbox.ts"; | |
| 18 | 18 | |
| 19 | 19 | export interface ClaudeInput { |
| 20 | 20 | readonly binary: string; |
| @@ -56,6 +56,8 @@ export interface ClaudeInput { | ||
| 56 | 56 | * on the result it already gave. Default `SETTLE_MS`. |
| 57 | 57 | */ |
| 58 | 58 | 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; | |
| 59 | 61 | } |
| 60 | 62 | |
| 61 | 63 | /** The idle guard's default: five minutes of silence after a result. */ |
| @@ -187,6 +189,8 @@ export async function runClaude(input: ClaudeInput): Promise<ClaudeOutcome> { | ||
| 187 | 189 | // to one pid never touches a grandchild holding stdout open. |
| 188 | 190 | detached: true, |
| 189 | 191 | }); |
| 192 | const unlabel = | |
| 193 | input.agent === undefined ? undefined : labelRun(proc.pid, input.agent); | |
| 190 | 194 | // What the agent hears: the prompt first, then whatever a person says |
| 191 | 195 | // while it works. Each is a user turn; the session ends when the forge |
| 192 | 196 | // closes its stdin after a turn nobody followed. |
| @@ -405,6 +409,7 @@ export async function runClaude(input: ClaudeInput): Promise<ClaudeOutcome> { | ||
| 405 | 409 | clearTimeout(timer); |
| 406 | 410 | clearTimeout(idle); |
| 407 | 411 | clearTimeout(settle); |
| 412 | unlabel?.(); | |
| 408 | 413 | closeInput(); |
| 409 | 414 | input.signal.removeEventListener("abort", onAbort); |
| 410 | 415 |
src/crew/runner.ts +1
| @@ -1159,6 +1159,7 @@ async function work( | ||
| 1159 | 1159 | resume, |
| 1160 | 1160 | env, |
| 1161 | 1161 | sandbox: shellPolicy(), |
| 1162 | agent: agent.user.username, | |
| 1162 | 1163 | timeoutMs: Math.max(deadline - Date.now(), 0), |
| 1163 | 1164 | log: write, |
| 1164 | 1165 | signal, |
src/crew/sandbox.ts +12
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | import { readdirSync, rmSync, statSync } from "node:fs"; |
| 32 | 32 | import { homedir, tmpdir } from "node:os"; |
| 33 | 33 | import { basename, isAbsolute, join, relative, sep } from "node:path"; |
| 34 | import { labelTree } from "../apps/contain.ts"; | |
| 34 | 35 | import { crewRoot } from "./workdir.ts"; |
| 35 | 36 | |
| 36 | 37 | export interface SandboxPolicy { |
| @@ -89,6 +90,17 @@ export async function sandboxAvailable(): Promise< | ||
| 89 | 90 | } |
| 90 | 91 | } |
| 91 | 92 | |
| 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 | ||
| 92 | 104 | /** The operator's own secrets, wherever they usually are. */ |
| 93 | 105 | const HOME_SECRETS = [ |
| 94 | 106 | ".ssh", |
src/system/view.ts +8 −7
| @@ -11,7 +11,7 @@ import { TICKS_PER_SECOND } from "./proc.ts"; | ||
| 11 | 11 | |
| 12 | 12 | export type Group = { |
| 13 | 13 | label: string; |
| 14 | kind: "check" | "app" | "forge" | "host"; | |
| 14 | kind: "check" | "app" | "crew" | "forge" | "host"; | |
| 15 | 15 | procs: number; |
| 16 | 16 | cpuCores: number | null; |
| 17 | 17 | rssBytes: number; |
| @@ -53,7 +53,7 @@ const TOP = 8; | ||
| 53 | 53 | export type Roots = readonly { label: string; root: number; cap?: { bytes: number; by: "kernel" | "watch" } }[]; |
| 54 | 54 | |
| 55 | 55 | 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"; | |
| 57 | 57 | |
| 58 | 58 | /** Which group each process is in: the nearest cell root above it, the forge, or the host. */ |
| 59 | 59 | 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 | ||
| 112 | 112 | procs: 0, |
| 113 | 113 | cpuCores: null, |
| 114 | 114 | 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, | |
| 117 | 118 | oldestSeconds: 0, |
| 118 | 119 | top: [], |
| 119 | 120 | tests: [], |
| @@ -186,7 +187,7 @@ export function forAgent(view: SystemView, visible: (label: string) => boolean): | ||
| 186 | 187 | const hide = new Map<string, string>(); |
| 187 | 188 | const n = { check: 0, app: 0 }; |
| 188 | 189 | 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; | |
| 190 | 191 | const kind = kindOf(label) as "check" | "app"; |
| 191 | 192 | return hide.get(label) ?? hide.set(label, `${kind}:private-${++n[kind]}`).get(label)!; |
| 192 | 193 | }; |
| @@ -194,8 +195,8 @@ export function forAgent(view: SystemView, visible: (label: string) => boolean): | ||
| 194 | 195 | ...view, |
| 195 | 196 | groups: view.groups.map((g) => |
| 196 | 197 | // 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" | |
| 199 | 200 | ? { ...g, top: [], tests: [] } |
| 200 | 201 | : visible(g.label) |
| 201 | 202 | ? g |
src/web/system-live.ts +1 −1
| @@ -24,7 +24,7 @@ function rank(items, key, kinds) { | ||
| 24 | 24 | export const systemLiveJs = /* js */ ` |
| 25 | 25 | (function () { |
| 26 | 26 | ${procSortJs} |
| 27 | var KINDS = ['forge', 'check', 'app', 'host'], key = 'cpu'; | |
| 27 | var KINDS = ['forge', 'check', 'crew', 'app', 'host'], key = 'cpu'; | |
| 28 | 28 | function read(el) { |
| 29 | 29 | return { el: el, kind: el.getAttribute('data-kind'), cpu: Number(el.getAttribute('data-cpu')) || 0, mem: Number(el.getAttribute('data-mem')) || 0 }; |
| 30 | 30 | } |
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 | }); |
new above
offline