lsp-max 26.7.3

Law-state LSP runtime: max LSP 3.18 coverage, process-mining conformance, receipt-chain admission
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Law table extension: periodic table of cognition breed correctness.
//!
//! Emits diagnostics (A8–A12 adversary classes + structural completeness) for every
//! PARTIAL_ALIVE breed in ../wasm4pm/crates/wasm4pm-cognition/breeds/registry.json.
//! Designed to run as a workspace-level diagnostic gate — integrates with `update_diagnostics`
//! and the lsp-max law engine.
//!
//! Laws enforced:
//!   COG-001  Missing breed module (.rs file)
//!   COG-002  Missing OCPN model (ocel/models/l1/`<breed>`.ocpn.json)
//!   COG-003  Missing OCEL fitness report (ocel/reports/`<breed>`.json)
//!   COG-004  Missing paper fixture (tests/fixtures/papers/`<breed>`.json)
//!   COG-005  Fixture missing expected.value — citation without assertion (A12)
//!   COG-006  OCEL report fitness != 1.0
//!   COG-007  OCEL report missing measured-fitness provenance (A10 evidence gap)
//!   COG-008  Missing docs card (docs/breeds/`<breed>`.md)
//!   COG-009  Missing TS fixture mirror (packages/cognition/src/__tests__/fixtures/papers/)
//!   COG-010  Oracle identifier leaked into production source (A8 — fresh-name violation)
//!   COG-011  Premature PARTIAL_ALIVE: registry flipped but required artifacts absent (A10)
//!   COG-012  Missing BreedId dispatch arm (breed not wired in dispatch.rs)

use lsp_types_max::DiagnosticSeverity;
use std::path::Path;
use wasm4pm_compat::fresh_names::FRESH_NAME_PAIRS;

/// Severity mapping per law — matches the ARD's defect classification.
fn severity(law: &str) -> DiagnosticSeverity {
    match law {
        // Hard failures — breed cannot be certified without these
        "COG-001" | "COG-002" | "COG-004" | "COG-010" | "COG-011" | "COG-012" => {
            DiagnosticSeverity::ERROR
        }
        // Evidence gaps — certification claim weakened
        "COG-003" | "COG-005" | "COG-006" | "COG-007" => DiagnosticSeverity::ERROR,
        // Completeness warnings — ceremony not fully done
        "COG-008" | "COG-009" => DiagnosticSeverity::WARNING,
        _ => DiagnosticSeverity::INFORMATION,
    }
}

/// One diagnostic finding for a single breed.
#[derive(Debug, Clone)]
pub struct BreedDiagnostic {
    /// Law identifier, e.g. `"COG-001"`.
    pub law_id: &'static str,
    /// The breed id that violated the law.
    pub breed_id: String,
    /// Human-readable diagnostic message.
    pub message: String,
    /// LSP severity level.
    pub severity: DiagnosticSeverity,
    /// Path of the missing/violating artifact (for LSP range attachment).
    pub artifact_path: String,
}

/// Derive the wasm4pm workspace root from a given `root_path`.
/// Handles both "we're inside wasm4pm" and "we're in lsp-max with ../wasm4pm sibling".
fn wasm4pm_root(root_path: &Path) -> std::path::PathBuf {
    // If registry.json exists here, we're already inside wasm4pm.
    let direct = root_path.join("crates/wasm4pm-cognition/breeds/registry.json");
    if direct.exists() {
        return root_path.to_path_buf();
    }
    // Otherwise assume sibling checkout.
    root_path.parent().unwrap_or(root_path).join("wasm4pm")
}

