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
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13604-a-bet-that-would-overrun-the-wallet-is-r → main 4 commits fork
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 | ||
| 6 | 6 | - **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. |
| 7 | 7 | - **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". |
| 8 | 8 | - **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. | |
| 10 | 10 | - **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. |
| 11 | 11 | - **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. |
| 12 | 12 | - **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 | ||
| 75 | 75 | */ |
| 76 | 76 | export function shortSentence(e: WalletEstimate | null): string | null { |
| 77 | 77 | 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}.`; | |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | export interface WalletEstimate { |
src/web/pages/bets.ts +2 −2
| @@ -261,9 +261,9 @@ export async function betAct(ctx: Ctx): Promise<Response> { | ||
| 261 | 261 | : verb === "verdict" && (verdict === "done" || verdict === "not_yet") ? giveVerdict(ctx.app, user, repo, id, verdict, field(form, "why")) |
| 262 | 262 | : null; |
| 263 | 263 | 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 | |
| 265 | 265 | // 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); | |
| 267 | 267 | 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"] }); |
| 268 | 268 | return flashTo(ctx, betHref(repo, id), { message: oneLine(`${result.value.name}: ${STATE_WORDS[result.value.state]}.`) }); |
| 269 | 269 | } |
test/bet-wallet-refusal.test.ts +7 −7
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /** |
| 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 | |
| 3 | 3 | * naming both numbers; equal to the balance goes through; with no grant (no cap) nothing changes. |
| 4 | 4 | * The page, the API (JSON) and the MCP say the same words. |
| 5 | 5 | */ |
| @@ -73,7 +73,7 @@ describe("the sentence", () => { | ||
| 73 | 73 | expect(walletRefusal(10, 0.5, 19)).toBeNull(); |
| 74 | 74 | expect(walletRefusal(10, 0.5, 20)).toBeNull(); |
| 75 | 75 | 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.", | |
| 77 | 77 | ); |
| 78 | 78 | }); |
| 79 | 79 | }); |
| @@ -89,8 +89,8 @@ describe("starting a bet", () => { | ||
| 89 | 89 | setModelGrant(app.db, alice.id, 50, alice.id, NOW); |
| 90 | 90 | const refused = backBet(app, alice, fresh(), over.id, 50, NOW); |
| 91 | 91 | 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 $"); | |
| 94 | 94 | expect(getBet(app.db, over.id)!.state).toBe("proposed"); |
| 95 | 95 | setModelGrant(app.db, alice.id, null, alice.id, NOW); |
| 96 | 96 | expect(backBet(app, alice, fresh(), over.id, 50, NOW).ok).toBe(true); |
| @@ -110,7 +110,7 @@ describe("starting a bet", () => { | ||
| 110 | 110 | }); |
| 111 | 111 | |
| 112 | 112 | 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 () => { | |
| 114 | 114 | app.db.query("UPDATE bet SET state = 'stopped' WHERE repo_id = ?1").run(repo.id); |
| 115 | 115 | const b = proposal(); |
| 116 | 116 | setModelGrant(app.db, alice.id, 5, alice.id, NOW); |
| @@ -122,9 +122,9 @@ describe("the doors", () => { | ||
| 122 | 122 | expect(page.status).toBe(303); |
| 123 | 123 | expect(page.headers.get("location")).toBe(`/alice/wallet/bets/${b.id}`); |
| 124 | 124 | 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); | |
| 126 | 126 | 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.]+/); | |
| 128 | 128 | const shown = await fetch(`${servers.url}/alice/wallet/bets/${b.id}`, { headers: { cookie: [cookie, ...page.headers.getSetCookie().map((c) => c.split(";")[0]!)].join("; ") } }); |
| 129 | 129 | expect((await shown.text()).replace(/&/g, "&")).toContain(sentence); |
| 130 | 130 | 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", () => { | ||
| 674 | 674 | expect(walletRefusal(undefined, 0.01, 1e9)).toBeNull(); |
| 675 | 675 | expect(walletRefusal(10, undefined, 1e9)).toBeNull(); |
| 676 | 676 | 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}`, | |
| 678 | 678 | ); |
| 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"); | |
| 680 | 680 | }); |
| 681 | 681 | |
| 682 | 682 | test("costRate is dollars per builder second over 30 days, and nothing without cost or history", async () => { |
| @@ -699,7 +699,7 @@ describe("the wallet", () => { | ||
| 699 | 699 | expect(budgetSeconds(100, fresh(into).autonomySlots) / 3600).toBeGreaterThan(125); |
| 700 | 700 | const refused = backBet(app, alice, fresh(into), bet.id, 100, NOW); |
| 701 | 701 | 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"); | |
| 703 | 703 | expect(!refused.ok && refused.error.message).toContain(WORD); |
| 704 | 704 | expect(getBet(app.db, bet.id)!.state).toBe("proposed"); |
| 705 | 705 | expect(backBet(app, alice, fresh(into), bet.id, 1, NOW).ok).toBe(true); |
| @@ -718,7 +718,7 @@ describe("the wallet", () => { | ||
| 718 | 718 | expect(backBet(app, alice, fresh(into), first.id, 50, NOW).ok).toBe(true); |
| 719 | 719 | const refused = backBet(app, alice, fresh(into), second.id, 40, NOW); |
| 720 | 720 | 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"); | |
| 722 | 722 | }); |
| 723 | 723 | |
| 724 | 724 | test("the planner starts no bet the wallet cannot cover, and starts it once it can", async () => { |
| @@ -778,7 +778,7 @@ describe("the wallet", () => { | ||
| 778 | 778 | await key("b"); |
| 779 | 779 | for (const ch of "\b\b\b100") await key(ch === "\b" ? "backspace" : ch); |
| 780 | 780 | await key("enter"); |
| 781 | expect(tui.screen.toText()).toContain("wallet has"); | |
| 781 | expect(tui.screen.toText()).toContain("balance $"); | |
| 782 | 782 | expect(stateOf(b.id)).toBe("proposed"); |
| 783 | 783 | tui.stop(); |
| 784 | 784 | } 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 | ||
| 57 | 57 | key("enter"); |
| 58 | 58 | await settleApp(app); |
| 59 | 59 | 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 $"); | |
| 62 | 62 | expect(text).toContain("lower the share"); |
| 63 | 63 | app.stop(); |
| 64 | 64 | }); |
new above
offline