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
@@ -832,6 +832,16 @@
"ContextCompactedNotification": {
"description": "Deprecated: Use `ContextCompaction` item type instead.",
"properties": {
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"threadId": {
"type": "string"
},
@@ -845,6 +855,13 @@
],
"type": "object"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"CreditsSnapshot": {
"properties": {
"balance": {
@@ -3181,6 +3198,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -7004,6 +7004,16 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Deprecated: Use `ContextCompaction` item type instead.",
"properties": {
"kind": {
"anyOf": [
{
"$ref": "#/definitions/v2/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"threadId": {
"type": "string"
},
@@ -7018,6 +7028,13 @@
"title": "ContextCompactedNotification",
"type": "object"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"CreditsSnapshot": {
"properties": {
"balance": {
@@ -13363,6 +13380,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/v2/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -3652,6 +3652,16 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Deprecated: Use `ContextCompaction` item type instead.",
"properties": {
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"threadId": {
"type": "string"
},
@@ -3666,6 +3676,13 @@
"title": "ContextCompactedNotification",
"type": "object"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"CreditsSnapshot": {
"properties": {
"balance": {
@@ -11218,6 +11235,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -1,7 +1,26 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
}
},
"description": "Deprecated: Use `ContextCompaction` item type instead.",
"properties": {
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"threadId": {
"type": "string"
},
@@ -195,6 +195,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1106,6 +1113,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -195,6 +195,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1106,6 +1113,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -331,6 +331,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1249,6 +1256,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -396,6 +396,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1763,6 +1770,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -334,6 +334,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1521,6 +1528,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -334,6 +334,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1521,6 +1528,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -334,6 +334,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1521,6 +1528,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -396,6 +396,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1763,6 +1770,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -334,6 +334,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1521,6 +1528,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -396,6 +396,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1763,6 +1770,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -334,6 +334,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1521,6 +1528,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -334,6 +334,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1521,6 +1528,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -331,6 +331,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1249,6 +1256,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -331,6 +331,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1249,6 +1256,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -331,6 +331,13 @@
],
"type": "string"
},
"ContextCompactionKind": {
"enum": [
"classic",
"prefix"
],
"type": "string"
},
"DynamicToolCallOutputContentItem": {
"oneOf": [
{
@@ -1249,6 +1256,16 @@
"id": {
"type": "string"
},
"kind": {
"anyOf": [
{
"$ref": "#/definitions/ContextCompactionKind"
},
{
"type": "null"
}
]
},
"type": {
"enum": [
"contextCompaction"
@@ -1,8 +1,9 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ContextCompactionKind } from "./ContextCompactionKind";
/**
* Deprecated: Use `ContextCompaction` item type instead.
*/
export type ContextCompactedNotification = { threadId: string, turnId: string, };
export type ContextCompactedNotification = { threadId: string, turnId: string, kind?: ContextCompactionKind, };
@@ -0,0 +1,5 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type ContextCompactionKind = "classic" | "prefix";
@@ -10,6 +10,7 @@ import type { CollabAgentToolCallStatus } from "./CollabAgentToolCallStatus";
import type { CommandAction } from "./CommandAction";
import type { CommandExecutionSource } from "./CommandExecutionSource";
import type { CommandExecutionStatus } from "./CommandExecutionStatus";
import type { ContextCompactionKind } from "./ContextCompactionKind";
import type { DynamicToolCallOutputContentItem } from "./DynamicToolCallOutputContentItem";
import type { DynamicToolCallStatus } from "./DynamicToolCallStatus";
import type { FileUpdateChange } from "./FileUpdateChange";
@@ -97,4 +98,4 @@ reasoningEffort: ReasoningEffort | null,
/**
* Last known status of the target agents, when available.
*/
agentsStates: { [key in string]?: CollabAgentState }, } | { "type": "webSearch", id: string, query: string, action: WebSearchAction | null, } | { "type": "imageView", id: string, path: string, } | { "type": "imageGeneration", id: string, status: string, revisedPrompt: string | null, result: string, savedPath?: string, } | { "type": "enteredReviewMode", id: string, review: string, } | { "type": "exitedReviewMode", id: string, review: string, } | { "type": "contextCompaction", id: string, };
agentsStates: { [key in string]?: CollabAgentState }, } | { "type": "webSearch", id: string, query: string, action: WebSearchAction | null, } | { "type": "imageView", id: string, path: string, } | { "type": "imageGeneration", id: string, status: string, revisedPrompt: string | null, result: string, savedPath?: string, } | { "type": "enteredReviewMode", id: string, review: string, } | { "type": "exitedReviewMode", id: string, review: string, } | { "type": "contextCompaction", id: string, kind?: ContextCompactionKind, };
@@ -69,6 +69,7 @@ export type { ConfigValueWriteParams } from "./ConfigValueWriteParams";
export type { ConfigWarningNotification } from "./ConfigWarningNotification";
export type { ConfigWriteResponse } from "./ConfigWriteResponse";
export type { ContextCompactedNotification } from "./ContextCompactedNotification";
export type { ContextCompactionKind } from "./ContextCompactionKind";
export type { CreditsSnapshot } from "./CreditsSnapshot";
export type { DeprecationNoticeNotification } from "./DeprecationNoticeNotification";
export type { DynamicToolCallOutputContentItem } from "./DynamicToolCallOutputContentItem";
@@ -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!(
@@ -1287,10 +1287,11 @@ pub(crate) async fn apply_bespoke_event_handling(
.send_server_notification(ServerNotification::PlanDelta(notification))
.await;
}
EventMsg::ContextCompacted(..) => {
EventMsg::ContextCompacted(event) => {
let notification = ContextCompactedNotification {
thread_id: conversation_id.to_string(),
turn_id: event_turn_id.clone(),
kind: event.kind.map(Into::into),
};
outgoing
.send_server_notification(ServerNotification::ContextCompacted(notification))
+1
View File
@@ -48,6 +48,7 @@ const SUPPORTED_EXPERIMENTAL_FEATURE_ENABLEMENT: &[&str] = &[
"tool_search",
"tool_suggest",
"tool_call_mcp_elicitation",
"prefix_compaction",
];
#[async_trait]
@@ -88,10 +88,13 @@ async fn auto_compaction_local_emits_started_and_completed_items() -> Result<()>
let started = wait_for_context_compaction_started(&mut mcp).await?;
let completed = wait_for_context_compaction_completed(&mut mcp).await?;
let ThreadItem::ContextCompaction { id: started_id } = started.item else {
let ThreadItem::ContextCompaction { id: started_id, .. } = started.item else {
unreachable!("started item should be context compaction");
};
let ThreadItem::ContextCompaction { id: completed_id } = completed.item else {
let ThreadItem::ContextCompaction {
id: completed_id, ..
} = completed.item
else {
unreachable!("completed item should be context compaction");
};
@@ -169,10 +172,13 @@ async fn auto_compaction_remote_emits_started_and_completed_items() -> Result<()
let started = wait_for_context_compaction_started(&mut mcp).await?;
let completed = wait_for_context_compaction_completed(&mut mcp).await?;
let ThreadItem::ContextCompaction { id: started_id } = started.item else {
let ThreadItem::ContextCompaction { id: started_id, .. } = started.item else {
unreachable!("started item should be context compaction");
};
let ThreadItem::ContextCompaction { id: completed_id } = completed.item else {
let ThreadItem::ContextCompaction {
id: completed_id, ..
} = completed.item
else {
unreachable!("completed item should be context compaction");
};
@@ -232,10 +238,13 @@ async fn thread_compact_start_triggers_compaction_and_returns_empty_response() -
let started = wait_for_context_compaction_started(&mut mcp).await?;
let completed = wait_for_context_compaction_completed(&mut mcp).await?;
let ThreadItem::ContextCompaction { id: started_id } = started.item else {
let ThreadItem::ContextCompaction { id: started_id, .. } = started.item else {
unreachable!("started item should be context compaction");
};
let ThreadItem::ContextCompaction { id: completed_id } = completed.item else {
let ThreadItem::ContextCompaction {
id: completed_id, ..
} = completed.item
else {
unreachable!("completed item should be context compaction");
};
@@ -281,9 +281,9 @@ async fn experimental_feature_enablement_set_rejects_non_allowlisted_feature() -
error.message
);
assert!(
error
.message
.contains("apps, plugins, tool_search, tool_suggest, tool_call_mcp_elicitation"),
error.message.contains(
"apps, plugins, tool_search, tool_suggest, tool_call_mcp_elicitation, prefix_compaction"
),
"{}",
error.message
);
+2
View File
@@ -31,6 +31,8 @@ pub struct CompactionInput<'a> {
pub reasoning: Option<Reasoning>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<TextControls>,
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub prefix_mode: bool,
}
/// Canonical input payload for the memory summarize endpoint.
+9
View File
@@ -34,6 +34,15 @@ pub fn features_schema(schema_gen: &mut SchemaGenerator) -> Schema {
);
continue;
}
if feature.id == codex_features::Feature::PrefixCompaction {
validation.properties.insert(
feature.key.to_string(),
schema_gen.subschema_for::<codex_features::FeatureToml<
codex_features::PrefixCompactionConfigToml,
>>(),
);
continue;
}
validation
.properties
.insert(feature.key.to_string(), schema_gen.subschema_for::<bool>());
+30
View File
@@ -431,6 +431,9 @@
"plugins": {
"type": "boolean"
},
"prefix_compaction": {
"$ref": "#/definitions/FeatureToml_for_PrefixCompactionConfigToml"
},
"prevent_idle_sleep": {
"type": "boolean"
},
@@ -628,6 +631,16 @@
}
]
},
"FeatureToml_for_PrefixCompactionConfigToml": {
"anyOf": [
{
"type": "boolean"
},
{
"$ref": "#/definitions/PrefixCompactionConfigToml"
}
]
},
"FeedbackConfigToml": {
"additionalProperties": false,
"properties": {
@@ -1366,6 +1379,20 @@
},
"type": "object"
},
"PrefixCompactionConfigToml": {
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean"
},
"threshold_percent": {
"description": "Percentage of the auto-compaction threshold that starts background prefix compaction. When omitted, Codex uses 60%.",
"format": "int64",
"type": "integer"
}
},
"type": "object"
},
"ProjectConfig": {
"additionalProperties": false,
"properties": {
@@ -2233,6 +2260,9 @@
"plugins": {
"type": "boolean"
},
"prefix_compaction": {
"$ref": "#/definitions/FeatureToml_for_PrefixCompactionConfigToml"
},
"prevent_idle_sleep": {
"type": "boolean"
},
+2
View File
@@ -417,6 +417,7 @@ impl ModelClient {
effort: Option<ReasoningEffortConfig>,
summary: ReasoningSummaryConfig,
session_telemetry: &SessionTelemetry,
prefix_mode: bool,
) -> Result<Vec<ResponseItem>> {
if prompt.input.is_empty() {
return Ok(Vec::new());
@@ -461,6 +462,7 @@ impl ModelClient {
parallel_tool_calls: prompt.parallel_tool_calls,
reasoning,
text,
prefix_mode,
};
let mut extra_headers = ApiHeaderMap::new();
+207
View File
@@ -21,6 +21,7 @@ use crate::compact::InitialContextInjection;
use crate::compact::run_inline_auto_compact_task;
use crate::compact::should_use_remote_compact_task;
use crate::compact_remote::run_inline_remote_auto_compact_task;
use crate::compact_remote::run_remote_prefix_compact_task;
use crate::config::ManagedFeatures;
use crate::connectors;
use crate::exec_policy::ExecPolicyManager;
@@ -297,6 +298,8 @@ use crate::skills_watcher::SkillsWatcher;
use crate::skills_watcher::SkillsWatcherEvent;
use crate::state::ActiveTurn;
use crate::state::MailboxDeliveryPhase;
use crate::state::PrefixCompactCandidate;
use crate::state::PrefixCompactStart;
use crate::state::SessionServices;
use crate::state::SessionState;
use crate::tasks::GhostSnapshotTask;
@@ -332,6 +335,7 @@ use codex_protocol::config_types::Personality;
use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig;
use codex_protocol::config_types::ServiceTier;
use codex_protocol::config_types::WindowsSandboxLevel;
use codex_protocol::items::ContextCompactionItem;
use codex_protocol::models::ContentItem;
use codex_protocol::models::DeveloperInstructions;
use codex_protocol::models::MessagePhase;
@@ -3887,6 +3891,37 @@ impl Session {
state.clone_history()
}
pub(crate) async fn begin_prefix_compact(
&self,
model_slug: String,
) -> Option<PrefixCompactStart> {
let mut state = self.state.lock().await;
state.begin_prefix_compact(model_slug)
}
pub(crate) async fn finish_prefix_compact(&self, candidate: PrefixCompactCandidate) {
let mut state = self.state.lock().await;
state.finish_prefix_compact(candidate);
}
pub(crate) async fn fail_prefix_compact(&self, generation: u64) {
let mut state = self.state.lock().await;
state.fail_prefix_compact(generation);
}
pub(crate) async fn abandon_prefix_compact(&self) {
let mut state = self.state.lock().await;
state.abandon_prefix_compact();
}
pub(crate) async fn take_ready_prefix_compact(
&self,
model_slug: &str,
) -> Option<PrefixCompactCandidate> {
let mut state = self.state.lock().await;
state.take_ready_prefix_compact(model_slug)
}
pub(crate) async fn reference_context_item(&self) -> Option<TurnContextItem> {
let state = self.state.lock().await;
state.reference_context_item()
@@ -5979,6 +6014,13 @@ pub(crate) async fn run_turn(
if pre_sampling_compacted && let Some(mut client_session) = prewarmed_client_session.take() {
client_session.reset_websocket_session();
}
maybe_start_prefix_compact(
&sess,
&turn_context,
sess.get_total_token_usage().await,
auto_compact_limit,
)
.await;
let skills_outcome = Some(turn_context.turn_skills.outcome.as_ref());
@@ -6309,6 +6351,14 @@ pub(crate) async fn run_turn(
continue;
}
maybe_start_prefix_compact(
&sess,
&turn_context,
total_usage_tokens,
auto_compact_limit,
)
.await;
if !needs_follow_up {
last_agent_message = sampling_request_last_agent_message;
let stop_hook_permission_mode = match turn_context.approval_policy.value() {
@@ -6537,6 +6587,16 @@ async fn run_auto_compact(
turn_context: &Arc<TurnContext>,
initial_context_injection: InitialContextInjection,
) -> CodexResult<()> {
if let Some(candidate) = sess
.take_ready_prefix_compact(&turn_context.model_info.slug)
.await
&& apply_prefix_compact_candidate(sess, turn_context, initial_context_injection, candidate)
.await?
{
return Ok(());
}
sess.abandon_prefix_compact().await;
if should_use_remote_compact_task(&turn_context.provider) {
run_inline_remote_auto_compact_task(
Arc::clone(sess),
@@ -6555,6 +6615,153 @@ async fn run_auto_compact(
Ok(())
}
async fn apply_prefix_compact_candidate(
sess: &Arc<Session>,
turn_context: &Arc<TurnContext>,
initial_context_injection: InitialContextInjection,
candidate: PrefixCompactCandidate,
) -> CodexResult<bool> {
let current_history = sess.clone_history().await;
let current_items = current_history.raw_items();
if current_items.len() < candidate.base_history.len()
|| current_items[..candidate.base_history.len()] != candidate.base_history
{
return Ok(false);
}
let compaction_item = TurnItem::ContextCompaction(ContextCompactionItem::new_prefix());
sess.emit_turn_item_started(turn_context, &compaction_item)
.await;
let mut new_history = candidate.replacement_prefix;
new_history.extend(
current_items[candidate.base_history.len()..]
.iter()
.filter(|item| !matches!(item, ResponseItem::GhostSnapshot { .. }))
.cloned(),
);
if matches!(
initial_context_injection,
InitialContextInjection::BeforeLastUserMessage
) {
let initial_context = sess.build_initial_context(turn_context.as_ref()).await;
new_history = compact::insert_initial_context_before_last_real_user_or_summary(
new_history,
initial_context,
);
}
new_history.extend(
current_items
.iter()
.filter(|item| matches!(item, ResponseItem::GhostSnapshot { .. }))
.cloned(),
);
let reference_context_item = match initial_context_injection {
InitialContextInjection::DoNotInject => None,
InitialContextInjection::BeforeLastUserMessage => Some(turn_context.to_turn_context_item()),
};
let compacted_item = CompactedItem {
message: String::new(),
replacement_history: Some(new_history.clone()),
};
sess.replace_compacted_history(new_history, reference_context_item, compacted_item)
.await;
sess.recompute_token_usage(turn_context).await;
sess.emit_turn_item_completed(turn_context, compaction_item)
.await;
Ok(true)
}
fn prefix_compact_token_limit(
auto_compact_limit: i64,
configured_threshold_percent: Option<i64>,
) -> Option<i64> {
if auto_compact_limit == i64::MAX || auto_compact_limit <= 1 {
return None;
}
let threshold_percent = configured_threshold_percent.unwrap_or(60);
if threshold_percent <= 0 {
return None;
}
let token_limit = ((auto_compact_limit as i128) * (threshold_percent as i128)) / 100;
let token_limit = i64::try_from(token_limit).unwrap_or(i64::MAX);
let token_limit = token_limit.clamp(1, auto_compact_limit.saturating_sub(1));
Some(token_limit)
}
async fn maybe_start_prefix_compact(
sess: &Arc<Session>,
turn_context: &Arc<TurnContext>,
total_usage_tokens: i64,
auto_compact_limit: i64,
) {
if !turn_context.features.enabled(Feature::PrefixCompaction) {
return;
}
if !should_use_remote_compact_task(&turn_context.provider) {
return;
}
let Some(prefix_compact_limit) = prefix_compact_token_limit(
auto_compact_limit,
turn_context.config.prefix_compaction.threshold_percent,
) else {
return;
};
if total_usage_tokens < prefix_compact_limit || total_usage_tokens >= auto_compact_limit {
return;
}
let Some(start) = sess
.begin_prefix_compact(turn_context.model_info.slug.clone())
.await
else {
return;
};
spawn_prefix_compact_task(Arc::clone(sess), Arc::clone(turn_context), start);
}
fn spawn_prefix_compact_task(
sess: Arc<Session>,
turn_context: Arc<TurnContext>,
start: PrefixCompactStart,
) {
tokio::spawn(async move {
let PrefixCompactStart {
generation,
model_slug,
base_history,
} = start;
let compacted = run_remote_prefix_compact_task(
Arc::clone(&sess),
Arc::clone(&turn_context),
base_history.clone(),
)
.await;
match compacted {
Ok(replacement_prefix) => {
sess.finish_prefix_compact(PrefixCompactCandidate {
generation,
model_slug,
base_history,
replacement_prefix,
})
.await;
}
Err(err) => {
tracing::debug!(turn_id = %turn_context.sub_id, "prefix compaction failed: {err:#}");
sess.fail_prefix_compact(generation).await;
}
}
});
}
fn collect_explicit_app_ids_from_skill_items(
skill_items: &[ResponseItem],
connectors: &[connectors::AppInfo],
+57
View File
@@ -5713,3 +5713,60 @@ async fn unified_exec_rejects_escalated_permissions_when_policy_not_on_request()
pretty_assertions::assert_eq!(output, expected);
}
#[test]
fn prefix_compact_token_limit_defaults_to_sixty_percent_of_auto_compact() {
assert_eq!(
prefix_compact_token_limit(
/*auto_compact_limit*/ 1_000, /*configured_threshold_percent*/ None
),
Some(600)
);
}
#[test]
fn prefix_compact_token_limit_uses_configured_threshold_percent() {
assert_eq!(
prefix_compact_token_limit(
/*auto_compact_limit*/ 1_000,
/*configured_threshold_percent*/ Some(75)
),
Some(750)
);
}
#[test]
fn prefix_compact_token_limit_stays_below_auto_compact() {
assert_eq!(
prefix_compact_token_limit(
/*auto_compact_limit*/ 1_000,
/*configured_threshold_percent*/ Some(100)
),
Some(999)
);
assert_eq!(
prefix_compact_token_limit(
/*auto_compact_limit*/ 1_000,
/*configured_threshold_percent*/ Some(125)
),
Some(999)
);
}
#[test]
fn prefix_compact_token_limit_ignores_non_positive_threshold_percent() {
assert_eq!(
prefix_compact_token_limit(
/*auto_compact_limit*/ 1_000,
/*configured_threshold_percent*/ Some(0)
),
None
);
assert_eq!(
prefix_compact_token_limit(
/*auto_compact_limit*/ 1_000,
/*configured_threshold_percent*/ Some(-1)
),
None
);
}
+12
View File
@@ -49,6 +49,18 @@ pub(crate) enum InitialContextInjection {
pub(crate) fn should_use_remote_compact_task(provider: &ModelProviderInfo) -> bool {
provider.is_openai()
|| provider
.base_url
.as_deref()
.is_some_and(is_codex_backend_base_url)
}
fn is_codex_backend_base_url(base_url: &str) -> bool {
let Ok(url) = url::Url::parse(base_url) else {
return false;
};
let path = url.path().trim_end_matches('/');
path.ends_with("/api/codex") || path.ends_with("/backend-api/codex")
}
pub(crate) async fn run_inline_auto_compact_task(
+75
View File
@@ -49,6 +49,80 @@ pub(crate) async fn run_remote_compact_task(
run_remote_compact_task_inner(&sess, &turn_context, InitialContextInjection::DoNotInject).await
}
pub(crate) async fn run_remote_prefix_compact_task(
sess: Arc<Session>,
turn_context: Arc<TurnContext>,
base_history: Vec<ResponseItem>,
) -> CodexResult<Vec<ResponseItem>> {
let mut history = ContextManager::new();
history.replace(base_history);
let base_instructions = sess.get_base_instructions().await;
let deleted_items = trim_function_call_history_to_fit_context_window(
&mut history,
turn_context.as_ref(),
&base_instructions,
);
if deleted_items > 0 {
info!(
turn_id = %turn_context.sub_id,
deleted_items,
"trimmed history items before prefix compaction"
);
}
let prompt_input = history.for_prompt(&turn_context.model_info.input_modalities);
let tool_router = built_tools(
sess.as_ref(),
turn_context.as_ref(),
&prompt_input,
&HashSet::new(),
/*skills_outcome*/ None,
&CancellationToken::new(),
)
.await?;
let prompt = Prompt {
input: prompt_input,
tools: tool_router.model_visible_specs(),
parallel_tool_calls: turn_context.model_info.supports_parallel_tool_calls,
base_instructions,
personality: turn_context.personality,
output_schema: None,
};
let new_history = sess
.services
.model_client
.compact_conversation_history(
&prompt,
&turn_context.model_info,
turn_context.reasoning_effort,
turn_context.reasoning_summary,
&turn_context.session_telemetry,
/*prefix_mode*/ true,
)
.or_else(|err| async {
let total_usage_breakdown = sess.get_total_token_usage_breakdown().await;
let compact_request_log_data =
build_compact_request_log_data(&prompt.input, &prompt.base_instructions.text);
log_remote_compact_failure(
turn_context.as_ref(),
&compact_request_log_data,
total_usage_breakdown,
&err,
);
Err(err)
})
.await?;
Ok(process_compacted_history(
sess.as_ref(),
turn_context.as_ref(),
new_history,
InitialContextInjection::DoNotInject,
)
.await)
}
async fn run_remote_compact_task_inner(
sess: &Arc<Session>,
turn_context: &Arc<TurnContext>,
@@ -124,6 +198,7 @@ async fn run_remote_compact_task_inner_impl(
turn_context.reasoning_effort,
turn_context.reasoning_summary,
&turn_context.session_telemetry,
/*prefix_mode*/ false,
)
.or_else(|err| async {
let total_usage_breakdown = sess.get_total_token_usage_breakdown().await;
+26
View File
@@ -1,4 +1,7 @@
use super::*;
use codex_model_provider_info::ModelProviderInfo;
use codex_model_provider_info::WireApi;
use codex_model_provider_info::create_oss_provider_with_base_url;
use pretty_assertions::assert_eq;
async fn process_compacted_history_with_test_session(
@@ -20,6 +23,29 @@ async fn process_compacted_history_with_test_session(
(refreshed, initial_context)
}
#[test]
fn remote_compact_task_supports_openai_provider() {
let provider = ModelProviderInfo::create_openai_provider(/*base_url*/ None);
assert!(should_use_remote_compact_task(&provider));
}
#[test]
fn remote_compact_task_supports_local_codex_backend_provider() {
let provider =
create_oss_provider_with_base_url("http://localhost:8061/api/codex", WireApi::Responses);
assert!(should_use_remote_compact_task(&provider));
}
#[test]
fn remote_compact_task_ignores_generic_openai_compatible_provider() {
let provider =
create_oss_provider_with_base_url("http://localhost:8082/v1", WireApi::Responses);
assert!(!should_use_remote_compact_task(&provider));
}
#[test]
fn content_items_to_text_joins_non_empty_segments() {
let items = vec![
+75
View File
@@ -4574,6 +4574,7 @@ fn test_precedence_fixture_with_o3_profile() -> std::io::Result<()> {
background_terminal_max_timeout: DEFAULT_MAX_BACKGROUND_TERMINAL_TIMEOUT_MS,
ghost_snapshot: GhostSnapshotConfig::default(),
multi_agent_v2: MultiAgentV2Config::default(),
prefix_compaction: PrefixCompactionConfig::default(),
features: Features::with_defaults().into(),
suppress_unstable_features_warning: false,
active_profile: Some("o3".to_string()),
@@ -4720,6 +4721,7 @@ fn test_precedence_fixture_with_gpt3_profile() -> std::io::Result<()> {
background_terminal_max_timeout: DEFAULT_MAX_BACKGROUND_TERMINAL_TIMEOUT_MS,
ghost_snapshot: GhostSnapshotConfig::default(),
multi_agent_v2: MultiAgentV2Config::default(),
prefix_compaction: PrefixCompactionConfig::default(),
features: Features::with_defaults().into(),
suppress_unstable_features_warning: false,
active_profile: Some("gpt3".to_string()),
@@ -4864,6 +4866,7 @@ fn test_precedence_fixture_with_zdr_profile() -> std::io::Result<()> {
background_terminal_max_timeout: DEFAULT_MAX_BACKGROUND_TERMINAL_TIMEOUT_MS,
ghost_snapshot: GhostSnapshotConfig::default(),
multi_agent_v2: MultiAgentV2Config::default(),
prefix_compaction: PrefixCompactionConfig::default(),
features: Features::with_defaults().into(),
suppress_unstable_features_warning: false,
active_profile: Some("zdr".to_string()),
@@ -4994,6 +4997,7 @@ fn test_precedence_fixture_with_gpt5_profile() -> std::io::Result<()> {
background_terminal_max_timeout: DEFAULT_MAX_BACKGROUND_TERMINAL_TIMEOUT_MS,
ghost_snapshot: GhostSnapshotConfig::default(),
multi_agent_v2: MultiAgentV2Config::default(),
prefix_compaction: PrefixCompactionConfig::default(),
features: Features::with_defaults().into(),
suppress_unstable_features_warning: false,
active_profile: Some("gpt5".to_string()),
@@ -6217,6 +6221,77 @@ hide_spawn_agent_metadata = false
Ok(())
}
#[tokio::test]
async fn prefix_compaction_bool_feature_enables_with_default_threshold() -> std::io::Result<()> {
let codex_home = TempDir::new()?;
std::fs::write(
codex_home.path().join(CONFIG_TOML_FILE),
r#"[features]
prefix_compaction = true
"#,
)?;
let config = ConfigBuilder::without_managed_config_for_tests()
.codex_home(codex_home.path().to_path_buf())
.fallback_cwd(Some(codex_home.path().to_path_buf()))
.build()
.await?;
assert!(config.features.enabled(Feature::PrefixCompaction));
assert_eq!(config.prefix_compaction.threshold_percent, None);
Ok(())
}
#[tokio::test]
async fn prefix_compaction_config_from_feature_table() -> std::io::Result<()> {
let codex_home = TempDir::new()?;
std::fs::write(
codex_home.path().join(CONFIG_TOML_FILE),
r#"[features.prefix_compaction]
enabled = true
threshold_percent = 75
"#,
)?;
let config = ConfigBuilder::without_managed_config_for_tests()
.codex_home(codex_home.path().to_path_buf())
.fallback_cwd(Some(codex_home.path().to_path_buf()))
.build()
.await?;
assert!(config.features.enabled(Feature::PrefixCompaction));
assert_eq!(config.prefix_compaction.threshold_percent, Some(75));
Ok(())
}
#[tokio::test]
async fn profile_prefix_compaction_config_overrides_base() -> std::io::Result<()> {
let codex_home = TempDir::new()?;
std::fs::write(
codex_home.path().join(CONFIG_TOML_FILE),
r#"profile = "custom_prefix"
[features.prefix_compaction]
threshold_percent = 75
[profiles.custom_prefix.features.prefix_compaction]
threshold_percent = 80
"#,
)?;
let config = ConfigBuilder::without_managed_config_for_tests()
.codex_home(codex_home.path().to_path_buf())
.fallback_cwd(Some(codex_home.path().to_path_buf()))
.build()
.await?;
assert_eq!(config.prefix_compaction.threshold_percent, Some(80));
Ok(())
}
#[tokio::test]
async fn feature_requirements_normalize_runtime_feature_mutations() -> std::io::Result<()> {
let codex_home = TempDir::new()?;
+34
View File
@@ -55,6 +55,7 @@ use codex_features::FeatureToml;
use codex_features::Features;
use codex_features::FeaturesToml;
use codex_features::MultiAgentV2ConfigToml;
use codex_features::PrefixCompactionConfigToml;
use codex_login::AuthManagerConfig;
use codex_mcp::McpConfig;
use codex_model_provider_info::LEGACY_OLLAMA_CHAT_PROVIDER_ID;
@@ -527,6 +528,9 @@ pub struct Config {
/// Settings specific to the task-path-based multi-agent tool surface.
pub multi_agent_v2: MultiAgentV2Config,
/// Settings specific to background prefix compaction.
pub prefix_compaction: PrefixCompactionConfig,
/// Centralized feature flags; source of truth for feature gating.
pub features: ManagedFeatures,
@@ -588,6 +592,11 @@ impl Default for MultiAgentV2Config {
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PrefixCompactionConfig {
pub threshold_percent: Option<i64>,
}
impl AuthManagerConfig for Config {
fn codex_home(&self) -> PathBuf {
self.codex_home.clone()
@@ -1339,6 +1348,29 @@ fn multi_agent_v2_toml_config(features: Option<&FeaturesToml>) -> Option<&MultiA
}
}
fn resolve_prefix_compaction_config(
config_toml: &ConfigToml,
config_profile: &ConfigProfile,
) -> PrefixCompactionConfig {
let base = prefix_compaction_toml_config(config_toml.features.as_ref());
let profile = prefix_compaction_toml_config(config_profile.features.as_ref());
PrefixCompactionConfig {
threshold_percent: profile
.and_then(|config| config.threshold_percent)
.or_else(|| base.and_then(|config| config.threshold_percent)),
}
}
fn prefix_compaction_toml_config(
features: Option<&FeaturesToml>,
) -> Option<&PrefixCompactionConfigToml> {
match features?.prefix_compaction.as_ref()? {
FeatureToml::Enabled(_) => None,
FeatureToml::Config(config) => Some(config),
}
}
pub(crate) fn resolve_web_search_mode_for_turn(
web_search_mode: &Constrained<WebSearchMode>,
sandbox_policy: &SandboxPolicy,
@@ -1671,6 +1703,7 @@ impl Config {
.unwrap_or(WebSearchMode::Cached);
let web_search_config = resolve_web_search_config(&cfg, &config_profile);
let multi_agent_v2 = resolve_multi_agent_v2_config(&cfg, &config_profile);
let prefix_compaction = resolve_prefix_compaction_config(&cfg, &config_profile);
let agent_roles =
agent_roles::load_agent_roles(&cfg, &config_layer_stack, &mut startup_warnings)?;
@@ -2110,6 +2143,7 @@ impl Config {
background_terminal_max_timeout,
ghost_snapshot,
multi_agent_v2,
prefix_compaction,
features,
suppress_unstable_features_warning: cfg
.suppress_unstable_features_warning
+2
View File
@@ -3,6 +3,8 @@ mod session;
mod turn;
pub(crate) use service::SessionServices;
pub(crate) use session::PrefixCompactCandidate;
pub(crate) use session::PrefixCompactStart;
pub(crate) use session::SessionState;
pub(crate) use turn::ActiveTurn;
pub(crate) use turn::MailboxDeliveryPhase;
+95
View File
@@ -33,6 +33,8 @@ pub(crate) struct SessionState {
pub(crate) active_connector_selection: HashSet<String>,
pub(crate) pending_session_start_source: Option<codex_hooks::SessionStartSource>,
granted_permissions: Option<PermissionProfile>,
next_prefix_compact_generation: u64,
prefix_compact: PrefixCompactState,
}
impl SessionState {
@@ -51,6 +53,8 @@ impl SessionState {
active_connector_selection: HashSet::new(),
pending_session_start_source: None,
granted_permissions: None,
next_prefix_compact_generation: 0,
prefix_compact: PrefixCompactState::Idle,
}
}
@@ -85,6 +89,72 @@ impl SessionState {
self.history.replace(items);
self.history
.set_reference_context_item(reference_context_item);
self.prefix_compact = PrefixCompactState::Idle;
}
pub(crate) fn begin_prefix_compact(
&mut self,
model_slug: String,
) -> Option<PrefixCompactStart> {
if !matches!(self.prefix_compact, PrefixCompactState::Idle) {
return None;
}
let base_history = self.history.raw_items().to_vec();
if base_history.is_empty() {
return None;
}
let generation = self.next_prefix_compact_generation;
self.next_prefix_compact_generation = self.next_prefix_compact_generation.saturating_add(1);
self.prefix_compact = PrefixCompactState::Running { generation };
Some(PrefixCompactStart {
generation,
model_slug,
base_history,
})
}
pub(crate) fn finish_prefix_compact(&mut self, candidate: PrefixCompactCandidate) {
if matches!(
self.prefix_compact,
PrefixCompactState::Running { generation } if generation == candidate.generation
) {
self.prefix_compact = PrefixCompactState::Ready(candidate);
}
}
pub(crate) fn fail_prefix_compact(&mut self, generation: u64) {
if matches!(
self.prefix_compact,
PrefixCompactState::Running { generation: running } if running == generation
) {
self.prefix_compact = PrefixCompactState::Idle;
}
}
pub(crate) fn abandon_prefix_compact(&mut self) {
self.prefix_compact = PrefixCompactState::Idle;
}
pub(crate) fn take_ready_prefix_compact(
&mut self,
model_slug: &str,
) -> Option<PrefixCompactCandidate> {
let PrefixCompactState::Ready(candidate) = std::mem::take(&mut self.prefix_compact) else {
return None;
};
let current_history = self.history.raw_items();
let prefix_is_current = candidate.model_slug == model_slug
&& current_history.len() >= candidate.base_history.len()
&& current_history[..candidate.base_history.len()] == candidate.base_history;
if prefix_is_current {
Some(candidate)
} else {
None
}
}
pub(crate) fn set_token_info(&mut self, info: Option<TokenUsageInfo>) {
@@ -216,6 +286,31 @@ impl SessionState {
}
}
#[derive(Debug, Default)]
enum PrefixCompactState {
#[default]
Idle,
Running {
generation: u64,
},
Ready(PrefixCompactCandidate),
}
#[derive(Debug, Clone)]
pub(crate) struct PrefixCompactStart {
pub(crate) generation: u64,
pub(crate) model_slug: String,
pub(crate) base_history: Vec<ResponseItem>,
}
#[derive(Debug, Clone)]
pub(crate) struct PrefixCompactCandidate {
pub(crate) generation: u64,
pub(crate) model_slug: String,
pub(crate) base_history: Vec<ResponseItem>,
pub(crate) replacement_prefix: Vec<ResponseItem>,
}
// Sometimes new snapshots don't include credits or plan information.
// Preserve those from the previous snapshot when missing. For `limit_id`, treat
// missing values as the default `"codex"` bucket.
+489
View File
@@ -2,9 +2,13 @@
use std::fs;
use std::path::PathBuf;
use std::sync::Arc as StdArc;
use std::sync::Mutex as StdMutex;
use std::time::Duration;
use anyhow::Result;
use codex_core::compact::SUMMARY_PREFIX;
use codex_features::Feature;
use codex_login::CodexAuth;
use codex_protocol::items::TurnItem;
use codex_protocol::models::ContentItem;
@@ -35,7 +39,15 @@ use core_test_support::wait_for_event;
use core_test_support::wait_for_event_match;
use pretty_assertions::assert_eq;
use serde_json::json;
use wiremock::Mock;
use wiremock::Respond;
use wiremock::ResponseTemplate;
use wiremock::matchers::method;
use wiremock::matchers::path_regex;
fn enable_prefix_compaction(config: &mut codex_core::config::Config) {
let _ = config.features.enable(Feature::PrefixCompaction);
}
fn approx_token_count(text: &str) -> i64 {
i64::try_from(text.len().saturating_add(3) / 4).unwrap_or(i64::MAX)
@@ -82,6 +94,70 @@ fn compacted_summary_only_output(summary: &str) -> Vec<ResponseItem> {
}]
}
async fn wait_for_response_requests(mock_response: &responses::ResponseMock, count: usize) {
for _ in 0..100 {
if mock_response.requests().len() >= count {
return;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
panic!(
"expected at least {count} request(s), got {}",
mock_response.requests().len()
);
}
async fn wait_for_json_request_count(
requests: &StdArc<StdMutex<Vec<serde_json::Value>>>,
count: usize,
) {
for _ in 0..100 {
if requests.lock().expect("request lock poisoned").len() >= count {
return;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
panic!(
"expected at least {count} request(s), got {}",
requests.lock().expect("request lock poisoned").len()
);
}
#[derive(Clone, Debug)]
struct PrefixRaceCompactResponder {
requests: StdArc<StdMutex<Vec<serde_json::Value>>>,
}
impl Respond for PrefixRaceCompactResponder {
fn respond(&self, request: &wiremock::Request) -> ResponseTemplate {
let body = request
.body_json::<serde_json::Value>()
.expect("compact request body should be JSON");
let prefix_mode = body
.get("prefix_mode")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false);
self.requests
.lock()
.expect("request lock poisoned")
.push(body);
let summary = if prefix_mode {
"PREFIX_STALE_SUMMARY"
} else {
"NORMAL_FALLBACK_SUMMARY"
};
let response = ResponseTemplate::new(200)
.insert_header("content-type", "application/json")
.set_body_json(json!({ "output": compacted_summary_only_output(summary) }));
if prefix_mode {
response.set_delay(Duration::from_secs(5))
} else {
response
}
}
}
fn remote_realtime_test_codex_builder(
realtime_server: &responses::WebSocketTestServer,
) -> TestCodexBuilder {
@@ -1050,6 +1126,419 @@ async fn remote_manual_compact_emits_context_compaction_items() -> Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn prefix_compact_feature_disabled_by_default_does_not_start_background_compact() -> Result<()>
{
skip_if_no_network!(Ok(()));
let harness = TestCodexHarness::with_builder(
test_codex()
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
.with_config(|config| {
config.model_context_window = Some(1_000);
config.model_auto_compact_token_limit = Some(800);
}),
)
.await?;
let codex = harness.test().codex.clone();
responses::mount_sse_sequence(
harness.server(),
vec![sse(vec![
responses::ev_assistant_message("m1", "FIRST_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-1", /*total_tokens*/ 650),
])],
)
.await;
let compact_requests = StdArc::new(StdMutex::new(Vec::new()));
Mock::given(method("POST"))
.and(path_regex(".*/responses/compact$"))
.respond_with(PrefixRaceCompactResponder {
requests: StdArc::clone(&compact_requests),
})
.mount(harness.server())
.await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "FIRST_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
assert_eq!(
compact_requests
.lock()
.expect("request lock poisoned")
.len(),
0
);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn prefix_compact_uses_configured_threshold_percent() -> Result<()> {
skip_if_no_network!(Ok(()));
let harness = TestCodexHarness::with_builder(
test_codex()
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
.with_config(|config| {
config.model_context_window = Some(1_000);
config.model_auto_compact_token_limit = Some(800);
config.prefix_compaction.threshold_percent = Some(90);
enable_prefix_compaction(config);
}),
)
.await?;
let codex = harness.test().codex.clone();
responses::mount_sse_sequence(
harness.server(),
vec![
sse(vec![
responses::ev_assistant_message("m1", "FIRST_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-1", /*total_tokens*/ 650),
]),
sse(vec![
responses::ev_assistant_message("m2", "SECOND_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-2", /*total_tokens*/ 750),
]),
],
)
.await;
let compact_requests = StdArc::new(StdMutex::new(Vec::new()));
Mock::given(method("POST"))
.and(path_regex(".*/responses/compact$"))
.respond_with(PrefixRaceCompactResponder {
requests: StdArc::clone(&compact_requests),
})
.mount(harness.server())
.await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "FIRST_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
assert_eq!(
compact_requests
.lock()
.expect("request lock poisoned")
.len(),
0
);
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "SECOND_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
wait_for_json_request_count(&compact_requests, /*count*/ 1).await;
let compact_requests = compact_requests
.lock()
.expect("request lock poisoned")
.clone();
assert_eq!(compact_requests.len(), 1);
assert_eq!(compact_requests[0]["prefix_mode"], json!(true));
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ready_prefix_compact_is_applied_by_pre_turn_auto_compact() -> Result<()> {
skip_if_no_network!(Ok(()));
let harness = TestCodexHarness::with_builder(
test_codex()
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
.with_config(|config| {
config.model_context_window = Some(1_000);
config.model_auto_compact_token_limit = Some(800);
enable_prefix_compaction(config);
}),
)
.await?;
let codex = harness.test().codex.clone();
let responses_mock = responses::mount_sse_sequence(
harness.server(),
vec![
sse(vec![
responses::ev_assistant_message("m1", "FIRST_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-1", /*total_tokens*/ 650),
]),
sse(vec![
responses::ev_assistant_message("m2", "SECOND_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-2", /*total_tokens*/ 850),
]),
sse(vec![
responses::ev_assistant_message("m3", "THIRD_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-3", /*total_tokens*/ 200),
]),
],
)
.await;
let compact_mock = responses::mount_compact_json_once(
harness.server(),
json!({ "output": compacted_summary_only_output("PREFIX_READY_SUMMARY") }),
)
.await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "FIRST_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
wait_for_response_requests(&compact_mock, /*count*/ 1).await;
assert_eq!(
compact_mock.single_request().body_json()["prefix_mode"],
json!(true)
);
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "SECOND_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "THIRD_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
assert_eq!(
compact_mock.requests().len(),
1,
"ready prefix compact should replace the foreground compact request"
);
let requests = responses_mock.requests();
assert_eq!(requests.len(), 3);
let third_request_body = requests[2].body_json().to_string();
assert!(third_request_body.contains("PREFIX_READY_SUMMARY"));
assert!(!third_request_body.contains("FIRST_REMOTE_USER"));
assert!(third_request_body.contains("SECOND_REMOTE_USER"));
assert!(third_request_body.contains("THIRD_REMOTE_USER"));
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn running_prefix_compact_is_abandoned_when_auto_compact_fires() -> Result<()> {
skip_if_no_network!(Ok(()));
let harness = TestCodexHarness::with_builder(
test_codex()
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
.with_config(|config| {
config.model_context_window = Some(1_000);
config.model_auto_compact_token_limit = Some(800);
enable_prefix_compaction(config);
}),
)
.await?;
let codex = harness.test().codex.clone();
let responses_mock = responses::mount_sse_sequence(
harness.server(),
vec![
sse(vec![
responses::ev_assistant_message("m1", "FIRST_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-1", /*total_tokens*/ 650),
]),
sse(vec![
responses::ev_assistant_message("m2", "SECOND_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-2", /*total_tokens*/ 850),
]),
sse(vec![
responses::ev_assistant_message("m3", "THIRD_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-3", /*total_tokens*/ 200),
]),
],
)
.await;
let compact_requests = StdArc::new(StdMutex::new(Vec::new()));
Mock::given(method("POST"))
.and(path_regex(".*/responses/compact$"))
.respond_with(PrefixRaceCompactResponder {
requests: StdArc::clone(&compact_requests),
})
.up_to_n_times(2)
.expect(2)
.mount(harness.server())
.await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "FIRST_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
wait_for_json_request_count(&compact_requests, /*count*/ 1).await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "SECOND_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "THIRD_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
let compact_requests = compact_requests
.lock()
.expect("request lock poisoned")
.clone();
assert_eq!(compact_requests.len(), 2);
assert_eq!(compact_requests[0]["prefix_mode"], json!(true));
assert!(compact_requests[1].get("prefix_mode").is_none());
let requests = responses_mock.requests();
assert_eq!(requests.len(), 3);
let third_request_body = requests[2].body_json().to_string();
assert!(third_request_body.contains("NORMAL_FALLBACK_SUMMARY"));
assert!(!third_request_body.contains("PREFIX_STALE_SUMMARY"));
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn running_prefix_compact_started_on_follow_up_boundary_is_abandoned_when_auto_compact_fires()
-> Result<()> {
skip_if_no_network!(Ok(()));
let harness = TestCodexHarness::with_builder(
test_codex()
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
.with_config(|config| {
config.model_context_window = Some(1_000);
config.model_auto_compact_token_limit = Some(800);
enable_prefix_compaction(config);
}),
)
.await?;
let codex = harness.test().codex.clone();
let responses_mock = responses::mount_sse_sequence(
harness.server(),
vec![
sse(vec![
responses::ev_function_call("call-prefix-mid-turn-1", DUMMY_FUNCTION_NAME, "{}"),
responses::ev_completed_with_tokens("resp-1", /*total_tokens*/ 650),
]),
sse(vec![
responses::ev_function_call("call-prefix-mid-turn-2", DUMMY_FUNCTION_NAME, "{}"),
responses::ev_completed_with_tokens("resp-2", /*total_tokens*/ 850),
]),
sse(vec![
responses::ev_assistant_message("m3", "THIRD_REMOTE_REPLY"),
responses::ev_completed_with_tokens("resp-3", /*total_tokens*/ 200),
]),
],
)
.await;
let compact_requests = StdArc::new(StdMutex::new(Vec::new()));
Mock::given(method("POST"))
.and(path_regex(".*/responses/compact$"))
.respond_with(PrefixRaceCompactResponder {
requests: StdArc::clone(&compact_requests),
})
.up_to_n_times(2)
.expect(2)
.mount(harness.server())
.await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "FIRST_REMOTE_USER".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
})
.await?;
wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await;
let compact_requests = compact_requests
.lock()
.expect("request lock poisoned")
.clone();
assert_eq!(compact_requests.len(), 2);
assert_eq!(compact_requests[0]["prefix_mode"], json!(true));
assert!(compact_requests[1].get("prefix_mode").is_none());
let requests = responses_mock.requests();
assert_eq!(requests.len(), 3);
let third_request_body = requests[2].body_json().to_string();
assert!(third_request_body.contains("NORMAL_FALLBACK_SUMMARY"));
assert!(!third_request_body.contains("PREFIX_STALE_SUMMARY"));
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn remote_manual_compact_failure_emits_task_error_event() -> Result<()> {
skip_if_no_network!(Ok(()));
+17
View File
@@ -21,3 +21,20 @@ impl FeatureConfig for MultiAgentV2ConfigToml {
self.enabled
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PrefixCompactionConfigToml {
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
/// Percentage of the auto-compaction threshold that starts background
/// prefix compaction. When omitted, Codex uses 60%.
#[serde(skip_serializing_if = "Option::is_none")]
pub threshold_percent: Option<i64>,
}
impl FeatureConfig for PrefixCompactionConfigToml {
fn enabled(&self) -> Option<bool> {
self.enabled
}
}
+23
View File
@@ -17,6 +17,7 @@ use toml::Table;
mod feature_configs;
mod legacy;
pub use feature_configs::MultiAgentV2ConfigToml;
pub use feature_configs::PrefixCompactionConfigToml;
use legacy::LegacyFeatureToggles;
pub use legacy::legacy_feature_keys;
@@ -186,6 +187,9 @@ pub enum Feature {
ResponsesWebsockets,
/// Legacy rollout flag for Responses API WebSocket transport v2 experiments.
ResponsesWebsocketsV2,
/// Precompute prefix compaction in the background before the foreground
/// auto-compaction threshold is reached.
PrefixCompaction,
}
impl Feature {
@@ -494,6 +498,8 @@ pub fn is_known_feature_key(key: &str) -> bool {
pub struct FeaturesToml {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub multi_agent_v2: Option<FeatureToml<MultiAgentV2ConfigToml>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub prefix_compaction: Option<FeatureToml<PrefixCompactionConfigToml>>,
/// Boolean feature toggles keyed by canonical or legacy feature name.
#[serde(flatten)]
entries: BTreeMap<String, bool>,
@@ -512,6 +518,13 @@ impl FeaturesToml {
if let Some(enabled) = self.multi_agent_v2.as_ref().and_then(FeatureToml::enabled) {
entries.insert(Feature::MultiAgentV2.key().to_string(), enabled);
}
if let Some(enabled) = self
.prefix_compaction
.as_ref()
.and_then(FeatureToml::enabled)
{
entries.insert(Feature::PrefixCompaction.key().to_string(), enabled);
}
entries
}
}
@@ -909,6 +922,16 @@ pub const FEATURES: &[FeatureSpec] = &[
stage: Stage::Removed,
default_enabled: false,
},
FeatureSpec {
id: Feature::PrefixCompaction,
key: "prefix_compaction",
stage: Stage::Experimental {
name: "Prefix compaction",
menu_description: "Precompute history compaction in the background before the normal context compaction threshold is reached.",
announcement: "",
},
default_enabled: false,
},
];
pub fn unstable_features_warning_event(
+90
View File
@@ -97,6 +97,23 @@ fn guardian_approval_is_experimental_and_user_toggleable() {
assert_eq!(Feature::GuardianApproval.default_enabled(), false);
}
#[test]
fn prefix_compaction_is_experimental_and_user_toggleable() {
let spec = Feature::PrefixCompaction.info();
let stage = spec.stage;
assert!(matches!(stage, Stage::Experimental { .. }));
assert_eq!(stage.experimental_menu_name(), Some("Prefix compaction"));
assert_eq!(
stage.experimental_menu_description(),
Some(
"Precompute history compaction in the background before the normal context compaction threshold is reached."
)
);
assert_eq!(stage.experimental_announcement(), None);
assert_eq!(Feature::PrefixCompaction.default_enabled(), false);
}
#[test]
fn request_permissions_is_under_development() {
assert_eq!(
@@ -320,6 +337,79 @@ usage_hint_enabled = false
);
}
#[test]
fn prefix_compaction_feature_config_deserializes_bool() {
let features: FeaturesToml = toml::from_str(
r#"
prefix_compaction = true
"#,
)
.expect("features table should deserialize");
assert_eq!(
features.entries(),
BTreeMap::from([("prefix_compaction".to_string(), true)])
);
assert_eq!(features.prefix_compaction, Some(FeatureToml::Enabled(true)));
}
#[test]
fn prefix_compaction_feature_config_deserializes_table() {
let features: FeaturesToml = toml::from_str(
r#"
[prefix_compaction]
enabled = true
threshold_percent = 75
"#,
)
.expect("features table should deserialize");
assert_eq!(
features.entries(),
BTreeMap::from([("prefix_compaction".to_string(), true)])
);
assert_eq!(
features.prefix_compaction,
Some(crate::FeatureToml::Config(
crate::PrefixCompactionConfigToml {
enabled: Some(true),
threshold_percent: Some(75),
}
))
);
}
#[test]
fn prefix_compaction_threshold_percent_does_not_enable_feature() {
let features_toml: FeaturesToml = toml::from_str(
r#"
[prefix_compaction]
threshold_percent = 75
"#,
)
.expect("features table should deserialize");
let features = Features::from_sources(
FeatureConfigSource {
features: Some(&features_toml),
..Default::default()
},
FeatureConfigSource::default(),
FeatureOverrides::default(),
);
assert_eq!(features.enabled(Feature::PrefixCompaction), false);
assert_eq!(features_toml.entries(), BTreeMap::new());
assert_eq!(
features_toml.prefix_compaction,
Some(crate::FeatureToml::Config(
crate::PrefixCompactionConfigToml {
enabled: None,
threshold_percent: Some(75),
}
))
);
}
#[test]
fn unstable_warning_event_only_mentions_enabled_under_development_features() {
let mut configured_features = Table::new();
+18 -1
View File
@@ -7,6 +7,7 @@ use crate::protocol::AgentMessageEvent;
use crate::protocol::AgentReasoningEvent;
use crate::protocol::AgentReasoningRawContentEvent;
use crate::protocol::ContextCompactedEvent;
use crate::protocol::ContextCompactionKind;
use crate::protocol::EventMsg;
use crate::protocol::ImageGenerationEndEvent;
use crate::protocol::UserMessageEvent;
@@ -129,17 +130,33 @@ pub struct ImageGenerationItem {
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema)]
pub struct ContextCompactionItem {
pub id: String,
#[serde(
default = "ContextCompactionKind::default_classic_option",
skip_serializing_if = "ContextCompactionKind::is_classic_option"
)]
#[ts(optional)]
pub kind: Option<ContextCompactionKind>,
}
impl ContextCompactionItem {
pub fn new() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
kind: Some(ContextCompactionKind::Classic),
}
}
pub fn new_prefix() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
kind: Some(ContextCompactionKind::Prefix),
}
}
pub fn as_legacy_event(&self) -> EventMsg {
EventMsg::ContextCompacted(ContextCompactedEvent {})
EventMsg::ContextCompacted(ContextCompactedEvent {
kind: self.kind.clone(),
})
}
}
+51 -1
View File
@@ -1998,8 +1998,38 @@ pub struct ModelRerouteEvent {
pub reason: ModelRerouteReason,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "snake_case")]
#[ts(rename_all = "snake_case")]
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)
}
}
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, TS)]
pub struct ContextCompactedEvent;
pub struct ContextCompactedEvent {
#[serde(
default = "ContextCompactionKind::default_classic_option",
skip_serializing_if = "ContextCompactionKind::is_classic_option"
)]
#[ts(optional)]
pub kind: Option<ContextCompactionKind>,
}
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, TS)]
pub struct TurnCompleteEvent {
@@ -3802,6 +3832,26 @@ mod tests {
.any(|root| root.is_path_writable(path))
}
#[test]
fn context_compacted_event_defaults_to_classic_kind() {
let event: ContextCompactedEvent =
serde_json::from_value(json!({})).expect("event should deserialize");
assert_eq!(event.kind, Some(ContextCompactionKind::Classic));
let serialized = serde_json::to_value(&event).expect("event should serialize");
assert_eq!(serialized, json!({}));
}
#[test]
fn context_compacted_event_serializes_prefix_kind() {
let event = ContextCompactedEvent {
kind: Some(ContextCompactionKind::Prefix),
};
let serialized = serde_json::to_value(&event).expect("event should serialize");
assert_eq!(serialized, json!({ "kind": "prefix" }));
}
#[test]
fn session_source_from_startup_arg_maps_known_values() {
assert_eq!(
+10 -1
View File
@@ -868,9 +868,17 @@ fn thread_item_to_core(item: &ThreadItem) -> Option<TurnItem> {
result: result.clone(),
saved_path: saved_path.clone(),
})),
ThreadItem::ContextCompaction { id } => {
ThreadItem::ContextCompaction { id, kind } => {
Some(TurnItem::ContextCompaction(ContextCompactionItem {
id: id.clone(),
kind: kind.clone().map(|kind| match kind {
codex_app_server_protocol::ContextCompactionKind::Classic => {
codex_protocol::protocol::ContextCompactionKind::Classic
}
codex_app_server_protocol::ContextCompactionKind::Prefix => {
codex_protocol::protocol::ContextCompactionKind::Prefix
}
}),
}))
}
ThreadItem::CommandExecution { .. }
@@ -1572,6 +1580,7 @@ mod tests {
},
ThreadItem::ContextCompaction {
id: "compact-1".to_string(),
kind: Some(codex_app_server_protocol::ContextCompactionKind::Classic),
},
],
status: TurnStatus::Completed,
+25 -11
View File
@@ -2177,15 +2177,16 @@ impl ChatWidget {
self.request_redraw();
}
fn on_agent_message(&mut self, message: String) {
self.finalize_completed_assistant_message(Some(&message));
}
fn on_context_compacted(&mut self) {
fn on_context_compacted(&mut self, prefix_compacted: bool) {
self.flush_answer_stream_with_separator();
self.handle_stream_finished();
let message = if prefix_compacted {
"Context prefix compacted"
} else {
"Context compacted"
};
self.add_to_history(history_cell::new_info_event(
"Context compacted".to_owned(),
message.to_owned(),
/*hint*/ None,
));
self.request_redraw();
@@ -6285,8 +6286,13 @@ impl ChatWidget {
ThreadItem::ExitedReviewMode { .. } => {
self.exit_review_mode_after_item();
}
ThreadItem::ContextCompaction { .. } => {
self.on_agent_message("Context compacted".to_owned());
ThreadItem::ContextCompaction { kind, .. } => {
if from_replay {
self.on_context_compacted(matches!(
kind,
Some(codex_app_server_protocol::ContextCompactionKind::Prefix)
));
}
}
ThreadItem::HookPrompt { .. } => {}
ThreadItem::CollabAgentToolCall {
@@ -6607,7 +6613,12 @@ impl ChatWidget {
| ServerNotification::WindowsWorldWritableWarning(_)
| ServerNotification::WindowsSandboxSetupCompleted(_)
| ServerNotification::AccountLoginCompleted(_) => {}
ServerNotification::ContextCompacted(_) => self.on_context_compacted(),
ServerNotification::ContextCompacted(notification) => {
self.on_context_compacted(matches!(
notification.kind,
Some(codex_app_server_protocol::ContextCompactionKind::Prefix)
))
}
}
}
@@ -6935,7 +6946,7 @@ impl ChatWidget {
// TODO(ccunningham): stop relying on legacy AgentMessage in review mode,
// including thread-snapshot replay, and forward
// ItemCompleted(TurnItem::AgentMessage(_)) instead.
self.on_agent_message(message)
self.finalize_completed_assistant_message(Some(&message))
}
EventMsg::AgentMessage(AgentMessageEvent { message, .. }) => {
if !message.is_empty() {
@@ -7079,7 +7090,10 @@ impl ChatWidget {
self.on_entered_review_mode(review_request, from_replay)
}
EventMsg::ExitedReviewMode(review) => self.on_exited_review_mode(review),
EventMsg::ContextCompacted(_) => self.on_context_compacted(),
EventMsg::ContextCompacted(event) => self.on_context_compacted(matches!(
event.kind,
Some(codex_protocol::protocol::ContextCompactionKind::Prefix)
)),
EventMsg::CollabAgentSpawnBegin(CollabAgentSpawnBeginEvent {
call_id,
model,
@@ -795,6 +795,70 @@ async fn replayed_in_progress_turn_marks_task_running() {
assert_eq!(status.header(), "Working");
}
#[tokio::test]
async fn replayed_context_compaction_renders_history_marker() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.replay_thread_turns(
vec![AppServerTurn {
id: "turn-1".to_string(),
items: vec![AppServerThreadItem::ContextCompaction {
id: "compact-1".to_string(),
kind: Some(codex_app_server_protocol::ContextCompactionKind::Classic),
}],
status: AppServerTurnStatus::Completed,
error: None,
started_at: None,
completed_at: None,
duration_ms: None,
}],
ReplayKind::ResumeInitialMessages,
);
let cells = drain_insert_history(&mut rx);
let text = cells
.iter()
.map(|cell| lines_to_single_string(cell))
.collect::<Vec<_>>()
.join("\n");
assert!(
text.contains("Context compacted"),
"expected replayed compaction marker, got {text:?}"
);
}
#[tokio::test]
async fn replayed_prefix_context_compaction_renders_history_marker() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.replay_thread_turns(
vec![AppServerTurn {
id: "turn-1".to_string(),
items: vec![AppServerThreadItem::ContextCompaction {
id: "compact-1".to_string(),
kind: Some(codex_app_server_protocol::ContextCompactionKind::Prefix),
}],
status: AppServerTurnStatus::Completed,
error: None,
started_at: None,
completed_at: None,
duration_ms: None,
}],
ReplayKind::ResumeInitialMessages,
);
let cells = drain_insert_history(&mut rx);
let text = cells
.iter()
.map(|cell| lines_to_single_string(cell))
.collect::<Vec<_>>()
.join("\n");
assert!(
text.contains("Context prefix compacted"),
"expected replayed prefix compaction marker, got {text:?}"
);
}
#[tokio::test]
async fn replayed_stream_error_does_not_set_retry_status_or_status_indicator() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;