Skip to content

Commit cd8f8c8

Browse files
committed
Fix auto-poke canceling pending cloud handoffs
1 parent 7206efd commit cd8f8c8

3 files changed

Lines changed: 123 additions & 16 deletions

File tree

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

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(super) fn check_cloud_handoff_input(app: &mut App, trimmed: &str) -> bool {
5656
}
5757
if app.has_unsent_handoff_work(Some(trimmed)) {
5858
app.push_display_message(DisplayMessage::error(
59-
"Cloud handoff not queued: unsent messages, commands, draft text, or attachments would be lost. Send or save them, then retry /cloud or /local. Nothing was removed.".to_string(),
59+
format!("Cloud handoff not queued: {} would be lost. Send or save that work, then retry /cloud or /local. Nothing was removed.", app.unsent_handoff_work(Some(trimmed)).join(", ")),
6060
));
6161
return false;
6262
}
@@ -289,20 +289,48 @@ impl App {
289289
}
290290

291291
fn has_unsent_handoff_work(&self, accepted_command: Option<&str>) -> bool {
292+
!self.unsent_handoff_work(accepted_command).is_empty()
293+
}
294+
295+
// Report categories, never private draft contents, so an empty-looking
296+
// composer does not leave users guessing which queue blocked the move.
297+
fn unsent_handoff_work(&self, accepted_command: Option<&str>) -> Vec<&'static str> {
292298
let draft = self.input.trim();
293-
(!draft.is_empty() && accepted_command != Some(draft))
294-
|| !self.queued_messages.is_empty()
295-
|| !self.queued_commands.is_empty()
296-
|| !self.hidden_queued_system_messages.is_empty()
297-
|| !self.pending_images.is_empty()
298-
|| !self.pasted_contents.is_empty()
299-
|| self
300-
.interleave_message
301-
.as_ref()
302-
.is_some_and(|text| !text.is_empty())
303-
|| !self.interleave_images.is_empty()
304-
|| !self.pending_soft_interrupts.is_empty()
305-
|| !self.pending_soft_interrupt_requests.is_empty()
299+
[
300+
(
301+
!draft.is_empty() && accepted_command != Some(draft),
302+
"composer draft",
303+
),
304+
(!self.queued_messages.is_empty(), "queued messages"),
305+
(!self.queued_commands.is_empty(), "queued commands"),
306+
(
307+
!self.hidden_queued_system_messages.is_empty(),
308+
"queued system reminders",
309+
),
310+
(!self.pending_images.is_empty(), "image attachments"),
311+
(!self.pasted_contents.is_empty(), "pasted contents"),
312+
(
313+
self.interleave_message
314+
.as_ref()
315+
.is_some_and(|text| !text.is_empty()),
316+
"pending interleave message",
317+
),
318+
(
319+
!self.interleave_images.is_empty(),
320+
"pending interleave images",
321+
),
322+
(
323+
!self.pending_soft_interrupts.is_empty(),
324+
"pending interrupts",
325+
),
326+
(
327+
!self.pending_soft_interrupt_requests.is_empty(),
328+
"unacknowledged interrupts",
329+
),
330+
]
331+
.into_iter()
332+
.filter_map(|(present, label)| present.then_some(label))
333+
.collect()
306334
}
307335

