diff --git a/lib/apps/fabro-cli/src/commands/run/petri_stream.rs b/lib/apps/fabro-cli/src/commands/run/petri_stream.rs index f3079d40b..2e70dba49 100644 --- a/lib/apps/fabro-cli/src/commands/run/petri_stream.rs +++ b/lib/apps/fabro-cli/src/commands/run/petri_stream.rs @@ -163,15 +163,32 @@ impl<'a> PetriItem<'a> { self.value().pointer(pointer)?.as_str() } - /// The condition the applied route matched, as written: the edge a - /// `route.applied` record names is a key into the subject node's - /// `meta.edges`, whose entry carries the edge's `condition` when it - /// has one. - pub(crate) fn matched_condition(self) -> Option<&'a str> { + /// Metadata for the exact edge named by `route.applied`, not another + /// edge to the same target or the stage's preferred-label signal. + fn selected_edge(self) -> Option<&'a Value> { let edge = self.body()?.get("edge")?.as_u64()?; - self.node()? - .pointer(&format!("/meta/edges/{edge}/condition"))? + self.node()?.pointer(&format!("/meta/edges/{edge}")) + } + + pub(crate) fn selected_edge_label(self) -> Option<&'a str> { + self.selected_edge()? + .get("label")? .as_str() + .filter(|label| !label.is_empty()) + } + + /// The condition the applied route matched, as written. + pub(crate) fn matched_condition(self) -> Option<&'a str> { + self.selected_edge()?.get("condition")?.as_str() + } +} + +/// Put an edge label on the arrow in both progress and pretty events. +/// Escape controls so a workflow label cannot create extra terminal lines. +pub(crate) fn route_arrow(label: Option<&str>) -> String { + match label.filter(|label| !label.is_empty()) { + Some(label) => format!("── {} ──▶", label.escape_debug()), + None => "→".to_string(), } } @@ -427,7 +444,7 @@ pub(crate) fn format_pretty( Some(format!( "{ts} {} {} {} {}{}{}", styles.dim.apply_to(view.node_name().unwrap_or("?")), - styles.dim.apply_to("\u{2192}"), + styles.dim.apply_to(route_arrow(view.selected_edge_label())), target, styles.dim.apply_to(&transition), styles.dim.apply_to(detail), diff --git a/lib/apps/fabro-cli/src/commands/run/remote_workflow.rs b/lib/apps/fabro-cli/src/commands/run/remote_workflow.rs index 8bec413d7..a1742ce55 100644 --- a/lib/apps/fabro-cli/src/commands/run/remote_workflow.rs +++ b/lib/apps/fabro-cli/src/commands/run/remote_workflow.rs @@ -752,7 +752,10 @@ mod tests { #[tokio::test] async fn remote_workflow_dropped_waiter_cancels_child_before_checkout_cleanup() { - let (fake, git) = fake_git("printf '%s' $$ > pid; exec /bin/sleep 60"); + // Publish the PID only after writing it: file existence is the readiness + // signal. + let (fake, git) = + fake_git("printf '%s' $$ > pid.tmp; /bin/mv pid.tmp pid; exec /bin/sleep 60"); let checkout = tempfile::tempdir().unwrap(); let path = checkout.path().to_path_buf(); let worker = tokio::spawn(async move { @@ -1154,7 +1157,8 @@ mod tests { #[tokio::test] async fn remote_workflow_timeout_and_cancel_reap_owned_children() { for timeout in [true, false] { - let (root, mut git) = fake_git("printf '%s' $$ > pid; exec /bin/sleep 60"); + let (root, mut git) = + fake_git("printf '%s' $$ > pid.tmp; /bin/mv pid.tmp pid; exec /bin/sleep 60"); git.timeout = Duration::from_secs(2); let cancel = CancellationToken::new(); let trigger = async { diff --git a/lib/apps/fabro-cli/src/commands/run/run_progress/event.rs b/lib/apps/fabro-cli/src/commands/run/run_progress/event.rs index 24cfa271f..d5f9d173a 100644 --- a/lib/apps/fabro-cli/src/commands/run/run_progress/event.rs +++ b/lib/apps/fabro-cli/src/commands/run/run_progress/event.rs @@ -140,6 +140,7 @@ pub(super) enum ProgressEvent { LoopRestart { from_node: String, to_node: String, + label: Option, }, RunNotice { level: RunNoticeLevel, diff --git a/lib/apps/fabro-cli/src/commands/run/run_progress/info_display.rs b/lib/apps/fabro-cli/src/commands/run/run_progress/info_display.rs index 420db28a1..d9c9c43af 100644 --- a/lib/apps/fabro-cli/src/commands/run/run_progress/info_display.rs +++ b/lib/apps/fabro-cli/src/commands/run/run_progress/info_display.rs @@ -4,6 +4,7 @@ use fabro_types::RunNoticeLevel; use super::renderer::ProgressRenderer; use super::styles; +use crate::commands::run::petri_stream; use crate::shared::{format_duration_ms, tilde_path}; pub(super) struct InfoDisplay { @@ -72,16 +73,11 @@ impl InfoDisplay { return; } - let detail = if let Some(condition) = condition { - format!(" [{condition}]") - } else if let Some(label) = label { - format!(" \"{label}\"") - } else { - String::new() - }; + let detail = condition.map_or_else(String::new, |condition| format!(" [{condition}]")); + let arrow = petri_stream::route_arrow(label); Self::insert_info_line( renderer, - &format!("\u{2192} {from_node} \u{2192} {to_node}{detail}"), + &format!("\u{2192} {from_node} {arrow} {to_node}{detail}"), ); } @@ -90,14 +86,16 @@ impl InfoDisplay { renderer: &ProgressRenderer, from_node: &str, to_node: &str, + label: Option<&str>, ) { if !self.verbose { return; } + let arrow = petri_stream::route_arrow(label); Self::insert_info_line( renderer, - &format!("\u{21ba} {from_node} \u{2192} {to_node} (loop restart)"), + &format!("\u{21ba} {from_node} {arrow} {to_node} (loop restart)"), ); } diff --git a/lib/apps/fabro-cli/src/commands/run/run_progress/mod.rs b/lib/apps/fabro-cli/src/commands/run/run_progress/mod.rs index ee02a3e6f..aa188ab23 100644 --- a/lib/apps/fabro-cli/src/commands/run/run_progress/mod.rs +++ b/lib/apps/fabro-cli/src/commands/run/run_progress/mod.rs @@ -318,8 +318,13 @@ impl ProgressUI { condition.as_deref(), ); } - ProgressEvent::LoopRestart { from_node, to_node } => { - self.info.on_loop_restart(renderer, &from_node, &to_node); + ProgressEvent::LoopRestart { + from_node, + to_node, + label, + } => { + self.info + .on_loop_restart(renderer, &from_node, &to_node, label.as_deref()); } ProgressEvent::RunNotice { level, diff --git a/lib/apps/fabro-cli/src/commands/run/run_progress/petri.rs b/lib/apps/fabro-cli/src/commands/run/run_progress/petri.rs index 1ae1a5510..b84b4b7d8 100644 --- a/lib/apps/fabro-cli/src/commands/run/run_progress/petri.rs +++ b/lib/apps/fabro-cli/src/commands/run/run_progress/petri.rs @@ -156,12 +156,13 @@ pub(super) fn progress_events( vec![ProgressEvent::LoopRestart { from_node: node_id, to_node: to_node.to_string(), + label: view.selected_edge_label().map(str::to_owned), }] } else { vec![ProgressEvent::EdgeSelected { from_node: node_id, to_node: to_node.to_string(), - label: None, + label: view.selected_edge_label().map(str::to_owned), condition: derived .get("transition") .and_then(Value::as_str) diff --git a/lib/apps/fabro-cli/src/commands/run/run_progress/tests.rs b/lib/apps/fabro-cli/src/commands/run/run_progress/tests.rs index 38068927e..7a174bfce 100644 --- a/lib/apps/fabro-cli/src/commands/run/run_progress/tests.rs +++ b/lib/apps/fabro-cli/src/commands/run/run_progress/tests.rs @@ -284,3 +284,139 @@ fn base_header_uses_only_the_commit_when_source_branch_is_unknown_or_ambiguous() } } } + +fn applied_route(from: &str, to: &str, label: &Value, back: bool) -> RunStreamItem { + RunStreamItem { + run_id: fixtures::RUN_1, + stream_seq: 1, + kind: RunStreamItemKind::Petri, + id: "execution 0/19/0".to_string(), + recorded_at: 0, + item: json!({ + "origin": "core", + "context": {"invocation": 0, "execution": 0}, + "subject": {"node": {"name": from, "meta": {"edges": { + "0": {"to": to, "label": "Not selected"}, + "7": {"to": to, "label": label} + }}}, "firing": 2}, + "record": {"seq": 19, "body": {"event": "route.applied", + "kind": "edge", "firing": 2, "group": 0, "edge": 7}}, + "derived": {"target": {"name": to}, "transition": "Continue", "back": back} + }), + } +} + +fn route_progress(item: &RunStreamItem, verbose: bool) -> String { + let output = Output::default(); + let renderer = ProgressRenderer::new_plain(Box::new(output.clone()), false); + let mut ui = ProgressUI::with_renderer(renderer, verbose); + ui.handle_stream_item(item); + output.text() +} + +#[test] +fn selected_route_labels_render_on_arrows_in_progress_and_pretty_events() { + let mut transcript = String::new(); + for (from, to, label, back) in [ + ( + "Run tests", + "Open pull request", + json!("Tests passed"), + false, + ), + ( + "Review changes", + "Revise implementation", + json!("Changes requested"), + false, + ), + ("Approval", "Deploy", json!("Approved"), false), + ("Verify fix", "Investigate", json!("Still failing"), true), + ("work", "a", json!("yes"), false), + ("start", "work", Value::Null, false), + ("verify", "work", Value::Null, true), + ] { + let item = applied_route(from, to, &label, back); + let before = petri_stream::raw_line(&item).unwrap(); + transcript.push_str(&route_progress(&item, true)); + transcript.push_str( + &petri_stream::format_pretty(&item, &Styles::new(false), &mut PrettyState::default()) + .unwrap(), + ); + transcript.push('\n'); + assert_eq!(route_progress(&item, false), ""); + assert_eq!(petri_stream::raw_line(&item).unwrap(), before); + } + insta::assert_snapshot!(transcript, @r#" + → Run tests ── Tests passed ──▶ Open pull request + 00:00:00 Run tests ── Tests passed ──▶ Open pull request continue + → Review changes ── Changes requested ──▶ Revise implementation + 00:00:00 Review changes ── Changes requested ──▶ Revise implementation continue + → Approval ── Approved ──▶ Deploy + 00:00:00 Approval ── Approved ──▶ Deploy continue + ↺ Verify fix ── Still failing ──▶ Investigate (loop restart) + 00:00:00 Verify fix ── Still failing ──▶ Investigate continue (loop) + → work ── yes ──▶ a + 00:00:00 work ── yes ──▶ a continue + → start → work + 00:00:00 start → work continue + ↺ verify → work (loop restart) + 00:00:00 verify → work continue (loop) + "#); +} + +#[test] +fn selected_route_labels_coexist_with_transition_and_condition_details() { + let mut item = applied_route("verify", "finish", &json!("Tests passed"), false); + item.item["subject"]["node"]["meta"]["edges"]["7"]["condition"] = json!("outcome=succeeded"); + item.item["derived"]["transition"] = json!("Restart"); + let pretty = + petri_stream::format_pretty(&item, &Styles::new(false), &mut PrettyState::default()) + .unwrap(); + insta::assert_snapshot!(format!("{}{pretty}", route_progress(&item, true)), @r#" + → verify ── Tests passed ──▶ finish [restart] + 00:00:00 verify ── Tests passed ──▶ finish restart when outcome=succeeded + "#); +} + +#[test] +fn unavailable_selected_route_labels_preserve_unlabeled_output() { + let baseline = applied_route("work", "next", &Value::Null, false); + let mut cases = vec![ + applied_route("work", "next", &json!(""), false), + applied_route("work", "next", &json!(42), false), + ]; + let mut missing_edge = baseline.clone(); + missing_edge.item["record"]["body"]["edge"] = json!(99); + cases.push(missing_edge); + let mut missing_metadata = baseline.clone(); + missing_metadata.item["subject"]["node"]["meta"] = json!({}); + cases.push(missing_metadata); + let mut no_edge = baseline.clone(); + no_edge.item["record"]["body"] = json!({"event": "route.applied", "kind": "jump"}); + cases.push(no_edge); + for item in cases { + assert_eq!(route_progress(&item, true), route_progress(&baseline, true)); + assert_eq!( + petri_stream::format_pretty(&item, &Styles::new(false), &mut PrettyState::default()), + petri_stream::format_pretty( + &baseline, + &Styles::new(false), + &mut PrettyState::default() + ), + ); + } +} + +#[test] +fn selected_route_labels_escape_controls_and_preserve_unicode() { + let item = applied_route( + "review", + "revise", + &json!("Réessayer \"tests\"\n\u{1b}[31m"), + false, + ); + insta::assert_snapshot!(route_progress(&item, true), @r#" + → review ── Réessayer \"tests\"\n\u{1b}[31m ──▶ revise + "#); +}