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

Merged opened this

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

Merged

trevato/claim-journey-credit-spent → main 1 commit

  • Merged. trevato/claim-journey-credit-spent was merged into main as d6e4919.

Review

Sign in to review this change.
3 files changed +41 −3
src/crew/runner.ts +10 −1
@@ -207,7 +207,7 @@ import {
207207 } from "./guard.ts";
208208 import { passportEnv } from "./passport_env.ts";
209209 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";
211211
212212 const log = createLog("crew");
213213
@@ -1431,6 +1431,8 @@ async function work(
14311431 }
14321432 const heldLeft = lentCreditLeft(picked.kind === "none", app.config.dataDir);
14331433 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);
14341436 const access = modelAccess(app, picked, repo.ownerId, repo.ownerName);
14351437 if (!access.ok) {
14361438 write(access.said);
@@ -1440,6 +1442,13 @@ async function work(
14401442 // What this run would spend before the account's ceilings: its own
14411443 // budget, under the subscription's reservation or the grant's rest.
14421444 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 }
14431452 const wantedBy =
14441453 picked.kind === "ready" && picked.capUsd !== undefined
14451454 ? Math.min(ownBudget, picked.capUsd)
src/spawn/funding.ts +15
@@ -171,6 +171,21 @@ export function lentCreditLeft(draws: boolean, dataDir: string): number | undefi
171171 export const cappedByLent = (wanted: number, lentLeft: number | undefined): number =>
172172 lentLeft === undefined ? wanted : Math.min(wanted, lentLeft);
173173
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
174189 /**
175190 * What a run cost that the host could have counted: on the owner's own key the
176191 * 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";
99 import { tmpdir } from "node:os";
1010 import { join } from "node:path";
1111 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";
1415 import { includedCredit, uncountedWords } from "../src/actions/crew_means.ts";
1516 import { crewNote } from "../src/web/studio/crew_key.ts";
1617
@@ -60,3 +61,16 @@ test("on the owner's own key no cost is passed, so no false 'not counted' note (
6061 await runEnded(child, same, { costUsd: lentCost(child, 0.78) });
6162 expect(fundingOf(dir)?.uncountedUsd).toBeUndefined();
6263 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs