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

A pull opened by a stranger shows its status link on the pull page's head, copyable in one tap #16009

Merged opened this

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

Merged

torque/forge:torque/15988-a-pull-opened-by-a-stranger-shows-its-st → main 0 commits fork 2 threads · 2 unresolved

  • Merged. torque/15988-a-pull-opened-by-a-stranger-shows-its-st was merged into main as 01543f9.

Review

Sign in to review this change.
6 files changed +102 −3
docs/features/report-status.md +1
@@ -35,5 +35,6 @@ The answer to a report or an offer (`…/offers`, the MCP `offer` tool) also car
3535 - **Shows:** `kind`, `state` (received, read, planned, landed or declined), its words, and the dates it arrived, last changed and expires. Never the title, the words, a name or an id.
3636 - **States:** a report is received until a person decides; accepting makes it planned, closing that issue makes it landed (done) or declined (dropped); dismissing declines it. An offer is received, then read once a reviewer reviews, then landed or declined. So a report is never `read` (a person's decision is its first change) and an offer is never `planned`; each shows only the states its own steps can reach.
3737 - **The token:** 192 random bits, read-only, good 90 days; a wrong, malformed or expired one is a 404 (`src/core/outsider_status.ts`, `src/http/offer_routes.ts`). The status routes answer 429 past 60 reads a minute per client, and links past their day are deleted hourly. Pinned by `test/outsider-status.test.ts`.
38 - **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).
3839
3940 Pinned by `test/report-status.test.ts`.
src/core/outsider_status.ts +32
@@ -15,6 +15,8 @@ import type { Database } from "bun:sqlite";
1515 import { getIssueById } from "./issues.ts";
1616 import { getPull } from "./pulls.ts";
1717 import { listReviews } from "./reviews.ts";
18 import { canWrite, type Repo } from "./repos.ts";
19 import type { User } from "./users.ts";
1820
1921 export type OutsiderState = "received" | "read" | "planned" | "landed" | "declined";
2022 export type OutsiderKind = "report" | "offer";
@@ -59,6 +61,18 @@ export function issueStatusToken(db: Database, kind: OutsiderKind, refId: string
5961 return token;
6062 }
6163
64 /**
65 * The live token of an offer's pull (its issue id), newest first; undefined when none or past its
66 * day.
67 */
68 export function offerStatusToken(db: Database, issueId: string, now = Date.now()): string | undefined {
69 return db
70 .query<{ token: string }, [string, string]>(
71 "SELECT token FROM outsider_status WHERE kind = 'offer' AND ref_id = ?1 AND expires_at > ?2 ORDER BY created_at DESC LIMIT 1",
72 )
73 .get(issueId, new Date(now).toISOString())?.token;
74 }
75
6276 /** Drop the links past their day; a dead token already answers 404, so this only frees rows. */
6377 export function pruneOutsiderStatus(db: Database, now = Date.now()): number {
6478 return db.query("DELETE FROM outsider_status WHERE expires_at <= ?1").run(new Date(now).toISOString()).changes;
@@ -126,3 +140,21 @@ export const statusLinks = (origin: string, token: string): { page: string; json
126140 page: `${origin}/status/${token}`,
127141 json: `${origin}/api/v1/status/${token}`,
128142 });
143
144 /**
145 * The status page link of an outsider's offer, for a person who may see it (#15988): a writer of
146 * the repository, on a pull an offer opened whose link is still live. It is a capability (anyone
147 * holding it reads the state), so a reader, a guest and an ordinary pull get undefined. The web
148 * page and the terminal's pull screen both ask here, so they cannot drift.
149 */
150 export function offerStatusLinkFor(
151 db: Database,
152 repo: Repo,
153 user: User | undefined,
154 pullId: string,
155 origin: () => string,
156 ): string | undefined {
157 if (user === undefined || !canWrite(db, repo, user)) return undefined;
158 const token = offerStatusToken(db, pullId);
159 return token === undefined ? undefined : statusLinks(origin(), token).page;
160 }
src/tui/screens/pull.ts +11 −1
@@ -27,6 +27,7 @@
2727 */
2828
2929 import { OFFER_PULL_WORDS, offerPull, previewOfferPull } from "../../actions/offer_pull.ts";
30 import { offerStatusLinkFor } from "../../core/outsider_status.ts";
3031 import { outsideClause } from "../../web/outside.ts";
3132 import { teamHoldRefusal, waitingOnTeams } from "../../core/teams_words.ts";
3233 import { crewFilters, guardedRefusal, isStrangerPull, strangerGuard, strangerNext } from "../../crew/stranger.ts";
@@ -362,6 +363,8 @@ export function pullScreen(
362363 * for anyone else's.
363364 */
364365 let strangerLine = "";
366 /** The offer's status link, for a writer only (`offerStatusLinkFor`, #15988); empty otherwise. */
367 let statusLink = "";
365368 /** `h` is a person's key: an agent's hold is refused by the action, so it is not offered. */
366369 const mayHold = (): boolean =>
367370 mayWrite() && f.user?.kind === "user" && issueRow !== undefined;
@@ -414,6 +417,10 @@ export function pullScreen(
414417 repo !== undefined && row !== undefined && row.state === "open" && row.mergedAt === null && isStrangerPull(f.db, repo, row)
415418 ? strangerNext(repo, await strangerGuard(f.app, repo, row))
416419 : "";
420 statusLink =
421 repo === undefined || row === undefined
422 ? ""
423 : (offerStatusLinkFor(f.db, repo, f.user, row.id, () => lookOrigin(f.app.config)) ?? "");
417424 combined =
418425 repo !== undefined && row !== undefined && facts !== null
419426 ? await (async () => {
@@ -1287,13 +1294,14 @@ export function pullScreen(
12871294 (checksLine === "" ? 0 : 1) +
12881295 (originCheckText === "" ? 0 : 1) +
12891296 teams.length;
1290 const [strip, head, nextRow, previewRow, boxRow, tabRow, body, comp] = split(
1297 const [strip, head, nextRow, linkRow, previewRow, boxRow, tabRow, body, comp] = split(
12911298 area,
12921299 "v",
12931300 [
12941301 len(whereRows(src)),
12951302 len(3),
12961303 len(strangerLine === "" ? 0 : 1),
1304 len(statusLink === "" ? 0 : 1),
12971305 len(isApp ? 1 : 0),
12981306 len(boxH),
12991307 len(2),
@@ -1354,6 +1362,8 @@ export function pullScreen(
13541362 }
13551363 if (strangerLine !== "" && nextRow!.h > 0)
13561364 s.text(nextRow!.x, nextRow!.y, truncate(`next: ${strangerLine}`, nextRow!.w), style(c.mutedFg, c.bg), nextRow!.x + nextRow!.w);
1365 if (statusLink !== "" && linkRow!.h > 0)
1366 s.text(linkRow!.x, linkRow!.y, truncate(`status link: ${statusLink}`, linkRow!.w), style(c.mutedFg, c.bg), linkRow!.x + linkRow!.w);
13571367 if (isApp && previewRow!.h > 0)
13581368 drawPreviewLine(s, previewRow!, c, preview);
13591369 drawMergeBox(s, boxRow!, c, issue, facts, view.summary(), mirrored(), {
src/web/studio/change.ts +14 −1
@@ -14,6 +14,7 @@ import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
1414 import { cardPath } from "../og/path.ts";
1515 import { pullProposalAt, type PullProposalAt } from "../../core/proposal_token.ts";
1616 import { attestationLine } from "../../core/attestation.ts";
17 import { offerStatusLinkFor } from "../../core/outsider_status.ts";
1718 import { getIssueById } from "../../core/issues.ts";
1819 import { mayEditIssue } from "../../actions/issues.ts";
1920 import { outsideLabelFrom } from "../outside.ts";
@@ -56,6 +57,7 @@ import {
5657 card,
5758 crumbs,
5859 confirmAction,
60 copyField,
5961 badge,
6062 icon,
6163 originTag,
@@ -249,6 +251,17 @@ function reviewEntry(
249251 };
250252 }
251253
254 /**
255 * The status link an outsider was handed for this offer (#15988), as one line with Copy. It is a
256 * capability (anyone holding it reads the state), so only a writer sees it, and only on a pull
257 * that an outsider's offer opened.
258 */

Medium: the web page is the only door that shows the link. The terminal's pull screen (src/tui/) must show it too, behind the same writer check, and reach the same data (offerStatusToken). Put the 'writer may see the link' decision in one core function so the page and the terminal cannot drift.

259 function statusLinkLine(ctx: Ctx, repo: Repo, pull: Pull): Html | "" {
260 const url = offerStatusLinkFor(ctx.app.db, repo, ctx.auth.user, pull.id, () => ctx.app.config.url ?? ctx.url.origin);
261 if (url === undefined) return "";
262 return html`<div class="small" data-status-link>${copyField("status-link", url, "Status link for the person who offered this", "Copy status link")}</div>`;
263 }
264
252265 /** A change's head, the same on both of its tabs: the title, its state,
253266 * who opened it and in answer to whom, where it came from, and what the
254267 * crew spent on it. */
@@ -309,7 +322,7 @@ export function changeHead(
309322 >`,
310323 )}
311324 </div>
312 ${when(step, (st) => pathLine(st, next === null ? undefined : `${next.who}, ${next.why}`))}${when(
325 ${statusLinkLine(ctx, repo, pull)}${when(step, (st) => pathLine(st, next === null ? undefined : `${next.who}, ${next.why}`))}${when(
313326 pull.mergedAt === null ? pausedSentence(ctx.app.db, { issueId: pull.id }) : undefined,
314327 (says) => html`<p class="muted small" data-paused role="status">${says}.</p>`,
315328 )}${when(
test/outsider-status.test.ts +26 −1
@@ -8,7 +8,8 @@ import { tmpdir } from "node:os";
88 import { join } from "node:path";
99 import { createApp, type App } from "../src/app.ts";
1010 import { editFiles } from "../src/actions/contents.ts";
11 import { createRepo } from "../src/actions/repos.ts";
11 import { openPull } from "../src/actions/pulls.ts";
12 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1213 import { acceptReport, dismissReport } from "../src/actions/reports.ts";
1314 import { setIssueState } from "../src/core/issues.ts";
1415 import { pruneOutsiderStatus, STATUS_TTL_MS } from "../src/core/outsider_status.ts";
@@ -16,6 +17,7 @@ import { getRepo } from "../src/core/repos.ts";
1617 import { submitReview } from "../src/core/reviews.ts";
1718 import { createUser, type User } from "../src/core/users.ts";
1819 import { serve, type Servers } from "../src/http/server.ts";
20 import { signInCookie } from "./helpers/studio-signin.ts";
1921 import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts";
2022
2123 const PATCH = `diff --git a/hello.txt b/hello.txt
@@ -176,4 +178,27 @@ describe("outsider status link", () => {
176178 for (let i = 0; i < 61; i++) last = (await get()).status;
177179 expect(last).toBe(429);
178180 });
181
182 test("the owner sees an offer's status link on its pull page, with Copy; a reader, a guest and an own pull do not", async () => {
183 const a = await offer();
184 const bob = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" });
185 if (!bob.ok) throw new Error(bob.error);
186 const page = async (cookie: string, number: number) =>
187 (await fetch(`${servers.url}/alice/game/pulls/${number}`, { headers: cookie === "" ? {} : { cookie } })).text();
188 const owner = await signInCookie(servers.url, "alice");
189 const mine = await page(owner, a.number);
190 expect(mine).toContain(a.statusPage);
191 expect(mine).toContain('data-copy="status-link"');
192 expect(await page(await signInCookie(servers.url, "bob"), a.number)).not.toContain(tokenOf(a.statusPage));
193 expect(await page("", a.number)).not.toContain(tokenOf(a.statusPage));
outdated

Box 2 is a pull the owner opened, not an offer with its token deleted. Open an ordinary pull as alice and assert it has no data-status-link. Add a test for the terminal screen too.

194 // A pull the owner opened herself has no line.
195 const branch = await commitToBranch(app, alice, repo(), { branch: "own", message: "own", files: [{ path: "own.txt", content: "x\n" }] });
196 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
197 const own = await openPull(app, alice, repo(), { title: "Own", baseBranch: "main", headBranch: "own" });
198 if (!own.ok) throw new Error(JSON.stringify(own.error));
199 const ownPage = await page(owner, own.value.number);
200 expect(ownPage).toContain("Own");
201 expect(ownPage).not.toContain("data-status-link");
202 expect(app.db.query("SELECT 1 FROM outsider_status WHERE ref_id = ?1").get(own.value.id)).toBeNull();
203 });
179204 });
test/pull-next-tui.test.ts +18
@@ -8,6 +8,7 @@ import { createApp, type App } from "../src/app.ts";
88 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
99 import { repoDir } from "../src/git/repos.ts";
1010 import { submitReview } from "../src/core/reviews.ts";
11 import { issueStatusToken, offerStatusLinkFor } from "../src/core/outsider_status.ts";
1112 import { createUser, type User } from "../src/core/users.ts";
1213 import { createForgeApp } from "../src/tui/forge.ts";
1314 import { pullScreen } from "../src/tui/screens/pull.ts";
@@ -118,3 +119,20 @@ test("the terminal's pull screen says when an approval was given for a base that
118119 if (!moved.ok) throw new Error(JSON.stringify(moved.error));
119120 await screenSaying(topic, "the base moved since the approval");
120121 });
122
123 test("the terminal's pull screen shows an offer's status link to a writer only, and none on an ordinary pull (#15988)", async () => {
124 const topic = await openTopic("lib-link", true);
125 const plain = await screenSaying(topic, "Topic");
126 if (plain.includes("status link:")) throw new Error(plain);
127 const token = issueStatusToken(app.db, "offer", topic.pull.id);
128 const url = offerStatusLinkFor(app.db, topic.repo, alice, topic.pull.id, () => "http://forge.test");
129 if (url !== `http://forge.test/status/${token}`) throw new Error(String(url));
130 const text = await screenSaying(topic, "status link:");
131 if (!text.includes(`/status/${token}`)) throw new Error(text);
132 const bob = await createUser(app.db, {
133 origin: "seed", trusted: true, username: "bobl", email: "[email protected]", password: "a-good-passphrase",
134 });
135 if (!bob.ok) throw new Error(String(bob.error));
136 if (offerStatusLinkFor(app.db, topic.repo, bob.value, topic.pull.id, () => "http://forge.test") !== undefined) throw new Error("a reader got the link");
137 if (offerStatusLinkFor(app.db, topic.repo, undefined, topic.pull.id, () => "http://forge.test") !== undefined) throw new Error("a guest got the link");
138 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs