diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index 82914f3a6f..da35627417 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -4493,6 +4493,21 @@ } ] }, + "UserPromptSubmitStoppedNotification": { + "properties": { + "threadId": { + "type": "string" + }, + "turnId": { + "type": "string" + } + }, + "required": [ + "threadId", + "turnId" + ], + "type": "object" + }, "WarningNotification": { "properties": { "message": { @@ -4969,6 +4984,26 @@ "title": "Hook/completedNotification", "type": "object" }, + { + "properties": { + "method": { + "enum": [ + "userPromptSubmit/stopped" + ], + "title": "UserPromptSubmit/stoppedNotificationMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/UserPromptSubmitStoppedNotification" + } + }, + "required": [ + "method", + "params" + ], + "title": "UserPromptSubmit/stoppedNotification", + "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 d579dfe060..b2b8e3f2b1 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 @@ -4042,6 +4042,26 @@ "title": "Hook/completedNotification", "type": "object" }, + { + "properties": { + "method": { + "enum": [ + "userPromptSubmit/stopped" + ], + "title": "UserPromptSubmit/stoppedNotificationMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/v2/UserPromptSubmitStoppedNotification" + } + }, + "required": [ + "method", + "params" + ], + "title": "UserPromptSubmit/stoppedNotification", + "type": "object" + }, { "properties": { "method": { @@ -17841,6 +17861,23 @@ } ] }, + "UserPromptSubmitStoppedNotification": { + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "threadId": { + "type": "string" + }, + "turnId": { + "type": "string" + } + }, + "required": [ + "threadId", + "turnId" + ], + "title": "UserPromptSubmitStoppedNotification", + "type": "object" + }, "Verbosity": { "description": "Controls output length/detail on GPT-5 models via the Responses API. Serialized with lowercase values to match the OpenAI API.", "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index f9e1f879cf..c70991ced3 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -11087,6 +11087,26 @@ "title": "Hook/completedNotification", "type": "object" }, + { + "properties": { + "method": { + "enum": [ + "userPromptSubmit/stopped" + ], + "title": "UserPromptSubmit/stoppedNotificationMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/UserPromptSubmitStoppedNotification" + } + }, + "required": [ + "method", + "params" + ], + "title": "UserPromptSubmit/stoppedNotification", + "type": "object" + }, { "properties": { "method": { @@ -15727,6 +15747,23 @@ } ] }, + "UserPromptSubmitStoppedNotification": { + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "threadId": { + "type": "string" + }, + "turnId": { + "type": "string" + } + }, + "required": [ + "threadId", + "turnId" + ], + "title": "UserPromptSubmitStoppedNotification", + "type": "object" + }, "Verbosity": { "description": "Controls output length/detail on GPT-5 models via the Responses API. Serialized with lowercase values to match the OpenAI API.", "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/UserPromptSubmitStoppedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/UserPromptSubmitStoppedNotification.json new file mode 100644 index 0000000000..8517c10497 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/UserPromptSubmitStoppedNotification.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "threadId": { + "type": "string" + }, + "turnId": { + "type": "string" + } + }, + "required": [ + "threadId", + "turnId" + ], + "title": "UserPromptSubmitStoppedNotification", + "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 c32618c090..5bb62a6641 100644 --- a/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts +++ b/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts @@ -60,6 +60,7 @@ import type { TurnCompletedNotification } from "./v2/TurnCompletedNotification"; import type { TurnDiffUpdatedNotification } from "./v2/TurnDiffUpdatedNotification"; import type { TurnPlanUpdatedNotification } from "./v2/TurnPlanUpdatedNotification"; import type { TurnStartedNotification } from "./v2/TurnStartedNotification"; +import type { UserPromptSubmitStoppedNotification } from "./v2/UserPromptSubmitStoppedNotification"; import type { WarningNotification } from "./v2/WarningNotification"; import type { WindowsSandboxSetupCompletedNotification } from "./v2/WindowsSandboxSetupCompletedNotification"; import type { WindowsWorldWritableWarningNotification } from "./v2/WindowsWorldWritableWarningNotification"; @@ -67,4 +68,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": "skills/changed", "params": SkillsChangedNotification } | { "method": "thread/name/updated", "params": ThreadNameUpdatedNotification } | { "method": "thread/goal/updated", "params": ThreadGoalUpdatedNotification } | { "method": "thread/goal/cleared", "params": ThreadGoalClearedNotification } | { "method": "thread/tokenUsage/updated", "params": ThreadTokenUsageUpdatedNotification } | { "method": "turn/started", "params": TurnStartedNotification } | { "method": "hook/started", "params": HookStartedNotification } | { "method": "turn/completed", "params": TurnCompletedNotification } | { "method": "hook/completed", "params": HookCompletedNotification } | { "method": "turn/diff/updated", "params": TurnDiffUpdatedNotification } | { "method": "turn/plan/updated", "params": TurnPlanUpdatedNotification } | { "method": "item/started", "params": ItemStartedNotification } | { "method": "item/autoApprovalReview/started", "params": ItemGuardianApprovalReviewStartedNotification } | { "method": "item/autoApprovalReview/completed", "params": ItemGuardianApprovalReviewCompletedNotification } | { "method": "item/completed", "params": ItemCompletedNotification } | { "method": "rawResponseItem/completed", "params": RawResponseItemCompletedNotification } | { "method": "item/agentMessage/delta", "params": AgentMessageDeltaNotification } | { "method": "item/plan/delta", "params": PlanDeltaNotification } | { "method": "command/exec/outputDelta", "params": CommandExecOutputDeltaNotification } | { "method": "item/commandExecution/outputDelta", "params": CommandExecutionOutputDeltaNotification } | { "method": "item/commandExecution/terminalInteraction", "params": TerminalInteractionNotification } | { "method": "item/fileChange/outputDelta", "params": FileChangeOutputDeltaNotification } | { "method": "item/fileChange/patchUpdated", "params": FileChangePatchUpdatedNotification } | { "method": "serverRequest/resolved", "params": ServerRequestResolvedNotification } | { "method": "item/mcpToolCall/progress", "params": McpToolCallProgressNotification } | { "method": "mcpServer/oauthLogin/completed", "params": McpServerOauthLoginCompletedNotification } | { "method": "mcpServer/startupStatus/updated", "params": McpServerStatusUpdatedNotification } | { "method": "account/updated", "params": AccountUpdatedNotification } | { "method": "account/rateLimits/updated", "params": AccountRateLimitsUpdatedNotification } | { "method": "app/list/updated", "params": AppListUpdatedNotification } | { "method": "remoteControl/status/changed", "params": RemoteControlStatusChangedNotification } | { "method": "externalAgentConfig/import/completed", "params": ExternalAgentConfigImportCompletedNotification } | { "method": "fs/changed", "params": FsChangedNotification } | { "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": "model/verification", "params": ModelVerificationNotification } | { "method": "warning", "params": WarningNotification } | { "method": "guardianWarning", "params": GuardianWarningNotification } | { "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/transcript/delta", "params": ThreadRealtimeTranscriptDeltaNotification } | { "method": "thread/realtime/transcript/done", "params": ThreadRealtimeTranscriptDoneNotification } | { "method": "thread/realtime/outputAudio/delta", "params": ThreadRealtimeOutputAudioDeltaNotification } | { "method": "thread/realtime/sdp", "params": ThreadRealtimeSdpNotification } | { "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 }; +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": "skills/changed", "params": SkillsChangedNotification } | { "method": "thread/name/updated", "params": ThreadNameUpdatedNotification } | { "method": "thread/goal/updated", "params": ThreadGoalUpdatedNotification } | { "method": "thread/goal/cleared", "params": ThreadGoalClearedNotification } | { "method": "thread/tokenUsage/updated", "params": ThreadTokenUsageUpdatedNotification } | { "method": "turn/started", "params": TurnStartedNotification } | { "method": "hook/started", "params": HookStartedNotification } | { "method": "turn/completed", "params": TurnCompletedNotification } | { "method": "hook/completed", "params": HookCompletedNotification } | { "method": "userPromptSubmit/stopped", "params": UserPromptSubmitStoppedNotification } | { "method": "turn/diff/updated", "params": TurnDiffUpdatedNotification } | { "method": "turn/plan/updated", "params": TurnPlanUpdatedNotification } | { "method": "item/started", "params": ItemStartedNotification } | { "method": "item/autoApprovalReview/started", "params": ItemGuardianApprovalReviewStartedNotification } | { "method": "item/autoApprovalReview/completed", "params": ItemGuardianApprovalReviewCompletedNotification } | { "method": "item/completed", "params": ItemCompletedNotification } | { "method": "rawResponseItem/completed", "params": RawResponseItemCompletedNotification } | { "method": "item/agentMessage/delta", "params": AgentMessageDeltaNotification } | { "method": "item/plan/delta", "params": PlanDeltaNotification } | { "method": "command/exec/outputDelta", "params": CommandExecOutputDeltaNotification } | { "method": "item/commandExecution/outputDelta", "params": CommandExecutionOutputDeltaNotification } | { "method": "item/commandExecution/terminalInteraction", "params": TerminalInteractionNotification } | { "method": "item/fileChange/outputDelta", "params": FileChangeOutputDeltaNotification } | { "method": "item/fileChange/patchUpdated", "params": FileChangePatchUpdatedNotification } | { "method": "serverRequest/resolved", "params": ServerRequestResolvedNotification } | { "method": "item/mcpToolCall/progress", "params": McpToolCallProgressNotification } | { "method": "mcpServer/oauthLogin/completed", "params": McpServerOauthLoginCompletedNotification } | { "method": "mcpServer/startupStatus/updated", "params": McpServerStatusUpdatedNotification } | { "method": "account/updated", "params": AccountUpdatedNotification } | { "method": "account/rateLimits/updated", "params": AccountRateLimitsUpdatedNotification } | { "method": "app/list/updated", "params": AppListUpdatedNotification } | { "method": "remoteControl/status/changed", "params": RemoteControlStatusChangedNotification } | { "method": "externalAgentConfig/import/completed", "params": ExternalAgentConfigImportCompletedNotification } | { "method": "fs/changed", "params": FsChangedNotification } | { "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": "model/verification", "params": ModelVerificationNotification } | { "method": "warning", "params": WarningNotification } | { "method": "guardianWarning", "params": GuardianWarningNotification } | { "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/transcript/delta", "params": ThreadRealtimeTranscriptDeltaNotification } | { "method": "thread/realtime/transcript/done", "params": ThreadRealtimeTranscriptDoneNotification } | { "method": "thread/realtime/outputAudio/delta", "params": ThreadRealtimeOutputAudioDeltaNotification } | { "method": "thread/realtime/sdp", "params": ThreadRealtimeSdpNotification } | { "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 }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/UserPromptSubmitStoppedNotification.ts b/codex-rs/app-server-protocol/schema/typescript/v2/UserPromptSubmitStoppedNotification.ts new file mode 100644 index 0000000000..e3dea4eecb --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/UserPromptSubmitStoppedNotification.ts @@ -0,0 +1,5 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type UserPromptSubmitStoppedNotification = { threadId: string, turnId: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index 893cc202e8..400e954ee0 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -426,6 +426,7 @@ export type { TurnStatus } from "./TurnStatus"; export type { TurnSteerParams } from "./TurnSteerParams"; export type { TurnSteerResponse } from "./TurnSteerResponse"; export type { UserInput } from "./UserInput"; +export type { UserPromptSubmitStoppedNotification } from "./UserPromptSubmitStoppedNotification"; export type { WarningNotification } from "./WarningNotification"; export type { WebSearchAction } from "./WebSearchAction"; export type { WindowsSandboxSetupCompletedNotification } from "./WindowsSandboxSetupCompletedNotification"; diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index b68d140313..71b4287fbd 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -1383,6 +1383,7 @@ server_notification_definitions! { HookStarted => "hook/started" (v2::HookStartedNotification), TurnCompleted => "turn/completed" (v2::TurnCompletedNotification), HookCompleted => "hook/completed" (v2::HookCompletedNotification), + UserPromptSubmitStopped => "userPromptSubmit/stopped" (v2::UserPromptSubmitStoppedNotification), TurnDiffUpdated => "turn/diff/updated" (v2::TurnDiffUpdatedNotification), TurnPlanUpdated => "turn/plan/updated" (v2::TurnPlanUpdatedNotification), ItemStarted => "item/started" (v2::ItemStartedNotification), diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 960f3e9687..eb27cbf720 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -6775,6 +6775,14 @@ pub struct HookCompletedNotification { pub run: HookRunSummary, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct UserPromptSubmitStoppedNotification { + pub thread_id: String, + pub turn_id: String, +} + #[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 093a4640bd..02d45921c5 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -167,7 +167,7 @@ Example with notification opt-out: - `thread/shellCommand` — run a user-initiated `!` shell command against a thread; this runs unsandboxed with full access rather than inheriting the thread sandbox policy. Returns `{}` immediately while progress streams through standard turn/item notifications and any active turn receives the formatted output in its message stream. - `thread/backgroundTerminals/clean` — terminate all running background terminals for a thread (experimental; requires `capabilities.experimentalApi`); returns `{}` when the cleanup request is accepted. - `thread/rollback` — drop the last N turns from the agent’s in-memory context and persist a rollback marker in the rollout so future resumes see the pruned history; returns the updated `thread` (with `turns` populated) on success. -- `turn/start` — add user input to a thread and begin Codex generation; responds with the initial `turn` object and streams `turn/started`, `item/*`, and `turn/completed` notifications. Prefer experimental `permissions` profile selection for permission overrides; the legacy `sandboxPolicy` field is still accepted but cannot be combined with `permissions`. For `collaborationMode`, `settings.developer_instructions: null` means "use built-in instructions for the selected mode". +- `turn/start` — add user input to a thread and begin Codex generation; responds with the initial `turn` object and streams `turn/started`, `item/*`, and `turn/completed` notifications. If `UserPromptSubmit` hooks stop the turn before it starts, `userPromptSubmit/stopped` reports that aggregate outcome once after all matching handlers finish. Prefer experimental `permissions` profile selection for permission overrides; the legacy `sandboxPolicy` field is still accepted but cannot be combined with `permissions`. For `collaborationMode`, `settings.developer_instructions: null` means "use built-in instructions for the selected mode". - `thread/inject_items` — append raw Responses API items to a loaded thread’s model-visible history without starting a user turn; returns `{}` on success. - `turn/steer` — add user input to an already in-flight regular turn without starting a new turn; returns the active `turnId` that accepted the input. Review and manual compaction turns reject `turn/steer`. - `turn/interrupt` — request cancellation of an in-flight turn by `(thread_id, turn_id)`; success is an empty `{}` response and the turn finishes with `status: "interrupted"`. diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index 628034da72..3dff2ceac2 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -81,6 +81,7 @@ use codex_app_server_protocol::TurnPlanStep; use codex_app_server_protocol::TurnPlanUpdatedNotification; use codex_app_server_protocol::TurnStartedNotification; use codex_app_server_protocol::TurnStatus; +use codex_app_server_protocol::UserPromptSubmitStoppedNotification; use codex_app_server_protocol::WarningNotification; use codex_app_server_protocol::build_file_change_approval_request_item; use codex_app_server_protocol::build_file_change_end_item; @@ -1062,6 +1063,15 @@ pub(crate) async fn apply_bespoke_event_handling( .send_server_notification(ServerNotification::HookCompleted(notification)) .await; } + EventMsg::UserPromptSubmitStopped(event) => { + let notification = UserPromptSubmitStoppedNotification { + thread_id: conversation_id.to_string(), + turn_id: event.turn_id, + }; + outgoing + .send_server_notification(ServerNotification::UserPromptSubmitStopped(notification)) + .await; + } EventMsg::ExitedReviewMode(review_event) => { let review = match review_event.review_output { Some(output) => render_review_output_text(&output), diff --git a/codex-rs/core/src/hook_runtime.rs b/codex-rs/core/src/hook_runtime.rs index 9a92854515..18b84529f7 100644 --- a/codex-rs/core/src/hook_runtime.rs +++ b/codex-rs/core/src/hook_runtime.rs @@ -27,6 +27,7 @@ use codex_protocol::protocol::HookRunStatus; use codex_protocol::protocol::HookRunSummary; use codex_protocol::protocol::HookSource; use codex_protocol::protocol::HookStartedEvent; +use codex_protocol::protocol::UserPromptSubmitStoppedEvent; use codex_protocol::user_input::UserInput; use serde_json::Value; @@ -269,13 +270,23 @@ pub(crate) async fn run_user_prompt_submit_hooks( }; let hooks = sess.hooks(); let preview_runs = hooks.preview_user_prompt_submit(&request); - run_context_injecting_hook( + let outcome = run_context_injecting_hook( sess, turn_context, preview_runs, hooks.run_user_prompt_submit(request), ) - .await + .await; + if outcome.should_stop { + sess.send_event( + turn_context, + EventMsg::UserPromptSubmitStopped(UserPromptSubmitStoppedEvent { + turn_id: turn_context.sub_id.clone(), + }), + ) + .await; + } + outcome } pub(crate) async fn inspect_pending_input( diff --git a/codex-rs/core/src/session/turn.rs b/codex-rs/core/src/session/turn.rs index faf869f497..85d464d8f1 100644 --- a/codex-rs/core/src/session/turn.rs +++ b/codex-rs/core/src/session/turn.rs @@ -1490,6 +1490,7 @@ pub(super) fn realtime_text_for_event(msg: &EventMsg) -> Option { | EventMsg::ItemStarted(_) | EventMsg::HookStarted(_) | EventMsg::HookCompleted(_) + | EventMsg::UserPromptSubmitStopped(_) | EventMsg::AgentMessageContentDelta(_) | EventMsg::PlanDelta(_) | EventMsg::ReasoningContentDelta(_) diff --git a/codex-rs/mcp-server/src/codex_tool_runner.rs b/codex-rs/mcp-server/src/codex_tool_runner.rs index 62d8b14fbf..4653f40817 100644 --- a/codex-rs/mcp-server/src/codex_tool_runner.rs +++ b/codex-rs/mcp-server/src/codex_tool_runner.rs @@ -365,6 +365,7 @@ async fn run_codex_tool_session_inner( | EventMsg::ItemCompleted(_) | EventMsg::HookStarted(_) | EventMsg::HookCompleted(_) + | EventMsg::UserPromptSubmitStopped(_) | EventMsg::AgentMessageContentDelta(_) | EventMsg::ReasoningContentDelta(_) | EventMsg::ReasoningRawContentDelta(_) diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index c3e4f5abaa..c484f8e100 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -1482,6 +1482,7 @@ pub enum EventMsg { ItemCompleted(ItemCompletedEvent), HookStarted(HookStartedEvent), HookCompleted(HookCompletedEvent), + UserPromptSubmitStopped(UserPromptSubmitStoppedEvent), AgentMessageContentDelta(AgentMessageContentDeltaEvent), PlanDelta(PlanDeltaEvent), @@ -1638,6 +1639,12 @@ pub struct HookCompletedEvent { pub run: HookRunSummary, } +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +pub struct UserPromptSubmitStoppedEvent { + pub turn_id: String, +} + #[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] #[serde(rename_all = "snake_case")] pub enum RealtimeConversationVersion { diff --git a/codex-rs/rollout-trace/src/protocol_event.rs b/codex-rs/rollout-trace/src/protocol_event.rs index f982e8028a..a920b39966 100644 --- a/codex-rs/rollout-trace/src/protocol_event.rs +++ b/codex-rs/rollout-trace/src/protocol_event.rs @@ -275,6 +275,7 @@ pub(crate) fn tool_runtime_trace_event(event: &EventMsg) -> Option Option<&'static s | EventMsg::ItemCompleted(_) | EventMsg::HookStarted(_) | EventMsg::HookCompleted(_) + | EventMsg::UserPromptSubmitStopped(_) | EventMsg::AgentMessageContentDelta(_) | EventMsg::PlanDelta(_) | EventMsg::ReasoningContentDelta(_) diff --git a/codex-rs/rollout/src/policy.rs b/codex-rs/rollout/src/policy.rs index 146e1dc365..6597be4404 100644 --- a/codex-rs/rollout/src/policy.rs +++ b/codex-rs/rollout/src/policy.rs @@ -166,6 +166,7 @@ fn event_msg_persistence_mode(ev: &EventMsg) -> Option { | EventMsg::ItemStarted(_) | EventMsg::HookStarted(_) | EventMsg::HookCompleted(_) + | EventMsg::UserPromptSubmitStopped(_) | EventMsg::AgentMessageContentDelta(_) | EventMsg::PlanDelta(_) | EventMsg::ReasoningContentDelta(_) diff --git a/codex-rs/tui/src/app/app_server_event_targets.rs b/codex-rs/tui/src/app/app_server_event_targets.rs index bc0567df51..8eece85383 100644 --- a/codex-rs/tui/src/app/app_server_event_targets.rs +++ b/codex-rs/tui/src/app/app_server_event_targets.rs @@ -65,6 +65,9 @@ pub(super) fn server_notification_thread_target( ServerNotification::HookStarted(notification) => Some(notification.thread_id.as_str()), ServerNotification::TurnCompleted(notification) => Some(notification.thread_id.as_str()), ServerNotification::HookCompleted(notification) => Some(notification.thread_id.as_str()), + ServerNotification::UserPromptSubmitStopped(notification) => { + Some(notification.thread_id.as_str()) + } ServerNotification::TurnDiffUpdated(notification) => Some(notification.thread_id.as_str()), ServerNotification::TurnPlanUpdated(notification) => Some(notification.thread_id.as_str()), ServerNotification::ItemStarted(notification) => Some(notification.thread_id.as_str()), diff --git a/codex-rs/tui/src/app/thread_events.rs b/codex-rs/tui/src/app/thread_events.rs index 5b278bd2c1..c18a33d270 100644 --- a/codex-rs/tui/src/app/thread_events.rs +++ b/codex-rs/tui/src/app/thread_events.rs @@ -50,6 +50,7 @@ impl ThreadEventStore { ThreadBufferedEvent::Request(_) | ThreadBufferedEvent::Notification(ServerNotification::HookStarted(_)) | ThreadBufferedEvent::Notification(ServerNotification::HookCompleted(_)) + | ThreadBufferedEvent::Notification(ServerNotification::UserPromptSubmitStopped(_),) | ThreadBufferedEvent::FeedbackSubmission(_) ) } diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 021686679f..9386b25752 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -4042,6 +4042,14 @@ impl ChatWidget { self.request_redraw(); } + fn on_user_prompt_submit_stopped(&mut self) { + if self.user_turn_pending_start { + self.user_turn_pending_start = false; + self.maybe_send_next_queued_input(); + } + self.request_redraw(); + } + fn flush_completed_hook_output(&mut self) { let Some(completed_cell) = self .active_hook_cell @@ -6255,6 +6263,9 @@ impl ChatWidget { ServerNotification::HookCompleted(notification) => { self.on_hook_completed(notification.run); } + ServerNotification::UserPromptSubmitStopped(_) => { + self.on_user_prompt_submit_stopped(); + } ServerNotification::Error(notification) => { if notification.will_retry { if !from_replay { diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 77717faad8..64b7b8e676 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -116,6 +116,7 @@ pub(super) use codex_app_server_protocol::TurnStartedNotification; pub(super) use codex_app_server_protocol::TurnStatus as AppServerTurnStatus; pub(super) use codex_app_server_protocol::UserInput; pub(super) use codex_app_server_protocol::UserInput as AppServerUserInput; +pub(super) use codex_app_server_protocol::UserPromptSubmitStoppedNotification as AppServerUserPromptSubmitStoppedNotification; pub(super) use codex_app_server_protocol::WarningNotification; pub(super) use codex_config::AppRequirementToml; pub(super) use codex_config::AppsRequirementsToml; diff --git a/codex-rs/tui/src/chatwidget/tests/app_server.rs b/codex-rs/tui/src/chatwidget/tests/app_server.rs index 13b86e2afb..34a855a68a 100644 --- a/codex-rs/tui/src/chatwidget/tests/app_server.rs +++ b/codex-rs/tui/src/chatwidget/tests/app_server.rs @@ -170,6 +170,191 @@ async fn live_app_server_turn_completed_clears_working_status_after_answer_item( assert!(chat.bottom_pane.status_widget().is_none()); } +#[tokio::test] +async fn blocked_user_prompt_submit_hook_continues_queued_inputs() { + let (mut chat, _rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.thread_id = Some(ThreadId::new()); + + chat.handle_server_notification( + ServerNotification::TurnStarted(TurnStartedNotification { + thread_id: "thread-1".to_string(), + turn: AppServerTurn { + id: "turn-1".to_string(), + items: Vec::new(), + status: AppServerTurnStatus::InProgress, + error: None, + started_at: Some(0), + completed_at: None, + duration_ms: None, + }, + }), + /*replay_kind*/ None, + ); + + for text in ["blocked prompt", "allowed prompt"] { + chat.bottom_pane + .set_composer_text(text.to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE)); + } + + chat.handle_server_notification( + ServerNotification::TurnCompleted(TurnCompletedNotification { + thread_id: "thread-1".to_string(), + turn: AppServerTurn { + id: "turn-1".to_string(), + items: Vec::new(), + status: AppServerTurnStatus::Completed, + error: None, + started_at: Some(0), + completed_at: Some(1), + duration_ms: Some(1), + }, + }), + /*replay_kind*/ None, + ); + + assert_matches!( + next_submit_op(&mut op_rx), + Op::UserTurn { items, .. } + if items + == vec![AppServerUserInput::Text { + text: "blocked prompt".to_string(), + text_elements: Vec::new(), + }] + ); + + handle_hook_completed_for_turn( + &mut chat, + Some("turn-2"), + hook_run( + "user-prompt-submit:0:/tmp/hooks.json", + AppServerHookEventName::UserPromptSubmit, + AppServerHookRunStatus::Blocked, + "checking queued prompt policy", + vec![AppServerHookOutputEntry { + kind: AppServerHookOutputEntryKind::Feedback, + text: "blocked prompt".to_string(), + }], + ), + ); + + assert_matches!( + op_rx.try_recv(), + Ok(Op::AddToHistory { text }) if text == "blocked prompt" + ); + assert_matches!(op_rx.try_recv(), Err(TryRecvError::Empty)); + + handle_user_prompt_submit_stopped(&mut chat, "turn-2"); + + assert_matches!( + next_submit_op(&mut op_rx), + Op::UserTurn { items, .. } + if items + == vec![AppServerUserInput::Text { + text: "allowed prompt".to_string(), + text_elements: Vec::new(), + }] + ); +} + +#[tokio::test] +async fn blocked_user_prompt_submit_stopped_only_resumes_queue_once_after_multiple_handlers() { + let (mut chat, _rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.thread_id = Some(ThreadId::new()); + + chat.handle_server_notification( + ServerNotification::TurnStarted(TurnStartedNotification { + thread_id: "thread-1".to_string(), + turn: AppServerTurn { + id: "turn-1".to_string(), + items: Vec::new(), + status: AppServerTurnStatus::InProgress, + error: None, + started_at: Some(0), + completed_at: None, + duration_ms: None, + }, + }), + /*replay_kind*/ None, + ); + + for text in ["blocked prompt", "allowed prompt", "later prompt"] { + chat.bottom_pane + .set_composer_text(text.to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE)); + } + + chat.handle_server_notification( + ServerNotification::TurnCompleted(TurnCompletedNotification { + thread_id: "thread-1".to_string(), + turn: AppServerTurn { + id: "turn-1".to_string(), + items: Vec::new(), + status: AppServerTurnStatus::Completed, + error: None, + started_at: Some(0), + completed_at: Some(1), + duration_ms: Some(1), + }, + }), + /*replay_kind*/ None, + ); + + assert_matches!( + next_submit_op(&mut op_rx), + Op::UserTurn { items, .. } + if items + == vec![AppServerUserInput::Text { + text: "blocked prompt".to_string(), + text_elements: Vec::new(), + }] + ); + + for run_id in [ + "user-prompt-submit:0:/tmp/hooks.json", + "user-prompt-submit:1:/tmp/hooks.json", + ] { + handle_hook_completed_for_turn( + &mut chat, + Some("turn-2"), + hook_run( + run_id, + AppServerHookEventName::UserPromptSubmit, + AppServerHookRunStatus::Blocked, + "checking queued prompt policy", + vec![AppServerHookOutputEntry { + kind: AppServerHookOutputEntryKind::Feedback, + text: "blocked prompt".to_string(), + }], + ), + ); + } + + assert_matches!( + op_rx.try_recv(), + Ok(Op::AddToHistory { text }) if text == "blocked prompt" + ); + assert_matches!(op_rx.try_recv(), Err(TryRecvError::Empty)); + + handle_user_prompt_submit_stopped(&mut chat, "turn-2"); + + assert_matches!( + next_submit_op(&mut op_rx), + Op::UserTurn { items, .. } + if items + == vec![AppServerUserInput::Text { + text: "allowed prompt".to_string(), + text_elements: Vec::new(), + }] + ); + assert_matches!( + op_rx.try_recv(), + Ok(Op::AddToHistory { text }) if text == "allowed prompt" + ); + assert_matches!(op_rx.try_recv(), Err(TryRecvError::Empty)); + assert_eq!(chat.queued_user_message_texts(), vec!["later prompt"]); +} + #[tokio::test] async fn live_app_server_turn_started_sets_feedback_turn_id() { let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; diff --git a/codex-rs/tui/src/chatwidget/tests/helpers.rs b/codex-rs/tui/src/chatwidget/tests/helpers.rs index 6920689ef3..59336f6b14 100644 --- a/codex-rs/tui/src/chatwidget/tests/helpers.rs +++ b/codex-rs/tui/src/chatwidget/tests/helpers.rs @@ -1539,16 +1539,34 @@ pub(super) fn handle_hook_started(chat: &mut ChatWidget, run: AppServerHookRunSu } pub(super) fn handle_hook_completed(chat: &mut ChatWidget, run: AppServerHookRunSummary) { + handle_hook_completed_for_turn(chat, /*turn_id*/ None, run); +} + +pub(super) fn handle_hook_completed_for_turn( + chat: &mut ChatWidget, + turn_id: Option<&str>, + run: AppServerHookRunSummary, +) { chat.handle_server_notification( ServerNotification::HookCompleted(AppServerHookCompletedNotification { thread_id: thread_id(chat), - turn_id: None, + turn_id: turn_id.map(str::to_string), run, }), /*replay_kind*/ None, ); } +pub(super) fn handle_user_prompt_submit_stopped(chat: &mut ChatWidget, turn_id: &str) { + chat.handle_server_notification( + ServerNotification::UserPromptSubmitStopped(AppServerUserPromptSubmitStoppedNotification { + thread_id: thread_id(chat), + turn_id: turn_id.to_string(), + }), + /*replay_kind*/ None, + ); +} + pub(super) fn hook_run( run_id: &str, event_name: codex_app_server_protocol::HookEventName,