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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14155-a-forge-link-that-is-about-to-expire-say → main 3 commits fork 3 threads · 3 unresolved
docs/features/client-link-times.md +4
| @@ -11,3 +11,7 @@ Each forge row for a link says **sent**, **opened** and **first tell**, each fro | ||
| 11 | 11 | A child that has not sent a heartbeat carrying `firstTellAt` is unknown, not empty, so its row says nothing about first tell rather than a false "not yet". The line appears at its next heartbeat. Only a beat that says `firstTellAt: null` reads "not yet". |
| 12 | 12 | |
| 13 | 13 | Tests: `test/client-list-events.test.ts` (the line, the parse, the earliest issue) and `test/client-list.test.ts` (a beat through `clientRows` onto the page). |
| 14 | ||
| 15 | ## Days left on an unclaimed link (#14155) | |
| 16 | ||
| 17 | An unclaimed forge's row says how long its open link has: "3 days left", "1 day left", "expires today" (`daysLeftWords`, `src/spawn/clients.ts`, read from the link's own expiry the child reports). The terminal's row says the same (`link open, 3 days left`). An expired link answers its person a plain page naming who to ask, with "Ask for a new link"; the owner's "Send a new link" (or "Resend") on the row issues a fresh one and ends the old at once (`test/forge-link-reissue.test.ts`, `test/client-link-days-left.test.ts`). |
src/spawn/clients.ts +18 −2
| @@ -234,10 +234,11 @@ const LINK_STATES: readonly LinkState[] = ["open", "used", "ended"]; | ||
| 234 | 234 | /** The link a child reported, or null when it said nothing usable. */ |
| 235 | 235 | function linkOfBody(v: unknown): LinkInfo | null { |
| 236 | 236 | 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>; | |
| 238 | 238 | if (!LINK_STATES.includes(state as LinkState)) return null; |
| 239 | 239 | 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); | |
| 241 | return { state: state as LinkState, revokedAt: iso(revokedAt), usedAt: iso(usedAt), openedAt: iso(openedAt), ...(expires === null ? {} : { expiresAt: expires }) }; | |
| 241 | 242 | } |
| 242 | 243 | |
| 243 | 244 | async function probe(app: App, child: Child): Promise<ForgeState | undefined> { |
| @@ -650,6 +651,21 @@ export function attentionOf(r: ClientRow, now: number): Attention | null { | ||
| 650 | 651 | return null; |
| 651 | 652 | } |
| 652 | 653 | |
| 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 | ||
| 653 | 669 | export type StageKey = "sent" | "opened" | "signedin" | "tell"; |
| 654 | 670 | |
| 655 | 671 | /** 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 @@ | ||
| 6 | 6 | |
| 7 | 7 | import type { Ctx, Screen } from "../app/app.ts"; |
| 8 | 8 | 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"; | |
| 10 | 10 | import { useLine } from "../../spawn/usage.ts"; |
| 11 | 11 | import { timeAgo } from "../../web/ui.ts"; |
| 12 | 12 | import { card } from "../ui/widgets.ts"; |
| @@ -29,11 +29,11 @@ import { appLinkSummary, appLinkWords } from "../../spawn/app_links.ts"; | ||
| 29 | 29 | /** |
| 30 | 30 | * [name, "Not reporting · for Maria · Claimed · 3 apps running"] per row; an empty line when none. |
| 31 | 31 | */ |
| 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][] { | |
| 33 | 33 | if (rows.length === 0) return [["", "You have not sent a forge yet"]]; |
| 34 | 34 | return rows.flatMap((r): [string, string][] => [[ |
| 35 | 35 | 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)}`] | |
| 37 | 37 | .filter((m): m is string => m !== null) |
| 38 | 38 | .join(" · "), |
| 39 | 39 | ], ...firstScreenLines(r), ...appLinkLines(r), ...addressLines(r)]); |
| @@ -49,9 +49,12 @@ function seenLine(r: ClientRow, sender: boolean): string | null { | ||
| 49 | 49 | * "link open", "link ended (undo with u)": what the sender can do about the link, as /forges shows |
| 50 | 50 | * it. |
| 51 | 51 | */ |
| 52 | function linkWords(link: LinkInfo | null): string | null { | |
| 52 | function linkWords(link: LinkInfo | null, now: number | null = null): string | null { | |
| 53 | 53 | 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"; | |
| 55 | 58 | } |
| 56 | 59 | |
| 57 | 60 | /** 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 @@ | ||
| 9 | 9 | import type { Ctx } from "../../http/context.ts"; |
| 10 | 10 | import { claimsBill } from "../../actions/forge_bill.ts"; |
| 11 | 11 | 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"; | |
| 13 | 13 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; |
| 14 | 14 | import { notFoundPage, pageHead } from "../chrome.ts"; |
| 15 | 15 | import { html, type Html } from "../html.ts"; |
| @@ -78,6 +78,7 @@ const XLARGE_SAYS = `${sizeWords("xlarge")}. ${sizesView().sizes.find((x) => x.s | ||
| 78 | 78 | |
| 79 | 79 | export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = false, kept?: string): Html { |
| 80 | 80 | const state = claimState(r); |
| 81 | const daysLeft = state === "no" ? daysLeftWords(r.link, now) : null; | |
| 81 | 82 | const seen = seenOf(r); |
| 82 | 83 | const meta = [ |
| 83 | 84 | r.for === null ? null : html`for ${r.for}`, |
| @@ -142,12 +143,14 @@ export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = f | ||
| 142 | 143 | : html`<span data-attention="${r.attention}">${badge(ATTENTION_WORDS[r.attention], { tone: "attn" })}</span>`}${badge(CLAIM_WORDS[state], { |
| 143 | 144 | ...(state === "yes" ? { tone: "success" as const } : { variant: "outline" as const }), |
| 144 | 145 | })}${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. |
||
| 146 | 149 | : null}${state === "yes" && r.made |
| 147 | 150 | ? 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" }) |
| 148 | 151 | : null}${r.link === null |
| 149 | 152 | ? 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" | |
| 151 | 154 | ? 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>` |
| 152 | 155 | : undoable(r.link, now) |
| 153 | 156 | ? 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-link-days-left.test.ts added +28
| @@ -0,0 +1,28 @@ | ||
| 1 | /** An unclaimed link says how long it has left (#14155); renew and expiry have their own tests. */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { daysLeftWords } from "../src/spawn/clients.ts"; | |
| 4 | ||
| 5 | const NOW = Date.parse("2026-11-16T12:00:00Z"); | |
| 6 | const at = (ms: number) => new Date(NOW + ms).toISOString(); | |
| 7 | const H = 3_600_000; | |
| 8 | const link = (over: object) => ({ state: "open" as const, revokedAt: null, expiresAt: at(3 * 24 * H), ...over }); | |
| 9 | ||
| 10 | describe("days left on a link", () => { | |
| 11 | test("counts whole days, rounded up, then says today, then expired", () => { | |
| 12 | expect(daysLeftWords(link({}), NOW)).toBe("3 days left"); | |
| 13 | expect(daysLeftWords(link({ expiresAt: at(2 * 24 * H + H) }), NOW)).toBe("3 days left"); | |
| 14 | expect(daysLeftWords(link({ expiresAt: at(30 * H) }), NOW)).toBe("2 days left"); | |
| 15 | expect(daysLeftWords(link({ expiresAt: at(25 * H) }), NOW)).toBe("2 days left"); | |
| 16 | expect(daysLeftWords(link({ expiresAt: at(24 * H) }), NOW)).toBe("1 day left"); | |
| 17 | expect(daysLeftWords(link({ expiresAt: at(5 * H) }), NOW)).toBe("expires today"); | |
| 18 | expect(daysLeftWords(link({ expiresAt: at(-H), state: "ended" }), NOW)).toBe("expired"); | |
| 19 | }); | |
| 20 | ||
| 21 | test("says nothing for a spent, withdrawn or unreported link", () => { | |
| 22 | expect(daysLeftWords(null, NOW)).toBeNull(); | |
| 23 | expect(daysLeftWords(link({ state: "used" }), NOW)).toBeNull(); | |
| 24 | expect(daysLeftWords(link({ state: "ended", revokedAt: at(-H) }), NOW)).toBeNull(); | |
| 25 | expect(daysLeftWords({ state: "open", revokedAt: null }, NOW)).toBeNull(); | |
| 26 | expect(daysLeftWords(link({ expiresAt: "garbage" }), NOW)).toBeNull(); | |
| 27 | }); | |
| 28 | }); |
test/client-list.test.ts +5 −2
| @@ -213,12 +213,15 @@ describe("the client list", () => { | ||
| 213 | 213 | expect(line).not.toContain('class="card'); |
| 214 | 214 | expect(maria).toContain('href="https://maria--forge.test"'); |
| 215 | 215 | 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)</); | |
| 216 | 219 | expect(html).not.toContain("forges-sent"); |
| 217 | 220 | const lou = html.slice(html.indexOf('data-forge="lou"')); |
| 218 | 221 | expect(lou).toMatch(/data-claimed="unknown"/); |
| 219 | 222 | expect(html).not.toContain("claim-link-box"); |
| 220 | 223 | expect(maria).toContain('class="row"'); |
| 221 | expect(maria).toContain("Send a new link"); | |
| 224 | expect(maria).toContain("Renew link"); | |
| 222 | 225 | expect(lou).toContain("it did not answer just now"); |
| 223 | 226 | // It did not answer, so it carries the one attention badge and leads the list. |
| 224 | 227 | expect(lou).toContain('data-attention="silent"'); |
| @@ -264,7 +267,7 @@ describe("the client list", () => { | ||
| 264 | 267 | test("an unclaimed row offers a fresh link: it replaces the old one, shown once; only an admin may ask", async () => { |
| 265 | 268 | const html = (await (await list(admin)).text()).replace(/\s+/g, " "); |
| 266 | 269 | 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"'); | |
| 268 | 271 | const lou = html.slice(html.indexOf('data-forge="lou"'), html.indexOf("</li>", html.indexOf('data-forge="lou"'))); |
| 269 | 272 | // Whether lou was claimed is unknown, so it offers nothing to send. |
| 270 | 273 | expect(lou).not.toContain("claim-link"); |
test/tui-forges.test.ts +10
| @@ -126,6 +126,16 @@ describe("forges you sent in the terminal", () => { | ||
| 126 | 126 | h.stop(); |
| 127 | 127 | }); |
| 128 | 128 | |
| 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 | ||
| 129 | 139 | test("a row says whether its link is open, ended, or can be undone; a used link says nothing", () => { |
| 130 | 140 | const line = (link: ClientRow["link"]) => forgeLines([row({ link })])[0]![1]; |
| 131 | 141 | expect(line({ state: "open", revokedAt: null })).toContain("link open"); |
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.