diff --git a/pkgs/fleet/book/concepts/bridge.md b/pkgs/fleet/book/concepts/bridge.md index 789c9fdfca..980e078224 100644 --- a/pkgs/fleet/book/concepts/bridge.md +++ b/pkgs/fleet/book/concepts/bridge.md @@ -11,11 +11,11 @@ handled. Source: https://github.com/developing-today/code/blob/main/pkgs/fleet/crates/fleet-bridge/src/lib.rs -One documentation fact first: there is **one usage text for the whole -domain** and no per-verb help in source — every per-verb `--help` prints the -same shared usage block, despite the domain help's "(options: fleet bridge -<command> --help)" status line. This chapter is the per-verb reference the -help does not provide. +One documentation fact first: every verb has its own help — +`fleet bridge --help` prints that verb's usage section plus the +shared defaults-and-exit-codes footer, and a bare `fleet bridge --help` +prints the whole-domain usage text. This chapter is the per-verb reference +in prose form. ## The two verb families diff --git a/pkgs/fleet/book/reference/bridge.md b/pkgs/fleet/book/reference/bridge.md index bc673b3418..b3403f419b 100644 --- a/pkgs/fleet/book/reference/bridge.md +++ b/pkgs/fleet/book/reference/bridge.md @@ -4,7 +4,7 @@ See also [The bridge: fleet messaging](../concepts/bridge.md). -Known discrepancy: every per-verb help invocation returns **one shared USAGE text** for the whole domain — there is no per-verb help in source, despite the domain status line "(options: fleet bridge <command> --help)". The per-verb sections below are written from source semantics. +Each verb has its own help: `fleet bridge --help` prints that verb's usage section plus the shared defaults-and-exit-codes footer, as the domain status line advertises. A bare `fleet bridge --help` prints the whole-domain usage text. ## Tokens, URLs, and defaults (all verbs) diff --git a/pkgs/fleet/crates/fleet-bridge/src/lib.rs b/pkgs/fleet/crates/fleet-bridge/src/lib.rs index d13d8f402b..31d3a5d7e4 100644 --- a/pkgs/fleet/crates/fleet-bridge/src/lib.rs +++ b/pkgs/fleet/crates/fleet-bridge/src/lib.rs @@ -91,6 +91,87 @@ $FLEET_BRIDGE_URL, then [bridge] url, then https://bridge.security.cab. Output is the bridge's JSON. Exit: 0 ok, 2 usage, 3 transport or credentials, 4 the bridge refused the request."; +/// The trailing block of USAGE, shared by every per-verb help text. +/// A test pins USAGE to end with this, so the two cannot drift. +const USAGE_FOOTER: &str = "\ +The agent defaults to $FLEET_AGENT, then [bridge] agent; groups to +$FLEET_AGENT_GROUPS, then [bridge] groups. The bridge URL is +$FLEET_BRIDGE_URL, then [bridge] url, then https://bridge.security.cab. +Output is the bridge's JSON. Exit: 0 ok, 2 usage, 3 transport or +credentials, 4 the bridge refused the request."; + +/// Per-verb usage for `fleet bridge --help` (#13197). The +/// domain's status line advertises per-verb help, so each verb answers +/// with its own section of USAGE plus the shared footer. Unknown verbs +/// return None and the caller falls back to USAGE, so a help request +/// never prints nothing. +pub fn verb_usage(verb: &str) -> Option { + let section = match verb { + "send" => { + "\ +usage: fleet bridge send --to [--command] [--sender NAME] + +central (owner token). targets: agent: (or a bare name), +group:, all; repeat --to or separate with commas. --command +marks it a command (default: a plain message). '-' reads the text +from stdin." + } + "agents" => { + "\ +usage: fleet bridge agents + +central (owner token). Registered agents, groups, unread counts." + } + "log" => { + "\ +usage: fleet bridge log [--agent A] [--direction inbound|outbound] [--kind message|command] + [--since-id N] [--before-id N] [--limit N] [--order asc|desc] + +central (owner token). The central message log, filtered." + } + "hello" => { + "\ +usage: fleet bridge hello [--agent A] [--groups g1,g2] + +agent (fleet token). Register / refresh groups." + } + "inbox" => { + "\ +usage: fleet bridge inbox [--agent A] [--groups g1,g2] [--commands | --messages] [--all] + [--since-id N] [--limit N] [--take] [--wait SECS] [--interval SECS] + +agent (fleet token). Unread by default; --all includes read ones; +--take marks what it returns as read; --wait polls until something +arrives or SECS pass." + } + "post" => { + "\ +usage: fleet bridge post [--agent A] [--reply-to ID] [--to ADDR] [--instruction | --kind K] + +agent (fleet token). Message to central by default; with --reply-to, +answers that row (and marks it read). --to sends to any address +instead: drew, claude, chatgpt, connectors, agent: (or a bare +name), group:, agents, all; repeat or separate with commas. +--instruction (or --kind instruction) makes it an instruction, which +needs --to." + } + "ack" => { + "\ +usage: fleet bridge ack [--agent A] (--up-to ID | ID...) + +agent (fleet token). Mark messages read." + } + "health" => { + "\ +usage: fleet bridge health + +The bridge's /healthz (no token)." + } + _ => return None, + }; + Some(format!("{section}\n\n{USAGE_FOOTER}")) +} + #[derive(Debug, PartialEq)] pub enum BridgeError { Usage(String), @@ -768,12 +849,23 @@ pub fn execute(ctx: &mut Ctx, args: &[String]) -> R { /// The domain entry point: `fleet bridge ...`. pub fn run(config: &Config, args: &[String]) -> i32 { - if args.is_empty() - || matches!(args[0].as_str(), "-h" | "--help" | "help") - || args.iter().skip(1).any(|a| a == "--help" || a == "-h") - { + // Help wins over every other argument (the #10838 convention): + // `fleet bridge --help` prints the verb's own usage (#13197); + // bare or top-level help prints the whole-domain USAGE. + if args.is_empty() { println!("{USAGE}"); - return if args.is_empty() { EXIT_USAGE } else { EXIT_OK }; + return EXIT_USAGE; + } + if matches!(args[0].as_str(), "-h" | "--help" | "help") { + println!("{USAGE}"); + return EXIT_OK; + } + if args.iter().skip(1).any(|a| a == "--help" || a == "-h") { + match verb_usage(&args[0]) { + Some(text) => println!("{text}"), + None => println!("{USAGE}"), + } + return EXIT_OK; } let env = |k: &str| std::env::var(k).ok(); let settings = match Settings::resolve(config, &env) { diff --git a/pkgs/fleet/crates/fleet-bridge/src/tests.rs b/pkgs/fleet/crates/fleet-bridge/src/tests.rs index e0f4acb815..f43a3eef3b 100644 --- a/pkgs/fleet/crates/fleet-bridge/src/tests.rs +++ b/pkgs/fleet/crates/fleet-bridge/src/tests.rs @@ -151,6 +151,28 @@ fn settings_env_beats_file_beats_default() { ); } +#[test] +fn every_verb_has_its_own_help() { + // #13197: the domain status line advertises `fleet bridge + // --help`; every verb execute() dispatches must answer with its own + // usage — its own usage line, the shared footer, and never the + // whole-domain USAGE text. + for verb in [ + "send", "agents", "log", "hello", "inbox", "post", "ack", "health", + ] { + let text = verb_usage(verb).unwrap_or_else(|| panic!("no per-verb help for {verb}")); + assert!( + text.starts_with(&format!("usage: fleet bridge {verb}")), + "{verb}: {text}" + ); + assert!(text.ends_with(USAGE_FOOTER), "{verb}: missing the footer"); + assert_ne!(text, USAGE, "{verb}: fell back to the shared text"); + } + assert!(verb_usage("bogus").is_none()); + // The footer is USAGE's own tail, so the two cannot drift apart. + assert!(USAGE.ends_with(USAGE_FOOTER)); +} + #[test] fn usage_errors_exit_2_before_any_request() { let s = secrets(&[]);