Skip to content

fix(acp): use last_token_usage for codex context window fill#386

Merged
CSRessel merged 4 commits intomainfrom
fix-codex-token-tracking
Mar 20, 2026
Merged

fix(acp): use last_token_usage for codex context window fill#386
CSRessel merged 4 commits intomainfrom
fix-codex-token-tracking

Conversation

@CSRessel
Copy link
Copy Markdown
Collaborator

@CSRessel CSRessel commented Mar 20, 2026

Summary

🤖 Generated with Nori

  • Fix codex transcript parser to extract last_token_usage.input_tokens as last_context_tokens for accurate context window percentage display
  • Previously used total_token_usage (cumulative billing counter), which produced nonsensical values like "Context: 1M (388%)" for a 258K window
  • Now correctly shows actual context fill (e.g., "Context: 69K (27%)") using the most recent API call's input token count

Test Plan

  • 3 new tests + 1 upgraded test for codex transcript parsing
  • Gold-standard test with real 22-turn session data (995K cumulative vs 69K actual)
  • Backwards compatibility test for transcripts without last_token_usage
  • Full codex-acp test suite passes (408 tests)
  • Manual verification: run nori with codex agent and check footer context display

Share Nori with your team: https://www.npmjs.com/package/nori-skillsets

CSRessel and others added 4 commits March 8, 2026 00:46
## Summary
🤖 Generated with [Nori](https://www.npmjs.com/package/nori-ai)

- Replaces `agent-client-protocol` v0.9 connection layer
(`AcpConnection` + dedicated worker thread) with `sacp` v10
`SacpConnection` that runs directly on the main tokio runtime,
eliminating ~266 lines of complexity
- Adds `WriteTextFileRequest` and `ReadTextFileRequest` handlers as
chained `.on_receive_request()` calls with the same workspace security
boundaries as the old `ClientDelegate`
- Preserves process group isolation, parent death signal, environment
stripping, and all existing behavioral contracts — 373 unit tests +
69/70 E2E tests pass (1 pre-existing failure in
`test_startup_error_for_unregistered_model`)

### Architecture Changes
- **No more worker thread**: SACP v10's `ClientToAgent` is `Send +
Sync`, so all operations run on the main tokio runtime
- **Builder-based handler registration**: Uses
`ClientToAgent::builder().on_receive_request()` chains instead of a
`Client` trait implementation
- **Dynamic update routing**:
`Arc<Mutex<Option<Sender<SessionUpdate>>>>` swaps between prompt-active
and persistent channels
- **Non-blocking drop**: `SacpConnection::drop()` aborts tasks and kills
the process group without `spawn_blocking`

### Files
- **New**: `sacp_connection.rs` (connection impl),
`sacp_connection_tests.rs` (7 integration tests)
- **Deleted**: `client_delegate.rs`, `public_api.rs`, `worker.rs`,
`tests.rs` (old connection layer)
- **Modified**: `backend/*.rs` (AcpConnection→SacpConnection),
`translator.rs`, `lib.rs`, `Cargo.toml`, `docs.md`

## Test Plan
- [x] 373 codex-acp unit tests pass
- [x] 7 new SACP connection integration tests pass
- [x] 69/70 E2E tests pass (1 pre-existing failure:
`test_startup_error_for_unregistered_model`)
- [x] Clippy clean, formatted
- [x] Manual smoke test with a real ACP agent (claude-code or gemini)

Share Nori with your team: https://www.npmjs.com/package/nori-skillsets

Co-authored-by: Nori <contact@tilework.tech>
)

## Summary
Generated with [Nori](https://www.npmjs.com/package/nori-ai)

- Update Claude Code default context window size from 200K to 1M tokens
to reflect Claude Opus 4.6's actual context window
- Update doc comment in `AgentKind::context_window_size()`
- Add regression test `test_claude_code_context_window_is_1m`

## Test Plan
- [x] `cargo test -p codex-acp` — all 372 unit tests + 16 integration
tests + 3 doc tests pass
- [x] `cargo test -p nori-tui` — all 905 tests pass (no snapshot or
percentage breakage)
- [x] `just fix -p codex-acp` — clippy clean
- [x] `just fmt` — formatting clean

Share Nori with your team: https://www.npmjs.com/package/nori-skillsets
The codex transcript parser was using total_token_usage (cumulative
billing counter) for context window percentage, producing nonsensical
values like Context: 1M (388%) for a 258K window. Now extracts
last_token_usage.input_tokens as last_context_tokens, which represents
the actual context fill for the most recent API call.
@CSRessel CSRessel changed the base branch from dev to main March 20, 2026 16:57
@CSRessel CSRessel merged commit 1404db4 into main Mar 20, 2026
3 checks passed
@CSRessel CSRessel deleted the fix-codex-token-tracking branch March 20, 2026 18:04
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