1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/// Enumerate every model file sitting in the pacha cache directory.
///
/// This is the single source of truth for "what is in the cache": `apr list`
/// prints it and `apr rm` resolves against it (RM-NS-001). The pacha
/// manifest is only an annotation on top of it — it can never be complete,
/// because files land in this directory from the streaming pull path, from
/// `apr convert`, and from plain file copies, none of which go through the
/// fetcher. The directory always is complete, so it is the namespace both
/// commands share.
fn scan_cache_dir(cache_dir: &Path) -> Vec<DiskModelEntry> {
let Ok(read_dir) = std::fs::read_dir(cache_dir) else {
return Vec::new();
};
let mut found = Vec::new();
for entry in read_dir.flatten() {
let path = entry.path();
if !path.is_file() {
continue;
}
let Some(ext) = path.extension().and_then(|s| s.to_str()) else {
continue;
};
let format = match ext {
"gguf" | "ggml" => "GGUF",
"apr" => "APR",
"safetensors" => "SafeTensors",
_ => continue,
};
let size_bytes = entry.metadata().map(|m| m.len()).unwrap_or(0);
let name = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown")
.to_string();
found.push(DiskModelEntry {
name,
size_bytes,
format,
path: path.clone(),
});
}
found
}
/// Scan the pacha cache directory for model files that are on disk but may not be
/// tracked in the manifest (e.g., downloaded before pacha GH-162 added manifest
/// persistence, or via direct writes outside the fetcher).
///
/// Contract: apr-list-disk-reconciliation-v1 F-LIST-DISK-001 (paiml/aprender#602).
fn scan_cache_dir_for_orphans(
cache_dir: &Path,
known_paths: &HashSet<std::path::PathBuf>,
) -> Vec<DiskModelEntry> {
scan_cache_dir(cache_dir)
.into_iter()
.filter(|e| !known_paths.contains(&e.path))
.collect()
}
struct DiskModelEntry {
name: String,
size_bytes: u64,
format: &'static str,
path: std::path::PathBuf,
}
/// List cached models
///
/// Contract: apr-list-quiet-wiring-v1 F-LIST-QUIET-001 (paiml/aprender#623).
/// When quiet=true, suppress help text and tabular decoration — emit one entry per line.
// serde_json::json!() macro uses infallible unwrap internally
#[allow(clippy::disallowed_methods)]
pub fn list(json: bool, quiet: bool) -> Result<()> {
let fetcher = ModelFetcher::new().map_err(|e| {
CliError::ValidationFailed(format!("Failed to initialize model fetcher: {e}"))
})?;
let models = fetcher.list();
// Contract: apr-list-disk-reconciliation-v1 F-LIST-DISK-001 (paiml/aprender#602).
// pacha's manifest may be missing or stale (e.g., downloads predating GH-162's
// save_manifest fix). Augment with a disk scan of the cache dir so orphan files
// are visible. Manifest entries take precedence; only files not already listed
// are added as disk orphans.
let known_paths: HashSet<std::path::PathBuf> =
models.iter().map(|m| m.path.clone()).collect();
let orphans = scan_cache_dir_for_orphans(fetcher.cache_dir(), &known_paths);
// Contract: apr-list-quiet-wiring-v1 F-LIST-QUIET-001 (paiml/aprender#623).
// Quiet mode: one identifier per line, no decoration, no help text.
// #2401: `emitln!` bypasses the crate-wide `--quiet` stdout gate. This IS
// the quiet output F-LIST-QUIET-001 requires, so it must survive the gate
// that silences ordinary reporting.
if quiet {
for m in &models {
emitln!("{}", m.name);
}
for o in &orphans {
emitln!("{}", o.name);
}
return Ok(());
}
// GH-248: JSON output mode
if json {
let mut models_json: Vec<serde_json::Value> = models
.iter()
.map(|m| {
serde_json::json!({
"name": m.name,
"size_bytes": m.size_bytes,
"format": m.format.name(),
"path": m.path.display().to_string(),
"source": "manifest",
})
})
.collect();
// Contract: apr-list-disk-reconciliation-v1 F-LIST-DISK-001 (paiml/aprender#602).
for o in &orphans {
models_json.push(serde_json::json!({
"name": o.name,
"size_bytes": o.size_bytes,
"format": o.format,
"path": o.path.display().to_string(),
"source": "disk_scan",
}));
}
let stats = fetcher.stats();
let orphan_bytes: u64 = orphans.iter().map(|o| o.size_bytes).sum();
let output = serde_json::json!({
"models": models_json,
"total": models.len() + orphans.len(),
"total_size_bytes": stats.total_size_bytes + orphan_bytes,
});
println!(
"{}",
serde_json::to_string_pretty(&output).unwrap_or_default()
);
return Ok(());
}
println!("{}", "=== Cached Models ===".cyan().bold());
println!();
if models.is_empty() && orphans.is_empty() {
println!("{}", "No cached models found.".dimmed());
println!();
println!("Pull a model with:");
println!(" apr pull hf://Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF/qwen2.5-coder-1.5b-instruct-q4_k_m.gguf");
println!();
println!("Or run directly (auto-downloads):");
println!(" apr run hf://Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF/qwen2.5-coder-1.5b-instruct-q4_k_m.gguf");
return Ok(());
}
// Print header
println!(
"{:<40} {:<12} {:<12} {}",
"NAME".dimmed(),
"SIZE".dimmed(),
"FORMAT".dimmed(),
"PATH".dimmed()
);
println!("{}", "-".repeat(104).dimmed());
for model in &models {
let size = format_bytes(model.size_bytes);
let format = model.format.name();
let name = if model.name.len() > 38 {
format!("{}...", &model.name[..35])
} else {
model.name.clone()
};
println!(
"{:<40} {:<12} {:<12} {}",
name.cyan(),
size.yellow(),
format,
model.path.display().to_string().dimmed()
);
}
// Contract: apr-list-disk-reconciliation-v1 F-LIST-DISK-001 (paiml/aprender#602).
for o in &orphans {
let size = format_bytes(o.size_bytes);
let name = if o.name.len() > 38 {
format!("{}...", &o.name[..35])
} else {
o.name.clone()
};
println!(
"{:<40} {:<12} {:<12} {} {}",
name.cyan(),
size.yellow(),
o.format,
o.path.display().to_string().dimmed(),
"(orphan)".dimmed()
);
}
println!();
// Print stats
let stats = fetcher.stats();
let orphan_bytes: u64 = orphans.iter().map(|o| o.size_bytes).sum();
let total_count = models.len() + orphans.len();
let total_bytes = stats.total_size_bytes + orphan_bytes;
if orphans.is_empty() {
println!("Total: {} models, {} used", total_count, format_bytes(total_bytes));
} else {
println!(
"Total: {} models ({} tracked + {} orphans), {} used",
total_count,
models.len(),
orphans.len(),
format_bytes(total_bytes)
);
}
Ok(())
}