1use crate::cli::DebugMode;
44use pounce_algorithm::debug::{
45 is_live_tolerance, DebugCtx, IterateSnapshot, ResidKind, Residual, BLOCK_NAMES,
46};
47use pounce_algorithm::debug_rank::{RankReport, RankRow};
48use pounce_common::debug::{Checkpoint, DebugAction, DebugHook, DebugState};
49use pounce_common::reg_options::{DefaultValue, OptionType, RegisteredOptions};
50use pounce_nlp::ipopt_nlp::SplitNames;
51use pounce_presolve::dulmage_mendelsohn::DulmageMendelsohnPartition;
52use pounce_presolve::incidence::EqualityIncidence;
53use pounce_presolve::matching::hopcroft_karp;
54use rustyline::completion::{Completer, Pair};
55use rustyline::error::ReadlineError;
56use rustyline::history::FileHistory;
57use rustyline::{Context, Editor, Helper, Highlighter, Hinter, Validator};
58use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
59use std::io::{IsTerminal, Write};
60use std::path::PathBuf;
61use std::rc::Rc;
62
63const COMMANDS: &[&str] = &[
65 "help",
66 "info",
67 "print",
68 "step",
69 "stepi",
70 "continue",
71 "run",
72 "break",
73 "tbreak",
74 "watchpoint",
75 "commands",
76 "stop-at",
77 "set",
78 "get",
79 "opt",
80 "complete",
81 "viz",
82 "save",
83 "load",
84 "sweep",
85 "multistart",
86 "goto",
87 "restart",
88 "resolve",
89 "ask",
90 "watch",
91 "diff",
92 "diagnose",
93 "source",
94 "progress",
95 "detach",
96 "quit",
97];
98
99const EVENTS: &[&str] = &[
102 "resto_entered",
103 "resto_exited",
104 "regularized",
105 "tiny_step",
106 "ls_rejected",
107 "mu_stalled",
108 "nan",
109];
110
111const MU_STALL_ITERS: u32 = 3;
114
115#[derive(Clone)]
118struct WatchPoint {
119 raw: String,
121 block: String,
122 idx: Option<usize>,
123 threshold: f64,
124 last: Option<Vec<f64>>,
126}
127
128const CHECKPOINTS: &[&str] = &[
130 "iter_start",
131 "after_mu",
132 "after_search_dir",
133 "after_step",
134 "step_rejected",
135 "pre_restoration_entry",
136 "post_restoration_exit",
137 "terminated",
138];
139
140pub struct RestartRequest {
144 pub seed_x: Vec<f64>,
147 pub options: Vec<(String, String)>,
149 pub warm: Option<IterateSnapshot>,
155}
156
157pub type RestartCell = Rc<std::cell::RefCell<Option<RestartRequest>>>;
160
161#[derive(Clone)]
163struct SweepRecord {
164 idx: usize,
166 seed: Vec<f64>,
168 status: String,
170 objective: f64,
172 inf_pr: f64,
174 iters: i32,
176}
177
178struct SweepState {
183 queue: VecDeque<Vec<f64>>,
185 current: Option<Vec<f64>>,
187 records: Vec<SweepRecord>,
189 total: usize,
191 saved_pause_iters: bool,
194}
195
196const SNAPSHOT_CAP: usize = 2000;
199
200fn is_success_status(s: &str) -> bool {
203 matches!(s, "Success" | "StopAtAcceptablePoint")
204}
205
206fn parse_floats(s: &str) -> Result<Vec<f64>, String> {
210 s.split(|c: char| c == ',' || c.is_whitespace())
211 .filter(|t| !t.is_empty())
212 .map(|t| t.parse::<f64>().map_err(|_| format!("bad number `{t}`")))
213 .collect()
214}
215
216fn splitmix_unit(state: &mut u64) -> f64 {
219 *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
220 let mut z = *state;
221 z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
222 z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
223 z ^= z >> 31;
224 ((z >> 11) as f64 / (1u64 << 53) as f64) * 2.0 - 1.0
226}
227
228fn seed_for(k: usize) -> u64 {
230 0x9E37_79B9_7F4A_7C15u64
231 ^ (k as u64)
232 .wrapping_mul(0xD1B5_4A32_D192_ED03)
233 .wrapping_add(1)
234}
235
236fn sample_start(base: &[f64], bounds: Option<(&[f64], &[f64])>, rel: f64, k: usize) -> Vec<f64> {
242 if k == 0 {
243 return base.to_vec();
244 }
245 let mut state = seed_for(k);
246 base.iter()
247 .enumerate()
248 .map(|(i, &xi)| {
249 let unit = splitmix_unit(&mut state); if let Some((lo, hi)) = bounds {
251 let (l, u) = (lo[i], hi[i]);
252 if l.is_finite() && u.is_finite() && u > l {
253 return l + (u - l) * (unit * 0.5 + 0.5);
255 }
256 }
257 xi + rel * (xi.abs() + 1.0) * unit
258 })
259 .collect()
260}
261
262#[cfg(test)]
264fn jitter(base: &[f64], rel: f64, k: usize) -> Vec<f64> {
265 sample_start(base, None, rel, k)
266}
267
268pub mod interrupt {
279 use std::sync::atomic::{AtomicBool, Ordering};
280
281 static PENDING: AtomicBool = AtomicBool::new(false);
282 #[cfg(unix)]
283 static INSTALLED: AtomicBool = AtomicBool::new(false);
284
285 #[cfg(unix)]
286 extern "C" fn handler(_sig: nix::libc::c_int) {
287 if PENDING.swap(true, Ordering::SeqCst) {
290 unsafe { nix::libc::_exit(130) };
292 }
293 }
294
295 #[cfg(unix)]
298 pub fn install() {
299 use nix::sys::signal::{self, SigHandler, Signal};
300 if INSTALLED.swap(true, Ordering::SeqCst) {
301 return;
302 }
303 unsafe {
305 let _ = signal::signal(Signal::SIGINT, SigHandler::Handler(handler));
306 }
307 }
308
309 #[cfg(not(unix))]
314 pub fn install() {}
315
316 pub fn take() -> bool {
318 PENDING.swap(false, Ordering::SeqCst)
319 }
320
321 #[cfg(test)]
323 pub fn set_pending_for_test() {
324 PENDING.store(true, Ordering::SeqCst);
325 }
326}
327
328#[derive(Clone, Copy)]
330enum Flow {
331 Stay,
333 Resume,
335 Stop,
337}
338
339struct CmdOut {
341 ok: bool,
342 lines: Vec<String>,
343 data: Option<serde_json::Value>,
344 flow: Flow,
345}
346
347impl CmdOut {
348 fn ok(lines: Vec<String>) -> Self {
349 Self {
350 ok: true,
351 lines,
352 data: None,
353 flow: Flow::Stay,
354 }
355 }
356 fn err(msg: impl Into<String>) -> Self {
357 Self {
358 ok: false,
359 lines: vec![msg.into()],
360 data: None,
361 flow: Flow::Stay,
362 }
363 }
364 fn with_data(mut self, data: serde_json::Value) -> Self {
365 self.data = Some(data);
366 self
367 }
368 fn flow(mut self, flow: Flow) -> Self {
369 self.flow = flow;
370 self
371 }
372}
373
374const METRICS: &[&str] = &[
381 "iter",
382 "mu",
383 "objective",
384 "inf_pr",
385 "inf_du",
386 "nlp_error",
387 "complementarity",
388];
389
390#[derive(Clone, Copy, Debug, PartialEq, Eq)]
392enum Metric {
393 Mu,
394 InfPr,
395 InfDu,
396 Obj,
397 NlpError,
398 Compl,
399 Iter,
400}
401
402impl Metric {
403 fn parse(s: &str) -> Option<Metric> {
404 Some(match s {
405 "mu" => Metric::Mu,
406 "inf_pr" => Metric::InfPr,
407 "inf_du" => Metric::InfDu,
408 "obj" | "objective" => Metric::Obj,
409 "err" | "nlp_error" => Metric::NlpError,
410 "compl" | "complementarity" => Metric::Compl,
411 "iter" => Metric::Iter,
412 _ => return None,
413 })
414 }
415 fn eval(self, ctx: &dyn DebugState) -> f64 {
416 match self {
417 Metric::Mu => ctx.mu(),
418 Metric::InfPr => ctx.inf_pr(),
419 Metric::InfDu => ctx.inf_du(),
420 Metric::Obj => ctx.objective(),
421 Metric::NlpError => ctx.nlp_error(),
422 Metric::Compl => ctx.complementarity(),
423 Metric::Iter => ctx.iter() as f64,
424 }
425 }
426}
427
428fn metric_fields(ctx: &dyn DebugState) -> Vec<(&'static str, serde_json::Value)> {
444 METRICS
445 .iter()
446 .map(|&name| {
447 let value = if name == "iter" {
448 serde_json::json!(ctx.iter())
449 } else {
450 let metric = Metric::parse(name)
451 .expect("every METRICS entry must have a matching Metric arm");
452 serde_json::json!(metric.eval(ctx))
453 };
454 (name, value)
455 })
456 .collect()
457}
458
459fn insert_metric_fields(ev: &mut serde_json::Value, ctx: &dyn DebugState) {
461 if let serde_json::Value::Object(map) = ev {
462 for (name, value) in metric_fields(ctx) {
463 map.insert(name.to_string(), value);
464 }
465 }
466}
467
468#[derive(Clone, Copy, Debug, PartialEq, Eq)]
470enum CmpOp {
471 Lt,
472 Le,
473 Gt,
474 Ge,
475 Eq,
476}
477
478impl CmpOp {
479 fn eval(self, lhs: f64, rhs: f64) -> bool {
480 match self {
481 CmpOp::Lt => lhs < rhs,
482 CmpOp::Le => lhs <= rhs,
483 CmpOp::Gt => lhs > rhs,
484 CmpOp::Ge => lhs >= rhs,
485 CmpOp::Eq => (lhs - rhs).abs() <= 1e-12 * rhs.abs().max(1.0),
491 }
492 }
493}
494
495#[derive(Clone, Debug)]
497struct Atom {
498 metric: Metric,
499 op: CmpOp,
500 rhs: f64,
501}
502
503impl Atom {
504 fn parse(expr: &str) -> Result<Atom, String> {
507 let expr = expr.trim();
508 let mut found: Option<(&str, usize, usize)> = None;
513 for (i, _) in expr.char_indices() {
514 let rest = &expr[i..];
515 if rest.starts_with("<=") || rest.starts_with(">=") || rest.starts_with("==") {
516 found = Some((&expr[i..i + 2], i, 2));
517 break;
518 }
519 if rest.starts_with('<') || rest.starts_with('>') {
520 found = Some((&expr[i..i + 1], i, 1));
521 break;
522 }
523 }
524 let (op, pos, oplen) = found
525 .ok_or_else(|| format!("no comparison operator in `{expr}` (use < <= > >= ==)"))?;
526 let metric_s = expr[..pos].trim();
527 let rhs_s = expr[pos + oplen..].trim();
528 let metric = Metric::parse(metric_s)
529 .ok_or_else(|| format!("unknown metric `{metric_s}` (one of {METRICS:?})"))?;
530 let rhs = rhs_s
531 .parse::<f64>()
532 .map_err(|_| format!("bad threshold `{rhs_s}`"))?;
533 let cmp = match op {
534 "<" => CmpOp::Lt,
535 "<=" => CmpOp::Le,
536 ">" => CmpOp::Gt,
537 ">=" => CmpOp::Ge,
538 "==" => CmpOp::Eq,
539 _ => unreachable!(),
540 };
541 Ok(Atom {
542 metric,
543 op: cmp,
544 rhs,
545 })
546 }
547
548 fn holds(&self, ctx: &dyn DebugState) -> bool {
549 self.op.eval(self.metric.eval(ctx), self.rhs)
550 }
551}
552
553#[derive(Clone, Copy, Debug, PartialEq, Eq)]
555enum Join {
556 And,
557 Or,
558}
559
560#[derive(Clone, Debug)]
565struct Condition {
566 first: Atom,
567 rest: Vec<(Join, Atom)>,
568 raw: String,
570}
571
572impl Condition {
573 fn parse(expr: &str) -> Result<Condition, String> {
574 let cleaned: String = expr.chars().filter(|c| !matches!(c, '(' | ')')).collect();
576 let mut atoms: Vec<(Option<Join>, &str)> = Vec::new();
578 let bytes = cleaned.as_bytes();
579 let mut start = 0usize;
580 let mut i = 0usize;
581 let mut pending: Option<Join> = None;
582 while i + 1 < bytes.len() {
583 let two = &cleaned[i..i + 2];
584 let join = match two {
585 "&&" => Some(Join::And),
586 "||" => Some(Join::Or),
587 _ => None,
588 };
589 if let Some(j) = join {
590 atoms.push((pending, &cleaned[start..i]));
591 pending = Some(j);
592 i += 2;
593 start = i;
594 } else {
595 i += 1;
596 }
597 }
598 atoms.push((pending, &cleaned[start..]));
599
600 let mut iter = atoms.into_iter();
601 let Some((_, first_s)) = iter.next() else {
602 return Err("empty condition".into());
603 };
604 let first = Atom::parse(first_s)?;
605 let mut rest = Vec::new();
606 for (join, s) in iter {
607 let join = join.ok_or("malformed compound condition (dangling &&/||)")?;
608 rest.push((join, Atom::parse(s)?));
609 }
610 Ok(Condition {
612 first,
613 rest,
614 raw: cleaned,
615 })
616 }
617
618 fn holds(&self, ctx: &dyn DebugState) -> bool {
619 let mut acc = self.first.holds(ctx);
620 for (join, atom) in &self.rest {
621 let v = atom.holds(ctx);
622 acc = match join {
623 Join::And => acc && v,
624 Join::Or => acc || v,
625 };
626 }
627 acc
628 }
629}
630
631fn path_candidates(word: &str) -> Vec<String> {
641 let (dir, prefix) = match word.rfind('/') {
643 Some(i) => (&word[..=i], &word[i + 1..]), None => ("", word),
645 };
646 let read_from = if dir.is_empty() { "." } else { dir };
647 let Ok(entries) = std::fs::read_dir(read_from) else {
648 return Vec::new();
649 };
650 let mut out: Vec<String> = Vec::new();
651 for e in entries.flatten() {
652 let name = e.file_name().to_string_lossy().into_owned();
653 if !name.starts_with(prefix) {
654 continue;
655 }
656 if name.starts_with('.') && !prefix.starts_with('.') {
657 continue;
658 }
659 let is_dir = e.file_type().map(|t| t.is_dir()).unwrap_or(false);
660 let mut cand = format!("{dir}{name}");
661 if is_dir {
662 cand.push('/');
663 }
664 out.push(cand);
665 }
666 out.sort();
667 out
668}
669
670fn completion_candidates(reg: Option<&RegisteredOptions>, before: &str, word: &str) -> Vec<String> {
671 let toks: Vec<&str> = before.split_whitespace().collect();
672 let starts = |opts: &[&str]| -> Vec<String> {
673 opts.iter()
674 .filter(|c| c.starts_with(word))
675 .map(|c| c.to_string())
676 .collect()
677 };
678 let opt_names = || -> Vec<String> {
679 reg.map(|r| {
680 r.registered_options_in_order()
681 .iter()
682 .map(|o| o.name.clone())
683 .filter(|n| n.starts_with(word))
684 .collect()
685 })
686 .unwrap_or_default()
687 };
688 match toks.as_slice() {
689 [] => starts(COMMANDS),
690 ["set"] => {
691 let mut v = starts(&["mu", "opt"]);
692 v.extend(starts(&BLOCK_NAMES));
693 v
694 }
695 ["set", "opt"] | ["get", "opt"] | ["get"] | ["opt"] | ["options"] => opt_names(),
696 ["set", "opt", name] => reg
698 .and_then(|r| r.get_option(name))
699 .map(|o| {
700 o.valid_strings
701 .iter()
702 .map(|e| e.value.clone())
703 .filter(|v| v.starts_with(word) && v != "*")
704 .collect()
705 })
706 .unwrap_or_default(),
707 ["stop-at"] | ["stopat"] => starts(CHECKPOINTS),
708 ["break", "if"] | ["b", "if"] => starts(METRICS),
709 ["break", "on"] | ["b", "on"] => starts(EVENTS),
710 ["break"] | ["b"] => starts(&["if", "on", "clear", "del"]),
711 ["watchpoint"] | ["wp"] => starts(&BLOCK_NAMES),
712 ["print"] | ["p"] | ["watch"] | ["display"] => {
713 let mut v = starts(&BLOCK_NAMES);
714 v.extend(starts(&[
715 "mu",
716 "obj",
717 "inf_pr",
718 "inf_du",
719 "err",
720 "compl",
721 "iter",
722 "kkt",
723 "active",
724 "inactive",
725 "residuals",
726 "equation",
727 "rank",
728 ]));
729 v
730 }
731 ["viz"] | ["plot"] => {
732 let mut v = starts(&BLOCK_NAMES);
733 v.extend(starts(&["kkt", "L"]));
734 v
735 }
736 ["complete"] => starts(COMMANDS),
737 ["save"] | ["load"] | ["sweep"] | ["source"] => path_candidates(word),
739 ["load", _] => starts(&BLOCK_NAMES),
741 _ => Vec::new(),
742 }
743}
744
745#[derive(Helper, Hinter, Highlighter, Validator)]
749struct DbgHelper {
750 reg: Option<Rc<RegisteredOptions>>,
751}
752
753impl Completer for DbgHelper {
754 type Candidate = Pair;
755 fn complete(
756 &self,
757 line: &str,
758 pos: usize,
759 _ctx: &Context<'_>,
760 ) -> rustyline::Result<(usize, Vec<Pair>)> {
761 let before = &line[..pos];
762 let start = before
763 .rfind(char::is_whitespace)
764 .map(|i| i + 1)
765 .unwrap_or(0);
766 let word = &before[start..];
767 let cands = completion_candidates(self.reg.as_deref(), &before[..start], word);
768 let pairs = cands
769 .into_iter()
770 .map(|c| Pair {
771 display: c.clone(),
772 replacement: c,
773 })
774 .collect();
775 Ok((start, pairs))
776 }
777}
778
779pub struct EquationBook {
789 names: Vec<String>,
792 equations: Vec<String>,
794}
795
796impl EquationBook {
797 pub fn new(names: Vec<String>, equations: Vec<String>) -> Self {
800 Self { names, equations }
801 }
802
803 pub fn len(&self) -> usize {
805 self.equations.len()
806 }
807
808 pub fn is_empty(&self) -> bool {
810 self.equations.is_empty()
811 }
812
813 fn label(&self, i: usize) -> String {
816 match self.names.get(i) {
817 Some(n) if !n.is_empty() => n.clone(),
818 _ => format!("c[{i}]"),
819 }
820 }
821
822 fn resolve(&self, key: &str) -> Option<usize> {
825 if let Some(i) = self.names.iter().position(|n| n == key) {
826 return Some(i);
827 }
828 key.parse::<usize>()
829 .ok()
830 .filter(|&i| i < self.equations.len())
831 }
832}
833
834const MAX_STRUCT_NAMES: usize = 10;
839
840const MAX_SINGULAR_VALUES_SHOWN: usize = 16;
843
844const MAX_RANK_CULPRITS: usize = 12;
847
848pub struct StructureBook {
866 inc: EqualityIncidence,
869 con_names: Vec<String>,
872 var_names: Vec<String>,
875}
876
877impl StructureBook {
878 pub fn new(inc: EqualityIncidence, con_names: Vec<String>, var_names: Vec<String>) -> Self {
884 Self {
885 inc,
886 con_names,
887 var_names,
888 }
889 }
890
891 fn con_label(&self, eq_row: usize) -> String {
894 let orig = self.inc.eq_row_inner_idx[eq_row];
895 match self.con_names.get(orig) {
896 Some(n) if !n.is_empty() => n.clone(),
897 _ => format!("c[{orig}]"),
898 }
899 }
900
901 fn var_label(&self, v: usize) -> String {
904 match self.var_names.get(v) {
905 Some(n) if !n.is_empty() => n.clone(),
906 _ => format!("x[{v}]"),
907 }
908 }
909
910 fn join_capped(labels: &[String]) -> String {
913 if labels.len() <= MAX_STRUCT_NAMES {
914 labels.join(", ")
915 } else {
916 let head = labels[..MAX_STRUCT_NAMES].join(", ");
917 let more = labels.len() - MAX_STRUCT_NAMES;
918 format!("{head}, … (+{more} more)")
919 }
920 }
921
922 fn findings(&self) -> Vec<(&'static str, &'static str, String)> {
932 let mut out = Vec::new();
933 if self.inc.n_eq_rows() == 0 {
934 return out;
935 }
936 let matching = hopcroft_karp(&self.inc);
937 let dm = DulmageMendelsohnPartition::from_matching(&self.inc, &matching);
938 if dm.over_rows.is_empty() {
939 return out;
940 }
941
942 let excess = dm.over_rows.len().saturating_sub(dm.over_cols.len());
946 let eq_labels: Vec<String> = dm.over_rows.iter().map(|&r| self.con_label(r)).collect();
947 let var_labels: Vec<String> = dm.over_cols.iter().map(|&v| self.var_label(v)).collect();
948 let eqs = Self::join_capped(&eq_labels);
949 let shared = if var_labels.is_empty() {
950 "no variables".to_string()
951 } else {
952 Self::join_capped(&var_labels)
953 };
954 out.push((
955 "warning",
956 "structural_singularity",
957 format!(
958 "Constraint Jacobian is structurally singular (Dulmage–Mendelsohn): {} equation(s) \
959 over-determine the {} variable(s) they jointly touch ({}), so ≥{} of them must be \
960 redundant or mutually inconsistent (LICQ fails on this block). Candidate \
961 dependent equations: {}. Inspect them with `print equation <name>`; this names \
962 the rows behind any δ_c dual-regularization / wrong-inertia signal.",
963 dm.over_rows.len(),
964 dm.over_cols.len(),
965 shared,
966 excess.max(1),
967 eqs
968 ),
969 ));
970 out
971 }
972}
973
974pub struct SolverDebugger {
975 mode: DebugMode,
976 reg: Option<Rc<RegisteredOptions>>,
977 step: bool,
979 run_to: Option<i32>,
981 breaks: Vec<i32>,
983 temp_breaks: Vec<i32>,
985 bp_commands: HashMap<i32, Vec<String>>,
988 conds: Vec<Condition>,
990 watchpoints: Vec<WatchPoint>,
992 last_mu: Option<f64>,
994 mu_stall: u32,
995 in_restoration: bool,
998 detached: bool,
1000 hello_sent: bool,
1003 pause_iters: bool,
1006 pause_terminal: bool,
1008 terminal_only_on_error: bool,
1010 interruptible: bool,
1012 emit_progress: bool,
1015 sub_step: bool,
1018 stop_at: HashSet<&'static str>,
1020 break_events: HashSet<&'static str>,
1022 snapshots: BTreeMap<i32, Box<dyn pounce_common::debug::IterSnapshot>>,
1025 restart: Option<RestartCell>,
1028 editor: Option<Editor<DbgHelper, FileHistory>>,
1032 hist_path: Option<PathBuf>,
1034 pump: Option<StdinPump>,
1037 watches: Vec<String>,
1040 pending_script: Option<String>,
1043 staged: Vec<(String, String)>,
1047 sweep: Option<SweepState>,
1050 prompt_interrupts: u8,
1055 equation_book: Option<EquationBook>,
1060 structure_book: Option<StructureBook>,
1065 script_queue: Option<SharedScript>,
1071}
1072
1073pub type SharedScript = Rc<std::cell::RefCell<VecDeque<String>>>;
1077
1078impl SolverDebugger {
1079 pub fn new(mode: DebugMode, reg: Option<Rc<RegisteredOptions>>) -> Self {
1082 Self {
1083 mode,
1084 reg,
1085 step: true,
1088 run_to: None,
1089 breaks: Vec::new(),
1090 temp_breaks: Vec::new(),
1091 bp_commands: HashMap::new(),
1092 conds: Vec::new(),
1093 watchpoints: Vec::new(),
1094 last_mu: None,
1095 mu_stall: 0,
1096 in_restoration: false,
1097 detached: false,
1098 hello_sent: false,
1099 pause_iters: true,
1100 pause_terminal: true,
1101 terminal_only_on_error: false,
1102 interruptible: true,
1103 emit_progress: true,
1104 sub_step: false,
1105 stop_at: HashSet::new(),
1106 break_events: HashSet::new(),
1107 snapshots: BTreeMap::new(),
1108 restart: None,
1109 editor: None,
1110 hist_path: None,
1111 pump: None,
1112 watches: Vec::new(),
1113 pending_script: None,
1114 staged: Vec::new(),
1115 sweep: None,
1116 prompt_interrupts: 0,
1117 equation_book: None,
1118 structure_book: None,
1119 script_queue: None,
1120 }
1121 }
1122
1123 pub fn quiet(mode: DebugMode, reg: Option<Rc<RegisteredOptions>>) -> Self {
1129 let mut d = Self::new(mode, reg);
1130 d.step = false;
1131 d.pause_iters = false;
1132 d.pause_terminal = false;
1133 d.detached = true;
1134 d
1135 }
1136
1137 pub fn with_script(mut self, path: String) -> Self {
1139 self.pending_script = Some(path);
1140 self
1141 }
1142
1143 pub fn set_equation_book(&mut self, book: EquationBook) {
1147 self.equation_book = Some(book);
1148 }
1149
1150 pub fn set_structure_book(&mut self, book: StructureBook) {
1156 self.structure_book = Some(book);
1157 }
1158
1159 pub fn with_shared_script(mut self, queue: SharedScript) -> Self {
1163 self.script_queue = Some(queue);
1164 self
1165 }
1166
1167 pub fn with_restart(mut self, cell: RestartCell) -> Self {
1170 self.restart = Some(cell);
1171 self
1172 }
1173
1174 pub fn on_error(mode: DebugMode, reg: Option<Rc<RegisteredOptions>>) -> Self {
1177 Self {
1178 step: false,
1179 pause_iters: false,
1180 terminal_only_on_error: true,
1181 ..Self::new(mode, reg)
1182 }
1183 }
1184
1185 pub fn on_interrupt(mode: DebugMode, reg: Option<Rc<RegisteredOptions>>) -> Self {
1189 Self {
1190 step: false,
1191 pause_iters: false,
1192 pause_terminal: false,
1193 ..Self::new(mode, reg)
1194 }
1195 }
1196
1197 pub fn staged_options(&self) -> &[(String, String)] {
1200 &self.staged
1201 }
1202
1203 fn should_pause(&mut self, iter: i32) -> bool {
1204 if self.detached {
1205 return false;
1206 }
1207 if self.step {
1208 return true;
1209 }
1210 if let Some(t) = self.run_to {
1211 if iter >= t {
1212 self.run_to = None;
1213 return true;
1214 }
1215 }
1216 if self.breaks.contains(&iter) {
1217 return true;
1218 }
1219 if let Some(pos) = self.temp_breaks.iter().position(|&b| b == iter) {
1221 self.temp_breaks.remove(pos);
1222 return true;
1223 }
1224 false
1225 }
1226
1227 fn matched_condition(&self, ctx: &dyn DebugState) -> Option<String> {
1230 if self.detached {
1231 return None;
1232 }
1233 self.conds
1234 .iter()
1235 .find(|c| c.holds(ctx))
1236 .map(|c| c.raw.clone())
1237 }
1238
1239 fn matched_event(&self, ctx: &dyn DebugState) -> Option<&'static str> {
1243 if self.detached || self.break_events.is_empty() {
1244 return None;
1245 }
1246 let cp = ctx.checkpoint();
1247 let tiny = 1e-10;
1249 EVENTS.iter().copied().find(|&e| {
1250 self.break_events.contains(e)
1251 && match e {
1252 "resto_entered" => cp == Checkpoint::PreRestoration,
1253 "resto_exited" => cp == Checkpoint::PostRestoration,
1254 "regularized" => {
1255 cp == Checkpoint::AfterSearchDirection && ctx.regularization() > 0.0
1256 }
1257 "tiny_step" => {
1258 cp == Checkpoint::AfterSearchDirection
1259 && ctx
1260 .delta_block("x")
1261 .map(|v| v.iter().fold(0.0_f64, |m, &x| m.max(x.abs())) < tiny)
1262 .unwrap_or(false)
1263 }
1264 "ls_rejected" => cp == Checkpoint::AfterStep && ctx.ls_count() > 1,
1265 "mu_stalled" => cp == Checkpoint::IterStart && self.mu_stall >= MU_STALL_ITERS,
1266 "nan" => !ctx.nlp_error().is_finite() || !ctx.objective().is_finite(),
1267 _ => false,
1268 }
1269 })
1270 }
1271
1272 fn update_mu_stall(&mut self, mu: f64) {
1274 if let Some(last) = self.last_mu {
1275 if (mu - last).abs() <= 1e-12 * last.abs().max(1.0) {
1276 self.mu_stall += 1;
1277 } else {
1278 self.mu_stall = 0;
1279 }
1280 }
1281 self.last_mu = Some(mu);
1282 }
1283
1284 fn matched_watchpoint(&mut self, ctx: &dyn DebugState) -> Option<String> {
1287 if self.detached {
1288 return None;
1289 }
1290 let mut hit = None;
1291 for wp in self.watchpoints.iter_mut() {
1292 let Some(full) = ctx.block(&wp.block) else {
1293 continue;
1294 };
1295 let cur: Vec<f64> = match wp.idx {
1296 Some(i) => match full.get(i) {
1297 Some(&v) => vec![v],
1298 None => continue,
1299 },
1300 None => full,
1301 };
1302 if let Some(prev) = &wp.last {
1303 if prev.len() == cur.len() {
1304 let changed = prev
1305 .iter()
1306 .zip(&cur)
1307 .any(|(p, c)| (p - c).abs() > wp.threshold);
1308 if changed && hit.is_none() {
1309 hit = Some(wp.raw.clone());
1310 }
1311 }
1312 }
1313 wp.last = Some(cur);
1314 }
1315 hit
1316 }
1317
1318 fn dispatch(&mut self, line: &str, ctx: &mut dyn DebugState) -> CmdOut {
1321 let owned = tokenize_quoted(line);
1325 let toks: Vec<&str> = owned.iter().map(String::as_str).collect();
1326 let Some(&verb) = toks.first() else {
1327 return CmdOut::ok(vec![]); };
1329 let rest = &toks[1..];
1330 match verb {
1331 "help" | "h" | "?" => self.cmd_help(),
1332 "info" | "i" => self.cmd_info(ctx),
1333 "print" | "p" => self.cmd_print(rest, ctx),
1334 "step" | "s" | "n" | "next" if rest.first() == Some(&"sub") => {
1337 self.sub_step = true;
1338 CmdOut::ok(vec![
1339 "stepping to the next checkpoint (sub-iteration)".into()
1340 ])
1341 .flow(Flow::Resume)
1342 }
1343 "step" | "s" | "n" | "next" => {
1344 self.step = true;
1345 CmdOut::ok(vec!["stepping one iteration".into()]).flow(Flow::Resume)
1346 }
1347 "stepi" | "si" => {
1348 self.sub_step = true;
1349 CmdOut::ok(vec![
1350 "stepping to the next checkpoint (sub-iteration)".into()
1351 ])
1352 .flow(Flow::Resume)
1353 }
1354 "continue" | "c" | "cont" => {
1355 self.step = false;
1356 self.sub_step = false;
1357 self.run_to = None;
1358 CmdOut::ok(vec!["continuing".into()]).flow(Flow::Resume)
1359 }
1360 "run" | "r" => self.cmd_run(rest),
1361 "break" | "b" => self.cmd_break(rest),
1362 "tbreak" | "tb" => match rest.first().and_then(|s| s.parse::<i32>().ok()) {
1363 Some(n) => {
1364 if !self.temp_breaks.contains(&n) {
1365 self.temp_breaks.push(n);
1366 }
1367 CmdOut::ok(vec![format!("temporary breakpoint at iteration {n}")])
1368 }
1369 None => CmdOut::err("usage: tbreak <iteration>"),
1370 },
1371 "watchpoint" | "wp" => self.cmd_watchpoint(rest, ctx),
1372 "commands" => self.cmd_commands(rest),
1373 "stop-at" | "stopat" => self.cmd_stop_at(rest),
1374 "progress" => match rest.first().copied() {
1375 Some("on") | None => {
1376 self.emit_progress = true;
1377 CmdOut::ok(vec!["progress events on".into()])
1378 }
1379 Some("off") => {
1380 self.emit_progress = false;
1381 CmdOut::ok(vec!["progress events off".into()])
1382 }
1383 _ => CmdOut::err("usage: progress [on|off]"),
1384 },
1385 "set" => self.cmd_set(rest, ctx),
1386 "get" => self.cmd_get(rest),
1387 "opt" | "options" => self.cmd_opt(rest),
1388 "complete" => self.cmd_complete(rest),
1389 "viz" | "plot" => self.cmd_viz(rest, ctx),
1390 "save" => self.cmd_save(rest, ctx),
1391 "load" => match as_nlp_mut(ctx) {
1392 Some(c) => self.cmd_load(rest, c),
1393 None => nlp_only("load"),
1394 },
1395 "sweep" => match as_nlp_mut(ctx) {
1396 Some(c) => self.cmd_sweep(rest, c),
1397 None => nlp_only("sweep"),
1398 },
1399 "multistart" => match as_nlp_mut(ctx) {
1400 Some(c) => self.cmd_multistart(rest, c),
1401 None => nlp_only("multistart"),
1402 },
1403 "goto" | "jump" => self.cmd_goto(rest, ctx),
1404 "restart" => match self.snapshots.keys().next().copied() {
1405 Some(k) => self.restore_to(k, ctx),
1406 None => CmdOut::err("no snapshots captured yet"),
1407 },
1408 "resolve" | "re-solve" => match as_nlp(ctx) {
1409 Some(c) => self.cmd_resolve(c),
1410 None => nlp_only("resolve"),
1411 },
1412 "ask" | "explain" | "claude" => self.cmd_ask(rest, ctx),
1413 "watch" | "display" => self.cmd_watch(rest),
1414 "diff" => self.cmd_diff(ctx),
1415 "diagnose" | "diag" => match as_nlp(ctx) {
1416 Some(c) => self.cmd_diagnose(c),
1417 None => nlp_only("diagnose"),
1418 },
1419 "source" => self.cmd_source(rest, ctx),
1420 "detach" => {
1421 self.detached = true;
1422 self.step = false;
1423 self.run_to = None;
1424 CmdOut::ok(vec!["detached — solving to completion".into()]).flow(Flow::Resume)
1425 }
1426 "pause" => CmdOut::ok(vec!["already paused".into()]),
1429 "coffee" | "brew" | "espresso" => self.cmd_coffee(),
1431 "quit" | "q" | "exit" => CmdOut::ok(vec!["stopping solve".into()]).flow(Flow::Stop),
1432 other => CmdOut::err(format!("unknown command `{other}` (try `help`)")),
1433 }
1434 }
1435
1436 fn cmd_coffee(&self) -> CmdOut {
1440 let color = matches!(self.mode, DebugMode::Repl)
1441 && std::io::stderr().is_terminal()
1442 && std::env::var_os("NO_COLOR").is_none();
1443 let paint = |r: u8, g: u8, b: u8, s: &str| -> String {
1444 if color {
1445 format!("\x1b[38;2;{r};{g};{b}m{s}\x1b[0m")
1446 } else {
1447 s.to_string()
1448 }
1449 };
1450 let cup = |s: &str| paint(0xEC, 0xEC, 0xEF, s);
1452 let dark = |s: &str| paint(0x5A, 0x32, 0x1E, s);
1453 let brew = |s: &str| paint(0x96, 0x5F, 0x37, s);
1454 let steam = |s: &str| paint(0xB4, 0xB9, 0xC3, s);
1455 let lines = vec![
1456 String::new(),
1457 format!(" {}", steam(") ) )")),
1458 format!(" {}", steam("( ( (")),
1459 format!(" {}", cup("._________.")),
1460 format!(" {}{}{}", cup("|"), dark("~~~~~~~~"), cup("|_")),
1461 format!(" {}{}{}", cup("| "), brew("COFFEE"), cup("| |")),
1462 format!(" {}{}{}", cup("| "), dark("~~~~~~"), cup("| |")),
1463 format!(" {}", cup("|________|_|")),
1464 format!(" {}", cup("\\________/")),
1465 format!(" {}", brew("a fresh cup for a stuck solve")),
1466 String::new(),
1467 ];
1468 CmdOut::ok(lines).with_data(serde_json::json!({"easter_egg": "coffee"}))
1469 }
1470
1471 fn cmd_help(&self) -> CmdOut {
1472 let lines = vec![
1473 "commands:".into(),
1474 " info | i summary of the current iterate".into(),
1475 " print | p <what> x|s|y_c|y_d|z_l|z_u|v_l|v_u | dx (step) |".into(),
1476 " mu|obj|inf_pr|inf_du|err|compl|iter | kkt | active | inactive".into(),
1477 " print residuals [pr|du] [k] top-k largest-magnitude residuals (default k=10)".into(),
1478 " print equation [name|row] source algebra of a constraint, by model name or row".into(),
1479 " print rank SVD rank of the equality Jacobian; names dependent equations".into(),
1480 " step | s | n run one iteration, pause again".into(),
1481 " stepi | si | step sub run to the next checkpoint (into sub-iteration phases)".into(),
1482 " progress [on|off] toggle per-iteration progress events (JSON mode)".into(),
1483 " stop-at <cp> always pause at a checkpoint: after_mu|after_search_dir|after_step".into(),
1484 " continue | c run to the next breakpoint".into(),
1485 " run | r <N> run until iteration N".into(),
1486 " break | b [N|clear|del N] set/list/clear breakpoints".into(),
1487 " break if <m><op><v> conditional bp; m in mu|inf_pr|inf_du|obj|err|iter,".into(),
1488 " op in < <= > >= == (e.g. break if inf_pr<1e-6)".into(),
1489 " break on <event> event bp: resto_entered|resto_exited|regularized|".into(),
1490 " tiny_step|ls_rejected|mu_stalled|nan".into(),
1491 " tbreak <N> one-shot breakpoint (deletes after firing)".into(),
1492 " watchpoint <blk>[<i>] [τ] pause when a value changes by > τ (alias wp)".into(),
1493 " commands <N> <c>;<c>… auto-run commands when iter N's breakpoint hits".into(),
1494 " set mu <v> overwrite the barrier parameter".into(),
1495 " set <blk>[<i>] <v> overwrite one component (e.g. set x[2] 1.5)".into(),
1496 " set <blk> <v0,v1,...> overwrite a whole block".into(),
1497 " set opt <name> <value> stage a solver option (validated)".into(),
1498 " get opt <name> show an option's effective value (staged or default)".into(),
1499 " opt [filter] list solver options (name/type/default)".into(),
1500 " complete <prefix> completion candidates (commands + options)".into(),
1501 " viz <x|s|dx|...|kkt|L> open the artifact in an external viewer".into(),
1502 " save [path] write the current iterate + residuals to JSON".into(),
1503 " load <file> [block] read a block (default x) from a save artifact / numeric file".into(),
1504 " sweep <file> one solve per start in <file>; tabulate outcomes".into(),
1505 " multistart <N> [rel] N restarts (uniform in each finite box; jitter else)".into(),
1506 " goto <k> | restart rewind to a captured iteration (primal-dual only)".into(),
1507 " resolve re-solve from the current x with staged `set opt`s".into(),
1508 " ask [question] ask an LLM about the state (default Claude Code; set".into(),
1509 " POUNCE_DBG_LLM=claude|codex|gemini|llm or a command template)".into(),
1510 " watch [target|clear|del] auto-print a `print` target at every pause".into(),
1511 " diff what changed in the iterate since the last iteration".into(),
1512 " diagnose | diag live health report: named culprit residuals, KKT inertia, stalls".into(),
1513 " source <file> run debugger commands from a file".into(),
1514 " detach stop pausing; solve to completion".into(),
1515 " quit | q stop the solve now".into(),
1516 ];
1517 CmdOut::ok(lines)
1518 }
1519
1520 fn cmd_info(&self, ctx: &dyn DebugState) -> CmdOut {
1521 let dims: Vec<_> = ctx.block_dims();
1522 let dims_json: serde_json::Map<String, serde_json::Value> = dims
1523 .iter()
1524 .map(|(n, d)| ((*n).to_string(), serde_json::json!(d)))
1525 .collect();
1526 let lines = vec![
1527 format!("iter = {}", ctx.iter()),
1528 format!("mu = {:.6e}", ctx.mu()),
1529 format!("objective = {:.8e}", ctx.objective()),
1530 format!("inf_pr = {:.6e}", ctx.inf_pr()),
1531 format!("inf_du = {:.6e}", ctx.inf_du()),
1532 format!("nlp_error = {:.6e}", ctx.nlp_error()),
1533 format!(
1534 "dims = {}",
1535 dims.iter()
1536 .map(|(n, d)| format!("{n}:{d}"))
1537 .collect::<Vec<_>>()
1538 .join(" ")
1539 ),
1540 ];
1541 let mut data = serde_json::json!({ "dims": dims_json });
1542 insert_metric_fields(&mut data, ctx);
1546 CmdOut::ok(lines).with_data(data)
1547 }
1548
1549 fn cmd_print(&self, rest: &[&str], ctx: &dyn DebugState) -> CmdOut {
1550 let Some(&what) = rest.first() else {
1551 return self.cmd_info(ctx);
1552 };
1553 if what == "kkt" {
1554 return self.cmd_print_kkt(ctx);
1555 }
1556 if what == "active" {
1557 return self.cmd_print_bounds(ctx, true);
1558 }
1559 if what == "inactive" {
1560 return self.cmd_print_bounds(ctx, false);
1561 }
1562 if what == "residuals" || what == "resid" {
1563 return self.cmd_print_residuals(&rest[1..], ctx);
1564 }
1565 if what == "equation" || what == "eqn" || what == "eq" {
1566 return self.cmd_print_equation(&rest[1..]);
1567 }
1568 if what == "rank" {
1569 return match as_nlp(ctx) {
1570 Some(c) => self.cmd_print_rank(c),
1571 None => nlp_only("print rank"),
1572 };
1573 }
1574 let delta = what.strip_prefix("d").filter(|b| is_block(ctx, b));
1576 if is_block(ctx, what) {
1577 match ctx.block(what) {
1578 Some(v) => CmdOut::ok(vec![fmt_vec(what, &v)])
1579 .with_data(serde_json::json!({"name": what, "values": v})),
1580 None => CmdOut::err(format!("no iterate yet for block `{what}`")),
1581 }
1582 } else if let Some(blk) = delta {
1583 match ctx.delta_block(blk) {
1584 Some(v) => CmdOut::ok(vec![fmt_vec(&format!("d{blk}"), &v)])
1585 .with_data(serde_json::json!({"name": format!("d{blk}"), "values": v})),
1586 None => CmdOut::err(format!("no search direction available for `d{blk}` yet")),
1587 }
1588 } else {
1589 let val = match what {
1590 "mu" => ctx.mu(),
1591 "obj" | "objective" => ctx.objective(),
1592 "inf_pr" => ctx.inf_pr(),
1593 "inf_du" => ctx.inf_du(),
1594 "err" | "nlp_error" => ctx.nlp_error(),
1595 "compl" | "complementarity" => ctx.complementarity(),
1596 "iter" => ctx.iter() as f64,
1597 _ => {
1598 return CmdOut::err(format!(
1599 "don't know how to print `{what}` (try a block name or mu|obj|inf_pr|inf_du|err|compl|iter)"
1600 ))
1601 }
1602 };
1603 CmdOut::ok(vec![format!("{what} = {val:.10e}")])
1604 .with_data(serde_json::json!({"name": what, "value": val}))
1605 }
1606 }
1607
1608 fn cmd_print_bounds(&self, ctx: &dyn DebugState, active: bool) -> CmdOut {
1614 let tol = 1e-6;
1615 let mut lines = Vec::new();
1616 let mut cats = serde_json::Map::new();
1617 for cat in ["x_l", "x_u", "s_l", "s_u"] {
1618 let Some(sl) = ctx.bound_slack(cat) else {
1619 continue;
1620 };
1621 if sl.is_empty() {
1622 continue;
1623 }
1624 let n = sl.len();
1625 if active {
1626 let min = sl.iter().copied().fold(f64::INFINITY, f64::min);
1627 let near = sl.iter().filter(|&&s| s.abs() < tol).count();
1628 lines.push(format!(
1629 "{cat}: {n} bound(s), {near} near-active (slack<{tol:.0e}), min slack {min:.3e}"
1630 ));
1631 cats.insert(
1632 cat.to_string(),
1633 serde_json::json!({"n": n, "near_active": near, "min_slack": min}),
1634 );
1635 } else {
1636 let max = sl.iter().copied().fold(f64::NEG_INFINITY, f64::max);
1637 let far = sl.iter().filter(|&&s| s.abs() >= tol).count();
1638 lines.push(format!(
1639 "{cat}: {n} bound(s), {far} inactive (slack≥{tol:.0e}), max slack {max:.3e}"
1640 ));
1641 cats.insert(
1642 cat.to_string(),
1643 serde_json::json!({"n": n, "inactive": far, "max_slack": max}),
1644 );
1645 }
1646 }
1647 if lines.is_empty() {
1648 lines.push("no bounded variables or inequality slacks".into());
1649 }
1650 CmdOut::ok(lines).with_data(serde_json::json!({"tol": tol, "categories": cats}))
1651 }
1652
1653 fn cmd_print_residuals(&self, rest: &[&str], ctx: &dyn DebugState) -> CmdOut {
1660 let mut k: Option<usize> = None;
1661 let mut filter: Option<bool> = None; for &arg in rest {
1663 if let Ok(n) = arg.parse::<usize>() {
1664 k = Some(n);
1665 } else {
1666 match arg {
1667 "primal" | "pr" => filter = Some(true),
1668 "dual" | "du" => filter = Some(false),
1669 other => {
1670 return CmdOut::err(format!(
1671 "usage: print residuals [primal|dual] [k] (got `{other}`)"
1672 ))
1673 }
1674 }
1675 }
1676 }
1677 let k = k.unwrap_or(10);
1678
1679 let mut all = Vec::new();
1680 if filter != Some(false) {
1681 let Some(primal) = ctx.constraint_residuals() else {
1682 return CmdOut::err("no iterate yet — residuals unavailable");
1683 };
1684 all.extend(primal);
1685 }
1686 if filter != Some(true) {
1687 let Some(dual) = ctx.dual_residuals() else {
1688 return CmdOut::err("no iterate yet — residuals unavailable");
1689 };
1690 all.extend(dual);
1691 }
1692
1693 let total = all.len();
1694 let top = rank_residuals(all, k);
1695 if top.is_empty() {
1696 return CmdOut::ok(vec!["no residuals at this iterate".into()])
1697 .with_data(serde_json::json!({"k": k, "total": total, "top": []}));
1698 }
1699
1700 let names = ctx
1708 .as_any()
1709 .and_then(|a| a.downcast_ref::<DebugCtx>())
1710 .and_then(|c| c.split_names());
1711 let name_of = |r: &Residual| resid_name(r, &names);
1712
1713 let lines = top
1714 .iter()
1715 .map(|r| {
1716 let label = match name_of(r) {
1717 Some(name) => format!("{}[{}]", r.kind.tag(), name),
1718 None => format!("{}[{}]", r.kind.tag(), r.index),
1719 };
1720 format!("{:>8} = {:+.6e} |{:.3e}|", label, r.value, r.value.abs())
1721 })
1722 .collect();
1723 let data: Vec<_> = top
1724 .iter()
1725 .map(|r| {
1726 serde_json::json!({
1727 "space": r.kind.tag(),
1728 "primal": r.kind.is_primal(),
1729 "index": r.index,
1730 "name": name_of(r),
1731 "value": r.value,
1732 })
1733 })
1734 .collect();
1735 CmdOut::ok(lines).with_data(serde_json::json!({"k": k, "total": total, "top": data}))
1736 }
1737
1738 fn cmd_print_equation(&self, rest: &[&str]) -> CmdOut {
1747 let Some(book) = self.equation_book.as_ref() else {
1748 return CmdOut::err(
1749 "no equation source — `print equation` needs an .nl model (none was loaded)",
1750 );
1751 };
1752 if book.is_empty() {
1753 return CmdOut::err("the model has no constraint equations to print");
1754 }
1755 let Some(&key) = rest.first() else {
1756 return CmdOut::ok(vec![format!(
1757 "{} constraint equation(s) — `print equation <name|row>` to show one",
1758 book.len()
1759 )])
1760 .with_data(serde_json::json!({"count": book.len()}));
1761 };
1762 let Some(i) = book.resolve(key) else {
1763 return CmdOut::err(format!(
1764 "no constraint named or indexed `{key}` (have {} equation(s); try a name or 0..{})",
1765 book.len(),
1766 book.len().saturating_sub(1)
1767 ));
1768 };
1769 let label = book.label(i);
1770 let Some(eq) = book.equations.get(i) else {
1773 return CmdOut::err(format!(
1774 "constraint `{key}` has no source algebra (index {i} out of range)"
1775 ));
1776 };
1777 CmdOut::ok(vec![format!("{label}: {eq}")]).with_data(serde_json::json!({
1778 "index": i,
1779 "name": book.names.get(i).filter(|n| !n.is_empty()),
1780 "equation": eq,
1781 }))
1782 }
1783
1784 fn cmd_diagnose(&self, ctx: &DebugCtx) -> CmdOut {
1799 const TOL: f64 = 1e-6;
1800 let names = ctx.split_names();
1801 let mut f: Vec<(&'static str, &'static str, String)> = Vec::new();
1803
1804 let inf_pr = ctx.inf_pr();
1806 if inf_pr > TOL {
1807 if let Some(resids) = ctx.constraint_residuals() {
1808 if let Some((label, val)) = worst_named(resids, &names) {
1809 let sev = if inf_pr > 1e-2 { "error" } else { "warning" };
1810 f.push((
1811 sev,
1812 "primal_infeasible",
1813 format!(
1814 "Primal infeasibility {inf_pr:.2e}; worst constraint residual is \
1815 {label} = {val:+.3e}. Inspect this equation's feasibility and scaling \
1816 at the current point (`print equation {label}`)."
1817 ),
1818 ));
1819 }
1820 }
1821 }
1822
1823 let inf_du = ctx.inf_du();
1825 if inf_du > TOL {
1826 if let Some(resids) = ctx.dual_residuals() {
1827 if let Some((label, val)) = worst_named(resids, &names) {
1828 f.push((
1829 "warning",
1830 "dual_infeasible",
1831 format!(
1832 "Dual infeasibility {inf_du:.2e}; largest stationarity residual is \
1833 {label} = {val:+.3e}."
1834 ),
1835 ));
1836 }
1837 }
1838 }
1839
1840 if let Some(k) = ctx.kkt() {
1842 if k.provides_inertia && !k.inertia_correct {
1843 f.push((
1844 "warning",
1845 "inertia_wrong",
1846 format!(
1847 "KKT inertia is wrong (n-={} vs expected {}): the system was \
1848 indefinite/singular and the step had to be stabilized. A persistent \
1849 mismatch points at a rank-deficient Jacobian or an indefinite Hessian.",
1850 k.n_neg, k.expected_neg
1851 ),
1852 ));
1853 }
1854 if k.delta_w > 1e-4 {
1855 f.push((
1856 "info",
1857 "heavy_regularization",
1858 format!(
1859 "Primal regularization δ_w={:.2e} applied — the Hessian was indefinite at \
1860 this step. Normal near saddle points; persistent large δ_w suggests a \
1861 problematic Hessian.",
1862 k.delta_w
1863 ),
1864 ));
1865 }
1866 if k.delta_c > 0.0 {
1867 f.push((
1868 "warning",
1869 "dual_regularization",
1870 format!(
1871 "Dual regularization δ_c={:.2e} applied — the constraint Jacobian is (near) \
1872 rank-deficient (linearly dependent or redundant equalities). Inspect the \
1873 equality residuals by name (`print residuals primal`).",
1874 k.delta_c
1875 ),
1876 ));
1877 }
1878 }
1879
1880 if let Some(book) = self.structure_book.as_ref() {
1884 f.extend(book.findings());
1885 }
1886
1887 if let Some(rep) = ctx.rank_report() {
1893 if rep.is_rank_deficient() {
1894 let culprits: Vec<String> = rep
1895 .culprits
1896 .iter()
1897 .take(MAX_RANK_CULPRITS)
1898 .map(|c| rank_row_label(&rep.rows[c.row], &names))
1899 .collect();
1900 let named = if culprits.is_empty() {
1901 String::new()
1902 } else {
1903 format!(" Implicated equations: {}.", culprits.join(", "))
1904 };
1905 f.push((
1906 "warning",
1907 "rank_deficient_jacobian",
1908 format!(
1909 "Equality Jacobian J_c is numerically rank-deficient at this iterate: \
1910 rank {}/{} (deficiency {}), σ_min={:.2e}, cond={}. Linearly dependent \
1911 or redundant equality constraints — the root cause behind δ_c \
1912 regularization / wrong inertia.{named}",
1913 rep.rank,
1914 rep.n_rows(),
1915 rep.deficiency(),
1916 rep.sigma_min(),
1917 fmt_cond(rep.cond),
1918 ),
1919 ));
1920 }
1921 }
1922
1923 let mut max_mult = 0.0_f64;
1925 for blk in ["y_c", "y_d", "z_l", "z_u", "v_l", "v_u"] {
1926 if let Some(v) = ctx.block(blk) {
1927 max_mult = v.iter().fold(max_mult, |m, &x| m.max(x.abs()));
1928 }
1929 }
1930 if max_mult > 1e8 {
1931 f.push((
1932 "warning",
1933 "large_multipliers",
1934 format!(
1935 "Largest multiplier magnitude is {max_mult:.2e}. Very large multipliers signal a \
1936 constraint-qualification failure or poor scaling — consider rescaling the \
1937 offending rows."
1938 ),
1939 ));
1940 }
1941
1942 let mut pinned = 0usize;
1944 for cat in ["x_l", "x_u"] {
1945 if let Some(sl) = ctx.bound_slack(cat) {
1946 pinned += sl.iter().filter(|&&s| s.abs() < TOL).count();
1947 }
1948 }
1949 if pinned > 0 {
1950 f.push((
1951 "info",
1952 "bounds_pinned",
1953 format!(
1954 "{pinned} variable bound(s) are active (slack < {TOL:.0e}). Active bounds are \
1955 expected at a solution, but a large count early can throttle the line search."
1956 ),
1957 ));
1958 }
1959
1960 let (alpha_pr, _) = ctx.alpha();
1962 if ctx.iter() > 0 && alpha_pr > 0.0 && alpha_pr < 1e-6 {
1963 f.push((
1964 "warning",
1965 "tiny_step",
1966 format!(
1967 "Accepted primal step α_pr={alpha_pr:.2e} is tiny — the line search is barely \
1968 moving. Often a poor search direction or an ill-conditioned KKT system."
1969 ),
1970 ));
1971 }
1972 let ls = ctx.ls_count();
1973 if ls >= 10 {
1974 f.push((
1975 "warning",
1976 "heavy_line_search",
1977 format!(
1978 "Line search needed {ls} trial points for the accepted step — search-direction \
1979 quality may be poor (check Hessian accuracy)."
1980 ),
1981 ));
1982 }
1983
1984 if self.in_restoration {
1986 f.push((
1987 "warning",
1988 "in_restoration",
1989 "Currently inside feasibility restoration: the line search could not make \
1990 progress on the original problem at the working point."
1991 .to_string(),
1992 ));
1993 }
1994 if self.mu_stall >= MU_STALL_ITERS {
1995 f.push((
1996 "warning",
1997 "mu_stalled",
1998 format!(
1999 "μ has not decreased for {} consecutive iterations — the barrier is stuck. \
2000 Try mu_strategy=adaptive or a smaller mu_init.",
2001 self.mu_stall
2002 ),
2003 ));
2004 }
2005
2006 if f.is_empty() {
2008 f.push((
2009 "info",
2010 "healthy",
2011 format!(
2012 "No issues detected at iter {}: inf_pr={:.2e}, inf_du={:.2e}, μ={:.2e}.",
2013 ctx.iter(),
2014 inf_pr,
2015 inf_du,
2016 ctx.mu()
2017 ),
2018 ));
2019 }
2020
2021 let rank = |s: &str| match s {
2023 "error" => 0,
2024 "warning" => 1,
2025 _ => 2,
2026 };
2027 f.sort_by_key(|(sev, _, _)| rank(sev));
2028
2029 let lines: Vec<String> = f
2030 .iter()
2031 .map(|(sev, code, msg)| format!("[{sev:>7}] {code}: {msg}"))
2032 .collect();
2033 let data: Vec<_> = f
2034 .iter()
2035 .map(|(sev, code, msg)| serde_json::json!({"severity": sev, "code": code, "message": msg}))
2036 .collect();
2037 let n = data.len();
2038 CmdOut::ok(lines)
2039 .with_data(serde_json::json!({"iter": ctx.iter(), "findings": data, "n_findings": n}))
2040 }
2041
2042 fn cmd_print_kkt(&self, ctx: &dyn DebugState) -> CmdOut {
2045 let Some(k) = ctx.kkt() else {
2046 return CmdOut::err(
2047 "no KKT factorization yet — stop at `after_search_dir` (e.g. `stop-at kkt`)",
2048 );
2049 };
2050 let inertia = if k.provides_inertia {
2051 format!(
2052 "n+={} n-={} (expected n-={}) → {}",
2053 k.n_pos,
2054 k.n_neg,
2055 k.expected_neg,
2056 if k.inertia_correct {
2057 "correct"
2058 } else {
2059 "WRONG (step stabilized)"
2060 }
2061 )
2062 } else {
2063 "n/a (backend reports no inertia)".to_string()
2064 };
2065 let lines = vec![
2066 format!("dim = {}", k.dim),
2067 format!("inertia = {inertia}"),
2068 format!("delta_w = {:.6e} (primal regularization)", k.delta_w),
2069 format!("delta_c = {:.6e} (dual regularization)", k.delta_c),
2070 format!("status = {}", k.status),
2071 ];
2072 CmdOut::ok(lines).with_data(serde_json::json!({
2073 "dim": k.dim,
2074 "n_pos": k.n_pos,
2075 "n_neg": k.n_neg,
2076 "expected_neg": k.expected_neg,
2077 "provides_inertia": k.provides_inertia,
2078 "inertia_correct": k.inertia_correct,
2079 "delta_w": k.delta_w,
2080 "delta_c": k.delta_c,
2081 "status": k.status,
2082 }))
2083 }
2084
2085 fn cmd_print_rank(&self, ctx: &DebugCtx) -> CmdOut {
2094 let Some(rep) = ctx.rank_report() else {
2095 return CmdOut::err(
2096 "no equality-constraint Jacobian to analyze (the problem has no equality \
2097 constraints, or there is no iterate yet)",
2098 );
2099 };
2100 let names = ctx.split_names();
2101 let (lines, data) =
2102 render_rank_report(&rep, &names, self.equation_book.as_ref(), ctx.iter());
2103 CmdOut::ok(lines).with_data(data)
2104 }
2105
2106 fn cmd_run(&mut self, rest: &[&str]) -> CmdOut {
2107 match rest.first().and_then(|s| s.parse::<i32>().ok()) {
2108 Some(n) => {
2109 self.run_to = Some(n);
2110 self.step = false;
2111 CmdOut::ok(vec![format!("running until iteration {n}")]).flow(Flow::Resume)
2112 }
2113 None => CmdOut::err("usage: run <iteration>"),
2114 }
2115 }
2116
2117 fn cmd_break(&mut self, rest: &[&str]) -> CmdOut {
2118 if rest.first().copied() == Some("if") {
2122 let expr: String = rest[1..].concat();
2123 if expr.is_empty() {
2124 return CmdOut::err(
2125 "usage: break if <metric><op><value> (e.g. break if inf_pr<1e-6)",
2126 );
2127 }
2128 return match Condition::parse(&expr) {
2129 Ok(c) => {
2130 let raw = c.raw.clone();
2131 if !self.conds.iter().any(|e| e.raw == raw) {
2132 self.conds.push(c);
2133 }
2134 CmdOut::ok(vec![format!("conditional breakpoint: {raw}")])
2135 .with_data(serde_json::json!({"condition": raw}))
2136 }
2137 Err(e) => CmdOut::err(e),
2138 };
2139 }
2140 if rest.first().copied() == Some("on") {
2142 let Some(&name) = rest.get(1) else {
2143 return CmdOut::err(format!("usage: break on <event> (one of {EVENTS:?})"));
2144 };
2145 let Some(&canon) = EVENTS.iter().find(|&&e| e == name) else {
2146 return CmdOut::err(format!("unknown event `{name}` (one of {EVENTS:?})"));
2147 };
2148 self.break_events.insert(canon);
2149 return CmdOut::ok(vec![format!("break on event `{canon}`")])
2150 .with_data(serde_json::json!({"event": canon}));
2151 }
2152 match rest {
2153 [] => {
2154 let mut bs = self.breaks.clone();
2155 bs.sort_unstable();
2156 let conds: Vec<String> = self.conds.iter().map(|c| c.raw.clone()).collect();
2157 let mut events: Vec<&str> = self.break_events.iter().copied().collect();
2158 events.sort_unstable();
2159 let mut lines = vec![format!("breakpoints: {bs:?}")];
2160 if !conds.is_empty() {
2161 lines.push(format!("conditions: {}", conds.join(", ")));
2162 }
2163 if !events.is_empty() {
2164 lines.push(format!("events: {}", events.join(", ")));
2165 }
2166 CmdOut::ok(lines).with_data(
2167 serde_json::json!({"breakpoints": bs, "conditions": conds, "events": events}),
2168 )
2169 }
2170 ["clear", "cond"] | ["clear", "conditions"] => {
2171 self.conds.clear();
2172 CmdOut::ok(vec!["cleared conditional breakpoints".into()])
2173 }
2174 ["clear", "events"] => {
2175 self.break_events.clear();
2176 CmdOut::ok(vec!["cleared event breakpoints".into()])
2177 }
2178 ["clear"] => {
2179 self.breaks.clear();
2180 self.conds.clear();
2181 self.break_events.clear();
2182 CmdOut::ok(vec!["cleared all breakpoints".into()])
2183 }
2184 ["del", n] | ["delete", n] => match n.parse::<i32>() {
2185 Ok(n) => {
2186 self.breaks.retain(|&b| b != n);
2187 CmdOut::ok(vec![format!("removed breakpoint {n}")])
2188 }
2189 Err(_) => CmdOut::err("usage: break del <iteration>"),
2190 },
2191 [n] => match n.parse::<i32>() {
2192 Ok(n) => {
2193 if !self.breaks.contains(&n) {
2194 self.breaks.push(n);
2195 }
2196 CmdOut::ok(vec![format!("breakpoint at iteration {n}")])
2197 }
2198 Err(_) => CmdOut::err("usage: break <iteration>"),
2199 },
2200 _ => CmdOut::err("usage: break [N | if <m><op><v> | clear | clear cond | del N]"),
2201 }
2202 }
2203
2204 fn cmd_stop_at(&mut self, rest: &[&str]) -> CmdOut {
2208 let canon = |s: &str| -> Option<&'static str> {
2209 match s {
2210 "mu" | "after_mu" => Some("after_mu"),
2211 "kkt" | "search_dir" | "after_search_dir" => Some("after_search_dir"),
2212 "step" | "after_step" => Some("after_step"),
2213 "rejected" | "ls_rejected" | "step_rejected" => Some("step_rejected"),
2214 "resto" | "restoration" | "pre_restoration_entry" => Some("pre_restoration_entry"),
2215 "resto_exit" | "post_restoration_exit" => Some("post_restoration_exit"),
2216 "iter" | "iter_start" => Some("iter_start"),
2217 "terminated" => Some("terminated"),
2218 _ => None,
2219 }
2220 };
2221 match rest {
2222 [] => {
2223 let mut v: Vec<&str> = self.stop_at.iter().copied().collect();
2224 v.sort_unstable();
2225 CmdOut::ok(vec![format!(
2226 "stop-at: {v:?} (available: {CHECKPOINTS:?})"
2227 )])
2228 .with_data(serde_json::json!({"stop_at": v, "available": CHECKPOINTS}))
2229 }
2230 ["clear"] => {
2231 self.stop_at.clear();
2232 CmdOut::ok(vec!["cleared stop-at checkpoints".into()])
2233 }
2234 [name] => match canon(name) {
2235 Some(c) => {
2236 self.stop_at.insert(c);
2237 CmdOut::ok(vec![format!("will stop at checkpoint `{c}`")])
2238 .with_data(serde_json::json!({"stop_at_added": c}))
2239 }
2240 None => CmdOut::err(format!(
2241 "unknown checkpoint `{name}` (one of {CHECKPOINTS:?})"
2242 )),
2243 },
2244 _ => CmdOut::err("usage: stop-at [<checkpoint> | clear]"),
2245 }
2246 }
2247
2248 fn cmd_set(&mut self, rest: &[&str], ctx: &mut dyn DebugState) -> CmdOut {
2249 match rest {
2250 ["mu", v] => match v.parse::<f64>() {
2251 Ok(mu) => match ctx.set_mu(mu) {
2252 Ok(()) => CmdOut::ok(vec![format!("mu := {mu:.6e}")]),
2253 Err(e) => CmdOut::err(e),
2254 },
2255 Err(_) => CmdOut::err("usage: set mu <value>"),
2256 },
2257 ["opt", name, value] => match as_nlp_mut(ctx) {
2258 Some(c) => self.cmd_set_opt(name, value, c),
2259 None => nlp_only("set opt"),
2260 },
2261 [target, value] => self.cmd_set_block(target, value, ctx),
2262 _ => CmdOut::err(
2263 "usage: set mu <v> | set <blk>[<i>] <v> | set <blk> <v0,v1,..> | set opt <name> <v>",
2264 ),
2265 }
2266 }
2267
2268 fn cmd_set_block(&mut self, target: &str, value: &str, ctx: &mut dyn DebugState) -> CmdOut {
2270 if let Some(open) = target.find('[') {
2272 if !target.ends_with(']') {
2273 return CmdOut::err("malformed component target (expected name[idx])");
2274 }
2275 let name = &target[..open];
2276 let idx_str = &target[open + 1..target.len() - 1];
2277 let Ok(idx) = idx_str.parse::<usize>() else {
2278 return CmdOut::err(format!("bad index `{idx_str}`"));
2279 };
2280 let Ok(val) = value.parse::<f64>() else {
2281 return CmdOut::err(format!("bad value `{value}`"));
2282 };
2283 return match ctx.set_component(name, idx, val) {
2284 Ok(()) => CmdOut::ok(vec![format!("{name}[{idx}] := {val:.6e}")]),
2285 Err(e) => CmdOut::err(e),
2286 };
2287 }
2288 let parsed: Result<Vec<f64>, _> =
2290 value.split(',').map(|s| s.trim().parse::<f64>()).collect();
2291 match parsed {
2292 Ok(vals) => match ctx.set_block(target, &vals) {
2293 Ok(()) => CmdOut::ok(vec![format!("{target} := {} value(s)", vals.len())]),
2294 Err(e) => CmdOut::err(e),
2295 },
2296 Err(_) => CmdOut::err("could not parse comma-separated values"),
2297 }
2298 }
2299
2300 fn cmd_set_opt(&mut self, name: &str, value: &str, ctx: &mut DebugCtx) -> CmdOut {
2301 let Some(reg) = self.reg.as_ref() else {
2302 return CmdOut::err("no options registry available");
2303 };
2304 let Some(opt) = reg.get_option(name) else {
2305 return CmdOut::err(format!("unknown option `{name}` (try `opt {name}`)"));
2306 };
2307 let valid = match opt.option_type {
2309 OptionType::OT_Number => value
2310 .parse::<f64>()
2311 .map(|v| opt.is_valid_number(v))
2312 .unwrap_or(false),
2313 OptionType::OT_Integer => value
2314 .parse::<i32>()
2315 .map(|v| opt.is_valid_integer(v))
2316 .unwrap_or(false),
2317 OptionType::OT_String => opt.is_valid_string(value),
2318 OptionType::OT_Unknown => true,
2319 };
2320 if !valid {
2321 return CmdOut::err(format!("`{value}` is not a valid value for `{name}`"));
2322 }
2323 self.staged.retain(|(k, _)| k != name);
2326 self.staged.push((name.to_string(), value.to_string()));
2327 if is_live_tolerance(name) {
2332 if let Ok(v) = value.parse::<f64>() {
2333 ctx.set_live_tolerance(name, v);
2334 return CmdOut::ok(vec![format!(
2335 "{name} = {value} (applied live — the next `step` uses it)"
2336 )])
2337 .with_data(serde_json::json!({
2338 "option": name, "value": value, "live": true
2339 }));
2340 }
2341 }
2342 CmdOut::ok(vec![format!(
2343 "staged {name} = {value} (validated; takes effect on `resolve` — built strategies don't re-read mid-solve)"
2344 )])
2345 .with_data(serde_json::json!({"option": name, "value": value, "staged": true}))
2346 }
2347
2348 fn cmd_get(&self, rest: &[&str]) -> CmdOut {
2355 let name = match rest {
2357 ["opt", n] => *n,
2358 [n] => *n,
2359 _ => return CmdOut::err("usage: get opt <name>"),
2360 };
2361 let Some(reg) = self.reg.as_ref() else {
2362 return CmdOut::err("no options registry available");
2363 };
2364 let Some(o) = reg.get_option(name) else {
2365 return CmdOut::err(format!("unknown option `{name}` (try `opt {name}`)"));
2366 };
2367 let def = default_str(&o.default);
2368 let staged = self
2369 .staged
2370 .iter()
2371 .find(|(k, _)| k == name)
2372 .map(|(_, v)| v.clone());
2373 let (value, source) = match &staged {
2374 Some(v) => (v.clone(), "staged"),
2375 None => (def.clone(), "default"),
2376 };
2377 CmdOut::ok(vec![format!("{name} = {value} ({source}; default={def})")]).with_data(
2378 serde_json::json!({
2379 "option": name, "value": value, "source": source,
2380 "default": def, "staged": staged,
2381 }),
2382 )
2383 }
2384
2385 fn cmd_opt(&self, rest: &[&str]) -> CmdOut {
2386 let Some(reg) = self.reg.as_ref() else {
2387 return CmdOut::err("no options registry available");
2388 };
2389 let filter = rest.first().copied().unwrap_or("");
2390 let mut lines = Vec::new();
2391 let mut data = Vec::new();
2392 for o in reg.registered_options_in_order() {
2393 if !filter.is_empty()
2394 && !o.name.contains(filter)
2395 && !o
2396 .category
2397 .to_ascii_lowercase()
2398 .contains(&filter.to_ascii_lowercase())
2399 {
2400 continue;
2401 }
2402 let ty = type_str(o.option_type);
2403 let def = default_str(&o.default);
2404 lines.push(format!(
2405 " {:<28} {:<7} default={:<12} {}",
2406 o.name, ty, def, o.short_description
2407 ));
2408 data.push(serde_json::json!({
2409 "name": o.name,
2410 "type": ty,
2411 "default": def,
2412 "category": o.category,
2413 "short": o.short_description,
2414 "valid": o.valid_strings.iter().map(|e| e.value.clone()).collect::<Vec<_>>(),
2415 }));
2416 }
2417 if lines.is_empty() {
2418 return CmdOut::ok(vec![format!("no options match `{filter}`")]);
2419 }
2420 if data.len() == 1 {
2422 if let Some(o) = reg.get_option(filter) {
2423 if !o.long_description.is_empty() {
2424 lines.push(String::new());
2425 lines.push(o.long_description.clone());
2426 }
2427 }
2428 }
2429 CmdOut::ok(lines).with_data(serde_json::json!({"options": data}))
2430 }
2431
2432 fn cmd_complete(&self, rest: &[&str]) -> CmdOut {
2437 let (before, word) = match rest.split_last() {
2438 Some((w, pre)) => (pre.join(" "), *w),
2439 None => (String::new(), ""),
2440 };
2441 let mut cands = completion_candidates(self.reg.as_deref(), &before, word);
2442 cands.sort();
2443 cands.dedup();
2444 CmdOut::ok(vec![cands.join(" ")]).with_data(serde_json::json!({"candidates": cands}))
2445 }
2446
2447 fn cmd_save(&self, rest: &[&str], ctx: &dyn DebugState) -> CmdOut {
2451 let iter = ctx.iter();
2452 let path = rest
2453 .first()
2454 .map(PathBuf::from)
2455 .unwrap_or_else(|| std::env::temp_dir().join(format!("pounce-dbg-iter{iter}.json")));
2456 let collect = |delta: bool| -> serde_json::Map<String, serde_json::Value> {
2457 let mut m = serde_json::Map::new();
2458 for b in block_names(ctx) {
2459 let v = if delta {
2460 ctx.delta_block(b)
2461 } else {
2462 ctx.block(b)
2463 };
2464 if let Some(v) = v {
2465 if !v.is_empty() {
2466 let key = if delta {
2467 format!("d{b}")
2468 } else {
2469 b.to_string()
2470 };
2471 m.insert(key, serde_json::json!(v));
2472 }
2473 }
2474 }
2475 m
2476 };
2477 let payload = serde_json::json!({
2478 "iter": iter,
2479 "mu": ctx.mu(),
2480 "objective": ctx.objective(),
2481 "inf_pr": ctx.inf_pr(),
2482 "inf_du": ctx.inf_du(),
2483 "nlp_error": ctx.nlp_error(),
2484 "iterate": collect(false),
2485 "delta": collect(true),
2486 });
2487 match std::fs::write(&path, format!("{payload}\n")) {
2488 Ok(()) => {
2489 let p = path.to_string_lossy().to_string();
2490 CmdOut::ok(vec![format!("saved iterate to {p}")])
2491 .with_data(serde_json::json!({"path": p}))
2492 }
2493 Err(e) => CmdOut::err(format!("save failed: {e}")),
2494 }
2495 }
2496
2497 fn cmd_load(&mut self, rest: &[&str], ctx: &mut DebugCtx) -> CmdOut {
2504 let Some(&path) = rest.first() else {
2505 return CmdOut::err("usage: load <file> [block] (inverse of `save`)");
2506 };
2507 let content = match std::fs::read_to_string(path) {
2508 Ok(c) => c,
2509 Err(e) => return CmdOut::err(format!("cannot read `{path}`: {e}")),
2510 };
2511 if let Ok(v) = serde_json::from_str::<serde_json::Value>(content.trim()) {
2515 let obj = v
2516 .get("iterate")
2517 .and_then(|o| o.as_object())
2518 .or_else(|| v.as_object());
2519 if let Some(obj) = obj {
2520 let mut loaded: Vec<(String, usize)> = Vec::new();
2521 let mut errs: Vec<String> = Vec::new();
2522 for &b in BLOCK_NAMES.iter() {
2523 let Some(arr) = obj.get(b).and_then(|a| a.as_array()) else {
2524 continue;
2525 };
2526 let vals: Option<Vec<f64>> = arr.iter().map(|x| x.as_f64()).collect();
2527 let Some(vals) = vals else {
2528 errs.push(format!("{b}: non-numeric entries"));
2529 continue;
2530 };
2531 match ctx.set_block(b, &vals) {
2532 Ok(()) => loaded.push((b.to_string(), vals.len())),
2533 Err(e) => errs.push(format!("{b}: {e}")),
2534 }
2535 }
2536 if loaded.is_empty() && errs.is_empty() {
2537 return CmdOut::err(
2538 "no recognizable blocks in JSON (expected `x`, `s`, … at top level or under `iterate`)",
2539 );
2540 }
2541 let mut lines: Vec<String> = loaded
2542 .iter()
2543 .map(|(b, n)| format!("loaded {b} ({n} values)"))
2544 .collect();
2545 lines.extend(errs.iter().map(|e| format!("skipped {e}")));
2546 return CmdOut::ok(lines).with_data(serde_json::json!({
2547 "loaded": loaded.iter().map(|(b, n)| serde_json::json!({"block": b, "n": n})).collect::<Vec<_>>(),
2548 "skipped": errs,
2549 }));
2550 }
2551 }
2552 let block = rest.get(1).copied().unwrap_or("x");
2554 let vals = match parse_floats(&content) {
2555 Ok(v) if !v.is_empty() => v,
2556 Ok(_) => return CmdOut::err("file held no numbers"),
2557 Err(e) => return CmdOut::err(e),
2558 };
2559 match ctx.set_block(block, &vals) {
2560 Ok(()) => CmdOut::ok(vec![format!("loaded {block} ({} values)", vals.len())])
2561 .with_data(serde_json::json!({"block": block, "n": vals.len()})),
2562 Err(e) => CmdOut::err(e),
2563 }
2564 }
2565
2566 fn cmd_sweep(&mut self, rest: &[&str], ctx: &mut DebugCtx) -> CmdOut {
2572 if self.restart.is_none() {
2573 return CmdOut::err("sweep needs re-solve, which is not available in this context");
2574 }
2575 let Some(&path) = rest.first() else {
2576 return CmdOut::err("usage: sweep <file> (one start per line, comma-separated)");
2577 };
2578 let content = match std::fs::read_to_string(path) {
2579 Ok(c) => c,
2580 Err(e) => return CmdOut::err(format!("cannot read `{path}`: {e}")),
2581 };
2582 let dim = ctx.block("x").map(|x| x.len()).unwrap_or(0);
2583 let mut seeds: Vec<Vec<f64>> = Vec::new();
2584 for (lineno, raw) in content.lines().enumerate() {
2585 let line = raw.trim();
2586 if line.is_empty() || line.starts_with('#') || line.starts_with("//") {
2587 continue;
2588 }
2589 match parse_floats(line) {
2590 Ok(v) if v.len() == dim => seeds.push(v),
2591 Ok(v) => {
2592 return CmdOut::err(format!(
2593 "line {}: got {} values, expected {dim} (= dim x)",
2594 lineno + 1,
2595 v.len()
2596 ));
2597 }
2598 Err(e) => return CmdOut::err(format!("line {}: {e}", lineno + 1)),
2599 }
2600 }
2601 self.start_sweep(seeds, &format!("sweep `{path}`"))
2602 }
2603
2604 fn cmd_multistart(&mut self, rest: &[&str], ctx: &mut DebugCtx) -> CmdOut {
2612 if self.restart.is_none() {
2613 return CmdOut::err(
2614 "multistart needs re-solve, which is not available in this context",
2615 );
2616 }
2617 let Some(n) = rest.first().and_then(|s| s.parse::<usize>().ok()) else {
2618 return CmdOut::err("usage: multistart <N> [rel] (N sampled restarts)");
2619 };
2620 if n == 0 {
2621 return CmdOut::err("N must be ≥ 1");
2622 }
2623 let rel = rest
2624 .get(1)
2625 .and_then(|s| s.parse::<f64>().ok())
2626 .unwrap_or(0.1);
2627 let Some(base) = ctx.block("x") else {
2628 return CmdOut::err("no current iterate to sample from");
2629 };
2630 let bounds = ctx
2632 .var_bounds()
2633 .filter(|(lo, hi)| lo.len() == base.len() && hi.len() == base.len());
2634 let n_box = bounds
2635 .as_ref()
2636 .map(|(lo, hi)| {
2637 lo.iter()
2638 .zip(hi)
2639 .filter(|(l, u)| l.is_finite() && u.is_finite() && u > l)
2640 .count()
2641 })
2642 .unwrap_or(0);
2643 let seeds: Vec<Vec<f64>> = (0..n)
2644 .map(|k| {
2645 let b = bounds
2646 .as_ref()
2647 .map(|(lo, hi)| (lo.as_slice(), hi.as_slice()));
2648 sample_start(&base, b, rel, k)
2649 })
2650 .collect();
2651 let n_var = base.len();
2652 let label = if n_box == n_var {
2653 format!("multistart {n} (box-sampled, {n_box}/{n_var} vars bounded)")
2654 } else if n_box > 0 {
2655 format!(
2656 "multistart {n} (box {n_box}/{n_var} vars; {} unbounded → jitter rel={rel})",
2657 n_var - n_box
2658 )
2659 } else {
2660 format!("multistart {n} (no finite boxes → jitter rel={rel})")
2661 };
2662 self.start_sweep(seeds, &label)
2663 }
2664
2665 fn start_sweep(&mut self, seeds: Vec<Vec<f64>>, label: &str) -> CmdOut {
2670 if seeds.is_empty() {
2671 return CmdOut::err("no start points");
2672 }
2673 let Some(cell) = self.restart.as_ref() else {
2674 return CmdOut::err("sweep needs re-solve, which is not available in this context");
2675 };
2676 let total = seeds.len();
2677 let mut queue: VecDeque<Vec<f64>> = seeds.into();
2678 let first = queue.pop_front().expect("non-empty");
2679 *cell.borrow_mut() = Some(RestartRequest {
2680 seed_x: first.clone(),
2681 options: self.staged.clone(),
2682 warm: None,
2683 });
2684 let saved_pause_iters = self.pause_iters;
2687 self.pause_iters = false;
2688 self.step = false;
2689 self.sub_step = false;
2690 self.run_to = None;
2691 self.sweep = Some(SweepState {
2692 queue,
2693 current: Some(first),
2694 records: Vec::new(),
2695 total,
2696 saved_pause_iters,
2697 });
2698 CmdOut::ok(vec![format!("{label}: running {total} start(s)…")])
2699 .with_data(serde_json::json!({"sweep": label, "starts": total}))
2700 .flow(Flow::Stop)
2701 }
2702
2703 fn drive_sweep(&mut self, ctx: &DebugCtx) -> Option<DebugAction> {
2710 let mut sweep = self.sweep.take()?;
2711 let rec = SweepRecord {
2712 idx: sweep.records.len(),
2713 seed: sweep.current.clone().unwrap_or_default(),
2714 status: ctx.status().unwrap_or("?").to_string(),
2715 objective: ctx.objective(),
2716 inf_pr: ctx.inf_pr(),
2717 iters: ctx.iter(),
2718 };
2719 self.emit_sweep_progress(&rec, sweep.total);
2720 sweep.records.push(rec);
2721 if let Some(next) = sweep.queue.pop_front() {
2722 sweep.current = Some(next.clone());
2723 if let Some(cell) = self.restart.as_ref() {
2724 *cell.borrow_mut() = Some(RestartRequest {
2725 seed_x: next,
2726 options: self.staged.clone(),
2727 warm: None,
2728 });
2729 }
2730 self.sweep = Some(sweep);
2731 return Some(DebugAction::Resume);
2732 }
2733 self.pause_iters = sweep.saved_pause_iters;
2735 self.emit_sweep_summary(&sweep);
2736 None
2737 }
2738
2739 fn emit_sweep_progress(&self, rec: &SweepRecord, total: usize) {
2742 match self.mode {
2743 DebugMode::Repl => eprintln!(
2744 " sweep {}/{}: {:<22} iters={:<4} obj={:.6e} inf_pr={:.2e}",
2745 rec.idx + 1,
2746 total,
2747 rec.status,
2748 rec.iters,
2749 rec.objective,
2750 rec.inf_pr,
2751 ),
2752 DebugMode::Json => emit_json(&serde_json::json!({
2753 "event": "sweep_result",
2754 "index": rec.idx,
2755 "total": total,
2756 "status": rec.status,
2757 "iters": rec.iters,
2758 "objective": rec.objective,
2759 "inf_pr": rec.inf_pr,
2760 "seed": rec.seed,
2761 })),
2762 }
2763 }
2764
2765 fn emit_sweep_summary(&self, sweep: &SweepState) {
2768 let succeeded: Vec<&SweepRecord> = sweep
2769 .records
2770 .iter()
2771 .filter(|r| is_success_status(&r.status))
2772 .collect();
2773 let mut distinct: Vec<f64> = Vec::new();
2775 for r in &succeeded {
2776 if !distinct
2777 .iter()
2778 .any(|&o| (o - r.objective).abs() <= 1e-6 * o.abs().max(1.0))
2779 {
2780 distinct.push(r.objective);
2781 }
2782 }
2783 let best = succeeded.iter().min_by(|a, b| {
2784 a.objective
2785 .partial_cmp(&b.objective)
2786 .unwrap_or(std::cmp::Ordering::Equal)
2787 });
2788 match self.mode {
2789 DebugMode::Repl => {
2790 eprintln!(
2791 "\n── sweep complete ── {} solves, {} succeeded, {} distinct minima",
2792 sweep.records.len(),
2793 succeeded.len(),
2794 distinct.len()
2795 );
2796 eprintln!(
2797 " {:>3} {:<22} {:>5} {:>14} {:>9}",
2798 "#", "status", "iters", "objective", "inf_pr"
2799 );
2800 for r in &sweep.records {
2801 eprintln!(
2802 " {:>3} {:<22} {:>5} {:>14.6e} {:>9.2e}",
2803 r.idx, r.status, r.iters, r.objective, r.inf_pr
2804 );
2805 }
2806 if let Some(b) = best {
2807 eprintln!(" best: solve #{} obj={:.8e}", b.idx, b.objective);
2808 }
2809 }
2810 DebugMode::Json => emit_json(&serde_json::json!({
2811 "event": "sweep_summary",
2812 "solves": sweep.records.len(),
2813 "succeeded": succeeded.len(),
2814 "distinct_minima": distinct.len(),
2815 "best_index": best.map(|b| b.idx),
2816 "best_objective": best.map(|b| b.objective),
2817 "records": sweep.records.iter().map(|r| serde_json::json!({
2818 "index": r.idx, "status": r.status, "iters": r.iters,
2819 "objective": r.objective, "inf_pr": r.inf_pr,
2820 })).collect::<Vec<_>>(),
2821 })),
2822 }
2823 }
2824
2825 fn cmd_goto(&mut self, rest: &[&str], ctx: &mut dyn DebugState) -> CmdOut {
2827 match rest.first().and_then(|s| s.parse::<i32>().ok()) {
2828 Some(k) => self.restore_to(k, ctx),
2829 None => CmdOut::err("usage: goto <iteration>"),
2830 }
2831 }
2832
2833 fn restore_to(&mut self, k: i32, ctx: &mut dyn DebugState) -> CmdOut {
2837 match self.snapshots.get(&k) {
2838 Some(snap) => {
2839 if !ctx.restore(snap.as_ref()) {
2840 return CmdOut::err(format!(
2841 "this solver does not support rewinding to iter {k}"
2842 ));
2843 }
2844 CmdOut::ok(vec![format!(
2845 "rewound to iter {k} (primal-dual only; strategy history not restored). \
2846 `continue`/`step` to resume."
2847 )])
2848 .with_data(serde_json::json!({"restored_iter": k}))
2849 }
2850 None => {
2851 let have: Vec<i32> = self.snapshots.keys().copied().collect();
2852 CmdOut::err(format!("no snapshot for iter {k} (captured: {have:?})"))
2853 }
2854 }
2855 }
2856
2857 fn cmd_resolve(&mut self, ctx: &DebugCtx) -> CmdOut {
2865 let Some(cell) = self.restart.as_ref() else {
2866 return CmdOut::err("re-solve is not available in this context");
2867 };
2868 let Some(seed_x) = ctx.block("x") else {
2869 return CmdOut::err("no current iterate to seed from");
2870 };
2871 let warm = ctx.snapshot();
2872 let mu = warm.as_ref().map(|s| s.mu());
2873 let options = self.staged.clone();
2874 let n_opt = options.len();
2875 let warm_msg = match mu {
2876 Some(mu) => format!(
2877 "re-solving warm from the current primal-dual iterate (μ={mu:.3e}) \
2878 with {n_opt} staged option override(s)…"
2879 ),
2880 None => format!(
2881 "re-solving from current x (primal-only) with {n_opt} staged option override(s)…"
2882 ),
2883 };
2884 *cell.borrow_mut() = Some(RestartRequest {
2885 seed_x,
2886 options,
2887 warm,
2888 });
2889 CmdOut::ok(vec![warm_msg])
2890 .with_data(serde_json::json!({
2891 "resolve": true,
2892 "options": n_opt,
2893 "warm": mu.is_some(),
2894 "mu": mu,
2895 }))
2896 .flow(Flow::Stop)
2897 }
2898
2899 fn cmd_ask(&self, rest: &[&str], ctx: &dyn DebugState) -> CmdOut {
2905 let question = if rest.is_empty() {
2906 "Explain the current state of this interior-point solve and suggest what to try next."
2907 .to_string()
2908 } else {
2909 rest.join(" ")
2910 };
2911 let prompt = build_ask_prompt(ctx, &question);
2912 match run_llm(&prompt) {
2913 Ok(reply) => {
2914 let lines: Vec<String> = reply.lines().map(|l| l.to_string()).collect();
2915 CmdOut::ok(lines).with_data(serde_json::json!({
2916 "question": question,
2917 "reply": reply,
2918 }))
2919 }
2920 Err(e) => CmdOut::err(e),
2921 }
2922 }
2923
2924 fn cmd_watch(&mut self, rest: &[&str]) -> CmdOut {
2927 match rest {
2928 [] => CmdOut::ok(vec![format!("watches: {:?}", self.watches)])
2929 .with_data(serde_json::json!({"watches": self.watches})),
2930 ["clear"] => {
2931 self.watches.clear();
2932 CmdOut::ok(vec!["cleared watches".into()])
2933 }
2934 ["del", w] | ["delete", w] => {
2935 self.watches.retain(|x| x != w);
2936 CmdOut::ok(vec![format!("unwatched {w}")])
2937 }
2938 [w] => {
2939 let w = w.to_string();
2940 if !self.watches.contains(&w) {
2941 self.watches.push(w.clone());
2942 }
2943 CmdOut::ok(vec![format!("watching {w}")])
2944 }
2945 _ => CmdOut::err("usage: watch [<target> | clear | del <target>]"),
2946 }
2947 }
2948
2949 fn cmd_watchpoint(&mut self, rest: &[&str], ctx: &dyn DebugState) -> CmdOut {
2953 match rest {
2954 [] => {
2955 let v: Vec<&str> = self.watchpoints.iter().map(|w| w.raw.as_str()).collect();
2956 CmdOut::ok(vec![format!("watchpoints: {v:?}")])
2957 .with_data(serde_json::json!({"watchpoints": v}))
2958 }
2959 ["clear"] => {
2960 self.watchpoints.clear();
2961 CmdOut::ok(vec!["cleared watchpoints".into()])
2962 }
2963 ["del", spec] | ["delete", spec] => {
2964 self.watchpoints.retain(|w| w.raw != *spec);
2965 CmdOut::ok(vec![format!("removed watchpoint {spec}")])
2966 }
2967 [spec, rest @ ..] => {
2968 let threshold = rest
2969 .first()
2970 .and_then(|s| s.parse::<f64>().ok())
2971 .unwrap_or(0.0);
2972 let (block, idx) = match spec.find('[') {
2974 Some(open) if spec.ends_with(']') => {
2975 let b = &spec[..open];
2976 match spec[open + 1..spec.len() - 1].parse::<usize>() {
2977 Ok(i) => (b.to_string(), Some(i)),
2978 Err(_) => return CmdOut::err(format!("bad index in `{spec}`")),
2979 }
2980 }
2981 _ => (spec.to_string(), None),
2982 };
2983 if !is_block(ctx, block.as_str()) {
2984 return CmdOut::err(format!("unknown block `{block}`"));
2985 }
2986 let raw = spec.to_string();
2987 if !self.watchpoints.iter().any(|w| w.raw == raw) {
2988 self.watchpoints.push(WatchPoint {
2989 raw: raw.clone(),
2990 block,
2991 idx,
2992 threshold,
2993 last: None,
2994 });
2995 }
2996 CmdOut::ok(vec![format!("watchpoint on {raw} (Δ>{threshold:.3e})")])
2997 }
2998 }
2999 }
3000
3001 fn cmd_commands(&mut self, rest: &[&str]) -> CmdOut {
3006 let Some(iter) = rest.first().and_then(|s| s.parse::<i32>().ok()) else {
3007 if rest.is_empty() {
3008 let mut items: Vec<(i32, Vec<String>)> = self
3009 .bp_commands
3010 .iter()
3011 .map(|(k, v)| (*k, v.clone()))
3012 .collect();
3013 items.sort_by_key(|(k, _)| *k);
3014 let lines = if items.is_empty() {
3015 vec!["no breakpoint command lists".into()]
3016 } else {
3017 items
3018 .iter()
3019 .map(|(k, v)| format!("iter {k}: {}", v.join(" ; ")))
3020 .collect()
3021 };
3022 return CmdOut::ok(lines);
3023 }
3024 return CmdOut::err(
3025 "usage: commands <iter> <cmd> ; <cmd> … (or: commands <iter> clear)",
3026 );
3027 };
3028 let tail = rest[1..].join(" ");
3029 let tail = tail.trim();
3030 if tail.is_empty() || tail == "clear" {
3031 self.bp_commands.remove(&iter);
3032 return CmdOut::ok(vec![format!("cleared commands for iteration {iter}")]);
3033 }
3034 let cmds: Vec<String> = tail
3035 .split(';')
3036 .map(|s| s.trim().to_string())
3037 .filter(|s| !s.is_empty())
3038 .collect();
3039 self.bp_commands.insert(iter, cmds.clone());
3040 CmdOut::ok(vec![format!(
3041 "commands for iter {iter}: {}",
3042 cmds.join(" ; ")
3043 )])
3044 .with_data(serde_json::json!({"iter": iter, "commands": cmds}))
3045 }
3046
3047 fn cmd_diff(&self, ctx: &dyn DebugState) -> CmdOut {
3050 let iter = ctx.iter();
3051 let Some((&piter, prev)) = self.snapshots.range(..iter).next_back() else {
3052 return CmdOut::err("no previous iterate to diff against");
3053 };
3054 let mut lines = vec![format!("Δ since iter {piter}:")];
3055 let dmu = ctx.mu() - prev.mu();
3056 lines.push(format!(" mu = {:.6e} (Δ {:+.3e})", ctx.mu(), dmu));
3057 let mut blocks = serde_json::Map::new();
3058 for b in block_names(ctx) {
3059 let (Some(cur), Some(old)) = (ctx.block(b), prev.block(b)) else {
3060 continue;
3061 };
3062 if cur.is_empty() || cur.len() != old.len() {
3063 continue;
3064 }
3065 let mut amax = 0.0_f64;
3066 let mut imax = 0usize;
3067 for (i, (c, o)) in cur.iter().zip(&old).enumerate() {
3068 let d = (c - o).abs();
3069 if d > amax {
3070 amax = d;
3071 imax = i;
3072 }
3073 }
3074 if amax > 0.0 {
3075 lines.push(format!(
3076 " {b}: max|Δ|={amax:.3e} at [{imax}] ({:.4e} → {:.4e})",
3077 old[imax], cur[imax]
3078 ));
3079 blocks.insert(
3080 b.to_string(),
3081 serde_json::json!({"max_abs_change": amax, "argmax": imax}),
3082 );
3083 }
3084 }
3085 if lines.len() == 2 {
3086 lines.push(" (no change)".into());
3087 }
3088 CmdOut::ok(lines).with_data(
3089 serde_json::json!({"from_iter": piter, "to_iter": iter, "dmu": dmu, "blocks": blocks}),
3090 )
3091 }
3092
3093 fn cmd_source(&mut self, rest: &[&str], ctx: &mut dyn DebugState) -> CmdOut {
3097 let Some(&path) = rest.first() else {
3098 return CmdOut::err("usage: source <file>");
3099 };
3100 let content = match std::fs::read_to_string(path) {
3101 Ok(c) => c,
3102 Err(e) => return CmdOut::err(format!("cannot read `{path}`: {e}")),
3103 };
3104 let mut lines = Vec::new();
3105 let mut flow = Flow::Stay;
3106 for raw in content.lines() {
3107 let cmd = raw.trim();
3108 if cmd.is_empty() || cmd.starts_with('#') || cmd.starts_with("//") {
3109 continue;
3110 }
3111 lines.push(format!("[source] {cmd}"));
3112 let out = self.dispatch(cmd, ctx);
3113 lines.extend(out.lines);
3114 if !matches!(out.flow, Flow::Stay) {
3115 flow = out.flow;
3116 break;
3117 }
3118 }
3119 CmdOut {
3120 ok: true,
3121 lines,
3122 data: None,
3123 flow,
3124 }
3125 }
3126
3127 fn cmd_viz(&self, rest: &[&str], ctx: &mut dyn DebugState) -> CmdOut {
3128 let Some(&target) = rest.first() else {
3129 return CmdOut::err("usage: viz <x|s|y_c|...|dx|kkt|L>");
3130 };
3131 if target == "kkt" {
3134 let Some(k) = ctx.kkt() else {
3135 return CmdOut::err(
3136 "no KKT factorization captured yet — nothing has been factored (iter 0), \
3137 or the debugger is detached. `step` once to capture.",
3138 );
3139 };
3140 let Some((dim, irn, jcn, vals)) = ctx.kkt_matrix() else {
3145 return CmdOut::err(
3146 "KKT matrix not captured here — the debugger is detached \
3147 (running free). `step` once to capture and re-run `viz kkt`.",
3148 );
3149 };
3150 let kiter = k.iter;
3153 let matrix = serde_json::json!({"dim": dim, "irn": irn, "jcn": jcn, "vals": vals,
3154 "format": "triplet_1based_lower"});
3155 let payload = serde_json::json!({
3156 "label": "kkt", "iter": kiter,
3157 "dim": k.dim, "n_pos": k.n_pos, "n_neg": k.n_neg,
3158 "expected_neg": k.expected_neg, "inertia_correct": k.inertia_correct,
3159 "delta_w": k.delta_w, "delta_c": k.delta_c, "status": k.status,
3160 "matrix": matrix,
3161 });
3162 return match write_json_and_open("kkt", kiter, &payload) {
3163 Ok((path, viewer)) => CmdOut::ok(vec![format!(
3164 "wrote {path} (KKT system, iter {kiter}); opened with `{viewer}`"
3165 )])
3166 .with_data(serde_json::json!({"path": path, "viewer": viewer})),
3167 Err(e) => CmdOut::err(e),
3168 };
3169 }
3170 if target == "L" {
3175 match ctx.kkt_l_factor() {
3176 Some((n, perm, l_irn, l_jcn, l_vals)) => {
3177 let kiter = ctx.kkt_captured_iter().unwrap_or_else(|| ctx.iter());
3179 let payload = serde_json::json!({
3180 "label": "L", "iter": kiter, "n": n, "perm": perm,
3181 "l_irn": l_irn, "l_jcn": l_jcn, "l_vals": l_vals,
3182 "format": "strict_lower_1based_permuted",
3183 });
3184 return match write_json_and_open("L", kiter, &payload) {
3185 Ok((path, viewer)) => CmdOut::ok(vec![format!(
3186 "wrote {path} (L factor, iter {kiter}); opened with `{viewer}`"
3187 )])
3188 .with_data(serde_json::json!({"path": path, "viewer": viewer})),
3189 Err(e) => CmdOut::err(e),
3190 };
3191 }
3192 None => {
3193 return CmdOut::err(
3194 "L factor not captured here — nothing factored yet (iter 0), \
3195 or the debugger is detached. `step` once to capture.",
3196 );
3197 }
3198 }
3199 }
3200 let (label, vals) = if is_block(ctx, target) {
3202 match ctx.block(target) {
3203 Some(v) => (target.to_string(), v),
3204 None => return CmdOut::err(format!("no data for block `{target}`")),
3205 }
3206 } else if let Some(blk) = target.strip_prefix("d").filter(|b| is_block(ctx, b)) {
3207 match ctx.delta_block(blk) {
3208 Some(v) => (format!("d{blk}"), v),
3209 None => return CmdOut::err(format!("no search direction for `d{blk}`")),
3210 }
3211 } else {
3212 return CmdOut::err(format!("don't know how to visualize `{target}`"));
3213 };
3214 match write_and_open(&label, ctx.iter(), &vals) {
3215 Ok((path, viewer)) => CmdOut::ok(vec![format!(
3216 "wrote {} ({} values); opened with `{}`",
3217 path,
3218 vals.len(),
3219 viewer
3220 )])
3221 .with_data(serde_json::json!({"path": path, "viewer": viewer, "n": vals.len()})),
3222 Err(e) => CmdOut::err(e),
3223 }
3224 }
3225
3226 fn emit_pause(&self, ctx: &dyn DebugState, reason: Option<&str>) {
3230 let terminal = matches!(ctx.checkpoint(), Checkpoint::Terminated);
3231 match self.mode {
3232 DebugMode::Repl => {
3233 if terminal {
3234 eprintln!(
3235 "\n── pounce-dbg ── TERMINATED ({}) iter {} obj={:.6e} inf_pr={:.2e} inf_du={:.2e}",
3236 ctx.status().unwrap_or("?"),
3237 ctx.iter(),
3238 ctx.objective(),
3239 ctx.inf_pr(),
3240 ctx.inf_du(),
3241 );
3242 } else {
3243 let resto = if self.in_restoration {
3244 " [restoration]"
3245 } else {
3246 ""
3247 };
3248 eprintln!(
3249 "\n── pounce-dbg ── iter {} @{}{} mu={:.3e} obj={:.6e} inf_pr={:.2e} inf_du={:.2e}",
3250 ctx.iter(),
3251 ctx.checkpoint().as_str(),
3252 resto,
3253 ctx.mu(),
3254 ctx.objective(),
3255 ctx.inf_pr(),
3256 ctx.inf_du(),
3257 );
3258 }
3259 if let Some(r) = reason {
3260 eprintln!(" ↳ {r}");
3261 }
3262 for w in &self.watches {
3263 let out = self.cmd_print(&[w.as_str()], ctx);
3264 if out.ok {
3265 for l in &out.lines {
3266 eprintln!(" watch {l}");
3267 }
3268 } else {
3269 eprintln!(" watch {w}: (n/a)");
3273 }
3274 }
3275 }
3276 DebugMode::Json => {
3277 let watches: Vec<serde_json::Value> = self
3278 .watches
3279 .iter()
3280 .map(|w| {
3281 let out = self.cmd_print(&[w.as_str()], ctx);
3282 serde_json::json!({"expr": w, "ok": out.ok, "output": out.lines, "data": out.data})
3283 })
3284 .collect();
3285 let dims: serde_json::Map<String, serde_json::Value> = ctx
3286 .block_dims()
3287 .into_iter()
3288 .map(|(n, d)| (n.to_string(), serde_json::json!(d)))
3289 .collect();
3290 let conds: Vec<String> = self.conds.iter().map(|c| c.raw.clone()).collect();
3291 let mut ev = serde_json::json!({
3292 "event": "pause",
3293 "checkpoint": ctx.checkpoint().as_str(),
3294 "status": ctx.status(),
3295 "in_restoration": self.in_restoration,
3296 "dims": dims,
3297 "breakpoints": self.breaks,
3298 "conditions": conds,
3299 "reason": reason,
3300 "watches": watches,
3301 });
3302 insert_metric_fields(&mut ev, ctx);
3305 emit_json(&ev);
3306 }
3307 }
3308 }
3309
3310 fn emit_progress_event(&self, ctx: &dyn DebugState) {
3315 let mut ev = serde_json::json!({ "event": "progress" });
3316 insert_metric_fields(&mut ev, ctx);
3318 emit_json(&ev);
3319 }
3320
3321 fn emit_result(&self, command: &str, out: &CmdOut, req_id: Option<&serde_json::Value>) {
3324 match self.mode {
3325 DebugMode::Repl => {
3326 let stderr = std::io::stderr();
3327 let mut h = stderr.lock();
3328 for l in &out.lines {
3329 let _ = writeln!(h, "{l}");
3330 }
3331 if !out.ok {
3332 let _ = writeln!(h, "(error)");
3333 }
3334 }
3335 DebugMode::Json => {
3336 let ev = serde_json::json!({
3337 "event": "result",
3338 "request_id": req_id,
3339 "command": command,
3340 "ok": out.ok,
3341 "output": out.lines,
3342 "data": out.data,
3343 });
3344 emit_json(&ev);
3345 }
3346 }
3347 }
3348
3349 fn emit_hello(&self) {
3354 let ev = serde_json::json!({
3355 "event": "hello",
3356 "protocol": "pounce-dbg/1",
3357 "pounce_version": env!("CARGO_PKG_VERSION"),
3358 "capabilities": {
3359 "inspect": true,
3360 "mutate_iterate": true,
3361 "mutate_mu": true,
3362 "conditional_breakpoints": "compound",
3363 "request_ids": true,
3364 "viz": ["block", "delta", "kkt", "L"],
3365 "save": true,
3366 "load": true,
3367 "sweep": self.restart.is_some(),
3368 "kkt_inspect": true,
3369 "equations": self.equation_book.is_some(),
3372 "diagnose": true,
3374 "structural_diagnose": self.structure_book.is_some(),
3377 "llm_assist": true,
3378 "rewind": "primal_dual",
3379 "resolve": self.restart.is_some(),
3380 "terminal_checkpoint": true,
3381 "interruptible": self.interruptible,
3382 "progress_events": self.emit_progress,
3384 "async_pause": "checkpoint",
3385 "pause_command": true,
3388 },
3389 "checkpoints": CHECKPOINTS,
3390 "events": EVENTS,
3391 "commands": COMMANDS,
3392 "blocks": BLOCK_NAMES,
3393 "metrics": METRICS,
3394 });
3395 emit_json(&ev);
3396 }
3397
3398 fn ensure_editor(&mut self) {
3402 if !matches!(self.mode, DebugMode::Repl)
3403 || self.editor.is_some()
3404 || !std::io::stdin().is_terminal()
3405 {
3406 return;
3407 }
3408 let mut ed: Editor<DbgHelper, FileHistory> = match Editor::new() {
3409 Ok(e) => e,
3410 Err(_) => return,
3411 };
3412 ed.set_helper(Some(DbgHelper {
3413 reg: self.reg.clone(),
3414 }));
3415 let path = std::env::var_os("HOME")
3416 .or_else(|| std::env::var_os("USERPROFILE"))
3417 .map(|h| PathBuf::from(h).join(".pounce_dbg_history"));
3418 if let Some(p) = &path {
3419 let _ = ed.load_history(p);
3420 }
3421 self.hist_path = path;
3422 self.editor = Some(ed);
3423 }
3424
3425 fn on_prompt_interrupt(&mut self) -> String {
3430 self.prompt_interrupts += 1;
3431 if self.prompt_interrupts >= 2 {
3432 self.prompt_interrupts = 0;
3433 eprintln!("(quitting — Ctrl-C)");
3434 "quit".to_string()
3435 } else {
3436 eprintln!("(Ctrl-C — press again, or `quit`/Ctrl-D, to stop the solve)");
3437 String::new()
3438 }
3439 }
3440
3441 fn next_command_line(&mut self) -> Option<String> {
3445 if let Some(q) = &self.script_queue {
3449 let cmd = q.borrow_mut().pop_front();
3450 if let Some(c) = &cmd {
3451 let _ = writeln!(std::io::stderr(), "pounce-dbg> {c}");
3452 }
3453 return cmd;
3454 }
3455 if let DebugMode::Repl = self.mode {
3456 if let Some(ed) = self.editor.as_mut() {
3457 return match ed.readline("pounce-dbg> ") {
3458 Ok(l) => {
3459 self.prompt_interrupts = 0;
3460 let _ = ed.add_history_entry(l.as_str());
3461 if let Some(p) = &self.hist_path {
3462 let _ = ed.save_history(p);
3463 }
3464 Some(l)
3465 }
3466 Err(ReadlineError::Interrupted) => Some(self.on_prompt_interrupt()),
3471 Err(ReadlineError::Eof) => None,
3473 Err(_) => None,
3474 };
3475 }
3476 let _ = write!(std::io::stderr(), "pounce-dbg> ");
3477 let _ = std::io::stderr().flush();
3478 return read_stdin_line();
3479 }
3480 self.pump.get_or_insert_with(StdinPump::start).next()
3483 }
3484}
3485
3486fn read_stdin_line() -> Option<String> {
3488 let mut line = String::new();
3489 match std::io::stdin().read_line(&mut line) {
3490 Ok(0) => None,
3491 Ok(_) => Some(line),
3492 Err(_) => None,
3493 }
3494}
3495
3496fn rank_residuals(mut entries: Vec<Residual>, k: usize) -> Vec<Residual> {
3504 entries.sort_by(|a, b| {
3505 b.value
3506 .abs()
3507 .partial_cmp(&a.value.abs())
3508 .unwrap_or(std::cmp::Ordering::Equal)
3509 });
3510 entries.truncate(k);
3511 entries
3512}
3513
3514fn render_rank_report(
3525 rep: &RankReport,
3526 names: &Option<SplitNames>,
3527 equations: Option<&EquationBook>,
3528 iter: i32,
3529) -> (Vec<String>, serde_json::Value) {
3530 let m = rep.n_rows();
3531 let n = rep.n_cols;
3532 let mut lines = vec![
3533 format!("equality Jacobian J_c: {m} row(s) × {n} column(s)"),
3534 format!(
3535 "numerical rank = {} / {} (deficiency {})",
3536 rep.rank,
3537 m,
3538 rep.deficiency()
3539 ),
3540 format!(
3541 "σ_max = {:.3e} σ_min = {:.3e} cond = {} (rank tol τ = {:.3e})",
3542 rep.sigma_max(),
3543 rep.sigma_min(),
3544 fmt_cond(rep.cond),
3545 rep.tol
3546 ),
3547 ];
3548
3549 let shown: Vec<String> = rep
3551 .singular_values
3552 .iter()
3553 .take(MAX_SINGULAR_VALUES_SHOWN)
3554 .map(|s| format!("{s:.3e}"))
3555 .collect();
3556 let tail = if rep.singular_values.len() > MAX_SINGULAR_VALUES_SHOWN {
3557 " …"
3558 } else {
3559 ""
3560 };
3561 lines.push(format!("singular values: [{}{tail}]", shown.join(", ")));
3562
3563 if rep.is_rank_deficient() {
3564 lines.push(format!(
3565 "rank-deficient: {} equation(s) lie in the near-null space \
3566 (linearly dependent / redundant) — the source of δ_c regularization:",
3567 rep.deficiency()
3568 ));
3569 let mut shown_any_eq = false;
3570 for c in rep.culprits.iter().take(MAX_RANK_CULPRITS) {
3571 let row = &rep.rows[c.row];
3572 let label = rank_row_label(row, names);
3573 lines.push(format!(" {label} (participation {:.2})", c.weight));
3574 if let Some(eq) = culprit_equation(row, names, equations) {
3578 lines.push(format!(" {eq}"));
3579 shown_any_eq = true;
3580 }
3581 }
3582 if rep.culprits.len() > MAX_RANK_CULPRITS {
3583 lines.push(format!(
3584 " … and {} more",
3585 rep.culprits.len() - MAX_RANK_CULPRITS
3586 ));
3587 }
3588 if !shown_any_eq {
3591 lines.push("inspect a row with `print equation <name>` to see its terms".to_string());
3592 }
3593 } else {
3594 lines.push("J_c has full row rank at this iterate.".to_string());
3595 }
3596
3597 let culprits_json: Vec<serde_json::Value> = rep
3598 .culprits
3599 .iter()
3600 .map(|c| {
3601 let row = &rep.rows[c.row];
3602 serde_json::json!({
3603 "row": c.row,
3604 "kind": row.kind.tag(),
3605 "index": row.index,
3606 "name": rank_row_name(row, names),
3607 "label": rank_row_label(row, names),
3608 "weight": c.weight,
3609 "equation": culprit_equation(row, names, equations),
3610 })
3611 })
3612 .collect();
3613
3614 let data = serde_json::json!({
3615 "iter": iter,
3616 "n_rows": m,
3617 "n_cols": n,
3618 "rank": rep.rank,
3619 "deficiency": rep.deficiency(),
3620 "rank_deficient": rep.is_rank_deficient(),
3621 "sigma_max": rep.sigma_max(),
3622 "sigma_min": rep.sigma_min(),
3623 "cond": cond_json(rep.cond),
3624 "tol": rep.tol,
3625 "singular_values": rep.singular_values,
3626 "culprits": culprits_json,
3627 });
3628
3629 (lines, data)
3630}
3631
3632fn culprit_equation(
3639 row: &RankRow,
3640 names: &Option<SplitNames>,
3641 equations: Option<&EquationBook>,
3642) -> Option<String> {
3643 let book = equations?;
3644 let name = rank_row_name(row, names)?;
3645 let i = book.resolve(&name)?;
3646 Some(book.equations.get(i)?.clone())
3647}
3648
3649fn rank_row_name(row: &RankRow, names: &Option<SplitNames>) -> Option<String> {
3654 let r = Residual {
3655 kind: row.kind,
3656 index: row.index,
3657 value: 0.0,
3658 };
3659 resid_name(&r, names).map(|s| s.to_string())
3660}
3661
3662fn rank_row_label(row: &RankRow, names: &Option<SplitNames>) -> String {
3665 match rank_row_name(row, names) {
3666 Some(name) => format!("{}[{}]", row.kind.tag(), name),
3667 None => format!("{}[{}]", row.kind.tag(), row.index),
3668 }
3669}
3670
3671fn fmt_cond(cond: f64) -> String {
3674 if cond.is_finite() {
3675 format!("{cond:.3e}")
3676 } else {
3677 "inf (σ_min = 0)".to_string()
3678 }
3679}
3680
3681fn cond_json(cond: f64) -> serde_json::Value {
3684 if cond.is_finite() {
3685 serde_json::json!(cond)
3686 } else {
3687 serde_json::Value::Null
3688 }
3689}
3690
3691fn resid_name<'a>(r: &Residual, names: &'a Option<SplitNames>) -> Option<&'a str> {
3692 let n = names.as_ref()?;
3693 let pool = match r.kind {
3694 ResidKind::Eq => &n.eq,
3695 ResidKind::Ineq | ResidKind::DualS => &n.ineq,
3696 ResidKind::DualX => &n.x_var,
3697 };
3698 pool.get(r.index).and_then(|o| o.as_deref())
3699}
3700
3701fn worst_named(resids: Vec<Residual>, names: &Option<SplitNames>) -> Option<(String, f64)> {
3705 let top = rank_residuals(resids, 1);
3706 let r = top.first()?;
3707 let label = match resid_name(r, names) {
3708 Some(name) => format!("{}[{}]", r.kind.tag(), name),
3709 None => format!("{}[{}]", r.kind.tag(), r.index),
3710 };
3711 Some((label, r.value))
3712}
3713
3714pub fn print_open_banner(mode: DebugMode) {
3718 if !matches!(mode, DebugMode::Repl) {
3719 return;
3720 }
3721 let color = std::io::stderr().is_terminal() && std::env::var_os("NO_COLOR").is_none();
3722 let paint = |r: u8, g: u8, b: u8, bold: bool, s: &str| -> String {
3723 if color {
3724 let w = if bold { "1;" } else { "" };
3725 format!("\x1b[{w}38;2;{r};{g};{b}m{s}\x1b[0m")
3726 } else {
3727 s.to_string()
3728 }
3729 };
3730 let orange = |s: &str| paint(0xE8, 0x7A, 0x1E, true, s);
3732 let gold = |s: &str| paint(0xFF, 0xB0, 0x00, true, s);
3733 let dim = |s: &str| paint(0x7A, 0x7E, 0x88, false, s);
3734 let item = |key: &str, gloss: &str| format!("{} {}", orange(key), dim(gloss));
3736
3737 let err = std::io::stderr();
3738 let mut h = err.lock();
3739 let _ = writeln!(h);
3740 for row in crate::print::logo_rows(color) {
3743 let _ = writeln!(h, " {row}");
3744 }
3745 let _ = writeln!(h);
3746 let _ = writeln!(
3747 h,
3748 " {} {}",
3749 gold("interior-point debugger"),
3750 dim(&format!(
3751 "· pounce {} · pdb for the IPM",
3752 env!("CARGO_PKG_VERSION")
3753 ))
3754 );
3755 let _ = writeln!(h);
3756 let _ = writeln!(
3758 h,
3759 " {} {} {} {} {}",
3760 item("s", "step"),
3761 item("c", "continue"),
3762 item("b", "N break"),
3763 item("r", "N run"),
3764 item("q", "quit"),
3765 );
3766 let _ = writeln!(
3767 h,
3768 " {} {} {} {} {}",
3769 item("p", "x print"),
3770 item("i", "info"),
3771 item("set", "x[i] v"),
3772 item("watch", "x"),
3773 item("viz", "kkt"),
3774 );
3775 let _ = writeln!(
3776 h,
3777 " {} {} {}",
3778 dim("type"),
3779 gold("help"),
3780 dim("for all commands · `ask` to consult Claude · Ctrl-C breaks in"),
3781 );
3782 let _ = writeln!(h);
3783}
3784
3785fn is_pause_command(line: &str) -> bool {
3788 parse_command(line, DebugMode::Json).command.trim() == "pause"
3789}
3790
3791struct StdinPump {
3796 inner: std::sync::Arc<(
3797 std::sync::Mutex<VecDeque<Option<String>>>,
3798 std::sync::Condvar,
3799 )>,
3800}
3801
3802impl StdinPump {
3803 fn start() -> Self {
3804 let inner = std::sync::Arc::new((
3805 std::sync::Mutex::new(VecDeque::new()),
3806 std::sync::Condvar::new(),
3807 ));
3808 let w = std::sync::Arc::clone(&inner);
3809 std::thread::spawn(move || {
3810 use std::io::BufRead;
3811 let stdin = std::io::stdin();
3812 let mut lock = stdin.lock();
3813 let (m, cv) = &*w;
3814 loop {
3815 let mut line = String::new();
3816 let item = match lock.read_line(&mut line) {
3817 Ok(0) | Err(_) => None, Ok(_) => Some(line),
3819 };
3820 let done = item.is_none();
3821 m.lock()
3822 .unwrap_or_else(std::sync::PoisonError::into_inner)
3823 .push_back(item);
3824 cv.notify_one();
3825 if done {
3826 break;
3827 }
3828 }
3829 });
3830 Self { inner }
3831 }
3832
3833 fn next(&self) -> Option<String> {
3835 let (m, cv) = &*self.inner;
3836 let mut q = m.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
3837 loop {
3838 match q.front() {
3839 None => {
3840 q = cv
3841 .wait(q)
3842 .unwrap_or_else(std::sync::PoisonError::into_inner)
3843 }
3844 Some(None) => return None, Some(Some(_)) => return q.pop_front().flatten(),
3846 }
3847 }
3848 }
3849
3850 fn try_take_pause(&self) -> bool {
3853 let (m, _) = &*self.inner;
3854 let mut q = m.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
3855 if let Some(Some(front)) = q.front() {
3856 if is_pause_command(front) {
3857 q.pop_front();
3858 return true;
3859 }
3860 }
3861 false
3862 }
3863}
3864
3865impl DebugHook for SolverDebugger {
3866 fn wants_kkt_capture(&self) -> bool {
3870 !self.detached
3871 }
3872
3873 fn arm(&mut self) {
3877 self.step = true;
3878 self.detached = false;
3879 self.pause_iters = true;
3880 self.pause_terminal = true;
3881 }
3882
3883 fn at_checkpoint(&mut self, ctx: &mut dyn DebugState) -> DebugAction {
3884 if matches!(self.mode, DebugMode::Json) && !self.hello_sent {
3887 self.emit_hello();
3888 self.hello_sent = true;
3889 }
3890 if let Checkpoint::Terminated = ctx.checkpoint() {
3894 if self.sweep.is_some() {
3898 if let Some(c) = as_nlp(ctx) {
3901 if let Some(action) = self.drive_sweep(c) {
3902 return action;
3903 }
3904 }
3905 }
3906 let failed = ctx.status().map(|s| !is_success_status(s)).unwrap_or(false);
3907 let should =
3908 self.pause_terminal && !self.detached && (!self.terminal_only_on_error || failed);
3909 if !should {
3910 return DebugAction::Resume;
3911 }
3912 self.ensure_editor();
3913 self.emit_pause(ctx, None);
3914 return self.prompt_loop(ctx);
3915 }
3916
3917 let cp = ctx.checkpoint();
3918 match cp {
3920 Checkpoint::PreRestoration => self.in_restoration = true,
3921 Checkpoint::PostRestoration => self.in_restoration = false,
3922 _ => {}
3923 }
3924 let is_iter_start = matches!(cp, Checkpoint::IterStart);
3925
3926 if is_iter_start {
3930 if let Some(snap) = ctx.snapshot() {
3931 self.snapshots.insert(ctx.iter(), snap);
3932 while self.snapshots.len() > SNAPSHOT_CAP {
3933 let Some(&oldest) = self.snapshots.keys().next() else {
3934 break;
3935 };
3936 self.snapshots.remove(&oldest);
3937 }
3938 }
3939 self.update_mu_stall(ctx.mu());
3941 }
3942
3943 let mut reason: Option<String> = None;
3947 let mut pause = self.sub_step || self.stop_at.contains(cp.as_str());
3948
3949 if let Some(ev) = self.matched_event(ctx) {
3953 pause = true;
3954 reason = Some(format!("event: {ev}"));
3955 }
3956
3957 if is_iter_start {
3958 if self.interruptible && interrupt::take() {
3959 pause = true;
3960 reason = Some("interrupt (Ctrl-C)".into());
3961 }
3962 if let Some(p) = self.pump.as_ref() {
3965 if p.try_take_pause() {
3966 pause = true;
3967 reason = Some("pause (requested)".into());
3968 }
3969 }
3970 if self.pause_iters {
3971 if self.should_pause(ctx.iter()) {
3972 pause = true;
3973 }
3974 if let Some(c) = self.matched_condition(ctx) {
3975 pause = true;
3976 reason = Some(c);
3977 }
3978 }
3979 if let Some(w) = self.matched_watchpoint(ctx) {
3982 pause = true;
3983 reason = Some(format!("watchpoint: {w}"));
3984 }
3985 }
3986
3987 if !pause {
3988 if is_iter_start && self.emit_progress && matches!(self.mode, DebugMode::Json) {
3992 self.emit_progress_event(ctx);
3993 }
3994 return DebugAction::Resume;
3995 }
3996 self.step = false;
3998 self.sub_step = false;
3999 self.emit_pause(ctx, reason.as_deref());
4000
4001 if is_iter_start {
4005 if let Some(cmds) = self.bp_commands.get(&ctx.iter()).cloned() {
4006 for c in cmds {
4007 let out = self.dispatch(&c, ctx);
4008 self.emit_result(&c, &out, None);
4009 match out.flow {
4010 Flow::Resume => return DebugAction::Resume,
4011 Flow::Stop => return DebugAction::Stop,
4012 Flow::Stay => {}
4013 }
4014 }
4015 }
4016 }
4017
4018 self.ensure_editor();
4019 self.prompt_loop(ctx)
4020 }
4021}
4022
4023impl SolverDebugger {
4024 fn prompt_loop(&mut self, ctx: &mut dyn DebugState) -> DebugAction {
4026 if let Some(path) = self.pending_script.take() {
4029 let out = self.cmd_source(&[path.as_str()], ctx);
4030 self.emit_result("source", &out, None);
4031 match out.flow {
4032 Flow::Resume => return DebugAction::Resume,
4033 Flow::Stop => return DebugAction::Stop,
4034 Flow::Stay => {}
4035 }
4036 }
4037 loop {
4038 let line = match self.next_command_line() {
4039 Some(l) => l,
4040 None => {
4041 return match self.mode {
4046 DebugMode::Repl => {
4047 self.detached = true;
4048 DebugAction::Resume
4049 }
4050 DebugMode::Json => DebugAction::Stop,
4051 };
4052 }
4053 };
4054 let parsed = parse_command(&line, self.mode);
4055 let cmd = parsed.command.trim().to_string();
4056 if cmd.is_empty() {
4057 continue;
4058 }
4059 let out = self.dispatch(&cmd, ctx);
4060 self.emit_result(&cmd, &out, parsed.id.as_ref());
4061 match out.flow {
4062 Flow::Stay => continue,
4063 Flow::Resume => return DebugAction::Resume,
4064 Flow::Stop => return DebugAction::Stop,
4065 }
4066 }
4067 }
4068}
4069
4070struct ParsedCmd {
4074 command: String,
4075 id: Option<serde_json::Value>,
4076}
4077
4078fn tokenize_quoted(line: &str) -> Vec<String> {
4084 let mut out = Vec::new();
4085 let mut cur = String::new();
4086 let mut in_quote = false;
4087 let mut has_tok = false;
4088 for c in line.chars() {
4089 match c {
4090 '"' => {
4091 in_quote = !in_quote;
4092 has_tok = true; }
4094 c if c.is_whitespace() && !in_quote => {
4095 if has_tok {
4096 out.push(std::mem::take(&mut cur));
4097 has_tok = false;
4098 }
4099 }
4100 c => {
4101 cur.push(c);
4102 has_tok = true;
4103 }
4104 }
4105 }
4106 if has_tok {
4107 out.push(cur);
4108 }
4109 out
4110}
4111
4112fn parse_command(line: &str, mode: DebugMode) -> ParsedCmd {
4116 let trimmed = line.trim();
4117 if let DebugMode::Json = mode {
4118 if trimmed.starts_with('{') {
4119 if let Ok(v) = serde_json::from_str::<serde_json::Value>(trimmed) {
4120 let cmd = v.get("cmd").and_then(|c| c.as_str()).unwrap_or("");
4121 let mut s = cmd.to_string();
4122 if let Some(args) = v.get("args").and_then(|a| a.as_array()) {
4123 for a in args {
4124 s.push(' ');
4125 let tok = a
4126 .as_str()
4127 .map(str::to_string)
4128 .unwrap_or_else(|| a.to_string());
4129 if tok.contains(char::is_whitespace) {
4132 s.push('"');
4133 s.push_str(&tok);
4134 s.push('"');
4135 } else {
4136 s.push_str(&tok);
4137 }
4138 }
4139 }
4140 return ParsedCmd {
4141 command: s,
4142 id: v.get("id").cloned(),
4143 };
4144 }
4145 }
4146 }
4147 ParsedCmd {
4148 command: trimmed.to_string(),
4149 id: None,
4150 }
4151}
4152
4153fn emit_json(v: &serde_json::Value) {
4154 let stdout = std::io::stdout();
4155 let mut h = stdout.lock();
4156 let _ = writeln!(h, "{v}");
4157 let _ = h.flush();
4158}
4159
4160fn as_nlp<'a>(ctx: &'a dyn DebugState) -> Option<&'a DebugCtx> {
4165 ctx.as_any().and_then(|a| a.downcast_ref::<DebugCtx>())
4166}
4167
4168fn as_nlp_mut<'a>(ctx: &'a mut dyn DebugState) -> Option<&'a mut DebugCtx> {
4170 ctx.as_any_mut().and_then(|a| a.downcast_mut::<DebugCtx>())
4171}
4172
4173fn nlp_only(cmd: &str) -> CmdOut {
4175 CmdOut::err(format!(
4176 "`{cmd}` is only available for the NLP solver (not the convex/conic solver)"
4177 ))
4178}
4179
4180fn block_names(ctx: &dyn DebugState) -> Vec<&'static str> {
4185 ctx.block_dims().into_iter().map(|(n, _)| n).collect()
4186}
4187
4188fn is_block(ctx: &dyn DebugState, name: &str) -> bool {
4190 block_names(ctx).iter().any(|n| *n == name)
4191}
4192
4193fn fmt_vec(name: &str, v: &[f64]) -> String {
4194 const MAX: usize = 12;
4195 if v.len() <= MAX {
4196 format!(
4197 "{name} = [{}]",
4198 v.iter()
4199 .map(|x| format!("{x:.6e}"))
4200 .collect::<Vec<_>>()
4201 .join(", ")
4202 )
4203 } else {
4204 let head = v[..MAX]
4205 .iter()
4206 .map(|x| format!("{x:.6e}"))
4207 .collect::<Vec<_>>()
4208 .join(", ");
4209 format!("{name} = [{head}, … ({} total)]", v.len())
4210 }
4211}
4212
4213fn type_str(t: OptionType) -> &'static str {
4214 match t {
4215 OptionType::OT_Number => "Number",
4216 OptionType::OT_Integer => "Integer",
4217 OptionType::OT_String => "String",
4218 OptionType::OT_Unknown => "Unknown",
4219 }
4220}
4221
4222fn default_str(d: &DefaultValue) -> String {
4223 match d {
4224 DefaultValue::None => "-".into(),
4225 DefaultValue::Number(v) => format!("{v}"),
4226 DefaultValue::Integer(v) => format!("{v}"),
4227 DefaultValue::String(s) => s.clone(),
4228 }
4229}
4230
4231fn write_and_open(label: &str, iter: i32, vals: &[f64]) -> Result<(String, String), String> {
4236 let payload = serde_json::json!({"label": label, "iter": iter, "values": vals});
4237 write_json_and_open(label, iter, &payload)
4238}
4239
4240fn build_ask_prompt(ctx: &dyn DebugState, question: &str) -> String {
4243 use std::fmt::Write as _;
4244 let mut p = String::new();
4245 p.push_str(
4246 "You are helping debug a paused run of POUNCE, a pure-Rust interior-point \
4247 optimization solver whose NLP core is ported from Ipopt. The solve is \
4248 stopped at a debugger checkpoint. \
4249 Use the state below to answer concisely and suggest concrete next steps \
4250 (options to try, what to inspect). State:\n\n",
4251 );
4252 let _ = writeln!(p, "checkpoint = {}", ctx.checkpoint().as_str());
4253 if let Some(s) = ctx.status() {
4254 let _ = writeln!(p, "status = {s}");
4255 }
4256 let _ = writeln!(p, "iter = {}", ctx.iter());
4257 let _ = writeln!(p, "mu = {:.6e}", ctx.mu());
4258 let _ = writeln!(p, "objective = {:.8e}", ctx.objective());
4259 let _ = writeln!(p, "inf_pr = {:.6e}", ctx.inf_pr());
4260 let _ = writeln!(p, "inf_du = {:.6e}", ctx.inf_du());
4261 let _ = writeln!(p, "nlp_error = {:.6e}", ctx.nlp_error());
4262 let (ap, ad) = ctx.alpha();
4263 let _ = writeln!(p, "alpha_pr = {ap:.4e}, alpha_du = {ad:.4e}");
4264 let _ = writeln!(p, "ls_trials = {}", ctx.ls_count());
4265 let dims: Vec<String> = ctx
4266 .block_dims()
4267 .into_iter()
4268 .map(|(n, d)| format!("{n}:{d}"))
4269 .collect();
4270 let _ = writeln!(p, "dims = {}", dims.join(" "));
4271 if let Some(k) = ctx.kkt() {
4272 let _ = writeln!(
4273 p,
4274 "kkt = dim {} inertia n+={} n-={} (expected n-={}, {}) delta_w={:.3e} delta_c={:.3e} status={}",
4275 k.dim,
4276 k.n_pos,
4277 k.n_neg,
4278 k.expected_neg,
4279 if k.inertia_correct { "correct" } else { "WRONG" },
4280 k.delta_w,
4281 k.delta_c,
4282 k.status
4283 );
4284 }
4285 let _ = write!(p, "\nQuestion: {question}\n");
4286 p
4287}
4288
4289const LLM_PROVIDERS: &[&str] = &["claude", "codex", "gemini", "llm"];
4295
4296fn llm_preset(name: &str, prompt: &str) -> Option<(String, Vec<String>, bool)> {
4297 match name {
4298 "claude" => Some(("claude".to_string(), vec!["-p".to_string()], true)),
4300 "codex" => Some((
4302 "codex".to_string(),
4303 vec!["exec".to_string(), prompt.to_string()],
4304 false,
4305 )),
4306 "gemini" => Some((
4308 "gemini".to_string(),
4309 vec!["-p".to_string(), prompt.to_string()],
4310 false,
4311 )),
4312 "llm" => Some(("llm".to_string(), vec![prompt.to_string()], false)),
4314 _ => None,
4315 }
4316}
4317
4318fn llm_command(prompt: &str) -> (String, Vec<String>, bool) {
4324 let raw = std::env::var("POUNCE_DBG_LLM").unwrap_or_default();
4325 let tmpl = raw.trim();
4326 if tmpl.is_empty() {
4327 return llm_preset("claude", prompt).expect("claude is a known provider");
4329 }
4330 if !tmpl.contains(char::is_whitespace) {
4333 if let Some(preset) = llm_preset(tmpl, prompt) {
4334 return preset;
4335 }
4336 }
4337 let mut parts = tmpl
4339 .split_whitespace()
4340 .map(str::to_string)
4341 .collect::<Vec<_>>();
4342 let prog = parts.remove(0);
4343 let mut substituted = false;
4344 for a in parts.iter_mut() {
4345 if a.contains("{}") {
4346 *a = a.replace("{}", prompt);
4347 substituted = true;
4348 }
4349 }
4350 (prog, parts, !substituted)
4351}
4352
4353fn run_llm(prompt: &str) -> Result<String, String> {
4356 use std::io::Write as _;
4357 use std::process::{Command, Stdio};
4358 let (prog, args, on_stdin) = llm_command(prompt);
4359 let mut cmd = Command::new(&prog);
4360 cmd.args(&args)
4361 .stdout(Stdio::piped())
4362 .stderr(Stdio::piped());
4363 cmd.stdin(if on_stdin {
4364 Stdio::piped()
4365 } else {
4366 Stdio::null()
4367 });
4368 let mut child = cmd.spawn().map_err(|e| {
4369 if e.kind() == std::io::ErrorKind::NotFound {
4370 format!(
4374 "LLM CLI `{prog}` is not installed or not on PATH. Install it, \
4375 or set POUNCE_DBG_LLM to another provider \
4376 ({}) or a full command template (e.g. `my-llm --ask {{}}`).",
4377 LLM_PROVIDERS.join(" | ")
4378 )
4379 } else {
4380 format!("could not launch `{prog}`: {e}")
4381 }
4382 })?;
4383 if on_stdin {
4384 if let Some(mut si) = child.stdin.take() {
4386 let _ = si.write_all(prompt.as_bytes());
4387 }
4388 }
4389 let out = child
4390 .wait_with_output()
4391 .map_err(|e| format!("`{prog}` failed: {e}"))?;
4392 if !out.status.success() {
4393 let err = String::from_utf8_lossy(&out.stderr);
4394 return Err(format!(
4395 "`{prog}` exited with {}: {}",
4396 out.status,
4397 err.trim()
4398 ));
4399 }
4400 let reply = String::from_utf8_lossy(&out.stdout).trim().to_string();
4401 if reply.is_empty() {
4402 Err(format!("`{prog}` returned no output"))
4403 } else {
4404 Ok(reply)
4405 }
4406}
4407
4408fn write_json_and_open(
4411 label: &str,
4412 iter: i32,
4413 payload: &serde_json::Value,
4414) -> Result<(String, String), String> {
4415 let dir = std::env::temp_dir();
4416 let path = dir.join(format!("pounce-dbg-{label}-iter{iter}.json"));
4417 std::fs::write(&path, payload.to_string()).map_err(|e| format!("write failed: {e}"))?;
4418 let path_s = path.to_string_lossy().to_string();
4419
4420 let mut candidates: Vec<(String, Vec<String>, String)> = Vec::new();
4430 match std::env::var("POUNCE_DBG_VIEWER") {
4431 Ok(tmpl) if !tmpl.trim().is_empty() => {
4432 let mut parts = tmpl
4433 .split_whitespace()
4434 .map(String::from)
4435 .collect::<Vec<_>>();
4436 let prog = parts.remove(0);
4437 let mut replaced = false;
4438 for a in parts.iter_mut() {
4439 if a.contains("{}") {
4440 *a = a.replace("{}", &path_s);
4441 replaced = true;
4442 }
4443 }
4444 if !replaced {
4445 parts.push(path_s.clone());
4446 }
4447 candidates.push((prog, parts, path_s.clone()));
4448 }
4449 _ => {
4450 candidates.push((
4451 "pounce-dbg-viz".to_string(),
4452 vec![path_s.clone()],
4453 path_s.clone(),
4454 ));
4455 let opener = if cfg!(target_os = "macos") {
4456 "open"
4457 } else {
4458 "xdg-open"
4459 };
4460 let artifact = write_html_viz(label, iter, payload).unwrap_or_else(|_| path_s.clone());
4463 candidates.push((opener.to_string(), vec![artifact.clone()], artifact));
4464 }
4465 }
4466
4467 let mut last_err = String::new();
4468 for (program, args, artifact) in &candidates {
4469 match std::process::Command::new(program).args(args).spawn() {
4470 Ok(_) => return Ok((artifact.clone(), format!("{program} {}", args.join(" ")))),
4471 Err(e) => last_err = format!("`{program}`: {e}"),
4472 }
4473 }
4474 Err(format!(
4475 "wrote {path_s} but could not launch a viewer ({last_err}). \
4476 Install the interactive viewer (`pip install 'pounce-solver[viz]'`) \
4477 or set POUNCE_DBG_VIEWER, e.g. `python my_plot.py {{}}`."
4478 ))
4479}
4480
4481fn write_html_viz(label: &str, iter: i32, payload: &serde_json::Value) -> Result<String, String> {
4488 let dir = std::env::temp_dir();
4489 let path = dir.join(format!("pounce-dbg-{label}-iter{iter}.html"));
4490 let html = VIZ_HTML_TEMPLATE.replace("__PAYLOAD__", &payload.to_string());
4491 std::fs::write(&path, html).map_err(|e| format!("write failed: {e}"))?;
4492 Ok(path.to_string_lossy().to_string())
4493}
4494
4495const VIZ_HTML_TEMPLATE: &str = r##"<!doctype html>
4500<html lang="en"><head><meta charset="utf-8">
4501<title>pounce-dbg viz</title>
4502<style>
4503 html,body{margin:0;background:#0e1116;color:#d6dae0;
4504 font:13px/1.5 -apple-system,BlinkMacSystemFont,"SF Mono",Menlo,monospace}
4505 .wrap{padding:18px 20px;max-width:880px;margin:0 auto}
4506 h1{font-size:15px;margin:0 0 4px;font-weight:600}
4507 .sub{color:#7d8694;margin:0 0 12px}
4508 .stats{color:#9aa4b2;white-space:pre-wrap;margin:0 0 14px;
4509 background:#161b22;border:1px solid #21262d;border-radius:6px;padding:10px 12px}
4510 canvas{background:#161b22;border:1px solid #30363d;border-radius:6px;
4511 max-width:100%;height:auto;image-rendering:pixelated}
4512 .legend{margin-top:10px;color:#9aa4b2}
4513 .pos{color:#4ea1ff}.neg{color:#ff6b6b}.bad{color:#ff6b6b;font-weight:600}
4514 .ok{color:#56d364;font-weight:600}
4515</style></head><body><div class="wrap">
4516<h1 id="title">pounce-dbg</h1>
4517<div class="sub" id="sub"></div>
4518<div class="stats" id="stats"></div>
4519<canvas id="c" width="820" height="820"></canvas>
4520<div class="legend" id="legend"></div>
4521</div>
4522<script>
4523const D = __PAYLOAD__;
4524const cv = document.getElementById('c');
4525const ctx = cv.getContext('2d');
4526const $ = id => document.getElementById(id);
4527const fmt = x => (x===null||x===undefined) ? '—'
4528 : (Math.abs(x) >= 1e4 || (x!==0 && Math.abs(x) < 1e-3) ? x.toExponential(3) : (+x).toPrecision(6));
4529
4530function clearCanvas(){ ctx.fillStyle='#161b22'; ctx.fillRect(0,0,cv.width,cv.height); }
4531
4532function spy(irn, jcn, vals, dim, symmetric, title){
4533 $('sub').textContent = title;
4534 clearCanvas();
4535 const W=cv.width, H=cv.height, pad=42;
4536 const span=Math.max(1, dim);
4537 const cell=(Math.min(W,H)-2*pad)/span;
4538 const px=Math.max(0.7, cell);
4539 // frame + light grid ticks
4540 ctx.strokeStyle='#30363d'; ctx.lineWidth=1;
4541 ctx.strokeRect(pad-0.5, pad-0.5, span*cell+1, span*cell+1);
4542 ctx.fillStyle='#6e7681'; ctx.font='11px monospace';
4543 ctx.fillText('0', pad-12, pad+9);
4544 ctx.fillText(String(dim), pad+span*cell-8, pad-8);
4545 ctx.fillText('row', pad-34, pad+span*cell/2);
4546 ctx.fillText('col', pad+span*cell/2-8, pad-22);
4547 let nnz=0;
4548 for(let k=0;k<irn.length;k++){
4549 const i=irn[k]-1, j=jcn[k]-1, v=vals?vals[k]:1;
4550 ctx.fillStyle = v>=0 ? 'rgba(78,161,255,0.92)' : 'rgba(255,107,107,0.92)';
4551 ctx.fillRect(pad+j*cell, pad+i*cell, px, px); nnz++;
4552 if(symmetric && i!==j){ ctx.fillRect(pad+i*cell, pad+j*cell, px, px); nnz++; }
4553 }
4554 $('legend').innerHTML =
4555 `<span class="pos">■</span> positive <span class="neg">■</span> negative`
4556 + ` · ${dim}×${dim}, ${nnz} plotted nonzeros`
4557 + (symmetric ? ' (lower triangle mirrored)' : '');
4558}
4559
4560function bars(values, title){
4561 $('sub').textContent = title;
4562 clearCanvas();
4563 const W=cv.width, H=cv.height, pad=42;
4564 const n=values.length;
4565 const maxAbs=Math.max(1e-300, ...values.map(v=>Math.abs(v)));
4566 const x0=pad, y0=H-pad, plotW=W-2*pad, plotH=H-2*pad, mid=pad+plotH/2;
4567 const bw=Math.max(0.7, plotW/Math.max(1,n));
4568 // zero axis
4569 ctx.strokeStyle='#30363d'; ctx.beginPath();
4570 ctx.moveTo(pad, mid); ctx.lineTo(W-pad, mid); ctx.stroke();
4571 ctx.fillStyle='#6e7681'; ctx.font='11px monospace';
4572 ctx.fillText('+'+fmt(maxAbs), 4, pad+10);
4573 ctx.fillText('-'+fmt(maxAbs), 4, H-pad-2);
4574 ctx.fillText('0', 4, mid+4);
4575 for(let k=0;k<n;k++){
4576 const v=values[k], h=(Math.abs(v)/maxAbs)*(plotH/2);
4577 ctx.fillStyle = v>=0 ? 'rgba(78,161,255,0.92)' : 'rgba(255,107,107,0.92)';
4578 if(v>=0) ctx.fillRect(pad+k*bw, mid-h, bw, h);
4579 else ctx.fillRect(pad+k*bw, mid, bw, h);
4580 }
4581 $('legend').innerHTML = `${n} components · max |val| = ${fmt(maxAbs)}`;
4582}
4583
4584const lbl = D.label || 'viz';
4585const iter = (D.iter!==undefined) ? D.iter : '?';
4586$('title').textContent = `pounce-dbg · viz ${lbl} · iter ${iter}`;
4587
4588if(D.matrix && D.matrix.irn){
4589 const m=D.matrix;
4590 const inertia = (D.inertia_correct===false)
4591 ? `<span class="bad">WRONG</span>` : `<span class="ok">correct</span>`;
4592 $('stats').innerHTML =
4593 `KKT augmented system dim=${D.dim}\n`+
4594 `inertia n+=${D.n_pos} n-=${D.n_neg} (expected n-=${D.expected_neg}, ${inertia})\n`+
4595 `regularization delta_w=${fmt(D.delta_w)} delta_c=${fmt(D.delta_c)}\n`+
4596 `factorization status: ${D.status}`;
4597 spy(m.irn, m.jcn, m.vals, m.dim, true, 'sparsity pattern (sign-colored)');
4598} else if(D.l_irn){
4599 $('stats').textContent =
4600 `LDLᵀ factor n=${D.n} nnz(L)=${D.l_irn.length} format=${D.format||''}`;
4601 spy(D.l_irn, D.l_jcn, D.l_vals, D.n, false, 'L factor sparsity (permuted, strict lower)');
4602} else if(D.values){
4603 $('stats').textContent = `vector ${lbl} length=${D.values.length}`;
4604 bars(D.values, 'component magnitudes (zero-centered)');
4605} else {
4606 $('stats').textContent = 'unrecognized payload — raw JSON:\n'+JSON.stringify(D,null,2);
4607}
4608</script></body></html>
4609"##;
4610
4611#[cfg(test)]
4612mod tests {
4613 use super::*;
4614
4615 fn dbg(mode: DebugMode) -> SolverDebugger {
4616 SolverDebugger::new(mode, None)
4617 }
4618
4619 #[test]
4620 fn json_command_object_is_flattened() {
4621 assert_eq!(
4622 parse_command("{\"cmd\":\"print x\"}", DebugMode::Json).command,
4623 "print x"
4624 );
4625 let p = parse_command(
4626 "{\"cmd\":\"set\",\"args\":[\"x[0]\",\"1.5\"],\"id\":7}",
4627 DebugMode::Json,
4628 );
4629 assert_eq!(p.command, "set x[0] 1.5");
4630 assert_eq!(p.id, Some(serde_json::json!(7)));
4632 let s = parse_command("step\n", DebugMode::Json);
4634 assert_eq!(s.command, "step");
4635 assert!(s.id.is_none());
4636 assert_eq!(
4637 parse_command(" print x \n", DebugMode::Repl).command,
4638 "print x"
4639 );
4640 }
4641
4642 #[test]
4643 fn pauses_at_first_checkpoint_then_only_when_rearmed() {
4644 let mut d = dbg(DebugMode::Repl);
4645 assert!(d.should_pause(0));
4647 d.step = false;
4649 assert!(!d.should_pause(1));
4650 assert!(!d.should_pause(2));
4651 }
4652
4653 #[test]
4654 fn breakpoints_and_run_to_arm_pauses() {
4655 let mut d = dbg(DebugMode::Repl);
4656 d.step = false;
4657 d.breaks = vec![3, 7];
4658 assert!(!d.should_pause(2));
4659 assert!(d.should_pause(3));
4660 assert!(d.should_pause(7));
4661 d.run_to = Some(5);
4663 assert!(!d.should_pause(4));
4664 assert!(d.should_pause(5));
4665 assert_eq!(d.run_to, None);
4666 assert!(!d.should_pause(6));
4667 }
4668
4669 #[test]
4670 fn atom_parses_metric_op_threshold() {
4671 let a = Atom::parse("mu<1e-4").unwrap();
4672 assert_eq!(a.metric, Metric::Mu);
4673 assert_eq!(a.op, CmpOp::Lt);
4674 assert_eq!(a.rhs, 1e-4);
4675
4676 let a = Atom::parse("inf_pr<=1e-6").unwrap();
4678 assert_eq!(a.metric, Metric::InfPr);
4679 assert_eq!(a.op, CmpOp::Le);
4680
4681 let a = Atom::parse("iter==10").unwrap();
4682 assert_eq!(a.metric, Metric::Iter);
4683 assert_eq!(a.op, CmpOp::Eq);
4684 assert_eq!(a.rhs, 10.0);
4685 }
4686
4687 struct MinimalState;
4693 impl DebugState for MinimalState {
4694 fn checkpoint(&self) -> Checkpoint {
4695 Checkpoint::IterStart
4696 }
4697 fn iter(&self) -> i32 {
4698 7
4699 }
4700 fn mu(&self) -> f64 {
4701 1e-3
4702 }
4703 fn objective(&self) -> f64 {
4704 42.0
4705 }
4706 fn inf_pr(&self) -> f64 {
4707 1e-4
4708 }
4709 fn inf_du(&self) -> f64 {
4710 2e-4
4711 }
4712 fn complementarity(&self) -> f64 {
4713 5e-4
4714 }
4715 fn alpha(&self) -> (f64, f64) {
4716 (1.0, 1.0)
4717 }
4718 fn block_dims(&self) -> Vec<(&'static str, usize)> {
4719 vec![]
4720 }
4721 fn block(&self, _name: &str) -> Option<Vec<f64>> {
4722 None
4723 }
4724 fn delta_block(&self, _name: &str) -> Option<Vec<f64>> {
4725 None
4726 }
4727 }
4728
4729 #[test]
4735 fn metric_fields_match_advertised_vocabulary() {
4736 let fields = metric_fields(&MinimalState);
4737
4738 let names: Vec<&str> = fields.iter().map(|(n, _)| *n).collect();
4740 assert_eq!(names, METRICS);
4741
4742 for &name in METRICS {
4745 assert!(
4746 name == "iter" || Metric::parse(name).is_some(),
4747 "METRICS entry `{name}` has no matching Metric arm"
4748 );
4749 }
4750
4751 let map: std::collections::HashMap<_, _> = fields.into_iter().collect();
4752 assert_eq!(map["iter"], serde_json::json!(7));
4754 assert_eq!(map["objective"], serde_json::json!(42.0));
4755 assert_eq!(map["nlp_error"], serde_json::Value::Null);
4758 }
4759
4760 #[test]
4761 fn atom_parse_rejects_garbage() {
4762 assert!(Atom::parse("inf_pr 1e-6").is_err()); assert!(Atom::parse("bogus<1").is_err()); assert!(Atom::parse("mu<abc").is_err()); }
4766
4767 #[test]
4768 fn compound_condition_parses_and_evaluates_left_to_right() {
4769 let c = Condition::parse("mu<1e-4&&inf_pr>1e-3").unwrap();
4771 assert_eq!(c.rest.len(), 1);
4772 assert_eq!(c.rest[0].0, Join::And);
4773
4774 let c = Condition::parse("iter>10&&(inf_du>1e-2||obj<0)").unwrap();
4776 assert_eq!(c.rest.len(), 2);
4777 assert_eq!(c.rest[0].0, Join::And);
4778 assert_eq!(c.rest[1].0, Join::Or);
4779 assert_eq!(c.raw, "iter>10&&inf_du>1e-2||obj<0");
4780
4781 assert!(Condition::parse("mu<1e-4&&bogus>0").is_err());
4783 }
4784
4785 #[test]
4786 fn completion_is_context_sensitive() {
4787 let c = completion_candidates(None, "", "co");
4789 assert!(c.contains(&"continue".to_string()));
4790 assert!(c.contains(&"complete".to_string()));
4791 assert!(!c.contains(&"step".to_string()));
4792
4793 let c = completion_candidates(None, "set ", "");
4795 assert!(c.contains(&"mu".to_string()));
4796 assert!(c.contains(&"opt".to_string()));
4797 assert!(c.contains(&"x".to_string()));
4798
4799 let c = completion_candidates(None, "break if ", "inf");
4801 assert!(c.contains(&"inf_pr".to_string()));
4802 assert!(c.contains(&"inf_du".to_string()));
4803 assert!(!c.contains(&"mu".to_string()));
4804
4805 let c = completion_candidates(None, "print ", "");
4807 assert!(c.contains(&"x".to_string()));
4808 assert!(c.contains(&"obj".to_string()));
4809 }
4810
4811 #[test]
4812 fn cmp_op_truth_table() {
4813 assert!(CmpOp::Lt.eval(1.0, 2.0));
4814 assert!(!CmpOp::Lt.eval(2.0, 2.0));
4815 assert!(CmpOp::Le.eval(2.0, 2.0));
4816 assert!(CmpOp::Gt.eval(3.0, 2.0));
4817 assert!(CmpOp::Ge.eval(2.0, 2.0));
4818 assert!(CmpOp::Eq.eval(2.0, 2.0));
4819 assert!(!CmpOp::Eq.eval(2.0, 2.5));
4820 }
4821
4822 #[test]
4823 fn interrupt_is_consumed_once() {
4824 interrupt::set_pending_for_test();
4825 assert!(interrupt::take(), "first take sees the pending Ctrl-C");
4826 assert!(!interrupt::take(), "second take is clear (consumed once)");
4827 }
4828
4829 #[test]
4830 fn on_interrupt_constructor_runs_free_but_interruptible() {
4831 let d = SolverDebugger::on_interrupt(DebugMode::Repl, None);
4832 assert!(!d.pause_iters, "on-interrupt does not pause each iter");
4833 assert!(!d.pause_terminal, "on-interrupt does not pause at terminal");
4834 assert!(d.interruptible, "on-interrupt honors Ctrl-C");
4835 assert!(!d.step, "on-interrupt starts un-armed");
4836 }
4837
4838 #[test]
4839 fn coffee_easter_egg_prints_art_but_stays_hidden() {
4840 let d = SolverDebugger::new(DebugMode::Repl, None);
4841 let out = d.cmd_coffee();
4842 assert!(out.ok);
4843 assert!(out.lines.len() > 5, "multi-line art");
4844 assert!(
4845 out.lines.iter().any(|l| l.contains("COFFEE")),
4846 "the mug says COFFEE"
4847 );
4848 assert!(
4850 !COMMANDS.contains(&"coffee"),
4851 "hidden from help/complete/Tab"
4852 );
4853 assert!(
4855 out.lines.iter().all(|l| !l.contains('\x1b')),
4856 "no color when stderr isn't a TTY"
4857 );
4858 }
4859
4860 #[test]
4861 fn double_ctrl_c_at_prompt_quits_single_cancels_line() {
4862 let mut d = SolverDebugger::new(DebugMode::Repl, None);
4863 assert_eq!(d.on_prompt_interrupt(), "");
4865 assert_eq!(d.on_prompt_interrupt(), "quit");
4867 assert_eq!(d.on_prompt_interrupt(), "");
4869 d.prompt_interrupts = 0;
4872 assert_eq!(d.on_prompt_interrupt(), "", "fresh streak after a command");
4873 }
4874
4875 #[test]
4876 fn stop_at_accepts_names_and_aliases() {
4877 let mut d = SolverDebugger::new(DebugMode::Repl, None);
4878 assert!(d.cmd_stop_at(&["after_search_dir"]).ok);
4879 assert!(d.stop_at.contains("after_search_dir"));
4880 assert!(d.cmd_stop_at(&["mu"]).ok);
4882 assert!(d.stop_at.contains("after_mu"));
4883 assert!(d.cmd_stop_at(&["kkt"]).ok);
4884 assert!(d.stop_at.contains("after_search_dir"));
4885 assert!(!d.cmd_stop_at(&["bogus"]).ok);
4887 assert!(d.cmd_stop_at(&["clear"]).ok);
4889 assert!(d.stop_at.is_empty());
4890 }
4891
4892 #[test]
4893 fn llm_command_defaults_and_overrides() {
4894 std::env::remove_var("POUNCE_DBG_LLM");
4896 let (prog, args, on_stdin) = llm_command("hi");
4897 assert_eq!(prog, "claude");
4898 assert_eq!(args, vec!["-p".to_string()]);
4899 assert!(on_stdin);
4900
4901 std::env::set_var("POUNCE_DBG_LLM", "mytool --ask {}");
4903 let (prog, args, on_stdin) = llm_command("why");
4904 assert_eq!(prog, "mytool");
4905 assert_eq!(args, vec!["--ask".to_string(), "why".to_string()]);
4906 assert!(!on_stdin);
4907
4908 std::env::set_var("POUNCE_DBG_LLM", "llm -m gpt");
4910 let (_, _, on_stdin) = llm_command("q");
4911 assert!(on_stdin);
4912
4913 std::env::set_var("POUNCE_DBG_LLM", "codex");
4917 let (prog, args, on_stdin) = llm_command("why is mu stuck");
4918 assert_eq!(prog, "codex");
4919 assert_eq!(
4920 args,
4921 vec!["exec".to_string(), "why is mu stuck".to_string()]
4922 );
4923 assert!(!on_stdin); std::env::set_var("POUNCE_DBG_LLM", "gemini");
4926 let (prog, args, _) = llm_command("q");
4927 assert_eq!(prog, "gemini");
4928 assert_eq!(args, vec!["-p".to_string(), "q".to_string()]);
4929
4930 std::env::set_var("POUNCE_DBG_LLM", "llm");
4931 let (prog, args, _) = llm_command("q");
4932 assert_eq!(prog, "llm");
4933 assert_eq!(args, vec!["q".to_string()]);
4934
4935 std::env::set_var("POUNCE_DBG_LLM", "claude");
4938 let (prog, args, on_stdin) = llm_command("q");
4939 assert_eq!(prog, "claude");
4940 assert_eq!(args, vec!["-p".to_string()]);
4941 assert!(on_stdin);
4942
4943 std::env::set_var("POUNCE_DBG_LLM", "mytool");
4946 let (prog, args, on_stdin) = llm_command("q");
4947 assert_eq!(prog, "mytool");
4948 assert!(args.is_empty());
4949 assert!(on_stdin);
4950
4951 std::env::set_var("POUNCE_DBG_LLM", "pounce-no-such-llm-xyz");
4954 let err = run_llm("hello").unwrap_err();
4955 assert!(err.contains("not installed or not on PATH"), "{err}");
4956 assert!(err.contains("codex"), "{err}");
4957
4958 std::env::remove_var("POUNCE_DBG_LLM");
4959 }
4960
4961 #[test]
4962 fn detach_disables_all_pausing() {
4963 let mut d = dbg(DebugMode::Repl);
4964 d.detached = true;
4965 d.step = true;
4966 d.breaks = vec![1];
4967 assert!(!d.should_pause(0));
4968 assert!(!d.should_pause(1));
4969 }
4970
4971 #[test]
4972 fn kkt_capture_tracks_attached_state() {
4973 let mut d = dbg(DebugMode::Repl);
4976 assert!(d.wants_kkt_capture());
4977 d.detached = true;
4978 assert!(!d.wants_kkt_capture());
4979 }
4980
4981 fn resid(kind: ResidKind, index: usize, value: f64) -> Residual {
4982 Residual { kind, index, value }
4983 }
4984
4985 #[test]
4986 fn rank_residuals_sorts_by_magnitude_and_truncates() {
4987 use ResidKind::*;
4988 let entries = vec![
4989 resid(Eq, 0, -0.5),
4990 resid(Ineq, 1, 3.0),
4991 resid(DualX, 2, -7.0),
4992 resid(DualS, 3, 1.0),
4993 ];
4994 let top = rank_residuals(entries, 2);
4995 assert_eq!(top.len(), 2);
4996 assert_eq!(top[0].value, -7.0);
4998 assert_eq!(top[0].kind, DualX);
4999 assert_eq!(top[1].value, 3.0);
5000 assert_eq!(top[1].kind, Ineq);
5001 }
5002
5003 #[test]
5004 fn rank_residuals_k_zero_and_k_over_len() {
5005 use ResidKind::*;
5006 let entries = vec![resid(Eq, 0, 1.0), resid(Ineq, 1, 2.0)];
5007 assert!(rank_residuals(entries.clone(), 0).is_empty());
5008 let all = rank_residuals(entries, 99);
5010 assert_eq!(all.len(), 2);
5011 assert_eq!(all[0].value, 2.0);
5012 }
5013
5014 #[test]
5015 fn rank_residuals_is_stable_on_magnitude_ties() {
5016 use ResidKind::*;
5017 let entries = vec![
5019 resid(Ineq, 5, -2.0),
5020 resid(Eq, 1, 2.0),
5021 resid(DualX, 9, -2.0),
5022 ];
5023 let top = rank_residuals(entries, 3);
5024 assert_eq!(
5025 top.iter().map(|r| r.kind).collect::<Vec<_>>(),
5026 vec![Ineq, Eq, DualX]
5027 );
5028 }
5029
5030 fn split_names_fixture() -> SplitNames {
5031 SplitNames {
5032 x_var: vec![Some("T_reactor".into()), None],
5033 eq: vec![Some("mass_balance".into()), Some("energy_balance".into())],
5034 ineq: vec![Some("pressure_cap".into())],
5035 }
5036 }
5037
5038 #[test]
5039 fn resid_name_maps_each_kind_to_its_pool() {
5040 use ResidKind::*;
5041 let names = Some(split_names_fixture());
5042 assert_eq!(
5045 resid_name(&resid(Eq, 1, 0.0), &names),
5046 Some("energy_balance")
5047 );
5048 assert_eq!(
5049 resid_name(&resid(Ineq, 0, 0.0), &names),
5050 Some("pressure_cap")
5051 );
5052 assert_eq!(
5053 resid_name(&resid(DualS, 0, 0.0), &names),
5054 Some("pressure_cap")
5055 );
5056 assert_eq!(resid_name(&resid(DualX, 0, 0.0), &names), Some("T_reactor"));
5057 assert_eq!(resid_name(&resid(DualX, 1, 0.0), &names), None);
5059 assert_eq!(resid_name(&resid(Eq, 9, 0.0), &names), None);
5060 assert_eq!(resid_name(&resid(Eq, 0, 0.0), &None), None);
5062 }
5063
5064 #[test]
5065 fn worst_named_picks_largest_and_labels_it() {
5066 use ResidKind::*;
5067 let names = Some(split_names_fixture());
5068 let resids = vec![resid(Eq, 0, 0.5), resid(Eq, 1, -3.2), resid(Ineq, 0, 1.1)];
5070 assert_eq!(
5071 worst_named(resids, &names),
5072 Some(("c[energy_balance]".to_string(), -3.2))
5073 );
5074 let resids = vec![resid(DualX, 7, 9.0)];
5076 assert_eq!(
5077 worst_named(resids, &None),
5078 Some(("grad_x_L[7]".to_string(), 9.0))
5079 );
5080 assert_eq!(worst_named(vec![], &names), None);
5082 }
5083
5084 use pounce_algorithm::debug_rank::RankCulprit;
5085
5086 fn rank_report_fixture() -> RankReport {
5087 RankReport {
5090 rows: vec![
5091 RankRow {
5092 kind: ResidKind::Eq,
5093 index: 0,
5094 },
5095 RankRow {
5096 kind: ResidKind::Eq,
5097 index: 1,
5098 },
5099 ],
5100 n_cols: 3,
5101 singular_values: vec![2.0, 0.0],
5102 tol: 1e-15,
5103 rank: 1,
5104 cond: f64::INFINITY,
5105 culprits: vec![
5106 RankCulprit {
5107 row: 0,
5108 weight: 0.5,
5109 },
5110 RankCulprit {
5111 row: 1,
5112 weight: 0.5,
5113 },
5114 ],
5115 }
5116 }
5117
5118 #[test]
5119 fn render_rank_report_names_culprits_and_builds_json() {
5120 let names = Some(split_names_fixture());
5121 let rep = rank_report_fixture();
5122 let (lines, data) = render_rank_report(&rep, &names, None, 7);
5124
5125 let text = lines.join("\n");
5126 assert!(text.contains("2 row(s) × 3 column(s)"), "{text}");
5127 assert!(text.contains("numerical rank = 1 / 2"), "{text}");
5128 assert!(text.contains("inf (σ_min = 0)"), "{text}");
5130 assert!(text.contains("c[mass_balance]"), "{text}");
5132 assert!(text.contains("c[energy_balance]"), "{text}");
5133 assert!(text.contains("participation 0.50"), "{text}");
5134 assert!(text.contains("print equation"), "{text}");
5136
5137 assert_eq!(data["iter"], 7);
5140 assert_eq!(data["rank"], 1);
5141 assert_eq!(data["deficiency"], 1);
5142 assert_eq!(data["rank_deficient"], true);
5143 assert!(data["cond"].is_null(), "non-finite cond ⇒ null: {data}");
5144 assert_eq!(data["culprits"][0]["name"], "mass_balance");
5145 assert_eq!(data["culprits"][0]["label"], "c[mass_balance]");
5146 assert!(data["culprits"][0]["equation"].is_null());
5147 assert_eq!(data["culprits"][1]["name"], "energy_balance");
5148 }
5149
5150 #[test]
5151 fn render_rank_report_prints_culprit_equations_inline() {
5152 let names = Some(split_names_fixture());
5153 let rep = rank_report_fixture();
5154 let book = EquationBook::new(
5157 vec!["mass_balance".into(), "energy_balance".into()],
5158 vec![
5159 "x[0] + x[1] - 10 = 0".into(),
5160 "T_reactor*flow - Q = 0".into(),
5161 ],
5162 );
5163 let (lines, data) = render_rank_report(&rep, &names, Some(&book), 7);
5164
5165 let text = lines.join("\n");
5166 assert!(text.contains("x[0] + x[1] - 10 = 0"), "{text}");
5169 assert!(text.contains("T_reactor*flow - Q = 0"), "{text}");
5170 assert!(!text.contains("inspect a row with"), "{text}");
5172
5173 assert_eq!(data["culprits"][0]["equation"], "x[0] + x[1] - 10 = 0");
5175 assert_eq!(data["culprits"][1]["equation"], "T_reactor*flow - Q = 0");
5176 }
5177
5178 #[test]
5179 fn render_rank_report_full_rank_reports_positive_signal() {
5180 let rep = RankReport {
5181 rows: vec![
5182 RankRow {
5183 kind: ResidKind::Eq,
5184 index: 0,
5185 },
5186 RankRow {
5187 kind: ResidKind::Eq,
5188 index: 1,
5189 },
5190 ],
5191 n_cols: 3,
5192 singular_values: vec![2.0, 1.0],
5193 tol: 1e-15,
5194 rank: 2,
5195 cond: 2.0,
5196 culprits: vec![],
5197 };
5198 let (lines, data) = render_rank_report(&rep, &None, None, 3);
5199 let text = lines.join("\n");
5200 assert!(text.contains("full row rank"), "{text}");
5201 assert!(!text.contains("rank-deficient"), "{text}");
5202 assert_eq!(data["rank_deficient"], false);
5203 assert_eq!(data["cond"], 2.0);
5204 assert_eq!(data["culprits"].as_array().map(|a| a.len()), Some(0));
5205 }
5206
5207 #[test]
5208 fn print_equation_resolves_by_name_index_and_errors() {
5209 let mut d = dbg(DebugMode::Repl);
5210 let out = d.cmd_print_equation(&[]);
5212 assert!(!out.ok);
5213 assert!(out.lines[0].contains("needs an .nl model"));
5214
5215 d.set_equation_book(EquationBook::new(
5216 vec!["mass_balance".into(), String::new()],
5217 vec!["x[0] + x[1] = 10".into(), "x[0] - x[1] <= 2".into()],
5218 ));
5219
5220 let out = d.cmd_print_equation(&[]);
5222 assert!(out.ok);
5223 assert!(out.lines[0].contains("2 constraint equation"));
5224
5225 let out = d.cmd_print_equation(&["mass_balance"]);
5227 assert!(out.ok);
5228 assert_eq!(out.lines[0], "mass_balance: x[0] + x[1] = 10");
5229
5230 let out = d.cmd_print_equation(&["1"]);
5232 assert!(out.ok);
5233 assert_eq!(out.lines[0], "c[1]: x[0] - x[1] <= 2");
5234
5235 let out = d.cmd_print_equation(&["nope"]);
5237 assert!(!out.ok);
5238 assert!(out.lines[0].contains("no constraint named or indexed"));
5239 }
5240
5241 fn eq_inc(n_vars: usize, eq_row_inner_idx: Vec<usize>, rows: &[&[usize]]) -> EqualityIncidence {
5245 let mut adj_ptr = vec![0usize];
5246 let mut vars = Vec::new();
5247 for r in rows {
5248 let mut v = r.to_vec();
5249 v.sort_unstable();
5250 v.dedup();
5251 vars.extend_from_slice(&v);
5252 adj_ptr.push(vars.len());
5253 }
5254 EqualityIncidence {
5255 n_vars,
5256 eq_row_inner_idx,
5257 adj_ptr,
5258 vars,
5259 }
5260 }
5261
5262 #[test]
5263 fn structural_singularity_names_overdetermined_equations() {
5264 let inc = eq_inc(2, vec![0, 1, 2], &[&[0, 1], &[0, 1], &[0, 1]]);
5270 let book = StructureBook::new(
5271 inc,
5272 vec!["balance_a".into(), "balance_b".into(), "balance_c".into()],
5273 vec!["flow".into(), "temp".into()],
5274 );
5275 let f = book.findings();
5276 assert_eq!(f.len(), 1);
5277 let (sev, code, msg) = &f[0];
5278 assert_eq!(*sev, "warning");
5279 assert_eq!(*code, "structural_singularity");
5280 assert!(msg.contains("balance_a"), "msg: {msg}");
5281 assert!(msg.contains("balance_b"), "msg: {msg}");
5282 assert!(msg.contains("balance_c"), "msg: {msg}");
5283 assert!(msg.contains("flow") && msg.contains("temp"), "msg: {msg}");
5284 assert!(msg.contains("≥1"), "msg: {msg}");
5285 }
5286
5287 #[test]
5288 fn structural_findings_silent_when_well_posed_and_fall_back_to_indices() {
5289 let inc = eq_inc(2, vec![0, 1], &[&[0], &[1]]);
5293 let book = StructureBook::new(inc, vec![], vec![]);
5294 assert!(book.findings().is_empty());
5295
5296 let inc = eq_inc(1, vec![0, 1, 3], &[&[0], &[0], &[0]]);
5300 let book = StructureBook::new(inc, vec![], vec![]);
5301 let f = book.findings();
5302 assert_eq!(f.len(), 1);
5303 let msg = &f[0].2;
5304 assert!(
5305 msg.contains("c[0]") && msg.contains("c[1]") && msg.contains("c[3]"),
5306 "msg: {msg}"
5307 );
5308 }
5309
5310 #[test]
5311 fn structural_singularity_handles_empty_row_with_no_variables() {
5312 let inc = eq_inc(1, vec![0, 1], &[&[0], &[]]);
5315 let book = StructureBook::new(inc, vec!["real".into(), "ghost".into()], vec!["x".into()]);
5316 let f = book.findings();
5317 assert_eq!(f.len(), 1);
5318 let msg = &f[0].2;
5319 assert!(msg.contains("ghost"), "msg: {msg}");
5320 assert!(msg.contains("no variables"), "msg: {msg}");
5321 }
5322
5323 #[test]
5324 fn parse_floats_accepts_commas_whitespace_and_newlines() {
5325 assert_eq!(parse_floats("1, 2 ,3").unwrap(), vec![1.0, 2.0, 3.0]);
5326 assert_eq!(parse_floats("1\n2\n-3.5").unwrap(), vec![1.0, 2.0, -3.5]);
5327 assert_eq!(parse_floats(" 1.0 2e-1 ").unwrap(), vec![1.0, 0.2]);
5328 assert!(parse_floats("1, nope, 3").is_err());
5329 assert_eq!(parse_floats("").unwrap(), Vec::<f64>::new());
5330 }
5331
5332 #[test]
5333 fn jitter_start_zero_is_the_unperturbed_base_and_is_deterministic() {
5334 let base = vec![1.0, -2.0, 0.0];
5335 assert_eq!(jitter(&base, 0.1, 0), base);
5337 let a = jitter(&base, 0.1, 1);
5339 let b = jitter(&base, 0.1, 1);
5340 assert_eq!(a, b);
5341 assert_ne!(a, base);
5342 for (j, (&p, &x)) in a.iter().zip(&base).enumerate() {
5343 let bound = 0.1 * (x.abs() + 1.0);
5344 assert!(
5345 (p - x).abs() <= bound + 1e-12,
5346 "component {j} moved {} > bound {bound}",
5347 (p - x).abs()
5348 );
5349 }
5350 assert_ne!(jitter(&base, 0.1, 1), jitter(&base, 0.1, 2));
5352 }
5353
5354 #[test]
5355 fn sample_start_draws_inside_finite_boxes_and_jitters_unbounded() {
5356 let base = vec![1.0, 1.0, 0.5];
5357 let lo = vec![0.0, 0.0, -1.0];
5359 let hi = vec![2.0, f64::INFINITY, 1.0];
5360 let b = Some((lo.as_slice(), hi.as_slice()));
5361 assert_eq!(sample_start(&base, b, 0.1, 0), base);
5363 for k in 1..50 {
5364 let s = sample_start(&base, b, 0.1, k);
5365 assert!((0.0..=2.0).contains(&s[0]), "var0 {} out of [0,2]", s[0]);
5367 assert!((-1.0..=1.0).contains(&s[2]), "var2 {} out of [-1,1]", s[2]);
5368 let bound = 0.1 * (base[1].abs() + 1.0);
5370 assert!(
5371 (s[1] - base[1]).abs() <= bound + 1e-12,
5372 "var1 jitter exceeded"
5373 );
5374 }
5375 assert_eq!(
5377 sample_start(&base, b, 0.1, 7),
5378 sample_start(&base, b, 0.1, 7)
5379 );
5380 }
5381
5382 #[test]
5383 fn path_completion_lists_matching_files_with_dir_prefix() {
5384 let dir = std::env::temp_dir().join("pounce_dbg_complete_test");
5385 let _ = std::fs::remove_dir_all(&dir);
5386 std::fs::create_dir_all(&dir).unwrap();
5387 std::fs::write(dir.join("starts.txt"), "0,0\n").unwrap();
5388 std::fs::write(dir.join("start2.txt"), "1,1\n").unwrap();
5389 std::fs::write(dir.join("other.json"), "{}").unwrap();
5390 std::fs::create_dir_all(dir.join("subdir")).unwrap();
5391
5392 let p = dir.to_string_lossy().to_string();
5393 let mut got = path_candidates(&format!("{p}/start"));
5395 got.sort();
5396 assert_eq!(
5397 got,
5398 vec![format!("{p}/start2.txt"), format!("{p}/starts.txt")]
5399 );
5400 let got = path_candidates(&format!("{p}/sub"));
5402 assert_eq!(got, vec![format!("{p}/subdir/")]);
5403 assert_eq!(path_candidates(&format!("{p}/")).len(), 4);
5405 assert!(completion_candidates(None, "load", &format!("{p}/star"))
5407 .iter()
5408 .all(|c| c.contains("start")));
5409
5410 let _ = std::fs::remove_dir_all(&dir);
5411 }
5412}