diff --git a/pm-stack/projector/README.md b/pm-stack/projector/README.md index f3f9ccb8ee..6d2e00f922 100644 --- a/pm-stack/projector/README.md +++ b/pm-stack/projector/README.md @@ -110,7 +110,7 @@ local source you don't commit. | `/hot/checkpoints` | POST | bearer | ack a checkpoint `{consumer, cursor, highWater}` | | `/reconcile` | POST | bearer | trigger a reconciliation poll | -### Integrated queue/PM view — machine face (#7078, #2072 slice 3) +### Integrated queue/PM view — machine + human faces (#7078, #2072 slice 3) `bun run src/cli.ts queue-view --tasks [--window-hours 24] [--consumer queue-view] [--no-ack]` prints one read-only JSON view joining fleet queue task records (JSON @@ -122,7 +122,15 @@ order, items claimed inside the window with their claiming system/lane, snapshot counts by status (which reconcile with `fleet queue health` when fed the full task list), and links to the existing aggregator/projector trend stores. It writes nothing and -collects nothing new; the human face is a later slice-3 piece. +collects nothing new. + +`--format html` on the same command prints the human face: the same +view rendered as one standalone HTML page/report (status counts, +queued in claim order, picked-up items with their claiming +system/lane, hot-list summary, and trend panels that link — never +copy — the same stores the machine face links). The read model is +identical for both faces; only the rendering differs +(`src/queue-view-html.ts`). **Subjects** are external keys: diff --git a/pm-stack/projector/src/cli.ts b/pm-stack/projector/src/cli.ts index e5a7045ad5..da79ea02b4 100644 --- a/pm-stack/projector/src/cli.ts +++ b/pm-stack/projector/src/cli.ts @@ -2,6 +2,7 @@ // bun run src/cli.ts reconcile // bun run src/cli.ts queue-view --tasks [--window-hours 24] // [--consumer queue-view] [--no-ack] +// [--format json|html] // // queue-view (#7078, #2072 slice 3 first piece) is the read-only // integrated queue/PM view, machine face: it joins fleet queue task @@ -13,6 +14,7 @@ import { readFileSync } from "node:fs"; import { openDb, migrate } from "./store"; import { buildQueueView, consumeHotList, defaultTrendLinks, type QueueTaskRecord } from "./queue-view"; +import { renderQueueViewHtml } from "./queue-view-html"; const cmd = process.argv[2] ?? "reconcile"; const env = process.env as Record; @@ -44,6 +46,11 @@ if (cmd === "reconcile") { console.error("queue-view: tasks file must be a JSON array or { tasks: [...] }"); process.exit(2); } + const format = arg("format") ?? "json"; + if (format !== "json" && format !== "html") { + console.error(`queue-view: --format must be json or html, got ${format}`); + process.exit(2); + } const windowHours = Number(arg("window-hours") ?? "24"); const consumer = arg("consumer") ?? "queue-view"; // Consume BEFORE capturing the window end: consumeHotList pins the @@ -65,7 +72,10 @@ if (cmd === "reconcile") { highWater: consumed.highWater, links: defaultTrendLinks(env), }); - console.log(JSON.stringify(view, null, 2)); + // One read model, two faces: --format json is the machine face + // (unchanged shape); --format html is the human face (#7078 second + // piece), the same view rendered as a page/report. + console.log(format === "html" ? renderQueueViewHtml(view) : JSON.stringify(view, null, 2)); if (!process.argv.includes("--no-ack")) { const cursor = await consumed.ack(); console.error(`[projector] queue-view: consumer ${consumer} checkpoint acked at ${cursor}`); diff --git a/pm-stack/projector/src/queue-view-html.ts b/pm-stack/projector/src/queue-view-html.ts new file mode 100644 index 0000000000..e9b319030a --- /dev/null +++ b/pm-stack/projector/src/queue-view-html.ts @@ -0,0 +1,152 @@ +// Integrated queue/PM view — human face (#7078, #2072 slice 3, second piece). +// +// The second face over the SAME read model as the machine face +// (src/queue-view.ts): `buildQueueView` output, rendered as one +// standalone HTML page/report. One read model, two faces — this module +// adds no join, no ingestion, and no write path: it renders exactly +// what the machine face emits (queued in claim order, claimed with the +// claiming system/lane, snapshot counts, hot-list consumption summary) +// and its trend panels LINK the existing aggregator/projector stores +// through the view's own `links` — never copies of their data. +// +// Pure: the same QueueView always renders the same page (modulo +// nothing at all — even `generated_at` comes from the view), so the +// rendering is unit-testable without a database or a live broker. +// Every interpolated value is HTML-escaped: titles and refs are +// GitHub/broker data, not markup. + +import type { ClaimedEntry, QueuedEntry, QueueView } from "./queue-view"; + +/** Escape a value for HTML text/attribute context. */ +export function escapeHtml(value: string): string { + return value + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """) + .replaceAll("'", "'"); +} + +const dash = (v: string | number | null | undefined): string => + v === null || v === undefined || v === "" ? "—" : escapeHtml(String(v)); + +function entryCells(e: QueuedEntry): string { + const hot = e.hot_events.length + ? `
    ${e.hot_events + .map( + (h) => + `
  • #${h.seq} ${dash(h.event)}${h.action ? `/${dash(h.action)}` : ""}` + + (h.actor ? ` by ${dash(h.actor)}` : "") + + (h.source_url ? ` — source` : "") + + `
  • `, + ) + .join("")}
