Skip to content

fix(cost): preserve catalog pricing evidence - #2776

Merged
Sinity merged 5 commits into
masterfrom
feature/fanout/cost-honesty
Jul 12, 2026
Merged

Sinity merged 5 commits into
masterfrom
feature/fanout/cost-honesty

Conversation

@Sinity

@Sinity Sinity commented Jul 12, 2026 •

Copy link
Copy Markdown
Owner

Summary

Make Codex cost evidence honest at both reconciliation boundaries: unreliable state_5.sqlite counters are separated from archive drift, and pricing catalog revisions are versioned by their content hash.

Problem

threads.tokens_used is external runtime state. Zero sentinels, stale repeated values, and inherited/cumulative counters were folded into outside-tolerance drift. Separately, price_catalogs.catalog_hash was write-only: a PRICING correction under the same effective date left DB price rows stale and new usage could select an arbitrary historical catalog.

Solution

  • Classify unreliable Codex state counters before physical or logical tolerance comparison, while retaining genuine mismatches as failures.
  • Reuse a catalog only when its persisted hash matches the active in-memory prices; otherwise seed a hash-versioned immutable catalog.
  • Resolve session_model_usage.priced_with from the catalog matching the current hash, preserving historical price evidence.

Acceptance matrix:

Bead Status Evidence
polylogue-d3xj Satisfied before this branch 8c9dfbb00 / PR #2647 normalizes Codex message input to disjoint fresh-input and cache-read lanes with a parser-to-storage-to-pricing fallback regression.
polylogue-w8et Satisfied 713a07fea reports zero sentinel, repeated stale-state fingerprints, and over-2,000,000-token counters as external-state-unreliable rather than drift.
polylogue-w379 Satisfied 292c4e84d versions stored hashes; 831c7d970 seeds that revision during ordinary existing-archive writes and links new usage to it.

Ref polylogue-d3xj

Ref polylogue-w8et

Ref polylogue-w379

Verification

  • nix develop --command devtools test tests/unit/devtools/test_cost_reconciliation_probe.py tests/unit/core/test_pricing.py tests/unit/sources/test_parsers_codex.py tests/unit/sources/test_parsers_claude_code_artifacts.py -k 'cost or pricing or token_lane or unreliable_state' — 29 passed, 77 deselected.
  • nix develop --command devtools test tests/unit/storage/test_pricing_chain_roundtrip.py tests/unit/core/test_pricing.py tests/unit/sources/test_parsers_codex.py tests/unit/sources/test_parsers_claude_code_artifacts.py -k 'pricing or token_lane' — 34 passed, 77 deselected.
  • nix develop --command devtools verify --quick — 15 checks passed.

Anti-vacuity: the w8et regression drives the production CLI main() path against a real archive index and copied state_5.sqlite; removing classification/exclusion returns unreliable values to the tolerance comparison. The w379 regression mutates the production PRICING dict, reseeds a real index, writes real usage, and fails if hash comparison, immutable revision seeding, or active-catalog linkage is removed.

Adversarial review notes

  • Iteration 1: two real gaps found and repaired—ordinary existing-archive writes did not seed a changed catalog revision (831c7d970), and direct messages could ignore explicit fresh-input lanes (6b3ce5712).
  • Iteration 2: no real acceptance-criterion gaps found. Borderline dismissed: external_total_tokens includes excluded unreliable state rows, but it does not drive comparison/pass-fail and the response separately reports those rows.

Problem:\nCodex state_5 threads.tokens_used can contain zero sentinels, stale\nrepeated defaults, and inherited cross-thread counters. Treating those\nvalues as archive drift made the reconciliation probe overstate failures.\n\nWhat changed:\nClassify these external-state-unreliable counters before tolerance\ncomparison and report their classes and samples separately. Remove\narchived/has_user_event from residual classification because they are not\ndiscriminating evidence.\n\nVerification:\nnix develop --command devtools test tests/unit/devtools/test_cost_reconciliation_probe.py tests/unit/core/test_pricing.py tests/unit/sources/test_parsers_codex.py tests/unit/sources/test_parsers_claude_code_artifacts.py -k 'cost or pricing or token_lane or unreliable_state' (29 passed, 77 deselected)\nnix develop --command devtools verify --quick (16 checks passed)\n\nRef polylogue-w8et\n\nCo-authored-by: Codex <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 50 minutes

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).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: c2cd1bce-e72d-4aae-9ebe-3ded52217133

📥 Commits

Reviewing files that changed from the base of the PR and between 901825e and 6b3ce57.

📒 Files selected for processing (7)
  • devtools/cost_reconciliation_probe.py
  • polylogue/sources/parsers/codex.py
  • polylogue/storage/sqlite/archive_tiers/pricing_seed.py
  • polylogue/storage/sqlite/archive_tiers/write.py
  • tests/unit/core/test_pricing.py
  • tests/unit/devtools/test_cost_reconciliation_probe.py
  • tests/unit/storage/test_pricing_chain_roundtrip.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/fanout/cost-honesty

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1283f7a170

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread devtools/cost_reconciliation_probe.py Resolved
Problem:\nThe price-catalog hash was written but never read. A PRICING correction under\nthe same effective date left model_prices stale and new usage pointed at an\narbitrary catalog row.\n\nWhat changed:\nUse the catalog hash to reuse an exact persisted revision or seed an immutable\nversioned catalog. Resolve priced_with from the row matching the active hash,\npreserving historical price evidence.\n\nVerification:\nnix develop --command devtools test tests/unit/storage/test_pricing_chain_roundtrip.py tests/unit/core/test_pricing.py tests/unit/sources/test_parsers_codex.py tests/unit/sources/test_parsers_claude_code_artifacts.py -k 'pricing or token_lane' (34 passed, 77 deselected)\nnix develop --command devtools verify --quick (15 checks passed)\n\nRef polylogue-w379\n\nCo-authored-by: Codex <[email protected]>
@Sinity Sinity changed the title fix(cost): classify unreliable Codex state counters fix(cost): preserve catalog pricing evidence Jul 12, 2026
Sinity and others added 3 commits July 13, 2026 00:02
Review fix from PR feedback: classify repeated cross-thread values as
repeated_stale_state_fingerprint only when the archive-backed state
fingerprint confirms staleness, instead of on value repetition alone.

Verified: 29 focused tests passed; devtools verify --quick passed.

Co-Authored-By: Claude <[email protected]>
Problem:\nExisting index archives skip bootstrap seeding, so a changed in-memory catalog\nhad no matching database revision when normal message writes ran.\n\nWhat changed:\nSeed or reuse the hash-matched catalog in the production usage writer. The\nregression mutates PRICING then writes to an existing archive without manually\nseeding a revision.\n\nVerification:\nnix develop --command devtools test tests/unit/storage/test_pricing_chain_roundtrip.py -k 'seed or priced_with or cost_usd' (8 passed)\n\nRef polylogue-w379\n\nCo-authored-by: Codex <[email protected]>
Problem:\nDirect Codex message usage could report uncached_input_tokens without an\ninclusive input_tokens value. The parser ignored that explicit fresh lane and\npriced it as zero in the message fallback.\n\nWhat changed:\nPrefer the explicit uncached-input aliases when present, otherwise derive fresh\ninput by subtracting cache reads. The regression drives parser output through\nstorage and estimate_session_cost's per-message fallback.\n\nVerification:\nnix develop --command devtools test tests/unit/core/test_pricing.py tests/unit/sources/test_parsers_codex.py tests/unit/sources/test_parsers_claude_code_artifacts.py -k 'pricing or token_lane or message_usage' (24 passed)\n\nRef polylogue-d3xj\n\nCo-authored-by: Codex <[email protected]>
@Sinity

Sinity commented Jul 12, 2026

Copy link
Copy Markdown
Owner Author

Adversarial review iteration 1

Found and repaired two real gaps:

  • w379: normal existing-archive writes skipped bootstrap seeding, so a changed PRICING hash had no persisted revision. 831c7d970 now seeds/reuses the hash-matched revision in the production writer; the regression mutates PRICING and writes without manually seeding (8 focused tests passed).
  • d3xj: direct message parsing ignored explicit uncached_input_tokens when inclusive input was absent. 6b3ce5712 now prefers that fresh lane, and the parser-derived values flow through the actual estimate_session_cost fallback (24 focused Codex/Claude-control tests passed).

The prior duplicate-total review finding is covered by 713a07fea: only repeated full stale-state fingerprints are excluded; equal totals with different timestamps remain real drift. w8et otherwise survived iteration 1. Local pre-push quick verification passed; hosted CI remains the known account-init failure pattern.

@Sinity

Sinity commented Jul 12, 2026

Copy link
Copy Markdown
Owner Author

Adversarial review iteration 2

No real acceptance-criterion gaps found across d3xj, w8et, or w379 after reviewing the full branch diff, bead contracts, PR body, and comments.

Borderline dismissed: the probe's external_total_tokens remains an all-state diagnostic while comparison metrics exclude unreliable rows. This does not affect pass/fail, and external_state_unreliable renders the excluded population separately. No code change is warranted.

@Sinity

Sinity commented Jul 12, 2026

Copy link
Copy Markdown
Owner Author

Final acceptance matrix

Bead Status Evidence
polylogue-d3xj Satisfied 6b3ce5712 prefers explicit uncached input when supplied and otherwise derives fresh input from inclusive input minus cache reads. The parser-derived lane is exercised through archive storage and estimate_session_cost message fallback.
polylogue-w8et Satisfied 713a07fea separates zero sentinels, repeated stale-state fingerprints, and implausibly large counters from genuine tolerance drift. The real SQLite fixture keeps coincident totals with distinct timestamps comparable.
polylogue-w379 Satisfied 292c4e84d adds hash-versioned catalog identity and 831c7d970 seeds/reuses it in the ordinary writer. The regression mutates PRICING, writes without manual seed, and verifies old/new rows plus priced_with.

No criteria are deferred or misframed. Local focused gates and the pre-push quick gate passed. GitHub Actions' no-step initialization failures remain the known account-infrastructure pattern; this PR was not merged and no beads were closed.

@Sinity
Sinity merged commit ab241c2 into master Jul 12, 2026
3 of 16 checks passed
@Sinity
Sinity deleted the feature/fanout/cost-honesty branch July 12, 2026 23:06
Sinity added a commit that referenced this pull request Jul 12, 2026
…789,2792,2797

d3xj, w8et, w379 (cost-honesty, PR #2776), fs1.3 (hermes-wedge, PR #2789), and
f2qv.5 (usage-convergence, PR #2797) fully satisfied — closed. cuxz
(temporal-provenance, PR #2786), th0 (test-harness, PR #2777), and fs1.11/
fs1.12 (hermes-wedge cluster, PR #2792) partially satisfied — noted with
disposition, left open per closure-discipline (no closing on partial ACs).

Co-Authored-By: Claude <[email protected]>
@Sinity

Sinity commented Oct 1, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

Only the rows listed above are dispositioned. Held findings remain open.

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.

1 participant