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

The v1 proof can read the lapse rule: /pricing states the grace period and that a lapsed forge sleeps, in text a probe can match #12514

Merged opened this

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

Merged

scout/forge:scout/12513-the-v1-proof-can-read-the-lapse-rule-pri → main 6 commits fork 1 thread · 1 unresolved

  • Merged. scout/12513-the-v1-proof-can-read-the-lapse-rule-pri was merged into main as f21d9cf.

Review

Sign in to review this change.
8 files changed +70 −9
docs/features/plans.md +3 −1
@@ -16,7 +16,9 @@ These are the defaults. A forge admin who is a person edits them:
1616 - the MCP `limits` tool with `plans` (dry run first, validated the same as the real change);
1717 - the terminal: the Plans screen, `e` on a plan, `<field> <value>`, then a confirm.
1818
19 `GET /api/v1/plans` is public and answers the plans, no secrets. It is revalidated by ETag, so an edit shows at once.
19 `GET /api/v1/plans` is public and answers the plans, no secrets, with the lapse rule beside them: `graceDays` (the days a lapsed payment has, the `billing.grace_days` setting) and `lapse: "sleep"` (after the grace the forge sleeps and keeps its data). It is revalidated by ETag, so an edit shows at once.
20
21 `/pricing` says the same rule in one line, in text a probe can match: "If a payment lapses, you have 7 days of grace. After that your forge sleeps and keeps its data." The element carries `data-grace-days` with the number. The number is always the `billing.grace_days` setting (`src/billing/lapse_rule.ts`), never a second constant, so changing the setting changes the page and the API together.
2022
2123 ## What a spawn gets
2224
scripts/v1-proven.ts +11 −1
@@ -144,7 +144,17 @@ export const probes: readonly Probe[] = [
144144 const offers = (JSON.parse(m?.[1] ?? "{}") as { offers?: { description?: string }[] }).offers ?? [];
145145 expect(offers.length >= 2, `/pricing lists ${offers.length} plans, not two or more`);
146146 expect(offers.some((o) => /sleeps/i.test(o.description ?? "")), "no plan says it sleeps when idle");
147 return unproven(`${offers.length} plans are listed and one says it sleeps when idle; that the free plan sleeps rather than is deleted has no public read`);
147 // The lapse rule: the page's sentence and data attribute, and the plans read, agree.
148 const attr = /data-grace-days="(\d+)"/.exec(got.text);
149 expect(attr !== null, "/pricing states no days of grace (data-grace-days)");
150 const said = /you have (\d+) days? of grace\. After that your forge sleeps and keeps its data/i.exec(got.text);
151 expect(said !== null, "/pricing does not say that a lapsed forge sleeps and keeps its data after its grace");
152 expect(said?.[1] === attr?.[1], `/pricing says ${said?.[1]} days of grace in words and ${attr?.[1]} in its attribute`);
153 const api = await bodyOk(get, "/api/v1/plans");
154 const rule = JSON.parse(api.text) as { graceDays?: unknown; lapse?: unknown };
155 expect(rule.lapse === "sleep", `/api/v1/plans says lapse ${JSON.stringify(rule.lapse)}, not "sleep"`);
156 expect(String(rule.graceDays) === attr?.[1], `/pricing says ${attr?.[1]} days of grace, /api/v1/plans ${String(rule.graceDays)}`);
157 return `${offers.length} plans; a lapsed payment has ${attr?.[1]} days of grace, then the forge sleeps and keeps its data (page and API agree)`;
148158 },
149159 },
150160 {
src/billing/lapse_rule.ts added +18
@@ -0,0 +1,18 @@
1 /**
2 * The lapse rule, said in public (#12513): how many days of grace a lapsed
3 * payment has, and that the forge then sleeps and keeps its data. The number
4 * is the `billing.grace_days` setting that enforces it, read here, never a
5 * second constant. /pricing prints the sentence and `GET /api/v1/plans` carries
6 * the same two facts, so a reader (the v1 proof) can match them.
7 */
8
9 import type { Database } from "bun:sqlite";
10 import { limitValue } from "../settings/limits.ts";
11
12 export type LapseRule = { graceDays: number; lapse: "sleep" };
13
14 export const lapseRule = (db: Database): LapseRule => ({ graceDays: limitValue(db, "billing.grace_days"), lapse: "sleep" });
15
16 /** Two plain sentences, for customers. */
17 export const lapseRuleWords = (r: LapseRule): string =>
18 `If a payment lapses, you have ${r.graceDays} ${r.graceDays === 1 ? "day" : "days"} of grace. After that your forge sleeps and keeps its data.`;
src/contract/openapi/routes/getApiV1Plans.ts +1 −1
@@ -1,3 +1,3 @@
11 import type { Route } from "../shared.ts";
22
3 export const route: Route = { method: "get", path: "/api/v1/plans", summary: "The plans (free, small, medium, large by default): key, name, says, size, priceUsd a month, creditUsd a month, forges one person may hold; public, revalidated by ETag, no secrets", auth: false, role: "anonymous" };
3 export const route: Route = { method: "get", path: "/api/v1/plans", summary: "The plans (free, small, medium, large by default): key, name, says, size, priceUsd a month, creditUsd a month, forges one person may hold; beside them graceDays (days a lapsed payment has, the billing.grace_days setting) and lapse sleep (the forge then sleeps and keeps its data); public, revalidated by ETag, no secrets", auth: false, role: "anonymous" };

The feature how-to, docs/features/plans.md line 19, must state the new graceDays and lapse fields and the lapse sentence on /pricing. It does not yet.

src/http/api/forges.ts +2 −1
@@ -5,6 +5,7 @@
55 */
66
77 import { lendCredit } from "../../actions/child_credit.ts";
8 import { lapseRule } from "../../billing/lapse_rule.ts";
89 import { readPlans } from "../../actions/plans.ts";
910 import { listChildren } from "../../actions/spawn.ts";
1011 import { usdWords } from "../../spawn/credit.ts";
@@ -37,7 +38,7 @@ function publicTable(ctx: Ctx, phase: string, view: () => unknown): Response {
3738 /** The plans (`settings/plans.ts`), public. Revalidated on every read, so an edit shows at once. */
3839 function plansAnswer(ctx: Ctx): Response {
3940 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
40 const text = timePhase(ctx.request, "plans", () => JSON.stringify({ items: readPlans(ctx.app) }));
41 const text = timePhase(ctx.request, "plans", () => JSON.stringify({ items: readPlans(ctx.app), ...lapseRule(ctx.app.db) }));
4142 const etag = weakEtag(text);
4243 const headers = { etag, "cache-control": "public, no-cache" };
4344 if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers });
src/web/pages/pricing.ts +3
@@ -8,6 +8,7 @@
88
99 import { setCookie } from "../../auth/cookies.ts";
1010 import { deliverLink, mayView, NO_PROVIDER, placeOrder, reconcileOrder, resendLink, type Refusal } from "../../actions/order.ts";
11 import { lapseRule, lapseRuleWords } from "../../billing/lapse_rule.ts";
1112 import { checkoutOf } from "../../billing/checkout.ts";
1213 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
1314 import { cookieValue, type Ctx } from "../../http/context.ts";
@@ -43,6 +44,7 @@ function plansView(ctx: Ctx, values: Values, errors: Partial<Record<Refusal["fie
4344 const ordered = /^ord_[0-9a-f]{32}$/.test(held) ? held : undefined;
4445 const selling = checkoutOf(ctx.app) !== undefined;
4546 const plans = getPlans(ctx.app.db);
47 const rule = lapseRule(ctx.app.db);
4648 const rows = plans.map((p: Plan) => {
4749 const free = p.key === FREE;
4850 const paid = !free && selling;
@@ -82,6 +84,7 @@ function plansView(ctx: Ctx, values: Values, errors: Partial<Record<Refusal["fie
8284 : null}
8385 <ul class="list setting-list">${rows}</ul>
8486 </form>
87 <p class="muted small" data-lapse="sleep" data-grace-days="${rule.graceDays}">${lapseRuleWords(rule)}</p>
8588 <p class="muted small">Paid already? ${ordered === undefined ? null : html`<a href="/pricing/done?order=${ordered}">Open your order</a> · `}<a href="/pricing/resend">Lost your link?</a></p>
8689 </div>`;
8790 }
test/pricing-order.test.ts +20
@@ -1,4 +1,5 @@
11 /** /pricing (#11138): order -> paid -> spawned -> claim link; duplicate delivery makes one forge; a failed spawn raises the alarm. */
2 import { limitValue, setLimit } from "../src/settings/limits.ts";
23 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
34 import { mkdtemp, rm } from "node:fs/promises";
45 import { tmpdir } from "node:os";
@@ -75,6 +76,25 @@ async function buy(name: string, plan = "small") {
7576 const completed = (o: { id: string; session_id: string }) => ({ id: o.session_id, metadata: { order: o.id }, customer: "cus_1", subscription: "sub_1", payment_status: "paid" });
7677
7778 describe("/pricing", () => {
79 test("says the lapse rule from the setting, and /api/v1/plans carries the same two facts (#12513)", async () => {
80 const read = async () => {
81 const text = await (await fetch(`${base}/pricing`)).text();
82 const api = (await (await fetch(`${base}/api/v1/plans`)).json()) as { graceDays: number; lapse: string };
83 return { text, api };
84 };
85 const first = await read();
86 const days = limitValue(app.db, "billing.grace_days");
87 expect(first.api).toMatchObject({ graceDays: days, lapse: "sleep" });
88 expect(first.text).toContain(`data-grace-days="${days}"`);
89 expect(first.text).toContain(`you have ${days} ${days === 1 ? "day" : "days"} of grace. After that your forge sleeps and keeps its data.`);
90 setLimit(app.db, "billing.grace_days", 11, admin.id);
91 const next = await read();
92 expect(next.api.graceDays).toBe(11);
93 expect(next.text).toContain('data-grace-days="11"');
94 expect(next.text).toContain("you have 11 days of grace");
95 setLimit(app.db, "billing.grace_days", days, admin.id);
96 });
97
7898 test("shows the plans with sizes and prices to a stranger, and a way to the free path", async () => {
7999 const res = await fetch(`${base}/pricing`);
80100 expect(res.status).toBe(200);
test/v1-proven.test.ts +12 −5
@@ -10,6 +10,8 @@ const HEADERS = {
1010 "cross-origin-opener-policy": "same-origin",
1111 };
1212 const ld = JSON.stringify({ offers: [{ description: "A small forge that sleeps when idle" }, { description: "Small, awake" }] });
13 const rule = (days: number, words = days) =>
14 `<p data-lapse="sleep" data-grace-days="${days}">If a payment lapses, you have ${words} days of grace. After that your forge sleeps and keeps its data.</p>`;
1315 const health = (schedules?: unknown) =>
1416 Response.json({ ok: true, version: "1.0.0", commit: "058b6cf", draining: false, ...(schedules === undefined ? {} : { schedules }) });
1517
@@ -22,7 +24,8 @@ function stub(over: Over = {}, seen: string[] = []): Fetch {
2224 "/llms.txt": () => new Response("Running dev at 058b6cf\nPress Start my forge."),
2325 "/api/v1/version": () => Response.json({ version: "1.0.0" }),
2426 "/pricing": () =>
25 new Response(`<form method="post" action="/pricing/order"></form><script type="application/ld+json">${ld}</script>`),
27 new Response(`<form method="post" action="/pricing/order"></form><script type="application/ld+json">${ld}</script>${rule(7)}`),
28 "/api/v1/plans": () => Response.json({ items: [], graceDays: 7, lapse: "sleep" }),
2629 "/settings/billing": () => new Response(null, { status: 303, headers: { location: "/signin?next=%2Fsettings%2Fbilling" } }),
2730 "/settings/crew": () => new Response(null, { status: 303, headers: { location: "/signin?next=%2Fsettings%2Fcrew" } }),
2831 "/api/v1/health": () => health({ overdue: 0, oldestSeconds: 0 }),
@@ -44,7 +47,7 @@ describe("v1-proven", () => {
4447 expect(probes.length).toBe(12);
4548 const status = (n: number) => results.find((r) => r.issue === n)?.status;
4649 // What no public read can show is unproven, never a pass.
47 expect(results.filter((r) => r.status === "unproven").map((r) => r.issue).sort()).toEqual([11140, 11141]);
50 expect(results.filter((r) => r.status === "unproven").map((r) => r.issue).sort()).toEqual([11141]);
4851 expect(results.filter((r) => r.status === "fail")).toEqual([]);
4952 expect(status(11595)).toBe("pass");
5053 expect(line(results[0]!)).toStartWith("pass #11541");
@@ -67,6 +70,10 @@ describe("v1-proven", () => {
6770 [11507, { "/llms.txt": () => new Response("Start my forge") }, [11507]],
6871 [11138, { "/pricing": () => new Response("<p>none</p>") }, [11138, 11140]],
6972 [11140, { "/pricing": () => new Response('<form method="post" action="/pricing/order"></form><script type="application/ld+json">{"offers":[]}</script>') }, [11140]],
73 [11140, { "/pricing": () => new Response(`<form method="post" action="/pricing/order"></form><script type="application/ld+json">${ld}</script>`) }, [11140]],
74 [11140, { "/api/v1/plans": () => Response.json({ items: [], graceDays: 3, lapse: "sleep" }) }, [11140]],
75 [11140, { "/api/v1/plans": () => Response.json({ items: [] }) }, [11140]],
76 [11140, { "/pricing": () => new Response(`<form method="post" action="/pricing/order"></form><script type="application/ld+json">${ld}</script>${rule(7, 5)}`) }, [11140]],
7077 [11141, { "/settings/billing": () => new Response("x", { status: 404 }) }, [11141]],
7178 [11515, { "/settings/crew": () => new Response("x", { status: 500 }) }, [11515]],
7279 [11595, { "/api/v1/health": () => health({ overdue: 2, oldestSeconds: 86_400 }) }, [11595]],
@@ -119,12 +126,12 @@ describe("v1-proven", () => {
119126 });
120127
121128 test("the script exits 3, not 0, when nothing fails but some items are unproven", async () => {
122 // Only the two no-public-read items are unproven.
129 // Only the no-public-read item is unproven.
123130 const { code, out } = await run(stub());
124131 expect(out).not.toContain("fail #");
125132 expect(code).toBe(3);
126 expect(out).toContain("unproven #11140");
127 expect(out).toContain("v1.0.0 is not proven: none fail, 2 of 12 unproven");
133 expect(out).toContain("unproven #11141");
134 expect(out).toContain("v1.0.0 is not proven: none fail, 1 of 12 unproven");
128135 });
129136
130137 test("the script exits 2 on bad usage", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs