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

A forge link that is about to expire says so to its owner, and renews in one tap #14175

Merged opened this

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

Merged

scout/forge:scout/14155-a-forge-link-that-is-about-to-expire-say → main 3 commits fork 3 threads · 3 unresolved

  • Merged. scout/14155-a-forge-link-that-is-about-to-expire-say was merged into main as 16f7213.

Review

Sign in to review this change.
7 files changed +79 −12
src/spawn/clients.ts +18 −2
@@ -234,10 +234,11 @@ const LINK_STATES: readonly LinkState[] = ["open", "used", "ended"];
234234 /** The link a child reported, or null when it said nothing usable. */
235235 function linkOfBody(v: unknown): LinkInfo | null {
236236 if (typeof v !== "object" || v === null) return null;
237 const { state, revokedAt } = v as Record<string, unknown>;
237 const { state, revokedAt, usedAt, openedAt, expiresAt } = v as Record<string, unknown>;
238238 if (!LINK_STATES.includes(state as LinkState)) return null;
239239 const iso = (x: unknown): string | null => (typeof x === "string" && !Number.isNaN(Date.parse(x)) ? x : null);
240 return { state: state as LinkState, revokedAt: iso(revokedAt), usedAt: iso((v as Record<string, unknown>)["usedAt"]), openedAt: iso((v as Record<string, unknown>)["openedAt"]) };
240 const expires = iso(expiresAt);
outdated

One line calls iso() twice per field, with casts. Destructure usedAt, openedAt and expiresAt once and spread only when non-null. Same pattern the rest of the function can share.

241 return { state: state as LinkState, revokedAt: iso(revokedAt), usedAt: iso(usedAt), openedAt: iso(openedAt), ...(expires === null ? {} : { expiresAt: expires }) };
241242 }
242243
243244 async function probe(app: App, child: Child): Promise<ForgeState | undefined> {
@@ -650,6 +651,21 @@ export function attentionOf(r: ClientRow, now: number): Attention | null {
650651 return null;
651652 }
652653
654 /**
655 * "3 days left", "expires today", or "expired": the days an open link has, from its own expiry;
656 * null when the link is spent, withdrawn or did not say. Whole days, rounded up, so the last day
657 * reads "1 day left" until the final hours say "expires today".
658 */
659 export function daysLeftWords(link: LinkInfo | null, now: number): string | null {
660 if (link === null || link.expiresAt === undefined || link.state === "used" || link.revokedAt !== null) return null;
661 const left = Date.parse(link.expiresAt) - now;
662 if (Number.isNaN(left)) return null;
663 if (left <= 0) return "expired";
664 if (left < 24 * 3_600_000) return "expires today";
665 const days = Math.ceil(left / 86_400_000);
666 return days === 1 ? "1 day left" : `${days} days left`;
667 }
668
653669 export type StageKey = "sent" | "opened" | "signedin" | "tell";
654670
655671 /** Where a sent link stands, the time that changed, and whether it is time to send it again. */
src/tui/screens/forges.ts +8 −5
@@ -6,7 +6,7 @@
66
77 import type { Ctx, Screen } from "../app/app.ts";
88 import { binding } from "../app/keys.ts";
9 import { ATTENTION_WORDS, firstScreenOf, CLAIM_WORDS, claimState, seenOf, seenParts, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts";
9 import { ATTENTION_WORDS, daysLeftWords, firstScreenOf, CLAIM_WORDS, claimState, seenOf, seenParts, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts";
1010 import { useLine } from "../../spawn/usage.ts";
1111 import { timeAgo } from "../../web/ui.ts";
1212 import { card } from "../ui/widgets.ts";
@@ -29,11 +29,11 @@ import { appLinkSummary, appLinkWords } from "../../spawn/app_links.ts";
2929 /**
3030 * [name, "Not reporting · for Maria · Claimed · 3 apps running"] per row; an empty line when none.
3131 */
32 export function forgeLines(rows: readonly ClientRow[], sender = false): [string, string][] {
32 export function forgeLines(rows: readonly ClientRow[], sender = false, now = Date.now()): [string, string][] {
3333 if (rows.length === 0) return [["", "You have not sent a forge yet"]];
3434 return rows.flatMap((r): [string, string][] => [[
3535 r.name,
36 [r.made ? (r.awake === true ? "awake" : "asleep") : null, r.made && r.where.inProcess ? r.where.says : null, r.stopped ?? null, crewHeldWords(r), r.attention === null ? null : ATTENTION_WORDS[r.attention], r.for === null ? null : `for ${r.for}`, CLAIM_WORDS[claimState(r)], linkWords(r.link), runningWords(r), seenLine(r, sender) !== null && r.stage?.key !== "tell" && r.stage?.resend !== true ? null : r.stage?.words ?? null, seenLine(r, sender), r.nextStep.words, spendWords(r)?.words ?? null, monthWords(r)?.words ?? null, creditWords(r), graceWords(r), useLine(r.size, r.use), r.made ? diskWords(r.disk) : null, r.made && seenLine(r, sender) === null ? (r.lastActivityAt === null ? "no person has done anything here" : `a person last did something ${timeAgo(r.lastActivityAt)}`) : null, claimedWords(r), keepWords(r), r.archive === null || r.archive === undefined ? null : `proposed for archive: ${r.archive.why}`, r.lastHeartbeatAt === null ? "no heartbeat yet" : `heartbeat ${timeAgo(r.lastHeartbeatAt)}`]
36 [r.made ? (r.awake === true ? "awake" : "asleep") : null, r.made && r.where.inProcess ? r.where.says : null, r.stopped ?? null, crewHeldWords(r), r.attention === null ? null : ATTENTION_WORDS[r.attention], r.for === null ? null : `for ${r.for}`, CLAIM_WORDS[claimState(r)], linkWords(r.link, claimState(r) === "no" ? now : null), runningWords(r), seenLine(r, sender) !== null && r.stage?.key !== "tell" && r.stage?.resend !== true ? null : r.stage?.words ?? null, seenLine(r, sender), r.nextStep.words, spendWords(r)?.words ?? null, monthWords(r)?.words ?? null, creditWords(r), graceWords(r), useLine(r.size, r.use), r.made ? diskWords(r.disk) : null, r.made && seenLine(r, sender) === null ? (r.lastActivityAt === null ? "no person has done anything here" : `a person last did something ${timeAgo(r.lastActivityAt)}`) : null, claimedWords(r), keepWords(r), r.archive === null || r.archive === undefined ? null : `proposed for archive: ${r.archive.why}`, r.lastHeartbeatAt === null ? "no heartbeat yet" : `heartbeat ${timeAgo(r.lastHeartbeatAt)}`]
3737 .filter((m): m is string => m !== null)
3838 .join(" · "),
3939 ], ...firstScreenLines(r), ...appLinkLines(r), ...addressLines(r)]);
@@ -49,9 +49,12 @@ function seenLine(r: ClientRow, sender: boolean): string | null {
4949 * "link open", "link ended (undo with u)": what the sender can do about the link, as /forges shows
5050 * it.
5151 */
52 function linkWords(link: LinkInfo | null): string | null {
52 function linkWords(link: LinkInfo | null, now: number | null = null): string | null {
5353 if (link === null || link.state === "used") return null;
54 return link.state === "open" ? "link open" : undoable(link) ? "link ended, undo with u" : "link ended";
54 const left = now === null ? null : daysLeftWords(link, now);
55 if (left === "expired") return "link expired, press n for a new one";
outdated

Days left only shows when state is open, and uses Date.now() instead of the row time. An expired link reads just "link ended", not "expired", so the terminal does not say what the page says. Pass now in and show expired. Add a TUI test; none covers this line.

56 if (link.state === "open") return left === null ? "link open" : `link open, ${left}`;
57 return undoable(link) ? "link ended, undo with u" : "link ended";
5558 }
5659
5760 /** Under a forge not yet claimed: what its person will see first, as /forges says it. */
src/web/pages/forges.ts +6 −3
@@ -9,7 +9,7 @@
99 import type { Ctx } from "../../http/context.ts";
1010 import { claimsBill } from "../../actions/forge_bill.ts";
1111 import { billRow } from "./bills.ts";
12 import { ATTENTION_WORDS, firstScreenOf, CLAIM_WORDS, claimState, memoryOf, seenOf, seenParts, type Seen, servedRows, dropServed, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts";
12 import { ATTENTION_WORDS, daysLeftWords, firstScreenOf, CLAIM_WORDS, claimState, memoryOf, seenOf, seenParts, type Seen, servedRows, dropServed, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts";
1313 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts";
1414 import { notFoundPage, pageHead } from "../chrome.ts";
1515 import { html, type Html } from "../html.ts";
@@ -78,6 +78,7 @@ const XLARGE_SAYS = `${sizeWords("xlarge")}. ${sizesView().sizes.find((x) => x.s
7878
7979 export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = false, kept?: string): Html {
8080 const state = claimState(r);
81 const daysLeft = state === "no" ? daysLeftWords(r.link, now) : null;
8182 const seen = seenOf(r);
8283 const meta = [
8384 r.for === null ? null : html`for ${r.for}`,
@@ -142,12 +143,14 @@ export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = f
142143 : html`<span data-attention="${r.attention}">${badge(ATTENTION_WORDS[r.attention], { tone: "attn" })}</span>`}${badge(CLAIM_WORDS[state], {
143144 ...(state === "yes" ? { tone: "success" as const } : { variant: "outline" as const }),
144145 })}${state === "no"
145 ? btn(r.stage?.resend === true ? "Resend" : "Send a new link", { size: "sm", href: `/forges/${r.name}/claim-link`, title: "See who it is for and what stops working, then confirm" })
146 ? daysLeft !== null && r.link?.state === "open"
147 ? html`<form method="post" action="/forges/${r.name}/claim-link" class="inline">${btn("Renew link", { size: "sm", title: "Issue a fresh link now and end the old one; shown once, on the next page" })}</form>`
148 : btn(r.stage?.resend === true ? "Resend" : "Send a new link", { size: "sm", href: `/forges/${r.name}/claim-link`, title: "See who it is for and what stops working, then confirm" })
outdated

Principle 1/7: the days-left span sits bare beside the badge, and daysLeftWords is called twice. Compute it once. A near-expiry link is the thing the owner should act on, but the row offers only a link to a confirm page, not a one-tap renew, so the issue title is not met. Offer a post-form button here.

146149 : null}${state === "yes" && r.made
147150 ? btn("Recovery link", { size: "sm", href: `/forges/${r.name}/recovery-link`, title: "For an owner who lost their passkey: see what it does, then confirm" })
148151 : null}${r.link === null
149152 ? null
150 : html`<span data-link="${r.link.state}">${badge(LINK_WORDS[r.link.state], { variant: "outline" as const })}</span>${r.link.state === "open"
153 : html`<span data-link="${r.link.state}">${badge(LINK_WORDS[r.link.state], { variant: "outline" as const })}</span>${state === "no" && daysLeft !== null ? html`<span data-days-left class="muted">${daysLeft}</span>` : null}${r.link.state === "open"
151154 ? html`<form method="post" action="/forges/${r.name}/revoke-link" class="inline">${btn("Revoke", { size: "sm", title: "End this link now; you can undo for a minute" })}</form>`
152155 : undoable(r.link, now)
153156 ? html`<form method="post" action="/forges/${r.name}/undo-link" class="inline">${btn("Undo", { size: "sm", title: "Bring the link back; possible for a minute after it ended" })}</form>`
test/client-list.test.ts +5 −2
@@ -213,12 +213,15 @@ describe("the client list", () => {
213213 expect(line).not.toContain('class="card');
214214 expect(maria).toContain('href="https://maria--forge.test"');
215215 expect(maria).toMatch(/data-claimed="no"/);
216 // The unclaimed link says how long it has left (#14155).
217 expect(maria).toMatch(/<form method="post" action="\/forges\/maria\/claim-link"[^>]*>[\s\S]*?<button/);
218 expect(maria).toMatch(/data-days-left[^>]*>(\d+ days? left|expires today)</);
216219 expect(html).not.toContain("forges-sent");
217220 const lou = html.slice(html.indexOf('data-forge="lou"'));
218221 expect(lou).toMatch(/data-claimed="unknown"/);
219222 expect(html).not.toContain("claim-link-box");
220223 expect(maria).toContain('class="row"');
221 expect(maria).toContain("Send a new link");
224 expect(maria).toContain("Renew link");
222225 expect(lou).toContain("it did not answer just now");
223226 // It did not answer, so it carries the one attention badge and leads the list.
224227 expect(lou).toContain('data-attention="silent"');
@@ -264,7 +267,7 @@ describe("the client list", () => {
264267 test("an unclaimed row offers a fresh link: it replaces the old one, shown once; only an admin may ask", async () => {
265268 const html = (await (await list(admin)).text()).replace(/\s+/g, " ");
266269 const maria = html.slice(html.indexOf('data-forge="maria"'), html.indexOf("</li>", html.indexOf('data-forge="maria"')));
267 expect(maria).toContain('href="/forges/maria/claim-link"');
270 expect(maria).toContain('action="/forges/maria/claim-link"');
268271 const lou = html.slice(html.indexOf('data-forge="lou"'), html.indexOf("</li>", html.indexOf('data-forge="lou"')));
269272 // Whether lou was claimed is unknown, so it offers nothing to send.
270273 expect(lou).not.toContain("claim-link");
test/tui-forges.test.ts +10
@@ -126,6 +126,16 @@ describe("forges you sent in the terminal", () => {
126126 h.stop();
127127 });
128128
129 test("an unclaimed row says the days its link has left, and says expired", () => {
130 const now = Date.parse("2026-11-16T12:00:00Z");
131 const line = (link: ClientRow["link"]) => forgeLines([row({ link })], false, now)[0]![1];
132 const at = (ms: number) => new Date(now + ms).toISOString();
133 expect(line({ state: "open", revokedAt: null, expiresAt: at(3 * 86_400_000) })).toContain("link open, 3 days left");
134 expect(line({ state: "open", revokedAt: null, expiresAt: at(-1000) })).toContain("link expired, press n for a new one");
135 expect(line({ state: "ended", revokedAt: null, expiresAt: at(-1000) })).toContain("link expired");
136 expect(line({ state: "open", revokedAt: null })).toContain("link open");
137 });
138
129139 test("a row says whether its link is open, ended, or can be undone; a used link says nothing", () => {
130140 const line = (link: ClientRow["link"]) => forgeLines([row({ link })])[0]![1];
131141 expect(line({ state: "open", revokedAt: null })).toContain("link open");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs