nenjo-knowledge 0.11.0

Knowledge pack primitives and embedded Nenjo knowledge
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
//! Shared knowledge pack primitives and embedded Nenjo knowledge.
//!
//! Knowledge packs expose a common metadata/search/read API for builtin,
//! project, filesystem, or remote document sets.

use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

#[cfg(feature = "nenjo")]
pub mod builtin;
pub mod tools;

/// Shared read-only metadata contract for any knowledge pack manifest.
///
/// This trait intentionally covers only pack identity and document metadata.
/// Concrete pack manifests, such as project or remote manifests, should expose
/// their own sync/cache mutation methods on their concrete types.
pub trait KnowledgePackManifest: Send + Sync {
    fn pack_id(&self) -> &str;
    fn pack_version(&self) -> &str;
    fn schema_version(&self) -> u32;
    fn root_uri(&self) -> &str;
    fn content_hash(&self) -> &str;
    fn docs(&self) -> &[KnowledgeDocManifest];

    fn read_doc_manifest(&self, path: &str) -> Option<&KnowledgeDocManifest> {
        self.docs()
            .iter()
            .find(|doc| doc.id == path || doc.virtual_path == path || doc.source_path == path)
    }
}

/// Serializable base manifest used by read-only packs and generic consumers.
///
/// Project and remote packs may deserialize into richer concrete types, but
/// their document entries should still use [`KnowledgeDocManifest`] so agents
/// and MCP tools see one metadata schema across all pack sources.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgePackManifestData {
    pub pack_id: String,
    pub pack_version: String,
    pub schema_version: u32,
    pub root_uri: String,
    #[serde(default)]
    pub content_hash: String,
    pub docs: Vec<KnowledgeDocManifest>,
}

impl KnowledgePackManifest for KnowledgePackManifestData {
    fn pack_id(&self) -> &str {
        &self.pack_id
    }

    fn pack_version(&self) -> &str {
        &self.pack_version
    }

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn root_uri(&self) -> &str {
        &self.root_uri
    }

    fn content_hash(&self) -> &str {
        &self.content_hash
    }

    fn docs(&self) -> &[KnowledgeDocManifest] {
        &self.docs
    }
}

/// Shared document metadata visible through knowledge pack APIs.
///
/// `size_bytes` and `updated_at` are sync hints used by local project caches.
/// Builtin and remote manifests may leave them empty/defaulted.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeDocManifest {
    pub id: String,
    pub virtual_path: String,
    pub source_path: String,
    pub title: String,
    pub summary: String,
    pub description: Option<String>,
    pub kind: KnowledgeDocKind,
    pub authority: KnowledgeDocAuthority,
    pub status: KnowledgeDocStatus,
    pub tags: Vec<String>,
    pub aliases: Vec<String>,
    pub keywords: Vec<String>,
    pub related: Vec<KnowledgeDocEdge>,
    #[serde(default)]
    pub size_bytes: i64,
    #[serde(default)]
    pub updated_at: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeDocEdgeType {
    PartOf,
    Defines,
    Governs,
    Classifies,
    References,
    DependsOn,
    Extends,
    RelatedTo,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeDocKind {
    Guide,
    Reference,
    Taxonomy,
    Domain,
    Entity,
    Policy,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeDocAuthority {
    Canonical,
    Supporting,
    Pattern,
    Reference,
    Advisory,
    Example,
    Draft,
    Deprecated,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeDocStatus {
    Stable,
    Draft,
    Deprecated,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeDocEdge {
    #[serde(rename = "type", alias = "edge_type")]
    pub edge_type: KnowledgeDocEdgeType,
    pub target: String,
    pub description: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct KnowledgeDocFilter {
    pub tags: Vec<String>,
    pub kind: Option<KnowledgeDocKind>,
    pub authority: Option<KnowledgeDocAuthority>,
    pub status: Option<KnowledgeDocStatus>,
    pub path_prefix: Option<String>,
    pub related_to: Option<String>,
    pub edge_type: Option<KnowledgeDocEdgeType>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeDocRead {
    pub manifest: KnowledgeDocManifest,
    pub content: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KnowledgeDocNeighbor {
    pub target: String,
    pub edges: Vec<KnowledgeDocNeighborEdge>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KnowledgeDocNeighborEdge {
    pub edge_type: KnowledgeDocEdgeType,
    pub source: String,
    pub target: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeDocSearchHit {
    pub id: String,
    pub virtual_path: String,
    pub title: String,
    pub summary: String,
    pub kind: KnowledgeDocKind,
    pub authority: KnowledgeDocAuthority,
    pub tags: Vec<String>,
    pub score: usize,
    pub matched: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeDocTree {
    pub root_uri: String,
    pub entries: Vec<KnowledgeDocTreeEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeDocTreeEntry {
    pub path: String,
    pub title: String,
    pub kind: KnowledgeDocKind,
    pub tags: Vec<String>,
}

enum SearchMode {
    MetadataOnly,
    FullText,
}

/// Runtime access to a knowledge pack's metadata and lazy document content.
pub trait KnowledgePack: Send + Sync {
    fn manifest(&self) -> &dyn KnowledgePackManifest;

    fn doc_content(&self, manifest: &KnowledgeDocManifest) -> Option<Cow<'_, str>>;

    fn list_tree(&self, prefix: Option<&str>) -> KnowledgeDocTree {
        let mut entries: Vec<_> = self
            .manifest()
            .docs()
            .iter()
            .filter(|doc| {
                prefix
                    .map(|prefix| doc.virtual_path.starts_with(prefix))
                    .unwrap_or(true)
            })
            .map(|doc| KnowledgeDocTreeEntry {
                path: doc.virtual_path.clone(),
                title: doc.title.clone(),
                kind: doc.kind,
                tags: doc.tags.clone(),
            })
            .collect();
        entries.sort_by(|a, b| a.path.cmp(&b.path));
        KnowledgeDocTree {
            root_uri: self.manifest().root_uri().to_string(),
            entries,
        }
    }

    fn list_docs(&self, filter: KnowledgeDocFilter) -> Vec<&KnowledgeDocManifest> {
        self.manifest()
            .docs()
            .iter()
            .filter(|doc| matches_filter(self, doc, &filter))
            .collect()
    }

    fn read_manifest(&self, path: &str) -> Option<&KnowledgeDocManifest> {
        self.manifest().read_doc_manifest(path)
    }

    fn read_doc(&self, path: &str) -> Option<KnowledgeDocRead> {
        let manifest = self.read_manifest(path)?.clone();
        let content = self.doc_content(&manifest)?.into_owned();
        Some(KnowledgeDocRead { manifest, content })
    }

    fn search_paths(&self, query: &str, filter: KnowledgeDocFilter) -> Vec<KnowledgeDocSearchHit> {
        search_pack(self, query, filter, SearchMode::MetadataOnly)
    }

    fn search_docs(&self, query: &str, filter: KnowledgeDocFilter) -> Vec<KnowledgeDocSearchHit> {
        search_pack(self, query, filter, SearchMode::FullText)
    }

    fn neighbors(
        &self,
        path: &str,
        edge_type: Option<KnowledgeDocEdgeType>,
    ) -> Vec<KnowledgeDocNeighbor> {
        let Some(source) = self.read_manifest(path) else {
            return Vec::new();
        };

        let mut neighbors: BTreeMap<String, KnowledgeDocNeighbor> = BTreeMap::new();

        for edge in &source.related {
            if let Some(expected) = edge_type
                && edge.edge_type != expected
            {
                continue;
            }
            if let Some(target) = self.read_manifest(&edge.target) {
                push_neighbor_edge(
                    &mut neighbors,
                    target.virtual_path.clone(),
                    KnowledgeDocNeighborEdge {
                        edge_type: edge.edge_type,
                        source: source.virtual_path.clone(),
                        target: target.virtual_path.clone(),
                        note: edge.description.clone(),
                    },
                );
            }
        }

        for candidate in self.manifest().docs() {
            for edge in &candidate.related {
                let points_to_source = self
                    .read_manifest(&edge.target)
                    .map(|target| {
                        target.id == source.id || target.virtual_path == source.virtual_path
                    })
                    .unwrap_or_else(|| {
                        edge.target == source.id || edge.target == source.virtual_path
                    });
                if !points_to_source {
                    continue;
                }
                if let Some(expected) = edge_type
                    && edge.edge_type != expected
                {
                    continue;
                }
                push_neighbor_edge(
                    &mut neighbors,
                    candidate.virtual_path.clone(),
                    KnowledgeDocNeighborEdge {
                        edge_type: edge.edge_type,
                        source: candidate.virtual_path.clone(),
                        target: source.virtual_path.clone(),
                        note: edge.description.clone(),
                    },
                );
            }
        }

        neighbors.into_values().collect()
    }
}

impl KnowledgeDocEdgeType {
    pub fn as_str(self) -> &'static str {
        match self {
            KnowledgeDocEdgeType::PartOf => "part_of",
            KnowledgeDocEdgeType::Defines => "defines",
            KnowledgeDocEdgeType::Governs => "governs",
            KnowledgeDocEdgeType::Classifies => "classifies",
            KnowledgeDocEdgeType::References => "references",
            KnowledgeDocEdgeType::DependsOn => "depends_on",
            KnowledgeDocEdgeType::Extends => "extends",
            KnowledgeDocEdgeType::RelatedTo => "related_to",
        }
    }
}

impl KnowledgeDocKind {
    pub fn as_str(self) -> &'static str {
        match self {
            KnowledgeDocKind::Guide => "guide",
            KnowledgeDocKind::Reference => "reference",
            KnowledgeDocKind::Taxonomy => "taxonomy",
            KnowledgeDocKind::Domain => "domain",
            KnowledgeDocKind::Entity => "entity",
            KnowledgeDocKind::Policy => "policy",
        }
    }
}

impl KnowledgeDocAuthority {
    pub fn as_str(self) -> &'static str {
        match self {
            KnowledgeDocAuthority::Canonical => "canonical",
            KnowledgeDocAuthority::Supporting => "supporting",
            KnowledgeDocAuthority::Pattern => "pattern",
            KnowledgeDocAuthority::Reference => "reference",
            KnowledgeDocAuthority::Advisory => "advisory",
            KnowledgeDocAuthority::Example => "example",
            KnowledgeDocAuthority::Draft => "draft",
            KnowledgeDocAuthority::Deprecated => "deprecated",
        }
    }
}

impl KnowledgeDocStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            KnowledgeDocStatus::Stable => "stable",
            KnowledgeDocStatus::Draft => "draft",
            KnowledgeDocStatus::Deprecated => "deprecated",
        }
    }
}

fn search_pack<P: KnowledgePack + ?Sized>(
    pack: &P,
    query: &str,
    filter: KnowledgeDocFilter,
    mode: SearchMode,
) -> Vec<KnowledgeDocSearchHit> {
    let needle = normalize(query);
    let mut hits = Vec::new();

    for manifest in pack.list_docs(filter) {
        let mut score = 0;
        let mut matched = BTreeSet::new();

        score += score_field(&needle, &manifest.id, 100, "id", &mut matched);
        score += score_field(
            &needle,
            &manifest.virtual_path,
            90,
            "virtual_path",
            &mut matched,
        );
        score += score_field(&needle, &manifest.title, 80, "title", &mut matched);
        score += score_field(&needle, &manifest.summary, 60, "summary", &mut matched);

        for alias in &manifest.aliases {
            score += score_field(&needle, alias, 75, "alias", &mut matched);
        }
        for tag in &manifest.tags {
            score += score_field(&needle, tag, 70, "tag", &mut matched);
        }
        for keyword in &manifest.keywords {
            score += score_field(&needle, keyword, 65, "keyword", &mut matched);
        }

        let content = match mode {
            SearchMode::MetadataOnly => None,
            SearchMode::FullText => pack.doc_content(manifest),
        };
        if let Some(content) = content.as_ref() {
            score += score_field(&needle, content, 20, "content", &mut matched);
        }

        if score > 0 || needle.is_empty() {
            hits.push(KnowledgeDocSearchHit {
                id: manifest.id.clone(),
                virtual_path: manifest.virtual_path.clone(),
                title: manifest.title.clone(),
                summary: manifest.summary.clone(),
                kind: manifest.kind,
                authority: manifest.authority,
                tags: manifest.tags.clone(),
                score,
                matched: matched.into_iter().collect(),
                content: matches!(mode, SearchMode::FullText)
                    .then(|| content.map(Cow::into_owned).unwrap_or_default()),
            });
        }
    }

    hits.sort_by(|a, b| {
        b.score
            .cmp(&a.score)
            .then_with(|| a.virtual_path.cmp(&b.virtual_path))
    });
    hits
}

fn matches_filter<P: KnowledgePack + ?Sized>(
    pack: &P,
    doc: &KnowledgeDocManifest,
    filter: &KnowledgeDocFilter,
) -> bool {
    if let Some(kind) = filter.kind
        && doc.kind != kind
    {
        return false;
    }
    if let Some(authority) = filter.authority
        && doc.authority != authority
    {
        return false;
    }
    if let Some(status) = filter.status
        && doc.status != status
    {
        return false;
    }
    if let Some(prefix) = &filter.path_prefix
        && !doc.virtual_path.starts_with(prefix)
    {
        return false;
    }
    if !filter.tags.is_empty()
        && !filter
            .tags
            .iter()
            .all(|tag| doc.tags.iter().any(|doc_tag| doc_tag == tag))
    {
        return false;
    }
    if let Some(target) = &filter.related_to {
        let has_edge = doc.related.iter().any(|edge| {
            let edge_matches_target = edge.target == *target
                || pack
                    .read_manifest(&edge.target)
                    .map(|edge_target| {
                        edge_target.id == *target || edge_target.virtual_path == *target
                    })
                    .unwrap_or(false);
            edge_matches_target
                && filter
                    .edge_type
                    .as_ref()
                    .map(|expected| edge.edge_type == *expected)
                    .unwrap_or(true)
        });
        if !has_edge {
            return false;
        }
    }
    true
}

fn push_neighbor_edge(
    neighbors: &mut BTreeMap<String, KnowledgeDocNeighbor>,
    neighbor_target: String,
    edge: KnowledgeDocNeighborEdge,
) {
    let neighbor =
        neighbors
            .entry(neighbor_target.clone())
            .or_insert_with(|| KnowledgeDocNeighbor {
                target: neighbor_target,
                edges: Vec::new(),
            });
    if !neighbor.edges.contains(&edge) {
        neighbor.edges.push(edge);
        neighbor.edges.sort_by(|left, right| {
            left.source
                .cmp(&right.source)
                .then_with(|| left.target.cmp(&right.target))
                .then_with(|| left.edge_type.as_str().cmp(right.edge_type.as_str()))
                .then_with(|| left.note.cmp(&right.note))
        });
    }
}

fn score_field(
    needle: &str,
    haystack: &str,
    weight: usize,
    label: &str,
    matched: &mut BTreeSet<String>,
) -> usize {
    if needle.is_empty() {
        return 1;
    }
    let haystack = normalize(haystack);
    if haystack == needle {
        matched.insert(label.to_string());
        weight * 2
    } else if haystack.contains(needle) {
        matched.insert(label.to_string());
        weight
    } else {
        0
    }
}

fn normalize(value: &str) -> String {
    value.trim().to_lowercase()
}