knot 1.12.1

Codebase Graph + Vector RAG Indexer for Java, TypeScript, JavaScript, Kotlin, Rust, Python, Groovy, C/C++, C#, Build Systems, and HTML/CSS codebases
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
//! Startup model-consistency guard ("the guard ladder").
//!
//! Every verdict the guard emits is a **pure** function of the configured
//! [`EmbedModelChoice`], the Qdrant collection's real vector dimension and
//! the persisted `:Repository` embed markers — fully unit-testable without
//! databases. The binaries wire the inputs in and act on the result:
//! `Abort` exits non-zero, `WarnPartial` logs at `warn!`.
//!
//! # Why a marker at all
//!
//! A Qdrant collection's vector size is fixed at creation, but a
//! same-dimension model switch would produce **no error at all** — only
//! silently degraded recall (new-model query vectors searching old-model
//! passages). The marker makes that class of failure explicit:
//!
//! - a repository indexed with another model is visible in the graph but
//!   unreachable by semantic search (it lives in another collection);
//! - a missing marker means a legacy index, and with the supported set
//!   closed to two models the collection's dimension infers the model
//!   unambiguously (`384 ⇒ AllMiniLML6V2`, `768 ⇒ BGEBaseENV15`), so a
//!   legacy index is never fail-closed.

use std::str::FromStr as _;

use crate::pipeline::embed::EmbedModelChoice;

/// Wire-up helper used by the binaries (knot, knot-mcp): resolve the
/// configured model, probe the collection's real vector dimension, read the
/// persisted markers, and **act** on the verdict. `WarnPartial` logs at
/// `warn!`; `Abort` surfaces as a hard `Err` (the binaries then exit
/// non-zero). Failures reading the marker data are degraded to a warning —
/// the guard protects against silent model mixing, not against reachable
/// databases going away (the normal connection path reports those).
pub async fn verify_startup(cfg: &crate::config::Config) -> anyhow::Result<()> {
    use crate::db::graph::{ConnectExt as _, GraphDb, RepoQueryExt as _};
    use crate::db::vector::probe_collection_dim;

    let choice =
        EmbedModelChoice::from_str(&cfg.embed_model).map_err(|e| anyhow::anyhow!("{e}"))?;

    let collection_dim = match probe_collection_dim(&cfg.qdrant_url, &cfg.qdrant_collection).await {
        Ok(dim) => dim,
        Err(e) => {
            tracing::warn!(
                "Embed-model guard skipped: could not probe collection '{}' ({e})",
                cfg.qdrant_collection
            );
            return Ok(());
        }
    };

    let markers = match GraphDb::connect(&cfg.neo4j_uri, &cfg.neo4j_user, &cfg.neo4j_password).await
    {
        Ok(db) => db.repo_embed_markers(&[]).await.unwrap_or_else(|e| {
            tracing::warn!("Embed markers unreadable ({e}) — model check skipped");
            Vec::new()
        }),
        Err(e) => {
            tracing::warn!("Neo4j unreachable for the embed-marker check ({e}) — skipped");
            Vec::new()
        }
    };

    let verdict = classify_startup(
        &choice,
        &cfg.embed_model,
        &cfg.qdrant_collection,
        collection_dim,
        &markers,
    );
    match verdict {
        StartupVerdict::Ok => Ok(()),
        StartupVerdict::WarnPartial {
            invisible,
            their_model,
        } => {
            tracing::warn!(
                "Repositories indexed with embedding model '{their_model}' exist in the graph \
                 but are invisible to semantic search from '{}': {}. \
                 Re-index them (`knot-indexer --clean`) with '{}' to include them.",
                cfg.qdrant_collection,
                invisible.join(", "),
                cfg.embed_model
            );
            Ok(())
        }
        StartupVerdict::Abort(msg) => Err(anyhow::anyhow!(msg)),
    }
}

/// The embedding-model marker persisted on a `:Repository` node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepoEmbedMarker {
    pub repo_name: String,
    /// `None` for a repository indexed before the marker existed.
    pub embed_model: Option<String>,
    pub embed_dim: Option<u64>,
    pub qdrant_collection: Option<String>,
}

