← Back to Pull requests

serve: a CLI-harness run's cost is its booked model requests #2303

Open
unknown opened this pull request with 516 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 10, 2026 23:54 UTC
Author

Fixes #2275.

Why

A run on a CLI harness (claude-agent, codex) booked no cost. Its model requests are booked: the model proxy writes runner.request and runner.turn records with their dollars into the run's session. But the run's end and its view both ignored those records. The result:

  • the run's cost and /v2/usage read $0 or unknown;

  • /v2/stats left the spend out, so a spend cap reading stats couldn't see it.
  • The learning bench saw every claude-agent run at $0.

    What

  • runner/metrics.ts: bookedSpend(entries, "model" | "paid") sums the booking records. That's every model session's requests at bookedUsd, else usd, or the paid-tool session's charges. A request with no known cost makes the sum null and keeps its known part.

  • runner/cli-run-spec.ts: the CLI run's end prices the run at its model bookings plus its paid calls. Before, it used runMetrics over the CLI record's entries, which carry no per-request cost and so gave $0.

  • store-reads/journal.ts: a CLI run's timeline gets its usage row in the booking's words ("Cost" or "Cost so far", the requests and paid calls), instead of having that row dropped.

  • The learning end's job and look-back costs for CLI runs read the same timeline, so they're priced now too.
  • Before / After

    | A claude-agent or codex run | Before | After |
    |---|---|---|
    | Run row cost (/v2/stats, caps) | paid calls only; model spend $0 | model bookings + paid calls |
    | Run view and Timeline usage row | none; cost read unknown | one usage row priced at the bookings |
    | A request whose cost is unknown | (n/a) | the sum reads null (unknown), never $0 |

    Evidence

  • runner-cli-web-tools.test.mjs: one new test per CLI wire, through a real runner on the faux harness and faux gateway, "a <claude-agent|codex> run's model requests are its cost: the booking's records, one usage row priced at them, and the run row and its view priced the same". Red on main: no usage row. Green on both wires.

  • The existing web_fetch test asserted "row cost = record's cost + fee", which encoded the $0. It now asserts "model bookings + fee".

  • store-cli-record: the "a CLI run drops the usage row" assertion now expects the booking's row.

  • Local TAP at this head, the 50 suites that read runTimeline, the CLI spec, runner/metrics, viewOverStore or store-reads/journal: 430 pass, 0 fail, 31 skipped (existing skips).
  • Prior art

    LiteLLM's proxy and OpenRouter both price a request from the gateway's own usage record, not from what the client reports. Here, the session proxy's booking is the record of truth for a CLI harness, whose own transcript carries no cost.

    Test plan

  • Local TAP above.

  • CI verify + lean.
  • Upgrading: nothing to do. Runs that ended before the upgrade keep their stored cost; their Timeline and view now show the booking's usage row.

    ACCEPT_IDENTITY=13 on push: every hit is the sandbox vendor name "Modal" in other changes' commit messages from main.

    🤖 Generated with Claude Code

    <!-- codesmith:footer -->
    ---
    <a href="https://backend.blacksmith.sh/track/enable-autofix?expires=1796946848&installation_model_id=431113&pr_number=2303&ref=codesmith_pr_footer&repository=Parcha-ai%2Fagentrun&return_to=https%3A%2F%2Fgithub.com%2FParcha-ai%2Fagentrun%2Fpull%2F2303&signature=16f1a87afcaedaae634aaa028151db26cc28f5506c1f070d98aacc942b32a719"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img alt="Autofix with [code]smith" src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png" width="190" height="28"></picture></a>
    <sup><a href="https://app.blacksmith.sh/Parcha-ai/codesmith/agentrun/pr/2303?autoLogin=true&ref=codesmith_pr_footer">Open in [code]smith</a> · Need help on this PR? Tag <code>@codesmith-bot</code> with what you need.</sup>

    <!-- codesmith:autofix:disabled -->
    <!-- /codesmith:footer -->

    Diff: 516 lines (+387 / -129) across 6 files Reviewer: greptile-apps[bot] Review duration: 18m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.53) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.53 · 516 lines diff across 6 files
    0.53
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.374
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #2303"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.7% (MEDIUM) · Higher = larger blast radius
    52.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 30.1% · Safe to merge
    30.1%