codescout 0.14.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
use anyhow::Result;

use super::artifact::{row_from_sql, ArtifactRow};
use super::Catalog;
use crate::librarian::filter::{compile, FilterNode};

pub struct FindOpts {
    pub filter: Option<FilterNode>,
    pub limit: usize,
    pub offset: usize,
    /// Pre-computed embedding vector for semantic KNN search.
    /// When Some, results are sorted by cosine distance (closest first)
    /// rather than updated_at. Filter AST is still applied as a post-filter
    /// on the top-K candidates.
    pub semantic: Option<Vec<f32>>,
}

pub fn find(cat: &Catalog, opts: &FindOpts) -> Result<Vec<ArtifactRow>> {
    if let Some(ref vec) = opts.semantic {
        return find_semantic(cat, opts, vec);
    }

    let mut sql = String::from(
        "SELECT id, abs_path, kind, status, title, owners, tags,\
         topic, time_scope, source, created_at, updated_at, file_mtime,\
         file_sha256, confidence FROM artifact",
    );
    let mut params: Vec<rusqlite::types::Value> = Vec::new();
    if let Some(f) = &opts.filter {
        let frag = compile(f)?;
        sql.push_str(" WHERE ");
        sql.push_str(&frag.sql);
        params.extend(frag.params);
    }
    sql.push_str(" ORDER BY updated_at DESC LIMIT ? OFFSET ?");
    params.push(rusqlite::types::Value::Integer(opts.limit as i64));
    params.push(rusqlite::types::Value::Integer(opts.offset as i64));

    let mut stmt = cat.conn.prepare(&sql)?;
    let rows = stmt.query_map(rusqlite::params_from_iter(params.iter()), row_from_sql)?;
    rows.collect::<Result<_, _>>().map_err(Into::into)
}

/// Count of artifacts matching `filter`. Used by listing tools to generate
/// progressive-disclosure hints ("N more in repo, M more in workspace").
pub fn count_matching(cat: &Catalog, filter: Option<&FilterNode>) -> Result<usize> {
    let mut sql = String::from("SELECT COUNT(*) FROM artifact");
    let mut params: Vec<rusqlite::types::Value> = Vec::new();
    if let Some(f) = filter {
        let frag = compile(f)?;
        sql.push_str(" WHERE ");
        sql.push_str(&frag.sql);
        params.extend(frag.params);
    }
    let mut stmt = cat.conn.prepare(&sql)?;
    let n: i64 = stmt.query_row(rusqlite::params_from_iter(params.iter()), |r| r.get(0))?;
    Ok(n.max(0) as usize)
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CatalogSummary {
    pub total: usize,
    pub by_kind: std::collections::BTreeMap<String, usize>,
    pub augmented: usize,
}

/// Catalog-level summary for the given scoped filter: total non-archived
/// artifact count, count by kind, and count of augmented artifacts.
/// Caller is responsible for passing a filter that already excludes
/// archived/superseded rows if desired.
pub fn catalog_summary(
    cat: &Catalog,
    scoped_filter: Option<&FilterNode>,
) -> Result<CatalogSummary> {
    let (where_sql, params) = match scoped_filter {
        Some(f) => {
            let frag = compile(f)?;
            (format!(" WHERE {}", frag.sql), frag.params)
        }
        None => (String::new(), Vec::new()),
    };

    let mut by_kind = std::collections::BTreeMap::new();
    let mut total = 0usize;
    {
        let sql = format!(
            "SELECT kind, COUNT(*) FROM artifact{} GROUP BY kind",
            where_sql
        );
        let mut stmt = cat.conn.prepare(&sql)?;
        let rows = stmt.query_map(rusqlite::params_from_iter(params.iter()), |r| {
            Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?))
        })?;
        for row in rows {
            let (kind, count) = row?;
            let c = count.max(0) as usize;
            total += c;
            by_kind.insert(kind, c);
        }
    }

    let augmented = {
        let aug_sql = format!(
            "SELECT COUNT(*) FROM artifact_augmentation \
             WHERE artifact_id IN (SELECT id FROM artifact{})",
            where_sql
        );
        let mut stmt = cat.conn.prepare(&aug_sql)?;
        let n: i64 = stmt.query_row(rusqlite::params_from_iter(params.iter()), |r| r.get(0))?;
        n.max(0) as usize
    };

    Ok(CatalogSummary {
        total,
        by_kind,
        augmented,
    })
}

