Skip to main content

bijux_cli/interface/cli/
dispatch.rs

1//! Top-level application entrypoint and route execution.
2
3mod delegation;
4mod help;
5mod policy;
6mod route_exec;
7mod suggest;
8
9use anyhow::Result;
10use serde_json::json;
11
12use crate::contracts::OutputFormat;
13use crate::interface::cli::handlers::install as install_handler;
14use crate::interface::cli::help::render_command_help;
15use crate::interface::cli::parser::parse_intent;
16use crate::routing::model::{alias_rewrites, built_in_route_paths};
17use crate::shared::output::render_value;
18use crate::shared::telemetry::{
19    truncate_chars, TelemetrySpan, MAX_COMMAND_FIELD_CHARS, MAX_TEXT_FIELD_CHARS,
20};
21
22const MAX_PATH_FIELD_SEGMENTS: usize = 32;
23const MAX_PATH_SEGMENT_CHARS: usize = 128;
24
25/// In-memory process output and exit result produced by the core app runner.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct AppRunResult {
28    /// Process exit code.
29    pub exit_code: i32,
30    /// Payload that should be written to stdout.
31    pub stdout: String,
32    /// Payload that should be written to stderr.
33    pub stderr: String,
34}
35
36fn root_usage_help_text() -> Result<String> {
37    let help_argv = vec!["bijux".to_string(), "--help".to_string()];
38    if let Some(help) = help::try_render_clap_help(&help_argv) {
39        return Ok(help);
40    }
41
42    Ok(format!("{}\n", render_command_help(&[])?.trim_end()))
43}
44
45fn bounded_command(command: &str) -> (String, bool) {
46    truncate_chars(command, MAX_COMMAND_FIELD_CHARS)
47}
48
49fn bounded_message(message: &str) -> (String, bool) {
50    truncate_chars(message, MAX_TEXT_FIELD_CHARS)
51}
52
53fn bounded_status(status: Option<&str>) -> (Option<String>, bool) {
54    match status {
55        Some(value) => {
56            let (bounded, truncated) = bounded_message(value);
57            (Some(bounded), truncated)
58        }
59        None => (None, false),
60    }
61}
62
63fn bounded_segments(path: &[String]) -> (Vec<String>, usize, usize) {
64    let mut bounded = Vec::with_capacity(path.len().min(MAX_PATH_FIELD_SEGMENTS));
65    let mut truncated_segment_count = 0usize;
66
67    for segment in path.iter().take(MAX_PATH_FIELD_SEGMENTS) {
68        let (value, truncated) = truncate_chars(segment, MAX_PATH_SEGMENT_CHARS);
69        bounded.push(value);
70        if truncated {
71            truncated_segment_count += 1;
72        }
73    }
74
75    let clipped_segment_count = path.len().saturating_sub(MAX_PATH_FIELD_SEGMENTS);
76    (bounded, truncated_segment_count, clipped_segment_count)
77}
78
79fn levenshtein_distance(left: &str, right: &str) -> usize {
80    if left.is_empty() {
81        return right.chars().count();
82    }
83    if right.is_empty() {
84        return left.chars().count();
85    }
86
87    let left_chars = left.chars().collect::<Vec<_>>();
88    let right_chars = right.chars().collect::<Vec<_>>();
89    let mut prev = (0..=right_chars.len()).collect::<Vec<usize>>();
90    let mut curr = vec![0usize; right_chars.len() + 1];
91
92    for (i, left_ch) in left_chars.iter().enumerate() {
93        curr[0] = i + 1;
94        for (j, right_ch) in right_chars.iter().enumerate() {
95            let substitution_cost = usize::from(left_ch != right_ch);
96            curr[j + 1] = (curr[j] + 1).min(prev[j + 1] + 1).min(prev[j] + substitution_cost);
97        }
98        std::mem::swap(&mut prev, &mut curr);
99    }
100
101    prev[right_chars.len()]
102}
103
104fn known_help_topics() -> Vec<String> {
105    let mut topics = built_in_route_paths().to_vec();
106    topics.extend(alias_rewrites().iter().map(|(alias, _)| (*alias).to_string()));
107    topics.push("help".to_string());
108    let mut expanded = Vec::new();
109    for topic in topics {
110        let mut prefix = Vec::new();
111        for segment in topic.split_whitespace() {
112            prefix.push(segment);
113            expanded.push(prefix.join(" "));
114        }
115    }
116    expanded.sort();
117    expanded.dedup();
118    expanded
119}
120
121fn is_known_help_topic(path: &[String]) -> bool {
122    if path.is_empty() {
123        return true;
124    }
125    let requested = path.join(" ").to_ascii_lowercase();
126    known_help_topics().iter().any(|candidate| candidate.eq_ignore_ascii_case(requested.as_str()))
127}
128
129fn suggest_help_topics(requested: &str) -> Vec<String> {
130    let requested = requested.trim().to_ascii_lowercase();
131    if requested.is_empty() {
132        return Vec::new();
133    }
134
135    let mut scored = Vec::new();
136    for candidate in known_help_topics() {
137        let normalized = candidate.to_ascii_lowercase();
138        if normalized == requested {
139            continue;
140        }
141        let prefix_match =
142            normalized.starts_with(&requested) || requested.starts_with(normalized.as_str());
143        let distance = levenshtein_distance(&requested, &normalized);
144        let threshold = (requested.chars().count().max(normalized.chars().count()) / 3).max(2);
145        if prefix_match || distance <= threshold {
146            scored.push((!prefix_match, distance, normalized.len(), candidate));
147        }
148    }
149
150    scored.sort();
151    scored.into_iter().map(|(_, _, _, candidate)| candidate).take(3).collect()
152}
153
154fn unknown_help_topic_result(requested: &str, telemetry: &TelemetrySpan) -> AppRunResult {
155    let suggestions = suggest_help_topics(requested);
156    let (requested_bounded, requested_truncated) = bounded_command(requested);
157    telemetry.record(
158        "dispatch.help.unknown_topic",
159        json!({
160            "requested": requested_bounded,
161            "requested_truncated": requested_truncated,
162            "suggestions_count": suggestions.len(),
163            "exit_code": 2,
164        }),
165    );
166    let mut stderr = format!("Unknown help topic: {requested}.\n");
167    if !suggestions.is_empty() {
168        stderr.push_str("Did you mean:\n");
169        for suggestion in suggestions {
170            stderr.push_str(&format!("  bijux help {suggestion}\n"));
171        }
172    }
173    stderr.push_str("Run `bijux --help` for available runtime commands.\n");
174    AppRunResult { exit_code: 2, stdout: String::new(), stderr }
175}
176
177/// Execute the CLI for provided argv and return output streams and exit code.
178pub fn run_app(argv: &[String]) -> Result<AppRunResult> {
179    let telemetry = TelemetrySpan::start("bijux-cli", argv);
180    telemetry.record("dispatch.entry", json!({"argv_count": argv.len()}));
181    let result = run_app_inner(argv, &telemetry);
182    match &result {
183        Ok(value) => telemetry.finish_exit(value.exit_code, value.stdout.len(), value.stderr.len()),
184        Err(error) => telemetry.finish_internal_error(&error.to_string(), 1),
185    }
186    result
187}
188
189fn run_app_inner(argv: &[String], telemetry: &TelemetrySpan) -> Result<AppRunResult> {
190    if argv.len() == 1 {
191        telemetry.record("dispatch.help.default", json!({"reason":"no_args"}));
192        let help_text = match root_usage_help_text() {
193            Ok(help) => help,
194            Err(error) => {
195                let (message, message_truncated) = bounded_message(&error.to_string());
196                telemetry.record(
197                    "dispatch.help.render.error",
198                    json!({"message": message, "message_truncated": message_truncated}),
199                );
200                return Err(error);
201            }
202        };
203        telemetry.record("dispatch.help.rendered", json!({"topic":"root", "exit_code": 0}));
204        return Ok(AppRunResult {
205            exit_code: 0,
206            stdout: format!("{}\n", help_text.trim_end()),
207            stderr: String::new(),
208        });
209    }
210
211    if argv.len() == 2 && matches!(argv[1].as_str(), "--version" | "-V") {
212        let (flag, flag_truncated) = bounded_command(&argv[1]);
213        telemetry.record(
214            "dispatch.version.alias",
215            json!({"flag": flag, "flag_truncated": flag_truncated}),
216        );
217        let normalized = vec![argv[0].clone(), "version".to_string()];
218        return run_app_inner(&normalized, telemetry);
219    }
220
221    if argv.len() >= 2 && argv[1] == "help" {
222        let path = match help::parse_help_command_path(argv) {
223            Ok(path) => path,
224            Err(message) => {
225                let (bounded, message_truncated) = bounded_message(&message);
226                telemetry.record(
227                    "dispatch.help.error",
228                    json!({"message": bounded, "message_truncated": message_truncated, "exit_code": 2}),
229                );
230                let mut stderr = message;
231                stderr.push('\n');
232                stderr.push_str("Run `bijux --help` for available runtime commands.\n");
233                return Ok(AppRunResult { exit_code: 2, stdout: String::new(), stderr });
234            }
235        };
236        let path_refs: Vec<&str> = path.iter().map(String::as_str).collect();
237        if delegation::is_known_bijux_tool_route(&path) {
238            let mut delegated_argv = vec!["bijux".to_string()];
239            delegated_argv.extend(path.clone());
240            delegated_argv.push("--help".to_string());
241            if let Some(delegated) = delegation::try_delegate_known_bijux_tool(&delegated_argv) {
242                let surface = delegation::delegated_command_surface(&delegated_argv)
243                    .unwrap_or_else(|| path.join(" "));
244                let (target, target_truncated) = bounded_command(&surface);
245                telemetry.record(
246                    "dispatch.delegated.help",
247                    json!({"target": target, "target_truncated": target_truncated, "exit_code": delegated.exit_code}),
248                );
249                return Ok(delegated);
250            }
251        }
252        if !is_known_help_topic(&path) {
253            return Ok(unknown_help_topic_result(&path_refs.join(" "), telemetry));
254        }
255        let rendered = match render_command_help(&path_refs) {
256            Ok(rendered) => rendered,
257            Err(_) => return Ok(unknown_help_topic_result(&path_refs.join(" "), telemetry)),
258        };
259        let topic = if path.is_empty() { "root".to_string() } else { path.join(" ") };
260        let (topic_bounded, topic_truncated) = bounded_command(&topic);
261        telemetry.record(
262            "dispatch.help.rendered",
263            json!({
264                "topic": topic_bounded,
265                "topic_truncated": topic_truncated,
266                "exit_code": 0,
267            }),
268        );
269        return Ok(AppRunResult {
270            exit_code: 0,
271            stdout: format!("{}\n", rendered.trim_end()),
272            stderr: String::new(),
273        });
274    }
275
276    let has_help_flag = argv.iter().any(|arg| matches!(arg.as_str(), "--help" | "-h"));
277    if has_help_flag && delegation::is_known_bijux_tool_route(&argv[1..]) {
278        if let Some(delegated) = delegation::try_delegate_known_bijux_tool(argv) {
279            let surface = delegation::delegated_command_surface(argv).unwrap_or_default();
280            let (target, target_truncated) = bounded_command(&surface);
281            telemetry.record(
282                "dispatch.delegated.help_flag",
283                json!({"target": target, "target_truncated": target_truncated, "exit_code": delegated.exit_code}),
284            );
285            return Ok(delegated);
286        }
287    }
288
289    if let Some(help) = help::try_render_clap_help(argv) {
290        telemetry.record(
291            "dispatch.clap.short_circuit",
292            json!({"kind":"help_or_version", "exit_code": 0}),
293        );
294        return Ok(AppRunResult { exit_code: 0, stdout: help, stderr: String::new() });
295    }
296
297    if let Some(delegated) = delegation::try_delegate_known_bijux_tool(argv) {
298        let surface = delegation::delegated_command_surface(argv).unwrap_or_default();
299        let (target, target_truncated) = bounded_command(&surface);
300        telemetry.record(
301            "dispatch.delegated.command",
302            json!({"target": target, "target_truncated": target_truncated, "exit_code": delegated.exit_code}),
303        );
304        return Ok(delegated);
305    }
306
307    if let Some(usage_error) = help::try_render_clap_usage_error(argv) {
308        telemetry
309            .record("dispatch.clap.short_circuit", json!({"kind":"usage_error", "exit_code": 2}));
310        return Ok(AppRunResult {
311            exit_code: 2,
312            stdout: String::new(),
313            stderr: if usage_error.ends_with('\n') {
314                usage_error
315            } else {
316                format!("{usage_error}\n")
317            },
318        });
319    }
320
321    let intent = match parse_intent(argv) {
322        Ok(intent) => intent,
323        Err(error) => {
324            let (message, message_truncated) = bounded_message(&error.to_string());
325            telemetry.record(
326                "dispatch.intent.error",
327                json!({"message": message, "message_truncated": message_truncated, "exit_code": 2}),
328            );
329            return Ok(AppRunResult {
330                exit_code: 2,
331                stdout: String::new(),
332                stderr: format!("{error}\n"),
333            });
334        }
335    };
336    let (command_path, command_path_truncated_segment_count, command_path_clipped_segment_count) =
337        bounded_segments(&intent.command_path);
338    let (
339        normalized_path,
340        normalized_path_truncated_segment_count,
341        normalized_path_clipped_segment_count,
342    ) = bounded_segments(&intent.normalized_path);
343    telemetry.record(
344        "dispatch.intent.parsed",
345        json!({
346            "command_path": command_path,
347            "command_path_truncated_segment_count": command_path_truncated_segment_count,
348            "command_path_clipped_segment_count": command_path_clipped_segment_count,
349            "normalized_path": normalized_path,
350            "normalized_path_truncated_segment_count": normalized_path_truncated_segment_count,
351            "normalized_path_clipped_segment_count": normalized_path_clipped_segment_count,
352            "quiet": intent.global_flags.quiet,
353        }),
354    );
355    let emitter_config = policy::emitter_config(&intent.global_flags);
356    if intent.normalized_path.is_empty() {
357        telemetry.record("dispatch.intent.empty", json!({}));
358        let usage = match root_usage_help_text() {
359            Ok(value) => value,
360            Err(error) => {
361                let (message, message_truncated) = bounded_message(&error.to_string());
362                telemetry.record(
363                    "dispatch.help.render.error",
364                    json!({"message": message, "message_truncated": message_truncated}),
365                );
366                return Err(error);
367            }
368        };
369        return Ok(AppRunResult { exit_code: 2, stdout: String::new(), stderr: usage });
370    }
371
372    if let Some(result) =
373        install_handler::try_run(&intent.normalized_path, argv, &intent.global_flags)?
374    {
375        let command_joined = intent.normalized_path.join(" ");
376        let (command, command_truncated) = bounded_command(&command_joined);
377        telemetry.record(
378            "dispatch.route.completed",
379            json!({
380                "command": command,
381                "command_truncated": command_truncated,
382                "status": if result.exit_code == 0 { Some("ok") } else { Some("error") },
383                "status_truncated": false,
384                "exit_code": result.exit_code,
385                "exit_kind": crate::shared::telemetry::exit_code_kind(result.exit_code),
386            }),
387        );
388        return Ok(result);
389    }
390
391    let response = route_exec::route_response(&intent.normalized_path, argv, &intent.global_flags);
392    let payload = match response {
393        Ok(route_exec::RouteResponse::Payload(value)) => value,
394        Ok(route_exec::RouteResponse::Process(result)) => {
395            let command_joined = intent.normalized_path.join(" ");
396            let (command, command_truncated) = bounded_command(&command_joined);
397            telemetry.record(
398                "dispatch.route.completed",
399                json!({
400                    "command": command,
401                    "command_truncated": command_truncated,
402                    "status": if result.exit_code == 0 { Some("ok") } else { Some("error") },
403                    "status_truncated": false,
404                    "exit_code": result.exit_code,
405                    "exit_kind": crate::shared::telemetry::exit_code_kind(result.exit_code),
406                }),
407            );
408            return Ok(result);
409        }
410        Err(error) => {
411            let message = error.to_string();
412            let code = policy::classify_error_exit_code(&message);
413            let command_joined = intent.normalized_path.join(" ");
414            let (command, command_truncated) = bounded_command(&command_joined);
415            let (message_bounded, message_truncated) = bounded_message(&message);
416            telemetry.record(
417                "dispatch.route.error",
418                json!({
419                    "command": command.clone(),
420                    "command_truncated": command_truncated,
421                    "exit_code": code,
422                    "exit_kind": crate::shared::telemetry::exit_code_kind(code),
423                    "message": message_bounded,
424                    "message_truncated": message_truncated,
425                }),
426            );
427            let mut suggestion_emitted = false;
428            let mut error_payload = json!({
429                "status": "error",
430                "code": code,
431                "message": message,
432                "command": intent.normalized_path.join(" "),
433            });
434            if message.starts_with("unknown route: ") {
435                if let Some(correction) =
436                    suggest::correction_for_unknown_route(&intent.normalized_path)
437                {
438                    let nearest_command = correction.nearest_command;
439                    let next_command = correction.next_command;
440                    let next_help = correction.next_help;
441                    let (nearest_command_bounded, nearest_command_truncated) =
442                        bounded_command(&nearest_command);
443                    let (next_command_bounded, next_command_truncated) =
444                        bounded_command(&next_command);
445                    let (next_help_bounded, next_help_truncated) = bounded_command(&next_help);
446                    error_payload["nearest_command"] = json!(nearest_command);
447                    error_payload["next_command"] = json!(next_command.clone());
448                    error_payload["next_help"] = json!(next_help.clone());
449                    error_payload["hint"] =
450                        json!(format!("Try `{}` or `{}`.", next_command, next_help));
451                    suggestion_emitted = true;
452                    telemetry.record(
453                        "dispatch.route.suggested",
454                        json!({
455                            "command": command,
456                            "command_truncated": command_truncated,
457                            "nearest_command": nearest_command_bounded,
458                            "nearest_command_truncated": nearest_command_truncated,
459                            "next_command": next_command_bounded,
460                            "next_command_truncated": next_command_truncated,
461                            "next_help": next_help_bounded,
462                            "next_help_truncated": next_help_truncated,
463                            "source": "error_path",
464                        }),
465                    );
466                }
467            }
468            if message.starts_with("unknown route: ") {
469                telemetry.record(
470                    "dispatch.route.unknown",
471                    json!({
472                        "command": command.clone(),
473                        "command_truncated": command_truncated,
474                        "exit_code": code,
475                        "exit_kind": crate::shared::telemetry::exit_code_kind(code),
476                        "source": "error_path",
477                        "suggestion_emitted": suggestion_emitted,
478                    }),
479                );
480            }
481            let rendered_error = match render_value(&error_payload, emitter_config) {
482                Ok(value) => value,
483                Err(error) => {
484                    let (message, message_truncated) = bounded_message(&error.to_string());
485                    telemetry.record(
486                            "dispatch.render.error",
487                            json!({"stream":"stderr","message": message, "message_truncated": message_truncated}),
488                        );
489                    return Err(error.into());
490                }
491            };
492            let error_content = if rendered_error.ends_with('\n') {
493                rendered_error
494            } else {
495                format!("{rendered_error}\n")
496            };
497            return Ok(AppRunResult {
498                exit_code: code,
499                stdout: String::new(),
500                stderr: error_content,
501            });
502        }
503    };
504
505    let rendered = if matches!(intent.normalized_path.as_slice(), [a, b] if a == "cli" && b == "version")
506        && emitter_config.format == OutputFormat::Text
507    {
508        crate::api::version::runtime_version_line()
509    } else if matches!(intent.normalized_path.as_slice(), [a, b] if a == "cli" && b == "completion")
510        && emitter_config.format == OutputFormat::Text
511    {
512        payload
513            .get("script")
514            .and_then(serde_json::Value::as_str)
515            .map(ToOwned::to_owned)
516            .unwrap_or_default()
517    } else {
518        match render_value(&payload, emitter_config) {
519            Ok(value) => value,
520            Err(error) => {
521                let (message, message_truncated) = bounded_message(&error.to_string());
522                telemetry.record(
523                    "dispatch.render.error",
524                    json!({"stream":"stdout","message": message, "message_truncated": message_truncated}),
525                );
526                return Err(error.into());
527            }
528        }
529    };
530    let content = if rendered.ends_with('\n') { rendered } else { format!("{rendered}\n") };
531
532    let route_exit_code = 0;
533    let command_joined = intent.normalized_path.join(" ");
534    let (command, command_truncated) = bounded_command(&command_joined);
535    let (status, status_truncated) =
536        bounded_status(payload.get("status").and_then(serde_json::Value::as_str));
537    telemetry.record(
538        "dispatch.route.completed",
539        json!({
540            "command": command.clone(),
541            "command_truncated": command_truncated,
542            "status": status,
543            "status_truncated": status_truncated,
544            "exit_code": route_exit_code,
545            "exit_kind": crate::shared::telemetry::exit_code_kind(route_exit_code),
546        }),
547    );
548
549    if intent.global_flags.quiet {
550        telemetry.record(
551            "dispatch.quiet.suppressed",
552            json!({
553                "command": command,
554                "command_truncated": command_truncated,
555                "exit_code": route_exit_code,
556                "suppressed_stdout_bytes": content.len(),
557                "suppressed_stderr_bytes": 0,
558            }),
559        );
560        return Ok(AppRunResult {
561            exit_code: route_exit_code,
562            stdout: String::new(),
563            stderr: String::new(),
564        });
565    }
566
567    Ok(AppRunResult { exit_code: route_exit_code, stdout: content, stderr: String::new() })
568}
569
570#[cfg(test)]
571mod tests {
572    use serde_json::Value;
573
574    use super::{run_app, MAX_PATH_SEGMENT_CHARS};
575    use crate::shared::telemetry::{install_test_telemetry_config, TEST_ENV_LOCK};
576
577    #[test]
578    fn run_app_writes_opt_in_telemetry_events() {
579        let _guard = TEST_ENV_LOCK.lock().expect("env lock");
580        let temp = tempfile::tempdir().expect("temp dir");
581        let sink = temp.path().join("telemetry").join("events.jsonl");
582        let _telemetry = install_test_telemetry_config(Some(sink.clone()), false);
583
584        let result = run_app(&["bijux".to_string(), "status".to_string()]).expect("run");
585        assert_eq!(result.exit_code, 0);
586
587        let body = std::fs::read_to_string(&sink).expect("telemetry output");
588        let rows: Vec<Value> =
589            body.lines().map(|line| serde_json::from_str(line).expect("json")).collect();
590        assert!(
591            rows.iter().any(|row| row["stage"] == "invocation.start"),
592            "telemetry should include invocation.start"
593        );
594        assert!(
595            rows.iter().any(|row| row["stage"] == "invocation.finish"),
596            "telemetry should include invocation.finish"
597        );
598        assert!(rows.iter().all(|row| row["runtime"] == "bijux-cli"));
599    }
600
601    #[test]
602    fn run_app_unknown_route_emits_unknown_stage_without_completed_stage() {
603        let _guard = TEST_ENV_LOCK.lock().expect("env lock");
604        let temp = tempfile::tempdir().expect("temp dir");
605        let sink = temp.path().join("telemetry").join("events.jsonl");
606        let _telemetry = install_test_telemetry_config(Some(sink.clone()), false);
607
608        let result =
609            run_app(&["bijux".to_string(), "definitely-not-a-command".to_string()]).expect("run");
610        assert_eq!(result.exit_code, 2);
611
612        let body = std::fs::read_to_string(&sink).expect("telemetry output");
613        let rows: Vec<Value> =
614            body.lines().map(|line| serde_json::from_str(line).expect("json")).collect();
615        let unknown = rows
616            .iter()
617            .find(|row| row["stage"] == "dispatch.route.unknown")
618            .expect("unknown route event");
619        assert_eq!(unknown["payload"]["exit_kind"], "usage");
620        assert_eq!(unknown["payload"]["suggestion_emitted"], true);
621        assert_eq!(unknown["payload"]["source"], "error_path");
622        assert!(rows.iter().any(|row| row["stage"] == "dispatch.route.suggested"));
623        assert!(
624            !rows.iter().any(|row| row["stage"] == "dispatch.route.completed"),
625            "unknown routes must not be reported as completed"
626        );
627    }
628
629    #[test]
630    fn run_app_quiet_mode_records_suppressed_byte_metrics() {
631        let _guard = TEST_ENV_LOCK.lock().expect("env lock");
632        let temp = tempfile::tempdir().expect("temp dir");
633        let sink = temp.path().join("telemetry").join("events.jsonl");
634        let _telemetry = install_test_telemetry_config(Some(sink.clone()), false);
635
636        let result = run_app(&["bijux".to_string(), "status".to_string(), "--quiet".to_string()])
637            .expect("run");
638        assert_eq!(result.exit_code, 0);
639        assert!(result.stdout.is_empty());
640        assert!(result.stderr.is_empty());
641
642        let body = std::fs::read_to_string(&sink).expect("telemetry output");
643        let rows: Vec<Value> =
644            body.lines().map(|line| serde_json::from_str(line).expect("json")).collect();
645        let suppressed = rows
646            .iter()
647            .find(|row| row["stage"] == "dispatch.quiet.suppressed")
648            .expect("quiet suppressed event");
649        assert!(suppressed["payload"]["suppressed_stdout_bytes"].as_u64().unwrap_or_default() > 0);
650        assert_eq!(suppressed["payload"]["suppressed_stderr_bytes"], 0);
651    }
652
653    #[test]
654    fn run_app_bounds_intent_path_segments_in_telemetry() {
655        let _guard = TEST_ENV_LOCK.lock().expect("env lock");
656        let temp = tempfile::tempdir().expect("temp dir");
657        let sink = temp.path().join("telemetry").join("events.jsonl");
658        let _telemetry = install_test_telemetry_config(Some(sink.clone()), false);
659
660        let oversized = "x".repeat(MAX_PATH_SEGMENT_CHARS + 48);
661        let result = run_app(&["bijux".to_string(), oversized.clone()]).expect("run");
662        assert_eq!(result.exit_code, 2);
663
664        let body = std::fs::read_to_string(&sink).expect("telemetry output");
665        let rows: Vec<Value> =
666            body.lines().map(|line| serde_json::from_str(line).expect("json")).collect();
667        let parsed = rows
668            .iter()
669            .find(|row| row["stage"] == "dispatch.intent.parsed")
670            .expect("intent parsed event");
671        let first = parsed["payload"]["normalized_path"][0].as_str().expect("first segment");
672        assert_eq!(first.chars().count(), MAX_PATH_SEGMENT_CHARS);
673        assert_eq!(parsed["payload"]["normalized_path_truncated_segment_count"], 1);
674    }
675
676    #[test]
677    fn help_unknown_topic_emits_suggestions_for_near_matches() {
678        let result =
679            run_app(&["bijux".to_string(), "help".to_string(), "sttaus".to_string()]).expect("run");
680        assert_eq!(result.exit_code, 2);
681        assert!(result.stderr.contains("Unknown help topic: sttaus."));
682        assert!(result.stderr.contains("Did you mean:"));
683        assert!(result.stderr.contains("bijux help status"));
684    }
685
686    #[test]
687    fn levenshtein_distance_is_deterministic_for_help_suggestions() {
688        assert_eq!(super::levenshtein_distance("status", "status"), 0);
689        assert_eq!(super::levenshtein_distance("status", "sttaus"), 2);
690    }
691
692    #[test]
693    fn help_unknown_topic_suggests_root_alias_commands() {
694        let result = run_app(&["bijux".to_string(), "help".to_string(), "versoin".to_string()])
695            .expect("run");
696        assert_eq!(result.exit_code, 2);
697        assert!(result.stderr.contains("bijux help version"));
698    }
699
700    #[test]
701    fn default_help_matches_help_flag_surface() {
702        let no_args = run_app(&["bijux".to_string()]).expect("run without args");
703        let explicit = run_app(&["bijux".to_string(), "--help".to_string()]).expect("run --help");
704        assert_eq!(no_args.exit_code, 0);
705        assert_eq!(explicit.exit_code, 0);
706        assert_eq!(no_args.stdout, explicit.stdout);
707        assert_eq!(no_args.stderr, explicit.stderr);
708    }
709}