Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pm-stack/projector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tasks.json> [--window-hours 24] [--consumer queue-view] [--no-ack]`
prints one read-only JSON view joining fleet queue task records (JSON
Expand All @@ -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:

Expand Down
12 changes: 11 additions & 1 deletion pm-stack/projector/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// bun run src/cli.ts reconcile
// bun run src/cli.ts queue-view --tasks <tasks.json> [--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
Expand All @@ -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<string, string | undefined>;
Expand Down Expand Up @@ -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
Expand All @@ -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}`);
Expand Down
152 changes: 152 additions & 0 deletions pm-stack/projector/src/queue-view-html.ts
Original file line number Diff line number Diff line change
@@ -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("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#39;");
}

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
? `<ul class="hot">${e.hot_events
.map(
(h) =>
`<li>#${h.seq} ${dash(h.event)}${h.action ? `/${dash(h.action)}` : ""}` +
(h.actor ? ` by ${dash(h.actor)}` : "") +
(h.source_url ? ` — <a href="${escapeHtml(h.source_url)}">source</a>` : "") +
`</li>`,
)
.join("")}</ul>`
: "";
return (
`<td>${e.task_id}</td><td>${dash(e.title)}${hot}</td><td>${dash(e.ref)}</td>` +
`<td>${dash(e.kind)}</td><td>${dash(e.priority)}</td><td>${dash(e.created_at)}</td>`
);
}

/**
* 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]) => `<tr><td>${escapeHtml(status)}</td><td>${n}</td></tr>`)
.join("\n ");

const queued = view.queued
.map((e, i) => ` <tr><td>${i + 1}</td>${entryCells(e)}</tr>`)
.join("\n");

const claimed = view.claimed
.map(
(e: ClaimedEntry) =>
` <tr>${entryCells(e)}<td>${dash(e.claimed_at)}</td>` +
`<td>${dash(e.claimed_by.worker_id)}</td><td>${dash(e.claimed_by.lane_id)}</td>` +
`<td>${dash(e.claimed_by.platform)}</td></tr>`,
)
.join("\n");

const trends = view.links
.map(
(l) =>
` <section class="trend-panel" data-store="${escapeHtml(l.store)}">\n` +
` <h3>${escapeHtml(l.label)}</h3>\n` +
` <p>Store: ${escapeHtml(l.store)} — <a href="${escapeHtml(l.ref)}">${escapeHtml(l.ref)}</a></p>\n` +
` </section>`,
)
.join("\n");

return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Queue/PM view — ${escapeHtml(view.window.since)} to ${escapeHtml(view.window.until)}</title>
<style>
body { font-family: system-ui, sans-serif; margin: 2rem; }
table { border-collapse: collapse; margin-bottom: 1.5rem; }
th, td { border: 1px solid #ccc; padding: 0.25rem 0.6rem; text-align: left; vertical-align: top; }
.trend-panel { border: 1px solid #ccc; border-radius: 0.5rem; padding: 0.5rem 1rem; margin-bottom: 1rem; }
ul.hot { margin: 0.25rem 0 0; padding-left: 1.2rem; }
</style>
</head>
<body>
<header>
<h1>Integrated queue/PM view</h1>
<p>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.</p>
</header>

<section id="counts">
<h2>Status counts</h2>
<table>
<thead><tr><th>Status</th><th>Tasks</th></tr></thead>
<tbody>
${counts}
</tbody>
</table>
</section>

<section id="queued">
<h2>Queued — in claim order (${view.queued.length})</h2>
<table>
<thead><tr><th>#</th><th>Task</th><th>Title</th><th>Ref</th><th>Kind</th><th>Priority</th><th>Created</th></tr></thead>
<tbody>
${queued}
</tbody>
</table>
</section>

<section id="claimed">
<h2>Picked up in window (${view.claimed.length})</h2>
<table>
<thead><tr><th>Task</th><th>Title</th><th>Ref</th><th>Kind</th><th>Priority</th><th>Created</th><th>Claimed at</th><th>Worker (system)</th><th>Lane</th><th>Platform</th></tr></thead>
<tbody>
${claimed}
</tbody>
</table>
</section>

<section id="trends">
<h2>Trend panels — linked stores (not copies)</h2>
${trends}
</section>

<section id="hot-list">
<h2>Hot list (slice-2 event stream)</h2>
<p>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}.</p>
</section>

</body>
</html>
`;
}
169 changes: 169 additions & 0 deletions pm-stack/projector/test/queue-view-html.test.ts
Original file line number Diff line number Diff line change
@@ -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>): 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>): 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(`<td>${status}</td><td>${n}</td>`);
}
});

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(`<a href="x">&'</a>`)).toBe(
"&lt;a href=&quot;x&quot;&gt;&amp;&#39;&lt;/a&gt;",
);
});

test("task titles are data, not markup: a script-shaped title renders escaped", () => {
const evil = buildQueueView(
[task({ id: 99, title: "<script>alert(1)</script>" })],
[],
{ since: SINCE, until: UNTIL, now: UNTIL },
);
const page = renderQueueViewHtml(evil);
expect(page).not.toContain("<script>alert(1)</script>");
expect(page).toContain("&lt;script&gt;alert(1)&lt;/script&gt;");
});
});
Loading