Skip to content

Commit 1e5536d

Browse files
committed
fix(tui): surface reasons for every cloud command failure
1 parent a102849 commit 1e5536d

2 files changed

Lines changed: 213 additions & 31 deletions

File tree

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

Lines changed: 193 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ fn start_cloud_command(
148148
if matches!(command, CloudCommand::Prepare { .. }) {
149149
app.pending_cloud_handoff = None;
150150
}
151-
app.push_display_message(DisplayMessage::error(format!(
152-
"Cannot locate the jcode binary: {error}"
153-
)));
151+
show_cloud_failure(
152+
app,
153+
trimmed,
154+
session_id,
155+
None,
156+
&format!("Cannot locate the jcode binary: {error}"),
157+
);
154158
return true;
155159
}
156160
};
@@ -192,9 +196,13 @@ fn start_cloud_command(
192196
if matches!(command, CloudCommand::Prepare { .. }) {
193197
app.pending_cloud_handoff = None;
194198
}
195-
app.push_display_message(DisplayMessage::error(format!(
196-
"Failed to run jcode cloud: {error}"
197-
)));
199+
show_cloud_failure(
200+
app,
201+
trimmed,
202+
session_id,
203+
None,
204+
&format!("Failed to run jcode cloud: {error}"),
205+
);
198206
return true;
199207
}
200208
};
@@ -435,9 +443,13 @@ impl App {
435443
if job.preparation_for.as_ref() == self.pending_cloud_handoff.as_ref() {
436444
self.pending_cloud_handoff = None;
437445
}
438-
self.push_display_message(DisplayMessage::error(format!(
439-
"Failed to run jcode cloud: {error}"
440-
)));
446+
show_cloud_failure(
447+
self,
448+
&job.trimmed,
449+
&job.session_id,
450+
None,
451+
&format!("Failed to run jcode cloud: {error}\n{}", job.stderr),
452+
);
441453
}
442454
}
443455
true
@@ -475,8 +487,8 @@ fn render_cloud_steps(steps: &[String], frame: usize, finished: Option<bool>) ->
475487
}
476488

