Add background prefix compaction prewarm

This commit is contained in:
Rohit Arunachalam
2026-04-09 20:31:41 -07:00
parent ff1ab61e4f
commit ebf13cda36
51 changed files with 1900 additions and 32 deletions
@@ -816,11 +816,14 @@ impl ThreadHistoryBuilder {
});
}
fn handle_context_compacted(&mut self, _payload: &ContextCompactedEvent) {
fn handle_context_compacted(&mut self, payload: &ContextCompactedEvent) {
let id = self.next_item_id();
self.ensure_turn()
.items
.push(ThreadItem::ContextCompaction { id });
.push(ThreadItem::ContextCompaction {
id,
kind: payload.kind.clone().map(Into::into),
});
}
fn handle_entered_review_mode(&mut self, payload: &codex_protocol::protocol::ReviewRequest) {
@@ -49,6 +49,7 @@ use codex_protocol::plan_tool::StepStatus as CorePlanStepStatus;
use codex_protocol::protocol::AgentStatus as CoreAgentStatus;
use codex_protocol::protocol::AskForApproval as CoreAskForApproval;
use codex_protocol::protocol::CodexErrorInfo as CoreCodexErrorInfo;
use codex_protocol::protocol::ContextCompactionKind as CoreContextCompactionKind;
use codex_protocol::protocol::CreditsSnapshot as CoreCreditsSnapshot;
use codex_protocol::protocol::ExecCommandSource as CoreExecCommandSource;
use codex_protocol::protocol::ExecCommandStatus as CoreExecCommandStatus;
@@ -138,6 +139,38 @@ pub enum NonSteerableTurnKind {
Compact,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "snake_case")]
#[ts(rename_all = "snake_case", export_to = "v2/")]
pub enum ContextCompactionKind {
#[default]
Classic,
Prefix,
}
impl ContextCompactionKind {
pub fn is_classic(&self) -> bool {
matches!(self, Self::Classic)
}
pub fn default_classic_option() -> Option<Self> {
Some(Self::Classic)
}
pub fn is_classic_option(kind: &Option<Self>) -> bool {
kind.as_ref().is_none_or(Self::is_classic)
}
}
impl From<CoreContextCompactionKind> for ContextCompactionKind {
fn from(kind: CoreContextCompactionKind) -> Self {
match kind {
CoreContextCompactionKind::Classic => Self::Classic,
CoreContextCompactionKind::Prefix => Self::Prefix,
}
}
}
/// This translation layer make sure that we expose codex error code in camel case.
///
/// When an upstream HTTP status is available (for example, from the Responses API or a provider),
@@ -4479,7 +4512,15 @@ pub enum ThreadItem {
ExitedReviewMode { id: String, review: String },
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
ContextCompaction { id: String },
ContextCompaction {
id: String,
#[serde(
default = "ContextCompactionKind::default_classic_option",
skip_serializing_if = "ContextCompactionKind::is_classic_option"
)]
#[ts(optional)]
kind: Option<ContextCompactionKind>,
},
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
@@ -4894,9 +4935,10 @@ impl From<CoreTurnItem> for ThreadItem {
result: image.result,
saved_path: image.saved_path,
},
CoreTurnItem::ContextCompaction(compaction) => {
ThreadItem::ContextCompaction { id: compaction.id }
}
CoreTurnItem::ContextCompaction(compaction) => ThreadItem::ContextCompaction {
id: compaction.id,
kind: compaction.kind.map(Into::into),
},
}
}
}
@@ -5534,6 +5576,12 @@ pub struct WindowsSandboxSetupCompletedNotification {
pub struct ContextCompactedNotification {
pub thread_id: String,
pub turn_id: String,
#[serde(
default = "ContextCompactionKind::default_classic_option",
skip_serializing_if = "ContextCompactionKind::is_classic_option"
)]
#[ts(optional)]
pub kind: Option<ContextCompactionKind>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS, ExperimentalApi)]
@@ -6395,6 +6443,50 @@ mod tests {
absolute_path("readable")
}
#[test]
fn context_compaction_thread_item_defaults_to_classic_kind() {
let item: ThreadItem = serde_json::from_value(json!({
"type": "contextCompaction",
"id": "compact-1"
}))
.expect("thread item should deserialize");
assert_eq!(
item,
ThreadItem::ContextCompaction {
id: "compact-1".to_string(),
kind: Some(ContextCompactionKind::Classic)
}
);
let serialized = serde_json::to_value(&item).expect("thread item should serialize");
assert_eq!(
serialized,
json!({
"type": "contextCompaction",
"id": "compact-1"
})
);
}
#[test]
fn context_compaction_thread_item_serializes_prefix_kind() {
let item = ThreadItem::ContextCompaction {
id: "compact-1".to_string(),
kind: Some(ContextCompactionKind::Prefix),
};
let serialized = serde_json::to_value(&item).expect("thread item should serialize");
assert_eq!(
serialized,
json!({
"type": "contextCompaction",
"id": "compact-1",
"kind": "prefix"
})
);
}
#[test]
fn collab_agent_state_maps_interrupted_status() {
assert_eq!(