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

/crew separates runs cut short before an account from runs refused one #16923

Merged opened this

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

Merged

crew-no-account → main 0 commits 1 thread · 1 unresolved

  • Merged. crew-no-account was merged into main as 4ae1fb3.

Review

Sign in to review this change.
4 files changed +116 −20
docs/features/crew-spend-by-kind.md +6 −6
@@ -1,15 +1,15 @@
11 ---
22 topic: Bring your own AI
3 row: Where the crew's money goes, by kind of work, and the runs that found no account
3 row: Where the crew's money goes, by kind of work, the runs refused an account, and the runs cut short before one
44 ---
55 # Crew spend by kind of work
66
77 Every agent run has a reason, and the reason says what kind of work it was: **build** (an assigned issue), **rework** (review feedback, a red check, a conflict), **review**, **plan** (any run by an agent seated as planner on that repository), or **other**. The forge reads the last 14 days of runs by kind, model and account, and what became of the pulls they worked on: merged, green on their first check, reverted. Cost per merged pull is shown only for a row with 20 merged pulls or more, so no decision rests on a handful.
88
9 - **Settings → Crew** (`src/web/pages/crew.ts`): under "This week", two facts: "Spent in 14 days" ($2,380: 46% rework, 41% build, 13% review) and "Runs with no account" (agent runs that failed before they reached an account), with the reason given most and a link to add or allow an account. Below the model review, "By kind of work · 14 days" lists each kind × model × account: runs, spent, merged of pulls, green first, per merged.
10 - **The terminal** (`src/tui/screens/weekcard.ts`): "Crew this week" lists the same two facts and the same rows after the members.
11 - **API and MCP**: `GET /api/v1/crew/week` and MCP `crew` { kind: "week" } carry `spend` ({ days, totalUsd, byKind, reworkShare, noAccount }); `GET /api/v1/crew/scorecard` and MCP `scorecard` carry `byKind`. A forge admin's.
9 - **Settings → Crew** (`src/web/pages/crew.ts`): under "This week", two or three facts: "Spent in 14 days" ($2,380: 46% rework, 41% build, 13% review); "Runs with no account" (agent runs refused an account: none allowed, no credit, a cap), with the reason given most and a link to add or allow an account; and, only when there are some, "Runs cut short before an account" (runs that stopped before picking one, such as a clone that failed or a restart), with the reason given most and no link, since each resumes on the next pass. Below the model review, "By kind of work · 14 days" lists each kind × model × account: runs, spent, merged of pulls, green first, per merged.
10 - **The terminal** (`src/tui/screens/weekcard.ts`): "Crew this week" lists the same two or three facts and the same rows after the members.
11 - **API and MCP**: `GET /api/v1/crew/week` and MCP `crew` { kind: "week" } carry `spend` ({ days, totalUsd, byKind, reworkShare, noAccount, interrupted }, the last two each { runs, most }); `GET /api/v1/crew/scorecard` and MCP `scorecard` carry `byKind`. A forge admin's.
1212
13 What to do with it: a high rework share points at review feedback and red checks, not at the model; runs with no account point at Settings → Subscriptions. The weekly model review (model-review.md) reads the same ledger and asks for a dearer model only when that model's own pulls did clearly better.
13 What to do with it: a high rework share points at review feedback and red checks, not at the model; runs with no account point at Settings → Subscriptions; runs cut short point at the step that stopped them (a clone storm on 2026-11-20 read as 1,745 runs with no account before the two were apart). The weekly model review (model-review.md) reads the same ledger and asks for a dearer model only when that model's own pulls did clearly better.
1414
15 Proof: `test/model-review.test.ts`, `test/crew-week-card.test.ts`. The reads are `kindStats` and `KIND_SQL` (`src/core/model_review.ts`) and `weekSpend`, `spendFacts` (`src/crew/week_card.ts`).
15 Proof: `test/model-review.test.ts`, `test/crew-week-card.test.ts`, `test/crew-cut-short.test.ts`. The reads are `kindStats` and `KIND_SQL` (`src/core/model_review.ts`) and `weekSpend`, `spendFacts` (`src/crew/week_card.ts`).
src/contract/openapi/routes/getApiV1CrewWeek.ts +1 −1
@@ -4,7 +4,7 @@ export const route: Route = {
44 method: "get",
55 path: "/api/v1/crew/week",
66 summary: "Read the crew's last 7 days",
7 detail: "Read the crew's last 7 days (a forge admin's, any token scope): { days, members: [{ username, merged, returned, spentUsd, idle }] }, merged pulls, pulls returned by review and ledger spend per agent; idle is true when all three are nothing; and spend: { days, totalUsd, byKind: { build, rework, review, plan, other }, reworkShare, noAccount: { runs, most } } over the last 14 days, noAccount being the agent runs that failed before reaching an account and the reason given most",
7 detail: "Read the crew's last 7 days (a forge admin's, any token scope): { days, members: [{ username, merged, returned, spentUsd, idle }] }, merged pulls, pulls returned by review and ledger spend per agent; idle is true when all three are nothing; and spend: { days, totalUsd, byKind: { build, rework, review, plan, other }, reworkShare, noAccount: { runs, most }, interrupted: { runs, most } } over the last 14 days, noAccount being the agent runs refused an account (none allowed, no credit, a cap) and interrupted those cut short before picking one (a clone that failed, a restart), each with the reason given most",
88 auth: true,
99 role: "admin",
1010 refusals: { unauthorized: "no valid credentials were sent", forbidden: "the caller is not a forge admin" },
src/crew/week_card.ts +30 −13
@@ -12,6 +12,7 @@
1212
1313 import type { Database } from "bun:sqlite";
1414 import { KIND_DAYS, KIND_SQL, WORK_KINDS, type WorkKind } from "../core/model_review.ts";
15 import { INTERRUPTED } from "./interrupted.ts";
1516
1617 export const WEEK_DAYS = 7;
1718
@@ -68,10 +69,16 @@ export interface WeekSpend {
6869 /** Rework's share (0–1) of the total; null with nothing spent. */
6970 readonly reworkShare: number | null;
7071 /**
71 * Agent runs that failed before they reached an account (none allowed, no credit, a cap), and
72 * the reason they gave most, clipped; empty with none.
72 * Agent runs refused an account (none allowed, no credit, a cap), and the reason they gave
73 * most, clipped; empty with none.
7374 */
7475 readonly noAccount: { readonly runs: number; readonly most: string };
76 /**
77 * Agent runs cut short before they picked an account (`interrupted: clone, …`, a restart): not
78 * an account's fault, and each resumes on its own. Kept apart so a clone storm does not read as
79 * missing accounts (1,745 runs on 2026-11-20 did).
80 */
81 readonly interrupted: { readonly runs: number; readonly most: string };
7582 }
7683
7784 /**
@@ -99,19 +106,24 @@ export function weekSpend(db: Database, now: Date = new Date()): WeekSpend {
99106 GROUP BY summary ORDER BY n DESC`,
100107 )
101108 .all(since);
102 const first = (failed[0]?.summary ?? "").split("\n")[0]!.trim();
109 const cut = failed.filter((r) => r.summary.startsWith(INTERRUPTED));
110 const refused = failed.filter((r) => !r.summary.startsWith(INTERRUPTED));
103111 return {
104112 days: KIND_DAYS,
105113 totalUsd,
106114 byKind,
107115 reworkShare: totalUsd > 0 ? byKind.rework / totalUsd : null,
108 noAccount: {
109 runs: failed.reduce((n, r) => n + r.n, 0),
110 most: first.length > 120 ? `${first.slice(0, 119)}…` : first,
111 },
116 noAccount: tally(refused),
117 interrupted: tally(cut),
112118 };
113119 }
114120
121 /** How many runs, and the first line of the reason given most (rows come most first), clipped. */
122 function tally(rows: readonly { summary: string; n: number }[]): { runs: number; most: string } {
123 const first = (rows[0]?.summary ?? "").split("\n")[0]!.trim();
124 return { runs: rows.reduce((n, r) => n + r.n, 0), most: first.length > 120 ? `${first.slice(0, 119)}…` : first };
125 }
126
115127 const dollars = (n: number) => `$${Math.round(n).toLocaleString("en-US")}`;
116128
117129 /** Where a run with no account is fixed: the accounts, and where each may be used. */
@@ -124,8 +136,11 @@ export interface SpendFact {
124136 readonly fix?: { readonly href: string; readonly words: string };
125137 }
126138
127 /** The spend as two short facts: /crew's list under the week's table and the terminal's rows. */
128 export function spendFacts(s: WeekSpend): readonly [SpendFact, SpendFact] {
139 /**
140 * The spend as short facts: /crew's list under the week's table and the terminal's rows. The runs
141 * cut short before an account are a third fact only when there are some.
142 */
143 export function spendFacts(s: WeekSpend): readonly SpendFact[] {

The return type is now a variable-length list. The docs and the terminal bullet still say 'two facts'. Update docs/features/crew-spend-by-kind.md to match (see the review body).

129144 const share = (k: WorkKind) => `${Math.round((100 * s.byKind[k]) / s.totalUsd)}% ${k}`;
130145 const parts = WORK_KINDS.filter((k) => k === "rework" || s.byKind[k] > 0)
131146 .sort((a, b) => (a === "rework" ? -1 : b === "rework" ? 1 : s.byKind[b] - s.byKind[a]))
@@ -134,11 +149,13 @@ export function spendFacts(s: WeekSpend): readonly [SpendFact, SpendFact] {
134149 label: `Spent in ${s.days} days`,
135150 value: s.totalUsd === 0 ? "Nothing" : `${dollars(s.totalUsd)}: ${parts.join(", ")}`,
136151 };
137 const n = s.noAccount.runs;
152 const said = (t: { runs: number; most: string }): string =>
153 `${t.runs.toLocaleString("en-US")}${t.most === "" ? "" : `; most often “${t.most.replace(/[.!?]$/, "")}”`}`;
138154 const label = "Runs with no account";
139 if (n === 0) return [spent, { label, value: "None" }];
140 const most = s.noAccount.most === "" ? "" : `; most often “${s.noAccount.most.replace(/[.!?]$/, "")}”`;
141 return [spent, { label, value: `${n.toLocaleString("en-US")}${most}`, fix: NO_ACCOUNT_FIX }];
155 const noAccount: SpendFact = s.noAccount.runs === 0 ? { label, value: "None" } : { label, value: said(s.noAccount), fix: NO_ACCOUNT_FIX };
156 // Nothing to fix by hand: each resumes on the next pass.
157 const cut: SpendFact[] = s.interrupted.runs === 0 ? [] : [{ label: "Runs cut short before an account", value: said(s.interrupted) }];
158 return [spent, noAccount, ...cut];
142159 }
143160
144161 export interface ReturnedPull {
test/crew-cut-short.test.ts added +79
@@ -0,0 +1,79 @@
1 /**
2 * /crew's "Runs with no account" counts runs refused an account, not runs cut short before they
3 * picked one. On 2026-11-20 a clone storm (pack pacing, fixed in 0.7.629) ended 1,745 crew runs
4 * with "interrupted: clone, resumes on the next pass — fetch failed: fatal: early EOF"; none had
5 * picked an account, so for two weeks the line read as 1,895 runs with no account. Those are
6 * their own fact now, with no "add an account" fix, since each resumes on its own.
7 */
8 import { afterAll, beforeAll, expect, test } from "bun:test";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { createApp, type App } from "../src/app.ts";
13 import { createRepo } from "../src/actions/repos.ts";
14 import { createAgentRow } from "../src/core/agents.ts";
15 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
16 import { createUser, type User } from "../src/core/users.ts";
17 import { spendFacts, weekSpend } from "../src/crew/week_card.ts";
18 import { seedAdmin } from "./helpers/admin.ts";
19 import { quietLogs } from "./helpers/globals.ts";
20
21 quietLogs();
22
23 let dir = "";
24 let app: App;
25 let agent: User;
26 let repoId = "";
27
28 const CLONE = "interrupted: clone, resumes on the next pass — fetch failed: fatal: early EOF";
29 const REFUSED = "No account may be used here.\nAllow one.";
30
31 const failed = (summary: string): void => {
32 const r = createRun(app.db, { repoId, kind: "agent", actorId: agent.id, reason: "assigned" });
33 claimRun(app.db, r.id);
34 finishRun(app.db, r.id, { state: "failed", summary });
35 };
36
37 beforeAll(async () => {
38 dir = await mkdtemp(join(tmpdir(), "forge-cut-short-"));
39 app = createApp({ dataDir: dir, port: 0 });
40 const made = await createUser(app.db, { username: "torque", email: "[email protected]", password: "", displayName: "Torque", agent: true, origin: "agent" });
41 if (!made.ok) throw new Error("user");
42 agent = made.value;
43 createAgentRow(app.db, { userId: agent.id, model: "opus", brief: "", budgetUsd: 0 });
44 const ada = await seedAdmin(app, { username: "ada", email: "[email protected]" });
45 if (!ada.ok) throw new Error("owner");
46 const repo = await createRepo(app, ada.value, { name: "hello", visibility: "public" });
47 if (!repo.ok) throw new Error(JSON.stringify(repo.error));
48 repoId = repo.value.id;
49 });
50
51 afterAll(async () => {
52 app.close();
53 await rm(dir, { recursive: true, force: true });
54 });
55
56 test("only clone failures: no runs lack an account, and the cut-short ones are said apart, with nothing to fix", () => {
57 for (let i = 0; i < 3; i++) failed(CLONE);
58 const spend = weekSpend(app.db);
59 expect(spend.noAccount).toEqual({ runs: 0, most: "" });
60 expect(spend.interrupted).toEqual({ runs: 3, most: CLONE });
61 const facts = spendFacts(spend);
62 expect(facts[1]).toEqual({ label: "Runs with no account", value: "None" });
63 expect(facts[2]).toEqual({ label: "Runs cut short before an account", value: `3; most often “${CLONE}”` });
64 expect(facts[2]!.fix).toBeUndefined();
65 });
66
67 test("refused runs are the no-account fact, with where to fix it; the clone failures stay apart", () => {
68 failed(REFUSED);
69 const spend = weekSpend(app.db);
70 expect(spend.noAccount).toEqual({ runs: 1, most: "No account may be used here." });
71 expect(spend.interrupted.runs).toBe(3);
72 const facts = spendFacts(spend);
73 expect(facts[1]).toEqual({
74 label: "Runs with no account",
75 value: "1; most often “No account may be used here”",
76 fix: { href: "/settings/subscriptions", words: "Add or allow an account" },
77 });
78 expect(facts).toHaveLength(3);
79 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs