Skip to content

fix(codex): skip replayed parent usage in fork logs - #649

Merged
IvGolovach merged 1 commit into
junhoyeo:mainfrom
IvGolovach:codex/submit-inflated-codex-totals-20260531
Jun 4, 2026
Merged

fix(codex): skip replayed parent usage in fork logs#649
IvGolovach merged 1 commit into
junhoyeo:mainfrom
IvGolovach:codex/submit-inflated-codex-totals-20260531

Conversation

@IvGolovach

@IvGolovach IvGolovach commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #601.

This updates the Codex session parser so forked subagent logs no longer count replayed parent token_count history as fresh usage. It also bumps the source message cache schema so previously cached inflated Codex parse results are not reused.

Why

Codex fork/subagent JSONL logs can embed the parent session metadata and replay the parent transcript before the child session reaches its own turn. Those replayed rows can carry child-local timestamps, so timestamp-based deduplication treats them as distinct usage and tokscale submit can produce impossible daily totals such as tens of billions of Codex tokens and five-figure daily costs. The server-side validation failure is correct; the local parser was overcounting inherited replay history.

Diff scope

  • crates/tokscale-core/src/sessions/codex.rs: tracks fork child session identity, skips nested parent replay until the first child-owned turn, and uses cumulative total_token_usage plus upstream session identity for Codex token_count dedup keys.
  • crates/tokscale-core/src/lib.rs: adds end-to-end parser/accounting regressions for parent replay across forked child logs while preserving real child usage and repeated legitimate turns.
  • crates/tokscale-core/src/message_cache.rs: bumps the source message cache schema from 16 to 17 to force reparsing with the corrected Codex semantics.

Branch integrity

  • Base branch: main
  • Validated base SHA: a86e688d620939d2c973c6d5625baa815ea223d7
  • Ahead/behind before PR open: 0 behind / 1 ahead
  • Merge base: a86e688d620939d2c973c6d5625baa815ea223d7
  • Diff proof was computed against freshly fetched origin/main.

Commit integrity

  • Introduced commit: c732360818e8f7a151101592243f624a0aa05118 fix(codex): skip replayed parent usage in fork logs
  • One logical parser/accounting fix.
  • Final diff contains only Codex parser tests, Codex parser logic, and the required cache schema bump.

Ledger and version proof

  • Ledger: not applicable - not required for selected validation mode/change family.
  • Version: not applicable - not required for selected validation mode/change family.

Diff hygiene

  • git diff --check origin/main...HEAD: PASS, no output.
  • Changed files:
    • crates/tokscale-core/src/lib.rs
    • crates/tokscale-core/src/message_cache.rs
    • crates/tokscale-core/src/sessions/codex.rs

Validation mode and proof

  • Selected mode: Mode 3 - shared parser/accounting behavior that directly affects local reports and submitted totals.
  • cargo fmt --all -- --check: PASS
  • cargo test -p tokscale-core codex: PASS, 72 passed.
  • cargo test -p tokscale-core: PASS, 817 passed, 1 ignored.
  • cargo clippy -p tokscale-core --all-targets -- -D warnings: PASS
  • Focused local verifier over real ~/.codex/sessions/2026/05/24/*.jsonl: PASS, corrected total was 177,833,822 tokens after skipping 614,239 inherited replay token_count rows from the local Codex corpus.
  • Full CLI cold parse over the entire local Codex corpus was not completed locally because it is dominated by multi-month, multi-GB JSONL I/O; the focused verifier used the real failing day and the Rust test suite covers the parser behavior.

Migration notes

  • DB migration: not applicable - no database migration changed.
  • Cache migration: source message cache schema bumps from 16 to 17 so old inflated Codex cache entries are ignored and rebuilt.

CI context confirmation

Pending - required CI will run after this PR is opened. CI workflow/context names are unchanged.

Runtime safety

The change is limited to local Codex source parsing and cache invalidation. It does not add network calls, blocking locks, unbounded queues, or submit API contract changes. No invariant regression introduced.

Documentation integrity

Not applicable - no user-facing command, runbook, or documented workflow changed.

Rollback plan

Rollback: revert this PR. DB downgrade: not applicable. Data repair: not applicable. Operational caveats: users may need to rerun local scan/submit after rollback if they want cache contents regenerated under the previous parser behavior.

Known residual risks

Remote CI is pending until the PR is opened. The parser intentionally relies on Codex v7-style turn/session ordering only while a forked child is replaying embedded parent metadata; if a future Codex format removes that ordering signal, the fallback is conservative and stops applying the replay skip rather than silently dropping child-owned usage.


Summary by cubic

Skips replayed parent token_count entries in Codex fork/subagent logs and deduplicates by cumulative totals to fix inflated usage and invalid submit totals. Also bumps the cache schema to force correct re-parsing.

  • Bug Fixes

    • Track the forked child session and ignore the embedded parent transcript until the child’s first turn.
    • Dedup token_count using cumulative total_token_usage plus upstream session id (fall back to timestamp only if totals are missing).
    • Add tests for parent replay across multiple forks and preserve real child turns.
  • Migration

    • Cache schema bumped to 17; Codex entries will reparse automatically.

Written for commit c732360. Summary will update on new commits.

Review in cubic

Validation
* Validation tier: Tier 3 - shared parser/accounting change affecting submit totals.
* cargo fmt --all -- --check: PASS
* cargo test -p tokscale-core codex: PASS, 72 passed.
* cargo test -p tokscale-core: PASS, 817 passed, 1 ignored.
* cargo clippy -p tokscale-core --all-targets -- -D warnings: PASS
* Focused local Codex 2026-05-24 verifier over real ~/.codex JSONL: PASS, 177,833,822 tokens after skipping 614,239 inherited replay token_count rows.
* git diff --check: PASS
* git diff --cached --check: PASS
* Ledger: not applicable - not required for selected validation tier/change family.
* Version: not applicable - not required for selected validation tier/change family.

Rollback
* git revert HEAD
@vercel

vercel Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
tokscale Ignored Ignored Preview Jun 1, 2026 12:02am

Request Review

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 3 files

Re-trigger cubic

@erophames

Copy link
Copy Markdown

Any movement on this? Tried to submit again and same error.

@IvGolovach
IvGolovach merged commit 4405584 into junhoyeo:main Jun 4, 2026
15 checks passed
@IvGolovach
IvGolovach deleted the codex/submit-inflated-codex-totals-20260531 branch June 4, 2026 21:28
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.

Validation Failure

3 participants