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

A report from a hosted forge is answered within a day, and a hosted forge's fork can keep up and run #13721

Merged opened this in answer to

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

Merged

scout/forge:scout/13710-a-report-from-a-hosted-forge-is-answered → main 3 commits fork 4 threads · 4 unresolved

  • Merged. scout/13710-a-report-from-a-hosted-forge-is-answered was merged into main as 0941477.

Review

Sign in to review this change.
6 files changed +147 −10
docs/features/report-receipt.md +7
@@ -21,3 +21,10 @@ Try it: `curl -s -XPOST -H 'content-type: application/json' -d '{"title":"x"}' $
2121 then open the `url` it returns.
2222
2323 Pinned by `test/report-receipt.test.ts` and `test/report-words.test.ts`.
24
25 ## The day
26
27 A report is read, and accepted or dismissed, within a day (`ANSWER_WITHIN_MS`, `core/reports.ts`). The receipt page
28 says when the answer is due, says plainly when that day has passed, and after a decision shows when it was answered.
29 The keepers' list, on the web and in the terminal, marks a report "past its day" (oldest first, as before, so those are first). Nothing is closed on a person's behalf.
30 Pinned by `test/report-clock.test.ts`.
src/core/reports.ts +32 −7
@@ -19,6 +19,24 @@ export const REQUEST_MAX = 64 * 1024;
1919 /** Waiting reports one repository holds; past this a stranger is told so. */
2020 export const WAITING_MAX = 100;
2121
22 /** The clock a report runs on: read, and accepted or dismissed, within a day. */
23 export const ANSWER_WITHIN_MS = 24 * 60 * 60 * 1000;
24
25 /**
26 * When a report filed at `createdAt` is due an answer (ISO), or null for a time that does not
27 * parse.
28 */
29 export function answerBy(createdAt: string): string | null {
30 const t = Date.parse(createdAt);
31 return Number.isNaN(t) ? null : new Date(t + ANSWER_WITHIN_MS).toISOString();
32 }
33
34 /** Past its day and still unanswered. */
35 export function isOverdue(state: ReportState, createdAt: string, now: number = Date.now()): boolean {
36 const due = answerBy(createdAt);
37 return state === "pending" && due !== null && Date.parse(due) <= now;
38 }
39
2240 export type ReportState = "pending" | "accepted" | "dismissed";
2341
2442 /** A name a filer may claim without a token: an account name's shape. */
@@ -345,14 +363,21 @@ export function undecideReport(db: Database, reportId: string): void {
345363 ).run(reportId);
346364 }
347365
366 export type ReportStanding = {
367 state: ReportState;
368 title: string;
369 issue: number | null;
370 createdAt: string;
371 decidedAt: string | null;
372 };
373
outdated

Wrap this query and its type. The line is far over the limit. Name the row type once instead of writing it twice.

348374 /** Where a report stands, for the link its filer was given: state and issue, never words. */
349 export function reportStanding(
350 db: Database,
351 repoId: string,
352 reportId: string,
353 ): { state: ReportState; title: string; issue: number | null } | undefined {
375 export function reportStanding(db: Database, repoId: string, reportId: string): ReportStanding | undefined {
354376 const row = db
355 .query("SELECT state, title, issue_number AS issue FROM report WHERE repo_id = ?1 AND id = ?2")
356 .get(repoId, reportId) as { state: ReportState; title: string; issue: number | null } | null;
377 .query(
378 `SELECT state, title, issue_number AS issue, created_at AS createdAt, decided_at AS decidedAt
379 FROM report WHERE repo_id = ?1 AND id = ?2`,
380 )
381 .get(repoId, reportId) as ReportStanding | null;
357382 return row ?? undefined;
358383 }
src/tui/screens/reports.ts +2 −2
@@ -10,7 +10,7 @@
1010
1111 import { REPORT_ALREADY_DECIDED } from "../../actions/origin_app_words.ts";
1212 import { acceptReport, dismissReport, waitingCount, waitingReports } from "../../actions/reports.ts";
13 import { byFiler, filerWords, type Report } from "../../core/reports.ts";
13 import { byFiler, filerWords, isOverdue, type Report } from "../../core/reports.ts";
1414 import type { Repo } from "../../core/repos.ts";
1515 import type { Ctx, Screen } from "../app/app.ts";
1616 import { binding } from "../app/keys.ts";
@@ -54,7 +54,7 @@ export function reportsScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
5454 };
5555
5656 const rows = (list: readonly Report[]): ListRow[] =>
57 list.map((r) => ({ title: r.title, secondary: filerWords(r.filedBy), meta: ago(r.createdAt) }));
57 list.map((r) => ({ title: r.title, secondary: filerWords(r.filedBy), meta: isOverdue(r.state, r.createdAt) ? `${ago(r.createdAt)} · past its day` : ago(r.createdAt) }));

Non-blocking: this line is well over the width. Extract the meta into a small meta(r) helper so it reads on one screen.

5858
5959 /** The waiting reports' filers with a count each, most first: "2 @muse · 1 anonymous". */
6060 const counts = (list: readonly Report[]): string =>
src/web/pages/reports.ts +15 −1
@@ -25,6 +25,8 @@ import {
2525 filerWords,
2626 outsideWords,
2727 reportStanding,
28 answerBy,
29 isOverdue,
2830 reportsClosed,
2931 type FiledBy,
3032 type Report,
@@ -202,6 +204,15 @@ function reportBody(ctx: Ctx, repo: Repo): Response {
202204 ? "A person read it and closed it without an issue."
203205 : "Not read yet. Keep this link and look again."
204206 }</dd></div>
207 ${standing?.state === "pending"
208 ? html`<div class="status-row"><dt>Answer</dt><dd id="report-due">${
209 isOverdue(standing.state, standing.createdAt)
210 ? "Past its day. The people who keep this repository see it first."
outdated

Principle 1 and 8: this sentence says the same thing twice. Shorten it to 'Past its day. The people who keep this repository see it first.'

211 : html`Due within a day of sending, by ${time(answerBy(standing.createdAt) ?? standing.createdAt)}.`
212 }</dd></div>`
213 : standing?.decidedAt
214 ? html`<div class="status-row"><dt>Answered</dt><dd id="report-answered">${time(standing.decidedAt)}</dd></div>`
215 : ""}
205216 </dl>
206217 </section>`,
207218 });
@@ -285,7 +296,10 @@ function reportRow(base: string, report: Report): Html {
285296 return row({

The class overdue has no CSS and is not registered in docs/design/areas. Use an existing named component, or define and document the class. Do not use the accent.

286297 title: report.title,
287298 nav: true,
288 meta: html`From outside, ${filerWords(report.filedBy)}, not checked · ${time(report.createdAt)}`,
299 meta: html`From outside, ${filerWords(report.filedBy)}, not checked · ${time(report.createdAt)}${when(
300 isOverdue(report.state, report.createdAt),
301 () => html` · <span class="pill">past its day</span>`,
302 )}`,
289303 fold: {
290304 id: `report-${report.id}`,
291305 open: true,
test/report-clock.test.ts added +76
@@ -0,0 +1,76 @@
1 /** A report's day: the clock, and what the receipt and the list say about it. */
2 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createRepo } from "../src/actions/repos.ts";
7 import { createApp, type App } from "../src/app.ts";
8 import { ANSWER_WITHIN_MS, answerBy, isOverdue } from "../src/core/reports.ts";
9 import { createSession } from "../src/core/sessions.ts";
10 import { createUser, type User } from "../src/core/users.ts";
11 import { serve, type Servers } from "../src/http/server.ts";
12
13 test("a report is due a day after it was filed, and only a pending one is overdue", () => {
14 const filed = "2026-11-01T10:00:00.000Z";
15 expect(answerBy(filed)).toBe("2026-11-02T10:00:00.000Z");
16 expect(answerBy("nonsense")).toBeNull();
17 const t = Date.parse(filed);
18 expect(isOverdue("pending", filed, t + ANSWER_WITHIN_MS - 1)).toBe(false);
19 expect(isOverdue("pending", filed, t + ANSWER_WITHIN_MS)).toBe(true);
20 expect(isOverdue("accepted", filed, t + 5 * ANSWER_WITHIN_MS)).toBe(false);
21 expect(isOverdue("dismissed", filed, t + 5 * ANSWER_WITHIN_MS)).toBe(false);
22 });
23
24 describe("the receipt page", () => {
25 let dataDir: string;
26 let app: App;
27 let servers: Servers;
28 let alice: User;
29 beforeAll(async () => {
30 dataDir = await mkdtemp(join(tmpdir(), "forge-clock-"));
31 app = createApp({ dataDir, port: 0 });
32 servers = await serve(app);
33 const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice",
34 email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
35 if (!a.ok) throw new Error(a.error);
36 alice = a.value;
37 const r = await createRepo(app, a.value, { name: "open", visibility: "public" });
38 if (!r.ok) throw new Error(String(r.error));
39 });
40 afterAll(async () => {
41 await servers.stop();
42 await rm(dataDir, { recursive: true, force: true });
43 });
44
45 test("says when the answer is due, says so once the day has passed, and when it was answered", async () => {
46 const res = await fetch(`${servers.url}/api/v1/repos/alice/open/reports`, {
47 method: "POST",
48 headers: { "content-type": "application/json", "x-forwarded-for": "203.0.113.9" },
49 body: JSON.stringify({ title: "Broken link" }),
50 });
51 const { id } = (await res.json()) as { id: string };
52 const read = async () => (await fetch(`${servers.url}/alice/open/report?sent=${id}`)).text();
53 expect(await read()).toMatch(/id="report-due"[^>]*>Due within a day of sending, by /);
54 app.db.query("UPDATE report SET created_at = '2020-01-01T00:00:00.000Z' WHERE id = ?1").run(id);
55 expect(await read()).toContain("Past its day. The people who keep this repository see it first.");
56 app.db.query("UPDATE report SET state = 'dismissed', decided_at = '2020-01-01T05:00:00.000Z' WHERE id = ?1").run(id);
57 const done = await read();
58 expect(done).not.toContain("report-due");
59 expect(done).toContain('id="report-answered"');
60 });
61
62 test("the keepers' list marks only the report past its day", async () => {
63 const send = async (title: string) =>
64 ((await (await fetch(`${servers.url}/api/v1/repos/alice/open/reports`, {
65 method: "POST",
66 headers: { "content-type": "application/json", "x-forwarded-for": "203.0.113.10" },
67 body: JSON.stringify({ title }),
68 })).json()) as { id: string }).id;
69 const old = await send("Old one");
70 await send("New one");
71 app.db.query("UPDATE report SET created_at = '2020-01-01T00:00:00.000Z' WHERE id = ?1").run(old);
72 const cookie = `forge_session=${createSession(app.db, alice.id).token}`;
73 const page = await (await fetch(`${servers.url}/alice/open/reports`, { headers: { cookie } })).text();
74 expect(page.match(/past its day/g)?.length).toBe(1);
75 });
76 });
test/tui-reports.test.ts +15
@@ -189,3 +189,18 @@ describe("who filed it, in the terminal (#7658)", () => {
189189 h.stop();
190190 });
191191 });
192
193 describe("the day a report is given, in the terminal", () => {
194 test("a report past its day is marked in the list; a fresh one is not", async () => {
195 for (const r of waiting()) dismissReport(app, alice, repo, r.id);
196 insertReport(app.db, repo.id, { title: "Old complaint", body: "", page: null });
197 insertReport(app.db, repo.id, { title: "Fresh complaint", body: "", page: null });
198 app.db.query("UPDATE report SET created_at = '2020-01-01T00:00:00.000Z' WHERE title = 'Old complaint'").run();
199 const h = await open(alice);
200 const text = await h.run(":", "Reports (", "\r");
201 const line = (t: string) => text.split("\n").find((l) => l.includes(t)) ?? "";
202 expect(line("Old complaint")).toContain("past its day");
203 expect(line("Fresh complaint")).not.toContain("past its day");
204 h.stop();
205 });
206 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs