fix: prevent panics in robot-mode JSON output and arithmetic paths

Peer code review found multiple panic-reachable paths:

1. serde_json::to_string().unwrap() in 4 robot-mode output functions
   (who.rs, main.rs x3). If serialization ever failed (e.g., NaN from
   edge-case division), the CLI would panic with an unhelpful stack trace.
   Replaced with unwrap_or_else that emits a structured JSON error fallback.

2. encode_rowid() in chunk_ids.rs used unchecked multiplication
   (document_id * 1000). On extreme document IDs this could silently wrap
   in release mode, causing embedding rowid collisions. Now uses
   checked_mul + checked_add with a diagnostic panic message.

3. HTTP response body truncation at byte index 500 in client.rs could
   split a multi-byte UTF-8 character, causing a panic. Now uses
   floor_char_boundary(500) for safe truncation.

4. who.rs reviews mode: SQL used `m.author_username != ?1` which silently
   dropped MRs with NULL author_username (SQL NULL != anything = NULL).
   Changed to `(m.author_username IS NULL OR m.author_username != ?1)`
   to match the pattern already used in expert mode.

5. handle_auth_test hardcoded exit code 5 for all errors regardless of
   type. Config not found (20), token not set (4), and network errors (8)
   all incorrectly returned 5. Now uses e.exit_code() from the actual
   LoreError, with proper suggestion hints in human mode.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Taylor Eernisse
2026-02-08 07:55:20 -05:00
parent 121a634653
commit e6b880cbcb
4 changed files with 53 additions and 21 deletions

View File

@@ -845,7 +845,12 @@ fn print_combined_ingest_json(
meta: RobotMeta { elapsed_ms },
};
println!("{}", serde_json::to_string(&output).unwrap());
println!(
"{}",
serde_json::to_string(&output).unwrap_or_else(|e| {
format!(r#"{{"ok":false,"error":{{"code":"INTERNAL_ERROR","message":"JSON serialization failed: {e}"}}}}"#)
})
);
}
#[derive(Serialize)]
@@ -874,7 +879,12 @@ fn print_combined_dry_run_json(
},
};
println!("{}", serde_json::to_string(&output).unwrap());
println!(
"{}",
serde_json::to_string(&output).unwrap_or_else(|e| {
format!(r#"{{"ok":false,"error":{{"code":"INTERNAL_ERROR","message":"JSON serialization failed: {e}"}}}}"#)
})
);
}
async fn handle_count(
@@ -966,7 +976,12 @@ fn print_init_json(result: &InitResult) {
.collect(),
},
};
println!("{}", serde_json::to_string(&output).unwrap());
println!(
"{}",
serde_json::to_string(&output).unwrap_or_else(|e| {
format!(r#"{{"ok":false,"error":{{"code":"INTERNAL_ERROR","message":"JSON serialization failed: {e}"}}}}"#)
})
);
}
async fn handle_init(
@@ -1198,17 +1213,20 @@ async fn handle_auth_test(
}
Err(e) => {
if robot_mode {
let output = FallbackErrorOutput {
error: FallbackError {
code: "AUTH_FAILED".to_string(),
message: e.to_string(),
},
};
eprintln!("{}", serde_json::to_string(&output)?);
let output = RobotErrorOutput::from(&e);
eprintln!(
"{}",
serde_json::to_string(&output).unwrap_or_else(|_| {
format!(r#"{{"error":{{"code":"{}","message":"{}"}}}}"#, e.code(), e)
})
);
} else {
eprintln!("{}", style(format!("Error: {e}")).red());
eprintln!("{} {}", style("Error:").red(), e);
if let Some(suggestion) = e.suggestion() {
eprintln!("{} {}", style("Hint:").yellow(), suggestion);
}
}
std::process::exit(5);
std::process::exit(e.exit_code());
}
}
}