Skip to content

Commit a102849

Browse files
committed
fix(server): reconcile completed turns before client dispatch
1 parent e86d6d9 commit a102849

2 files changed

Lines changed: 456 additions & 34 deletions

File tree

‎crates/jcode-app-core/src/server/client_lifecycle.rs‎

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ struct ProcessingMessage {
9191
server_failover: bool,
9292
}
9393

94+
type ProcessingCompletion = (u64, Result<()>, Option<String>);
95+
9496
struct ProcessingState<'a> {
9597
client_is_processing: &'a mut bool,
9698
message_id: &'a mut Option<u64>,
@@ -487,7 +489,7 @@ pub(super) async fn handle_client(
487489
// Per-client state
488490
let mut client_is_processing = false;
489491
let (processing_done_tx, mut processing_done_rx) =
490-
mpsc::unbounded_channel::<(u64, Result<()>, Option<String>)>();
492+
mpsc::unbounded_channel::<ProcessingCompletion>();
491493
let mut processing_task: Option<tokio::task::JoinHandle<()>> = None;
492494
let mut processing_message_id: Option<u64> = None;
493495
let mut processing_session_id: Option<String> = None;
@@ -770,33 +772,16 @@ pub(super) async fn handle_client(
770772
}
771773
}
772774
done = processing_done_rx.recv() => {
773-
if let Some((done_id, result, completion_report)) = done {
774-
if Some(done_id) != processing_message_id {
775-
crate::logging::warn(&format!(
776-
"Done event id={} doesn't match processing_message_id={:?}, dropping",
777-
done_id, processing_message_id
778-
));
779-
continue;
780-
}
781-
crate::logging::info(&format!(
782-
"Processing done for message id={}, result={}",
783-
done_id,
784-
if result.is_ok() { "ok" } else { "err" }
785-
));
786-
processing_message_id = None;
787-
processing_task = None;
788-
client_is_processing = false;
789-
{
790-
let mut connections = client_connections.write().await;
791-
if let Some(info) = connections.get_mut(&client_connection_id) {
792-
info.is_processing = false;
793-
info.current_tool_name = None;
794-
}
795-
}
796-
797-
let done_session = processing_session_id.take();
798-
record_processing_completion(
799-
done_session.as_deref(), result, completion_report,
775+
if let Some(done) = done {
776+
reconcile_processing_completions(
777+
Some(done), &mut processing_done_rx,
778+
&mut ProcessingState {
779+
client_is_processing: &mut client_is_processing,
780+
message_id: &mut processing_message_id,
781+
session_id: &mut processing_session_id,
782+
task: &mut processing_task,
783+
},
784+
&client_connection_id, &client_connections,
800785
&SwarmStatusRefs {
801786
members: &swarm_members,
802787
swarms_by_id: &swarms_by_id,
@@ -923,6 +908,28 @@ pub(super) async fn handle_client(
923908
}
924909
};
925910
let request_decoded_at = Instant::now();
911+
// Direct input wins the biased select above. A client may already have
912+
// observed Done/Error while this actor still holds the finished turn's
913+
// state. Completion is enqueued before the terminal event, so drain it
914+
// before any request (especially Message/SoftInterrupt) inspects that
915+
// state. Keep streaming terminal events on their ordered fanout channel.
916+
reconcile_processing_completions(
917+
None, &mut processing_done_rx,
918+
&mut ProcessingState {
919+
client_is_processing: &mut client_is_processing,
920+
message_id: &mut processing_message_id,
921+
session_id: &mut processing_session_id,
922+
task: &mut processing_task,
923+
},
924+
&client_connection_id, &client_connections,
925+
&SwarmStatusRefs {
926+
members: &swarm_members,
927+
swarms_by_id: &swarms_by_id,
928+
event_history: &event_history,
929+
event_counter: &event_counter,
930+
event_tx: &swarm_event_tx,
931+
},
932+
).await;
926933
// Reserve before Ack or any session mutation, not just when the turn
927934
// eventually polls. Upgrade requests themselves must not reserve work.
928935
let _request_activity = if matches!(&request, Request::UpgradeRuntime { .. }) {
@@ -3303,6 +3310,51 @@ pub(super) async fn handle_client(
33033310
connection_result
33043311
}
33053312

3313+
/// Retire finished turns before interpreting a request against client-local state.
3314+
/// The task publishes completion before its terminal event, and never waits on
3315+
/// this actor. Joining it also closes the cross-thread gap between those sends:
3316+
/// a new turn cannot start before the old terminal is on the ordered stream.
3317+
async fn reconcile_processing_completions(
3318+
mut completion: Option<ProcessingCompletion>,
3319+
receiver: &mut mpsc::UnboundedReceiver<ProcessingCompletion>,
3320+
state: &mut ProcessingState<'_>,
3321+
connection_id: &str,
3322+
connections: &Arc<RwLock<HashMap<String, ClientConnectionInfo>>>,
3323+
swarm: &SwarmStatusRefs<'_>,
3324+
) {
3325+
while let Some((done_id, result, completion_report)) =
3326+
completion.take().or_else(|| receiver.try_recv().ok())
3327+
{
3328+
if Some(done_id) != *state.message_id {
3329+
crate::logging::warn(&format!(
3330+
"Done event id={} doesn't match processing_message_id={:?}, dropping",
3331+
done_id, state.message_id
3332+
));
3333+
continue;
3334+
}
3335+
if let Some(task) = state.task.take() {
3336+
let _ = task.await;
3337+
}
3338+
crate::logging::info(&format!(
3339+
"Processing done for message id={}, result={}",
3340+
done_id,
3341+
if result.is_ok() { "ok" } else { "err" }
3342+
));
3343+
*state.message_id = None;
3344+
*state.client_is_processing = false;
3345+
{
3346+
let mut connections = connections.write().await;
3347+
if let Some(info) = connections.get_mut(connection_id) {
3348+
info.is_processing = false;
3349+
info.current_tool_name = None;
3350+
}
3351+
}
3352+
let done_session = state.session_id.take();
3353+
record_processing_completion(done_session.as_deref(), result, completion_report, swarm)
3354+
.await;
3355+
}
3356+
}
3357+
33063358
async fn record_processing_completion(
33073359
done_session: Option<&str>,
33083360
result: Result<()>,
@@ -3405,7 +3457,7 @@ async fn start_processing_message(
34053457
state: &mut ProcessingState<'_>,
34063458
agent: &Arc<Mutex<Agent>>,
34073459
client_event_tx: &mpsc::UnboundedSender<ServerEvent>,
3408-
processing_done_tx: &mpsc::UnboundedSender<(u64, Result<()>, Option<String>)>,
3460+
processing_done_tx: &mpsc::UnboundedSender<ProcessingCompletion>,
34093461
client_terminal_env: Vec<(String, String)>,
34103462
swarm: &SwarmStatusRefs<'_>,
34113463
) {
@@ -3572,8 +3624,12 @@ async fn start_processing_message(
35723624
if pending > 0 {
35733625
let _ = tx.send(ServerEvent::SoftInterruptQueue { pending });
35743626
}
3575-
let _ = tx.send(terminal_event);
3627+
// Publish completion first: observing Done/Error must imply that the
3628+
// actor can reconcile this turn before dispatching the next request.
3629+
// These sends do not await, and the terminal stays behind MessageEnd on
3630+
// the same fanout channel (including for newly attached clients).
35763631
let _ = done_tx.send((id, result, completion_report));
3632+
let _ = tx.send(terminal_event);
35773633
});
35783634
}
35793635

0 commit comments

Comments
 (0)