From cca2414f20063567f8e7e9e4a4c6243f3110e1b2 Mon Sep 17 00:00:00 2001 From: Rohit Arunachalam Date: Sat, 18 Apr 2026 21:55:58 -0700 Subject: [PATCH] Simplify prefix compaction prewarm --- .../schema/json/ServerNotification.json | 27 -- .../codex_app_server_protocol.schemas.json | 27 -- .../codex_app_server_protocol.v2.schemas.json | 27 -- .../json/v2/ContextCompactedNotification.json | 19 - .../json/v2/ItemCompletedNotification.json | 17 - .../json/v2/ItemStartedNotification.json | 17 - .../schema/json/v2/ReviewStartResponse.json | 17 - .../schema/json/v2/ThreadForkResponse.json | 17 - .../schema/json/v2/ThreadListResponse.json | 17 - .../json/v2/ThreadMetadataUpdateResponse.json | 17 - .../schema/json/v2/ThreadReadResponse.json | 17 - .../schema/json/v2/ThreadResumeResponse.json | 17 - .../json/v2/ThreadRollbackResponse.json | 17 - .../schema/json/v2/ThreadStartResponse.json | 17 - .../json/v2/ThreadStartedNotification.json | 17 - .../json/v2/ThreadTurnsListResponse.json | 17 - .../json/v2/ThreadUnarchiveResponse.json | 17 - .../json/v2/TurnCompletedNotification.json | 17 - .../schema/json/v2/TurnStartResponse.json | 17 - .../json/v2/TurnStartedNotification.json | 17 - .../v2/ContextCompactedNotification.ts | 3 +- .../typescript/v2/ContextCompactionKind.ts | 5 - .../schema/typescript/v2/ThreadItem.ts | 3 +- .../schema/typescript/v2/index.ts | 1 - .../src/protocol/thread_history.rs | 7 +- .../app-server-protocol/src/protocol/v2.rs | 77 +-- .../app-server/src/bespoke_event_handling.rs | 3 +- codex-rs/codex-api/src/common.rs | 8 +- codex-rs/codex-api/src/lib.rs | 1 - codex-rs/config/src/schema.rs | 9 - codex-rs/core/config.schema.json | 28 +- codex-rs/core/src/client.rs | 3 +- codex-rs/core/src/compact.rs | 16 +- codex-rs/core/src/compact_remote.rs | 61 +-- codex-rs/core/src/compact_tests.rs | 16 +- codex-rs/core/src/config/config_tests.rs | 56 +-- codex-rs/core/src/config/mod.rs | 34 -- codex-rs/core/src/session/mod.rs | 38 +- .../core/src/session/prefix_compaction.rs | 248 ++++++++++ codex-rs/core/src/session/tests.rs | 50 +- codex-rs/core/src/session/turn.rs | 218 +-------- codex-rs/core/src/state/mod.rs | 2 - codex-rs/core/src/state/session.rs | 106 +---- codex-rs/core/tests/suite/compact_remote.rs | 441 +----------------- codex-rs/features/src/feature_configs.rs | 17 - codex-rs/features/src/lib.rs | 10 - codex-rs/features/src/tests.rs | 58 --- codex-rs/protocol/src/items.rs | 19 +- codex-rs/protocol/src/protocol.rs | 52 +-- codex-rs/tui/src/app/app_server_adapter.rs | 11 +- codex-rs/tui/src/chatwidget.rs | 16 +- .../src/chatwidget/tests/history_replay.rs | 33 -- 52 files changed, 341 insertions(+), 1681 deletions(-) delete mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactionKind.ts create mode 100644 codex-rs/core/src/session/prefix_compaction.rs diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index 414c3fbae6..5695877afa 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -839,16 +839,6 @@ "ContextCompactedNotification": { "description": "Deprecated: Use `ContextCompaction` item type instead.", "properties": { - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "threadId": { "type": "string" }, @@ -862,13 +852,6 @@ ], "type": "object" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "CreditsSnapshot": { "properties": { "balance": { @@ -3290,16 +3273,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 2a9b5d26ab..fb393c1145 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -7159,16 +7159,6 @@ "$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" }, @@ -7183,13 +7173,6 @@ "title": "ContextCompactedNotification", "type": "object" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "CreditsSnapshot": { "properties": { "balance": { @@ -13859,16 +13842,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/v2/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index b1b57b70cd..458367e205 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -3756,16 +3756,6 @@ "$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" }, @@ -3780,13 +3770,6 @@ "title": "ContextCompactedNotification", "type": "object" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "CreditsSnapshot": { "properties": { "balance": { @@ -11703,16 +11686,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ContextCompactedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ContextCompactedNotification.json index 8e961aaae2..8d2d4b1261 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ContextCompactedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ContextCompactedNotification.json @@ -1,26 +1,7 @@ { "$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" }, diff --git a/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json index 4f4837d7c1..c5a84c4272 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json @@ -199,13 +199,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1131,16 +1124,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json index 8e3a5829b7..cb9201f0a6 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json @@ -199,13 +199,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1131,16 +1124,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json index 6ad9ff7bc5..05dfaa4b72 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json @@ -335,13 +335,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1274,16 +1267,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json index ddb1a6df4a..909c2af4d8 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json @@ -396,13 +396,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1788,16 +1781,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json index 975e1e91c3..2db3296515 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json @@ -338,13 +338,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1550,16 +1543,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json index d3f97a124a..40da3390ec 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json @@ -338,13 +338,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1550,16 +1543,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json index 95424830d5..400c0b661a 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json @@ -338,13 +338,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1550,16 +1543,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json index e2e7845b40..aef0cd737c 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json @@ -396,13 +396,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1788,16 +1781,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json index 18fb163ba3..6a50d6d978 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json @@ -338,13 +338,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1550,16 +1543,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json index 6e87882d8c..a3d20555f1 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json @@ -396,13 +396,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1788,16 +1781,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json index 072649329a..c4b1e54490 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json @@ -338,13 +338,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1550,16 +1543,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadTurnsListResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadTurnsListResponse.json index 185b49d911..2c1dfb14f8 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadTurnsListResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadTurnsListResponse.json @@ -335,13 +335,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1274,16 +1267,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json index ab30642edc..e55c9651fa 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json @@ -338,13 +338,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1550,16 +1543,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json index 3dc06491f1..2848940085 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json @@ -335,13 +335,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1274,16 +1267,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json b/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json index 4f4f277a5e..c3f31e5322 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json @@ -335,13 +335,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1274,16 +1267,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json index 03c52f578d..7e8ca749c0 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json @@ -335,13 +335,6 @@ ], "type": "string" }, - "ContextCompactionKind": { - "enum": [ - "classic", - "prefix" - ], - "type": "string" - }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -1274,16 +1267,6 @@ "id": { "type": "string" }, - "kind": { - "anyOf": [ - { - "$ref": "#/definitions/ContextCompactionKind" - }, - { - "type": "null" - } - ] - }, "type": { "enum": [ "contextCompaction" diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactedNotification.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactedNotification.ts index c2fe0baf67..6927609de7 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactedNotification.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactedNotification.ts @@ -1,9 +1,8 @@ // 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, kind?: ContextCompactionKind, }; +export type ContextCompactedNotification = { threadId: string, turnId: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactionKind.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactionKind.ts deleted file mode 100644 index 77e3c53117..0000000000 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ContextCompactionKind.ts +++ /dev/null @@ -1,5 +0,0 @@ -// 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"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts index 7ea43d9554..747a70c54d 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts @@ -11,7 +11,6 @@ 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"; @@ -99,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: AbsolutePathBuf, } | { "type": "imageGeneration", id: string, status: string, revisedPrompt: string | null, result: string, savedPath?: AbsolutePathBuf, } | { "type": "enteredReviewMode", id: string, review: string, } | { "type": "exitedReviewMode", id: string, review: string, } | { "type": "contextCompaction", id: string, kind?: ContextCompactionKind, }; +agentsStates: { [key in string]?: CollabAgentState }, } | { "type": "webSearch", id: string, query: string, action: WebSearchAction | null, } | { "type": "imageView", id: string, path: AbsolutePathBuf, } | { "type": "imageGeneration", id: string, status: string, revisedPrompt: string | null, result: string, savedPath?: AbsolutePathBuf, } | { "type": "enteredReviewMode", id: string, review: string, } | { "type": "exitedReviewMode", id: string, review: string, } | { "type": "contextCompaction", id: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index 830a73d26d..f9884efb7c 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -70,7 +70,6 @@ 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"; diff --git a/codex-rs/app-server-protocol/src/protocol/thread_history.rs b/codex-rs/app-server-protocol/src/protocol/thread_history.rs index fc63e525e4..ef6593d09c 100644 --- a/codex-rs/app-server-protocol/src/protocol/thread_history.rs +++ b/codex-rs/app-server-protocol/src/protocol/thread_history.rs @@ -833,14 +833,11 @@ 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, - kind: payload.kind.clone().map(Into::into), - }); + .push(ThreadItem::ContextCompaction { id }); } fn handle_entered_review_mode(&mut self, payload: &codex_protocol::protocol::ReviewRequest) { diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index eb3c1c9a3f..6ab79f4763 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -51,7 +51,6 @@ 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; @@ -143,38 +142,6 @@ 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 { - Some(Self::Classic) - } - - pub fn is_classic_option(kind: &Option) -> bool { - kind.as_ref().is_none_or(Self::is_classic) - } -} - -impl From 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), @@ -4774,15 +4741,7 @@ pub enum ThreadItem { ExitedReviewMode { id: String, review: String }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] - ContextCompaction { - id: String, - #[serde( - default = "ContextCompactionKind::default_classic_option", - skip_serializing_if = "ContextCompactionKind::is_classic_option" - )] - #[ts(optional)] - kind: Option, - }, + ContextCompaction { id: String }, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] @@ -5217,10 +5176,9 @@ impl From for ThreadItem { result: image.result, saved_path: image.saved_path, }, - CoreTurnItem::ContextCompaction(compaction) => ThreadItem::ContextCompaction { - id: compaction.id, - kind: compaction.kind.map(Into::into), - }, + CoreTurnItem::ContextCompaction(compaction) => { + ThreadItem::ContextCompaction { id: compaction.id } + } } } } @@ -5877,12 +5835,6 @@ 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, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS, ExperimentalApi)] @@ -6798,7 +6750,7 @@ mod tests { } #[test] - fn context_compaction_thread_item_defaults_to_classic_kind() { + fn context_compaction_thread_item_serializes_without_kind() { let item: ThreadItem = serde_json::from_value(json!({ "type": "contextCompaction", "id": "compact-1" @@ -6809,7 +6761,6 @@ mod tests { item, ThreadItem::ContextCompaction { id: "compact-1".to_string(), - kind: Some(ContextCompactionKind::Classic) } ); @@ -6823,24 +6774,6 @@ mod tests { ); } - #[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!( diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index 3d11a3a7fa..0b243c0304 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -1334,7 +1334,7 @@ pub(crate) async fn apply_bespoke_event_handling( .send_server_notification(ServerNotification::PlanDelta(notification)) .await; } - EventMsg::ContextCompacted(event) => { + EventMsg::ContextCompacted(_) => { // Core still fans out this deprecated event for legacy clients; // v2 clients receive the canonical ContextCompaction item instead. if matches!(api_version, ApiVersion::V2) { @@ -1343,7 +1343,6 @@ pub(crate) async fn apply_bespoke_event_handling( 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)) diff --git a/codex-rs/codex-api/src/common.rs b/codex-rs/codex-api/src/common.rs index 313aca810e..6bc3336544 100644 --- a/codex-rs/codex-api/src/common.rs +++ b/codex-rs/codex-api/src/common.rs @@ -33,13 +33,7 @@ pub struct CompactionInput<'a> { #[serde(skip_serializing_if = "Option::is_none")] pub text: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub mode: Option, -} - -#[derive(Debug, Clone, Copy, Serialize)] -#[serde(rename_all = "snake_case")] -pub enum CompactionMode { - Prefix, + pub mode: Option<&'a str>, } /// Canonical input payload for the memory summarize endpoint. diff --git a/codex-rs/codex-api/src/lib.rs b/codex-rs/codex-api/src/lib.rs index 78ea8fe0b9..d92bb5b355 100644 --- a/codex-rs/codex-api/src/lib.rs +++ b/codex-rs/codex-api/src/lib.rs @@ -21,7 +21,6 @@ pub use crate::auth::AuthProvider; pub use crate::auth::SharedAuthProvider; pub use crate::auth::auth_header_telemetry; pub use crate::common::CompactionInput; -pub use crate::common::CompactionMode; pub use crate::common::MemorySummarizeInput; pub use crate::common::MemorySummarizeOutput; pub use crate::common::OpenAiVerbosity; diff --git a/codex-rs/config/src/schema.rs b/codex-rs/config/src/schema.rs index 4f16b8461d..91aa50addb 100644 --- a/codex-rs/config/src/schema.rs +++ b/codex-rs/config/src/schema.rs @@ -34,15 +34,6 @@ 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::>(), - ); - continue; - } validation .properties .insert(feature.key.to_string(), schema_gen.subschema_for::()); diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index f18718d18f..e90df599f3 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -435,7 +435,7 @@ "type": "boolean" }, "prefix_compaction": { - "$ref": "#/definitions/FeatureToml_for_PrefixCompactionConfigToml" + "type": "boolean" }, "prevent_idle_sleep": { "type": "boolean" @@ -649,16 +649,6 @@ } ] }, - "FeatureToml_for_PrefixCompactionConfigToml": { - "anyOf": [ - { - "type": "boolean" - }, - { - "$ref": "#/definitions/PrefixCompactionConfigToml" - } - ] - }, "FeedbackConfigToml": { "additionalProperties": false, "properties": { @@ -1457,20 +1447,6 @@ }, "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": { @@ -2358,7 +2334,7 @@ "type": "boolean" }, "prefix_compaction": { - "$ref": "#/definitions/FeatureToml_for_PrefixCompactionConfigToml" + "type": "boolean" }, "prevent_idle_sleep": { "type": "boolean" diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 872fe607f9..879d1380b8 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -35,7 +35,6 @@ use codex_api::ApiError; use codex_api::AuthProvider; use codex_api::CompactClient as ApiCompactClient; use codex_api::CompactionInput as ApiCompactionInput; -use codex_api::CompactionMode as ApiCompactionMode; use codex_api::Compression; use codex_api::MemoriesClient as ApiMemoriesClient; use codex_api::MemorySummarizeInput as ApiMemorySummarizeInput; @@ -411,7 +410,7 @@ impl ModelClient { effort: Option, summary: ReasoningSummaryConfig, session_telemetry: &SessionTelemetry, - mode: Option, + mode: Option<&'static str>, ) -> Result> { if prompt.input.is_empty() { return Ok(Vec::new()); diff --git a/codex-rs/core/src/compact.rs b/codex-rs/core/src/compact.rs index da46982516..6bb2caf99a 100644 --- a/codex-rs/core/src/compact.rs +++ b/codex-rs/core/src/compact.rs @@ -58,20 +58,12 @@ pub(crate) enum InitialContextInjection { DoNotInject, } -pub(crate) fn should_use_remote_compact_task(provider: &ModelProviderInfo) -> bool { - provider.supports_remote_compaction() - || provider - .base_url - .as_deref() - .is_some_and(is_codex_backend_base_url) +pub(crate) fn provider_supports_inline_remote_compaction(provider: &ModelProviderInfo) -> bool { + provider.is_openai() || provider.supports_remote_compaction() } -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) fn should_use_remote_compact_task(provider: &ModelProviderInfo) -> bool { + provider_supports_inline_remote_compaction(provider) } pub(crate) async fn run_inline_auto_compact_task( diff --git a/codex-rs/core/src/compact_remote.rs b/codex-rs/core/src/compact_remote.rs index 57a10c7fe9..e148bffa2a 100644 --- a/codex-rs/core/src/compact_remote.rs +++ b/codex-rs/core/src/compact_remote.rs @@ -31,6 +31,8 @@ use tokio_util::sync::CancellationToken; use tracing::error; use tracing::info; +const PREFIX_COMPACTION_MODE: &str = "prefix"; + pub(crate) async fn run_inline_remote_auto_compact_task( sess: Arc, turn_context: Arc, @@ -96,23 +98,13 @@ pub(crate) async fn run_remote_prefix_compact_task( } let prompt_input = history.for_prompt(&turn_context.model_info.input_modalities); - let tool_router = built_tools( + let prompt = build_remote_compact_prompt( sess.as_ref(), turn_context.as_ref(), - &prompt_input, - &HashSet::new(), - /*skills_outcome*/ None, - &CancellationToken::new(), + prompt_input, + base_instructions, ) .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 @@ -123,7 +115,7 @@ pub(crate) async fn run_remote_prefix_compact_task( turn_context.reasoning_effort, turn_context.reasoning_summary, &turn_context.session_telemetry, - Some(codex_api::CompactionMode::Prefix), + Some(PREFIX_COMPACTION_MODE), ) .or_else(|err| async { let total_usage_breakdown = sess.get_total_token_usage_breakdown().await; @@ -215,23 +207,13 @@ async fn run_remote_compact_task_inner_impl( .collect(); let prompt_input = history.for_prompt(&turn_context.model_info.input_modalities); - let tool_router = built_tools( + let prompt = build_remote_compact_prompt( sess.as_ref(), turn_context.as_ref(), - &prompt_input, - &HashSet::new(), - /*skills_outcome*/ None, - &CancellationToken::new(), + prompt_input, + base_instructions, ) .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 mut new_history = sess .services @@ -285,6 +267,31 @@ async fn run_remote_compact_task_inner_impl( Ok(()) } +async fn build_remote_compact_prompt( + sess: &Session, + turn_context: &TurnContext, + prompt_input: Vec, + base_instructions: BaseInstructions, +) -> CodexResult { + let tool_router = built_tools( + sess, + turn_context, + &prompt_input, + &HashSet::new(), + /*skills_outcome*/ None, + &CancellationToken::new(), + ) + .await?; + Ok(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, + }) +} + pub(crate) async fn process_compacted_history( sess: &Session, turn_context: &TurnContext, diff --git a/codex-rs/core/src/compact_tests.rs b/codex-rs/core/src/compact_tests.rs index a8d5b0030b..e5d49db035 100644 --- a/codex-rs/core/src/compact_tests.rs +++ b/codex-rs/core/src/compact_tests.rs @@ -27,15 +27,7 @@ async fn process_compacted_history_with_test_session( 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)); + assert!(provider_supports_inline_remote_compaction(&provider)); } #[test] @@ -43,7 +35,7 @@ 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)); + assert!(!provider_supports_inline_remote_compaction(&provider)); } #[test] @@ -212,7 +204,7 @@ fn build_token_limited_compacted_history_appends_summary_message() { } #[test] -fn should_use_remote_compact_task_for_azure_provider() { +fn provider_supports_inline_remote_compaction_for_azure_provider() { let provider = ModelProviderInfo { name: "Azure".into(), base_url: Some("https://example.com/openai".into()), @@ -232,7 +224,7 @@ fn should_use_remote_compact_task_for_azure_provider() { supports_websockets: false, }; - assert!(should_use_remote_compact_task(&provider)); + assert!(provider_supports_inline_remote_compaction(&provider)); } #[tokio::test] diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index d3cea30a28..24d0e58ba8 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -4842,7 +4842,6 @@ async 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()), @@ -4993,7 +4992,6 @@ async 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()), @@ -5142,7 +5140,6 @@ async 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()), @@ -5276,7 +5273,6 @@ async 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()), @@ -6526,7 +6522,7 @@ hide_spawn_agent_metadata = false } #[tokio::test] -async fn prefix_compaction_bool_feature_enables_with_default_threshold() -> std::io::Result<()> { +async fn prefix_compaction_bool_feature_enables() -> std::io::Result<()> { let codex_home = TempDir::new()?; std::fs::write( codex_home.path().join(CONFIG_TOML_FILE), @@ -6542,56 +6538,6 @@ prefix_compaction = true .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(()) } diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 25e89e2896..6320bff2e3 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -56,7 +56,6 @@ use codex_features::FeatureToml; use codex_features::Features; use codex_features::FeaturesToml; use codex_features::MultiAgentV2ConfigToml; -use codex_features::PrefixCompactionConfigToml; use codex_git_utils::resolve_root_git_project_for_trust; use codex_login::AuthManagerConfig; use codex_mcp::McpConfig; @@ -554,9 +553,6 @@ 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, @@ -618,11 +614,6 @@ impl Default for MultiAgentV2Config { } } -#[derive(Debug, Clone, Default, PartialEq, Eq)] -pub struct PrefixCompactionConfig { - pub threshold_percent: Option, -} - impl AuthManagerConfig for Config { fn codex_home(&self) -> PathBuf { self.codex_home.to_path_buf() @@ -1423,29 +1414,6 @@ 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, sandbox_policy: &SandboxPolicy, @@ -1793,7 +1761,6 @@ 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(fs, &cfg, &config_layer_stack, &mut startup_warnings) @@ -2280,7 +2247,6 @@ impl Config { background_terminal_max_timeout, ghost_snapshot, multi_agent_v2, - prefix_compaction, features, suppress_unstable_features_warning: cfg .suppress_unstable_features_warning diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index d29596a75b..e478ac6250 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -164,6 +164,7 @@ use codex_protocol::exec_output::StreamOutput; mod handlers; mod mcp; +pub(crate) mod prefix_compaction; mod review; mod rollout_reconstruction; #[allow(clippy::module_inception)] @@ -173,6 +174,8 @@ pub(crate) mod turn_context; #[cfg(test)] use self::handlers::submission_dispatch_span; use self::handlers::submission_loop; +#[cfg(test)] +use self::prefix_compaction::prefix_compact_token_limit; use self::review::spawn_review_thread; use self::session::AppServerClientMetadata; use self::session::Session; @@ -184,8 +187,6 @@ use self::turn::AssistantMessageStreamParsers; use self::turn::collect_explicit_app_ids_from_skill_items; #[cfg(test)] use self::turn::filter_connectors_for_input; -#[cfg(test)] -use self::turn::prefix_compact_token_limit; use self::turn::realtime_text_for_event; use self::turn_context::TurnContext; use self::turn_context::TurnSkillsContext; @@ -268,8 +269,6 @@ 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; #[cfg(test)] @@ -2279,37 +2278,6 @@ impl Session { self.services.model_client.advance_window_generation(); } - pub(crate) async fn begin_prefix_compact( - &self, - model_slug: String, - ) -> Option { - 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 { - let mut state = self.state.lock().await; - state.take_ready_prefix_compact(model_slug) - } - async fn persist_rollout_response_items(&self, items: &[ResponseItem]) { let rollout_items: Vec = items .iter() diff --git a/codex-rs/core/src/session/prefix_compaction.rs b/codex-rs/core/src/session/prefix_compaction.rs new file mode 100644 index 0000000000..162f05a3c6 --- /dev/null +++ b/codex-rs/core/src/session/prefix_compaction.rs @@ -0,0 +1,248 @@ +use std::sync::Arc; + +use crate::compact::provider_supports_inline_remote_compaction; +use crate::compact_remote::run_remote_prefix_compact_task; +use crate::session::session::Session; +use crate::session::turn_context::TurnContext; +use codex_features::Feature; +use codex_protocol::error::Result as CodexResult; +use codex_protocol::items::ContextCompactionItem; +use codex_protocol::items::TurnItem; +use codex_protocol::models::ResponseItem; +use codex_protocol::protocol::CompactedItem; +use codex_protocol::protocol::TurnContextItem; +use tokio::task::JoinHandle; +use tracing::debug; +use tracing::trace; + +pub(crate) type PrefixCompactTask = JoinHandle>; + +#[derive(Debug, Clone)] +pub(crate) struct PrefixCompactCandidate { + base_history: Vec, + replacement_prefix: Vec, + captured_context: Vec, + captured_reference_context_item: Option, +} + +pub(crate) async fn maybe_start_prefix_compact( + sess: &Arc, + turn_context: &Arc, + total_usage_tokens: i64, + auto_compact_limit: i64, +) { + if !turn_context.features.enabled(Feature::PrefixCompaction) { + return; + } + + if !provider_supports_inline_remote_compaction(turn_context.provider.info()) { + return; + } + + let Some(prefix_compact_limit) = prefix_compact_token_limit(auto_compact_limit) else { + return; + }; + if total_usage_tokens < prefix_compact_limit || total_usage_tokens >= auto_compact_limit { + return; + } + + let captured_context = sess.build_initial_context(turn_context.as_ref()).await; + let captured_reference_context_item = Some(turn_context.to_turn_context_item()); + + let mut state = sess.state.lock().await; + if state.prefix_compact_task.is_some() { + trace!( + turn_id = %turn_context.sub_id, + total_usage_tokens, + prefix_compact_limit, + auto_compact_limit, + "prefix compaction already running or ready" + ); + return; + } + + let base_history = state.history.raw_items().to_vec(); + if base_history.is_empty() { + return; + } + + let task = spawn_prefix_compact_task( + Arc::clone(sess), + Arc::clone(turn_context), + base_history.clone(), + captured_context, + captured_reference_context_item, + ); + state.prefix_compact_task = Some(task); + + debug!( + turn_id = %turn_context.sub_id, + model_slug = %turn_context.model_info.slug, + total_usage_tokens, + prefix_compact_limit, + auto_compact_limit, + base_history_len = base_history.len(), + "starting background prefix compaction" + ); +} + +pub(crate) async fn try_apply_ready_prefix_compact( + sess: &Arc, + turn_context: &Arc, +) -> CodexResult { + let Some(task) = take_finished_prefix_compact_task(sess).await else { + return Ok(false); + }; + + let candidate = match task.await { + Ok(Ok(candidate)) => candidate, + Ok(Err(err)) => { + debug!( + turn_id = %turn_context.sub_id, + "prefix compaction failed: {err:#}" + ); + return Ok(false); + } + Err(err) => { + debug!( + turn_id = %turn_context.sub_id, + "prefix compaction task failed: {err:#}" + ); + return Ok(false); + } + }; + + if apply_prefix_compact_candidate(sess, turn_context, candidate).await? { + return Ok(true); + } + + debug!( + turn_id = %turn_context.sub_id, + "prefix compaction candidate is stale; running foreground auto-compaction" + ); + Ok(false) +} + +pub(crate) async fn abandon_prefix_compact(sess: &Arc) { + let mut state = sess.state.lock().await; + abort_prefix_compact_task(&mut state.prefix_compact_task); +} + +pub(crate) fn abort_prefix_compact_task(task: &mut Option) { + if let Some(task) = task.take() { + task.abort(); + } +} + +pub(super) fn prefix_compact_token_limit(auto_compact_limit: i64) -> Option { + if auto_compact_limit == i64::MAX || auto_compact_limit <= 1 { + return None; + } + + let token_limit = auto_compact_limit.saturating_mul(60) / 100; + let token_limit = token_limit.clamp(1, auto_compact_limit.saturating_sub(1)); + Some(token_limit) +} + +async fn take_finished_prefix_compact_task(sess: &Arc) -> Option { + let mut state = sess.state.lock().await; + if state + .prefix_compact_task + .as_ref() + .is_some_and(|task| task.is_finished()) + { + state.prefix_compact_task.take() + } else { + None + } +} + +async fn apply_prefix_compact_candidate( + sess: &Arc, + turn_context: &Arc, + candidate: PrefixCompactCandidate, +) -> CodexResult { + 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 + { + debug!( + turn_id = %turn_context.sub_id, + current_history_len = current_items.len(), + base_history_len = candidate.base_history.len(), + "prefix compaction candidate no longer matches current history" + ); + return Ok(false); + } + + let compaction_item = TurnItem::ContextCompaction(ContextCompactionItem::new()); + sess.emit_turn_item_started(turn_context, &compaction_item) + .await; + + let retained_suffix: Vec = current_items[candidate.base_history.len()..] + .iter() + .filter(|item| !matches!(item, ResponseItem::GhostSnapshot { .. })) + .cloned() + .collect(); + let reference_context_item = sess + .reference_context_item() + .await + .or(candidate.captured_reference_context_item); + let mut new_history = candidate.replacement_prefix; + new_history.extend(candidate.captured_context); + new_history.extend(retained_suffix); + + new_history.extend( + current_items + .iter() + .filter(|item| matches!(item, ResponseItem::GhostSnapshot { .. })) + .cloned(), + ); + + 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; + debug!( + turn_id = %turn_context.sub_id, + "applied prefix compaction candidate" + ); + Ok(true) +} + +fn spawn_prefix_compact_task( + sess: Arc, + turn_context: Arc, + base_history: Vec, + captured_context: Vec, + captured_reference_context_item: Option, +) -> PrefixCompactTask { + tokio::spawn(async move { + let replacement_prefix = run_remote_prefix_compact_task( + Arc::clone(&sess), + Arc::clone(&turn_context), + base_history.clone(), + ) + .await?; + debug!( + turn_id = %turn_context.sub_id, + model_slug = %turn_context.model_info.slug, + base_history_len = base_history.len(), + replacement_prefix_len = replacement_prefix.len(), + captured_context_len = captured_context.len(), + "background prefix compaction ready" + ); + Ok(PrefixCompactCandidate { + base_history, + replacement_prefix, + captured_context, + captured_reference_context_item, + }) + }) +} diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 764a391e6f..be179bb417 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -6048,56 +6048,12 @@ async fn unified_exec_rejects_escalated_permissions_when_policy_not_on_request() #[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 - ), + prefix_compact_token_limit(/*auto_compact_limit*/ 1_000), 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 - ); +fn prefix_compact_token_limit_is_none_for_tiny_auto_compact_limit() { + assert_eq!(prefix_compact_token_limit(/*auto_compact_limit*/ 1), None); } diff --git a/codex-rs/core/src/session/turn.rs b/codex-rs/core/src/session/turn.rs index 5644b743e5..29a7022a9c 100644 --- a/codex-rs/core/src/session/turn.rs +++ b/codex-rs/core/src/session/turn.rs @@ -15,7 +15,6 @@ use crate::compact::collect_user_messages; 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::connectors; use crate::feedback_tags; use crate::hook_runtime::PendingInputHookDisposition; @@ -39,10 +38,11 @@ use crate::parse_turn_item; use crate::plugins::build_plugin_injections; use crate::resolve_skill_dependencies_for_turn; use crate::session::PreviousTurnSettings; +use crate::session::prefix_compaction::abandon_prefix_compact; +use crate::session::prefix_compaction::maybe_start_prefix_compact; +use crate::session::prefix_compaction::try_apply_ready_prefix_compact; use crate::session::session::Session; use crate::session::turn_context::TurnContext; -use crate::state::PrefixCompactCandidate; -use crate::state::PrefixCompactStart; use crate::stream_events_utils::HandleOutputCtx; use crate::stream_events_utils::handle_non_tool_response_item; use crate::stream_events_utils::handle_output_item_done; @@ -75,7 +75,6 @@ use codex_hooks::HookResult; use codex_protocol::config_types::ModeKind; use codex_protocol::error::CodexErr; use codex_protocol::error::Result as CodexResult; -use codex_protocol::items::ContextCompactionItem; use codex_protocol::items::PlanItem; use codex_protocol::items::TurnItem; use codex_protocol::items::UserMessageItem; @@ -88,7 +87,6 @@ use codex_protocol::protocol::AgentMessageContentDeltaEvent; use codex_protocol::protocol::AgentReasoningSectionBreakEvent; use codex_protocol::protocol::AskForApproval; use codex_protocol::protocol::CodexErrorInfo; -use codex_protocol::protocol::CompactedItem; use codex_protocol::protocol::ErrorEvent; use codex_protocol::protocol::EventMsg; use codex_protocol::protocol::PlanDeltaEvent; @@ -109,7 +107,6 @@ use futures::prelude::*; use futures::stream::FuturesOrdered; use tokio_util::sync::CancellationToken; use tracing::Instrument; -use tracing::debug; use tracing::error; use tracing::field; use tracing::info; @@ -819,26 +816,11 @@ async fn run_auto_compact( reason: CompactionReason, phase: CompactionPhase, ) -> CodexResult<()> { - let prefix_candidate = sess - .take_ready_prefix_compact(&turn_context.model_info.slug) - .await; - if let Some(candidate) = prefix_candidate { - if apply_prefix_compact_candidate(sess, turn_context, candidate).await? { - return Ok(()); - } - debug!( - turn_id = %turn_context.sub_id, - "prefix compaction candidate is stale; running foreground auto-compaction" - ); - } else if turn_context.features.enabled(Feature::PrefixCompaction) { - debug!( - turn_id = %turn_context.sub_id, - model_slug = %turn_context.model_info.slug, - "prefix compaction candidate is not ready; running foreground auto-compaction" - ); + if try_apply_ready_prefix_compact(sess, turn_context).await? { + return Ok(()); } - sess.abandon_prefix_compact().await; + abandon_prefix_compact(sess).await; if should_use_remote_compact_task(turn_context.provider.info()) { run_inline_remote_auto_compact_task( Arc::clone(sess), @@ -861,194 +843,6 @@ async fn run_auto_compact( Ok(()) } -async fn apply_prefix_compact_candidate( - sess: &Arc, - turn_context: &Arc, - candidate: PrefixCompactCandidate, -) -> CodexResult { - 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 - { - debug!( - turn_id = %turn_context.sub_id, - generation = candidate.generation, - model_slug = %candidate.model_slug, - current_history_len = current_items.len(), - base_history_len = candidate.base_history.len(), - "prefix compaction candidate no longer matches current history" - ); - return Ok(false); - } - - let compaction_item = TurnItem::ContextCompaction(ContextCompactionItem::new_prefix()); - sess.emit_turn_item_started(turn_context, &compaction_item) - .await; - - let retained_suffix: Vec = current_items[candidate.base_history.len()..] - .iter() - .filter(|item| !matches!(item, ResponseItem::GhostSnapshot { .. })) - .cloned() - .collect(); - let reference_context_item = sess - .reference_context_item() - .await - .or(candidate.captured_reference_context_item); - let mut new_history = candidate.replacement_prefix; - new_history.extend(candidate.captured_context); - new_history.extend(retained_suffix); - - new_history.extend( - current_items - .iter() - .filter(|item| matches!(item, ResponseItem::GhostSnapshot { .. })) - .cloned(), - ); - - 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; - debug!( - turn_id = %turn_context.sub_id, - generation = candidate.generation, - model_slug = %candidate.model_slug, - "applied prefix compaction candidate" - ); - Ok(true) -} - -pub(super) fn prefix_compact_token_limit( - auto_compact_limit: i64, - configured_threshold_percent: Option, -) -> Option { - 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, - turn_context: &Arc, - 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.info()) { - 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(mut start) = sess - .begin_prefix_compact(turn_context.model_info.slug.clone()) - .await - else { - trace!( - turn_id = %turn_context.sub_id, - total_usage_tokens, - prefix_compact_limit, - auto_compact_limit, - "prefix compaction already running or ready" - ); - return; - }; - start.captured_context = sess.build_initial_context(turn_context.as_ref()).await; - start.captured_reference_context_item = Some(turn_context.to_turn_context_item()); - - debug!( - turn_id = %turn_context.sub_id, - generation = start.generation, - model_slug = %start.model_slug, - total_usage_tokens, - prefix_compact_limit, - auto_compact_limit, - base_history_len = start.base_history.len(), - captured_context_len = start.captured_context.len(), - "starting background prefix compaction" - ); - spawn_prefix_compact_task(Arc::clone(sess), Arc::clone(turn_context), start); -} - -fn spawn_prefix_compact_task( - sess: Arc, - turn_context: Arc, - start: PrefixCompactStart, -) { - tokio::spawn(async move { - let PrefixCompactStart { - generation, - model_slug, - base_history, - captured_context, - captured_reference_context_item, - } = start; - let compacted = run_remote_prefix_compact_task( - Arc::clone(&sess), - Arc::clone(&turn_context), - base_history.clone(), - ) - .await; - match compacted { - Ok(replacement_prefix) => { - debug!( - turn_id = %turn_context.sub_id, - generation, - model_slug = %model_slug, - base_history_len = base_history.len(), - replacement_prefix_len = replacement_prefix.len(), - captured_context_len = captured_context.len(), - "background prefix compaction ready" - ); - sess.finish_prefix_compact(PrefixCompactCandidate { - generation, - model_slug, - base_history, - replacement_prefix, - captured_context, - captured_reference_context_item, - }) - .await; - } - Err(err) => { - debug!( - turn_id = %turn_context.sub_id, - generation, - model_slug = %model_slug, - "prefix compaction failed: {err:#}" - ); - sess.fail_prefix_compact(generation).await; - } - } - }); -} - pub(super) fn collect_explicit_app_ids_from_skill_items( skill_items: &[ResponseItem], connectors: &[connectors::AppInfo], diff --git a/codex-rs/core/src/state/mod.rs b/codex-rs/core/src/state/mod.rs index 94739d57ce..f3ebc7225d 100644 --- a/codex-rs/core/src/state/mod.rs +++ b/codex-rs/core/src/state/mod.rs @@ -3,8 +3,6 @@ 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; diff --git a/codex-rs/core/src/state/session.rs b/codex-rs/core/src/state/session.rs index e3f77626bc..5ca5945607 100644 --- a/codex-rs/core/src/state/session.rs +++ b/codex-rs/core/src/state/session.rs @@ -9,6 +9,8 @@ use std::collections::HashSet; use crate::agent_identity::RegisteredAgentTask; use crate::context_manager::ContextManager; use crate::session::PreviousTurnSettings; +use crate::session::prefix_compaction::PrefixCompactTask; +use crate::session::prefix_compaction::abort_prefix_compact_task; use crate::session::session::SessionConfiguration; use crate::session_startup_prewarm::SessionStartupPrewarmHandle; use codex_protocol::protocol::RateLimitSnapshot; @@ -35,8 +37,7 @@ pub(crate) struct SessionState { pub(crate) active_connector_selection: HashSet, pub(crate) pending_session_start_source: Option, granted_permissions: Option, - next_prefix_compact_generation: u64, - prefix_compact: PrefixCompactState, + pub(crate) prefix_compact_task: Option, next_turn_is_first: bool, } @@ -57,8 +58,7 @@ impl SessionState { active_connector_selection: HashSet::new(), pending_session_start_source: None, granted_permissions: None, - next_prefix_compact_generation: 0, - prefix_compact: PrefixCompactState::Idle, + prefix_compact_task: None, next_turn_is_first: true, } } @@ -104,74 +104,7 @@ 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 { - 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, - captured_context: Vec::new(), - captured_reference_context_item: None, - }) - } - - 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(Box::new(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 { - 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 - } + abort_prefix_compact_task(&mut self.prefix_compact_task); } pub(crate) fn set_token_info(&mut self, info: Option) { @@ -315,35 +248,6 @@ impl SessionState { } } -#[derive(Debug, Default)] -enum PrefixCompactState { - #[default] - Idle, - Running { - generation: u64, - }, - Ready(Box), -} - -#[derive(Debug, Clone)] -pub(crate) struct PrefixCompactStart { - pub(crate) generation: u64, - pub(crate) model_slug: String, - pub(crate) base_history: Vec, - pub(crate) captured_context: Vec, - pub(crate) captured_reference_context_item: Option, -} - -#[derive(Debug, Clone)] -pub(crate) struct PrefixCompactCandidate { - pub(crate) generation: u64, - pub(crate) model_slug: String, - pub(crate) base_history: Vec, - pub(crate) replacement_prefix: Vec, - pub(crate) captured_context: Vec, - pub(crate) captured_reference_context_item: Option, -} - // 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. diff --git a/codex-rs/core/tests/suite/compact_remote.rs b/codex-rs/core/tests/suite/compact_remote.rs index 95195a54d6..ab6ace090c 100644 --- a/codex-rs/core/tests/suite/compact_remote.rs +++ b/codex-rs/core/tests/suite/compact_remote.rs @@ -2,8 +2,6 @@ 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; @@ -41,11 +39,7 @@ use core_test_support::wait_for_event_match; use core_test_support::wait_for_event_with_timeout; 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); @@ -120,54 +114,6 @@ async fn wait_for_response_requests(mock_response: &responses::ResponseMock, cou ); } -async fn wait_for_json_request_count( - requests: &StdArc>>, - 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>>, -} - -impl Respond for PrefixRaceCompactResponder { - fn respond(&self, request: &wiremock::Request) -> ResponseTemplate { - let body = request - .body_json::() - .expect("compact request body should be JSON"); - let prefix_mode = body.get("mode").and_then(serde_json::Value::as_str) == Some("prefix"); - 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 { @@ -1147,64 +1093,7 @@ async fn remote_manual_compact_emits_context_compaction_items() -> Result<()> { } #[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<()> { +async fn prefix_compact_uses_sixty_percent_of_auto_compact_limit() -> Result<()> { skip_if_no_network!(Ok(())); let harness = TestCodexHarness::with_builder( @@ -1213,7 +1102,6 @@ async fn prefix_compact_uses_configured_threshold_percent() -> Result<()> { .with_config(|config| { config.model_context_window = Some(20_000); config.model_auto_compact_token_limit = Some(800); - config.prefix_compaction.threshold_percent = Some(90); enable_prefix_compaction(config); }), ) @@ -1225,114 +1113,22 @@ async fn prefix_compact_uses_configured_threshold_percent() -> Result<()> { vec![ sse(vec![ responses::ev_assistant_message("m1", "FIRST_REMOTE_REPLY"), - responses::ev_completed_with_tokens("resp-1", /*total_tokens*/ 650), + responses::ev_completed_with_tokens("resp-1", /*total_tokens*/ 450), ]), sse(vec![ responses::ev_assistant_message("m2", "SECOND_REMOTE_REPLY"), - responses::ev_completed_with_tokens("resp-2", /*total_tokens*/ 750), + responses::ev_completed_with_tokens("resp-2", /*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 - ); - - 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]["mode"], json!("prefix")); - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn oversized_prefix_snapshot_skips_prefix_compact_request() -> Result<()> { - skip_if_no_network!(Ok(())); - - let large_reply = "LARGE_REMOTE_REPLY ".repeat(1_000); - 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( + let compact_mock = responses::mount_compact_json_once( harness.server(), - vec![ - sse(vec![ - responses::ev_assistant_message("m1", &large_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), - ]), - ], + json!({ "output": compacted_summary_only_output("PREFIX_THRESHOLD_SUMMARY") }), ) .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(1) - .expect(1) - .mount(harness.server()) - .await; - codex .submit(Op::UserInput { items: vec![UserInput::Text { @@ -1344,6 +1140,7 @@ async fn oversized_prefix_snapshot_skips_prefix_compact_request() -> Result<()> }) .await?; wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await; + assert_eq!(compact_mock.requests().len(), 0); codex .submit(Op::UserInput { @@ -1356,31 +1153,13 @@ async fn oversized_prefix_snapshot_skips_prefix_compact_request() -> Result<()> }) .await?; wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await; + wait_for_response_requests(&compact_mock, /*count*/ 1).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(), 1); - assert!(compact_requests[0].get("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")); + assert_eq!(compact_mock.requests().len(), 1); + assert_eq!( + compact_mock.single_request().body_json()["mode"], + json!("prefix") + ); Ok(()) } @@ -1524,202 +1303,6 @@ async fn ready_prefix_compact_is_applied_by_pre_turn_auto_compact() -> Result<() 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(20_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!( - compact_requests - .iter() - .any(|request| request["mode"] == json!("prefix")), - "expected one background prefix compact request" - ); - assert!( - compact_requests - .iter() - .any(|request| request.get("mode").is_none()), - "expected one foreground fallback 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("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(20_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!( - compact_requests - .iter() - .any(|request| request["mode"] == json!("prefix")), - "expected one background prefix compact request" - ); - assert!( - compact_requests - .iter() - .any(|request| request.get("mode").is_none()), - "expected one foreground fallback 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("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(())); diff --git a/codex-rs/features/src/feature_configs.rs b/codex-rs/features/src/feature_configs.rs index fb39e90b0c..0db4e4e82e 100644 --- a/codex-rs/features/src/feature_configs.rs +++ b/codex-rs/features/src/feature_configs.rs @@ -21,20 +21,3 @@ 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, - /// 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, -} - -impl FeatureConfig for PrefixCompactionConfigToml { - fn enabled(&self) -> Option { - self.enabled - } -} diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 91838db3f7..7a2e70156f 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -17,7 +17,6 @@ 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; @@ -533,8 +532,6 @@ 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>, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub prefix_compaction: Option>, /// Boolean feature toggles keyed by canonical or legacy feature name. #[serde(flatten)] entries: BTreeMap, @@ -553,13 +550,6 @@ 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 } } diff --git a/codex-rs/features/src/tests.rs b/codex-rs/features/src/tests.rs index c78808f89a..d02c8e8fa3 100644 --- a/codex-rs/features/src/tests.rs +++ b/codex-rs/features/src/tests.rs @@ -430,64 +430,6 @@ prefix_compaction = true 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] diff --git a/codex-rs/protocol/src/items.rs b/codex-rs/protocol/src/items.rs index 93af51581a..601858dd5f 100644 --- a/codex-rs/protocol/src/items.rs +++ b/codex-rs/protocol/src/items.rs @@ -7,7 +7,6 @@ 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; @@ -131,33 +130,17 @@ 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, } 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 { - kind: self.kind.clone(), - }) + EventMsg::ContextCompacted(ContextCompactedEvent {}) } } diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 363a5ed2d8..5e0729a7f8 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -2075,38 +2075,8 @@ 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 { - Some(Self::Classic) - } - - pub fn is_classic_option(kind: &Option) -> bool { - kind.as_ref().is_none_or(Self::is_classic) - } -} - #[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, TS)] -pub struct ContextCompactedEvent { - #[serde( - default = "ContextCompactionKind::default_classic_option", - skip_serializing_if = "ContextCompactionKind::is_classic_option" - )] - #[ts(optional)] - pub kind: Option, -} +pub struct ContextCompactedEvent {} #[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, TS)] pub struct TurnCompleteEvent { @@ -3961,26 +3931,6 @@ 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!( diff --git a/codex-rs/tui/src/app/app_server_adapter.rs b/codex-rs/tui/src/app/app_server_adapter.rs index 4d71fb6407..3dfc9fd235 100644 --- a/codex-rs/tui/src/app/app_server_adapter.rs +++ b/codex-rs/tui/src/app/app_server_adapter.rs @@ -890,17 +890,9 @@ fn thread_item_to_core(item: &ThreadItem) -> Option { result: result.clone(), saved_path: saved_path.clone(), })), - ThreadItem::ContextCompaction { id, kind } => { + ThreadItem::ContextCompaction { id } => { 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 { .. } @@ -1603,7 +1595,6 @@ mod tests { }, ThreadItem::ContextCompaction { id: "compact-1".to_string(), - kind: Some(codex_app_server_protocol::ContextCompactionKind::Classic), }, ], status: TurnStatus::Completed, diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index a1e3e673db..6257b9c878 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -2206,16 +2206,11 @@ impl ChatWidget { self.request_redraw(); } - fn on_context_compacted(&mut self, prefix_compacted: bool) { + fn on_context_compacted(&mut self) { 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( - message.to_owned(), + "Context compacted".to_owned(), /*hint*/ None, )); self.request_redraw(); @@ -5994,11 +5989,8 @@ impl ChatWidget { ThreadItem::ExitedReviewMode { .. } => { self.exit_review_mode_after_item(); } - ThreadItem::ContextCompaction { kind, .. } => { - self.on_context_compacted(matches!( - kind, - Some(codex_app_server_protocol::ContextCompactionKind::Prefix) - )); + ThreadItem::ContextCompaction { .. } => { + self.on_context_compacted(); } ThreadItem::HookPrompt { .. } => {} ThreadItem::CollabAgentToolCall { diff --git a/codex-rs/tui/src/chatwidget/tests/history_replay.rs b/codex-rs/tui/src/chatwidget/tests/history_replay.rs index 933d70c238..f0921a6189 100644 --- a/codex-rs/tui/src/chatwidget/tests/history_replay.rs +++ b/codex-rs/tui/src/chatwidget/tests/history_replay.rs @@ -808,7 +808,6 @@ async fn replayed_context_compaction_renders_history_marker() { 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, @@ -831,38 +830,6 @@ async fn replayed_context_compaction_renders_history_marker() { ); } -#[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::>() - .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;