Add pre_compact and post_compact hooks for context compaction
What feature would you like to see?
Please add two compaction lifecycle hooks:
pre_compact: fires immediately before Codex compacts the conversation contextpost_compact: fires immediately after compaction completes
Today Codex appears to expose hook surfaces around tool execution and session lifecycle. It also appears to emit compaction-related notifications/events such as thread/compacted / ContextCompacted after compaction. But there does not appear to be a first-class pair of user-configurable compaction hooks that users can rely on as stable automation points.
That gap makes it hard to build robust long-running workflows.
Why pre_compact matters:
- persist high-value session state before any context is discarded
- generate project-specific summaries or checkpoints while the full pre-compaction context is still available
- snapshot external state right before compaction, instead of trying to guess when compaction is about to happen
Why post_compact matters:
- react immediately after compaction succeeds
- rebuild derived state, refresh prompts, or append audit logs using the new compacted session state
- distinguish successful compaction from attempted compaction
Suggested behavior:
pre_compacttriggers once per compaction attempt, right before compaction beginspost_compacttriggers once after successful compaction finishes- provide thread/session identifiers and working directory
- provide a short reason or trigger source when available, such as manual
/compactvs automatic compaction - command hooks would already be useful; prompt hooks would also be welcome if they fit the current hook model
- if
pre_compactfails, either continue with surfaced warning or support an explicit block/abort policy - if compaction fails,
post_compactshould not fire, or should include an explicit failure status if the design prefers a single post-event path
Why this matters:
- post-compaction notifications/events alone are too late for workflows that need the full pre-compaction context
stopis not a substitute, because compaction can happen long before a task is done- external wrappers have to approximate compaction timing, which is brittle
- the pair of hooks would make compaction automation symmetrical with other lifecycle boundaries
A minimal first version would already be valuable even if these hooks are command-only.
Are you interested in implementing this feature?
No
Additional information
Local verification on codex-cli 0.117.0:
- hook/event names visible in the installed binary include
pre_tool_use,post_tool_use,session_start,user_prompt_submit, andstop - compaction-related post notifications/events such as
thread/compactedandContextCompactedare present - I could not find corresponding first-class
pre_compact/post_compacthooks
This seems like a natural addition for the CLI and would fit well with existing context-management behavior.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