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

The System screen shows each forge's snapshot stream lag, worded to pass the quiet-page rules #17053

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16978-the-system-screen-shows-each-forge-s-sna → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/16978-the-system-screen-shows-each-forge-s-sna was merged into main as be89dac.

Review

Sign in to review this change.
6 files changed +111 −2
docs/features/hosted-stream.md +1 −1
@@ -13,7 +13,7 @@ The nightly snapshot keeps each hosted forge once a day ([hosted-backup.md](host
1313
1414 ## Where it is read
1515
16 `streamLag(dataDir)` in `src/snapshot/stream.ts` answers each forge's last whole pass, last failure and lag. No page shows it yet.
16 `streamLag(dataDir)` in `src/snapshot/stream.ts` answers each forge's last whole pass, last failure and lag. /system's *Snapshot stream* section and its terminal screen (`src/tui/screens/system.ts`, `streamLines`) say it per hosted forge in `src/snapshot/stream_words.ts`'s words: how long ago the last whole pass finished and the last failure; a forge with no pass yet says the first is due within five minutes. Nothing is drawn when no forge is hosted.
1717
1818 ## Restore a forge from the stream
1919
scripts/unused-exports-allow.json −1
@@ -175,7 +175,6 @@
175175 "src/web/studio/home_cache.ts setHomeClock",
176176 "src/web/studio/home_last.ts forgetHome",
177177 "src/web/studio/review_evidence.ts rememberStat",
178 "src/snapshot/stream.ts streamLag",
179178 "src/web/sw.ts swSource",
180179 "src/spawn/machines.ts forgetMachines",
181180 "src/spawn/machines.ts registerMachine",
src/snapshot/stream_words.ts added +36
@@ -0,0 +1,36 @@
1 /**
2 * The stream's lag in words (#16978), one sentence per hosted forge, for /system and its terminal
3 * screen alike. A forge with no whole pass yet says when the first is due, never a bare "No …".
4 * Plain data in (`streamLag`'s), plain data out.
5 */
6
7 import { agoWords } from "../kernel/ago.ts";
8 import { STREAM_EVERY_MS, type StreamLag } from "./stream.ts";
9
10 export interface StreamSentence {
11 readonly name: string;
12 /** What is said after the name. */
13 readonly text: string;
14 /** `${name}: ${text}`. */
15 readonly sentence: string;
16 /** Three passes behind, or none yet after the first was due. */
17 readonly late: boolean;
18 }
19
20 const minutes = (ms: number): string => `${Math.round(ms / 60_000)} minutes`;
21
22 const said = (name: string, late: boolean, text: string): StreamSentence => ({ name, late, text, sentence: `${name}: ${text}` });
23
24 /** What one forge's stream says at `now`. */
25 export function streamSentence(l: StreamLag, now: number = Date.now()): StreamSentence {
26 const failed = l.error === undefined ? "" : ` Last failure: ${l.error}`;
27 if (l.at === undefined) {
28 return said(l.name, l.error !== undefined, `first pass is due within ${minutes(STREAM_EVERY_MS)} of the forge starting, then every ${minutes(STREAM_EVERY_MS)}.${failed}`);
29 }
30 return said(l.name, l.late, `last whole pass ${agoWords(l.at, now)}.${failed}`);
31 }
32
33 /** Every streamed forge's sentence, by name; none when no forge is hosted. */
34 export function streamSentences(lags: readonly StreamLag[], now: number = Date.now()): StreamSentence[] {
35 return lags.map((l) => streamSentence(l, now));
36 }
src/tui/screens/system.ts +9
@@ -67,6 +67,8 @@ import { readHealth } from "../../actions/health.ts";
6767 import { egressReportedLine } from "../../system/egress_line.ts";
6868 import { restoreLine } from "../../snapshot/restore_line.ts";
6969 import { lastDrill, lastPass } from "../../snapshot/drills.ts";
70 import { streamLag } from "../../snapshot/stream.ts";
71 import { streamSentences } from "../../snapshot/stream_words.ts";
7072 import { featureHealth } from "../../system/feature_health.ts";
7173 import { readerPaces } from "../../system/telemetry_reads.ts";
7274 import { live as liveMachine } from "../../system/live.ts";
@@ -659,6 +661,12 @@ export function restoreLines(session: Session, now = Date.now()): string[] {
659661 return [restoreLine(lastPass(session.db), lastDrill(session.db), now).sentence];
660662 }
661663
664 /** Each hosted forge's snapshot stream lag, as /system's Snapshot stream says it (#16978). */
665 export function streamLines(dataDir: string, now = Date.now()): string[] {
666 const rows = streamSentences(streamLag(dataDir, now), now);
667 return rows.length === 0 ? [] : ["Snapshot stream", ...rows.map((r) => `${r.late ? "late " : ""}${r.sentence}`)];
668 }
669
662670 /** The machine, as /system says it, and which agents may read it: the owner's, a person (#1789). */
663671 export const entry: PaletteEntry = {
664672 rank: 100,
@@ -678,6 +686,7 @@ export const entry: PaletteEntry = {
678686 ...forgeLines(readHealth(session.app, session.user)),
679687 ...egressLines(session),
680688 ...restoreLines(session),
689 ...streamLines(session.app.config.dataDir),
681690 ...(who.length === 0 ? [] : ["", ...who]),
682691 ...engineLines(session.app.config.engine),
683692 ...(self.length === 0 ? [] : ["", ...self]),
src/web/pages/system.ts +10
@@ -66,6 +66,8 @@ import { BREAK_STEPS, MACHINES, declaredFooting, footing, offMachine, whereWords
6666 import { laneWords, readOffsiteLane, type OffsiteLane } from "../../snapshot/offsite_lane.ts";
6767 import { restoreLine, type RestoreLine } from "../../snapshot/restore_line.ts";
6868 import { lastDrill, lastPass } from "../../snapshot/drills.ts";
69 import { streamLag } from "../../snapshot/stream.ts";
70 import { streamSentences, type StreamSentence } from "../../snapshot/stream_words.ts";
6971 import { stalePullsSection } from "./maintenance.ts";
7072 import { shapeWords } from "../../engine/mode.ts";
7173 import { badge, btn, chartBox, disclosure, meter, pill, row, time } from "../ui.ts";
@@ -560,6 +562,13 @@ export function restoreSection(l: RestoreLine): Html {
560562 <p>${l.sentence}</p></section>`;
561563 }
562564
565 /** Each hosted forge's snapshot stream lag (#16978); nothing drawn when no forge is hosted. */
566 export function streamSection(rows: readonly StreamSentence[]): Html {
567 if (rows.length === 0) return html``;
568 return html`<section class="settings-block" id="sys-stream"><h2>Snapshot stream</h2>
569 <dl class="status-rows" id="sys-stream-facts">${rows.map((r) => fact(r.late ? `${r.name} (late)` : r.name, r.text))}</dl></section>`;
outdated

Principle 1 and clean code: r.sentence.slice(r.name.length + 2) depends on the exact ': ' join in stream_words.ts. Return the text after the name as its own field from streamSentence and use it here.

570 }
571
563572 /** The last hour's stalls by route, most milliseconds first; the rows are `stallHour`'s. */
564573 export function stallSection(rows: readonly RouteStall[], worst: Html = html``): Html {
565574 return html`<section class="settings-block"><h2>Main-thread stalls, last hour</h2>${
@@ -831,6 +840,7 @@ export async function systemPage(ctx: Ctx, patterns: () => readonly string[]): P
831840 ${footingSection(standsOn)}
832841 ${restoreSection(timePhase(ctx.request, "restore", () => restoreLine(lastPass(ctx.app.db), lastDrill(ctx.app.db), Date.now())))}
833842 ${offsiteSection(timePhase(ctx.request, "offsite", () => readOffsiteLane(ctx.app.config.dataDir, ctx.app.db)))}
843 ${streamSection(timePhase(ctx.request, "stream", () => streamSentences(streamLag(ctx.app.config.dataDir))))}
834844 ${featuresSection(timePhase(ctx.request, "features", () => featureHealth(ctx.app, reader)))}
835845 ${lagSection(liveOf(ctx.app).lagMinutes.readings())}
836846 <div id="sys-stalls">${stallSection(stalls, worstStallsTable(worst, nowMono, Date.now()))}</div>
test/system-stream-lag.test.ts added +55
@@ -0,0 +1,55 @@
1 /** /system and its terminal screen say each hosted forge's snapshot stream lag (#16978). */
2 import { expect, test } from "bun:test";
3 import { mkdtempSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { noteStream, streamLag, STREAM_LATE_MS } from "../src/snapshot/stream.ts";
7 import { streamSentences } from "../src/snapshot/stream_words.ts";
8 import { streamSection } from "../src/web/pages/system.ts";
9 import { streamLines } from "../src/tui/screens/system.ts";
10
11 const now = Date.parse("2026-12-01T12:00:00Z");
12 const ago = (ms: number): string => new Date(now - ms).toISOString();
13 const dir = (): string => mkdtempSync(join(tmpdir(), "forge-streamlag-"));
14
15 test("a lagging forge and a fresh one are told apart, with the last failure", () => {
16 const d = dir();
17 noteStream(d, [
18 { name: "fresh", at: ago(2 * 60_000), bytes: 10 },
19 { name: "lagging", at: ago(STREAM_LATE_MS + 60 * 60_000), error: "bucket refused the put" },
20 ]);
21 const rows = streamSentences(streamLag(d, now), now);
22 expect(rows.find((r) => r.name === "fresh")).toMatchObject({ late: false, sentence: "fresh: last whole pass 2 minutes ago." });
23 expect(rows.find((r) => r.name === "lagging")).toMatchObject({
24 late: true,
25 sentence: "lagging: last whole pass 1 hour ago. Last failure: bucket refused the put",
26 });
27 const out = String(streamSection(rows));
28 expect(out).toContain("lagging (late)");
29 expect(out).toContain("last whole pass 2 minutes ago");
30 expect(rows.find((r) => r.name === "lagging")!.text).toBe("last whole pass 1 hour ago. Last failure: bucket refused the put");
31 expect(streamLines(d, now)).toEqual(["Snapshot stream", ...rows.map((r) => `${r.late ? "late " : ""}${r.sentence}`)]);
32 });
33
34 test("a forge with no pass yet says when the first is due, not a bare No", () => {
35 const d = dir();
36 noteStream(d, [{ name: "new" }]);
37 const [r] = streamSentences(streamLag(d, now), now);
38 expect(r!.sentence).toContain("first pass is due within 5 minutes");
39 expect(r!.sentence).not.toMatch(/^No\b/);
40 expect(r!.late).toBe(false);
41 });
42
43 test("no hosted forge draws nothing", () => {
44 const d = dir();
45 expect(String(streamSection(streamSentences(streamLag(d, now), now)))).toBe("");
46 expect(streamLines(d, now)).toEqual([]);
47 });
48
49 test("a failure carrying markup is escaped on the page", () => {
50 const d = dir();
51 noteStream(d, [{ name: "odd", at: ago(60_000), error: `<img src=x onerror=alert(1)> ${"long ".repeat(200)}` }]);
52 const out = String(streamSection(streamSentences(streamLag(d, now), now)));
53 expect(out).not.toContain("<img");
54 expect(out).toContain("&lt;img");
55 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs