Repository navigation
fix: harden model validation and doctor checks #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2560994
497c880
3f3d6ef
480bc26
d5acd04
5a9e485
07abb61
e587b84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,16 @@ | ||
| --- | ||
| name: feature request | ||
| about: an idea worth arguing about | ||
| about: propose a focused improvement | ||
| title: '' | ||
| labels: enhancement | ||
| assignees: '' | ||
| --- | ||
|
|
||
| what is the actual problem you are trying to solve? not "add feature x" — the | ||
| thing that makes feature x worth writing. | ||
| what problem are you trying to solve? | ||
|
|
||
| what would you change? | ||
| what behaviour would you expect from scribe? | ||
|
|
||
| what did you try already? | ||
| what alternatives or workarounds have you tried? | ||
|
|
||
| anything that would make you reject this idea? if the answer is "it adds a 200 | ||
| mb dependency for one flag", say so. i will probably agree. | ||
| are there compatibility, performance, privacy or maintenance trade-offs to | ||
| consider? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,20 @@ | ||
| --- | ||
| name: pull request | ||
| about: changes for review | ||
| title: '' | ||
| labels: '' | ||
| assignees: '' | ||
| --- | ||
|
|
||
| what does this change, in one sentence? | ||
| what does this change? | ||
|
|
||
| why is it needed? link the issue if there is one. | ||
|
|
||
| how did you verify it? paste the commands you actually ran, not the ones you | ||
| meant to run. | ||
| how did you verify it? include the commands you actually ran. | ||
|
|
||
| ```sh | ||
|
|
||
| ``` | ||
|
|
||
| if this touches the worker, the download path, or the layout, explain the | ||
| trade-off you picked. those are the three places where the obvious fix is | ||
| usually the wrong one. | ||
| if this touches transcription workers, downloads, model caching, exports or | ||
| terminal rendering, explain the relevant failure mode and trade-off. | ||
|
|
||
| checklist (delete the ones that do not apply): | ||
| checklist: | ||
|
|
||
| - [ ] `cargo test` passes | ||
| - [ ] `cargo clippy --all-targets -- -D warnings` is clean | ||
| - [ ] `cargo fmt --all -- --check` is clean | ||
| - [ ] docs updated if behaviour changed | ||
| - [ ] `cargo fmt --all -- --check` passes | ||
| - [ ] `cargo clippy --locked --all-targets --all-features -- -D warnings` passes | ||
| - [ ] `cargo test --locked --all-targets --all-features` passes | ||
| - [ ] documentation is updated when behaviour changes | ||
| - [ ] no unrelated files or generated artifacts are included |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,21 @@ struct Row { | |
| detail: String, | ||
| } | ||
|
|
||
| fn cache_status(cache: &std::path::Path) -> (Verdict, &'static str) { | ||
| if cache.is_dir() { | ||
| (Verdict::Ok, "directory exists; write access not tested") | ||
| } else if cache.exists() { | ||
| (Verdict::Fail, "path exists but is not a directory") | ||
| } else if cache.parent().is_some_and(std::path::Path::is_dir) { | ||
| ( | ||
| Verdict::Warn, | ||
| "will be created on first download; write access not tested", | ||
| ) | ||
| } else { | ||
| (Verdict::Fail, "parent directory is missing") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Critical · Fresh cache reports failure When the cache directory or one of its parent directories has not been created yet, Treat a missing cache path as creatable when its directory tree can be created on first download, and return a warning instead of a failure. Also at Prompt for AI agentsIn `src/doctor.rs` at line 36:
**Fresh cache reports failure**
When the cache directory or one of its parent directories has not been created yet, `cache_status` returns `Fail` because it checks only the immediate parent, so `scribe --doctor` exits with status 1 for a cache tree that `DownloadLock::acquire` can create recursively; the regression test also fails because it expects `Warn`.
Treat a missing cache path as creatable when its directory tree can be created on first download, and return a warning instead of a failure.
Also at `src/doctor.rs:37`, `src/doctor.rs:290-292`. |
||
| } | ||
| } | ||
|
|
||
| fn version(binary: &std::path::Path, args: &[&str]) -> Option<String> { | ||
| let output = std::process::Command::new(binary) | ||
| .args(args) | ||
|
|
@@ -109,18 +124,11 @@ pub fn run(selected_model: Option<&str>) -> i32 { | |
| }); | ||
|
|
||
| let cache = model::cache_dir(); | ||
| let cache_ok = cache | ||
| .parent() | ||
| .map(|p| p.is_dir()) | ||
| .unwrap_or_else(|| cache.is_dir()); | ||
| let (cache_verdict, cache_detail) = cache_status(&cache); | ||
| rows.push(Row { | ||
| label: "model cache", | ||
| verdict: if cache_ok { Verdict::Ok } else { Verdict::Fail }, | ||
| detail: format!( | ||
| "{} ({})", | ||
| cache.display(), | ||
| if cache_ok { "writable" } else { "not writable" } | ||
| ), | ||
| verdict: cache_verdict, | ||
| detail: format!("{} ({cache_detail})", cache.display()), | ||
| }); | ||
|
|
||
| let chosen = selected_model.map(str::to_string).or_else(|| { | ||
|
|
@@ -276,6 +284,40 @@ pub fn run(selected_model: Option<&str>) -> i32 { | |
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn cache_status_does_not_claim_unchecked_write_access() { | ||
| let dir = std::env::temp_dir().join(format!("scribe-doctor-{}", std::process::id())); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| std::fs::create_dir_all(&dir).unwrap(); | ||
| let cache = dir.join("models"); | ||
| assert_eq!( | ||
| cache_status(&cache), | ||
| ( | ||
| Verdict::Warn, | ||
| "will be created on first download; write access not tested" | ||
| ) | ||
| ); | ||
| std::fs::create_dir_all(&cache).unwrap(); | ||
| assert_eq!( | ||
| cache_status(&cache), | ||
| (Verdict::Ok, "directory exists; write access not tested") | ||
| ); | ||
| std::fs::remove_dir_all(&dir).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cache_status_rejects_a_file_at_the_cache_path() { | ||
| let dir = std::env::temp_dir().join(format!("scribe-doctor-file-{}", std::process::id())); | ||
| std::fs::create_dir_all(&dir).unwrap(); | ||
| let cache = dir.join("models"); | ||
| std::fs::write(&cache, b"not a directory").unwrap(); | ||
| assert_eq!( | ||
| cache_status(&cache), | ||
| (Verdict::Fail, "path exists but is not a directory") | ||
| ); | ||
| std::fs::remove_dir_all(&dir).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn verdict_labels_are_short() { | ||
| assert_eq!(Verdict::Ok.label(), "ok"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -495,6 +495,16 @@ pub fn validate_cached(info: &ModelInfo, path: &Path) -> Result<(), String> { | |
|
|
||
| /// custom models have no authoritative checksum in the catalog. the least we | ||
| /// can do is reject empty stubs and files that cannot possibly be ggml models. | ||
| fn read_model_header(path: &Path) -> Result<[u8; 4], String> { | ||
| use std::io::Read; | ||
| let mut file = | ||
| File::open(path).map_err(|e| format!("cannot read model {}: {e}", path.display()))?; | ||
| let mut magic = [0u8; 4]; | ||
| file.read_exact(&mut magic) | ||
| .map_err(|e| format!("cannot read model header {}: {e}", path.display()))?; | ||
| Ok(magic) | ||
| } | ||
|
|
||
| pub fn validate_custom(path: &Path) -> Result<(), String> { | ||
| let len = fs::metadata(path) | ||
| .map_err(|e| format!("cannot inspect {}: {e}", path.display()))? | ||
|
|
@@ -506,13 +516,8 @@ pub fn validate_custom(path: &Path) -> Result<(), String> { | |
| len | ||
| )); | ||
| } | ||
| let mut magic = [0u8; 4]; | ||
| use std::io::Read; | ||
| if let Ok(mut file) = File::open(path) | ||
| && file.read_exact(&mut magic).is_ok() | ||
| && &magic != b"ggml" | ||
| && &magic != b"GGUF" | ||
| { | ||
| let magic = read_model_header(path)?; | ||
| if &magic != b"ggml" && &magic != b"GGUF" { | ||
| return Err(format!( | ||
| "{} does not look like a ggml model (bad header). refusing to hand it to whisper", | ||
| path.display() | ||
|
|
@@ -1378,6 +1383,16 @@ mod tests { | |
| fs::remove_dir_all(&dir).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn custom_model_header_read_errors_are_rejected() { | ||
| let dir = std::env::temp_dir().join(format!("scribe-header-{}", std::process::id())); | ||
| fs::create_dir_all(&dir).unwrap(); | ||
| let path = dir.join("truncated.bin"); | ||
| fs::write(&path, b"gg").unwrap(); | ||
| assert!(read_model_header(&path).is_err()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Validator rejection goes untested When Exercise Prompt for AI agentsIn `src/model.rs` at line 1392:
**Validator rejection goes untested**
When `validate_custom` is changed to ignore a header-read error, `custom_model_header_read_errors_are_rejected` asserts only that `read_model_header` returns an error, so it still passes if `validate_custom` stops propagating that error and accepts an unreadable header.
Exercise `validate_custom` with an input that passes the minimum-size check but produces a header-read error, and assert that validation rejects it. |
||
| fs::remove_dir_all(&dir).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn validate_custom_rejects_stubs_and_wrong_headers() { | ||
| let dir = std::env::temp_dir().join(format!("scribe-custom-{}", std::process::id())); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Doctor misses unusable cache
When the configured cache path is a dangling symlink and its parent directory exists,
cache.exists()follows symlinks and returns false, socache_statusreports that the cache will be created;DownloadLock::acquirethen fails whencreate_dir_allencounters the symlink, and the user sees a download error despite the doctor warning.Detect an occupied dangling-symlink path without following it and report it as unusable instead of saying it will be created.
Also at
src/doctor.rs:33-34.Prompt for AI agents