| Phase | 7 — Core engine |
| Status | Not started |
| Depends on | 28 |
| Size | L |
| Drop-in critical | ★ (compaction triggers + history rewrite) |
Port context-window management and auto-compaction: token-threshold triggers, pre/mid/post-turn compaction, summarization, history replacement, and remote compaction endpoints.
reference-codex/codex-rs/core/src/compact.rs,compact_remote.rs,compact_remote_v2.rs.core/src/state/auto_compact_window.rs(AutoCompactWindowSnapshot),core/src/session/turn.rs(auto_compact_token_status), context-injection logic (InitialContextInjection).
- Token accounting/windows:
auto_compact_scope_tokens,*_limit,full_context_window_limit, window ordinal; per-scope evaluation (AutoCompactTokenLimitScope). - Trigger evaluation: pre-turn, mid-turn, post-turn decisions on whether to compact, matching Codex thresholds.
- Compaction execution: inline summarization (prompt templates) and remote compact
endpoints (v1/v2) with inline fallback; produce a
compactedrollout line withreplacement_history; emitContextCompacted. - Context injection: skill/plugin/tool/instruction fragments per turn and
reinjection after compaction; workspace-root resolution;
Compactop handling.
- For sequences crossing the threshold (recorded token usages),
codexgotriggers compaction at the same point and produces the samecompactedline +ContextCompactedevent as Codex. - Post-compaction history (replacement + reinjected fragments) matches Codex.
- Remote-compact path and inline fallback both produce equivalent results.
- Thresholds and scope arithmetic must be exact; off-by-one in token math shifts the trigger point and breaks differential tests.
- Summarization prompts must be copied verbatim.
- The base turn loop (28); manual
/compactUI (39).