Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

app-server: Replay pending item requests on `thread/resume` (#12560)

Ruslan Nigmatullin69d7a456bbff
diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json
index eca77d2bfa..81797e282f 100644
--- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json
+++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json
@@ -1422,6 +1422,32 @@
],
"type": "object"
},
+ "RequestId": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "format": "int64",
+ "type": "integer"
+ }
+ ]
+ },
+ "ServerRequestResolvedNotification": {
+ "properties": {
+ "requestId": {
+ "$ref": "#/definitions/RequestId"
+ },
+ "threadId": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "requestId",
+ "threadId"
+ ],
+ "type": "object"
+ },
"SessionSource": {
"oneOf": [
{
@@ -3422,6 +3448,26 @@
"title": "Item/fileChange/outputDeltaNotification",
"type": "object"
},
+ {
+ "properties": {
+ "method": {
+ "enum": [
+ "serverRequest/resolved"
+ ],
+ "title": "ServerRequest/resolvedNotificationMethod",
+ "type": "string"
+ },
+ "params": {
+ "$ref": "#/definitions/ServerRequestResolvedNotification"
+ }
+ },
+ "required": [
+ "method",
+ "params"
+ ],
+ "title": "ServerRequest/resolvedNotification",
+ "type": "object"
+ },
{
"properties": {
"method": {
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 a0863ad61d..a9fc41aed7 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
@@ -357,7 +357,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -382,7 +382,7 @@
"description": "NEW APIs",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -406,7 +406,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -430,7 +430,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -454,7 +454,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -478,7 +478,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -502,7 +502,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -526,7 +526,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -550,7 +550,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -574,7 +574,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -598,7 +598,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -622,7 +622,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -646,7 +646,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -670,7 +670,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -694,7 +694,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -718,7 +718,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -742,7 +742,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -766,7 +766,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -790,7 +790,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -814,7 +814,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -838,7 +838,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -862,7 +862,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -886,7 +886,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -910,7 +910,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -934,7 +934,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -958,7 +958,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -981,7 +981,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1005,7 +1005,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1029,7 +1029,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1053,7 +1053,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1077,7 +1077,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1100,7 +1100,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1123,7 +1123,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1148,7 +1148,7 @@
"description": "Execute a command (argv vector) under the server's sandbox.",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1172,7 +1172,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1196,7 +1196,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1220,7 +1220,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1244,7 +1244,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1268,7 +1268,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1292,7 +1292,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1315,7 +1315,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -1339,7 +1339,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -3065,7 +3065,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"message": {
"type": "string"
@@ -4943,7 +4943,7 @@
"$ref": "#/definitions/JSONRPCErrorError"
},
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
}
},
"required": [
@@ -5011,7 +5011,7 @@
"description": "A request that expects a response.",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"type": "string"
@@ -5030,7 +5030,7 @@
"description": "A successful (non-error) response to a request.",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"result": true
},
@@ -5544,6 +5544,7 @@
"type": "object"
},
"RequestId": {
+ "$schema": "http://json-schema.org/draft-07/schema#",
"anyOf": [
{
"type": "string"
@@ -5553,7 +5554,7 @@
"type": "integer"
}
],
- "description": "ID of a request, which can be either a string or an integer."
+ "title": "RequestId"
},
"RequestUserInputQuestion": {
"properties": {
@@ -6194,6 +6195,26 @@
"title": "Item/fileChange/outputDeltaNotification",
"type": "object"
},
+ {
+ "properties": {
+ "method": {
+ "enum": [
+ "serverRequest/resolved"
+ ],
+ "title": "ServerRequest/resolvedNotificationMethod",
+ "type": "string"
+ },
+ "params": {
+ "$ref": "#/definitions/v2/ServerRequestResolvedNotification"
+ }
+ },
+ "required": [
+ "method",
+ "params"
+ ],
+ "title": "ServerRequest/resolvedNotification",
+ "type": "object"
+ },
{
"properties": {
"method": {
@@ -6647,7 +6668,7 @@
"description": "NEW APIs Sent when approval is requested for a specific command execution. This request is used for Turns started via turn/start.",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -6672,7 +6693,7 @@
"description": "Sent when approval is requested for a specific file change. This request is used for Turns started via turn/start.",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -6697,7 +6718,7 @@
"description": "EXPERIMENTAL - Request input from the user for a tool call.",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -6722,7 +6743,7 @@
"description": "Execute a dynamic tool call on the client.",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -6746,7 +6767,7 @@
{
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -6771,7 +6792,7 @@
"description": "DEPRECATED APIs below Request to approve a patch. This request is used for Turns started via the legacy APIs (i.e. SendUserTurn, SendUserMessage).",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -6796,7 +6817,7 @@
"description": "Request to exec a command. This request is used for Turns started via the legacy APIs (i.e. SendUserTurn, SendUserMessage).",
"properties": {
"id": {
- "$ref": "#/definitions/RequestId"
+ "$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
@@ -11110,6 +11131,17 @@
],
"type": "object"
},
+ "RequestId": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "format": "int64",
+ "type": "integer"
+ }
+ ]
+ },
"ResidencyRequirement": {
"enum": [
"us"
@@ -11849,6 +11881,23 @@
},
"type": "object"
},
+ "ServerRequestResolvedNotification": {
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "properties": {
+ "requestId": {
+ "$ref": "#/definitions/v2/RequestId"
+ },
+ "threadId": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "requestId",
+ "threadId"
+ ],
+ "title": "ServerRequestResolvedNotification",
+ "type": "object"
+ },
"SessionSource": {
"oneOf": [
{
diff --git a/codex-rs/app-server-protocol/schema/json/v2/ServerRequestResolvedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ServerRequestResolvedNotification.json
new file mode 100644
index 0000000000..18a5e76079
--- /dev/null
+++ b/codex-rs/app-server-protocol/schema/json/v2/ServerRequestResolvedNotification.json
@@ -0,0 +1,30 @@
+{
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "definitions": {
+ "RequestId": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "format": "int64",
+ "type": "integer"
+ }
+ ]
+ }
+ },
+ "properties": {
+ "requestId": {
+ "$ref": "#/definitions/RequestId"
+ },
+ "threadId": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "requestId",
+ "threadId"
+ ],
+ "title": "ServerRequestResolvedNotification",
+ "type": "object"
+}
\ No newline at end of file
diff --git a/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts b/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts
index 9b082158c5..43345d9093 100644
--- a/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts
+++ b/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts
@@ -27,6 +27,7 @@ import type { RawResponseItemCompletedNotification } from "./v2/RawResponseItemC
import type { ReasoningSummaryPartAddedNotification } from "./v2/ReasoningSummaryPartAddedNotification";
import type { ReasoningSummaryTextDeltaNotification } from "./v2/ReasoningSummaryTextDeltaNotification";
import type { ReasoningTextDeltaNotification } from "./v2/ReasoningTextDeltaNotification";
+import type { ServerRequestResolvedNotification } from "./v2/ServerRequestResolvedNotification";
import type { TerminalInteractionNotification } from "./v2/TerminalInteractionNotification";
import type { ThreadArchivedNotification } from "./v2/ThreadArchivedNotification";
import type { ThreadClosedNotification } from "./v2/ThreadClosedNotification";
@@ -50,4 +51,4 @@ import type { WindowsWorldWritableWarningNotification } from "./v2/WindowsWorldW
/**
* Notification sent from the server to the client.
*/
-export type ServerNotification = { "method": "error", "params": ErrorNotification } | { "method": "thread/started", "params": ThreadStartedNotification } | { "method": "thread/status/changed", "params": ThreadStatusChangedNotification } | { "method": "thread/archived", "params": ThreadArchivedNotification } | { "method": "thread/unarchived", "params": ThreadUnarchivedNotification } | { "method": "thread/closed", "params": ThreadClosedNotification } | { "method": "thread/name/updated", "params": ThreadNameUpdatedNotification } | { "method": "thread/tokenUsage/updated", "params": ThreadTokenUsageUpdatedNotification } | { "method": "turn/started", "params": TurnStartedNotification } | { "method": "turn/completed", "params": TurnCompletedNotification } | { "method": "turn/diff/updated", "params": TurnDiffUpdatedNotification } | { "method": "turn/plan/updated", "params": TurnPlanUpdatedNotification } | { "method": "item/started", "params": ItemStartedNotification } | { "method": "item/completed", "params": ItemCompletedNotification } | { "method": "rawResponseItem/completed", "params": RawResponseItemCompletedNotification } | { "method": "item/agentMessage/delta", "params": AgentMessageDeltaNotification } | { "method": "item/plan/delta", "params": PlanDeltaNotification } | { "method": "item/commandExecution/outputDelta", "params": CommandExecutionOutputDeltaNotification } | { "method": "item/commandExecution/terminalInteraction", "params": TerminalInteractionNotification } | { "method": "item/fileChange/outputDelta", "params": FileChangeOutputDeltaNotification } | { "method": "item/mcpToolCall/progress", "params": McpToolCallProgressNotification } | { "method": "mcpServer/oauthLogin/completed", "params": McpServerOauthLoginCompletedNotification } | { "method": "account/updated", "params": AccountUpdatedNotification } | { "method": "account/rateLimits/updated", "params": AccountRateLimitsUpdatedNotification } | { "method": "app/list/updated", "params": AppListUpdatedNotification } | { "method": "item/reasoning/summaryTextDelta", "params": ReasoningSummaryTextDeltaNotification } | { "method": "item/reasoning/summaryPartAdded", "params": ReasoningSummaryPartAddedNotification } | { "method": "item/reasoning/textDelta", "params": ReasoningTextDeltaNotification } | { "method": "thread/compacted", "params": ContextCompactedNotification } | { "method": "model/rerouted", "params": ModelReroutedNotification } | { "method": "deprecationNotice", "params": DeprecationNoticeNotification } | { "method": "configWarning", "params": ConfigWarningNotification } | { "method": "fuzzyFileSearch/sessionUpdated", "params": FuzzyFileSearchSessionUpdatedNotification } | { "method": "fuzzyFileSearch/sessionCompleted", "params": FuzzyFileSearchSessionCompletedNotification } | { "method": "thread/realtime/started", "params": ThreadRealtimeStartedNotification } | { "method": "thread/realtime/itemAdded", "params": ThreadRealtimeItemAddedNotification } | { "method": "thread/realtime/outputAudio/delta", "params": ThreadRealtimeOutputAudioDeltaNotification } | { "method": "thread/realtime/error", "params": ThreadRealtimeErrorNotification } | { "method": "thread/realtime/closed", "params": ThreadRealtimeClosedNotification } | { "method": "windows/worldWritableWarning", "params": WindowsWorldWritableWarningNotification } | { "method": "windowsSandbox/setupCompleted", "params": WindowsSandboxSetupCompletedNotification } | { "method": "account/login/completed", "params": AccountLoginCompletedNotification } | { "method": "authStatusChange", "params": AuthStatusChangeNotification } | { "method": "loginChatGptComplete", "params": LoginChatGptCompleteNotification } | { "method": "sessionConfigured", "params": SessionConfiguredNotification };
+export type ServerNotification = { "method": "error", "params": ErrorNotification } | { "method": "thread/started", "params": ThreadStartedNotification } | { "method": "thread/status/changed", "params": ThreadStatusChangedNotification } | { "method": "thread/archived", "params": ThreadArchivedNotification } | { "method": "thread/unarchived", "params": ThreadUnarchivedNotification } | { "method": "thread/closed", "params": ThreadClosedNotification } | { "method": "thread/name/updated", "params": ThreadNameUpdatedNotification } | { "method": "thread/tokenUsage/updated", "params": ThreadTokenUsageUpdatedNotification } | { "method": "turn/started", "params": TurnStartedNotification } | { "method": "turn/completed", "params": TurnCompletedNotification } | { "method": "turn/diff/updated", "params": TurnDiffUpdatedNotification } | { "method": "turn/plan/updated", "params": TurnPlanUpdatedNotification } | { "method": "item/started", "params": ItemStartedNotification } | { "method": "item/completed", "params": ItemCompletedNotification } | { "method": "rawResponseItem/completed", "params": RawResponseItemCompletedNotification } | { "method": "item/agentMessage/delta", "params": AgentMessageDeltaNotification } | { "method": "item/plan/delta", "params": PlanDeltaNotification } | { "method": "item/commandExecution/outputDelta", "params": CommandExecutionOutputDeltaNotification } | { "method": "item/commandExecution/terminalInteraction", "params": TerminalInteractionNotification } | { "method": "item/fileChange/outputDelta", "params": FileChangeOutputDeltaNotification } | { "method": "serverRequest/resolved", "params": ServerRequestResolvedNotification } | { "method": "item/mcpToolCall/progress", "params": McpToolCallProgressNotification } | { "method": "mcpServer/oauthLogin/completed", "params": McpServerOauthLoginCompletedNotification } | { "method": "account/updated", "params": AccountUpdatedNotification } | { "method": "account/rateLimits/updated", "params": AccountRateLimitsUpdatedNotification } | { "method": "app/list/updated", "params": AppListUpdatedNotification } | { "method": "item/reasoning/summaryTextDelta", "params": ReasoningSummaryTextDeltaNotification } | { "method": "item/reasoning/summaryPartAdded", "params": ReasoningSummaryPartAddedNotification } | { "method": "item/reasoning/textDelta", "params": ReasoningTextDeltaNotification } | { "method": "thread/compacted", "params": ContextCompactedNotification } | { "method": "model/rerouted", "params": ModelReroutedNotification } | { "method": "deprecationNotice", "params": DeprecationNoticeNotification } | { "method": "configWarning", "params": ConfigWarningNotification } | { "method": "fuzzyFileSearch/sessionUpdated", "params": FuzzyFileSearchSessionUpdatedNotification } | { "method": "fuzzyFileSearch/sessionCompleted", "params": FuzzyFileSearchSessionCompletedNotification } | { "method": "thread/realtime/started", "params": ThreadRealtimeStartedNotification } | { "method": "thread/realtime/itemAdded", "params": ThreadRealtimeItemAddedNotification } | { "method": "thread/realtime/outputAudio/delta", "params": ThreadRealtimeOutputAudioDeltaNotification } | { "method": "thread/realtime/error", "params": ThreadRealtimeErrorNotification } | { "method": "thread/realtime/closed", "params": ThreadRealtimeClosedNotification } | { "method": "windows/worldWritableWarning", "params": WindowsWorldWritableWarningNotification } | { "method": "windowsSandbox/setupCompleted", "params": WindowsSandboxSetupCompletedNotification } | { "method": "account/login/completed", "params": AccountLoginCompletedNotification } | { "method": "authStatusChange", "params": AuthStatusChangeNotification } | { "method": "loginChatGptComplete", "params": LoginChatGptCompleteNotification } | { "method": "sessionConfigured", "params": SessionConfiguredNotification };
diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ServerRequestResolvedNotification.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ServerRequestResolvedNotification.ts
new file mode 100644
index 0000000000..56c53cc445
--- /dev/null
+++ b/codex-rs/app-server-protocol/schema/typescript/v2/ServerRequestResolvedNotification.ts
@@ -0,0 +1,6 @@
+// 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 { RequestId } from "../RequestId";
+
+export type ServerRequestResolvedNotification = { threadId: string, requestId: RequestId, };
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 4a881427d9..757eb72893 100644
--- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts
+++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts
@@ -142,6 +142,7 @@ export type { ReviewTarget } from "./ReviewTarget";
export type { SandboxMode } from "./SandboxMode";
export type { SandboxPolicy } from "./SandboxPolicy";
export type { SandboxWorkspaceWrite } from "./SandboxWorkspaceWrite";
+export type { ServerRequestResolvedNotification } from "./ServerRequestResolvedNotification";
export type { SessionSource } from "./SessionSource";
export type { SkillDependencies } from "./SkillDependencies";
export type { SkillErrorInfo } from "./SkillErrorInfo";
diff --git a/codex-rs/app-server-protocol/src/jsonrpc_lite.rs b/codex-rs/app-server-protocol/src/jsonrpc_lite.rs
index 65ffa2a005..372bdbe404 100644
--- a/codex-rs/app-server-protocol/src/jsonrpc_lite.rs
+++ b/codex-rs/app-server-protocol/src/jsonrpc_lite.rs
@@ -8,7 +8,9 @@ use ts_rs::TS;
pub const JSONRPC_VERSION: &str = "2.0";
-#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Hash, Eq, JsonSchema, TS)]
+#[derive(
+ Debug, Clone, PartialEq, PartialOrd, Ord, Deserialize, Serialize, Hash, Eq, JsonSchema, TS,
+)]
#[serde(untagged)]
pub enum RequestId {
String(String),
diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs
index 9a96aefc2e..f63ebd37a2 100644
--- a/codex-rs/app-server-protocol/src/protocol/common.rs
+++ b/codex-rs/app-server-protocol/src/protocol/common.rs
@@ -548,6 +548,14 @@ macro_rules! server_request_definitions {
)*
}
+ impl ServerRequest {
+ pub fn id(&self) -> &RequestId {
+ match self {
+ $(Self::$variant { request_id, .. } => request_id,)*
+ }
+ }
+ }
+
#[derive(Debug, Clone, PartialEq, JsonSchema)]
#[allow(clippy::large_enum_variant)]
pub enum ServerRequestPayload {
@@ -838,6 +846,7 @@ server_notification_definitions! {
CommandExecutionOutputDelta => "item/commandExecution/outputDelta" (v2::CommandExecutionOutputDeltaNotification),
TerminalInteraction => "item/commandExecution/terminalInteraction" (v2::TerminalInteractionNotification),
FileChangeOutputDelta => "item/fileChange/outputDelta" (v2::FileChangeOutputDeltaNotification),
+ ServerRequestResolved => "serverRequest/resolved" (v2::ServerRequestResolvedNotification),
McpToolCallProgress => "item/mcpToolCall/progress" (v2::McpToolCallProgressNotification),
McpServerOauthLoginCompleted => "mcpServer/oauthLogin/completed" (v2::McpServerOauthLoginCompletedNotification),
AccountUpdated => "account/updated" (v2::AccountUpdatedNotification),
@@ -1106,6 +1115,7 @@ mod tests {
);
let payload = ServerRequestPayload::ExecCommandApproval(params);
+ assert_eq!(request.id(), &RequestId::Integer(7));
assert_eq!(payload.request_with_id(RequestId::Integer(7)), request);
Ok(())
}
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 97ad8d521f..111bab84ce 100644
--- a/codex-rs/app-server-protocol/src/protocol/thread_history.rs
+++ b/codex-rs/app-server-protocol/src/protocol/thread_history.rs
@@ -22,6 +22,7 @@ use codex_protocol::models::MessagePhase;
use codex_protocol::protocol::AgentReasoningEvent;
use codex_protocol::protocol::AgentReasoningRawContentEvent;
use codex_protocol::protocol::AgentStatus;
+use codex_protocol::protocol::ApplyPatchApprovalRequestEvent;
use codex_protocol::protocol::CompactedItem;
use codex_protocol::protocol::ContextCompactedEvent;
use codex_protocol::protocol::DynamicToolCallResponseEvent;
@@ -126,6 +127,9 @@ impl ThreadHistoryBuilder {
EventMsg::WebSearchEnd(payload) => self.handle_web_search_end(payload),
EventMsg::ExecCommandBegin(payload) => self.handle_exec_command_begin(payload),
EventMsg::ExecCommandEnd(payload) => self.handle_exec_command_end(payload),
+ EventMsg::ApplyPatchApprovalRequest(payload) => {
+ self.handle_apply_patch_approval_request(payload)
+ }
EventMsg::PatchApplyBegin(payload) => self.handle_patch_apply_begin(payload),
EventMsg::PatchApplyEnd(payload) => self.handle_patch_apply_end(payload),
EventMsg::DynamicToolCallRequest(payload) => {
@@ -364,6 +368,19 @@ impl ThreadHistoryBuilder {
self.upsert_item_in_turn_id(&payload.turn_id, item);
}
+ fn handle_apply_patch_approval_request(&mut self, payload: &ApplyPatchApprovalRequestEvent) {
+ let item = ThreadItem::FileChange {
+ id: payload.call_id.clone(),
+ changes: convert_patch_changes(&payload.changes),
+ status: PatchApplyStatus::InProgress,
+ };
+ if payload.turn_id.is_empty() {
+ self.upsert_item_in_current_turn(item);
+ } else {
+ self.upsert_item_in_turn_id(&payload.turn_id, item);
+ }
+ }
+
fn handle_patch_apply_begin(&mut self, payload: &PatchApplyBeginEvent) {
let item = ThreadItem::FileChange {
id: payload.call_id.clone(),
@@ -1080,6 +1097,7 @@ mod tests {
use codex_protocol::protocol::AgentMessageEvent;
use codex_protocol::protocol::AgentReasoningEvent;
use codex_protocol::protocol::AgentReasoningRawContentEvent;
+ use codex_protocol::protocol::ApplyPatchApprovalRequestEvent;
use codex_protocol::protocol::CodexErrorInfo;
use codex_protocol::protocol::CompactedItem;
use codex_protocol::protocol::DynamicToolCallResponseEvent;
@@ -1088,6 +1106,7 @@ mod tests {
use codex_protocol::protocol::ItemStartedEvent;
use codex_protocol::protocol::McpInvocation;
use codex_protocol::protocol::McpToolCallEndEvent;
+ use codex_protocol::protocol::PatchApplyBeginEvent;
use codex_protocol::protocol::ThreadRolledBackEvent;
use codex_protocol::protocol::TurnAbortReason;
use codex_protocol::protocol::TurnAbortedEvent;
@@ -1980,6 +1999,133 @@ mod tests {
);
}
+ #[test]
+ fn patch_apply_begin_updates_active_turn_snapshot_with_file_change() {
+ let turn_id = "turn-1";
+ let mut builder = ThreadHistoryBuilder::new();
+ let events = vec![
+ EventMsg::TurnStarted(TurnStartedEvent {
+ turn_id: turn_id.to_string(),
+ model_context_window: None,
+ collaboration_mode_kind: Default::default(),
+ }),
+ EventMsg::UserMessage(UserMessageEvent {
+ message: "apply patch".into(),
+ images: None,
+ text_elements: Vec::new(),
+ local_images: Vec::new(),
+ }),
+ EventMsg::PatchApplyBegin(PatchApplyBeginEvent {
+ call_id: "patch-call".into(),
+ turn_id: turn_id.to_string(),
+ auto_approved: false,
+ changes: [(
+ PathBuf::from("README.md"),
+ codex_protocol::protocol::FileChange::Add {
+ content: "hello\n".into(),
+ },
+ )]
+ .into_iter()
+ .collect(),
+ }),
+ ];
+
+ for event in &events {
+ builder.handle_event(event);
+ }
+
+ let snapshot = builder
+ .active_turn_snapshot()
+ .expect("active turn snapshot");
+ assert_eq!(snapshot.id, turn_id);
+ assert_eq!(snapshot.status, TurnStatus::InProgress);
+ assert_eq!(
+ snapshot.items,
+ vec![
+ ThreadItem::UserMessage {
+ id: "item-1".into(),
+ content: vec![UserInput::Text {
+ text: "apply patch".into(),
+ text_elements: Vec::new(),
+ }],
+ },
+ ThreadItem::FileChange {
+ id: "patch-call".into(),
+ changes: vec![FileUpdateChange {
+ path: "README.md".into(),
+ kind: PatchChangeKind::Add,
+ diff: "hello\n".into(),
+ }],
+ status: PatchApplyStatus::InProgress,
+ },
+ ]
+ );
+ }
+
+ #[test]
+ fn apply_patch_approval_request_updates_active_turn_snapshot_with_file_change() {
+ let turn_id = "turn-1";
+ let mut builder = ThreadHistoryBuilder::new();
+ let events = vec![
+ EventMsg::TurnStarted(TurnStartedEvent {
+ turn_id: turn_id.to_string(),
+ model_context_window: None,
+ collaboration_mode_kind: Default::default(),
+ }),
+ EventMsg::UserMessage(UserMessageEvent {
+ message: "apply patch".into(),
+ images: None,
+ text_elements: Vec::new(),
+ local_images: Vec::new(),
+ }),
+ EventMsg::ApplyPatchApprovalRequest(ApplyPatchApprovalRequestEvent {
+ call_id: "patch-call".into(),
+ turn_id: turn_id.to_string(),
+ changes: [(
+ PathBuf::from("README.md"),
+ codex_protocol::protocol::FileChange::Add {
+ content: "hello\n".into(),
+ },
+ )]
+ .into_iter()
+ .collect(),
+ reason: None,
+ grant_root: None,
+ }),
+ ];
+
+ for event in &events {
+ builder.handle_event(event);
+ }
+
+ let snapshot = builder
+ .active_turn_snapshot()
+ .expect("active turn snapshot");
+ assert_eq!(snapshot.id, turn_id);
+ assert_eq!(snapshot.status, TurnStatus::InProgress);
+ assert_eq!(
+ snapshot.items,
+ vec![
+ ThreadItem::UserMessage {
+ id: "item-1".into(),
+ content: vec![UserInput::Text {
+ text: "apply patch".into(),
+ text_elements: Vec::new(),
+ }],
+ },
+ ThreadItem::FileChange {
+ id: "patch-call".into(),
+ changes: vec![FileUpdateChange {
+ path: "README.md".into(),
+ kind: PatchChangeKind::Add,
+ diff: "hello\n".into(),
+ }],
+ status: PatchApplyStatus::InProgress,
+ },
+ ]
+ );
+ }
+
#[test]
fn late_turn_complete_does_not_close_active_turn() {
let events = vec![
diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs
index 8052eb080e..c3d2cef29b 100644
--- a/codex-rs/app-server-protocol/src/protocol/v2.rs
+++ b/codex-rs/app-server-protocol/src/protocol/v2.rs
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::path::PathBuf;
+use crate::RequestId;
use crate::protocol::common::AuthMode;
use codex_experimental_api_macros::ExperimentalApi;
use codex_protocol::account::PlanType;
@@ -3745,6 +3746,14 @@ pub struct FileChangeOutputDeltaNotification {
pub delta: String,
}
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
+#[serde(rename_all = "camelCase")]
+#[ts(export_to = "v2/")]
+pub struct ServerRequestResolvedNotification {
+ pub thread_id: String,
+ pub request_id: RequestId,
+}
+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md
index 1e5c37d286..b7341801de 100644
--- a/codex-rs/app-server/README.md
+++ b/codex-rs/app-server/README.md
@@ -712,7 +712,8 @@ Order of messages:
1. `item/started` — shows the pending `commandExecution` item with `command`, `cwd`, and other fields so you can render the proposed action.
2. `item/commandExecution/requestApproval` (request) — carries the same `itemId`, `threadId`, `turnId`, optionally `approvalId` (for subcommand callbacks), and `reason`. For normal command approvals, it also includes `command`, `cwd`, and `commandActions` for friendly display. When `initialize.params.capabilities.experimentalApi = true`, it may also include experimental `additionalPermissions` describing requested per-command sandbox access; any filesystem paths in that payload are absolute on the wire. For network-only approvals, those command fields may be omitted and `networkApprovalContext` is provided instead. Optional persistence hints may also be included via `proposedExecpolicyAmendment` and `proposedNetworkPolicyAmendments`. Clients can prefer `availableDecisions` when present to render the exact set of choices the server wants to expose, while still falling back to the older heuristics if it is omitted.
3. Client response — for example `{ "decision": "accept" }`, `{ "decision": "acceptForSession" }`, `{ "decision": { "acceptWithExecpolicyAmendment": { "execpolicy_amendment": [...] } } }`, `{ "decision": { "applyNetworkPolicyAmendment": { "network_policy_amendment": { "host": "example.com", "action": "allow" } } } }`, `{ "decision": "decline" }`, or `{ "decision": "cancel" }`.
-4. `item/completed` — final `commandExecution` item with `status: "completed" | "failed" | "declined"` and execution output. Render this as the authoritative result.
+4. `serverRequest/resolved` — `{ threadId, requestId }` confirms the pending request has been resolved or cleared, including lifecycle cleanup on turn start/complete/interrupt.
+5. `item/completed` — final `commandExecution` item with `status: "completed" | "failed" | "declined"` and execution output. Render this as the authoritative result.
### File change approvals
@@ -721,10 +722,15 @@ Order of messages:
1. `item/started` — emits a `fileChange` item with `changes` (diff chunk summaries) and `status: "inProgress"`. Show the proposed edits and paths to the user.
2. `item/fileChange/requestApproval` (request) — includes `itemId`, `threadId`, `turnId`, and an optional `reason`.
3. Client response — `{ "decision": "accept" }` or `{ "decision": "decline" }`.
-4. `item/completed` — returns the same `fileChange` item with `status` updated to `completed`, `failed`, or `declined` after the patch attempt. Rely on this to show success/failure and finalize the diff state in your UI.
+4. `serverRequest/resolved` — `{ threadId, requestId }` confirms the pending request has been resolved or cleared, including lifecycle cleanup on turn start/complete/interrupt.
+5. `item/completed` — returns the same `fileChange` item with `status` updated to `completed`, `failed`, or `declined` after the patch attempt. Rely on this to show success/failure and finalize the diff state in your UI.
UI guidance for IDEs: surface an approval dialog as soon as the request arrives. The turn will proceed after the server receives a response to the approval request. The terminal `item/completed` notification will be sent with the appropriate status.
+### request_user_input
+
+When the client responds to `item/tool/requestUserInput`, the server emits `serverRequest/resolved` with `{ threadId, requestId }`. If the pending request is cleared by turn start, turn completion, or turn interruption before the client answers, the server emits the same notification for that cleanup.
+
### Dynamic tool calls (experimental)
`dynamicTools` on `thread/start` and the corresponding `item/tool/call` request/response flow are experimental APIs. To enable them, set `initialize.params.capabilities.experimentalApi = true`.
diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs
index 0bc2f1118f..35725e7a89 100644
--- a/codex-rs/app-server/src/bespoke_event_handling.rs
+++ b/codex-rs/app-server/src/bespoke_event_handling.rs
@@ -6,6 +6,8 @@ use crate::error_code::INTERNAL_ERROR_CODE;
use crate::error_code::INVALID_REQUEST_ERROR_CODE;
use crate::outgoing_message::ClientRequestResult;
use crate::outgoing_message::ThreadScopedOutgoingMessageSender;
+use crate::server_request_error::is_turn_transition_server_request_error;
+use crate::thread_state::ThreadListenerCommand;
use crate::thread_state::ThreadState;
use crate::thread_state::TurnSummary;
use crate::thread_status::ThreadWatchActiveGuard;
@@ -56,6 +58,7 @@ use codex_app_server_protocol::RawResponseItemCompletedNotification;
use codex_app_server_protocol::ReasoningSummaryPartAddedNotification;
use codex_app_server_protocol::ReasoningSummaryTextDeltaNotification;
use codex_app_server_protocol::ReasoningTextDeltaNotification;
+use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ServerRequestPayload;
use codex_app_server_protocol::TerminalInteractionNotification;
@@ -132,6 +135,38 @@ struct CommandExecutionCompletionItem {
command_actions: Vec<V2ParsedCommand>,
}
+async fn resolve_server_request_on_thread_listener(
+ thread_state: &Arc<Mutex<ThreadState>>,
+ request_id: RequestId,
+) {
+ let (completion_tx, completion_rx) = oneshot::channel();
+ let listener_command_tx = {
+ let state = thread_state.lock().await;
+ state.listener_command_tx()
+ };
+ let Some(listener_command_tx) = listener_command_tx else {
+ error!("failed to remove pending client request: thread listener is not running");
+ return;
+ };
+
+ if listener_command_tx
+ .send(ThreadListenerCommand::ResolveServerRequest {
+ request_id,
+ completion_tx,
+ })
+ .is_err()
+ {
+ error!(
+ "failed to remove pending client request: thread listener command channel is closed"
+ );
+ return;
+ }
+
+ if let Err(err) = completion_rx.await {
+ error!("failed to remove pending client request: {err}");
+ }
+}
+
#[allow(clippy::too_many_arguments)]
pub(crate) async fn apply_bespoke_event_handling(
event: Event,
@@ -151,11 +186,15 @@ pub(crate) async fn apply_bespoke_event_handling(
} = event;
match msg {
EventMsg::TurnStarted(_) => {
+ // While not technically necessary as it was already done on TurnComplete, be extra cautios and abort any pending server requests.
+ outgoing.abort_pending_server_requests().await;
thread_watch_manager
.note_turn_started(&conversation_id.to_string())
.await;
}
EventMsg::TurnComplete(_ev) => {
+ // All per-thread requests are bound to a turn, so abort them.
+ outgoing.abort_pending_server_requests().await;
let turn_failed = thread_state.lock().await.turn_summary.last_error.is_some();
thread_watch_manager
.note_turn_completed(&conversation_id.to_string(), turn_failed)
@@ -263,7 +302,7 @@ pub(crate) async fn apply_bespoke_event_handling(
reason,
grant_root,
};
- let rx = outgoing
+ let (_pending_request_id, rx) = outgoing
.send_request(ServerRequestPayload::ApplyPatchApproval(params))
.await;
tokio::spawn(async move {
@@ -307,7 +346,7 @@ pub(crate) async fn apply_bespoke_event_handling(
reason,
grant_root,
};
- let rx = outgoing
+ let (pending_request_id, rx) = outgoing
.send_request(ServerRequestPayload::FileChangeRequestApproval(params))
.await;
tokio::spawn(async move {
@@ -316,6 +355,7 @@ pub(crate) async fn apply_bespoke_event_handling(
conversation_id,
item_id,
patch_changes,
+ pending_request_id,
rx,
conversation,
outgoing,
@@ -362,7 +402,7 @@ pub(crate) async fn apply_bespoke_event_handling(
reason,
parsed_cmd,
};
- let rx = outgoing
+ let (_pending_request_id, rx) = outgoing
.send_request(ServerRequestPayload::ExecCommandApproval(params))
.await;
tokio::spawn(async move {
@@ -435,7 +475,7 @@ pub(crate) async fn apply_bespoke_event_handling(
proposed_network_policy_amendments: proposed_network_policy_amendments_v2,
available_decisions: Some(available_decisions),
};
- let rx = outgoing
+ let (pending_request_id, rx) = outgoing
.send_request(ServerRequestPayload::CommandExecutionRequestApproval(
params,
))
@@ -447,6 +487,7 @@ pub(crate) async fn apply_bespoke_event_handling(
approval_id,
call_id,
completion_item,
+ pending_request_id,
rx,
conversation,
outgoing,
@@ -489,14 +530,16 @@ pub(crate) async fn apply_bespoke_event_handling(
item_id: request.call_id,
questions,
};
- let rx = outgoing
+ let (pending_request_id, rx) = outgoing
.send_request(ServerRequestPayload::ToolRequestUserInput(params))
.await;
tokio::spawn(async move {
on_request_user_input_response(
event_turn_id,
+ pending_request_id,
rx,
conversation,
+ thread_state,
user_input_guard,
)
.await;
@@ -550,7 +593,7 @@ pub(crate) async fn apply_bespoke_event_handling(
tool: tool.clone(),
arguments: arguments.clone(),
};
- let rx = outgoing
+ let (_pending_request_id, rx) = outgoing
.send_request(ServerRequestPayload::DynamicToolCall(params))
.await;
tokio::spawn(async move {
@@ -1136,6 +1179,7 @@ pub(crate) async fn apply_bespoke_event_handling(
// Until we migrate the core to be aware of a first class FileChangeItem
// and emit the corresponding EventMsg, we repurpose the call_id as the item_id.
let item_id = patch_begin_event.call_id.clone();
+ let changes = convert_patch_changes(&patch_begin_event.changes);
let first_start = {
let mut state = thread_state.lock().await;
@@ -1147,7 +1191,7 @@ pub(crate) async fn apply_bespoke_event_handling(
if first_start {
let item = ThreadItem::FileChange {
id: item_id.clone(),
- changes: convert_patch_changes(&patch_begin_event.changes),
+ changes,
status: PatchApplyStatus::InProgress,
};
let notification = ItemStartedNotification {
@@ -1329,6 +1373,8 @@ pub(crate) async fn apply_bespoke_event_handling(
}
// If this is a TurnAborted, reply to any pending interrupt requests.
EventMsg::TurnAborted(turn_aborted_event) => {
+ // All per-thread requests are bound to a turn, so abort them.
+ outgoing.abort_pending_server_requests().await;
let pending = {
let mut state = thread_state.lock().await;
std::mem::take(&mut state.pending_interrupts)
@@ -1725,6 +1771,7 @@ async fn on_patch_approval_response(
let response = receiver.await;
let value = match response {
Ok(Ok(value)) => value,
+ Ok(Err(err)) if is_turn_transition_server_request_error(&err) => return,
Ok(Err(err)) => {
error!("request failed with client error: {err:?}");
if let Err(submit_err) = codex
@@ -1781,6 +1828,7 @@ async fn on_exec_approval_response(
let response = receiver.await;
let value = match response {
Ok(Ok(value)) => value,
+ Ok(Err(err)) if is_turn_transition_server_request_error(&err) => return,
Ok(Err(err)) => {
error!("request failed with client error: {err:?}");
return;
@@ -1816,14 +1864,18 @@ async fn on_exec_approval_response(
async fn on_request_user_input_response(
event_turn_id: String,
+ pending_request_id: RequestId,
receiver: oneshot::Receiver<ClientRequestResult>,
conversation: Arc<CodexThread>,
+ thread_state: Arc<Mutex<ThreadState>>,
user_input_guard: ThreadWatchActiveGuard,
) {
let response = receiver.await;
+ resolve_server_request_on_thread_listener(&thread_state, pending_request_id).await;
drop(user_input_guard);
let value = match response {
Ok(Ok(value)) => value,
+ Ok(Err(err)) if is_turn_transition_server_request_error(&err) => return,
Ok(Err(err)) => {
error!("request failed with client error: {err:?}");
let empty = CoreRequestUserInputResponse {
@@ -1934,6 +1986,7 @@ async fn on_file_change_request_approval_response(
conversation_id: ThreadId,
item_id: String,
changes: Vec<FileUpdateChange>,
+ pending_request_id: RequestId,
receiver: oneshot::Receiver<ClientRequestResult>,
codex: Arc<CodexThread>,
outgoing: ThreadScopedOutgoingMessageSender,
@@ -1941,6 +1994,7 @@ async fn on_file_change_request_approval_response(
permission_guard: ThreadWatchActiveGuard,
) {
let response = receiver.await;
+ resolve_server_request_on_thread_listener(&thread_state, pending_request_id).await;
drop(permission_guard);
let (decision, completion_status) = match response {
Ok(Ok(value)) => {
@@ -1958,6 +2012,7 @@ async fn on_file_change_request_approval_response(
// Only short-circuit on declines/cancels/failures.
(decision, completion_status)
}
+ Ok(Err(err)) if is_turn_transition_server_request_error(&err) => return,
Ok(Err(err)) => {
error!("request failed with client error: {err:?}");
(ReviewDecision::Denied, Some(PatchApplyStatus::Failed))
@@ -1999,6 +2054,7 @@ async fn on_command_execution_request_approval_response(
approval_id: Option<String>,
item_id: String,
completion_item: Option<CommandExecutionCompletionItem>,
+ pending_request_id: RequestId,
receiver: oneshot::Receiver<ClientRequestResult>,
conversation: Arc<CodexThread>,
outgoing: ThreadScopedOutgoingMessageSender,
@@ -2006,6 +2062,7 @@ async fn on_command_execution_request_approval_response(
permission_guard: ThreadWatchActiveGuard,
) {
let response = receiver.await;
+ resolve_server_request_on_thread_listener(&thread_state, pending_request_id).await;
drop(permission_guard);
let (decision, completion_status) = match response {
Ok(Ok(value)) => {
@@ -2057,6 +2114,7 @@ async fn on_command_execution_request_approval_response(
};
(decision, completion_status)
}
+ Ok(Err(err)) if is_turn_transition_server_request_error(&err) => return,
Ok(Err(err)) => {
error!("request failed with client error: {err:?}");
(ReviewDecision::Denied, Some(CommandExecutionStatus::Failed))
diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs
index 1f6e1ac0f1..959cc92520 100644
--- a/codex-rs/app-server/src/codex_message_processor.rs
+++ b/codex-rs/app-server/src/codex_message_processor.rs
@@ -100,6 +100,7 @@ use codex_app_server_protocol::NewConversationResponse;
use codex_app_server_protocol::ProductSurface as ApiProductSurface;
use codex_app_server_protocol::RemoveConversationListenerParams;
use codex_app_server_protocol::RemoveConversationSubscriptionResponse;
+use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ResumeConversationParams;
use codex_app_server_protocol::ResumeConversationResponse;
use codex_app_server_protocol::ReviewDelivery as ApiReviewDelivery;
@@ -112,6 +113,7 @@ use codex_app_server_protocol::SendUserMessageResponse;
use codex_app_server_protocol::SendUserTurnParams;
use codex_app_server_protocol::SendUserTurnResponse;
use codex_app_server_protocol::ServerNotification;
+use codex_app_server_protocol::ServerRequestResolvedNotification;
use codex_app_server_protocol::SessionConfiguredNotification;
use codex_app_server_protocol::SetDefaultModelParams;
use codex_app_server_protocol::SetDefaultModelResponse;
@@ -297,8 +299,12 @@ use tracing::info;
use tracing::warn;
use uuid::Uuid;
+#[cfg(test)]
+use codex_app_server_protocol::ServerRequest;
+
use crate::filters::compute_source_filters;
use crate::filters::source_kind_matches;
+use crate::thread_state::ThreadListenerCommand;
use crate::thread_state::ThreadState;
use crate::thread_state::ThreadStateManager;
@@ -3221,11 +3227,11 @@ impl CodexMessageProcessor {
};
let command = crate::thread_state::ThreadListenerCommand::SendThreadResumeResponse(
- crate::thread_state::PendingThreadResumeRequest {
+ Box::new(crate::thread_state::PendingThreadResumeRequest {
request_id: request_id.clone(),
rollout_path,
config_snapshot,
- },
+ }),
);
if listener_command_tx.send(command).is_err() {
let err = JSONRPCErrorError {
@@ -4844,7 +4850,9 @@ impl CodexMessageProcessor {
async fn finalize_thread_teardown(&mut self, thread_id: ThreadId) {
self.pending_thread_unloads.lock().await.remove(&thread_id);
- self.outgoing.cancel_requests_for_thread(thread_id).await;
+ self.outgoing
+ .cancel_requests_for_thread(thread_id, None)
+ .await;
self.thread_state_manager
.remove_thread_state(thread_id)
.await;
@@ -4905,7 +4913,9 @@ impl CodexMessageProcessor {
self.pending_thread_unloads.lock().await.insert(thread_id);
// Any pending app-server -> client requests for this thread can no longer be
// answered; cancel their callbacks before shutdown/unload.
- self.outgoing.cancel_requests_for_thread(thread_id).await;
+ self.outgoing
+ .cancel_requests_for_thread(thread_id, None)
+ .await;
self.thread_state_manager
.remove_thread_state(thread_id)
.await;
@@ -6507,21 +6517,15 @@ impl CodexMessageProcessor {
let Some(listener_command) = listener_command else {
break;
};
- match listener_command {
- crate::thread_state::ThreadListenerCommand::SendThreadResumeResponse(
- resume_request,
- ) => {
- handle_pending_thread_resume_request(
- conversation_id,
- codex_home.as_path(),
- &thread_state,
- &thread_watch_manager,
- &outgoing_for_task,
- resume_request,
- )
- .await;
- }
- }
+ handle_thread_listener_command(
+ conversation_id,
+ codex_home.as_path(),
+ &thread_state,
+ &thread_watch_manager,
+ &outgoing_for_task,
+ listener_command,
+ )
+ .await;
}
}
}
@@ -6830,6 +6834,37 @@ impl CodexMessageProcessor {
}
}
+async fn handle_thread_listener_command(
+ conversation_id: ThreadId,
+ codex_home: &Path,
+ thread_state: &Arc<Mutex<ThreadState>>,
+ thread_watch_manager: &ThreadWatchManager,
+ outgoing: &Arc<OutgoingMessageSender>,
+ listener_command: ThreadListenerCommand,
+) {
+ match listener_command {
+ ThreadListenerCommand::SendThreadResumeResponse(resume_request) => {
+ handle_pending_thread_resume_request(
+ conversation_id,
+ codex_home,
+ thread_state,
+ thread_watch_manager,
+ outgoing,
+ *resume_request,
+ )
+ .await;
+ }
+ ThreadListenerCommand::ResolveServerRequest {
+ request_id,
+ completion_tx,
+ } => {
+ resolve_pending_server_request(conversation_id, thread_state, outgoing, request_id)
+ .await;
+ let _ = completion_tx.send(());
+ }
+ }
+}
+
async fn handle_pending_thread_resume_request(
conversation_id: ThreadId,
codex_home: &Path,
@@ -6918,9 +6953,36 @@ async fn handle_pending_thread_resume_request(
reasoning_effort,
};
outgoing.send_response(request_id, response).await;
+ outgoing
+ .replay_requests_to_connection_for_thread(connection_id, conversation_id)
+ .await;
+
thread_state.lock().await.add_connection(connection_id);
}
+async fn resolve_pending_server_request(
+ conversation_id: ThreadId,
+ thread_state: &Arc<Mutex<ThreadState>>,
+ outgoing: &Arc<OutgoingMessageSender>,
+ request_id: RequestId,
+) {
+ let thread_id = conversation_id.to_string();
+ let subscribed_connection_ids = thread_state.lock().await.subscribed_connection_ids();
+ let outgoing = ThreadScopedOutgoingMessageSender::new(
+ outgoing.clone(),
+ subscribed_connection_ids,
+ conversation_id,
+ );
+ outgoing
+ .send_server_notification(ServerNotification::ServerRequestResolved(
+ ServerRequestResolvedNotification {
+ thread_id,
+ request_id,
+ },
+ ))
+ .await;
+}
+
async fn load_thread_for_running_resume_response(
conversation_id: ThreadId,
rollout_path: &Path,
@@ -7668,7 +7730,11 @@ pub(crate) fn summary_to_thread(summary: ConversationSummary) -> Thread {
#[cfg(test)]
mod tests {
use super::*;
+ use crate::outgoing_message::OutgoingEnvelope;
+ use crate::outgoing_message::OutgoingMessage;
use anyhow::Result;
+ use codex_app_server_protocol::ServerRequestPayload;
+ use codex_app_server_protocol::ToolRequestUserInputParams;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use pretty_assertions::assert_eq;
@@ -7862,6 +7928,67 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn aborting_pending_request_clears_pending_state() -> Result<()> {
+ let thread_id = ThreadId::from_string("bfd12a78-5900-467b-9bc5-d3d35df08191")?;
+ let thread_state = Arc::new(Mutex::new(ThreadState::default()));
+ let connection_id = ConnectionId(7);
+ thread_state.lock().await.add_connection(connection_id);
+
+ let (outgoing_tx, mut outgoing_rx) = tokio::sync::mpsc::channel(8);
+ let outgoing = Arc::new(OutgoingMessageSender::new(outgoing_tx));
+ let thread_outgoing = ThreadScopedOutgoingMessageSender::new(
+ outgoing.clone(),
+ vec![connection_id],
+ thread_id,
+ );
+
+ let (request_id, client_request_rx) = thread_outgoing
+ .send_request(ServerRequestPayload::ToolRequestUserInput(
+ ToolRequestUserInputParams {
+ thread_id: thread_id.to_string(),
+ turn_id: "turn-1".to_string(),
+ item_id: "call-1".to_string(),
+ questions: vec![],
+ },
+ ))
+ .await;
+ thread_outgoing.abort_pending_server_requests().await;
+
+ let request_message = outgoing_rx.recv().await.expect("request should be sent");
+ let OutgoingEnvelope::ToConnection {
+ connection_id: request_connection_id,
+ message:
+ OutgoingMessage::Request(ServerRequest::ToolRequestUserInput {
+ request_id: sent_request_id,
+ ..
+ }),
+ } = request_message
+ else {
+ panic!("expected tool request to be sent to the subscribed connection");
+ };
+ assert_eq!(request_connection_id, connection_id);
+ assert_eq!(sent_request_id, request_id);
+
+ let response = client_request_rx
+ .await
+ .expect("callback should be resolved");
+ let error = response.expect_err("request should be aborted during cleanup");
+ assert_eq!(
+ error.message,
+ "client request resolved because the turn state was changed"
+ );
+ assert_eq!(error.data, Some(json!({ "reason": "turnTransition" })));
+ assert!(
+ outgoing
+ .pending_requests_for_thread(thread_id)
+ .await
+ .is_empty()
+ );
+ assert!(outgoing_rx.try_recv().is_err());
+ Ok(())
+ }
+
#[test]
fn summary_from_state_db_metadata_preserves_agent_nickname() -> Result<()> {
let conversation_id = ThreadId::from_string("bfd12a78-5900-467b-9bc5-d3d35df08191")?;
diff --git a/codex-rs/app-server/src/dynamic_tools.rs b/codex-rs/app-server/src/dynamic_tools.rs
index 522bcfdcac..c5e7550d9f 100644
--- a/codex-rs/app-server/src/dynamic_tools.rs
+++ b/codex-rs/app-server/src/dynamic_tools.rs
@@ -9,6 +9,7 @@ use tokio::sync::oneshot;
use tracing::error;
use crate::outgoing_message::ClientRequestResult;
+use crate::server_request_error::is_turn_transition_server_request_error;
pub(crate) async fn on_call_response(
call_id: String,
@@ -18,6 +19,7 @@ pub(crate) async fn on_call_response(
let response = receiver.await;
let (response, _error) = match response {
Ok(Ok(value)) => decode_response(value),
+ Ok(Err(err)) if is_turn_transition_server_request_error(&err) => return,
Ok(Err(err)) => {
error!("request failed with client error: {err:?}");
fallback_response("dynamic tool request failed")
diff --git a/codex-rs/app-server/src/lib.rs b/codex-rs/app-server/src/lib.rs
index 972f5af1e6..dcc438e292 100644
--- a/codex-rs/app-server/src/lib.rs
+++ b/codex-rs/app-server/src/lib.rs
@@ -63,6 +63,7 @@ mod fuzzy_file_search;
mod message_processor;
mod models;
mod outgoing_message;
+mod server_request_error;
mod thread_state;
mod thread_status;
mod transport;
diff --git a/codex-rs/app-server/src/outgoing_message.rs b/codex-rs/app-server/src/outgoing_message.rs
index 44de783455..d6605ebe59 100644
--- a/codex-rs/app-server/src/outgoing_message.rs
+++ b/codex-rs/app-server/src/outgoing_message.rs
@@ -17,6 +17,7 @@ use tokio::sync::oneshot;
use tracing::warn;
use crate::error_code::INTERNAL_ERROR_CODE;
+use crate::server_request_error::TURN_TRANSITION_PENDING_REQUEST_ERROR_REASON;
#[cfg(test)]
use codex_protocol::account::PlanType;
@@ -62,6 +63,7 @@ pub(crate) struct ThreadScopedOutgoingMessageSender {
struct PendingCallbackEntry {
callback: oneshot::Sender<ClientRequestResult>,
thread_id: Option<ThreadId>,
+ request: ServerRequest,
}
impl ThreadScopedOutgoingMessageSender {
@@ -80,12 +82,12 @@ impl ThreadScopedOutgoingMessageSender {
pub(crate) async fn send_request(
&self,
payload: ServerRequestPayload,
- ) -> oneshot::Receiver<ClientRequestResult> {
+ ) -> (RequestId, oneshot::Receiver<ClientRequestResult>) {
self.outgoing
- .send_request_to_thread_connections(
- self.thread_id,
- self.connection_ids.as_slice(),
+ .send_request_to_connections(
+ Some(self.connection_ids.as_slice()),
payload,
+ Some(self.thread_id),
)
.await
}
@@ -99,6 +101,20 @@ impl ThreadScopedOutgoingMessageSender {
.await;
}
+ pub(crate) async fn abort_pending_server_requests(&self) {
+ self.outgoing
+ .cancel_requests_for_thread(
+ self.thread_id,
+ Some(JSONRPCErrorError {
+ code: INTERNAL_ERROR_CODE,
+ message: "client request resolved because the turn state was changed"
+ .to_string(),
+ data: Some(serde_json::json!({ "reason": TURN_TRANSITION_PENDING_REQUEST_ERROR_REASON })),
+ }),
+ )
+ .await
+ }
+
pub(crate) async fn send_response<T: Serialize>(
&self,
request_id: ConnectionRequestId,
@@ -129,38 +145,23 @@ impl OutgoingMessageSender {
&self,
request: ServerRequestPayload,
) -> (RequestId, oneshot::Receiver<ClientRequestResult>) {
- self.send_request_with_id_to_connections(&[], request, None)
- .await
- }
-
- async fn send_request_to_thread_connections(
- &self,
- thread_id: ThreadId,
- connection_ids: &[ConnectionId],
- request: ServerRequestPayload,
- ) -> oneshot::Receiver<ClientRequestResult> {
- if connection_ids.is_empty() {
- let (_tx, rx) = oneshot::channel();
- return rx;
- }
- let (_request_id, receiver) = self
- .send_request_with_id_to_connections(connection_ids, request, Some(thread_id))
- .await;
- receiver
+ self.send_request_to_connections(None, request, None).await
}
fn next_request_id(&self) -> RequestId {
RequestId::Integer(self.next_server_request_id.fetch_add(1, Ordering::Relaxed))
}
- async fn send_request_with_id_to_connections(
+ async fn send_request_to_connections(
&self,
- connection_ids: &[ConnectionId],
+ connection_ids: Option<&[ConnectionId]>,
request: ServerRequestPayload,
thread_id: Option<ThreadId>,
) -> (RequestId, oneshot::Receiver<ClientRequestResult>) {
let id = self.next_request_id();
let outgoing_message_id = id.clone();
+ let request = request.request_with_id(outgoing_message_id.clone());
+
let (tx_approve, rx_approve) = oneshot::channel();
{
let mut request_id_to_callback = self.request_id_to_callback.lock().await;
@@ -169,36 +170,39 @@ impl OutgoingMessageSender {
PendingCallbackEntry {
callback: tx_approve,
thread_id,
+ request: request.clone(),
},
);
}
- let outgoing_message =
- OutgoingMessage::Request(request.request_with_id(outgoing_message_id.clone()));
- let send_result = if connection_ids.is_empty() {
- self.sender
- .send(OutgoingEnvelope::Broadcast {
- message: outgoing_message,
- })
- .await
- } else {
- let mut send_error = None;
- for connection_id in connection_ids {
- if let Err(err) = self
- .sender
- .send(OutgoingEnvelope::ToConnection {
- connection_id: *connection_id,
- message: outgoing_message.clone(),
+ let outgoing_message = OutgoingMessage::Request(request);
+ let send_result = match connection_ids {
+ None => {
+ self.sender
+ .send(OutgoingEnvelope::Broadcast {
+ message: outgoing_message,
})
.await
- {
- send_error = Some(err);
- break;
- }
}
- match send_error {
- Some(err) => Err(err),
- None => Ok(()),
+ Some(connection_ids) => {
+ let mut send_error = None;
+ for connection_id in connection_ids {
+ if let Err(err) = self
+ .sender
+ .send(OutgoingEnvelope::ToConnection {
+ connection_id: *connection_id,
+ message: outgoing_message.clone(),
+ })
+ .await
+ {
+ send_error = Some(err);
+ break;
+ }
+ }
+ match send_error {
+ Some(err) => Err(err),
+ None => Ok(()),
+ }
}
};
@@ -210,11 +214,28 @@ impl OutgoingMessageSender {
(outgoing_message_id, rx_approve)
}
+ pub(crate) async fn replay_requests_to_connection_for_thread(
+ &self,
+ connection_id: ConnectionId,
+ thread_id: ThreadId,
+ ) {
+ let requests = self.pending_requests_for_thread(thread_id).await;
+ for request in requests {
+ if let Err(err) = self
+ .sender
+ .send(OutgoingEnvelope::ToConnection {
+ connection_id,
+ message: OutgoingMessage::Request(request),
+ })
+ .await
+ {
+ warn!("failed to resend request to client: {err:?}");
+ }
+ }
+ }
+
pub(crate) async fn notify_client_response(&self, id: RequestId, result: Result) {
- let entry = {
- let mut request_id_to_callback = self.request_id_to_callback.lock().await;
- request_id_to_callback.remove_entry(&id)
- };
+ let entry = self.take_request_callback(&id).await;
match entry {
Some((id, entry)) => {
@@ -229,10 +250,7 @@ impl OutgoingMessageSender {
}
pub(crate) async fn notify_client_error(&self, id: RequestId, error: JSONRPCErrorError) {
- let entry = {
- let mut request_id_to_callback = self.request_id_to_callback.lock().await;
- request_id_to_callback.remove_entry(&id)
- };
+ let entry = self.take_request_callback(&id).await;
match entry {
Some((id, entry)) => {
@@ -248,23 +266,62 @@ impl OutgoingMessageSender {
}
pub(crate) async fn cancel_request(&self, id: &RequestId) -> bool {
- let entry = {
- let mut request_id_to_callback = self.request_id_to_callback.lock().await;
- request_id_to_callback.remove_entry(id)
- };
- entry.is_some()
+ self.take_request_callback(id).await.is_some()
}
- pub(crate) async fn cancel_requests_for_thread(&self, thread_id: ThreadId) {
+ async fn take_request_callback(
+ &self,
+ id: &RequestId,
+ ) -> Option<(RequestId, PendingCallbackEntry)> {
let mut request_id_to_callback = self.request_id_to_callback.lock().await;
- let request_ids = request_id_to_callback
+ request_id_to_callback.remove_entry(id)
+ }
+
+ pub(crate) async fn pending_requests_for_thread(
+ &self,
+ thread_id: ThreadId,
+ ) -> Vec<ServerRequest> {
+ let request_id_to_callback = self.request_id_to_callback.lock().await;
+ let mut requests = request_id_to_callback
.iter()
- .filter_map(|(request_id, entry)| {
- (entry.thread_id == Some(thread_id)).then_some(request_id.clone())
+ .filter_map(|(_, entry)| {
+ (entry.thread_id == Some(thread_id)).then_some(entry.request.clone())
})
.collect::<Vec<_>>();
- for request_id in request_ids {
- request_id_to_callback.remove(&request_id);
+ requests.sort_by(|left, right| left.id().cmp(right.id()));
+ requests
+ }
+
+ pub(crate) async fn cancel_requests_for_thread(
+ &self,
+ thread_id: ThreadId,
+ error: Option<JSONRPCErrorError>,
+ ) {
+ let entries = {
+ let mut request_id_to_callback = self.request_id_to_callback.lock().await;
+ let request_ids = request_id_to_callback
+ .iter()
+ .filter_map(|(request_id, entry)| {
+ (entry.thread_id == Some(thread_id)).then_some(request_id.clone())
+ })
+ .collect::<Vec<_>>();
+
+ let mut entries = Vec::with_capacity(request_ids.len());
+ for request_id in request_ids {
+ if let Some(entry) = request_id_to_callback.remove(&request_id) {
+ entries.push(entry);
+ }
+ }
+ entries
+ };
+
+ if let Some(error) = error {
+ for entry in entries {
+ if let Err(err) = entry.callback.send(Err(error.clone())) {
+ let request_id = entry.request.id();
+ warn!("could not notify callback for {request_id:?} due to: {err:?}",);
+ }
+ }
}
}
@@ -441,14 +498,18 @@ mod tests {
use codex_app_server_protocol::ApplyPatchApprovalParams;
use codex_app_server_protocol::AuthMode;
use codex_app_server_protocol::ConfigWarningNotification;
+ use codex_app_server_protocol::DynamicToolCallParams;
+ use codex_app_server_protocol::FileChangeRequestApprovalParams;
use codex_app_server_protocol::LoginChatGptCompleteNotification;
use codex_app_server_protocol::ModelRerouteReason;
use codex_app_server_protocol::ModelReroutedNotification;
use codex_app_server_protocol::RateLimitSnapshot;
use codex_app_server_protocol::RateLimitWindow;
+ use codex_app_server_protocol::ToolRequestUserInputParams;
use codex_protocol::ThreadId;
use pretty_assertions::assert_eq;
use serde_json::json;
+ use std::sync::Arc;
use tokio::time::timeout;
use uuid::Uuid;
@@ -723,4 +784,121 @@ mod tests {
.expect("waiter should receive a callback");
assert_eq!(result, Err(error));
}
+
+ #[tokio::test]
+ async fn pending_requests_for_thread_returns_thread_requests_in_request_id_order() {
+ let (tx, _rx) = mpsc::channel::<OutgoingEnvelope>(8);
+ let outgoing = Arc::new(OutgoingMessageSender::new(tx));
+ let thread_id = ThreadId::new();
+ let thread_outgoing = ThreadScopedOutgoingMessageSender::new(
+ outgoing.clone(),
+ vec![ConnectionId(1)],
+ thread_id,
+ );
+
+ let (dynamic_tool_request_id, _dynamic_tool_waiter) = thread_outgoing
+ .send_request(ServerRequestPayload::DynamicToolCall(
+ DynamicToolCallParams {
+ thread_id: thread_id.to_string(),
+ turn_id: "turn-1".to_string(),
+ call_id: "call-0".to_string(),
+ tool: "tool".to_string(),
+ arguments: json!({}),
+ },
+ ))
+ .await;
+ let (first_request_id, _first_waiter) = thread_outgoing
+ .send_request(ServerRequestPayload::ToolRequestUserInput(
+ ToolRequestUserInputParams {
+ thread_id: thread_id.to_string(),
+ turn_id: "turn-1".to_string(),
+ item_id: "call-1".to_string(),
+ questions: vec![],
+ },
+ ))
+ .await;
+ let (second_request_id, _second_waiter) = thread_outgoing
+ .send_request(ServerRequestPayload::FileChangeRequestApproval(
+ FileChangeRequestApprovalParams {
+ thread_id: thread_id.to_string(),
+ turn_id: "turn-1".to_string(),
+ item_id: "call-2".to_string(),
+ reason: None,
+ grant_root: None,
+ },
+ ))
+ .await;
+ let pending_requests = outgoing.pending_requests_for_thread(thread_id).await;
+ assert_eq!(
+ pending_requests
+ .iter()
+ .map(ServerRequest::id)
+ .collect::<Vec<_>>(),
+ vec![
+ &dynamic_tool_request_id,
+ &first_request_id,
+ &second_request_id
+ ]
+ );
+ }
+
+ #[tokio::test]
+ async fn cancel_requests_for_thread_cancels_all_thread_requests() {
+ let (tx, _rx) = mpsc::channel::<OutgoingEnvelope>(8);
+ let outgoing = Arc::new(OutgoingMessageSender::new(tx));
+ let thread_id = ThreadId::new();
+ let thread_outgoing = ThreadScopedOutgoingMessageSender::new(
+ outgoing.clone(),
+ vec![ConnectionId(1)],
+ thread_id,
+ );
+
+ let (_dynamic_tool_request_id, dynamic_tool_waiter) = thread_outgoing
+ .send_request(ServerRequestPayload::DynamicToolCall(
+ DynamicToolCallParams {
+ thread_id: thread_id.to_string(),
+ turn_id: "turn-1".to_string(),
+ call_id: "call-0".to_string(),
+ tool: "tool".to_string(),
+ arguments: json!({}),
+ },
+ ))
+ .await;
+ let (_request_id, user_input_waiter) = thread_outgoing
+ .send_request(ServerRequestPayload::ToolRequestUserInput(
+ ToolRequestUserInputParams {
+ thread_id: thread_id.to_string(),
+ turn_id: "turn-1".to_string(),
+ item_id: "call-1".to_string(),
+ questions: vec![],
+ },
+ ))
+ .await;
+ let error = JSONRPCErrorError {
+ code: INTERNAL_ERROR_CODE,
+ message: "tracked request cancelled".to_string(),
+ data: None,
+ };
+
+ outgoing
+ .cancel_requests_for_thread(thread_id, Some(error.clone()))
+ .await;
+
+ let dynamic_tool_result = timeout(Duration::from_secs(1), dynamic_tool_waiter)
+ .await
+ .expect("dynamic tool waiter should resolve")
+ .expect("dynamic tool waiter should receive a callback");
+ let user_input_result = timeout(Duration::from_secs(1), user_input_waiter)
+ .await
+ .expect("user input waiter should resolve")
+ .expect("user input waiter should receive a callback");
+ assert_eq!(dynamic_tool_result, Err(error.clone()));
+ assert_eq!(user_input_result, Err(error));
+ assert!(
+ outgoing
+ .pending_requests_for_thread(thread_id)
+ .await
+ .is_empty()
+ );
+ }
}
diff --git a/codex-rs/app-server/src/server_request_error.rs b/codex-rs/app-server/src/server_request_error.rs
new file mode 100644
index 0000000000..524b50e44b
--- /dev/null
+++ b/codex-rs/app-server/src/server_request_error.rs
@@ -0,0 +1,42 @@
+use codex_app_server_protocol::JSONRPCErrorError;
+
+pub(crate) const TURN_TRANSITION_PENDING_REQUEST_ERROR_REASON: &str = "turnTransition";
+
+pub(crate) fn is_turn_transition_server_request_error(error: &JSONRPCErrorError) -> bool {
+ error
+ .data
+ .as_ref()
+ .and_then(|data| data.get("reason"))
+ .and_then(serde_json::Value::as_str)
+ == Some(TURN_TRANSITION_PENDING_REQUEST_ERROR_REASON)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::is_turn_transition_server_request_error;
+ use codex_app_server_protocol::JSONRPCErrorError;
+ use pretty_assertions::assert_eq;
+ use serde_json::json;
+
+ #[test]
+ fn turn_transition_error_is_detected() {
+ let error = JSONRPCErrorError {
+ code: -1,
+ message: "client request resolved because the turn state was changed".to_string(),
+ data: Some(json!({ "reason": "turnTransition" })),
+ };
+
+ assert_eq!(is_turn_transition_server_request_error(&error), true);
+ }
+
+ #[test]
+ fn unrelated_error_is_not_detected() {
+ let error = JSONRPCErrorError {
+ code: -1,
+ message: "boom".to_string(),
+ data: Some(json!({ "reason": "other" })),
+ };
+
+ assert_eq!(is_turn_transition_server_request_error(&error), false);
+ }
+}
diff --git a/codex-rs/app-server/src/thread_state.rs b/codex-rs/app-server/src/thread_state.rs
index 04e2a8240e..dbacb0ea7c 100644
--- a/codex-rs/app-server/src/thread_state.rs
+++ b/codex-rs/app-server/src/thread_state.rs
@@ -1,5 +1,6 @@
use crate::outgoing_message::ConnectionId;
use crate::outgoing_message::ConnectionRequestId;
+use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ThreadHistoryBuilder;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnError;
@@ -28,8 +29,16 @@ pub(crate) struct PendingThreadResumeRequest {
pub(crate) config_snapshot: ThreadConfigSnapshot,
}
+// ThreadListenerCommand is used to perform operations in the context of the thread listener, for serialization purposes.
pub(crate) enum ThreadListenerCommand {
- SendThreadResumeResponse(PendingThreadResumeRequest),
+ // SendThreadResumeResponse is used to resume an already running thread by sending the thread's history to the client and atomically subscribing for new updates.
+ SendThreadResumeResponse(Box<PendingThreadResumeRequest>),
+ // ResolveServerRequest is used to notify the client that the request has been resolved.
+ // It is executed in the thread listener's context to ensure that the resolved notification is ordered with regard to the request itself.
+ ResolveServerRequest {
+ request_id: RequestId,
+ completion_tx: oneshot::Sender<()>,
+ },
}
/// Per-conversation accumulation of the latest states e.g. error message while a turn runs.
diff --git a/codex-rs/app-server/tests/suite/v2/request_user_input.rs b/codex-rs/app-server/tests/suite/v2/request_user_input.rs
index 926cb9bbc6..f77ddfb4f7 100644
--- a/codex-rs/app-server/tests/suite/v2/request_user_input.rs
+++ b/codex-rs/app-server/tests/suite/v2/request_user_input.rs
@@ -4,9 +4,11 @@ use app_test_support::create_final_assistant_message_sse_response;
use app_test_support::create_mock_responses_server_sequence;
use app_test_support::create_request_user_input_sse_response;
use app_test_support::to_response;
+use codex_app_server_protocol::JSONRPCMessage;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ServerRequest;
+use codex_app_server_protocol::ServerRequestResolvedNotification;
use codex_app_server_protocol::ThreadStartParams;
use codex_app_server_protocol::ThreadStartResponse;
use codex_app_server_protocol::TurnStartParams;
@@ -86,6 +88,7 @@ async fn request_user_input_round_trip() -> Result<()> {
assert_eq!(params.turn_id, turn.id);
assert_eq!(params.item_id, "call1");
assert_eq!(params.questions.len(), 1);
+ let resolved_request_id = request_id.clone();
mcp.send_response(
request_id,
@@ -96,17 +99,31 @@ async fn request_user_input_round_trip() -> Result<()> {
}),
)
.await?;
-
- timeout(
- DEFAULT_READ_TIMEOUT,
- mcp.read_stream_until_notification_message("codex/event/task_complete"),
- )
- .await??;
- timeout(
- DEFAULT_READ_TIMEOUT,
- mcp.read_stream_until_notification_message("turn/completed"),
- )
- .await??;
+ let mut saw_resolved = false;
+ loop {
+ let message = timeout(DEFAULT_READ_TIMEOUT, mcp.read_next_message()).await??;
+ let JSONRPCMessage::Notification(notification) = message else {
+ continue;
+ };
+ match notification.method.as_str() {
+ "serverRequest/resolved" => {
+ let resolved: ServerRequestResolvedNotification = serde_json::from_value(
+ notification
+ .params
+ .clone()
+ .expect("serverRequest/resolved params"),
+ )?;
+ assert_eq!(resolved.thread_id, thread.id);
+ assert_eq!(resolved.request_id, resolved_request_id);
+ saw_resolved = true;
+ }
+ "turn/completed" => {
+ assert!(saw_resolved, "serverRequest/resolved should arrive first");
+ break;
+ }
+ _ => {}
+ }
+ }
Ok(())
}
diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs
index f939061b0b..7ae56761c1 100644
--- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs
+++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs
@@ -1,15 +1,28 @@
use anyhow::Context;
use anyhow::Result;
use app_test_support::McpProcess;
+use app_test_support::create_apply_patch_sse_response;
use app_test_support::create_fake_rollout_with_text_elements;
+use app_test_support::create_final_assistant_message_sse_response;
use app_test_support::create_mock_responses_server_repeating_assistant;
+use app_test_support::create_mock_responses_server_sequence;
+use app_test_support::create_shell_command_sse_response;
use app_test_support::rollout_path;
use app_test_support::to_response;
use chrono::Utc;
+use codex_app_server_protocol::AskForApproval;
+use codex_app_server_protocol::CommandExecutionApprovalDecision;
+use codex_app_server_protocol::CommandExecutionRequestApprovalResponse;
+use codex_app_server_protocol::FileChangeApprovalDecision;
+use codex_app_server_protocol::FileChangeRequestApprovalResponse;
+use codex_app_server_protocol::ItemStartedNotification;
use codex_app_server_protocol::JSONRPCError;
use codex_app_server_protocol::JSONRPCNotification;
use codex_app_server_protocol::JSONRPCResponse;
+use codex_app_server_protocol::PatchApplyStatus;
+use codex_app_server_protocol::PatchChangeKind;
use codex_app_server_protocol::RequestId;
+use codex_app_server_protocol::ServerRequest;
use codex_app_server_protocol::SessionSource;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::ThreadResumeParams;
@@ -642,6 +655,306 @@ async fn thread_resume_rejoins_running_thread_even_with_override_mismatch() -> R
Ok(())
}
+#[tokio::test]
+async fn thread_resume_replays_pending_command_execution_request_approval() -> Result<()> {
+ let responses = vec![
+ create_final_assistant_message_sse_response("seeded")?,
+ create_shell_command_sse_response(
+ vec![
+ "python3".to_string(),
+ "-c".to_string(),
+ "print(42)".to_string(),
+ ],
+ None,
+ Some(5000),
+ "call-1",
+ )?,
+ create_final_assistant_message_sse_response("done")?,
+ ];
+ let server = create_mock_responses_server_sequence(responses).await;
+ let codex_home = TempDir::new()?;
+ create_config_toml(codex_home.path(), &server.uri())?;
+
+ let mut primary = McpProcess::new(codex_home.path()).await?;
+ timeout(DEFAULT_READ_TIMEOUT, primary.initialize()).await??;
+
+ let start_id = primary
+ .send_thread_start_request(ThreadStartParams {
+ model: Some("gpt-5.1-codex-max".to_string()),
+ ..Default::default()
+ })
+ .await?;
+ let start_resp: JSONRPCResponse = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(start_id)),
+ )
+ .await??;
+ let ThreadStartResponse { thread, .. } = to_response::<ThreadStartResponse>(start_resp)?;
+
+ let seed_turn_id = primary
+ .send_turn_start_request(TurnStartParams {
+ thread_id: thread.id.clone(),
+ input: vec![UserInput::Text {
+ text: "seed history".to_string(),
+ text_elements: Vec::new(),
+ }],
+ ..Default::default()
+ })
+ .await?;
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(seed_turn_id)),
+ )
+ .await??;
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_notification_message("turn/completed"),
+ )
+ .await??;
+ primary.clear_message_buffer();
+
+ let running_turn_id = primary
+ .send_turn_start_request(TurnStartParams {
+ thread_id: thread.id.clone(),
+ input: vec![UserInput::Text {
+ text: "run command".to_string(),
+ text_elements: Vec::new(),
+ }],
+ approval_policy: Some(AskForApproval::UnlessTrusted),
+ ..Default::default()
+ })
+ .await?;
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(running_turn_id)),
+ )
+ .await??;
+
+ let original_request = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_request_message(),
+ )
+ .await??;
+ let ServerRequest::CommandExecutionRequestApproval { .. } = &original_request else {
+ panic!("expected CommandExecutionRequestApproval request, got {original_request:?}");
+ };
+
+ let resume_id = primary
+ .send_thread_resume_request(ThreadResumeParams {
+ thread_id: thread.id.clone(),
+ ..Default::default()
+ })
+ .await?;
+ let resume_resp: JSONRPCResponse = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(resume_id)),
+ )
+ .await??;
+ let ThreadResumeResponse {
+ thread: resumed_thread,
+ ..
+ } = to_response::<ThreadResumeResponse>(resume_resp)?;
+ assert_eq!(resumed_thread.id, thread.id);
+ assert!(
+ resumed_thread
+ .turns
+ .iter()
+ .any(|turn| matches!(turn.status, TurnStatus::InProgress))
+ );
+
+ let replayed_request = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_request_message(),
+ )
+ .await??;
+ pretty_assertions::assert_eq!(replayed_request, original_request);
+
+ let ServerRequest::CommandExecutionRequestApproval { request_id, .. } = replayed_request else {
+ panic!("expected CommandExecutionRequestApproval request");
+ };
+ primary
+ .send_response(
+ request_id,
+ serde_json::to_value(CommandExecutionRequestApprovalResponse {
+ decision: CommandExecutionApprovalDecision::Accept,
+ })?,
+ )
+ .await?;
+
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_notification_message("turn/completed"),
+ )
+ .await??;
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn thread_resume_replays_pending_file_change_request_approval() -> Result<()> {
+ let tmp = TempDir::new()?;
+ let codex_home = tmp.path().join("codex_home");
+ std::fs::create_dir(&codex_home)?;
+ let workspace = tmp.path().join("workspace");
+ std::fs::create_dir(&workspace)?;
+
+ let patch = r#"*** Begin Patch
+*** Add File: README.md
++new line
+*** End Patch
+"#;
+ let responses = vec![
+ create_final_assistant_message_sse_response("seeded")?,
+ create_apply_patch_sse_response(patch, "patch-call")?,
+ create_final_assistant_message_sse_response("done")?,
+ ];
+ let server = create_mock_responses_server_sequence(responses).await;
+ create_config_toml(&codex_home, &server.uri())?;
+
+ let mut primary = McpProcess::new(&codex_home).await?;
+ timeout(DEFAULT_READ_TIMEOUT, primary.initialize()).await??;
+
+ let start_id = primary
+ .send_thread_start_request(ThreadStartParams {
+ model: Some("gpt-5.1-codex-max".to_string()),
+ cwd: Some(workspace.to_string_lossy().into_owned()),
+ ..Default::default()
+ })
+ .await?;
+ let start_resp: JSONRPCResponse = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(start_id)),
+ )
+ .await??;
+ let ThreadStartResponse { thread, .. } = to_response::<ThreadStartResponse>(start_resp)?;
+
+ let seed_turn_id = primary
+ .send_turn_start_request(TurnStartParams {
+ thread_id: thread.id.clone(),
+ input: vec![UserInput::Text {
+ text: "seed history".to_string(),
+ text_elements: Vec::new(),
+ }],
+ cwd: Some(workspace.clone()),
+ ..Default::default()
+ })
+ .await?;
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(seed_turn_id)),
+ )
+ .await??;
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_notification_message("turn/completed"),
+ )
+ .await??;
+ primary.clear_message_buffer();
+
+ let running_turn_id = primary
+ .send_turn_start_request(TurnStartParams {
+ thread_id: thread.id.clone(),
+ input: vec![UserInput::Text {
+ text: "apply patch".to_string(),
+ text_elements: Vec::new(),
+ }],
+ cwd: Some(workspace.clone()),
+ approval_policy: Some(AskForApproval::UnlessTrusted),
+ ..Default::default()
+ })
+ .await?;
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(running_turn_id)),
+ )
+ .await??;
+
+ let original_started = timeout(DEFAULT_READ_TIMEOUT, async {
+ loop {
+ let notification = primary
+ .read_stream_until_notification_message("item/started")
+ .await?;
+ let started: ItemStartedNotification =
+ serde_json::from_value(notification.params.clone().expect("item/started params"))?;
+ if let ThreadItem::FileChange { .. } = started.item {
+ return Ok::<ThreadItem, anyhow::Error>(started.item);
+ }
+ }
+ })
+ .await??;
+ let expected_readme_path = workspace.join("README.md");
+ let expected_file_change = ThreadItem::FileChange {
+ id: "patch-call".to_string(),
+ changes: vec![codex_app_server_protocol::FileUpdateChange {
+ path: expected_readme_path.to_string_lossy().into_owned(),
+ kind: PatchChangeKind::Add,
+ diff: "new line\n".to_string(),
+ }],
+ status: PatchApplyStatus::InProgress,
+ };
+ assert_eq!(original_started, expected_file_change);
+
+ let original_request = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_request_message(),
+ )
+ .await??;
+ let ServerRequest::FileChangeRequestApproval { .. } = &original_request else {
+ panic!("expected FileChangeRequestApproval request, got {original_request:?}");
+ };
+ primary.clear_message_buffer();
+
+ let resume_id = primary
+ .send_thread_resume_request(ThreadResumeParams {
+ thread_id: thread.id.clone(),
+ ..Default::default()
+ })
+ .await?;
+ let resume_resp: JSONRPCResponse = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_response_message(RequestId::Integer(resume_id)),
+ )
+ .await??;
+ let ThreadResumeResponse {
+ thread: resumed_thread,
+ ..
+ } = to_response::<ThreadResumeResponse>(resume_resp)?;
+ assert_eq!(resumed_thread.id, thread.id);
+ assert!(
+ resumed_thread
+ .turns
+ .iter()
+ .any(|turn| matches!(turn.status, TurnStatus::InProgress))
+ );
+
+ let replayed_request = timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_request_message(),
+ )
+ .await??;
+ assert_eq!(replayed_request, original_request);
+
+ let ServerRequest::FileChangeRequestApproval { request_id, .. } = replayed_request else {
+ panic!("expected FileChangeRequestApproval request");
+ };
+ primary
+ .send_response(
+ request_id,
+ serde_json::to_value(FileChangeRequestApprovalResponse {
+ decision: FileChangeApprovalDecision::Accept,
+ })?,
+ )
+ .await?;
+
+ timeout(
+ DEFAULT_READ_TIMEOUT,
+ primary.read_stream_until_notification_message("turn/completed"),
+ )
+ .await??;
+
+ Ok(())
+}
+
#[tokio::test]
async fn thread_resume_with_overrides_defers_updated_at_until_turn_start() -> Result<()> {
let server = create_mock_responses_server_repeating_assistant("Done").await;
diff --git a/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs b/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs
index 486e915f6f..8f944dca07 100644
--- a/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs
+++ b/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs
@@ -8,6 +8,8 @@ use app_test_support::to_response;
use codex_app_server_protocol::JSONRPCNotification;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::RequestId;
+use codex_app_server_protocol::ServerRequest;
+use codex_app_server_protocol::ServerRequestResolvedNotification;
use codex_app_server_protocol::ThreadStartParams;
use codex_app_server_protocol::ThreadStartResponse;
use codex_app_server_protocol::TurnCompletedNotification;
@@ -48,7 +50,7 @@ async fn turn_interrupt_aborts_running_turn() -> Result<()> {
"call_sleep",
)?])
.await;
- create_config_toml(&codex_home, &server.uri())?;
+ create_config_toml(&codex_home, &server.uri(), "never")?;
let mut mcp = McpProcess::new(&codex_home).await?;
timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??;
@@ -120,15 +122,134 @@ async fn turn_interrupt_aborts_running_turn() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn turn_interrupt_resolves_pending_command_approval_request() -> Result<()> {
+ #[cfg(target_os = "windows")]
+ let shell_command = vec![
+ "powershell".to_string(),
+ "-Command".to_string(),
+ "Start-Sleep -Seconds 10".to_string(),
+ ];
+ #[cfg(not(target_os = "windows"))]
+ let shell_command = vec!["sleep".to_string(), "10".to_string()];
+
+ let tmp = TempDir::new()?;
+ let codex_home = tmp.path().join("codex_home");
+ std::fs::create_dir(&codex_home)?;
+ let working_directory = tmp.path().join("workdir");
+ std::fs::create_dir(&working_directory)?;
+
+ let server = create_mock_responses_server_sequence(vec![create_shell_command_sse_response(
+ shell_command.clone(),
+ Some(&working_directory),
+ Some(10_000),
+ "call_sleep_approval",
+ )?])
+ .await;
+ create_config_toml(&codex_home, &server.uri(), "untrusted")?;
+
+ let mut mcp = McpProcess::new(&codex_home).await?;
+ timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??;
+
+ let thread_req = mcp
+ .send_thread_start_request(ThreadStartParams {
+ model: Some("mock-model".to_string()),
+ ..Default::default()
+ })
+ .await?;
+ let thread_resp: JSONRPCResponse = timeout(
+ DEFAULT_READ_TIMEOUT,
+ mcp.read_stream_until_response_message(RequestId::Integer(thread_req)),
+ )
+ .await??;
+ let ThreadStartResponse { thread, .. } = to_response::<ThreadStartResponse>(thread_resp)?;
+
+ let turn_req = mcp
+ .send_turn_start_request(TurnStartParams {
+ thread_id: thread.id.clone(),
+ input: vec![V2UserInput::Text {
+ text: "run sleep".to_string(),
+ text_elements: Vec::new(),
+ }],
+ cwd: Some(working_directory),
+ ..Default::default()
+ })
+ .await?;
+ let turn_resp: JSONRPCResponse = timeout(
+ DEFAULT_READ_TIMEOUT,
+ mcp.read_stream_until_response_message(RequestId::Integer(turn_req)),
+ )
+ .await??;
+ let TurnStartResponse { turn } = to_response::<TurnStartResponse>(turn_resp)?;
+
+ let request = timeout(
+ DEFAULT_READ_TIMEOUT,
+ mcp.read_stream_until_request_message(),
+ )
+ .await??;
+ let ServerRequest::CommandExecutionRequestApproval { request_id, params } = request else {
+ panic!("expected CommandExecutionRequestApproval request");
+ };
+ assert_eq!(params.item_id, "call_sleep_approval");
+ assert_eq!(params.thread_id, thread.id);
+ assert_eq!(params.turn_id, turn.id);
+
+ let interrupt_id = mcp
+ .send_turn_interrupt_request(TurnInterruptParams {
+ thread_id: thread.id.clone(),
+ turn_id: turn.id.clone(),
+ })
+ .await?;
+ let interrupt_resp: JSONRPCResponse = timeout(
+ DEFAULT_READ_TIMEOUT,
+ mcp.read_stream_until_response_message(RequestId::Integer(interrupt_id)),
+ )
+ .await??;
+ let _resp: TurnInterruptResponse = to_response::<TurnInterruptResponse>(interrupt_resp)?;
+
+ let resolved_notification = timeout(
+ DEFAULT_READ_TIMEOUT,
+ mcp.read_stream_until_notification_message("serverRequest/resolved"),
+ )
+ .await??;
+ let resolved: ServerRequestResolvedNotification = serde_json::from_value(
+ resolved_notification
+ .params
+ .clone()
+ .expect("serverRequest/resolved params must be present"),
+ )?;
+ assert_eq!(resolved.thread_id, thread.id);
+ assert_eq!(resolved.request_id, request_id);
+
+ let completed_notif: JSONRPCNotification = timeout(
+ DEFAULT_READ_TIMEOUT,
+ mcp.read_stream_until_notification_message("turn/completed"),
+ )
+ .await??;
+ let completed: TurnCompletedNotification = serde_json::from_value(
+ completed_notif
+ .params
+ .expect("turn/completed params must be present"),
+ )?;
+ assert_eq!(completed.thread_id, thread.id);
+ assert_eq!(completed.turn.status, TurnStatus::Interrupted);
+
+ Ok(())
+}
+
// Helper to create a config.toml pointing at the mock model server.
-fn create_config_toml(codex_home: &std::path::Path, server_uri: &str) -> std::io::Result<()> {
+fn create_config_toml(
+ codex_home: &std::path::Path,
+ server_uri: &str,
+ approval_policy: &str,
+) -> std::io::Result<()> {
let config_toml = codex_home.join("config.toml");
std::fs::write(
config_toml,
format!(
r#"
model = "mock-model"
-approval_policy = "never"
+approval_policy = "{approval_policy}"
sandbox_mode = "danger-full-access"
model_provider = "mock_provider"
diff --git a/codex-rs/app-server/tests/suite/v2/turn_start.rs b/codex-rs/app-server/tests/suite/v2/turn_start.rs
index f396aa8c44..44063a3cad 100644
--- a/codex-rs/app-server/tests/suite/v2/turn_start.rs
+++ b/codex-rs/app-server/tests/suite/v2/turn_start.rs
@@ -22,12 +22,14 @@ use codex_app_server_protocol::FileChangeRequestApprovalResponse;
use codex_app_server_protocol::ItemCompletedNotification;
use codex_app_server_protocol::ItemStartedNotification;
use codex_app_server_protocol::JSONRPCError;
+use codex_app_server_protocol::JSONRPCMessage;
use codex_app_server_protocol::JSONRPCNotification;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::PatchApplyStatus;
use codex_app_server_protocol::PatchChangeKind;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ServerRequest;
+use codex_app_server_protocol::ServerRequestResolvedNotification;
use codex_app_server_protocol::TextElement;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::ThreadStartParams;
@@ -1071,6 +1073,7 @@ async fn turn_start_exec_approval_toggle_v2() -> Result<()> {
panic!("expected CommandExecutionRequestApproval request");
};
assert_eq!(params.item_id, "call1");
+ let resolved_request_id = request_id.clone();
// Approve and wait for task completion
mcp.send_response(
@@ -1080,16 +1083,31 @@ async fn turn_start_exec_approval_toggle_v2() -> Result<()> {
})?,
)
.await?;
- timeout(
- DEFAULT_READ_TIMEOUT,
- mcp.read_stream_until_notification_message("codex/event/task_complete"),
- )
- .await??;
- timeout(
- DEFAULT_READ_TIMEOUT,
- mcp.read_stream_until_notification_message("turn/completed"),
- )
- .await??;
+ let mut saw_resolved = false;
+ loop {
+ let message = timeout(DEFAULT_READ_TIMEOUT, mcp.read_next_message()).await??;
+ let JSONRPCMessage::Notification(notification) = message else {
+ continue;
+ };
+ match notification.method.as_str() {
+ "serverRequest/resolved" => {
+ let resolved: ServerRequestResolvedNotification = serde_json::from_value(
+ notification
+ .params
+ .clone()
+ .expect("serverRequest/resolved params"),
+ )?;
+ assert_eq!(resolved.thread_id, thread.id);
+ assert_eq!(resolved.request_id, resolved_request_id);
+ saw_resolved = true;
+ }
+ "turn/completed" => {
+ assert!(saw_resolved, "serverRequest/resolved should arrive first");
+ break;
+ }
+ _ => {}
+ }
+ }
// Second turn with approval_policy=never should not elicit approval
let second_turn_id = mcp
@@ -1527,6 +1545,7 @@ async fn turn_start_file_change_approval_v2() -> Result<()> {
assert_eq!(params.item_id, "patch-call");
assert_eq!(params.thread_id, thread.id);
assert_eq!(params.turn_id, turn.id);
+ let resolved_request_id = request_id.clone();
let expected_readme_path = workspace.join("README.md");
let expected_readme_path = expected_readme_path.to_string_lossy().into_owned();
pretty_assertions::assert_eq!(
@@ -1545,18 +1564,49 @@ async fn turn_start_file_change_approval_v2() -> Result<()> {
})?,
)
.await?;
-
- let output_delta_notif = timeout(
- DEFAULT_READ_TIMEOUT,
- mcp.read_stream_until_notification_message("item/fileChange/outputDelta"),
- )
- .await??;
- let output_delta: FileChangeOutputDeltaNotification = serde_json::from_value(
- output_delta_notif
- .params
- .clone()
- .expect("item/fileChange/outputDelta params"),
- )?;
+ let mut saw_resolved = false;
+ let mut output_delta: Option<FileChangeOutputDeltaNotification> = None;
+ let mut completed_file_change: Option<ThreadItem> = None;
+ while !(output_delta.is_some() && completed_file_change.is_some()) {
+ let message = timeout(DEFAULT_READ_TIMEOUT, mcp.read_next_message()).await??;
+ let JSONRPCMessage::Notification(notification) = message else {
+ continue;
+ };
+ match notification.method.as_str() {
+ "serverRequest/resolved" => {
+ let resolved: ServerRequestResolvedNotification = serde_json::from_value(
+ notification
+ .params
+ .clone()
+ .expect("serverRequest/resolved params"),
+ )?;
+ assert_eq!(resolved.thread_id, thread.id);
+ assert_eq!(resolved.request_id, resolved_request_id);
+ saw_resolved = true;
+ }
+ "item/fileChange/outputDelta" => {
+ assert!(saw_resolved, "serverRequest/resolved should arrive first");
+ let notification: FileChangeOutputDeltaNotification = serde_json::from_value(
+ notification
+ .params
+ .clone()
+ .expect("item/fileChange/outputDelta params"),
+ )?;
+ output_delta = Some(notification);
+ }
+ "item/completed" => {
+ let completed: ItemCompletedNotification = serde_json::from_value(
+ notification.params.clone().expect("item/completed params"),
+ )?;
+ if let ThreadItem::FileChange { .. } = completed.item {
+ assert!(saw_resolved, "serverRequest/resolved should arrive first");
+ completed_file_change = Some(completed.item);
+ }
+ }
+ _ => {}
+ }
+ }
+ let output_delta = output_delta.expect("file change output delta should be observed");
assert_eq!(output_delta.thread_id, thread.id);
assert_eq!(output_delta.turn_id, turn.id);
assert_eq!(output_delta.item_id, "patch-call");
@@ -1566,38 +1616,23 @@ async fn turn_start_file_change_approval_v2() -> Result<()> {
output_delta.delta
);
- let completed_file_change = timeout(DEFAULT_READ_TIMEOUT, async {
- loop {
- let completed_notif = mcp
- .read_stream_until_notification_message("item/completed")
- .await?;
- let completed: ItemCompletedNotification = serde_json::from_value(
- completed_notif
- .params
- .clone()
- .expect("item/completed params"),
- )?;
- if let ThreadItem::FileChange { .. } = completed.item {
- return Ok::<ThreadItem, anyhow::Error>(completed.item);
- }
- }
- })
- .await??;
+ let completed_file_change =
+ completed_file_change.expect("file change completion should be observed");
let ThreadItem::FileChange { ref id, status, .. } = completed_file_change else {
unreachable!("loop ensures we break on file change items");
};
assert_eq!(id, "patch-call");
assert_eq!(status, PatchApplyStatus::Completed);
+ let readme_contents = std::fs::read_to_string(expected_readme_path)?;
+ assert_eq!(readme_contents, "new line\n");
+
timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_notification_message("codex/event/task_complete"),
)
.await??;
- let readme_contents = std::fs::read_to_string(expected_readme_path)?;
- assert_eq!(readme_contents, "new line\n");
-
Ok(())
}