Skip to content

Commit 4fdaaaf

Browse files
committed
Reject a sent message while the session displays a subagent thread
`codex send` submits through the chat widget, which routes to the displayed thread. With `/agents` or `/multi-agents` showing a subagent thread, a message for the primary thread was submitted to that subagent thread and reported as delivered. The session now rejects it with `the session is displaying another thread` and submits nothing. - Queued messages are not affected: they live in the primary thread's input state, which is stored on a view switch and restored on return. - A test covers rejection while a subagent thread is displayed and acceptance once the endpoint's thread is displayed again.
1 parent 00ae961 commit 4fdaaaf

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎codex-rs/send/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ command exits 0 when the recipient accepted the message, and prints one of:
1616
| --- | --- |
1717
| `Delivered message to thread …` | The text entered the turn pipeline, as a steer or a new turn. |
1818
| `Thread … accepted the message and will submit it when it can take input.` | The session is still connecting, paused after a disconnect, or waiting on a reserved turn start. The text is held in the session's follow-up queue. |
19+
| `thread … rejected the message: the session is displaying another thread` (exit 1) | The recipient is displaying one of its subagent threads (`/agents`, `/multi-agents`). Nothing was submitted; resend after it returns to the thread. |
1920
| `no live session is listening for thread …` (exit 1) | No session owns that thread, or the recipient opted out. |
2021
| `the session endpoint … is not accepting connections` (exit 1) | A session exited without cleanup. The next session on that thread replaces the stale socket. |
2122

‎codex-rs/tui/src/app/event_dispatch.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ impl App {
12391239
self.submit_thread_op(app_server, thread_id, op).await?;
12401240
}
12411241
AppEvent::SendMessageReceived { request, reply } => {
1242-
let response = self.chat_widget.submit_sent_message(request);
1242+
let response = self.receive_sent_message(request);
12431243
// A sender that gave up waiting has already closed its side.
12441244
let _ = reply.send(response);
12451245
}

‎codex-rs/tui/src/app/send_endpoint.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::sync::atomic::AtomicBool;
1010
use std::sync::atomic::Ordering;
1111

1212
use codex_protocol::ThreadId;
13+
use codex_send::SendRequest;
1314
use codex_send::SendResponse;
1415
use tokio::sync::oneshot;
1516
use tokio::task::JoinHandle;
@@ -48,6 +49,23 @@ impl Drop for SendEndpointTask {
4849
}
4950

5051
impl App {
52+
/// Rejects the message while a subagent thread is displayed: the chat
53+
/// widget submits to the displayed thread, not to the endpoint's thread.
54+
pub(super) fn receive_sent_message(&mut self, request: SendRequest) -> SendResponse {
55+
if !self.endpoint_thread_is_displayed() {
56+
return SendResponse::Rejected {
57+
message: "the session is displaying another thread".to_string(),
58+
};
59+
}
60+
self.chat_widget.submit_sent_message(request)
61+
}
62+
63+
fn endpoint_thread_is_displayed(&self) -> bool {
64+
self.send_endpoint
65+
.as_ref()
66+
.is_some_and(|endpoint| self.active_thread_id == Some(endpoint.thread_id))
67+
}
68+
5169
pub(super) fn bind_send_endpoint(&mut self, thread_id: ThreadId) {
5270
if !self.config.ipc_enabled {
5371
return;

‎codex-rs/tui/src/app/send_endpoint_tests.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ async fn rebinding_the_same_thread_waits_for_the_previous_cleanup() {
9292
assert!(tokio::fs::try_exists(&socket).await.expect("probe socket"));
9393
}
9494

95+
#[tokio::test]
96+
async fn sent_message_is_rejected_while_another_thread_is_displayed() {
97+
let mut app = make_test_app().await;
98+
let codex_home = short_codex_home();
99+
let primary = ThreadId::new();
100+
let displayed_subagent = ThreadId::new();
101+
app.primary_thread_id = Some(primary);
102+
app.send_endpoint = Some(SendEndpointTask {
103+
thread_id: primary,
104+
socket_path: session_socket_path(codex_home.path(), primary),
105+
bound: Arc::new(AtomicBool::new(false)),
106+
task: None,
107+
});
108+
let request = |id: &str| SendRequest {
109+
id: id.to_string(),
110+
text: "hello".to_string(),
111+
};
112+
113+
app.active_thread_id = Some(displayed_subagent);
114+
let response = app.receive_sent_message(request("r1"));
115+
116+
assert_eq!(
117+
response,
118+
codex_send::SendResponse::Rejected {
119+
message: "the session is displaying another thread".to_string(),
120+
}
121+
);
122+
assert!(!app.chat_widget.has_queued_follow_up_messages());
123+
124+
app.active_thread_id = Some(primary);
125+
let response = app.receive_sent_message(request("r2"));
126+
127+
assert_eq!(response, codex_send::SendResponse::Queued);
128+
assert!(app.chat_widget.has_queued_follow_up_messages());
129+
}
130+
95131
#[tokio::test]
96132
async fn endpoint_is_not_bound_when_ipc_is_disabled() {
97133
let mut app = make_test_app().await;

0 commit comments

Comments
 (0)