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

A bet that would overrun the wallet is refused before it starts, naming the estimate and the balance #13623

Merged opened this

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

Merged

torque/forge:torque/13604-a-bet-that-would-overrun-the-wallet-is-r → main 4 commits fork

  • Merged. torque/13604-a-bet-that-would-overrun-the-wallet-is-r was merged into main as 20daa9d.

Review

Sign in to review this change.
6 files changed +18 −18
docs/features/bets.md +1 −1
@@ -6,7 +6,7 @@ A bet is a name, an appetite (a share of the week), what done looks like in a fe
66 - **Propose.** "Ask the planner to propose bets" groups open issues in no live bet: by first label; else by the pillar an unlabelled issue holds up (later and structural issues are skipped); else by the title word most unlabelled issues share. A group of two or more becomes a proposal at 20% of the week. At most five proposals a pass; no model, so the same state gives the same proposals.
77 - **Notes.** Only notes written by the repo's owner or an admin count (a visitor's never join or appear). Such a note joins a proposal when its issue is held there, it names one of the group's issues as `#N`, or it shares two words with the group's name and its issues' titles; it is quoted in the journeys as "The owner's note".
88 - **Back.** The owner (a person who administers the repo, never an agent) backs one with a share. At most 5 run, and their shares stay within 100%.
9 - **The wallet.** Backing is refused, with one sentence naming both numbers, when the bet's estimate is above what is free of the owner's model grant; an estimate equal to it starts. A form shows the sentence as a flash on the bet; a JSON client gets 409. The estimate is the bet's budget (below) at the dollars a builder second has cost this repository's runs over 30 days, and the wallet is the grant's remainder less what the bets already running have yet to spend. With no grant or no history nothing is refused. The planner's own starts skip a bet the wallet cannot cover. The sentence ends with the way out: lower the share, or ask a site admin to raise the grant (Settings, Accounts). The web page, MCP `bet` and terminal show the same sentence.
9 - **The wallet.** Backing is refused, with one sentence, "Estimate $X, balance $Y", when the bet's estimate is above what is free of the owner's model grant; an estimate equal to it starts. A form shows the sentence as a flash on the bet; a JSON client gets 422. The estimate is the bet's budget (below) at the dollars a builder second has cost this repository's runs over 30 days, and the wallet is the grant's remainder less what the bets already running have yet to spend. With no grant or no history nothing is refused. The planner's own starts skip a bet the wallet cannot cover. The sentence ends with the way out: lower the share, or ask a site admin to raise the grant (Settings, Accounts). The web page, MCP `bet` and terminal show the same sentence.
1010 - **Before you press.** A proposed bet's card (and its row in the terminal) prints `Estimate $X of $Y left`, the estimate beside the wallet the refusal would judge by. When the estimate passes the wallet the line adds `short by $Z`, the *Back it* button is disabled, and the reason sits beside it. With no grant or no history there is no line. The line is on proposed cards only: a stopped bet re-backed from its page shows no estimate first, though the refusal still judges it.
1111 - **Picks.** Once a repository has ever had a bet backed, builders pick only scopes of a running bet, issues the owner asked for or raised, and small fixes, polish included (polish held to its share of the week, see polish-share.md). An issue in no bet does not start by itself. A repository with no bet picks as before.
1212 - **Appetite.** The budget is share × builder slots × one week of builder time, counted from the runs on the bet's scopes since it was backed. At the budget the bet stops: no extension. Its left line defaults to the open scopes; the lead rewrites it in one line; the owner re-backs, cuts or ends it (ending as done is for a stopped bet only; a running bet finishes when every scope closes).
src/crew/bets.ts +1 −1
@@ -75,7 +75,7 @@ export const WALLET_WAY_OUT = "lower the share, or ask a site admin to raise you
7575 */
7676 export function shortSentence(e: WalletEstimate | null): string | null {
7777 if (e === null || e.short === 0) return null;
78 return `This bet is estimated at ${usd(e.estimate)} but the wallet has ${usd(e.free)} free after the bets already running: ${WALLET_WAY_OUT}.`;
78 return `Estimate ${usd(e.estimate)}, balance ${usd(e.free)} (after the bets already running): ${WALLET_WAY_OUT}.`;
7979 }
8080
8181 export interface WalletEstimate {
src/web/pages/bets.ts +2 −2
@@ -261,9 +261,9 @@ export async function betAct(ctx: Ctx): Promise<Response> {
261261 : verb === "verdict" && (verdict === "done" || verdict === "not_yet") ? giveVerdict(ctx.app, user, repo, id, verdict, field(form, "why"))
262262 : null;
263263 if (result === null) return notFoundPage(ctx);
264 // A wallet refusal is a 409 for a client that asked for JSON; a form gets the flash back on the
264 // A wallet refusal is a 422 for a client that asked for JSON; a form gets the flash back on the
265265 // bet.
266 if (!result.ok && result.error.error === "wallet" && prefersJson(ctx)) return json({ ok: false, error: result.error.message }, 409);
266 if (!result.ok && result.error.error === "wallet" && prefersJson(ctx)) return json({ ok: false, error: result.error.message }, 422);
267267 if (!result.ok) return flashTo(ctx, result.error.error === "not_found" ? list : betHref(repo, id), { tone: "danger", message: result.error.message || WHY[result.error.error === "not_found" ? "not_found" : "forbidden"] });
268268 return flashTo(ctx, betHref(repo, id), { message: oneLine(`${result.value.name}: ${STATE_WORDS[result.value.state]}.`) });
269269 }
test/bet-wallet-refusal.test.ts +7 −7
@@ -1,5 +1,5 @@
11 /**
2 * #13490: a bet whose estimate passes the wallet's balance is refused with 409 and one sentence
2 * #13490: a bet whose estimate passes the wallet's balance is refused with 422 and one sentence
33 * naming both numbers; equal to the balance goes through; with no grant (no cap) nothing changes.
44 * The page, the API (JSON) and the MCP say the same words.
55 */
@@ -73,7 +73,7 @@ describe("the sentence", () => {
7373 expect(walletRefusal(10, 0.5, 19)).toBeNull();
7474 expect(walletRefusal(10, 0.5, 20)).toBeNull();
7575 expect(walletRefusal(10, 0.5, 21)).toBe(
76 "This bet is estimated at $10.50 but the wallet has $10.00 free after the bets already running: lower the share, or ask a site admin to raise your grant.",
76 "Estimate $10.50, balance $10.00 (after the bets already running): lower the share, or ask a site admin to raise your grant.",
7777 );
7878 });
7979 });
@@ -89,8 +89,8 @@ describe("starting a bet", () => {
8989 setModelGrant(app.db, alice.id, 50, alice.id, NOW);
9090 const refused = backBet(app, alice, fresh(), over.id, 50, NOW);
9191 expect(!refused.ok && refused.error.error).toBe("wallet");
92 expect(!refused.ok && refused.error.message).toContain("estimated at $84.00");
93 expect(!refused.ok && refused.error.message).toContain("the wallet has $");
92 expect(!refused.ok && refused.error.message).toContain("Estimate $84.00");
93 expect(!refused.ok && refused.error.message).toContain("balance $");
9494 expect(getBet(app.db, over.id)!.state).toBe("proposed");
9595 setModelGrant(app.db, alice.id, null, alice.id, NOW);
9696 expect(backBet(app, alice, fresh(), over.id, 50, NOW).ok).toBe(true);
@@ -110,7 +110,7 @@ describe("starting a bet", () => {
110110 });
111111
112112 describe("the doors", () => {
113 test("JSON answers 409, a form flashes the sentence on the bet, and the MCP says the same words", async () => {
113 test("JSON answers 422, a form flashes the sentence on the bet, and the MCP says the same words", async () => {
114114 app.db.query("UPDATE bet SET state = 'stopped' WHERE repo_id = ?1").run(repo.id);
115115 const b = proposal();
116116 setModelGrant(app.db, alice.id, 5, alice.id, NOW);
@@ -122,9 +122,9 @@ describe("the doors", () => {
122122 expect(page.status).toBe(303);
123123 expect(page.headers.get("location")).toBe(`/alice/wallet/bets/${b.id}`);
124124 const api = await fetch(path, { method: "POST", redirect: "manual", headers: { ...headers, accept: "application/json" }, body: "share=100" });
125 expect(api.status).toBe(409);
125 expect(api.status).toBe(422);
126126 const sentence = ((await api.json()) as { error: string }).error;
127 expect(sentence).toMatch(/^This bet is estimated at \$[\d.]+ but the wallet has \$[\d.]+ free/);
127 expect(sentence).toMatch(/^Estimate \$[\d.]+, balance \$[\d.]+/);
128128 const shown = await fetch(`${servers.url}/alice/wallet/bets/${b.id}`, { headers: { cookie: [cookie, ...page.headers.getSetCookie().map((c) => c.split(";")[0]!)].join("; ") } });
129129 expect((await shown.text()).replace(/&amp;/g, "&")).toContain(sentence);
130130 const mcp = await betTool.run(mcpCtx(), alice, { app: "alice/wallet", verb: "back", bet: b.id, share: 100 });
test/bets.test.ts +5 −5
@@ -674,9 +674,9 @@ describe("the wallet", () => {
674674 expect(walletRefusal(undefined, 0.01, 1e9)).toBeNull();
675675 expect(walletRefusal(10, undefined, 1e9)).toBeNull();
676676 expect(walletRefusal(10, 0.01, 2000)).toBe(
677 `This bet is estimated at $20.00 but the wallet has $10.00 free after the bets already running: ${WORD}`,
677 `Estimate $20.00, balance $10.00 (after the bets already running): ${WORD}`,
678678 );
679 expect(walletRefusal(10, 0.01, 1000, 1000)).toContain("the wallet has $0.00 free");
679 expect(walletRefusal(10, 0.01, 1000, 1000)).toContain("balance $0.00");
680680 });
681681
682682 test("costRate is dollars per builder second over 30 days, and nothing without cost or history", async () => {
@@ -699,7 +699,7 @@ describe("the wallet", () => {
699699 expect(budgetSeconds(100, fresh(into).autonomySlots) / 3600).toBeGreaterThan(125);
700700 const refused = backBet(app, alice, fresh(into), bet.id, 100, NOW);
701701 expect(refused.ok).toBe(false);
702 expect(!refused.ok && refused.error.message).toContain("the wallet has $100.00 free");
702 expect(!refused.ok && refused.error.message).toContain("balance $100.00");
703703 expect(!refused.ok && refused.error.message).toContain(WORD);
704704 expect(getBet(app.db, bet.id)!.state).toBe("proposed");
705705 expect(backBet(app, alice, fresh(into), bet.id, 1, NOW).ok).toBe(true);
@@ -718,7 +718,7 @@ describe("the wallet", () => {
718718 expect(backBet(app, alice, fresh(into), first.id, 50, NOW).ok).toBe(true);
719719 const refused = backBet(app, alice, fresh(into), second.id, 40, NOW);
720720 expect(refused.ok).toBe(false);
721 expect(!refused.ok && refused.error.message).toContain("free after the bets already running");
721 expect(!refused.ok && refused.error.message).toContain("after the bets already running");
722722 });
723723
724724 test("the planner starts no bet the wallet cannot cover, and starts it once it can", async () => {
@@ -778,7 +778,7 @@ describe("the wallet", () => {
778778 await key("b");
779779 for (const ch of "\b\b\b100") await key(ch === "\b" ? "backspace" : ch);
780780 await key("enter");
781 expect(tui.screen.toText()).toContain("wallet has");
781 expect(tui.screen.toText()).toContain("balance $");
782782 expect(stateOf(b.id)).toBe("proposed");
783783 tui.stop();
784784 } finally {
test/tui-bets-wallet.test.ts +2 −2
@@ -57,8 +57,8 @@ test("backing over the wallet shows the same sentence and leaves the bet propose
5757 key("enter");
5858 await settleApp(app);
5959 const text = app.screen.toText().replace(/\s+/g, " ");
60 expect(text).toContain("This bet is estimated at $");
61 expect(text).toContain("but the wallet has $");
60 expect(text).toContain("Estimate $");
61 expect(text).toContain(", balance $");
6262 expect(text).toContain("lower the share");
6363 app.stop();
6464 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs