Skip to content

fix(codex): read the response of a model request in a live session (0.4.1) - #336

Merged
gangtao merged 1 commit into
mainfrom
fix/codex-live-response
Nov 30, 2026
Merged

gangtao merged 1 commit into
mainfrom
fix/codex-live-response

Conversation

@gangtao

@gangtao gangtao commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #335.

Problem

Codex writes the token_usage_record line of a request after the PostModelRequest hook has returned. Plugin 0.4.0 looked for that line, so in a live session it found no response, sent the row as 0.3.0 did, and added 0.5 s to every model request.

Change

  • transcript.py: when the closing line is not in the file, the response is the output items written since the previous token_usage_record of the turn (or the start of the turn). A file that has the closing line is matched on it, by response_id when both sides have one, else by turn and token counts.
  • The plugin waits (up to 0.25 s, was 0.5 s) only when the file has nothing of the request yet. In all live runs below it did not wait.
  • Compaction requests (kind contains compaction) are not looked up: their summary is not a response item. Found in the live compaction run, where each one cost the full wait.
  • Docs (README, docs/cim.md, CLAUDE.md): response.tool_calls[].id is the model's call id. It equals tool_use_id for a function called directly (spawn_agent, wait_agent) and not for shell commands, where the model calls exec and each command has an id of its own. Also: PostModelRequest and the tool hooks of the same response run in no fixed order.
  • Version 0.4.1. No config change.

Tests

Unit tests: 122 pass (12 need the SDK; run with it too). New: the file cut where a live hook sees it, response_id matching, no wait when the items are there, compaction.

Live, Codex CLI 0.172.0 (codex exec), plugin from this branch, local Timeplus Enterprise 3.5.1-rc.1 with app 0.13.0. For every model request, what was stored was compared with the response read from the finished rollout file by response_id.

Case Export Model requests Result
Three commands and an answer hooks 4 match
Parallel commands in one exec, failing command, apply_patch, effort high, long answer hooks 6 match
AGENTGUARD_LLM_CONTENT=false hooks 2 match, no text
AGENTGUARD_LLM_CONTENT_CHARS=20 hooks 2 match, text cut
AGENTGUARD_INGEST_API=rest (port 8000) hooks 2 match
Two turns (resume) hooks 5 match
Sub-agent hooks 5 (2 in the sub-agent) match
Turn interrupted with SIGINT, then a new turn hooks 4 match, interrupt row sent
Forced compaction (3 compactions) hooks 7 match, none for compaction requests
Failed request (unknown model) hooks 1 row sent, no response
Three commands and an answer otel 4 match
Text limit 15, content off otel 2 + 2 match
Sub-agent, parallel commands otel 6 match
  • The plugin took 75 to 160 ms per hook (process start included); no hook waited.
  • In no live request was the closing line in the file when the hook ran.
  • App 0.13.0: llm_response CIM rows carry the response on both exports; the Agent Monitor shows the LLM calls of a running Codex turn.
  • Replay of 54 local rollout files (2,288 requests, Codex 0.163 to 0.172): the result on the file cut before the closing line equals the result on the finished file for every request.

Not tested

  • PermissionRequest: codex exec cannot ask for permission, so the hook did not fire.
  • The interactive Codex TUI and the desktop app (only codex exec was run).
  • MCP tool calls and web search calls.
  • Codex versions other than 0.172.0, live.

🤖 Generated with Claude Code

Codex writes the token_usage_record line of a request after the
PostModelRequest hook has returned, so plugin 0.4.0 found no response in
a live session and added 0.5 s to every model request (#335).

- Take the response items written since the previous token_usage_record
  of the turn when the closing line is not in the file yet.
- Match a closed request by response_id, else by turn and token counts.
- Wait (up to 0.25 s) only when the file has nothing of the request yet.
- Do not read the file for compaction requests.
- Docs: response.tool_calls[].id is the model's call id; it equals
  tool_use_id for a direct function call, not for the commands an exec
  call runs.
- Version 0.4.1.

Fixes #335

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@gangtao
gangtao merged commit 7c24a1f into main Nov 30, 2026
4 checks passed
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 plugin 0.4.0: a live session sends no response, because Codex writes token_usage_record after the PostModelRequest hook returns

1 participant