impl RepoEmbedMarker {
    /// Build a marker straight from the Neo4j `:Repository` row parsed with
    /// `coalesce` fallbacks (`''` / `-1` map to `None`).
    pub fn from_row(
        repo_name: String,
        embed_model: String,
        embed_dim: i64,
        qdrant_collection: String,
    ) -> Self {
        Self {
            repo_name,
            embed_model: if embed_model.is_empty() {
                None
            } else {
                Some(embed_model)
            },
            embed_dim: if embed_dim < 0 {
                None
            } else {
                Some(embed_dim as u64)
            },
            qdrant_collection: if qdrant_collection.is_empty() {
                None
            } else {
                Some(qdrant_collection)
            },
        }
    }
}

/// What the guard ladder decided.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StartupVerdict {
    /// Proceed silently.
    Ok,
    /// Some repositories were indexed with another model: they exist in the
    /// graph but cannot be reached by semantic search.
    WarnPartial {
        invisible: Vec<String>,
        their_model: String,
    },
    /// Hard configuration error; the candidate exits non-zero with the
    /// message.
    Abort(String),
}

/// Closed-set inference for a legacy index that carries no marker.
///
/// Returns `None` for a dimension outside the supported set (the marker
/// stays unknown; rule 2 of [`classify_startup`] still catches a genuine
/// contradiction at the collection level).
pub fn infer_model_from_dim(dim: u64) -> Option<&'static str> {
    match dim {
        384 => Some("AllMiniLML6V2"),
        768 => Some("BGEBaseENV15"),
        _ => None,
    }
}

/// Operator-facing wording for the guard's remediation messages.
///
/// The ladder itself is audience-agnostic, but its *advice* is not: knot's
/// binaries read `KNOT_QDRANT_COLLECTION` and re-index with
/// `knot-indexer --clean`, while a library consumer such as knot-server reads
/// `KNOT_SERVER_QDRANT_COLLECTION` and re-indexes through its REST API.
/// Passing the labels in keeps a single implementation of the ladder while the
/// remediation stays actionable for each audience.
#[derive(Debug, Clone, Copy)]
pub struct GuardHints<'a> {
    /// Environment variable that selects the Qdrant collection.
    pub collection_var: &'a str,
    /// Environment variable that selects the embedding model.
    pub embed_model_var: &'a str,
    /// Operator-facing phrase describing how to force a full re-index.
    pub reindex_cmd: &'a str,
}

impl Default for GuardHints<'_> {
    fn default() -> Self {
        Self {
            collection_var: "KNOT_QDRANT_COLLECTION",
            embed_model_var: "KNOT_EMBED_MODEL",
            reindex_cmd: "`knot-indexer --clean`",
        }
    }
}

/// The guard ladder (rules in order — see the plan, §F4.3):
///
/// 1. absent collection → fresh deployment, never blocked;
/// 2. collection dimension mismatch → abort (vector size is fixed at
///    creation, so the configured model cannot ever write valid vectors);
/// 3. markers all without `embed_model` → legacy index, dimension already
///    agreed via rule 2, self-heals on the next index run;
/// 4. every marked repo on another model → abort;
/// 5. some marked repos differ → warn (they are invisible to search);
/// 6. otherwise → ok.
pub fn classify_startup(
    configured: &EmbedModelChoice,
    configured_name: &str,
    collection: &str,
    collection_dim: Option<u64>,
    markers: &[RepoEmbedMarker],
) -> StartupVerdict {
    classify_startup_with_hints(
        GuardContext {
            configured,
            configured_name,
            collection,
            collection_dim,
        },
        markers,
        &GuardHints::default(),
    )
}

/// Target/environment context passed into [`classify_startup_with_hints`].
#[derive(Debug, Clone, Copy)]
pub struct GuardContext<'a> {
    pub configured: &'a EmbedModelChoice,
    pub configured_name: &'a str,
    pub collection: &'a str,
    pub collection_dim: Option<u64>,
}

/// [`classify_startup`] with caller-supplied remediation wording.
pub fn classify_startup_with_hints(
    ctx: GuardContext<'_>,
    markers: &[RepoEmbedMarker],
    hints: &GuardHints<'_>,
) -> StartupVerdict {
    // Rule 1: fresh deployment.
    let Some(stored_dim) = ctx.collection_dim else {
        return StartupVerdict::Ok;
    };

    // Rule 2: the collection is a fixed-size voucher for exactly one model.
    if stored_dim != ctx.configured.dim {
        return StartupVerdict::Abort(format!(
            "Qdrant collection '{collection}' holds {stored_dim}-dimensional vectors but the \
                 configured embedding model '{configured_name}' produces {configured_dim}-dimensional ones. \
                 A collection's vector size is fixed at creation: point {collection_var} at \
                 a different collection (the default for '{configured_name}' is '{default_collection}') or \
                 switch {embed_model_var} back to a model matching {stored_dim}, then \
                 {reindex_cmd} the affected repositories.",
            collection = ctx.collection,
            configured_dim = ctx.configured.dim,
            configured_name = ctx.configured_name,
            default_collection = ctx.configured.default_collection("knot_entities"),
            collection_var = hints.collection_var,
            embed_model_var = hints.embed_model_var,
            reindex_cmd = hints.reindex_cmd
        ));
    }

    // Rule 3: legacy index (no marker) — proceed and self-heal on the next
    // run; the dimension already agreed via rule 2.
    let marked: Vec<&RepoEmbedMarker> =
        markers.iter().filter(|m| m.embed_model.is_some()).collect();
    if marked.is_empty() {
        return StartupVerdict::Ok;
    }

    // Rules 4/5: compare markers' models against the configured one.
    let mismatched: Vec<&RepoEmbedMarker> = marked
        .iter()
        .copied()
        .filter(|m| m.embed_model.as_deref() != Some(ctx.configured_name))
        .collect();

    if mismatched.is_empty() {
        return StartupVerdict::Ok;
    }

    if mismatched.len() == marked.len() {
        return StartupVerdict::Abort(format!(
            "All {} marked repositories were indexed with another embedding model \
             ({}): the configured model is '{configured_name}'. Set {} to the \
             model already in use, or re-index every repository \
             ({}) with '{configured_name}'.",
            marked.len(),
            mismatched[0].embed_model.as_deref().unwrap_or("?"),
            hints.embed_model_var,
            hints.reindex_cmd,
            configured_name = ctx.configured_name,
        ));
    }

    StartupVerdict::WarnPartial {
        invisible: mismatched.iter().map(|m| m.repo_name.clone()).collect(),
        their_model: mismatched[0].embed_model.clone().unwrap_or_default(),
    }
}

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

    fn marker(repo: &str, model: Option<&str>) -> RepoEmbedMarker {
        RepoEmbedMarker {
            repo_name: repo.to_string(),
            embed_model: model.map(String::from),
            embed_dim: model.map(|_| 384),
            qdrant_collection: model.map(|_| "knot_entities".to_string()),
        }
    }

    fn minilm() -> (EmbedModelChoice, &'static str) {
        let choice = EmbedModelChoice::from_str("AllMiniLML6V2").expect("default model known");
        (choice, "AllMiniLML6V2")
    }

    #[test]
    fn infer_model_from_dim_covers_the_closed_set() {
        assert_eq!(infer_model_from_dim(384), Some("AllMiniLML6V2"));
        assert_eq!(infer_model_from_dim(768), Some("BGEBaseENV15"));
        assert_eq!(infer_model_from_dim(1024), None);
        assert_eq!(infer_model_from_dim(0), None);
    }

    #[test]
    fn rule_1_absent_collection_is_ok() {
        let (choice, name) = minilm();
        assert_eq!(
            classify_startup(&choice, name, "knot_entities", None, &[]),
            StartupVerdict::Ok
        );
    }

    #[test]
    fn rule_2_dimension_mismatch_aborts_with_counters() {
        let bge = EmbedModelChoice::from_str("BGEBaseENV15").expect("opt-in model known");
        let verdict = classify_startup(&bge, "BGEBaseENV15", "knot_entities", Some(384), &[]);
        match &verdict {
            StartupVerdict::Abort(msg) => {
                assert!(msg.contains("knot_entities"), "{msg}");
                assert!(msg.contains("384"), "{msg}");
                assert!(msg.contains("768"), "{msg}");
                assert!(msg.contains("BGEBaseENV15"), "{msg}");
                assert!(msg.contains("--clean"), "{msg}");
            }
            other => panic!("expected Abort, got {other:?}"),
        }
    }

    #[test]
    fn rule_3_legacy_markers_with_no_model_are_ok() {
        let (choice, name) = minilm();
        assert_eq!(
            classify_startup(
                &choice,
                name,
                "knot_entities",
                Some(384),
                &[marker("legacy-a", None), marker("legacy-b", None)],
            ),
            StartupVerdict::Ok
        );
    }

    #[test]
    fn rule_4_every_marked_repo_on_another_model_aborts() {
        let (choice, name) = minilm();
        let verdict = classify_startup(
            &choice,
            name,
            "knot_entities",
            Some(384),
            &[
                marker("a", Some("BGEBaseENV15")),
                marker("b", Some("BGEBaseENV15")),
            ],
        );
        match &verdict {
            StartupVerdict::Abort(msg) => {
                assert!(msg.contains("BGEBaseENV15"), "{msg}");
                assert!(msg.contains("AllMiniLML6V2"), "{msg}");
            }
            other => panic!("expected Abort, got {other:?}"),
        }
    }

    #[test]
    fn rule_5_some_marked_repos_differ_warn_and_name_them() {
        let (choice, name) = minilm();
        let verdict = classify_startup(
            &choice,
            name,
            "knot_entities",
            Some(384),
            &[
                marker("ok-repo", Some("AllMiniLML6V2")),
                marker("other-repo", Some("BGEBaseENV15")),
                marker("legacy-repo", None),
            ],
        );
        match verdict {
            StartupVerdict::WarnPartial {
                invisible,
                their_model,
            } => {
                assert_eq!(invisible, vec!["other-repo".to_string()]);
                assert_eq!(their_model, "BGEBaseENV15");
            }
            other => panic!("expected WarnPartial, got {other:?}"),
        }
    }

    #[test]
    fn rule_6_all_marked_repos_agree_is_ok() {
        let (choice, name) = minilm();
        assert_eq!(
            classify_startup(
                &choice,
                name,
                "knot_entities",
                Some(384),
                &[marker("a", Some("AllMiniLML6V2")), marker("b", None)],
            ),
            StartupVerdict::Ok
        );
    }

    #[test]
    fn custom_hints_replace_the_default_remediation_wording() {
        // H1: a library consumer (knot-server) must not be told to set knot's
        // variable or run knot-indexer. The ladder is identical; only the
        // advice changes.
        let hints = GuardHints {
            collection_var: "KNOT_SERVER_QDRANT_COLLECTION",
            embed_model_var: "KNOT_EMBED_MODEL",
            reindex_cmd: "`POST /api/repos/{id}/sync`",
        };
        let bge = EmbedModelChoice::from_str("BGEBaseENV15").expect("opt-in model known");
        match classify_startup_with_hints(
            GuardContext {
                configured: &bge,
                configured_name: "BGEBaseENV15",
                collection: "knot_entities",
                collection_dim: Some(384),
            },
            &[],
            &hints,
        ) {
            StartupVerdict::Abort(msg) => {
                assert!(msg.contains("KNOT_SERVER_QDRANT_COLLECTION"), "{msg}");
                assert!(msg.contains("POST /api/repos/{id}/sync"), "{msg}");
                assert!(!msg.contains("knot-indexer"), "{msg}");
            }
            other => panic!("expected Abort, got {other:?}"),
        }

        let (minilm, name) = minilm();
        let markers = [marker("a", Some("BGEBaseENV15"))];
        match classify_startup_with_hints(
            GuardContext {
                configured: &minilm,
                configured_name: name,
                collection: "knot_entities",
                collection_dim: Some(384),
            },
            &markers,
            &hints,
        ) {
            StartupVerdict::Abort(msg) => {
                assert!(msg.contains("KNOT_EMBED_MODEL"), "{msg}");
                assert!(msg.contains("POST /api/repos/{id}/sync"), "{msg}");
                assert!(!msg.contains("knot-indexer"), "{msg}");
            }
            other => panic!("expected Abort, got {other:?}"),
        }
    }
}