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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
use super::*;

/// Taste — evaluate spore safety (library level).
///
/// Two modes:
/// - Download mode (verdict=None): fetch spore to cache and return metadata
/// - Record mode (verdict=Some): record a taste verdict for a cached spore
#[allow(clippy::too_many_arguments)]
pub async fn taste(
    uri_str: &str,
    verdict: Option<substrate::TasteVerdict>,
    notes: Option<&str>,
    synapse_url: Option<&str>,
    synapse_token_secret: Option<&str>,
    domain_for_signing: Option<&str>,
    sink: &dyn crate::EventSink,
) -> Result<crate::output::TasteOutput, crate::HyphaError> {
    taste_at(
        uri_str,
        verdict,
        notes,
        synapse_url,
        synapse_token_secret,
        domain_for_signing,
        crate::time::now_epoch_ms(),
        sink,
    )
    .await
}

#[allow(clippy::too_many_arguments)]
async fn taste_at(
    uri_str: &str,
    verdict: Option<substrate::TasteVerdict>,
    notes: Option<&str>,
    synapse_url: Option<&str>,
    synapse_token_secret: Option<&str>,
    domain_for_signing: Option<&str>,
    now_epoch_ms: u64,
    sink: &dyn crate::EventSink,
) -> Result<crate::output::TasteOutput, crate::HyphaError> {
    let uri = CmnUri::parse(uri_str).map_err(|e| crate::HyphaError::new("invalid_uri", e))?;

    match uri.hash.clone() {
        Some(hash) => match verdict {
            Some(v) => taste_record_lib(
                sink,
                uri_str,
                &uri,
                &hash,
                v,
                notes,
                synapse_url,
                synapse_token_secret,
                domain_for_signing,
                now_epoch_ms,
            )
            .await
            .map(crate::output::TasteOutput::Record),
            None => taste_download_lib(
                sink,
                uri_str,
                &uri,
                &hash,
                synapse_url,
                synapse_token_secret,
            )
            .await
            .map(crate::output::TasteOutput::Download),
        },
        None => match verdict {
            Some(v) => taste_domain_record_lib(sink, uri_str, &uri, v, notes, now_epoch_ms)
                .map(crate::output::TasteOutput::Record),
            None => taste_domain_download_lib(sink, uri_str, &uri)
                .await
                .map(crate::output::TasteOutput::Download),
        },
    }
}

async fn taste_download_lib(
    sink: &dyn crate::EventSink,
    uri_str: &str,
    uri: &CmnUri,
    hash: &str,
    synapse_url: Option<&str>,
    synapse_token_secret: Option<&str>,
) -> Result<crate::output::TasteDownloadOutput, crate::HyphaError> {
    let cache = CacheDir::new()?;
    fetch_spore_to_cache(sink, &cache, uri_str).await?;
    let domain_cache = cache.domain(&uri.domain);
    let spore_path = domain_cache.spore_path(hash);

    let manifest_path = spore_path.join("spore.json");
    let manifest = std::fs::read_to_string(&manifest_path)
        .ok()
        .and_then(|s| serde_json::from_str::<substrate::Spore>(&s).ok());

    let (name, synopsis) = manifest
        .as_ref()
        .map(|spore| {
            (
                spore.capsule.core.name.clone(),
                spore.capsule.core.synopsis.clone(),
            )
        })
        .unwrap_or_default();

    let taste_verdict = domain_cache
        .load_taste(hash)
        .map(|t| crate::output::TasteVerdict {
            verdict: t.verdict,
            notes: t.notes,
            tasted_at_epoch_ms: t.tasted_at_epoch_ms,
        });

    let parent = manifest
        .as_ref()
        .and_then(|spore| spore.spawned_from_uri().map(str::to_string));

    let remote_tastes = if let Some(synapse_arg) = synapse_url {
        match crate::config::resolve_synapse(Some(synapse_arg), synapse_token_secret) {
            Ok(resolved) => {
                match fetch_taste_reports(&resolved.url, hash, resolved.token_secret.as_deref())
                    .await
                {
                    Ok(tastes) => Some(tastes),
                    Err(e) => {
                        sink.emit(crate::HyphaEvent::Warn {
                            message: format!("Failed to fetch taste reports: {}", e),
                        });
                        None
                    }
                }
            }
            Err(e) => {
                sink.emit(crate::HyphaEvent::Warn {
                    message: format!("Failed to resolve synapse: {}", e),
                });
                None
            }
        }
    } else {
        None
    };

    Ok(crate::output::TasteDownloadOutput {
        uri: uri_str.to_string(),
        cache_path: spore_path.display().to_string(),
        name: if name.is_empty() { None } else { Some(name) },
        synopsis: if synopsis.is_empty() {
            None
        } else {
            Some(synopsis)
        },
        parent,
        taste: taste_verdict,
        remote_tastes,
    })
}

#[allow(clippy::too_many_arguments)]
async fn taste_record_lib(
    sink: &dyn crate::EventSink,
    uri_str: &str,
    uri: &CmnUri,
    hash: &str,
    verdict: substrate::TasteVerdict,
    notes: Option<&str>,
    synapse_url: Option<&str>,
    synapse_token_secret: Option<&str>,
    domain_for_signing: Option<&str>,
    now_epoch_ms: u64,
) -> Result<crate::output::TasteRecordOutput, crate::HyphaError> {
    let cache = CacheDir::new()?;
    let domain_cache = cache.domain(&uri.domain);

    let spore_path = domain_cache.spore_path(hash);
    if !spore_path.exists() {
        return Err(crate::HyphaError::with_hint(
            "NOT_CACHED",
            "Spore not cached",
            format!("run: hypha taste {}", uri_str),
        ));
    }

    let taste_cache = TasteVerdictCache {
        verdict,
        notes: notes.map(|n| n.to_string()),
        tasted_at_epoch_ms: now_epoch_ms,
    };

    domain_cache.save_taste(hash, &taste_cache)?;

    let mut output = crate::output::TasteRecordOutput {
        uri: uri_str.to_string(),
        verdict,
        notes: notes.map(|n| n.to_string()),
        tasted_at_epoch_ms: taste_cache.tasted_at_epoch_ms,
        shared: None,
        synapse: None,
        share_error: None,
    };

    // Resolve domain + synapse: CLI args > [taste] config > [defaults] config
    let config = crate::config::HyphaConfig::load()?;
    let effective_domain = domain_for_signing
        .or(config.defaults.taste.domain.as_deref())
        .or(config.defaults.domain.as_deref());
    let effective_synapse = synapse_url
        .or(config.defaults.taste.synapse.as_deref())
        .or(config.defaults.synapse.as_deref());

    if let (Some(signing_domain), Some(synapse_arg)) = (effective_domain, effective_synapse) {
        match crate::config::resolve_synapse(Some(synapse_arg), synapse_token_secret) {
            Ok(resolved) => {
                match share_taste_report_lib(
                    uri_str,
                    verdict,
                    notes,
                    signing_domain,
                    &resolved.url,
                    resolved.token_secret.as_deref(),
                    now_epoch_ms,
                )
                .await
                {
                    Ok(_) => {
                        output.shared = Some(true);
                        output.synapse = Some(resolved.url);
                    }
                    Err(e) => {
                        sink.emit(crate::HyphaEvent::Warn {
                            message: format!("Failed to share taste report: {}", e),
                        });
                        output.shared = Some(false);
                        output.share_error = Some(e.to_string());
                    }
                }
            }
            Err(e) => {
                sink.emit(crate::HyphaEvent::Warn {
                    message: format!("Failed to resolve synapse: {}", e),
                });
                output.shared = Some(false);
                output.share_error = Some(e.to_string());
            }
        }
    }

    Ok(output)
}

/// Taste download for domain-only URI: resolve domain, show mycelium info.
async fn taste_domain_download_lib(
    sink: &dyn crate::EventSink,
    uri_str: &str,
    uri: &CmnUri,
) -> Result<crate::output::TasteDownloadOutput, crate::HyphaError> {
    let cache = CacheDir::new()?;
    let domain_cache = cache.domain(&uri.domain);

    let entry = get_cmn_entry(sink, &domain_cache, cache.cmn_ttl_ms).await?;

    let _capsule = primary_capsule(&entry)
        .map_err(|e| crate::HyphaError::new("manifest_failed", e.message))?;

    let name = entry
        .capsules
        .first()
        .map(|c| c.uri.as_str())
        .unwrap_or(&uri.domain)
        .to_string();

    let taste_verdict = domain_cache
        .load_domain_taste()
        .map(|t| crate::output::TasteVerdict {
            verdict: t.verdict,
            notes: t.notes,
            tasted_at_epoch_ms: t.tasted_at_epoch_ms,
        });

    Ok(crate::output::TasteDownloadOutput {
        uri: uri_str.to_string(),
        cache_path: domain_cache.mycelium_dir().display().to_string(),
        name: Some(name),
        synopsis: None,
        parent: None,
        taste: taste_verdict,
        remote_tastes: None,
    })
}

/// Taste record for domain-only URI: record verdict for the domain.
fn taste_domain_record_lib(
    _sink: &dyn crate::EventSink,
    uri_str: &str,
    uri: &CmnUri,
    verdict: substrate::TasteVerdict,
    notes: Option<&str>,
    now_epoch_ms: u64,
) -> Result<crate::output::TasteRecordOutput, crate::HyphaError> {
    let cache = CacheDir::new()?;
    let domain_cache = cache.domain(&uri.domain);

    if !domain_cache.mycelium_dir().exists() {
        return Err(crate::HyphaError::with_hint(
            "NOT_CACHED",
            "Domain not cached",
            format!("run: hypha taste {}", uri_str),
        ));
    }

    let taste_cache = TasteVerdictCache {
        verdict,
        notes: notes.map(|n| n.to_string()),
        tasted_at_epoch_ms: now_epoch_ms,
    };

    domain_cache.save_domain_taste(&taste_cache)?;

    Ok(crate::output::TasteRecordOutput {
        uri: uri_str.to_string(),
        verdict,
        notes: notes.map(|n| n.to_string()),
        tasted_at_epoch_ms: taste_cache.tasted_at_epoch_ms,
        shared: None,
        synapse: None,
        share_error: None,
    })
}

/// Check taste verdict before operations like spawn/grow/absorb.
///
/// Library-level version: uses `EventSink` for warnings and returns
/// `HyphaError` on failure.
pub fn check_taste(
    sink: &dyn crate::EventSink,
    cache: &CacheDir,
    uri_str: &str,
    domain: &str,
    hash: &str,
) -> Result<(), crate::HyphaError> {
    check_taste_for_operation(
        sink,
        cache,
        uri_str,
        domain,
        hash,
        substrate::GateOperation::Spawn,
    )
}

fn check_taste_for_operation(
    sink: &dyn crate::EventSink,
    cache: &CacheDir,
    uri_str: &str,
    domain: &str,
    hash: &str,
    operation: substrate::GateOperation,
) -> Result<(), crate::HyphaError> {
    let domain_cache = cache.domain(domain);

    let cached_taste = domain_cache.load_taste(hash);
    let verdict = cached_taste.as_ref().map(|taste| taste.verdict);

    match substrate::TasteVerdict::gate_action_for(operation, verdict) {
        substrate::GateAction::Block if cached_taste.is_none() => {
            Err(crate::HyphaError::with_hint(
                "NOT_TASTED",
                "Spore has not been tasted",
                format!(
                    "run: hypha taste {} && hypha taste {} --verdict safe",
                    uri_str, uri_str
                ),
            ))
        }
        substrate::GateAction::Block => {
            let note_suffix = cached_taste
                .and_then(|taste| taste.notes.as_ref().map(|note| format!(": {}", note)))
                .unwrap_or_default();
            Err(crate::HyphaError::new(
                "TOXIC",
                format!("Spore was marked as toxic{}", note_suffix),
            ))
        }
        substrate::GateAction::Warn => {
            let note_suffix = cached_taste
                .and_then(|taste| taste.notes.as_ref().map(|note| format!(": {}", note)))
                .unwrap_or_default();
            sink.emit(crate::HyphaEvent::Warn {
                message: format!(
                    "Spore was marked as rotten{}. Recommend sandboxed environment.",
                    note_suffix
                ),
            });
            Ok(())
        }
        substrate::GateAction::Proceed => Ok(()),
    }
}

pub fn check_taste_verdict_for_replicate(
    out: &Output,
    cache: &CacheDir,
    uri_str: &str,
    domain: &str,
    hash: &str,
) -> Result<(), ExitCode> {
    check_taste_verdict(
        out,
        cache,
        uri_str,
        domain,
        hash,
        substrate::GateOperation::Replicate,
    )
}

/// CLI wrapper for taste check — used by `handle_*` functions and `spore::handle_replicate`.
fn check_taste_verdict(
    out: &Output,
    cache: &CacheDir,
    uri_str: &str,
    domain: &str,
    hash: &str,
    operation: substrate::GateOperation,
) -> Result<(), ExitCode> {
    let sink = crate::api::OutSink(out);
    check_taste_for_operation(&sink, cache, uri_str, domain, hash, operation)
        .map_err(|e| out.error_hypha(&e))
}

