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

An issue's cost is a field in the API and the MCP, the same number its page prints #13139

Merged opened this

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

Merged

scout/forge:scout/13106-an-issue-s-cost-is-a-field-in-the-api-an → main 2 commits fork

  • Merged. scout/13106-an-issue-s-cost-is-a-field-in-the-api-an was merged into main as 402794f.

Review

Sign in to review this change.
5 files changed +93 −3
docs/features/issue-cost.md +5 −2
@@ -29,6 +29,9 @@ to know what a piece of work cost before they look at the whole ledger.
2929 allowed to see the issue. A sealed pull the viewer may not see is left out
3030 of the sum. A guest sees nothing.
3131
32 ## Not done
32 ## API and MCP
3333
34 There is no API or MCP field for the cost yet.
34 `GET /api/v1/repos/:owner/:name/issues/:n` and the MCP `issue` tool carry
35 `cost: {usd, own, pulls: [{number, usd}]}`, the number the page prints, from
36 `issueCostFor`. The field is absent when nothing was spent, for a guest, and
37 for a viewer who may not read the runs or see the owner's facts.
src/core/issue_cost.ts +20
@@ -110,3 +110,23 @@ export function costRows(cost: IssueCost): CostRow[] {
110110 for (const p of spent) rows.push({ label: "Pull", usd: p.usd, pull: p.number });
111111 return rows;
112112 }
113
114 /**
115 * The cost as the API and the MCP carry it: the same total the page prints,
116 * in dollars, with this issue's own runs and each closing pull that spent
117 * something. Absent (undefined) when `issueCostFor` said nothing.
118 */
119 export interface CostField {
120 readonly usd: number;
121 readonly own: number;
122 readonly pulls: readonly { readonly number: number; readonly usd: number }[];
123 }
124
125 export function costField(cost: IssueCost | null): CostField | undefined {
126 if (cost === null) return undefined;
127 return {
128 usd: cost.totalUsd,
129 own: cost.ownUsd,
130 pulls: cost.pulls.filter((p) => p.usd > 0).map((p) => ({ number: p.number, usd: p.usd })),
131 };
132 }
src/http/api/issues.ts +8
@@ -5,6 +5,8 @@
55 * (#7450); the rules live in `actions/issues.ts` and its neighbours.
66 */
77
8 import { mayReadRuns } from "../../actions/runs.ts";
9 import { costField, issueCostFor } from "../../core/issue_cost.ts";
810 import { holds, weakEtag } from "../etag.ts";
911 import { ISSUE_TEAMS_SAY, nameTeams } from "../../actions/issue_teams.ts";
1012 import { askEstimate, recordEstimate } from "../../actions/estimates.ts";
@@ -248,9 +250,15 @@ export function issueRoutes(router: Router<Handler>): void {
248250 };
249251 });
250252 const labelled = timePhase(ctx.request, "labels", () => withLabels(ctx, issue));
253 // The page's number (`issueCostFor`), for a viewer who reads the runs.
254 const cost =
255 issue.kind === "issue" && mayReadRuns(ctx.app, ctx.auth.user, repo)
256 ? costField(issueCostFor(ctx.app.db, repo, issue, ctx.auth.user))
257 : undefined;
251258 return timePhase(ctx.request, "render", () =>
252259 tagged(ctx.request, JSON.stringify({
253260 ...labelled,
261 ...(cost === undefined ? {} : { cost }),
254262 ...(issue.kind === "pull"
255263 ? {
256264 pull: `/api/v1/repos/${repo.ownerName}/${repo.name}/pulls/${issue.number}`,
src/http/mcp/tools/issue.ts +7 −1
@@ -1,3 +1,5 @@
1 import { mayReadRuns } from "../../../actions/runs.ts";
2 import { costField, issueCostFor } from "../../../core/issue_cost.ts";
13 import { mayView } from "../../../core/sealed.ts";
24 import { countThread, EMBEDDED_COMMENTS, getIssue, labelsFor, listComments } from "../../../core/issues.ts";
35 import { isCrew } from "../../../core/agents.ts";
@@ -9,7 +11,7 @@ import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"
911
1012 export const tool: Tool = {
1113 name: "issue",
12 doc: "`{ repo, number }`: one issue's title, state, closeReason, body, labels, assignee, the newest 50 comments (author, authorIsAgent, body, at), `commentsTotal` and `commentsNext` (older ones: `GET …/comments?cursor=`), and blockedBy/blocks, as `GET …/issues/:number`; a sealed one you cannot see is not found; draws the `ui://forge/issue` card: title, state, assignee, labels and what it waits for, never the body or comments",
14 doc: "`{ repo, number }`: one issue's title, state, closeReason, body, labels, assignee, the newest 50 comments (author, authorIsAgent, body, at), `commentsTotal` and `commentsNext` (older ones: `GET …/comments?cursor=`), blockedBy/blocks and `cost` (`{usd, own, pulls}`, absent when nothing was spent or it is not yours to see), as `GET …/issues/:number`; a sealed one you cannot see is not found; draws the `ui://forge/issue` card: title, state, assignee, labels and what it waits for, never the body or comments",
1315 description:
1416 "One issue with comments, `blockedBy`, `blocks`; non-writers' text is `untrusted`.",
1517 inputSchema: {
@@ -58,6 +60,9 @@ export const tool: Tool = {
5860 const flag = (x: { untrusted?: string }) =>
5961 x.untrusted === undefined ? {} : { untrusted: true };
6062 const [self] = mark([issue]);
63 const cost = mayReadRuns(ctx.app, user, repo)
64 ? costField(issueCostFor(db, repo, issue, user))
65 : undefined;
6166 const answer = good({
6267 number: issue.number,
6368 title: issue.title,
@@ -80,6 +85,7 @@ export const tool: Tool = {
8085 commentsNext: comments.length > 0 && comments.length < total ? comments[0]!.id : null,
8186 blockedBy: deps.blockedBy.filter(seen).map(brief),
8287 blocks: deps.blocks.filter(seen).map(brief),
88 ...(cost === undefined ? {} : { cost }),
8389 });
8490 // Where the issue lives, for the card's links.
8591 return { ...answer, card: { repo: `${repo.ownerName}/${repo.name}` } };
test/issue-cost.test.ts +53
@@ -15,6 +15,7 @@ import { createApp, type App } from "../src/app.ts";
1515 import { costLine, costRows, issueCost, issueCostFor } from "../src/core/issue_cost.ts";
1616 import type { Repo } from "../src/core/repos.ts";
1717 import { createRun } from "../src/core/runs.ts";
18 import { createToken } from "../src/core/tokens.ts";
1819 import { createUser, type User } from "../src/core/users.ts";
1920 import { serve, type Servers } from "../src/http/server.ts";
2021
@@ -162,4 +163,56 @@ describe("issue cost roll-up", () => {
162163 expect(costLine(c)).toBe(`Cost $0.75: this issue $0.50, #${p.number} $0.25`);
163164 expect(costRows(c).map((r) => r.label)).toEqual(["Cost", "This issue", "Pull"]);
164165 });
166
167 test("the page, the API and the MCP print one number; a guest and a reader get none", async () => {
168 const t = open("Everywhere");
169 spend(t.id, 0.5);
170 const p = open("Fix everywhere", `Closes #${t.number}`);
171 asPull(p.id);
172 spend(p.id, 0.25);
173 const c = await createUser(app.db, {
174 origin: "seed",
175 trusted: true,
176 username: "carol",
177 email: "[email protected]",
178 password: "a-good-passphrase",
179 });
180 if (!c.ok) throw new Error(c.error);
181 const mine = createToken(app.db, alice.id, "mine", "ro").token;
182 const hers = createToken(app.db, c.value.id, "hers", "ro").token;
183 const getApi = async (token?: string) =>
184 (await (
185 await fetch(`${servers.url}/api/v1/repos/alice/shop/issues/${t.number}`, {
186 headers: token === undefined ? {} : { authorization: `Bearer ${token}` },
187 })
188 ).json()) as { cost?: { usd: number; own: number; pulls: { number: number; usd: number }[] } };
189 const viaMcp = async (token: string) => {
190 const res = await fetch(`${servers.url}/mcp`, {
191 method: "POST",
192 headers: {
193 "content-type": "application/json",
194 accept: "application/json, text/event-stream",
195 authorization: `Bearer ${token}`,
196 },
197 body: JSON.stringify({
198 jsonrpc: "2.0",
199 id: 1,
200 method: "tools/call",
201 params: { name: "issue", arguments: { repo: "alice/shop", number: t.number } },
202 }),
203 });
204 const body = (await res.json()) as any;
205 // A repository the reader cannot enter is no answer at all, and no cost.
206 const text = String(body.result.content[0].text);
207 return (text.startsWith("{") ? JSON.parse(text) : {}) as { cost?: { usd: number } };
208 };
209 const expected = { usd: 0.75, own: 0.5, pulls: [{ number: p.number, usd: 0.25 }] };
210 const html = await page(t.number, await signin("alice", "a-good-passphrase"));
211 expect(costDl(html)).toContain("$0.75");
212 expect((await getApi(mine)).cost).toEqual(expected);
213 expect((await viaMcp(mine)).cost).toEqual(expected);
214 expect((await getApi()).cost).toBeUndefined();
215 expect((await getApi(hers)).cost).toBeUndefined();
216 expect((await viaMcp(hers)).cost).toBeUndefined();
217 });
165218 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs