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

Parked opened this
Closed without merging

scout/forge:scout/11462-a-refused-first-ask-leaves-a-receipt-the → main 4 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
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 {
3030 }
3131
3232 /** 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;
3445
3546 /** The note a person is given when the app was made and filed but autonomy could not be turned on. */
3647 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 {
4152 return name !== undefined && app.config.forgeRepo?.toLowerCase() === `${actor.username}/${name}`.toLowerCase();
4253 }
4354
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
4472 export async function firstAsk(
4573 app: App,
4674 actor: User | undefined,
@@ -49,19 +77,20 @@ export async function firstAsk(
4977 from: string | null = null,
5078 ): Promise<Result<FirstAsk, FirstAskError>> {
5179 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);
5382 // Everything the tell could refuse is asked before the app exists, so a
5483 // refused ask leaves nothing behind and a retry makes no second app. The
5584 // 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);
6089 // 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);
6291 // An empty description: the issue the tell files is the one issue of the ask.
6392 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);
6594 let repo = made.repo;
6695 let autonomyFailed = false;
6796 if (crewCanRun) {
test/first-ask-new-app.test.ts +41
@@ -5,6 +5,7 @@
55 */
66
77 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";
89 import { listIssues, MAX_BODY } from "../src/core/issues.ts";
910 import { TOO_LONG_WORDS } from "../src/actions/tell.ts";
1011 import { FORGE_REPO_WORDS, SAY_SOMETHING_WORDS } from "../src/actions/tell_words.ts";
@@ -21,6 +22,10 @@ import { quietLogs } from "./helpers/globals.ts";
2122
2223 quietLogs();
2324
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
2429 let forge: ClaimedForge;
2530 beforeAll(async () => {
2631 forge = await claimedForge("forge-first-ask-");
@@ -88,18 +93,27 @@ function flashOf(res: Response): string {
8893 test("an empty ask makes no app and says so", async () => {
8994 const db = forge.child.db;
9095 const before = listReposOwnedBy(db, getUserByName(db, "maria")!.id).length;
96 const had = receipts(db).length;
9197 const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: " " });
9298 expect(res.status).toBe(303);
9399 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);
94104 expect(listReposOwnedBy(db, getUserByName(db, "maria")!.id).length).toBe(before);
95105 });
96106
97107 test("an over-long ask makes no app and says so", async () => {
98108 const db = forge.child.db;
99109 const before = listReposOwnedBy(db, getUserByName(db, "maria")!.id).length;
110 const had = receipts(db).length;
100111 const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: "zebra ".repeat(MAX_BODY) });
101112 expect(res.status).toBe(303);
102113 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);
103117 expect(listReposOwnedBy(db, getUserByName(db, "maria")!.id).length).toBe(before);
104118 });
105119
@@ -114,6 +128,7 @@ test("an ask that would name the forge's own repository is refused and makes no
114128 const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: ask });
115129 expect(res.status).toBe(303);
116130 expect(flashOf(res)).toContain(FORGE_REPO_WORDS);
131 expect(receipts(db).filter((r) => JSON.parse(r.payload).words === FORGE_REPO_WORDS).length).toBe(1);
117132 expect(listReposOwnedBy(db, maria.id).length).toBe(count);
118133 } finally {
119134 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
124139 const cardCtx = (admin: boolean) =>
125140 ({ app: forge.child, auth: { user: { isAdmin: admin } } }) as unknown as Parameters<typeof crewKeyCard>[0];
126141
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
127168 test("an app's page never asks for a key while included credit is left (#11508)", () => {
128169 expect(crewCanRun(forge.child)).toBe(true);
129170 for (const admin of [true, false]) expect(crewKeyCard(cardCtx(admin), true)).toBeNull();

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs