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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13106-an-issue-s-cost-is-a-field-in-the-api-an → main 2 commits fork
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. | ||
| 29 | 29 | allowed to see the issue. A sealed pull the viewer may not see is left out |
| 30 | 30 | of the sum. A guest sees nothing. |
| 31 | 31 | |
| 32 | ## Not done | |
| 32 | ## API and MCP | |
| 33 | 33 | |
| 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[] { | ||
| 110 | 110 | for (const p of spent) rows.push({ label: "Pull", usd: p.usd, pull: p.number }); |
| 111 | 111 | return rows; |
| 112 | 112 | } |
| 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 @@ | ||
| 5 | 5 | * (#7450); the rules live in `actions/issues.ts` and its neighbours. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { mayReadRuns } from "../../actions/runs.ts"; | |
| 9 | import { costField, issueCostFor } from "../../core/issue_cost.ts"; | |
| 8 | 10 | import { holds, weakEtag } from "../etag.ts"; |
| 9 | 11 | import { ISSUE_TEAMS_SAY, nameTeams } from "../../actions/issue_teams.ts"; |
| 10 | 12 | import { askEstimate, recordEstimate } from "../../actions/estimates.ts"; |
| @@ -248,9 +250,15 @@ export function issueRoutes(router: Router<Handler>): void { | ||
| 248 | 250 | }; |
| 249 | 251 | }); |
| 250 | 252 | 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; | |
| 251 | 258 | return timePhase(ctx.request, "render", () => |
| 252 | 259 | tagged(ctx.request, JSON.stringify({ |
| 253 | 260 | ...labelled, |
| 261 | ...(cost === undefined ? {} : { cost }), | |
| 254 | 262 | ...(issue.kind === "pull" |
| 255 | 263 | ? { |
| 256 | 264 | 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"; | |
| 1 | 3 | import { mayView } from "../../../core/sealed.ts"; |
| 2 | 4 | import { countThread, EMBEDDED_COMMENTS, getIssue, labelsFor, listComments } from "../../../core/issues.ts"; |
| 3 | 5 | import { isCrew } from "../../../core/agents.ts"; |
| @@ -9,7 +11,7 @@ import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts" | ||
| 9 | 11 | |
| 10 | 12 | export const tool: Tool = { |
| 11 | 13 | 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", | |
| 13 | 15 | description: |
| 14 | 16 | "One issue with comments, `blockedBy`, `blocks`; non-writers' text is `untrusted`.", |
| 15 | 17 | inputSchema: { |
| @@ -58,6 +60,9 @@ export const tool: Tool = { | ||
| 58 | 60 | const flag = (x: { untrusted?: string }) => |
| 59 | 61 | x.untrusted === undefined ? {} : { untrusted: true }; |
| 60 | 62 | const [self] = mark([issue]); |
| 63 | const cost = mayReadRuns(ctx.app, user, repo) | |
| 64 | ? costField(issueCostFor(db, repo, issue, user)) | |
| 65 | : undefined; | |
| 61 | 66 | const answer = good({ |
| 62 | 67 | number: issue.number, |
| 63 | 68 | title: issue.title, |
| @@ -80,6 +85,7 @@ export const tool: Tool = { | ||
| 80 | 85 | commentsNext: comments.length > 0 && comments.length < total ? comments[0]!.id : null, |
| 81 | 86 | blockedBy: deps.blockedBy.filter(seen).map(brief), |
| 82 | 87 | blocks: deps.blocks.filter(seen).map(brief), |
| 88 | ...(cost === undefined ? {} : { cost }), | |
| 83 | 89 | }); |
| 84 | 90 | // Where the issue lives, for the card's links. |
| 85 | 91 | 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"; | ||
| 15 | 15 | import { costLine, costRows, issueCost, issueCostFor } from "../src/core/issue_cost.ts"; |
| 16 | 16 | import type { Repo } from "../src/core/repos.ts"; |
| 17 | 17 | import { createRun } from "../src/core/runs.ts"; |
| 18 | import { createToken } from "../src/core/tokens.ts"; | |
| 18 | 19 | import { createUser, type User } from "../src/core/users.ts"; |
| 19 | 20 | import { serve, type Servers } from "../src/http/server.ts"; |
| 20 | 21 | |
| @@ -162,4 +163,56 @@ describe("issue cost roll-up", () => { | ||
| 162 | 163 | expect(costLine(c)).toBe(`Cost $0.75: this issue $0.50, #${p.number} $0.25`); |
| 163 | 164 | expect(costRows(c).map((r) => r.label)).toEqual(["Cost", "This issue", "Pull"]); |
| 164 | 165 | }); |
| 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 | }); | |
| 165 | 218 | }); |
new above
offline