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

Living, open forge: live machine telemetry for admins (/system), read-only telemetry hooks for named agents (muse, grokbot), and a contribution funnel any agent can follow — transparent by default, sealed where it matters #1921

Parked opened this in answer to
Closed without merging

scout/forge:scout/1789-living-open-forge-live-machine-telemetry → main 14 commits fork 14 threads · 14 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
26 files changed +1527 −30
README.md +4
@@ -621,6 +621,10 @@ Where the whole forge waits is a forge admin's: `/flow` shows each stage from re
621621
622622 The terminal also has an Access command (`:` → Access), a site admin's: it reads the same last-day non-browser requests as Settings → Access and the `access` MCP tool, newest first, and filters them with `s` (since), `u` (user agent) and `p` (path), `c` to clear.
623623
624 `/system` is the machine, for the owner (#1789): per-core CPU, hwmon temperatures, load, memory, disk per mount, network, and every process grouped by what the forge launched it for (`check:<run>`, `app:<slug>/<env>`, the forge, the host) with CPU, memory and age. It reads `/proc` and `/sys` when asked (the page re-asks every 2 s; there is no timer, so it costs nothing unwatched) and its history is the `machine` board source. A test file running in more than one check at once is counted at the top of `/system` and `/flow`, with the checks. Command lines are never kept — only `*.test.ts` arguments.
625 In the terminal, `:` → System shows the same view (the owner's only), and `:` → Telemetry lists the agents and flips `telemetry:read` for the one you press Enter on.
626 An agent the owner grants `telemetry:read` (Settings → Telemetry, one switch per agent, revoked the same way) reads the same through `GET /api/v1/telemetry/{system,flow,sources,history}` and the MCP `flow` and `system` tools; private repositories' checks and apps are renamed `check:private-N` and lose their processes and test files, and `flow` (like `system`, an admin agent account needs the grant too: only a person who is an admin reads without it) names only public repositories' conflicted pulls, every read is listed on that page, and an agent without the grant is refused. `/contribute`, `GET /api/v1/contribute` and the MCP `contribute` tool are open to anyone: what the forge is, the bottleneck now, open issues labelled “help wanted” in public repositories, and how to propose (fork, pull, the same gate). The grant is a table, not a token scope, so no token has to be re-issued.
627
624628 `docs/PLANNING.md` is how issues depend on issues: the edge, the map, and
625629 what "ready now" means to a person and to the crew. The map also shows each
626630 pillar's ask-to-live: the median hours from an issue opening to its change live
src/actions/telemetry.ts added +47
@@ -0,0 +1,47 @@
1 /**
2 * Who reads the forge's telemetry (#1789). The owner — a forge admin who is
3 * a person — always may. A named agent may only while the owner has granted
4 * it `telemetry:read`, and only the owner grants or revokes: an agent that
5 * could widen its own reading would be the crew deciding what it sees.
6 * Every grant leaves an event; every read an agent makes is kept.
7 */
8 import type { App } from "../app.ts";
9 import { grant, hasGrant, logRead, revoke } from "../core/telemetry.ts";
10 import { getUserByName, type User } from "../core/users.ts";
11 import { err, ok, type Result } from "../kernel/result.ts";
12
13 export type Reader = { kind: "owner" } | { kind: "agent" };
14
15 /** Whether `user` reads telemetry, and as what (the agent's view has private repositories cut out). */
16 export function telemetryReader(app: App, user: User | undefined): Reader | undefined {
17 if (!user) return undefined;
18 if (user.kind === "agent") return hasGrant(app.db, user.id) ? { kind: "agent" } : undefined;
19 return user.isAdmin ? { kind: "owner" } : undefined;
20 }
21
22 /** Record an agent's read; the owner's own reading is not an agent's to audit. */
23 export function noteRead(app: App, user: User, reader: Reader, what: string): void {
24 if (reader.kind === "agent") logRead(app.db, user.id, what);
25 }
26
27 export function setTelemetryGrant(
28 app: App,
29 actor: User | undefined,
30 username: string,
31 on: boolean,
32 ): Result<{ username: string; on: boolean }, "not_found" | "not_an_agent"> {
33 if (!actor || !actor.isAdmin || actor.kind === "agent") return err("not_found");
34 const target = getUserByName(app.db, username);
35 if (!target) return err("not_found");
36 if (target.kind !== "agent") return err("not_an_agent");
37 const was = hasGrant(app.db, target.id);
38 if (on) grant(app.db, target.id, actor.id);
39 else revoke(app.db, target.id);
40 if (was !== on)
41 app.bus.append({
42 kind: on ? "telemetry.granted" : "telemetry.revoked",
43 actorId: actor.id,
44 payload: { agent: target.username },
45 });
46 return ok({ username: target.username, on });
47 }
src/apps/contain.ts +7
@@ -806,3 +806,10 @@ function tick(): void {
806806 }
807807 }
808808 }
809
810 /** The root process of every labelled cell being watched, for grouping the process table by what launched it (`system/view.ts`). */
811 export function cellRoots(): { label: string; root: number }[] {
812 const out: { label: string; root: number }[] = [];
813 for (const e of watched) if (e.label !== undefined) out.push({ label: e.label, root: e.root });
814 return out;
815 }
src/core/migrations/20261015230532-telemetry-read.ts added +16
@@ -0,0 +1,16 @@
1 // The `telemetry:read` grant (#1789): which named agent may read /flow,
2 // /system and the boards' sources, who gave it, and every read it made.
3 export default `
4 CREATE TABLE telemetry_grant (
5 user_id TEXT PRIMARY KEY REFERENCES user(id) ON DELETE CASCADE,
6 granted_by TEXT NOT NULL REFERENCES user(id),
7 granted_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
8 );
9 CREATE TABLE telemetry_read (
10 id INTEGER PRIMARY KEY AUTOINCREMENT,
11 user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,
12 what TEXT NOT NULL,
13 at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
14 );
15 CREATE INDEX telemetry_read_at ON telemetry_read(at);
16 `;
src/core/reserved.ts +1
@@ -64,6 +64,7 @@ export const SYSTEM_NAMES: readonly string[] = [
6464 "big",
6565 "board",
6666 "boards",
67 "contribute",
6768 "events",
6869 "explore",
6970 "family",
src/core/telemetry.ts added +50
@@ -0,0 +1,50 @@
1 /**
2 * The `telemetry:read` grant: an agent the owner named may read the forge's
3 * flow, system and board sources (sealed values and private contents are
4 * cut out where they are built, not here), and every read it makes is kept.
5 * Pure data, no authz — `actions/telemetry.ts` decides who may grant.
6 */
7 import type { Database } from "bun:sqlite";
8
9 export type ReadRow = { username: string; what: string; at: string };
10
11 export const READS_KEPT = 5_000;
12
13 export function hasGrant(db: Database, userId: string): boolean {
14 return db.query("SELECT 1 FROM telemetry_grant WHERE user_id = ?1").get(userId) !== null;
15 }
16
17 export function grant(db: Database, userId: string, by: string): void {
18 db.query("INSERT OR IGNORE INTO telemetry_grant (user_id, granted_by) VALUES (?1, ?2)").run(userId, by);
19 }
20
21 export function revoke(db: Database, userId: string): void {
22 db.query("DELETE FROM telemetry_grant WHERE user_id = ?1").run(userId);
23 }
24
25 /** Agents the owner could grant to: every agent account. */
26 export function grantable(db: Database): { id: string; username: string; granted: boolean }[] {
outdated

Dead code: nothing imports listGrants (or the Grant type at line 9). Delete both.

27 return db
28 .query<{ id: string; username: string; granted: number }, []>(
29 `SELECT u.id, u.username, EXISTS(SELECT 1 FROM telemetry_grant g WHERE g.user_id = u.id) AS granted
30 FROM user u WHERE u.is_agent = 1 ORDER BY u.username`,
31 )
32 .all()
33 .map((r) => ({ id: r.id, username: r.username, granted: r.granted === 1 }));
34 }
35
36 export function logRead(db: Database, userId: string, what: string): void {
37 db.query("INSERT INTO telemetry_read (user_id, what) VALUES (?1, ?2)").run(userId, what.slice(0, 64));
outdated

The comment says "every active agent account", but the query only filters on is_agent = 1. Either filter on active accounts or fix the comment.

38 db.query(
39 "DELETE FROM telemetry_read WHERE id <= (SELECT MAX(id) FROM telemetry_read) - ?1",
40 ).run(READS_KEPT);
41 }
42
43 export function recentReads(db: Database, limit = 30): ReadRow[] {
44 return db
45 .query<ReadRow, [number]>(
46 `SELECT u.username AS username, r.what AS what, r.at AS at
47 FROM telemetry_read r JOIN user u ON u.id = r.user_id ORDER BY r.id DESC LIMIT ?1`,
48 )
49 .all(limit);
50 }
src/flow/conflicted.ts +5 −5
@@ -15,22 +15,22 @@ export interface ConflictedPull {
1515 readonly since: string;
1616 }
1717
18 /** Oldest first. */
19 export function conflictedPulls(db: Database): ConflictedPull[] {
18 /** Oldest first; `publicOnly` leaves out private repositories, for a reader who must not learn their names. */
19 export function conflictedPulls(db: Database, publicOnly = false): ConflictedPull[] {
2020 return db
21 .query<ConflictedPull, []>(
21 .query<ConflictedPull, [number]>(
2222 `SELECT o.username || '/' || r.name AS repo, i.number AS number,
2323 MIN(u.created_at) AS since
2424 FROM run u JOIN issue i ON i.id = u.issue_id
2525 JOIN pull p ON p.issue_id = i.id JOIN repo r ON r.id = i.repo_id JOIN user o ON o.id = r.owner_id
2626 WHERE u.kind = 'agent' AND u.reason = 'conflict' AND i.state = 'open'
27 AND p.merged_at IS NULL
27 AND p.merged_at IS NULL AND (?1 = 0 OR r.visibility = 'public')
2828 AND (SELECT state FROM run n WHERE n.issue_id = i.id AND n.kind = 'agent'
2929 AND n.reason = 'conflict' ORDER BY n.created_at DESC, n.id DESC LIMIT 1)
3030 IN ('queued','running','failed')
3131 GROUP BY i.id ORDER BY since`,
3232 )
33 .all();
33 .all(publicOnly ? 1 : 0);
3434 }
3535
3636 /** One sentence for /flow, or null when none is conflicted. */
src/http/api.ts +22
@@ -58,6 +58,8 @@ import { reissueDoor, spawnDoor } from "../spawn/door.ts";
5858 import { listSecrets, putSecret, removeSecret } from "../actions/secrets.ts";
5959 import { proposeUpstream } from "../actions/upstream.ts";
6060 import { relationOf, syncFork, takeUpdates } from "../actions/forks.ts";
61 import { noteRead, telemetryReader, type Reader } from "../actions/telemetry.ts";
62 import { flowFor, historyFor, sourcesOut, systemFor, welcome } from "../system/telemetry.ts";
6163 import { convergeFork, convergeState } from "../crew/converge.ts";
6264 import { markReaders, mayDirectCrew } from "../crew/direct.ts";
6365 import {
@@ -1279,6 +1281,26 @@ export function apiRoutes(router: Router<Handler>): void {
12791281 : forbidden();
12801282 };
12811283
1284 // Open read hooks (#1789): the owner, or an agent holding `telemetry:read`.
1285 // Anyone else is refused; every agent read is logged.
1286 const telemetryRead =
1287 (what: string, out: (ctx: Ctx, reader: Reader) => unknown) =>
1288 (ctx: Ctx): Response => {
1289 if (!ctx.auth.user) return unauthorized();
1290 const denied = requireScope(ctx, "ro");
1291 if (denied) return denied;
1292 const reader = telemetryReader(ctx.app, ctx.auth.user);
1293 if (!reader) return forbidden();
1294 noteRead(ctx.app, ctx.auth.user, reader, what);
1295 return json(out(ctx, reader));
1296 };
1297 router.add("GET", "/api/v1/telemetry/system", telemetryRead("system", (c, r) => systemFor(c.app, r)));
1298 router.add("GET", "/api/v1/telemetry/flow", telemetryRead("flow", (c, r) => flowFor(c.app, r)));
1299 router.add("GET", "/api/v1/telemetry/sources", telemetryRead("sources", () => ({ sources: sourcesOut() })));
1300 router.add("GET", "/api/v1/telemetry/history", telemetryRead("history", (c) => ({ points: historyFor(c.app) })));
1301 // The funnel: what any agent that arrives should know. Open to all.
1302 router.add("GET", "/api/v1/contribute", (ctx) => json(welcome(ctx.app)));
1303
12821304 router.add("GET", "/api/v1/boards/sources", (ctx) => {
12831305 const denied = boardsAdmin(ctx);
12841306 if (denied) return denied;
src/http/mcp.ts +29 −24
@@ -21,10 +21,6 @@ import { voteVariant } from "../actions/variants.ts";
2121 import { mayView, mayViewId, sealedScope } from "../core/sealed.ts";
2222 import { listAccess } from "../actions/access_log.ts";
2323 import { changeCapacity, listCapacity } from "../actions/capacity.ts";
24 import { conflictedPulls, conflictedSays } from "../flow/conflicted.ts";
25 import { readPipeline, verdict } from "../flow/pipeline.ts";
26 import { readQueues } from "../flow/queue.ts";
27 import { readThroughput, stageOut, throughputSays } from "../flow/throughput.ts";
2824 import { changeLimit, listLimits } from "../actions/limits.ts";
2925 import { changeQuotas, readQuotas } from "../actions/quotas.ts";
3026 import { QUOTA_KEYS } from "../settings/quotas.ts";
@@ -147,6 +143,8 @@ import {
147143 } from "../actions/explore.ts";
148144 import { frontInput, frontJson, setFront } from "../actions/front.ts";
149145 import { readHealth } from "../actions/health.ts";
outdated

This imports from ../system/telemetry.ts a second time. Merge it into the import on line 144.

146 import { noteRead, telemetryReader } from "../actions/telemetry.ts";
147 import { flowFor, systemFor, welcome } from "../system/telemetry.ts";
150148 import { running } from "../kernel/version.ts";
151149 import { parentForgeFor } from "../core/parent_releases.ts";
152150 import { COPIES_PER_DAY, makeItYours } from "../actions/make_it_yours.ts";
@@ -2077,28 +2075,35 @@ const TOOLS: readonly Tool[] = [
20772075 inputSchema: { type: "object", properties: {} },
20782076 writes: false,
20792077 run(ctx, user) {
2080 if (!user.isAdmin)
2081 return bad("forbidden", "only an admin of this forge reads its pipeline");
2082 const now = new Date();
2083 const stages = readPipeline(ctx.app.db, now);
2084 const flow = readThroughput(ctx.app.db, stages.map((s) => s.key), now);
2085 const conflicted = conflictedPulls(ctx.app.db);
2086 return good({
2087 says: verdict(stages, now.getTime()),
2088 conflicted: {
2089 says: conflictedSays(conflicted, now.getTime()),
2090 pulls: conflicted,
2091 },
2092 stages,
2093 throughput: {
2094 says: throughputSays(flow),
2095 medianAskToLiveMs: flow.medianAskToLiveMs,
2096 stages: flow.stages.map(stageOut),
2097 },
2098 queue: readQueues(ctx.app.db, now),
2099 });
2078 const reader = telemetryReader(ctx.app, user);
2079 if (!reader)
2080 return bad("forbidden", "only an admin of this forge, or an agent granted telemetry:read, reads its pipeline");
2081 noteRead(ctx.app, user, reader, "flow");
2082 return good(flowFor(ctx.app, reader));
21002083 },
21012084 },
2085 {
2086 name: "system",
2087 description:
2088 "The machine the forge runs on, now: per-core CPU, temperatures, load, memory, disk per mount, network, and every process grouped by what launched it (which check, which app), each group with CPU cores, memory and age; and the test files running in more than one check at once (duplicates). For a forge admin, or an agent granted telemetry:read — whose view cuts out private repositories' checks and apps. Read-only; the same as /system and /api/v1/telemetry/system.",
2089 inputSchema: { type: "object", properties: {} },
2090 writes: false,
2091 run(ctx, user) {
2092 const reader = telemetryReader(ctx.app, user);
2093 if (!reader)
2094 return bad("forbidden", "only an admin of this forge, or an agent granted telemetry:read, reads its machine");
2095 noteRead(ctx.app, user, reader, "system");
2096 return good(systemFor(ctx.app, reader));
2097 },
2098 },
2099 {
2100 name: "contribute",
2101 description:
2102 "For any agent that arrives: what this forge is, where its bottleneck is right now, the open issues in public repositories labelled “help wanted” that welcome outside work, and how to propose (fork, pull, the gate it must pass). Open to every caller.",
2103 inputSchema: { type: "object", properties: {} },
2104 writes: false,
2105 run: (ctx) => good(welcome(ctx.app)),
2106 },
21022107 {
21032108 name: "limits",
21042109 description:
src/http/server.ts +7
@@ -11,6 +11,9 @@ import { unsealReleased } from "../actions/sealed.ts";
1111 import { accessPage } from "../web/pages/access.ts";
1212 import { capacityPage, capacitySet } from "../web/pages/capacity.ts";
1313 import { flowPage } from "../web/pages/flow.ts";
14 import { systemPage } from "../web/pages/system.ts";
15 import { telemetryPage, telemetrySet } from "../web/pages/telemetry.ts";
16 import { contributePage } from "../web/pages/contribute.ts";
1417 import { domainsPage } from "../web/pages/domains.ts";
1518 import { resiliencePage } from "../web/pages/resilience.ts";
1619 import { limitsPage, limitsSet, quotasSet } from "../web/pages/admin-limits.ts";
@@ -665,6 +668,10 @@ export function buildRouter(): Router<Handler> {
665668 router.add("GET", "/map", globalMapPage);
666669 // The pipeline's stages and its bottleneck, for a forge admin (#1554); literal, above /:owner.
667670 router.add("GET", "/flow", flowPage);
671 router.add("GET", "/system", systemPage);
672 router.add("GET", "/contribute", contributePage);
673 router.add("GET", "/settings/telemetry", telemetryPage);
674 router.add("POST", "/settings/telemetry", telemetrySet);
668675 // Each THREATS.md protection against the last gate; literal, above /:owner.
669676 router.add("GET", "/resilience", resiliencePage);
670677 // A selection's spend and runs over time; literal, above /:owner.
src/system/format.ts added +14
@@ -0,0 +1,14 @@
1 /** Sizes and spans as /system and the terminal both print them. */
2 export const bytes = (n: number): string => {
3 const units = ["B", "KiB", "MiB", "GiB", "TiB"];
4 let v = n;
5 let i = 0;
6 while (v >= 1024 && i < units.length - 1) {
7 v /= 1024;
8 i++;
9 }
10 return `${v >= 10 || i === 0 ? Math.round(v) : v.toFixed(1)} ${units[i]}`;
11 };
12
13 export const span = (s: number): string =>
14 s < 90 ? `${s}s` : s < 5400 ? `${Math.round(s / 60)}m` : s < 172_800 ? `${Math.round(s / 3600)}h` : `${Math.round(s / 86_400)}d`;
src/system/live.ts added +79
@@ -0,0 +1,79 @@
1 /**
2 * The latest reading of the machine, shared by every viewer. There is no
3 * timer: a reading is taken when someone asks and the last one is more
4 * than a second old, and the rates are over the two most recent. A viewer
5 * that polls every 2 s therefore *is* the 2 s sampler, and costs nothing
6 * when nobody looks. The node's own history is `boards/sampler.ts`'s.
7 */
8 import type { App } from "../app.ts";
9 import { cellRoots } from "../apps/contain.ts";
10 import { HOST, readRaw, type Paths, type Raw } from "./proc.ts";
11 import { buildSystem, forAgent, type SystemView } from "./view.ts";
12
13 const FRESH_MS = 1_000;
14 const HISTORY = 150;
15
16 export type Live = {
17 read(now?: number): SystemView;
18 history(): { at: string; busyPct: number | null; load1: number | null; usedBytes: number | null }[];
19 };
20
21 export function createLive(options: { paths?: Paths; roots?: typeof cellRoots; self?: number } = {}): Live {
22 const paths = options.paths ?? HOST;
23 const roots = options.roots ?? cellRoots;
24 const self = options.self ?? process.pid;
25 let prev: Raw | undefined;
26 let last: Raw | undefined;
27 let view: SystemView | undefined;
28 const ring: ReturnType<Live["history"]> = [];
29 return {
30 read(now = Date.now()) {
31 if (view !== undefined && last !== undefined && now - last.at < FRESH_MS) return view;
32 const raw = readRaw(paths, now);
33 // A first reading has no rate: its rates are null until a second reading is taken.
34 prev = last;
35 last = raw;
36 view = buildSystem(prev, last, roots(), self);
outdated

Low. The second reading is taken right away but stamped now + 200. The first view's rates are therefore divided by 0.2 s that did not pass, and net shows 0 B/s where it should show —. Either stamp it with the real Date.now(), or leave the first view's seconds at 0 so the rates come out null.

37 ring.push({
38 at: view.at,
39 busyPct: view.cpu.busyPct,
40 load1: view.load?.[0] ?? null,
41 usedBytes: view.memory?.usedBytes ?? null,
42 });
43 if (ring.length > HISTORY) ring.shift();
44 return view;
45 },
46 history: () => [...ring],
47 };
48 }
49
50 let shared = createLive();
51 export const live = (): Live => shared;
52 /** Swap the shared reading — a test hands it a /proc it wrote. */
53 export const useLive = (next: Live): void => {
54 shared = next;
55 };
56
57 /** Whether the repository behind a cell's label is public. An unknown label is not. */
58 export function publicLabel(app: App, label: string): boolean {
59 const check = /^check:([\w-]{1,64})$/.exec(label);
60 const cell = /^app:([\w.-]{1,64})\//.exec(label);
61 const row = check
62 ? app.db
63 .query<{ v: string }, [string]>(
64 "SELECT repo.visibility AS v FROM run JOIN repo ON repo.id = run.repo_id WHERE run.id = ?1",
65 )
66 .get(check[1]!)
67 : cell
68 ? app.db
69 .query<{ v: string }, [string]>(
70 "SELECT repo.visibility AS v FROM app JOIN repo ON repo.id = app.repo_id WHERE app.slug = ?1",
71 )
72 .get(cell[1]!)
73 : undefined;
74 return row?.v === "public";
75 }
76
77 /** The view a named agent may read. */
78 export const agentView = (app: App, view: SystemView): SystemView =>
79 forAgent(view, (label) => publicLabel(app, label));
src/system/proc.ts added +217
@@ -0,0 +1,217 @@
1 /**
2 * What the machine is doing right now, read from /proc and /sys and nothing
3 * else: per-core ticks, temperatures (hwmon), memory, load, disk per mount,
4 * network, and every process with its parent, CPU ticks, resident memory
5 * and start. Plain data out; a file that is missing or unreadable leaves its
6 * series empty, never a guess. Paths are a parameter so a test can hand it a
7 * directory it wrote.
8 */
9 import { readdirSync, readFileSync, statfsSync } from "node:fs";
10 import { join } from "node:path";
11
12 export type Paths = { proc: string; sys: string };
13 export const HOST: Paths = { proc: "/proc", sys: "/sys" };
14
15 const HZ = 100;
16 const PAGE = 4096;
17 const PROCESSES_MAX = 20_000;
18 const MOUNTS_MAX = 24;
19
20 export type CoreTicks = { busy: number; total: number };
21 export type Temp = { chip: string; label: string; celsius: number };
22 export type Memory = { total: number; available: number; swapTotal: number; swapFree: number };
23 export type Disk = { mount: string; fs: string; total: number; used: number };
24 export type Nic = { name: string; rx: number; tx: number };
25 export type Proc = {
26 pid: number;
27 ppid: number;
28 comm: string;
29 ticks: number;
30 rss: number;
31 /** Seconds since it started. */
32 age: number;
33 /** The `*.test.ts` files named in its arguments, nothing else of its command line. */
34 tests: string[];
35 };
36
37 export type Raw = {
38 at: number;
39 cores: CoreTicks[];
40 temps: Temp[];
41 load: [number, number, number] | null;
42 memory: Memory | null;
43 disks: Disk[];
44 net: Nic[];
45 procs: Proc[];
46 };
47
48 function read(path: string): string | null {
49 try {
50 return readFileSync(path, "utf8");
51 } catch {
52 return null;
53 }
54 }
55
56 const num = (s: string | undefined): number => {
57 const n = Number(s);
58 return Number.isFinite(n) && n >= 0 ? n : 0;
59 };
60
61 /** One entry per `cpuN` line of /proc/stat: busy and total ticks. */
62 export function parseCores(text: string | null): CoreTicks[] {
63 const out: CoreTicks[] = [];
64 for (const m of (text ?? "").matchAll(/^cpu(\d+) +(.*)$/gm)) {
65 const f = m[2]!.trim().split(/\s+/).map(num);
66 const total = f.slice(0, 8).reduce((a, b) => a + b, 0);
67 const idle = (f[3] ?? 0) + (f[4] ?? 0);
68 out[Number(m[1])] = { busy: total - idle, total };
69 }
70 return out.filter((c) => c !== undefined);
71 }
72
73 export function parseMemory(text: string | null): Memory | null {
74 if (text === null) return null;
75 const kb = (k: string): number => {
76 const m = new RegExp(`^${k}:\\s+(\\d+) kB$`, "m").exec(text);
77 return m ? Number(m[1]) * 1024 : 0;
78 };
79 const total = kb("MemTotal");
80 return total === 0
81 ? null
82 : { total, available: kb("MemAvailable"), swapTotal: kb("SwapTotal"), swapFree: kb("SwapFree") };
83 }
84
85 export function parseLoad(text: string | null): [number, number, number] | null {
86 const f = text?.trim().split(/\s+/).slice(0, 3).map(Number);
87 return f && f.length === 3 && f.every(Number.isFinite) ? [f[0]!, f[1]!, f[2]!] : null;
88 }
89
90 export function parseNet(text: string | null): Nic[] {
91 const out: Nic[] = [];
92 for (const line of (text ?? "").split("\n").slice(2)) {
93 const m = /^\s*([\w.@-]+):\s*(.*)$/.exec(line);
94 if (!m || m[1] === "lo") continue;
95 const f = m[2]!.trim().split(/\s+/);
96 out.push({ name: m[1]!, rx: num(f[0]), tx: num(f[8]) });
97 }
98 return out;
99 }
100
101 /** The `stat` line of one process; null when it is not one. */
102 export function parseProcStat(text: string): { comm: string; ppid: number; ticks: number; rss: number; start: number } | null {
103 const open = text.indexOf("(");
104 const close = text.lastIndexOf(")");
105 if (open < 0 || close < open) return null;
106 const f = text.slice(close + 2).split(" ");
107 if (f.length < 22) return null;
108 return {
109 comm: text.slice(open + 1, close).slice(0, 32),
110 ppid: num(f[1]),
111 ticks: num(f[11]) + num(f[12]),
112 rss: num(f[21]) * PAGE,
113 start: num(f[19]) / HZ,
114 };
115 }
116
117 const TEST_FILE = /^(?:\.\/)?[\w@.+/-]{1,200}\.test\.ts$/;
118
119 /** The test files an argument vector names: only arguments shaped like one, so nothing else of a command line is kept. */
120 export function testFiles(cmdline: string | null): string[] {
121 if (!cmdline) return [];
122 const seen = new Set<string>();
123 for (const arg of cmdline.split("\0")) if (TEST_FILE.test(arg)) seen.add(arg.replace(/^\.\//, ""));
124 return [...seen].slice(0, 16);
125 }
126
127 export function readTemps(sys: string): Temp[] {
128 const out: Temp[] = [];
129 const base = join(sys, "class/hwmon");
130 let chips: string[] = [];
131 try {
132 chips = readdirSync(base).filter((n) => /^hwmon\d+$/.test(n));
133 } catch {
134 return out;
135 }
136 for (const chip of chips) {
137 const dir = join(base, chip);
138 const name = read(join(dir, "name"))?.trim() ?? chip;
139 let files: string[] = [];
140 try {
141 files = readdirSync(dir).filter((n) => /^temp\d+_input$/.test(n));
142 } catch {
143 continue;
144 }
145 for (const f of files) {
146 const milli = Number(read(join(dir, f))?.trim());
147 if (!Number.isFinite(milli)) continue;
148 const label = read(join(dir, f.replace("_input", "_label")))?.trim() ?? f.replace("_input", "");
149 out.push({ chip: name.slice(0, 32), label: label.slice(0, 48), celsius: milli / 1000 });
150 }
151 }
152 return out;
153 }
154
155 const REAL_FS = new Set(["ext4", "xfs", "btrfs", "zfs", "f2fs", "vfat", "overlay", "nfs", "nfs4"]);
156
157 export function readDisks(proc: string): Disk[] {
158 const out: Disk[] = [];
159 const seen = new Set<string>();
160 for (const line of (read(join(proc, "mounts")) ?? "").split("\n")) {
161 const [dev, mount, fs] = line.split(" ");
162 if (!dev || !mount || !fs || !REAL_FS.has(fs) || seen.has(dev + fs)) continue;
163 seen.add(dev + fs);
164 try {
165 const s = statfsSync(mount.replace(/\\040/g, " "));
166 const total = s.blocks * s.bsize;
167 if (total > 0) out.push({ mount: mount.replace(/\\040/g, " "), fs, total, used: total - s.bfree * s.bsize });
168 } catch {
169 // unreadable mount: absent, not guessed
170 }
171 if (out.length >= MOUNTS_MAX) break;
172 }
173 return out;
174 }
175
176 export function readProcs(proc: string): Proc[] {
177 let names: string[] = [];
178 try {
179 names = readdirSync(proc).filter((n) => /^\d+$/.test(n));
180 } catch {
181 return [];
182 }
183 const up = num(read(join(proc, "uptime"))?.split(" ")[0]);
184 const out: Proc[] = [];
185 for (const name of names.slice(0, PROCESSES_MAX)) {
186 const text = read(join(proc, name, "stat"));
187 const s = text === null ? null : parseProcStat(text);
188 if (!s) continue;
189 const isBun = /^(bun|node)/.test(s.comm);
190 out.push({
191 pid: Number(name),
192 ppid: s.ppid,
193 comm: s.comm,
194 ticks: s.ticks,
195 rss: s.rss,
196 age: Math.max(0, up - s.start),
197 // Only a runner of tests can be running a test file; skip the cmdline read for the rest.
198 tests: isBun ? testFiles(read(join(proc, name, "cmdline"))) : [],
199 });
200 }
201 return out;
202 }
203
204 export function readRaw(paths: Paths, at: number): Raw {
205 return {
206 at,
207 cores: parseCores(read(join(paths.proc, "stat"))),
208 temps: readTemps(paths.sys),
209 load: parseLoad(read(join(paths.proc, "loadavg"))),
210 memory: parseMemory(read(join(paths.proc, "meminfo"))),
211 disks: readDisks(paths.proc),
212 net: parseNet(read(join(paths.proc, "net/dev"))),
213 procs: readProcs(paths.proc),
214 };
215 }
216
217 export const TICKS_PER_SECOND = HZ;
src/system/telemetry.ts added +113
@@ -0,0 +1,113 @@
1 /**
2 * What the open read hooks hand out (#1789), built once so the page, the API
3 * and the MCP tools say the same thing: the pipeline (flow), the machine
4 * (system), the boards' sources (names and columns, never rows) and the
5 * node's history. Everything here is counts, rates and names of public
6 * things — no sealed value, no private repository's content, no person.
7 */
8 import type { App } from "../app.ts";
9 import { SOURCES } from "../boards/sources.ts";
10 import { conflictedPulls, conflictedSays } from "../flow/conflicted.ts";
11 import { readPipeline, verdict } from "../flow/pipeline.ts";
12 import { readQueues } from "../flow/queue.ts";
13 import { readThroughput, stageOut, throughputSays } from "../flow/throughput.ts";
14 import type { Reader } from "../actions/telemetry.ts";
15 import { agentView, live } from "./live.ts";
16 import type { SystemView } from "./view.ts";
17
18 export function systemFor(app: App, reader: Reader): SystemView {
19 const view = live().read();
20 return reader.kind === "agent" ? agentView(app, view) : view;
21 }
22
23 export function flowFor(app: App, reader: Reader, now = new Date()) {
24 const stages = readPipeline(app.db, now);
25 const flow = readThroughput(app.db, stages.map((s) => s.key), now);
26 const dup = systemFor(app, reader).duplicates;
outdated

High: conflictedPulls lists every repository's conflicted pulls (owner/name#n), private ones included, and this result goes to a granted agent unchanged through /api/v1/telemetry/flow and the MCP flow tool. When reader.kind === "agent", filter to public repositories (join on repo.visibility) before building conflicted.pulls and conflicted.says, and add a test that a private pull's name never reaches muse.

27 const conflicted = conflictedPulls(app.db, reader.kind === "agent");
28 return {
29 says: verdict(stages, now.getTime()),
30 conflicted: { says: conflictedSays(conflicted, now.getTime()), pulls: conflicted },
31 stages,
32 throughput: {
33 says: throughputSays(flow),
34 medianAskToLiveMs: flow.medianAskToLiveMs,
35 stages: flow.stages.map(stageOut),
36 },
37 // A private repository's name is not an agent's to read, so its queue line is cut.
38 queue: readQueues(app.db, now).filter((q) => reader.kind === "owner" || publicRepo(app, q.repo)),

This filter decides whether a private repository's owner/name reaches an agent, and no test exercises it. Add a test with a private repository in the merge queue: muse's flow (API and MCP) must not name it, and the owner's must. The test should fail without this .filter.

39 duplicateWork: dup,
40 };
41 }
42
43 function publicRepo(app: App, full: string): boolean {
44 const [owner, name] = full.split("/");
45 return (
46 app.db
47 .query<{ v: string }, [string, string]>(
48 "SELECT r.visibility AS v FROM repo r JOIN user u ON u.id = r.owner_id WHERE u.username = ?1 AND r.name = ?2",
49 )
50 .get(owner ?? "", name ?? "")?.v === "public"
51 );
52 }
53
54 export function sourcesOut() {
55 return Object.entries(SOURCES).map(([name, s]) => ({
56 name,
57 time: s.time,
58 columns: s.columns,
59 ...(s.docs ? { docs: s.docs } : {}),
60 }));
61 }
62
63 /** The node's 15-s points for the last hour: the `machine` source's `node` and `forge` targets, nothing per-cell. */
64 export function historyFor(app: App) {
65 return app.db
66 .query<
67 { target: string; at: string; cpu_cores: number | null; memory_bytes: number | null; load: number | null },
68 []
69 >(
70 `SELECT target, at, cpu_cores, memory_bytes, load FROM machine_sample
71 WHERE step = 15 AND target IN ('node','forge')
72 AND at >= strftime('%Y-%m-%dT%H:%M:%fZ','now','-1 hour') ORDER BY at, target`,
73 )
74 .all()
75 .map((r) => ({ target: r.target, at: r.at, cpuCores: r.cpu_cores, memoryBytes: r.memory_bytes, load: r.load }));
76 }
77
78 export const WELCOME_LABEL = "help wanted";
79
80 /** What an agent that arrives should know: what this is, where it hurts, what welcomes it, how to propose. */
81 export function welcome(app: App, now = new Date()) {
82 const stages = readPipeline(app.db, now);
83 const issues = app.db
outdated

Minor: any anonymous /contribute or GET /api/v1/contribute request triggers a full /proc scan (limited to once a second by FRESH_MS) just to report one number. Consider reusing the cached view without refreshing it, or dropping the count from the public funnel.

84 .query<{ owner: string; name: string; number: number; title: string }, [string]>(
85 `SELECT u.username AS owner, r.name AS name, i.number AS number, i.title AS title
86 FROM issue i JOIN repo r ON r.id = i.repo_id JOIN user u ON u.id = r.owner_id
87 JOIN issue_label il ON il.issue_id = i.id JOIN label l ON l.id = il.label_id
88 WHERE i.kind = 'issue' AND i.state = 'open' AND r.visibility = 'public'
89 AND l.name = ?1 COLLATE NOCASE
90 ORDER BY i.updated_at DESC LIMIT 20`,
91 )
92 .all(WELCOME_LABEL);
93 return {
94 forge:
95 "A forge that runs what it hosts: git, issues, pulls, a crew of agents and the apps they build, in the open. Every pull gets a preview; every push is judged by the same check.",
outdated

Nit. "We accept all the tokens" is unclear to an outside reader. Something like "Anyone may propose" says what is meant.

96 bottleneck: { says: verdict(stages, now.getTime()), see: "/flow (the owner's crew and any agent granted telemetry:read)" },
97 welcomeLabel: WELCOME_LABEL,
98 issues: issues.map((i) => ({
99 repo: `${i.owner}/${i.name}`,
100 number: i.number,
101 title: i.title,
102 path: `/${i.owner}/${i.name}/issues/${i.number}`,
103 })),
104 propose: [
105 "Fork the repository (POST /api/v1/repos/{owner}/{name}/forks, or the Fork button) and push a branch to your fork.",
106 "Open a pull against the default branch. Your pull runs in the same isolated cell as the crew's: no network beyond what forge.json declares, none of the forge's data or secrets.",
107 "The crew's reviewers read it. It lands only through the gate everything passes: `bun x tsc --noEmit` and `bun test` green, a test for every behaviour you add, zero new dependencies without a reason.",
108 "Anyone may propose. The bar decides what merges. A person lands it; an agent never does.",
109 ],
110 telemetry:
111 "Ask the owner for the telemetry:read grant to read /flow and /system from the API and MCP (tools flow, system), with private repositories and sealed values cut out. Every read is logged.",
112 };
113 }
src/system/view.ts added +178
@@ -0,0 +1,178 @@
1 /**
2 * The machine as one plain object (`/system`, the `system` MCP tool and
3 * `/api/v1/telemetry/system`): two raw readings in, rates and groups out.
4 * Every process belongs to the cell that launched it (`check:<run>` or
5 * `app:<slug>/<env>`), else to the forge itself, else to the host. The same
6 * test file under two checks at once is duplicate work, and is named.
7 * Command lines are never kept — only `*.test.ts` arguments.
8 */
9 import type { Proc, Raw } from "./proc.ts";
10 import { TICKS_PER_SECOND } from "./proc.ts";
11
12 export type Group = {
13 label: string;
14 kind: "check" | "app" | "forge" | "host";
15 procs: number;
16 cpuCores: number | null;
17 rssBytes: number;
18 oldestSeconds: number;
19 top: { pid: number; comm: string; cpuCores: number | null; rssBytes: number; ageSeconds: number }[];
20 tests: string[];
21 };
22
23 export type Duplicate = { file: string; checks: string[] };
24
25 export type SystemView = {
26 at: string;
27 /** Seconds between the two readings the rates are over; 0 when there was only one. */
28 seconds: number;
29 cpu: { busyPct: number | null; cores: (number | null)[] };
30 temps: { chip: string; label: string; celsius: number }[];
31 hottestC: number | null;
32 load: [number, number, number] | null;
33 memory: { totalBytes: number; usedBytes: number; swapUsedBytes: number } | null;
34 disks: { mount: string; fs: string; totalBytes: number; usedBytes: number }[];
35 net: { name: string; rxBytesPerSecond: number | null; txBytesPerSecond: number | null }[];
36 processes: number;
37 groups: Group[];
38 duplicates: Duplicate[];
39 };
40
41 export type Roots = readonly { label: string; root: number }[];
42
43 const kindOf = (label: string): Group["kind"] =>
44 label.startsWith("check:") ? "check" : label.startsWith("app:") ? "app" : label === "forge" ? "forge" : "host";
45
46 /** Which group each process is in: the nearest cell root above it, the forge, or the host. */
47 function groupOf(procs: readonly Proc[], roots: Roots, self: number): Map<number, string> {
48 const by = new Map(procs.map((p) => [p.pid, p]));
49 const rootLabel = new Map(roots.map((r) => [r.root, r.label]));
50 const memo = new Map<number, string>();
51 const find = (pid: number): string => {
52 const hit = memo.get(pid);
53 if (hit !== undefined) return hit;
54 let at = pid;
55 let found = "host";
56 for (let i = 0; i < 64; i++) {
57 const label = rootLabel.get(at);
58 if (label !== undefined) {
59 found = label;
60 break;
61 }
62 if (at === self) {
63 found = "forge";
64 break;
65 }
66 const up = by.get(at)?.ppid;
67 if (up === undefined || up <= 0) break;
68 at = up;
69 }
70 memo.set(pid, found);
71 return found;
72 };
73 for (const p of procs) find(p.pid);
74 return memo;
75 }
76
77 const rate = (a: number | undefined, b: number | undefined, s: number): number | null =>
78 a === undefined || b === undefined || s <= 0 || b < a ? null : (b - a) / s;
79
80 export function buildSystem(prev: Raw | undefined, next: Raw, roots: Roots, self: number): SystemView {
81 const seconds = prev === undefined ? 0 : Math.max(0, (next.at - prev.at) / 1000);
82 const cores = next.cores.map((c, i) => {
83 const p = prev?.cores[i];
84 if (!p || seconds <= 0 || c.total <= p.total) return null;
85 return Math.round(((c.busy - p.busy) / (c.total - p.total)) * 1000) / 10;
86 });
87 const known = cores.filter((c): c is number => c !== null);
88 const before = new Map((prev?.procs ?? []).map((p) => [p.pid, p]));
89 const where = groupOf(next.procs, roots, self);
90 const groups = new Map<string, Group>();
91 const files = new Map<string, Set<string>>();
92 for (const p of next.procs) {
93 const label = where.get(p.pid) ?? "host";
94 const g =
95 groups.get(label) ??
96 { label, kind: kindOf(label), procs: 0, cpuCores: null, rssBytes: 0, oldestSeconds: 0, top: [], tests: [] };
97 const old = before.get(p.pid);
98 const cpu = old && old.age <= p.age ? rate(old.ticks, p.ticks, seconds * TICKS_PER_SECOND) : null;
99 g.procs++;
100 g.rssBytes += p.rss;
101 g.oldestSeconds = Math.max(g.oldestSeconds, Math.round(p.age));
102 if (cpu !== null) g.cpuCores = (g.cpuCores ?? 0) + cpu;
103 g.top.push({ pid: p.pid, comm: p.comm, cpuCores: cpu, rssBytes: p.rss, ageSeconds: Math.round(p.age) });
104 for (const f of p.tests) {
105 if (!g.tests.includes(f)) g.tests.push(f);
106 if (g.kind === "check") (files.get(f) ?? files.set(f, new Set()).get(f)!).add(label);
107 }
108 groups.set(label, g);
109 }
110 for (const g of groups.values()) {
111 g.top = g.top
112 .sort((a, b) => (b.cpuCores ?? 0) - (a.cpuCores ?? 0) || b.rssBytes - a.rssBytes)
113 .slice(0, 5);
114 if (g.cpuCores !== null) g.cpuCores = Math.round(g.cpuCores * 100) / 100;
115 }
116 const mem = next.memory;
117 return {
118 at: new Date(next.at).toISOString(),
119 seconds,
120 cpu: {
121 busyPct: known.length === 0 ? null : Math.round((known.reduce((a, b) => a + b, 0) / known.length) * 10) / 10,
122 cores,
123 },
124 temps: next.temps,
125 hottestC: next.temps.length === 0 ? null : Math.max(...next.temps.map((t) => t.celsius)),
126 load: next.load,
127 memory: mem && {
128 totalBytes: mem.total,
129 usedBytes: mem.total - mem.available,
130 swapUsedBytes: mem.swapTotal - mem.swapFree,
131 },
132 disks: next.disks.map((d) => ({ mount: d.mount, fs: d.fs, totalBytes: d.total, usedBytes: d.used })),
133 net: next.net.map((n) => {
134 const o = prev?.net.find((x) => x.name === n.name);
135 return {
136 name: n.name,
137 rxBytesPerSecond: rate(o?.rx, n.rx, seconds),
138 txBytesPerSecond: rate(o?.tx, n.tx, seconds),
139 };
140 }),
141 processes: next.procs.length,
142 groups: [...groups.values()].sort((a, b) => (b.cpuCores ?? 0) - (a.cpuCores ?? 0) || b.rssBytes - a.rssBytes),
143 duplicates: [...files]
144 .filter(([, labels]) => labels.size > 1)
145 .map(([file, labels]) => ({ file, checks: [...labels].sort() }))
146 .sort((a, b) => b.checks.length - a.checks.length || (a.file < b.file ? -1 : 1)),
147 };
148 }
149
150 /**
151 * What a named agent may read: a private repository's checks and apps keep
152 * their place in the totals but lose their name, their test files and their
153 * processes. `visible` says whether a label's repository is public.
154 */
155 export function forAgent(view: SystemView, visible: (label: string) => boolean): SystemView {
156 const hide = new Map<string, string>();
157 const n = { check: 0, app: 0 };
158 const name = (label: string): string => {
159 if (visible(label) || kindOf(label) === "forge" || kindOf(label) === "host") return label;
160 const kind = kindOf(label) as "check" | "app";
161 return hide.get(label) ?? hide.set(label, `${kind}:private-${++n[kind]}`).get(label)!;
162 };
163 return {
164 ...view,
165 groups: view.groups.map((g) =>
166 // The forge and the host hold whatever no labelled cell claims (a crew run in a private clone,
outdated

Low. An agent sees the host group's top process names. A private app or check whose cell is not watched (so it has no label) lands in host, which means its process names reach an agent. Consider clearing top for host in forAgent, since the counts are enough.

167 // an unwatched sandbox): their counts stay, their processes and test files never leave.
168 g.kind === "forge" || g.kind === "host"
169 ? { ...g, top: [], tests: [] }
170 : visible(g.label)
171 ? g
172 : { ...g, label: name(g.label), top: [], tests: [] },
173 ),
174 duplicates: view.duplicates
175 .map((d) => ({ file: d.checks.every(visible) ? d.file : "(private)", checks: d.checks.map(name).sort() }))
176 .sort((a, b) => b.checks.length - a.checks.length),
177 };
outdated

duplicateCount is never used; /flow and welcome read .duplicates.length directly. Delete it.

178 }
src/tui/forge.ts +50
@@ -24,6 +24,7 @@ import {
2424 ORIGIN_MARK,
2525 PULL_MARK,
2626 errorText,
27 pickerScreen,
2728 promptScreen,
2829 openOnWeb,
2930 runAsync,
@@ -41,8 +42,15 @@ import { accessScreen } from "./screens/access.ts";
4142 import { domainsScreen } from "./screens/domains.ts";
4243 import { resilienceLines, resilienceScreen } from "./screens/resilience.ts";
4344 import { flowLines, flowScreen } from "./screens/flow.ts";
45 <<<<<<< HEAD
46 import { systemLines, systemScreen } from "./screens/system.ts";
47 import { grantable, READS_KEPT, recentReads } from "../core/telemetry.ts";
48 import { setTelemetryGrant } from "../actions/telemetry.ts";
49 import { live as liveMachine } from "../system/live.ts";
50 =======
4451 import { releasesScreen } from "./screens/releases.ts";
4552 import { mayRead } from "../release/gate.ts";
53 >>>>>>> origin/main
4654 import { stalledScreen } from "./screens/stalled.ts";
4755 import { issuesScreen, newIssueScreen } from "./screens/issues.ts";
4856 import { limbsScreen } from "./screens/limbs.ts";
@@ -236,6 +244,48 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] {
236244 hint: "the pipeline's bottleneck and throughput",
237245 run: () => ctx.push(flowScreen(ctx, flowLines(session.db))),
238246 });
247 // The machine, as /system says it, and which agents may read it: the owner's, a person (#1789).
248 if (session.user?.isAdmin === true && session.user.kind !== "agent") {
outdated

This file already has blank(title) at line 466 for exactly this. Use blank("Telemetry") here.

249 commands.push({
250 id: "system",
251 title: "System",
252 hint: "the machine now: CPU, memory, disks, processes",
253 run: () => ctx.push(systemScreen(ctx, systemLines(liveMachine().read()))),
254 });
255 commands.push({
256 id: "telemetry",
257 title: "Telemetry",
258 hint: "which agents may read flow and system; enter flips the grant",
259 run: () => {
260 const actor = session.user;
261 const agents = grantable(session.db);
262 if (agents.length === 0) return ctx.toast("No agent accounts yet");
263 const last = new Map<string, { n: number; what: string; at: string }>();
264 for (const r of recentReads(session.db, READS_KEPT).reverse()) {
265 last.set(r.username, { n: (last.get(r.username)?.n ?? 0) + 1, what: r.what, at: r.at });
266 }
267 const readsOf = (name: string): string => {
268 const l = last.get(name);
269 return l ? `${l.n} read${l.n === 1 ? "" : "s"}, last ${l.what} ${l.at}` : "no reads";
270 };
271 ctx.push(
272 pickerScreen(ctx, blank("Telemetry"), {
273 title: "Telemetry: telemetry:read",
274 items: agents.map((a) => ({ id: a.username, label: a.username, hint: `${a.granted ? "granted" : "not granted"} · ${readsOf(a.username)}` })),
275 onPick: ([name]) => {
276 const was = agents.find((a) => a.username === name);
277 if (!was) return;
278 const done = setTelemetryGrant(session.app, actor, was.username, !was.granted);
279 ctx.toast(
280 done.ok ? `${was.username}: telemetry:read ${done.value.on ? "granted" : "revoked"}` : "Could not change the grant",
281 done.ok ? "success" : "attn",
282 );
283 },
284 }),
285 );
286 },
287 });
288 }
239289 // Each THREATS.md protection's standing, as /resilience says it: a site admin's.
240290 if (session.user?.isAdmin === true)
241291 commands.push({
src/tui/screens/flow.ts +5 −1
@@ -17,11 +17,13 @@ import type { Ctx, Screen } from "../app/app.ts";
1717 import { binding } from "../app/keys.ts";
1818 import { pad, truncate, wrap } from "../term/width.ts";
1919 import { dialog } from "../ui/widgets.ts";
20 import { live } from "../../system/live.ts";
21 import { duplicateLines } from "./system.ts";
2022
2123 const WIDTH = 64;
2224
2325 /** What the dialog says, line by line, wrapped to fit inside it. */
24 export function flowLines(db: Database, now = new Date()): string[] {
26 export function flowLines(db: Database, now = new Date(), dups = live().read().duplicates): string[] {
2527 const stages = readPipeline(db, now);
2628 const flow = readThroughput(db, stages.map((s) => s.key), now);
2729 const inner = WIDTH - 6;
@@ -46,6 +48,8 @@ export function flowLines(db: Database, now = new Date()): string[] {
4648 `${pad(truncate(q.repo, 30), 30)} ${pad(String(q.queued), 6, "right")} ${pad(String(q.gating), 6, "right")} ${pad(String(q.landedHour), 9, "right")}`,
4749 )),
4850 ...(conflicted === null ? [] : ["", ...wrap(conflicted, inner)]),
51 "",
52 ...duplicateLines(dups, inner),
4953 ];
5054 }
5155
src/tui/screens/system.ts added +66
@@ -0,0 +1,66 @@
1 /**
2 * The System command's dialog, the owner's (#1789): the machine now, in the
3 * lines of `/system` — CPU, load, heat, memory, disks, and the heaviest
4 * groups of processes with the test files running in more than one check.
5 * It reads `live().read()`, the same view the page and the API serve.
6 * Read-only; Enter, Esc or q closes it.
7 */
8
9 import type { Ctx, Screen } from "../app/app.ts";
10 import type { SystemView } from "../../system/view.ts";
11 import { binding } from "../app/keys.ts";
12 import { bytes } from "../../system/format.ts";
13 import { pad, wrap } from "../term/width.ts";
14 import { dialog } from "../ui/widgets.ts";
15
16 const WIDTH = 72;
17 const GROUPS = 6;
18
19 const pct = (n: number | null): string => (n === null ? "—" : `${n.toFixed(1)}%`);
20
21 /** The duplicate-work sentence, wrapped: the one /flow and /system say. */
22 export function duplicateLines(dups: SystemView["duplicates"], inner: number): string[] {
23 return dups.length === 0
24 ? ["No test file is running in more than one check."]
25 : wrap(
26 `${dups.length} test file${dups.length === 1 ? " is" : "s are"} running in more than one check: ${dups
27 .slice(0, 3)
28 .map((d) => `${d.file} (${d.checks.join(", ")})`)
29 .join("; ")}`,
30 inner,
31 );
32 }
33
34 export function systemLines(v: SystemView): string[] {
35 const inner = WIDTH - 6;
36 return [
37 `CPU ${pct(v.cpu.busyPct)} over ${v.cpu.cores.length} cores · load ${v.load ? v.load.map((l) => l.toFixed(2)).join(" ") : "—"}`,
38 `hottest ${v.hottestC === null ? "—" : `${v.hottestC.toFixed(0)}°C`} · memory ${v.memory ? `${bytes(v.memory.usedBytes)} of ${bytes(v.memory.totalBytes)}` : "—"} · ${v.processes} processes`,
39 ...v.disks.map((d) => `${pad(d.mount, 20)} ${Math.round((d.usedBytes / Math.max(1, d.totalBytes)) * 100)}% of ${bytes(d.totalBytes)}`),
40 "",
41 ...v.groups.slice(0, GROUPS).map((g) => `${pad(g.label, 28)} ${pad(String(g.procs), 4, "right")} procs`),
42 "",
43 ...duplicateLines(v.duplicates, inner),
44 ];
45 }
46
47 export function systemScreen(ctx: Ctx, lines: readonly string[]): Screen {
48 return {
49 title: "System",
50 keys: [binding(["enter", "esc", "q"], "close")],
51 render(area, s) {
52 const buttons = dialog(s, area, ctx.theme.c, {
53 title: "System",
54 body: lines,
55 actions: [{ label: "Close" }],
56 selected: 0,
57 width: WIDTH,
58 });
59 buttons.forEach((rect) => ctx.hit(rect, () => ctx.pop()));
60 },
61 key(e) {
62 if (e.key === "enter" || e.key === "esc" || e.key === "q") ctx.pop();
63 return true;
64 },
65 };
66 }
src/web/layout.ts +2
@@ -329,6 +329,7 @@ function paletteIndex(ctx: Ctx, extra: PaletteItem[]): Html {
329329 ...(ctx.auth.user?.isAdmin
330330 ? [
331331 { t: "Flow", h: "/flow", c: "Go to" },
332 { t: "System", h: "/system", c: "Go to" },
332333 { t: "Forges you sent", h: "/forges", c: "Go to" },
333334 { t: "Family of forges", h: "/family", c: "Go to" },
334335 ]
@@ -510,6 +511,7 @@ function footer(ctx: Ctx): Html {
510511 : html`<span class="reduced" id="reduced-protection"
511512 >${reduced}</span
512513 >`}
514 <a class="muted small" href="/contribute">Contribute</a>
513515 <span class="spacer"></span>
514516 <button
515517 class="btn js-only"
src/web/pages/contribute.ts added +36
@@ -0,0 +1,36 @@
1 /**
2 * /contribute, open to anyone: what this forge is, where its bottleneck is
3 * right now, the open issues that welcome outside work, and how to propose
4 * (#1789). The same words as the MCP `contribute` tool and
5 * `GET /api/v1/contribute` (`system/telemetry.ts`).
6 */
7 import type { Ctx } from "../../http/context.ts";
8 import { welcome } from "../../system/telemetry.ts";
9 import { html } from "../html.ts";
10 import { page } from "../layout.ts";
11 import { card } from "../ui.ts";
12
13 // GET /contribute
14 export function contributePage(ctx: Ctx): Response {
15 const w = welcome(ctx.app);
16 const body = html`<div class="page-head">
17 <h1>Help build this forge</h1>
18 <p class="muted">${w.forge}</p>
19 </div>
20 <p id="bottleneck"><strong>Where it hurts now:</strong> ${w.bottleneck.says}</p>
21 ${card(
22 w.issues.length === 0
23 ? html`<div class="card-body"><p class="muted">No open issue carries the “${w.welcomeLabel}” label right now.</p></div>`
24 : html`<ul class="list">${w.issues.map(
25 (i) => html`<li class="row"><a href="${i.path}">${i.repo}#${String(i.number)}</a> ${i.title}</li>`,
26 )}</ul>`,
27 { head: html`<h2>Issues that welcome outside work</h2>`, pad: false },
28 )}
29 ${card(
30 html`<div class="card-body"><ol id="propose">${w.propose.map((s) => html`<li>${s}</li>`)}</ol>
31 <p class="muted small">${w.telemetry}</p>
32 <p class="muted small">For agents: <span class="mono">GET /api/v1/contribute</span>, or the MCP tool <span class="mono">contribute</span>.</p></div>`,
33 { head: html`<h2>How to propose</h2>` },
34 )}`;
35 return page({ ctx, title: "Contribute", body });
36 }
src/web/pages/flow.ts +9
@@ -41,6 +41,7 @@ import {
4141 } from "../../flow/throughput.ts";
4242 import { readQueues, type QueueRow } from "../../flow/queue.ts";
4343 import type { Ctx } from "../../http/context.ts";
44 import { live } from "../../system/live.ts";
4445 import { notFoundPage, signInFirst } from "../chrome.ts";
4546 import { html, type Html } from "../html.ts";
4647 import { page } from "../layout.ts";
@@ -106,6 +107,7 @@ export function flowPage(ctx: Ctx): Response {
106107 const flow = readThroughput(ctx.app.db, stages.map((s) => s.key), new Date(now));
107108 const out = new Map(flow.stages.map((t) => [t.key, t]));
108109 const landed = out.get("approved");
110 const dups = live().read().duplicates;
109111 const conflicted = conflictedSays(conflictedPulls(ctx.app.db), now);
110112 const listed = asked(ctx.url);
111113 const shown = listed === null ? undefined : out.get(listed.key);
@@ -132,6 +134,13 @@ export function flowPage(ctx: Ctx): Response {
132134 <span data-median>${flow.medianAskToLiveMs === null ? "—" : hoursSpan(flow.medianAskToLiveMs)}</span>
133135 (24 h)
134136 </p>
137 <p id="duplicate-work" data-count="${String(dups.length)}" class="${dups.length === 0 ? "muted" : ""}">
138 ${dups.length === 0
139 ? html`No test file is running in more than one check.`
140 : html`<strong>${String(dups.length)} test file${dups.length === 1 ? " is" : "s are"} running in more than one check:</strong>
141 ${dups.slice(0, 5).map((d) => html`<span class="mono small">${d.file} (${d.checks.join(", ")})</span> `)}`}
142 <a href="/system">System</a>
143 </p>
135144 ${card(
136145 html`<div class="table-scroll">
137146 <table class="table" aria-label="Pipeline stages">
src/web/pages/origins.ts +7
@@ -81,6 +81,7 @@ export type AccountSection =
8181 | "backups"
8282 | "secrets"
8383 | "capacity"
84 | "telemetry"
8485 | "access"
8586 | "resilience"
8687 | "limits"
@@ -135,6 +136,12 @@ export function accountSections(
135136 icon: "activity" as const,
136137 current: current === "capacity",
137138 },
139 {
140 label: "Telemetry",
141 href: "/settings/telemetry",
142 icon: "activity" as const,
143 current: current === "telemetry",
144 },
138145 {
139146 label: "Access",
140147 href: "/settings/access",
src/web/pages/system.ts added +95
@@ -0,0 +1,95 @@
1 /**
2 * /system, for the owner: the machine as it is now — cores, temperatures,
3 * load, memory, disks, network — and every process grouped by what the
4 * forge launched it for, each with CPU, memory and age. The same test file
5 * in more than one check at once is named at the top. It refreshes itself
6 * every 2 s (a few lines of static script; `?pause` turns that off), so a
7 * viewer *is* the sampler (`system/live.ts`). Numbers are `system/view.ts`'s.
8 */
9 import { telemetryReader } from "../../actions/telemetry.ts";
10 import type { Ctx } from "../../http/context.ts";
11 import { bytes, span } from "../../system/format.ts";
12 import { live } from "../../system/live.ts";
13 import { notFoundPage, signInFirst } from "../chrome.ts";
14 import { html, type Html } from "../html.ts";
15 import { page } from "../layout.ts";
16 import { card } from "../ui.ts";
17
18 const pct = (n: number | null): string => (n === null ? "—" : `${n.toFixed(1)}%`);
19 const coresOf = (n: number | null): string => (n === null ? "—" : n.toFixed(2));
20
21 const RELOAD = `if (!/[?&]pause\\b/.test(location.search)) setTimeout(function () { location.reload(); }, 2000);`;
22
23 // GET /system
24 export function systemPage(ctx: Ctx): Response {
25 if (!ctx.auth.user) return signInFirst(ctx);
26 const reader = telemetryReader(ctx.app, ctx.auth.user);
27 if (reader?.kind !== "owner") return notFoundPage(ctx);
28 const v = live().read();
29 const dup: Html =
30 v.duplicates.length === 0
31 ? html`<p id="duplicates" data-count="0" class="muted">No test file is running in more than one check.</p>`
32 : html`<p id="duplicates" data-count="${String(v.duplicates.length)}">
33 <strong>${String(v.duplicates.length)} test file${v.duplicates.length === 1 ? " is" : "s are"}
34 running in more than one check at once:</strong>
35 </p>
36 <ul class="list small mono">
37 ${v.duplicates.map((d) => html`<li>${d.file} · ${String(d.checks.length)} checks: ${d.checks.join(", ")}</li>`)}
38 </ul>`;
39 const cores = v.cpu.cores.map(
40 (c, i) => html`<span class="mono small" title="cpu${String(i)}">${c === null ? "—" : String(Math.round(c))}</span> `,
41 );
42 const body = html`<div class="page-head">
43 <h1>System</h1>
44 <p class="muted">
45 The machine now, sampled each time this page refreshes (every 2 s);
46 <a href="/system?pause">pause</a>. Its history is on
47 <a href="/boards">Boards</a> (the machine source); the pipeline is on
48 <a href="/flow">Flow</a>.
49 </p>
50 </div>
51 ${dup}
52 ${card(
53 html`<div class="card-body">
54 <p id="summary">
55 CPU <strong>${pct(v.cpu.busyPct)}</strong> over ${String(v.cpu.cores.length)} cores ·
56 load ${v.load ? v.load.map((l) => l.toFixed(2)).join(" ") : "—"} ·
57 hottest ${v.hottestC === null ? "—" : `${v.hottestC.toFixed(0)}°C`} ·
58 memory ${v.memory ? `${bytes(v.memory.usedBytes)} of ${bytes(v.memory.totalBytes)}` : "—"} ·
59 ${String(v.processes)} processes
60 </p>
61 <p class="small" id="cores" aria-label="Per-core CPU, percent">${cores}</p>
62 <p class="small mono" id="temps">${v.temps.map((t) => `${t.chip} ${t.label} ${t.celsius.toFixed(0)}°C`).join(" · ")}</p>
63 </div>`,
64 { head: html`<h2>Machine</h2>` },
65 )}
66 ${card(
67 html`<div class="table-scroll"><table class="table" aria-label="Disks and network">
68 <thead><tr><th scope="col">Disk / interface</th><th scope="col">Used</th><th scope="col">Total / rate</th></tr></thead>
69 <tbody>
70 ${v.disks.map(
71 (d) => html`<tr><td class="mono">${d.mount} <span class="muted small">${d.fs}</span></td>
72 <td>${String(Math.round((d.usedBytes / d.totalBytes) * 100))}%</td><td>${bytes(d.totalBytes)}</td></tr>`,
73 )}
74 ${v.net.map(
75 (n) => html`<tr><td class="mono">${n.name}</td><td>↓ ${n.rxBytesPerSecond === null ? "—" : `${bytes(n.rxBytesPerSecond)}/s`}</td>
76 <td>↑ ${n.txBytesPerSecond === null ? "—" : `${bytes(n.txBytesPerSecond)}/s`}</td></tr>`,
77 )}
78 </tbody></table></div>`,
79 { head: html`<h2>Disks and network</h2>`, pad: false },
80 )}
81 ${card(
82 html`<div class="table-scroll"><table class="table" aria-label="Processes by what launched them">
83 <thead><tr><th scope="col">Launched for</th><th scope="col">Processes</th><th scope="col">CPU (cores)</th>
84 <th scope="col">Memory</th><th scope="col">Oldest</th><th scope="col">Busiest</th></tr></thead>
85 <tbody>
86 ${v.groups.map(
87 (g) => html`<tr data-kind="${g.kind}"><td class="mono">${g.label}</td><td>${String(g.procs)}</td>
88 <td>${coresOf(g.cpuCores)}</td><td>${bytes(g.rssBytes)}</td><td>${span(g.oldestSeconds)}</td>
89 <td class="small mono">${g.top.slice(0, 3).map((p) => `${p.comm} ${coresOf(p.cpuCores)}`).join(" · ")}</td></tr>`,
90 )}
91 </tbody></table></div>`,
92 { head: html`<h2>Processes</h2>`, pad: false },
93 )}`;
94 return page({ ctx, title: "System", body, wide: true, scriptJs: RELOAD });
95 }
src/web/pages/telemetry.ts added +85
@@ -0,0 +1,85 @@
1 /**
2 * Settings → Telemetry, for the owner: which named agents hold
3 * `telemetry:read`, one switch each, and the reads they have made (#1789).
4 */
5 import { setTelemetryGrant } from "../../actions/telemetry.ts";
6 import { grantable, recentReads } from "../../core/telemetry.ts";
7 import type { Ctx } from "../../http/context.ts";
8 import { notFoundPage, settingsLayout, signInFirst } from "../chrome.ts";
9 import { field, readForm } from "../form.ts";
10 import { html } from "../html.ts";
11 import { flashTo, page } from "../layout.ts";
12 import { btn, card, icon, row, time } from "../ui.ts";
13 import { accountSections } from "./origins.ts";
14
15 function owner(ctx: Ctx): Response | undefined {
16 if (!ctx.auth.user) return signInFirst(ctx);
17 return ctx.auth.user.isAdmin && ctx.auth.user.kind !== "agent" ? undefined : notFoundPage(ctx);
18 }
19
20 // GET /settings/telemetry
21 export function telemetryPage(ctx: Ctx): Response {
22 const denied = owner(ctx);
23 if (denied) return denied;
24 const agents = grantable(ctx.app.db);
25 const reads = recentReads(ctx.app.db);
26 const body = html`${card(
27 html`<div class="card-body"><p class="muted small">
28 A granted agent reads <a href="/flow">Flow</a>, <a href="/system">System</a> and the
29 boards' sources through the API and MCP — counts and rates, with
30 private repositories and sealed values cut out. Revoke at any time;
31 every read is listed below.
32 </p></div>
33 <ul class="list">
34 ${agents.length === 0
35 ? html`<li class="muted small">No agent accounts yet.</li>`
36 : agents.map((a) =>
37 row({
38 lead: icon("bot"),
39 title: html`${a.username} <span class="muted small mono">telemetry:read</span>`,
40 meta: html`${a.granted ? "Granted" : "Not granted"}`,
41 trail: html`<form class="row-actions" method="post" action="/settings/telemetry">
42 <input type="hidden" name="agent" value="${a.username}" />
43 <input type="hidden" name="on" value="${a.granted ? "0" : "1"}" />
44 ${btn(a.granted ? "Revoke" : "Grant", {
45 size: "sm",
46 variant: a.granted ? "outline" : "primary",
47 label: `${a.granted ? "Revoke" : "Grant"} telemetry:read for ${a.username}`,
48 })}
49 </form>`,
50 }),
51 )}
52 </ul>`,
53 { head: html`${icon("activity")}<h2>Telemetry grants</h2>`, pad: false },
54 )}
55 ${card(
56 reads.length === 0
57 ? html`<div class="card-body"><p class="muted small">No agent has read anything yet.</p></div>`
58 : html`<ul class="list">${reads.map((r) =>
59 row({ lead: icon("history"), title: html`${r.username} <span class="mono">${r.what}</span>`, meta: time(r.at) }),
60 )}</ul>`,
61 { head: html`${icon("history")}<h2>Reads</h2>`, pad: false },
62 )}`;
63 return page({
64 ctx,
65 title: "Telemetry",
66 body: html`<div class="page-head"><h1>Telemetry</h1></div>
67 ${settingsLayout(accountSections("telemetry", { admin: true }), body, "Account settings")}`,
68 });
69 }
70
71 // POST /settings/telemetry
72 export async function telemetrySet(ctx: Ctx): Promise<Response> {
73 const denied = owner(ctx);
74 if (denied) return denied;
75 const form = await readForm(ctx, "admin");
76 if (form instanceof Response) return form;
77 const changed = setTelemetryGrant(ctx.app, ctx.auth.user, field(form, "agent"), field(form, "on") === "1");
78 return flashTo(
79 ctx,
80 "/settings/telemetry",
81 changed.ok
82 ? { message: `${changed.value.username} ${changed.value.on ? "may now read" : "no longer reads"} telemetry.` }
83 : { tone: "danger", message: "Only an agent account can be granted telemetry:read." },
84 );
85 }
test/mcp.test.ts +2
@@ -209,6 +209,8 @@ describe("the forge over MCP", () => {
209209 "access",
210210 "capacity",
211211 "flow",
212 "system",
213 "contribute",
212214 "limits",
213215 "quotas",
214216 "boards",
test/system-telemetry.test.ts added +381
@@ -0,0 +1,381 @@
1 /**
2 * Live machine telemetry and its open read hooks (#1789): /proc and /sys
3 * read into one view; the same test file in two checks counted as duplicate
4 * work; /system and /flow read by the owner and by an agent holding
5 * `telemetry:read` — with private repositories cut out — and refused to an
6 * agent without it; the grant is the owner's alone and every agent read is
7 * logged; the funnel is open to all.
8 */
9 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
10 import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
11 import { mkdtemp, rm } from "node:fs/promises";
12 import { tmpdir } from "node:os";
13 import { join } from "node:path";
14 import { createApp, type App } from "../src/app.ts";
15 import { createAgent } from "../src/actions/crew.ts";
16 import { createRepo } from "../src/actions/repos.ts";
17 import { setTelemetryGrant } from "../src/actions/telemetry.ts";
18 import { recentReads } from "../src/core/telemetry.ts";
19 import { createToken } from "../src/core/tokens.ts";
20 import { createUser, type User } from "../src/core/users.ts";
21 import { serve, type Servers } from "../src/http/server.ts";
22 import { parseCores, parseNet, testFiles } from "../src/system/proc.ts";
23 import { createLive, live, useLive } from "../src/system/live.ts";
24 import type { Ctx, Screen } from "../src/tui/app/app.ts";
25 import { forgeCommands, Session } from "../src/tui/forge.ts";
26 import { systemLines } from "../src/tui/screens/system.ts";
27 import { flowLines } from "../src/tui/screens/flow.ts";
28 import { hasGrant, logRead } from "../src/core/telemetry.ts";
29
30 const stat = (pid: number, comm: string, ppid: number, ticks: number): string => {
31 const f: (number | string)[] = new Array<number>(40).fill(0);
32 f[0] = "S";
33 f[1] = ppid;
34 f[11] = ticks;
35 f[19] = 100;
36 f[21] = 256;
37 return `${pid} (${comm}) ${f.join(" ")}`;
38 };
39
40 function fakeHost(root: string, ticks: number): void {
41 const proc = join(root, "proc");
42 mkdirSync(join(proc, "net"), { recursive: true });
43 mkdirSync(join(root, "sys/class/hwmon/hwmon0"), { recursive: true });
44 writeFileSync(join(root, "sys/class/hwmon/hwmon0/name"), "coretemp\n");
45 writeFileSync(join(root, "sys/class/hwmon/hwmon0/temp1_input"), "76000\n");
46 writeFileSync(join(root, "sys/class/hwmon/hwmon0/temp1_label"), "Core 0\n");
47 writeFileSync(
48 join(proc, "stat"),
49 `cpu ${ticks} 0 0 100 0 0 0 0\ncpu0 ${ticks} 0 0 100 0 0 0 0\ncpu1 0 0 0 ${ticks} 0 0 0 0\n`,
50 );
51 writeFileSync(join(proc, "meminfo"), "MemTotal: 1000 kB\nMemAvailable: 400 kB\nSwapTotal: 0 kB\nSwapFree: 0 kB\n");
52 writeFileSync(join(proc, "loadavg"), "64.00 60.00 50.00 3/3071 999\n");
53 writeFileSync(join(proc, "uptime"), "5000.00 100.00\n");
54 writeFileSync(join(proc, "mounts"), "");
55 writeFileSync(
56 join(proc, "net/dev"),
57 "Inter-| Receive\n face |bytes\n lo: 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0\n eth0: ${r} 0 0 0 0 0 0 0 ${t} 0 0 0 0 0 0 0\n"
58 .replace("${r}", String(ticks * 10))
59 .replace("${t}", String(ticks * 5)),
60 );
61 const proc1 = (pid: number, comm: string, ppid: number, args: string) => {
62 mkdirSync(join(proc, String(pid)), { recursive: true });
63 writeFileSync(join(proc, String(pid), "stat"), stat(pid, comm, ppid, ticks));
64 writeFileSync(join(proc, String(pid), "cmdline"), args);
65 };
66 proc1(1, "forge", 0, "bun\0src/main.ts\0");
67 proc1(200, "bwrap", 1, "bwrap\0");
68 proc1(201, "bun", 200, "bun\0test\0test/shared.test.ts\0--secret-flag=hunter2\0");
outdated

privateRun and publicRun are dead parameters kept alive with void, and so is addToCrew further down. Drop them.

69 proc1(300, "bwrap", 1, "bwrap\0");
70 proc1(301, "bun", 300, "bun\0test\0./test/shared.test.ts\0");
71 proc1(400, "bwrap", 1, "bwrap\0");
72 proc1(401, "bun", 400, "bun\0test\0test/alone.test.ts\0");
73 proc1(500, "bun", 1, "bun\0test\0test/private-thing.test.ts\0");
74 }
75
76 describe("the machine, read from /proc and /sys", () => {
77 test("per-core ticks, interfaces and test-file arguments — nothing else of a command line", () => {
78 expect(parseCores("cpu 9 0 0 1\ncpu0 3 0 0 1 0 0 0 0\ncpu1 1 0 0 3 0 0 0 0\n")).toEqual([
79 { busy: 3, total: 4 },
80 { busy: 1, total: 4 },
81 ]);
82 expect(parseNet("h\nh\n lo: 5 0 0 0 0 0 0 0 5\neth0: 7 0 0 0 0 0 0 0 9\n")).toEqual([{ name: "eth0", rx: 7, tx: 9 }]);
83 expect(testFiles("bun\0test\0./test/a.test.ts\0--token=abc\0../etc/x\0$(id).test.ts\0")).toEqual(["test/a.test.ts"]);
84 });
85
86 test("two checks running one test file are duplicate work, grouped by what launched them", () => {
87 const root = mkdtempSync(join(tmpdir(), "forge-sys-"));
88 try {
89 fakeHost(root, 100);
90 const roots = [
91 { label: "check:RUN1", root: 200 },
92 { label: "check:RUN2", root: 300 },
93 { label: "check:RUN3", root: 400 },
94 ];
95 const l = createLive({ paths: { proc: join(root, "proc"), sys: join(root, "sys") }, roots: () => roots, self: 1 });
96 const v = l.read(1_000_000);
97 expect(v.duplicates).toEqual([{ file: "test/shared.test.ts", checks: ["check:RUN1", "check:RUN2"] }]);
98 expect(v.groups.find((g) => g.label === "check:RUN1")?.procs).toBe(2);
99 expect(v.groups.find((g) => g.label === "forge")?.procs).toBe(2);
100 expect(v.cpu.cores.length).toBe(2);
101 expect(v.hottestC).toBe(76);
102 expect(v.load?.[0]).toBe(64);
103 expect(v.memory).toEqual({ totalBytes: 1024_000, usedBytes: 614_400, swapUsedBytes: 0 });
104 expect(JSON.stringify(v)).not.toContain("hunter2");
105 // A later reading gives rates over the two latest.
106 fakeHost(root, 300);
107 const w = l.read(1_002_000);
108 expect(w.seconds).toBeCloseTo(2, 5);
109 expect(v.seconds).toBe(0);
110 expect(v.net[0]?.rxBytesPerSecond).toBeNull();
111 expect(w.net[0]?.rxBytesPerSecond).toBeCloseTo(2000 / 2, 3);
112 expect(l.history().length).toBe(2);
113 } finally {
114 rmSync(root, { recursive: true, force: true });
115 }
116 });
117 });
118
119 describe("telemetry:read: the owner grants it, an agent reads with it, nobody else does", () => {
120 let dataDir: string;
121 let host: string;
122 let app: App;
123 let servers: Servers;
124 let alice: User;
125 let bob: User;
126 let muse: User;
127 let grok: User;
128 let tokens: Record<string, string>;
129
130 const get = (token: string | undefined, path: string) =>
131 fetch(`${servers.url}${path}`, token ? { headers: { authorization: `Bearer ${token}` } } : {});
132 const login = async (username: string): Promise<string> => {
133 const res = await fetch(`${servers.url}/signin`, {
134 method: "POST",
135 redirect: "manual",
136 headers: { "content-type": "application/x-www-form-urlencoded", origin: servers.url },
137 body: new URLSearchParams({ username, password: "a-good-passphrase" }),
138 });
139 return (res.headers.get("set-cookie") ?? "").split(";")[0] ?? "";
140 };
141 const tool = async (token: string, name: string) => {
142 const res = await fetch(`${servers.url}/mcp`, {
143 method: "POST",
144 headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" },
145 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: {} } }),
146 });
147 const b = (await res.json()) as { result: { isError?: boolean; content: { text: string }[] } };
148 return { error: b.result.isError === true, text: b.result.content[0]!.text };
149 };
150
151 beforeAll(async () => {
152 dataDir = await mkdtemp(join(tmpdir(), "forge-telemetry-"));
153 host = mkdtempSync(join(tmpdir(), "forge-host-"));
154 app = createApp({ dataDir, port: 0 });
155 servers = await serve(app);
156 const mk = async (username: string, adminIfFirst = false) => {
157 const u = await createUser(app.db, {
158 trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst,
159 });
160 if (!u.ok) throw new Error(u.error);
161 return u.value;
162 };
163 alice = await mk("alice", true);
164 bob = await mk("bob");
165 const agent = async (username: string) => {
166 const made = await createAgent(app, alice, { username });
167 if (!made.ok) throw new Error(String(made.error));
168 return made.value.user;
169 };
170 muse = await agent("muse");
171 grok = await agent("grokbot");
172 const priv = await createRepo(app, alice, { name: "vault-of-secrets", visibility: "private" });
173 const pub = await createRepo(app, alice, { name: "open-garden", visibility: "public" });
174 if (!priv.ok || !pub.ok) throw new Error("repos");
175 for (const [id, repo] of [["RUN1", priv.value], ["RUN2", pub.value], ["RUN3", pub.value]] as const)
176 app.db
177 .query("INSERT INTO run (id, repo_id, kind, state) VALUES (?1, ?2, 'deploy', 'running')")
178 .run(id, repo.id);
179 // One pull in conflict in each repository: only the public one may reach an agent.
180 for (const [n, repo] of [[1, priv.value], [2, pub.value]] as const) {
181 const id = `PULL${n}`;
182 app.db
183 .query("INSERT INTO issue (id, repo_id, number, kind, title, author_id) VALUES (?1, ?2, ?3, 'pull', 'p', ?4)")
184 .run(id, repo.id, n, alice.id);
185 app.db
186 .query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'f', 'a', 'b')")
187 .run(id);
188 app.db
189 .query("INSERT INTO run (id, repo_id, kind, issue_id, reason, state) VALUES (?1, ?2, 'agent', ?3, 'conflict', 'queued')")
190 .run(`CONF${n}`, repo.id, id);
191 }
192 fakeHost(host, 100);
193 useLive(
194 createLive({
195 paths: { proc: join(host, "proc"), sys: join(host, "sys") },
196 roots: () => [
197 { label: "check:RUN1", root: 200 },
198 { label: "check:RUN2", root: 300 },
199 { label: "check:RUN3", root: 400 },
200 ],
201 self: 1,
202 }),
203 );
204 tokens = {
205 alice: createToken(app.db, alice.id, "t", "admin").token,
206 bob: createToken(app.db, bob.id, "t", "rw").token,
207 muse: createToken(app.db, muse.id, "t", "ro").token,
208 grok: createToken(app.db, grok.id, "t", "ro").token,
209 };
210 expect(setTelemetryGrant(app, alice, "muse", true).ok).toBe(true);
211 });
212
213 afterAll(async () => {
214 useLive(createLive());
215 servers.stop();
216 app.close();
217 await rm(dataDir, { recursive: true, force: true });
218 rmSync(host, { recursive: true, force: true });
219 });
220
221 test("an agent without the grant, a member and nobody are refused", async () => {
222 for (const path of ["/api/v1/telemetry/system", "/api/v1/telemetry/flow", "/api/v1/telemetry/sources"]) {
223 expect((await get(tokens["grok"], path)).status).toBe(403);
224 expect((await get(tokens["bob"], path)).status).toBe(403);
225 expect((await get(undefined, path)).status).toBe(401);
226 }
227 expect((await tool(tokens["grok"]!, "system")).error).toBe(true);
228 expect((await tool(tokens["grok"]!, "flow")).error).toBe(true);
229 const bobCookie = await login("bob");
230 expect((await fetch(`${servers.url}/system`, { headers: { cookie: bobCookie } })).status).toBe(404);
231 });
232
233 test("a granted agent reads /system and /flow, with private repositories cut out", async () => {
234 const sys = await get(tokens["muse"], "/api/v1/telemetry/system");
235 expect(sys.status).toBe(200);
236 const text = await sys.text();
237 expect(text).not.toContain("RUN1");
238 expect(text).not.toContain("hunter2");
239 // A test outside any cell lands in `forge`; its file name is not an agent's to read.
240 expect(text).not.toContain("private-thing");
241 const v = JSON.parse(text) as { groups: { label: string; kind: string; top: unknown[]; procs: number }[]; duplicates: { file: string; checks: string[] }[] };
242 const hidden = v.groups.find((g) => g.label === "check:private-1");
243 expect(hidden?.procs).toBe(2);
244 expect(hidden?.top).toEqual([]);
245 expect(v.groups.some((g) => g.label === "check:RUN2")).toBe(true);
246 expect(v.duplicates).toEqual([{ file: "(private)", checks: ["check:RUN2", "check:private-1"] }]);
247
248 const flow = await get(tokens["muse"], "/api/v1/telemetry/flow");
249 expect(flow.status).toBe(200);
250 const f = (await flow.json()) as { says: string; stages: unknown[]; duplicateWork: unknown[] };
251 expect(f.stages.length).toBeGreaterThan(0);
252 expect(f.duplicateWork.length).toBe(1);
253 // A conflicted private pull is not named to an agent, in the list or in the sentence.
254 const flowText = JSON.stringify(f);
255 expect(flowText).toContain("alice/open-garden#2");
256 expect(flowText).not.toContain("vault-of-secrets");
257 const viaMcp = await tool(tokens["muse"]!, "flow");
258 expect(viaMcp.text).toContain("alice/open-garden#2");
259 expect(viaMcp.text).not.toContain("vault-of-secrets");
260 const history = await get(tokens["muse"], "/api/v1/telemetry/history");
261 expect(history.status).toBe(200);
262 expect(Array.isArray(((await history.json()) as { points: unknown[] }).points)).toBe(true);
263 expect((await get(tokens["grok"], "/api/v1/telemetry/history")).status).toBe(403);
264 const sources = (await (await get(tokens["muse"], "/api/v1/telemetry/sources")).json()) as { sources: { name: string }[] };
265 expect(sources.sources.map((s) => s.name)).toContain("machine");
266 expect((await tool(tokens["muse"]!, "system")).error).toBe(false);
267 expect((await tool(tokens["muse"]!, "flow")).error).toBe(false);
268 });
269
270 test("a private repository's merge-queue line reaches the owner, never an agent", async () => {
271 const priv = app.db
272 .query<{ id: string }, []>("SELECT id FROM repo WHERE name = 'vault-of-secrets'")
273 .get()!;
274 app.db
275 .query("INSERT INTO issue (id, repo_id, number, kind, title, author_id) VALUES ('PULLQ', ?1, 9, 'pull', 'q', ?2)")
276 .run(priv.id, alice.id);
277 app.db
278 .query(
279 "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) SELECT 'PULLQ', default_branch, 'q', 'a', 'b', strftime('%Y-%m-%dT%H:%M:%fZ','now') FROM repo WHERE id = ?1",
280 )
281 .run(priv.id);
282 type Flow = { queue: { repo: string }[] };
283 const names = (f: Flow) => f.queue.map((q) => q.repo);
284 const mcp = async (t: string) => JSON.parse((await tool(t, "flow")).text) as Flow;
285 const api = async (t: string) => (await (await get(t, "/api/v1/telemetry/flow")).json()) as Flow;
286
287 expect(names(await api(tokens["alice"]!))).toContain("alice/vault-of-secrets");
288 expect(names(await mcp(tokens["alice"]!))).toContain("alice/vault-of-secrets");
289 expect(names(await api(tokens["muse"]!))).not.toContain("alice/vault-of-secrets");
290 expect(names(await mcp(tokens["muse"]!))).not.toContain("alice/vault-of-secrets");
291 });
292
293 test("the owner sees everything; every agent read is logged; revoking closes the door", async () => {
294 expect(await (await get(tokens["alice"], "/api/v1/telemetry/flow")).text()).toContain("vault-of-secrets");
295 expect(await (await get(tokens["alice"], "/api/v1/telemetry/system")).text()).toContain("private-thing");
296 const own = await (await get(tokens["alice"], "/api/v1/telemetry/system")).text();
297 expect(own).toContain("check:RUN1");
298 const page = await fetch(`${servers.url}/system`, { headers: { cookie: await login("alice") } });
299 expect(page.status).toBe(200);
300 const html = await page.text();
301 expect(html).toContain('id="duplicates" data-count="1"');
302 expect(html).toContain("test/shared.test.ts");
303 const flowPage = await fetch(`${servers.url}/flow`, { headers: { cookie: await login("alice") } });
304 expect(await flowPage.text()).toContain('id="duplicate-work" data-count="1"');
305
306 const reads = recentReads(app.db).filter((r) => r.username === "muse");
307 expect(reads.map((r) => r.what)).toContain("system");
308 expect(reads.map((r) => r.what)).toContain("flow");
309 expect(recentReads(app.db).some((r) => r.username === "alice")).toBe(false);
310
311 // The grant is the owner's alone — not an agent's own, not a member's.
312 expect(setTelemetryGrant(app, muse, "grokbot", true)).toEqual({ ok: false, error: "not_found" });
313 expect(setTelemetryGrant(app, bob, "grokbot", true)).toEqual({ ok: false, error: "not_found" });
314 expect(setTelemetryGrant(app, alice, "bob", true)).toEqual({ ok: false, error: "not_an_agent" });
315
316 const cookie = await login("alice");
317 const revoke = await fetch(`${servers.url}/settings/telemetry`, {
318 method: "POST",
319 redirect: "manual",
320 headers: { cookie, "content-type": "application/x-www-form-urlencoded", origin: servers.url },
321 body: new URLSearchParams({ agent: "muse", on: "0" }),
322 });
323 expect(revoke.status).toBe(303);
324 expect((await get(tokens["muse"], "/api/v1/telemetry/system")).status).toBe(403);
325 // A cross-origin grant is refused.
326 const forged = await fetch(`${servers.url}/settings/telemetry`, {
327 method: "POST",
328 redirect: "manual",
329 headers: { cookie, "content-type": "application/x-www-form-urlencoded", origin: "https://evil.example" },
330 body: new URLSearchParams({ agent: "grokbot", on: "1" }),
331 });
332 expect(forged.status).not.toBe(303);
333 expect((await get(tokens["grok"], "/api/v1/telemetry/system")).status).toBe(403);
334 const settings = await fetch(`${servers.url}/settings/telemetry`, { headers: { cookie } });
335 expect(settings.status).toBe(200);
336 });
337
338 test("the funnel is open to anyone and names the bottleneck, the welcoming issues and the gate", async () => {
339 const res = await get(undefined, "/api/v1/contribute");
340 expect(res.status).toBe(200);
341 const w = (await res.json()) as { bottleneck: { says: string }; propose: string[]; issues: unknown[] };
342 expect(w.bottleneck.says.length).toBeGreaterThan(0);
343 expect(w.propose.join(" ")).toContain("bun test");
344 const html = await (await get(undefined, "/contribute")).text();
345 expect(html).toContain("How to propose");
346 expect((await tool(tokens["grok"]!, "contribute")).error).toBe(false);
347 });
348
349 test("in the terminal, the owner is offered System and Telemetry; nobody else is", () => {
350 const run = (user: User, id: string) => {
351 const pushed: Screen[] = [];
352 const toasts: string[] = [];
353 const ctx = { push: (x: Screen) => pushed.push(x), pop: () => {}, toast: (t: string) => toasts.push(t) } as unknown as Ctx;
354 const session = new Session(app, { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user });
355 return { cmd: forgeCommands(session)(ctx).find((c) => c.id === id), pushed, toasts };
356 };
357 for (const u of [bob, muse]) {
358 expect(run(u, "system").cmd).toBeUndefined();
359 expect(run(u, "telemetry").cmd).toBeUndefined();
360 }
361 const sys = run(alice, "system");
362 sys.cmd!.run();
363 expect(sys.pushed.map((x) => x.title)).toEqual(["System"]);
364 expect(systemLines(JSON.parse(JSON.stringify(live().read()))).join("\n")).toContain("1 test file is running in more than one check");
365
366 expect(flowLines(app.db, new Date(), JSON.parse(JSON.stringify(live().read().duplicates))).join("\n")).toContain(
367 "1 test file is running in more than one check",
368 );
369 expect(flowLines(app.db, new Date(), []).join("\n")).toContain("No test file is running in more than one check.");
370
371 logRead(app.db, grok.id, "system");
372 const tel = run(alice, "telemetry");
373 tel.cmd!.run();
374 expect(tel.pushed.map((x) => x.title)).toEqual(["Telemetry: telemetry:read"]);
375 expect(hasGrant(app.db, grok.id)).toBe(false);
376 // Enter on the list's first row (grokbot, alphabetically before muse) flips its grant.
377 tel.pushed[0]!.key({ key: "enter" } as never);
378 expect(hasGrant(app.db, grok.id)).toBe(true);
379 expect(tel.toasts.join()).toContain("granted");
380 });
381 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs