alix 0.5.0

A learning tool built for understanding, not just remembering.
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
//! Goal-driven exploration of a source (`alix generate <dir>`'s planning half).
//!
//! Where [`crate::trace_ai::suggest`] is the flat recon menu of candidate *traces*,
//! `explore` is goal-driven exploration: given a source and a learning **goal**,
//! it manufactures the ordered set of **means** — fact *decks* and *traces* —
//! that, worked through, would reach the goal. The means are chosen by the shape
//! of the knowledge (edges → traces, nodes → decks), sized to the goal by
//! saturation, and ordered by prerequisite. It **prints the plan**, and with
//! `--into <dir>` also **materializes** it into a workspace folder — a
//! `alix.toml` plus one stub deck/trace file per item, wired by `% requires:`.

use std::{
    collections::HashMap,
    fs,
    path::{Path, PathBuf},
};

use anyhow::{Context, Result, bail};

use crate::{
    ask,
    backend::ensure_source_reachable,
    config::{AskConfig, TraceConfig},
    deck::{Deck, is_url},
    share, title,
    trace::{self, resolve_source},
    trace_ai::{self, build_run_config, clean_to_cards},
    workspace,
};

/// Explore a source toward `goal` and return an ordered learning plan — the
/// decks and traces worth authoring, each tagged and dependency-ordered. One
/// read-only exploration pass (the same tools and cwd as [`crate::trace_ai::build`]);
/// discovers nothing in depth and writes nothing. `source` is a scope directly
/// (a repo `.`, a directory, a file, or a URL), not a deck.
pub fn explore(source: &str, goal: &str, cfg: &TraceConfig, ask_cfg: &AskConfig) -> Result<String> {
    let url = is_url(source);
    // Gate on backend capability before resolving or exploring the source.
    ensure_source_reachable(ask_cfg, url)?;
    let cwd = if url {
        None
    } else {
        let (base_dir, _) = resolve_source(None, Some(source));
        Some(base_dir)
    };
    let prompt = explore_prompt(source, goal, url, cfg);
    let run_cfg = build_run_config(cfg, ask_cfg, cwd, url);
    let raw = ask::run(&run_cfg, &prompt, &[])?;
    let plan = raw.trim().to_string();
    if plan.is_empty() {
        bail!("the exploration produced no plan");
    }
    Ok(plan)
}

/// Builds the exploration prompt: explore the source and emit an ordered,
/// prerequisite-sorted plan of means (decks + traces) sized to the goal by
/// saturation. The counterpart to [`crate::trace_ai::suggest`]'s recon prompt, one
/// tier up.
fn explore_prompt(source: &str, goal: &str, url: bool, cfg: &TraceConfig) -> String {
    let explore = if url {
        format!("Read the source page at {source} with the WebFetch tool (fetch it once).")
    } else {
        "Your working directory is the source root. Explore it with the Read, Glob \
         and Grep tools — read it the way you would cold: the manifest (what kind of \
         thing + its stack), then the module names (the domain nouns), then the \
         entry point, then the main path. You can read any file under the source; \
         you have no write or shell access."
            .to_string()
    };
    let scope = if url {
        "the same URL"
    } else {
        "the whole source `.`, or a narrower path (a subdirectory or single file) \
         for a tightly-scoped item"
    };
    let mut p = format!(
        "You are EXPLORING a source for a learner whose GOAL is:\n\n    {goal}\n\n\
         Produce the ordered SET OF MEANS — facts decks and traces — that, \
         worked through in order, would achieve that goal. Do NOT build any deck or \
         trace in depth (no cards, no checkpoints) — that is a separate step the \
         learner runs later on each item. Output a PLAN: an ordered list of \
         items.\n\n\
         Source (the scope): {source}\n{explore}\n\n\
         TWO KINDS OF MEANS, chosen by the SHAPE of the knowledge:\n\
         - a TRACE drills an EDGE — a path predicted hop by hop, \"how X becomes \
         Y\": a data flow, a control flow, or a derivation, a real sequence with two \
         ends. Use a trace where the knowledge IS a path.\n\
         - a DECK drills NODES — a table of related facts with no path to predict (a \
         config's knobs, an on-disk format, a glossary of terms). Use a deck where \
         the knowledge is a SET OF FACTS.\n\
         Pick the form that fits each part; never force facts into a fake path, nor \
         a real path into loose facts.\n\n\
         COVERAGE is sized to the GOAL, not to a number. Identify the parts of the \
         source the goal requires understanding, and emit one item per part, \
         choosing trace or deck by shape. STOP at SATURATION: when one more item \
         would teach no new mechanism or concept the learner has not already met. A \
         broad goal (\"understand the whole X\") covers every major subsystem; a \
         narrow goal (\"how Y works\") covers only Y's parts. Do not pad to look \
         thorough; do not drop a part the goal needs.\n\n\
         ORDER by PREREQUISITE. Sort so that whatever must be understood FIRST comes \
         first — the foundational data model and parsing before the flow that uses \
         them before the outer surfaces. Give each item a `requires:` list naming \
         the earlier item numbers it builds on (none for the foundations). The order \
         must be a valid TOPOLOGICAL order: every item's requirements appear above \
         it.\n\n\
         FORMAT — output ONLY the plan, no preamble, no code fences. Start with \
         three heading lines, then the numbered items:\n\n\
         Goal    {goal}\n\
         Source  <one line: what this source is>\n\
         Spine   <the single most central path, arrow-joined nouns>\n\n\
         1. [deck]  <a short topic noun phrase, e.g. the deck format>\n   \
         requires: none\n   % source: <{scope}>\n\
         2. [trace] <the path-question, e.g. how deck text becomes a list of Cards>\n   \
         requires: 1\n   % source: <{scope}>\n\
         3. …\n\n\
         Tag EVERY item [trace] or [deck]. Keep each title SHORT — one line, a \
         handful of words: a [deck] is a noun phrase naming the topic (`the crate \
         surface`, `error taxonomy`), a [trace] is a terse path-question (`how a \
         request becomes a profile`). Do NOT pack the contents into the title — no \
         `X: a, b, and c` enumerations, no parenthetical lists; the cards hold the \
         detail. Do not resolve line numbers or write cards/checkpoints — later \
         steps do that. Use as many items as the goal needs (stop at saturation), \
         ordered by prerequisite.\n\n\
         `% source:` FORMAT: a [deck]'s source must be actual FILE path(s) (the \
         exam reads them) — never a bare directory. For several files, join them \
         with ` + `, writing the FIRST as a full path and the rest RELATIVE to its \
         directory, e.g. `% source: <root>/README.md + src/lib.rs`. A [trace] may \
         use a single directory or file as its locator base."
    );
    if let Some(extra) = cfg
        .extra
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
    {
        p.push_str("\n\nAdditional instructions:\n");
        p.push_str(extra);
    }
    p
}

/// Generate the explore walk: a short predict-verify trace over a source's
/// SHAPE — what it is → its parts → its entry → its spine → what to trace first —
/// each hop citing real structural evidence. Returns the checkpoint cards
/// (header-less); the caller wraps them in a `% trace:`/`% source:` deck. Reuses
/// the same read-only exploration as [`explore`].
pub fn walk(source: &str, goal: &str, cfg: &TraceConfig, ask_cfg: &AskConfig) -> Result<String> {
    let url = is_url(source);
    // Gate on backend capability before resolving or exploring the source.
    ensure_source_reachable(ask_cfg, url)?;
    let cwd = if url {
        None
    } else {
        let (base_dir, _) = resolve_source(None, Some(source));
        Some(base_dir)
    };
    let prompt = walk_prompt(source, goal, url, cfg);
    let run_cfg = build_run_config(cfg, ask_cfg, cwd, url);
    let raw = ask::run(&run_cfg, &prompt, &[])?;
    let cards = clean_to_cards(&raw);
    if cards.trim().is_empty() {
        bail!("the explore walk produced no checkpoints");
    }
    Ok(cards)
}

/// Builds the explore-walk prompt: produce trace checkpoints about the
/// source's *shape* (manifest → nouns → entry → spine → what to trace), each
/// citing real structural evidence, in the standard trace checkpoint format.
fn walk_prompt(source: &str, goal: &str, url: bool, cfg: &TraceConfig) -> String {
    let explore = if url {
        format!("Read the source page at {source} with the WebFetch tool (fetch it once).")
    } else {
        "Your working directory is the source root. Explore it with the Read, Glob \
         and Grep tools — read the manifest, the module/file names, the entry point, \
         and the most central file. You have no write or shell access."
            .to_string()
    };
    let locator = if url {
        "a short quoted span from the page — the exact words the answer rests on"
    } else {
        "ONE contiguous range, `file:start-end`, relative to the source root (e.g. \
         `Cargo.toml:8-20` or `src/lib.rs:12-33`) — never comma-separated"
    };
    let mut p = format!(
        "You are building an EXPLORE walk: a short predict-and-verify trace that \
         teaches a newcomer the SHAPE of a source by making them PREDICT its \
         structure before each reveal. The learner's aim:\n\n    {goal}\n\n{explore}\n\n\
         Walk it the way you read a codebase cold, one hop per step:\n\
         1. from the manifest / dependencies — what KIND of thing is this?\n\
         2. from the module / file names — what are its core DOMAIN NOUNS (the model)?\n\
         3. from the entry point — how is it DRIVEN (its commands / surfaces)?\n\
         4. from the most central file — what is the SPINE (the main path data takes)?\n\
         5. (last) given that shape, what are the first PATHS worth tracing next? — \
         name 2-4 concrete candidate traces (the menu).\n\n\
         Each hop must CITE REAL STRUCTURAL EVIDENCE as its `% at:` locator — the \
         actual lines the answer rests on (the manifest's dependency list, the \
         module-declaration lines, the entry enum, the central function's signature). \
         The reveal is the real text; the source is the oracle, never invented. Every \
         hop has a locator.\n\n\
         FORMAT — output ONLY the checkpoint cards: no header, no `% trace:`/`% source:`, \
         no preamble, no code fences. Each checkpoint (key-point and directive lines \
         are indented with a TAB):\n\n    \
         # <the shape question, asked plainly>\n\t<a key point a correct answer \
         hits>\n\t<another key point>\n\t% at: <locator>\n\t! <one connecting \
         insight>\n\n\
         where `% at:` is {locator}.\n\n\
         RULES: each question reasons FORWARD from the previous reveal (hop 1 has no \
         prior); carry the STATE, never \"checkpoint N\"; ask plainly — do NOT prefix \
         with \"Predict\"; keep the answer out of the question; every key point must be \
         GROUNDED in the cited lines; the LAST hop lands on the candidate traces (the \
         menu of what to trace first). Keep each question one or two sentences and each \
         key point one line. Use 4-6 checkpoints."
    );
    if let Some(extra) = cfg
        .extra
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
    {
        p.push_str("\n\nAdditional instructions:\n");
        p.push_str(extra);
    }
    p
}

/// Explore a source once (one CLI session), then RESUME that session to fill
/// every plan item with real content using the understanding just built —
/// checkpoints for each `[trace]`, fact cards for each `[deck]`. Returns the plan
/// and a map from item number to its filled body. Exploring once (not per item)
/// keeps the items coherent (each aware of the others) and amortizes the read.
// Only reachable via `generate`'s multi-item workspace build (`build_workspace`),
// itself untested (two real AI calls + materialize/merge, judged too
// contorted for the deterministic fake-backend approach); its `url == true`
// branch is additionally dead — the one CLI call site is dir-gated.
#[cfg_attr(coverage_nightly, coverage(off))]
pub fn explore_and_fill(
    source: &str,
    goal: &str,
    cfg: &TraceConfig,
    ask_cfg: &AskConfig,
) -> Result<(String, HashMap<usize, String>)> {
    let url = is_url(source);
    // Gate on backend capability before resolving or exploring the source.
    ensure_source_reachable(ask_cfg, url)?;
    let cwd = if url {
        None
    } else {
        let (base_dir, _) = resolve_source(None, Some(source));
        Some(base_dir)
    };
    let run_cfg = build_run_config(cfg, ask_cfg, cwd, url);
    let mut session = ask::CliSession::new();

    // 1. Explore — establishes the session and its understanding of the source.
    let plan = ask::run(
        &run_cfg,
        &explore_prompt(source, goal, url, cfg),
        &session.args(),
    )?
    .trim()
    .to_string();
    session.started = true;
    if plan.is_empty() {
        bail!("the exploration produced no plan");
    }
    let items = parse_plan(&plan);
    if items.is_empty() {
        bail!("the plan has no items to fill");
    }

    // 2. Resume — fill every item from what the session already learned.
    let filled = ask::run(&run_cfg, &fill_prompt(&items), &session.args())?;
    Ok((plan, parse_filled(&filled)))
}

/// Builds the fill prompt for the RESUMED explore session: write the full content
/// for every plan item, keyed by `=== item N ===` delimiters, reusing the
/// understanding from the exploration just done.
fn fill_prompt(items: &[Item]) -> String {
    let mut list = String::new();
    for item in items {
        let kind = match item.kind {
            Kind::Trace => "trace",
            Kind::Deck => "deck",
        };
        list.push_str(&format!("{}. [{}] {}\n", item.num, kind, item.title));
    }
    format!(
        "You just explored this source and produced the plan below. Now WRITE THE \
         FULL CONTENT for EVERY item, reusing what you already learned — only read \
         more if you must verify a line number. Because you are writing the whole \
         set at once, make it COHERENT: an item must NOT re-teach what an earlier \
         item (its prerequisite) covers — build on it, keep terminology consistent, \
         and don't overlap.\n\n\
         For each item, emit a delimiter line exactly `=== item <N> ===` (its plan \
         number) followed by its content:\n\
         - a [trace] → the predict-verify CHECKPOINT cards: each is a `# ` question \
         at column 0, then TAB-indented key points, a `% at: file:start-end` locator \
         citing the REAL lines, and an optional `! ` note. Each hop opens on the \
         previous reveal, predicts forward, and its key points are grounded in the \
         cited lines.\n\
         - a [deck] → FACT cards: each is a `# ` front at column 0, then TAB-indented \
         back line(s), plus a `% at: file:start-end` locator citing the REAL lines \
         whenever the fact maps to a specific range (so the card can show its source \
         on reveal; omit it when the fact synthesizes across several places). One \
         fact per card, concise and recall-oriented. Do NOT cram an enumeration \
         into one prose answer: if the answer is a list of several items, split it \
         into several one-idea cards (one card per item or group), or give it clean \
         structure with one point per indented line (no bullet or dash prefix — \
         bullets come later from the format augment); keep an atomic answer atomic.\n\n\
         Every `% at:` `file` part MUST be written relative to the SAME root — the \
         source root you explored (your working directory) — as ONE consistent path \
         per file across ALL items; never drop or add a leading directory (always \
         `src/foo.rs`, never sometimes `foo.rs`), so the frozen citations all resolve.\n\n\
         Do NOT repeat any header directive (`% trace:`, `% title:`, `% source:`, \
         `% requires:`) — those are already written; output only the `# ` cards. \
         Output ONLY the delimited item bodies: no preamble, no code fences, nothing \
         between the last card of one item and the next `=== item ===` line.\n\n\
         The plan:\n{list}"
    )
}

/// Splits the fill response into per-item bodies on `=== item N ===` delimiters.
/// Lenient: text before the first delimiter is dropped; an item with no body is
/// omitted (so it stays a stub).
pub(crate) fn parse_filled(raw: &str) -> HashMap<usize, String> {
    let mut out: HashMap<usize, String> = HashMap::new();
    let mut current: Option<usize> = None;
    let mut buf = String::new();
    for line in raw.lines() {
        if let Some(num) = parse_item_delimiter(line) {
            if let Some(n) = current.take() {
                let body = buf.trim();
                if !body.is_empty() {
                    out.insert(n, body.to_string());
                }
            }
            buf.clear();
            current = Some(num);
        } else if current.is_some() {
            buf.push_str(line);
            buf.push('\n');
        }
    }
    if let Some(n) = current {
        let body = buf.trim();
        if !body.is_empty() {
            out.insert(n, body.to_string());
        }
    }
    out
}

/// Matches a `=== item N ===` delimiter line, returning N.
fn parse_item_delimiter(line: &str) -> Option<usize> {
    line.trim()
        .strip_prefix("=== item")?
        .trim()
        .strip_suffix("===")?
        .trim()
        .parse()
        .ok()
}

/// One item of an exploration plan: a facts deck or a trace to author.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Item {
    pub num: usize,
    pub kind: Kind,
    pub title: String,
    pub requires: Vec<usize>,
    pub source: String,
}

/// Whether a plan item is a trace (an edge) or a facts deck (nodes).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
    Trace,
    Deck,
}

/// What [`materialize`] wrote.
pub struct Materialized {
    pub dir: PathBuf,
    pub traces: usize,
    pub decks: usize,
    /// How many items were written with real content (vs. left as stubs).
    pub filled: usize,
}

/// Parses the printed plan back into items (lenient — unrecognized lines, the
/// header, and prose are skipped). An item starts at a `N. [trace|deck] <title>`
/// line and absorbs the following `requires:` and `% source:` lines. Public so
/// the CLI can count a plan's items before deciding deck-vs-workspace.
pub fn parse_plan(plan: &str) -> Vec<Item> {
    let mut items: Vec<Item> = Vec::new();
    for line in plan.lines() {
        let t = line.trim();
        if let Some((num, kind, title)) = parse_item_header(t) {
            items.push(Item {
                num,
                kind,
                title,
                requires: Vec::new(),
                source: String::new(),
            });
        } else if let Some(item) = items.last_mut() {
            if let Some(rest) = t.strip_prefix("requires:") {
                item.requires = rest
                    .split(|c: char| !c.is_ascii_digit())
                    .filter_map(|p| p.parse().ok())
                    .collect();
            } else if let Some(rest) = t.strip_prefix("% source:") {
                item.source = rest.trim().to_string();
            }
        }
    }
    items
}

/// Matches a `N. [trace|deck] <title>` header line, returning its number, kind
/// and title; `None` for any other line.
fn parse_item_header(t: &str) -> Option<(usize, Kind, String)> {
    let dot = t.find('.')?;
    let num: usize = t[..dot].trim().parse().ok()?;
    let rest = t[dot + 1..].trim_start().strip_prefix('[')?;
    let close = rest.find(']')?;
    let kind = match rest[..close].trim() {
        "trace" => Kind::Trace,
        "deck" => Kind::Deck,
        _ => return None,
    };
    let title = rest[close + 1..].trim().to_string();
    (!title.is_empty()).then_some((num, kind, title))
}

/// Scaffolds the plan into a workspace folder `dir`: an `alix.toml` (the goal +
/// an empty `[defaults]`) and one stub file per item — a `% trace:` deck for a
/// trace, a `% title:` facts deck for a deck — wired by `% requires:` (item
/// numbers mapped to the member file names), with each `% source:` rewritten
/// absolute against the source root. `dir` is created if missing; materialize
/// itself takes no view on a populated `dir` — callers that write straight
/// into a real destination decide that (`alix generate` instead stages into a
/// scratch dir and [`merge_built`]s the result, so a populated destination
/// never blocks it).
pub fn materialize(
    plan: &str,
    dir: &Path,
    goal: &str,
    title: Option<&str>,
    source: &str,
    filled: Option<&HashMap<usize, String>>,
) -> Result<Materialized> {
    let mut items = parse_plan(plan);
    if items.is_empty() {
        bail!("the plan has no items to materialize");
    }
    // The model ignores the prompt's "keep titles short" guidance, so enforce it
    // here: condense each title before it becomes the header AND the file name.
    for item in &mut items {
        item.title = title::condense(&item.title);
    }
    fs::create_dir_all(dir).with_context(|| format!("cannot create {}", dir.display()))?;

    let root = if is_url(source) {
        None
    } else {
        Some(fs::canonicalize(source).unwrap_or_else(|_| PathBuf::from(source)))
    };
    let names: Vec<String> = items.iter().map(file_name).collect();
    let by_num: HashMap<usize, &String> = items
        .iter()
        .zip(&names)
        .map(|(it, n)| (it.num, n))
        .collect();

    // `title` names the workspace (omitted → the folder name is used); the goal
    // becomes its one-line `description`.
    let mut manifest = String::from("# Generated by `alix generate`.\n");
    if let Some(title) = title {
        manifest.push_str(&format!("title = \"{}\"\n", toml_escape(title)));
    }
    manifest.push_str(&format!(
        "description = \"{}\"\n\n[defaults]\n",
        toml_escape(goal)
    ));
    // The live source root the frozen `% at:` provenance resolves against — the
    // tutor grounds here for context, and drift detection reads it. Cascades into
    // every member deck (`% origin:`), overridable per deck/card.
    if let Some(root) = &root {
        manifest.push_str(&format!(
            "origin = \"{}\"\n",
            toml_escape(&root.display().to_string())
        ));
    }
    fs::write(dir.join(crate::workspace::MANIFEST), manifest)?;

    let mut traces = 0;
    let mut decks = 0;
    let mut filled_count = 0;
    for (item, name) in items.iter().zip(&names) {
        let mut body = String::new();
        match item.kind {
            Kind::Trace => {
                traces += 1;
                body.push_str(&format!("% trace: {}\n", item.title));
            }
            Kind::Deck => {
                decks += 1;
                body.push_str(&format!("% title: {}\n", item.title));
            }
        }
        body.push_str(&format!(
            "% source: {}\n",
            rewrite_scope(&item.source, root.as_deref())
        ));
        for req in &item.requires {
            if let Some(dep) = by_num.get(req) {
                body.push_str(&format!("% requires: {dep}\n"));
            }
        }
        body.push('\n');
        match filled
            .and_then(|f| f.get(&item.num))
            .map(|s| s.trim())
            .filter(|s| !s.is_empty())
        {
            // `--build` filled this item with real checkpoints / cards.
            Some(content) => {
                filled_count += 1;
                body.push_str(content);
                body.push('\n');
            }
            // A stub: header only, to be filled later.
            None => body.push_str(match item.kind {
                Kind::Trace => {
                    "% Stub from `alix generate`. Build the path:  alix generate <this file>\n"
                }
                Kind::Deck => {
                    "% Stub from `alix generate`. Author cards here, or `alix generate` from the source.\n"
                }
            }),
        }
        fs::write(dir.join(name), body)?;
    }

    Ok(Materialized {
        dir: dir.to_path_buf(),
        traces,
        decks,
        filled: filled_count,
    })
}

/// What a merge did: how many files moved in, and the conflicts that
/// kept the user's originals (the new files stay in `staging`).
pub struct MergeReport {
    pub moved: usize,
    pub conflicts: Vec<String>,
}

/// Moves everything materialized in `staging` into `dest` (created if
/// missing), one top-level entry at a time. A name that already exists in
/// `dest` is a conflict: the existing file is kept and the new one stays
/// in `staging` — unless `force`, which replaces it. Directories (assets/)
/// follow the same top-level rule; their contents are not merged inside.
pub fn merge_built(staging: &Path, dest: &Path, force: bool) -> Result<MergeReport> {
    fs::create_dir_all(dest).with_context(|| format!("cannot create {}", dest.display()))?;
    let mut moved = 0;
    let mut conflicts = Vec::new();
    for entry in
        fs::read_dir(staging).with_context(|| format!("cannot read {}", staging.display()))?
    {
        let entry = entry?;
        let name = entry.file_name().to_string_lossy().into_owned();
        let from = entry.path();
        let to = dest.join(&name);
        if to.exists() {
            if !force {
                conflicts.push(name);
                continue;
            }
            if to.is_dir() {
                fs::remove_dir_all(&to)
                    .with_context(|| format!("cannot remove {}", to.display()))?;
            } else {
                fs::remove_file(&to).with_context(|| format!("cannot remove {}", to.display()))?;
            }
        }
        share::move_into(&from, &to)?;
        moved += 1;
    }
    Ok(MergeReport { moved, conflicts })
}

/// Outcome of freezing a workspace: how many decks and files were frozen, plus
/// any *cited* decks that froze nothing. A cited deck that can't be frozen is
/// almost always a broken or stale `% source:` — surfaced here so the caller can
/// warn instead of leaving the user with a silently empty `assets/`.
#[derive(Debug, Default)]
pub struct SnapshotSummary {
    pub decks: usize,
    pub files: usize,
    /// `"<member>: <reason>"` for each cited deck whose excerpts couldn't be read.
    pub failed: Vec<String>,
}

/// Freezes each cited deck's source excerpts into the workspace's `assets/` — the
/// default final step of a workspace `alix generate`, so the workspace is
/// self-contained and its line-number locators never drift. A cited deck is a
/// trace (its checkpoints) or a fact deck whose cards carry `% at:`. A deck with
/// no citations (a plain fact deck, a URL source) is skipped; a deck that DOES
/// cite local excerpts but whose source can't be read is recorded in
/// [`SnapshotSummary::failed`], never silently dropped.
pub fn snapshot_workspace(dir: &Path) -> Result<SnapshotSummary> {
    let mut summary = SnapshotSummary::default();
    let ws = workspace::Workspace::load(dir)?;
    // The workspace-wide origin (`alix.toml [defaults] origin`): a deck whose
    // source root matches it inherits it; one that diverges writes its own.
    let workspace_origin = ws.settings.origin.as_deref().map(PathBuf::from);
    for member in ws.members {
        let Ok(deck) = Deck::load(&member) else {
            continue;
        };
        if !(deck.is_trace() || deck.cards.iter().any(|c| c.at.is_some())) {
            continue;
        }
        // `summary.files` is the running snippet count, passed as the start so each
        // deck's snippets get unique names in the shared `assets/`.
        match trace_ai::snapshot(&deck, summary.files, workspace_origin.as_deref()) {
            Ok(report) => {
                summary.decks += 1;
                summary.files += report.copied.len();
                for missing in &report.missing {
                    eprintln!(
                        "warning: {}: cited file not found, not frozen: {missing}",
                        member.display()
                    );
                }
            }
            // The deck cites local excerpts but none could be frozen — almost
            // always a broken/stale `% source:`. Record it; don't swallow it.
            Err(e) => summary.failed.push(format!("{}: {e:#}", member.display())),
        }
    }
    Ok(summary)
}

/// The member file name for an item: `NN-<slug>.txt`, the zero-padded number
/// (preserving plan order) plus a slug of the title.
fn file_name(item: &Item) -> String {
    format!("{:02}-{}.txt", item.num, slug(&item.title))
}

/// A short kebab slug from a title: up to the first six alphanumeric words.
fn slug(title: &str) -> String {
    let words: Vec<String> = title
        .split(|c: char| !c.is_ascii_alphanumeric())
        .filter(|w| !w.is_empty())
        .take(6)
        .map(str::to_ascii_lowercase)
        .collect();
    if words.is_empty() {
        "item".to_string()
    } else {
        words.join("-")
    }
}

/// Rewrites a plan `% source:` scope to point at the real source: absolute under
/// the source root for a local path (`.` → the root itself), left as-is for a
/// URL or when there is no local root.
///
/// The model sometimes writes the scope relative to the PROJECT ROOT *above* the
/// `--source` (e.g. `crates/x/src/lib.rs` while `root` already IS `…/crates/x`).
/// A plain `root.join(scope)` would double the overlap into a path that doesn't
/// exist (`…/crates/x/crates/x/src/lib.rs`) — which silently broke both freezing
/// and citation reads. So the first ` + ` part is anchored overlap-aware via
/// [`trace::resolve_under_base`] (the write-time twin of how `% at:` locators
/// resolve on read), and any further ` + ` parts stay relative to it.
fn rewrite_scope(scope: &str, root: Option<&Path>) -> String {
    let scope = scope.trim();
    let Some(root) = root else {
        return scope.to_string();
    };
    if is_url(scope) {
        return scope.to_string();
    }
    let (first, rest) = match scope.split_once(" + ") {
        Some((a, b)) => (a.trim(), Some(b.trim())),
        None => (scope, None),
    };
    let anchored = if first == "." {
        root.to_path_buf()
    } else if Path::new(first).is_absolute() {
        PathBuf::from(first)
    } else {
        trace::resolve_under_base(root, first)
    };
    match rest {
        Some(rest) => format!("{} + {}", anchored.display(), rest),
        None => anchored.display().to_string(),
    }
}

/// Escapes a string for a double-quoted TOML value.
fn toml_escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

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

    #[test]
    fn explore_prompt_carries_goal_means_coverage_and_order() {
        let p = explore_prompt(
            ".",
            "understand the alix repo",
            false,
            &TraceConfig::default(),
        );
        assert!(p.contains("understand the alix repo")); // the goal is echoed
        assert!(p.contains("SET OF MEANS")); // decks + traces, not just traces
        assert!(p.contains("[trace]")); // both kinds tagged
        assert!(p.contains("[deck]"));
        assert!(p.contains("SHAPE")); // chosen by edge-vs-node shape
        assert!(p.contains("SATURATION")); // stop rule, not a count
        assert!(p.contains("requires:")); // prerequisite edges
        assert!(p.contains("TOPOLOGICAL order")); // dependency-ordered
        assert!(p.contains("no cards, no checkpoints")); // a plan, not built items
        assert!(p.contains("Read, Glob")); // read-only local exploration
        assert!(!p.contains("WebFetch")); // local source needs no web tool
    }

    #[test]
    fn explore_prompt_url_uses_webfetch() {
        let p = explore_prompt(
            "https://x",
            "understand the page",
            true,
            &TraceConfig::default(),
        );
        assert!(p.contains("WebFetch"));
        assert!(!p.contains("Glob")); // no local file tools for a URL source
    }

    #[test]
    fn fill_prompt_asks_fact_cards_to_cite_their_source() {
        let items = vec![
            Item {
                num: 1,
                kind: Kind::Deck,
                title: "Basics".to_string(),
                requires: Vec::new(),
                source: ".".to_string(),
            },
            Item {
                num: 2,
                kind: Kind::Trace,
                title: "The path".to_string(),
                requires: vec![1],
                source: ".".to_string(),
            },
        ];
        let p = fill_prompt(&items);
        assert!(p.contains("FACT cards"));
        // Fact cards get a `% at:` citation so the card can show its source.
        assert!(p.contains("% at: file:start-end"));
        assert!(p.contains("show its source on reveal"));
        // Every `% at:` path is pinned to one consistent root, so freezing resolves.
        assert!(p.contains("relative to the SAME root"));
        // A compound answer must be split / shaped, not crammed into one prose
        // line (mirrors the `generate` source prompt) — otherwise an explore-built
        // deck shows enumerations as a wall of text.
        assert!(p.contains("enumeration"));
        assert!(p.contains("split it"));
    }

    #[test]
    fn walk_prompt_predicts_shape_with_evidence() {
        let p = walk_prompt(".", "understand the repo", false, &TraceConfig::default());
        assert!(p.contains("EXPLORE walk")); // a predict-verify walk, not a plan dump
        assert!(p.contains("PREDICT its")); // the learner predicts the shape
        assert!(p.contains("DOMAIN NOUNS")); // nouns hop
        assert!(p.contains("SPINE")); // spine hop
        assert!(p.contains("CITE REAL STRUCTURAL EVIDENCE")); // grounded reveals
        assert!(p.contains("candidate traces")); // last hop lands on the menu
        assert!(p.contains("% at:")); // standard trace locator format
        assert!(p.contains("Read, Glob")); // read-only local exploration
        assert!(!p.contains("WebFetch"));
    }

    const SAMPLE_PLAN: &str = "\
Goal    understand X
Source  a thing
Spine   a -> b

1. [deck]  The deck format: markers and directives
   requires: none
   % source: README.md
2. [trace] How text becomes Cards
   requires: 1
   % source: src/parser.rs
10. [trace] How a request is served
    requires: 1, 2
    % source: src/serve.rs