` + : ""; + return ( + `${e.task_id}${dash(e.title)}${hot}${dash(e.ref)}` + + `${dash(e.kind)}${dash(e.priority)}${dash(e.created_at)}` + ); +} + +/** + * Render a QueueView as a standalone HTML page. Sections, in order: + * header (generated_at + window), status counts, queued (in the + * view's claim order — the renderer never re-sorts), picked up + * (claimed, with claiming system/lane), trend panels (one per link + * the machine face emitted, href = the link's ref verbatim), and the + * hot-list consumption summary. + */ +export function renderQueueViewHtml(view: QueueView): string { + const counts = Object.entries(view.counts) + .sort(([a], [b]) => a.localeCompare(b)) + .map(([status, n]) => `${escapeHtml(status)}${n}`) + .join("\n "); + + const queued = view.queued + .map((e, i) => ` ${i + 1}${entryCells(e)}`) + .join("\n"); + + const claimed = view.claimed + .map( + (e: ClaimedEntry) => + ` ${entryCells(e)}${dash(e.claimed_at)}` + + `${dash(e.claimed_by.worker_id)}${dash(e.claimed_by.lane_id)}` + + `${dash(e.claimed_by.platform)}`, + ) + .join("\n"); + + const trends = view.links + .map( + (l) => + `
\n` + + `

${escapeHtml(l.label)}

\n` + + `

Store: ${escapeHtml(l.store)} — ${escapeHtml(l.ref)}

\n` + + `
`, + ) + .join("\n"); + + return ` + + + +Queue/PM view — ${escapeHtml(view.window.since)} to ${escapeHtml(view.window.until)} + + + +
+

Integrated queue/PM view

+

