eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
use std::env;
use std::fs;
use std::process;

use eggsact::mcp::registry::tools_for_profile_audience;
use eggsact::mcp::registry::{
    all_tools_vec, available_profiles, ToolCost, ToolExposure, ToolListAudience, ToolSpec,
    ToolStability,
};

const BEGIN_PROFILES: &str = "<!-- BEGIN GENERATED: profile reference -->";
const END_PROFILES: &str = "<!-- END GENERATED: profile reference -->";
const BEGIN_REGISTRY_FACTS: &str = "<!-- BEGIN GENERATED: registry facts -->";
const END_REGISTRY_FACTS: &str = "<!-- END GENERATED: registry facts -->";

const REGENERATE_COMMAND: &str = "cargo run --features dev-tools --bin generate-docs";

const CODEGG_PROFILES: &[&str] = &[
    "codegg_core_min",
    "codegg_core",
    "codegg_preflight",
    "codegg_patch",
    "codegg_config",
    "codegg_unicode_security",
    "codegg_shell",
    "codegg_repo_audit",
];

fn exposure_short(e: &ToolExposure) -> &'static str {
    match e {
        ToolExposure::Default => "default",
        ToolExposure::Contextual => "contextual",
        ToolExposure::ExpertOnly => "expert",
        ToolExposure::HarnessOnly => "harness",
        ToolExposure::Hidden => "hidden",
    }
}

fn cost_short(c: &ToolCost) -> &'static str {
    match c {
        ToolCost::Cheap => "cheap",
        ToolCost::Moderate => "mod",
        ToolCost::Heavy => "heavy",
    }
}

fn stability_short(s: &ToolStability) -> &'static str {
    match s {
        ToolStability::Stable => "stable",
        ToolStability::Deprecated => "deprecated",
        ToolStability::Experimental => "exp",
    }
}

fn generate_profile_reference() -> String {
    let profiles = available_profiles();
    let mut out = String::new();
    out.push_str(
        "| Profile | Model Tools | Harness Tools | Model Tool Names | Harness-Only Tools |\n",
    );
    out.push_str(
        "|---------|-------------|---------------|------------------|--------------------|\n",
    );

    for &profile in profiles {
        let model_tools = tools_for_profile_audience(profile, ToolListAudience::Model);
        let harness_tools = tools_for_profile_audience(profile, ToolListAudience::Harness);
        let model_count = model_tools.len();
        let harness_count = harness_tools.len();

        let mut model_names: Vec<&str> = model_tools.iter().map(|s| s.name).collect();
        model_names.sort();
        let model_names_str = model_names
            .iter()
            .map(|n| format!("`{}`", n))
            .collect::<Vec<_>>()
            .join(", ");

        let model_set: std::collections::HashSet<&str> =
            model_tools.iter().map(|s| s.name).collect();
        let mut harness_only: Vec<&str> = harness_tools
            .iter()
            .filter(|s| !model_set.contains(s.name))
            .map(|s| s.name)
            .collect();
        harness_only.sort();
        let harness_only_str = harness_only
            .iter()
            .map(|n| format!("`{}`", n))
            .collect::<Vec<_>>()
            .join(", ");

        out.push_str(&format!(
            "| `{}` | {} | {} | {} | {} |\n",
            profile, model_count, harness_count, model_names_str, harness_only_str
        ));
    }

    out
}

fn generate_registry_facts() -> String {
    let tools = all_tools_vec();
    let mut categories = std::collections::BTreeMap::<&str, usize>::new();
    for tool in tools {
        *categories.entry(tool.category).or_default() += 1;
    }

    let mut out = String::new();
    out.push_str("| Registry fact | Value |\n|---|---:|\n");
    out.push_str(&format!("| Underlying tools | {} |\n", tools.len()));
    out.push_str(&format!(
        "| Full/Model direct tools | {} |\n",
        tools_for_profile_audience("full", ToolListAudience::Model).len()
    ));
    out.push_str(&format!(
        "| Full/Model discovery tools | {} |\n",
        eggsact::mcp::discovery::discovery_legacy_definitions(
            "full",
            ToolListAudience::Model,
            None,
        )
        .len()
    ));
    out.push_str("\n| Category | Count |\n|---|---:|\n");
    for (category, count) in categories {
        out.push_str(&format!("| `{category}` | {count} |\n"));
    }
    out.push_str("\n| Profile | Model | Harness |\n|---|---:|---:|\n");
    for &profile in available_profiles() {
        let model = tools_for_profile_audience(profile, ToolListAudience::Model).len();
        let harness = tools_for_profile_audience(profile, ToolListAudience::Harness).len();
        out.push_str(&format!("| `{profile}` | {model} | {harness} |\n"));
    }
    out
}

fn required_args(schema: &serde_json::Value) -> Vec<(String, String)> {
    let mut result = Vec::new();
    let required = match schema.get("required").and_then(|r| r.as_array()) {
        Some(arr) => arr,
        None => return result,
    };
    let props = match schema.get("properties").and_then(|p| p.as_object()) {
        Some(obj) => obj,
        None => return result,
    };
    for req in required {
        if let Some(name) = req.as_str() {
            let typ = props
                .get(name)
                .and_then(|p| p.get("type"))
                .and_then(|t| t.as_str())
                .unwrap_or("any");
            result.push((name.to_string(), typ.to_string()));
        }
    }
    result
}

fn generate_tool_card(spec: &ToolSpec, profile: &str) -> String {
    let schema = (spec.input_schema)();
    let req = required_args(&schema);

    let mut card = format!("### `{}`\n\n", spec.name);
    card.push_str(&format!("{}\n\n", spec.description));
    card.push_str(&format!(
        "- **Tier**: {} | **Cost**: {} | **Stability**: {}\n",
        spec.tier,
        cost_short(&spec.cost),
        stability_short(&spec.stability)
    ));
    card.push_str(&format!(
        "- **Exposure**: {}\n",
        exposure_short(&spec.exposure)
    ));
    card.push_str(&format!("- **Profile**: `{}`\n", profile));

    if spec.composite {
        card.push_str("- **Composite**: yes\n");
    }

    if !req.is_empty() {
        card.push_str("- **Required args**:\n");
        for (name, typ) in &req {
            card.push_str(&format!("  - `{}` ({})\n", name, typ));
        }
    } else {
        card.push_str("- **Required args**: none\n");
    }

    if !spec.aliases.is_empty() {
        card.push_str(&format!(
            "- **Aliases**: {}\n",
            spec.aliases
                .iter()
                .map(|a| format!("`{}`", a))
                .collect::<Vec<_>>()
                .join(", ")
        ));
    }

    card.push('\n');
    card
}

fn generate_tool_cards() -> String {
    let mut out = String::new();
    out.push_str("# Tool Cards\n\n");
    out.push_str(
        "Generated from the ToolSpec registry. Each section corresponds to a codegg profile.\n\n",
    );

    for &profile in CODEGG_PROFILES {
        let tools: Vec<&ToolSpec> = tools_for_profile_audience(profile, ToolListAudience::Model)
            .into_iter()
            .filter(|spec| spec.exposure != ToolExposure::Hidden)
            .collect();
        if tools.is_empty() {
            continue;
        }
        out.push_str(&format!("## `{}`\n\n", profile));

        let mut sorted = tools;
        sorted.sort_by_key(|s| s.name);

        for spec in sorted {
            out.push_str(&generate_tool_card(spec, profile));
        }
    }

    out
}

fn extract_between<'a>(content: &'a str, begin: &str, end: &str) -> Option<&'a str> {
    let start = content.find(begin)? + begin.len();
    let rest = &content[start..];
    let end_pos = rest.find(end)?;
    Some(&rest[..end_pos])
}

