Skip to content

Commit f18b6db

Browse files
committed
fix(tui): wait for history before sending fork startup prompts
1 parent a90e6eb commit f18b6db

2 files changed

Lines changed: 44 additions & 88 deletions

File tree

‎crates/jcode-tui/src/tui/app/remote.rs‎

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,56 +1377,10 @@ pub(super) async fn process_remote_followups(app: &mut App, remote: &mut RemoteC
13771377
return;
13781378
}
13791379

1380-
// A headed fork stages its first prompt before launching the new client. We
1381-
// can send that prompt immediately after Subscribe, without waiting for the
1382-
// client to receive and render History: requests and events share one
1383-
// ordered socket, so the server finishes writing the Subscribe History
1384-
// response before it reads this Message request. Do not echo the user turn
1385-
// locally here because the still-in-flight History payload would clear it.
1386-
// Preserve the echo and apply it immediately after History instead.
1387-
//
1388-
// This removes the visible, intermittent pause between the fork window
1389-
// opening and its prompt starting, which was proportional to history payload
1390-
// transfer/render time for large parent sessions.
1391-
if !remote.has_loaded_history()
1392-
&& app.submit_input_on_startup
1393-
&& !app.is_processing
1394-
&& !app.remote_model_switch_in_flight
1395-
&& !app.auth_catalog_refresh_pending
1396-
&& (!app.input.is_empty() || !app.pending_images.is_empty())
1397-
{
1398-
app.submit_input_on_startup = false;
1399-
app.startup_submit_deferred_reason = None;
1400-
let prepared = input::take_prepared_input(app);
1401-
app.pending_startup_prompt_echo = Some(prepared.raw_input.clone());
1402-
app.last_submitted_input = Some(prepared.raw_input);
1403-
crate::logging::info(&format!(
1404-
"Startup auto-submit sent behind ordered Subscribe: input_chars={} pending_images={}",
1405-
prepared.expanded.chars().count(),
1406-
prepared.images.len(),
1407-
));
1408-
if let Err(error) = begin_remote_send(
1409-
app,
1410-
remote,
1411-
prepared.expanded,
1412-
prepared.images,
1413-
false,
1414-
None,
1415-
false,
1416-
0,
1417-
)
1418-
.await
1419-
{
1420-
crate::logging::warn(&format!("Early startup auto-submit failed: {error}"));
1421-
app.push_display_message(DisplayMessage::error(format!(
1422-
"Failed to submit startup prompt: {}",
1423-
error
1424-
)));
1425-
app.set_status_notice("Startup prompt failed");
1426-
}
1427-
return;
1428-
}
1429-
1380+
// Drain Subscribe's History before sending the staged startup prompt.
1381+
// Both directions can carry large image payloads: awaiting a prompt write
1382+
// here would prevent this event loop from reading History while the server
1383+
// is still writing it, deadlocking both peers on socket backpressure.
14301384
if !remote.has_loaded_history() {
14311385
note_startup_submit_deferred(app, "remote history not loaded yet");
14321386
return;

‎crates/jcode-tui/src/tui/app/remote_tests.rs‎

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,13 @@ fn process_remote_followups_auto_submits_staged_startup_prompt() {
417417
}
418418

419419
#[test]
420-
fn process_remote_followups_sends_startup_prompt_before_history_arrives() {
420+
fn process_remote_followups_waits_for_history_before_sending_startup_image() {
421421
let mut app = create_test_app();
422422
app.is_remote = true;
423423
app.runtime_mode = crate::tui::app::AppRuntimeMode::RemoteClient;
424424
app.input = "Start the fork immediately".to_string();
425+
let images = vec![("image/png".to_string(), "aW1hZ2U=".to_string())];
426+
app.pending_images = images.clone();
425427
app.cursor_pos = app.input.len();
426428
app.submit_input_on_startup = true;
427429

@@ -430,57 +432,56 @@ fn process_remote_followups_sends_startup_prompt_before_history_arrives() {
430432
let mut remote = crate::tui::backend::RemoteConnection::dummy();
431433
assert!(!remote.has_loaded_history());
432434

435+
let next_request_id = remote.next_request_id_for_test();
433436
rt.block_on(process_remote_followups(&mut app, &mut remote));
434437

435-
assert!(!app.submit_input_on_startup);
436-
assert!(app.input.is_empty());
437-
assert!(
438-
app.is_processing,
439-
"the ordered startup request should start immediately"
440-
);
438+
assert!(app.submit_input_on_startup);
439+
assert_eq!(app.input, "Start the fork immediately");
440+
assert_eq!(app.pending_images, images);
441+
assert!(!app.is_processing);
442+
assert!(app.current_message_id.is_none());
443+
assert!(app.pending_startup_prompt_echo.is_none());
444+
assert_eq!(remote.next_request_id_for_test(), next_request_id);
441445
assert!(
442-
app.pending_startup_prompt_echo.as_deref() == Some("Start the fork immediately"),
443-
"the user echo must be retained until bootstrap History is applied"
446+
!app.display_messages()
447+
.iter()
448+
.any(|message| message.role == "user")
444449
);
445450

446-
let startup_id = app.current_message_id.expect("startup request was sent");
447-
let next_request_id = remote.next_request_id_for_test();
448-
let processing_started = app.processing_started;
449-
let visible_turn_started = app.visible_turn_started;
450-
// A queued recovery continuation must not dispatch behind the already-sent
451-
// startup turn when Subscribe's earlier, idle History snapshot arrives.
452-
app.hidden_queued_system_messages
453-
.push("Continue after reload".to_string());
454451
handle_server_event(&mut app, startup_history("startup-session"), &mut remote);
455452
assert!(remote.has_loaded_history());
456453
assert_eq!(app.remote_session_id.as_deref(), Some("startup-session"));
454+
assert!(!app.is_processing);
455+
assert_eq!(remote.next_request_id_for_test(), next_request_id);
456+
457+
rt.block_on(process_remote_followups(&mut app, &mut remote));
458+
assert!(!app.submit_input_on_startup);
459+
assert!(app.input.is_empty());
460+
assert!(app.pending_images.is_empty());
457461
assert!(app.is_processing);
458462
assert!(matches!(app.status, crate::tui::ProcessingStatus::Sending));
459-
assert_eq!(app.current_message_id, Some(startup_id));
460-
assert_eq!(app.processing_started, processing_started);
461-
assert_eq!(app.visible_turn_started, visible_turn_started);
462-
assert_eq!(
463-
app.rate_limit_pending_message
464-
.as_ref()
465-
.map(|pending| pending.content.as_str()),
466-
Some("Start the fork immediately")
467-
);
463+
let startup_id = app.current_message_id.expect("startup request was sent");
464+
assert_eq!(startup_id, next_request_id);
465+
let sent = app
466+
.rate_limit_pending_message
467+
.as_ref()
468+
.expect("sent payload");
469+
assert_eq!(sent.content, "Start the fork immediately");
470+
assert_eq!(sent.images, images);
468471

472+
let next_request_id = remote.next_request_id_for_test();
469473
rt.block_on(process_remote_followups(&mut app, &mut remote));
470-
assert_eq!(
471-
remote.next_request_id_for_test(),
472-
next_request_id,
473-
"History must not enable a second send"
474-
);
474+
assert_eq!(remote.next_request_id_for_test(), next_request_id);
475475
assert_eq!(app.current_message_id, Some(startup_id));
476-
assert!(app.is_processing);
477476
assert_eq!(
478-
app.hidden_queued_system_messages,
479-
vec!["Continue after reload"]
477+
app.display_messages()
478+
.iter()
479+
.filter(|message| {
480+
message.role == "user" && message.content == "Start the fork immediately"
481+
})
482+
.count(),
483+
1
480484
);
481-
assert!(app.display_messages().iter().any(|message| {
482-
message.role == "user" && message.content == "Start the fork immediately"
483-
}));
484485
assert!(app.pending_startup_prompt_echo.is_none());
485486
}
486487

@@ -628,9 +629,10 @@ fn startup_send_state_is_not_preserved_for_real_session_switch() {
628629
let rt = tokio::runtime::Runtime::new().expect("runtime");
629630
let _guard = rt.enter();
630631
let mut remote = crate::tui::backend::RemoteConnection::dummy();
632+
remote.mark_history_loaded();
631633
rt.block_on(process_remote_followups(&mut app, &mut remote));
632634
assert!(app.current_message_id.is_some());
633-
assert!(app.pending_startup_prompt_echo.is_some());
635+
assert!(app.is_processing);
634636

635637
handle_server_event(&mut app, startup_history("different-session"), &mut remote);
636638

0 commit comments

Comments
 (0)