Public: anyone can read this; the crew can change it.
Spent credit is said in plain words before a run starts, never the model tool's own error #13789
Check · Review · Land · Live done — Landed and live in production.
trevato/claim-journey-credit-spent → main 1 commit
3 files
changed +41
−3
src/crew/runner.ts +10 −1
| @@ -207,7 +207,7 @@ import { | ||
| 207 | 207 | } from "./guard.ts"; |
| 208 | 208 | import { passportEnv } from "./passport_env.ts"; |
| 209 | 209 | import { hostOfApiBase, isGatewayAddress, ownEnv, sandboxTmpEnv } from "./runner_env.ts"; |
| 210 | import { cappedByLent, fundedEnv, fundingOf, lentCost, lentCreditLeft, refreshFundingOnce, runEnded } from "../spawn/funding.ts"; | |
| 210 | import { cappedByLent, fundedEnv, fundingOf, lentCost, lentCreditLeft, lentSpent, refreshFundingOnce, runEnded } from "../spawn/funding.ts"; | |
| 211 | 211 | |
| 212 | 212 | const log = createLog("crew"); |
| 213 | 213 | |
| @@ -1431,6 +1431,8 @@ async function work( | ||
| 1431 | 1431 | } |
| 1432 | 1432 | const heldLeft = lentCreditLeft(picked.kind === "none", app.config.dataDir); |
| 1433 | 1433 | if (picked.kind === "none" && (heldLeft === undefined || heldLeft < MIN_RUN_USD)) await refreshFundingOnce(app); |
| 1434 | // The forge's own key drawing on lent credit: the same stale copy, asked about the same way. | |
| 1435 | if (picked.kind === "env" && lentSpent(lentCreditLeft(true, app.config.dataDir), MIN_RUN_USD) !== undefined) await refreshFundingOnce(app); | |
| 1434 | 1436 | const access = modelAccess(app, picked, repo.ownerId, repo.ownerName); |
| 1435 | 1437 | if (!access.ok) { |
| 1436 | 1438 | write(access.said); |
| @@ -1440,6 +1442,13 @@ async function work( | ||
| 1440 | 1442 | // What this run would spend before the account's ceilings: its own |
| 1441 | 1443 | // budget, under the subscription's reservation or the grant's rest. |
| 1442 | 1444 | const lentLeft = lentCreditLeft(picked.kind === "env" || picked.kind === "none", app.config.dataDir); |
| 1445 | // Spent credit is said in the forge's words before anything starts: a run budgeted $0 would | |
| 1446 | // only hand the person the model tool's refusal of that figure. | |
| 1447 | const spentSaid = lentSpent(lentLeft, MIN_RUN_USD); | |
| 1448 | if (spentSaid !== undefined) { | |
| 1449 | write(spentSaid); | |
| 1450 | return { state: "failed", summary: spentSaid }; | |
| 1451 | } | |
| 1443 | 1452 | const wantedBy = |
| 1444 | 1453 | picked.kind === "ready" && picked.capUsd !== undefined |
| 1445 | 1454 | ? Math.min(ownBudget, picked.capUsd) |
src/spawn/funding.ts +15
| @@ -171,6 +171,21 @@ export function lentCreditLeft(draws: boolean, dataDir: string): number | undefi | ||
| 171 | 171 | export const cappedByLent = (wanted: number, lentLeft: number | undefined): number => |
| 172 | 172 | lentLeft === undefined ? wanted : Math.min(wanted, lentLeft); |
| 173 | 173 | |
| 174 | /** | |
| 175 | * What a run is told when the credit it would draw on is spent: why it did not start, and the two | |
| 176 | * ways on. | |
| 177 | */ | |
| 178 | export const CREDIT_SPENT_SAID = | |
| 179 | "The included credit is spent, so the crew did not start on this. Add your own key on the home page to keep building, or wait for next month."; | |
| 180 | ||
| 181 | /** | |
| 182 | * The sentence for a run that would draw on lent credit with less than `min` dollars of it left; | |
| 183 | * undefined when it may start. Without it the run starts on a budget of $0 and the person reads | |
| 184 | * the model tool's own refusal of that figure. | |
| 185 | */ | |
| 186 | export const lentSpent = (lentLeft: number | undefined, min: number): string | undefined => | |
| 187 | lentLeft !== undefined && lentLeft < min ? CREDIT_SPENT_SAID : undefined; | |
| 188 | ||
| 174 | 189 | /** |
| 175 | 190 | * What a run cost that the host could have counted: on the owner's own key the |
| 176 | 191 | * lent credit was never involved, so nothing (#13715). |
test/credit-moves.test.ts +16 −2
| @@ -9,8 +9,9 @@ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; | ||
| 9 | 9 | import { tmpdir } from "node:os"; |
| 10 | 10 | import { join } from "node:path"; |
| 11 | 11 | import { createApp, type App } from "../src/app.ts"; |
| 12 | import { addSubscription } from "../src/core/subscriptions.ts"; | |
| 13 | import { fundingOf, keepFunding, lentCost, refreshFunding, runEnded } from "../src/spawn/funding.ts"; | |
| 12 | import { addSubscription, MIN_RUN_USD } from "../src/core/subscriptions.ts"; | |
| 13 | import { pauseCause } from "../src/crew/pacing.ts"; | |
| 14 | import { CREDIT_SPENT_SAID, fundingOf, keepFunding, lentCost, lentCreditLeft, lentSpent, refreshFunding, runEnded } from "../src/spawn/funding.ts"; | |
| 14 | 15 | import { includedCredit, uncountedWords } from "../src/actions/crew_means.ts"; |
| 15 | 16 | import { crewNote } from "../src/web/studio/crew_key.ts"; |
| 16 | 17 | |
| @@ -60,3 +61,16 @@ test("on the owner's own key no cost is passed, so no false 'not counted' note ( | ||
| 60 | 61 | await runEnded(child, same, { costUsd: lentCost(child, 0.78) }); |
| 61 | 62 | expect(fundingOf(dir)?.uncountedUsd).toBeUndefined(); |
| 62 | 63 | }); |
| 64 | ||
| 65 | test("spent credit stops a run before it starts, in words a person can act on", async () => { | |
| 66 | // Credit of its own left, or no lent credit at all (a forge on its own key): the run may start. | |
| 67 | expect(lentSpent(undefined, MIN_RUN_USD)).toBeUndefined(); | |
| 68 | expect(lentSpent(MIN_RUN_USD, MIN_RUN_USD)).toBeUndefined(); | |
| 69 | // Spent: never a budget of $0 handed to the model's tool, whose refusal the person would read. | |
| 70 | await refreshFunding(child, async () => new Response(JSON.stringify({ capUsd: 2, spentUsd: 2 }))); | |
| 71 | const said = lentSpent(lentCreditLeft(true, dir), MIN_RUN_USD); | |
| 72 | expect(said).toBe(CREDIT_SPENT_SAID); | |
| 73 | expect(said).toContain("Add your own key"); | |
| 74 | expect(said).not.toMatch(/budget|usd|--|error|exit/i); | |
| 75 | expect(pauseCause(CREDIT_SPENT_SAID)).toBe("credit"); | |
| 76 | }); |
new above
offline