fix: emit structured JSON from Codex context hooks - #85
Closed
MoonCaves wants to merge 1 commit into
Closed
Conversation
Codex classifies hook stdout beginning with `[` or `{` as attempted JSON and
parses it. Mnemon's SessionStart (prime) and UserPromptSubmit hooks emit context
beginning with `[mnemon]`, so Codex tries to parse it, fails, and reports
`hook returned invalid ... JSON output` on every session start and prompt.
Move the existing context into Codex's documented
`hookSpecificOutput.additionalContext` envelope:
- user_prompt.sh: emit the reminder as a static UserPromptSubmit object.
- prime.sh: pipe the existing status/warning/guide output through
`python3 json.dumps` into a SessionStart object (python3 is already required by
the sibling stop.sh). Existing context and MNEMON_DATA_DIR path resolution are
unchanged.
stop.sh already emitted valid JSON and is untouched.
Add internal/setup/assets/codex_hooks_test.go, which executes the embedded hook
byte slices and asserts their external JSON contract across the missing-binary,
stats, empty-status, MNEMON_DATA_DIR override, legacy-fallback, and
special-character-guide cases. The tests fail on the pre-patch hooks and pass
with this change.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Codex's hook parser classifies any hook stdout beginning with
[or{as attempted JSON (looks_like_json()incodex-rs/hooks/src/engine/output_parser.rs) and parses it. Mnemon'sSessionStart(prime) andUserPromptSubmithooks emit context that begins with[mnemon], so Codex tries to parse it, fails, and reportshook returned invalid ... JSON outputon every session start and every prompt.Fix
Move the existing context into Codex's documented
hookSpecificOutput.additionalContextenvelope, with no change to the context text itself:user_prompt.sh— emit the reminder as a single staticUserPromptSubmitobject.prime.sh— pipe the existing status / missing-binary-warning / guide output throughpython3 json.dumpsinto oneSessionStartobject.python3is already required by the siblingstop.sh, so this adds no new dependency. The existingMNEMON_DATA_DIR(with legacy~/.mnemon) path resolution is unchanged.stop.shalready emitted valid JSON and is untouched.Tests
internal/setup/assets/codex_hooks_test.goexecutes the actual embedded hook byte slices (CodexUserPromptHook,CodexPrimeHook) as subprocesses and asserts only their external JSON contract: exactly one object, correcthookEventName, andadditionalContextcarrying the prior text verbatim. Coverage includes missingmnemon, populatedmnemon status, empty status,MNEMON_DATA_DIRoverride, legacy-path fallback, and a guide fixture with quotes / backslash / tab / newline / non-ASCII (byte-exact round-trip). The tests fail against the pre-patch hooks and pass with this change.go test ./...andmake testpass.This is a correctness repair, not a feature.