";

    #[test]
    fn parse_plan_extracts_items_kinds_requires_and_source() {
        let items = parse_plan(SAMPLE_PLAN);
        assert_eq!(3, items.len()); // header lines are skipped
        assert_eq!(Kind::Deck, items[0].kind);
        assert_eq!(1, items[0].num);
        assert!(items[0].title.starts_with("The deck format"));
        assert!(items[0].requires.is_empty()); // "none" → no deps
        assert_eq!("README.md", items[0].source);
        assert_eq!(Kind::Trace, items[1].kind);
        assert_eq!(vec![1], items[1].requires);
        assert_eq!(10, items[2].num); // two-digit number parsed
        assert_eq!(vec![1, 2], items[2].requires);
    }

    #[test]
    fn materialize_writes_manifest_and_wired_stubs() {
        let dir = std::env::temp_dir().join(format!("alix-explore-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);

        let report =
            materialize(SAMPLE_PLAN, &dir, "understand the repo", None, ".", None).unwrap();
        assert_eq!(2, report.traces);
        assert_eq!(1, report.decks);
        assert_eq!(0, report.filled); // no fill map → all stubs

        let manifest = fs::read_to_string(dir.join("alix.toml")).unwrap();
        // The goal becomes the description; with no --title, the folder names it.
        assert!(manifest.contains("description = \"understand the repo\""));
        assert!(!manifest.contains("title ="));
        assert!(manifest.contains("[defaults]"));

        let deck = fs::read_to_string(dir.join("01-the-deck-format.txt")).unwrap();
        assert!(deck.contains("% title: The Deck Format")); // condensed + title-cased
        assert!(deck.contains("% source: ")); // rewritten absolute

        let trace = fs::read_to_string(dir.join("02-how-text-becomes-cards.txt")).unwrap();
        assert!(trace.contains("% trace: How Text Becomes Cards"));
        // requires 1 → the first item's file name
        assert!(trace.contains("% requires: 01-the-deck-format.txt"));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn materialize_writes_into_a_pre_existing_populated_dir_without_refusing() {
        // materialize itself takes no view on a populated `dir` — callers that
        // write straight into a real destination (none in production anymore;
        // `alix generate` stages fresh and merges) would decide that, but
        // materialize just writes alongside whatever's already there.
        let dir = std::env::temp_dir().join(format!("alix-explore-ne-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        fs::write(dir.join("keep.txt"), "existing").unwrap();

        materialize(SAMPLE_PLAN, &dir, "g", None, ".", None).unwrap();

        assert_eq!(
            "existing",
            fs::read_to_string(dir.join("keep.txt")).unwrap()
        );
        assert!(dir.join("alix.toml").exists());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn materialize_writes_title_and_description() {
        let dir = std::env::temp_dir().join(format!("alix-explore-title-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);

        materialize(
            SAMPLE_PLAN,
            &dir,
            "the goal",
            Some("Repo Internals"),
            ".",
            None,
        )
        .unwrap();
        let manifest = fs::read_to_string(dir.join("alix.toml")).unwrap();
        assert!(manifest.contains("title = \"Repo Internals\""));
        assert!(manifest.contains("description = \"the goal\""));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn parse_filled_splits_on_item_delimiters() {
        let raw = "\
preamble ignored
=== item 1 ===
# front a
\tback a
=== item 2 ===
# question
\tkey point
\t% at: src/x.rs:1-3
=== item 3 ===
";
        let filled = parse_filled(raw);
        assert_eq!(2, filled.len()); // item 3 has no body → omitted
        assert!(filled[&1].contains("# front a"));
        assert!(filled[&2].contains("% at: src/x.rs:1-3"));
        assert!(!filled.contains_key(&3));
    }

    #[test]
    fn materialize_writes_filled_content_when_given() {
        let dir = std::env::temp_dir().join(format!("alix-explore-fill-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        let mut filled = HashMap::new();
        filled.insert(
            2usize,
            "# how does text become Cards?\n\tparse_str runs a state machine\n\t% at: src/parser.rs:1-9"
                .to_string(),
        );

        let report = materialize(SAMPLE_PLAN, &dir, "g", None, ".", Some(&filled)).unwrap();
        assert_eq!(1, report.filled); // only item 2 was filled

        // item 2 (a trace) keeps its header AND carries the filled checkpoint
        let trace = fs::read_to_string(dir.join("02-how-text-becomes-cards.txt")).unwrap();
        assert!(trace.contains("% trace: How Text Becomes Cards"));
        assert!(trace.contains("# how does text become Cards?"));
        assert!(trace.contains("% at: src/parser.rs:1-9"));
        // item 1 had no fill → still a stub
        let deck = fs::read_to_string(dir.join("01-the-deck-format.txt")).unwrap();
        assert!(deck.contains("% Stub from `alix generate`"));

        let _ = fs::remove_dir_all(&dir);
    }

    /// Fresh staging + dest dirs under a per-test scratch root; both wiped on
    /// entry (and left for the caller to clean up).
    fn merge_test_dirs(label: &str) -> (PathBuf, PathBuf) {
        let root = std::env::temp_dir().join(format!("alix-merge-{label}-{}", std::process::id()));
        let staging = root.join("staging");
        let dest = root.join("dest");
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(&staging).unwrap();
        (staging, dest)
    }

    #[test]
    fn a_clean_merge_moves_every_entry_and_reports_zero_conflicts() {
        let (staging, dest) = merge_test_dirs("clean");
        fs::write(staging.join("alix.toml"), "[defaults]\n").unwrap();
        fs::write(staging.join("01-a.txt"), "deck a\n").unwrap();

        let report = merge_built(&staging, &dest, false).unwrap();

        assert_eq!(2, report.moved);
        assert!(report.conflicts.is_empty());
        assert_eq!(
            "[defaults]\n",
            fs::read_to_string(dest.join("alix.toml")).unwrap()
        );
        assert_eq!(
            "deck a\n",
            fs::read_to_string(dest.join("01-a.txt")).unwrap()
        );
        // staging is drained — every entry landed in dest
        assert_eq!(0, fs::read_dir(&staging).unwrap().count());

        let _ = fs::remove_dir_all(staging.parent().unwrap());
    }

    #[test]
    fn a_name_collision_keeps_the_original_content_and_leaves_the_new_file_in_staging() {
        let (staging, dest) = merge_test_dirs("collide");
        fs::create_dir_all(&dest).unwrap();
        fs::write(dest.join("01-a.txt"), "the user's own deck\n").unwrap();
        fs::write(staging.join("01-a.txt"), "the freshly generated deck\n").unwrap();

        let report = merge_built(&staging, &dest, false).unwrap();

        assert_eq!(0, report.moved);
        assert_eq!(vec!["01-a.txt".to_string()], report.conflicts);
        // the original is untouched...
        assert_eq!(
            "the user's own deck\n",
            fs::read_to_string(dest.join("01-a.txt")).unwrap()
        );
        // ...and the new version is still sitting in staging, not lost
        assert_eq!(
            "the freshly generated deck\n",
            fs::read_to_string(staging.join("01-a.txt")).unwrap()
        );

        let _ = fs::remove_dir_all(staging.parent().unwrap());
    }

    #[test]
    fn force_replaces_a_colliding_file_with_the_new_version() {
        let (staging, dest) = merge_test_dirs("force");
        fs::create_dir_all(&dest).unwrap();
        fs::write(dest.join("01-a.txt"), "stale\n").unwrap();
        fs::write(staging.join("01-a.txt"), "fresh\n").unwrap();

        let report = merge_built(&staging, &dest, true).unwrap();

        assert_eq!(1, report.moved);
        assert!(report.conflicts.is_empty());
        assert_eq!(
            "fresh\n",
            fs::read_to_string(dest.join("01-a.txt")).unwrap()
        );

        let _ = fs::remove_dir_all(staging.parent().unwrap());
    }

    #[test]
    fn a_directory_entry_merges_as_one_unit_under_the_same_collision_rule() {
        let (staging, dest) = merge_test_dirs("dir");
        fs::create_dir_all(staging.join("assets")).unwrap();
        fs::write(staging.join("assets/img.svg"), "new\n").unwrap();

        // clean merge: the whole `assets/` dir moves in as one entry
        let report = merge_built(&staging, &dest, false).unwrap();
        assert_eq!(1, report.moved);
        assert!(report.conflicts.is_empty());
        assert_eq!(
            "new\n",
            fs::read_to_string(dest.join("assets/img.svg")).unwrap()
        );

        // a second build's `assets/` collides with the whole dir, not its contents
        fs::create_dir_all(staging.join("assets")).unwrap();
        fs::write(staging.join("assets/img.svg"), "newer\n").unwrap();
        let report = merge_built(&staging, &dest, false).unwrap();
        assert_eq!(0, report.moved);
        assert_eq!(vec!["assets".to_string()], report.conflicts);
        assert_eq!(
            "new\n", // the original dir's content survives untouched
            fs::read_to_string(dest.join("assets/img.svg")).unwrap()
        );

        let _ = fs::remove_dir_all(staging.parent().unwrap());
    }

    #[test]
    fn snapshot_workspace_freezes_traces_and_cited_facts() {
        let dir = std::env::temp_dir().join(format!("alix-snap-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("src")).unwrap();
        fs::write(dir.join("src/a.rs"), "x\ny\nz\n").unwrap();
        fs::write(dir.join("alix.toml"), "[defaults]\n").unwrap();
        let src = dir.join("src");
        // a built trace citing src/a.rs (source = the dir)
        fs::write(
            dir.join("01-t.txt"),
            format!(
                "% trace: t\n% source: {}\n# h\n\tp\n\t% at: a.rs:1-2\n",
                src.display()
            ),
        )
        .unwrap();
        // a fact deck citing the same source with `% at:` — also frozen
        fs::write(
            dir.join("02-d.txt"),
            format!("% source: {}\n# q\n\ta\n\t% at: a.rs:3\n", src.display()),
        )
        .unwrap();
        // a fact deck with no citations — skipped
        fs::write(dir.join("03-plain.txt"), "% title: d\n# q\n\ta\n").unwrap();

        let summary = snapshot_workspace(&dir).unwrap();
        assert_eq!((2, 2), (summary.decks, summary.files)); // trace + cited fact, one snippet each
        assert!(summary.failed.is_empty(), "{:?}", summary.failed);
        // Two unique snippet names in the shared assets/ (the offset prevents a
        // collision), and the whole file is never copied.
        assert!(dir.join("assets/01.rs").is_file());
        assert!(dir.join("assets/02.rs").is_file());
        assert!(!dir.join("assets/a.rs").exists());
        // the fact deck was repointed off its live locator onto a frozen snippet
        let fact = fs::read_to_string(dir.join("02-d.txt")).unwrap();
        assert!(fact.contains("% source: assets\n"), "{fact}");
        assert!(!fact.contains("% at: a.rs:3"), "{fact}");
        assert!(fact.contains("% at: 0"), "{fact}");

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn snapshot_workspace_surfaces_a_deck_whose_source_cannot_be_frozen() {
        // A cited deck whose `% source:` points nowhere must be REPORTED, not
        // silently swallowed (the bug that left a freshly built workspace with no
        // `assets/` and no warning).
        let dir = std::env::temp_dir().join(format!("alix-snap-fail-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        fs::write(dir.join("alix.toml"), "[defaults]\n").unwrap();
        fs::write(
            dir.join("01-broken.txt"),
            format!(
                "% source: {}/does-not-exist\n# q\n\ta\n\t% at: src/x.rs:1\n",
                dir.display()
            ),
        )
        .unwrap();

        let summary = snapshot_workspace(&dir).unwrap();
        assert_eq!(0, summary.decks); // nothing frozen
        assert_eq!(1, summary.failed.len(), "{:?}", summary.failed);
        assert!(
            summary.failed[0].contains("01-broken.txt"),
            "{:?}",
            summary.failed
        );
        assert!(!dir.join("assets").exists()); // no empty assets dir left behind

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn rewrite_scope_anchors_a_repo_relative_scope_without_doubling() {
        // The `--source` is a subdir (a crate), but the model writes its source
        // scope relative to the project root *above* it. The written `% source:`
        // must be the real file, never `…/mycrate/crates/mycrate/…` (the doubling
        // that silently broke freezing and citation reads).
        let root = std::env::temp_dir().join(format!("alix-scope-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        let crate_src = root.join("crates/mycrate/src");
        fs::create_dir_all(&crate_src).unwrap();
        fs::write(crate_src.join("lib.rs"), "fn main() {}\n").unwrap();
        let lib = crate_src.join("lib.rs").display().to_string();
        let source = root.join("crates/mycrate"); // the `--source`

        // repo-root-relative scope → resolved overlap-aware to the real file
        assert_eq!(
            lib,
            rewrite_scope("crates/mycrate/src/lib.rs", Some(&source))
        );
        // a clean crate-relative scope still resolves directly
        assert_eq!(lib, rewrite_scope("src/lib.rs", Some(&source)));
        // `.` → the source root itself; an absolute path is left untouched
        assert_eq!(
            source.display().to_string(),
            rewrite_scope(".", Some(&source))
        );
        assert_eq!(lib, rewrite_scope(&lib, Some(&source)));
        // multi-source: only the first ` + ` part is re-anchored, the rest stays
        assert_eq!(
            format!("{lib} + other.rs"),
            rewrite_scope("crates/mycrate/src/lib.rs + other.rs", Some(&source))
        );

        let _ = fs::remove_dir_all(&root);
    }
}