308336
/// Shared by polling and deterministic dispatch tests. No process starts here.
@@ -318,9 +346,12 @@ impl App {
318346
if changed_session || self.has_unsent_handoff_work(None) {
319347
self.pending_cloud_handoff = None;
320348
let reason = if changed_session {
321-
"the active session changed. Retry /cloud or /local from the session you want to move"
349+
"the active session changed. Retry /cloud or /local from the session you want to move".to_string()
322350
} else {
323-
"unsent messages, commands, draft text, or attachments would be lost. Send or save them, then retry /cloud or /local"
351+
format!(
352+
"{} would be lost. Send or save that work, then retry /cloud or /local",
353+
self.unsent_handoff_work(None).join(", ")
354+
)
324355
};
325356
self.push_display_message(DisplayMessage::error(format!(
326357
"Pending cloud handoff canceled: {reason}. Nothing was removed."
@@ -728,6 +759,27 @@ mod tests {
728759
]
729760
}
730761

762+
#[test]
763+
fn handoff_error_identifies_blocking_queue_without_its_contents() {
764+
let mut app = crate::tui::app::tests::create_test_app();
765+
let session = super::super::commands::active_session_id(&app);
766+
app.hidden_queued_system_messages
767+
.push("private reminder".into());
768+
handle_cloud_command(&mut app, "/cloud", &session);
769+
let error = &app.display_messages.last().unwrap().content;
770+
assert!(error.contains("queued system reminders would be lost"));
771+
assert!(!error.contains("private reminder"));
772+
773+
app.hidden_queued_system_messages.clear();
774+
app.pending_cloud_handoff = Some(pending(CloudCommand::Return, &session));
775+
app.pending_soft_interrupt_requests
776+
.push((1, "private interrupt".into()));
777+
assert!(app.take_ready_cloud_handoff().is_none());
778+
let error = &app.display_messages.last().unwrap().content;
779+
assert!(error.contains("unacknowledged interrupts would be lost"));
780+
assert!(!error.contains("private interrupt"));
781+
}
782+
731783
#[test]
732784
fn acceptance_refuses_unsent_state_without_consuming_it() {
733785
for (label, populate) in unsent_cases() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,12 @@ impl App {
15631563
self.stop_auto_continuation_after_guardrail();
15641564
return false;
15651565
}
1566+
// A handoff owns this idle boundary. Queuing a local continuation here
1567+
// makes its dispatch safety check see new unsent work and cancel the
1568+
// transfer, even though the user did not submit anything else.
1569+
if self.cloud_handoff_blocks_work() {
1570+
return false;
1571+
}
15661572
// The user's own next prompt goes first. A poke queued now would be
15671573
// merged into it (or sent ahead of it) after an Esc redirect.
15681574
if self.has_pending_user_followup() {

‎crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,55 @@ fn test_open_work_rearms_auto_poke_after_completion_gate_breaker() {
5050

5151
include!("model_picker_startup_tests.rs");
5252

53+
#[test]
54+
fn test_cloud_handoff_defers_turn_end_auto_poke_without_disarming_it() {
55+
with_temp_jcode_home(|| {
56+
for command in ["/cloud", "/local"] {
57+
let mut app = create_test_app();
58+
crate::todo::save_todos(
59+
&app.session.id,
60+
&[crate::todo::TodoItem {
61+
stage: None,
62+
details: None,
63+
group: None,
64+
id: "open".into(),
65+
content: "Continue on the destination".into(),
66+
status: "in_progress".into(),
67+
priority: "high".into(),
68+
blocked_by: Vec::new(),
69+
assigned_to: None,
70+
confidence: None,
71+
completion_confidence: None,
72+
confidence_history: Vec::new(),
73+
}],
74+
)
75+
.unwrap();
76+
app.auto_poke_default_on = true;
77+
app.auto_poke_incomplete_todos = true;
78+
app.is_processing = true;
79+
let session = app.session.id.clone();
80+
assert!(super::commands_cloud::handle_cloud_command(
81+
&mut app, command, &session
82+
));
83+
84+
// The local completion path used to enqueue a poke here, which
85+
// immediately made the pending transfer fail its safety check.
86+
// Remote Done uses the same turn-end scheduling entry point.
87+
super::local::finish_turn(&mut app);
88+
assert!(app.pending_cloud_handoff.is_some());
89+
assert!(app.queued_messages.is_empty());
90+
assert!(!app.pending_queued_dispatch);
91+
assert!(app.auto_poke_incomplete_todos);
92+
assert!(app.last_auto_poke_fingerprint.is_none());
93+
94+
// Cancellation restores normal scheduling, not a disabled poke.
95+
super::commands_cloud::handle_cloud_command(&mut app, "/cloud cancel", &session);
96+
assert!(app.schedule_turn_end_followups());
97+
assert!(!app.queued_messages.is_empty());
98+
}
99+
});
100+
}
101+
53102
#[test]
54103
fn test_remote_model_command_opens_picker_without_catalog_request() {
55104
let mut app = create_test_app();

0 commit comments

Comments
 (0)