1use crate::accessible_text::{identifier, single_line};
2use serde::Deserialize;
3use serde_json::Value;
4use std::io::{self, Write};
5use tokio::sync::mpsc;
6use tokio_util::sync::CancellationToken;
7
8#[derive(Deserialize)]
9#[serde(
10 tag = "type",
11 rename_all = "lowercase",
12 rename_all_fields = "camelCase",
13 deny_unknown_fields
14)]
15enum ActiveReport {
16 Empty {},
17 Active {
18 model_id: String,
19 backend: String,
20 endpoint: String,
21 port: u16,
22 owned_by_us: bool,
23 runtime_model_id: Option<String>,
24 context: Option<u32>,
25 cache: Option<rigspark_runtime::cache::CacheProfile>,
26 },
27}
28
29fn line(value: &str) -> io::Result<String> {
30 single_line(value)
31}
32
33#[derive(Deserialize, PartialEq, Eq)]
34#[serde(rename_all = "lowercase")]
35enum Runnable {
36 Yes,
37 Slow,
38 No,
39}
40#[derive(Deserialize)]
41#[serde(rename_all = "camelCase", deny_unknown_fields)]
42struct Throughput {
43 known: bool,
44 low_tok_per_sec: f64,
45 high_tok_per_sec: f64,
46}
47#[derive(Deserialize)]
48#[serde(rename_all = "camelCase", deny_unknown_fields)]
49struct ThroughputEvidence {
50 source: String,
51 unknown_reason: Option<String>,
52}
53#[derive(Deserialize)]
54#[serde(rename_all = "camelCase", deny_unknown_fields)]
55struct CanRunEvidence {
56 model_id: String,
57 runnable: Runnable,
58 throughput: Throughput,
59 quant: Option<String>,
60 reason: Option<String>,
61 backends: Vec<String>,
62 throughput_backend: String,
63 required_bytes: Option<f64>,
64 usable_bytes: f64,
65 throughput_evidence: ThroughputEvidence,
66 context: Option<f64>,
67 context_fit_known: Option<bool>,
68}
69
70pub fn can_run_screen(report: &Value) -> io::Result<String> {
71 if report["backends"]
72 .as_array()
73 .is_some_and(|values| values.len() > 1000)
74 {
75 return Err(io::Error::other("backend list exceeds 1000 entries"));
76 }
77 let report: CanRunEvidence =
78 serde_json::from_value(report.clone()).map_err(io::Error::other)?;
79 let valid_bytes = |value: f64| {
80 value.is_finite()
81 && (0.0..=9_007_199_254_740_991.0).contains(&value)
82 && value.fract() == 0.0
83 };
84 let unknown_reason = if report.runnable == Runnable::No {
85 "not-evaluated-model-does-not-fit"
86 } else {
87 "no-sourced-performance-profile"
88 };
89 let throughput = &report.throughput;
90 let valid_throughput = throughput.low_tok_per_sec.is_finite()
91 && throughput.high_tok_per_sec.is_finite()
92 && throughput.low_tok_per_sec >= 0.0
93 && throughput.high_tok_per_sec >= throughput.low_tok_per_sec
94 && if throughput.known {
95 report.throughput_evidence.unknown_reason.is_none()
96 } else {
97 throughput.low_tok_per_sec == 0.0
98 && throughput.high_tok_per_sec == 0.0
99 && report.throughput_evidence.unknown_reason.as_deref() == Some(unknown_reason)
100 };
101 if !valid_bytes(report.usable_bytes)
102 || report
103 .required_bytes
104 .is_some_and(|value| !valid_bytes(value))
105 || !valid_throughput
106 || report.throughput_evidence.source != "offline-estimate"
107 || !rigspark_core::catalog::BACKENDS.contains(&report.throughput_backend.as_str())
108 || report.context.is_some_and(|value| {
109 !value.is_finite() || value.fract() != 0.0 || !(1.0..=10_000_000.0).contains(&value)
110 })
111 || report.context.is_some() != report.context_fit_known.is_some()
112 || (report.runnable == Runnable::Yes && !throughput.known)
113 || (report.runnable == Runnable::No
114 && (throughput.known
115 || report.quant.is_some()
116 || !report.reason.as_deref().is_some_and(|reason| {
117 ["ram-bound", "vram-bound", "disk-bound", "context-bound"].contains(&reason)
118 })))
119 || (report.runnable != Runnable::No
120 && (report.required_bytes.is_none()
121 || report.quant.as_ref().is_none_or(|quant| quant.is_empty())
122 || report.reason.is_some()))
123 {
124 return Err(io::Error::other("invalid or inconsistent can-run evidence"));
125 }
126 let verdict = match report.runnable {
127 Runnable::Yes => "yes",
128 Runnable::Slow => "slow",
129 Runnable::No => "no",
130 };
131 let fit = match report.required_bytes {
132 Some(required) => format!("{required} of {} usable bytes", report.usable_bytes),
133 None => format!(
134 "does not fit: {}",
135 report.reason.as_deref().unwrap_or("unknown")
136 ),
137 };
138 let (label, source, unknown) = if throughput.known {
139 (
140 format!(
141 "{}\u{2013}{} tok/s",
142 throughput.low_tok_per_sec, throughput.high_tok_per_sec
143 ),
144 "offline-estimate".into(),
145 String::new(),
146 )
147 } else {
148 (
149 "unknown".into(),
150 format!("offline-estimate; {unknown_reason}"),
151 format!("\nUnknown reason: {unknown_reason}"),
152 )
153 };
154 let mut backends = report
155 .backends
156 .iter()
157 .take(10)
158 .map(|backend| line(backend))
159 .collect::<io::Result<Vec<_>>>()?
160 .join(", ");
161 if backends.is_empty() {
162 backends = "none".into();
163 }
164 if report.backends.len() > 10 {
165 backends.push_str(&format!(" (+{} more)", report.backends.len() - 10));
166 }
167 let safe_id = !report.model_id.is_empty()
168 && !report.model_id.starts_with('-')
169 && report.model_id.bytes().all(|byte| {
170 byte.is_ascii_lowercase() || byte.is_ascii_digit() || b"._:/-".contains(&byte)
171 })
172 && !report.model_id.split('/').any(|part| part == "..");
173 let next = if report.runnable != Runnable::No && safe_id {
174 format!("\nNext: rigspark up {}", report.model_id)
175 } else {
176 String::new()
177 };
178 let context_warning = if report.context_fit_known == Some(false) {
179 "\nRequested context fit unknown: attention geometry unavailable"
180 } else {
181 ""
182 };
183 Ok(bounded_document(format!(
184 "rigspark / Can Run / Accessible\n1. Target\n{}\n2. Verdict\n{verdict}; quant {}; reason {}; {}{context_warning}\n3. Throughput\n{}; source {}; backend {}{unknown}\n4. Backends\n{backends}{next}\n5. Controls\nCommands: ? help; q quit\n",
185 identifier(&report.model_id)?,
186 line(report.quant.as_deref().unwrap_or("unknown"))?,
187 line(report.reason.as_deref().unwrap_or("none"))?,
188 line(&fit)?,
189 line(&label)?,
190 line(&source)?,
191 line(&report.throughput_backend)?
192 )))
193}
194
195#[derive(Deserialize)]
196#[serde(deny_unknown_fields)]
197struct Check {
198 name: String,
199 status: CheckStatus,
200 detail: String,
201}
202#[derive(Deserialize)]
203#[serde(rename_all = "lowercase")]
204enum CheckStatus {
205 Ok,
206 Warn,
207 Fail,
208}
209#[derive(Deserialize)]
210#[serde(deny_unknown_fields)]
211struct ScoreParts {
212 vram: f64,
213 ram: f64,
214 compute: f64,
215 storage: f64,
216}
217#[derive(Deserialize)]
218#[serde(deny_unknown_fields)]
219struct Score {
220 total: f64,
221 sub: ScoreParts,
222 bottleneck: Bottleneck,
223}
224#[derive(Deserialize)]
225#[serde(rename_all = "lowercase")]
226enum Bottleneck {
227 Vram,
228 Ram,
229 Compute,
230 Storage,
231}
232#[derive(Deserialize)]
233#[serde(rename_all = "camelCase", deny_unknown_fields)]
234struct DoctorReport {
235 ok: bool,
236 checks: Vec<Check>,
237 backends: Vec<rigspark_runtime::diagnostics::BackendInfo>,
238 hardware_score: Option<Score>,
239}
240
241fn bounded_document(document: String) -> String {
242 const NOTICE: &str =
243 "[output bounded; refine the search or inspect a numbered item for more]\n";
244 let budget = 32768 - NOTICE.len();
245 let mut output = String::new();
246 for line in document.split_inclusive('\n') {
247 if output.len() + line.len() > budget {
248 output.push_str(NOTICE);
249 return output;
250 }
251 output.push_str(line);
252 }
253 output
254}
255
256pub fn doctor_screen(report: &Value) -> io::Result<String> {
257 if report["checks"]
258 .as_array()
259 .is_some_and(|values| values.len() > 1000)
260 || report["backends"]
261 .as_array()
262 .is_some_and(|values| values.len() > 1000)
263 {
264 return Err(io::Error::other("diagnostic collection exceeds 1000 items"));
265 }
266 let report: DoctorReport = serde_json::from_value(report.clone()).map_err(io::Error::other)?;
267 if report.ok
268 != report
269 .checks
270 .iter()
271 .all(|check| !matches!(check.status, CheckStatus::Fail))
272 {
273 return Err(io::Error::other("inconsistent diagnostic status"));
274 }
275 let mut checks = Vec::new();
276 for (index, check) in report.checks.iter().take(20).enumerate() {
277 let status = match check.status {
278 CheckStatus::Ok => "OK",
279 CheckStatus::Warn => "WARN",
280 CheckStatus::Fail => "FAIL",
281 };
282 checks.push(format!(
283 "{}. {status} {}: {}",
284 index + 1,
285 line(&check.name)?,
286 line(&check.detail)?
287 ));
288 }
289 if report.checks.len() > 20 {
290 checks.push(format!("+{} more checks", report.checks.len() - 20));
291 }
292 let mut backends = Vec::new();
293 for (index, backend) in report.backends.iter().take(20).enumerate() {
294 backends.push(format!(
295 "{}. {}; {}; version {}; {}; {}",
296 index + 1,
297 line(&backend.name)?,
298 if backend.installed {
299 "installed"
300 } else {
301 "not installed"
302 },
303 line(backend.version.as_deref().unwrap_or("unknown"))?,
304 if backend.is_default {
305 "default"
306 } else {
307 "not default"
308 },
309 line(&backend.install_hint)?
310 ));
311 }
312 if report.backends.len() > 20 {
313 backends.push(format!("+{} more backends", report.backends.len() - 20));
314 }
315 let score = match report.hardware_score {
316 None => "Unknown (not sourced)".into(),
317 Some(score) => {
318 if [
319 score.total,
320 score.sub.vram,
321 score.sub.ram,
322 score.sub.compute,
323 score.sub.storage,
324 ]
325 .iter()
326 .any(|value| !value.is_finite() || !(0.0..=100.0).contains(value))
327 {
328 return Err(io::Error::other("invalid hardware score"));
329 }
330 let bottleneck = match score.bottleneck {
331 Bottleneck::Vram => "VRAM",
332 Bottleneck::Ram => "RAM",
333 Bottleneck::Compute => "Compute",
334 Bottleneck::Storage => "Storage",
335 };
336 format!(
337 "{}/100; bottleneck {bottleneck}; VRAM {}; RAM {}; Compute {}; Storage {}",
338 score.total, score.sub.vram, score.sub.ram, score.sub.compute, score.sub.storage
339 )
340 }
341 };
342 Ok(bounded_document(format!(
343 "rigspark / Doctor / Accessible\n1. Diagnostics\n{}\n2. Backends\n{}\n3. Hardware score\n{score}\n4. Controls\nCommands: ? help; q quit. Suggested commands are text only.\n",
344 checks.join("\n"),
345 backends.join("\n")
346 )))
347}
348
349pub fn active_server_screen(report: &Value) -> io::Result<String> {
350 let report: ActiveReport = serde_json::from_value(report.clone()).map_err(io::Error::other)?;
351 match report {
352 ActiveReport::Empty {}=>Ok("rigspark / Active Server / Accessible\n1. Status\nNo active model.\n2. Next\nrigspark up <model>\n".into()),
353 ActiveReport::Active {model_id,backend,endpoint,port,owned_by_us,runtime_model_id,context,cache}=>{
354 if port==0 || !rigspark_core::catalog::BACKENDS.contains(&backend.as_str())
355 || context.is_some_and(|value|value==0 || value>10_000_000)
356 || runtime_model_id.as_ref().is_some_and(|value|value.len()>8192) {
357 return Err(io::Error::other("invalid active-server report"));
358 }
359 rigspark_runtime::state::loopback(&endpoint).map_err(io::Error::other)?;
360 let cache = cache.map(|cache| format!("Cache: {}\n", cache.summary())).unwrap_or_default();
361 Ok(format!("rigspark / Active Server / Accessible\n1. Model\n{}\n2. Runtime\nBackend: {}\nEndpoint: {}\nPort: {port}\nOwnership: {}\n{cache}",identifier(&model_id)?,line(&backend)?,line(&endpoint)?,if owned_by_us {"owned"} else {"attached"}))
362 }
363 }
364}
365
366pub async fn run_screen(
367 screen: &str,
368 input: &mut mpsc::Receiver<io::Result<String>>,
369 output: &mut impl Write,
370 cancel: &CancellationToken,
371) -> io::Result<()> {
372 if screen.len() > 32768 {
373 return Err(io::Error::other("accessible screen exceeds 32 KiB"));
374 }
375 if cancel.is_cancelled() {
376 return Err(io::Error::new(
377 io::ErrorKind::Interrupted,
378 "interactive input cancelled",
379 ));
380 }
381 output.write_all(screen.as_bytes())?;
382 loop {
383 output.flush()?;
384 let Some(raw) = crate::accessible::answer(input, cancel).await? else {
385 return Ok(());
386 };
387 let sanitized = single_line(&raw)?;
388 let command = sanitized.trim();
389 match command {
390 "q" => return Ok(()),
391 "?" => writeln!(output, "Commands: ? help; q quit")?,
392 value if value.starts_with('/') => {
393 writeln!(output, "Search is available on model-list screens only.")?
394 }
395 value
396 if value.len() <= 3
397 && value
398 .as_bytes()
399 .first()
400 .is_some_and(|byte| (b'1'..=b'9').contains(byte))
401 && value.bytes().all(|byte| byte.is_ascii_digit()) =>
402 {
403 writeln!(
404 output,
405 "Numbered details are available on model-list screens only."
406 )?
407 }
408 _ => writeln!(output, "Unknown command. Enter ? for help.")?,
409 }
410 }
411}