Public: anyone can read this; the crew can change it.
A bet refuses to start when the repository's wallet cannot cover its estimate, in one plain sentence #13354
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13297-a-bet-refuses-to-start-when-the-reposito → main 5 commits fork
6 files
changed +229
−17
docs/features/bets.md +1
| @@ -6,6 +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, when the bet's estimate passes what is left of the owner's model grant by more than a quarter. 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 | 10 | - **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. |
| 10 | 11 | - **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). |
| 11 | 12 | - **Scopes** read *figuring out* (no run yet), *executing*, or *raised hand* (nothing moved for two days), or *done*. |
src/actions/bets.ts +33 −9
| @@ -9,15 +9,16 @@ | ||
| 9 | 9 | import type { App } from "../app.ts"; |
| 10 | 10 | import { listCrewRoles } from "../core/agents.ts"; |
| 11 | 11 | import { |
| 12 | type Bet, betRuns, betScopes, getBet, insertBet, listBets, markBacked, markEnded, setLeftLine, setTryUrl, setVerdict, | |
| 12 | type Bet, betRuns, betScopes, costRate, getBet, insertBet, listBets, markBacked, markEnded, setLeftLine, setTryUrl, setVerdict, | |
| 13 | 13 | } from "../core/bets.ts"; |
| 14 | 14 | import { notesOfRepo } from "../core/app_notes.ts"; |
| 15 | 15 | import { themeWords, groupByWords } from "../core/bet_themes.ts"; |
| 16 | 16 | import { getIssuesByNumbers, labelsForIssues, numberKey } from "../core/issues.ts"; |
| 17 | 17 | import { isStructural, LATER, pillarsOf, servedBy } from "../core/pillars.ts"; |
| 18 | 18 | import { canAdmin, type Repo } from "../core/repos.ts"; |
| 19 | import { grantLeft } from "../core/model_grant.ts"; | |
| 19 | 20 | import { getUserById, type User } from "../core/users.ts"; |
| 20 | import { AUTO_SHARE, autoStarts, budgetSeconds, mayBack, spentSeconds, spentUp } from "../crew/bets.ts"; | |
| 21 | import { AUTO_SHARE, autoStarts, budgetSeconds, mayBack, spentSeconds, spentUp, walletRefusal } from "../crew/bets.ts"; | |
| 21 | 22 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 22 | 23 | |
| 23 | 24 | export type BetError = "forbidden" | "not_found" | "refused"; |
| @@ -47,9 +48,9 @@ const backed = (app: App, repo: Repo) => listBets(app.db, repo.id).filter((b) => | ||
| 47 | 48 | * no group holds seeds a bet of its own, named from it. A note written by |
| 48 | 49 | * the repo's owner or an admin joins a group when its issue is held there, it |
| 49 | 50 | * names one of the group's issues as #N, or it shares two words with the |
| 50 | * group's name and titles; it is quoted in the bet. Visitors' notes never join. Each group becomes a bet with | |
| 51 | * a modest share and the issues as scopes, led by the crew's first builder. | |
| 52 | * Run again it adds only what is new. At most five. | |
| 51 | * group's name and titles; it is quoted in the bet. Visitors' notes never join. Each group becomes | |
| 52 | * a bet with a modest share and the issues as scopes, led by the crew's first builder. Run again it | |
| 53 | * adds only what is new. At most five. | |
| 53 | 54 | */ |
| 54 | 55 | export function proposeBets(app: App, repo: Repo, now = new Date()): Bet[] { |
| 55 | 56 | const live = new Set( |
| @@ -84,9 +85,9 @@ export function proposeBets(app: App, repo: Repo, now = new Date()): Bet[] { | ||
| 84 | 85 | else loose.push(i); |
| 85 | 86 | } |
| 86 | 87 | for (const [word, items] of groupByWords(loose)) for (const i of items) add(word, i); |
| 87 | // The owner's and admins' notes (a visitor's never): each is the issue its words became. A note joins the theme | |
| 88 | // that holds its issue, names one of its issues as #N, or shares two words with its name and its issues' titles; | |
| 89 | // its issue joins too when no theme holds it. | |
| 88 | // The owner's and admins' notes (a visitor's never): each is the issue its words became. A note | |
| 89 | // joins the theme that holds its issue, names one of its issues as #N, or shares two words with | |
| 90 | // its name and its issues' titles; its issue joins too when no theme holds it. | |
| 90 | 91 | const byNumber = new Map(open.map((i) => [i.number, i])); |
| 91 | 92 | const trusted = new Map<string, boolean>(); |
| 92 | 93 | const mayNote = (visitor: string): boolean => { |
| @@ -151,6 +152,20 @@ export function proposeBets(app: App, repo: Repo, now = new Date()): Bet[] { | ||
| 151 | 152 | return made; |
| 152 | 153 | } |
| 153 | 154 | |
| 155 | /** | |
| 156 | * The wallet's sentence for a bet at `share` beside the `running` ones, or null when it is covered. | |
| 157 | */ | |
| 158 | function tooPoor(app: App, repo: Repo, share: number, running: readonly Bet[], now: Date): string | null { | |
| 159 | const wallet = grantLeft(app.db, repo.ownerId, now); | |
| 160 | const rate = costRate(app.db, repo.id, now); | |
| 161 | const seconds = budgetSeconds(share, repo.autonomySlots); | |
| 162 | const committed = running.reduce((sum, b) => { | |
| 163 | const spent = spentSeconds(betRuns(app.db, b.id, b.backedAt ?? b.proposedAt), now); | |
| 164 | return sum + Math.max(0, budgetSeconds(b.appetite, repo.autonomySlots) - spent); | |
| 165 | }, 0); | |
| 166 | return walletRefusal(wallet, rate, seconds, committed); | |
| 167 | } | |
| 168 | ||
| 154 | 169 | /** The owner backs a proposed or stopped bet with a share of the week. */ |
| 155 | 170 | export function backBet(app: App, user: User, repo: Repo, id: string, share: number, now = new Date()): Answer { |
| 156 | 171 | if (!owns(app, repo, user)) return refuse("forbidden", "only the owner backs a bet"); |
| @@ -160,6 +175,8 @@ export function backBet(app: App, user: User, repo: Repo, id: string, share: num | ||
| 160 | 175 | const others = backed(app, repo).filter((b) => b.id !== id); |
| 161 | 176 | const why = mayBack(others, share); |
| 162 | 177 | if (why !== null) return refuse("refused", why); |
| 178 | const poor = tooPoor(app, repo, share, others, now); | |
| 179 | if (poor !== null) return refuse("refused", poor); | |
| 163 | 180 | markBacked(app.db, id, share, "owner", now); |
| 164 | 181 | app.bus.append({ kind: "bet.backed", repoId: repo.id, actorId: user.id, payload: { bet: id, share, by: "owner" } }); |
| 165 | 182 | return ok(getBet(app.db, id)!); |
| @@ -202,7 +219,10 @@ export function writeLeft(app: App, user: User, repo: Repo, id: string, line: st | ||
| 202 | 219 | return ok(getBet(app.db, id)!); |
| 203 | 220 | } |
| 204 | 221 | |
| 205 | /** The owner's word on a finished bet: "feels done", or "not yet" and why (it returns to stopped with that reason). */ | |
| 222 | /** | |
| 223 | * The owner's word on a finished bet: "feels done", or "not yet" and why (it returns to stopped | |
| 224 | * with that reason). | |
| 225 | */ | |
| 206 | 226 | export function giveVerdict(app: App, user: User, repo: Repo, id: string, verdict: "done" | "not_yet", why: string, now = new Date()): Answer { |
| 207 | 227 | if (!owns(app, repo, user)) return refuse("forbidden", "only the owner says whether it feels done"); |
| 208 | 228 | const bet = load(app, repo, id); |
| @@ -246,7 +266,11 @@ export function tickBets(app: App, repo: Repo, now = new Date()): { finished: nu | ||
| 246 | 266 | const all = listBets(app.db, repo.id); |
| 247 | 267 | const running = all.filter((b) => b.state === "backed").length; |
| 248 | 268 | const proposed = all.filter((b) => b.state === "proposed"); |
| 269 | const live = [...backed(app, repo)]; | |
| 249 | 270 | for (const id of autoStarts(proposed, all.some((b) => b.backedBy !== "") ? 1 : running, now)) { |
| 271 | // The planner starts only what the wallet covers, beside what it has already started. | |
| 272 | if (tooPoor(app, repo, AUTO_SHARE, live, now) !== null) continue; | |
| 273 | live.push(getBet(app.db, id)!); | |
| 250 | 274 | markBacked(app.db, id, AUTO_SHARE, "planner", now); |
| 251 | 275 | app.bus.append({ kind: "bet.backed", repoId: repo.id, payload: { bet: id, share: AUTO_SHARE, by: "planner" } }); |
| 252 | 276 | out.started++; |
src/core/bets.ts +22 −2
| @@ -124,7 +124,10 @@ export interface Scope { | ||
| 124 | 124 | readonly movedAt: string; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | /** A bet's scopes: each issue with whether it closed, whether a run ever worked it, and when it last moved. */ | |
| 127 | /** | |
| 128 | * A bet's scopes: each issue with whether it closed, whether a run ever worked it, and when it last | |
| 129 | * moved. | |
| 130 | */ | |
| 128 | 131 | export function betScopes(db: Database, betId: string): Scope[] { |
| 129 | 132 | return db |
| 130 | 133 | .query<{ id: string; number: number; title: string; state: string; updated_at: string; last_run: string | null }, [string]>( |
| @@ -152,7 +155,10 @@ export function betRuns(db: Database, betId: string, since: string): Span[] { | ||
| 152 | 155 | .map((r) => ({ startedAt: r.started_at, finishedAt: r.finished_at })); |
| 153 | 156 | } |
| 154 | 157 | |
| 155 | /** The bet name for pulls (by number) in `repoId`: through the run that built each from a scope, or the pull as a scope itself. */ | |
| 158 | /** | |
| 159 | * The bet name for pulls (by number) in `repoId`: through the run that built each from a scope, or | |
| 160 | * the pull as a scope itself. | |
| 161 | */ | |
| 156 | 162 | export function betNamesOfPulls(db: Database, repoId: string, numbers: readonly number[]): Map<number, string> { |
| 157 | 163 | const out = new Map<number, string>(); |
| 158 | 164 | if (numbers.length === 0) return out; |
| @@ -171,3 +177,17 @@ export function betNamesOfPulls(db: Database, repoId: string, numbers: readonly | ||
| 171 | 177 | for (const r of rows) if (!out.has(r.number)) out.set(r.number, r.name); |
| 172 | 178 | return out; |
| 173 | 179 | } |
| 180 | ||
| 181 | /** | |
| 182 | * Dollars per builder second this repository's runs cost over the last 30 days; undefined with no | |
| 183 | * history. | |
| 184 | */ | |
| 185 | export function costRate(db: Database, repoId: string, now: Date): number | undefined { | |
| 186 | const since = new Date(now.getTime() - 30 * 86400_000).toISOString(); | |
| 187 | const row = db | |
| 188 | .query<{ cost: number; ms: number }, [string, string]>( | |
| 189 | "SELECT COALESCE(SUM(cost_usd), 0) AS cost, COALESCE(SUM(wall_ms), 0) AS ms FROM usage WHERE repo_id = ?1 AND at >= ?2", | |
| 190 | ) | |
| 191 | .get(repoId, since); | |
| 192 | return row !== null && row.ms > 0 && row.cost > 0 ? row.cost / (row.ms / 1000) : undefined; | |
| 193 | } |
src/crew/bets.ts +19
| @@ -54,6 +54,25 @@ export function mayBack(backed: readonly { readonly appetite: number }[], share: | ||
| 54 | 54 | return null; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | /** An estimate counts as too much only when it passes the wallet by this much (it is rough). */ | |
| 58 | export const WALLET_MARGIN = 1.25; | |
| 59 | ||
| 60 | const usd = (n: number): string => `$${n.toFixed(2)}`; | |
| 61 | ||
| 62 | /** | |
| 63 | * The sentence that refuses a bet its wallet cannot cover, or null. `rate` is dollars per builder | |
| 64 | * second from the repository's own recent runs, `wallet` the dollars left of the owner's grant, | |
| 65 | * `committed` the builder seconds the bets already running have yet to spend (the same wallet pays | |
| 66 | * for them). With no grant or no history there is no estimate to hold the bet to. | |
| 67 | */ | |
| 68 | export function walletRefusal(wallet: number | undefined, rate: number | undefined, seconds: number, committed = 0): string | null { | |
| 69 | if (wallet === undefined || rate === undefined || !(rate > 0)) return null; | |
| 70 | const estimate = rate * seconds; | |
| 71 | const free = Math.max(0, wallet - rate * committed); | |
| 72 | if (estimate <= free * WALLET_MARGIN) return null; | |
| 73 | return `This bet is estimated at ${usd(estimate)} but the wallet has ${usd(free)} free after the bets already running: lower the share, or ask a site admin to raise your grant.`; | |
| 74 | } | |
| 75 | ||
| 57 | 76 | export interface Proposed { |
| 58 | 77 | readonly id: string; |
| 59 | 78 | readonly proposedAt: string; |
test/bets.test.ts +153 −6
| @@ -14,11 +14,14 @@ import { openIssue } from "../src/actions/issues.ts"; | ||
| 14 | 14 | import { commitToBranch, createRepo, updateRepoSettings } from "../src/actions/repos.ts"; |
| 15 | 15 | import { insertNote } from "../src/core/app_notes.ts"; |
| 16 | 16 | import { addDependency } from "../src/core/dependencies.ts"; |
| 17 | import { betScopes, getBet, insertBet, listBets, markBacked } from "../src/core/bets.ts"; | |
| 17 | import { betScopes, costRate, getBet, insertBet, listBets, markBacked } from "../src/core/bets.ts"; | |
| 18 | 18 | import { createLabel, getIssueById, setIssueLabels, type Issue } from "../src/core/issues.ts"; |
| 19 | 19 | import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 20 | 20 | import { createUser, type User } from "../src/core/users.ts"; |
| 21 | import { autoStarts, budgetSeconds, mayBack, mayPick, scopeState, spentSeconds, spentUp } from "../src/crew/bets.ts"; | |
| 21 | import { setModelGrant } from "../src/core/model_grant.ts"; | |
| 22 | import { createRun } from "../src/core/runs.ts"; | |
| 23 | import { recordUsage } from "../src/core/usage.ts"; | |
| 24 | import { autoStarts, budgetSeconds, mayBack, mayPick, scopeState, spentSeconds, spentUp, walletRefusal } from "../src/crew/bets.ts"; | |
| 22 | 25 | import { driveRepo } from "../src/crew/autonomy.ts"; |
| 23 | 26 | import { serve, type Servers } from "../src/http/server.ts"; |
| 24 | 27 | |
| @@ -83,7 +86,10 @@ function open(title: string, into?: Repo): Issue { | ||
| 83 | 86 | return made.value; |
| 84 | 87 | } |
| 85 | 88 | |
| 86 | /** A repository of its own, so a test's issues and bets touch no other test's; named to sort after "notes", whose bets the terminal lists first. */ | |
| 89 | /** | |
| 90 | * A repository of its own, so a test's issues and bets touch no other test's; named to sort after | |
| 91 | * "notes", whose bets the terminal lists first. | |
| 92 | */ | |
| 87 | 93 | async function blank(name: string): Promise<Repo> { |
| 88 | 94 | const made = await createRepo(app, alice, { name, visibility: "public" }); |
| 89 | 95 | if (!made.ok) throw new Error(String(made.error)); |
| @@ -96,7 +102,10 @@ function label(into: Repo, name: string, issues: readonly Issue[]): void { | ||
| 96 | 102 | for (const i of issues) setIssueLabels(app.db, i.id, [made.value.id]); |
| 97 | 103 | } |
| 98 | 104 | |
| 99 | /** The same state, built anew in a fresh repository: a pillar pair with a note naming it, a word pair, a label pair. */ | |
| 105 | /** | |
| 106 | * The same state, built anew in a fresh repository: a pillar pair with a note naming it, a word | |
| 107 | * pair, a label pair. | |
| 108 | */ | |
| 100 | 109 | async function sameState(name: string): Promise<{ name: string; journeys: string; scopes: string[] }[]> { |
| 101 | 110 | const into = await blank(name); |
| 102 | 111 | const umbrella = open("P7 Speed: be quick", into); |
| @@ -362,6 +371,7 @@ describe("the pages", () => { | ||
| 362 | 371 | |
| 363 | 372 | import { betNamesOfPulls } from "../src/core/bets.ts"; |
| 364 | 373 | import { getUserByName } from "../src/core/users.ts"; |
| 374 | import type { Ctx } from "../src/http/context.ts"; | |
| 365 | 375 | import { tool as betTool } from "../src/http/mcp/tools/bet.ts"; |
| 366 | 376 | import { tool as betsTool } from "../src/http/mcp/tools/bets.ts"; |
| 367 | 377 | import { createForgeApp } from "../src/tui/forge.ts"; |
| @@ -443,12 +453,17 @@ describe("who sees and who acts, on the page", () => { | ||
| 443 | 453 | }); |
| 444 | 454 | }); |
| 445 | 455 | |
| 456 | function mcpCtx(user: User, scope: "rw" | "ro"): Ctx { | |
| 457 | const url = new URL("http://forge.test/mcp"); | |
| 458 | return { app, request: new Request(url.href), url, params: {}, auth: { user, scope, via: "token" }, theme: undefined }; | |
| 459 | } | |
| 460 | ||
| 446 | 461 | function setLeft(id: string): void { |
| 447 | 462 | app.db.query("UPDATE bet SET left_line = 'secret reason' WHERE id = ?1").run(id); |
| 448 | 463 | } |
| 449 | 464 | |
| 450 | 465 | describe("the MCP", () => { |
| 451 | const ctxFor = (user: User) => ({ app, auth: { user, scope: "rw" } }) as never; | |
| 466 | const ctxFor = (user: User, scope: "rw" | "ro" = "rw"): Ctx => mcpCtx(user, scope); | |
| 452 | 467 | test("the owner reads the plan and acts; a reader reads names; an agent and a reader are refused every act", async () => { |
| 453 | 468 | const b = fresh2("Mcp", []); |
| 454 | 469 | const agent = getUserByName(app.db, "b1")!; |
| @@ -468,7 +483,7 @@ describe("the MCP", () => { | ||
| 468 | 483 | expect(ok.ok).toBe(true); |
| 469 | 484 | expect(stateOf(b.id)).toBe("backed"); |
| 470 | 485 | // A read-only token cannot act. |
| 471 | const ro = await betTool.run({ app, auth: { user: alice, scope: "ro" } } as never, alice, { app: "alice/notes", verb: "stop", bet: b.id }); | |
| 486 | const ro = await betTool.run(ctxFor(alice, "ro"), alice, { app: "alice/notes", verb: "stop", bet: b.id }); | |
| 472 | 487 | expect(ro.ok).toBe(false); |
| 473 | 488 | expect(stateOf(b.id)).toBe("backed"); |
| 474 | 489 | }); |
| @@ -640,3 +655,135 @@ describe("the bets list and a bet's page are rows, not boxes", () => { | ||
| 640 | 655 | expect(one).not.toContain('class="card'); |
| 641 | 656 | }); |
| 642 | 657 | }); |
| 658 | ||
| 659 | describe("the wallet", () => { | |
| 660 | const WORD = "lower the share, or ask a site admin to raise your grant."; | |
| 661 | /** $1 of builder time an hour, written through the usage ledger's own writer. */ | |
| 662 | const spend = (into: Repo, at: string, costUsd = 1): void => | |
| 663 | recordUsage(app.db, { | |
| 664 | runId: createRun(app.db, { repoId: into.id, kind: "agent" }).id, repoId: into.id, issueId: null, agentId: null, subscriptionId: null, | |
| 665 | tokensIn: 0, tokensOut: 0, costUsd, wallMs: 3600_000, outcome: "ok", at, | |
| 666 | }); | |
| 667 | const fresh = (into: Repo) => getRepoById(app.db, into.id)!; | |
| 668 | const proposal = (into: Repo, name: string, appetite = 20) => | |
| 669 | insertBet(app.db, { repoId: into.id, name, journeys: "", appetite, leadId: null, issueIds: [] }, NOW); | |
| 670 | ||
| 671 | test("the sentence refuses only past the margin, counts what is running, and says nothing without a grant or history", () => { | |
| 672 | expect(walletRefusal(10, 0.01, 1000)).toBeNull(); | |
| 673 | expect(walletRefusal(10, 0.01, 1250)).toBeNull(); | |
| 674 | expect(walletRefusal(undefined, 0.01, 1e9)).toBeNull(); | |
| 675 | expect(walletRefusal(10, undefined, 1e9)).toBeNull(); | |
| 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}`, | |
| 678 | ); | |
| 679 | expect(walletRefusal(10, 0.01, 1000, 1000)).toContain("the wallet has $0.00 free"); | |
| 680 | }); | |
| 681 | ||
| 682 | test("costRate is dollars per builder second over 30 days, and nothing without cost or history", async () => { | |
| 683 | const into = await blank("wallet-rate"); | |
| 684 | expect(costRate(app.db, into.id, NOW)).toBeUndefined(); | |
| 685 | spend(into, "2026-11-02T10:00:00.000Z", 0); | |
| 686 | expect(costRate(app.db, into.id, NOW)).toBeUndefined(); | |
| 687 | spend(into, "2026-09-30T10:00:00.000Z", 50); | |
| 688 | expect(costRate(app.db, into.id, NOW)).toBeUndefined(); | |
| 689 | spend(into, "2026-11-02T10:00:00.000Z", 1); | |
| 690 | // The free run's hour counts too: $1 over two builder hours. | |
| 691 | expect(costRate(app.db, into.id, NOW)).toBeCloseTo(1 / 7200, 10); | |
| 692 | }); | |
| 693 | ||
| 694 | test("backing a bet the wallet cannot cover is refused with the sentence; a smaller share goes through", async () => { | |
| 695 | const into = await blank("wallet-bets"); | |
| 696 | const bet = proposal(into, "Costly"); | |
| 697 | setModelGrant(app.db, alice.id, 100, alice.id, NOW); | |
| 698 | spend(into, "2026-11-02T10:00:00.000Z"); | |
| 699 | expect(budgetSeconds(100, fresh(into).autonomySlots) / 3600).toBeGreaterThan(125); | |
| 700 | const refused = backBet(app, alice, fresh(into), bet.id, 100, NOW); | |
| 701 | expect(refused.ok).toBe(false); | |
| 702 | expect(!refused.ok && refused.error.message).toContain("the wallet has $100.00 free"); | |
| 703 | expect(!refused.ok && refused.error.message).toContain(WORD); | |
| 704 | expect(getBet(app.db, bet.id)!.state).toBe("proposed"); | |
| 705 | expect(backBet(app, alice, fresh(into), bet.id, 1, NOW).ok).toBe(true); | |
| 706 | }); | |
| 707 | ||
| 708 | test("two bets that each fit alone are refused together: the running one's estimate comes off the wallet", async () => { | |
| 709 | const into = await blank("wallet-two"); | |
| 710 | const first = proposal(into, "First"); | |
| 711 | const second = proposal(into, "Second"); | |
| 712 | setModelGrant(app.db, alice.id, 100, alice.id, NOW); | |
| 713 | spend(into, "2026-11-02T10:00:00.000Z"); | |
| 714 | const slots = fresh(into).autonomySlots; | |
| 715 | // One slot-week is $168 at $1 an hour: 60% is ~$100.8 and 40% ~$67.2, each inside the wallet's | |
| 716 | // margin. | |
| 717 | expect(slots).toBe(1); | |
| 718 | expect(backBet(app, alice, fresh(into), first.id, 60, NOW).ok).toBe(true); | |
| 719 | const refused = backBet(app, alice, fresh(into), second.id, 40, NOW); | |
| 720 | expect(refused.ok).toBe(false); | |
| 721 | expect(!refused.ok && refused.error.message).toContain("free after the bets already running"); | |
| 722 | }); | |
| 723 | ||
| 724 | test("the planner starts no bet the wallet cannot cover, and starts it once it can", async () => { | |
| 725 | const into = await blank("wallet-planner"); | |
| 726 | const bet = proposal(into, "Idle"); | |
| 727 | const later = new Date(NOW.getTime() + 2 * 86400_000); | |
| 728 | spend(into, "2026-11-02T10:00:00.000Z"); | |
| 729 | setModelGrant(app.db, alice.id, 5, alice.id, NOW); | |
| 730 | expect(tickBets(app, fresh(into), later).started).toBe(0); | |
| 731 | expect(getBet(app.db, bet.id)!.state).toBe("proposed"); | |
| 732 | setModelGrant(app.db, alice.id, 500, alice.id, NOW); | |
| 733 | expect(tickBets(app, fresh(into), later).started).toBe(1); | |
| 734 | expect(getBet(app.db, bet.id)!.state).toBe("backed"); | |
| 735 | }); | |
| 736 | ||
| 737 | describe("at the doors", () => { | |
| 738 | const now = () => new Date().toISOString(); | |
| 739 | const poorNotes = (): void => { | |
| 740 | app.db.query("DELETE FROM bet WHERE repo_id = ?1").run(repo.id); | |
| 741 | setModelGrant(app.db, alice.id, 1, alice.id, new Date()); | |
| 742 | spend(repo, now()); | |
| 743 | }; | |
| 744 | const restore = (): void => { | |
| 745 | setModelGrant(app.db, alice.id, null, alice.id, new Date()); | |
| 746 | app.db.query("DELETE FROM usage WHERE repo_id = ?1").run(repo.id); | |
| 747 | }; | |
| 748 | ||
| 749 | test("MCP says the same sentence and leaves the bet proposed", async () => { | |
| 750 | poorNotes(); | |
| 751 | try { | |
| 752 | const b = fresh2("McpWallet", []); | |
| 753 | const r = await betTool.run(mcpCtx(alice, "rw"), alice, { app: "alice/notes", verb: "back", bet: b.id, share: 100 }); | |
| 754 | expect(r.ok).toBe(false); | |
| 755 | expect(JSON.stringify(r)).toContain(WORD); | |
| 756 | expect(stateOf(b.id)).toBe("proposed"); | |
| 757 | } finally { | |
| 758 | restore(); | |
| 759 | } | |
| 760 | }); | |
| 761 | ||
| 762 | test("the terminal shows it and leaves the bet proposed", async () => { | |
| 763 | poorNotes(); | |
| 764 | try { | |
| 765 | const b = fresh2("TermWallet", []); | |
| 766 | const terminal = headlessTerminal(160, 32); | |
| 767 | const identity: TuiIdentity = { sshUsername: "t", remoteAddress: "127.0.0.1", user: alice }; | |
| 768 | const { app: tui } = createForgeApp(app, identity, { terminal, exitHooks: false }); | |
| 769 | tui.start(); | |
| 770 | const key = async (k: string) => { | |
| 771 | tui.dispatch({ type: "key", key: k, ctrl: k === "k" && false, alt: false, shift: false }); | |
| 772 | await settleApp(tui); | |
| 773 | }; | |
| 774 | tui.dispatch({ type: "key", key: "k", ctrl: true, alt: false, shift: false }); | |
| 775 | await settleApp(tui); | |
| 776 | for (const ch of "Bets") await key(ch); | |
| 777 | await key("enter"); | |
| 778 | await key("b"); | |
| 779 | for (const ch of "\b\b\b100") await key(ch === "\b" ? "backspace" : ch); | |
| 780 | await key("enter"); | |
| 781 | expect(tui.screen.toText()).toContain("wallet has"); | |
| 782 | expect(stateOf(b.id)).toBe("proposed"); | |
| 783 | tui.stop(); | |
| 784 | } finally { | |
| 785 | restore(); | |
| 786 | } | |
| 787 | }); | |
| 788 | }); | |
| 789 | }); |
test/refusals-lead-somewhere.test.ts +1
| @@ -42,6 +42,7 @@ const TERMINAL: Readonly<Record<string, string>> = { | ||
| 42 | 42 | "src/http/respond.ts\tYour token's scope or your role on this resource": "The same hint, by code.", |
| 43 | 43 | "src/actions/migrate.ts\tThat address is inside the network, not a public": "It gives the reader's own way through in the same sentence: connect the host as an origin.", |
| 44 | 44 | "src/actions/contribute_token.ts\tthis repository does not take outside contributions": "A door its owner chose to keep shut, said as a fact to a stranger with no account here.", |
| 45 | "src/crew/bets.ts\tThis bet is estimated at ${usd(estimate)} but the wallet": "It gives the reader's own way through in the same sentence (lower the share); the grant is the owner's to raise, and the sentence is shared by the page, the planner and the tool.", | |
| 45 | 46 | "src/actions/spawn.ts\t${name} is already claimed; ask its owner to": "The owner is of another forge (the hosted one), which this forge cannot ask on anyone's behalf.", |
| 46 | 47 | }; |
| 47 | 48 |
new above
offline