Skip to content

Fix Codex host detection at runtime, and PostToolUse capture on Codex - #464

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/463
Aug 29, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/463

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What

#460 shipped Codex-native summarizer routing, but it was unreachable: pipeline/host.py keyed Codex detection on CODEX_HOME/PLUGIN_ROOT, neither of which a real codex exec process ever exports to a child (CODEX_HOME is a configuration path Codex reads, not a signature it exports). Every test in #460's own suite passed because a hand-built test environment set both variables happily -- only a live process could show the defect.

Closes #463

detect_host() now keys Codex's signature_vars on CODEX_SESSION_ID/CODEX_THREAD_ID, which a real Codex process (codex-cli 0.150.1, verified against a live codex exec run from inside this worktree) does export. Pinned against tests/fixtures/codex-env-463.txt, a fixture captured from that live session rather than one constructed in the test -- a constructed fixture would have accepted CODEX_HOME just as readily as the code it replaced.

A second, distinct defect from the same investigation: a Codex session launched from inside a Claude Code session (nested agents) carries both hosts' native signatures in its environment at once. detect_host() keeps registry precedence (Claude Code wins) in that case. The REGISTRY comment in pipeline/host.py argues this at length: there is no ancestry marker in flat environment variables, so "prefer the innermost host" cannot be implemented honestly -- it would guess a direction, and would guess wrong for the (rarer, but real) reverse nesting of Claude Code launched inside a Codex sandbox. A third AMBIGUOUS host state was considered and rejected: detect_host() has exactly one consumer, and that consumer's own contract is already binary (codex vs. everything else is claude), so AMBIGUOUS would collapse into the same branch as UNKNOWN with no behavioural difference -- a label nothing reads.

Closes #459

Separately: scripts/post-tool-hook.sh's per-tool-call save path derived a session directory by slugging PROJECT_DIR under ~/.claude/projects/ -- Claude Code's own transcript layout. Codex writes to ~/.codex/sessions/<yyyy>/<mm>/<dd>/rollout-*.jsonl instead, so the slug never matched and every Codex tool call logged a loud, correct WARNING: no session dir whose suggested remedy (make a directory under ~/.claude/projects/) was advice for the wrong host. The hook now reads transcript_path off its own stdin payload -- present on every hook event, on every host, the same field session-start-hook.sh/session-end-hook.sh already trust (#407/#424) -- and uses it directly when it names a real file, falling back to the exact prior SESSION_DIR/slug behaviour when stdin offers nothing usable (an older CLI, a test harness, an unrecognised host).

Also adds three README.md rows (REMEMBER_SUMMARIZER, REMEMBER_SUMMARIZER_FALLBACK, REMEMBER_CODEX_BIN) that #460 shipped without documenting.

A reviewer finding, argued down

A spawned reviewer flagged that the Claude-Code-wins tie-break above means a Codex-nested-in-Claude-Code session resolves REMEMBER_SUMMARIZER=auto to claude, not codex -- reproducing the shape of #460's original complaint in that one nested scenario, and possibly failing outright in a network-disabled Codex sandbox. This is real and is the exact edge case the REGISTRY comment above already discusses and decides: BELOW-BAR, not fixed here. The reviewer's own proposed fix (prefer Codex on tie) is not strictly better -- it fails the reverse nesting direction (Claude Code launched inside a Codex sandbox) the same way, for the same underlying reason: flat environment variables carry no ancestry information, so any fixed tie-break direction is wrong for one of the two nesting orders. Solving this in general needs a real ancestry signal neither host publishes, which is a bigger and separately-scoped fix than this issue asked for. Filed as an open question, not a bug this diff introduces.

Test plan

  • tests/test_codex_signature_463.py (new): detect_host() against a captured real Codex environment; CODEX_HOME/PLUGIN_ROOT alone no longer signal Codex (must-not-fire pair); the nested-signature tie-break is pinned.
  • tests/test_codex_transcript_path_459.py (new): a Codex-shaped PostToolUse payload with a transcript outside ~/.claude/projects/ is captured; the no session dir WARNING is absent in that case; an unusable transcript_path degrades to the historical SESSION_DIR/slug lookup rather than disabling capture.
  • tests/test_summarizer_routing_460.py updated (CODEX_HOME setenv swapped for CODEX_SESSION_ID, the only valid signature now) and still green.
  • Existing regression controls (tests/test_non_ascii_paths.py, tests/test_claude_config_dir.py, tests/test_post_tool_session_id.py) unchanged and green.
  • Full suite: 1872 passed, 43 skipped (platform-specific skips only), 91.76% coverage.
  • python3 .oss/assemble_changelog.py --check passes.

…dex (#463, #459)

#460's Codex routing keyed detect_host() to CODEX_HOME/PLUGIN_ROOT, neither
of which Codex ever exports to a child process -- CODEX_HOME is a config
path it reads, not a signature. detect_host() now keys on
CODEX_SESSION_ID/CODEX_THREAD_ID, pinned against a fixture captured from a
live codex exec session rather than one a test could construct to agree
with itself. Nested Codex-inside-Claude-Code (both signatures present at
once) keeps registry precedence, argued in the REGISTRY comment in
pipeline/host.py.

scripts/post-tool-hook.sh's per-tool-call save path derived a session
directory by slugging PROJECT_DIR under ~/.claude/projects/ -- Claude
Code's own layout, never Codex's ~/.codex/sessions/. It now reads
transcript_path off the hook's own stdin payload (present on every hook
event, every host) and uses it directly when valid, falling back to the
historical SESSION_DIR/slug lookup exactly as before when stdin offers
nothing usable.

Adds three README rows (REMEMBER_SUMMARIZER, REMEMBER_SUMMARIZER_FALLBACK,
REMEMBER_CODEX_BIN) that #460 never documented.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@fdaviddpt
fdaviddpt merged commit 7948b6f into main Aug 29, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/463 branch August 29, 2026 12:43
fdaviddpt added a commit that referenced this pull request Aug 29, 2026
…t its environment (#465) (#466)

* fix: Codex summarizer routing reads the transcript the host wrote, not its environment (#465)

Under REMEMBER_SUMMARIZER=auto, a default-configured Codex session still
routed to `claude -p` -- #464's CODEX_SESSION_ID/CODEX_THREAD_ID fixture
was captured from a Codex tool shell, not the SessionEnd hook process that
actually spawns the summarizer, and a live capture from inside that hook
(env dumped, CLAUDE_CODE_* stripped, codex-cli 0.150.1) shows neither
variable survives into it. pipeline.haiku._choose_summarizer_provider()
now reads REMEMBER_TRANSCRIPT_PATH and sniffs the transcript's own first
line (pipeline.extract.sniff_file_envelope(), #443) instead -- what the
host wrote, not what it exported. Verified against a live, default-
configured `codex exec` session: SessionEnd now logs `provider: codex`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* fix: address self-review findings on #465 -- stale docstring + unlogged sniff failure

Explore (reviewer) found pipeline/host.py's module docstring still
described the summarizer as "chosen by detect_host()", directly
contradicting the AMBIGUOUS-comment update three commits' worth of prose
below it -- both now describe the real #465 mechanism.

oss:auditor found _choose_summarizer_provider() silently collapsed "the
transcript is genuinely Claude Code" and "the transcript could not be
sniffed" (unreadable, deleted mid-race, or an unrecognised shape) into the
same unlogged "claude" result -- indistinguishable to an operator
debugging a wrongly-billed session. It now warns (via the existing _warn())
only on the "could not sniff" branch, never on the ordinary "no transcript
at all" path, with positive/negative tests for both.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant