dbg-cli 0.3.3

A universal debugger CLI that lets AI agents observe runtime state instead of guessing from source code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::sync::OnceLock;

use regex::Regex;
use serde_json::{Map, Value};

use super::canonical::{BreakLoc, CanonicalOps, HitEvent};
use super::{Backend, CleanResult, Dependency, DependencyCheck, SpawnConfig};

pub struct OcamlDebugBackend;

impl Backend for OcamlDebugBackend {
    fn name(&self) -> &'static str {
        "ocamldebug"
    }

    fn description(&self) -> &'static str {
        "OCaml bytecode debugger"
    }

    fn types(&self) -> &'static [&'static str] {
        &["ocaml", "ml"]
    }

    fn spawn_config(&self, target: &str, args: &[String]) -> anyhow::Result<SpawnConfig> {
        let mut spawn_args = vec![target.into()];
        if !args.is_empty() {
            spawn_args.extend(args.iter().cloned());
        }

        Ok(SpawnConfig {
            bin: "ocamldebug".into(),
            args: spawn_args,
            env: vec![],
            init_commands: vec![],
        })
    }

    fn prompt_pattern(&self) -> &str {
        r"\(ocd\) "
    }

    fn dependencies(&self) -> Vec<Dependency> {
        vec![Dependency {
            name: "ocamldebug",
            check: DependencyCheck::Binary {
                name: "ocamldebug",
                alternatives: &["ocamldebug"],
                version_cmd: Some(("ocaml", &["-version"])),
            },
            install: "opam install ocaml  # or: sudo apt install ocaml-interp",
        }]
    }

    fn format_breakpoint(&self, spec: &str) -> String {
        // Support "Module line" -> "break @ Module line"
        // and bare "line" -> "break @ line"
        // and "functionName" -> "break functionName"
        let trimmed = spec.trim();
        if trimmed.chars().next().is_some_and(|c| c.is_ascii_digit()) {
            // Bare line number
            format!("break @ {trimmed}")
        } else if trimmed.contains(' ') {
            // "Module line" or "Module line col"
            format!("break @ {trimmed}")
        } else if trimmed.contains(':') {
            // "Module:line" convenience syntax -> "Module line"
            let parts: Vec<&str> = trimmed.splitn(2, ':').collect();
            format!("break @ {} {}", parts[0], parts[1])
        } else {
            // Function name
            format!("break {trimmed}")
        }
    }

    fn run_command(&self) -> &'static str {
        "run"
    }

    fn quit_command(&self) -> &'static str {
        "quit"
    }

    fn parse_help(&self, raw: &str) -> String {
        // ocamldebug help is a flat "List of commands:" followed by space-separated words
        let mut cmds: Vec<String> = Vec::new();
        let text = raw
            .strip_prefix("List of commands:")
            .unwrap_or(raw);
        for tok in text.split_whitespace() {
            if tok.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
                && tok.len() > 1
                && tok.len() < 25
            {
                cmds.push(tok.to_string());
            }
        }
        cmds.sort();
        cmds.dedup();
        format!("ocamldebug: {}", cmds.join(", "))
    }

    fn adapters(&self) -> Vec<(&'static str, &'static str)> {
        vec![("ocaml.md", include_str!("../../skills/adapters/ocaml.md"))]
    }

    fn canonical_ops(&self) -> Option<&dyn CanonicalOps> { Some(self) }

    fn clean(&self, cmd: &str, output: &str) -> CleanResult {
        let trimmed = cmd.trim();
        let mut events = Vec::new();
        let mut lines: Vec<String> = Vec::new();

        for line in output.lines() {
            let l = line.trim();

            // Extract stop events (breakpoint hits, time positions)
            if l.starts_with("Time:") || l.starts_with("Time :") {
                events.push(l.to_string());
            }

            // Extract breakpoint hit events
            if l.starts_with("Breakpoint:") {
                events.push(l.to_string());
                continue; // redundant with Time: line
            }

            // Extract breakpoint-set confirmations
            if l.starts_with("Breakpoint ") && l.contains("at") {
                events.push(l.to_string());
            }

            // Filter loading/startup noise
            if l.starts_with("Loading program")
                || l.starts_with("Waiting for connection")
                || l == "Position out of range."
            {
                continue;
            }

            // For backtrace, filter internal frames
            if (trimmed == "bt" || trimmed == "backtrace")
                && (l.contains("Debugger") || l.contains("ocamldebug"))
            {
                continue;
            }

            // Clean <|b|> markers (active expression indicators) -> visible marker
            let cleaned = line.replace("<|b|>", ">>> ").replace("<|e|>", "");
            lines.push(cleaned);
        }

        CleanResult {
            output: lines.join("\n"),
            events,
        }
    }
}

/// Extract an OCaml module name from a file path.
/// `"/path/to/algos.ml"` → `"Algos"` (capitalised stem).
fn module_from_path(path: &str) -> String {
    let stem = std::path::Path::new(path)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(path);
    let mut chars = stem.chars();
    match chars.next() {
        Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
        None => stem.to_string(),
    }
}

impl CanonicalOps for OcamlDebugBackend {
    fn tool_name(&self) -> &'static str { "ocamldebug" }
    fn auto_capture_locals(&self) -> bool { false }

    fn op_break(&self, loc: &BreakLoc) -> anyhow::Result<String> {
        Ok(match loc {
            BreakLoc::FileLine { file, line } => {
                let module = module_from_path(file);
                format!("break @ {module} {line}")
            }
            BreakLoc::Fqn(name) => format!("break {name}"),
            BreakLoc::ModuleMethod { module, method: _ } => format!("break @ {module}"),
        })
    }
    fn op_run(&self, _args: &[String]) -> anyhow::Result<String> { Ok("run".into()) }
    fn op_continue(&self) -> anyhow::Result<String> { Ok("run".into()) }
    fn op_step(&self) -> anyhow::Result<String> { Ok("step".into()) }
    fn op_next(&self) -> anyhow::Result<String> { Ok("next".into()) }
    fn op_finish(&self) -> anyhow::Result<String> { Ok("finish".into()) }
    fn op_stack(&self, n: Option<u32>) -> anyhow::Result<String> {
        Ok(match n {
            Some(k) => format!("bt {k}"),
            None => "bt".into(),
        })
    }
    fn op_frame(&self, n: u32) -> anyhow::Result<String> { Ok(format!("frame {n}")) }
    fn op_locals(&self) -> anyhow::Result<String> {
        // ocamldebug genuinely has no "enumerate local bindings" command:
        // `info` has no `locals`/`variables` subcommand, and `print` with
        // no argument just reprints the last value ("print" alone returns
        // blank on a fresh stop). The closest native signal is `frame`,
        // which reports the current function symbol and source line; we
        // emit that here as a best-effort "where am I" reply so agents
        // aren't left with an empty string. For real value inspection
        // agents must fall back to `dbg print <name>` per binding —
        // that's how ocamldebug is designed to be driven.
        Ok("frame".into())
    }
    fn op_print(&self, expr: &str) -> anyhow::Result<String> { Ok(format!("print {expr}")) }
    fn op_list(&self, _loc: Option<&str>) -> anyhow::Result<String> { Ok("list".into()) }
    fn op_breaks(&self) -> anyhow::Result<String> {
        // ocamldebug's breakpoint list lives under `info breakpoints`;
        // the default `breakpoint list` that the canonical trait emits
        // is parsed as a module/function name and returns "Unknown
        // command." for the second token.
        Ok("info breakpoints".into())
    }

    fn parse_hit(&self, output: &str) -> Option<HitEvent> {
        // ocamldebug stop output:
        //   Time: 19 - pc: 0:144156 - module Algos
        //   Breakpoint: 1
        //   16   let next = !a + !b in    (* ← fibonacci hot line *)
        //
        // We match the "Time: N - pc: ... - module M" line to extract
        // the module, then look for an indented source line "NN  ..." to
        // get the line number.
        static TIME_RE: OnceLock<Regex> = OnceLock::new();
        static SRC_RE: OnceLock<Regex> = OnceLock::new();
        let time_re = TIME_RE.get_or_init(|| {
            Regex::new(r"Time:\s*\d+\s*-\s*pc:\s*[\d:]+\s*-\s*module\s+(\S+)").unwrap()
        });
        let src_re = SRC_RE.get_or_init(|| {
            Regex::new(r"^\s*(\d+)\s+").unwrap()
        });

        let mut module = None;
        let mut line_no = None;
        for line in output.lines() {
            if module.is_none() {
                if let Some(c) = time_re.captures(line) {
                    module = Some(c[1].to_string());
                }
            } else if line_no.is_none() {
                // After the Time line, look for a source line number
                // (skip "Breakpoint: N" lines).
                let trimmed = line.trim();
                if trimmed.starts_with("Breakpoint:") {
                    continue;
                }
                if let Some(c) = src_re.captures(trimmed) {
                    line_no = c[1].parse::<u32>().ok();
                }
            }
        }

        let m = module?;
        let ln = line_no?;
        // Use lowercase_module.ml:line as the location_key so the
        // stem matcher (strips extension → "algos:16") can fuzzy-match
        // against the absolute path the agent passes to `dbg hits`.
        let file = format!("{}.ml", m.to_lowercase());
        Some(HitEvent {
            location_key: format!("{file}:{ln}"),
            thread: None,
            frame_symbol: Some(m),
            file: Some(file),
            line: Some(ln),
        })
    }

    fn parse_locals(&self, output: &str) -> Option<Value> {
        // ocamldebug `print` shows the most recent value:
        //   x : int = 42
        // Best-effort: single name = value pair.
        let mut obj = Map::new();
        for line in output.lines() {
            let line = line.trim();
            if let Some(eq_pos) = line.find('=') {
                let left = line[..eq_pos].trim();
                let value = line[eq_pos + 1..].trim().to_string();
                // left is "name : type", extract just the name
                let name = left.split(':').next().unwrap_or(left).trim();
                if !name.is_empty() {
                    obj.insert(
                        name.to_string(),
                        serde_json::json!({ "value": value }),
                    );
                }
            }
        }
        if obj.is_empty() { None } else { Some(Value::Object(obj)) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_breakpoint_line() {
        assert_eq!(OcamlDebugBackend.format_breakpoint("42"), "break @ 42");
    }

    #[test]
    fn format_breakpoint_module_line() {
        assert_eq!(
            OcamlDebugBackend.format_breakpoint("Parser 42"),
            "break @ Parser 42"
        );
    }

    #[test]
    fn format_breakpoint_module_colon_line() {
        assert_eq!(
            OcamlDebugBackend.format_breakpoint("Parser:42"),
            "break @ Parser 42"
        );
    }

    #[test]
    fn format_breakpoint_function() {
        assert_eq!(
            OcamlDebugBackend.format_breakpoint("parse_expr"),
            "break parse_expr"
        );
    }

    #[test]
    fn clean_extracts_time_events() {
        let input = "Time: 21 - pc: 0:42756 - module Parser\nval x : int = 42";
        let r = OcamlDebugBackend.clean("step", input);
        assert!(r.events.iter().any(|e| e.contains("Time:")));
        assert!(r.output.contains("val x"));
    }

    #[test]
    fn clean_filters_loading_noise() {
        let input = "Loading program ./my_program\nactual output";
        let r = OcamlDebugBackend.clean("run", input);
        assert!(!r.output.contains("Loading program"));
        assert!(r.output.contains("actual output"));
    }

    #[test]
    fn clean_passthrough_normal() {
        let input = "x : int = 42";
        let r = OcamlDebugBackend.clean("print x", input);
        assert_eq!(r.output.trim(), "x : int = 42");
        assert!(r.events.is_empty());
    }

    #[test]
    fn clean_replaces_markers() {
        let input = "2   <|b|>if n = 0 then 1";
        let r = OcamlDebugBackend.clean("step", input);
        assert!(r.output.contains(">>> if n = 0"));
        assert!(!r.output.contains("<|b|>"));
    }

    #[test]
    fn clean_filters_position_out_of_range() {
        let input = "1 let x = 42\nPosition out of range.";
        let r = OcamlDebugBackend.clean("list", input);
        assert!(!r.output.contains("Position out of range"));
        assert!(r.output.contains("let x = 42"));
    }

    #[test]
    fn clean_extracts_breakpoint_hit() {
        let input = "Time: 19 - pc: 0:144156 - module Test\nBreakpoint: 1\n2   <|b|>if n = 0 then 1";
        let r = OcamlDebugBackend.clean("run", input);
        assert!(r.events.iter().any(|e| e.contains("Breakpoint: 1")));
        assert!(!r.output.contains("Breakpoint:"));
    }

    #[test]
    fn spawn_config_basic() {
        let cfg = OcamlDebugBackend
            .spawn_config("./my_program", &[])
            .unwrap();
        assert_eq!(cfg.bin, "ocamldebug");
        assert!(cfg.args.contains(&"./my_program".to_string()));
    }

    #[test]
    fn spawn_config_with_args() {
        let cfg = OcamlDebugBackend
            .spawn_config("./my_program", &["arg1".into(), "arg2".into()])
            .unwrap();
        assert!(cfg.args.contains(&"arg1".to_string()));
        assert!(cfg.args.contains(&"arg2".to_string()));
    }

    #[test]
    fn prompt_pattern_matches() {
        let re = regex::Regex::new(OcamlDebugBackend.prompt_pattern()).unwrap();
        assert!(re.is_match("(ocd) "));
    }

    #[test]
    fn parse_hit_from_time_line() {
        let out = "Time: 19 - pc: 0:144156 - module Algos\nBreakpoint: 1\n16   let next = !a + !b in    (* ← fibonacci hot line *)";
        let hit = OcamlDebugBackend.parse_hit(out).expect("should parse");
        assert_eq!(hit.location_key, "algos.ml:16");
        assert_eq!(hit.line, Some(16));
        assert_eq!(hit.frame_symbol.as_deref(), Some("Algos"));
    }

    #[test]
    fn parse_hit_none_on_program_exit() {
        let out = "Time: 705\nProgram exit.";
        assert!(OcamlDebugBackend.parse_hit(out).is_none());
    }

    #[test]
    fn module_from_path_capitalises_stem() {
        assert_eq!(module_from_path("/path/to/algos.ml"), "Algos");
        assert_eq!(module_from_path("parser.ml"), "Parser");
    }

    #[test]
    fn canonical_ops_returns_self() {
        let b: Box<dyn Backend> = Box::new(OcamlDebugBackend);
        assert_eq!(b.canonical_ops().unwrap().tool_name(), "ocamldebug");
    }

    #[test]
    fn parse_help_extracts_commands() {
        let raw = "List of commands: cd complete pwd directory kill pid address help quit shell\nenvironment run reverse step backstep goto finish next start previous print\ndisplay source break delete set show info frame backtrace bt up down last\nlist load_printer install_printer remove_printer";
        let result = OcamlDebugBackend.parse_help(raw);
        assert!(result.contains("backtrace"));
        assert!(result.contains("break"));
        assert!(result.contains("backstep"));
        assert!(result.contains("reverse"));
        assert!(result.contains("run"));
        assert!(result.contains("goto"));
        assert!(result.contains("print"));
    }
}