Public: anyone can read this; the crew can change it.
The sentinel files a generic finding, with no name or number, for a repository that is not public #14972
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14909-the-sentinel-files-a-generic-finding-wit → main 7 commits fork 3 threads · 3 unresolved
5 files
changed +216
−11
docs/features/sentinel-withheld.md added +12
| @@ -0,0 +1,12 @@ | ||
| 1 | --- | |
| 2 | topic: When something is wrong | |
| 3 | row: Names the sentinel left out | |
| 4 | --- | |
| 5 | # Names the sentinel left out (#14909) | |
| 6 | ||
| 7 | The sentinel files its findings in the forge's own repository, which others read. When a finding is about a repository that is not public, the issue names that repository by its id only: no slug, no run summary, no issue numbers. | |
| 8 | ||
| 9 | - **Web**: a forge admin's `/system` has a section **Names the sentinel left out**, one row per open sentinel issue that withheld a name: the repository's name (a link), its id, and the issue. | |
| 10 | - **Terminal**: the palette's **System** screen shows the same list under the same heading, one line each: issue, name, id. | |
| 11 | ||
| 12 | Nothing shows when no open issue withheld a name. The rows are read by `withheldFindings` (`src/crew/sentinel.ts`); tested in `test/sentinel-withheld.test.ts`. | |
src/crew/sentinel.ts +48 −11
| @@ -335,21 +335,56 @@ function repoNamed(db: Database, repoId: string, ownId: string | undefined): str | ||
| 335 | 335 | |
| 336 | 336 | /** |
| 337 | 337 | * What a `stuck:` or `budget:` span says in place of its sentence and link when its run's |
| 338 | * repository is not `shownHere`. | |
| 338 | * repository is not `shownHere`: the repository by its id alone (#14909), so an admin can match | |
| 339 | * it on /system (`withheldFindings`). | |
| 339 | 340 | */ |
| 340 | const VEILED_SENTENCE = "A run in a private repository; its details are on /system."; | |
| 341 | const veiledSentence = (repoId: string): string => | |
| 342 | `A run in a private repository (${repoId}); its details are on /system.`; | |
| 341 | 343 | const VEILED_HREF = "/system"; |
| 342 | 344 | |
| 343 | 345 | /** |
| 344 | * The run a `stuck:<run>` or `budget:<run>` cause names, when its repository | |
| 345 | * is not `shownHere`: then its sentence (the run's summary), its link (the | |
| 346 | * repository's slug) and its log stay out of a finding. | |
| 346 | * The repository of the run a `stuck:<run>` or `budget:<run>` cause names, | |
| 347 | * when that repository is not `shownHere`: then its sentence (the run's | |
| 348 | * summary), its link (the repository's slug) and its log stay out of a finding. | |
| 347 | 349 | */ |
| 348 | function veiledRun(db: Database, cause: string, ownId: string | undefined): boolean { | |
| 350 | function veiledRun(db: Database, cause: string, ownId: string | undefined): string | undefined { | |
| 349 | 351 | const id = /^(?:stuck|budget):(.+)$/.exec(cause)?.[1]; |
| 350 | if (id === undefined) return false; | |
| 352 | if (id === undefined) return undefined; | |
| 351 | 353 | const run = db.query<{ repo_id: string }, [string]>("SELECT repo_id FROM run WHERE id = ?1").get(id); |
| 352 | return run !== null && !shownHere(db, run.repo_id, ownId); | |
| 354 | return run !== null && !shownHere(db, run.repo_id, ownId) ? run.repo_id : undefined; | |
| 355 | } | |
| 356 | ||
| 357 | /** A sentinel issue that names a repository by its id only, and the name it left out. */ | |
| 358 | export interface WithheldFinding { | |
| 359 | readonly key: string; | |
| 360 | readonly issue: number; | |
| 361 | readonly repoId: string; | |
| 362 | readonly slug: string; | |
| 363 | } | |
| 364 | ||
| 365 | /** | |
| 366 | * Open sentinel findings that withheld a repository's name, each with the name: the admin-only | |
| 367 | * /system page and the terminal's System screen list them, so the owner keeps what the public | |
| 368 | * issue leaves out (#14909). | |
| 369 | */ | |
| 370 | export function withheldFindings(db: Database, forgeRepoId: string | undefined = undefined): WithheldFinding[] { | |
| 371 | const ownId = forgeRepoId ?? ownRepoId(db); | |
| 372 | const rows = db | |
| 373 | .query<{ key: string; number: number }, []>( | |
| 374 | `SELECT t.key, i.number FROM sentinel_finding t JOIN issue i ON i.id = t.issue_id | |
| 375 | WHERE i.state = 'open' ORDER BY i.number`, | |
| 376 | ) | |
| 377 | .all(); | |
| 378 | const out: WithheldFinding[] = []; | |
| 379 | for (const r of rows) { | |
| 380 | const direct = /^(?:stops|parks|push|gate):(.+)$/.exec(r.key)?.[1]; | |
| 381 | const run = /^degraded:((?:stuck|budget)-.+)$/.exec(r.key)?.[1]; | |
| 382 | const repoId = | |
| 383 | direct ?? (run === undefined ? undefined : veiledRun(db, run.replace(/^(stuck|budget)-/, "$1:"), ownId)); | |
| 384 | if (repoId === undefined || shownHere(db, repoId, ownId)) continue; | |
| 385 | out.push({ key: r.key, issue: r.number, repoId, slug: repoSlug(db, repoId) }); | |
| 386 | } | |
| 387 | return out; | |
| 353 | 388 | } |
| 354 | 389 | |
| 355 | 390 | interface SummaryRow { |
| @@ -495,7 +530,8 @@ export function degradedCauses( | ||
| 495 | 530 | const ownId = forgeRepoId ?? ownRepoId(db); |
| 496 | 531 | for (const span of spans(db, from, now)) { |
| 497 | 532 | if (!ownCause(span.cause, forgeRepoId)) continue; |
| 498 | const s = veiledRun(db, span.cause, ownId) ? { ...span, sentence: VEILED_SENTENCE, href: VEILED_HREF } : span; | |
| 533 | const hid = veiledRun(db, span.cause, ownId); | |
| 534 | const s = hid !== undefined ? { ...span, sentence: veiledSentence(hid), href: VEILED_HREF } : span; | |
| 499 | 535 | const start = Math.max(s.openedAt, from); |
| 500 | 536 | const held = Math.min(s.closedAt ?? now, now) - start; |
| 501 | 537 | const seen = byCause.get(s.cause); |
| @@ -589,8 +625,9 @@ export function budgetBreaches(db: Database, now: number, forgeRepoId?: string, | ||
| 589 | 625 | const ownId = forgeRepoId ?? ownRepoId(db); |
| 590 | 626 | for (const span of spans(db, now - DAY_MS, now)) { |
| 591 | 627 | if (span.closedAt !== null || !BREACH_CAUSE.test(span.cause) || !ownCause(span.cause, forgeRepoId)) continue; |
| 592 | const veiled = veiledRun(db, span.cause, ownId); | |
| 593 | const s = veiled ? { ...span, sentence: VEILED_SENTENCE, href: VEILED_HREF } : span; | |
| 628 | const hid = veiledRun(db, span.cause, ownId); | |
| 629 | const veiled = hid !== undefined; | |
| 630 | const s = veiled ? { ...span, sentence: veiledSentence(hid), href: VEILED_HREF } : span; | |
| 594 | 631 | out.push({ |
| 595 | 632 | key: `degraded:${keyPart(s.cause)}`, |
| 596 | 633 | title: `${s.cause} is past its budget: ${clip(s.sentence, 120)}`, |
src/tui/screens/system.ts +12
| @@ -65,6 +65,7 @@ import { live as liveMachine } from "../../system/live.ts"; | ||
| 65 | 65 | import { childDirOf, listChildrenNow } from "../../actions/spawn.ts"; |
| 66 | 66 | import { adoptionsOf } from "../../spawn/cell.ts"; |
| 67 | 67 | import type { AdoptionRow } from "../../spawn/adopted.ts"; |
| 68 | import { withheldFindings, type WithheldFinding } from "../../crew/sentinel.ts"; | |
| 68 | 69 | import { childrenUse, realSelf, selfUse, sizeWords, useWords, type ChildUseRow, type SelfUse } from "../../spawn/usage.ts"; |
| 69 | 70 | |
| 70 | 71 | const WIDTH = 72; |
| @@ -514,6 +515,15 @@ export function adoptedLines(rows: readonly AdoptionRow[], inner = WIDTH - 6): s | ||
| 514 | 515 | return ["Child forges across the restart", ...rows.map((r) => truncate(`${pad(r.name, 16)} ${r.says}`, inner))]; |
| 515 | 516 | } |
| 516 | 517 | |
| 518 | /** | |
| 519 | * Each open sentinel issue that named a private repository by its id only, with the name, as | |
| 520 | * /system's "Names the sentinel left out" says it (#14909). | |
| 521 | */ | |
| 522 | export function withheldLines(rows: readonly WithheldFinding[], inner = WIDTH - 6): string[] { | |
| 523 | if (rows.length === 0) return []; | |
| 524 | return ["Names the sentinel left out", ...rows.map((r) => truncate(`#${pad(String(r.issue), 7)} ${pad(r.slug, 24)} ${r.repoId}`, inner))]; | |
| 525 | } | |
| 526 | ||
| 517 | 527 | /** The egress fault, one line, as /system says it; nothing when egress is enforced. */ |
| 518 | 528 | export function egressLines(session: Session): string[] { |
| 519 | 529 | const said = egressReportedLine(session.db); |
| @@ -537,6 +547,7 @@ export const entry: PaletteEntry = { | ||
| 537 | 547 | const self = selfLines(selfUse(realSelf(session.app.config.dataDir))); |
| 538 | 548 | const kids = childLines(childrenUse(listChildrenNow(session.app), (bodyId) => childDirOf(session.app, bodyId))); |
| 539 | 549 | const left = adoptedLines(adoptionsOf(session.app)); |
| 550 | const hid = withheldLines(withheldFindings(session.db)); | |
| 540 | 551 | const who = askingLines(session); |
| 541 | 552 | ctx.push(systemScreen(ctx, [ |
| 542 | 553 | ...forgeLines(readHealth(session.app, session.user)), |
| @@ -547,6 +558,7 @@ export const entry: PaletteEntry = { | ||
| 547 | 558 | ...(self.length === 0 ? [] : ["", ...self]), |
| 548 | 559 | ...(kids.length === 0 ? [] : ["", ...kids]), |
| 549 | 560 | ...(left.length === 0 ? [] : ["", ...left]), |
| 561 | ...(hid.length === 0 ? [] : ["", ...hid]), | |
| 550 | 562 | "", |
| 551 | 563 | ...systemLines(liveMachine().read()), |
| 552 | 564 | ], session)); |
src/web/pages/system.ts +20
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | * against its size (#10853, `spawn/usage.ts`). |
| 23 | 23 | */ |
| 24 | 24 | import { askingOf, type Asker } from "../../perf/asking.ts"; |
| 25 | import { sentinelRepoPath, withheldFindings, type WithheldFinding } from "../../crew/sentinel.ts"; | |
| 25 | 26 | import { turnStatsOf } from "../../http/app_turn.ts"; |
| 26 | 27 | import { mayReadRefusals } from "../../core/first_ask_refusals.ts"; |
| 27 | 28 | import { outsideDependencies } from "../../keeper/at_boot.ts"; |
| @@ -237,6 +238,24 @@ export function adoptedSection(rows: readonly AdoptionRow[]): Html { | ||
| 237 | 238 | )}</ul></section>`; |
| 238 | 239 | } |
| 239 | 240 | |
| 241 | /** | |
| 242 | * The sentinel's open issues that left a private repository's name out (#14909), each with the | |
| 243 | * name: the issue is filed where others read it, this page is a forge admin's. Nothing when none. | |
| 244 | */ | |
| 245 | export function withheldSection(rows: readonly WithheldFinding[], repoPath: string): Html { | |
| 246 | if (rows.length === 0) return html``; | |
Principle 1: the h2 "Names the sentinel left out" already says it; the |
||
| 247 | return html`<section class="settings-block" id="sys-withheld"><h2>Names the sentinel left out</h2> | |
| 248 | <ul class="list" aria-label="Each sentinel issue that withheld a repository's name">${rows.map((r) => | |
| 249 | row({ | |
| 250 | title: r.slug, | |
| 251 | href: `/${r.slug}`, | |
| 252 | titleAttr: r.slug, | |
| 253 | meta: html`<span class="mono small">${r.repoId}</span> <a href="/${repoPath}/issues/${String(r.issue)}">#${String(r.issue)}</a>`, | |
| 254 | data: { withheld: r.key }, | |
| 255 | }), | |
| 256 | )}</ul></section>`; | |
| 257 | } | |
| 258 | ||
| 240 | 259 | /** |
| 241 | 260 | * The forges that run inside this process and what each took of its one serving thread in the last |
| 242 | 261 | * hour (`perf/child_time.ts`): nothing when none is open here and none was. A forge in a cell is |
| @@ -708,6 +727,7 @@ export async function systemPage(ctx: Ctx): Promise<Response> { | ||
| 708 | 727 | ${engineSection(ctx.app.config.engine)} |
| 709 | 728 | ${childrenSection(children)} |
| 710 | 729 | ${adoptedSection(owner ? adoptionsOf(ctx.app) : [])} |
| 730 | ${owner ? withheldSection(timePhase(ctx.request, "withheld", () => withheldFindings(ctx.app.db)), sentinelRepoPath()) : html``} | |
| 711 | 731 | ${inProcessSection(inProcess)} |
| 712 | 732 | <section class="settings-block"><h2>Disks and network</h2> |
| 713 | 733 | <ul class="list" id="sys-disks">${at("sys-disks")}</ul></section> |
test/sentinel-withheld.test.ts added +124
| @@ -0,0 +1,124 @@ | ||
| 1 | /** | |
| 2 | * A sentinel issue names a private repository by its opaque id alone (#14768, | |
| 3 | * sentinel-private.test.ts); the admin-only /system page and the terminal's System screen keep | |
| 4 | * the name it left out, with the issue (#14909). | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { addCrew, createAgentRow } from "../src/core/agents.ts"; | |
| 14 | import { listIssues } from "../src/core/issues.ts"; | |
| 15 | import type { Repo } from "../src/core/repos.ts"; | |
| 16 | import { createRun } from "../src/core/runs.ts"; | |
| 17 | import { createUser } from "../src/core/users.ts"; | |
| 18 | import { budgetBreaches, hunt, withheldFindings } from "../src/crew/sentinel.ts"; | |
| 19 | import { withheldSection } from "../src/web/pages/system.ts"; | |
| 20 | import { withheldLines } from "../src/tui/screens/system.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | const NOW = Date.now(); | |
| 25 | const SUMMARY = "git push: operation not permitted on branch secret-plans in /work/hush"; | |
| 26 | ||
| 27 | let dir: string; | |
| 28 | let app: App; | |
| 29 | let own: Repo; | |
| 30 | let hush: Repo; | |
| 31 | let open: Repo; | |
| 32 | const kept = process.env.FORGE_SENTINEL_REPO; | |
| 33 | ||
| 34 | function failedPushes(repo: Repo): string[] { | |
| 35 | return [0, 1, 2].map((n) => { | |
| 36 | const run = createRun(app.db, { repoId: repo.id, kind: "agent" }); | |
| 37 | app.db | |
| 38 | .query("UPDATE run SET state = 'failed', summary = ?2, created_at = ?3, finished_at = ?3 WHERE id = ?1") | |
| 39 | .run(run.id, SUMMARY, new Date(NOW - (n + 1) * 60_000).toISOString()); | |
| 40 | return run.id; | |
| 41 | }); | |
| 42 | } | |
| 43 | ||
| 44 | beforeAll(async () => { | |
| 45 | process.env.FORGE_SENTINEL_REPO = "alice/forge"; | |
| 46 | dir = await mkdtemp(join(tmpdir(), "forge-sentinel-private-")); | |
| 47 | app = createApp({ dataDir: dir, port: 0 }); | |
| 48 | const a = await createUser(app.db, { | |
| 49 | origin: "seed", | |
| 50 | trusted: true, | |
| 51 | username: "alice", | |
| 52 | email: "[email protected]", | |
| 53 | password: "a-good-passphrase", | |
| 54 | adminIfFirst: true, | |
| 55 | }); | |
| 56 | if (!a.ok) throw new Error(a.error); | |
| 57 | const repo = async (name: string, visibility: "public" | "private"): Promise<Repo> => { | |
| 58 | const made = await createRepo(app, a.value, { name, visibility }); | |
| 59 | if (!made.ok) throw new Error(String(made.error)); | |
| 60 | return made.value; | |
| 61 | }; | |
| 62 | own = await repo("forge", "public"); | |
| 63 | hush = await repo("hush", "private"); | |
| 64 | open = await repo("shown", "public"); | |
| 65 | const s = await createUser(app.db, { origin: "seed", username: "sentinel", email: "[email protected]", agent: true }); | |
| 66 | if (!s.ok) throw new Error("agent"); | |
| 67 | createAgentRow(app.db, { userId: s.value.id, brief: "hunt", createdBy: a.value.id }); | |
| 68 | addCrew(app.db, own.id, s.value.id, a.value.id); | |
| 69 | }); | |
| 70 | ||
| 71 | afterAll(async () => { | |
| 72 | if (kept === undefined) delete process.env.FORGE_SENTINEL_REPO; | |
| 73 | else process.env.FORGE_SENTINEL_REPO = kept; | |
| 74 | app.db.close(); | |
| 75 | await rm(dir, { recursive: true, force: true }); | |
| 76 | }); | |
| 77 | ||
| 78 | describe("a run past its budget in a private repository", () => { | |
| 79 | test("names the repository by id, without its summary or its link", () => { | |
| 80 | const run = createRun(app.db, { repoId: hush.id, kind: "agent" }); | |
| 81 | app.db.query("UPDATE run SET state = 'failed', summary = ?2 WHERE id = ?1").run(run.id, SUMMARY); | |
| 82 | app.db | |
| 83 | .query("INSERT INTO degraded_span (cause, sentence, href, opened_at) VALUES (?1, ?2, ?3, ?4)") | |
| 84 | .run(`budget:${run.id}`, `A run was killed twice: ${SUMMARY}.`, `/alice/hush/runs/${run.id}`, NOW - 60_000); | |
| 85 | const f = budgetBreaches(app.db, NOW, own.id).find((x) => x.cause === `budget:${run.id}`); | |
| 86 | expect(f).toBeDefined(); | |
| 87 | const text = [f!.title, ...f!.evidence, f!.seen?.where ?? ""].join("\n"); | |
| 88 | expect(text).not.toContain("alice/hush"); | |
| 89 | expect(text).not.toContain("secret-plans"); | |
| 90 | expect(text).toContain(hush.id); | |
| 91 | }); | |
| 92 | }); | |
| 93 | ||
| 94 | describe("the name withheld from the issue", () => { | |
| 95 | test("is filed nowhere but the admin-only /system section", () => { | |
| 96 | failedPushes(hush); | |
| 97 | failedPushes(open); | |
| 98 | hunt(app, "alice/forge", NOW); | |
| 99 | const filed = listIssues(app.db, own.id, { state: "open" }); | |
| 100 | const pushIssue = filed.find((i) => i.title.includes(hush.id) && i.title.includes("push")); | |
| 101 | expect(pushIssue).toBeDefined(); | |
| 102 | for (const i of filed) expect(`${i.title}\n${i.body}`).not.toContain("alice/hush"); | |
| 103 | const rows = withheldFindings(app.db, own.id); | |
| 104 | expect(rows.find((r) => r.key === `push:${hush.id}`)).toEqual({ | |
| 105 | key: `push:${hush.id}`, | |
| 106 | issue: pushIssue!.number, | |
| 107 | repoId: hush.id, | |
| 108 | slug: "alice/hush", | |
| 109 | }); | |
| 110 | expect(rows.some((r) => r.repoId === open.id)).toBe(false); | |
| 111 | const html = String(withheldSection(rows, "alice/forge")); | |
| 112 | expect(html).toContain('href="/alice/hush"'); | |
| 113 | expect(html).toContain(`/alice/forge/issues/${pushIssue!.number}`); | |
| 114 | const said = withheldLines(rows).join("\n"); | |
| 115 | expect(said).toContain("Names the sentinel left out"); | |
| 116 | expect(said).toContain("alice/hush"); | |
| 117 | expect(said).toContain(`#${pushIssue!.number}`); | |
| 118 | }); | |
| 119 | ||
| 120 | test("an empty list draws nothing", () => { | |
| 121 | expect(String(withheldSection([], "alice/forge"))).toBe(""); | |
| 122 | expect(withheldLines([])).toEqual([]); | |
| 123 | }); | |
| 124 | }); |
new above
offline
Wrong pointer: the tests are in
test/sentinel-withheld.test.ts;test/sentinel-private.test.tsis main's file and does not coverwithheldFindings. Fix: name the right file.