From b72a5c4fdb04ffe3240391da2de87268923d72ba Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 08:55:36 -0600 Subject: [PATCH 1/3] id(cli/repl): reject zero result limits at both boundaries (#3059) --first 0 / --last 0 silently reported no matches for queries that do match, and peek --lines 0 rendered a header/elision-only preview. CLI: a parse_result_limit value_parser on all eight first/last fields and on peek --lines/-n rejects zero at parse time (clap names the flag in the error). REPL: FindArgs::validate_result_limits bails on Some(0) for --first/--last and parse_peek_args bails on --lines 0. Bare-flag defaults (1/1/5) and the #2009 nonnumeric fallback to 1 are preserved; the #3029 first/last conflict behaviour is untouched. Regression tests at both boundaries. --- pkgs/id/src/cli.rs | 67 +++++++++++++++++++++++++++++++++----- pkgs/id/src/repl/runner.rs | 34 ++++++++++++++++++- 2 files changed, 91 insertions(+), 10 deletions(-) diff --git a/pkgs/id/src/cli.rs b/pkgs/id/src/cli.rs index 59bbcf5e6a..195e1ca4fe 100644 --- a/pkgs/id/src/cli.rs +++ b/pkgs/id/src/cli.rs @@ -197,6 +197,18 @@ pub struct Cli { /// Commands are organized by their primary function: storage, retrieval, /// search, or system operations. #[derive(Subcommand, Debug)] +/// Parses a result limit (`--first` / `--last` / peek `--lines`): zero is +/// rejected with a clear error instead of silently producing an empty result +/// or a header-only preview (#3059). Clap names the flag in its error prefix, +/// so the message stays flag-specific at the boundary. +fn parse_result_limit(raw: &str) -> Result { + match raw.parse::() { + Ok(0) => Err("zero is not a valid limit: use at least 1".to_owned()), + Ok(n) => Ok(n), + Err(err) => Err(err.to_string()), + } +} + pub enum Command { /// Start a server that accepts put/get requests from peers. /// @@ -727,7 +739,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -735,7 +748,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Exclude matches where name or hash contains this pattern (repeatable). @@ -790,7 +804,7 @@ pub enum Command { #[arg(long)] name: bool, /// Number of lines to show from head and tail (default: 5). - #[arg(short = 'n', long, default_value = "5")] + #[arg(short = 'n', long, default_value = "5", value_parser = parse_result_limit)] lines: usize, /// Show only head lines (no tail). #[arg(long, conflicts_with = "tail_only")] @@ -818,7 +832,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -826,7 +841,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Exclude matches where name or hash contains this pattern (repeatable). @@ -914,7 +930,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -922,7 +939,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Print count of matches instead of the matches themselves. @@ -991,7 +1009,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -999,7 +1018,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Print count of matches instead of the matches themselves. @@ -2923,6 +2943,35 @@ mod tests { } #[test] + #[test] + fn test_cli_rejects_zero_result_limits() { + std::thread::Builder::new() + .stack_size(8 * 1024 * 1024) + .spawn(|| { + // #3059: an explicit zero limit is an error at the CLI boundary, never a + // silent empty result (find/search/show/peek share the flag pair). + for cmd in ["find", "search", "show", "peek"] { + assert!( + Cli::try_parse_from(["id", cmd, "--first", "0", "query"]).is_err(), + "{cmd} --first 0 must be rejected" + ); + assert!( + Cli::try_parse_from(["id", cmd, "--last", "0", "query"]).is_err(), + "{cmd} --last 0 must be rejected" + ); + } + assert!(Cli::try_parse_from(["id", "peek", "--lines", "0", "query"]).is_err()); + assert!(Cli::try_parse_from(["id", "peek", "-n", "0", "query"]).is_err()); + // Bare flags keep their defaults (1 / 1 / 5 lines). + assert!(Cli::try_parse_from(["id", "find", "--first", "query"]).is_ok()); + assert!(Cli::try_parse_from(["id", "find", "--last", "query"]).is_ok()); + assert!(Cli::try_parse_from(["id", "peek", "query"]).is_ok()); + }) + .unwrap() + .join() + .unwrap(); + } + fn test_cli_rejects_first_and_last_together() { std::thread::Builder::new() .stack_size(8 * 1024 * 1024) diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index 07387c2d1e..f1e3112e68 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -602,11 +602,20 @@ struct FindArgs<'a> { } impl FindArgs<'_> { - /// Reject mutually exclusive result limits before any search work starts. + /// Reject mutually exclusive and zero result limits before any search work starts. fn validate_result_limits(&self) -> Result<()> { if self.first.is_some() && self.last.is_some() { anyhow::bail!("--first and --last cannot be used together"); } + // #3059: an explicit zero limit is an error, never a silent empty result. (A + // nonnumeric value keeps the #2009 behaviour: it falls back to the default of 1 + // in the parsers, so only a parsed 0 reaches this check.) + if self.first == Some(0) { + anyhow::bail!("--first must be at least 1 (got 0)"); + } + if self.last == Some(0) { + anyhow::bail!("--last must be at least 1 (got 0)"); + } Ok(()) } @@ -1296,6 +1305,10 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> Result<(FindArgs<'a>, PeekOptions)> } find_args.validate_result_limits()?; + // #3059: `--lines 0` / `-n 0` would render a header/elision-only preview. + if peek_opts.lines == 0 { + anyhow::bail!("--lines must be at least 1 (got 0)"); + } Ok((find_args, peek_opts)) } @@ -1549,6 +1562,25 @@ mod tests { } #[test] + #[test] + fn test_parse_rejects_zero_result_limits() { + // #3059: explicit zero limits are errors at the REPL boundary. + assert!(parse_find_args(&["q", "--first", "0"], "tag").is_err()); + assert!(parse_find_args(&["q", "--last", "0"], "tag").is_err()); + assert!(parse_peek_args(&["q", "--lines", "0"]).is_err()); + assert!(parse_peek_args(&["q", "-n", "0"]).is_err()); + assert!(parse_peek_args(&["q", "--first", "0"]).is_err()); + // Bare flags keep the default of 1; a nonnumeric value keeps the #2009 fallback. + assert_eq!(parse_find_args(&["q", "--first"], "tag").unwrap().first, Some(1)); + assert_eq!(parse_find_args(&["q", "--last"], "tag").unwrap().last, Some(1)); + assert_eq!( + parse_find_args(&["q", "--first", "abc"], "tag").unwrap().first, + Some(1) + ); + // Peek without --lines keeps the default of 5. + assert_eq!(parse_peek_args(&["q"]).unwrap().1.lines, 5); + } + fn test_parse_find_args_basic() { let rest = vec!["query1", "query2"]; let args = parse_find_args(&rest, "union").unwrap(); From d0a08e4b7c897f910f64dd2807238678b9bbcb43 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 08:55:53 -0600 Subject: [PATCH 2/3] style(id): rustfmt the zero-limit tests (#3059) --- pkgs/id/src/repl/runner.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index f1e3112e68..7efc7a0297 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -1571,10 +1571,18 @@ mod tests { assert!(parse_peek_args(&["q", "-n", "0"]).is_err()); assert!(parse_peek_args(&["q", "--first", "0"]).is_err()); // Bare flags keep the default of 1; a nonnumeric value keeps the #2009 fallback. - assert_eq!(parse_find_args(&["q", "--first"], "tag").unwrap().first, Some(1)); - assert_eq!(parse_find_args(&["q", "--last"], "tag").unwrap().last, Some(1)); assert_eq!( - parse_find_args(&["q", "--first", "abc"], "tag").unwrap().first, + parse_find_args(&["q", "--first"], "tag").unwrap().first, + Some(1) + ); + assert_eq!( + parse_find_args(&["q", "--last"], "tag").unwrap().last, + Some(1) + ); + assert_eq!( + parse_find_args(&["q", "--first", "abc"], "tag") + .unwrap() + .first, Some(1) ); // Peek without --lines keeps the default of 5. From 9cce0700bf267c07259c8da7e612e77a21487445 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 09:03:33 -0600 Subject: [PATCH 3/3] test(id): put the query before bare limit flags in the zero-limit test (#3059, PR #3738 Codex P2) --- pkgs/id/src/cli.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/cli.rs b/pkgs/id/src/cli.rs index 195e1ca4fe..c897da74e5 100644 --- a/pkgs/id/src/cli.rs +++ b/pkgs/id/src/cli.rs @@ -2963,8 +2963,8 @@ mod tests { assert!(Cli::try_parse_from(["id", "peek", "--lines", "0", "query"]).is_err()); assert!(Cli::try_parse_from(["id", "peek", "-n", "0", "query"]).is_err()); // Bare flags keep their defaults (1 / 1 / 5 lines). - assert!(Cli::try_parse_from(["id", "find", "--first", "query"]).is_ok()); - assert!(Cli::try_parse_from(["id", "find", "--last", "query"]).is_ok()); + assert!(Cli::try_parse_from(["id", "find", "query", "--first"]).is_ok()); + assert!(Cli::try_parse_from(["id", "find", "query", "--last"]).is_ok()); assert!(Cli::try_parse_from(["id", "peek", "query"]).is_ok()); }) .unwrap()