Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions pkgs/atlas-search/crates/atlas-search/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,13 +580,17 @@ fn u(v: i64) -> usize {
/// Validate one CLI count flag against the service's request
/// contract (design section 2; `validate_request` in server.rs):
/// `--k` at least 1, `--pool` between 1 and 400, and the
/// zero-is-valid counts (`--expand`, `--trunc`, `--neighbors`)
/// merely non-negative. Out-of-range values are a fatal (exit 2),
/// never a silent clamp to 0 (#5407).
/// zero-is-valid counts (`--expand`, `--trunc`, `--neighbors`) are
/// non-negative; `--neighbors` is also capped at the service's limit.
/// Out-of-range values are fatal (exit 2), never silently clamped to 0
/// (#5407).
fn count(flag: &str, v: i64, min: i64, max: Option<i64>) -> Result<usize, CliError> {
if v >= min && max.is_none_or(|m| v <= m) {
return Ok(usize::try_from(v).expect("range-checked count converts"));
}
if v < min && min == 0 {
return Err((2, format!("{flag} must not be negative")));
}
let msg = match (min, max) {
(_, Some(m)) => format!("{flag} must be between {min} and {m} (design section 2)"),
(1, None) => format!("{flag} must be at least 1"),
Expand Down Expand Up @@ -661,7 +665,7 @@ fn cmd_search(args: SearchArgs) -> Result<(), CliError> {
}

fn cmd_get_chunk(args: GetChunkArgs) -> Result<(), CliError> {
let neighbors = count("--neighbors", args.neighbors, 0, None)?;
let neighbors = count("--neighbors", args.neighbors, 0, Some(100))?;
let note = via_service_note_get_chunk(&args.backend);
// The Python's get-chunk takes no vector args: pick_backend
// sees the defaults (getattr fallbacks in its main()).
Expand Down Expand Up @@ -766,13 +770,27 @@ mod tests {
assert!(count("--pool", 0, 1, Some(400)).is_err());
assert!(count("--pool", -5, 1, Some(400)).is_err());
// zero-is-valid counts: 0 stays valid, negatives do not
for flag in ["--expand", "--trunc", "--neighbors"] {
for flag in ["--expand", "--trunc"] {
assert_eq!(count(flag, 0, 0, None), Ok(0), "{flag}");
assert_eq!(
count(flag, -1, 0, None).unwrap_err(),
(2, format!("{flag} must not be negative")),
"{flag}"
);
}
// --neighbors has a 100 upper bound (service cap)
assert_eq!(count("--neighbors", 0, 0, Some(100)), Ok(0));
assert_eq!(count("--neighbors", 100, 0, Some(100)), Ok(100));
assert_eq!(
count("--neighbors", 101, 0, Some(100)).unwrap_err(),
(
2,
"--neighbors must be between 0 and 100 (design section 2)".to_string()
)
);
assert_eq!(
count("--neighbors", -1, 0, Some(100)).unwrap_err(),
(2, "--neighbors must not be negative".to_string())
);
}
}
20 changes: 20 additions & 0 deletions pkgs/atlas-search/crates/atlas-search/tests/cli_parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ fn cli_get_chunk_not_found_exits_1() {
);
}

#[test]
fn cli_get_chunk_neighbors_above_cap_exits_2() {
// Command-level coverage for the --neighbors cap (#6594): the unit
// tests in main.rs call count() directly, so dropping the cap at
// the cmd_get_chunk call site would fail nothing there. Through
// the binary, 101 must be refused before any lookup, with exit 2
// and the bound message on stderr.
let db = build_fixture_db("neighborscap");
let out = run_bin(
&db,
&["get-chunk", "2", "--neighbors", "101", "--backend", "local"],
);
assert_eq!(out.status.code(), Some(2));
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("--neighbors must be between 0 and 100"),
"stderr: {stderr}"
);
}

#[test]
fn cli_text_goldens_match_python() {
let db = build_fixture_db("text");
Expand Down
Loading