/// Share a signed taste report to a Synapse instance via pulse (library level, no Output).
#[allow(clippy::too_many_arguments)]
async fn share_taste_report_lib(
    uri_str: &str,
    verdict: substrate::TasteVerdict,
    notes: Option<&str>,
    signing_domain: &str,
    synapse_url: &str,
    synapse_token: Option<&str>,
    now_epoch_ms: u64,
) -> Result<(), crate::HyphaError> {
    crate::site::validate_site_domain_path(signing_domain)?;
    let site = crate::site::SiteDir::new(signing_domain);

    if !site.private_key_path().exists() {
        return Err(crate::HyphaError::new(
            "identity_not_found",
            format!("No identity found for domain '{}'", signing_domain),
        ));
    }

    let target_uri = substrate::normalize_taste_target_uri(uri_str).map_err(|e| {
        crate::HyphaError::new(
            "invalid_uri",
            format!("Invalid target URI '{}': {}", uri_str, e),
        )
    })?;

    let identity = crate::auth::get_identity_with_site(signing_domain, &site).map_err(|e| {
        crate::HyphaError::new("identity_error", format!("Failed to load identity: {}", e))
    })?;

    let core = substrate::TasteCore {
        target_uri: target_uri.clone(),
        domain: signing_domain.to_string(),
        key: identity.public_key.clone(),
        verdict,
        notes: notes.map(|note| vec![note.to_string()]).unwrap_or_default(),
        tasted_at_epoch_ms: now_epoch_ms,
    };

    let core_signature = crate::auth::sign_json_with_site(&site, &core).map_err(|e| match e {
        crate::auth::JsonSignError::Jcs(message) => crate::HyphaError::new("jcs_error", message),
        crate::auth::JsonSignError::Sign(err) => {
            crate::HyphaError::new("sign_error", format!("Failed to sign core: {}", err))
        }
    })?;

    let mut payload = substrate::Taste {
        schema: substrate::TASTE_SCHEMA.to_string(),
        capsule: substrate::TasteCapsule {
            uri: String::new(),
            core,
            core_signature,
        },
        capsule_signature: String::new(),
    };

    let taste_hash = payload.computed_uri_hash().map_err(|e| {
        crate::HyphaError::new(
            "jcs_error",
            format!("JCS hash input serialization failed: {}", e),
        )
    })?;
    payload.capsule.uri = substrate::build_taste_uri(signing_domain, &taste_hash);

    let capsule_signature =
        crate::auth::sign_json_with_site(&site, &payload.capsule).map_err(|e| match e {
            crate::auth::JsonSignError::Jcs(message) => {
                crate::HyphaError::new("jcs_error", message)
            }
            crate::auth::JsonSignError::Sign(err) => {
                crate::HyphaError::new("sign_error", format!("Failed to sign capsule: {}", err))
            }
        })?;
    payload.capsule_signature = capsule_signature;

    // Validate against schema before writing/sending
    let payload_value = serde_json::to_value(&payload).map_err(|e| {
        crate::HyphaError::new(
            "serialize_error",
            format!("Failed to serialize taste report: {}", e),
        )
    })?;
    if let Err(e) = substrate::validate_schema(&payload_value) {
        return Err(crate::HyphaError::new(
            "schema_error",
            format!("Taste schema validation failed: {}", e),
        ));
    }

    // Write to site's taste dir for local persistence
    let taste_dir = site.taste_dir();
    if let Err(e) = std::fs::create_dir_all(&taste_dir) {
        return Err(crate::HyphaError::new(
            "write_error",
            format!("Failed to create taste dir: {}", e),
        ));
    }
    let taste_path = taste_dir.join(format!("{}.json", taste_hash));
    let payload_json = payload.to_pretty_json().map_err(|e| {
        crate::HyphaError::new(
            "serialize_error",
            format!("Failed to format taste report: {}", e),
        )
    })?;
    std::fs::write(&taste_path, &payload_json).map_err(|e| {
        crate::HyphaError::new(
            "write_error",
            format!(
                "Failed to write taste report to {}: {}",
                taste_path.display(),
                e
            ),
        )
    })?;

    let client = substrate::client::http_client(30).map_err(|e| {
        crate::HyphaError::new(
            "synapse_error",
            format!("Failed to create HTTP client: {}", e),
        )
    })?;
    let opts = fetch_opts(synapse_token);
    substrate::client::post_synapse_pulse(&client, synapse_url, &payload_value, opts)
        .await
        .map_err(|e| crate::HyphaError::new("synapse_error", e.to_string()))?;

    Ok(())
}

/// Fetch taste reports for a spore from a Synapse instance
async fn fetch_taste_reports(
    synapse_url: &str,
    hash: &str,
    token: Option<&str>,
) -> Result<serde_json::Value, crate::HyphaError> {
    let client = substrate::client::http_client(30).map_err(|e| {
        crate::HyphaError::new(
            "synapse_error",
            format!("Failed to create HTTP client: {}", e),
        )
    })?;
    substrate::client::fetch_taste_reports(&client, synapse_url, hash, fetch_opts(token))
        .await
        .map_err(|e| crate::HyphaError::new("synapse_error", e.to_string()))
}

#[allow(clippy::too_many_arguments)]
pub async fn handle_taste(
    out: &Output,
    uri_str: &str,
    verdict: Option<substrate::TasteVerdict>,
    notes: Option<&str>,
    synapse_url: Option<&str>,
    synapse_token_secret: Option<&str>,
    domain_for_signing: Option<&str>,
) -> ExitCode {
    let sink = crate::api::OutSink(out);
    match taste_at(
        uri_str,
        verdict,
        notes,
        synapse_url,
        synapse_token_secret,
        domain_for_signing,
        crate::time::now_epoch_ms(),
        &sink,
    )
    .await
    {
        Ok(output) => out.ok(serde_json::to_value(output).unwrap_or_default()),
        Err(e) => out.error_hypha(&e),
    }
}