Public: anyone can read this; the crew can change it.
A refused first ask leaves a receipt: the person's words, the plain reason, nothing made #11523
scout/forge:scout/11462-a-refused-first-ask-leaves-a-receipt-the → main 4 commits fork
3 files
changed +87
−8
docs/features/first-ask.md added +9
| @@ -0,0 +1,9 @@ | ||
| 1 | # A refused first ask leaves a receipt | |
| 2 | ||
| 3 | When a first ask (`firstAsk`, `src/actions/first_ask.ts`) is refused — empty, too long, the asker not verified or not a person, a bad `from`, the forge's own repository, a name taken or any other reason no app could be made — one `first_ask.refused` event is written. It holds the actor, the first 200 characters of the ask, the refusal's words and `made: false`. It never holds a secret or a claim code. No app is made. | |
| 4 | ||
| 5 | The page shows the same words as before. A caller of `firstAsk` gets the event id as `error.receipt`. | |
| 6 | ||
| 7 | Read them from the event table: `SELECT created_at, actor_id, payload FROM event WHERE kind = 'first_ask.refused'`, or `listEvents(db, { kind: "first_ask.refused" })`. They carry no repository, so the activity feed does not show them. A success leaves none. | |
| 8 | ||
| 9 | Cap: 20 receipts per actor per hour. Past it a refusal still refuses, writes nothing and `receipt` is null. |
src/actions/first_ask.ts +37 −8
| @@ -30,7 +30,18 @@ export interface FirstAsk { | ||
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** What a failed first ask says, in words: the empty sentence, or why no app could be made. */ |
| 33 | export type FirstAskError = { readonly words: string }; | |
| 33 | export type FirstAskError = { | |
| 34 | readonly words: string; | |
| 35 | /** The receipt this refusal left (an event id); null once the actor's hourly cap of receipts is spent. */ | |
| 36 | readonly receipt: string | null; | |
| 37 | }; | |
| 38 | ||
| 39 | /** The event kind of a refused first ask; the receipt is read from the event table by this kind. */ | |
| 40 | export const FIRST_ASK_REFUSED = "first_ask.refused"; | |
| 41 | /** Receipts one actor can leave per hour; a refusal past it still refuses, and leaves nothing. */ | |
| 42 | export const REFUSAL_RECEIPTS_PER_HOUR = 20; | |
| 43 | /** The longest cut of the ask a receipt keeps. */ | |
| 44 | export const RECEIPT_ASK_CHARS = 200; | |
| 34 | 45 | |
| 35 | 46 | /** The note a person is given when the app was made and filed but autonomy could not be turned on. */ |
| 36 | 47 | export const AUTONOMY_FAILED_WORDS = "The app is made, but the crew could not be set to start on it by itself. Turn on autonomy in its settings."; |
| @@ -41,6 +52,23 @@ function resolvesToForgeRepo(app: App, actor: User, sentence: string): boolean { | ||
| 41 | 52 | return name !== undefined && app.config.forgeRepo?.toLowerCase() === `${actor.username}/${name}`.toLowerCase(); |
| 42 | 53 | } |
| 43 | 54 | |
| 55 | /** Leave the receipt of a refusal: who, a cut of what they said, the words. Never a secret or a claim code. */ | |
| 56 | function refuse(app: App, actor: User | undefined, said: string, words: string): { words: string; receipt: string | null } { | |
| 57 | const since = new Date(Date.now() - 3_600_000).toISOString(); | |
| 58 | const spent = actor === undefined | |
| 59 | ? 0 | |
| 60 | : (app.db | |
| 61 | .query("SELECT COUNT(*) AS n FROM event WHERE kind = ?1 AND actor_id = ?2 AND created_at > ?3") | |
| 62 | .get(FIRST_ASK_REFUSED, actor.id, since) as { n: number }).n; | |
| 63 | if (spent >= REFUSAL_RECEIPTS_PER_HOUR) return { words, receipt: null }; | |
| 64 | const event = app.bus.append({ | |
| 65 | kind: FIRST_ASK_REFUSED, | |
| 66 | actorId: actor?.id, | |
| 67 | payload: { ask: said.trim().slice(0, RECEIPT_ASK_CHARS), words, made: false }, | |
| 68 | }); | |
| 69 | return { words, receipt: event.id }; | |
| 70 | } | |
| 71 | ||
| 44 | 72 | export async function firstAsk( |
| 45 | 73 | app: App, |
| 46 | 74 | actor: User | undefined, |
| @@ -49,19 +77,20 @@ export async function firstAsk( | ||
| 49 | 77 | from: string | null = null, |
| 50 | 78 | ): Promise<Result<FirstAsk, FirstAskError>> { |
| 51 | 79 | const text = said.trim(); |
| 52 | if (text === "") return err({ words: SAY_SOMETHING_WORDS }); | |
| 80 | const no = (words: string) => err(refuse(app, actor, text, words)); | |
| 81 | if (text === "") return no(SAY_SOMETHING_WORDS); | |
| 53 | 82 | // Everything the tell could refuse is asked before the app exists, so a |
| 54 | 83 | // refused ask leaves nothing behind and a retry makes no second app. The |
| 55 | 84 | // new app's owner is the actor, who may direct the crew on it. |
| 56 | if (text.length > MAX_BODY) return err({ words: TOO_LONG_WORDS }); | |
| 57 | if (actor === undefined || actor.kind === "org") return err({ words: DIRECTION_NOTE }); | |
| 58 | if (!requireVerified(app, actor).ok) return err({ words: "Verify your email first." }); | |
| 59 | if (from !== null && typeof parseFrom(from) !== "string") return err({ words: FROM_WORDS }); | |
| 85 | if (text.length > MAX_BODY) return no(TOO_LONG_WORDS); | |
| 86 | if (actor === undefined || actor.kind === "org") return no(DIRECTION_NOTE); | |
| 87 | if (!requireVerified(app, actor).ok) return no("Verify your email first."); | |
| 88 | if (from !== null && typeof parseFrom(from) !== "string") return no(FROM_WORDS); | |
| 60 | 89 | // The forge's own repository is never an ask's target: refused before any app exists. |
| 61 | if (resolvesToForgeRepo(app, actor, text)) return err({ words: FORGE_REPO_WORDS }); | |
| 90 | if (resolvesToForgeRepo(app, actor, text)) return no(FORGE_REPO_WORDS); | |
| 62 | 91 | // An empty description: the issue the tell files is the one issue of the ask. |
| 63 | 92 | const made = await newAppFromSentence(app, actor, { sentence: text, description: "" }); |
| 64 | if (!made.ok) return err({ words: NEW_APP_ERRORS[made.code] ?? NOT_TOLD_WORDS }); | |
| 93 | if (!made.ok) return no(NEW_APP_ERRORS[made.code] ?? NOT_TOLD_WORDS); | |
| 65 | 94 | let repo = made.repo; |
| 66 | 95 | let autonomyFailed = false; |
| 67 | 96 | if (crewCanRun) { |
test/first-ask-new-app.test.ts +41
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 8 | import { firstAsk, FIRST_ASK_REFUSED, RECEIPT_ASK_CHARS, REFUSAL_RECEIPTS_PER_HOUR } from "../src/actions/first_ask.ts"; | |
| 8 | 9 | import { listIssues, MAX_BODY } from "../src/core/issues.ts"; |
| 9 | 10 | import { TOO_LONG_WORDS } from "../src/actions/tell.ts"; |
| 10 | 11 | import { FORGE_REPO_WORDS, SAY_SOMETHING_WORDS } from "../src/actions/tell_words.ts"; |
| @@ -21,6 +22,10 @@ import { quietLogs } from "./helpers/globals.ts"; | ||
| 21 | 22 | |
| 22 | 23 | quietLogs(); |
| 23 | 24 | |
| 25 | type Receipt = { actor_id: string; payload: string }; | |
| 26 | const receipts = (db: import("bun:sqlite").Database): Receipt[] => | |
| 27 | db.query("SELECT actor_id, payload FROM event WHERE kind = ?1 ORDER BY seq").all(FIRST_ASK_REFUSED) as Receipt[]; | |
| 28 | ||
| 24 | 29 | let forge: ClaimedForge; |
| 25 | 30 | beforeAll(async () => { |
| 26 | 31 | forge = await claimedForge("forge-first-ask-"); |
| @@ -88,18 +93,27 @@ function flashOf(res: Response): string { | ||
| 88 | 93 | test("an empty ask makes no app and says so", async () => { |
| 89 | 94 | const db = forge.child.db; |
| 90 | 95 | const before = listReposOwnedBy(db, getUserByName(db, "maria")!.id).length; |
| 96 | const had = receipts(db).length; | |
| 91 | 97 | const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: " " }); |
| 92 | 98 | expect(res.status).toBe(303); |
| 93 | 99 | expect(flashOf(res)).toContain(SAY_SOMETHING_WORDS); |
| 100 | const left = receipts(db); | |
| 101 | expect(left.length).toBe(had + 1); | |
| 102 | expect(JSON.parse(left.at(-1)!.payload)).toMatchObject({ ask: "", words: SAY_SOMETHING_WORDS, made: false }); | |
| 103 | expect(left.at(-1)!.actor_id).toBe(getUserByName(db, "maria")!.id); | |
| 94 | 104 | expect(listReposOwnedBy(db, getUserByName(db, "maria")!.id).length).toBe(before); |
| 95 | 105 | }); |
| 96 | 106 | |
| 97 | 107 | test("an over-long ask makes no app and says so", async () => { |
| 98 | 108 | const db = forge.child.db; |
| 99 | 109 | const before = listReposOwnedBy(db, getUserByName(db, "maria")!.id).length; |
| 110 | const had = receipts(db).length; | |
| 100 | 111 | const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: "zebra ".repeat(MAX_BODY) }); |
| 101 | 112 | expect(res.status).toBe(303); |
| 102 | 113 | expect(flashOf(res)).toContain(TOO_LONG_WORDS); |
| 114 | const left = receipts(db); | |
| 115 | expect(left.length).toBe(had + 1); | |
| 116 | expect(JSON.parse(left.at(-1)!.payload).ask.length).toBe(RECEIPT_ASK_CHARS); | |
| 103 | 117 | expect(listReposOwnedBy(db, getUserByName(db, "maria")!.id).length).toBe(before); |
| 104 | 118 | }); |
| 105 | 119 | |
| @@ -114,6 +128,7 @@ test("an ask that would name the forge's own repository is refused and makes no | ||
| 114 | 128 | const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: ask }); |
| 115 | 129 | expect(res.status).toBe(303); |
| 116 | 130 | expect(flashOf(res)).toContain(FORGE_REPO_WORDS); |
| 131 | expect(receipts(db).filter((r) => JSON.parse(r.payload).words === FORGE_REPO_WORDS).length).toBe(1); | |
| 117 | 132 | expect(listReposOwnedBy(db, maria.id).length).toBe(count); |
| 118 | 133 | } finally { |
| 119 | 134 | if (before === undefined) delete forge.child.config.forgeRepo; |
| @@ -124,6 +139,32 @@ test("an ask that would name the forge's own repository is refused and makes no | ||
| 124 | 139 | const cardCtx = (admin: boolean) => |
| 125 | 140 | ({ app: forge.child, auth: { user: { isAdmin: admin } } }) as unknown as Parameters<typeof crewKeyCard>[0]; |
| 126 | 141 | |
| 142 | test("each other refusal leaves one receipt with its words and the receipt id; a success leaves none; the hourly cap holds", async () => { | |
| 143 | const db = forge.child.db; | |
| 144 | const maria = getUserByName(db, "maria")!; | |
| 145 | const base = receipts(db).length; | |
| 146 | // Not an actor who may direct: refused, receipt without an actor. | |
| 147 | const anon = await firstAsk(forge.child, undefined, "A kite shop page", false); | |
| 148 | expect(anon.ok).toBe(false); | |
| 149 | if (!anon.ok) expect(anon.error.receipt).not.toBeNull(); | |
| 150 | // A success leaves no receipt of its own. | |
| 151 | const first = await firstAsk(forge.child, maria, "Lighthouse visitor log", false); | |
| 152 | expect(first.ok).toBe(true); | |
| 153 | expect(receipts(db).length).toBe(base + 1); | |
| 154 | // No name can be made from the words: refused after the app step, still one receipt. | |
| 155 | const again = await firstAsk(forge.child, maria, "!!! ???", false); | |
| 156 | expect(again.ok).toBe(false); | |
| 157 | if (!again.ok) expect(again.error.receipt).not.toBeNull(); | |
| 158 | expect(receipts(db).length).toBe(base + 2); | |
| 159 | // The cap: past it a refusal still refuses and leaves no receipt. | |
| 160 | for (let i = 0; i < REFUSAL_RECEIPTS_PER_HOUR + 3; i++) await firstAsk(forge.child, maria, "", false); | |
| 161 | const mine = receipts(db).filter((r) => r.actor_id === maria.id).length; | |
| 162 | expect(mine).toBeLessThanOrEqual(REFUSAL_RECEIPTS_PER_HOUR + 1); | |
| 163 | const capped = await firstAsk(forge.child, maria, "", false); | |
| 164 | expect(capped.ok).toBe(false); | |
| 165 | if (!capped.ok) expect(capped.error.receipt).toBeNull(); | |
| 166 | }); | |
| 167 | ||
| 127 | 168 | test("an app's page never asks for a key while included credit is left (#11508)", () => { |
| 128 | 169 | expect(crewCanRun(forge.child)).toBe(true); |
| 129 | 170 | for (const admin of [true, false]) expect(crewKeyCard(cardCtx(admin), true)).toBeNull(); |
new above
offline