/// Two-phase semantic search with iterative K backfill:
///
/// 1. KNN query against artifact_vec to get top-K candidate ids by cosine distance.
/// 2. Fetch full artifact rows for those ids, applying the filter AST.
/// 3. If the post-filter result set is smaller than requested and K < K_CAP,
///    double K and retry (ensures selective filters still return results).
///
/// Results are returned in KNN distance order (closest first).
fn find_semantic(cat: &Catalog, opts: &FindOpts, query_vec: &[f32]) -> Result<Vec<ArtifactRow>> {
    // Encode as little-endian f32 bytes — the format vec_f32() expects.
    let blob: Vec<u8> = query_vec.iter().flat_map(|f| f.to_le_bytes()).collect();

    let target = opts.limit + opts.offset;
    let mut k = (target * 5).max(100) as i64;
    const K_CAP: i64 = 2000;

    loop {
        let knn_sql = "SELECT id FROM artifact_vec \
                       WHERE embedding MATCH vec_f32(?1) ORDER BY distance LIMIT ?2";
        let mut knn_stmt = cat.conn.prepare(knn_sql)?;
        let candidate_ids: Vec<String> = knn_stmt
            .query_map(rusqlite::params![blob, k], |row| row.get(0))?
            .collect::<Result<_, _>>()?;

        if candidate_ids.is_empty() {
            return Ok(vec![]);
        }

        // Params list: [candidate_id_0, candidate_id_1, ..., <filter params>]
        // Candidate ids occupy positions ?1..?N.
        let n = candidate_ids.len();
        let placeholders: String = (1..=n)
            .map(|i| format!("?{i}"))
            .collect::<Vec<_>>()
            .join(", ");

        // CASE WHEN id = ?1 THEN 0 WHEN id = ?2 THEN 1 ... preserves KNN order.
        let order_case: String = (0..n)
            .map(|i| format!("WHEN id = ?{} THEN {}", i + 1, i))
            .collect::<Vec<_>>()
            .join(" ");

        let mut sql = format!(
            "SELECT id, abs_path, kind, status, title, owners, tags, \
             topic, time_scope, source, created_at, updated_at, file_mtime, \
             file_sha256, confidence FROM artifact \
             WHERE id IN ({placeholders})",
        );

        let mut params: Vec<rusqlite::types::Value> = candidate_ids
            .iter()
            .map(|id| rusqlite::types::Value::Text(id.clone()))
            .collect();

        if let Some(f) = &opts.filter {
            let frag = compile(f)?;
            // Filter fragment uses ?1, ?2, ... but those are already taken by candidate ids.
            // Shift all ?N in the filter SQL by n so they start at ?(n+1).
            let shifted = shift_param_indices(&frag.sql, n);
            sql.push_str(" AND ");
            sql.push_str(&shifted);
            params.extend(frag.params);
        }

        // No LIMIT/OFFSET yet — collect all matching rows to check count before deciding
        // whether to retry with a larger K.
        sql.push_str(&format!(" ORDER BY CASE {order_case} ELSE {n} END"));

        let mut stmt = cat.conn.prepare(&sql)?;
        let all_rows: Vec<ArtifactRow> = stmt
            .query_map(rusqlite::params_from_iter(params.iter()), row_from_sql)?
            .collect::<Result<_, _>>()?;

        // If we have enough rows, or we've hit K_CAP, return the page.
        if all_rows.len() >= target || k >= K_CAP {
            return Ok(all_rows
                .into_iter()
                .skip(opts.offset)
                .take(opts.limit)
                .collect());
        }

        // Not enough results yet and we can still expand — double K and retry.
        k = (k * 2).min(K_CAP);
    }
}

