Wire background compaction into the turn lifecycle (#53800)

## What changed

- Launch background compaction at eligible pre-turn, mid-turn, and post-turn checkpoints when `AsyncCompaction` is enabled, capturing the current world state and client routing state.
- Adopt ready results at compaction checkpoints while preserving newer messages, tool outputs, and world-state updates. Continue normally if an optional result is rejected.
- Run one foreground compaction if an adopted result still exceeds the context limit.
- Prevent cancelled turns from launching new background work while preserving already-running work for later reuse.

## Testing

Add unit and scenario coverage for stale history and account changes, cancellation, pending-result reuse and fallback, retained tool outputs, compaction event pairing, and token usage accounting. Add a context snapshot covering a collaboration-mode change while compaction runs.

GitOrigin-RevId: 04ac028d9fe2e3ea64aacaaeb351c3c776cf3940
This commit is contained in:
rka-oai
2026-10-13 23:51:47 +00:00
committed by copyberry
parent 2b2d792305
commit 9e03960cf8
11 changed files with 1235 additions and 80 deletions
+8
View File
@@ -388,6 +388,14 @@ impl CodexThread {
self.session.flush_rollout().await
}
/// Test barrier: waits for background compaction without consuming its published result.
/// Call after the request starts, with foreground work gated to prevent new launches.
#[doc(hidden)]
pub async fn wait_for_background_compaction_for_test(&self) {
crate::session::async_compaction::wait_for_background_compaction_for_test(&self.session)
.await;
}
pub async fn submit_with_trace(
&self,
op: Op,