Generated at ${escapeHtml(view.generated_at)} — window ${escapeHtml(view.window.since)} to ${escapeHtml(view.window.until)}. + Human face over the queue-view read model (#7078): the same join the machine face emits as JSON, rendered. It writes nothing and collects nothing new.

+
+ +
+

Status counts

+ + + + ${counts} + +
StatusTasks
+
+ +
+

Queued — in claim order (${view.queued.length})

+ + + +${queued} + +
#TaskTitleRefKindPriorityCreated
+
+ +
+

Picked up in window (${view.claimed.length})

+ + + +${claimed} + +
TaskTitleRefKindPriorityCreatedClaimed atWorker (system)LanePlatform
+
+ + + +
+

Hot list (slice-2 event stream)

+

Consumer ${dash(view.hot_list.consumer)} — consumed ${view.hot_list.events_consumed} event(s) + from seq ${view.hot_list.from_seq} to high-water ${view.hot_list.high_water}.

+
+ + + +`; +} diff --git a/pm-stack/projector/test/queue-view-html.test.ts b/pm-stack/projector/test/queue-view-html.test.ts new file mode 100644 index 0000000000..a34f531d86 --- /dev/null +++ b/pm-stack/projector/test/queue-view-html.test.ts @@ -0,0 +1,169 @@ +// Integrated queue/PM view — human face (#7078, #2072 slice 3 second +// piece): the rendering of the SAME read model the machine face emits. +// Oracles: the QueueView fixture itself — the page must show the +// view's counts verbatim, list queued entries in the view's order +// (the broker's claim order, computed by buildQueueView), attribute +// claimed entries to the claiming system/lane on the record, and its +// trend panels must link exactly the refs the machine face emitted +// (defaultTrendLinks) — links, never copies. +import { describe, test, expect } from "bun:test"; +import { buildQueueView, defaultTrendLinks, type QueueTaskRecord } from "../src/queue-view"; +import { escapeHtml, renderQueueViewHtml } from "../src/queue-view-html"; +import type { HotEventRow } from "../src/store"; + +const SINCE = "2026-11-10T00:00:00Z"; +const UNTIL = "2026-11-11T00:00:00Z"; + +const task = (over: Partial): QueueTaskRecord => ({ + id: 1, + queue: "repo-health-code-pilot", + title: "task", + status: "queued", + priority: 30, + created_at: "2026-11-09T00:00:00Z", + claimed_at: null, + worker_id: null, + lane_id: null, + platform: null, + dedupe_key: null, + payload: { repo: "developing-today/code", kind: "small_fix", issue: { number: 100 } }, + ...over, +}); + +const hotRow = (over: Partial): HotEventRow => ({ + seq: 1, + event_id: "d1:issue:100", + delivery_id: "d1", + repo: "developing-today/code", + event: "issues", + action: "opened", + ref: "developing-today/code#100", + actor: "dezren39", + source_url: "https://github.com/developing-today/code/issues/100", + source_timestamp: "2026-11-10T01:00:00Z", + received_at: "2026-11-10T01:00:00Z", + ...over, +}); + +const tasks = [ + task({ id: 11, title: "later in claim order", priority: 20, created_at: "2026-11-09T02:00:00Z" }), + task({ + id: 12, + title: "first in claim order", + priority: 10, + created_at: "2026-11-09T01:00:00Z", + payload: { repo: "developing-today/code", kind: "triage", issue: { number: 200 } }, + }), + task({ + id: 13, + title: "claimed work", + status: "claimed", + claimed_at: "2026-11-10T05:00:00Z", + worker_id: "repo-worker-code", + lane_id: "code-d", + platform: "muse", + payload: { repo: "developing-today/code", kind: "small_fix", issue: { number: 300 } }, + }), + task({ id: 15, title: "finished work", status: "done", payload: { repo: "developing-today/code", kind: "small_fix", issue: { number: 500 } } }), +]; +const events = [hotRow({ seq: 7, ref: "developing-today/code#100" })]; + +const view = buildQueueView(tasks, events, { + since: SINCE, + until: UNTIL, + now: UNTIL, + consumer: "queue-view", + fromSeq: 0, + highWater: 7, + links: defaultTrendLinks({}), +}); +const html = renderQueueViewHtml(view); + +describe("queue-view human face: sections and counts", () => { + test("page carries every section of the read model", () => { + for (const marker of [ + "Status counts", + "Queued — in claim order", + "Picked up in window", + "Trend panels", + "Hot list", + ]) { + expect(html).toContain(marker); + } + expect(html).toContain(SINCE); + expect(html).toContain(UNTIL); + }); + + test("counts shown are the view's counts, verbatim", () => { + expect(view.counts).toEqual({ queued: 2, claimed: 1, done: 1 }); + for (const [status, n] of Object.entries(view.counts)) { + expect(html).toContain(`${status}${n}`); + } + }); + + test("hot-list summary names the consumer and consumption range", () => { + expect(html).toContain("queue-view"); + expect(html).toContain("consumed 1 event(s)"); + expect(html).toContain("high-water 7"); + }); +}); + +describe("queue-view human face: order and attribution", () => { + test("queued entries render in the view's claim order, never re-sorted", () => { + expect(view.queued.map((q) => q.task_id)).toEqual([12, 11]); + const first = html.indexOf("first in claim order"); + const later = html.indexOf("later in claim order"); + expect(first).toBeGreaterThan(0); + expect(later).toBeGreaterThan(first); + }); + + test("claimed entries name the claiming system/lane/platform from the record", () => { + expect(html).toContain("claimed work"); + expect(html).toContain("repo-worker-code"); + expect(html).toContain("code-d"); + expect(html).toContain("muse"); + expect(html).toContain("2026-11-10T05:00:00Z"); + }); + + test("joined hot events render under their entry with a source link", () => { + expect(html).toContain("issues/opened"); + expect(html).toContain('href="https://github.com/developing-today/code/issues/100"'); + }); +}); + +describe("queue-view human face: trend panels link the machine face's stores", () => { + test("every trend link the machine face emits appears as a panel href, verbatim", () => { + expect(view.links.length).toBeGreaterThanOrEqual(4); + for (const link of view.links) { + expect(html).toContain(`href="${link.ref}"`); + expect(html).toContain(escapeHtml(link.label)); + expect(html).toContain(`data-store="${link.store}"`); + } + }); + + test("panels cover usage, quotas, and scheduled jobs (the spec's trend set)", () => { + const labels = view.links.map((l) => l.label).join(" "); + expect(labels).toContain("Usage & billing trends"); + expect(labels).toContain("Plan quota snapshots"); + expect(labels).toContain("Scheduled-job activity stream"); + }); +}); + +describe("queue-view human face: escaping", () => { + test("escapeHtml escapes markup-significant characters", () => { + expect(escapeHtml(`&'`)).toBe( + "<a href="x">&'</a>", + ); + }); + + test("task titles are data, not markup: a script-shaped title renders escaped", () => { + const evil = buildQueueView( + [task({ id: 99, title: "" })], + [], + { since: SINCE, until: UNTIL, now: UNTIL }, + ); + const page = renderQueueViewHtml(evil); + expect(page).not.toContain(""); + expect(page).toContain("<script>alert(1)</script>"); + }); +});