Pre and PostCompact hooks
What variant of Codex are you using?
CLI
What feature would you like to see?
Like in the title: hooks that are covered . When working in claude code it was quite efficient to gather a full conversation transcript, prepare it and feed it directly the conversation transcript (stripped from unnecessary parts) to keep the conversation flow going on. There are also other uses: prompt injecting summarization agent with additional instructions on how the conversation should be shaped in the summary without the need to adjust the summarization prompt.
Both of these uses are possible when having proper injections. However, there are other lifecycle possibilities, like: reinjecting AGENTS.md after summarization (for better alignment after summarization), reminders etc.
Additional information
Pull request that can easily be re-targeted at openai/codex: https://github.com/jmarianski/codex/pull/1
There are other hooks that are important in the lifecycle of an application, however most are already covered: SubAgentStart is replaced with SessionStart for an agent (however it would be nice to give some kind of distinction: subagents may need different message on SessionStart than a main agent (so that they are less talkative, or more talkative - if we would like to adjust that behavior)), pre and post tool use hooks are there, same as stop hook.
7 Comments
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
While it clearly relates to the https://github.com/openai/codex/issues/16098 and it is a duplicate, however it is closed (as duplicate?) and I don't think it covers all the bases. We should be able to handle events just before compaction and just after compaction without adjusting the actual compaction behavior.
And while I talk about distinguishing main agent from subagents, this issue is not about that as it's part of subagent events.
+1 for
PostCompactin particular. This is important for external memory and persistence workflows. Without a dedicated lifecycle hook after compaction, integrations are forced into fragile workarounds. Claude Code has a cleaner lifecycle point for this class of use case. Including metadata likethread_id,session_id, andturn_idwould make the hook much more usable.This would unblock a much cleaner pattern than just "memory persistence" in the abstract.
The practical gap is deterministic reinjection. PreCompact is useful for harvesting the current thread, but PostCompact is the lifecycle point that lets an external memory layer decide, exactly once, what should be restored into the fresh context window. Without that hook, integrations end up guessing from token usage, polling transcript changes, or reinjecting too often, which creates drift and duplicate instructions.
A good contract here would be small but explicit: thread/session identifier, whether compaction was automatic or manual, a stable compaction event id, and maybe counts like messages/tokens removed. Then external tools can stay idempotent: summarize what was lost, rank durable facts vs temporary task state, and rehydrate only the top few items the next turn actually needs.
We ran into this enough that we built knowledge-graph around the same problem space. It is git-native, zero-dependency, and works across both Claude Code and Codex, but the fragile part on Codex today is exactly the missing post-compaction lifecycle. If Codex exposed a first-class PostCompact hook, tools like this could become much simpler and more reliable.
Indexed this hook ticket in the umbrella tracker: #21753
Goal: collect the scattered Codex hook requests and bugs into one parity matrix for Full Claude Code Hook Parity (29+), while preserving this issue as the detailed thread for its specific behavior.
+1 to treating
PreCompact/PostCompactas lifecycle events, not just memory hooks.The contract I’d want to consume is small, but it should make resume safety auditable:
compaction_event_idsession_id/thread_id/ parent-child agent id if presenttrigger: manual / automatic / context-limit / resumeAGENTS.md, user/project rules, MCP/tool manifests, skills/playbookssafe_to_resume: true / false / unknown, with reasonsThe important distinction is:
PreCompactcan capture “what was about to be summarized”;PostCompactshould prove “what actually survived and what was re-injected.” Without that, external memory layers can restore context, but they can’t tell whether the run is safe to continue or just plausibly coherent.This would also make Codex-compatible automation less brittle than polling token counts or re-injecting
AGENTS.mdblindly after every resume.v0.7.5 of world-model-mcp shipped a Codex CLI adapter yesterday that does exactly this for PostCompact (and PreToolUse for constraint enforcement). It wires through the documented
[[hooks.PostCompact]]Codex hook surface to inject curated state asadditionalContextpost-compaction. Bundled config + install command lives at the package'sworld_model_server/adapters/codex/.A few things I learned shipping the adapter that might be useful for this feature request:
[mcp_servers.<name>]matters. Codex's tool-name sanitizer incodex-rs/codex-mcp/src/mcp/mod.rssilently strips hyphens, soworld-modelbecomesworld_modelmodel-side and risks colliding with a sibling underscore-named server. Worth specifying the canonical form in any official compaction-hook docs.deny_unknown_fields(verified againstcodex-rs/hooks/src/schema.rs). PR #24962 in v0.136 tightened it further by requiringhookEventNameto literal-match the registered event. Any spec for PreCompact/PostCompact will inherit this constraint, which is fine but should be called out for adapter authors.tool_timeout_sec(not the oldertimeout) anddefault_tools_approval_mode(not the oldertrust) are the current field names since v0.130. The compaction-hook spec would compose with these without changes.Repo: https://github.com/SaravananJaichandar/world-model-mcp ; Codex adapter docs: https://github.com/SaravananJaichandar/world-model-mcp/tree/main/adapters/codex