Public: anyone can read this; the crew can change it.
A client list row offers Send again: it replaces the forge link with a fresh one and the old link stops working, with a receipt #13507
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13485-a-client-list-row-offers-send-again-it-r → main 2 commits fork 2 threads · 2 unresolved
3 files
changed +134
−2
docs/features/forge-link-states.md +4
| @@ -77,3 +77,7 @@ resend notice and the last tell keep their own words. | ||
| 77 | 77 | - **Last seen** appears once the client signed in: the time of a person's last |
| 78 | 78 | action there. It is blank before, never "now". |
| 79 | 79 | - Nothing is said while the forge is still being made or did not answer. |
| 80 | - **Link replaced** appears after the sender taps Send again: the earlier link | |
| 81 | stopped working and a fresh one is waiting, and the time is that of the | |
| 82 | receipt (`forge.claim_reissued`). It shows only while the forge is unclaimed; | |
| 83 | once the client has claimed, it is dropped and "last seen" stands alone. |
src/spawn/clients.ts +28 −2
| @@ -110,6 +110,11 @@ export interface ClientRow { | ||
| 110 | 110 | * not say. |
| 111 | 111 | */ |
| 112 | 112 | readonly link: LinkInfo | null; |
| 113 | /** | |
| 114 | * When its link was last sent again and the old one ended (the `forge.claim_reissued` event), | |
| 115 | * ISO; null when never. | |
| 116 | */ | |
| 117 | readonly replacedAt?: string | null; | |
| 113 | 118 | /** Why this forge needs its owner now; null when it does not. */ |
| 114 | 119 | readonly attention: Attention | null; |
| 115 | 120 | /** |
| @@ -317,6 +322,7 @@ export async function clientRows(app: App, options: { probe?: boolean } = {}): P | ||
| 317 | 322 | const people = await Promise.all(children.map((c) => (c.done ? childPerson(childDirOf(app, c.bodyId)) : undefined))); |
| 318 | 323 | const links = await Promise.all(children.map((c) => (c.apps.length === 0 ? [] : appLinkLines(app, c)))); |
| 319 | 324 | const notices = readNotices(app); |
| 325 | const replaced = linksReplaced(app); | |
| 320 | 326 | // Asked once, and only when a forge here is not in a cell: what its row says depends on whether |
| 321 | 327 | // this host could hold it in one. |
| 322 | 328 | const cells: CellsVerdict = children.some((c) => c.placement !== "cell") ? await cellsHere() : { ok: true }; |
| @@ -356,6 +362,7 @@ export async function clientRows(app: App, options: { probe?: boolean } = {}): P | ||
| 356 | 362 | disk: liveChildDisk(app.config.dataDir, c.bodyId, mountinfo), |
| 357 | 363 | lastHeartbeatAt: beats[i] === undefined ? null : new Date(beats[i]!.receivedAt).toISOString(), |
| 358 | 364 | link: states[i]?.link ?? null, |
| 365 | replacedAt: replaced.get(c.name) ?? null, | |
| 359 | 366 | // Read from the record this forge wrote when it made the child, never from the child's word. |
| 360 | 367 | madeFrom: madeFromOf(childDirOf(app, c.bodyId)) ?? null, |
| 361 | 368 | nextStep: nextStepOf(states[i]?.claimed ?? null, beats[i]?.beat), |
| @@ -364,6 +371,20 @@ export async function clientRows(app: App, options: { probe?: boolean } = {}): P | ||
| 364 | 371 | ); |
| 365 | 372 | } |
| 366 | 373 | |
| 374 | /** | |
| 375 | * When each forge's link was last replaced, from the receipts in this forge's own events: one | |
| 376 | * query. It reads only `forge.claim_reissued` rows through the `event_kind_seq(kind, seq)` index, | |
| 377 | * and those are one per owner tap on Send again, so the scan is small and not per-forge. | |
| 378 | */ | |
| 379 | function linksReplaced(app: App): ReadonlyMap<string, string> { | |
| 380 | const rows = app.db | |
| 381 | .query<{ name: string | null; at: string }, []>( | |
| 382 | "SELECT json_extract(payload, '$.name') AS name, MAX(created_at) AS at FROM event WHERE kind = 'forge.claim_reissued' GROUP BY name", | |
| 383 | ) | |
| 384 | .all(); | |
| 385 | return new Map(rows.flatMap((r) => (r.name === null ? [] : [[r.name, r.at] as const]))); | |
| 386 | } | |
| 387 | ||
| 367 | 388 | type Month = NonNullable<ClientRow["month"]>; |
| 368 | 389 | interface Months { |
| 369 | 390 | readonly cache: Map<string, { at: number; value: Month | null }>; |
| @@ -602,13 +623,17 @@ export interface Seen { | ||
| 602 | 623 | readonly openedAt: string | null; |
| 603 | 624 | /** ISO of a person's last action there; null until they signed in. */ |
| 604 | 625 | readonly lastSeenAt: string | null; |
| 626 | /** | |
| 627 | * ISO of the last "send again", which ended the earlier link; null once claimed or when never. | |
| 628 | */ | |
| 629 | readonly replacedAt?: string | null; | |
| 605 | 630 | } |
| 606 | 631 | |
| 607 | 632 | /** Null while the forge is still being made or did not answer: nothing is claimed unknown. */ |
| 608 | export function seenOf(r: Pick<ClientRow, "made" | "claimed" | "sentAt" | "link" | "lastActivityAt">): Seen | null { | |
| 633 | export function seenOf(r: Pick<ClientRow, "made" | "claimed" | "sentAt" | "link" | "lastActivityAt" | "replacedAt">): Seen | null { | |
| 609 | 634 | if (!r.made || r.claimed === null || r.link === null) return null; |
| 610 | 635 | const openedAt = r.link.openedAt ?? null; |
| 611 | return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null }; | |
| 636 | return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null, replacedAt: r.claimed ? null : r.replacedAt ?? null }; | |
| 612 | 637 | } |
| 613 | 638 | |
| 614 | 639 | /** The one line as parts to join with " · ": the words, and the instant to say beside them. */ |
| @@ -616,6 +641,7 @@ export function seenParts(s: Seen): { words: string; at: string | null }[] { | ||
| 616 | 641 | return [ |
| 617 | 642 | ...(s.sentAt === null ? [] : [{ words: "sent", at: s.sentAt }]), |
| 618 | 643 | s.opened ? { words: s.openedAt === null ? "opened (time not noted)" : "opened", at: s.openedAt } : { words: "not opened yet", at: null }, |
| 644 | ...(s.replacedAt == null ? [] : [{ words: "link replaced", at: s.replacedAt }]), | |
| 619 | 645 | ...(s.lastSeenAt === null ? [] : [{ words: "last seen", at: s.lastSeenAt }]), |
| 620 | 646 | ]; |
| 621 | 647 | } |
test/client-link-replace.test.ts added +102
| @@ -0,0 +1,102 @@ | ||
| 1 | /** | |
| 2 | * The client list says when a forge's link was sent again (#13485): "link replaced <when>" beside | |
| 3 | * the row, read from the `forge.claim_reissued` receipt; the old link is refused, the new one | |
| 4 | * works, and a link already claimed is not replaced. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { childDirOf } from "../src/actions/spawn.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { createToken } from "../src/core/tokens.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { firstBoot } from "../src/spawn/first-boot.ts"; | |
| 17 | import { forgeRow } from "../src/web/pages/forges.ts"; | |
| 18 | import { forgeLines } from "../src/tui/screens/forges.ts"; | |
| 19 | import { clientRows, seenOf, seenParts } from "../src/spawn/clients.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | describe("client list: link replaced", () => { | |
| 25 | let dir = ""; | |
| 26 | let parent: App; | |
| 27 | let servers: Servers; | |
| 28 | let child: App; | |
| 29 | let kid: Servers; | |
| 30 | let token = ""; | |
| 31 | let cookie = ""; | |
| 32 | let first = ""; | |
| 33 | const tokenOf = (url: string) => url.split("#")[1] ?? ""; | |
| 34 | const claim = (t: string) => | |
| 35 | fetch(`${kid.url}/claim`, { method: "POST", headers: { origin: kid.url }, body: new URLSearchParams({ token: t, username: "someone", email: "[email protected]" }) }); | |
| 36 | ||
| 37 | beforeAll(async () => { | |
| 38 | dir = await mkdtemp(join(tmpdir(), "client-link-replace-")); | |
| 39 | parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 40 | const made = await createUser(parent.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-claim-passphrase", isAdmin: true }); | |
| 41 | if (!made.ok) throw new Error("user"); | |
| 42 | token = createToken(parent.db, made.value.id, "rw", "rw").token; | |
| 43 | cookie = `forge_session=${createSession(parent.db, made.value.id).token}`; | |
| 44 | servers = await serve(parent); | |
| 45 | const res = await fetch(`${servers.url}/api/v1/forges`, { | |
| 46 | method: "POST", | |
| 47 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, | |
| 48 | body: JSON.stringify({ name: "maria", for: "Maria at the bakery", dryRun: false }), | |
| 49 | }); | |
| 50 | if (res.status !== 201) throw new Error(`spawn ${res.status}`); | |
| 51 | const body = (await res.json()) as { claimUrl: string; bodyId: string }; | |
| 52 | first = body.claimUrl; | |
| 53 | child = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 }); | |
| 54 | await firstBoot(child); | |
| 55 | kid = await serve(child); | |
| 56 | }, 60_000); | |
| 57 | ||
| 58 | afterAll(async () => { | |
| 59 | kid.stop(); | |
| 60 | child.close(); | |
| 61 | servers.stop(); | |
| 62 | parent.close(); | |
| 63 | await rm(dir, { recursive: true, force: true }); | |
| 64 | }); | |
| 65 | ||
| 66 | test("before any send-again the row says nothing about a replaced link", async () => { | |
| 67 | const rows = await clientRows(parent, { probe: false }); | |
| 68 | expect(rows[0]!.replacedAt).toBeNull(); | |
| 69 | }); | |
| 70 | ||
| 71 | test("send again: old link refused, new link works, receipt read as 'link replaced'", async () => { | |
| 72 | const res = await fetch(`${servers.url}/forges/maria/claim-link`, { method: "POST", headers: { cookie, origin: "https://forge.test" } }); | |
| 73 | expect(res.status).toBe(201); | |
| 74 | const fresh = tokenOf(/https?:\/\/[^"<\s]*\/claim#[A-Za-z0-9_-]+/.exec(await res.text())?.[0] ?? ""); | |
| 75 | expect((await claim(tokenOf(first))).status).toBe(404); | |
| 76 | expect((await claim(fresh)).status).not.toBe(404); | |
| 77 | const rows = await clientRows(parent, { probe: false }); | |
| 78 | expect(rows[0]!.replacedAt).not.toBeNull(); | |
outdated
This hand-builds a |
||
| 79 | const seen = seenOf({ made: true, claimed: false, sentAt: rows[0]!.sentAt, link: { state: "open", revokedAt: null }, lastActivityAt: null, replacedAt: rows[0]!.replacedAt ?? null })!; | |
| 80 | expect(seenParts(seen).map((p) => p.words)).toContain("link replaced"); | |
| 81 | // The doors: the /forges row and the terminal row, for a forge that answered unclaimed. | |
| 82 | const row = { ...rows[0]!, made: true, claimed: false, link: { state: "open", revokedAt: null } } as typeof rows[0]; | |
| 83 | expect(String(forgeRow(row, Date.now()))).toContain("link replaced"); | |
| 84 | expect(forgeLines([row], true)[0]![1]).toContain("link replaced"); | |
| 85 | }); | |
| 86 | ||
| 87 | test("once claimed the part is dropped", () => { | |
| 88 | const row = { made: true, claimed: true, sentAt: "2026-01-01T00:00:00Z", link: { state: "used", revokedAt: null }, lastActivityAt: "2026-01-02T00:00:00Z", replacedAt: "2026-01-01T01:00:00Z" } as const; | |
| 89 | const words = seenParts(seenOf(row)!).map((p) => p.words); | |
| 90 | expect(words).toContain("last seen"); | |
| 91 | expect(words).not.toContain("link replaced"); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a claimed forge's link is not replaced", async () => { | |
| 95 | const made = await createUser(child.db, { origin: "seed", trusted: true, username: "claimer", email: "[email protected]", password: "a-long-claim-passphrase", isAdmin: true }); | |
| 96 | if (!made.ok) throw new Error("claim"); | |
| 97 | const res = await fetch(`${servers.url}/forges/maria/claim-link`, { method: "POST", headers: { cookie, origin: "https://forge.test" } }); | |
| 98 | expect(res.status).toBeGreaterThanOrEqual(400); | |
| 99 | const n = (parent.db.query("SELECT count(*) AS n FROM event WHERE kind = 'forge.claim_reissued'").get() as { n: number }).n; | |
| 100 | expect(n).toBe(1); | |
| 101 | }); | |
| 102 | }); |
new above
offline
Show 'link replaced' only while the forge is unclaimed. After a claim it is stale noise next to 'last seen'. Add a test.