Skip to content

feat: Add Codex PreToolUse/PostToolUse hooks for graph context augmentation #715

Description

@Maple0517

Summary

codebase-memory-mcp already implements Codex SessionStart hook (#330) and Claude Code PreToolUse hook, but has no PreToolUse/PostToolUse hooks for Codex CLI. Codex now supports the same lifecycle hooks as Claude Code, and deserves the same proactive graph integration.

Problem

Codex CLI now supports PreToolUse/PostToolUse hooks similar to Claude Code. Currently:

  1. Claude Code gets PreToolUse hook: Intercepts Grep/Glob, injects graph context via hook-augment subcommand
  2. Codex only gets SessionStart: A static reminder that doesn't provide dynamic graph context
  3. Inferior experience: Codex users don't get proactive graph context injection during tool calls

Current State

What Claude Code gets (from cli.c)

Hook Behavior
PreToolUse Intercept Grep/Glob, inject graph context via hook-augment (non-blocking)
SessionStart One-line reminder to use graph tools
SubagentStart Inject context into Claude subagents

What Codex gets today (from #330 implementation)

Hook Behavior
SessionStart ✅ One-line reminder (implemented)
PreToolUse ❌ Missing
PostToolUse ❌ Missing

Proposed Solution

Extend the Codex hooks to include PreToolUse/PostToolUse, following the Claude Code pattern:

1. PreToolUse hook (high-value)

Intercept Grep/Glob/Bash calls and inject graph context:

# ~/.codex/config.toml
[[hooks.PreToolUse]]
matcher = "Grep|Glob|Bash"

[[hooks.PreToolUse.hooks]]
type = "command"
command = "/path/to/codebase-memory-mcp hook-augment"
timeout = 10

Implementation approach:

  • Reuse existing hook-augment subcommand (already implemented for Claude Code)
  • Add Codex-specific hook format in cli.c
  • Follow same non-blocking design as Claude Code hooks

2. PostToolUse hook (optional)

Augment tool output with graph context:

[[hooks.PostToolUse]]
matcher = "Bash"

[[hooks.PostToolUse.hooks]]
type = "command"
command = "/path/to/codebase-memory-mcp hook-augment --post"
timeout = 10

Technical Notes

Codex hooks format

Codex uses TOML-based config (~/.codex/config.toml):

[[hooks.PreToolUse]]
matcher = "ToolName1|ToolName2"

[[hooks.PreToolUse.hooks]]
type = "command"
command = "script-path"
timeout = 10

Differences from Claude Code

Aspect Claude Code Codex
Config format JSON (settings.json) TOML (config.toml)
Config location ~/.claude/settings.json ~/.codex/config.toml
Hook output JSON to stdout JSON to stdout
Timeout seconds seconds

Existing infrastructure

The hook-augment subcommand already exists and is agent-agnostic:

  • Takes a search token from stdin
  • Calls search_graph to find matching symbols
  • Returns structured context to stdout
  • Non-blocking (exits 0 on all failure paths)

Implementation location

  • Extend cbm_upsert_codex_hooks() in cli.c to add PreToolUse hook
  • Add cbm_install_codex_hook_script() to install the hook script
  • Reuse existing hook-augment subcommand

Hook script for Codex

Create a Codex-specific hook script that:

  1. Reads the tool call from stdin (JSON format)
  2. Extracts the search token (from Grep pattern, Glob pattern, or Bash command)
  3. Calls codebase-memory-mcp hook-augment with the token
  4. Returns the augmented context to stdout

Alternative: Reuse the existing cbm-code-discovery-gate shim script with Codex-specific path resolution.

Acceptance Criteria

  1. codebase-memory-mcp install detects Codex and installs PreToolUse hook
  2. PreToolUse hook intercepts Grep/Glob/Bash (same as Claude Code)
  3. Hook is non-blocking (exits 0 on all failure paths)
  4. Hook injects graph context via search_graph results
  5. codebase-memory-mcp uninstall removes Codex PreToolUse hook
  6. Tests cover fresh install, update, and removal
  7. Documentation updated in README.md Multi-Agent Support table

References

Additional Context

Proposed Multi-Agent Support Table Update

The README table currently shows:

Agent MCP Config Instructions Hooks
Claude Code .claude/.mcp.json 4 Skills PreToolUse (Grep/Glob graph augment, non-blocking)
Codex CLI .codex/config.toml .codex/AGENTS.md SessionStart reminder

After this change:

Agent MCP Config Instructions Hooks
Claude Code .claude/.mcp.json 4 Skills PreToolUse (Grep/Glob graph augment, non-blocking)
Codex CLI .codex/config.toml .codex/AGENTS.md PreToolUse (Grep/Glob graph augment, non-blocking) + SessionStart reminder

Note: I'm willing to contribute a PR if maintainers agree with this direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    editor/integrationEditor compatibility and CLI integrationenhancementNew feature or requestpriority/normalStandard review queue; useful PR with ordinary maintainer urgency.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions