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

The owner resends a client's forge link from the client list: the old link stops working and a receipt names the new one's expiry #15706

Merged opened this

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

Merged

scout/forge:scout/15655-the-owner-resends-a-client-s-forge-link → main 7 commits fork 2 threads · 2 unresolved

  • Merged. scout/15655-the-owner-resends-a-client-s-forge-link was merged into main as 8643bcb.

Review

Sign in to review this change.
13 files changed +155 −26
src/http/mcp/tools/forges.ts +13 −2
@@ -10,13 +10,14 @@ import { CHILD_NAME } from "../../../spawn/placement.ts";
1010 import { checkMove } from "../../../spawn/move_check.ts";
1111 import { moveToCell } from "../../../spawn/move_cell.ts";
1212 import { resizeForge } from "../../../spawn/manage.ts";
13 import { good, bad, type Tool } from "../shared.ts";
13 import { previewDoor, reissueDoor } from "../../../spawn/door.ts";
14 import { good, bad, type Tool, str, LINK_REVEALED } from "../shared.ts";
1415
1516 const sentSays = (n: number) => (n === 0 ? "This forge has sent no forges yet." : `${n} forge${n === 1 ? "" : "s"} sent from here.`);
1617
1718 export const tool: Tool = {
1819 name: "forges",
19 doc: "an admin's list of the forges sent from here: name, address, whom for, apps, claimed, stage (sent/opened/signed in/last tell; resend true when unopened 3 days: claim_link), last activity (a person or the crew, never a heartbeat), who claimed it and when, kept (its newest backup's age, or never kept), keep cost, archive reason, reachable, size, use, madeFrom; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large) or, with `plan`, grants it by hand; `size` workshop raises a forge; `{ forge, sandbox: true }` reads what moving a forge out of this forge's own process into a sandbox would meet (hooks, command-running keys, size) and with `dryRun: false` moves it, proven and put back on any failure (`acknowledge: true` when it was held for what its repositories hold); each forge carries `placement` and `where`; gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false; `archive` a name or names stops each, writes its data and ledger as one file to the backup destination, then removes it; `restore` a name brings an archived one back; `kind` refused lists the last 20 refused first asks; `kind` clients lists each client in the page's words",
20 doc: "an admin's list of the forges sent from here: name, address, whom for, apps, claimed, stage (sent/opened/signed in/last tell; resend true when unopened 3 days: claim_link), last activity (a person or the crew, never a heartbeat), who claimed it and when, kept (its newest backup's age, or never kept), keep cost, archive reason, reachable, size, use, madeFrom; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large) or, with `plan`, grants it by hand; `size` workshop raises a forge; `{ forge, sandbox: true }` reads what moving a forge out of this forge's own process into a sandbox would meet (hooks, command-running keys, size) and with `dryRun: false` moves it, proven and put back on any failure (`acknowledge: true` when it was held for what its repositories hold); each forge carries `placement` and `where`; gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false; `archive` a name or names stops each, writes its data and ledger as one file to the backup destination, then removes it; `restore` a name brings an archived one back; `kind` refused lists the last 20 refused first asks; `kind` clients lists each client in the page's words; `action` resend + `forge`: previews, with `dryRun: false` replaces its link",
2021 description:
2122 "Forges sent from here (admin), clients, a forge plan or size.",
2223 inputSchema: { type: "object", properties: { kind: { type: "string", enum: ["refused", "clients"] }, forge: { type: "string" }, plan: { type: "string" }, codes: { type: "boolean" }, code: { type: "object" }, revoke: { type: "string" }, archive: { type: ["string", "array"], items: { type: "string" } }, restore: { type: "string" }, sandbox: { type: "boolean" }, size: { type: "string" }, acknowledge: { type: "boolean" }, dryRun: { type: "boolean" } } },
@@ -26,6 +27,16 @@ export const tool: Tool = {
2627 async run(ctx, user, args) {
2728 if (!user.isAdmin)
2829 return bad("forbidden", "only an admin of this forge sees the forges it sent");
30 if (args["action"] !== undefined) {
31 if (args["action"] !== "resend") return bad("invalid", "action is resend");
32 const name = str(args["forge"]);
33 if (args["dryRun"] !== false) {
34 const seen = await previewDoor(ctx, user, name);
35 return seen.status < 400 ? good({ ...seen.body, says: `${String(seen.body["says"])} Nothing changed yet. Send dryRun: false to do it.`, reissued: false }) : bad(String(seen.body["error"]), String(seen.body["message"]));
36 }
37 const door = await reissueDoor(ctx, user, name);
38 return door.status >= 400 ? bad(String(door.body["error"]), String(door.body["message"])) : good({ says: LINK_REVEALED, ...door.body });
39 }
2940 if (args["kind"] !== undefined) {
3041 if (args["kind"] === "clients") {
3142 const lines = clientLines(await clientRows(ctx.app));
src/spawn/claim.ts +20 −2
@@ -56,11 +56,15 @@ interface Stored {
5656 readonly r?: string;
5757 /** When a browser first opened the claim page, ISO; absent until one did. */
5858 readonly o?: string;
59 /**
60 * sha256 of the link this one replaced (sent again), so the old link can say so; never the token.
61 */
62 readonly p?: string;
5963 }
6064
6165 export type ClaimState =
6266 | { readonly state: "ok"; readonly orgId: string }
63 | { readonly state: "used" | "expired" | "revoked" | "unknown" | "owned" };
67 | { readonly state: "used" | "expired" | "revoked" | "unknown" | "owned" | "replaced" };
6468
6569 /** What a spawned child's `parent.json` says, when it has one. */
6670 export interface Spawned {
@@ -159,10 +163,12 @@ export function mintClaim(
159163 now = Date.now(),
160164 ): string {
161165 const token = newToken();
166 const earlier = readStored(db, orgId)?.stored;
162167 const value: Stored = {
163168 h: sha256(token),
164169 e: new Date(now + CLAIM_TTL_MS).toISOString(),
165170 u: null,
171 ...(earlier === undefined ? {} : { p: earlier.h }),
166172 };
167173 db.query(
168174 `INSERT INTO user_setting (user_id, key, value) VALUES (?1, ?2, ?3)
@@ -201,6 +207,18 @@ function find(
201207 return stored.h === sha256(token) ? { raw: row.value, stored } : undefined;
202208 }
203209
210 /** Whether `token` is the link the current one replaced. */
211 function replacedBy(db: Database, orgId: string, token: string): boolean {
212 if (!TOKEN.test(token)) return false;
213 const now = db.query<{ value: string }, [string, string]>("SELECT value FROM user_setting WHERE user_id = ?1 AND key = ?2").get(orgId, KEY);
214 if (!now) return false;
215 try {
216 return (JSON.parse(now.value) as Stored).p === sha256(token);
217 } catch {
218 return false;
219 }
220 }
221
204222 function judge(
205223 db: Database,
206224 orgId: string,
@@ -208,7 +226,7 @@ function judge(
208226 now: number,
209227 ): { state: ClaimState; raw?: string; stored?: Stored } {
210228 const found = find(db, orgId, token);
211 if (found === undefined) return { state: { state: "unknown" } };
229 if (found === undefined) return { state: { state: replacedBy(db, orgId, token) ? "replaced" : "unknown" } };
212230 if (found.stored.u !== null) return { state: { state: "used" } };
213231 if (found.stored.r !== undefined) return { state: { state: "revoked" } };
214232 if (Date.parse(found.stored.e) <= now) return { state: { state: "expired" } };
src/spawn/clients.ts +4 −1
@@ -737,13 +737,15 @@ export interface Seen {
737737 * ISO of the last "send again", which ended the earlier link; null once claimed or when never.
738738 */
739739 readonly replacedAt?: string | null;
740 /** When the link now standing stops working, ISO, said beside a replaced link; null otherwise. */
741 readonly expiresAt?: string | null;
740742 }
741743
742744 /** Null while the forge is still being made or did not answer: nothing is claimed unknown. */
743745 export function seenOf(r: Pick<ClientRow, "made" | "claimed" | "sentAt" | "firstTellAt" | "link" | "lastActivityAt" | "replacedAt">): Seen | null {
744746 if (!r.made || r.claimed === null || r.link === null) return null;
745747 const openedAt = r.link.openedAt ?? null;
746 return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null, firstTellAt: r.firstTellAt, replacedAt: r.claimed ? null : r.replacedAt ?? null };
748 return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null, firstTellAt: r.firstTellAt, replacedAt: r.claimed ? null : r.replacedAt ?? null, expiresAt: !r.claimed && r.replacedAt != null && r.link.state === "open" ? r.link.expiresAt ?? null : null };
747749 }
748750
749751 /** The one line as parts to join with " · ": the words, and the instant to say beside them. */
@@ -754,6 +756,7 @@ export function seenParts(s: Seen): { words: string; at: string | null }[] {
754756 // Nothing while unknown (undefined); "not yet" only once the child has said there is none.
755757 ...(s.firstTellAt === undefined ? [] : [s.firstTellAt === null ? { words: "first tell: not yet", at: null } : { words: "first tell", at: s.firstTellAt }]),
756758 ...(s.replacedAt == null ? [] : [{ words: "link replaced", at: s.replacedAt }]),
759 ...(s.expiresAt == null ? [] : [{ words: "new link works until", at: s.expiresAt }]),
757760 ...(s.lastSeenAt === null ? [] : [{ words: "last seen", at: s.lastSeenAt }]),
758761 ];
759762 }
src/web/pages/claim.ts +6
@@ -169,6 +169,11 @@ function dead(
169169 title: "This link was turned off",
170170 text: LINK_TURNED_OFF,
171171 },
172 replaced: {
173 status: 410,
174 title: "This link has been replaced",
175 text: `A newer link was sent for this forge. Use the newest one, or ask ${ask} to send it again.`,
176 },
172177 owned: {
173178 status: 410,
174179 title: "This forge already has an owner",
@@ -492,6 +497,7 @@ const LINK_REFUSED: Record<Exclude<ClaimState["state"], "ok">, string> = {
492497 expired: "This link has expired.",
493498 revoked: LINK_TURNED_OFF,
494499 owned: "This forge already has an owner.",
500 replaced: "This link has been replaced by a newer one.",
495501 };
496502
497503 // POST /claim/options, /welcome/options — a passkey challenge, for a live link and a fit name.
src/web/pages/forges.ts +2 −4
@@ -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, daysLeftWords, 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, STALE_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";
@@ -157,9 +157,7 @@ export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = f
157157 : html`<span data-attention="${r.attention}">${badge(ATTENTION_WORDS[r.attention], { tone: "attn" })}</span>`}${badge(CLAIM_WORDS[state], {
158158 ...(state === "yes" ? { tone: "success" as const } : { variant: "outline" as const }),
159159 })}${state === "no"
160 ? daysLeft !== null && r.link?.state === "open"
161 ? 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>`
162 : 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" })
160 ? btn("Resend link", { size: "sm", href: `/forges/${r.name}/claim-link`, title: r.stage?.resend ? `${STALE_WORDS}: send it again. See who it is for and what stops working, then confirm` : "See who it is for and what stops working, then confirm" })
163161 : null}${state === "yes" && r.made
164162 ? 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" })
165163 : null}${r.link === null
test/client-list.test.ts +3 −3
@@ -214,14 +214,14 @@ describe("the client list", () => {
214214 expect(maria).toContain('href="https://maria--forge.test"');
215215 expect(maria).toMatch(/data-claimed="no"/);
216216 // 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/);
217 expect(maria).toMatch(/href="\/forges\/maria\/claim-link"[^>]*>[\s\S]*?Resend link/);
218218 expect(maria).toMatch(/data-days-left[^>]*>(\d+ days? left|expires today)</);
219219 expect(html).not.toContain("forges-sent");
220220 const lou = html.slice(html.indexOf('data-forge="lou"'));
221221 expect(lou).toMatch(/data-claimed="unknown"/);
222222 expect(html).not.toContain("claim-link-box");
223223 expect(maria).toContain('class="row"');
224 expect(maria).toContain("Renew link");
224 expect(maria).toContain("Resend link");
225225 expect(lou).toContain("it did not answer just now");
226226 // It did not answer, so it carries the one attention badge and leads the list.
227227 expect(lou).toContain('data-attention="silent"');
@@ -267,7 +267,7 @@ describe("the client list", () => {
267267 test("an unclaimed row offers a fresh link: it replaces the old one, shown once; only an admin may ask", async () => {
268268 const html = (await (await list(admin)).text()).replace(/\s+/g, " ");
269269 const maria = html.slice(html.indexOf('data-forge="maria"'), html.indexOf("</li>", html.indexOf('data-forge="maria"')));
270 expect(maria).toContain('action="/forges/maria/claim-link"');
270 expect(maria).toContain('href="/forges/maria/claim-link"');
271271 const lou = html.slice(html.indexOf('data-forge="lou"'), html.indexOf("</li>", html.indexOf('data-forge="lou"')));
272272 // Whether lou was claimed is unknown, so it offers nothing to send.
273273 expect(lou).not.toContain("claim-link");
test/client-states.test.ts +2 −2
@@ -70,14 +70,14 @@ describe("client states", () => {
7070 // The stage's "Sent" is folded into the one seen line (#12694), not said twice.
7171 expect(fresh).toContain("data-seen");
7272 expect(fresh).not.toContain(`>${STAGE_WORDS.sent} `);
73 expect(fresh).not.toContain("Resend");
73 expect(fresh).toContain("Resend link");
7474 const stale = String(forgeRow(stage({ sentAt: ago(4 * DAY) }), NOW));
7575 expect(stale).toContain(STALE_WORDS);
7676 expect(stale).toContain("Resend");
7777 expect(stale).toContain("/forges/acme/claim-link");
7878 // The row's button opens the preview (a link), not a form that issues at once (#13166).
7979 expect(stale).not.toContain('method="post" action="/forges/acme/claim-link"');
80 expect(String(forgeRow(stage(), NOW))).toContain("Send a new link");
80 expect(String(forgeRow(stage(), NOW))).toContain("Resend link");
8181 });
8282
8383 test("the terminal line carries the same words, and resend", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs