Skip to content

feat: finish model cost accounting - #46

Merged
Programcode merged 4 commits into
mainfrom
feat/model-cost-accounting-20260907
Sep 19, 2026
Merged

Programcode merged 4 commits into
mainfrom
feat/model-cost-accounting-20260907

Conversation

@Programcode

@Programcode Programcode commented Sep 19, 2026 •

Copy link
Copy Markdown
Owner

Problem and result

Incomplete streams could retain a lower-bound OpenRouter charge, schema drift could be hidden by a fallback insert, and URL imports appeared as unknown AI cost. This change makes incomplete or cancelled provider work non-billable while retaining observed usage, removes the audit-column downgrade path after verified schema parity, and keeps importer volume separate from AI cost.

The plan predated PR #43, so the implementation was rebased onto the current OpenRouter-only handlers instead of restoring removed direct-provider routes.

Active app accounting never looks up a static provider rate: every allowlisted model request runs through OpenRouter, and the stored USD charge is OpenRouter's per-response usage.cost. The refreshed provider snapshots remain legacy audit evidence only.

Changes

  • request terminal OpenRouter usage and preserve its authoritative reported cost only for complete work
  • mark truncated, errored, and client-aborted streams incomplete, including aborts after a finish reason but before the terminal frame
  • keep a single audit insert in both AI functions; missing columns now fail visibly instead of retrying a base row
  • include imports in request and feature volume while excluding them from AI known/unknown, model, feature, daily, and user cost
  • page the seven-day admin source rows so aggregates do not stop at the PostgREST row cap
  • preserve historical finite costs even when legacy token columns are null
  • refresh and hash official pricing captures with exact tsx@4.19.2; all 11 normalized rates still match, and gemini-3.1-flash-lite-preview remains unknown
  • record the execution and review trail in .audit/model-cost-accounting.tsv

Verification

  • Node v22.23.2, npm 12.0.2, Deno 2.9.6
  • focused pricing/admin UI: 37 passed
  • native Edge suite: 181 passed
  • Supabase Vitest scope: 161 passed
  • full coverage: 1,026 passed; 90.29% statements, 82.81% branches, 92.26% functions, 94.3% lines
  • lint, typecheck, docs check, and production build passed
  • isolated desktop/mobile Chromium on port 18082 with two workers: 134 passed
  • independent release review found and verified fixes for abort-after-finish, historical stored costs, and row-cap pagination

Browser proof

The fixture contains seven requests: six OpenRouter calls and one source import. The import remains visible as Source Import volume, while cost-by-feature and cost-by-model contain only AI work. The recording covers desktop and Pixel 7 layouts.

f963842b-6d26-4bc1-9475-89e89233d3e6-e72a6d26-a7ed-4155-b759-badfb4c330dd-mp4.mp4

Database and staging

  • staging brifeyypjauyusmkptkx and production uziwdtkzofptihahfooo both have migration 20260822000000 and all seven audit columns
  • no new schema migration or historical backfill
  • staged functions are active as ai v18, ai-stream v19, and admin v13
  • protected staging run passed: https://github.com/Programcode/DebateLab/actions/runs/35456451358
  • current-head CI passed quality, database, and desktop/mobile browser jobs: https://github.com/Programcode/DebateLab/actions/runs/35459711614
  • the live request stored 785 input tokens, 70 output tokens, $0.00015275, openrouter-response-usage, openrouter-usage-v1, and raw usage
  • previous staging versions and hashes were recorded before deployment; rollback is redeploying the three function trees from origin/main@99c2b53

Adam will perform the final merge. Current-head CI and staging evidence are clear; CodeRabbit was rate-limited, and Adam waived the remaining review wait.

Copilot AI lite review requested due to automatic review settings September 19, 2026 16:56
@vercel

vercel Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
debate-navigator Ready Ready Preview Sep 19, 2026 6:29pm UTC

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 12 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Free

Run ID: 1617da1a-ae5e-42d8-99ea-65589cf361b9

📥 Commits

Reviewing files that changed from the base of the PR and between e1e0070 and aa8b1ba.

⛔ Files ignored due to path filters (2)
  • .audit/model-cost-accounting.tsv is excluded by !**/*.tsv
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (18)
  • docs/pricing-snapshots/anthropic.md
  • docs/pricing-snapshots/google-gemini.html
  • docs/pricing-snapshots/metadata.json
  • docs/pricing-snapshots/openai.md
  • docs/testing.md
  • package.json
  • src/pages/AdminPage.tsx
  • src/test/pages/AdminPage.test.tsx
  • supabase/functions/_shared/admin-cost.test.ts
  • supabase/functions/_shared/admin-cost.ts
  • supabase/functions/_shared/cost.test.ts
  • supabase/functions/_shared/cost.ts
  • supabase/functions/_shared/model-pricing.ts
  • supabase/functions/admin/index.ts
  • supabase/functions/ai-stream/handler_test.ts
  • supabase/functions/ai-stream/index.ts
  • supabase/functions/ai-stream/index_test.ts
  • supabase/functions/ai/index.ts

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Essentials by visiting https://app.coderabbit.ai/settings/billing.

Comment @coderabbitai help to get the list of available commands.

@Programcode

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Two moderate findings block approval, and the audit evidence path needs a portability fix.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Completes OpenRouter model-cost accounting, separates source-import volume from AI costs, and refreshes pricing evidence.

Changes:

  • Tracks terminal usage and marks incomplete streams non-billable.
  • Adds required audit inserts and paginated, import-aware admin aggregation.
  • Updates pricing captures, tests, tooling, and documentation.
File Reviewed changes / findings
supabase/​functions/​ai/​index.ts Requires audit-column inserts without fallback.
supabase/​functions/​ai-stream/​index.ts Tracks terminal usage and incomplete streams. Moderate finding (3 votes): exception paths after a finish reason can retain provider cost.
supabase/​functions/​ai-stream/​index_test.ts Tests stream accounting scenarios.
supabase/​functions/​ai-stream/​handler_test.ts Verifies terminal usage requests.
supabase/​functions/​admin/​index.ts Adds paginated, import-aware aggregation. Moderate finding (1 vote): unknown-cost counts can be mislabeled as missing token data.
supabase/​functions/​_shared/​model-pricing.ts Updates pricing snapshot metadata.
supabase/​functions/​_shared/​cost.ts Adds incomplete-usage marking.
supabase/​functions/​_shared/​cost.test.ts Tests incomplete cost handling.
supabase/​functions/​_shared/​admin-cost.ts Adds cost classification and pagination helpers.
supabase/​functions/​_shared/​admin-cost.test.ts Tests admin accounting rules.
package.json Adds the pinned tsx dependency.
package-lock.json Locks dependency changes.
docs/​testing.md Documents stream-accounting tests.
docs/​pricing-snapshots/​openai.md Refreshes OpenAI pricing evidence.
docs/​pricing-snapshots/​anthropic.md Refreshes Anthropic pricing evidence.
docs/​pricing-snapshots/​metadata.json Updates pricing hashes and metadata.
.audit/​model-cost-accounting.tsv Records execution and review evidence. Nit (1 vote): browser evidence uses a host-specific local path.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread supabase/functions/ai-stream/index.ts Resolved
@Programcode

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Programcode

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Programcode

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Programcode

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Programcode
Programcode merged commit e9c4354 into main Sep 19, 2026
7 checks passed
@Programcode
Programcode deleted the feat/model-cost-accounting-20260907 branch September 19, 2026 19:22

This branch was successfully deployed

1 active deployment
Preview — aa8b1bae Deployed Sep 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants