Ergebnisse: Token-Verbrauch je Modell und Batch, Fehlergrund je Lauf, Ergebnisdateien mit Vorschau und Ordner öffnen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
153
src-tauri/src/artifacts.rs
Normal file
153
src-tauri/src/artifacts.rs
Normal file
@ -0,0 +1,153 @@
|
||||
//! Ergebnisdateien eines Laufs anzeigen: die Dateien, die ein Modell in `/work` hinterlassen hat.
|
||||
//! Sie liegen nach dem Lauf unter `<datenordner>/runs/<batch>/<lauf>/work` und werden nur gelesen.
|
||||
|
||||
use serde::Serialize;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
const MAX_FILES: usize = 200;
|
||||
const MAX_DEPTH: usize = 8;
|
||||
/// Größte Textmenge je Datei, die an die Oberfläche geht.
|
||||
const MAX_TEXT_BYTES: usize = 64 * 1024;
|
||||
/// Namen, die kein Ergebnis des Modells sind.
|
||||
const SKIP_NAMES: [&str; 4] = ["__pycache__", ".pytest_cache", ".git", "node_modules"];
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
pub struct FileEntry {
|
||||
/// Pfad relativ zum Ergebnisordner.
|
||||
pub path: String,
|
||||
pub size: u64,
|
||||
/// Inhalt, wenn es Text ist. `None` bei Binärdateien.
|
||||
pub text: Option<String>,
|
||||
/// Der Text wurde gekürzt.
|
||||
pub truncated: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
pub struct FilesReport {
|
||||
/// Ordner mit den Ergebnissen (zum Öffnen im Dateimanager).
|
||||
pub dir: String,
|
||||
pub files: Vec<FileEntry>,
|
||||
/// Es gab mehr Dateien als angezeigt werden.
|
||||
pub more: bool,
|
||||
}
|
||||
|
||||
/// Liest die Dateien eines Ergebnisordners. Ist `allowed_base` gesetzt, muss der Ordner darunter liegen.
|
||||
pub fn list_files(dir: &Path, allowed_base: Option<&Path>) -> Result<FilesReport, String> {
|
||||
let real = dir.canonicalize().map_err(|_| format!("Ergebnisordner {} existiert nicht (mehr).", dir.display()))?;
|
||||
if let Some(base) = allowed_base {
|
||||
let base = base.canonicalize().map_err(|e| format!("Basisordner nicht lesbar: {e}"))?;
|
||||
if !real.starts_with(&base) {
|
||||
return Err("Der Ergebnisordner liegt außerhalb des Datenordners.".into());
|
||||
}
|
||||
}
|
||||
let mut files = Vec::new();
|
||||
let mut more = false;
|
||||
walk(&real, &real, 0, &mut files, &mut more);
|
||||
files.sort_by(|a: &FileEntry, b| a.path.cmp(&b.path));
|
||||
Ok(FilesReport { dir: real.display().to_string(), files, more })
|
||||
}
|
||||
|
||||
fn walk(root: &Path, dir: &Path, depth: usize, out: &mut Vec<FileEntry>, more: &mut bool) {
|
||||
if depth > MAX_DEPTH {
|
||||
return;
|
||||
}
|
||||
let Ok(rd) = fs::read_dir(dir) else { return };
|
||||
let mut entries: Vec<PathBuf> = rd.flatten().map(|e| e.path()).collect();
|
||||
entries.sort();
|
||||
for path in entries {
|
||||
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
if SKIP_NAMES.contains(&name) {
|
||||
continue;
|
||||
}
|
||||
let Ok(meta) = fs::symlink_metadata(&path) else { continue };
|
||||
if meta.file_type().is_symlink() {
|
||||
continue;
|
||||
}
|
||||
if meta.is_dir() {
|
||||
walk(root, &path, depth + 1, out, more);
|
||||
} else if meta.is_file() {
|
||||
if out.len() >= MAX_FILES {
|
||||
*more = true;
|
||||
return;
|
||||
}
|
||||
out.push(read_entry(root, &path, meta.len()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_entry(root: &Path, path: &Path, size: u64) -> FileEntry {
|
||||
let rel = path.strip_prefix(root).unwrap_or(path).display().to_string();
|
||||
let mut text = None;
|
||||
let mut truncated = false;
|
||||
if let Ok(bytes) = fs::read(path) {
|
||||
let cut = bytes.len().min(MAX_TEXT_BYTES);
|
||||
truncated = bytes.len() > cut;
|
||||
let head = &bytes[..cut];
|
||||
if !head.contains(&0) {
|
||||
// Eine abgeschnittene Mehrbyte-Zeichenfolge am Ende ist kein Binärfund.
|
||||
text = match std::str::from_utf8(head) {
|
||||
Ok(t) => Some(t.to_string()),
|
||||
Err(e) if truncated && e.valid_up_to() + 4 > head.len() => Some(String::from_utf8_lossy(&head[..e.valid_up_to()]).to_string()),
|
||||
Err(_) => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
FileEntry { path: rel, size, text, truncated }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn scratch(tag: &str) -> PathBuf {
|
||||
let d = std::env::temp_dir().join(format!("cb-art-{tag}-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&d);
|
||||
fs::create_dir_all(&d).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dateien_werden_mit_text_gelistet_und_unwichtiges_ausgelassen() {
|
||||
let d = scratch("liste");
|
||||
fs::write(d.join("ergebnis.txt"), "4711\n").unwrap();
|
||||
fs::create_dir_all(d.join("sub")).unwrap();
|
||||
fs::write(d.join("sub/code.py"), "print(1)").unwrap();
|
||||
fs::create_dir_all(d.join("__pycache__")).unwrap();
|
||||
fs::write(d.join("__pycache__/x.pyc"), "x").unwrap();
|
||||
fs::write(d.join("bild.bin"), [0u8, 159, 146, 150]).unwrap();
|
||||
#[cfg(unix)]
|
||||
std::os::unix::fs::symlink("/etc/passwd", d.join("link")).unwrap();
|
||||
|
||||
let r = list_files(&d, None).unwrap();
|
||||
let names: Vec<&str> = r.files.iter().map(|f| f.path.as_str()).collect();
|
||||
assert_eq!(names, vec!["bild.bin", "ergebnis.txt", "sub/code.py"]);
|
||||
assert_eq!(r.files[1].text.as_deref(), Some("4711\n"));
|
||||
assert!(r.files[0].text.is_none(), "Binärdatei ohne Text");
|
||||
assert!(!r.more);
|
||||
let _ = fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grosse_datei_wird_gekuerzt() {
|
||||
let d = scratch("gross");
|
||||
fs::write(d.join("gross.txt"), "a".repeat(MAX_TEXT_BYTES + 100)).unwrap();
|
||||
let r = list_files(&d, None).unwrap();
|
||||
assert!(r.files[0].truncated);
|
||||
assert_eq!(r.files[0].text.as_ref().unwrap().len(), MAX_TEXT_BYTES);
|
||||
let _ = fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordner_ausserhalb_der_basis_wird_abgelehnt() {
|
||||
let base = scratch("basis");
|
||||
let inside = base.join("runs/1/x/work");
|
||||
fs::create_dir_all(&inside).unwrap();
|
||||
let outside = scratch("draussen");
|
||||
assert!(list_files(&inside, Some(&base)).is_ok());
|
||||
assert!(list_files(&outside, Some(&base)).is_err());
|
||||
assert!(list_files(&base.join("fehlt"), Some(&base)).is_err());
|
||||
let _ = fs::remove_dir_all(&base);
|
||||
let _ = fs::remove_dir_all(&outside);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
mod agent;
|
||||
mod artifacts;
|
||||
mod config;
|
||||
mod discover;
|
||||
mod external;
|
||||
@ -515,6 +516,27 @@ fn run_steps(
|
||||
store.steps_for_run(batch_id, &model, &task_id, repeat_idx)
|
||||
}
|
||||
|
||||
/// Die Dateien, die ein Lauf in `/work` hinterlassen hat, zum Ansehen. Bei Läufen der App nur unterhalb
|
||||
/// des Datenordners, bei Importen (extern) der Ordner, den man selbst angegeben hat.
|
||||
#[tauri::command]
|
||||
fn run_files(
|
||||
paths: State<'_, AppPaths>,
|
||||
batch_id: i64,
|
||||
model: String,
|
||||
task_id: String,
|
||||
repeat_idx: u32,
|
||||
) -> Result<artifacts::FilesReport, String> {
|
||||
let store = Store::open(&paths.0.data_dir.join(DB_FILE))?;
|
||||
let run = store
|
||||
.runs_for_batch(batch_id)?
|
||||
.into_iter()
|
||||
.find(|r| r.model == model && r.task_id == task_id && r.repeat_idx == repeat_idx)
|
||||
.ok_or("Lauf nicht gefunden.")?;
|
||||
let dir = run.artifact_path.ok_or("Für diesen Lauf wurden keine Dateien gesichert.")?;
|
||||
let base = paths.0.data_dir.join("runs");
|
||||
artifacts::list_files(std::path::Path::new(&dir), if run.external { None } else { Some(base.as_path()) })
|
||||
}
|
||||
|
||||
/// Zustand der Container-Laufzeit (Podman) für das Setup.
|
||||
#[derive(Serialize)]
|
||||
pub struct RuntimeStatus {
|
||||
@ -610,6 +632,7 @@ pub fn run() {
|
||||
batch_results,
|
||||
batch_ranking,
|
||||
run_steps,
|
||||
run_files,
|
||||
runtime_status,
|
||||
image_status,
|
||||
build_image
|
||||
|
||||
@ -49,6 +49,9 @@ pub struct ModelRank {
|
||||
pub successes: usize,
|
||||
pub median_duration_ms: u128,
|
||||
pub mean_completion_tokens: f64,
|
||||
/// Summe der Prompt- und Ausgabe-Tokens aller gewerteten Läufe. Bei Referenzen (extern) 0.
|
||||
pub prompt_tokens_total: u64,
|
||||
pub completion_tokens_total: u64,
|
||||
/// Median der Tokens pro Sekunde über die Läufe (Tokens geteilt durch die gesamte Laufzeit,
|
||||
/// also inklusive Tool- und Prüfzeit, nicht die reine Erzeugungsgeschwindigkeit).
|
||||
pub median_tps: Option<f64>,
|
||||
@ -204,6 +207,8 @@ pub fn compute(batch_id: i64, all_runs: &[RunRecord], tasks: &BTreeMap<String, T
|
||||
successes: rs.iter().filter(|r| r.error_class == ErrorClass::Ok).count(),
|
||||
median_duration_ms: median_u128(&durations),
|
||||
mean_completion_tokens: mean_tokens,
|
||||
prompt_tokens_total: rs.iter().map(|r| r.prompt_tokens as u64).sum(),
|
||||
completion_tokens_total: rs.iter().map(|r| r.completion_tokens as u64).sum(),
|
||||
median_tps,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user