/// Find the byte range of all generated blocks for a (begin, end) marker pair.
/// Returns `(start, end_exclusive, is_well_formed)` triples.
///
/// A block is well-formed when an end marker follows its begin marker
/// (consumed in begin-order, end-order pairing). Otherwise it is an
/// orphan whose stop point is the next `begin` marker or the next markdown
/// heading (whichever comes first), so we never swallow unrelated content
/// like adjacent sections.
fn find_all_generated_spans(content: &str, begin: &str, end: &str) -> Vec<(usize, usize, bool)> {
    let mut begins = Vec::new();
    let mut ends = Vec::new();
    let mut search = 0;
    while let Some(pos) = content[search..].find(begin) {
        begins.push(search + pos);
        search = search + pos + begin.len();
    }
    search = 0;
    while let Some(pos) = content[search..].find(end) {
        ends.push(search + pos);
        search = search + pos + end.len();
    }

    let mut spans = Vec::new();
    if begins.is_empty() {
        return spans;
    }

    let mut end_iter = ends.iter().peekable();
    for (idx, &b) in begins.iter().enumerate() {
        // Skip ends that precede this begin
        while let Some(&&e) = end_iter.peek() {
            if e < b {
                end_iter.next();
            } else {
                break;
            }
        }
        match end_iter.peek() {
            Some(&&e) => {
                // Well-formed block: begin..end (exclusive of end marker)
                spans.push((b, e + end.len(), true));
                end_iter.next();
            }
            None => {
                // Orphaned begin — stop at next begin or next heading
                // (whichever comes first), so we don't swallow adjacent
                // sections that happen to follow an orphan block.
                let after = b + begin.len();
                let next_begin = begins.get(idx + 1).copied().unwrap_or(usize::MAX);
                let mut stop = next_begin;
                // Walk forward looking for a heading line
                let mut cursor = after;
                while cursor < content.len() && cursor < stop {
                    let line_end = content[cursor..]
                        .find('\n')
                        .map(|p| cursor + p)
                        .unwrap_or(content.len());
                    let line = &content[cursor..line_end];
                    if line.starts_with("# ") || line.starts_with("## ") || line.starts_with("### ")
                    {
                        stop = cursor;
                        break;
                    }
                    cursor = line_end + 1;
                }
                // If no begin/heading limit applies, default to end of file.
                if stop == usize::MAX {
                    stop = content.len();
                }
                spans.push((b, stop, false));
            }
        }
    }

    spans
}

/// Strip all generated blocks (including orphans) and return cleaned content.
fn strip_all_generated_blocks(content: &str, begin: &str, end: &str) -> String {
    let spans = find_all_generated_spans(content, begin, end);
    if spans.is_empty() {
        return content.to_string();
    }
    let mut result = String::with_capacity(content.len());
    let mut last_end = 0;
    for (start, stop, _) in &spans {
        result.push_str(&content[last_end..*start]);
        last_end = *stop;
    }
    result.push_str(&content[last_end..]);
    // Collapse runs of > 2 blank lines introduced by removal
    while result.contains("\n\n\n\n") {
        result = result.replace("\n\n\n\n", "\n\n\n");
    }
    result
}

/// Count orphan BEGIN markers (BEGIN without a matching END after it).
fn count_orphan_begins(content: &str, begin: &str, end: &str) -> usize {
    let spans = find_all_generated_spans(content, begin, end);
    spans
        .iter()
        .filter(|(_, _, well_formed)| !well_formed)
        .count()
}

fn read_file(path: &str) -> String {
    fs::read_to_string(path).unwrap_or_else(|e| {
        eprintln!("error: cannot read {}: {}", path, e);
        process::exit(1);
    })
}

fn write_file(path: &str, content: &str) {
    fs::write(path, content).unwrap_or_else(|e| {
        eprintln!("error: cannot write {}: {}", path, e);
        process::exit(1);
    });
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let check_mode = args.iter().any(|a| a == "--check");
    let output_dir = args
        .windows(2)
        .find(|w| w[0] == "--output-dir")
        .map(|w| w[1].clone())
        .unwrap_or_else(|| ".".to_string());

    let profile_content = generate_profile_reference();
    let registry_content = generate_registry_facts();
    let cards_content = generate_tool_cards();

    let mut stale_files = Vec::new();

    // Check/update architecture/mcp-server.md
    let arch_path = format!("{}/architecture/mcp-server.md", output_dir);
    let arch_file = read_file(&arch_path);
    let existing = extract_between(&arch_file, BEGIN_PROFILES, END_PROFILES).unwrap_or("");
    let arch_orphans = count_orphan_begins(&arch_file, BEGIN_PROFILES, END_PROFILES);
    let needs_update = !arch_file.contains(BEGIN_PROFILES)
        || arch_orphans > 0
        || existing.trim() != profile_content.trim();
    if needs_update {
        stale_files.push(arch_path.clone());
        if !check_mode {
            // Always strip ALL generated blocks (including orphans) first.
            let cleaned = strip_all_generated_blocks(&arch_file, BEGIN_PROFILES, END_PROFILES);
            let marker_section = format!(
                "\n{}\n{}\n{}\n",
                BEGIN_PROFILES, profile_content, END_PROFILES
            );
            let updated = if let Some(pos) = cleaned.find("### Profile Reference") {
                let heading_end = pos + "### Profile Reference".len();
                let rest = &cleaned[heading_end..];
                let insert_at = heading_end + rest.find('\n').unwrap_or(0) + 1;
                let mut out = String::with_capacity(cleaned.len() + marker_section.len());
                out.push_str(&cleaned[..insert_at]);
                out.push_str(&marker_section);
                out.push_str(&cleaned[insert_at..]);
                out
            } else {
                let mut out = String::with_capacity(arch_file.len() + marker_section.len() + 1);
                out.push_str(&cleaned);
                out.push('\n');
                out.push_str(&marker_section);
                out
            };
            write_file(&arch_path, &updated);
        }
    }

    // Check/update generated registry facts in architecture/overview.md.
    let overview_path = format!("{}/architecture/overview.md", output_dir);
    let overview_file = read_file(&overview_path);
    let existing =
        extract_between(&overview_file, BEGIN_REGISTRY_FACTS, END_REGISTRY_FACTS).unwrap_or("");
    let overview_orphans =
        count_orphan_begins(&overview_file, BEGIN_REGISTRY_FACTS, END_REGISTRY_FACTS);
    let overview_needs_update = !overview_file.contains(BEGIN_REGISTRY_FACTS)
        || overview_orphans > 0
        || existing.trim() != registry_content.trim();
    if overview_needs_update {
        stale_files.push(overview_path.clone());
        if !check_mode {
            let cleaned = strip_all_generated_blocks(
                &overview_file,
                BEGIN_REGISTRY_FACTS,
                END_REGISTRY_FACTS,
            );
            let marker_section = format!(
                "\n{}\n{}\n{}\n",
                BEGIN_REGISTRY_FACTS, registry_content, END_REGISTRY_FACTS
            );
            let updated = if let Some(pos) = cleaned.find("## Generated Registry Facts") {
                let heading_end = pos + "## Generated Registry Facts".len();
                let rest = &cleaned[heading_end..];
                let insert_at = heading_end + rest.find('\n').unwrap_or(0) + 1;
                let mut out = String::with_capacity(cleaned.len() + marker_section.len());
                out.push_str(&cleaned[..insert_at]);
                out.push_str(&marker_section);
                out.push_str(&cleaned[insert_at..]);
                out
            } else {
                let mut out = String::with_capacity(cleaned.len() + marker_section.len() + 1);
                out.push_str(&cleaned);
                out.push('\n');
                out.push_str("## Generated Registry Facts\n");
                out.push_str(&marker_section);
                out
            };
            write_file(&overview_path, &updated);
        }
    }

    // Check/update generated/tool-cards.md
    let cards_path = format!("{}/generated/tool-cards.md", output_dir);
    let cards_file = fs::read_to_string(&cards_path).unwrap_or_default();
    if cards_file.trim() != cards_content.trim() {
        stale_files.push(cards_path.clone());
        if !check_mode {
            let dir = format!("{}/generated", output_dir);
            fs::create_dir_all(&dir).unwrap_or_else(|e| {
                eprintln!("error: cannot create {}: {}", dir, e);
                process::exit(1);
            });
            write_file(&cards_path, &cards_content);
        }
    }

    if check_mode && !stale_files.is_empty() {
        eprintln!("Stale generated docs:");
        for f in &stale_files {
            eprintln!("  {}", f);
        }
        eprintln!("Run `{REGENERATE_COMMAND}` to regenerate.");
        process::exit(1);
    }

    if !check_mode {
        if stale_files.is_empty() {
            eprintln!("All generated docs are up to date.");
        } else {
            eprintln!("Generated docs updated:");
            for f in &stale_files {
                eprintln!("  {}", f);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use eggsact::mcp::registry::{
        all_tools_vec, tools_for_profile_audience, ToolExposure, ToolListAudience,
    };

    #[test]
    fn generated_tool_cards_exclude_hidden_tools() {
        let cards = generate_tool_cards();
        let all = all_tools_vec();
        let hidden: Vec<&str> = all
            .iter()
            .filter(|s| s.exposure == ToolExposure::Hidden)
            .map(|s| s.name)
            .collect();
        for name in &hidden {
            let header = format!("### `{}`", name);
            assert!(
                !cards.contains(&header),
                "tool cards should not include hidden tool {}",
                name
            );
        }
        // If no hidden tools exist, the test still passes — it guards future additions.
    }

    #[test]
    fn profile_counts_match_registry() {
        let profile_ref = generate_profile_reference();
        for &profile in available_profiles() {
            let model_tools = tools_for_profile_audience(profile, ToolListAudience::Model);
            let harness_tools = tools_for_profile_audience(profile, ToolListAudience::Harness);
            let model_count = model_tools.len();
            let harness_count = harness_tools.len();
            let profile_line = format!("| `{}` | {} | {} |", profile, model_count, harness_count);
            assert!(
                profile_ref.contains(&profile_line),
                "profile reference missing or wrong count for {}: expected model={} harness={}",
                profile,
                model_count,
                harness_count
            );
        }
    }

    #[test]
    fn profile_reference_includes_harness_only_tools() {
        let profile_ref = generate_profile_reference();
        // For profiles that have harness-only tools, verify at least one appears in the output
        for &profile in available_profiles() {
            let model_tools = tools_for_profile_audience(profile, ToolListAudience::Model);
            let harness_tools = tools_for_profile_audience(profile, ToolListAudience::Harness);
            let model_set: std::collections::HashSet<&str> =
                model_tools.iter().map(|s| s.name).collect();
            let harness_only: Vec<&str> = harness_tools
                .iter()
                .filter(|s| !model_set.contains(s.name))
                .map(|s| s.name)
                .collect();
            if !harness_only.is_empty() {
                // At least one harness-only tool should appear in the profile reference line
                let profile_line = profile_ref
                    .lines()
                    .find(|l| l.contains(&format!("`{}`", profile)))
                    .unwrap_or("");
                for name in &harness_only {
                    assert!(
                        profile_line.contains(&format!("`{}`", name)),
                        "profile '{}' has harness-only tool '{}' but it's not in the profile reference",
                        profile,
                        name
                    );
                }
            }
        }
    }

    #[test]
    fn tool_cards_reference_only_known_tools() {
        let cards = generate_tool_cards();
        let all = all_tools_vec();
        let known: std::collections::HashSet<&str> = all.iter().map(|s| s.name).collect();
        for line in cards.lines() {
            if let Some(name) = line.strip_prefix("### `").and_then(|s| s.strip_suffix('`')) {
                assert!(
                    known.contains(name),
                    "tool card references unknown tool: {}",
                    name
                );
            }
        }
    }

    #[test]
    fn tool_card_required_args_match_schema() {
        let all = all_tools_vec();
        for spec in all {
            let schema = (spec.input_schema)();
            let req = required_args(&schema);
            let card = generate_tool_card(spec, "test_profile");
            if req.is_empty() {
                assert!(
                    card.contains("**Required args**: none"),
                    "{}: expected 'none' in card",
                    spec.name
                );
            } else {
                for (arg_name, _) in &req {
                    assert!(
                        card.contains(&format!("`{}`", arg_name)),
                        "{}: card missing required arg `{}`",
                        spec.name,
                        arg_name
                    );
                }
            }
        }
    }

    /// The stale-doc message must reference the actual cargo binary name
    /// (`generate-docs`, with a dash) so users can copy/paste it without
    /// hitting "no such binary". Cargo uses the `name` field from
    /// `[[bin]]` in `Cargo.toml`, not the source filename.
    #[test]
    fn stale_docs_message_uses_cargo_bin_name() {
        assert_eq!(
            REGENERATE_COMMAND,
            "cargo run --features dev-tools --bin generate-docs"
        );
        assert!(
            REGENERATE_COMMAND.contains("generate-docs"),
            "REGENERATE_COMMAND must use the dash form, got: {REGENERATE_COMMAND}"
        );
        assert!(
            !REGENERATE_COMMAND.contains("generate_docs"),
            "REGENERATE_COMMAND must not use the underscore form, got: {REGENERATE_COMMAND}"
        );
    }

    // -- Orphan / triplication handling tests -----------------------------

    const TB: &str = "<!-- BEGIN GENERATED: test -->";
    const TE: &str = "<!-- END GENERATED: test -->";

    #[test]
    fn find_all_spans_handles_well_formed_block() {
        let content = format!("pre\n{}hello\n{}post\n", TB, TE);
        let spans = find_all_generated_spans(&content, TB, TE);
        assert_eq!(spans.len(), 1);
        let (s, e, well) = spans[0];
        assert!(well, "well-formed block should be flagged well-formed");
        assert_eq!(&content[s..e], format!("{}hello\n{}", TB, TE));
    }

    #[test]
    fn find_all_spans_detects_orphans() {
        // Two BEGIN, one END in middle — first well-formed, second orphan.
        let content = format!("{}first\n{}\n{}second still going\n", TB, TE, TB);
        let spans = find_all_generated_spans(&content, TB, TE);
        assert_eq!(spans.len(), 2);
        assert!(spans[0].2, "first should be well-formed");
        assert!(!spans[1].2, "second should be orphan");
    }

    #[test]
    fn find_all_spans_handles_triplication() {
        // Three BEGINs, one END at first block — 1 well-formed, 2 orphans.
        let content = format!("{}one\n{}\n{}two\n{}three\n", TB, TE, TB, TB);
        let spans = find_all_generated_spans(&content, TB, TE);
        assert_eq!(spans.len(), 3);
        let well_count = spans.iter().filter(|(_, _, w)| *w).count();
        let orphan_count = spans.iter().filter(|(_, _, w)| !*w).count();
        assert_eq!(well_count, 1);
        assert_eq!(orphan_count, 2);
    }

    #[test]
    fn count_orphan_begins_zero_when_all_paired() {
        // First pair is orphan, second pair is well-formed — still 1 orphan.
        let content = format!("{}a\n{}b\n{}c\n{}", TB, "mid", TB, TE);
        assert_eq!(count_orphan_begins(&content, TB, TE), 1);
    }

    #[test]
    fn count_orphan_begins_zero_when_no_orphans() {
        // One BEGIN, one END — no orphans.
        let content = format!("{}a\n{}", TB, TE);
        assert_eq!(count_orphan_begins(&content, TB, TE), 0);
    }

    #[test]
    fn count_orphan_begins_correct_for_triplicated() {
        let content = format!("{}one\n{}\n{}two\n{}three\n", TB, TE, TB, TB);
        assert_eq!(count_orphan_begins(&content, TB, TE), 2);
    }

    #[test]
    fn strip_all_removes_triplicated_blocks() {
        // 1 well-formed (with `one`) and 2 orphans (`two` and `three`)
        // followed by a clean section header `## Next` (which must survive).
        let content = format!(
            "## Heading\n\n{}one\n{}\n{}two\n{}three\n\n## Next\n",
            TB, TE, TB, TB
        );
        let cleaned = strip_all_generated_blocks(&content, TB, TE);
        assert!(!cleaned.contains(TB), "no BEGIN markers should remain");
        assert!(!cleaned.contains(TE), "no END markers should remain");
        assert!(
            cleaned.contains("## Heading"),
            "preceding heading preserved"
        );
        assert!(cleaned.contains("## Next"), "following heading preserved");
        // Orphan blocks are intentionally discarded — the content under an
        // orphan marker is from a prior failed run and is unsafe to keep.
        assert!(!cleaned.contains("two"));
        assert!(!cleaned.contains("three"));
        assert!(!cleaned.contains("one"));
    }
}

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

    fn assert_marker_pair(content: &str, begin: &str, end: &str, file: &str) {
        let begin_count = content.matches(begin).count();
        let end_count = content.matches(end).count();
        assert_eq!(
            begin_count, 1,
            "{file}: expected exactly one `{begin}`, found {begin_count}"
        );
        assert_eq!(
            end_count, 1,
            "{file}: expected exactly one `{end}`, found {end_count}"
        );
        let begin_pos = content.find(begin).unwrap();
        let end_pos = content.find(end).unwrap();
        assert!(
            begin_pos < end_pos,
            "{file}: `{begin}` must appear before `{end}`"
        );
    }

    #[test]
    fn mcp_server_doc_markers_are_well_formed() {
        let content = std::fs::read_to_string("architecture/mcp-server.md")
            .expect("failed to read architecture/mcp-server.md from repo root");
        assert_marker_pair(
            &content,
            BEGIN_PROFILES,
            END_PROFILES,
            "architecture/mcp-server.md",
        );
    }
}