codex icon indicating copy to clipboard operation
codex copied to clipboard

hooks: no session identifier in hook payloads; SessionStart not emitted for app-server threads

Open chrisl10 opened this issue 1 month ago • 2 comments

Summary

Hooks receive no session identifier, and SessionStart is not emitted for threads created programmatically over app-server. Together these make per-session hook integrations (session memory, compaction continuity, telemetry) impossible on programmatic threads and unreliable on exec.

Environment

  • codex-cli 0.162.1, macOS
  • ~/.codex/hooks.json with SessionStart/UserPromptSubmit/PreToolUse/PostToolUse registered, [features] hooks = true
  • Driven both via codex exec and via a long-lived codex app-server process creating threads over MCP (orchestrator-style)

Observed

  1. codex exec: hooks fire (SessionStart runs and returns additionalContext correctly), but the JSON payload delivered on stdin carries no session identifier, so a hook cannot key captured events to a conversation.
  2. app-server with programmatically created threads: no SessionStart hook invocation observed for new threads at all (PostToolUse-style events may fire during turns, but there is no session boundary signal), so session-scoped context injection never happens for these threads.

Impact

Any hook-based integration that does per-session capture or injection. Concrete case: context-mode (the MCP+hooks context manager) works fully on platforms where hooks carry a session id, and on Claude Code writes a session DB immediately; on Codex its capture layer writes nothing because there is no session key, and programmatic threads never even receive the SessionStart injection. The upstream integration documents the Codex session-id field as N/A for this reason.

Ask

  • Include a stable session/thread identifier in hook payloads (stable across turns of one conversation, fresh per thread), so hooks can key state.
  • Emit SessionStart (or document an equivalent boundary event) for threads created through app-server.

Related adjacent gap already tracked: #18491 (PreToolUse updatedInput unsupported) - same theme of the hooks platform missing pieces integrations need.

chrisl10 avatar Oct 15 '26 17:10 chrisl10

(response from codex)

Thanks for the report. From the 0.162.1 sources, this appears to be expected behavior rather than a confirmed bug:

  • Command hooks already receive session_id on stdin.
  • SessionStart runs on the thread’s first turn, not immediately on thread/start.
  • Hooks must be trusted individually; [features] hooks = true alone does not make every hook eligible to run.
  • MCP-tool hooks receive only their configured arguments, so their template must include session_id if the tool needs it.

An immediate creation-time hook or replaying SessionStart after a hook becomes trusted would be a feature request.

If a trusted command hook still receives stdin without session_id, or does not run on the first turn, please share the hook configuration, raw stdin, and exact app-server request sequence. That would let us investigate a concrete bug.

etraut-openai avatar Oct 16 '26 05:10 etraut-openai

(Response from codex)

I checked this on macOS in both codex exec and a long-lived stdio app-server. Trusted command hooks receive session_id on stdin. SessionStart runs when the first turn starts, so creating the thread alone won't trigger it. For an MCP-tool hook, include the session ID in the configured arguments. Closing as expected behavior.

etraut-openai avatar Nov 29 '26 04:11 etraut-openai