Skip to content

Add a MessageDisplay hook for mid-turn reply streaming - #1

Open
yanchenko wants to merge 9 commits into
mainfrom
yanchenko/message-display-hook-proposal
Open

yanchenko wants to merge 9 commits into
mainfrom
yanchenko/message-display-hook-proposal

Conversation

@yanchenko

Copy link
Copy Markdown
Owner

Summary

  • Adds a MessageDisplay hook event so mid-turn assistant replies can be observed while streaming (today only Stop, once per turn, is available) — matching Claude Code's and the newly-merged Qwen Code MessageDisplay hook.
  • Full design/rationale, prior-art research, and adversarial review notes: docs/proposals/message-display-hook.md.
  • Motivating use case: DontSpeak (local voice narration for coding CLIs) currently has to bypass Codex's hook system entirely and attach to the app-server socket directly, which needs manual codex app-server daemon start + codex --remote unix:// setup. A native hook removes that friction, same as it already works for Claude Code and Qwen Code.

What's implemented

  • HookEventName::MessageDisplay (protocol/src/protocol.rs), plumbed through every exhaustive match site (config schema, discovery, dispatcher, analytics, TUI hooks browser, app-server v2 wire types, schema fixtures).
  • New hooks/src/events/message_display.rs: detached, fire-and-forget dispatch (reuses existing TOML matcher-group discovery + trust-on-first-use approval — only the per-invocation execution step is detached) with a true silence-based debounce, a cap on concurrently in-flight child processes, and an independently-bounded stdin write.
  • Configurable [hooks] message_display_debounce_ms (default 200ms); a final (is_final: true) delivery always bypasses the debounce.
  • Wired into both real insertion points in the turn-streaming loop in core/src/session/turn.rs (plain path + plan-mode path).
  • Regenerated schema fixtures (hook input schema, app-server-protocol TS/JSON, core/config.schema.json) and TUI snapshot fixtures.

This is NOT going to upstream directly

Per docs/contributing.md, openai/codex only accepts invited PRs — this stays on my own fork. It's the working reference for a lightweight issue I may open upstream, and/or just a personal patch for my own use in the meantime.

Test plan

  • cargo test -p codex-config -p codex-protocol -p codex-hooks -p codex-tui — all pass, including new coverage for debounce coalescing, is_final bypassing the debounce, and the zero-cost no-handlers-configured path.
  • Targeted codex-core integration suites (hooks, hooks_mcp, compact, token_budget, plan-mode) plus the full --lib unit suite — all pass.
  • cargo clippy --all-targets -D warnings clean on every touched crate.

