use std::fmt::Write;
use crate::executor;
use crate::scope;
use crate::Priority;
#[must_use]
pub fn dump_task_tree() -> String {
let tasks = executor::debug_task_snapshot();
let queued = executor::debug_queued_task_ids();
let mut by_scope: std::collections::BTreeMap<u64, Vec<(u64, Priority)>> =
std::collections::BTreeMap::new();
for (tid, pri, sid) in &tasks {
by_scope.entry(*sid).or_default().push((*tid, *pri));
}
let mut out = String::new();
out.push_str("=== Auralis Task Tree ===\n");
let _ = writeln!(&mut out, "Total active tasks: {}\n", tasks.len());
if tasks.is_empty() {
out.push_str("(no active tasks)\n");
return out;
}
for (scope_id, mut scope_tasks) in by_scope {
scope_tasks.sort_by_key(|(tid, _)| *tid);
let label = scope::scope_debug_label(scope_id);
match label {
Some(ref lbl) => {
let _ = writeln!(&mut out, "Scope {scope_id} \"{lbl}\":");
}
None => {
let _ = writeln!(&mut out, "Scope {scope_id}:");
}
}
for (tid, pri) in &scope_tasks {
let pri_char = match pri {
Priority::High => 'H',
Priority::Low => 'L',
};
let q = if queued.contains(tid) { " queued" } else { "" };
let _ = writeln!(&mut out, " task {tid} [{pri_char}]{q}");
}
}
out
}