Skip to content

🧾 parse: expand usage.iterations into per-model attempt rows - #487

Merged
martinciu merged 8 commits into
mainfrom
456-parse-effort-usage
Aug 6, 2026
Merged

🧾 parse: expand usage.iterations into per-model attempt rows#487
martinciu merged 8 commits into
mainfrom
456-parse-effort-usage

Conversation

@martinciu

Copy link
Copy Markdown
Owner

🧭 Summary

Closes #456.

Billed usage.iterations entries served by a different model than the turn's — refused fallback attempts and cross-model advisor calls — were silently folded into the serving model's totals. This PR expands informative iterations at parse time into per-model attempt rows (message_id keyed <id>:it:<idx>), so every aggregation surface (models panel, projects, buckets, Recost) attributes tokens and cost to the model that actually consumed them.

🔧 What changed

  • 🧩 pkg/parse — new expandIterations + toMessages: own-model entries fold into the parent (iterations are authoritative when informative — also repairs the rare near-zeroed/under-reported outer usage), each non-zero foreign entry becomes an attempt row inheriting the full envelope. Fail-open paths (undecodable blob, oversized probe, missing message.id) keep today's behavior byte-for-byte.
  • 🔁 Both parse entry points wired — the reader path (ParseWithErrors) and the incremental tail path (ParseFromOffsetWithErrors) that the indexer/watcher actually use. The tail path was initially missed and caught by the real-data differential below; a regression test now pins it.
  • 🗃️ pkg/cacheSchemaVersion "11""12" with zero schema-text changes: the bump only forces the wipe-and-rebuild that backfills attempt rows from historic JSONL on first launch. Quota history (usage_samples/usage_limits) survives, as pinned by the new migration test.
  • 🧪 Pinning tests — attempt rows flow through InsertMessages idempotently, redistribute in ModelAggregates, and re-price against their own model's rate in Recost — all with no SQL changes (design claim held on first run).

📊 Real-data differential (live JSONL, ts < 2026-08-06T07:00Z)

Baseline binary (pre-change) vs this branch, both cold-indexing the same tree:

Model Δ tokens (in+out) Δ cost Why
claude-opus-4-7 +1,433,310 +$26.23 17 attempt rows + parent folds
claude-opus-5 +1,373,747 +$8.23 4 attempt rows + parent folds
claude-fable-5 +441 +$0.83 2 attempt rows (refused fallbacks)
claude-opus-4-8 +379 +$0.54 parent folds (served 4 informative turns)
claude-sonnet-4-6 0 +$0.07 under-reported cache_write_5m repaired on 17 informative parents
claude-sonnet-5 0 +$0.05 same repair, 5 parents
every other model 0 $0.00 byte-identical

Totals: 124,970 → 124,993 rows (+23 attempt rows, matching the 40 informative turns on this machine), +2,807,877 tokens, +$35.95 — previously misattributed or invisible.

✅ Testing

  • 🧪 go test ./... — all packages pass
  • 🧹 make lint — 0 issues
  • 🔬 Real-binary smoke: index --rebuild differential above; cost-only movers individually verified as informative-turn parents with repaired cache columns

📣 Surface changes (for release notes batching)

  • Schema v12: first launch after upgrade wipes and re-indexes the message cache from JSONL (quota history preserved). Attempt rows appear as extra messages rows keyed <id>:it:<idx>; per-model totals shift accordingly.

🤖 Generated with Claude Code

@martinciu
martinciu merged commit d9f8a08 into main Aug 6, 2026
7 checks passed
@martinciu
martinciu deleted the 456-parse-effort-usage branch August 6, 2026 11:39
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.

🧾 parse/cache: capture effort + usage.iterations from JSONL — fallback-aware per-model token/cost accounting

1 participant