Public: anyone can read this; the crew can change it.
The operator sees a forge's refused first asks: the last 20 on /forges, with the plain reason #11538
scout/forge:scout/11526-the-operator-sees-a-forge-s-refused-firs → main 6 commits fork
5 files
changed +122
−2
docs/features/first-ask.md added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | # First ask | |
| 2 | ||
| 3 | A first ask that is refused (empty, too long, nobody signed in, unverified, and | |
| 4 | so on) leaves one row: who asked, the first 200 characters, and the refusal's | |
| 5 | words. Never the email or a claim code. | |
| 6 | ||
| 7 | - **Where it is read:** an admin sees the last 20 under "Refused first asks" on | |
| 8 | `/forges`, and through the MCP `forges` tool with `kind: "refused"`. | |
| 9 | - **Caps:** at most 20 rows per person per hour (the next is dropped), kept 30 | |
| 10 | days, pruned on write. | |
| 11 | - The ask is shown as escaped text; a non-admin gets no section. |
src/core/first_ask_refusals.ts +20
| @@ -50,3 +50,23 @@ export function listRefusals(db: Database, limit: number, now: number): Refusal[ | ||
| 50 | 50 | .all(n, now - KEEP_MS) as { id: number; at: number; actor_id: string | null; ask: string; words: string }[]; |
| 51 | 51 | return rows.map((r) => ({ id: r.id, at: r.at, actorId: r.actor_id, ask: r.ask, words: r.words })); |
| 52 | 52 | } |
| 53 | ||
| 54 | export interface RefusalRow { | |
| 55 | readonly at: number; | |
| 56 | /** The username, or "nobody signed in". Never an email. */ | |
| 57 | readonly who: string; | |
| 58 | readonly ask: string; | |
| 59 | readonly words: string; | |
| 60 | } | |
| 61 | ||
| 62 | /** The newest `limit` refusals with the asker's username: one indexed read, one join. */ | |
| 63 | export function refusalRows(db: Database, limit: number, now: number): RefusalRow[] { | |
| 64 | const n = Math.min(500, Math.max(1, Math.trunc(limit) || 1)); | |
| 65 | const rows = db | |
| 66 | .query( | |
| 67 | `SELECT r.at, u.username AS who, r.ask, r.words FROM first_ask_refusals r | |
| 68 | LEFT JOIN user u ON u.id = r.actor_id WHERE r.at >= ?2 ORDER BY r.at DESC, r.id DESC LIMIT ?1`, | |
| 69 | ) | |
| 70 | .all(n, now - KEEP_MS) as { at: number; who: string | null; ask: string; words: string }[]; | |
| 71 | return rows.map((r) => ({ at: r.at, who: r.who ?? "nobody signed in", ask: r.ask, words: r.words })); | |
| 72 | } |
src/http/mcp/tools/forges.ts +7 −2
| @@ -2,15 +2,16 @@ import { BILLING_WORDS, grantPlan, planGrant, readPlan } from "../../../actions/ | ||
| 2 | 2 | import { CODE_ERRORS, makeCode, planCode, readCodes, revoke } from "../../../actions/codes.ts"; |
| 3 | 3 | import { publicOrigin } from "../../../core/public_origin.ts"; |
| 4 | 4 | import { scopeAllows } from "../../../core/tokens.ts"; |
| 5 | import { refusalRows } from "../../../core/first_ask_refusals.ts"; | |
| 5 | 6 | import { clientRows } from "../../../spawn/clients.ts"; |
| 6 | 7 | import { good, bad, type Tool } from "../shared.ts"; |
| 7 | 8 | |
| 8 | 9 | export const tool: Tool = { |
| 9 | 10 | name: "forges", |
| 10 | 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, reachable, size, use; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large, status, source, changes) or, with `plan`, grants it by hand; gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false", | |
| 11 | 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, reachable, size, use; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large, status, source, changes) or, with `plan`, grants it by hand; `kind: refused` lists the last 20 refused first asks (when, who, ask, reason); gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false", | |
| 11 | 12 | description: |
| 12 | 13 | "Forges sent from here (admin; a stage says when to resend), or one forge's plan to read or grant.", |
| 13 | inputSchema: { type: "object", properties: { forge: { type: "string" }, plan: { type: "string" }, codes: { type: "boolean" }, code: { type: "object" }, revoke: { type: "string" }, dryRun: { type: "boolean" } } }, | |
| 14 | inputSchema: { type: "object", properties: { kind: { type: "string" }, forge: { type: "string" }, plan: { type: "string" }, codes: { type: "boolean" }, code: { type: "object" }, revoke: { type: "string" }, dryRun: { type: "boolean" } } }, | |
| 14 | 15 | writes: true, |
| 15 | 16 | dryByDefault: true, |
| 16 | 17 | readsWithout: ["forge", "plan", "code", "revoke"], |
| @@ -52,6 +53,10 @@ export const tool: Tool = { | ||
| 52 | 53 | const set = grantPlan(ctx.app, user, args["forge"], args["plan"]); |
| 53 | 54 | return set.ok ? good(set.value) : refuse(set.error); |
| 54 | 55 | } |
| 56 | if (args["kind"] === "refused") { | |
| 57 | const refused = refusalRows(ctx.app.db, 20, Date.now()).map((r) => ({ at: new Date(r.at).toISOString(), who: r.who, ask: r.ask, reason: r.words })); | |
| 58 | return good({ says: refused.length === 0 ? "No first ask was refused lately." : `${refused.length} refused first ask${refused.length === 1 ? "" : "s"}, newest first.`, refused }); | |
| 59 | } | |
| 55 | 60 | const forges = await clientRows(ctx.app); |
| 56 | 61 | return good({ |
| 57 | 62 | says: |
src/web/pages/forges.ts +15
| @@ -22,6 +22,7 @@ import { diskWords } from "../../spawn/disk_mount.ts"; | ||
| 22 | 22 | import { listChildren } from "../../actions/spawn.ts"; |
| 23 | 23 | import { ownCode, removeForge, stopForge, type Managed } from "../../spawn/manage.ts"; |
| 24 | 24 | import { readOwnCode } from "../../spawn/own_code.ts"; |
| 25 | import { refusalRows } from "../../core/first_ask_refusals.ts"; | |
| 25 | 26 | import { claimLinkBody } from "./new-forge.ts"; |
| 26 | 27 | |
| 27 | 28 | |
| @@ -141,6 +142,19 @@ export function sideBySide(rows: readonly ClientRow[]): Html { | ||
| 141 | 142 | </div>`; |
| 142 | 143 | } |
| 143 | 144 | |
| 145 | /** The last 20 refused first asks: when, who, the ask as text, the reason. */ | |
| 146 | export function refusedSection(ctx: Ctx): Html { | |
| 147 | const rows = refusalRows(ctx.app.db, 20, Date.now()); | |
| 148 | return html`<section class="settings-block" id="refused-first-asks"> | |
| 149 | <h2>Refused first asks</h2> | |
| 150 | ${rows.length === 0 | |
| 151 | ? html`<p class="muted">None refused lately.</p>` | |
| 152 | : html`<ul class="list setting-list"> | |
| 153 | ${rows.map((r) => html`<li data-refused><span class="muted small">${time(new Date(r.at).toISOString())} · ${r.who}</span><br />${r.ask}<br /><span class="muted small">${r.words}</span></li>`)} | |
| 154 | </ul>`} | |
| 155 | </section>`; | |
| 156 | } | |
| 157 | ||
| 144 | 158 | // GET /forges — an admin's only. |
| 145 | 159 | export async function forgesPage(ctx: Ctx): Promise<Response> { |
| 146 | 160 | const user = ctx.auth.user; |
| @@ -179,6 +193,7 @@ export async function forgesPage(ctx: Ctx): Promise<Response> { | ||
| 179 | 193 | </ul> |
| 180 | 194 | </section>`} |
| 181 | 195 | ${await ownCodeSection(ctx)} |
| 196 | ${refusedSection(ctx)} | |
| 182 | 197 | <p class="mt"><a href="/fleet/graph">Fleet graph</a></p> |
| 183 | 198 | </div>`, |
| 184 | 199 | }); |
test/forges-refused.test.ts added +69
| @@ -0,0 +1,69 @@ | ||
| 1 | /** Refused first asks are read on /forges and by the MCP forges tool (#11526). */ | |
| 2 | ||
| 3 | import { afterEach, beforeEach, expect, test } from "bun:test"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { recordRefusal } from "../src/core/first_ask_refusals.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { refusedSection } from "../src/web/pages/forges.ts"; | |
| 11 | import { tool } from "../src/http/mcp/tools/forges.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | let dir = ""; | |
| 16 | let app: App; | |
| 17 | beforeEach(async () => { | |
| 18 | dir = await mkdtemp(join(tmpdir(), "forge-refused-")); | |
| 19 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 20 | }); | |
| 21 | afterEach(async () => { | |
| 22 | app.db.close(); | |
| 23 | await rm(dir, { recursive: true, force: true }); | |
| 24 | }); | |
| 25 | ||
| 26 | const ctx = () => ({ app, request: new Request("https://forge.test/forges"), params: {}, url: new URL("https://forge.test/forges") }) as never; | |
| 27 | ||
| 28 | async function seed() { | |
| 29 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "maria", email: "[email protected]", password: "a-long-refusal-passphrase", isAdmin: true }); | |
| 30 | if (!u.ok) throw new Error("user"); | |
| 31 | recordRefusal(app.db, { actorId: u.value.id, ask: "<script>alert(1)</script>", words: "Say something." }, Date.now()); | |
| 32 | recordRefusal(app.db, { actorId: null, ask: "a menu", words: "Sign in first." }, Date.now()); | |
| 33 | return u.value; | |
| 34 | } | |
| 35 | ||
| 36 | test("the section lists the rows, markup stays text, no email", async () => { | |
| 37 | await seed(); | |
| 38 | const out = String(refusedSection(ctx())); | |
| 39 | expect(out).toContain("Refused first asks"); | |
| 40 | expect(out).toContain("<script>"); | |
| 41 | expect(out).not.toContain("<script>"); | |
| 42 | expect(out).toContain("maria"); | |
| 43 | expect(out).toContain("nobody signed in"); | |
| 44 | expect(out).toContain("Sign in first."); | |
| 45 | expect(out).not.toContain("[email protected]"); | |
| 46 | }); | |
| 47 | ||
| 48 | test("empty state is one quiet line", () => { | |
| 49 | expect(String(refusedSection(ctx()))).toContain("None refused lately."); | |
| 50 | }); | |
| 51 | ||
| 52 | test("MCP: an admin gets the rows under kind refused, a non-admin is refused", async () => { | |
| 53 | const admin = await seed(); | |
| 54 | const c = { app, url: new URL("https://forge.test/mcp"), auth: { scope: "admin" } } as never; | |
| 55 | const res = JSON.stringify(await tool.run(c, admin, { kind: "refused" })); | |
| 56 | expect(res).toContain("Say something."); | |
| 57 | expect(res).not.toContain("[email protected]"); | |
| 58 | const other = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-long-refusal-passphrase" }); | |
| 59 | if (!other.ok) throw new Error("user"); | |
| 60 | const no = JSON.stringify(await tool.run(c, other.value, { kind: "refused" })); | |
| 61 | expect(no).toContain("forbidden"); | |
| 62 | expect(no).not.toContain("Say something."); | |
| 63 | }); | |
| 64 | ||
| 65 | test("a non-admin gets no section: /forges is not found", async () => { | |
| 66 | const { forgesPage } = await import("../src/web/pages/forges.ts"); | |
| 67 | const res = await forgesPage({ app, request: new Request("https://forge.test/forges"), auth: { user: undefined }, params: {}, url: new URL("https://forge.test/forges") } as never); | |
| 68 | expect(res.status).toBe(404); | |
| 69 | }); |
new above
offline