codex - 💡(How to fix) Fix Add a post-compaction hook for deterministic memory reinjection [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#19061Fetched 2026-04-23 07:31:09
View on GitHub
Comments
2
Participants
2
Timeline
7
Reactions
0
Timeline (top)
labeled ×4commented ×2closed ×1

Root Cause

Right now Codex does not expose a post-compaction hook, so external memory systems have to use brittle workarounds.

In our case, we maintain long-term memory with hooks and want to reinject that memory exactly once on the next turn after Codex auto-compacts. Claude Code already has a hook for this class of workflow, but Codex currently does not.

Because the hook is missing, the current workaround is:

  • scan the rollout JSONL logs under ~/.codex/sessions/
  • detect the exact structural event type == "event_msg" with payload.type == "context_compacted"
  • set a one-shot pending flag
  • reinject memory on the next UserPromptSubmit

That works, but it is not a good contract to build against:

  • it depends on rollout log file naming and JSON shape staying stable
  • it forces plugins to parse internal logs instead of using an official lifecycle event
  • it is easy to get edge cases wrong around partial trailing writes, offsets, and thread/session identifiers
  • reinjection becomes "best effort on the next submit" instead of a deterministic post-compaction lifecycle event

Fix Action

Fix / Workaround

Right now Codex does not expose a post-compaction hook, so external memory systems have to use brittle workarounds.

Because the hook is missing, the current workaround is:

This would make custom memory reinjection much more reliable and would remove a whole class of brittle workaround logic.

RAW_BUFFERClick to expand / collapse

What variant of Codex are you using?

CLI / app-server with hooks

What feature would you like to see?

A dedicated hook that fires after Codex compacts context for a thread.

Something like PostCompact, ContextCompacted, or another official post-compaction hook event would solve this.

Why this matters

Right now Codex does not expose a post-compaction hook, so external memory systems have to use brittle workarounds.

In our case, we maintain long-term memory with hooks and want to reinject that memory exactly once on the next turn after Codex auto-compacts. Claude Code already has a hook for this class of workflow, but Codex currently does not.

Because the hook is missing, the current workaround is:

  • scan the rollout JSONL logs under ~/.codex/sessions/
  • detect the exact structural event type == "event_msg" with payload.type == "context_compacted"
  • set a one-shot pending flag
  • reinject memory on the next UserPromptSubmit

That works, but it is not a good contract to build against:

  • it depends on rollout log file naming and JSON shape staying stable
  • it forces plugins to parse internal logs instead of using an official lifecycle event
  • it is easy to get edge cases wrong around partial trailing writes, offsets, and thread/session identifiers
  • reinjection becomes "best effort on the next submit" instead of a deterministic post-compaction lifecycle event

What would help

An official hook that runs after compaction with enough metadata to act safely, for example:

  • thread_id
  • session_id
  • turn_id
  • whether compaction was automatic or manual
  • timestamp

Even a minimal official event would be much better than asking plugin authors to tail and parse rollout logs.

Ideal outcome

With a real post-compaction hook, external memory / persistence plugins could stop doing log-based detection entirely and use the supported hook path instead.

This would make custom memory reinjection much more reliable and would remove a whole class of brittle workaround logic.

extent analysis

TL;DR

Implementing a dedicated post-compaction hook in Codex, such as PostCompact or ContextCompacted, would provide a reliable way for external memory systems to reinject memory after context compaction.

Guidance

  • The current workaround of scanning rollout JSONL logs for the context_compacted event is brittle and prone to errors, making a dedicated hook a more reliable solution.
  • To mitigate the current issues, plugin authors could focus on improving the log parsing logic to handle edge cases such as partial trailing writes and thread/session identifiers.
  • A post-compaction hook with metadata like thread_id, session_id, turn_id, and compaction type would enable safer and more deterministic memory reinjection.
  • Plugin authors could explore alternative solutions, such as using existing hooks or events, to reduce reliance on log parsing until a dedicated post-compaction hook is available.

Example

No code example is provided as the issue focuses on the need for a new hook rather than a specific implementation.

Notes

The implementation of a post-compaction hook would require changes to the Codex API and may involve modifications to the existing hook system. The exact details of the hook's implementation and metadata are not specified in the issue.

Recommendation

Apply a workaround, as the issue implies that a dedicated post-compaction hook is not currently available, and developing a custom solution or improving the existing log parsing logic may be necessary until a official hook is implemented.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

codex - 💡(How to fix) Fix Add a post-compaction hook for deterministic memory reinjection [2 comments, 2 participants]