Public: anyone can read this; the crew can change it.
Re-picking after an unopenable token keeps the budget reservation, so a budgeted account stays capped #15010
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14995-re-picking-after-an-unopenable-token-kee → main 1 commit fork
2 files
changed +116
−1
src/core/subscriptions.ts +9 −1
| @@ -711,7 +711,15 @@ export function pickSubscription( | ||
| 711 | 711 | new Date(now.getTime() + 24 * 3_600_000).toISOString(), |
| 712 | 712 | "its token would not open; add it again", |
| 713 | 713 | ); |
| 714 | return pickSubscription(db, dataDir, scope, now); | |
| 714 | // The pick goes on with the same reserve: dropped, the run's own | |
| 715 | // reservation from an earlier attempt counted against it as another | |
| 716 | // run's, so a budgeted account looked spent and the run went uncapped | |
| 717 | // (#14995). What it held on this account is let go. | |
| 718 | if (reserve !== undefined) | |
| 719 | db.query( | |
| 720 | "UPDATE run SET budget_reserved_usd = NULL WHERE id = ?1 AND subscription_id = ?2", | |
| 721 | ).run(reserve.runId, awake.id); | |
| 722 | return pickSubscription(db, dataDir, scope, now, reserve); | |
| 715 | 723 | } |
| 716 | 724 | const subscription = toSubscription(awake); |
| 717 | 725 | touchSubscription(db, awake.id, now); |
test/subscription-unopenable-repick.test.ts added +107
| @@ -0,0 +1,107 @@ | ||
| 1 | /** | |
| 2 | * An account whose token will not open is rested and the pick goes on to the | |
| 3 | * next on the grant (#14995). The pick goes on with the run's reserve: its | |
| 4 | * own reservation from an earlier attempt is not counted against it, the | |
| 5 | * next account's reservation is made for it, and what it held on the | |
| 6 | * unopenable account is let go. | |
| 7 | */ | |
| 8 | ||
| 9 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 10 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 11 | import { tmpdir } from "node:os"; | |
| 12 | import { join } from "node:path"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import type { Repo } from "../src/core/repos.ts"; | |
| 16 | import { createRun } from "../src/core/runs.ts"; | |
| 17 | import { addSubscription, EVERYONE, pickSubscription, setBudget, setGrants } from "../src/core/subscriptions.ts"; | |
| 18 | import { createUser } from "../src/core/users.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | let dataDir = ""; | |
| 24 | let app: App; | |
| 25 | let repo: Repo; | |
| 26 | let broken = ""; | |
| 27 | let good = ""; | |
| 28 | ||
| 29 | const scope = () => ({ repoId: repo.id, ownerId: repo.ownerId, agentId: "nobody" }); | |
| 30 | const reservation = (runId: string) => | |
| 31 | app.db | |
| 32 | .query<{ subscription_id: string | null; budget_reserved_usd: number | null }, [string]>( | |
| 33 | "SELECT subscription_id, budget_reserved_usd FROM run WHERE id = ?1", | |
| 34 | ) | |
| 35 | .get(runId)!; | |
| 36 | ||
| 37 | beforeAll(async () => { | |
| 38 | dataDir = await mkdtemp(join(tmpdir(), "forge-unopenable-")); | |
| 39 | app = createApp({ dataDir, port: 0 }); | |
| 40 | const u = await createUser(app.db, { | |
| 41 | origin: "seed", | |
| 42 | trusted: true, | |
| 43 | username: "ada", | |
| 44 | email: "[email protected]", | |
| 45 | password: "a-long-unopenable-passphrase", | |
| 46 | isAdmin: true, | |
| 47 | }); | |
| 48 | if (!u.ok) throw new Error("user"); | |
| 49 | const r = await createRepo(app, u.value, { name: "mill" }); | |
| 50 | if (!r.ok) throw new Error("repo"); | |
| 51 | repo = r.value; | |
| 52 | const a = addSubscription(app.db, dataDir, { name: "a-broken", token: "sk-ant-api03-broken-key-for-the-repick-test" }); | |
| 53 | const b = addSubscription(app.db, dataDir, { name: "b-good", token: "sk-ant-api03-good-key-for-the-repick-test" }); | |
| 54 | if (!a.ok || !b.ok) throw new Error("sub"); | |
| 55 | broken = a.value.id; | |
| 56 | good = b.value.id; | |
| 57 | setGrants(app.db, repo.id, EVERYONE, [broken, good]); | |
| 58 | expect(setBudget(app.db, broken, 5)).toBe(true); | |
| 59 | expect(setBudget(app.db, good, 5)).toBe(true); | |
| 60 | }, 30_000); | |
| 61 | ||
| 62 | afterAll(async () => { | |
| 63 | app.close(); | |
| 64 | await rm(dataDir, { recursive: true, force: true }); | |
| 65 | }); | |
| 66 | ||
| 67 | /** The first account's token sealed for another account: it will not open. */ | |
| 68 | function breakFirst(): void { | |
| 69 | app.db.query("UPDATE run SET state = 'ok' WHERE state IN ('queued', 'running')").run(); | |
| 70 | app.db.query("UPDATE subscription SET resting_until = NULL, last_used_at = NULL WHERE id IN (?1, ?2)").run(broken, good); | |
| 71 | app.db.query("UPDATE subscription SET last_used_at = '2000-01-01T00:00:00.000Z' WHERE id = ?1").run(good); | |
| 72 | app.db | |
| 73 | .query("UPDATE subscription SET token_enc = (SELECT token_enc FROM subscription WHERE id = ?2) WHERE id = ?1") | |
| 74 | .run(broken, good); | |
| 75 | } | |
| 76 | ||
| 77 | test("a re-pick past an unopenable token does not count the run's own reservation against it", () => { | |
| 78 | breakFirst(); | |
| 79 | const run = createRun(app.db, { repoId: repo.id, kind: "agent" }); | |
| 80 | // An earlier attempt reserved $4.90 of the good account's $5 for this run. | |
| 81 | app.db.query("UPDATE run SET subscription_id = ?1, budget_reserved_usd = 4.9 WHERE id = ?2").run(good, run.id); | |
| 82 | const picked = pickSubscription(app.db, dataDir, scope(), new Date(), { runId: run.id, usd: 4 }); | |
| 83 | expect(picked.kind).toBe("ready"); | |
| 84 | if (picked.kind !== "ready") return; | |
| 85 | expect(picked.subscription.id).toBe(good); | |
| 86 | expect(picked.capUsd).toBe(4); | |
| 87 | expect(reservation(run.id)).toEqual({ subscription_id: good, budget_reserved_usd: 4 }); | |
| 88 | app.db.query("UPDATE run SET state = 'ok' WHERE id = ?1").run(run.id); | |
| 89 | }); | |
| 90 | ||
| 91 | test("the reservation held on the unopenable account is let go and made on the next", () => { | |
| 92 | breakFirst(); | |
| 93 | const run = createRun(app.db, { repoId: repo.id, kind: "agent" }); | |
| 94 | app.db.query("UPDATE run SET subscription_id = ?1, budget_reserved_usd = 3 WHERE id = ?2").run(broken, run.id); | |
| 95 | const picked = pickSubscription(app.db, dataDir, scope(), new Date(), { runId: run.id, usd: 2 }); | |
| 96 | expect(picked.kind === "ready" && picked.subscription.id).toBe(good); | |
| 97 | expect(picked.kind === "ready" && picked.capUsd).toBe(2); | |
| 98 | expect(reservation(run.id)).toEqual({ subscription_id: good, budget_reserved_usd: 2 }); | |
| 99 | app.db.query("UPDATE run SET state = 'ok' WHERE id = ?1").run(run.id); | |
| 100 | }); | |
| 101 | ||
| 102 | test("a look without a reserve still passes the unopenable account and reserves nothing", () => { | |
| 103 | breakFirst(); | |
| 104 | const picked = pickSubscription(app.db, dataDir, scope(), new Date()); | |
| 105 | expect(picked.kind === "ready" && picked.subscription.id).toBe(good); | |
| 106 | expect(picked.kind === "ready" && picked.capUsd).toBeUndefined(); | |
| 107 | }); |
new above
offline