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

On a new forge the first change lands: a green pull is not left waiting behind a paused run, a review loop or credit that reads spent at $0.48 of $2 #11542

Parked opened this in answer to
Closed without merging

mason/forge:mason/11541-on-a-new-forge-the-first-change-lands-a → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +101 −3
src/crew/autonomy.ts +5
@@ -293,6 +293,7 @@ function activeRuns(app: App, repoId: string) {
293293 export function slottedRuns(
294294 active: readonly Run[],
295295 crew: readonly { readonly agent: Agent; readonly role: CrewRole }[],
296 now: Date = new Date(),
296297 ): Run[] {
297298 const notBuilders = new Set(
298299 crew.filter((c) => c.role !== "builder").map((c) => c.agent.user.id),
@@ -300,6 +301,10 @@ export function slottedRuns(
300301 return active.filter(
301302 (r) =>
302303 r.reason !== "review" &&
304 // A queued run paused until a later time (a provider's limit, the
305 // credit) is waiting, not working: it holds no slot, so the pull that
306 // needs its builder is not left behind it (#11541).
307 !(r.state === "queued" && r.notBefore !== null && r.notBefore > now.toISOString()) &&
303308 (r.actorId === null || !notBuilders.has(r.actorId)),
304309 );
305310 }
src/crew/dispatch.ts +1
@@ -1429,6 +1429,7 @@ function resumeLater(app: App, run: Run, pause: Pause): void {
14291429 log.info("run paused", {
14301430 run: run.id,
14311431 until: pause.resumeAt,
1432 why: pause.why ?? pause.summary ?? "the provider's limit",
14321433 attempt: pause.attempt,
14331434 queued: queued?.id,
14341435 });
src/crew/limits.ts +11
@@ -26,6 +26,8 @@ const SLACK_MS = 60_000;
2626 */
2727 export function isRateLimited(outcome: ClaudeOutcome): boolean {
2828 if (outcome.ok || isNetworkError(outcome)) return false;
29 // Spent credit does not lift by waiting: not a pause (#11541).
30 if (isCreditSpent(outcome.result)) return false;
2931 return (
3032 outcome.reason === "api_error" ||
3133 /rate[ _-]?limit|session limit|usage limit|weekly limit|hit your \w+ limit|overloaded|too many requests|\b(?:429|529)\b/i.test(
@@ -34,6 +36,15 @@ export function isRateLimited(outcome: ClaudeOutcome): boolean {
3436 );
3537 }
3638
39 /** The credit lent to this forge, or an account's budget, is spent: the model door's 402, in its words. */
40 export function isCreditSpent(message: string): boolean {
41 return /billing_error|credit balance is too low|from its parent is spent|\b402\b/i.test(message);
42 }
43
44 /** What a run that met spent credit says, on the pull and in its log. */
45 export const CREDIT_SPENT_WORDS =
46 "the crew's credit is spent; land the pull as it is, or add a model key to go on";
47
3748 const MONTHS = [
3849 "jan",
3950 "feb",
src/crew/runner.ts +16 −1
@@ -141,8 +141,11 @@ import {
141141 NETWORK_RETRY_SECONDS,
142142 pause,
143143 } from "./network.ts";
144 import { refreshFunding } from "../spawn/funding.ts";
144145 import { BUDGET_REACHED, runClaude, type ClaudeOutcome } from "./claude.ts";
145146 import {
147 CREDIT_SPENT_WORDS,
148 isCreditSpent,
146149 isRateLimited,
147150 limitOf,
148151 MAX_PAUSES,
@@ -511,6 +514,9 @@ async function runClaimed(
511514 await reapOrphans(app, run, write);
512515 await forgetLandedClone(app, run);
513516 meterUsage(app, claimed, outcome, meter, started);
517 // A forge on lent credit hears the host's count now, so its home, app and
518 // pull pages read the same figure the run just moved (#11541).
519 void refreshFunding(app);
514520 if (overran) {
515521 const summary = budgetSummary(
516522 "a review run",
@@ -539,7 +545,7 @@ async function runClaimed(
539545 });
540546 const paused = cancelRun(app.db, run.id, outcome.summary);
541547 if (paused) announceRun(app, paused);
542 return { resumeAt: outcome.resumeAt, attempt: run.attempt + 1 };
548 return { resumeAt: outcome.resumeAt, attempt: run.attempt + 1, why: outcome.summary };
543549 }
544550 // A run the provider or a restart cut short says so, and when it goes
545551 // on: interrupted, never a park (`interrupted.ts`).
@@ -897,6 +903,8 @@ export interface Pause {
897903 readonly attempt: number;
898904 /** What the queued task says while it waits, when not the provider's limit. */
899905 readonly summary?: string | undefined;
906 /** Why the run paused, for the log line: the run's own words. */
907 readonly why?: string | undefined;
900908 }
901909
902910 /** The file names a conflict or converge run's summary carries, after "main|parent|upstream moved: ". */
@@ -1729,6 +1737,13 @@ async function work(
17291737 limitLifts = resumeAt(result.result, new Date());
17301738 }
17311739
1740 // The credit is spent: waiting does not bring it back. The run ends
1741 // here, holding nothing, and says so in words a person can act on.
1742 if (!result.ok && !signal.aborted && isCreditSpent(result.result)) {
1743 write(CREDIT_SPENT_WORDS);
1744 return { state: "failed", summary: CREDIT_SPENT_WORDS, costUsd: result.costUsd };
1745 }
1746
17321747 watch.lap("harness");
17331748 ran.inHarness = false;
17341749 // The reserve: the same session gets one more turn, to land what exists
src/web/studio/cannot_finish.ts added +40
@@ -0,0 +1,40 @@
1 /**
2 * The crew cannot finish a change (#11541): its credit is spent, so no run
3 * will answer a review. Said plainly on the pull, with the two ways forward:
4 * land it as it is (the preview is right there) or add a model key.
5 */
6
7 import type { App } from "../../app.ts";
8 import { CREDIT_SPENT_WORDS } from "../../crew/limits.ts";
9 import type { Pull } from "../../core/pulls.ts";
10 import { checkSummary, latestRunsForIssues } from "../../core/runs.ts";
11 import { html, type Html } from "../html.ts";
12 import { btn } from "../ui.ts";
13 import { crewCanRun, includedCredit } from "./crew_key.ts";
14
15 /** Whether the crew has stopped for want of credit: its last run on this pull said so, or the lent credit is gone. */
16 export function creditStopped(app: App, pull: Pull): boolean {
17 const last = latestRunsForIssues(app.db, [pull.id], ["agent"]).get(pull.id);
18 if (last?.state === "failed" && (last.summary ?? "").includes(CREDIT_SPENT_WORDS)) return true;
19 return includedCredit(app) !== undefined && !crewCanRun(app);
20 }
21
22 /** The card, or null when the crew can go on, the change is not open, or its check is not green. */
23 export function cannotFinishCard(
24 app: App,
25 pull: Pull,
26 headOid: string,
27 thread: string,
28 admin: boolean,
29 ): Html | null {
30 if (pull.state !== "open" || pull.mergedAt !== null) return null;
31 if (checkSummary(app.db, pull.id, headOid).state !== "ok") return null;
32 if (!creditStopped(app, pull)) return null;
33 return html`<section class="settings-block mt mb" id="cannot-finish" role="status">
34 <p>The crew's credit is spent, so it cannot answer the review. The change is green and its preview is above: land it as it is, or add a model key to let the crew finish.</p>
35 <div class="bar mt">
36 <form method="post" action="${thread}/ship">${btn("Land it as it is", { size: "sm", variant: "primary" })}</form>
37 ${admin ? html`<a class="btn btn-sm" href="/settings/subscriptions">Add a key</a>` : html`<span class="muted small">Its admin can add a key.</span>`}
38 </div>
39 </section>`;
40 }
src/web/studio/change.ts +4 −1
@@ -87,6 +87,7 @@ import { tellLine, tellsAnswered } from "./branch_tell.ts";
8787 import { previewVision } from "../../crew/vision.ts";
8888 import { visionPreviewCard } from "./direction.ts";
8989 import { holdCard } from "./hold.ts";
90 import { cannotFinishCard } from "./cannot_finish.ts";
9091 import { pullMayRun } from "../../core/pull_trust.ts";
9192 import { landingLine } from "../../flow/landing_line.ts";
9293 import { nextOfPull } from "../../flow/next_actor.ts";
@@ -756,7 +757,9 @@ async function changeConversation(
756757 </p>`,
757758 )}
758759 <div id="pull-check">${checkLine}</div>
759 ${when(runLine, (l) => l)} ${gate} ${decisionSection}
760 ${when(runLine, (l) => l)} ${gate}
761 ${writer && status !== undefined && pull.state === "open" ? cannotFinishCard(ctx.app, pull, status.headOid, thread, ctx.auth.user?.isAdmin === true) : null}
762 ${decisionSection}
760763 ${when(visionPreview, (v) => visionPreviewCard(v))}
761764 ${look}
762765 <div class="split">
test/credit-spent-not-paused.test.ts added +23
@@ -0,0 +1,23 @@
1 import { expect, test } from "bun:test";
2 import { CREDIT_SPENT_WORDS, isCreditSpent, isRateLimited } from "../src/crew/limits.ts";
3 import { slottedRuns } from "../src/crew/autonomy.ts";
4 import type { ClaudeOutcome } from "../src/crew/claude.ts";
5
6 const failed = (result: string): ClaudeOutcome => ({ ok: false, reason: "api_error", result }) as unknown as ClaudeOutcome;
7
8 test("spent credit is not a pause", () => {
9 const said = "API Error: 402 billing_error: lab's $2 from its parent is spent; the parent's admin can raise it.";
10 expect(isCreditSpent(said)).toBe(true);
11 expect(isRateLimited(failed(said))).toBe(false);
12 expect(isRateLimited(failed("API Error: 429 rate_limit_error"))).toBe(true);
13 expect(CREDIT_SPENT_WORDS).toContain("land the pull as it is");
14 });
15
16 test("a run paused until later holds no builder slot", () => {
17 const run = (state: string, notBefore: string | null) =>
18 ({ id: "r", state, reason: "feedback", actorId: null, notBefore }) as never;
19 const now = new Date("2026-11-12T00:35:00Z");
20 const later = "2026-11-12T00:52:00Z";
21 expect(slottedRuns([run("queued", later), run("running", null)], [], now)).toHaveLength(1);
22 expect(slottedRuns([run("queued", "2026-11-12T00:30:00Z")], [], now)).toHaveLength(1);
23 });
test/pages-budget.test.ts +1 −1
@@ -37,7 +37,7 @@ const DEFAULT = { statements: 60, git: 8 };
3737 const HEAVIER: Record<string, { statements?: number; git?: number }> = {
3838 "/:owner/:name/issues/:number": { statements: 70 },
3939 "/:owner/:name/next": { statements: 64 },
40 "/:owner/:name/pulls/:number": { statements: 110 }, // +1: the header Talk count (#10327); +1: the landing line (#10860)
40 "/:owner/:name/pulls/:number": { statements: 111 }, // +1: the cannot-finish card reads the last run (#11541); +1: the header Talk count (#10327); +1: the landing line (#10860)
4141 "/board": { statements: 70 }, // +6: the money line (#510) — one settings read, then the month's and the 30 days' meter, storage and ledger reads; constant in the number of apps
4242 "/settings/capacity": { statements: 71 },
4343 };

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs