codescout 0.15.0

High-performance coding agent toolkit MCP server
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Output formatter for the CLI. Pretty by default, JSON via `--json`.

use anyhow::Result;
use serde_json::Value;
use std::io::{IsTerminal, Write};

#[derive(Debug, Clone, Copy, Default)]
pub struct OutputOpts {
    pub json: bool,
    pub no_color: bool,
}

/// Resolve `--no-color` based on stdout capability when not explicitly set.
fn effective_no_color(opts: &OutputOpts) -> bool {
    opts.no_color || !std::io::stdout().is_terminal()
}

pub(crate) enum Shape {
    FindResult,
    GetResult,
    GraphResult,
    StateAtResult,
    EventList,
    StaleList,
    WriteAck,
    Unknown,
}

pub(crate) fn infer_shape(v: &Value) -> Shape {
    if v.is_string() && v.as_str() == Some("ok") {
        return Shape::WriteAck;
    }
    // artifact_event(list) returns a bare JSON array — disambiguate by
    // checking first item for event-shaped keys.
    if let Some(arr) = v.as_array() {
        if let Some(first) = arr.first() {
            if first.get("kind").is_some() && first.get("created_at").is_some() {
                return Shape::EventList;
            }
        }
    }
    if let Some(obj) = v.as_object() {
        // artifact_refresh(list_stale) returns {count, threshold_hours, items, next_step}.
        // Match on `threshold_hours` + `items` before the generic FindResult
        // check below (FindResult uses `items` + `total`).
        if obj.contains_key("threshold_hours") && obj.contains_key("items") {
            return Shape::StaleList;
        }
        if obj.contains_key("items") && obj.contains_key("total") {
            // could be FindResult or EventList — disambiguate on shape of items
            if let Some(first) = obj
                .get("items")
                .and_then(|i| i.as_array())
                .and_then(|a| a.first())
            {
                if first.get("kind").is_some() && first.get("created_at").is_some() {
                    return Shape::EventList;
                }
            }
            return Shape::FindResult;
        }
        if obj.contains_key("nodes") && obj.contains_key("edges") {
            return Shape::GraphResult;
        }
        if obj.contains_key("as_of") && obj.contains_key("status_at_as_of") {
            return Shape::StateAtResult;
        }
        if obj.contains_key("id") && obj.contains_key("body") {
            return Shape::GetResult;
        }
    }
    Shape::Unknown
}

/// Print to stdout — main entrypoint used by every verb after a tool call.
pub fn print(value: &Value, opts: &OutputOpts) -> Result<()> {
    let stdout = std::io::stdout();
    let mut h = stdout.lock();
    write_value(value, opts, &mut h)
}

pub(crate) fn write_value<W: Write>(value: &Value, opts: &OutputOpts, w: &mut W) -> Result<()> {
    let no_color = effective_no_color(opts);
    if opts.json {
        serde_json::to_writer_pretty(&mut *w, value)?;
        writeln!(w)?;
        return Ok(());
    }
    match infer_shape(value) {
        Shape::WriteAck => write_ack(value, no_color, w),
        Shape::FindResult => write_find_table(value, no_color, w),
        Shape::GetResult => write_get_summary(value, no_color, w),
        Shape::GraphResult => write_graph_tree(value, no_color, w),
        Shape::StateAtResult => write_state_summary(value, no_color, w),
        Shape::EventList => write_event_list(value, no_color, w),
        Shape::StaleList => write_stale_list(value, no_color, w),
        // Other pretty branches land in later tasks. Until then, fall back
        // to JSON for everything else so output is never silent.
        _ => fallback_json(value, w),
    }
}

fn write_ack<W: Write>(value: &Value, _no_color: bool, w: &mut W) -> Result<()> {
    // "ok" string → "ok". Object with {"ok":true, "id":...} → "ok: created <id>".
    if let Some(obj) = value.as_object() {
        if let Some(id) = obj.get("id").and_then(|v| v.as_str()) {
            writeln!(w, "ok: {id}")?;
            return Ok(());
        }
    }
    writeln!(w, "ok")?;
    Ok(())
}

fn fallback_json<W: Write>(value: &Value, w: &mut W) -> Result<()> {
    serde_json::to_writer_pretty(&mut *w, value)?;
    writeln!(w)?;
    Ok(())
}

fn write_find_table<W: Write>(value: &Value, _no_color: bool, w: &mut W) -> Result<()> {
    let items = value.get("items").and_then(|v| v.as_array());
    let Some(items) = items else {
        return fallback_json(value, w);
    };
    if items.is_empty() {
        writeln!(w, "(no results)")?;
        return Ok(());
    }
    // Compute column widths from the data so each row aligns.
    let mut widths = [8usize, 7, 7, 40]; // id, kind, status, title (rel_path follows wrapped)
    for it in items {
        let id = it.get("id").and_then(|v| v.as_str()).unwrap_or("");
        let kind = it.get("kind").and_then(|v| v.as_str()).unwrap_or("");
        let status = it.get("status").and_then(|v| v.as_str()).unwrap_or("");
        let title = it.get("title").and_then(|v| v.as_str()).unwrap_or("");
        widths[0] = widths[0].max(id.len());
        widths[1] = widths[1].max(kind.len());
        widths[2] = widths[2].max(status.len());
        widths[3] = widths[3].max(title.len()).min(60);
    }
    writeln!(
        w,
        "{:<w0$}  {:<w1$}  {:<w2$}  {:<w3$}  rel_path",
        "id",
        "kind",
        "status",
        "title",
        w0 = widths[0],
        w1 = widths[1],
        w2 = widths[2],
        w3 = widths[3]
    )?;
    for it in items {
        let id = it.get("id").and_then(|v| v.as_str()).unwrap_or("");
        let kind = it.get("kind").and_then(|v| v.as_str()).unwrap_or("");
        let status = it.get("status").and_then(|v| v.as_str()).unwrap_or("");
        let title = it.get("title").and_then(|v| v.as_str()).unwrap_or("");
        let rel_path = it.get("rel_path").and_then(|v| v.as_str()).unwrap_or("");
        let title_trunc = if title.len() > widths[3] {
            format!("{}", &title[..widths[3].saturating_sub(1)])
        } else {
            title.to_string()
        };
        writeln!(
            w,
            "{:<w0$}  {:<w1$}  {:<w2$}  {:<w3$}  {}",
            id,
            kind,
            status,
            title_trunc,
            rel_path,
            w0 = widths[0],
            w1 = widths[1],
            w2 = widths[2],
            w3 = widths[3]
        )?;
    }
    if let Some(total) = value.get("total").and_then(|v| v.as_u64()) {
        if (total as usize) > items.len() {
            writeln!(
                w,
                "\nShowing {} of {} — narrow with --kind / --tag / --filter, or paginate with --offset.",
                items.len(),
                total
            )?;
        }
    }
    Ok(())
}

fn write_get_summary<W: Write>(value: &Value, _no_color: bool, w: &mut W) -> Result<()> {
    let id = value.get("id").and_then(|v| v.as_str()).unwrap_or("?");
    let title = value
        .get("title")
        .and_then(|v| v.as_str())
        .unwrap_or("(untitled)");
    let kind = value.get("kind").and_then(|v| v.as_str()).unwrap_or("?");
    let status = value.get("status").and_then(|v| v.as_str()).unwrap_or("?");
    writeln!(w, "{title}  [{kind}/{status}]  {id}")?;
    if let Some(path) = value.get("abs_path").and_then(|v| v.as_str()) {
        writeln!(w, "{path}")?;
    }
    writeln!(w)?;
    if let Some(body) = value.get("body").and_then(|v| v.as_str()) {
        writeln!(w, "{body}")?;
    } else {
        // No body field — print the whole JSON as a fallback so users still see the data.
        fallback_json(value, w)?;
    }
    Ok(())
}

fn write_graph_tree<W: Write>(value: &Value, _no_color: bool, w: &mut W) -> Result<()> {
    let nodes = value
        .get("nodes")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    let edges = value
        .get("edges")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();

    if nodes.is_empty() {
        writeln!(w, "(empty graph)")?;
        return Ok(());
    }
    let id_to_title: std::collections::HashMap<String, String> = nodes
        .iter()
        .filter_map(|n| {
            let id = n.get("id")?.as_str()?.to_string();
            let title = n
                .get("title")
                .and_then(|v| v.as_str())
                .unwrap_or("(untitled)")
                .to_string();
            Some((id, title))
        })
        .collect();

    let root = nodes
        .first()
        .and_then(|n| n.get("id"))
        .and_then(|v| v.as_str())
        .unwrap_or("?");
    writeln!(
        w,
        "{}{}",
        root,
        id_to_title.get(root).cloned().unwrap_or_default()
    )?;
    for e in &edges {
        let src = e.get("src_id").and_then(|v| v.as_str()).unwrap_or("");
        let dst = e.get("dst_id").and_then(|v| v.as_str()).unwrap_or("");
        let rel = e.get("rel").and_then(|v| v.as_str()).unwrap_or("?");
        let other = if src == root { dst } else { src };
        let title = id_to_title.get(other).cloned().unwrap_or_default();
        let arrow = if src == root { "" } else { "" };
        writeln!(w, "  {arrow} [{rel}] {other}{title}")?;
    }
    Ok(())
}

fn write_state_summary<W: Write>(value: &Value, _no_color: bool, w: &mut W) -> Result<()> {
    let title = value
        .get("frontmatter")
        .and_then(|fm| fm.get("title"))
        .and_then(|v| v.as_str())
        .unwrap_or("(untitled)");
    let status_at = value
        .get("status_at_as_of")
        .and_then(|v| v.as_str())
        .unwrap_or("?");
    let freshness_now = value
        .get("freshness_now")
        .and_then(|v| v.as_str())
        .unwrap_or("?");

    writeln!(w, "{title}")?;
    if let Some(as_of) = value.get("as_of").and_then(|v| v.as_i64()) {
        writeln!(w, "  as_of (ms):       {as_of}")?;
    } else if let Some(as_of) = value.get("as_of").and_then(|v| v.as_str()) {
        writeln!(w, "  as_of:            {as_of}")?;
    } else {
        writeln!(w, "  as_of:            ?")?;
    }
    writeln!(w, "  status_at_as_of:  {status_at}")?;
    writeln!(w, "  freshness_now:    {freshness_now}")?;
    if let Some(chain) = value.get("supersession_chain").and_then(|v| v.as_array()) {
        if !chain.is_empty() {
            writeln!(w, "  supersession_chain ({}):", chain.len())?;
            for item in chain {
                if let Some(id) = item.get("id").and_then(|v| v.as_str()) {
                    writeln!(w, "    - {id}")?;
                }
            }
        }
    }
    Ok(())
}

fn write_event_list<W: Write>(value: &Value, _no_color: bool, w: &mut W) -> Result<()> {
    // The librarian `artifact_event(action="list")` tool returns a bare JSON
    // array of event rows. Accept either a top-level array or an object with
    // an `items` array (defensive, in case the envelope shape evolves).
    let items: &Vec<Value> = if let Some(arr) = value.as_array() {
        arr
    } else if let Some(arr) = value.get("items").and_then(|v| v.as_array()) {
        arr
    } else {
        return fallback_json(value, w);
    };
    if items.is_empty() {
        writeln!(w, "(no events)")?;
        return Ok(());
    }
    writeln!(
        w,
        "{:<16}  {:<16}  {:<12}  payload",
        "created_at", "kind", "author"
    )?;
    for it in items {
        let created = it.get("created_at").and_then(|v| v.as_i64()).unwrap_or(0);
        let kind = it.get("kind").and_then(|v| v.as_str()).unwrap_or("?");
        let author = it.get("author").and_then(|v| v.as_str()).unwrap_or("");
        let payload = it
            .get("payload")
            .map(|v| {
                let s = serde_json::to_string(v).unwrap_or_default();
                if s.len() > 80 {
                    format!("{}", &s[..79])
                } else {
                    s
                }
            })
            .unwrap_or_default();
        writeln!(
            w,
            "{:<16}  {:<16}  {:<12}  {}",
            created, kind, author, payload
        )?;
    }
    Ok(())
}

fn write_stale_list<W: Write>(value: &Value, _no_color: bool, w: &mut W) -> Result<()> {
    // The librarian `artifact_refresh(action="list_stale")` tool returns
    // `{count, threshold_hours, items, next_step}` where each item carries
    // `{id, kind, title, abs_path, last_refreshed_at, refresh_count, age_hours}`.
    // Accept either the canonical envelope or a bare-array shape so the
    // renderer is forgiving if the envelope evolves.
    let items: &Vec<Value> = if let Some(arr) = value.as_array() {
        arr
    } else if let Some(arr) = value.get("items").and_then(|v| v.as_array()) {
        arr
    } else {
        return fallback_json(value, w);
    };
    if items.is_empty() {
        writeln!(w, "(no stale artifacts)")?;
        return Ok(());
    }
    writeln!(w, "{:<18}  {:>9}  {:<8}  title", "id", "age_hours", "kind")?;
    for it in items {
        let id = it.get("id").and_then(|v| v.as_str()).unwrap_or("");
        let kind = it.get("kind").and_then(|v| v.as_str()).unwrap_or("");
        let age = it
            .get("age_hours")
            .and_then(|v| v.as_i64())
            .map(|h| h.to_string())
            .unwrap_or_else(|| "never".to_string());
        let title = it
            .get("title")
            .and_then(|v| v.as_str())
            .unwrap_or("(untitled)");
        writeln!(w, "{id:<18}  {age:>9}  {kind:<8}  {title}")?;
    }
    Ok(())
}

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

    #[test]
    fn json_mode_emits_pretty_json() {
        let v = json!({"items": [{"id": "abc", "title": "t"}]});
        let mut buf = Vec::new();
        write_value(
            &v,
            &OutputOpts {
                json: true,
                no_color: true,
            },
            &mut buf,
        )
        .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(s.contains("\"items\""));
        assert!(s.ends_with('\n'));
    }

    #[test]
    fn unknown_shape_falls_back_to_json() {
        // A bare string the shape inferrer doesn't recognise.
        let v = json!("ok");
        let mut buf = Vec::new();
        write_value(
            &v,
            &OutputOpts {
                json: false,
                no_color: true,
            },
            &mut buf,
        )
        .unwrap();
        let s = String::from_utf8(buf).unwrap();
        // Either "ok" recognised as a WriteAck or fallback JSON — both must include "ok".
        assert!(s.contains("ok"), "expected 'ok' in output; got: {s}");
    }

    #[test]
    fn infer_shape_recognises_find_result() {
        let v = json!({"items": [{"id":"a"}], "total": 1});
        assert!(matches!(infer_shape(&v), Shape::FindResult));
    }

    #[test]
    fn infer_shape_unknown_for_arbitrary_object() {
        let v = json!({"weird": "shape"});
        assert!(matches!(infer_shape(&v), Shape::Unknown));
    }

    #[test]
    fn infer_shape_recognises_state_at_result() {
        let v = json!({
            "as_of": 1_700_000_000_000_i64,
            "status_at_as_of": "active",
            "frontmatter": {"title": "Test"}
        });
        assert!(matches!(infer_shape(&v), Shape::StateAtResult));
    }

    #[test]
    fn pretty_event_list_renders_header_and_rows() {
        // Real envelope: bare JSON array of event rows with keys matching
        // librarian's timeline::call output (id, kind, payload, created_at, author).
        let v = json!([
            {
                "id": "ev-1",
                "kind": "note",
                "payload": {"text": "hello"},
                "created_at": 1_700_000_000_000_i64,
                "author": "alice"
            }
        ]);
        let mut buf = Vec::new();
        write_value(
            &v,
            &OutputOpts {
                json: false,
                no_color: true,
            },
            &mut buf,
        )
        .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(
            s.lines()
                .any(|l| l.contains("created_at") && l.contains("kind")),
            "expected header; got: {s}"
        );
        assert!(s.contains("note"), "expected event kind; got: {s}");
        assert!(s.contains("1700000000000"), "expected timestamp; got: {s}");
    }

    #[test]
    fn pretty_stale_list_renders_header_and_rows() {
        // Real envelope shape from `artifact_refresh::call(action=list_stale)`:
        // `{count, threshold_hours, items, next_step}` with per-item keys
        // `{id, kind, title, abs_path, last_refreshed_at, refresh_count, age_hours}`.
        let v = json!({
            "count": 1,
            "threshold_hours": 24,
            "items": [
                {
                    "id": "abc123",
                    "kind": "spec",
                    "title": "Old Spec",
                    "abs_path": "/tmp/old.md",
                    "last_refreshed_at": "2026-04-01T00:00:00Z",
                    "refresh_count": 2,
                    "age_hours": 999
                }
            ],
            "next_step": "Call artifact_refresh(id) on each item …"
        });
        let mut buf = Vec::new();
        write_value(
            &v,
            &OutputOpts {
                json: false,
                no_color: true,
            },
            &mut buf,
        )
        .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(
            s.lines()
                .any(|l| l.contains("id") && l.contains("age_hours") && l.contains("title")),
            "expected header; got: {s}"
        );
        assert!(s.contains("abc123"), "expected id; got: {s}");
        assert!(s.contains("Old Spec"), "expected title; got: {s}");
        assert!(s.contains("999"), "expected age_hours rendered; got: {s}");
    }

    #[test]
    fn infer_shape_recognises_stale_list() {
        let v = json!({
            "count": 0,
            "threshold_hours": 24,
            "items": [],
            "next_step": "No stale augmented artifacts in scope."
        });
        assert!(matches!(infer_shape(&v), Shape::StaleList));
    }

    #[test]
    fn infer_shape_recognises_event_list_bare_array() {
        let v = json!([
            {"id": "ev-1", "kind": "note", "created_at": 1_700_000_000_000_i64, "payload": null}
        ]);
        assert!(matches!(infer_shape(&v), Shape::EventList));
    }

    #[test]
    fn pretty_find_result_renders_table_with_id_kind_status_title() {
        let v = json!({
            "items": [
                {"id":"abcd1234","kind":"tracker","status":"active","title":"Ship Feature X","rel_path":"docs/trackers/x.md"},
                {"id":"bbbb5678","kind":"spec","status":"draft","title":"Design Y","rel_path":"docs/specs/y.md"}
            ],
            "total": 2
        });
        let mut buf = Vec::new();
        write_value(
            &v,
            &OutputOpts {
                json: false,
                no_color: true,
            },
            &mut buf,
        )
        .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(s.contains("abcd1234"), "row 1 id missing; got: {s}");
        assert!(
            s.contains("Ship Feature X"),
            "row 1 title missing; got: {s}"
        );
        assert!(
            s.contains("docs/specs/y.md"),
            "row 2 rel_path missing; got: {s}"
        );
        // The "kind | status" axis should be visible regardless of exact framing.
        assert!(s.contains("tracker"));
        assert!(s.contains("draft"));
        // The table must NOT be JSON — assert the column-header line is present
        // so a JSON fallback would visibly fail this test.
        assert!(
            s.lines().any(|line| line.contains("id")
                && line.contains("kind")
                && line.contains("status")
                && line.contains("title")),
            "expected a table header line; got: {s}"
        );
    }
}