Public: anyone can read this; the crew can change it.

Talking to the forge's agent refuses "no model account" when the only grant is a dollar model grant #12955

Merged opened this in answer to

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

Merged

scout/forge:scout/12954-talking-to-the-forge-s-agent-refuses-no → main 7 commits fork 3 threads · 3 unresolved

  • Merged. scout/12954-talking-to-the-forge-s-agent-refuses-no was merged into main as fc910c0.

Review

Sign in to review this change.
7 files changed +158 −51
docs/features/talk.md +1 −1
@@ -6,7 +6,7 @@ One door, `talk()` in `src/actions/talk.ts`, three ways in. It sends the words a
66 - **MCP:** `say` with `text` and no `repo` or `number`. The agent's own token is refused, so it cannot wake itself.
77 - **Terminal:** `ssh <forge> talk <words>` (the words are not passed to a shell), and `forge talk <words>`, which prints the answer.
88
9 It needs a granted Claude account (`no_account`), and says `resting` when none can serve it or your daily ceiling is spent. At most four turns run across the forge at once (`TURNS_LIVE_MAX`); a new one past that is `busy` (429, `retry-after: 60`), while a message into your running turn always goes through. A message that is refused is not kept in the conversation.
9 It needs a way to spend on a model: a granted Claude account, a monthly dollar grant, or credit the host lent this forge (a forge admin uses the forge's own key). With none you get `no_account` (403), and the words say to ask an admin; the grant is set at /settings/accounts, which only an admin can open. It says `resting` (429) when the account is resting, a dollar grant is spent for the month, or your daily ceiling is spent. A turn on a grant or lent credit is budgeted at most what is left of it, and is booked against it. When the copy of the lent credit is short, the refusal also starts the one refresh of it from the host (at most once in 15 s), so the next message sees the fresh figure. At most four turns run across the forge at once (`TURNS_LIVE_MAX`); a new one past that is `busy` (429, `retry-after: 60`), while a message into your running turn always goes through. A message that is refused is not kept in the conversation.
1010
1111 `GET /api/v1/conversation/turns?limit=` takes 1 to 200 turns; absent, empty or not a number is 50.
1212
src/crew/conversation.ts +4 −5
@@ -18,7 +18,6 @@
1818 import { chmodSync, mkdirSync } from "node:fs";
1919 import { join } from "node:path";
2020 import type { App } from "../app.ts";
21 import { dailyLeft } from "../core/account_ceiling.ts";
2221 import { aboutContext } from "../core/talk_about.ts";
2322 import { asksWhatNeedsMe, decisionsAnswer } from "../actions/talk_decisions.ts";
2423 import { buildTurnEnded } from "../actions/build_pull.ts";
@@ -42,6 +41,7 @@ import { appendRunLog } from "../runs/log.ts";
4241 import { runAgent, type AgentOutcome, type RunnerLimit } from "./agent_runner.ts";
4342 import { CONVERSATION_BRIEF } from "./conversation_brief.ts";
4443 import { isRateLimited, takeLimit, whenText } from "./limits.ts";
44 import { turnBudget } from "./model_access.ts";
4545 import { credentialEnv, holdModelCredential, listenForSteer, steerRun } from "./runner.ts";
4646 import { sandboxPolicy } from "./sandbox.ts";
4747
@@ -487,11 +487,10 @@ function begin(
487487 runId,
488488 usd: options.budgetUsd,
489489 });
490 if (picked.kind === "none") return err("no_account");
491490 if (picked.kind === "resting") return err("resting");
492 const left = dailyLeft(app.db, userId, now);
493 const budget = Math.min(options.budgetUsd, picked.kind === "ready" ? (picked.capUsd ?? Infinity) : Infinity, left ?? Infinity);
494 if (budget <= 0) return err("resting");
491 const spend = turnBudget(app, picked, userId, options.budgetUsd, now);
492 if (!spend.ok) return err(spend.refusal);
493 const budget = spend.budget;
outdated

Same access and budget lines as one_shot.ts; fold into model_access.ts. Lent credit does not cap the budget here and is not refreshed before refusing.

495494 const subscriptionId = picked.kind === "ready" ? picked.subscription.id : PLATFORM;
496495 // A new turn's token replaces the last one's.
497496 const via = `conversation:${conversation.id}`;
src/crew/model_access.ts added +79
@@ -0,0 +1,79 @@
1 /**
2 * Model access: may this account spend on a model, by any grant? One question,
3 * asked by the crew's runner and by the talk path alike, so a grant that lets
4 * one run lets the other.
5 *
6 * A subscription pick that is `none` is not yet a refusal: the owner's capped
7 * monthly dollar grant (`core/model_grant.ts`) and the host's lent credit are
8 * ways past it. Plain data in, plain data out.
9 */
10 import type { App } from "../app.ts";
11 import { dailyLeft } from "../core/account_ceiling.ts";
12 import { grantLeft } from "../core/model_grant.ts";
13 import { storedText } from "../kernel/blocked.ts";
14 import { MIN_RUN_USD, type Pick } from "../core/subscriptions.ts";
15 import { getUserById } from "../core/users.ts";
16 import { cappedByLent, lentCreditLeft, refreshFundingOnce } from "../spawn/funding.ts";
17 import { isOwnForge, NO_ACCOUNT_SAID, NO_KEY_SAID } from "./forge_budget.ts";
18
19 export type ModelAccess =
20 | {
21 ok: true;
22 /** Dollars left of the owner's grant this month, when the way in is a dollar grant. */
23 grantCap?: number | undefined;
24 }
25 | { ok: false; said: string; grantCap?: number | undefined };
26
27 /** What a refusal says: where an admin (or the person) lifts it. */
28 export function noGrantSaid(app: App, ownerId: string, ownerName: string): string {
29 if (isOwnForge(app) || getUserById(app.db, ownerId)?.isAdmin === true) return NO_KEY_SAID;
30 const row = app.db.query<{ kind: string }, [string]>("SELECT kind FROM user WHERE id = ?1").get(ownerId);
31 if (row?.kind !== "org") return NO_ACCOUNT_SAID;
32 return storedText(`The account \`${ownerName}\` has no model grant.`, "an admin of this forge", "grant one");
33 }
34
35 /**
36 * Whether `picked` (a subscription pick for `ownerId`) leaves a way to spend:
37 * a subscription, the platform key, a dollar grant, or lent credit.
38 */
39 export function modelAccess(app: App, picked: Pick, ownerId: string, ownerName: string): ModelAccess {
40 if (picked.kind !== "none") return { ok: true };
41 const grantCap = grantLeft(app.db, ownerId, new Date());
42 const lent = lentCreditLeft(true, app.config.dataDir);
43 if (lent !== undefined && lent >= MIN_RUN_USD) return { ok: true, grantCap };
44 if (grantCap !== undefined && grantCap >= MIN_RUN_USD) return { ok: true, grantCap };
45 const said = grantCap === undefined ? noGrantSaid(app, ownerId, ownerName) : storedText("This month's model grant is spent.", "an admin of this forge", "raise it");
46 return { ok: false, said, grantCap };
47 }
48
49 /** What a turn that asks the question gets: the dollars it may spend, or why it may not. */
50 export type TurnBudget = { ok: true; budget: number } | { ok: false; refusal: "no_account" | "resting"; said: string };
51
52 /**
53 * The talk path's whole question in one place (conversation turns and one-shot asks):
54 * may this person spend, and how much. The budget is the smallest of what the turn wants,
55 * the subscription's cap, the grant's rest, the host's lent credit left and the day's rest.
56 *
57 * Talk answers synchronously, so it cannot await the host as the runner does: when the lent
58 * credit copy is short it starts the one bounded refresh (`refreshFundingOnce`) behind the
59 * refusal, and the next message reads the fresh figure.
60 */
61 export function turnBudget(app: App, picked: Pick, userId: string, wantUsd: number, now: Date): TurnBudget {
62 const username = getUserById(app.db, userId)?.username ?? "";
63 if (picked.kind === "none") {
64 const held = lentCreditLeft(true, app.config.dataDir);
65 if (held === undefined || held < MIN_RUN_USD)
66 void refreshFundingOnce(app).catch((error: unknown) => app.log.error("funding refresh failed", { error: String(error) }));
67 }
68 const access = modelAccess(app, picked, userId, username);
69 if (!access.ok) return { ok: false, refusal: access.grantCap === undefined ? "no_account" : "resting", said: access.said };
70 const drawsLent = picked.kind === "none" || picked.kind === "env";
71 const wanted = Math.min(
72 wantUsd,
73 picked.kind === "ready" ? (picked.capUsd ?? Infinity) : Infinity,
74 picked.kind === "none" ? (access.grantCap ?? Infinity) : Infinity,
75 dailyLeft(app.db, userId, now) ?? Infinity,
76 );
77 const budget = cappedByLent(wanted, lentCreditLeft(drawsLent, app.config.dataDir));
78 return budget > 0 ? { ok: true, budget } : { ok: false, refusal: "resting", said: "no budget is left" };
79 }
src/crew/one_shot.ts +4 −5
@@ -8,7 +8,6 @@
88 */
99
1010 import type { App } from "../app.ts";
11 import { dailyLeft } from "../core/account_ceiling.ts";
1211 import { PLATFORM, pickSubscription } from "../core/subscriptions.ts";
1312 import { id } from "../kernel/id.ts";
1413 import { redactValues, releaseValues } from "../kernel/log.ts";
@@ -16,6 +15,7 @@ import { err, ok, type Result } from "../kernel/result.ts";
1615 import { runAgent } from "./agent_runner.ts";
1716 import { conversationOptions, sessionsDir, turnDir } from "./conversation.ts";
1817 import { isRateLimited } from "./limits.ts";
18 import { turnBudget } from "./model_access.ts";
1919 import { credentialEnv, holdModelCredential } from "./runner.ts";
2020 import { sandboxPolicy } from "./sandbox.ts";
2121
@@ -32,11 +32,10 @@ export async function oneShot(app: App, userId: string, system: string, prompt:
3232 const now = new Date();
3333 const runId = id();
3434 const picked = pickSubscription(app.db, app.config.dataDir, { repoId: "", ownerId: userId, agentId: "" }, now, { runId, usd: ONE_SHOT_USD });
35 if (picked.kind === "none") return err("no_account");
3635 if (picked.kind === "resting") return err("resting");
37 const left = dailyLeft(app.db, userId, now);
38 const budget = Math.min(ONE_SHOT_USD, options.budgetUsd, picked.kind === "ready" ? (picked.capUsd ?? Infinity) : Infinity, left ?? Infinity);
39 if (budget <= 0) return err("resting");
36 const spend = turnBudget(app, picked, userId, Math.min(ONE_SHOT_USD, options.budgetUsd), now);
37 if (!spend.ok) return err(spend.refusal);
38 const budget = spend.budget;
4039 const subscriptionId = picked.kind === "ready" ? picked.subscription.id : PLATFORM;
4140 app.db
4241 .query("INSERT INTO one_shot_spend (id, user_id, subscription_id, cost_usd, at) VALUES (?1, ?2, ?3, ?4, ?5)")
src/crew/runner.ts +9 −38
@@ -157,7 +157,7 @@ import { laterGate } from "./later.ts";
157157 import { MERGES_CLEAN_NOW, mergesCleanNow } from "./clean_now.ts";
158158 import { handedLimbs } from "./handed_limbs.ts";
159159 import { parkedTwice, parkReasons } from "./parks.ts";
160 import { budgetLimit, budgetVerdict, cutOffBy, isOwnForge, NO_ACCOUNT_SAID, NO_KEY_SAID } from "./forge_budget.ts";
160 import { budgetLimit, budgetVerdict, cutOffBy, isOwnForge } from "./forge_budget.ts";
161161 import { boxesOf, landedBoxes } from "./box_pass.ts";
162162 import { reviewRange } from "./review_range.ts";
163163 import { heldNow, stuckOf } from "./held.ts";
@@ -166,6 +166,7 @@ import { resumeDecision } from "./resume.ts";
166166 import { owed } from "./team_share.ts";
167167 import { pauseReason } from "./pacing.ts";
168168 import { grantLeft } from "../core/model_grant.ts";
169 import { modelAccess } from "./model_access.ts";
169170 import { dailyLeft, reserveCeiling } from "../core/account_ceiling.ts";
170171 import {
171172 credentialEnvOf,
@@ -278,21 +279,6 @@ export function steerRun(app: App, runId: string, text: string): boolean {
278279 return send(text);
279280 }
280281
281 /** What a refused run says: an org is named with the page where an admin grants it; a person keeps the plain words. */
282 function noGrantSaid(app: App, ownerId: string, ownerName: string): string {
283 const db = app.db;
284 // A forge that is its owner's own, or a repository its admin owns, has no
285 // one else to ask: the key is theirs to add (#11515).
286 if (isOwnForge(app) || getUserById(db, ownerId)?.isAdmin === true)
287 return NO_KEY_SAID;
288 const org = db
289 .query<{ kind: string }, [string]>("SELECT kind FROM user WHERE id = ?1")
290 .get(ownerId)?.kind === "org";
291 return org
292 ? storedText(`The account \`${ownerName}\` has no model grant.`, "an admin of this forge", "grant one")
293 : NO_ACCOUNT_SAID;
294 }
295
296282 /**
297283 * What a reviewer reads: the pull's diff (`review_range.ts` — a landed pull's
298284 * is its merge commit's), and its pictures. `landedAs` names the merge commit
@@ -1424,31 +1410,16 @@ async function work(
14241410 // else's run waits for a grant rather than spend what is not theirs.
14251411 // The owner's capped monthly grant (`core/model_grant.ts`) is the one
14261412 // way past it: the forge's token, up to what is left of the month.
1427 const grantCap =
1428 picked.kind === "none"
1429 ? grantLeft(app.db, repo.ownerId, new Date())
1430 : undefined;
1431 // The host's included credit is a way past it too (#12105): a hosted forge
1432 // before it is claimed owns its apps through an org, which is no admin.
1433 // A copy that says there is too little may be stale (a raise the host made
1434 // while this forge was down): ask the host before refusing the run (at most once in 15 s,
1435 // whatever the queue).
1413 // The host's included credit is a way past it too (#12105); a copy that says
1414 // there is too little may be stale: ask the host before refusing (at most once in 15 s).
14361415 const heldLeft = lentCreditLeft(picked.kind === "none", app.config.dataDir);
14371416 if (picked.kind === "none" && (heldLeft === undefined || heldLeft < MIN_RUN_USD)) await refreshFundingOnce(app);
1438 const lentFirst = lentCreditLeft(picked.kind === "none", app.config.dataDir);
1439 const lentOpen = lentFirst !== undefined && lentFirst >= MIN_RUN_USD;
1440 if (
1441 picked.kind === "none" &&
1442 !lentOpen &&
1443 (grantCap === undefined || grantCap < MIN_RUN_USD)
1444 ) {
1445 const said =
1446 grantCap === undefined
1447 ? noGrantSaid(app, repo.ownerId, repo.ownerName)
1448 : storedText("This month's model grant is spent.", "an admin of this forge", "raise it");
1449 write(said);
1450 return { state: "failed", summary: said };
1417 const access = modelAccess(app, picked, repo.ownerId, repo.ownerName);
1418 if (!access.ok) {
1419 write(access.said);
1420 return { state: "failed", summary: access.said };
14511421 }
1422 const grantCap = picked.kind === "none" ? access.grantCap : undefined;
14521423 // What this run would spend before the account's ceilings: its own
14531424 // budget, under the subscription's reservation or the grant's rest.
14541425 const lentLeft = lentCreditLeft(picked.kind === "env" || picked.kind === "none", app.config.dataDir);
test/conversation-talk.test.ts +35 −1
@@ -5,7 +5,7 @@
55 */
66
77 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
8 import { chmodSync, existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
8 import { chmodSync, existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
99 import { mkdtemp, rm } from "node:fs/promises";
1010 import { tmpdir } from "node:os";
1111 import { join } from "node:path";
@@ -13,6 +13,7 @@ import { createApp, type App } from "../src/app.ts";
1313 import { configureConversation } from "../src/crew/conversation.ts";
1414 import { addKey, fingerprint, parseAuthorizedKey } from "../src/core/keys.ts";
1515 import { WORDS_MAX } from "../src/core/fence.ts";
16 import { setModelGrant } from "../src/core/model_grant.ts";
1617 import { createToken } from "../src/core/tokens.ts";
1718 import { createUser } from "../src/core/users.ts";
1819 import { mintViaToken } from "../src/core/via_token.ts";
@@ -179,6 +180,39 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Heard.","t
179180 expect(((await read.json()) as { turns: unknown[] }).turns).toEqual([]);
180181 });
181182
183 test("a person whose only grant is a dollar grant can talk; with neither the refusal says to ask an admin", async () => {
184 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "erin", email: "[email protected]", password: "a-good-passphrase" });
185 if (!made.ok) throw new Error(made.error);
186 const erin = createToken(app.db, made.value.id, "personal", "rw").token;
187 const send = () => fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: headers(erin), body: JSON.stringify({ text: "hi" }) });
188 const refused = await send();
189 expect(refused.status).toBe(403);
190 expect(JSON.stringify(await refused.json())).toContain("An admin of this forge can grant one");
191 expect(setModelGrant(app.db, made.value.id, 5, made.value.id, new Date())).toBe(true);
192 const ok = await send();
193 expect(ok.status).toBe(202);
outdated

not.toBe(403) passes on any failure. Assert the success status, and add spent-grant => resting and lent-credit cases.

194 // The message was taken, not refused: it is on the person's own conversation.
195 const read = await fetch(`${servers.url}/api/v1/conversation/turns`, { headers: headers(erin) });
196 expect(JSON.stringify(await read.json())).toContain('"hi"');
197 });
198
199 test("a spent dollar grant is resting (429), and the host's lent credit alone lets a turn through", async () => {
200 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "finn", email: "[email protected]", password: "a-good-passphrase" });
201 if (!made.ok) throw new Error(made.error);
202 const finn = createToken(app.db, made.value.id, "personal", "rw").token;
203 const send = () => fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: headers(finn), body: JSON.stringify({ text: "hi" }) });
204 expect(setModelGrant(app.db, made.value.id, 0.1, made.value.id, new Date())).toBe(true);
205 expect((await send()).status).toBe(429);
206 setModelGrant(app.db, made.value.id, null, made.value.id, new Date());
207 expect((await send()).status).toBe(403);
208 writeFileSync(join(dir, "parent.json"), JSON.stringify({ credit: { token: "bearer-token-1", capUsd: 5, spentUsd: 1 } }));
209 try {
210 expect((await send()).status).toBe(202);
211 } finally {
212 rmSync(join(dir, "parent.json"));
213 }
214 });
215
182216 test("the turns page size is clamped, and junk is the default", async () => {
183217 expect(turnsLimit(null)).toBe(50);
184218 expect(turnsLimit("")).toBe(50);
test/crew-one-shot.test.ts +26 −1
@@ -1,11 +1,13 @@
11 /** oneShot (#9569): the refusals, the budget clamp and the cost booking, against a fake binary. */
22 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { chmodSync, readFileSync, writeFileSync } from "node:fs";
3 import { chmodSync, readFileSync, rmSync, writeFileSync } from "node:fs";
44 import { mkdtemp, rm } from "node:fs/promises";
55 import { tmpdir } from "node:os";
66 import { join } from "node:path";
77 import { createApp } from "../src/app.ts";
88 import { dailyLeft, setAccountCeiling } from "../src/core/account_ceiling.ts";
9 import { turnBudget } from "../src/crew/model_access.ts";
10 import { setModelGrant } from "../src/core/model_grant.ts";
911 import { createUser } from "../src/core/users.ts";
1012 import { configureConversation } from "../src/crew/conversation.ts";
1113 import { ONE_SHOT_USD, oneShot } from "../src/crew/one_shot.ts";
@@ -40,6 +42,29 @@ describe("oneShot", () => {
4042 on();
4143 expect(await oneShot(app, member, "s", "p")).toEqual({ ok: false, error: "no_account" });
4244 });
45 test("a member whose only grant is a dollar grant may ask, and one with neither may not", async () => {
46 on();
47 expect(await oneShot(app, member, "s", "p")).toEqual({ ok: false, error: "no_account" });
48 expect(setModelGrant(app.db, member, 5, admin, new Date())).toBe(true);
49 expect(await oneShot(app, member, "s", "p")).toEqual({ ok: true, value: "{}" });
50 setModelGrant(app.db, member, null, admin, new Date());
51 expect(await oneShot(app, member, "s", "p")).toEqual({ ok: false, error: "no_account" });
52 });
53 test("a spent grant is resting, not a missing account", async () => {
54 on();
55 expect(setModelGrant(app.db, member, 0.1, admin, new Date())).toBe(true);
56 expect(await oneShot(app, member, "s", "p")).toEqual({ ok: false, error: "resting" });
57 setModelGrant(app.db, member, null, admin, new Date());
58 });
59 test("lent credit alone lets a member ask, and caps the turn at what is left of it", async () => {
60 on();
61 writeFileSync(join(dir, "parent.json"), JSON.stringify({ credit: { token: "bearer-token-1", capUsd: 1, spentUsd: 0.9 } }));
62 expect(turnBudget(app, { kind: "none" }, member, 1, new Date())).toEqual({ ok: false, refusal: "no_account", said: expect.any(String) });
63 writeFileSync(join(dir, "parent.json"), JSON.stringify({ credit: { token: "bearer-token-1", capUsd: 1, spentUsd: 0.5 } }));
64 expect(turnBudget(app, { kind: "none" }, member, 1, new Date())).toEqual({ ok: true, budget: 0.5 });
65 expect(await oneShot(app, member, "s", "p")).toEqual({ ok: true, value: "{}" });
66 rmSync(join(dir, "parent.json"));
67 });
4368 test("the answer comes back, the budget is the smallest cap, and the cost is booked", async () => {
4469 on(0.03);
4570 const before = spent();

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs