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

Revert "An outsider's receipt says what happens next and when the owner last acted, then keeps a status link" #16660

Merged opened this

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

Merged

revert-16647 → main 0 commits

  • Merged. revert-16647 was merged into main as b4e2341.

Review

Sign in to review this change.
4 files changed +21 −136
docs/features/report-status.md −7
@@ -38,10 +38,3 @@ The answer to a report or an offer (`…/offers`, the MCP `offer` tool) also car
3838 - **Where the owner finds an offer's link (#15988):** on that pull's page, one line with Copy, and on the terminal's pull screen as a `status link:` row. Both ask `offerStatusLinkFor` (`src/core/outsider_status.ts`): writers only, because the link is a capability, and only on a pull an offer opened. A pull the owner opened, or a reader or guest looking, shows no such line. Pinned by `test/outsider-status.test.ts` (page) and `test/pull-next-tui.test.ts` (terminal).
3939
4040 Pinned by `test/report-status.test.ts`.
41
42 ## What the receipt says next (#16308)
43
44 The status page and JSON also carry `next` (two sentences: what happens next and where to look) and
45 `ownerActed`: when someone who can write to the repository last merged, reviewed, commented or
46 decided a report there, as a relative time ("3 days ago"), or null. Never a date, name or count.
47 The JSON adds `statusPage` and `statusJson`; the page prints both links.
src/core/outsider_status.ts +16 −75
@@ -12,12 +12,11 @@
1212 * says so.
1313 */
1414 import type { Database } from "bun:sqlite";
15 import { agoWords } from "../kernel/ago.ts";
1615 import { getIssueById } from "./issues.ts";
1716 import { getPull } from "./pulls.ts";
1817 import { listReviews } from "./reviews.ts";
19 import { canWrite, getRepoById, type Repo } from "./repos.ts";
20 import { getUserById, type User } from "./users.ts";
18 import { canWrite, type Repo } from "./repos.ts";
19 import type { User } from "./users.ts";
2120
2221 export type OutsiderState = "received" | "read" | "planned" | "landed" | "declined";
2322 export type OutsiderKind = "report" | "offer";
@@ -42,57 +41,6 @@ export interface OutsiderStatus {
4241 readonly updated: string | null;
4342 /** After this the token answers 404. */
4443 readonly expires: string;
45 /** What happens next, in two sentences. */
46 readonly next: string;
47 /**
48 * When someone who can merge last acted in the repository, as a relative time ("3 days ago"),
49 * or null when no one has: never a date, a name or a count, so it says nothing of who they are.
50 */
51 readonly ownerActed: string | null;
52 }
53
54 /** What happens next from each state: two sentences, the second always saying where to look. */
55 export const OUTSIDER_NEXT_WORDS: Readonly<Record<OutsiderState, string>> = {
56 received:
57 "A person who maintains this repository reads it next, and nothing runs or lands before they decide. Keep this link: it shows the decision here when there is one.",
58 read:
59 "A person who maintains this repository has read it and decides whether it lands. Keep this link: it shows the decision here when there is one.",
60 planned:
61 "A person took it up; it becomes a change in the repository when someone does the work. Keep this link: it shows here when that is done.",
62 landed: "Nothing more is needed from you. This link keeps showing the result until it expires.",
63 declined: "Nothing more happens to this one. You may file a new one that answers the reason, if you think it still matters.",
64 };
65
66 /**
67 * When a person who can write to `repo` last merged, reviewed, commented or decided a report in
68 * it, or undefined. Only the time leaves: the author is checked, never returned.
69 */
70 export function maintainerActedAt(db: Database, repo: Repo): string | undefined {
71 // A private repository's activity is not for a stranger holding a token.
72 if (repo.visibility !== "public") return undefined;
73 // One row per actor, newest first: bounded by distinct actors, so readers cannot crowd out a
74 // writer.
75 const rows = db
76 .query<{ at: string; uid: string }, [string]>(
77 `SELECT MAX(at) AS at, uid FROM (
78 SELECT p.merged_at AS at, p.merged_by AS uid FROM pull p JOIN issue i ON i.id = p.issue_id WHERE i.repo_id = ?1 AND p.merged_at IS NOT NULL
79 UNION ALL SELECT r.created_at, r.author_id FROM review r JOIN issue i ON i.id = r.issue_id WHERE i.repo_id = ?1
80 UNION ALL SELECT c.created_at, c.author_id FROM comment c JOIN issue i ON i.id = c.issue_id WHERE i.repo_id = ?1
81 UNION ALL SELECT decided_at, decided_by FROM report WHERE repo_id = ?1 AND decided_at IS NOT NULL
82 ) WHERE uid IS NOT NULL GROUP BY uid ORDER BY at DESC`,
83 )
84 .all(repo.id);
85 const writers = new Map<string, boolean>();
86 for (const row of rows) {
87 let may = writers.get(row.uid);
88 if (may === undefined) {
89 const user = getUserById(db, row.uid);
90 may = user !== undefined && canWrite(db, repo, user);
91 writers.set(row.uid, may);
92 }
93 if (may) return row.at;
94 }
95 return undefined;
9644 }
9745
9846 /** 192 random bits, hex: never derived from a number, a clock or the thing's own id. */
@@ -131,7 +79,6 @@ export function pruneOutsiderStatus(db: Database, now = Date.now()): number {
13179 }
13280
13381 interface ReportRow {
134 repo_id: string;
13582 state: "pending" | "accepted" | "dismissed";
13683 decided_at: string | null;
13784 issue_state: string | null;
@@ -139,34 +86,32 @@ interface ReportRow {
13986 closed_at: string | null;
14087 }
14188
142 function ofReport(db: Database, id: string): { state: OutsiderState; updated: string | null; repoId: string } | undefined {
89 function ofReport(db: Database, id: string): { state: OutsiderState; updated: string | null } | undefined {
14390 const row = db
14491 .query<ReportRow, [string]>(
145 `SELECT report.repo_id, report.state, report.decided_at, issue.state AS issue_state, issue.close_reason, issue.closed_at
92 `SELECT report.state, report.decided_at, issue.state AS issue_state, issue.close_reason, issue.closed_at
14693 FROM report LEFT JOIN issue ON issue.repo_id = report.repo_id AND issue.number = report.issue_number
14794 WHERE report.id = ?1`,
14895 )
14996 .get(id);
15097 if (row === null) return undefined;
151 const at = (state: OutsiderState, updated: string | null) => ({ state, updated, repoId: row.repo_id });
152 if (row.state === "pending") return at("received", null);
153 if (row.state === "dismissed") return at("declined", row.decided_at);
154 if (row.issue_state !== "closed") return at("planned", row.decided_at);
155 if (row.close_reason === "dropped") return at("declined", row.closed_at);
156 if (row.close_reason === null || row.close_reason === "done") return at("landed", row.closed_at);
157 return at("planned", row.decided_at);
98 if (row.state === "pending") return { state: "received", updated: null };
99 if (row.state === "dismissed") return { state: "declined", updated: row.decided_at };
100 if (row.issue_state !== "closed") return { state: "planned", updated: row.decided_at };
101 if (row.close_reason === "dropped") return { state: "declined", updated: row.closed_at };
102 if (row.close_reason === null || row.close_reason === "done") return { state: "landed", updated: row.closed_at };
103 return { state: "planned", updated: row.decided_at };
158104 }
159105
160 function ofOffer(db: Database, issueId: string): { state: OutsiderState; updated: string | null; repoId: string | null } {
106 function ofOffer(db: Database, issueId: string): { state: OutsiderState; updated: string | null } {
161107 const issue = getIssueById(db, issueId);
162 const repoId = issue?.repoId ?? null;
163108 const pull = issue === undefined ? undefined : getPull(db, issue.repoId, issue.number);
164 if (pull === undefined) return { state: "declined", updated: issue?.closedAt ?? null, repoId };
165 if (pull.mergedAt !== null) return { state: "landed", updated: pull.mergedAt, repoId };
166 if (pull.state === "closed") return { state: "declined", updated: issue?.closedAt ?? null, repoId };
109 if (pull === undefined) return { state: "declined", updated: issue?.closedAt ?? null };
110 if (pull.mergedAt !== null) return { state: "landed", updated: pull.mergedAt };
111 if (pull.state === "closed") return { state: "declined", updated: issue?.closedAt ?? null };
167112 const reviews = listReviews(db, pull.id);
168 if (reviews.length === 0) return { state: "received", updated: null, repoId };
169 return { state: "read", repoId, updated: reviews.reduce((a, r) => (r.createdAt > a ? r.createdAt : a), "") || null };
113 if (reviews.length === 0) return { state: "received", updated: null };
114 return { state: "read", updated: reviews.reduce((a, r) => (r.createdAt > a ? r.createdAt : a), "") || null };
170115 }
171116
172117 /** The status a token shows now; undefined for a wrong, malformed or expired token. */
@@ -180,8 +125,6 @@ export function readOutsiderStatus(db: Database, token: string, now = Date.now()
180125 if (row === null || Date.parse(row.expires_at) <= now) return undefined;
181126 const got = row.kind === "report" ? ofReport(db, row.ref_id) : ofOffer(db, row.ref_id);
182127 if (got === undefined) return undefined;
183 const repo = got.repoId === null ? undefined : getRepoById(db, got.repoId);
184 const acted = repo === undefined ? undefined : maintainerActedAt(db, repo);
185128 return {
186129 kind: row.kind,
187130 state: got.state,
@@ -189,8 +132,6 @@ export function readOutsiderStatus(db: Database, token: string, now = Date.now()
189132 received: row.created_at,
190133 updated: got.updated,
191134 expires: row.expires_at,
192 next: OUTSIDER_NEXT_WORDS[got.state],
193 ownerActed: acted === undefined ? null : agoWords(acted, now),
194135 };
195136 }
196137
src/http/offer_routes.ts +4 −8
@@ -86,13 +86,11 @@ const escape = escapeHtml;
8686
8787 const day = (iso: string): string => iso.slice(0, 10);
8888
89 function pageOf(s: OutsiderStatus, token: string, origin: string): string {
90 const links = statusLinks(origin, token);
91 const acted = s.ownerActed === null ? "No one who can merge has acted in this repository yet." : `Someone who can merge last acted in this repository ${s.ownerActed}.`;
89 function pageOf(s: OutsiderStatus): string {
9290 const what = s.kind === "report" ? "report" : "offered change";
9391 const rows = [`<li>Received: ${escape(day(s.received))}</li>`];
9492 if (s.updated !== null) rows.push(`<li>${escape(s.state[0]!.toUpperCase() + s.state.slice(1))}: ${escape(day(s.updated))}</li>`);
95 return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>Your ${what}: ${escape(s.state)}</title></head><body><main><h1>Your ${what}: ${escape(s.state)}</h1><p>${escape(s.words)}</p><p id="next">${escape(s.next)}</p><p id="owner-acted">${escape(acted)}</p><ul>${rows.join("")}</ul><p>Status link: <a href="${escape(links.page)}">${escape(links.page)}</a><br>The same facts as JSON: <a href="${escape(links.json)}">${escape(links.json)}</a></p><p>This link works until ${escape(day(s.expires))}. It shows only this state: nothing you wrote.</p></main></body></html>`;
93 return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>Your ${what}: ${escape(s.state)}</title></head><body><main><h1>Your ${what}: ${escape(s.state)}</h1><p>${escape(s.words)}</p><ul>${rows.join("")}</ul><p>This link works until ${escape(day(s.expires))}. It shows only this state: nothing you wrote.</p></main></body></html>`;
9694 }
9795
9896 /**
@@ -119,7 +117,7 @@ export const outsiderStatusPage: Handler = (ctx) => {
119117 if (status === "slow") return slow();
120118 if (status === undefined) return notFoundPage(ctx);
121119 return securityFloor(
122 new Response(pageOf(status, ctx.params.token ?? "", ctx.url.origin), {
120 new Response(pageOf(status), {
123121 headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "referrer-policy": "no-referrer" },
124122 }),
125123 );
@@ -128,7 +126,5 @@ export const outsiderStatusPage: Handler = (ctx) => {
128126 export const outsiderStatusJson: Handler = (ctx) => {
129127 const status = found(ctx);
130128 if (status === "slow") return slow();
131 if (status === undefined) return notFound();
132 const links = statusLinks(ctx.url.origin, ctx.params.token ?? "");
133 return json({ ...status, statusPage: links.page, statusJson: links.json }, 200, { "cache-control": "no-store" });
129 return status === undefined ? notFound() : json(status, 200, { "cache-control": "no-store" });
134130 };
test/outsider-status.test.ts +1 −46
@@ -87,7 +87,7 @@ describe("outsider status link", () => {
8787 const expires = Date.parse(got.body.expires) - Date.parse(got.body.received);
8888 expect(expires).toBe(STATUS_TTL_MS);
8989 expect(JSON.stringify(got.body)).not.toContain("private");
90 expect(Object.keys(got.body).sort()).toEqual(["expires", "kind", "next", "ownerActed", "received", "state", "statusJson", "statusPage", "updated", "words"]);
90 expect(Object.keys(got.body).sort()).toEqual(["expires", "kind", "received", "state", "updated", "words"]);
9191 const page = await fetch(r.statusPage);
9292 expect(page.status).toBe(200);
9393 expect(page.headers.get("x-content-type-options")).toBe("nosniff");
@@ -201,49 +201,4 @@ describe("outsider status link", () => {
201201 expect(ownPage).not.toContain("data-status-link");
202202 expect(app.db.query("SELECT 1 FROM outsider_status WHERE ref_id = ?1").get(own.value.id)).toBeNull();
203203 });
204 test("the receipt says what happens next and when a maintainer last acted, in words, with no one named", async () => {
205 const r = await report();
206 const before = (await read(r.statusJson)).body;
207 expect(before.next.split(". ").length).toBe(2);
208 const page = await (await fetch(r.statusPage)).text();
209 expect(page).toContain(before.next);
210 expect(page).toContain(r.statusPage);
211 expect(page).toContain(r.statusJson);
212 expect(before.statusJson).toBe(r.statusJson);
213 // A reader's comment is not a maintainer acting.
214 const carol = await createUser(app.db, { origin: "seed", trusted: true, username: "carol", email: "[email protected]", password: "a-good-passphrase" });
215 if (!carol.ok) throw new Error(carol.error);
216 const a = await offer();
217 const old = new Date(Date.now() - 3 * 86_400_000).toISOString();
218 // Everything a maintainer did so far happened three days ago; then a reader comments now.
219 for (const q of ["UPDATE review SET created_at = ?1", "UPDATE comment SET created_at = ?1", "UPDATE report SET decided_at = ?1 WHERE decided_at IS NOT NULL", "UPDATE pull SET merged_at = ?1 WHERE merged_at IS NOT NULL"])
220 app.db.query(q).run(old);
221 app.db.query("INSERT INTO comment (id, issue_id, author_id, body, created_at) VALUES ('c-carol', ?1, ?2, 'hi', ?3)").run(issueOf(a.number), carol.value.id, new Date().toISOString());
222 const got = (await read(r.statusJson)).body;
223 expect(got.ownerActed).toBe("3 days ago");
224 const html = await (await fetch(r.statusPage)).text();
225 expect(html).toContain("last acted in this repository");
226 for (const leak of ["alice", "carol", SECRET, old.slice(0, 10)]) {
227 expect(JSON.stringify(got)).not.toContain(leak);
228 expect(html.replace(/\/(status|api)[^"<]*/g, "")).not.toContain(leak);
229 }
230 });
231 test("many later reader comments do not push a maintainer's action out of the answer", async () => {
232 const r = await report();
233 const dave = await createUser(app.db, { origin: "seed", trusted: true, username: "dave", email: "[email protected]", password: "a-good-passphrase" });
234 if (!dave.ok) throw new Error(dave.error);
235 const a = await offer();
236 const old = new Date(Date.now() - 5 * 86_400_000).toISOString();
237 for (const q of ["UPDATE review SET created_at = ?1", "UPDATE comment SET created_at = ?1", "UPDATE report SET decided_at = ?1 WHERE decided_at IS NOT NULL", "UPDATE pull SET merged_at = ?1 WHERE merged_at IS NOT NULL"])
238 app.db.query(q).run(old);
239 for (let i = 0; i < 60; i++)
240 app.db.query("INSERT INTO comment (id, issue_id, author_id, body, created_at) VALUES (?1, ?2, ?3, 'hi', ?4)").run(`c-dave-${i}`, issueOf(a.number), dave.value.id, new Date(Date.now() - i * 1000).toISOString());
241 expect((await read(r.statusJson)).body.ownerActed).toBe("5 days ago");
242 });
243 test("a private repository's receipt does not say when its maintainers acted", async () => {
244 const r = await report();
245 app.db.query("UPDATE repo SET visibility = 'private'").run();
246 expect((await read(r.statusJson)).body.ownerActed).toBeNull();
247 app.db.query("UPDATE repo SET visibility = 'public'").run();
248 });
249204 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs