Skip to content

fix: stop Codex reading UserPromptSubmit as Failed on every prompt (#451) - #452

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/451
Aug 29, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/451

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What

Codex's own hook engine sniffs the first non-whitespace byte of a UserPromptSubmit handler's stdout (codex-rs/hooks/src/engine/output_parser.rs::looks_like_json): { or [ means "this is my JSON contract," and if that byte fails to parse against Codex's own output schema, the whole hook run is marked Failed -- regardless of exit code. scripts/user-prompt-hook.sh's prompt stamp, [HH:MM TZ -- user], has always opened with [, so Codex misread it as an attempted (and invalid) JSON contract on every single prompt. Invisible before #444, which is what first let the hook succeed (i.e. print anything) on a host that never sets CLAUDE_PROJECT_DIR.

The hook now detects host via CLAUDE_PROJECT_DIR (set by Claude Code, never by Codex or -- per its own docs -- Gemini CLI, the same signal resolve-paths.sh already keys a related distinction on) and wraps stdout in the JSON envelope Codex's own schema names on any host where it is unset. Claude Code's own output is unchanged, byte-for-byte.

Closes #451.

Mechanism, confirmed against openai/codex source

Sparse-cloned openai/codex and read codex-rs/hooks/src/engine/output_parser.rs and codex-rs/hooks/src/events/user_prompt_submit.rs::parse_completed directly -- this was reasoned in the issue, now observed against the real parser. Reproduced live against the pre-fix script (commit 163bc51): a Codex-shaped invocation prints [11:04 CEST -- floriandavid], which Codex's own logic (ported into the new test file's oracle, pinned against literal fixtures lifted from codex-rs's own test suite) marks Failed.

SessionStart -- confirmed NOT the same defect

The issue's brief raised this as an open question (SessionStart is very probably the same bug and my probe could not see it). Drove the real, unmodified scripts/session-start-hook.sh end-to-end against three shapes -- an empty store, a full store (every memory file present), and a store whose handoff was already delivered once (the one place in that 1400-line script that ever prints a line opening with [). In all three, the first stdout byte is a letter or =, never [/{ -- and the one bracket-opening line is always preceded, on the same run, by an unconditional === LAST HANDOFF === header one line earlier. session-start-hook.sh was NOT modified. See tests/test_session_start_first_byte_is_never_bracket_or_brace.

Self-review found two follow-on issues, both fixed here

Spawned an independent reviewer and oss:auditor against the first commit. Findings, both real, both fixed in the second commit:

  1. Silent absence (auditor, fails-to-preserve): when jq is unavailable on a non-Claude-Code host, the fix silently dropped the stamp/notice with status Completed and no diagnostic anywhere -- strictly worse than pre-fix, which at least printed the raw stamp visibly (even though flagged Failed). Fixed by logging the loss via the existing log() helper (guarded so it costs nothing on the env-cache fast path, which never sources log.sh).
  2. Guard scope (auditor): the new test file's Windows skip was a module-level pytestmark covering three pure-Python tests that need no bash subprocess at all -- narrowed to a per-test @WINDOWS_SKIP on only the six tests that actually drive one.

A reviewer-found low-severity coverage gap (the jq-unavailable path had no test) is now covered by test_codex_payload_without_jq_stays_silent_and_logs_the_loss.

Docs

README.md (this repo's only docs_targets entry) is deliberately not touched -- it is held by a concurrent lane (#437) this tick per the brief. If this fix changes what README's Codex section should say, that is for the maintainer to route; nothing here assumes it.

Tests

tests/test_codex_upsubmit_stdout_451.py -- 9 tests: a Python oracle ported from and pinned against codex-rs's own test fixtures; Claude Code byte-for-byte regression control; Codex-shaped positive control (stamp survives inside the envelope); a prompt_stamp: off must-not-fire pairing; the jq-unavailable diagnostic-logging test; a notice-carrying systemMessage test; and the three-shape SessionStart refutation. All green. Existing tests/test_prompt_stamp_301.py and tests/test_hook_stdout_labelling_280.py (Claude Code regression controls named in the brief) pass unchanged.

Red confirmed by running the new Codex test against the pre-fix script (163bc51): test_codex_payload_wraps_the_stamp_and_reads_completed fails there with the exact reported symptom.

…nted (#451)

Codex's own hook engine sniffs the first non-whitespace byte of a
UserPromptSubmit handler's stdout: `{`/`[` means "this is my JSON
contract" (codex-rs/hooks/src/engine/output_parser.rs::looks_like_json),
parsed against user-prompt-submit.command.output.schema.json. This hook's
prompt stamp, "[HH:MM TZ -- user]", opens with `[`, so Codex tried to read
it as that contract, failed, and reported the hook Failed even though it
did its job -- invisible before #444, which is what first made the hook
succeed on a host that never sets CLAUDE_PROJECT_DIR.

On such a host (Codex, and -- reasoned, not observed -- Gemini CLI) the
hook now wraps its stdout in the JSON envelope Codex's own schema names.
Claude Code, where CLAUDE_PROJECT_DIR is always set, is unchanged
byte-for-byte (tests/test_prompt_stamp_301.py,
tests/test_hook_stdout_labelling_280.py). session-start-hook.sh was
suspected of the same defect and does not have it -- every line it prints
opens with a letter or `=`, confirmed against a full store, a
repeated-handoff delivery and an empty store.

Co-Authored-By: Max <noreply>
…#451)

Self-review of the #451 fix found two follow-on issues in the same file:

1. The jq-unavailable fallback only ran inside the `elif command -v jq`
   branch, so when jq was missing entirely (rather than present-but-failing)
   the outer if/elif had no matching arm at all and nothing happened --
   correct in effect (stdout stays silent, never the raw bracketed stamp),
   but by accident, not by branch coverage. Restructured so both "jq
   missing" and "jq present but produced nothing" share one `else`.

2. That shared branch now logs the loss via the existing `log()` helper
   (guarded by `declare -F log`, since the fast path never sources log.sh
   and must not gain a new spawn just to log an edge case it cannot reach):
   losing the prompt stamp/notice silently, with no diagnostic anywhere, is
   the same defect class in the other direction -- indistinguishable from
   "the user configured no stamp at all."

Also narrowed tests/test_codex_upsubmit_stdout_451.py's Windows skip from a
module-level `pytestmark` to a per-test `@WINDOWS_SKIP` on the six tests
that actually drive a `bash` subprocess -- the three pure-Python oracle
tests need no bash and were being skipped on windows-latest for a reason
that did not apply to them.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit def10e9 into main Aug 29, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/451 branch August 29, 2026 09:32
fdaviddpt added a commit that referenced this pull request Sep 1, 2026
…"not yet known" list (#453) (#495)

* docs: rewrite README's Codex section for the stdout-contract divergence (#451/#452) and rebuild its "not yet known" list (#453)

The Codex section previously said nothing about the UserPromptSubmit
stdout contract that made every Codex prompt read as Failed until
#451/#452, and its "not yet known" list still framed the open questions
from before that fix existed. Add a paragraph naming the mechanism (Codex
sniffs the first stdout byte and marks the run Failed on a bare `[`/`{`
that fails its own schema), record that session-start-hook.sh was checked
against three store shapes and does not share the defect, and rebuild the
list from what is actually still unknown today.

Co-Authored-By: Max <noreply>

* fix: #453 review -- README overclaimed "every line", test only proves the first stdout byte

The Explore reviewer caught it: scripts/session-start-hook.sh has one line
that opens with `[` ("[already delivered ...]"), so "every line ... never
`{`/`[`" was false on its face. What the cited test actually proves is
narrower and still sufficient -- the first stdout byte, which is what
Codex's own sniffing checks -- because that bracket-opening line is always
preceded on the same run by an unconditional `=== LAST HANDOFF ===`
header. Corrected the claim and carried the test's own OBSERVED-not-proven
caveat into the README.

Co-Authored-By: Max <noreply>
fdaviddpt added a commit that referenced this pull request Sep 4, 2026
…EADME (#534) (#537)

* Fix Gemini CLI CLAUDE_PROJECT_DIR assumption across shell layer and README (#534)

pipeline/host.py's GEMINI host was already corrected (#456) to read
CLAUDE_PROJECT_DIR, since gemini-cli 0.57.0's own bundled docs list it as
a compatibility alias Gemini sets -- but that correction was narrow and
dead-code-only. The much larger "Codex and Gemini CLI never set
CLAUDE_PROJECT_DIR" assumption baked into scripts/resolve-paths.sh,
scripts/lib-env-cache.sh, scripts/session-end-hook.sh,
scripts/post-tool-hook.sh, scripts/user-prompt-hook.sh, and README.md
went untouched.

The REMEMBER_HOOK_CWD fallback in resolve-paths.sh and the cache-key
fallback in lib-env-cache.sh needed only corrected comments: both stay
correct as fallbacks for any host that genuinely leaves
CLAUDE_PROJECT_DIR unset (Codex, live-confirmed via
tests/fixtures/codex-env-463.txt), and Gemini setting the variable just
means priority 1 wins and the fallback is never reached on that host.

scripts/user-prompt-hook.sh's UserPromptSubmit JSON-envelope branch
needed an actual behavioural fix: it used to key on "is
CLAUDE_PROJECT_DIR set" as a stand-in for "is this Codex", which would
have silently routed a Gemini session through Codex's JSON envelope --
unverified, since Gemini CLI's own BeforeAgent stdout contract has never
been observed and #532 blocks a live session. It now keys on Codex's own
live-verified signature (CODEX_SESSION_ID/CODEX_THREAD_ID, the same pair
pipeline/host.py's CODEX.signature_vars already uses), so the JSON
envelope is Codex-only by construction and every other host, Gemini
included, gets plain stdout -- a documented, defensible default, not a
claim about what Gemini's own contract wants.

TDD via tests/test_gemini_stdout_envelope_534.py, red on the pre-fix
gate (a host with neither CLAUDE_PROJECT_DIR nor a Codex signature used
to get the JSON envelope by default); tests/test_codex_upsubmit_stdout_451.py's
own Codex fixture updated to set the same signature, matching a real
codex exec process. README corrected everywhere it repeated the old
shared-fact claim. Changelog fragment added.

Co-Authored-By: Max <noreply>

* Self-review: reject the Codex-signature stdout-envelope gate (#534)

The auditor spawned in self-review found that the previous commit's
UserPromptSubmit JSON-envelope gate change -- keying on
CODEX_SESSION_ID/CODEX_THREAD_ID instead of CLAUDE_PROJECT_DIR -- was a
real regression, not an improvement: #465 already measured, live, that
neither variable survives into a process Codex spawns as a HOOK, only
into a Codex TOOL-SHELL command (pipeline/haiku.py's own note,
tests/test_codex_signature_463.py's docstring). scripts/user-prompt-hook.sh
is registered as exactly such a hook in hooks/hooks.codex.json, so the
new gate would have silently disabled the JSON envelope on every real
Codex UserPromptSubmit invocation and reopened #451/#452 (every Codex
prompt reading Failed again). tests/fixtures/codex-env-463.txt, the
fixture that pair is pinned against, was itself captured from a Codex
tool-shell command, not from inside a hook.

Reverted the gate to CLAUDE_PROJECT_DIR set/unset, unchanged from before
this PR. That mechanism was never wrong for the behavioural question
#534 was asked to settle: Gemini setting CLAUDE_PROJECT_DIR (per its own
docs, unverified live) just moves Gemini into the same plain-stdout
branch Claude Code already takes, which was already true before any code
change here -- only the comments describing it were stale. Documented
the considered-and-rejected Codex-signature alternative in
scripts/user-prompt-hook.sh, README.md, and the changelog fragment, so
the reasoning survives for a future reader who reaches for the same
"obvious" fix.

Rewrote tests/test_gemini_stdout_envelope_534.py as a positive/negative
control pinning the gate's actual (unchanged) behaviour rather than a
red/green story for a gate that no longer changes; reverted the
CODEX_SESSION_ID/CODEX_THREAD_ID additions to
tests/test_codex_upsubmit_stdout_451.py's Codex fixture, documenting why
they were deliberately removed rather than left inert. Also corrected
docs/reading-the-transcript-path.md, an adjacent doc the auditor's
sibling Explore review found still asserting the same stale "Gemini
documents no environment variables at all" claim.

Co-Authored-By: Max <noreply>

* Correct session-start-hook.sh's own stale CLAUDE_PROJECT_DIR comment (#534)

The auditor spawned in self-review found scripts/session-start-hook.sh:122
still asserting "only Claude Code also publishes it as CLAUDE_PROJECT_DIR"
-- the same "Codex and Gemini CLI never set CLAUDE_PROJECT_DIR" shared-fact
claim this issue set out to correct everywhere, left in the one hook script
not named in the original file list because it was missed rather than
excluded. Corrected to name Codex's own live-confirmed non-setting (#463)
and Gemini CLI's own claimed compatibility alias (#456, unverified live,
#532) separately, matching the sibling comment already fixed in
scripts/session-end-hook.sh.

Co-Authored-By: Max <noreply>
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 reports UserPromptSubmit as Failed once the hook actually produces output, because its stdout contract is not Claude Code's

1 participant