Skip to content

fix(codex): attribute resumed token counts to later model#496

Merged
junhoyeo merged 1 commit intojunhoyeo:mainfrom
IvGolovach:codex/codex-resume-model-attribution
May 3, 2026
Merged

fix(codex): attribute resumed token counts to later model#496
junhoyeo merged 1 commit intojunhoyeo:mainfrom
IvGolovach:codex/codex-resume-model-attribution

Conversation

@IvGolovach
Copy link
Copy Markdown
Contributor

@IvGolovach IvGolovach commented May 3, 2026

Summary

Fix Codex resumed-session attribution so early token_count events emitted before turn_context inherit the later model instead of staying under unknown.

Fixes #490.

Why

Codex 0.124 resumed sessions can replay event_msg / token_count rows before the first turn_context row that carries payload.model. The previous parser emitted those early rows immediately with modelId = unknown, which also meant pricing stayed at 0 even after the real model appeared later in the same session file.

Diff scope

  • crates/tokscale-core/src/sessions/codex.rs: buffer model-less Codex token_count messages until the parser observes a model, then flush them with that model; preserve unknown only when no model ever appears.
  • crates/tokscale-core/src/lib.rs: avoid writing Codex source-cache entries when a file still has unresolved model-less token events.
  • crates/tokscale-core/src/message_cache.rs: bump the source-message cache schema from 8 to 9 so stale cached unknown Codex rows are invalidated.

This is a narrow runtime parser/cache change. No frontend, release, workflow, migration, or CLI command behavior changed.

Branch integrity

  • Base branch: main
  • origin/main: 97809e755b58b07e21c0eaceb9005f0ba073de0c
  • Ahead / behind from git rev-list --left-right --count origin/main...HEAD: 0 behind / 1 ahead
  • Merge base: 97809e755b58b07e21c0eaceb9005f0ba073de0c
  • git merge-base --is-ancestor origin/main HEAD: PASS
  • Fast-forward safety: branch contains the current validated base.

Commit integrity

  • Introduced commit: c3ca2ce9cfc113bc465dff8965f49da6cb83d865 fix(codex): attribute resumed token counts to later model
  • One logical change per commit: yes.
  • Ledger-Id trailers: Not applicable — scripts/ledger/ does not exist in this repository.
  • Duplicate Ledger-Id check: Not applicable — scripts/ledger/ does not exist in this repository.

Ledger proof

Not applicable — scripts/ledger/ does not exist in this repository.

Diff hygiene

git diff --name-status origin/main...HEAD:

M	crates/tokscale-core/src/lib.rs
M	crates/tokscale-core/src/message_cache.rs
M	crates/tokscale-core/src/sessions/codex.rs
  • git diff --check origin/main...HEAD: PASS, no output.
  • No .env files, local environment files, secrets, tokens, credentials, cache files, build artifacts, or unrelated generated files are included.
  • Required metadata note: CACHE_SCHEMA_VERSION was bumped because the interpretation of cached Codex source messages changed; invalidating old cache entries prevents previously cached unknown rows from surviving the parser fix.

Validation tier and test proof

Validation tier: Tier 2 — narrow runtime parser/cache change.

Reason: the change is localized to Codex session parsing and source-message cache eligibility. It does not alter migrations, auth, permissions, payment/billing logic, global configuration, CI, release tooling, production infrastructure, or external service contracts.

Run:

  • scripts/ledger/check: Not applicable — scripts/ledger/ does not exist in this repository.
  • git fetch --no-tags origin main:refs/remotes/origin/main: PASS.
  • cargo test -p tokscale-core token_count: PASS, 10 passed.
  • cargo test -p tokscale-core codex_cache: PASS, 2 passed.
  • cargo test -p tokscale-core: PASS, 599 passed, 1 ignored; codebuff integration tests 10 passed; hermes integration tests 3 passed; doc-tests 0 passed.
  • cargo clippy -p tokscale-core --all-features -- -D warnings: PASS.
  • cargo fmt --all --check: PASS.
  • git diff --check: PASS.
  • git diff --cached --check: PASS.
  • Cyrillic scan for touched source files: PASS, no matches.

Not run:

  • Full workspace test suite — not required for this Tier 2 parser/cache change because the touched runtime surface is covered by tokscale-core tests and clippy.
  • Native build matrix — left to GitHub CI; no native build configuration changed.

External gates:

  • Pending — required GitHub checks can run only after this PR is opened.

Verification-pack proof

Not applicable — no infra, governance, replay, invariant, or verification-pack files changed.

Migration notes

Not applicable — no DB migration changed.

CI context confirmation

  • CI context names unchanged.
  • Not applicable — no CI/workflow context changes.
  • Required contexts: Pending — PR has not been opened yet, so GitHub has not created check runs for this head branch.
  • Live branch-protection status: unavailable from the local check; GitHub API returned 404 for the branch-protection required-status-checks endpoint.

Runtime safety

Reviewed runtime files/modules:

  • crates/tokscale-core/src/sessions/codex.rs
  • crates/tokscale-core/src/lib.rs
  • crates/tokscale-core/src/message_cache.rs

Safety reasoning:

  • No new blocking locks are introduced; parsing still uses the existing sequential reader flow.
  • No new unbounded queues are introduced beyond a per-file in-memory buffer for model-less token events before the first model appears; the buffer is drained as soon as a model is observed or at EOF.
  • No parser invariants are removed; token delta logic, zero-token filtering, timestamp fallback tracking, workspace attribution, and provider/agent propagation are preserved.
  • Cache writes are disabled only for unresolved model-less Codex files, preventing temporary unknown attribution from being persisted.

No invariant regression introduced.

Documentation integrity

Not applicable — no docs, runbooks, public commands, release procedure, deployment procedure, or user-facing documented behavior changed.

Rollback plan

  • Rollback command after merge: git revert <post_merge_commit_sha>.
  • Replace <post_merge_commit_sha> with the final squash or merge commit SHA after merge.
  • DB downgrade required: no.
  • Data repair required: no.
  • Operational caveats: rollback would restore cache schema version 8; users may keep regenerated cache entries from the fixed parser until the cache naturally refreshes or is removed.

Known residual risks

No known residual risks.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 3, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
tokscale Ignored Ignored Preview May 3, 2026 8:29am

Request Review

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="crates/tokscale-core/src/sessions/codex.rs">

<violation number="1" location="crates/tokscale-core/src/sessions/codex.rs:238">
P2: Pending model-less token_count rows can remain buffered when a headless JSON line sets `current_model` but returns `None`, so earlier rows are later emitted as `unknown` instead of the discovered model.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread crates/tokscale-core/src/sessions/codex.rs
Validation

- scripts/ledger/check: Not applicable - scripts/ledger/ does not exist in this repository.

- git fetch --no-tags origin main:refs/remotes/origin/main: PASS

- cargo test -p tokscale-core test_model_only_headless_line_flushes_pending_token_counts: PASS, 1 passed

- cargo test -p tokscale-core token_count: PASS, 11 passed

- cargo test -p tokscale-core codex_cache: PASS, 2 passed

- cargo test -p tokscale-core: PASS, 600 passed, 1 ignored; codebuff 10 passed; hermes 3 passed; doc-tests 0 passed

- cargo clippy -p tokscale-core --all-features -- -D warnings: PASS

- cargo fmt --all --check: PASS

- git diff --check: PASS

- git diff --cached --check: PASS

- Cyrillic scan for touched source files: PASS, no matches

Rollback

- git revert HEAD
@IvGolovach IvGolovach force-pushed the codex/codex-resume-model-attribution branch from c3ca2ce to 58a9732 Compare May 3, 2026 08:29
Copy link
Copy Markdown
Owner

@junhoyeo junhoyeo left a comment

Choose a reason for hiding this comment

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

Thanks!

@junhoyeo junhoyeo merged commit e11e0fd into junhoyeo:main May 3, 2026
14 checks passed
@IvGolovach IvGolovach deleted the codex/codex-resume-model-attribution branch May 3, 2026 22:03
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.

codex 0.124.0: 'unknown' model with cost=0 on resumed sessions (token_count events replay before turn_context)

2 participants