paperclip/server/src
Dotta efcce9cc8e
fix(adapters): record unpriced CLI usage (#9505)
## Thinking Path

> - Paperclip is the open source control plane people use to manage
AI-agent companies
> - Budgets and spend telemetry are control-plane safety features, not
just reporting
> - Local Codex and Claude adapters can execute through either ACP or
their native CLI engines
> - The ACP lane records usage and reported cost, but CLI JSON output
often reports tokens without a price
> - The CLI lane was either losing per-run usage semantics or coercing
missing cost to zero, making real usage indistinguishable from a
genuinely free run
> - This pull request preserves CLI usage as per-run totals and records
token-bearing runs without a reported price as explicitly unpriced
ledger events
> - The benefit is accurate usage accounting and a visible pricing gap
instead of silently misleading zero-cost telemetry

## Linked Issues or Issue Description

Refs #9471
Refs #9230

**Bug description**

A `codex_local` run using the CLI engine can emit a final
`turn.completed` event with millions of input tokens and tens of
thousands of output tokens while the agent's spend ledger remains
indistinguishable from a true zero-usage, zero-cost run. Claude CLI
output has the same missing-price edge case.

**Expected behavior**

Token-bearing CLI runs should persist their usage. If the adapter
reports a price, the ledger should record it as reported; if the CLI
reports usage but no price, the ledger should explicitly mark the event
as unpriced rather than silently treating missing price data as a
reported `$0` cost.

**Reproduction shape**

1. Configure `codex_local` with `engine: cli`.
2. Run a task that produces a `turn.completed` usage payload.
3. Observe token usage in the run stream.
4. Before this change, missing price data is represented as ordinary
zero-cost spend and the CLI usage basis is not consistently propagated.

## What Changed

- Mark Codex and Claude native CLI usage totals as `per_run` and
propagate that basis through success and failure results.
- Stop coercing missing Claude CLI cost to `0`.
- Add `cost_status` to cost events with `reported` and `unpriced`
values, including an idempotent migration and shared validation/types.
- Persist token-bearing runs without a reported price as `unpriced`
ledger events while retaining zero cents until an authoritative price
exists.
- Add parser, execute-path, heartbeat-accounting, and cost-service
regression coverage for both local CLI adapters.
- Document the cost-status invariant and CLI accounting behavior.

## Verification

- `pnpm exec vitest run
packages/adapters/codex-local/src/server/parse.test.ts
packages/adapters/claude-local/src/server/parse.test.ts
server/src/__tests__/codex-local-execute.test.ts
server/src/__tests__/claude-local-execute.test.ts
server/src/__tests__/heartbeat-cost-accounting.test.ts
server/src/__tests__/costs-service.test.ts` — 6 files / 102 tests
passed.
- `pnpm --filter @paperclipai/shared typecheck`
- `pnpm --filter @paperclipai/db typecheck` — includes migration
numbering and safety checks.
- `pnpm --filter @paperclipai/adapter-codex-local typecheck`
- `pnpm --filter @paperclipai/adapter-claude-local typecheck`
- `pnpm --filter @paperclipai/server typecheck`

## Risks

- Existing cost rows default to `reported`, preserving current
interpretation; only new token-bearing events with absent cost are
marked `unpriced`.
- This change does not invent model pricing. Budget hard stops still
cannot charge an unknown amount, but operators and evals can now
distinguish missing pricing from a genuinely reported zero cost.
- Consumers that enumerate cost-event fields should tolerate the
additive `costStatus` field.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- OpenAI Codex, exact model `gpt-5.3-codex`, with repository tool use
and code execution; default reasoning mode.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-07-13 20:44:38 -05:00
..
__tests__ fix(adapters): record unpriced CLI usage (#9505) 2026-07-13 20:44:38 -05:00
adapters fix: forward onSpawn to hermes and process adapters for PID persistence (#8722) 2026-07-13 12:33:08 -05:00
auth build(deps): bump better-auth from 1.4.18 to 1.6.20 (#8464) 2026-06-23 11:01:40 -07:00
built-ins/agents/reflection-coach [codex] Add built-in agents and Reflection Coach bundle (#9206) 2026-07-09 16:29:30 -05:00
http [codex] Runtime control-plane fixes (#6380) 2026-05-20 10:37:11 -05:00
lib Add low-trust review containment (#7530) 2026-06-05 16:48:02 -05:00
middleware Fix apiCompression corrupting and dropping Better Auth responses for gzip clients (#9381) 2026-07-10 16:04:29 -05:00
onboarding-assets [codex] Add heartbeat policy eval coverage (#9087) 2026-07-06 16:31:14 -05:00
realtime Add browser SSH terminal for custom image setup (#8911) 2026-07-03 16:44:21 -07:00
routes feat: add attention queue and Decisions surface (#9380) 2026-07-10 17:09:57 -05:00
secrets fix: validate session ID as UUID before --resume + error diagnostics (DLD-889) (#1742) 2026-06-09 20:21:10 -07:00
services fix(adapters): record unpriced CLI usage (#9505) 2026-07-13 20:44:38 -05:00
storage Address artifact PR review feedback 2026-05-30 20:51:46 +00:00
types feat(secrets): add user-specific runtime secrets (#8825) 2026-07-05 05:58:20 -05:00
agent-auth-jwt.ts Skill Studio: three-pane skill IDE with sandboxed test runs (#9241) 2026-07-09 13:08:56 -05:00
app.ts feat: add attention queue and Decisions surface (#9380) 2026-07-10 17:09:57 -05:00
attachment-types.ts Allow inline video attachment previews 2026-06-05 03:28:07 +00:00
board-claim.ts [codex] Add agent permissions and controls plan (#6386) 2026-05-22 08:12:52 -05:00
config-file.ts refactor: rename packages to @paperclipai and CLI binary to paperclipai 2026-03-03 08:45:26 -06:00
config.ts Add secrets provider vaults and remote import (#5429) 2026-05-09 18:22:17 -05:00
dev-runner-worktree.ts [codex] Bundle local branch fixes from PAP-10032 (#6604) 2026-05-25 07:25:26 -05:00
dev-server-status.ts [codex] UI and dev ops quality-of-life (#6384) 2026-05-19 15:52:39 -05:00
dev-watch-ignore.ts feat(adapters): external adapter plugin system with dynamic UI parser 2026-04-03 21:11:20 +01:00
errors.ts feat(secrets): add user-specific runtime secrets (#8825) 2026-07-05 05:58:20 -05:00
first-admin-claim.ts [codex] Add private browser first-admin claim flow (#6755) 2026-05-27 21:15:01 -10:00
home-paths.ts [codex] Add LLM Wiki plugin host support (#5597) 2026-05-10 07:34:12 -05:00
index.ts Ensure worktree execution starts only after activation (#9374) 2026-07-10 16:11:26 -05:00
instrumentation.ts feat(server): opt-in OpenTelemetry auto-instrumentation (#3735) 2026-06-12 10:44:22 -07:00
log-redaction.ts Sync/master post pap1497 followups 2026 04 15 (#3779) 2026-04-15 21:13:56 -05:00
paths.ts feat(cli): add client commands and home-based local runtime defaults 2026-02-20 07:10:58 -06:00
redaction.ts feat(secrets): add user-specific runtime secrets (#8825) 2026-07-05 05:58:20 -05:00
runtime-api.ts Prefer loopback runtime API URL for local agents (#5102) 2026-06-20 14:03:21 -07:00
server-info.ts Fix stale server info debug metadata (#8753) 2026-06-29 13:10:58 -07:00
startup-banner.ts Introduce bind presets for deployment setup 2026-04-11 07:09:07 -05:00
static-index-html.ts [codex] Bundle local branch fixes from PAP-10032 (#6604) 2026-05-25 07:25:26 -05:00
telemetry.ts fix: add periodic flush and graceful shutdown for server-side telemetry 2026-04-02 10:47:29 -05:00
ui-branding.ts Ensure worktree execution starts only after activation (#9374) 2026-07-10 16:11:26 -05:00
version.ts [codex] Quiet packaged version fallback diagnostics (#9207) 2026-07-10 19:31:36 -05:00
vite-html-renderer.ts [codex] Add backup endpoint and dev runtime hardening (#4087) 2026-04-20 06:08:55 -05:00
worktree-config.ts [codex] Bundle local branch fixes from PAP-10032 (#6604) 2026-05-25 07:25:26 -05:00