/// Parse dispatch.rs to build a map of breed_id → module stem that actually
/// implements it.  Falls back to `breed_id` itself when no override is found.
/// This is the source-of-truth derivation: convention-free, self-updating.
///
/// Two-pass approach:
/// 1. Parse all `module::Struct` tokens from use declarations to build struct→module.
/// 2. Parse `"breed_id" => run_breed(&Struct, ...)` match arms to bind breed_id→module.
fn dispatch_module_map(dispatch_src: &str) -> std::collections::BTreeMap<String, String> {
    let mut struct_to_module: std::collections::BTreeMap<String, String> =
        std::collections::BTreeMap::new();

    // Pass 1: extract all `module::Struct` tokens from the source.
    // Split the entire file on whitespace/commas/braces/parens to get tokens.
    for token in dispatch_src.split(|c: char| {
        c == ','
            || c == '{'
            || c == '}'
            || c == '('
            || c == ')'
            || c == '\n'
            || c == '\t'
            || c == ' '
    }) {
        let token = token.trim();
        // Skip comment fragments
        if token.starts_with("//") {
            continue;
        }
        if let Some(pos) = token.find("::") {
            let module_part = token[..pos].trim();
            let struct_part = token[pos + 2..].trim();
            // module must be all lowercase snake_case; struct must start with uppercase
            if !module_part.is_empty()
                && !struct_part.is_empty()
                && module_part.chars().all(|c| c.is_alphanumeric() || c == '_')
                && struct_part.chars().next().is_some_and(|c| c.is_uppercase())
                && module_part != "crate"
                && module_part != "super"
                && module_part != "self"
            {
                struct_to_module
                    .entry(struct_part.to_string())
                    .or_insert_with(|| module_part.to_string());
            }
        }
    }

    // Pass 2: match `"breed_id" => run_breed(&StructName, ...)` or `"breed_id" => StructName.run(...)` arms.
    let mut map = std::collections::BTreeMap::new();
    for line in dispatch_src.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix('"') {
            if let Some(end) = rest.find('"') {
                let breed_id = &rest[..end];
                let after = rest[end + 1..].trim();
                if let Some(arm_raw) = after.strip_prefix("=>") {
                    let arm = arm_raw.trim();
                    // `run_breed(&StructName, ...)` or `StructName.run(...)`
                    let struct_name = if let Some(inner) = arm.strip_prefix("run_breed(&") {
                        inner.split(',').next().unwrap_or("").trim().to_string()
                    } else {
                        arm.split(['.', '(', ' '])
                            .next()
                            .unwrap_or("")
                            .trim()
                            .to_string()
                    };
                    if !struct_name.is_empty() {
                        let module =
                            struct_to_module
                                .get(&struct_name)
                                .cloned()
                                .unwrap_or_else(|| {
                                    // Convention fallback: PascalCase → snake_case
                                    {
                                        let mut snake =
                                            String::with_capacity(struct_name.len() + 4);
                                        for (i, c) in struct_name.chars().enumerate() {
                                            if c.is_uppercase() && i > 0 {
                                                snake.push('_');
                                                snake.push(c.to_ascii_lowercase());
                                            } else {
                                                snake.push(c.to_ascii_lowercase());
                                            }
                                        }
                                        snake
                                    }
                                });
                        map.insert(breed_id.to_string(), module);
                    }
                }
            }
        }
    }
    map
}

/// Run all cognition breed laws against the workspace at `root_path`.
/// Returns a list of diagnostics — empty means all laws satisfied.
pub fn audit_breeds(root_path: &Path) -> Vec<BreedDiagnostic> {
    let wasm4pm = wasm4pm_root(root_path);
    let registry_path = wasm4pm.join("crates/wasm4pm-cognition/breeds/registry.json");

    let registry_json = match std::fs::read_to_string(&registry_path) {
        Ok(s) => s,
        Err(_) => return vec![], // registry absent — not a wasm4pm workspace, skip silently
    };

    let registry: serde_json::Value = match serde_json::from_str(&registry_json) {
        Ok(v) => v,
        Err(_) => {
            return vec![BreedDiagnostic {
                law_id: "COG-000",
                breed_id: "registry".into(),
                message: "breeds/registry.json is not valid JSON".into(),
                severity: DiagnosticSeverity::ERROR,
                artifact_path: registry_path.to_string_lossy().into(),
            }]
        }
    };

    // Derive module locations from dispatch.rs — source-of-truth, not convention.
    let dispatch_path = wasm4pm.join("crates/wasm4pm-cognition/src/breeds/dispatch.rs");
    let dispatch_src = std::fs::read_to_string(&dispatch_path).unwrap_or_default();
    let module_map = dispatch_module_map(&dispatch_src);

    // Support both array-of-objects and {breeds: [...]} shapes.
    let breeds_val = registry.get("breeds").unwrap_or(&registry);

    let breeds = match breeds_val.as_array() {
        Some(a) => a.clone(),
        None => {
            if let Some(obj) = breeds_val.as_object() {
                obj.iter()
                    .map(|(k, v)| {
                        let mut entry = v.clone();
                        if let Some(o) = entry.as_object_mut() {
                            o.entry("breed_id")
                                .or_insert(serde_json::Value::String(k.clone()));
                        }
                        entry
                    })
                    .collect()
            } else {
                return vec![];
            }
        }
    };

    let mut diags: Vec<BreedDiagnostic> = Vec::new();

    for breed in &breeds {
        let bid = match breed
            .get("breed_id")
            .and_then(|v| v.as_str())
            .or_else(|| breed.get("id").and_then(|v| v.as_str()))
        {
            Some(s) => s.to_string(),
            None => continue,
        };

        let status = breed
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("UNKNOWN");

        // Only audit breeds that claim to be implemented.
        if status != "PARTIAL_ALIVE" {
            continue;
        }

        // Derive module path from dispatch.rs parse — not from naming convention.
        let module_stem = module_map.get(&bid).cloned().unwrap_or_else(|| bid.clone());
        let module_path_str = format!("crates/wasm4pm-cognition/src/breeds/{module_stem}.rs");
        let module = wasm4pm.join(&module_path_str);
        let ocpn = wasm4pm.join(format!("ocel/models/l1/{bid}.ocpn.json"));
        let report = wasm4pm.join(format!("ocel/reports/{bid}.json"));
        let fix_rs = wasm4pm.join(format!(
            "crates/wasm4pm-cognition/tests/fixtures/papers/{bid}.json"
        ));
        let fix_ts = wasm4pm.join(format!(
            "packages/cognition/src/__tests__/fixtures/papers/{bid}.json"
        ));
        let doc = wasm4pm.join(format!("docs/breeds/{bid}.md"));

        // COG-001: breed module missing
        if !module.exists() {
            diags.push(BreedDiagnostic {
                law_id: "COG-001",
                breed_id: bid.clone(),
                message: format!("[COG-001] PARTIAL_ALIVE breed '{bid}' has no implementation module — {module_path_str} missing"),
                severity: severity("COG-001"),
                artifact_path: module.to_string_lossy().into(),
            });
        }

        // COG-002: OCPN model missing
        if !ocpn.exists() {
            diags.push(BreedDiagnostic {
                law_id: "COG-002",
                breed_id: bid.clone(),
                message: format!("[COG-002] '{bid}' OCPN model missing — ocel/models/l1/{bid}.ocpn.json required for conformance gate"),
                severity: severity("COG-002"),
                artifact_path: ocpn.to_string_lossy().into(),
            });
        }

        // COG-003 / COG-006 / COG-007: report missing or invalid
        if !report.exists() {
            diags.push(BreedDiagnostic {
                law_id: "COG-003",
                breed_id: bid.clone(),
                message: format!("[COG-003] '{bid}' OCEL fitness report missing — ocel/reports/{bid}.json must be earned (A10 evidence gap)"),
                severity: severity("COG-003"),
                artifact_path: report.to_string_lossy().into(),
            });
        } else if let Ok(rjson) = std::fs::read_to_string(&report) {
            if let Ok(rv) = serde_json::from_str::<serde_json::Value>(&rjson) {
                let fitness = rv.get("fitness").and_then(|v| v.as_f64()).unwrap_or(0.0);
                if (fitness - 1.0).abs() > 1e-9 {
                    diags.push(BreedDiagnostic {
                        law_id: "COG-006",
                        breed_id: bid.clone(),
                        message: format!(
                            "[COG-006] '{bid}' OCEL fitness report claims {fitness:.6} != 1.0"
                        ),
                        severity: severity("COG-006"),
                        artifact_path: report.to_string_lossy().into(),
                    });
                }
                // run_id may be nested under provenance.run_id (23 of 55 reports) or
                // absent entirely (10 minimal-schema reports). Accept either location.
                let run_id_present = rv.get("run_id").is_some()
                    || rv.get("provenance").and_then(|p| p.get("run_id")).is_some();
                let has_provenance = rv.get("measured_by").is_some()
                    && rv.get("measured_on").is_some()
                    && run_id_present;
                if !has_provenance {
                    let missing_fields: Vec<&str> = [
                        rv.get("measured_by").is_none().then_some("measured_by"),
                        rv.get("measured_on").is_none().then_some("measured_on"),
                        (!run_id_present).then_some("run_id (or provenance.run_id)"),
                    ]
                    .into_iter()
                    .flatten()
                    .collect();
                    diags.push(BreedDiagnostic {
                        law_id: "COG-007",
                        breed_id: bid.clone(),
                        message: format!("[COG-007] '{bid}' report lacks measured-fitness provenance — missing: {} (A10 evidence gap)", missing_fields.join(", ")),
                        severity: severity("COG-007"),
                        artifact_path: report.to_string_lossy().into(),
                    });
                }
            }
        }

        // COG-004 / COG-005: Rust fixture missing or missing expected value (A12)
        if !fix_rs.exists() {
            diags.push(BreedDiagnostic {
                law_id: "COG-004",
                breed_id: bid.clone(),
                message: format!("[COG-004] '{bid}' paper fixture missing — tests/fixtures/papers/{bid}.json required (A12 prevention)"),
                severity: severity("COG-004"),
                artifact_path: fix_rs.to_string_lossy().into(),
            });
        } else if let Ok(fjson) = std::fs::read_to_string(&fix_rs) {
            if let Ok(fv) = serde_json::from_str::<serde_json::Value>(&fjson) {
                let has_expected = fv.get("expected").is_some()
                    || fv.get("expected_value").is_some()
                    || fv.get("asserted_value").is_some()
                    || fv.get("paper_value").is_some();
                if !has_expected {
                    diags.push(BreedDiagnostic {
                        law_id: "COG-005",
                        breed_id: bid.clone(),
                        message: format!("[COG-005][A12] '{bid}' fixture cites paper but has no expected.value — citation without assertion is a fraud signal"),
                        severity: severity("COG-005"),
                        artifact_path: fix_rs.to_string_lossy().into(),
                    });
                }
            }
        }

        // COG-008: docs card missing
        if !doc.exists() {
            diags.push(BreedDiagnostic {
                law_id: "COG-008",
                breed_id: bid.clone(),
                message: format!(
                    "[COG-008] '{bid}' docs card missing — docs/breeds/{bid}.md (ceremony row 12)"
                ),
                severity: severity("COG-008"),
                artifact_path: doc.to_string_lossy().into(),
            });
        }

        // COG-009: TS fixture mirror missing
        if !fix_ts.exists() {
            diags.push(BreedDiagnostic {
                law_id: "COG-009",
                breed_id: bid.clone(),
                message: format!("[COG-009] '{bid}' TS fixture mirror missing — packages/cognition/src/__tests__/fixtures/papers/{bid}.json"),
                severity: severity("COG-009"),
                artifact_path: fix_ts.to_string_lossy().into(),
            });
        }

        // COG-010: fresh-name oracle leak into production source (A8)
        // Fresh names are ontology-derived from wasm4pm_compat::fresh_names::FRESH_NAME_PAIRS.
        if module.exists() {
            if let Ok(source) = std::fs::read_to_string(&module) {
                let fresh_for_breed: Vec<&str> = FRESH_NAME_PAIRS
                    .iter()
                    .filter(|(b, _)| *b == bid.as_str())
                    .map(|(_, n)| *n)
                    .collect();
                for name in &fresh_for_breed {
                    let name = *name;
                    // Match whole-word occurrences only (exclude test-file paths and comments that
                    // merely document the oracle contract).
                    if source.contains(name) {
                        // Exclude lines that are comments documenting the oracle (// or ///)
                        // A non-comment occurrence is a strong A8 signal.
                        let non_comment_hit = source.lines().any(|line| {
                            let trimmed = line.trim();
                            !trimmed.starts_with("//") && line.contains(name)
                        });
                        if non_comment_hit {
                            diags.push(BreedDiagnostic {
                                law_id: "COG-010",
                                breed_id: bid.clone(),
                                message: format!(
                                    "[COG-010][A8] '{bid}' production source contains oracle fresh-name '{}' — oracle injection fraud signal",
                                    name
                                ),
                                severity: severity("COG-010"),
                                artifact_path: module.to_string_lossy().into(),
                            });
                            break; // one hit per breed is enough to flag
                        }
                    }
                }
            }
        }

        // COG-011: premature PARTIAL_ALIVE (module exists but OCPN or report absent,
        //          or report exists but admitted=false)
        let has_module = module.exists();
        let has_ocpn = ocpn.exists();
        let has_report = report.exists();
        let has_fixture = fix_rs.exists();
        let report_admitted = if has_report {
            std::fs::read_to_string(&report)
                .ok()
                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
                .and_then(|v| v.get("admitted").and_then(|a| a.as_bool()))
                .unwrap_or(false)
        } else {
            false
        };
        let dod_complete = has_module && has_ocpn && has_report && report_admitted && has_fixture;
        if !dod_complete && has_module {
            let missing: Vec<&str> = [
                (!has_ocpn).then_some("OCPN"),
                (!has_report).then_some("report"),
                (has_report && !report_admitted).then_some("admitted=true in report"),
                (!has_fixture).then_some("fixture"),
            ]
            .into_iter()
            .flatten()
            .collect();
            if !missing.is_empty() {
                diags.push(BreedDiagnostic {
                    law_id: "COG-011",
                    breed_id: bid.clone(),
                    message: format!(
                        "[COG-011][A10] '{bid}' is PARTIAL_ALIVE but DoD incomplete — missing: {}",
                        missing.join(", ")
                    ),
                    severity: severity("COG-011"),
                    artifact_path: registry_path.to_string_lossy().into(),
                });
            }
        }

        // COG-012: dispatch arm presence — use already-parsed dispatch_src
        if !dispatch_src.is_empty() {
            let arm_present = dispatch_src.contains(&format!("\"{bid}\""))
                || dispatch_src.contains(&format!("BreedId::{}", snake_to_pascal(&bid)));
            if !arm_present {
                diags.push(BreedDiagnostic {
                    law_id: "COG-012",
                    breed_id: bid.clone(),
                    message: format!("[COG-012] '{bid}' has no dispatch arm in src/breeds/dispatch.rs — breed is unreachable at runtime"),
                    severity: severity("COG-012"),
                    artifact_path: dispatch_path.to_string_lossy().into(),
                });
            }
        }
    }

    diags
}

/// Convert snake_case breed id to PascalCase for BreedId enum variant matching.
fn snake_to_pascal(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut cap_next = true;
    for c in s.chars() {
        if c == '_' {
            cap_next = true;
        } else if cap_next {
            out.extend(c.to_uppercase());
            cap_next = false;
        } else {
            out.push(c);
        }
    }
    out
}

/// Summary counts — used by the release gate check.
#[derive(Debug, Default)]
pub struct AuditSummary {
    /// Number of PARTIAL_ALIVE breeds audited.
    pub total_partial_alive: usize,
    /// Total ERROR-severity diagnostics emitted.
    pub error_count: usize,
    /// Total WARNING-severity diagnostics emitted.
    pub warning_count: usize,
    /// A8 oracle-injection violations (COG-010).
    pub a8_violations: usize,
    /// A10 premature-status-flip violations (COG-011).
    pub a10_violations: usize,
    /// A12 citation-without-assertion violations (COG-005).
    pub a12_violations: usize,
}

impl AuditSummary {
    /// Aggregate diagnostic counts from a completed audit run.
    pub fn from_diagnostics(diags: &[BreedDiagnostic]) -> Self {
        let mut s = AuditSummary::default();
        for d in diags {
            match d.severity {
                DiagnosticSeverity::ERROR => s.error_count += 1,
                DiagnosticSeverity::WARNING => s.warning_count += 1,
                _ => {}
            }
            if d.law_id == "COG-010" {
                s.a8_violations += 1;
            }
            if d.law_id == "COG-011" {
                s.a10_violations += 1;
            }
            if d.law_id == "COG-005" {
                s.a12_violations += 1;
            }
        }
        s
    }

    /// Returns true iff no errors and no A8 violations — the release gate condition.
    pub fn is_release_gate_green(&self) -> bool {
        self.error_count == 0 && self.a8_violations == 0
    }
}

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

    #[test]
    fn snake_to_pascal_conversions() {
        assert_eq!(snake_to_pascal("ltl_monitor"), "LtlMonitor");
        assert_eq!(snake_to_pascal("bayesian_network"), "BayesianNetwork");
        assert_eq!(snake_to_pascal("asp"), "Asp");
        assert_eq!(snake_to_pascal("meta_reasoning"), "MetaReasoning");
    }

    #[test]
    fn audit_returns_empty_when_no_wasm4pm_root() {
        // A path with no registry.json silently returns no diagnostics.
        let diags = audit_breeds(Path::new("/tmp/nonexistent_workspace_xyz"));
        assert!(diags.is_empty());
    }

    #[test]
    fn dispatch_module_map_derives_non_conventional_locations() {
        // Simulate the dispatch.rs use list that has frame::Eliza and production_rules::Mycin.
        let src = r#"
use crate::breeds::{
    frame::Eliza, frames_inheritance::FramesInheritance,
    production_rules::Mycin,
    cbr::Cbr,
};

fn dispatch(breed: &str, input: &BreedInput) {
    match breed {
        "eliza" => run_breed(&Eliza, input),
        "mycin" => run_breed(&Mycin, input),
        "cbr"   => run_breed(&Cbr, input),
    }
}
"#;
        let map = dispatch_module_map(src);
        assert_eq!(
            map.get("eliza").map(|s| s.as_str()),
            Some("frame"),
            "eliza should derive from frame::Eliza import"
        );
        assert_eq!(
            map.get("mycin").map(|s| s.as_str()),
            Some("production_rules"),
            "mycin should derive from production_rules::Mycin import"
        );
        assert_eq!(
            map.get("cbr").map(|s| s.as_str()),
            Some("cbr"),
            "cbr conventional naming should work"
        );
    }

    #[test]
    fn audit_real_wasm4pm_workspace() {
        let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .to_path_buf();
        let wasm4pm = super::wasm4pm_root(&root);
        if !wasm4pm
            .join("crates/wasm4pm-cognition/breeds/registry.json")
            .exists()
        {
            // Not in a layout with ../wasm4pm sibling — skip.
            return;
        }
        let diags = audit_breeds(&root);
        // Print for visibility in CI; do not fail on count (this is a live audit, not a fixture).
        for d in &diags {
            eprintln!("{}", d.message);
        }
        let summary = AuditSummary::from_diagnostics(&diags);
        eprintln!(
            "Cognition audit: {} errors, {} warnings, {} A8, {} A10, {} A12",
            summary.error_count,
            summary.warning_count,
            summary.a8_violations,
            summary.a10_violations,
            summary.a12_violations
        );
        // A8 (oracle injection) is always a hard error — no registered breed may have one.
        assert_eq!(
            summary.a8_violations, 0,
            "A8 oracle injection violations detected"
        );
    }
}