cmn-hypha 0.3.0

CMN CLI tool — spawn, grow, release, taste, bond, and absorb spores on the Code Mycelial Network
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
use std::path::PathBuf;

use substrate::CmnEntry;

use crate::sink::HyphaError;

use super::{
    locked_update_file, locked_write_file, CacheStatus, DomainStatePin, FetchStatus, KeyTrustEntry,
    TasteVerdictCache,
};

pub const DOMAIN_STATE_JUMP_THRESHOLD: u64 = 1000;

/// Domain-specific cache operations
pub struct DomainCache {
    pub root: PathBuf,
    pub domain: String,
}

impl DomainCache {
    /// Get the mycelium metadata directory
    pub fn mycelium_dir(&self) -> PathBuf {
        self.root.join("mycelium")
    }

    /// Get the spore cache directory
    pub fn spore_dir(&self) -> PathBuf {
        self.root.join("spore")
    }

    /// Get the cache path for a specific spore
    pub fn spore_path(&self, hash: &str) -> PathBuf {
        self.spore_dir().join(hash)
    }

    /// Get the repos cache directory for this domain
    pub fn repos_dir(&self) -> PathBuf {
        self.root.join("repos")
    }

    /// Get the cache path for a specific repository by root_commit
    pub fn repo_path(&self, root_commit: &str) -> PathBuf {
        self.repos_dir().join(root_commit)
    }

    /// Get path to cached cmn.json
    pub fn cmn_path(&self) -> PathBuf {
        self.mycelium_dir().join("cmn.json")
    }

    /// Get path to the local cmn.json domain-state pin.
    pub fn domain_state_path(&self) -> PathBuf {
        self.mycelium_dir().join("domain_state.json")
    }

    /// Load cached cmn.json entry
    pub fn load_cmn(&self) -> Option<CmnEntry> {
        let path = self.cmn_path();
        if path.exists() {
            std::fs::read_to_string(&path)
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
        } else {
            None
        }
    }

    /// Save cmn.json entry to cache
    pub fn save_cmn(&self, entry: &CmnEntry) -> Result<(), crate::sink::HyphaError> {
        let dir = self.mycelium_dir();
        std::fs::create_dir_all(&dir).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to create mycelium dir: {}", e),
            )
        })?;

        let content = serde_json::to_string_pretty(entry).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to serialize cmn entry: {}", e),
            )
        })?;

        locked_write_file(&self.cmn_path(), &content)
    }

    /// Load the local anti-rollback pin for this domain.
    pub fn load_domain_state(&self) -> Option<DomainStatePin> {
        let path = self.domain_state_path();
        if path.exists() {
            std::fs::read_to_string(&path)
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
        } else {
            None
        }
    }

    /// Verify the fetched cmn.json against the local pin and advance the pin.
    pub fn validate_and_pin_cmn_state(
        &self,
        entry: &CmnEntry,
    ) -> Result<(), crate::sink::HyphaError> {
        let capsule = entry.primary_capsule().map_err(|e| {
            HyphaError::new("domain_state_invalid", format!("Invalid cmn.json: {e}"))
        })?;
        let expected_uri = substrate::build_domain_uri(&self.domain);
        if capsule.uri != expected_uri {
            return Err(HyphaError::new(
                "domain_state_invalid",
                format!(
                    "cmn.json primary capsule uri {} does not match domain {}",
                    capsule.uri, self.domain
                ),
            ));
        }
        let digest = entry.capsules_digest().map_err(|e| {
            HyphaError::new(
                "domain_state_invalid",
                format!("Failed to digest cmn.json capsules: {e}"),
            )
        })?;

        if let Some(pin) = self.load_domain_state() {
            if capsule.serial < pin.serial {
                return Err(HyphaError::new(
                    "domain_state_rollback",
                    format!(
                        "cmn.json serial {} is lower than pinned serial {} for {}",
                        capsule.serial, pin.serial, self.domain
                    ),
                ));
            }
            if capsule.serial == pin.serial && digest != pin.capsules_digest {
                return Err(HyphaError::new(
                    "domain_state_equivocation",
                    format!(
                        "cmn.json serial {} for {} has a different capsules digest than the local pin",
                        capsule.serial, self.domain
                    ),
                ));
            }
            if capsule.serial.saturating_sub(pin.serial) > DOMAIN_STATE_JUMP_THRESHOLD {
                return Err(HyphaError::new(
                    "domain_state_jump",
                    format!(
                        "cmn.json serial jumped from {} to {} for {}",
                        pin.serial, capsule.serial, self.domain
                    ),
                ));
            }
            if capsule.key != pin.current_key {
                capsule
                    .verify_rotation_chain_from(&pin.current_key)
                    .map_err(|e| {
                        HyphaError::new(
                            "domain_key_rotation_unproven",
                            format!(
                                "cmn.json key changed for {} without a valid rotation chain: {e}",
                                self.domain
                            ),
                        )
                    })?;
            }
        }

        self.save_domain_state(&DomainStatePin {
            serial: capsule.serial,
            capsules_digest: digest,
            current_key: capsule.key.clone(),
            pinned_at_epoch_ms: crate::time::now_epoch_ms(),
        })
    }

    fn save_domain_state(&self, pin: &DomainStatePin) -> Result<(), crate::sink::HyphaError> {
        let dir = self.mycelium_dir();
        std::fs::create_dir_all(&dir).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to create mycelium dir: {}", e),
            )
        })?;

        let content = serde_json::to_string_pretty(pin).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to serialize domain state: {}", e),
            )
        })?;

        locked_write_file(&self.domain_state_path(), &content)
    }

    /// Get path to mycelium.json (complete manifest with spores list)
    pub fn mycelium_path(&self) -> PathBuf {
        self.mycelium_dir().join("mycelium.json")
    }

    /// Load cached full mycelium manifest
    pub fn load_mycelium(&self) -> Option<serde_json::Value> {
        let path = self.mycelium_path();
        if path.exists() {
            std::fs::read_to_string(&path)
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
        } else {
            None
        }
    }

    /// Save full mycelium manifest to cache
    pub fn save_mycelium(
        &self,
        mycelium: &serde_json::Value,
    ) -> Result<(), crate::sink::HyphaError> {
        let dir = self.mycelium_dir();
        std::fs::create_dir_all(&dir).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to create mycelium dir: {}", e),
            )
        })?;

        let content = crate::mycelium::format_mycelium(mycelium).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to serialize mycelium: {}", e),
            )
        })?;

        locked_write_file(&self.mycelium_path(), &content)
    }

    /// Get path to status.json
    pub fn status_path(&self) -> PathBuf {
        self.mycelium_dir().join("status.json")
    }

    /// Load cache status
    pub fn load_status(&self) -> CacheStatus {
        let path = self.status_path();
        if path.exists() {
            std::fs::read_to_string(&path)
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default()
        } else {
            CacheStatus::default()
        }
    }

    /// Save cache status
    pub fn save_status(&self, status: &CacheStatus) -> Result<(), crate::sink::HyphaError> {
        let dir = self.mycelium_dir();
        std::fs::create_dir_all(&dir).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to create mycelium dir: {}", e),
            )
        })?;

        let content = serde_json::to_string_pretty(status).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to serialize status: {}", e),
            )
        })?;

        locked_write_file(&self.status_path(), &content)
    }

    /// Get path to domain-level taste.json
    pub fn domain_taste_path(&self) -> PathBuf {
        self.mycelium_dir().join("taste.json")
    }

    /// Load cached taste verdict for the domain itself
    pub fn load_domain_taste(&self) -> Option<TasteVerdictCache> {
        let path = self.domain_taste_path();
        if path.exists() {
            std::fs::read_to_string(&path)
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
        } else {
            None
        }
    }

    /// Save taste verdict for the domain itself
    pub fn save_domain_taste(
        &self,
        verdict: &TasteVerdictCache,
    ) -> Result<(), crate::sink::HyphaError> {
        let dir = self.mycelium_dir();
        std::fs::create_dir_all(&dir).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to create mycelium dir: {}", e),
            )
        })?;

        let content = serde_json::to_string_pretty(verdict).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to serialize domain taste verdict: {}", e),
            )
        })?;

        locked_write_file(&self.domain_taste_path(), &content)
    }

    /// Get path to taste.json for a spore
    pub fn taste_path(&self, hash: &str) -> PathBuf {
        self.spore_path(hash).join("taste.json")
    }

    /// Load cached taste verdict for a spore
    pub fn load_taste(&self, hash: &str) -> Option<TasteVerdictCache> {
        let path = self.taste_path(hash);
        if path.exists() {
            std::fs::read_to_string(&path)
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
        } else {
            None
        }
    }

    /// Save taste verdict for a spore
    pub fn save_taste(
        &self,
        hash: &str,
        verdict: &TasteVerdictCache,
    ) -> Result<(), crate::sink::HyphaError> {
        let dir = self.spore_path(hash);
        std::fs::create_dir_all(&dir).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to create spore dir: {}", e),
            )
        })?;

        let content = serde_json::to_string_pretty(verdict).map_err(|e| {
            HyphaError::new(
                "cache_write_failed",
                format!("Failed to serialize taste verdict: {}", e),
            )
        })?;

        locked_write_file(&self.taste_path(hash), &content)
    }

    /// Get path to key_trust.json for this domain
    pub fn key_trust_path(&self) -> PathBuf {
        self.mycelium_dir().join("key_trust.json")
    }

    /// Load cached key trust entries
    pub fn load_key_trust(&self) -> Vec<KeyTrustEntry> {
        let path = self.key_trust_path();
        if path.exists() {
            std::fs::read_to_string(&path)
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default()
        } else {
            Vec::new()
        }
    }

    /// Save a key trust entry (domain confirmed this key).
    ///
    /// Read-modify-write happens under the directory lock so concurrent
    /// confirmations can't clobber each other's entries.
    pub fn save_key_trust(&self, key: &str) -> Result<(), crate::sink::HyphaError> {
        self.save_key_trust_with_retirement(key, None)
    }

    /// Save a key trust entry with the retirement cutoff advertised by cmn.json.
    pub fn save_key_trust_with_retirement(
        &self,
        key: &str,
        retired_at_epoch_ms: Option<u64>,
    ) -> Result<(), crate::sink::HyphaError> {
        locked_update_file(&self.key_trust_path(), |existing| {
            let mut entries: Vec<KeyTrustEntry> = existing
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            if let Some(entry) = entries.iter_mut().find(|e| e.key == key) {
                entry.confirmed_at_epoch_ms = crate::time::now_epoch_ms();
                entry.retired_at_epoch_ms = retired_at_epoch_ms;
            } else {
                entries.push(KeyTrustEntry {
                    key: key.to_string(),
                    confirmed_at_epoch_ms: crate::time::now_epoch_ms(),
                    retired_at_epoch_ms,
                });
            }
            serde_json::to_string_pretty(&entries).map_err(|e| {
                HyphaError::new(
                    "cache_write_failed",
                    format!("Failed to serialize key trust: {}", e),
                )
            })
        })
    }

    /// Check if a key is trusted within the given TTL (milliseconds).
    /// Applies clock skew tolerance to prevent false negatives from clock drift.
    pub fn is_key_trusted(&self, key: &str, ttl_ms: u64, clock_skew_tolerance_ms: u64) -> bool {
        self.is_key_trusted_entry(key, ttl_ms, clock_skew_tolerance_ms)
            .is_some()
    }

    /// Check if a key is trusted for content signed at the given timestamp.
    pub fn is_key_trusted_for_time(
        &self,
        key: &str,
        signed_at_epoch_ms: u64,
        ttl_ms: u64,
        clock_skew_tolerance_ms: u64,
    ) -> bool {
        self.is_key_trusted_entry(key, ttl_ms, clock_skew_tolerance_ms)
            .map(|entry| {
                entry
                    .retired_at_epoch_ms
                    .map(|retired_at| signed_at_epoch_ms <= retired_at)
                    .unwrap_or(true)
            })
            .unwrap_or(false)
    }

    fn is_key_trusted_entry(
        &self,
        key: &str,
        ttl_ms: u64,
        clock_skew_tolerance_ms: u64,
    ) -> Option<KeyTrustEntry> {
        let entries = self.load_key_trust();
        let now = crate::time::now_epoch_ms();
        let effective_ttl = ttl_ms.saturating_add(clock_skew_tolerance_ms);
        entries
            .into_iter()
            .find(|e| e.key == key && now.saturating_sub(e.confirmed_at_epoch_ms) < effective_ttl)
    }

    /// Update cmn.json fetch status (locked read-modify-write).
    pub fn update_cmn_status(&self, success: bool, error: Option<&str>) {
        let _ = locked_update_file(&self.status_path(), |existing| {
            let mut status: CacheStatus = existing
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            if success {
                status.cmn = FetchStatus::success();
            } else {
                status.cmn =
                    FetchStatus::failure(error.unwrap_or("Unknown error"), Some(&status.cmn));
            }
            serde_json::to_string_pretty(&status).map_err(|e| {
                HyphaError::new(
                    "cache_write_failed",
                    format!("Failed to serialize status: {}", e),
                )
            })
        });
    }
}