yanchenko and others added 9 commits July 11, 2026 20:58
Draft proposal for closing the gap where Codex has no hook event that
fires while a reply is streaming (only Stop, once, at turn end) —
matching what Claude Code and the newly-merged Qwen Code MessageDisplay
hook (QwenLM/qwen-code#6489) already provide on their own hook systems.

Not yet submitted as an issue/PR; draft for review first.
Fixes from a critical review pass:
- Flag docs/contributing.md's invitation-only policy up front — the
  actual next step is a lightweight issue, not a PR or this level of
  design doc as a first move.
- Disambiguate the legacy_notify.rs precedent: only the detached
  execution primitive is reused, not its lack of matcher/trust config
  (an earlier draft read as self-contradictory on this point).
- Correct "one insertion point" to two call sites (plan-mode text
  flows through a separate function) — the original framing would
  have shipped a bug dropping narration during plan-mode turns.
- Expand the "~9 mechanical match-arm sites" list with several missed
  sites (analytics, TUI hooks browser + transcript rendering, a second
  independently-tested schema-fixture pipeline, snapshot fixtures).
- Correct the docs/config.md location claim; no file in this repo
  documents individual hook events today.
- Note real tokio concurrency around turns/subagents that the original
  "small buffer" sizing for debounce state understated.
- Expand the motivation section so it's self-contained context for
  another agent/reviewer, describing Claude Code's and Qwen Code's
  MessageDisplay hooks and this proposal's purpose without requiring
  prior conversation context.
Adds the new HookEventName::MessageDisplay variant (protocol crate) and
the matching TOML matcher-group field on HookEventsToml plus a new
top-level [hooks] message_display_debounce_ms config field (config
crate), threaded through into Config and the regenerated config.schema.json.
Additive/backward-compatible only; per docs/proposals/message-display-hook.md.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
New hooks/src/events/message_display.rs: a per-turn MessageDisplayHandle
that debounces (~200ms, configurable) cumulative-text deliveries per
streamed item, bypasses the debounce for the final delivery, caps
concurrently in-flight child processes with a semaphore, and
independently bounds each child's stdin write (guarding openai#27550)
since it deliberately does not reuse command_runner's blocking-wait
execution path (see the proposal doc §1 disambiguation: discovery,
matcher parsing, and trust-on-first-use are fully reused; only the
per-invocation execution primitive is replaced, modeled on
legacy_notify.rs's spawn-and-don't-wait shape).

Also wires MessageDisplay through every mechanical exhaustive-match site
the HookEventName enum touches in this crate: dispatcher matcher-class
arms, event-name/scope labels, ConfiguredHandler's label, the input-only
JSON schema (HookEventNameWire + MessageDisplayCommandInput, regenerated
fixture), and HooksConfig/ClaudeHooksEngine's construction to thread the
new debounce config value through.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Adds MessageDisplay to the v2 HookEventName enum mirror and to
ManagedHooksRequirements (plus its config_processor.rs mapping), which
the compiler's exhaustiveness check on the ManagedHooksRequirementsToml
destructure in app-server/src/request_processors/config_processor.rs
flagged as a required update the design doc's own site enumeration had
missed. Regenerates the TS/JSON schema fixtures both
`write_hooks_schema_fixtures` and `write_schema_fixtures` produce.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Builds a per-turn MessageDisplayHandle (None when no handlers are
configured, so the token-streaming hot path is a single Option check)
in run_turn and threads it through both real dispatch call sites the
design doc identifies: emit_streamed_assistant_text_delta's plain path
and handle_plan_segments' Normal-segment path (plan-mode turns route
through the latter and would otherwise narrate nothing).

Deviates from the doc's own §2/§3 plan in one respect: rather than
threading an `is_final: bool` through emit_streamed_assistant_text_delta
itself, MessageDisplayHandle tracks per-item cumulative text internally
and exposes a separate finish_item(), called unconditionally by the two
flush wrappers (flush_assistant_text_segments_for_item/_all - the latter
has two call sites in run_turn, one more than the doc counted). Tracing
the existing early-return guards showed the doc's original plan would
silently drop the final MessageDisplay delivery whenever a flush round
parses no fresh text, which is the common case since most items already
streamed their last chunk via a prior delta.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Mechanical exhaustiveness fixes the compiler surfaced beyond the design
doc's own enumeration: analytics' metric event-name mapping, the sample
thread-manager binary's direct Config literal, and a test-only
hook_event_label match in tui/src/chatwidget/tests/helpers.rs.

Also fills in the two TUI sites needing real copy rather than plain
enumeration: hooks_browser_view's event_description gets user-facing
text for the /hooks browser, and hook_cell's hook_event_label gets a
label (not a panic) even though MessageDisplay never actually emits
HookStarted/HookCompleted, confirming the doc's own suspicion that this
site doesn't need real behavior, just a safe stub.

Regenerates the 4 insta snapshots that enumerate all HookEventName
variants (3 in bottom_pane/hooks_browser_view, 1 in
chatwidget/hooks_popup_shows_list_diagnostics - the latter not called
out by name in the design doc, which only gestured at "chatwidget
snapshots" generally).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Short status update at the top of the proposal recording that the §5
"smallest defensible first PR" scope is implemented on this branch, plus
the one place implementation diverged from the doc's own §2/§3 plan and
why.

Co-Authored-By: Claude Sonnet 5 <[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

Development

Successfully merging this pull request may close these issues.

1 participant