relay-knowledge 1.1.17

Graph-database-based knowledge graph project.
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
584
585
586
587
588
589
590
591
use std::path::PathBuf;

use tokio::fs;
use tokio::time::Duration;
#[cfg(test)]
use tokio::time::sleep;

#[cfg(test)]
use crate::project::{
    AGENT_CONTRACT_DIR_NAME, KNOWLEDGE_MAP_FILE_NAME, KNOWLEDGE_MAP_HISTORY_DIR_NAME,
};
use crate::{
    api::RequestContext,
    domain::{BusinessGlossary, KnowledgeMap, RepositoryMapType, validate_directory_collection},
    project::{
        CODESPEC_MAP_RELATIVE_PATH, KNOWLEDGE_MAP_RELATIVE_PATH, KNOWLEDGE_MAP_TOPICS_DIR_NAME,
        LEGACY_AGENT_CONTRACT_DIR_NAME, LEGACY_BUSINESS_GLOSSARY_RELATIVE_PATH,
    },
};

mod artifact;
mod contracts;
mod error;
mod fs_contract;
mod governance;
mod history;
mod lock;
mod migration;
mod query;
mod source_mutation;
mod validation;

pub(crate) use history::MAX_HISTORY_PAGE_SIZE;
#[cfg(test)]
use lock::{
    ADVISORY_LOCK_MARKER, cleanup_transition_locks, transition_lock_prepared_path,
    transition_lock_prepared_path_with_identity,
};

use artifact::*;
pub use contracts::{
    KnowledgeMapAgentSnippetResponse, KnowledgeMapHistoryResponse, KnowledgeMapHistoryWindow,
    KnowledgeMapMutationResponse, KnowledgeMapRouteResponse, KnowledgeMapShowResponse,
    KnowledgeMapSourceAddRequest, KnowledgeMapValidationResponse, KnowledgeMapView,
};
use contracts::{MutableKnowledgeMap, baseline_directories, metadata, now_stamp};
pub use error::KnowledgeMapServiceError;
use fs_contract::*;

const WRITE_LOCK_TIMEOUT: Duration = Duration::from_secs(10);

/// File-backed service for the shared YAML knowledge navigation contract.
pub struct KnowledgeMapService {
    repository_root: PathBuf,
    map_type: RepositoryMapType,
}

impl KnowledgeMapService {
    pub fn new(repository_root: PathBuf) -> Self {
        Self {
            repository_root,
            map_type: RepositoryMapType::Knowledge,
        }
    }

    pub(crate) fn for_type(&self, map_type: RepositoryMapType) -> Self {
        Self {
            repository_root: self.repository_root.clone(),
            map_type,
        }
    }

    pub async fn init(
        &self,
        context: &RequestContext,
    ) -> Result<KnowledgeMapMutationResponse, KnowledgeMapServiceError> {
        let _mutation_locks = self.acquire_legacy_aware_mutation_locks().await?;
        let _rollback_committed = self.recover_legacy_rollback_transition().await?;
        self.recover_manifest_backup().await?;
        self.recover_legacy_redirect_transition().await?;
        self.prepare_legacy_migration().await?;
        self.ensure_baseline_files().await?;
        let path = self.map_path();
        if fs::try_exists(&path).await? {
            let existing = fs::read_to_string(&path).await?;
            let existing_schema_version =
                serde_norway::from_str::<KnowledgeMapSchemaProbe>(&existing)
                    .map_err(|error| KnowledgeMapServiceError::Yaml(error.to_string()))?
                    .schema_version;
            let mut snapshot = self.load_for_mutation().await?;
            let (software_changed, business_changed, glossary_created) =
                if self.map_type == RepositoryMapType::Knowledge {
                    let (software_changed, business_changed) = snapshot
                        .map
                        .ensure_reserved_repository_routes_snapshot(snapshot.omitted_through)?;
                    (
                        software_changed,
                        business_changed,
                        self.ensure_default_business_glossary().await?,
                    )
                } else {
                    (false, false, false)
                };
            if software_changed || business_changed {
                snapshot.map.record_change(
                    "builtin-routes.ensure",
                    "Ensured repository software-model and business-knowledge routes.".to_owned(),
                    now_stamp(),
                );
                self.write_map(&mut snapshot).await?;
                return Ok(self.mutation_response(
                    context,
                    snapshot.map.map_version,
                    "initialized repository software-model and business-knowledge routes"
                        .to_owned(),
                ));
            }
            if existing_schema_version == 1 || snapshot.requires_publish {
                let response_summary =
                    snapshot.record_required_publication(existing_schema_version, now_stamp());
                self.write_map(&mut snapshot).await?;
                return Ok(self.mutation_response(
                    context,
                    snapshot.map.map_version,
                    response_summary,
                ));
            }
            self.finalize_recent_history_migration().await?;
            return Ok(self.mutation_response(
                context,
                snapshot.map.map_version,
                if glossary_created {
                    "created missing repository business glossary".to_owned()
                } else if self.map_type == RepositoryMapType::Codespec {
                    "CodeSpec map and governed baseline directories already exist".to_owned()
                } else {
                    "Knowledge map and built-in repository routes already exist".to_owned()
                },
            ));
        }

        let mut snapshot = MutableKnowledgeMap::initial(self.map_type, now_stamp());
        if self.map_type == RepositoryMapType::Knowledge {
            self.ensure_default_business_glossary().await?;
        }
        self.write_map(&mut snapshot).await?;
        Ok(self.mutation_response(
            context,
            snapshot.map.map_version,
            match self.map_type {
                RepositoryMapType::Knowledge => {
                    "created Knowledge map with software-model and business-knowledge routes"
                        .to_owned()
                }
                RepositoryMapType::Codespec => {
                    "created CodeSpec map with governed baseline directories".to_owned()
                }
            },
        ))
    }

    pub fn agent_snippet(&self, context: &RequestContext) -> KnowledgeMapAgentSnippetResponse {
        KnowledgeMapAgentSnippetResponse {
            metadata: metadata(context),
            snippet: format!(
                "CodeSpec map: {CODESPEC_MAP_RELATIVE_PATH}\nKnowledge map: {KNOWLEDGE_MAP_RELATIVE_PATH}"
            ),
        }
    }

    async fn load_or_initial(&self) -> Result<MutableKnowledgeMap, KnowledgeMapServiceError> {
        let path = self.map_path();
        if fs::try_exists(&path).await? || fs::try_exists(self.backup_path()).await? {
            self.load_for_mutation().await
        } else {
            Ok(MutableKnowledgeMap::initial(self.map_type, now_stamp()))
        }
    }

    async fn load_for_mutation(&self) -> Result<MutableKnowledgeMap, KnowledgeMapServiceError> {
        let content = self.read_root_content().await?;
        let probe = serde_norway::from_str::<KnowledgeMapSchemaProbe>(&content)
            .map_err(|error| KnowledgeMapServiceError::Yaml(error.to_string()))?;
        if probe.schema_version == 1 {
            self.require_knowledge_map("legacy map read")?;
            let mut map = serde_norway::from_str::<KnowledgeMap>(&content)
                .map_err(|error| KnowledgeMapServiceError::Yaml(error.to_string()))?;
            map.schema_version = KnowledgeMap::SCHEMA_VERSION;
            let _normalized_legacy_builtin_sources = normalize_legacy_builtin_sources(&mut map);
            return Ok(MutableKnowledgeMap {
                map_type: RepositoryMapType::Knowledge,
                directories: baseline_directories(RepositoryMapType::Knowledge),
                map,
                omitted_through: 0,
                requires_publish: true,
                legacy_glossary_uri_normalized: false,
            });
        }
        if !matches!(
            probe.schema_version,
            LEGACY_ARTIFACT_SCHEMA_VERSION
                | DIRECTORY_ARTIFACT_SCHEMA_VERSION
                | ARTIFACT_SCHEMA_VERSION
        ) {
            return Err(KnowledgeMapServiceError::Yaml(format!(
                "unsupported schema_version {}",
                probe.schema_version
            )));
        }
        let manifest = parse_manifest(&content)?;
        let history_checkpoint = history_checkpoint(&manifest);
        self.validate_manifest_identity(&manifest)?;
        if probe.schema_version != ARTIFACT_SCHEMA_VERSION {
            self.validate_archived_history(&manifest.history).await?;
        }
        let mut requires_publish = probe.schema_version != ARTIFACT_SCHEMA_VERSION;
        let mut topics = Vec::with_capacity(manifest.topics.len());
        let mut sources = Vec::new();
        let mut routes = Vec::new();
        let mut legacy_glossary_uri_normalized = false;
        for topic_ref in &manifest.topics {
            let (shard, normalized_legacy_glossary_uri) = self
                .load_topic_shard_for_mutation(topic_ref, manifest.schema_version)
                .await?;
            requires_publish |= normalized_legacy_glossary_uri;
            legacy_glossary_uri_normalized |= normalized_legacy_glossary_uri;
            topics.push(shard.topic);
            sources.extend(shard.sources);
            routes.extend(shard.route);
        }
        let mut map = KnowledgeMap {
            schema_version: KnowledgeMap::SCHEMA_VERSION,
            map_version: manifest.map_version,
            updated_at: manifest.updated_at,
            topics,
            sources,
            routes,
            history: manifest.history.recent,
        };
        let normalized_legacy_builtin_sources = normalize_legacy_builtin_sources(&mut map);
        requires_publish |= normalized_legacy_builtin_sources;
        legacy_glossary_uri_normalized |= normalized_legacy_builtin_sources;
        if probe.schema_version != LEGACY_ARTIFACT_SCHEMA_VERSION
            || self.map_type != RepositoryMapType::Knowledge
        {
            map.validate_snapshot(history_checkpoint)?;
        }
        Ok(MutableKnowledgeMap {
            map_type: self.map_type,
            directories: if manifest.directories.is_empty() {
                baseline_directories(self.map_type)
            } else {
                manifest.directories
            },
            map,
            omitted_through: history_checkpoint,
            requires_publish,
            legacy_glossary_uri_normalized,
        })
    }

    async fn write_map(
        &self,
        snapshot: &mut MutableKnowledgeMap,
    ) -> Result<(), KnowledgeMapServiceError> {
        snapshot.map.validate_snapshot(snapshot.omitted_through)?;
        validate_directory_collection(self.map_type, &snapshot.directories, true)?;
        let dir = self.repository_root.join(self.contract_dir_name());
        fs::create_dir_all(&dir).await?;
        let mut topic_refs = Vec::with_capacity(snapshot.map.topics.len());
        for topic in &snapshot.map.topics {
            let shard = KnowledgeMapTopicShard {
                schema_version: ARTIFACT_SCHEMA_VERSION,
                topic: topic.clone(),
                sources: snapshot
                    .map
                    .sources
                    .iter()
                    .filter(|source| source.topic == topic.id)
                    .cloned()
                    .collect(),
                route: snapshot
                    .map
                    .routes
                    .iter()
                    .find(|route| route.topic == topic.id)
                    .cloned(),
            };
            let yaml = serialize_yaml(&shard)?;
            let digest = content_digest(yaml.as_bytes());
            let relative = format!(
                "{KNOWLEDGE_MAP_TOPICS_DIR_NAME}/topic-{}-{digest}.yaml",
                stable_id(&topic.id)
            );
            publish_immutable_in(
                &self.repository_root,
                self.contract_dir_name(),
                &relative,
                yaml.as_bytes(),
            )
            .await?;
            topic_refs.push(KnowledgeMapTopicRef {
                id: topic.id.clone(),
                title: topic.title.clone(),
                description: topic.description.clone(),
                source_ids: shard
                    .sources
                    .iter()
                    .map(|source| source.id.clone())
                    .collect(),
                r#ref: relative,
                digest,
            });
        }

        if snapshot.map.history.len() > RECENT_HISTORY_LIMIT {
            let omitted = snapshot.map.history.len() - RECENT_HISTORY_LIMIT;
            let discarded = snapshot
                .map
                .history
                .drain(..omitted)
                .next_back()
                .ok_or_else(|| {
                    KnowledgeMapServiceError::Integrity(
                        "recent history compaction did not discard an entry".to_owned(),
                    )
                })?;
            snapshot.omitted_through = discarded.version;
        }
        snapshot.map.validate_snapshot(snapshot.omitted_through)?;
        let manifest = KnowledgeMapManifest {
            schema_version: ARTIFACT_SCHEMA_VERSION,
            artifact_kind: Some("map".to_owned()),
            map_type: Some(snapshot.map_type),
            map_version: snapshot.map.map_version,
            updated_at: snapshot.map.updated_at.clone(),
            directories: snapshot.directories.clone(),
            topics: topic_refs,
            history: KnowledgeMapHistoryManifest {
                archived_through: 0,
                omitted_through: snapshot.omitted_through,
                archive: None,
                index: None,
                recent: snapshot.map.history.clone(),
            },
        };
        self.publish_manifest(serialize_yaml(&manifest)?.as_bytes())
            .await?;
        if fs::try_exists(self.legacy_backup_path()).await? {
            self.publish_legacy_redirect().await?;
        }
        if let Err(error) = self.finalize_recent_history_migration().await {
            tracing::warn!(
                map_type = self.map_type.as_str(),
                map_version = snapshot.map.map_version,
                error = %error,
                "repository map was committed but recent-history cleanup requires maintenance"
            );
        }
        snapshot.requires_publish = false;
        Ok(())
    }

    async fn finalize_recent_history_migration(&self) -> Result<(), KnowledgeMapServiceError> {
        let current = read_root_file(&self.repository_root, &self.map_path()).await?;
        let current_probe = serde_norway::from_str::<KnowledgeMapSchemaProbe>(&current)
            .map_err(|error| KnowledgeMapServiceError::Yaml(error.to_string()))?;
        if current_probe.schema_version != ARTIFACT_SCHEMA_VERSION {
            return Ok(());
        }
        if fs::try_exists(self.backup_path()).await? {
            let backup = read_root_file(&self.repository_root, &self.backup_path()).await?;
            let backup_probe = serde_norway::from_str::<KnowledgeMapSchemaProbe>(&backup)
                .map_err(|error| KnowledgeMapServiceError::Yaml(error.to_string()))?;
            if backup_probe.schema_version != ARTIFACT_SCHEMA_VERSION {
                self.publish_manifest(current.as_bytes()).await?;
            }
        }
        cleanup_history_artifacts_in(
            &self.repository_root,
            self.contract_dir_name(),
            HISTORY_READER_GRACE_PERIOD,
        )
        .await?;
        if self.map_type == RepositoryMapType::Knowledge
            && self.legacy_history_cleanup_is_safe().await?
        {
            cleanup_history_artifacts_in(
                &self.repository_root,
                LEGACY_AGENT_CONTRACT_DIR_NAME,
                HISTORY_READER_GRACE_PERIOD,
            )
            .await?;
        }
        let manifest = parse_manifest(&current)?;
        cleanup_superseded_topic_shards_in(
            &self.repository_root,
            self.contract_dir_name(),
            &self.backup_path(),
            &manifest,
            HISTORY_READER_GRACE_PERIOD,
        )
        .await;
        Ok(())
    }

    async fn load_topic_shard_for_mutation(
        &self,
        topic_ref: &KnowledgeMapTopicRef,
        manifest_schema_version: u16,
    ) -> Result<(KnowledgeMapTopicShard, bool), KnowledgeMapServiceError> {
        let contract_dir = self.read_contract_dir_name().await?;
        self.load_topic_shard_with_legacy_glossary_normalization(
            contract_dir,
            topic_ref,
            manifest_schema_version,
            true,
        )
        .await
    }

    async fn load_topic_shard_in(
        &self,
        contract_dir: &str,
        topic_ref: &KnowledgeMapTopicRef,
        manifest_schema_version: u16,
    ) -> Result<KnowledgeMapTopicShard, KnowledgeMapServiceError> {
        self.load_topic_shard_with_legacy_glossary_normalization(
            contract_dir,
            topic_ref,
            manifest_schema_version,
            false,
        )
        .await
        .map(|(shard, _normalized_legacy_glossary_uri)| shard)
    }

    async fn load_topic_shard_with_legacy_glossary_normalization(
        &self,
        contract_dir: &str,
        topic_ref: &KnowledgeMapTopicRef,
        manifest_schema_version: u16,
        normalize_visible_legacy_glossary_uri: bool,
    ) -> Result<(KnowledgeMapTopicShard, bool), KnowledgeMapServiceError> {
        let content = read_verified_ref_in(
            &self.repository_root,
            contract_dir,
            &topic_ref.r#ref,
            &topic_ref.digest,
        )
        .await?;
        let mut shard = serde_norway::from_str::<KnowledgeMapTopicShard>(&content)
            .map_err(|error| KnowledgeMapServiceError::Yaml(error.to_string()))?;
        let mut normalized_legacy_glossary_uri = false;
        if contract_dir == LEGACY_AGENT_CONTRACT_DIR_NAME || normalize_visible_legacy_glossary_uri {
            for source in &mut shard.sources {
                if source.id == "repository-business-glossary"
                    && source.uri == LEGACY_BUSINESS_GLOSSARY_RELATIVE_PATH
                {
                    source.uri = crate::project::BUSINESS_GLOSSARY_RELATIVE_PATH.to_owned();
                    source.version = source.version.saturating_add(1);
                    normalized_legacy_glossary_uri = true;
                }
            }
        }
        let expected_ref = format!(
            "{KNOWLEDGE_MAP_TOPICS_DIR_NAME}/topic-{}-{}.yaml",
            stable_id(&topic_ref.id),
            topic_ref.digest
        );
        if topic_ref.r#ref != expected_ref
            || shard.schema_version != manifest_schema_version
            || shard.topic.id != topic_ref.id
            || shard.topic.title != topic_ref.title
            || shard.topic.description != topic_ref.description
            || !shard
                .sources
                .iter()
                .map(|source| source.id.as_str())
                .eq(topic_ref.source_ids.iter().map(String::as_str))
        {
            return Err(KnowledgeMapServiceError::Integrity(format!(
                "topic shard '{}' identity, metadata, or schema does not match the manifest",
                topic_ref.r#ref
            )));
        }
        validate_topic_shard(&shard)?;
        Ok((shard, normalized_legacy_glossary_uri))
    }

    async fn publish_manifest(&self, content: &[u8]) -> Result<(), KnowledgeMapServiceError> {
        let path = self.map_path();
        let temp = temporary_path(&path);
        let backup = self.backup_path();
        if let Err(error) = fs::write(&temp, content).await {
            let _ = fs::remove_file(&temp).await;
            return Err(error.into());
        }
        let existed = fs::try_exists(&path).await?;
        if existed {
            if fs::try_exists(&backup).await? {
                fs::remove_file(&backup).await?;
            }
            fs::rename(&path, &backup).await?;
        }
        if let Err(error) = fs::rename(&temp, &path).await {
            if existed {
                let _ = fs::rename(&backup, &path).await;
            }
            let _ = fs::remove_file(temp).await;
            return Err(KnowledgeMapServiceError::Io(error));
        }
        Ok(())
    }

    async fn recover_manifest_backup(&self) -> Result<(), KnowledgeMapServiceError> {
        let path = self.map_path();
        let backup = self.backup_path();
        if !fs::try_exists(&path).await? && fs::try_exists(&backup).await? {
            fs::rename(backup, path).await?;
        }
        Ok(())
    }

    fn mutation_response(
        &self,
        context: &RequestContext,
        map_version: u64,
        summary: String,
    ) -> KnowledgeMapMutationResponse {
        KnowledgeMapMutationResponse {
            metadata: metadata(context),
            path: self.relative_path().to_owned(),
            map_type: self.map_type,
            map_version,
            summary,
        }
    }

    fn business_glossary_path(&self) -> PathBuf {
        self.repository_root
            .join(crate::project::BUSINESS_GLOSSARY_RELATIVE_PATH)
    }

    async fn ensure_default_business_glossary(&self) -> Result<bool, KnowledgeMapServiceError> {
        let contract = self.repository_root.join(self.contract_dir_name());
        let owned_contract = ensure_owned_directory(&self.repository_root, &contract).await?;
        let path = self.business_glossary_path();
        if fs::try_exists(&path).await? {
            ensure_regular_file_within(&path, &owned_contract).await?;
            let content = fs::read(&path).await?;
            BusinessGlossary::parse(&content)?;
            return Ok(false);
        }
        let yaml = serialize_yaml(&BusinessGlossary::empty_v1())?;
        let temp = temporary_path(&path);
        if let Err(error) = fs::write(&temp, yaml.as_bytes()).await {
            let _ = fs::remove_file(&temp).await;
            return Err(error.into());
        }
        if let Err(error) = fs::rename(&temp, &path).await {
            let _ = fs::remove_file(temp).await;
            return Err(error.into());
        }
        Ok(true)
    }
}

fn history_checkpoint(manifest: &KnowledgeMapManifest) -> u64 {
    if manifest.schema_version == ARTIFACT_SCHEMA_VERSION {
        manifest.history.omitted_through
    } else {
        manifest.history.archived_through
    }
}

#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "reserved_contract_tests.rs"]
mod reserved_contract_tests;

#[cfg(test)]
#[path = "identity_contract_tests.rs"]
mod identity_contract_tests;

#[cfg(test)]
#[path = "storage_tests.rs"]
mod map_storage_tests;