477489
/// Keep remote diagnostics literal so Markdown cannot fold their newlines.
478-
fn format_cloud_failure(command: &str, stderr: &str) -> String {
479-
let details = stderr
490+
fn cloud_failure_details(stderr: &str) -> String {
491+
stderr
480492
.lines()
481493
.filter(|line| {
482494
let line = line.trim();
@@ -491,9 +503,63 @@ fn format_cloud_failure(command: &str, stderr: &str) -> String {
491503
&& line.contains("openssh.com/pq.html"))
492504
})
493505
.collect::<Vec<_>>()
494-
.join("\n");
506+
.join("\n")
507+
}
508+
509+
fn cloud_failure_reason(details: &str) -> &str {
510+
// Remote commands often wrap the useful cause in another Error line.
511+
details
512+
.lines()
513+
.rev()
514+
.find_map(|line| line.trim().strip_prefix("Error: "))
515+
.or_else(|| details.lines().map(str::trim).find(|line| !line.is_empty()))
516+
.unwrap_or("No error details were returned by the cloud command.")
517+
}
518+
519+
fn show_cloud_failure(
520+
app: &mut App,
521+
command: &str,
522+
session: &str,
523+
exit: Option<i32>,
524+
details: &str,
525+
) {
526+
record_cloud_problem(command, session, exit, details);
527+
app.push_display_message(DisplayMessage::error(format_cloud_failure(
528+
command, details,
529+
)));
530+
app.set_status_notice(&format!(
531+
"Cloud failed: {}",
532+
cloud_failure_reason(&cloud_failure_details(details))
533+
));
534+
}
535+
536+
fn cloud_output_failure(job: &CloudJob, output: &std::process::Output) -> String {
537+
let stderr = format!(
538+
"{}\n{}",
539+
job.stderr,
540+
String::from_utf8_lossy(&output.stderr)
541+
);
542+
if !cloud_failure_details(&stderr).trim().is_empty() {
543+
return stderr;
544+
}
545+
let stdout = String::from_utf8_lossy(&output.stdout);
546+
if !cloud_failure_details(&stdout).trim().is_empty() {
547+
return stdout.into_owned();
548+
}
549+
format!(
550+
"Cloud command exited with {} without error details.",
551+
output.status
552+
)
553+
}
554+
555+
/// Put the reason first while keeping complete, literal diagnostics below.
556+
fn format_cloud_failure(command: &str, stderr: &str) -> String {
557+
let details = cloud_failure_details(stderr);
495558
let details = details.trim();
496-
let mut message = format!("{command} failed. Nothing moved, the session is still here.");
559+
let mut message = format!(
560+
"{command} failed. Reason: {}\nCheck /cloud status before retrying if the session location is uncertain.",
561+
cloud_failure_reason(details)
562+
);
497563
if details.is_empty() {
498564
message.push_str("\n\nNo error details were returned by the cloud command.");
499565
return message;
@@ -574,37 +640,71 @@ fn finish_cloud_command(app: &mut App, job: CloudJob, output: std::process::Outp
574640
}
575641
} else {
576642
app.pending_cloud_handoff = None;
577-
record_cloud_problem(
578-
"prepare",
643+
app.replace_display_message_content(
644+
job.progress_message,
645+
render_cloud_steps(&job.steps, 0, Some(false)),
646+
);
647+
let details = if output.status.success() {
648+
"Cloud host preparation returned an invalid response: missing non-empty host."
649+
.to_string()
650+
} else {
651+
cloud_output_failure(&job, &output)
652+
};
653+
show_cloud_failure(
654+
app,
655+
"Cloud host preparation",
579656
&job.session_id,
580657
output.status.code(),
581-
&job.stderr,
658+
&details,
582659
);
583-
app.push_display_message(DisplayMessage::error(format_cloud_failure(
584-
"Cloud host preparation",
585-
&job.stderr,
586-
)));
587660
}
588661
return;
589662
}
663+
if !output.status.success() {
664+
let details = cloud_output_failure(&job, &output);
665+
show_cloud_failure(
666+
app,
667+
&job.trimmed,
668+
&job.session_id,
669+
output.status.code(),
670+
&details,
671+
);
672+
return;
673+
}
674+
if matches!(
675+
job.command,
676+
CloudCommand::Move { .. } | CloudCommand::Return
677+
) {
678+
let report = serde_json::from_slice::<serde_json::Value>(&output.stdout);
679+
let valid = report.as_ref().is_ok_and(|report| match job.command {
680+
CloudCommand::Move { .. } => {
681+
report["host"].as_str().is_some_and(|host| !host.is_empty())
682+
}
683+
CloudCommand::Return => report.is_object(),
684+
_ => unreachable!(),
685+
});
686+
if !valid {
687+
app.replace_display_message_content(
688+
job.progress_message,
689+
render_cloud_steps(&job.steps, 0, Some(false)),
690+
);
691+
show_cloud_failure(
692+
app,
693+
&job.trimmed,
694+
&job.session_id,
695+
output.status.code(),
696+
"Cloud command returned an invalid response. Could not confirm the handoff result. Run /cloud status to check the session location before retrying.",
697+
);
698+
return;
699+
}
700+
}
590701
let CloudJob {
591702
command,
592-
trimmed,
593703
session_id,
594-
stderr,
595704
..
596705
} = job;
597706
let session_id = session_id.as_str();
598-
let trimmed = trimmed.as_str();
599707
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
600-
if !output.status.success() {
601-
record_cloud_problem(trimmed, session_id, output.status.code(), &stderr);
602-
app.push_display_message(DisplayMessage::error(format_cloud_failure(
603-
trimmed, &stderr,
604-
)));
605-
app.set_status_notice("Cloud move failed");
606-
return;
607-
}
608708
match command {
609709
CloudCommand::Cancel | CloudCommand::Prepare { .. } => {
610710
unreachable!("handled before transfer completion")
@@ -1286,6 +1386,68 @@ mod tests {
12861386
assert!(!message.contains('\r'));
12871387
}
12881388

1389+
#[cfg(unix)]
1390+
#[test]
1391+
fn every_cloud_completion_failure_shows_a_reason() {
1392+
for command in [
1393+
CloudCommand::Move { host: None },
1394+
CloudCommand::Return,
1395+
CloudCommand::Where,
1396+
CloudCommand::Prepare { host: None },
1397+
] {
1398+
for source in ["stream", "stderr", "stdout", "empty", "wait", "invalid"] {
1399+
let mut app = crate::tui::app::tests::create_test_app();
1400+
app.is_processing = true;
1401+
if matches!(command, CloudCommand::Prepare { .. }) {
1402+
app.pending_cloud_handoff = Some(pending(
1403+
CloudCommand::Move { host: None },
1404+
"original-session",
1405+
));
1406+
}
1407+
if source == "invalid" && matches!(command, CloudCommand::Where) {
1408+
continue;
1409+
}
1410+
let tx = fake_job(&mut app, command.clone());
1411+
let cause = "cloud checkout contains unreturned work; refusing to overwrite";
1412+
let mut result = output(
1413+
source == "invalid",
1414+
if source == "stdout" { cause } else { "" },
1415+
);
1416+
if source == "stream" {
1417+
tx.send(CloudEvent::Stderr(format!(
1418+
"Error: remote failed\nError: {cause}"
1419+
)))
1420+
.unwrap();
1421+
} else if source == "stderr" {
1422+
result.stderr = cause.as_bytes().to_vec();
1423+
}
1424+
tx.send(CloudEvent::Done(if source == "wait" {
1425+
Err("could not wait for child".into())
1426+
} else {
1427+
Ok(result)
1428+
}))
1429+
.unwrap();
1430+
assert!(app.poll_cloud_job());
1431+
let message = &app.display_messages.last().unwrap().content;
1432+
let notice = &app.status_notice.as_ref().expect("failure notice").0;
1433+
let expected = match source {
1434+
"empty" => "without error details",
1435+
"invalid" => "invalid response",
1436+
"wait" => "could not wait for child",
1437+
_ => cause,
1438+
};
1439+
assert!(
1440+
message.lines().next().unwrap().contains(expected),
1441+
"{source}: {message}"
1442+
);
1443+
assert!(notice.contains(expected), "{source}: {notice}");
1444+
assert!(!app.should_quit);
1445+
assert!(app.cloud_handoff_requested.is_none());
1446+
assert!(app.pending_cloud_handoff.is_none());
1447+
}
1448+
}
1449+
}
1450+
12891451
#[test]
12901452
fn parses_cloud_commands() {
12911453
assert_eq!(

‎crates/jcode-tui/src/tui/ui_tests/prepare.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,3 +1372,23 @@ fn test_prepare_messages_keeps_multiline_error_rows() {
13721372
assert!(first < error && error < cause, "rows collapsed: {all}");
13731373
assert!(!rendered[first].contains("Error:"), "rows collapsed: {all}");
13741374
}
1375+
1376+
#[test]
1377+
fn cloud_failure_reason_is_visible_in_rendered_chat() {
1378+
for width in [40, 100] {
1379+
let state = TestState {
1380+
display_messages: vec![DisplayMessage::error(
1381+
"/cloud failed. Reason: Unreturned cloud changes would be overwritten. Recover the earlier session first.\n\nError: remote receive failed",
1382+
)],
1383+
..Default::default()
1384+
};
1385+
let rendered = prepare::prepare_messages(&state, width, 40)
1386+
.materialize_all_lines()
1387+
.iter()
1388+
.map(extract_line_text)
1389+
.collect::<Vec<_>>()
1390+
.join("\n");
1391+
assert!(rendered.contains("Reason:"), "{rendered}");
1392+
assert!(rendered.contains("session first."), "{rendered}");
1393+
}
1394+
}

0 commit comments

Comments
 (0)