Skip to content

Stop Codex rejecting SessionStart hook output (split stdout/stderr) - #19

Merged
adit-chandra merged 2 commits into
mainfrom
fix-codex-sessionstart-stdout-schema
Jun 30, 2026
Merged

adit-chandra merged 2 commits into
mainfrom
fix-codex-sessionstart-stdout-schema

Conversation

@adit-chandra

Copy link
Copy Markdown
Contributor

Problem

Codex now fails the host-enrollment SessionStart hook every session:

• SessionStart hook (failed)
  error: hook returned invalid session start JSON output

The bootstrap writes its full diagnostic status object to stdout on every SessionStart:

{"host":"codex","needs_restart":true,"status":"needs_restart","systemMessage":"Promptless telemetry configured for Codex. Restart Codex to start telemetry."}

Codex validates SessionStart hook stdout against a strict schema — its Rust parser deserializes into a fixed struct with serde deny_unknown_fields, accepting only continue / stopReason / systemMessage / suppressOutput / hookSpecificOutput. Our diagnostic keys (status, host, needs_restart, reason) are outside that schema, so Codex rejects the whole object as "invalid session start JSON output". This hits every Codex session — including the common gate-disabled steady state ({"status":"disabled",...}), not just the configured case. (Strict per-event validation confirmed by openai/codex#18887, #27052.)

The premise added in #15 — "the extra field is ignored by the host hook parsers" — holds for Claude Code (lenient: ignores unrecognized top-level fields, which is why Claude never errored) but is false for Codex (strict). The error actually first appeared in #11, which dropped --quiet and started writing this JSON to Codex's stdout; #15 added systemMessage on the same wrong assumption and so didn't fix it.

Fix

Split the streams in _emit:

  • stdout carries only the schema-valid systemMessage ({"systemMessage": "..."}), and nothing at all when there is no message — Codex treats empty stdout as success.
  • stderr carries the full diagnostic object (status / host / needs_restart / reason, plus the message), which neither host parses as hook output but stays available for manual troubleshooting (the --quiet-off intent from Run host enrollment SessionStart hook without --quiet #11).

Claude keeps rendering the systemMessage banner; Codex stops erroring.

Tests

  • A shared _assert_session_start_streams helper now asserts the split on every bootstrap run: stdout is empty or exactly {systemMessage} (never the schema-breaking keys), stderr carries the diagnostic. This guards all existing cases against future stdout leaks.
  • New test_bootstrap_stdout_stays_codex_schema_safe pins the exact Codex-breaking fields out of stdout and verifies steady-state stdout is empty.
  • Updated two gate-disabled assertions (stderr == "" → stdout == "") since the diagnostic now lives on stderr.

Validation

  • uv run pytest — 136 passed
  • uv run ruff check src tests, uv run ruff format --check src tests, uv run ty check src tests — clean
  • Bootstrap script (extensionless, missed by CI globs) py_compiles and is ruff/format-clean
  • Manual smoke test of the gate-disabled Codex path: stdout is now empty (Codex success), diagnostic on stderr

The host-enrollment bootstrap wrote its full diagnostic status object to
stdout on every SessionStart:

  {"host":"codex","needs_restart":true,"status":"needs_restart",
   "systemMessage":"Promptless telemetry configured for Codex..."}

Codex validates SessionStart hook *stdout* against a strict schema (serde
`deny_unknown_fields`): only continue/stopReason/systemMessage/
suppressOutput/hookSpecificOutput are accepted. The diagnostic keys
(status/host/needs_restart/reason) are outside that schema, so Codex
rejects the whole object with "hook returned invalid session start JSON
output" — on every session, including the common gate-disabled case.

The #15 premise ("the extra field is ignored by the host hook parsers")
holds for Claude Code (lenient) but not Codex (strict). The error first
appeared in #11, which dropped --quiet and started writing this JSON to
Codex's stdout; #15 added systemMessage on the same wrong assumption.

Fix: split the streams in `_emit`. stdout carries only the schema-valid
systemMessage (and nothing when there is no message — Codex treats empty
stdout as success); the full diagnostic object goes to stderr, which
neither host parses as hook output but remains available for debugging.

- Tests assert the stream split (stdout is empty or exactly
  {systemMessage}; stderr carries the diagnostic) via a shared helper, so
  every bootstrap run now guards against stdout schema leaks.
- New regression test pins the exact Codex-breaking fields out of stdout.

uv run pytest (136 passed); ruff check / format --check / ty check clean;
bootstrap script py_compiles and is ruff/format-clean.
…t-stdout-schema

# Conflicts:
#	tests/test_managed_bootstrap.py
@github-actions

Copy link
Copy Markdown

Codex Bug Bot

No new bugs detected in this PR.

@adit-chandra
adit-chandra merged commit aa34e59 into main Jun 30, 2026
6 checks passed
@adit-chandra
adit-chandra deleted the fix-codex-sessionstart-stdout-schema branch June 30, 2026 04:40
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