/// Shift all `?N` parameter placeholders in a SQL fragment by `offset`.
/// e.g. shift_param_indices("x = ?1 AND y = ?2", 3) → "x = ?4 AND y = ?5"
fn shift_param_indices(sql: &str, offset: usize) -> String {
    // Replace ?N tokens with ?{N+offset}. Walk char-by-char to avoid regex dep.
    let mut out = String::with_capacity(sql.len() + 8);
    let mut chars = sql.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '?' {
            // Collect digits following '?'
            let mut digits = String::new();
            while chars.peek().map(|d| d.is_ascii_digit()).unwrap_or(false) {
                digits.push(chars.next().unwrap());
            }
            if digits.is_empty() {
                out.push('?');
            } else {
                let n: usize = digits.parse().unwrap_or(1);
                out.push('?');
                out.push_str(&(n + offset).to_string());
            }
        } else {
            out.push(c);
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::librarian::catalog::artifact::{self, ArtifactRow};
    use serde_json::json;

    fn art(id: &str, kind: &str, status: &str) -> ArtifactRow {
        ArtifactRow {
            id: id.into(),
            abs_path: std::path::PathBuf::from(format!("/test/{id}.md")),
            kind: kind.into(),
            status: status.into(),
            title: None,
            owners: vec![],
            tags: vec!["t".into()],
            topic: None,
            time_scope: None,
            source: None,
            created_at: 0,
            updated_at: id.chars().last().map(|c| c as i64).unwrap_or(0),
            file_mtime: 0,
            file_sha256: "x".into(),
            confidence: 1.0,
        }
    }

    #[test]
    fn find_by_kind() {
        let cat = Catalog::open_in_memory().unwrap();
        artifact::upsert(&cat, &art("a", "spec", "active")).unwrap();
        artifact::upsert(&cat, &art("b", "plan", "active")).unwrap();
        let rows = find(
            &cat,
            &FindOpts {
                filter: Some(serde_json::from_value(json!({"kind": {"eq": "spec"}})).unwrap()),
                limit: 10,
                offset: 0,
                semantic: None,
            },
        )
        .unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].id, "a");
    }

    #[test]
    fn find_with_and_composition() {
        let cat = Catalog::open_in_memory().unwrap();
        artifact::upsert(&cat, &art("a", "spec", "active")).unwrap();
        artifact::upsert(&cat, &art("b", "spec", "archived")).unwrap();
        let rows = find(
            &cat,
            &FindOpts {
                filter: Some(
                    serde_json::from_value(json!({"and": [
                        {"kind": {"eq": "spec"}},
                        {"status": {"eq": "active"}}
                    ]}))
                    .unwrap(),
                ),
                limit: 10,
                offset: 0,
                semantic: None,
            },
        )
        .unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].id, "a");
    }

    #[test]
    fn catalog_summary_counts_by_kind_and_total() {
        use crate::librarian::catalog::artifact::{upsert, ArtifactRow};
        let cat = crate::librarian::catalog::Catalog::open_in_memory().unwrap();
        let now = chrono::Utc::now().timestamp_millis();
        for (id, kind) in [("a1", "tracker"), ("a2", "tracker"), ("a3", "plan")] {
            upsert(
                &cat,
                &ArtifactRow {
                    id: id.into(),
                    abs_path: std::path::PathBuf::from(format!("/test/r/{id}.md")),
                    kind: kind.into(),
                    status: "draft".into(),
                    title: None,
                    owners: vec![],
                    tags: vec![],
                    topic: None,
                    time_scope: None,
                    source: None,
                    created_at: now,
                    updated_at: now,
                    file_mtime: now,
                    file_sha256: "".into(),
                    confidence: 1.0,
                },
            )
            .unwrap();
        }
        let s = catalog_summary(&cat, None).unwrap();
        assert_eq!(s.total, 3);
        assert_eq!(s.by_kind["tracker"], 2);
        assert_eq!(s.by_kind["plan"], 1);
        assert_eq!(s.augmented, 0);
    }

    #[test]
    fn catalog_summary_counts_augmented() {
        use crate::librarian::catalog::artifact::{upsert, ArtifactRow};
        use crate::librarian::catalog::augmentation;
        let cat = crate::librarian::catalog::Catalog::open_in_memory().unwrap();
        let now = chrono::Utc::now().timestamp_millis();
        let now_ts = chrono::Utc::now()
            .format("%Y-%m-%dT%H:%M:%S%.3fZ")
            .to_string();
        upsert(
            &cat,
            &ArtifactRow {
                id: "a1".into(),
                abs_path: std::path::PathBuf::from("/test/r/a1.md"),
                kind: "tracker".into(),
                status: "draft".into(),
                title: None,
                owners: vec![],
                tags: vec![],
                topic: None,
                time_scope: None,
                source: None,
                created_at: now,
                updated_at: now,
                file_mtime: now,
                file_sha256: "".into(),
                confidence: 1.0,
            },
        )
        .unwrap();
        upsert(
            &cat,
            &ArtifactRow {
                id: "a2".into(),
                abs_path: std::path::PathBuf::from("/test/r/a2.md"),
                kind: "plan".into(),
                status: "draft".into(),
                title: None,
                owners: vec![],
                tags: vec![],
                topic: None,
                time_scope: None,
                source: None,
                created_at: now,
                updated_at: now,
                file_mtime: now,
                file_sha256: "".into(),
                confidence: 1.0,
            },
        )
        .unwrap();
        augmentation::upsert(
            &cat,
            &crate::librarian::catalog::augmentation::AugmentationRow {
                artifact_id: "a1".into(),
                prompt: "track".into(),
                params: "{}".into(),
                last_refreshed_at: None,
                refresh_count: 0,
                created_at: now_ts.clone(),
                updated_at: now_ts,
                render_template: None,
                params_schema: None,
                append_mode: false,
                history_cap: None,
            },
        )
        .unwrap();
        let s = catalog_summary(&cat, None).unwrap();
        assert_eq!(s.total, 2);
        assert_eq!(s.augmented, 1);
    }

    #[test]
    fn catalog_summary_respects_scoped_filter() {
        use crate::librarian::catalog::artifact::{upsert, ArtifactRow};
        use crate::librarian::catalog::augmentation;
        use crate::librarian::filter::FilterNode;
        use serde_json::json;
        let cat = crate::librarian::catalog::Catalog::open_in_memory().unwrap();
        let now = chrono::Utc::now().timestamp_millis();
        let now_ts = chrono::Utc::now()
            .format("%Y-%m-%dT%H:%M:%S%.3fZ")
            .to_string();
        for (id, repo) in [("a1", "repo-a"), ("a2", "repo-b")] {
            upsert(
                &cat,
                &ArtifactRow {
                    id: id.into(),
                    abs_path: std::path::PathBuf::from(format!("/{repo}/{id}.md")),
                    kind: "plan".into(),
                    status: "draft".into(),
                    title: None,
                    owners: vec![],
                    tags: vec![],
                    topic: None,
                    time_scope: None,
                    source: None,
                    created_at: now,
                    updated_at: now,
                    file_mtime: now,
                    file_sha256: "".into(),
                    confidence: 1.0,
                },
            )
            .unwrap();
        }
        // Augment the repo-b artifact — filter to repo-a must exclude it
        augmentation::upsert(
            &cat,
            &crate::librarian::catalog::augmentation::AugmentationRow {
                artifact_id: "a2".into(),
                prompt: "track".into(),
                params: "{}".into(),
                last_refreshed_at: None,
                refresh_count: 0,
                created_at: now_ts.clone(),
                updated_at: now_ts,
                render_template: None,
                params_schema: None,
                append_mode: false,
                history_cap: None,
            },
        )
        .unwrap();
        let f = FilterNode::Leaf(
            [("abs_path".to_string(), json!({"prefix": "/repo-a/"}))]
                .into_iter()
                .collect(),
        );
        let s = catalog_summary(&cat, Some(&f)).unwrap();
        assert_eq!(s.total, 1);
        assert_eq!(
            s.augmented, 0,
            "augmented count must respect the scope filter"
        );
    }
}